org.el 616 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777
  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.48
  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.48
  63. ;; - Agenda views can be made in batch mode from the command line.
  64. ;; - `org-store-link' does the right thing in dired-mode.
  65. ;; - File links can contain environment variables (thanks to Ed Hirgelt).
  66. ;; - Full Emacs 21 compatibility has been restored.
  67. ;; - Bug fixes.
  68. ;;
  69. ;; Version 4.47
  70. ;; - Custom commands may produce an agenda which contains several blocks,
  71. ;; each block created by a different agenda command.
  72. ;; - Agenda commands can be restricted to the current file, region, subtree.
  73. ;; - The timeline command must now be called through the agenda
  74. ;; dispatcher (C-c a L). `C-c C-r' no longer works.
  75. ;; - Agenda items can be sorted by tag. The *last* tag is used for this.
  76. ;; - The prefix and the sorting strategy for agenda items can depend
  77. ;; upon the agenda type.
  78. ;; - The handling of `mailto:' links can be customized, see the new
  79. ;; variable `org-link-mailto-program'.
  80. ;; - `mailto' links can specify a subject after a double colon,
  81. ;; like [[mailto:carsten@orgmode.org::Org-mode is buggy]].
  82. ;; - In the #+STARTUP line, M-TAB completes valid keywords.
  83. ;; - In the #+TAGS: line, M-TAB after ":" inserts all currently used tags.
  84. ;; - Again full Emacs 21 support: Checkboxes and publishing are fixed.
  85. ;; - More minor bug fixes.
  86. ;;
  87. ;; Version 4.45
  88. ;; - Checkbox lists can show statistics about checked items.
  89. ;; - C-TAB will cycle the visibility of archived subtrees.
  90. ;;; - Documentation about checkboxes has been moved to chapter 5.
  91. ;; - Bux fixes.
  92. ;;
  93. ;; Version 4.44
  94. ;; - Clock table can be done for a limited time interval.
  95. ;; - Obsolete support for the old outline mode has been removed.
  96. ;; - Bug fixes and code cleaning.
  97. ;;
  98. ;; Version 4.43
  99. ;; - Bug fixes
  100. ;; - `s' key in the agenda saves all org-mode buffers.
  101. ;;
  102. ;; Version 4.41
  103. ;; - Shift-curser keys can modify inactive time stamps (inactive time
  104. ;; stamps are the ones in [...] brackets.
  105. ;; - Toggle all checkboxes in a region/below a headline.
  106. ;; - Bug fixes.
  107. ;;
  108. ;;; Code:
  109. (eval-when-compile
  110. (require 'cl)
  111. (require 'calendar))
  112. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  113. ;; the file noutline.el being loaded.
  114. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  115. ;; We require noutline, which might be provided in outline.el
  116. (require 'outline) (require 'noutline)
  117. ;; Other stuff we need.
  118. (require 'time-date)
  119. (require 'easymenu)
  120. ;;; Customization variables
  121. (defvar org-version "4.47"
  122. "The version number of the file org.el.")
  123. (defun org-version ()
  124. (interactive)
  125. (message "Org-mode version %s" org-version))
  126. ;; Compatibility constants
  127. (defconst org-xemacs-p (featurep 'xemacs)) ; not used by org.el itself
  128. (defconst org-format-transports-properties-p
  129. (let ((x "a"))
  130. (add-text-properties 0 1 '(test t) x)
  131. (get-text-property 0 'test (format "%s" x)))
  132. "Does format transport text properties?")
  133. (defgroup org nil
  134. "Outline-based notes management and organizer."
  135. :tag "Org"
  136. :group 'outlines
  137. :group 'hypermedia
  138. :group 'calendar)
  139. (defgroup org-startup nil
  140. "Options concerning startup of Org-mode."
  141. :tag "Org Startup"
  142. :group 'org)
  143. (defcustom org-startup-folded t
  144. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  145. This can also be configured on a per-file basis by adding one of
  146. the following lines anywhere in the buffer:
  147. #+STARTUP: fold
  148. #+STARTUP: nofold
  149. #+STARTUP: content"
  150. :group 'org-startup
  151. :type '(choice
  152. (const :tag "nofold: show all" nil)
  153. (const :tag "fold: overview" t)
  154. (const :tag "content: all headlines" content)))
  155. (defcustom org-startup-truncated t
  156. "Non-nil means, entering Org-mode will set `truncate-lines'.
  157. This is useful since some lines containing links can be very long and
  158. uninteresting. Also tables look terrible when wrapped."
  159. :group 'org-startup
  160. :type 'boolean)
  161. (defcustom org-startup-align-all-tables nil
  162. "Non-nil means, align all tables when visiting a file.
  163. This is useful when the column width in tables is forced with <N> cookies
  164. in table fields. Such tables will look correct only after the first re-align.
  165. This can also be configured on a per-file basis by adding one of
  166. the following lines anywhere in the buffer:
  167. #+STARTUP: align
  168. #+STARTUP: noalign"
  169. :group 'org-startup
  170. :type 'boolean)
  171. (defcustom org-startup-with-deadline-check nil
  172. "Non-nil means, entering Org-mode will run the deadline check.
  173. This means, if you start editing an org file, you will get an
  174. immediate reminder of any due deadlines.
  175. This can also be configured on a per-file basis by adding one of
  176. the following lines anywhere in the buffer:
  177. #+STARTUP: dlcheck
  178. #+STARTUP: nodlcheck"
  179. :group 'org-startup
  180. :type 'boolean)
  181. (defcustom org-insert-mode-line-in-empty-file nil
  182. "Non-nil means insert the first line setting Org-mode in empty files.
  183. When the function `org-mode' is called interactively in an empty file, this
  184. normally means that the file name does not automatically trigger Org-mode.
  185. To ensure that the file will always be in Org-mode in the future, a
  186. line enforcing Org-mode will be inserted into the buffer, if this option
  187. has been set."
  188. :group 'org-startup
  189. :type 'boolean)
  190. (defcustom org-CUA-compatible nil
  191. "Non-nil means use alternative key bindings for S-<cursor movement>.
  192. Org-mode used S-<cursor movement> for changing timestamps and priorities.
  193. S-<cursor movement> is also used for example by `CUA-mode' to select text.
  194. If you want to use Org-mode together with `CUA-mode', Org-mode needs to use
  195. alternative bindings. Setting this variable to t will replace the following
  196. keys both in Org-mode and in the Org-agenda buffer.
  197. S-RET -> C-S-RET
  198. S-up -> M-p
  199. S-down -> M-n
  200. S-left -> M--
  201. S-right -> M-+
  202. If you do not like the alternative keys, take a look at the variable
  203. `org-disputed-keys'.
  204. This option is only relevant at load-time of Org-mode. Changing it requires
  205. a restart of Emacs to become effective."
  206. :group 'org-startup
  207. :type 'boolean)
  208. (defvar org-disputed-keys
  209. '((S-up [(shift up)] [(meta ?p)])
  210. (S-down [(shift down)] [(meta ?n)])
  211. (S-left [(shift left)] [(meta ?-)])
  212. (S-right [(shift right)] [(meta ?+)])
  213. (S-return [(shift return)] [(control shift return)]))
  214. "Keys for which Org-mode and other modes compete.
  215. This is an alist, cars are symbols for lookup, 1st element is the default key,
  216. second element will be used when `org-CUA-compatible' is t.")
  217. (defun org-key (key)
  218. "Select a key according to `org-CUA-compatible'."
  219. (nth (if org-CUA-compatible 2 1)
  220. (or (assq key org-disputed-keys)
  221. (error "Invalid Key %s in `org-key'" key))))
  222. (defcustom org-ellipsis nil
  223. "The ellipsis to use in the Org-mode outline.
  224. When nil, just use the standard three dots. When a string, use that instead,
  225. and just in Org-mode (which will then use its own display table).
  226. Changing this requires executing `M-x org-mode' in a buffer to become
  227. effective."
  228. :group 'org-startup
  229. :type '(choice (const :tag "Default" nil)
  230. (string :tag "String" :value "...#")))
  231. (defvar org-display-table nil
  232. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  233. (defgroup org-keywords nil
  234. "Keywords in Org-mode."
  235. :tag "Org Keywords"
  236. :group 'org)
  237. (defcustom org-deadline-string "DEADLINE:"
  238. "String to mark deadline entries.
  239. A deadline is this string, followed by a time stamp. Should be a word,
  240. terminated by a colon. You can insert a schedule keyword and
  241. a timestamp with \\[org-deadline].
  242. Changes become only effective after restarting Emacs."
  243. :group 'org-keywords
  244. :type 'string)
  245. (defcustom org-scheduled-string "SCHEDULED:"
  246. "String to mark scheduled TODO entries.
  247. A schedule is this string, followed by a time stamp. Should be a word,
  248. terminated by a colon. You can insert a schedule keyword and
  249. a timestamp with \\[org-schedule].
  250. Changes become only effective after restarting Emacs."
  251. :group 'org-keywords
  252. :type 'string)
  253. (defcustom org-closed-string "CLOSED:"
  254. "String used as the prefix for timestamps logging closing a TODO entry."
  255. :group 'org-keywords
  256. :type 'string)
  257. (defcustom org-clock-string "CLOCK:"
  258. "String used as prefix for timestamps clocking work hours on an item."
  259. :group 'org-keywords
  260. :type 'string)
  261. (defcustom org-comment-string "COMMENT"
  262. "Entries starting with this keyword will never be exported.
  263. An entry can be toggled between COMMENT and normal with
  264. \\[org-toggle-comment].
  265. Changes become only effective after restarting Emacs."
  266. :group 'org-keywords
  267. :type 'string)
  268. (defcustom org-quote-string "QUOTE"
  269. "Entries starting with this keyword will be exported in fixed-width font.
  270. Quoting applies only to the text in the entry following the headline, and does
  271. not extend beyond the next headline, even if that is lower level.
  272. An entry can be toggled between QUOTE and normal with
  273. \\[org-toggle-fixed-width-section]."
  274. :group 'org-keywords
  275. :type 'string)
  276. (defgroup org-structure nil
  277. "Options concerning the general structure of Org-mode files."
  278. :tag "Org Structure"
  279. :group 'org)
  280. (defgroup org-cycle nil
  281. "Options concerning visibility cycling in Org-mode."
  282. :tag "Org Cycle"
  283. :group 'org-structure)
  284. (defcustom org-cycle-global-at-bob t
  285. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  286. This makes it possible to do global cycling without having to use S-TAB or
  287. C-u TAB. For this special case to work, the first line of the buffer
  288. must not be a headline - it may be empty ot some other text. When used in
  289. this way, `org-cycle-hook' is disables temporarily, to make sure the
  290. cursor stays at the beginning of the buffer.
  291. When this option is nil, don't do anything special at the beginning
  292. of the buffer."
  293. :group 'org-cycle
  294. :type 'boolean)
  295. (defcustom org-cycle-emulate-tab t
  296. "Where should `org-cycle' emulate TAB.
  297. nil Never
  298. white Only in completely white lines
  299. whitestart Only at the beginning of lines, before the first non-white char.
  300. t Everywhere except in headlines
  301. If TAB is used in a place where it does not emulate TAB, the current subtree
  302. visibility is cycled."
  303. :group 'org-cycle
  304. :type '(choice (const :tag "Never" nil)
  305. (const :tag "Only in completely white lines" white)
  306. (const :tag "Before first char in a line" whitestart)
  307. (const :tag "Everywhere except in headlines" t)
  308. ))
  309. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  310. org-optimize-window-after-visibility-change)
  311. "Hook that is run after `org-cycle' has changed the buffer visibility.
  312. The function(s) in this hook must accept a single argument which indicates
  313. the new state that was set by the most recent `org-cycle' command. The
  314. argument is a symbol. After a global state change, it can have the values
  315. `overview', `content', or `all'. After a local state change, it can have
  316. the values `folded', `children', or `subtree'."
  317. :group 'org-cycle
  318. :type 'hook)
  319. (defgroup org-edit-structure nil
  320. "Options concerning structure editing in Org-mode."
  321. :tag "Org Edit Structure"
  322. :group 'org-structure)
  323. (defcustom org-odd-levels-only nil
  324. "Non-nil means, skip even levels and only use odd levels for the outline.
  325. This has the effect that two stars are being added/taken away in
  326. promotion/demotion commands. It also influences how levels are
  327. handled by the exporters.
  328. Changing it requires restart of `font-lock-mode' to become effective
  329. for fontification also in regions already fontified.
  330. You may also set this on a per-file basis by adding one of the following
  331. lines to the buffer:
  332. #+STARTUP: odd
  333. #+STARTUP: oddeven"
  334. :group 'org-edit-structure
  335. :group 'org-font-lock
  336. :type 'boolean)
  337. (defcustom org-adapt-indentation t
  338. "Non-nil means, adapt indentation when promoting and demoting.
  339. When this is set and the *entire* text in an entry is indented, the
  340. indentation is increased by one space in a demotion command, and
  341. decreased by one in a promotion command. If any line in the entry
  342. body starts at column 0, indentation is not changed at all."
  343. :group 'org-edit-structure
  344. :type 'boolean)
  345. (defcustom org-insert-heading-hook nil
  346. "Hook being run after inserting a new heading."
  347. :group 'org-edit-structure
  348. :type 'boolean)
  349. (defcustom org-enable-fixed-width-editor t
  350. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  351. This currently only means, they are never auto-wrapped.
  352. When nil, such lines will be treated like ordinary lines.
  353. See also the QUOTE keyword."
  354. :group 'org-edit-structure
  355. :type 'boolean)
  356. (defgroup org-sparse-trees nil
  357. "Options concerning sparse trees in Org-mode."
  358. :tag "Org Sparse Trees"
  359. :group 'org-structure)
  360. (defcustom org-highlight-sparse-tree-matches t
  361. "Non-nil means, highlight all matches that define a sparse tree.
  362. The highlights will automatically disappear the next time the buffer is
  363. changed by an edit command."
  364. :group 'org-sparse-trees
  365. :type 'boolean)
  366. (defcustom org-show-hierarchy-above t
  367. "Non-nil means, show full hierarchy when showing a spot in the tree.
  368. Turning this off makes sparse trees more compact, but also less clear."
  369. :group 'org-sparse-trees
  370. :type 'boolean)
  371. (defcustom org-show-following-heading t
  372. "Non-nil means, show heading following match in `org-occur'.
  373. When doing an `org-occur' it is useful to show the headline which
  374. follows the match, even if they do not match the regexp. This makes it
  375. easier to edit directly inside the sparse tree. However, if you use
  376. `org-occur' mainly as an overview, the following headlines are
  377. unnecessary clutter."
  378. :group 'org-sparse-trees
  379. :type 'boolean)
  380. (defcustom org-occur-hook '(org-first-headline-recenter)
  381. "Hook that is run after `org-occur' has constructed a sparse tree.
  382. This can be used to recenter the window to show as much of the structure
  383. as possible."
  384. :group 'org-sparse-trees
  385. :type 'hook)
  386. (defgroup org-plain-lists nil
  387. "Options concerning plain lists in Org-mode."
  388. :tag "Org Plain lists"
  389. :group 'org-structure)
  390. (defcustom org-cycle-include-plain-lists t
  391. "Non-nil means, include plain lists into visibility cycling.
  392. This means that during cycling, plain list items will *temporarily* be
  393. interpreted as outline headlines with a level given by 1000+i where i is the
  394. indentation of the bullet. In all other operations, plain list items are
  395. not seen as headlines. For example, you cannot assign a TODO keyword to
  396. such an item."
  397. :group 'org-plain-lists
  398. :type 'boolean)
  399. (defcustom org-plain-list-ordered-item-terminator t
  400. "The character that makes a line with leading number an ordered list item.
  401. Valid values are ?. and ?\). To get both terminators, use t. While
  402. ?. may look nicer, it creates the danger that a line with leading
  403. number may be incorrectly interpreted as an item. ?\) therefore is
  404. the safe choice."
  405. :group 'org-plain-lists
  406. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  407. (const :tag "paren like in \"2)\"" ?\))
  408. (const :tab "both" t)))
  409. (defcustom org-auto-renumber-ordered-lists t
  410. "Non-nil means, automatically renumber ordered plain lists.
  411. Renumbering happens when the sequence have been changed with
  412. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  413. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  414. :group 'org-plain-lists
  415. :type 'boolean)
  416. (defcustom org-provide-checkbox-statistics t
  417. "Non-nil means, update checkbox statistics after insert and toggle.
  418. When this is set, checkbox statistics is updated each time you either insert
  419. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  420. with \\[org-ctrl-c-ctrl-c\\]."
  421. :group 'org
  422. :type 'boolean)
  423. (defgroup org-archive nil
  424. "Options concerning archiving in Org-mode."
  425. :tag "Org Archive"
  426. :group 'org-structure)
  427. (defcustom org-archive-tag "ARCHIVE"
  428. "The tag that marks a subtree as archived.
  429. An archived subtree does not open during visibility cycling, and does
  430. not contribute to the agenda listings."
  431. :group 'org-archive
  432. :group 'org-keywords
  433. :type 'string)
  434. (defcustom org-agenda-skip-archived-trees t
  435. "Non-nil means, the agenda will skip any items located in archived trees.
  436. An archived tree is a tree marked with the tag ARCHIVE."
  437. :group 'org-archive
  438. :group 'org-agenda-display
  439. :type 'boolean)
  440. (defcustom org-cycle-open-archived-trees nil
  441. "Non-nil means, `org-cycle' will open archived trees.
  442. An archived tree is a tree marked with the tag ARCHIVE.
  443. When nil, archived trees will stay folded. You can still open them with
  444. normal outline commands like `show-all', but not with the cycling commands."
  445. :group 'org-archive
  446. :group 'org-cycle
  447. :type 'boolean)
  448. (defcustom org-sparse-tree-open-archived-trees nil
  449. "Non-nil means sparse tree construction shows matches in archived trees.
  450. When nil, matches in these trees are highlighted, but the trees are kept in
  451. collapsed state."
  452. :group 'org-archive
  453. :group 'org-sparse-trees
  454. :type 'boolean)
  455. (defcustom org-archive-location "%s_archive::"
  456. "The location where subtrees should be archived.
  457. This string consists of two parts, separated by a double-colon.
  458. The first part is a file name - when omitted, archiving happens in the same
  459. file. %s will be replaced by the current file name (without directory part).
  460. Archiving to a different file is useful to keep archived entries from
  461. contributing to the Org-mode Agenda.
  462. The part after the double colon is a headline. The archived entries will be
  463. filed under that headline. When omitted, the subtrees are simply filed away
  464. at the end of the file, as top-level entries.
  465. Here are a few examples:
  466. \"%s_archive::\"
  467. If the current file is Projects.org, archive in file
  468. Projects.org_archive, as top-level trees. This is the default.
  469. \"::* Archived Tasks\"
  470. Archive in the current file, under the top-level headline
  471. \"* Archived Tasks\".
  472. \"~/org/archive.org::\"
  473. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  474. \"basement::** Finished Tasks\"
  475. Archive in file ./basement (relative path), as level 3 trees
  476. below the level 2 heading \"** Finished Tasks\".
  477. You may set this option on a per-file basis by adding to the buffer a
  478. line like
  479. #+ARCHIVE: basement::** Finished Tasks"
  480. :group 'org-archive
  481. :type 'string)
  482. (defcustom org-archive-mark-done t
  483. "Non-nil means, mark entries as DONE when they are moved to the archive file."
  484. :group 'org-archive
  485. :type 'boolean)
  486. (defcustom org-archive-stamp-time t
  487. "Non-nil means, add a time stamp to entries moved to an archive file.
  488. The time stamp will be added directly after the TODO state keyword in the
  489. first line, so it is probably best to use this in combinations with
  490. `org-archive-mark-done'."
  491. :group 'org-archive
  492. :type 'boolean)
  493. (defgroup org-table nil
  494. "Options concerning tables in Org-mode."
  495. :tag "Org Table"
  496. :group 'org)
  497. (defcustom org-enable-table-editor 'optimized
  498. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  499. When nil, such lines will be treated like ordinary lines.
  500. When equal to the symbol `optimized', the table editor will be optimized to
  501. do the following:
  502. - Use automatic overwrite mode in front of whitespace in table fields.
  503. This make the structure of the table stay in tact as long as the edited
  504. field does not exceed the column width.
  505. - Minimize the number of realigns. Normally, the table is aligned each time
  506. TAB or RET are pressed to move to another field. With optimization this
  507. happens only if changes to a field might have changed the column width.
  508. Optimization requires replacing the functions `self-insert-command',
  509. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  510. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  511. very good at guessing when a re-align will be necessary, but you can always
  512. force one with \\[org-ctrl-c-ctrl-c].
  513. If you would like to use the optimized version in Org-mode, but the
  514. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  515. This variable can be used to turn on and off the table editor during a session,
  516. but in order to toggle optimization, a restart is required.
  517. See also the variable `org-table-auto-blank-field'."
  518. :group 'org-table
  519. :type '(choice
  520. (const :tag "off" nil)
  521. (const :tag "on" t)
  522. (const :tag "on, optimized" optimized)))
  523. (defcustom orgtbl-optimized (eq org-enable-table-editor 'optimized)
  524. "Non-nil means, use the optimized table editor version for `orgtbl-mode'.
  525. In the optimized version, the table editor takes over all simple keys that
  526. normally just insert a character. In tables, the characters are inserted
  527. in a way to minimize disturbing the table structure (i.e. in overwrite mode
  528. for empty fields). Outside tables, the correct binding of the keys is
  529. restored.
  530. The default for this option is t if the optimized version is also used in
  531. Org-mode. See the variable `org-enable-table-editor' for details. Changing
  532. this variable requires a restart of Emacs to become effective."
  533. :group 'org-table
  534. :type 'boolean)
  535. (defgroup org-table-settings nil
  536. "Settings for tables in Org-mode."
  537. :tag "Org Table Settings"
  538. :group 'org-table)
  539. (defcustom org-table-default-size "5x2"
  540. "The default size for newly created tables, Columns x Rows."
  541. :group 'org-table-settings
  542. :type 'string)
  543. (defcustom org-table-number-regexp "^[<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*$"
  544. "Regular expression for recognizing numbers in table columns.
  545. If a table column contains mostly numbers, it will be aligned to the
  546. right. If not, it will be aligned to the left.
  547. The default value of this option is a regular expression which allows
  548. anything which looks remotely like a number as used in scientific
  549. context. For example, all of the following will be considered a
  550. number:
  551. 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
  552. Other options offered by the customize interface are more restrictive."
  553. :group 'org-table-settings
  554. :type '(choice
  555. (const :tag "Positive Integers"
  556. "^[0-9]+$")
  557. (const :tag "Integers"
  558. "^[-+]?[0-9]+$")
  559. (const :tag "Floating Point Numbers"
  560. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
  561. (const :tag "Floating Point Number or Integer"
  562. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
  563. (const :tag "Exponential, Floating point, Integer"
  564. "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
  565. (const :tag "Very General Number-Like"
  566. "^[<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*$")
  567. (string :tag "Regexp:")))
  568. (defcustom org-table-number-fraction 0.5
  569. "Fraction of numbers in a column required to make the column align right.
  570. In a column all non-white fields are considered. If at least this
  571. fraction of fields is matched by `org-table-number-fraction',
  572. alignment to the right border applies."
  573. :group 'org-table-settings
  574. :type 'number)
  575. (defgroup org-table-editing nil
  576. "Bahavior of tables during editing in Org-mode."
  577. :tag "Org Table Editing"
  578. :group 'org-table)
  579. (defcustom org-table-automatic-realign t
  580. "Non-nil means, automatically re-align table when pressing TAB or RETURN.
  581. When nil, aligning is only done with \\[org-table-align], or after column
  582. removal/insertion."
  583. :group 'org-table-editing
  584. :type 'boolean)
  585. (defcustom org-table-limit-column-width t ;kw
  586. "Non-nil means, allow to limit the width of table columns with <N> fields."
  587. :group 'org-table-editing
  588. :type 'boolean)
  589. (defcustom org-table-auto-blank-field t
  590. "Non-nil means, automatically blank table field when starting to type into it.
  591. This only happens when typing immediately after a field motion
  592. command (TAB, S-TAB or RET).
  593. Only relevant when `org-enable-table-editor' is equal to `optimized'."
  594. :group 'org-table-editing
  595. :type 'boolean)
  596. (defcustom org-table-tab-jumps-over-hlines t
  597. "Non-nil means, tab in the last column of a table with jump over a hline.
  598. If a horizontal separator line is following the current line,
  599. `org-table-next-field' can either create a new row before that line, or jump
  600. over the line. When this option is nil, a new line will be created before
  601. this line."
  602. :group 'org-table-editing
  603. :type 'boolean)
  604. (defcustom org-table-tab-recognizes-table.el t
  605. "Non-nil means, TAB will automatically notice a table.el table.
  606. When it sees such a table, it moves point into it and - if necessary -
  607. calls `table-recognize-table'."
  608. :group 'org-table-editing
  609. :type 'boolean)
  610. (defgroup org-table-calculation nil
  611. "Options concerning tables in Org-mode."
  612. :tag "Org Table Calculation"
  613. :group 'org-table)
  614. (defcustom org-table-copy-increment t
  615. "Non-nil means, increment when copying current field with \\[org-table-copy-down]."
  616. :group 'org-table-calculation
  617. :type 'boolean)
  618. (defcustom org-calc-default-modes
  619. '(calc-internal-prec 12
  620. calc-float-format (float 5)
  621. calc-angle-mode deg
  622. calc-prefer-frac nil
  623. calc-symbolic-mode nil
  624. calc-date-format (YYYY "-" MM "-" DD " " Www (" " HH ":" mm))
  625. calc-display-working-message t
  626. )
  627. "List with Calc mode settings for use in calc-eval for table formulas.
  628. The list must contain alternating symbols (Calc modes variables and values).
  629. Don't remove any of the default settings, just change the values. Org-mode
  630. relies on the variables to be present in the list."
  631. :group 'org-table-calculation
  632. :type 'plist)
  633. (defcustom org-table-formula-evaluate-inline t
  634. "Non-nil means, TAB and RET evaluate a formula in current table field.
  635. If the current field starts with an equal sign, it is assumed to be a formula
  636. which should be evaluated as described in the manual and in the documentation
  637. string of the command `org-table-eval-formula'. This feature requires the
  638. Emacs calc package.
  639. When this variable is nil, formula calculation is only available through
  640. the command \\[org-table-eval-formula]."
  641. :group 'org-table-calculation
  642. :type 'boolean)
  643. (defcustom org-table-formula-use-constants t
  644. "Non-nil means, interpret constants in formulas in tables.
  645. A constant looks like `$c' or `$Grav' and will be replaced before evaluation
  646. by the value given in `org-table-formula-constants', or by a value obtained
  647. from the `constants.el' package."
  648. :group 'org-table-calculation
  649. :type 'boolean)
  650. (defcustom org-table-formula-constants nil
  651. "Alist with constant names and values, for use in table formulas.
  652. The car of each element is a name of a constant, without the `$' before it.
  653. The cdr is the value as a string. For example, if you'd like to use the
  654. speed of light in a formula, you would configure
  655. (setq org-table-formula-constants '((\"c\" . \"299792458.\")))
  656. and then use it in an equation like `$1*$c'."
  657. :group 'org-table-calculation
  658. :type '(repeat
  659. (cons (string :tag "name")
  660. (string :tag "value"))))
  661. (defcustom org-table-formula-numbers-only nil
  662. "Non-nil means, calculate only with numbers in table formulas.
  663. Then all input fields will be converted to a number, and the result
  664. must also be a number. When nil, calc's full potential is available
  665. in table calculations, including symbolics etc."
  666. :group 'org-table-calculation
  667. :type 'boolean)
  668. (defcustom org-table-allow-automatic-line-recalculation t
  669. "Non-nil means, lines marked with |#| or |*| will be recomputed automatically.
  670. Automatically means, when TAB or RET or C-c C-c are pressed in the line."
  671. :group 'org-table-calculation
  672. :type 'boolean)
  673. (defgroup org-link nil
  674. "Options concerning links in Org-mode."
  675. :tag "Org Link"
  676. :group 'org)
  677. (defcustom org-descriptive-links t
  678. "Non-nil means, hide link part and only show description of bracket links.
  679. Bracket links are like [[link][descritpion]]. This variable sets the initial
  680. state in new org-mode buffers. The setting can then be toggled on a
  681. per-buffer basis from the Org->Hyperlinks menu."
  682. :group 'org-link
  683. :type 'boolean)
  684. (defcustom org-link-style 'bracket
  685. "The style of links to be inserted with \\[org-insert-link].
  686. Possible values are:
  687. bracket [[link][description]]. This is recommended
  688. plain Description \\n link. The old way, no longer recommended."
  689. :group 'org-link
  690. :type '(choice
  691. (const :tag "Bracket (recommended)" bracket)
  692. (const :tag "Plain (no longer recommended)" plain)))
  693. (defcustom org-link-format "%s"
  694. "Default format for external, URL-like linkes in the buffer.
  695. This is a format string for printf, %s will be replaced by the link text.
  696. The recommended value is just \"%s\", since links will be protected by
  697. enclosing them in double brackets. If you prefer plain links (see variable
  698. `org-link-style'), \"<%s>\" is useful. Some people also recommend an
  699. additional URL: prefix, so the format would be \"<URL:%s>\"."
  700. :group 'org-link
  701. :type '(choice
  702. (const :tag "\"%s\" (e.g. http://www.there.com)" "%s")
  703. (const :tag "\"<%s>\" (e.g. <http://www.there.com>)" "<%s>")
  704. (const :tag "\"<URL:%s>\" (e.g. <URL:http://www.there.com>)" "<URL:%s>")
  705. (string :tag "Other" :value "<%s>")))
  706. (defcustom org-link-file-path-type 'adaptive
  707. "How the path name in file links should be stored.
  708. Valid values are:
  709. relative relative to the current directory, i.e. the directory of the file
  710. into which the link is being inserted.
  711. absolute absolute path, if possible with ~ for home directory.
  712. noabbrev absolute path, no abbreviation of home directory.
  713. adaptive Use relative path for files in the current directory and sub-
  714. directories of it. For other files, use an absolute path."
  715. :group 'org-link
  716. :type '(choice
  717. (const relative)
  718. (const absolute)
  719. (const noabbrev)
  720. (const adaptive)))
  721. (defcustom org-activate-links '(bracket angle plain radio tag date)
  722. "Types of links that should be activated in Org-mode files.
  723. This is a list of symbols, each leading to the activation of a certain link
  724. type. In principle, it does not hurt to turn on most link types - there may
  725. be a small gain when turning off unused link types. The types are:
  726. bracket The recommended [[link][description]] or [[link]] links with hiding.
  727. angular Links in angular brackes that may contain whitespace like
  728. <bbdb:Carsten Dominik>.
  729. plain Plain links in normal text, no whitespace, like http://google.com.
  730. radio Text that is matched by a radio target, see manual for details.
  731. tag Tag settings in a headline (link to tag search).
  732. date Time stamps (link to calendar).
  733. camel CamelCase words defining text searches.
  734. Changing this variable requires a restart of Emacs to become effective."
  735. :group 'org-link
  736. :type '(set (const :tag "Double bracket links (new style)" bracket)
  737. (const :tag "Angular bracket links (old style)" angular)
  738. (const :tag "plain text links" plain)
  739. (const :tag "Radio target matches" radio)
  740. (const :tag "Tags" tag)
  741. (const :tag "Timestamps" date)
  742. (const :tag "CamelCase words" camel)))
  743. (defgroup org-link-store nil
  744. "Options concerning storing links in Org-mode"
  745. :tag "Org Store Link"
  746. :group 'org-link)
  747. (defcustom org-context-in-file-links t
  748. "Non-nil means, file links from `org-store-link' contain context.
  749. A search string will be added to the file name with :: as separator and
  750. used to find the context when the link is activated by the command
  751. `org-open-at-point'.
  752. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  753. negates this setting for the duration of the command."
  754. :group 'org-link-store
  755. :type 'boolean)
  756. (defcustom org-file-link-context-use-camel-case nil
  757. "Non-nil means, use CamelCase to store a search context in a file link.
  758. When nil, the search string simply consists of the words of the string.
  759. CamelCase is deprecated, and support for it may be dropped in the future."
  760. :group 'org-link-store
  761. :type 'boolean)
  762. (defcustom org-keep-stored-link-after-insertion nil
  763. "Non-nil means, keep link in list for entire session.
  764. The command `org-store-link' adds a link pointing to the current
  765. location to an internal list. These links accumulate during a session.
  766. The command `org-insert-link' can be used to insert links into any
  767. Org-mode file (offering completion for all stored links). When this
  768. option is nil, every link which has been inserted once using \\[org-insert-link]
  769. will be removed from the list, to make completing the unused links
  770. more efficient."
  771. :group 'org-link-store
  772. :type 'boolean)
  773. (defcustom org-usenet-links-prefer-google nil
  774. "Non-nil means, `org-store-link' will create web links to Google groups.
  775. When nil, Gnus will be used for such links.
  776. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  777. negates this setting for the duration of the command."
  778. :group 'org-link-store
  779. :type 'boolean)
  780. (defgroup org-link-follow nil
  781. "Options concerning following links in Org-mode"
  782. :tag "Org Follow Link"
  783. :group 'org-link)
  784. (defcustom org-tab-follows-link nil
  785. "Non-nil means, on links TAB will follow the link.
  786. Needs to be set before org.el is loaded."
  787. :group 'org-link-follow
  788. :type 'boolean)
  789. (defcustom org-return-follows-link nil
  790. "Non-nil means, on links RET will follow the link.
  791. Needs to be set before org.el is loaded."
  792. :group 'org-link-follow
  793. :type 'boolean)
  794. (defcustom org-mouse-1-follows-link t
  795. "Non-nil means, mouse-1 on a link will follow the link.
  796. A longer mouse click will still set point. Does not wortk on XEmacs.
  797. Needs to be set before org.el is loaded."
  798. :group 'org-link-follow
  799. :type 'boolean)
  800. (defcustom org-mark-ring-length 4
  801. "Number of different positions to be recorded in the ring
  802. Changing this requires a restart of Emacs to work correctly."
  803. :group 'org-link-follow
  804. :type 'interger)
  805. (defcustom org-link-frame-setup
  806. '((vm . vm-visit-folder-other-frame)
  807. (gnus . gnus-other-frame)
  808. (file . find-file-other-window))
  809. "Setup the frame configuration for following links.
  810. When following a link with Emacs, it may often be useful to display
  811. this link in another window or frame. This variable can be used to
  812. set this up for the different types of links.
  813. For VM, use any of
  814. `vm-visit-folder'
  815. `vm-visit-folder-other-frame'
  816. For Gnus, use any of
  817. `gnus'
  818. `gnus-other-frame'
  819. For FILE, use any of
  820. `find-file'
  821. `find-file-other-window'
  822. `find-file-other-frame'
  823. For the calendar, use the variable `calendar-setup'.
  824. For BBDB, it is currently only possible to display the matches in
  825. another window."
  826. :group 'org-link-follow
  827. :type '(list
  828. (cons (const vm)
  829. (choice
  830. (const vm-visit-folder)
  831. (const vm-visit-folder-other-window)
  832. (const vm-visit-folder-other-frame)))
  833. (cons (const gnus)
  834. (choice
  835. (const gnus)
  836. (const gnus-other-frame)))
  837. (cons (const file)
  838. (choice
  839. (const find-file)
  840. (const find-file-other-window)
  841. (const find-file-other-frame)))))
  842. (defcustom org-open-non-existing-files nil
  843. "Non-nil means, `org-open-file' will open non-existing file.
  844. When nil, an error will be generated."
  845. :group 'org-link-follow
  846. :type 'boolean)
  847. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  848. "Function and arguments to call for following mailto links.
  849. This is a list with the first element being a lisp function, and the
  850. remaining elements being arguments to the function. In string arguments,
  851. %a will be replaced by the address, and %s will be replaced by the subject
  852. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  853. :group 'org-link-follow
  854. :type '(choice
  855. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  856. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  857. (const :tag "message-mail" (message-mail "%a" "%s"))
  858. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  859. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  860. "Non-nil means, ask for confirmation before executing shell links.
  861. Shell links can be dangerous, just thing about a link
  862. [[shell:rm -rf ~/*][Google Search]]
  863. This link would show up in your Org-mode document as \"Google Search\"
  864. but really it would remove your entire home directory.
  865. Therefore I *definitely* advise against setting this variable to nil.
  866. Just change it to `y-or-n-p' of you want to confirm with a single key press
  867. rather than having to type \"yes\"."
  868. :group 'org-link-follow
  869. :type '(choice
  870. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  871. (const :tag "with y-or-n (faster)" y-or-n-p)
  872. (const :tag "no confirmation (dangerous)" nil)))
  873. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  874. "Non-nil means, ask for confirmation before executing elisp links.
  875. Elisp links can be dangerous, just thing about a link
  876. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  877. This link would show up in your Org-mode document as \"Google Search\"
  878. but really it would remove your entire home directory.
  879. Therefore I *definitely* advise against setting this variable to nil.
  880. Just change it to `y-or-n-p' of you want to confirm with a single key press
  881. rather than having to type \"yes\"."
  882. :group 'org-link-follow
  883. :type '(choice
  884. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  885. (const :tag "with y-or-n (faster)" y-or-n-p)
  886. (const :tag "no confirmation (dangerous)" nil)))
  887. (defconst org-file-apps-defaults-gnu
  888. '((remote . emacs)
  889. (t . mailcap))
  890. "Default file applications on a UNIX or GNU/Linux system.
  891. See `org-file-apps'.")
  892. (defconst org-file-apps-defaults-macosx
  893. '((remote . emacs)
  894. (t . "open %s")
  895. ("ps" . "gv %s")
  896. ("ps.gz" . "gv %s")
  897. ("eps" . "gv %s")
  898. ("eps.gz" . "gv %s")
  899. ("dvi" . "xdvi %s")
  900. ("fig" . "xfig %s"))
  901. "Default file applications on a MacOS X system.
  902. The system \"open\" is known as a default, but we use X11 applications
  903. for some files for which the OS does not have a good default.
  904. See `org-file-apps'.")
  905. (defconst org-file-apps-defaults-windowsnt
  906. (list
  907. '(remote . emacs)
  908. (cons t
  909. (list (if (featurep 'xemacs)
  910. 'mswindows-shell-execute
  911. 'w32-shell-execute)
  912. "open" 'file)))
  913. "Default file applications on a Windows NT system.
  914. The system \"open\" is used for most files.
  915. See `org-file-apps'.")
  916. (defcustom org-file-apps
  917. '(
  918. ("txt" . emacs)
  919. ("tex" . emacs)
  920. ("ltx" . emacs)
  921. ("org" . emacs)
  922. ("el" . emacs)
  923. ("bib" . emacs)
  924. )
  925. "External applications for opening `file:path' items in a document.
  926. Org-mode uses system defaults for different file types, but
  927. you can use this variable to set the application for a given file
  928. extension. The entries in this list are cons cells where the car identifies
  929. files and the cdr the corresponding command. Possible values for the
  930. file identifier are
  931. \"ext\" A string identifying an extension
  932. `directory' Matches a directory
  933. `remote' Matches a remote file, accessible through tramp or efs.
  934. Remote files most likely should be visited through emacs
  935. because external applications cannot handle such paths.
  936. t Default for all remaining files
  937. Possible values for the command are:
  938. `emacs' The file will be visited by the current Emacs process.
  939. `default' Use the default application for this file type.
  940. string A command to be executed by a shell; %s will be replaced
  941. by the path to the file.
  942. sexp A Lisp form which will be evaluated. The file path will
  943. be available in the Lisp variable `file'.
  944. For more examples, see the system specific constants
  945. `org-file-apps-defaults-macosx'
  946. `org-file-apps-defaults-windowsnt'
  947. `org-file-apps-defaults-gnu'."
  948. :group 'org-link-follow
  949. :type '(repeat
  950. (cons (choice :value ""
  951. (string :tag "Extension")
  952. (const :tag "Default for unrecognized files" t)
  953. (const :tag "Remote file" remote)
  954. (const :tag "Links to a directory" directory))
  955. (choice :value ""
  956. (const :tag "Visit with Emacs" emacs)
  957. (const :tag "Use system default" default)
  958. (string :tag "Command")
  959. (sexp :tag "Lisp form")))))
  960. (defcustom org-mhe-search-all-folders nil
  961. "Non-nil means, that the search for the mh-message will be extended to
  962. all folders if the message cannot be found in the folder given in the link.
  963. Searching all folders is very effective with one of the search engines
  964. supported by MH-E, but will be slow with pick."
  965. :group 'org-link-follow
  966. :type 'boolean)
  967. (defgroup org-remember nil
  968. "Options concerning interaction with remember.el."
  969. :tag "Org Remember"
  970. :group 'org)
  971. (defcustom org-directory "~/org"
  972. "Directory with org files.
  973. This directory will be used as default to prompt for org files.
  974. Used by the hooks for remember.el."
  975. :group 'org-remember
  976. :type 'directory)
  977. (defcustom org-default-notes-file "~/.notes"
  978. "Default target for storing notes.
  979. Used by the hooks for remember.el. This can be a string, or nil to mean
  980. the value of `remember-data-file'."
  981. :group 'org-remember
  982. :type '(choice
  983. (const :tag "Default from remember-data-file" nil)
  984. file))
  985. (defcustom org-remember-templates nil
  986. "Templates for the creation of remember buffers.
  987. When nil, just let remember make the buffer.
  988. When not nil, this is a list of 3-element lists. In each entry, the first
  989. element is a character, a unique key to select this template.
  990. The second element is the template. The third element is optional and can
  991. specify a destination file for remember items created with this template.
  992. The default file is given by `org-default-notes-file'.
  993. The template specifies the structure of the remember buffer. It should have
  994. a first line starting with a star, to act as the org-mode headline.
  995. Furthermore, the following %-escapes will be replaced with content:
  996. %t time stamp, date only
  997. %T time stamp with date and time
  998. %u inactive time stamp, date only
  999. %U inactive time stamp with date and time
  1000. %n user name
  1001. %a annotation, normally the link created with org-store-link
  1002. %i initial content, the region when remember is called with C-u.
  1003. If %i is indented, the entire inserted text will be indented as well.
  1004. %? This will be removed, and the cursor placed at this position."
  1005. :group 'org-remember
  1006. :type '(repeat :tag "enabled"
  1007. (list :value (?a "\n" nil)
  1008. (character :tag "Selection Key")
  1009. (string :tag "Template")
  1010. (file :tag "Destination file (optional)"))))
  1011. (defcustom org-reverse-note-order nil
  1012. "Non-nil means, store new notes at the beginning of a file or entry.
  1013. When nil, new notes will be filed to the end of a file or entry."
  1014. :group 'org-remember
  1015. :type '(choice
  1016. (const :tag "Reverse always" t)
  1017. (const :tag "Reverse never" nil)
  1018. (repeat :tag "By file name regexp"
  1019. (cons regexp boolean))))
  1020. (defgroup org-todo nil
  1021. "Options concerning TODO items in Org-mode."
  1022. :tag "Org TODO"
  1023. :group 'org)
  1024. (defcustom org-todo-keywords '("TODO" "DONE")
  1025. "List of TODO entry keywords.
  1026. \\<org-mode-map>By default, this is '(\"TODO\" \"DONE\"). The last entry in the list is
  1027. considered to mean that the entry is \"done\". All the other mean that
  1028. action is required, and will make the entry show up in todo lists, diaries
  1029. etc.
  1030. The command \\[org-todo] cycles an entry through these states, and an
  1031. additional state where no keyword is present. For details about this
  1032. cycling, see also the variable `org-todo-interpretation'
  1033. Changes become only effective after restarting Emacs."
  1034. :group 'org-todo
  1035. :group 'org-keywords
  1036. :type '(repeat (string :tag "Keyword")))
  1037. (defcustom org-todo-interpretation 'sequence
  1038. "Controls how TODO keywords are interpreted.
  1039. This variable is only relevant if `org-todo-keywords' contains more than two
  1040. states. \\<org-mode-map>Possible values are `sequence' and `type'.
  1041. When `sequence', \\[org-todo] will always switch to the next state in the
  1042. `org-todo-keywords' list. When `type', \\[org-todo] only cycles from state
  1043. to state when executed several times in direct succession. Otherwise, it
  1044. switches directly to DONE from any state.
  1045. See the manual for more information."
  1046. :group 'org-todo
  1047. :group 'org-keywords
  1048. :type '(choice (const sequence)
  1049. (const type)))
  1050. (defcustom org-after-todo-state-change-hook nil
  1051. "Hook which is run after the state of a TODO item was changed.
  1052. The new state (a string with a TODO keyword, or nil) is available in the
  1053. Lisp variable `state'."
  1054. :group 'org-todo
  1055. :type 'hook)
  1056. (defcustom org-log-done nil
  1057. "When set, insert a (non-active) time stamp when TODO entry is marked DONE.
  1058. When the state of an entry is changed from nothing to TODO, remove a previous
  1059. closing date.
  1060. This can also be configured on a per-file basis by adding one of
  1061. the following lines anywhere in the buffer:
  1062. #+STARTUP: logging
  1063. #+STARTUP: nologging"
  1064. :group 'org-todo
  1065. :type 'boolean)
  1066. (defgroup org-priorities nil
  1067. "Priorities in Org-mode."
  1068. :tag "Org Priorities"
  1069. :group 'org-todo)
  1070. (defcustom org-default-priority ?B
  1071. "The default priority of TODO items.
  1072. This is the priority an item get if no explicit priority is given."
  1073. :group 'org-priorities
  1074. :type 'character)
  1075. (defcustom org-lowest-priority ?C
  1076. "The lowest priority of TODO items. A character like ?A, ?B etc."
  1077. :group 'org-priorities
  1078. :type 'character)
  1079. (defgroup org-time nil
  1080. "Options concerning time stamps and deadlines in Org-mode."
  1081. :tag "Org Time"
  1082. :group 'org)
  1083. (defcustom org-insert-labeled-timestamps-at-point nil
  1084. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1085. When nil, these labeled time stamps are forces into the second line of an
  1086. entry, just after the headline. When scheduling from the global TODO list,
  1087. the time stamp will always be forced into the second line."
  1088. :group 'org-time
  1089. :type 'boolean)
  1090. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1091. "Formats for `format-time-string' which are used for time stamps.
  1092. It is not recommended to change this constant.")
  1093. (defcustom org-time-stamp-rounding-minutes 0
  1094. "Number of minutes to round time stamps to upon insertion.
  1095. When zero, insert the time unmodified. Useful rounding numbers
  1096. should be factors of 60, so for example 5, 10, 15.
  1097. When this is not zero, you can still force an exact time-stamp by using
  1098. a double prefix argument to a time-stamp command like `C-c .' or `C-c !'."
  1099. :group 'org-time
  1100. :type 'integer)
  1101. (defcustom org-deadline-warning-days 30
  1102. "No. of days before expiration during which a deadline becomes active.
  1103. This variable governs the display in the org file."
  1104. :group 'org-time
  1105. :type 'number)
  1106. (defcustom org-popup-calendar-for-date-prompt t
  1107. "Non-nil means, pop up a calendar when prompting for a date.
  1108. In the calendar, the date can be selected with mouse-1. However, the
  1109. minibuffer will also be active, and you can simply enter the date as well.
  1110. When nil, only the minibuffer will be available."
  1111. :group 'org-time
  1112. :type 'boolean)
  1113. (defcustom org-calendar-follow-timestamp-change t
  1114. "Non-nil means, make the calendar window follow timestamp changes.
  1115. When a timestamp is modified and the calendar window is visible, it will be
  1116. moved to the new date."
  1117. :group 'org-time
  1118. :type 'boolean)
  1119. (defgroup org-tags nil
  1120. "Options concerning tags in Org-mode."
  1121. :tag "Org Tags"
  1122. :group 'org)
  1123. (defcustom org-tag-alist nil
  1124. "List of tags allowed in Org-mode files.
  1125. When this list is nil, Org-mode will base TAG input on what is already in the
  1126. buffer.
  1127. The value of this variable is an alist, the car may be (and should) be a
  1128. character that is used to select that tag through the fast-tag-selection
  1129. interface. See the manual for details."
  1130. :group 'org-tags
  1131. :type '(repeat
  1132. (choice
  1133. (cons (string :tag "Tag name")
  1134. (character :tag "Access char"))
  1135. (const :tag "Start radio group" (:startgroup))
  1136. (const :tag "End radio group" (:endgroup)))))
  1137. (defcustom org-use-fast-tag-selection 'auto
  1138. "Non-nil means, use fast tag selection scheme.
  1139. This is a special interface to select and deselect tags with single keys.
  1140. When nil, fast selection is never used.
  1141. When the symbol `auto', fast selection is used if and only if selection
  1142. characters for tags have been configured, either through the variable
  1143. `org-tag-alist' or through a #+TAGS line in the buffer.
  1144. When t, fast selection is always used and selection keys are assigned
  1145. automatically if necessary."
  1146. :group 'org-tags
  1147. :type '(choice
  1148. (const :tag "Always" t)
  1149. (const :tag "Never" nil)
  1150. (const :tag "When selection characters are configured" 'auto)))
  1151. (defcustom org-tags-column 48
  1152. "The column to which tags should be indented in a headline.
  1153. If this number is positive, it specifies the column. If it is negative,
  1154. it means that the tags should be flushright to that column. For example,
  1155. -79 works well for a normal 80 character screen."
  1156. :group 'org-tags
  1157. :type 'integer)
  1158. (defcustom org-auto-align-tags t
  1159. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1160. These operations change the length of a headline and therefore shift
  1161. the tags around. With this options turned on, after each such operation
  1162. the tags are again aligned to `org-tags-column'."
  1163. :group 'org-tags
  1164. :type 'boolean)
  1165. (defcustom org-use-tag-inheritance t
  1166. "Non-nil means, tags in levels apply also for sublevels.
  1167. When nil, only the tags directly given in a specific line apply there.
  1168. If you turn off this option, you very likely want to turn on the
  1169. companion option `org-tags-match-list-sublevels'."
  1170. :group 'org-tags
  1171. :type 'boolean)
  1172. (defcustom org-tags-match-list-sublevels nil
  1173. "Non-nil means list also sublevels of headlines matching tag search.
  1174. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1175. the sublevels of a headline matching a tag search often also match
  1176. the same search. Listing all of them can create very long lists.
  1177. Setting this variable to nil causes subtrees of a match to be skipped.
  1178. This option is off by default, because inheritance in on. If you turn
  1179. inheritance off, you very likely want to turn this option on.
  1180. As a special case, if the tag search is restricted to TODO items, the
  1181. value of this variable is ignored and sublevels are always checked, to
  1182. make sure all corresponding TODO items find their way into the list."
  1183. :group 'org-tags
  1184. :type 'boolean)
  1185. (defvar org-tags-history nil
  1186. "History of minibuffer reads for tags.")
  1187. (defvar org-last-tags-completion-table nil
  1188. "The last used completion table for tags.")
  1189. (defgroup org-agenda nil
  1190. "Options concerning agenda display Org-mode."
  1191. :tag "Org Agenda"
  1192. :group 'org)
  1193. (defvar org-category nil
  1194. "Variable used by org files to set a category for agenda display.
  1195. Such files should use a file variable to set it, for example
  1196. -*- mode: org; org-category: \"ELisp\"
  1197. or contain a special line
  1198. #+CATEGORY: ELisp
  1199. If the file does not specify a category, then file's base name
  1200. is used instead.")
  1201. (make-variable-buffer-local 'org-category)
  1202. (defcustom org-agenda-files nil
  1203. "The files to be used for agenda display.
  1204. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1205. \\[org-remove-file]. You can also use customize to edit the list.
  1206. If the value of the variable is not a list but a single file name, then
  1207. the list of agenda files is actually stored and maintained in that file, one
  1208. agenda file per line."
  1209. :group 'org-agenda
  1210. :type '(choice
  1211. (repeat :tag "List of files" file)
  1212. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1213. (defcustom org-agenda-custom-commands ;'(("w" todo "WAITING"))
  1214. '(("w" todo "WAITING" ((aaa 1) (bbb 2))))
  1215. "Custom commands for the agenda.
  1216. These commands will be offered on the splash screen displayed by the
  1217. agenda dispatcher \\[org-agenda]. Each entry is a list like this:
  1218. (key type match options)
  1219. key The key (a single char as a string) to be associated with the command.
  1220. type The command type, any of the following symbols:
  1221. todo Entries with a specific TODO keyword, in all agenda files.
  1222. tags Tags match in all agenda files.
  1223. tags-todo Tags match in all agenda files, TODO entries only.
  1224. todo-tree Sparse tree of specific TODO keyword in *current* file.
  1225. tags-tree Sparse tree with all tags matches in *current* file.
  1226. occur-tree Occur sparse tree for *current* file.
  1227. match What to search for:
  1228. - a single keyword for TODO keyword searches
  1229. - a tags match expression for tags searches
  1230. - a regular expression for occur searches
  1231. options A list of option setttings, similar to that in a let form, so like
  1232. this: ((opt1 val1) (opt2 val2) ...)
  1233. You can also define a set of commands, to create a composite agenda buffer.
  1234. In this case, an entry looks like this:
  1235. (key desc (cmd1 cmd2 ...) general-options)
  1236. where
  1237. desc A description string to be displayed in the dispatcher menu.
  1238. cmd An agenda command, similar to the above. However, tree commands
  1239. are no allowed, but instead you can get agenda and global todo list.
  1240. So valid commands for a set are:
  1241. (agenda)
  1242. (alltodo)
  1243. (todo \"match\" options)
  1244. (tags \"match\" options )
  1245. (tags-todo \"match\" options)
  1246. Each command can carry a list of options, and another set of options can be
  1247. given for the whole set of commands. Individual command options take
  1248. precedence over the general options."
  1249. :group 'org-agenda
  1250. :type '(repeat
  1251. (choice
  1252. (list :tag "Single command"
  1253. (string :tag "Key")
  1254. (choice
  1255. (const :tag "Tags search (all agenda files)" tags)
  1256. (const :tag "Tags search of TODO entries (all agenda files)" tags-todo)
  1257. (const :tag "TODO keyword search (all agenda files)" todo)
  1258. (const :tag "Tags sparse tree (current buffer)" tags-tree)
  1259. (const :tag "TODO keyword tree (current buffer)" todo-tree)
  1260. (const :tag "Occur tree (current buffer)" occur-tree))
  1261. (string :tag "Match")
  1262. (repeat :tag "Local options"
  1263. (list (variable :tag "Option") (sexp :tag "Value"))))
  1264. (list :tag "Command series, all agenda files"
  1265. (string :tag "Key")
  1266. (string :tag "Description")
  1267. (repeat
  1268. (choice
  1269. (const :tag "Agenda" (agenda))
  1270. (const :tag "TODO list" (alltodo))
  1271. (list :tag "Tags search"
  1272. (const :format "" tags)
  1273. (string :tag "Match")
  1274. (repeat :tag "Local options"
  1275. (list (variable :tag "Option")
  1276. (sexp :tag "Value"))))
  1277. (list :tag "Tags search, TODO entries only"
  1278. (const :format "" tags-todo)
  1279. (string :tag "Match")
  1280. (repeat :tag "Local options"
  1281. (list (variable :tag "Option")
  1282. (sexp :tag "Value"))))
  1283. (list :tag "TODO keyword search"
  1284. (const :format "" todo)
  1285. (string :tag "Match")
  1286. (repeat :tag "Local options"
  1287. (list (variable :tag "Option")
  1288. (sexp :tag "Value"))))))
  1289. (repeat :tag "General options"
  1290. (list (variable :tag "Option")
  1291. (sexp :tag "Value")))))))
  1292. (defcustom org-agenda-todo-list-sublevels t
  1293. "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
  1294. When nil, the sublevels of a TODO entry are not checked, resulting in
  1295. potentially much shorter TODO lists."
  1296. :group 'org-agenda
  1297. :group 'org-todo
  1298. :type 'boolean)
  1299. (defcustom org-agenda-todo-ignore-scheduled nil
  1300. "Non-nil means, don't show scheduled entries in the global todo list.
  1301. The idea behind this is that by scheduling it, you have already taken care
  1302. of this item."
  1303. :group 'org-agenda
  1304. :group 'org-todo
  1305. :type 'boolean)
  1306. (defcustom org-timeline-show-empty-dates 3
  1307. "Non-nil means, `org-timeline' also shows dates without an entry.
  1308. When nil, only the days which actually have entries are shown.
  1309. When t, all days between the first and the last date are shown.
  1310. When an integer, show also empty dates, but if there is a gap of more than
  1311. N days, just insert a special line indicating the size of the gap."
  1312. :group 'org-agenda
  1313. :type '(choice
  1314. (const :tag "None" nil)
  1315. (const :tag "All" t)
  1316. (number :tag "at most")))
  1317. (defcustom org-agenda-include-all-todo nil
  1318. "Set means weekly/daily agenda will always contain all TODO entries.
  1319. The TODO entries will be listed at the top of the agenda, before
  1320. the entries for specific days."
  1321. :group 'org-agenda
  1322. :type 'boolean)
  1323. (defcustom org-agenda-include-diary nil
  1324. "If non-nil, include in the agenda entries from the Emacs Calendar's diary."
  1325. :group 'org-agenda
  1326. :type 'boolean)
  1327. (defcustom org-calendar-to-agenda-key [?c]
  1328. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1329. The command `org-calendar-goto-agenda' will be bound to this key. The
  1330. default is the character `c' because then `c' can be used to switch back and
  1331. forth between agenda and calendar."
  1332. :group 'org-agenda
  1333. :type 'sexp)
  1334. (defgroup org-agenda-setup nil
  1335. "Options concerning setting up the Agenda window in Org Mode."
  1336. :tag "Org Agenda Window Setup"
  1337. :group 'org-agenda)
  1338. (defcustom org-agenda-mouse-1-follows-link nil
  1339. "Non-nil means, mouse-1 on a link will follow the link in the agenda.
  1340. A longer mouse click will still set point. Does not wortk on XEmacs.
  1341. Needs to be set before org.el is loaded."
  1342. :group 'org-agenda-setup
  1343. :type 'boolean)
  1344. (defcustom org-agenda-start-with-follow-mode nil
  1345. "The initial value of follwo-mode in a newly created agenda window."
  1346. :group 'org-agenda-setup
  1347. :type 'boolean)
  1348. (defcustom org-select-agenda-window t
  1349. "Non-nil means, after creating an agenda, move cursor into Agenda window.
  1350. When nil, cursor will remain in the current window."
  1351. :group 'org-agenda-setup
  1352. :type 'boolean)
  1353. (defcustom org-fit-agenda-window t
  1354. "Non-nil means, change window size of agenda to fit content."
  1355. :group 'org-agenda-setup
  1356. :type 'boolean)
  1357. (defgroup org-agenda-display nil
  1358. "Options concerning what to display initially in Agenda."
  1359. :tag "Org Agenda Display"
  1360. :group 'org-agenda)
  1361. (defcustom org-agenda-show-all-dates t
  1362. "Non-nil means, `org-agenda' shows every day in the selected range.
  1363. When nil, only the days which actually have entries are shown."
  1364. :group 'org-agenda-display
  1365. :type 'boolean)
  1366. (defcustom org-agenda-start-on-weekday 1
  1367. "Non-nil means, start the overview always on the specified weekday.
  1368. 0 denotes Sunday, 1 denotes Monday etc.
  1369. When nil, always start on the current day."
  1370. :group 'org-agenda-display
  1371. :type '(choice (const :tag "Today" nil)
  1372. (number :tag "Weekday No.")))
  1373. (defcustom org-agenda-ndays 7
  1374. "Number of days to include in overview display.
  1375. Should be 1 or 7."
  1376. :group 'org-agenda-display
  1377. :type 'number)
  1378. (defcustom org-agenda-use-time-grid t
  1379. "Non-nil means, show a time grid in the agenda schedule.
  1380. A time grid is a set of lines for specific times (like every two hours between
  1381. 8:00 and 20:00). The items scheduled for a day at specific times are
  1382. sorted in between these lines.
  1383. For details about when the grid will be shown, and what it will look like, see
  1384. the variable `org-agenda-time-grid'."
  1385. :group 'org-agenda-display
  1386. :type 'boolean)
  1387. (defcustom org-agenda-time-grid
  1388. '((daily today require-timed)
  1389. "----------------"
  1390. (800 1000 1200 1400 1600 1800 2000))
  1391. "The settings for time grid for agenda display.
  1392. This is a list of three items. The first item is again a list. It contains
  1393. symbols specifying conditions when the grid should be displayed:
  1394. daily if the agenda shows a single day
  1395. weekly if the agenda shows an entire week
  1396. today show grid on current date, independent of daily/weekly display
  1397. require-timed show grid only if at least one item has a time specification
  1398. The second item is a string which will be places behing the grid time.
  1399. The third item is a list of integers, indicating the times that should have
  1400. a grid line."
  1401. :group 'org-agenda-display
  1402. :type
  1403. '(list
  1404. (set :greedy t :tag "Grid Display Options"
  1405. (const :tag "Show grid in single day agenda display" daily)
  1406. (const :tag "Show grid in weekly agenda display" weekly)
  1407. (const :tag "Always show grid for today" today)
  1408. (const :tag "Show grid only if any timed entries are present"
  1409. require-timed)
  1410. (const :tag "Skip grid times already present in an entry"
  1411. remove-match))
  1412. (string :tag "Grid String")
  1413. (repeat :tag "Grid Times" (integer :tag "Time"))))
  1414. (let ((sorting-choice
  1415. '(choice
  1416. (const time-up) (const time-down)
  1417. (const category-keep) (const category-up) (const category-down)
  1418. (const tag-down) (const tag-up)
  1419. (const priority-up) (const priority-down))))
  1420. (defcustom org-agenda-sorting-strategy
  1421. '((agenda time-up category-keep priority-down)
  1422. (todo category-keep priority-down)
  1423. (tags category-keep))
  1424. "Sorting structure for the agenda items of a single day.
  1425. This is a list of symbols which will be used in sequence to determine
  1426. if an entry should be listed before another entry. The following
  1427. symbols are recognized:
  1428. time-up Put entries with time-of-day indications first, early first
  1429. time-down Put entries with time-of-day indications first, late first
  1430. category-keep Keep the default order of categories, corresponding to the
  1431. sequence in `org-agenda-files'.
  1432. category-up Sort alphabetically by category, A-Z.
  1433. category-down Sort alphabetically by category, Z-A.
  1434. tag-up Sort alphabetically by last tag, A-Z.
  1435. tag-down Sort alphabetically by last tag, Z-A.
  1436. priority-up Sort numerically by priority, high priority last.
  1437. priority-down Sort numerically by priority, high priority first.
  1438. The different possibilities will be tried in sequence, and testing stops
  1439. if one comparison returns a \"not-equal\". For example, the default
  1440. '(time-up category-keep priority-down)
  1441. means: Pull out all entries having a specified time of day and sort them,
  1442. in order to make a time schedule for the current day the first thing in the
  1443. agenda listing for the day. Of the entries without a time indication, keep
  1444. the grouped in categories, don't sort the categories, but keep them in
  1445. the sequence given in `org-agenda-files'. Within each category sort by
  1446. priority.
  1447. Leaving out `category-keep' would mean that items will be sorted across
  1448. categories by priority."
  1449. :group 'org-agenda-display
  1450. :type `(choice
  1451. (repeat :tag "General" ,sorting-choice)
  1452. (list :tag "Individually"
  1453. (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
  1454. (repeat ,sorting-choice))
  1455. (cons (const :tag "Strategy for TODO lists" todo)
  1456. (repeat ,sorting-choice))
  1457. (cons (const :tag "Strategy for Tags matches" tags)
  1458. (repeat ,sorting-choice))))))
  1459. (defcustom org-sort-agenda-notime-is-late t
  1460. "Non-nil means, items without time are considered late.
  1461. This is only relevant for sorting. When t, items which have no explicit
  1462. time like 15:30 will be considered as 99:01, i.e. later than any items which
  1463. do have a time. When nil, the default time is before 0:00. You can use this
  1464. option to decide if the schedule for today should come before or after timeless
  1465. agenda entries."
  1466. :group 'org-agenda-display
  1467. :type 'boolean)
  1468. (defgroup org-agenda-prefix nil
  1469. "Options concerning the entry prefix in the Org-mode agenda display."
  1470. :tag "Org Agenda Prefix"
  1471. :group 'org-agenda)
  1472. (defcustom org-agenda-prefix-format
  1473. '((agenda . " %-12:c%?-12t% s")
  1474. (timeline . " % s")
  1475. (todo . " %-12:c")
  1476. (tags . " %-12:c"))
  1477. "Format specifications for the prefix of items in the agenda views.
  1478. An alist with four entries, for the different agenda types. The keys to the
  1479. sublists are `agenda', `timeline', `todo', and `tags'. The values
  1480. are format strings.
  1481. This format works similar to a printf format, with the following meaning:
  1482. %c the category of the item, \"Diary\" for entries from the diary, or
  1483. as given by the CATEGORY keyword or derived from the file name.
  1484. %T the *last* tag of the item. Last because inherited tags come
  1485. first in the list.
  1486. %t the time-of-day specification if one applies to the entry, in the
  1487. format HH:MM
  1488. %s Scheduling/Deadline information, a short string
  1489. All specifiers work basically like the standard `%s' of printf, but may
  1490. contain two additional characters: A question mark just after the `%' and
  1491. a whitespace/punctuation character just before the final letter.
  1492. If the first character after `%' is a question mark, the entire field
  1493. will only be included if the corresponding value applies to the
  1494. current entry. This is useful for fields which should have fixed
  1495. width when present, but zero width when absent. For example,
  1496. \"%?-12t\" will result in a 12 character time field if a time of the
  1497. day is specified, but will completely disappear in entries which do
  1498. not contain a time.
  1499. If there is punctuation or whitespace character just before the final
  1500. format letter, this character will be appended to the field value if
  1501. the value is not empty. For example, the format \"%-12:c\" leads to
  1502. \"Diary: \" if the category is \"Diary\". If the category were be
  1503. empty, no additional colon would be interted.
  1504. The default value of this option is \" %-12:c%?-12t% s\", meaning:
  1505. - Indent the line with two space characters
  1506. - Give the category in a 12 chars wide field, padded with whitespace on
  1507. the right (because of `-'). Append a colon if there is a category
  1508. (because of `:').
  1509. - If there is a time-of-day, put it into a 12 chars wide field. If no
  1510. time, don't put in an empty field, just skip it (because of '?').
  1511. - Finally, put the scheduling information and append a whitespace.
  1512. As another example, if you don't want the time-of-day of entries in
  1513. the prefix, you could use:
  1514. (setq org-agenda-prefix-format \" %-11:c% s\")
  1515. See also the variables `org-agenda-remove-times-when-in-prefix' and
  1516. `org-agenda-remove-tags-when-in-prefix'."
  1517. :type '(choice
  1518. (string :tag "General format")
  1519. (list :greedy t :tag "View dependent"
  1520. (cons (const agenda) (string :tag "Format"))
  1521. (cons (const timeline) (string :tag "Format"))
  1522. (cons (const todo) (string :tag "Format"))
  1523. (cons (const tags) (string :tag "Format"))))
  1524. :group 'org-agenda-prefix)
  1525. (defvar org-prefix-format-compiled nil
  1526. "The compiled version of the most recently used prefix format.
  1527. See the variable `org-agenda-prefix-format'.")
  1528. (defcustom org-agenda-remove-times-when-in-prefix t
  1529. "Non-nil means, remove duplicate time specifications in agenda items.
  1530. When the format `org-agenda-prefix-format' contains a `%t' specifier, a
  1531. time-of-day specification in a headline or diary entry is extracted and
  1532. placed into the prefix. If this option is non-nil, the original specification
  1533. \(a timestamp or -range, or just a plain time(range) specification like
  1534. 11:30-4pm) will be removed for agenda display. This makes the agenda less
  1535. cluttered.
  1536. The option can be t or nil. It may also be the symbol `beg', indicating
  1537. that the time should only be removed what it is located at the beginning of
  1538. the headline/diary entry."
  1539. :group 'org-agenda-prefix
  1540. :type '(choice
  1541. (const :tag "Always" t)
  1542. (const :tag "Never" nil)
  1543. (const :tag "When at beginning of entry" beg)))
  1544. (defcustom org-agenda-remove-tags-when-in-prefix nil
  1545. "Non-nil means, remove the tags from the headline copy in the agenda.
  1546. When this is the symbol `prefix', only remove tags when
  1547. `org-agenda-prefix-format' contains a `%T' specifier."
  1548. :group 'org-agenda-prefix
  1549. :type '(choice
  1550. (const :tag "Always" t)
  1551. (const :tag "Never" nil)
  1552. (const :tag "When prefix format contains %T" prefix)))
  1553. (defcustom org-agenda-align-tags-to-column 65
  1554. "Shift tags in agenda items to this column."
  1555. :group 'org-agenda-prefix ;; FIXME
  1556. :type 'integer)
  1557. (defgroup org-latex nil
  1558. "Options for embedding LaTeX code into Org-mode"
  1559. :tag "Org LaTeX"
  1560. :group 'org)
  1561. (defcustom org-format-latex-options
  1562. '(:foreground "Black" :background "Transparent" :scale 1.0
  1563. :matchers ("begin" "$" "$$" "\\(" "\\["))
  1564. "Options for creating images from LaTeX fragments.
  1565. This is a property list with the following properties:
  1566. :foreground the foreground color, for example \"Black\".
  1567. :background the background color, or \"Transparent\".
  1568. :scale a scaling factor for the size of the images
  1569. :matchers a list indicating which matchers should be used to
  1570. find LaTeX fragments. Valid members of this list are:
  1571. \"begin\" find environments
  1572. \"$\" find math expressions surrounded by $...$
  1573. \"$$\" find math expressions surrounded by $$....$$
  1574. \"\\(\" find math expressions surrounded by \\(...\\)
  1575. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  1576. :group 'org-latex
  1577. :type 'plist)
  1578. (defgroup org-export nil
  1579. "Options for exporting org-listings."
  1580. :tag "Org Export"
  1581. :group 'org)
  1582. (defgroup org-export-general nil
  1583. "General options for exporting Org-mode files."
  1584. :tag "Org Export General"
  1585. :group 'org-export)
  1586. (defcustom org-export-publishing-directory "."
  1587. "Path to the location where exported files should be located.
  1588. This path may be relative to the directory where the Org-mode file lives.
  1589. The default is to put them into the same directory as the Org-mode file.
  1590. The variable may also be an alist with export types `:html', `:ascii',
  1591. `:ical', or `:xoxo' and the corresponding directories. If a direcoty path
  1592. is relative, it is interpreted relative to the directory where the exported
  1593. Org-mode files lives."
  1594. :group 'org-export-general
  1595. :type '(choice
  1596. (directory)
  1597. (repeat
  1598. (cons
  1599. (choice :tag "Type"
  1600. (const :html) (const :ascii) (const :ical) (const :xoxo))
  1601. (directory)))))
  1602. (defcustom org-export-language-setup
  1603. '(("en" "Author" "Date" "Table of Contents")
  1604. ("cs" "Autor" "Datum" "Obsah")
  1605. ("da" "Ophavsmand" "Dato" "Indhold")
  1606. ("de" "Autor" "Datum" "Inhaltsverzeichnis")
  1607. ("es" "Autor" "Fecha" "\xccndice")
  1608. ("fr" "Auteur" "Date" "Table des Mati\xe8res")
  1609. ("it" "Autore" "Data" "Indice")
  1610. ("nl" "Auteur" "Datum" "Inhoudsopgave")
  1611. ("nn" "Forfattar" "Dato" "Innhold") ;; nn = Norsk (nynorsk)
  1612. ("sv" "F\xf6rfattarens" "Datum" "Inneh\xe5ll"))
  1613. "Terms used in export text, translated to different languages.
  1614. Use the variable `org-export-default-language' to set the language,
  1615. or use the +OPTION lines for a per-file setting."
  1616. :group 'org-export-general
  1617. :type '(repeat
  1618. (list
  1619. (string :tag "HTML language tag")
  1620. (string :tag "Author")
  1621. (string :tag "Date")
  1622. (string :tag "Table of Contents"))))
  1623. (defcustom org-export-default-language "en"
  1624. "The default language of HTML export, as a string.
  1625. This should have an association in `org-export-language-setup'."
  1626. :group 'org-export-general
  1627. :type 'string)
  1628. (defcustom org-export-headline-levels 3
  1629. "The last level which is still exported as a headline.
  1630. Inferior levels will produce itemize lists when exported.
  1631. Note that a numeric prefix argument to an exporter function overrides
  1632. this setting.
  1633. This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
  1634. :group 'org-export-general
  1635. :type 'number)
  1636. (defcustom org-export-with-section-numbers t
  1637. "Non-nil means, add section numbers to headlines when exporting.
  1638. This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
  1639. :group 'org-export-general
  1640. :type 'boolean)
  1641. (defcustom org-export-with-toc t
  1642. "Non-nil means, create a table of contents in exported files.
  1643. The TOC contains headlines with levels up to`org-export-headline-levels'.
  1644. Headlines which contain any TODO items will be marked with \"(*)\" in
  1645. ASCII export, and with red color in HTML output.
  1646. In HTML output, the TOC will be clickable.
  1647. This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"."
  1648. :group 'org-export-general
  1649. :type 'boolean)
  1650. (defcustom org-export-mark-todo-in-toc nil
  1651. "Non-nil means, mark TOC lines that contain any open TODO items."
  1652. :group 'org-export-general
  1653. :type 'boolean)
  1654. (defcustom org-export-preserve-breaks nil
  1655. "Non-nil means, preserve all line breaks when exporting.
  1656. Normally, in HTML output paragraphs will be reformatted. In ASCII
  1657. export, line breaks will always be preserved, regardless of this variable.
  1658. This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
  1659. :group 'org-export-general
  1660. :type 'boolean)
  1661. (defcustom org-export-with-archived-trees 'headline
  1662. "Whether subtrees with the ARCHIVE tag should be exported.
  1663. This can have three different values
  1664. nil Do not export, pretend this tree is not present
  1665. t Do export the entire tree
  1666. headline Only export the headline, but skip the tree below it."
  1667. :group 'org-export-general
  1668. :group 'org-archive
  1669. :type '(choice
  1670. (const :tag "not at all" nil)
  1671. (const :tag "headline only" 'headline)
  1672. (const :tag "entirely" t)))
  1673. (defcustom org-export-with-timestamps t
  1674. "Nil means, do not export time stamps and associated keywords."
  1675. :group 'org-export-general
  1676. :type 'boolean)
  1677. (defcustom org-export-remove-timestamps-from-toc t
  1678. "Nil means, remove timestamps from the table of contents entries."
  1679. :group 'org-export-general
  1680. :type 'boolean)
  1681. (defcustom org-export-with-tags 'not-in-toc
  1682. "Nil means, do not export tags, just remove them from headlines.
  1683. If this is the sysmbol `not-in-toc', tags will be removed from table of
  1684. contents entries, but still be shown in the headlines of the document."
  1685. :group 'org-export-general
  1686. :type '(choice
  1687. (const :tag "Off" nil)
  1688. (const :tag "Not in TOC" not-in-toc)
  1689. (const :tag "On" t)))
  1690. (defgroup org-export-translation nil
  1691. "Options for translating special ascii sequences for the export backends."
  1692. :tag "Org Export Translation"
  1693. :group 'org-export)
  1694. (defcustom org-export-with-emphasize t
  1695. "Non-nil means, interpret *word*, /word/, and _word_ as emphasized text.
  1696. If the export target supports emphasizing text, the word will be
  1697. typeset in bold, italic, or underlined, respectively. Works only for
  1698. single words, but you can say: I *really* *mean* *this*.
  1699. Not all export backends support this.
  1700. This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
  1701. :group 'org-export-translation
  1702. :type 'boolean)
  1703. (defcustom org-export-with-sub-superscripts t
  1704. "Non-nil means, interpret \"_\" and \"^\" for export.
  1705. When this option is turned on, you can use TeX-like syntax for sub- and
  1706. superscripts. Several characters after \"_\" or \"^\" will be
  1707. considered as a single item - so grouping with {} is normally not
  1708. needed. For example, the following things will be parsed as single
  1709. sub- or superscripts.
  1710. 10^24 or 10^tau several digits will be considered 1 item.
  1711. 10^-12 or 10^-tau a leading sign with digits or a word
  1712. x^2-y^3 will be read as x^2 - y^3, because items are
  1713. terminated by almost any nonword/nondigit char.
  1714. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  1715. Still, ambiguity is possible - so when in doubt use {} to enclose the
  1716. sub/superscript.
  1717. Not all export backends support this, but HTML does.
  1718. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  1719. :group 'org-export-translation
  1720. :type 'boolean)
  1721. (defcustom org-export-with-TeX-macros t
  1722. "Non-nil means, interpret simple TeX-like macros when exporting.
  1723. For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
  1724. No only real TeX macros will work here, but the standard HTML entities
  1725. for math can be used as macro names as well. For a list of supported
  1726. names in HTML export, see the constant `org-html-entities'.
  1727. Not all export backends support this.
  1728. This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
  1729. :group 'org-export-translation
  1730. :group 'org-latex
  1731. :type 'boolean)
  1732. (defcustom org-export-with-LaTeX-fragments nil
  1733. "Non-nil means, convert LaTeX fragments to images when exporting to HTML.
  1734. When set, the exporter will find LaTeX environments if the \\begin line is
  1735. the first non-white thing on a line. It will also find the math delimiters
  1736. like $a=b$ and \\( a=b \\) for inline math, $$a=b$$ and \\[ a=b \\] for
  1737. display math.
  1738. This option can also be set with the +OPTIONS line, e.g. \"LaTeX:t\"."
  1739. :group 'org-export-translation
  1740. :group 'org-latex
  1741. :type 'boolean)
  1742. (defcustom org-export-with-fixed-width t
  1743. "Non-nil means, lines starting with \":\" will be in fixed width font.
  1744. This can be used to have pre-formatted text, fragments of code etc. For
  1745. example:
  1746. : ;; Some Lisp examples
  1747. : (while (defc cnt)
  1748. : (ding))
  1749. will be looking just like this in also HTML. See also the QUOTE keyword.
  1750. Not all export backends support this.
  1751. This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
  1752. :group 'org-export-translation
  1753. :type 'boolean)
  1754. (defcustom org-match-sexp-depth 3
  1755. "Number of stacked braces for sub/superscript matching.
  1756. This has to be set before loading org.el to be effective."
  1757. :group 'org-export-translation
  1758. :type 'integer)
  1759. (defgroup org-export-tables nil
  1760. "Options for exporting tables in Org-mode."
  1761. :tag "Org Export Tables"
  1762. :group 'org-export)
  1763. (defcustom org-export-with-tables t
  1764. "If non-nil, lines starting with \"|\" define a table.
  1765. For example:
  1766. | Name | Address | Birthday |
  1767. |-------------+----------+-----------|
  1768. | Arthur Dent | England | 29.2.2100 |
  1769. Not all export backends support this.
  1770. This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
  1771. :group 'org-export-tables
  1772. :type 'boolean)
  1773. (defcustom org-export-highlight-first-table-line t
  1774. "Non-nil means, highlight the first table line.
  1775. In HTML export, this means use <th> instead of <td>.
  1776. In tables created with table.el, this applies to the first table line.
  1777. In Org-mode tables, all lines before the first horizontal separator
  1778. line will be formatted with <th> tags."
  1779. :group 'org-export-tables
  1780. :type 'boolean)
  1781. (defcustom org-export-table-remove-special-lines t
  1782. "Remove special lines and marking characters in calculating tables.
  1783. This removes the special marking character column from tables that are set
  1784. up for spreadsheet calculations. It also removes the entire lines
  1785. marked with `!', `_', or `^'. The lines with `$' are kept, because
  1786. the values of constants may be useful to have."
  1787. :group 'org-export-tables
  1788. :type 'boolean)
  1789. (defcustom org-export-prefer-native-exporter-for-tables nil
  1790. "Non-nil means, always export tables created with table.el natively.
  1791. Natively means, use the HTML code generator in table.el.
  1792. When nil, Org-mode's own HTML generator is used when possible (i.e. if
  1793. the table does not use row- or column-spanning). This has the
  1794. advantage, that the automatic HTML conversions for math symbols and
  1795. sub/superscripts can be applied. Org-mode's HTML generator is also
  1796. much faster."
  1797. :group 'org-export-tables
  1798. :type 'boolean)
  1799. (defgroup org-export-ascii nil
  1800. "Options specific for ASCII export of Org-mode files."
  1801. :tag "Org Export ASCII"
  1802. :group 'org-export)
  1803. (defcustom org-export-ascii-underline '(?\$ ?\# ?^ ?\~ ?\= ?\-)
  1804. "Characters for underlining headings in ASCII export.
  1805. In the given sequence, these characters will be used for level 1, 2, ..."
  1806. :group 'org-export-ascii
  1807. :type '(repeat character))
  1808. (defcustom org-export-ascii-bullets '(?* ?+ ?-)
  1809. "Bullet characters for headlines converted to lists in ASCII export.
  1810. The first character is is used for the first lest level generated in this
  1811. way, and so on. If there are more levels than characters given here,
  1812. the list will be repeated.
  1813. Note that plain lists will keep the same bullets as the have in the
  1814. Org-mode file."
  1815. :group 'org-export-ascii
  1816. :type '(repeat character))
  1817. (defcustom org-export-ascii-show-new-buffer t
  1818. "Non-nil means, popup buffer containing the exported ASCII text.
  1819. Otherwise the buffer will just be saved to a file and stay hidden."
  1820. :group 'org-export-ascii
  1821. :type 'boolean)
  1822. (defgroup org-export-xml nil
  1823. "Options specific for XML export of Org-mode files."
  1824. :tag "Org Export XML"
  1825. :group 'org-export)
  1826. (defgroup org-export-html nil
  1827. "Options specific for HTML export of Org-mode files."
  1828. :tag "Org Export HTML"
  1829. :group 'org-export)
  1830. (defcustom org-export-html-style
  1831. "<style type=\"text/css\">
  1832. html {
  1833. font-family: Times, serif;
  1834. font-size: 12pt;
  1835. }
  1836. .title { text-align: center; }
  1837. .todo { color: red; }
  1838. .done { color: green; }
  1839. .timestamp { color: grey }
  1840. .timestamp-kwd { color: CadetBlue }
  1841. .tag { background-color:lightblue; font-weight:normal }
  1842. .target { background-color: lavender; }
  1843. pre {
  1844. border: 1pt solid #AEBDCC;
  1845. background-color: #F3F5F7;
  1846. padding: 5pt;
  1847. font-family: courier, monospace;
  1848. }
  1849. table { border-collapse: collapse; }
  1850. td, th {
  1851. vertical-align: top;
  1852. border: 1pt solid #ADB9CC;
  1853. }
  1854. </style>"
  1855. "The default style specification for exported HTML files.
  1856. Since there are different ways of setting style information, this variable
  1857. needs to contain the full HTML structure to provide a style, including the
  1858. surrounding HTML tags. The style specifications should include definitions
  1859. for new classes todo, done, title, and deadline. For example, legal values
  1860. would be:
  1861. <style type=\"text/css\">
  1862. p { font-weight: normal; color: gray; }
  1863. h1 { color: black; }
  1864. .title { text-align: center; }
  1865. .todo, .deadline { color: red; }
  1866. .done { color: green; }
  1867. </style>
  1868. or, if you want to keep the style in a file,
  1869. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  1870. As the value of this option simply gets inserted into the HTML <head> header,
  1871. you can \"misuse\" it to add arbitrary text to the header."
  1872. :group 'org-export-html
  1873. :type 'string)
  1874. (defcustom org-export-html-title-format "<h1 class=\"title\">%s</h1>\n"
  1875. "Format for typesetting the document title in HTML export."
  1876. :group 'org-export-html
  1877. :type 'string)
  1878. (defcustom org-export-html-toplevel-hlevel 2
  1879. "The <H> level for level 1 headings in HTML export."
  1880. :group 'org-export-html
  1881. :type 'string)
  1882. (defcustom org-export-html-link-org-files-as-html t
  1883. "Non-nil means, make file links to `file.org' point to `file.html'.
  1884. When org-mode is exporting an org-mode file to HTML, links to
  1885. non-html files are directly put into a href tag in HTML.
  1886. However, links to other Org-mode files (recognized by the
  1887. extension `.org.) should become links to the corresponding html
  1888. file, assuming that the linked org-mode file will also be
  1889. converted to HTML.
  1890. When nil, the links still point to the plain `.org' file."
  1891. :group 'org-export-html
  1892. :type 'boolean)
  1893. (defcustom org-export-html-inline-images 'maybe
  1894. "Non-nil means, inline images into exported HTML pages.
  1895. This is done using an <img> tag. When nil, an anchor with href is used to
  1896. link to the image. If this option is `maybe', then images in links with
  1897. an empty description will be inlined, while images with a description will
  1898. be linked only."
  1899. :group 'org-export-html
  1900. :type '(choice (const :tag "Never" nil)
  1901. (const :tag "Always" t)
  1902. (const :tag "When there is no description" maybe)))
  1903. (defcustom org-export-html-expand t
  1904. "Non-nil means, for HTML export, treat @<...> as HTML tag.
  1905. When nil, these tags will be exported as plain text and therefore
  1906. not be interpreted by a browser.
  1907. This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
  1908. :group 'org-export-html
  1909. :type 'boolean)
  1910. (defcustom org-export-html-table-tag
  1911. "<table border=\"1\" cellspacing=\"0\" cellpadding=\"6\">"
  1912. "The HTML tag used to start a table.
  1913. This must be a <table> tag, but you may change the options like
  1914. borders and spacing."
  1915. :group 'org-export-html
  1916. :type 'string)
  1917. (defcustom org-export-html-with-timestamp nil
  1918. "If non-nil, write `org-export-html-html-helper-timestamp'
  1919. into the exported HTML text. Otherwise, the buffer will just be saved
  1920. to a file."
  1921. :group 'org-export-html
  1922. :type 'boolean)
  1923. (defcustom org-export-html-html-helper-timestamp
  1924. "<br/><br/><hr><p><!-- hhmts start --> <!-- hhmts end --></p>\n"
  1925. "The HTML tag used as timestamp delimiter for HTML-helper-mode."
  1926. :group 'org-export-html
  1927. :type 'string)
  1928. (defcustom org-export-html-show-new-buffer nil
  1929. "Non-nil means, popup buffer containing the exported html text.
  1930. Otherwise, the buffer will just be saved to a file and stay hidden."
  1931. :group 'org-export-html
  1932. :type 'boolean)
  1933. (defgroup org-export-icalendar nil
  1934. "Options specific for iCalendar export of Org-mode files."
  1935. :tag "Org Export iCalendar"
  1936. :group 'org-export)
  1937. (defcustom org-combined-agenda-icalendar-file "~/org.ics"
  1938. "The file name for the iCalendar file covering all agenda files.
  1939. This file is created with the command \\[org-export-icalendar-all-agenda-files].
  1940. The file name should be absolute."
  1941. :group 'org-export-icalendar
  1942. :type 'file)
  1943. (defcustom org-icalendar-include-todo nil
  1944. "Non-nil means, export to iCalendar files should also cover TODO items."
  1945. :group 'org-export-icalendar
  1946. :type 'boolean)
  1947. (defcustom org-icalendar-combined-name "OrgMode"
  1948. "Calendar name for the combined iCalendar representing all agenda files."
  1949. :group 'org-export-icalendar
  1950. :type 'string)
  1951. (defgroup org-font-lock nil
  1952. "Font-lock settings for highlighting in Org-mode."
  1953. :tag "Org Font Lock"
  1954. :group 'org)
  1955. (defcustom org-level-color-stars-only nil
  1956. "Non-nil means fontify only the stars in each headline.
  1957. When nil, the entire headline is fontified.
  1958. Changing it requires restart of `font-lock-mode' to become effective
  1959. also in regions already fontified."
  1960. :group 'org-font-lock
  1961. :type 'boolean)
  1962. (defcustom org-hide-leading-stars nil
  1963. "Non-nil means, hide the first N-1 stars in a headline.
  1964. This works by using the face `org-hide' for these stars. This
  1965. face is white for a light background, and black for a dark
  1966. background. You may have to customize the face `org-hide' to
  1967. make this work.
  1968. Changing it requires restart of `font-lock-mode' to become effective
  1969. also in regions already fontified.
  1970. You may also set this on a per-file basis by adding one of the following
  1971. lines to the buffer:
  1972. #+STARTUP: hidestars
  1973. #+STARTUP: showstars"
  1974. :group 'org-font-lock
  1975. :type 'boolean)
  1976. (defcustom org-fontify-done-headline nil
  1977. "Non-nil means, change the face of a headline if it is marked DONE.
  1978. Normally, only the TODO/DONE keyword indicates the state of a headline.
  1979. When this is non-nil, the headline after the keyword is set to the
  1980. `org-headline-done' as an additional indication."
  1981. :group 'org-font-lock
  1982. :type 'boolean)
  1983. (defcustom org-fontify-emphasized-text t
  1984. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  1985. Changing this variable requires a restart of Emacs to take effect."
  1986. :group 'org-font-lock
  1987. :type 'boolean)
  1988. (defvar org-emph-re nil
  1989. "Regular expression for matching emphasis.")
  1990. (defvar org-emphasis-regexp-components) ; defined just below
  1991. (defvar org-emphasis-alist) ; defined just below
  1992. (defun org-set-emph-re (var val)
  1993. "Set variable and compute the emphasis regular expression."
  1994. (set var val)
  1995. (when (and (boundp 'org-emphasis-alist)
  1996. (boundp 'org-emphasis-regexp-components)
  1997. org-emphasis-alist org-emphasis-regexp-components)
  1998. (let* ((e org-emphasis-regexp-components)
  1999. (pre (car e))
  2000. (post (nth 1 e))
  2001. (border (nth 2 e))
  2002. (body (nth 3 e))
  2003. (nl (nth 4 e))
  2004. (stacked (nth 5 e))
  2005. (body1 (concat body "*?"))
  2006. (markers (mapconcat 'car org-emphasis-alist "")))
  2007. ;; make sure special characters appear at the right position in the class
  2008. (if (string-match "\\^" markers)
  2009. (setq markers (concat (replace-match "" t t markers) "^")))
  2010. (if (string-match "-" markers)
  2011. (setq markers (concat (replace-match "" t t markers) "-")))
  2012. (while (>= (setq nl (1- nl)) 0) (setq body1 (concat body1 "\n?" body "*?")))
  2013. ;; Make the regexp
  2014. (setq org-emph-re
  2015. (concat "\\([" pre (if stacked markers) "]\\|^\\)"
  2016. "\\("
  2017. "\\([" markers "]\\)"
  2018. "\\("
  2019. (if stacked (concat "[^" border markers "]")) ; FIXME: correct?
  2020. body1
  2021. (if stacked (concat "[^" border markers "]")) ; FIXME: correct?
  2022. "\\)"
  2023. "\\3\\)"
  2024. "\\([" post (if stacked markers) "]\\|$\\)")))))
  2025. (defcustom org-emphasis-regexp-components
  2026. '(" \t(" " \t.,?;:'\")" " \t\r\n,." "." 1 nil)
  2027. "Components used to build the reqular expression for emphasis.
  2028. This is a list with 6 entries. Terminology: In an emphasis string
  2029. like \" *strong word* \", we call the initial space PREMATCH, the final
  2030. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2031. and \"trong wor\" is the body. The different components in this variable
  2032. specify what is allowed/forbidden in each part:
  2033. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2034. post Chars allowed as postmatch. End of line will be allowed too.
  2035. border The chars *forbidden* as border characters. In addition to the
  2036. characters given here, all marker characters are forbidden too.
  2037. body-regexp A regexp like \".\" to match a body character. Don't use
  2038. non-shy groups here, and don't allow newline here.
  2039. newline The maximum number of newlines allowed in an emphasis exp.
  2040. stacked Non-nil means, allow stacked styles. This works only in HTML
  2041. export. When this is set, all marker characters (as given in
  2042. `org-emphasis-alist') will be allowed as pre/post, aiding
  2043. inside-out matching.
  2044. Use customize to modify this, or restart emacs after changing it."
  2045. :group 'org-font-lock
  2046. :set 'org-set-emph-re
  2047. :type '(list
  2048. (sexp :tag "Allowed chars in pre ")
  2049. (sexp :tag "Allowed chars in post ")
  2050. (sexp :tag "Forbidden chars in border ")
  2051. (sexp :tag "Regexp for body ")
  2052. (integer :tag "number of newlines allowed")
  2053. (boolean :tag "Stacking allowed ")))
  2054. (defcustom org-emphasis-alist
  2055. '(("*" bold "<b>" "</b>")
  2056. ("/" italic "<i>" "</i>")
  2057. ("_" underline "<u>" "</u>")
  2058. ("=" shadow "<code>" "</code>")
  2059. ("+" (:strike-through t) "<del>" "</del>")
  2060. )
  2061. "Special syntax for emphasised text.
  2062. Text starting and ending with a special character will be emphasized, for
  2063. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2064. characters, the face to bbe used by font-lock for highlighting in Org-mode
  2065. emacs buffers, and the HTML tags to be used for this.
  2066. Use customize to modify this, or restart emacs after changing it."
  2067. :group 'org-font-lock
  2068. :set 'org-set-emph-re
  2069. :type '(repeat
  2070. (list
  2071. (string :tag "Marker character")
  2072. (choice
  2073. (face :tag "Font-lock-face")
  2074. (plist :tag "Face property list"))
  2075. (string :tag "HTML start tag")
  2076. (string :tag "HTML end tag"))))
  2077. (defgroup org-faces nil
  2078. "Faces in Org-mode."
  2079. :tag "Org Faces"
  2080. :group 'org-font-lock)
  2081. (defun org-compatible-face (specs)
  2082. "Make a compatible face specification.
  2083. XEmacs and Emacs 21 do not know about the `min-colors' attribute.
  2084. For them we convert a (min-colors 8) entry to a `tty' entry and move it
  2085. to the top of the list. The `min-colors' attribute will be removed from
  2086. any other entries, and any resulting duplicates will be removed entirely."
  2087. (if (or (featurep 'xemacs) (< emacs-major-version 22))
  2088. (let (r e a)
  2089. (while (setq e (pop specs))
  2090. (cond
  2091. ((memq (car e) '(t default)) (push e r))
  2092. ((setq a (member '(min-colors 8) (car e)))
  2093. (nconc r (list (cons (cons '(type tty) (delq (car a) (car e)))
  2094. (cdr e)))))
  2095. ((setq a (assq 'min-colors (car e)))
  2096. (setq e (cons (delq a (car e)) (cdr e)))
  2097. (or (assoc (car e) r) (push e r)))
  2098. (t (or (assoc (car e) r) (push e r)))))
  2099. (nreverse r))
  2100. specs))
  2101. (defface org-hide
  2102. '((((background light)) (:foreground "white"))
  2103. (((background dark)) (:foreground "black")))
  2104. "Face used to hide leading stars in headlines.
  2105. The forground color of this face should be equal to the background
  2106. color of the frame."
  2107. :group 'org-faces)
  2108. (defface org-level-1 ;; font-lock-function-name-face
  2109. (org-compatible-face
  2110. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2111. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2112. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2113. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2114. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  2115. (t (:bold t))))
  2116. "Face used for level 1 headlines."
  2117. :group 'org-faces)
  2118. (defface org-level-2 ;; font-lock-variable-name-face
  2119. (org-compatible-face
  2120. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  2121. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  2122. (((class color) (min-colors 8) (background light)) (:foreground "yellow"))
  2123. (((class color) (min-colors 8) (background dark)) (:foreground "yellow" :bold t))
  2124. (t (:bold t))))
  2125. "Face used for level 2 headlines."
  2126. :group 'org-faces)
  2127. (defface org-level-3 ;; font-lock-keyword-face
  2128. (org-compatible-face
  2129. '((((class color) (min-colors 88) (background light)) (:foreground "Purple"))
  2130. (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
  2131. (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
  2132. (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
  2133. (((class color) (min-colors 8) (background light)) (:foreground "purple" :bold t))
  2134. (((class color) (min-colors 8) (background dark)) (:foreground "cyan" :bold t))
  2135. (t (:bold t))))
  2136. "Face used for level 3 headlines."
  2137. :group 'org-faces)
  2138. (defface org-level-4 ;; font-lock-comment-face
  2139. (org-compatible-face
  2140. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2141. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2142. (((class color) (min-colors 16) (background light)) (:foreground "red"))
  2143. (((class color) (min-colors 16) (background dark)) (:foreground "red1"))
  2144. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2145. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2146. (t (:bold t))))
  2147. "Face used for level 4 headlines."
  2148. :group 'org-faces)
  2149. (defface org-level-5 ;; font-lock-type-face
  2150. (org-compatible-face
  2151. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
  2152. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
  2153. (((class color) (min-colors 8)) (:foreground "green"))))
  2154. "Face used for level 5 headlines."
  2155. :group 'org-faces)
  2156. (defface org-level-6 ;; font-lock-constant-face
  2157. (org-compatible-face
  2158. '((((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
  2159. (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
  2160. (((class color) (min-colors 8)) (:foreground "magenta"))))
  2161. "Face used for level 6 headlines."
  2162. :group 'org-faces)
  2163. (defface org-level-7 ;; font-lock-builtin-face
  2164. (org-compatible-face
  2165. '((((class color) (min-colors 16) (background light)) (:foreground "Orchid"))
  2166. (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue"))
  2167. (((class color) (min-colors 8)) (:foreground "blue"))))
  2168. "Face used for level 7 headlines."
  2169. :group 'org-faces)
  2170. (defface org-level-8 ;; font-lock-string-face
  2171. (org-compatible-face
  2172. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2173. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2174. (((class color) (min-colors 8)) (:foreground "green"))))
  2175. "Face used for level 8 headlines."
  2176. :group 'org-faces)
  2177. (defface org-special-keyword ;; font-lock-string-face
  2178. (org-compatible-face
  2179. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2180. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2181. (t (:italic t))))
  2182. "Face used for special keywords."
  2183. :group 'org-faces)
  2184. (defface org-warning ;; font-lock-warning-face
  2185. (org-compatible-face
  2186. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  2187. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  2188. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2189. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2190. (t (:bold t))))
  2191. "Face for deadlines and TODO keywords."
  2192. :group 'org-faces)
  2193. (defface org-headline-done ;; font-lock-string-face
  2194. (org-compatible-face
  2195. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2196. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2197. (((class color) (min-colors 8) (background light)) (:bold nil))))
  2198. "Face used to indicate that a headline is DONE.
  2199. This face is only used if `org-fontify-done-headline' is set."
  2200. :group 'org-faces)
  2201. (defface org-archived ; similar to shadow
  2202. (org-compatible-face
  2203. '((((class color grayscale) (min-colors 88) (background light))
  2204. (:foreground "grey50"))
  2205. (((class color grayscale) (min-colors 88) (background dark))
  2206. (:foreground "grey70"))
  2207. (((class color) (min-colors 8) (background light))
  2208. (:foreground "green"))
  2209. (((class color) (min-colors 8) (background dark))
  2210. (:foreground "yellow"))))
  2211. "Face for headline with the ARCHIVE tag."
  2212. :group 'org-faces)
  2213. (defface org-link
  2214. '((((class color) (background light)) (:foreground "Purple" :underline t))
  2215. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  2216. (t (:underline t)))
  2217. "Face for links."
  2218. :group 'org-faces)
  2219. (defface org-date
  2220. '((((class color) (background light)) (:foreground "Purple" :underline t))
  2221. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  2222. (t (:underline t)))
  2223. "Face for links."
  2224. :group 'org-faces)
  2225. (defface org-tag
  2226. '((t (:bold t)))
  2227. "Face for tags."
  2228. :group 'org-faces)
  2229. (defface org-todo ;; font-lock-warning-face
  2230. (org-compatible-face
  2231. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  2232. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  2233. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2234. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2235. (t (:inverse-video t :bold t))))
  2236. "Face for TODO keywords."
  2237. :group 'org-faces)
  2238. (defface org-done ;; font-lock-type-face
  2239. (org-compatible-face
  2240. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen" :bold t))
  2241. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen" :bold t))
  2242. (((class color) (min-colors 8)) (:foreground "green"))
  2243. (t (:bold t))))
  2244. "Face used for DONE."
  2245. :group 'org-faces)
  2246. (defface org-table ;; font-lock-function-name-face
  2247. (org-compatible-face
  2248. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2249. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2250. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2251. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2252. (((class color) (min-colors 8) (background light)) (:foreground "blue"))
  2253. (((class color) (min-colors 8) (background dark)))))
  2254. "Face used for tables."
  2255. :group 'org-faces)
  2256. (defface org-formula
  2257. (org-compatible-face
  2258. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2259. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2260. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2261. (((class color) (min-colors 8) (background dark)) (:foreground "red"))
  2262. (t (:bold t :italic t))))
  2263. "Face for formulas."
  2264. :group 'org-faces)
  2265. (defface org-scheduled-today
  2266. (org-compatible-face
  2267. '((((class color) (min-colors 88) (background light)) (:foreground "DarkGreen"))
  2268. (((class color) (min-colors 88) (background dark)) (:foreground "PaleGreen"))
  2269. (((class color) (min-colors 8)) (:foreground "green"))
  2270. (t (:bold t :italic t))))
  2271. "Face for items scheduled for a certain day."
  2272. :group 'org-faces)
  2273. (defface org-scheduled-previously
  2274. (org-compatible-face
  2275. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2276. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2277. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2278. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2279. (t (:bold t))))
  2280. "Face for items scheduled previously, and not yet done."
  2281. :group 'org-faces)
  2282. (defface org-upcoming-deadline
  2283. (org-compatible-face
  2284. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2285. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2286. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2287. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2288. (t (:bold t))))
  2289. "Face for items scheduled previously, and not yet done."
  2290. :group 'org-faces)
  2291. (defface org-time-grid ;; font-lock-variable-name-face
  2292. (org-compatible-face
  2293. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  2294. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  2295. (((class color) (min-colors 8)) (:foreground "yellow" :weight light))))
  2296. "Face used for time grids."
  2297. :group 'org-faces)
  2298. (defconst org-level-faces
  2299. '(org-level-1 org-level-2 org-level-3 org-level-4
  2300. org-level-5 org-level-6 org-level-7 org-level-8
  2301. ))
  2302. (defconst org-n-levels (length org-level-faces))
  2303. ;; Variables for pre-computed regular expressions, all buffer local
  2304. (defvar org-done-string nil
  2305. "The last string in `org-todo-keywords', indicating an item is DONE.")
  2306. (make-variable-buffer-local 'org-done-string)
  2307. (defvar org-todo-regexp nil
  2308. "Matches any of the TODO state keywords.")
  2309. (make-variable-buffer-local 'org-todo-regexp)
  2310. (defvar org-not-done-regexp nil
  2311. "Matches any of the TODO state keywords except the last one.")
  2312. (make-variable-buffer-local 'org-not-done-regexp)
  2313. (defvar org-todo-line-regexp nil
  2314. "Matches a headline and puts TODO state into group 2 if present.")
  2315. (make-variable-buffer-local 'org-todo-line-regexp)
  2316. (defvar org-todo-line-tags-regexp nil
  2317. "Matches a headline and puts TODO state into group 2 if present.
  2318. Also put tags into group 4 if tags are present.")
  2319. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2320. (defvar org-nl-done-regexp nil
  2321. "Matches newline followed by a headline with the DONE keyword.")
  2322. (make-variable-buffer-local 'org-nl-done-regexp)
  2323. (defvar org-looking-at-done-regexp nil
  2324. "Matches the DONE keyword a point.")
  2325. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2326. (defvar org-todo-kwd-priority-p nil
  2327. "Do TODO items have priorities?")
  2328. (make-variable-buffer-local 'org-todo-kwd-priority-p)
  2329. (defvar org-todo-kwd-max-priority nil
  2330. "Maximum priority of TODO items.")
  2331. (make-variable-buffer-local 'org-todo-kwd-max-priority)
  2332. (defvar org-ds-keyword-length 12
  2333. "Maximum length of the Deadline and SCHEDULED keywords.")
  2334. (make-variable-buffer-local 'org-ds-keyword-length)
  2335. (defvar org-deadline-regexp nil
  2336. "Matches the DEADLINE keyword.")
  2337. (make-variable-buffer-local 'org-deadline-regexp)
  2338. (defvar org-deadline-time-regexp nil
  2339. "Matches the DEADLINE keyword together with a time stamp.")
  2340. (make-variable-buffer-local 'org-deadline-time-regexp)
  2341. (defvar org-deadline-line-regexp nil
  2342. "Matches the DEADLINE keyword and the rest of the line.")
  2343. (make-variable-buffer-local 'org-deadline-line-regexp)
  2344. (defvar org-scheduled-regexp nil
  2345. "Matches the SCHEDULED keyword.")
  2346. (make-variable-buffer-local 'org-scheduled-regexp)
  2347. (defvar org-scheduled-time-regexp nil
  2348. "Matches the SCHEDULED keyword together with a time stamp.")
  2349. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2350. (defvar org-closed-time-regexp nil
  2351. "Matches the CLOSED keyword together with a time stamp.")
  2352. (make-variable-buffer-local 'org-closed-time-regexp)
  2353. (defvar org-keyword-time-regexp nil
  2354. "Matches any of the 3 keywords, together with the time stamp.")
  2355. (make-variable-buffer-local 'org-keyword-time-regexp)
  2356. (defvar org-maybe-keyword-time-regexp nil
  2357. "Matches a timestamp, possibly preceeded by a keyword.")
  2358. (make-variable-buffer-local 'org-keyword-time-regexp)
  2359. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  2360. mouse-map t)
  2361. "Properties to remove when a string without properties is wanted.")
  2362. (defsubst org-match-string-no-properties (num &optional string)
  2363. (if (featurep 'xemacs)
  2364. (let ((s (match-string num string)))
  2365. (remove-text-properties 0 (length s) org-rm-props s)
  2366. s)
  2367. (match-string-no-properties num string)))
  2368. (defsubst org-no-properties (s)
  2369. (remove-text-properties 0 (length s) org-rm-props s)
  2370. s)
  2371. (defsubst org-set-local (var value)
  2372. "Make VAR local in current buffer and set it to VALUE."
  2373. (set (make-variable-buffer-local var) value))
  2374. (defsubst org-mode-p ()
  2375. "Check if the current buffer is in Org-mode."
  2376. (eq major-mode 'org-mode))
  2377. (defsubst org-last (list)
  2378. "Return the last element of LIST."
  2379. (car (last list)))
  2380. (defun org-let (list &rest body)
  2381. (eval (cons 'let (cons list body))))
  2382. (put 'org-let 'lisp-indent-function 1)
  2383. (defun org-let2 (list1 list2 &rest body)
  2384. (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
  2385. (put 'org-let2 'lisp-indent-function 2)
  2386. (defconst org-startup-options
  2387. '(("fold" org-startup-folded t)
  2388. ("overview" org-startup-folded t)
  2389. ("nofold" org-startup-folded nil)
  2390. ("showall" org-startup-folded nil)
  2391. ("content" org-startup-folded content)
  2392. ("hidestars" org-hide-leading-stars t)
  2393. ("showstars" org-hide-leading-stars nil)
  2394. ("odd" org-odd-levels-only t)
  2395. ("oddeven" org-odd-levels-only nil)
  2396. ("align" org-startup-align-all-tables t)
  2397. ("noalign" org-startup-align-all-tables nil)
  2398. ("logging" org-log-done t)
  2399. ("nologging" org-log-done nil)
  2400. ("dlcheck" org-startup-with-deadline-check t)
  2401. ("nodlcheck" org-startup-with-deadline-check nil)))
  2402. (defun org-set-regexps-and-options ()
  2403. "Precompute regular expressions for current buffer."
  2404. (when (org-mode-p)
  2405. (let ((re (org-make-options-regexp
  2406. '("CATEGORY" "SEQ_TODO" "PRI_TODO" "TYP_TODO"
  2407. "STARTUP" "ARCHIVE" "TAGS" "CALC")))
  2408. (splitre "[ \t]+")
  2409. kwds int key value cat arch tags)
  2410. (save-excursion
  2411. (save-restriction
  2412. (widen)
  2413. (goto-char (point-min))
  2414. (while (re-search-forward re nil t)
  2415. (setq key (match-string 1) value (org-match-string-no-properties 2))
  2416. (cond
  2417. ((equal key "CATEGORY")
  2418. (if (string-match "[ \t]+$" value)
  2419. (setq value (replace-match "" t t value)))
  2420. (setq cat (intern value)))
  2421. ((equal key "SEQ_TODO")
  2422. (setq int 'sequence
  2423. kwds (append kwds (org-split-string value splitre))))
  2424. ((equal key "PRI_TODO")
  2425. (setq int 'priority
  2426. kwds (append kwds (org-split-string value splitre))))
  2427. ((equal key "TYP_TODO")
  2428. (setq int 'type
  2429. kwds (append kwds (org-split-string value splitre))))
  2430. ((equal key "TAGS")
  2431. (setq tags (append tags (org-split-string value splitre))))
  2432. ((equal key "STARTUP")
  2433. (let ((opts (org-split-string value splitre))
  2434. l var val)
  2435. (while (setq l (assoc (pop opts) org-startup-options))
  2436. (setq var (nth 1 l) val (nth 2 l))
  2437. (set (make-local-variable var) val))))
  2438. ((equal key "ARCHIVE")
  2439. (string-match " *$" value)
  2440. (setq arch (replace-match "" t t value))
  2441. (remove-text-properties 0 (length arch)
  2442. '(face t fontified t) arch)))
  2443. )))
  2444. (and cat (org-set-local 'org-category cat))
  2445. (and kwds (org-set-local 'org-todo-keywords kwds))
  2446. (and arch (org-set-local 'org-archive-location arch))
  2447. (and int (org-set-local 'org-todo-interpretation int))
  2448. (when tags
  2449. (let (e tgs)
  2450. (while (setq e (pop tags))
  2451. (cond
  2452. ((equal e "{") (push '(:startgroup) tgs))
  2453. ((equal e "}") (push '(:endgroup) tgs))
  2454. ((string-match "^\\([0-9a-zA-Z_@]+\\)(\\(.\\))$" e)
  2455. (push (cons (match-string 1 e)
  2456. (string-to-char (match-string 2 e)))
  2457. tgs))
  2458. (t (push (list e) tgs))))
  2459. (org-set-local 'org-tag-alist nil)
  2460. (while (setq e (pop tgs))
  2461. (or (and (stringp (car e))
  2462. (assoc (car e) org-tag-alist))
  2463. (push e org-tag-alist))))))
  2464. ;; Compute the regular expressions and other local variables
  2465. (setq org-todo-kwd-priority-p (equal org-todo-interpretation 'priority)
  2466. org-todo-kwd-max-priority (1- (length org-todo-keywords))
  2467. org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2468. (length org-scheduled-string)))
  2469. org-done-string
  2470. (nth (1- (length org-todo-keywords)) org-todo-keywords)
  2471. org-todo-regexp
  2472. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords
  2473. "\\|") "\\)\\>")
  2474. org-not-done-regexp
  2475. (concat "\\<\\("
  2476. (mapconcat 'regexp-quote
  2477. (nreverse (cdr (reverse org-todo-keywords)))
  2478. "\\|")
  2479. "\\)\\>")
  2480. org-todo-line-regexp
  2481. (concat "^\\(\\*+\\)[ \t]*\\(?:\\("
  2482. (mapconcat 'regexp-quote org-todo-keywords "\\|")
  2483. "\\)\\>\\)? *\\(.*\\)")
  2484. org-nl-done-regexp
  2485. (concat "[\r\n]\\*+[ \t]+" org-done-string "\\>")
  2486. org-todo-line-tags-regexp
  2487. (concat "^\\(\\*+\\)[ \t]*\\(?:\\("
  2488. (mapconcat 'regexp-quote org-todo-keywords "\\|")
  2489. "\\)\\>\\)? *\\(.*?\\([ \t]:[a-zA-Z0-9:_@]+:[ \t]*\\)?$\\)")
  2490. org-looking-at-done-regexp (concat "^" org-done-string "\\>")
  2491. org-deadline-regexp (concat "\\<" org-deadline-string)
  2492. org-deadline-time-regexp
  2493. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2494. org-deadline-line-regexp
  2495. (concat "\\<\\(" org-deadline-string "\\).*")
  2496. org-scheduled-regexp
  2497. (concat "\\<" org-scheduled-string)
  2498. org-scheduled-time-regexp
  2499. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2500. org-closed-time-regexp
  2501. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2502. org-keyword-time-regexp
  2503. (concat "\\<\\(" org-scheduled-string
  2504. "\\|" org-deadline-string
  2505. "\\|" org-closed-string
  2506. "\\|" org-clock-string "\\)"
  2507. " *[[<]\\([^]>]+\\)[]>]")
  2508. org-maybe-keyword-time-regexp
  2509. (concat "\\(\\<\\(" org-scheduled-string
  2510. "\\|" org-deadline-string
  2511. "\\|" org-closed-string
  2512. "\\|" org-clock-string "\\)\\)?"
  2513. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}[^]\r\n>]*?[]>]\\)"))
  2514. (org-set-font-lock-defaults)))
  2515. ;; Tell the compiler about dynamically scoped variables,
  2516. ;; and variables from other packages
  2517. (defvar calc-embedded-close-formula) ; defined by the calc package
  2518. (defvar calc-embedded-open-formula) ; defined by the calc package
  2519. (defvar font-lock-unfontify-region-function) ; defined by font-lock.el
  2520. (defvar zmacs-regions) ; XEmacs regions
  2521. (defvar original-date) ; dynamically scoped in calendar
  2522. (defvar org-old-auto-fill-inhibit-regexp) ; local variable used by `orgtbl-mode'
  2523. (defvar orgtbl-mode-menu) ; defined when orgtbl mode get initialized
  2524. (defvar org-html-entities) ; defined later in this file
  2525. (defvar org-goto-start-pos) ; dynamically scoped parameter
  2526. (defvar org-time-was-given) ; dynamically scoped parameter
  2527. (defvar org-ts-what) ; dynamically scoped parameter
  2528. (defvar org-current-export-file) ; dynamically scoped parameter
  2529. (defvar org-current-export-dir) ; dynamically scoped parameter
  2530. (defvar mark-active) ; Emacs only, not available in XEmacs.
  2531. (defvar timecnt) ; dynamically scoped parameter
  2532. (defvar levels-open) ; dynamically scoped parameter
  2533. (defvar entry) ; dynamically scoped parameter
  2534. (defvar state) ; dynamically scoped into `org-after-todo-state-change-hook'
  2535. (defvar date) ; dynamically scoped parameter
  2536. (defvar description) ; dynamically scoped parameter
  2537. (defvar ans1) ; dynamically scoped parameter
  2538. (defvar ans2) ; dynamically scoped parameter
  2539. (defvar starting-day) ; local variable
  2540. (defvar include-all-loc) ; local variable
  2541. (defvar vm-message-pointer) ; from vm
  2542. (defvar vm-folder-directory) ; from vm
  2543. (defvar gnus-other-frame-object) ; from gnus
  2544. (defvar wl-summary-buffer-elmo-folder) ; from wanderlust
  2545. (defvar wl-summary-buffer-folder-name) ; from wanderlust
  2546. (defvar gnus-group-name) ; from gnus
  2547. (defvar gnus-article-current) ; from gnus
  2548. (defvar w3m-current-url) ; from w3m
  2549. (defvar w3m-current-title) ; from w3m
  2550. (defvar mh-progs) ; from MH-E
  2551. (defvar mh-current-folder) ; from MH-E
  2552. (defvar mh-show-folder-buffer) ; from MH-E
  2553. (defvar mh-index-folder) ; from MH-E
  2554. (defvar mh-searcher) ; from MH-E
  2555. (defvar org-selected-point) ; dynamically scoped parameter
  2556. (defvar calendar-mode-map) ; from calendar.el
  2557. (defvar last-arg) ; local variable
  2558. (defvar remember-save-after-remembering) ; from remember.el
  2559. (defvar remember-data-file) ; from remember.el
  2560. (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
  2561. (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
  2562. (defvar orgtbl-mode) ; defined later in this file
  2563. (defvar Info-current-file) ; from info.el
  2564. (defvar Info-current-node) ; from info.el
  2565. (defvar texmathp-why) ; from texmathp.el
  2566. (defvar org-latex-regexps)
  2567. ;;; Define the mode
  2568. (defvar org-mode-map
  2569. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  2570. (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.")
  2571. (copy-keymap outline-mode-map))
  2572. "Keymap for Org-mode.")
  2573. (defvar org-struct-menu) ; defined later in this file
  2574. (defvar org-org-menu) ; defined later in this file
  2575. (defvar org-tbl-menu) ; defined later in this file
  2576. ;; We use a before-change function to check if a table might need
  2577. ;; an update.
  2578. (defvar org-table-may-need-update t
  2579. "Indicates that a table might need an update.
  2580. This variable is set by `org-before-change-function'.
  2581. `org-table-align' sets it back to nil.")
  2582. (defvar org-mode-hook nil)
  2583. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  2584. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  2585. ;;;###autoload
  2586. (define-derived-mode org-mode outline-mode "Org"
  2587. "Outline-based notes management and organizer, alias
  2588. \"Carsten's outline-mode for keeping track of everything.\"
  2589. Org-mode develops organizational tasks around a NOTES file which
  2590. contains information about projects as plain text. Org-mode is
  2591. implemented on top of outline-mode, which is ideal to keep the content
  2592. of large files well structured. It supports ToDo items, deadlines and
  2593. time stamps, which magically appear in the diary listing of the Emacs
  2594. calendar. Tables are easily created with a built-in table editor.
  2595. Plain text URL-like links connect to websites, emails (VM), Usenet
  2596. messages (Gnus), BBDB entries, and any files related to the project.
  2597. For printing and sharing of notes, an Org-mode file (or a part of it)
  2598. can be exported as a structured ASCII or HTML file.
  2599. The following commands are available:
  2600. \\{org-mode-map}"
  2601. ;; Get rid of Outline menus, they are not needed
  2602. ;; Need to do this here because define-derived-mode sets up
  2603. ;; the keymap so late.
  2604. (if (featurep 'xemacs)
  2605. (progn
  2606. ;; Assume this is Greg's port, it used easymenu
  2607. (easy-menu-remove outline-mode-menu-heading)
  2608. (easy-menu-remove outline-mode-menu-show)
  2609. (easy-menu-remove outline-mode-menu-hide))
  2610. (define-key org-mode-map [menu-bar headings] 'undefined)
  2611. (define-key org-mode-map [menu-bar hide] 'undefined)
  2612. (define-key org-mode-map [menu-bar show] 'undefined))
  2613. (easy-menu-add org-org-menu)
  2614. (easy-menu-add org-tbl-menu)
  2615. (org-install-agenda-files-menu)
  2616. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  2617. (org-add-to-invisibility-spec '(org-cwidth))
  2618. (when (featurep 'xemacs)
  2619. (org-set-local 'line-move-ignore-invisible t))
  2620. (setq outline-regexp "\\*+")
  2621. ;;(setq outline-regexp "\\(?:\\*+\\|[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\) \\)")
  2622. (setq outline-level 'org-outline-level)
  2623. (when (and org-ellipsis (stringp org-ellipsis))
  2624. (unless org-display-table
  2625. (setq org-display-table (make-display-table)))
  2626. (set-display-table-slot org-display-table
  2627. 4 (string-to-vector org-ellipsis))
  2628. (setq buffer-display-table org-display-table))
  2629. (org-set-regexps-and-options)
  2630. ;; Calc embedded
  2631. (org-set-local 'calc-embedded-open-mode "# ")
  2632. (modify-syntax-entry ?# "<")
  2633. (if org-startup-truncated (setq truncate-lines t))
  2634. (org-set-local 'font-lock-unfontify-region-function
  2635. 'org-unfontify-region)
  2636. ;; Activate before-change-function
  2637. (org-set-local 'org-table-may-need-update t)
  2638. (org-add-hook 'before-change-functions 'org-before-change-function nil
  2639. 'local)
  2640. ;; Check for running clock before killing a buffer
  2641. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  2642. ;; Paragraphs and auto-filling
  2643. (org-set-autofill-regexps)
  2644. (org-update-radio-target-regexp)
  2645. (if (and org-insert-mode-line-in-empty-file
  2646. (interactive-p)
  2647. (= (point-min) (point-max)))
  2648. (insert " -*- mode: org -*-\n\n"))
  2649. (unless org-inhibit-startup
  2650. (when org-startup-align-all-tables
  2651. (let ((bmp (buffer-modified-p)))
  2652. (org-table-map-tables 'org-table-align)
  2653. (set-buffer-modified-p bmp)))
  2654. (if org-startup-with-deadline-check
  2655. (call-interactively 'org-check-deadlines)
  2656. (cond
  2657. ((eq org-startup-folded t)
  2658. (org-cycle '(4)))
  2659. ((eq org-startup-folded 'content)
  2660. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  2661. (org-cycle '(4)) (org-cycle '(4))))))))
  2662. (defsubst org-call-with-arg (command arg)
  2663. "Call COMMAND interactively, but pretend prefix are was ARG."
  2664. (let ((current-prefix-arg arg)) (call-interactively command)))
  2665. (defsubst org-current-line (&optional pos)
  2666. (+ (if (bolp) 1 0) (count-lines (point-min) (or pos (point)))))
  2667. (defun org-current-time ()
  2668. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  2669. (if (> org-time-stamp-rounding-minutes 0)
  2670. (let ((r org-time-stamp-rounding-minutes)
  2671. (time (decode-time)))
  2672. (apply 'encode-time
  2673. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  2674. (nthcdr 2 time))))
  2675. (current-time)))
  2676. (defun org-add-props (string plist &rest props)
  2677. "Add text properties to entire string, from beginning to end.
  2678. PLIST may be a list of properties, PROPS are individual properties and values
  2679. that will be added to PLIST. Returns the string that was modified."
  2680. (add-text-properties
  2681. 0 (length string) (if props (append plist props) plist) string)
  2682. string)
  2683. (put 'org-add-props 'lisp-indent-function 2)
  2684. ;;; Font-Lock stuff
  2685. (defvar org-mouse-map (make-sparse-keymap))
  2686. (define-key org-mouse-map
  2687. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  2688. (define-key org-mouse-map
  2689. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  2690. (when org-mouse-1-follows-link
  2691. (define-key org-mouse-map [follow-link] 'mouse-face))
  2692. (when org-tab-follows-link
  2693. (define-key org-mouse-map [(tab)] 'org-open-at-point)
  2694. (define-key org-mouse-map "\C-i" 'org-open-at-point))
  2695. (when org-return-follows-link
  2696. (define-key org-mouse-map [(return)] 'org-open-at-point)
  2697. (define-key org-mouse-map "\C-m" 'org-open-at-point))
  2698. (require 'font-lock)
  2699. (defconst org-non-link-chars "]\t\n\r<>")
  2700. (defconst org-link-types '("https?" "ftp" "mailto" "file" "news" "bbdb" "vm"
  2701. "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  2702. (defconst org-link-re-with-space
  2703. (concat
  2704. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2705. "\\([^" org-non-link-chars " ]"
  2706. "[^" org-non-link-chars "]*"
  2707. "[^" org-non-link-chars " ]\\)>?")
  2708. "Matches a link with spaces, optional angular brackets around it.")
  2709. (defconst org-link-re-with-space2
  2710. (concat
  2711. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2712. "\\([^" org-non-link-chars " ]"
  2713. "[^]\t\n\r]*"
  2714. "[^" org-non-link-chars " ]\\)>?")
  2715. "Matches a link with spaces, optional angular brackets around it.")
  2716. (defconst org-angle-link-re
  2717. (concat
  2718. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2719. "\\([^" org-non-link-chars " ]"
  2720. "[^" org-non-link-chars "]*"
  2721. "\\)>")
  2722. "Matches link with angular brackets, spaces are allowed.")
  2723. (defconst org-plain-link-re
  2724. (concat
  2725. "\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2726. "\\([^]\t\n\r<>,;() ]+\\)")
  2727. "Matches plain link, without spaces.")
  2728. (defconst org-bracket-link-regexp
  2729. "\\[\\[\\([^]]+\\)\\]\\(\\[\\([^]]+\\)\\]\\)?\\]"
  2730. "Matches a link in double brackets.")
  2731. (defconst org-bracket-link-analytic-regexp
  2732. (concat
  2733. "\\[\\["
  2734. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  2735. "\\([^]]+\\)"
  2736. "\\]"
  2737. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  2738. "\\]"))
  2739. ; 1: http:
  2740. ; 2: http
  2741. ; 3: path
  2742. ; 4: [desc]
  2743. ; 5: desc
  2744. (defconst org-ts-lengths
  2745. (cons (length (format-time-string (car org-time-stamp-formats)))
  2746. (length (format-time-string (cdr org-time-stamp-formats))))
  2747. "This holds the lengths of the two different time formats.")
  2748. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}[^\r\n>]*?\\)>"
  2749. "Regular expression for fast time stamp matching.")
  2750. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}[^\r\n>]*?\\)[]>]"
  2751. "Regular expression for fast time stamp matching.")
  2752. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  2753. "Regular expression matching time strings for analysis.")
  2754. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 ">")
  2755. "Regular expression matching time stamps, with groups.")
  2756. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[]>]")
  2757. "Regular expression matching time stamps (also [..]), with groups.")
  2758. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  2759. "Regular expression matching a time stamp range.")
  2760. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  2761. org-ts-regexp "\\)?")
  2762. "Regular expression matching a time stamp or time stamp range.")
  2763. (defvar org-�§emph-face nil)
  2764. (defun org-do-emphasis-faces (limit)
  2765. "Run through the buffer and add overlays to links."
  2766. (if (re-search-forward org-emph-re limit t)
  2767. (progn
  2768. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  2769. 'face
  2770. (nth 1 (assoc (match-string 3)
  2771. org-emphasis-alist)))
  2772. (add-text-properties (match-beginning 2) (match-end 2)
  2773. '(font-lock-multiline t))
  2774. (backward-char 1)
  2775. t)))
  2776. (defun org-activate-plain-links (limit)
  2777. "Run through the buffer and add overlays to links."
  2778. (if (re-search-forward org-plain-link-re limit t)
  2779. (progn
  2780. (add-text-properties (match-beginning 0) (match-end 0)
  2781. (list 'mouse-face 'highlight
  2782. 'keymap org-mouse-map
  2783. ))
  2784. t)))
  2785. (defun org-activate-angle-links (limit)
  2786. "Run through the buffer and add overlays to links."
  2787. (if (re-search-forward org-angle-link-re limit t)
  2788. (progn
  2789. (add-text-properties (match-beginning 0) (match-end 0)
  2790. (list 'mouse-face 'highlight
  2791. 'keymap org-mouse-map
  2792. ))
  2793. t)))
  2794. (defun org-activate-bracket-links (limit)
  2795. "Run through the buffer and add overlays to bracketed links."
  2796. (if (re-search-forward org-bracket-link-regexp limit t)
  2797. (let* ((help (concat "LINK: "
  2798. (org-match-string-no-properties 1)))
  2799. ;; FIXME: above we should remove the escapes.
  2800. ;; but that requires another match, protecting match data,
  2801. ;; a lot of overhead for font-lock.
  2802. (ip (list 'invisible 'org-link 'intangible t 'rear-nonsticky t
  2803. 'keymap org-mouse-map 'mouse-face 'highlight
  2804. 'help-echo help))
  2805. (vp (list 'rear-nonsticky t
  2806. 'keymap org-mouse-map 'mouse-face 'highlight
  2807. 'help-echo help)))
  2808. ;; We need to remove the invisible property here. Table narrowing
  2809. ;; may have made some of this invisible.
  2810. (remove-text-properties (match-beginning 0) (match-end 0)
  2811. '(invisible nil))
  2812. (if (match-end 3)
  2813. (progn
  2814. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  2815. (add-text-properties (match-beginning 3) (match-end 3) vp)
  2816. (add-text-properties (match-end 3) (match-end 0) ip))
  2817. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  2818. (add-text-properties (match-beginning 1) (match-end 1) vp)
  2819. (add-text-properties (match-end 1) (match-end 0) ip))
  2820. t)))
  2821. (defun org-activate-dates (limit)
  2822. "Run through the buffer and add overlays to dates."
  2823. (if (re-search-forward org-tsr-regexp limit t)
  2824. (progn
  2825. (add-text-properties (match-beginning 0) (match-end 0)
  2826. (list 'mouse-face 'highlight
  2827. 'keymap org-mouse-map))
  2828. t)))
  2829. (defvar org-target-link-regexp nil
  2830. "Regular expression matching radio targets in plain text.")
  2831. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  2832. "Regular expression matching a link target.")
  2833. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  2834. "Regular expression matching a link target.")
  2835. (defun org-activate-target-links (limit)
  2836. "Run through the buffer and add overlays to target matches."
  2837. (when org-target-link-regexp
  2838. (let ((case-fold-search t))
  2839. (if (re-search-forward org-target-link-regexp limit t)
  2840. (progn
  2841. (add-text-properties (match-beginning 0) (match-end 0)
  2842. (list 'mouse-face 'highlight
  2843. 'keymap org-mouse-map
  2844. 'help-echo "Radio target link"
  2845. 'org-linked-text t))
  2846. t)))))
  2847. (defun org-update-radio-target-regexp ()
  2848. "Find all radio targets in this file and update the regular expression."
  2849. (interactive)
  2850. (when (memq 'radio org-activate-links)
  2851. (setq org-target-link-regexp
  2852. (org-make-target-link-regexp (org-all-targets 'radio)))
  2853. (org-restart-font-lock)))
  2854. (defun org-hide-wide-columns (limit)
  2855. (let (s e)
  2856. (setq s (text-property-any (point) (or limit (point-max))
  2857. 'org-cwidth t))
  2858. (when s
  2859. (setq e (next-single-property-change s 'org-cwidth))
  2860. (add-text-properties s e '(invisible org-cwidth intangible t))
  2861. (goto-char e)
  2862. t)))
  2863. (defun org-restart-font-lock ()
  2864. "Restart font-lock-mode, to force refontification."
  2865. (when (and (boundp 'font-lock-mode) font-lock-mode)
  2866. (font-lock-mode -1)
  2867. (font-lock-mode 1)))
  2868. (defun org-all-targets (&optional radio)
  2869. "Return a list of all targets in this file.
  2870. With optional argument RADIO, only find radio targets."
  2871. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  2872. rtn)
  2873. (save-excursion
  2874. (goto-char (point-min))
  2875. (while (re-search-forward re nil t)
  2876. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  2877. rtn)))
  2878. (defun org-make-target-link-regexp (targets)
  2879. "Make regular expression matching all strings in TARGETS.
  2880. The regular expression finds the targets also if there is a line break
  2881. between words."
  2882. (and targets
  2883. (concat
  2884. "\\<\\("
  2885. (mapconcat
  2886. (lambda (x)
  2887. (while (string-match " +" x)
  2888. (setq x (replace-match "\\s-+" t t x)))
  2889. x)
  2890. targets
  2891. "\\|")
  2892. "\\)\\>")))
  2893. (defvar org-camel-regexp "\\*?\\<[A-Z]+[a-z]+[A-Z][a-zA-Z]*\\>"
  2894. "Matches CamelCase words, possibly with a star before it.")
  2895. (defun org-activate-camels (limit)
  2896. "Run through the buffer and add overlays to dates."
  2897. (if (re-search-forward org-camel-regexp limit t)
  2898. (progn
  2899. (add-text-properties (match-beginning 0) (match-end 0)
  2900. (list 'mouse-face 'highlight
  2901. 'keymap org-mouse-map))
  2902. t)))
  2903. (defun org-activate-tags (limit)
  2904. (if (re-search-forward "[ \t]\\(:[A-Za-z_@0-9:]+:\\)[ \r\n]" limit t)
  2905. (progn
  2906. (add-text-properties (match-beginning 1) (match-end 1)
  2907. (list 'mouse-face 'highlight
  2908. 'keymap org-mouse-map))
  2909. t)))
  2910. (defun org-font-lock-level ()
  2911. (save-excursion
  2912. (org-back-to-heading t)
  2913. (- (match-end 0) (match-beginning 0))))
  2914. (defun org-outline-level ()
  2915. (save-excursion
  2916. (looking-at outline-regexp)
  2917. (if (match-beginning 1)
  2918. (+ (org-get-string-indentation (match-string 1)) 1000)
  2919. (- (match-end 0) (match-beginning 0)))))
  2920. (defvar org-font-lock-keywords nil)
  2921. (defun org-set-font-lock-defaults ()
  2922. (let* ((em org-fontify-emphasized-text)
  2923. (lk org-activate-links)
  2924. (org-font-lock-extra-keywords
  2925. ;; Headlines
  2926. (list
  2927. '("^\\(\\**\\)\\(\\*\\)\\(.*\\)" (1 (org-get-level-face 1))
  2928. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  2929. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  2930. (1 'org-table))
  2931. ;; Links
  2932. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  2933. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  2934. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  2935. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  2936. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  2937. (if (memq 'camel lk) '(org-activate-camels (0 'org-link t)))
  2938. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  2939. (if org-table-limit-column-width
  2940. '(org-hide-wide-columns (0 nil append)))
  2941. ;; TODO lines
  2942. (list (concat "^\\*+[ \t]*" org-not-done-regexp)
  2943. '(1 'org-todo t))
  2944. ;; Priorities
  2945. (list (concat "\\[#[A-Z]\\]") '(0 'org-special-keyword t))
  2946. ;; Special keywords
  2947. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  2948. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  2949. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  2950. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  2951. ;; Emphasis
  2952. (if em '(org-do-emphasis-faces))
  2953. ;; Checkboxes, similar to Frank Ruell's org-checklet.el
  2954. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[ X]\\]\\)"
  2955. 2 'bold prepend)
  2956. (if org-provide-checkbox-statistics
  2957. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  2958. (0 (org-get-checkbox-statistics-face) t)))
  2959. ;; COMMENT
  2960. (list (concat "^\\*+[ \t]*\\<\\(" org-comment-string
  2961. "\\|" org-quote-string "\\)\\>")
  2962. '(1 'org-special-keyword t))
  2963. '("^#.*" (0 'font-lock-comment-face t))
  2964. ;; DONE
  2965. (if org-fontify-done-headline
  2966. (list (concat "^[*]+ +\\<\\(" org-done-string "\\)\\(.*\\)\\>")
  2967. '(1 'org-done t) '(2 'org-headline-done t))
  2968. (list (concat "^[*]+ +\\<\\(" org-done-string "\\)\\>")
  2969. '(1 'org-done t)))
  2970. ;; Table stuff
  2971. '("^[ \t]*\\(:.*\\)" (1 'org-table t))
  2972. '("| *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  2973. '("^[ \t]*| *\\([#!$*_^]\\) *|" (1 'org-formula t))
  2974. (if org-format-transports-properties-p
  2975. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  2976. '("^\\*+ \\(.*:ARCHIVE:.*\\)" (1 'org-archived prepend))
  2977. )))
  2978. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  2979. ;; Now set the full font-lock-keywords
  2980. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  2981. (org-set-local 'font-lock-defaults
  2982. '(org-font-lock-keywords t nil nil backward-paragraph))
  2983. (kill-local-variable 'font-lock-keywords) nil))
  2984. (defvar org-m nil)
  2985. (defvar org-l nil)
  2986. (defvar org-f nil)
  2987. (defun org-get-level-face (n)
  2988. "Get the right face for match N in font-lock matching of healdines."
  2989. (setq org-l (- (match-end 2) (match-beginning 1)))
  2990. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  2991. ; (setq org-f (nth (1- (% org-l org-n-levels)) org-level-faces))
  2992. (setq org-f (nth (% (1- org-l) org-n-levels) org-level-faces))
  2993. (cond
  2994. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  2995. ((eq n 2) org-f)
  2996. (t (if org-level-color-stars-only nil org-f))))
  2997. (defun org-unfontify-region (beg end &optional maybe_loudly)
  2998. "Remove fontification and activation overlays from links."
  2999. (font-lock-default-unfontify-region beg end)
  3000. (let* ((buffer-undo-list t)
  3001. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3002. (inhibit-modification-hooks t)
  3003. deactivate-mark buffer-file-name buffer-file-truename)
  3004. (remove-text-properties beg end
  3005. '(mouse-face nil keymap nil org-linked-text nil
  3006. invisible nil intangible nil))))
  3007. ;;; Visibility cycling
  3008. (defvar org-cycle-global-status nil)
  3009. (make-variable-buffer-local 'org-cycle-global-status)
  3010. (defvar org-cycle-subtree-status nil)
  3011. (make-variable-buffer-local 'org-cycle-subtree-status)
  3012. ;;;###autoload
  3013. (defun org-cycle (&optional arg)
  3014. "Visibility cycling for Org-mode.
  3015. - When this function is called with a prefix argument, rotate the entire
  3016. buffer through 3 states (global cycling)
  3017. 1. OVERVIEW: Show only top-level headlines.
  3018. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3019. 3. SHOW ALL: Show everything.
  3020. - When point is at the beginning of a headline, rotate the subtree started
  3021. by this line through 3 different states (local cycling)
  3022. 1. FOLDED: Only the main headline is shown.
  3023. 2. CHILDREN: The main headline and the direct children are shown.
  3024. From this state, you can move to one of the children
  3025. and zoom in further.
  3026. 3. SUBTREE: Show the entire subtree, including body text.
  3027. - When there is a numeric prefix, go up to a heading with level ARG, do
  3028. a `show-subtree' and return to the previous cursor position. If ARG
  3029. is negative, go up that many levels.
  3030. - When point is not at the beginning of a headline, execute
  3031. `indent-relative', like TAB normally does. See the option
  3032. `org-cycle-emulate-tab' for details.
  3033. - Special case: if point is the the beginning of the buffer and there is
  3034. no headline in line 1, this function will act as if called with prefix arg."
  3035. (interactive "P")
  3036. (let* ((outline-regexp
  3037. (if org-cycle-include-plain-lists
  3038. "\\(?:\\*+\\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3039. outline-regexp))
  3040. (bob-special (and org-cycle-global-at-bob (bobp)
  3041. (not (looking-at outline-regexp))))
  3042. (org-cycle-hook
  3043. (if bob-special
  3044. (delq 'org-optimize-window-after-visibility-change
  3045. (copy-sequence org-cycle-hook))
  3046. org-cycle-hook))
  3047. (pos (point)))
  3048. (if (or bob-special (equal arg '(4)))
  3049. ;; special case: use global cycling
  3050. (setq arg t))
  3051. (cond
  3052. ((org-at-table-p 'any)
  3053. ;; Enter the table or move to the next field in the table
  3054. (or (org-table-recognize-table.el)
  3055. (progn
  3056. (if arg (org-table-edit-field t)
  3057. (org-table-justify-field-maybe)
  3058. (call-interactively 'org-table-next-field)))))
  3059. ((eq arg t) ;; Global cycling
  3060. (cond
  3061. ((and (eq last-command this-command)
  3062. (eq org-cycle-global-status 'overview))
  3063. ;; We just created the overview - now do table of contents
  3064. ;; This can be slow in very large buffers, so indicate action
  3065. (message "CONTENTS...")
  3066. (org-content)
  3067. (message "CONTENTS...done")
  3068. (setq org-cycle-global-status 'contents)
  3069. (run-hook-with-args 'org-cycle-hook 'contents))
  3070. ((and (eq last-command this-command)
  3071. (eq org-cycle-global-status 'contents))
  3072. ;; We just showed the table of contents - now show everything
  3073. (show-all)
  3074. (message "SHOW ALL")
  3075. (setq org-cycle-global-status 'all)
  3076. (run-hook-with-args 'org-cycle-hook 'all))
  3077. (t
  3078. ;; Default action: go to overview
  3079. (org-overview)
  3080. (message "OVERVIEW")
  3081. (setq org-cycle-global-status 'overview)
  3082. (run-hook-with-args 'org-cycle-hook 'overview))))
  3083. ((integerp arg)
  3084. ;; Show-subtree, ARG levels up from here.
  3085. (save-excursion
  3086. (org-back-to-heading)
  3087. (outline-up-heading (if (< arg 0) (- arg)
  3088. (- (funcall outline-level) arg)))
  3089. (org-show-subtree)))
  3090. ((save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3091. ;; At a heading: rotate between three different views
  3092. (org-back-to-heading)
  3093. (let ((goal-column 0) eoh eol eos)
  3094. ;; First, some boundaries
  3095. (save-excursion
  3096. (org-back-to-heading)
  3097. (save-excursion
  3098. (beginning-of-line 2)
  3099. (while (and (not (eobp)) ;; this is like `next-line'
  3100. (get-char-property (1- (point)) 'invisible))
  3101. (beginning-of-line 2)) (setq eol (point)))
  3102. (outline-end-of-heading) (setq eoh (point))
  3103. (org-end-of-subtree t) (setq eos (point))
  3104. (outline-next-heading))
  3105. ;; Find out what to do next and set `this-command'
  3106. (cond
  3107. ((and (= eos eoh)
  3108. ;; Nothing is hidden behind this heading
  3109. (message "EMPTY ENTRY")
  3110. (setq org-cycle-subtree-status nil)))
  3111. ((>= eol eos)
  3112. ;; Entire subtree is hidden in one line: open it
  3113. (org-show-entry)
  3114. (show-children)
  3115. (message "CHILDREN")
  3116. (setq org-cycle-subtree-status 'children)
  3117. (run-hook-with-args 'org-cycle-hook 'children))
  3118. ((and (eq last-command this-command)
  3119. (eq org-cycle-subtree-status 'children))
  3120. ;; We just showed the children, now show everything.
  3121. (org-show-subtree)
  3122. (message "SUBTREE")
  3123. (setq org-cycle-subtree-status 'subtree)
  3124. (run-hook-with-args 'org-cycle-hook 'subtree))
  3125. (t
  3126. ;; Default action: hide the subtree.
  3127. (hide-subtree)
  3128. (message "FOLDED")
  3129. (setq org-cycle-subtree-status 'folded)
  3130. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3131. ;; TAB emulation
  3132. (buffer-read-only (org-back-to-heading))
  3133. ((org-try-cdlatex-tab))
  3134. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3135. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3136. (or (and (eq org-cycle-emulate-tab 'white)
  3137. (= (match-end 0) (point-at-eol)))
  3138. (and (eq org-cycle-emulate-tab 'whitestart)
  3139. (>= (match-end 0) pos))))
  3140. t
  3141. (eq org-cycle-emulate-tab t))
  3142. (if (and (looking-at "[ \n\r\t]")
  3143. (string-match "^[ \t]*$" (buffer-substring
  3144. (point-at-bol) (point))))
  3145. (progn
  3146. (beginning-of-line 1)
  3147. (and (looking-at "[ \t]+") (replace-match ""))))
  3148. (indent-relative))
  3149. (t (save-excursion
  3150. (org-back-to-heading)
  3151. (org-cycle))))))
  3152. ;;;###autoload
  3153. (defun org-global-cycle (&optional arg)
  3154. "Cycle the global visibility. For details see `org-cycle'."
  3155. (interactive "P")
  3156. (if (integerp arg)
  3157. (progn
  3158. (show-all)
  3159. (hide-sublevels arg)
  3160. (setq org-cycle-global-status 'contents))
  3161. (org-cycle '(4))))
  3162. (defun org-overview ()
  3163. "Switch to overview mode, shoing only top-level headlines.
  3164. Really, this shows all headlines with level equal or greater than the level
  3165. of the first headline in the buffer. This is important, because if the
  3166. first headline is not level one, then (hide-sublevels 1) gives confusing
  3167. results."
  3168. (interactive)
  3169. (hide-sublevels (save-excursion
  3170. (goto-char (point-min))
  3171. (if (re-search-forward (concat "^" outline-regexp) nil t)
  3172. (progn
  3173. (goto-char (match-beginning 0))
  3174. (funcall outline-level))
  3175. 1))))
  3176. ;; FIXME: allow an argument to give a limiting level for this.
  3177. (defun org-content ()
  3178. "Show all headlines in the buffer, like a table of contents"
  3179. (interactive)
  3180. (save-excursion
  3181. ;; Visit all headings and show their offspring
  3182. (goto-char (point-max))
  3183. (catch 'exit
  3184. (while (and (progn (condition-case nil
  3185. (outline-previous-visible-heading 1)
  3186. (error (goto-char (point-min))))
  3187. t)
  3188. (looking-at outline-regexp))
  3189. (show-branches)
  3190. (if (bobp) (throw 'exit nil))))))
  3191. (defun org-optimize-window-after-visibility-change (state)
  3192. "Adjust the window after a change in outline visibility.
  3193. This function is the default value of the hook `org-cycle-hook'."
  3194. (when (get-buffer-window (current-buffer))
  3195. (cond
  3196. ((eq state 'overview) (org-first-headline-recenter 1))
  3197. ((eq state 'content) nil)
  3198. ((eq state 'all) nil)
  3199. ((eq state 'folded) nil)
  3200. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  3201. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  3202. (defun org-subtree-end-visible-p ()
  3203. "Is the end of the current subtree visible?"
  3204. (pos-visible-in-window-p
  3205. (save-excursion (org-end-of-subtree t) (point))))
  3206. (defun org-first-headline-recenter (&optional N)
  3207. "Move cursor to the first headline and recenter the headline.
  3208. Optional argument N means, put the headline into the Nth line of the window."
  3209. (goto-char (point-min))
  3210. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  3211. (beginning-of-line)
  3212. (recenter (prefix-numeric-value N))))
  3213. (defvar org-goto-window-configuration nil)
  3214. (defvar org-goto-marker nil)
  3215. (defvar org-goto-map (make-sparse-keymap))
  3216. (let ((cmds '(isearch-forward isearch-backward)) cmd)
  3217. (while (setq cmd (pop cmds))
  3218. (substitute-key-definition cmd cmd org-goto-map global-map)))
  3219. (define-key org-goto-map "\C-m" 'org-goto-ret)
  3220. (define-key org-goto-map [(left)] 'org-goto-left)
  3221. (define-key org-goto-map [(right)] 'org-goto-right)
  3222. (define-key org-goto-map [(?q)] 'org-goto-quit)
  3223. (define-key org-goto-map [(control ?g)] 'org-goto-quit)
  3224. (define-key org-goto-map "\C-i" 'org-cycle)
  3225. (define-key org-goto-map [(tab)] 'org-cycle)
  3226. (define-key org-goto-map [(down)] 'outline-next-visible-heading)
  3227. (define-key org-goto-map [(up)] 'outline-previous-visible-heading)
  3228. (define-key org-goto-map "n" 'outline-next-visible-heading)
  3229. (define-key org-goto-map "p" 'outline-previous-visible-heading)
  3230. (define-key org-goto-map "f" 'outline-forward-same-level)
  3231. (define-key org-goto-map "b" 'outline-backward-same-level)
  3232. (define-key org-goto-map "u" 'outline-up-heading)
  3233. (define-key org-goto-map "\C-c\C-n" 'outline-next-visible-heading)
  3234. (define-key org-goto-map "\C-c\C-p" 'outline-previous-visible-heading)
  3235. (define-key org-goto-map "\C-c\C-f" 'outline-forward-same-level)
  3236. (define-key org-goto-map "\C-c\C-b" 'outline-backward-same-level)
  3237. (define-key org-goto-map "\C-c\C-u" 'outline-up-heading)
  3238. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  3239. (while l (define-key org-goto-map (int-to-string (pop l)) 'digit-argument)))
  3240. (defconst org-goto-help
  3241. "Select a location to jump to, press RET
  3242. \[Up]/[Down]=next/prev headline TAB=cycle visibility RET=select [Q]uit")
  3243. (defun org-goto ()
  3244. "Go to a different location of the document, keeping current visibility.
  3245. When you want to go to a different location in a document, the fastest way
  3246. is often to fold the entire buffer and then dive into the tree. This
  3247. method has the disadvantage, that the previous location will be folded,
  3248. which may not be what you want.
  3249. This command works around this by showing a copy of the current buffer in
  3250. overview mode. You can dive into the tree in that copy, to find the
  3251. location you want to reach. When pressing RET, the command returns to the
  3252. original buffer in which the visibility is still unchanged. It then jumps
  3253. to the new location, making it and the headline hierarchy above it visible."
  3254. (interactive)
  3255. (let* ((org-goto-start-pos (point))
  3256. (selected-point
  3257. (org-get-location (current-buffer) org-goto-help)))
  3258. (if selected-point
  3259. (progn
  3260. (org-mark-ring-push org-goto-start-pos)
  3261. (goto-char selected-point)
  3262. (if (or (org-invisible-p) (org-invisible-p2))
  3263. (org-show-hierarchy-above)))
  3264. (error "Quit"))))
  3265. (defun org-get-location (buf help)
  3266. "Let the user select a location in the Org-mode buffer BUF.
  3267. This function uses a recursive edit. It returns the selected position
  3268. or nil."
  3269. (let (org-selected-point)
  3270. (save-excursion
  3271. (save-window-excursion
  3272. (delete-other-windows)
  3273. (switch-to-buffer (get-buffer-create "*org-goto*"))
  3274. (with-output-to-temp-buffer "*Help*"
  3275. (princ help))
  3276. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  3277. (setq buffer-read-only nil)
  3278. (erase-buffer)
  3279. (insert-buffer-substring buf)
  3280. (let ((org-startup-truncated t)
  3281. (org-startup-folded t)
  3282. (org-startup-align-all-tables nil)
  3283. (org-startup-with-deadline-check nil))
  3284. (org-mode))
  3285. (setq buffer-read-only t)
  3286. (if (boundp 'org-goto-start-pos)
  3287. (goto-char org-goto-start-pos)
  3288. (goto-char (point-min)))
  3289. (org-beginning-of-line)
  3290. (message "Select location and press RET")
  3291. ;; now we make sure that during selection, ony very few keys work
  3292. ;; and that it is impossible to switch to another window.
  3293. (let ((gm (current-global-map))
  3294. (overriding-local-map org-goto-map))
  3295. (unwind-protect
  3296. (progn
  3297. (use-global-map org-goto-map)
  3298. (recursive-edit))
  3299. (use-global-map gm)))))
  3300. (kill-buffer "*org-goto*")
  3301. org-selected-point))
  3302. (defun org-goto-ret (&optional arg)
  3303. "Finish `org-goto' by going to the new location."
  3304. (interactive "P")
  3305. (setq org-selected-point (point)
  3306. current-prefix-arg arg)
  3307. (throw 'exit nil))
  3308. (defun org-goto-left ()
  3309. "Finish `org-goto' by going to the new location."
  3310. (interactive)
  3311. (if (org-on-heading-p)
  3312. (progn
  3313. (beginning-of-line 1)
  3314. (setq org-selected-point (point)
  3315. current-prefix-arg (- (match-end 0) (match-beginning 0)))
  3316. (throw 'exit nil))
  3317. (error "Not on a heading")))
  3318. (defun org-goto-right ()
  3319. "Finish `org-goto' by going to the new location."
  3320. (interactive)
  3321. (if (org-on-heading-p)
  3322. (progn
  3323. (outline-end-of-subtree)
  3324. (or (eobp) (forward-char 1))
  3325. (setq org-selected-point (point)
  3326. current-prefix-arg (- (match-end 0) (match-beginning 0)))
  3327. (throw 'exit nil))
  3328. (error "Not on a heading")))
  3329. (defun org-goto-quit ()
  3330. "Finish `org-goto' without cursor motion."
  3331. (interactive)
  3332. (setq org-selected-point nil)
  3333. (throw 'exit nil))
  3334. ;;; Promotion, Demotion, Inserting new headlines
  3335. (defvar org-ignore-region nil
  3336. "To temporarily disable the active region.")
  3337. (defun org-insert-heading (&optional force-heading)
  3338. "Insert a new heading or item with same depth at point.
  3339. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  3340. If point is at the beginning of a headline, insert a sibling before the
  3341. current headline. If point is in the middle of a headline, split the headline
  3342. at that position and make the rest of the headline part of the sibling below
  3343. the current headline."
  3344. (interactive "P")
  3345. (if (= (buffer-size) 0)
  3346. (insert "\n* ")
  3347. (when (or force-heading (not (org-insert-item)))
  3348. (let* ((head (save-excursion
  3349. (condition-case nil
  3350. (progn
  3351. (org-back-to-heading)
  3352. (match-string 0))
  3353. (error "*"))))
  3354. pos)
  3355. (cond
  3356. ((and (org-on-heading-p) (bolp)
  3357. (save-excursion (backward-char 1) (not (org-invisible-p))))
  3358. (open-line 1))
  3359. ((and (bolp) (save-excursion
  3360. (backward-char 1) (not (org-invisible-p))))
  3361. nil)
  3362. (t (newline)))
  3363. (insert head) (just-one-space)
  3364. (setq pos (point))
  3365. (end-of-line 1)
  3366. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  3367. (run-hooks 'org-insert-heading-hook)))))
  3368. (defun org-in-item-p ()
  3369. "It the cursor inside a plain list item.
  3370. Does not have to be the first line."
  3371. (save-excursion
  3372. (condition-case nil
  3373. (progn
  3374. (org-beginning-of-item)
  3375. (org-at-item-p)
  3376. t)
  3377. (error nil))))
  3378. (defun org-insert-item (&optional checkbox)
  3379. "Insert a new item at the current level.
  3380. Return t when things worked, nil when we are not in an item."
  3381. (when (save-excursion
  3382. (condition-case nil
  3383. (progn
  3384. (org-beginning-of-item)
  3385. (org-at-item-p)
  3386. t)
  3387. (error nil)))
  3388. (let* ((bul (match-string 0))
  3389. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  3390. (match-end 0)))
  3391. pos)
  3392. (cond
  3393. ((and (org-at-item-p) (<= (point) eow))
  3394. ;; before the bullet
  3395. (beginning-of-line 1)
  3396. (open-line 1))
  3397. ((<= (point) eow)
  3398. (beginning-of-line 1))
  3399. (t (newline)))
  3400. (insert bul (if checkbox "[ ]" ""))
  3401. (just-one-space)
  3402. (setq pos (point))
  3403. (end-of-line 1)
  3404. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  3405. (org-maybe-renumber-ordered-list)
  3406. (and checkbox (org-update-checkbox-count-maybe))
  3407. t))
  3408. (defun org-insert-todo-heading (arg)
  3409. "Insert a new heading with the same level and TODO state as current heading.
  3410. If the heading has no TODO state, or if the state is DONE, use the first
  3411. state (TODO by default). Also with prefix arg, force first state."
  3412. (interactive "P")
  3413. (when (not (org-insert-item 'checkbox))
  3414. (org-insert-heading)
  3415. (save-excursion
  3416. (org-back-to-heading)
  3417. (outline-previous-heading)
  3418. (looking-at org-todo-line-regexp))
  3419. (if (or arg
  3420. (not (match-beginning 2))
  3421. (equal (match-string 2) org-done-string))
  3422. (insert (car org-todo-keywords) " ")
  3423. (insert (match-string 2) " "))))
  3424. (defun org-promote-subtree ()
  3425. "Promote the entire subtree.
  3426. See also `org-promote'."
  3427. (interactive)
  3428. (save-excursion
  3429. (org-map-tree 'org-promote))
  3430. (org-fix-position-after-promote))
  3431. (defun org-demote-subtree ()
  3432. "Demote the entire subtree. See `org-demote'.
  3433. See also `org-promote'."
  3434. (interactive)
  3435. (save-excursion
  3436. (org-map-tree 'org-demote))
  3437. (org-fix-position-after-promote))
  3438. (defun org-do-promote ()
  3439. "Promote the current heading higher up the tree.
  3440. If the region is active in `transient-mark-mode', promote all headings
  3441. in the region."
  3442. (interactive)
  3443. (save-excursion
  3444. (if (org-region-active-p)
  3445. (org-map-region 'org-promote (region-beginning) (region-end))
  3446. (org-promote)))
  3447. (org-fix-position-after-promote))
  3448. (defun org-do-demote ()
  3449. "Demote the current heading lower down the tree.
  3450. If the region is active in `transient-mark-mode', demote all headings
  3451. in the region."
  3452. (interactive)
  3453. (save-excursion
  3454. (if (org-region-active-p)
  3455. (org-map-region 'org-demote (region-beginning) (region-end))
  3456. (org-demote)))
  3457. (org-fix-position-after-promote))
  3458. (defun org-fix-position-after-promote ()
  3459. "Make sure that after pro/demotion cursor position is right."
  3460. (if (and (equal (char-after) ?\n)
  3461. (save-excursion
  3462. (skip-chars-backward "a-zA-Z0-9_@")
  3463. (looking-at org-todo-regexp)))
  3464. (insert " "))
  3465. (and (equal (char-after) ?\ )
  3466. (equal (char-before) ?*)
  3467. (forward-char 1)))
  3468. (defun org-get-legal-level (level change)
  3469. "Rectify a level change under the influence of `org-odd-levels-only'
  3470. LEVEL is a current level, CHANGE is by how much the level should be
  3471. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  3472. even level numbers will become the next higher odd number."
  3473. (if org-odd-levels-only
  3474. (cond ((not change) (1+ (* 2 (/ level 2))))
  3475. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  3476. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  3477. (max 1 (+ level change))))
  3478. (defun org-promote ()
  3479. "Promote the current heading higher up the tree.
  3480. If the region is active in `transient-mark-mode', promote all headings
  3481. in the region."
  3482. (org-back-to-heading t)
  3483. (let* ((level (save-match-data (funcall outline-level)))
  3484. (up-head (make-string (org-get-legal-level level -1) ?*))
  3485. (diff (abs (- level (length up-head)))))
  3486. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover"))
  3487. (replace-match up-head nil t)
  3488. ;; Fixup tag positioning
  3489. (and org-auto-align-tags (org-set-tags nil t))
  3490. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  3491. (defun org-demote ()
  3492. "Demote the current heading lower down the tree.
  3493. If the region is active in `transient-mark-mode', demote all headings
  3494. in the region."
  3495. (org-back-to-heading t)
  3496. (let* ((level (save-match-data (funcall outline-level)))
  3497. (down-head (make-string (org-get-legal-level level 1) ?*))
  3498. (diff (abs (- level (length down-head)))))
  3499. (replace-match down-head nil t)
  3500. ;; Fixup tag positioning
  3501. (and org-auto-align-tags (org-set-tags nil t))
  3502. (if org-adapt-indentation (org-fixup-indentation diff))))
  3503. (defun org-map-tree (fun)
  3504. "Call FUN for every heading underneath the current one."
  3505. (org-back-to-heading)
  3506. (let ((level (funcall outline-level)))
  3507. (save-excursion
  3508. (funcall fun)
  3509. (while (and (progn
  3510. (outline-next-heading)
  3511. (> (funcall outline-level) level))
  3512. (not (eobp)))
  3513. (funcall fun)))))
  3514. (defun org-map-region (fun beg end)
  3515. "Call FUN for every heading between BEG and END."
  3516. (let ((org-ignore-region t))
  3517. (save-excursion
  3518. (setq end (copy-marker end))
  3519. (goto-char beg)
  3520. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  3521. (< (point) end))
  3522. (funcall fun))
  3523. (while (and (progn
  3524. (outline-next-heading)
  3525. (< (point) end))
  3526. (not (eobp)))
  3527. (funcall fun)))))
  3528. (defun org-fixup-indentation (diff)
  3529. "Change the indentation in the current entry by DIFF
  3530. However, if any line in the current entry has no indentation, or if it
  3531. would end up with no indentation after the change, nothing at all is done."
  3532. (save-excursion
  3533. (let ((end (save-excursion (outline-next-heading)
  3534. (point-marker)))
  3535. (prohibit (if (> diff 0)
  3536. "^\\S-"
  3537. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  3538. col)
  3539. (unless (save-excursion (re-search-forward prohibit end t))
  3540. (while (re-search-forward "^[ \t]+" end t)
  3541. (goto-char (match-end 0))
  3542. (setq col (current-column))
  3543. (if (< diff 0) (replace-match ""))
  3544. (indent-to (+ diff col))))
  3545. (move-marker end nil))))
  3546. ;;; Vertical tree motion, cutting and pasting of subtrees
  3547. (defun org-move-subtree-up (&optional arg)
  3548. "Move the current subtree up past ARG headlines of the same level."
  3549. (interactive "p")
  3550. (org-move-subtree-down (- (prefix-numeric-value arg))))
  3551. (defun org-move-subtree-down (&optional arg)
  3552. "Move the current subtree down past ARG headlines of the same level."
  3553. (interactive "p")
  3554. (setq arg (prefix-numeric-value arg))
  3555. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  3556. 'outline-get-last-sibling))
  3557. (ins-point (make-marker))
  3558. (cnt (abs arg))
  3559. beg end txt folded)
  3560. ;; Select the tree
  3561. (org-back-to-heading)
  3562. (setq beg (point))
  3563. (save-match-data
  3564. (save-excursion (outline-end-of-heading)
  3565. (setq folded (org-invisible-p)))
  3566. (outline-end-of-subtree))
  3567. (outline-next-heading)
  3568. (setq end (point))
  3569. ;; Find insertion point, with error handling
  3570. (goto-char beg)
  3571. (while (> cnt 0)
  3572. (or (and (funcall movfunc) (looking-at outline-regexp))
  3573. (progn (goto-char beg)
  3574. (error "Cannot move past superior level or buffer limit")))
  3575. (setq cnt (1- cnt)))
  3576. (if (> arg 0)
  3577. ;; Moving forward - still need to move over subtree
  3578. (progn (outline-end-of-subtree)
  3579. (outline-next-heading)
  3580. (if (not (or (looking-at (concat "^" outline-regexp))
  3581. (bolp)))
  3582. (newline))))
  3583. (move-marker ins-point (point))
  3584. (setq txt (buffer-substring beg end))
  3585. (delete-region beg end)
  3586. (insert txt)
  3587. (goto-char ins-point)
  3588. (if folded (hide-subtree))
  3589. (move-marker ins-point nil)))
  3590. (defvar org-subtree-clip ""
  3591. "Clipboard for cut and paste of subtrees.
  3592. This is actually only a copy of the kill, because we use the normal kill
  3593. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  3594. (defvar org-subtree-clip-folded nil
  3595. "Was the last copied subtree folded?
  3596. This is used to fold the tree back after pasting.")
  3597. (defun org-cut-subtree ()
  3598. "Cut the current subtree into the clipboard.
  3599. This is a short-hand for marking the subtree and then cutting it."
  3600. (interactive)
  3601. (org-copy-subtree 'cut))
  3602. (defun org-copy-subtree (&optional cut)
  3603. "Cut the current subtree into the clipboard.
  3604. This is a short-hand for marking the subtree and then copying it.
  3605. If CUT is non nil, actually cut the subtree."
  3606. (interactive)
  3607. (let (beg end folded)
  3608. (org-back-to-heading)
  3609. (setq beg (point))
  3610. (save-match-data
  3611. (save-excursion (outline-end-of-heading)
  3612. (setq folded (org-invisible-p)))
  3613. (outline-end-of-subtree))
  3614. (if (equal (char-after) ?\n) (forward-char 1))
  3615. (setq end (point))
  3616. (goto-char beg)
  3617. (when (> end beg)
  3618. (setq org-subtree-clip-folded folded)
  3619. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  3620. (setq org-subtree-clip (current-kill 0))
  3621. (message "%s: Subtree with %d characters"
  3622. (if cut "Cut" "Copied")
  3623. (length org-subtree-clip)))))
  3624. (defun org-paste-subtree (&optional level tree)
  3625. "Paste the clipboard as a subtree, with modification of headline level.
  3626. The entire subtree is promoted or demoted in order to match a new headline
  3627. level. By default, the new level is derived from the visible headings
  3628. before and after the insertion point, and taken to be the inferior headline
  3629. level of the two. So if the previous visible heading is level 3 and the
  3630. next is level 4 (or vice versa), level 4 will be used for insertion.
  3631. This makes sure that the subtree remains an independent subtree and does
  3632. not swallow low level entries.
  3633. You can also force a different level, either by using a numeric prefix
  3634. argument, or by inserting the heading marker by hand. For example, if the
  3635. cursor is after \"*****\", then the tree will be shifted to level 5.
  3636. If you want to insert the tree as is, just use \\[yank].
  3637. If optional TREE is given, use this text instead of the kill ring."
  3638. (interactive "P")
  3639. (unless (org-kill-is-subtree-p tree)
  3640. (error
  3641. (substitute-command-keys
  3642. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  3643. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  3644. (^re (concat "^\\(" outline-regexp "\\)"))
  3645. (re (concat "\\(" outline-regexp "\\)"))
  3646. (^re_ (concat "\\(" outline-regexp "\\)[ \t]*"))
  3647. (old-level (if (string-match ^re txt)
  3648. (- (match-end 0) (match-beginning 0))
  3649. -1))
  3650. (force-level (cond (level (prefix-numeric-value level))
  3651. ((string-match
  3652. ^re_ (buffer-substring (point-at-bol) (point)))
  3653. (- (match-end 0) (match-beginning 0)))
  3654. (t nil)))
  3655. (previous-level (save-excursion
  3656. (condition-case nil
  3657. (progn
  3658. (outline-previous-visible-heading 1)
  3659. (if (looking-at re)
  3660. (- (match-end 0) (match-beginning 0))
  3661. 1))
  3662. (error 1))))
  3663. (next-level (save-excursion
  3664. (condition-case nil
  3665. (progn
  3666. (outline-next-visible-heading 1)
  3667. (if (looking-at re)
  3668. (- (match-end 0) (match-beginning 0))
  3669. 1))
  3670. (error 1))))
  3671. (new-level (or force-level (max previous-level next-level)))
  3672. (shift (if (or (= old-level -1)
  3673. (= new-level -1)
  3674. (= old-level new-level))
  3675. 0
  3676. (- new-level old-level)))
  3677. (shift1 shift)
  3678. (delta (if (> shift 0) -1 1))
  3679. (func (if (> shift 0) 'org-demote 'org-promote))
  3680. (org-odd-levels-only nil)
  3681. beg end)
  3682. ;; Remove the forces level indicator
  3683. (if force-level
  3684. (delete-region (point-at-bol) (point)))
  3685. ;; Make sure we start at the beginning of an empty line
  3686. (if (not (bolp)) (insert "\n"))
  3687. (if (not (looking-at "[ \t]*$"))
  3688. (progn (insert "\n") (backward-char 1)))
  3689. ;; Paste
  3690. (setq beg (point))
  3691. (if (string-match "[ \t\r\n]+\\'" txt)
  3692. (setq txt (replace-match "\n" t t txt)))
  3693. (insert txt)
  3694. (setq end (point))
  3695. (if (looking-at "[ \t\r\n]+")
  3696. (replace-match "\n"))
  3697. (goto-char beg)
  3698. ;; Shift if necessary
  3699. (if (= shift 0)
  3700. (message "Pasted at level %d, without shift" new-level)
  3701. (save-restriction
  3702. (narrow-to-region beg end)
  3703. (while (not (= shift 0))
  3704. (org-map-region func (point-min) (point-max))
  3705. (setq shift (+ delta shift)))
  3706. (goto-char (point-min))
  3707. (message "Pasted at level %d, with shift by %d levels"
  3708. new-level shift1)))
  3709. (if (and kill-ring
  3710. (eq org-subtree-clip (current-kill 0))
  3711. org-subtree-clip-folded)
  3712. ;; The tree was folded before it was killed/copied
  3713. (hide-subtree))))
  3714. (defun org-kill-is-subtree-p (&optional txt)
  3715. "Check if the current kill is an outline subtree, or a set of trees.
  3716. Returns nil if kill does not start with a headline, or if the first
  3717. headline level is not the largest headline level in the tree.
  3718. So this will actually accept several entries of equal levels as well,
  3719. which is OK for `org-paste-subtree'.
  3720. If optional TXT is given, check this string instead of the current kill."
  3721. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  3722. (start-level (and kill
  3723. (string-match (concat "\\`" outline-regexp) kill)
  3724. (- (match-end 0) (match-beginning 0))))
  3725. (re (concat "^" outline-regexp))
  3726. (start 1))
  3727. (if (not start-level)
  3728. nil ;; does not even start with a heading
  3729. (catch 'exit
  3730. (while (setq start (string-match re kill (1+ start)))
  3731. (if (< (- (match-end 0) (match-beginning 0)) start-level)
  3732. (throw 'exit nil)))
  3733. t))))
  3734. (defun org-narrow-to-subtree ()
  3735. "Narrow buffer to the current subtree."
  3736. (interactive)
  3737. (save-excursion
  3738. (narrow-to-region
  3739. (progn (org-back-to-heading) (point))
  3740. (progn (org-end-of-subtree t) (point)))))
  3741. ;;; Plain list items
  3742. (defun org-at-item-p ()
  3743. "Is point in a line starting a hand-formatted item?"
  3744. (let ((llt org-plain-list-ordered-item-terminator))
  3745. (save-excursion
  3746. (goto-char (point-at-bol))
  3747. (looking-at
  3748. (cond
  3749. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  3750. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  3751. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  3752. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  3753. (defun org-at-item-checkbox-p ()
  3754. "Is point at a line starting a plain-list item with a checklet?"
  3755. (and (org-at-item-p)
  3756. (save-excursion
  3757. (goto-char (match-end 0))
  3758. (skip-chars-forward " \t")
  3759. (looking-at "\\[[ X]\\]"))))
  3760. (defun org-toggle-checkbox (&optional arg)
  3761. "Toggle the checkbox in the current line."
  3762. (interactive "P")
  3763. (catch 'exit
  3764. (let (beg end status (firstnew 'unknown))
  3765. (cond
  3766. ((org-region-active-p)
  3767. (setq beg (region-beginning) end (region-end)))
  3768. ((org-on-heading-p)
  3769. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  3770. ((org-at-item-checkbox-p)
  3771. (save-excursion
  3772. (replace-match (if (equal (match-string 0) "[ ]") "[X]" "[ ]") t t))
  3773. (throw 'exit t))
  3774. (t (error "Not at a checkbox or heading, and no active region")))
  3775. (save-excursion
  3776. (goto-char beg)
  3777. (while (< (point) end)
  3778. (when (org-at-item-checkbox-p)
  3779. (setq status (equal (match-string 0) "[X]"))
  3780. (when (eq firstnew 'unknown)
  3781. (setq firstnew (not status)))
  3782. (replace-match
  3783. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  3784. (beginning-of-line 2)))))
  3785. (org-update-checkbox-count-maybe))
  3786. (defun org-update-checkbox-count-maybe ()
  3787. "Update checkbox statistics unless turned off by user."
  3788. (when org-provide-checkbox-statistics
  3789. (org-update-checkbox-count)))
  3790. (defun org-update-checkbox-count (&optional all)
  3791. "Update the checkbox statistics in the current section.
  3792. This will find all statistic cookies like [57%] and [6/12] and update them
  3793. with the current numbers. With optional prefix argument ALL, do this for
  3794. the whole buffer."
  3795. (interactive "P")
  3796. (save-excursion
  3797. (let* ((buffer-invisibility-spec nil)
  3798. (beg (progn (outline-back-to-heading) (point)))
  3799. (end (move-marker (make-marker)
  3800. (progn (outline-next-heading) (point))))
  3801. (re "\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)")
  3802. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[ X]\\]\\)")
  3803. b1 e1 f1 c-on c-off lim (cstat 0))
  3804. (when all
  3805. (goto-char (point-min))
  3806. (outline-next-heading)
  3807. (setq beg (point) end (point-max)))
  3808. (goto-char beg)
  3809. (while (re-search-forward re end t)
  3810. (setq cstat (1+ cstat)
  3811. b1 (match-beginning 0)
  3812. e1 (match-end 0)
  3813. f1 (match-beginning 1)
  3814. lim (cond
  3815. ((org-on-heading-p) (outline-next-heading) (point))
  3816. ((org-at-item-p) (org-end-of-item) (point))
  3817. (t nil))
  3818. c-on 0 c-off 0)
  3819. (goto-char e1)
  3820. (when lim
  3821. (while (re-search-forward re-box lim t)
  3822. (if (equal (match-string 2) "[ ]")
  3823. (setq c-off (1+ c-off))
  3824. (setq c-on (1+ c-on))))
  3825. (delete-region b1 e1)
  3826. (goto-char b1)
  3827. (insert (if f1
  3828. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  3829. (format "[%d/%d]" c-on (+ c-on c-off))))))
  3830. (when (interactive-p)
  3831. (message "Checkbox satistics updated %s (%d places)"
  3832. (if all "in entire file" "in current outline entry") cstat)))))
  3833. (defun org-get-checkbox-statistics-face ()
  3834. "Select the face for checkbox statistics.
  3835. The face will be `org-done' when all relevant boxes are checked. Otherwise
  3836. it will be `org-todo'."
  3837. (if (match-end 1)
  3838. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  3839. (if (and (> (match-end 2) (match-beginning 2))
  3840. (equal (match-string 2) (match-string 3)))
  3841. 'org-done
  3842. 'org-todo)))
  3843. (defun org-get-indentation (&optional line)
  3844. "Get the indentation of the current line, interpreting tabs.
  3845. When LINE is given, assume it represents a line and compute its indentation."
  3846. (if line
  3847. (if (string-match "^ *" (org-remove-tabs line))
  3848. (match-end 0))
  3849. (save-excursion
  3850. (beginning-of-line 1)
  3851. (skip-chars-forward " \t")
  3852. (current-column))))
  3853. (defun org-remove-tabs (s &optional width)
  3854. "Replace tabulators in S with spaces.
  3855. Assumes that s is a single line, starting in column 0."
  3856. (setq width (or width tab-width))
  3857. (while (string-match "\t" s)
  3858. (setq s (replace-match
  3859. (make-string
  3860. (- (* width (/ (+ (match-beginning 0) width) width))
  3861. (match-beginning 0)) ?\ )
  3862. t t s)))
  3863. s)
  3864. ;; FIXME: document properly.
  3865. (defun org-fix-indentation (line ind)
  3866. "If the current indenation is smaller than ind1, leave it alone.
  3867. If it is larger than ind, reduce it by ind."
  3868. (let* ((l (org-remove-tabs line))
  3869. (i (org-get-indentation l))
  3870. (i1 (car ind)) (i2 (cdr ind)))
  3871. (if (>= i i2) (setq l (substring line i2)))
  3872. (if (> i1 0)
  3873. (concat (make-string i1 ?\ ) l)
  3874. l)))
  3875. (defun org-beginning-of-item ()
  3876. "Go to the beginning of the current hand-formatted item.
  3877. If the cursor is not in an item, throw an error."
  3878. (interactive)
  3879. (let ((pos (point))
  3880. (limit (save-excursion (org-back-to-heading)
  3881. (beginning-of-line 2) (point)))
  3882. ind ind1)
  3883. (if (org-at-item-p)
  3884. (beginning-of-line 1)
  3885. (beginning-of-line 1)
  3886. (skip-chars-forward " \t")
  3887. (setq ind (current-column))
  3888. (if (catch 'exit
  3889. (while t
  3890. (beginning-of-line 0)
  3891. (if (< (point) limit) (throw 'exit nil))
  3892. (unless (looking-at "[ \t]*$")
  3893. (skip-chars-forward " \t")
  3894. (setq ind1 (current-column))
  3895. (if (< ind1 ind)
  3896. (throw 'exit (org-at-item-p))))))
  3897. nil
  3898. (goto-char pos)
  3899. (error "Not in an item")))))
  3900. (defun org-end-of-item ()
  3901. "Go to the end of the current hand-formatted item.
  3902. If the cursor is not in an item, throw an error."
  3903. (interactive)
  3904. (let ((pos (point))
  3905. (limit (save-excursion (outline-next-heading) (point)))
  3906. (ind (save-excursion
  3907. (org-beginning-of-item)
  3908. (skip-chars-forward " \t")
  3909. (current-column)))
  3910. ind1)
  3911. (if (catch 'exit
  3912. (while t
  3913. (beginning-of-line 2)
  3914. (if (>= (point) limit) (throw 'exit t))
  3915. (unless (looking-at "[ \t]*$")
  3916. (skip-chars-forward " \t")
  3917. (setq ind1 (current-column))
  3918. (if (<= ind1 ind) (throw 'exit t)))))
  3919. (beginning-of-line 1)
  3920. (goto-char pos)
  3921. (error "Not in an item"))))
  3922. (defun org-next-item ()
  3923. "Move to the beginning of the next item in the current plain list.
  3924. Error if not at a plain list, or if this is the last item in the list."
  3925. (interactive)
  3926. (let (ind ind1 (pos (point)))
  3927. (org-beginning-of-item)
  3928. (setq ind (org-get-indentation))
  3929. (org-end-of-item)
  3930. (setq ind1 (org-get-indentation))
  3931. (unless (and (org-at-item-p) (= ind ind1))
  3932. (goto-char pos)
  3933. (error "On last item"))))
  3934. (defun org-previous-item ()
  3935. "Move to the beginning of the previous item in the current plain list.
  3936. Error if not at a plain list, or if this is the last item in the list."
  3937. (interactive)
  3938. (let (beg ind (pos (point)))
  3939. (org-beginning-of-item)
  3940. (setq beg (point))
  3941. (setq ind (org-get-indentation))
  3942. (goto-char beg)
  3943. (catch 'exit
  3944. (while t
  3945. (beginning-of-line 0)
  3946. (if (looking-at "[ \t]*$")
  3947. nil
  3948. (if (<= (org-get-indentation) ind)
  3949. (throw 'exit t)))))
  3950. (condition-case nil
  3951. (org-beginning-of-item)
  3952. (error (goto-char pos)
  3953. (error "On first item")))))
  3954. (defun org-move-item-down ()
  3955. "Move the plain list item at point down, i.e. swap with following item.
  3956. Subitems (items with larger indentation) are considered part of the item,
  3957. so this really moves item trees."
  3958. (interactive)
  3959. (let (beg end ind ind1 (pos (point)) txt)
  3960. (org-beginning-of-item)
  3961. (setq beg (point))
  3962. (setq ind (org-get-indentation))
  3963. (org-end-of-item)
  3964. (setq end (point))
  3965. (setq ind1 (org-get-indentation))
  3966. (if (and (org-at-item-p) (= ind ind1))
  3967. (progn
  3968. (org-end-of-item)
  3969. (setq txt (buffer-substring beg end))
  3970. (save-excursion
  3971. (delete-region beg end))
  3972. (setq pos (point))
  3973. (insert txt)
  3974. (goto-char pos)
  3975. (org-maybe-renumber-ordered-list))
  3976. (goto-char pos)
  3977. (error "Cannot move this item further down"))))
  3978. (defun org-move-item-up (arg)
  3979. "Move the plain list item at point up, i.e. swap with previous item.
  3980. Subitems (items with larger indentation) are considered part of the item,
  3981. so this really moves item trees."
  3982. (interactive "p")
  3983. (let (beg end ind ind1 (pos (point)) txt)
  3984. (org-beginning-of-item)
  3985. (setq beg (point))
  3986. (setq ind (org-get-indentation))
  3987. (org-end-of-item)
  3988. (setq end (point))
  3989. (goto-char beg)
  3990. (catch 'exit
  3991. (while t
  3992. (beginning-of-line 0)
  3993. (if (looking-at "[ \t]*$")
  3994. nil
  3995. (if (<= (setq ind1 (org-get-indentation)) ind)
  3996. (throw 'exit t)))))
  3997. (condition-case nil
  3998. (org-beginning-of-item)
  3999. (error (goto-char beg)
  4000. (error "Cannot move this item further up")))
  4001. (setq ind1 (org-get-indentation))
  4002. (if (and (org-at-item-p) (= ind ind1))
  4003. (progn
  4004. (setq txt (buffer-substring beg end))
  4005. (save-excursion
  4006. (delete-region beg end))
  4007. (setq pos (point))
  4008. (insert txt)
  4009. (goto-char pos)
  4010. (org-maybe-renumber-ordered-list))
  4011. (goto-char pos)
  4012. (error "Cannot move this item further up"))))
  4013. (defun org-maybe-renumber-ordered-list ()
  4014. "Renumber the ordered list at point if setup allows it.
  4015. This tests the user option `org-auto-renumber-ordered-lists' before
  4016. doing the renumbering."
  4017. (and org-auto-renumber-ordered-lists
  4018. (org-at-item-p)
  4019. (match-beginning 3)
  4020. (org-renumber-ordered-list 1)))
  4021. (defun org-get-string-indentation (s)
  4022. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  4023. (let ((n -1) (i 0) (w tab-width) c)
  4024. (catch 'exit
  4025. (while (< (setq n (1+ n)) (length s))
  4026. (setq c (aref s n))
  4027. (cond ((= c ?\ ) (setq i (1+ i)))
  4028. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  4029. (t (throw 'exit t)))))
  4030. i))
  4031. (defun org-renumber-ordered-list (arg)
  4032. "Renumber an ordered plain list.
  4033. Cursor needs to be in the first line of an item, the line that starts
  4034. with something like \"1.\" or \"2)\"."
  4035. (interactive "p")
  4036. (unless (and (org-at-item-p)
  4037. (match-beginning 3))
  4038. (error "This is not an ordered list"))
  4039. (let ((line (org-current-line))
  4040. (col (current-column))
  4041. (ind (org-get-string-indentation
  4042. (buffer-substring (point-at-bol) (match-beginning 3))))
  4043. ;; (term (substring (match-string 3) -1))
  4044. ind1 (n (1- arg)))
  4045. ;; find where this list begins
  4046. (catch 'exit
  4047. (while t
  4048. (catch 'next
  4049. (beginning-of-line 0)
  4050. (if (looking-at "[ \t]*$") (throw 'next t))
  4051. (skip-chars-forward " \t") (setq ind1 (current-column))
  4052. (if (or (< ind1 ind)
  4053. (and (= ind1 ind)
  4054. (not (org-at-item-p))))
  4055. (throw 'exit t)))))
  4056. ;; Walk forward and replace these numbers
  4057. (catch 'exit
  4058. (while t
  4059. (catch 'next
  4060. (beginning-of-line 2)
  4061. (if (eobp) (throw 'exit nil))
  4062. (if (looking-at "[ \t]*$") (throw 'next nil))
  4063. (skip-chars-forward " \t") (setq ind1 (current-column))
  4064. (if (> ind1 ind) (throw 'next t))
  4065. (if (< ind1 ind) (throw 'exit t))
  4066. (if (not (org-at-item-p)) (throw 'exit nil))
  4067. (if (not (match-beginning 3))
  4068. (error "unordered bullet in ordered list. Press \\[undo] to recover"))
  4069. (delete-region (match-beginning 3) (1- (match-end 3)))
  4070. (goto-char (match-beginning 3))
  4071. (insert (format "%d" (setq n (1+ n)))))))
  4072. (goto-line line)
  4073. (move-to-column col)))
  4074. (defvar org-last-indent-begin-marker (make-marker))
  4075. (defvar org-last-indent-end-marker (make-marker))
  4076. (defun org-outdent-item (arg)
  4077. "Outdent a local list item."
  4078. (interactive "p")
  4079. (org-indent-item (- arg)))
  4080. (defun org-indent-item (arg)
  4081. "Indent a local list item."
  4082. (interactive "p")
  4083. (unless (org-at-item-p)
  4084. (error "Not on an item"))
  4085. (save-excursion
  4086. (let (beg end ind ind1)
  4087. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  4088. (setq beg org-last-indent-begin-marker
  4089. end org-last-indent-end-marker)
  4090. (org-beginning-of-item)
  4091. (setq beg (move-marker org-last-indent-begin-marker (point)))
  4092. (org-end-of-item)
  4093. (setq end (move-marker org-last-indent-end-marker (point))))
  4094. (goto-char beg)
  4095. (skip-chars-forward " \t") (setq ind (current-column))
  4096. (if (< (+ arg ind) 0) (error "Cannot outdent beyond margin"))
  4097. (while (< (point) end)
  4098. (beginning-of-line 1)
  4099. (skip-chars-forward " \t") (setq ind1 (current-column))
  4100. (delete-region (point-at-bol) (point))
  4101. (indent-to-column (+ ind1 arg))
  4102. (beginning-of-line 2)))))
  4103. ;;; Archiving
  4104. (defun org-archive-subtree (&optional find-done)
  4105. "Move the current subtree to the archive.
  4106. The archive can be a certain top-level heading in the current file, or in
  4107. a different file. The tree will be moved to that location, the subtree
  4108. heading be marked DONE, and the current time will be added.
  4109. When called with prefix argument FIND-DONE, find whole trees without any
  4110. open TODO items and archive them (after getting confirmation from the user).
  4111. If the cursor is not at a headline when this comand is called, try all level
  4112. 1 trees. If the cursor is on a headline, only try the direct children of
  4113. this heading. "
  4114. (interactive "P")
  4115. (if find-done
  4116. (org-archive-all-done)
  4117. ;; Save all relevant TODO keyword-relatex variables
  4118. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  4119. (tr-org-todo-keywords org-todo-keywords)
  4120. (tr-org-todo-interpretation org-todo-interpretation)
  4121. (tr-org-done-string org-done-string)
  4122. (tr-org-todo-regexp org-todo-regexp)
  4123. (tr-org-todo-line-regexp org-todo-line-regexp)
  4124. (this-buffer (current-buffer))
  4125. file heading buffer level newfile-p)
  4126. (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
  4127. (progn
  4128. (setq file (format (match-string 1 org-archive-location)
  4129. (file-name-nondirectory buffer-file-name))
  4130. heading (match-string 2 org-archive-location)))
  4131. (error "Invalid `org-archive-location'"))
  4132. (if (> (length file) 0)
  4133. (setq newfile-p (not (file-exists-p file))
  4134. buffer (find-file-noselect file))
  4135. (setq buffer (current-buffer)))
  4136. (unless buffer
  4137. (error "Cannot access file \"%s\"" file))
  4138. (if (and (> (length heading) 0)
  4139. (string-match "^\\*+" heading))
  4140. (setq level (match-end 0))
  4141. (setq heading nil level 0))
  4142. (save-excursion
  4143. ;; We first only copy, in case something goes wrong
  4144. ;; we need to protect this-command, to avoid kill-region sets it,
  4145. ;; which would lead to duplication of subtrees
  4146. (let (this-command) (org-copy-subtree))
  4147. (set-buffer buffer)
  4148. ;; Enforce org-mode for the archive buffer
  4149. (if (not (org-mode-p))
  4150. ;; Force the mode for future visits.
  4151. (let ((org-insert-mode-line-in-empty-file t))
  4152. (call-interactively 'org-mode)))
  4153. (when newfile-p
  4154. (goto-char (point-max))
  4155. (insert (format "\nArchived entries from file %s\n\n"
  4156. (buffer-file-name this-buffer))))
  4157. ;; Force the TODO keywords of the original buffer
  4158. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  4159. (org-todo-keywords tr-org-todo-keywords)
  4160. (org-todo-interpretation tr-org-todo-interpretation)
  4161. (org-done-string tr-org-done-string)
  4162. (org-todo-regexp tr-org-todo-regexp)
  4163. (org-todo-line-regexp tr-org-todo-line-regexp))
  4164. (goto-char (point-min))
  4165. (if heading
  4166. (progn
  4167. (if (re-search-forward
  4168. (concat "\\(^\\|\r\\)"
  4169. (regexp-quote heading) "[ \t]*\\($\\|\r\\)")
  4170. nil t)
  4171. (goto-char (match-end 0))
  4172. ;; Heading not found, just insert it at the end
  4173. (goto-char (point-max))
  4174. (or (bolp) (insert "\n"))
  4175. (insert "\n" heading "\n")
  4176. (end-of-line 0))
  4177. ;; Make the subtree visible
  4178. (show-subtree)
  4179. (org-end-of-subtree t)
  4180. (skip-chars-backward " \t\r\n]")
  4181. (and (looking-at "[ \t\r\n]*")
  4182. (replace-match "\n\n")))
  4183. ;; No specific heading, just go to end of file.
  4184. (goto-char (point-max)) (insert "\n"))
  4185. ;; Paste
  4186. (org-paste-subtree (1+ level))
  4187. ;; Mark the entry as done, i.e. set to last work in org-todo-keywords
  4188. (if org-archive-mark-done
  4189. (org-todo (length org-todo-keywords)))
  4190. ;; Move cursor to right after the TODO keyword
  4191. (when org-archive-stamp-time
  4192. (beginning-of-line 1)
  4193. (looking-at org-todo-line-regexp)
  4194. (goto-char (or (match-end 2) (match-beginning 3)))
  4195. (insert "(" (format-time-string (cdr org-time-stamp-formats)
  4196. (org-current-time))
  4197. ")"))
  4198. ;; Save the buffer, if it is not the same buffer.
  4199. (if (not (eq this-buffer buffer)) (save-buffer))))
  4200. ;; Here we are back in the original buffer. Everything seems to have
  4201. ;; worked. So now cut the tree and finish up.
  4202. (let (this-command) (org-cut-subtree))
  4203. (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
  4204. (message "Subtree archived %s"
  4205. (if (eq this-buffer buffer)
  4206. (concat "under heading: " heading)
  4207. (concat "in file: " (abbreviate-file-name file)))))))
  4208. (defun org-archive-all-done (&optional tag)
  4209. "Archive sublevels of the current tree without open TODO items.
  4210. If the cursor is not on a headline, try all level 1 trees. If
  4211. it is on a headline, try all direct children.
  4212. When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
  4213. (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
  4214. (rea (concat ".*:" org-archive-tag ":"))
  4215. (begm (make-marker))
  4216. (endm (make-marker))
  4217. (question (if tag "Set ARCHIVE tag (no open TODO items)? "
  4218. "Move subtree to archive (no open TODO items)? "))
  4219. beg end (cntarch 0))
  4220. (if (org-on-heading-p)
  4221. (progn
  4222. (setq re1 (concat "^" (regexp-quote
  4223. (make-string
  4224. (1+ (- (match-end 0) (match-beginning 0)))
  4225. ?*))
  4226. " "))
  4227. (move-marker begm (point))
  4228. (move-marker endm (org-end-of-subtree)))
  4229. (setq re1 "^* ")
  4230. (move-marker begm (point-min))
  4231. (move-marker endm (point-max)))
  4232. (save-excursion
  4233. (goto-char begm)
  4234. (while (re-search-forward re1 endm t)
  4235. (setq beg (match-beginning 0)
  4236. end (save-excursion (org-end-of-subtree t) (point)))
  4237. (goto-char beg)
  4238. (if (re-search-forward re end t)
  4239. (goto-char end)
  4240. (goto-char beg)
  4241. (if (and (or (not tag) (not (looking-at rea)))
  4242. (y-or-n-p question))
  4243. (progn
  4244. (if tag
  4245. (org-toggle-tag org-archive-tag 'on)
  4246. (org-archive-subtree))
  4247. (setq cntarch (1+ cntarch)))
  4248. (goto-char end)))))
  4249. (message "%d trees archived" cntarch)))
  4250. (defun org-cycle-hide-archived-subtrees (state)
  4251. "Re-hide all archived subtrees after a visibility state change."
  4252. (when (and (not org-cycle-open-archived-trees)
  4253. (not (memq state '(overview folded))))
  4254. (save-excursion
  4255. (let* ((globalp (memq state '(contents all)))
  4256. (beg (if globalp (point-min) (point)))
  4257. (end (if globalp (point-max) (org-end-of-subtree))))
  4258. (org-hide-archived-subtrees beg end)
  4259. (goto-char beg)
  4260. (if (looking-at (concat ".*:" org-archive-tag ":"))
  4261. (message (substitute-command-keys
  4262. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  4263. (defun org-force-cycle-archived ()
  4264. "Cycle subtree even if it is archived."
  4265. (interactive)
  4266. (setq this-command 'org-cycle)
  4267. (let ((org-cycle-open-archived-trees t))
  4268. (call-interactively 'org-cycle)))
  4269. (defun org-hide-archived-subtrees (beg end)
  4270. "Re-hide all archived subtrees after a visibility state change."
  4271. (save-excursion
  4272. (let* ((re (concat ":" org-archive-tag ":")))
  4273. (goto-char beg)
  4274. (while (re-search-forward re end t)
  4275. (and (org-on-heading-p) (hide-subtree))
  4276. (org-end-of-subtree)))))
  4277. (defun org-toggle-tag (tag &optional onoff)
  4278. "Toggle the tag TAG for the current line.
  4279. If ONOFF is `on' or `off', don't toggle but set to this state."
  4280. (unless (org-on-heading-p) (error "Not on headling"))
  4281. (let (res current)
  4282. (save-excursion
  4283. (beginning-of-line)
  4284. (if (re-search-forward "[ \t]:\\([a-zA-Z0-9_@:]+\\):[ \t]*$"
  4285. (point-at-eol) t)
  4286. (progn
  4287. (setq current (match-string 1))
  4288. (replace-match ""))
  4289. (setq current ""))
  4290. (setq current (nreverse (org-split-string current ":")))
  4291. (cond
  4292. ((eq onoff 'on)
  4293. (setq res t)
  4294. (or (member tag current) (push tag current)))
  4295. ((eq onoff 'off)
  4296. (or (not (member tag current)) (setq current (delete tag current))))
  4297. (t (if (member tag current)
  4298. (setq current (delete tag current))
  4299. (setq res t)
  4300. (push tag current))))
  4301. (end-of-line 1)
  4302. (when current
  4303. (insert " :" (mapconcat 'identity (nreverse current) ":") ":"))
  4304. (org-set-tags nil t))
  4305. res))
  4306. (defun org-toggle-archive-tag (&optional arg)
  4307. "Toggle the archive tag for the current headline.
  4308. With prefix ARG, check all children of current headline and offer tagging
  4309. the children that do not contain any open TODO items."
  4310. (interactive "P")
  4311. (if arg
  4312. (org-archive-all-done 'tag)
  4313. (let (set)
  4314. (save-excursion
  4315. (org-back-to-heading t)
  4316. (setq set (org-toggle-tag org-archive-tag))
  4317. (when set (hide-subtree)))
  4318. (and set (beginning-of-line 1))
  4319. (message "Subtree %s" (if set "archived" "unarchived")))))
  4320. (defvar org-agenda-multi nil) ; dynammically scoped
  4321. (defvar org-agenda-buffer-name "*Org Agenda*")
  4322. (defun org-prepare-agenda ()
  4323. (if org-agenda-multi
  4324. (progn
  4325. (setq buffer-read-only nil)
  4326. (goto-char (point-max))
  4327. (unless (= (point) 1)
  4328. (insert "\n" (make-string (window-width) ?=) "\n"))
  4329. (narrow-to-region (point) (point-max)))
  4330. (org-agenda-maybe-reset-markers 'force)
  4331. (org-prepare-agenda-buffers (org-agenda-files))
  4332. (unless (equal (current-buffer) (get-buffer org-agenda-buffer-name))
  4333. (delete-other-windows)
  4334. (switch-to-buffer-other-window
  4335. (get-buffer-create org-agenda-buffer-name)))
  4336. (setq buffer-read-only nil)
  4337. (erase-buffer)
  4338. (org-agenda-mode))
  4339. (setq buffer-read-only nil)) ;;; FIXME: do we need all these occasions????
  4340. (defun org-prepare-agenda-buffers (files)
  4341. "Create buffers for all agenda files, protect archived trees and comments."
  4342. (interactive)
  4343. (let ((pa '(:org-archived t))
  4344. (pc '(:org-comment t))
  4345. (pall '(:org-archived t :org-comment t))
  4346. (rea (concat ":" org-archive-tag ":"))
  4347. bmp file re)
  4348. (save-excursion
  4349. (while (setq file (pop files))
  4350. (org-check-agenda-file file)
  4351. (set-buffer (org-get-agenda-file-buffer file))
  4352. (widen)
  4353. (setq bmp (buffer-modified-p))
  4354. (save-excursion
  4355. (remove-text-properties (point-min) (point-max) pall)
  4356. (when org-agenda-skip-archived-trees
  4357. (goto-char (point-min))
  4358. (while (re-search-forward rea nil t)
  4359. (if (org-on-heading-p)
  4360. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  4361. (goto-char (point-min))
  4362. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  4363. (while (re-search-forward re nil t)
  4364. (add-text-properties
  4365. (match-beginning 0) (org-end-of-subtree t) pc)))
  4366. (set-buffer-modified-p bmp)))))
  4367. (defun org-agenda-skip ()
  4368. "Throw to `:skip' in places that should be skipped."
  4369. (let ((p (point-at-bol)))
  4370. (and org-agenda-skip-archived-trees
  4371. (get-text-property p :org-archived)
  4372. (org-end-of-subtree)
  4373. (throw :skip t))
  4374. (and (get-text-property p :org-comment)
  4375. (org-end-of-subtree)
  4376. (throw :skip t))
  4377. (if (equal (char-after p) ?#) (throw :skip t))))
  4378. (defun org-agenda-toggle-archive-tag ()
  4379. "Toggle the archive tag for the current entry."
  4380. (interactive)
  4381. (org-agenda-check-no-diary)
  4382. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  4383. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  4384. (org-agenda-error)))
  4385. (buffer (marker-buffer hdmarker))
  4386. (pos (marker-position hdmarker))
  4387. (buffer-read-only nil)
  4388. newhead)
  4389. (with-current-buffer buffer
  4390. (widen)
  4391. (goto-char pos)
  4392. (org-show-hidden-entry)
  4393. (save-excursion
  4394. (and (outline-next-heading)
  4395. (org-flag-heading nil))) ; show the next heading
  4396. (call-interactively 'org-toggle-archive-tag)
  4397. (end-of-line 1)
  4398. (setq newhead (org-get-heading)))
  4399. (org-agenda-change-all-lines newhead hdmarker)
  4400. (beginning-of-line 1)))
  4401. ;;; Dynamic blocks
  4402. (defun org-find-dblock (name)
  4403. "Find the first dynamic block with name NAME in the buffer.
  4404. If not found, stay at current position and return nil."
  4405. (let (pos)
  4406. (save-excursion
  4407. (goto-char (point-min))
  4408. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  4409. nil t)
  4410. (match-beginning 0))))
  4411. (if pos (goto-char pos))
  4412. pos))
  4413. (defconst org-dblock-start-re
  4414. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  4415. "Matches the startline of a dynamic block, with parameters.")
  4416. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  4417. "Matches the end of a dyhamic block.")
  4418. (defun org-create-dblock (plist)
  4419. "Create a dynamic block section, with parameters taken from PLIST.
  4420. PLIST must containe a :name entry which is used as name of the block."
  4421. (unless (bolp) (newline))
  4422. (let ((name (plist-get plist :name)))
  4423. (insert "#+BEGIN: " name)
  4424. (while plist
  4425. (if (eq (car plist) :name)
  4426. (setq plist (cddr plist))
  4427. (insert " " (prin1-to-string (pop plist)))))
  4428. (insert "\n\n#+END:\n")
  4429. (beginning-of-line -2)))
  4430. (defun org-prepare-dblock ()
  4431. "Prepare dynamic block for refresh.
  4432. This empties the block, puts the cursor at the insert position and returns
  4433. the property list including an extra property :name with the block name."
  4434. (unless (looking-at org-dblock-start-re)
  4435. (error "Not at a dynamic block"))
  4436. (let* ((begdel (1+ (match-end 0)))
  4437. (name (match-string 1))
  4438. (params (append (list :name name)
  4439. (read (concat "(" (match-string 3) ")")))))
  4440. (unless (re-search-forward org-dblock-end-re nil t)
  4441. (error "Dynamic block not terminated"))
  4442. (delete-region begdel (match-beginning 0))
  4443. (goto-char begdel)
  4444. (open-line 1)
  4445. params))
  4446. (defun org-map-dblocks (&optional command)
  4447. "Apply COMMAND to all dynamic blocks in the current buffer.
  4448. If COMMAND is not given, use `org-update-dblock'."
  4449. (let ((cmd (or command 'org-update-dblock))
  4450. pos)
  4451. (save-excursion
  4452. (goto-char (point-min))
  4453. (while (re-search-forward org-dblock-start-re nil t)
  4454. (goto-char (setq pos (match-beginning 0)))
  4455. (condition-case nil
  4456. (funcall cmd)
  4457. (error (message "Error during update of dynamic block")))
  4458. (goto-char pos)
  4459. (unless (re-search-forward org-dblock-end-re nil t)
  4460. (error "Dynamic block not terminated"))))))
  4461. (defun org-dblock-update (&optional arg)
  4462. "User command for updating dynamic blocks.
  4463. Update the dynamic block at point. With prefix ARG, update all dynamic
  4464. blocks in the buffer."
  4465. (interactive "P")
  4466. (if arg
  4467. (org-update-all-dblocks)
  4468. (or (looking-at org-dblock-start-re)
  4469. (org-beginning-of-dblock))
  4470. (org-update-dblock)))
  4471. (defun org-update-dblock ()
  4472. "Update the dynamic block at point
  4473. This means to empty the block, parse for parameters and then call
  4474. the correct writing function."
  4475. (let* ((pos (point))
  4476. (params (org-prepare-dblock))
  4477. (name (plist-get params :name))
  4478. (cmd (intern (concat "org-dblock-write:" name))))
  4479. (funcall cmd params)
  4480. (goto-char pos)))
  4481. (defun org-beginning-of-dblock ()
  4482. "Find the beginning of the dynamic block at point.
  4483. Error if there is no scuh block at point."
  4484. (let ((pos (point))
  4485. beg)
  4486. (end-of-line 1)
  4487. (if (and (re-search-backward org-dblock-start-re nil t)
  4488. (setq beg (match-beginning 0))
  4489. (re-search-forward org-dblock-end-re nil t)
  4490. (> (match-end 0) pos))
  4491. (goto-char beg)
  4492. (goto-char pos)
  4493. (error "Not in a dynamic block"))))
  4494. (defun org-update-all-dblocks ()
  4495. "Update all dynamic blocks in the buffer.
  4496. This function can be used in a hook."
  4497. (when (org-mode-p)
  4498. (org-map-dblocks 'org-update-dblock)))
  4499. ;;; Completion
  4500. (defun org-complete (&optional arg)
  4501. "Perform completion on word at point.
  4502. At the beginning of a headline, this completes TODO keywords as given in
  4503. `org-todo-keywords'.
  4504. If the current word is preceded by a backslash, completes the TeX symbols
  4505. that are supported for HTML support.
  4506. If the current word is preceded by \"#+\", completes special words for
  4507. setting file options.
  4508. In the line after \"#+STARTUP:, complete valid keywords.\"
  4509. At all other locations, this simply calls `ispell-complete-word'."
  4510. (interactive "P")
  4511. (catch 'exit
  4512. (let* ((end (point))
  4513. (beg1 (save-excursion
  4514. ;FIXME??? (if (equal (char-before (point)) ?\ ) (backward-char 1))
  4515. (skip-chars-backward "a-zA-Z_@0-9")
  4516. (point)))
  4517. (beg (save-excursion
  4518. ;FIXME??? (if (equal (char-before (point)) ?\ ) (backward-char 1))
  4519. (skip-chars-backward "a-zA-Z0-9_:$")
  4520. (point)))
  4521. (confirm (lambda (x) (stringp (car x))))
  4522. (camel (equal (char-before beg) ?*))
  4523. (tag (equal (char-before beg1) ?:))
  4524. (texp (equal (char-before beg) ?\\))
  4525. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  4526. beg)
  4527. "#+"))
  4528. (startup (string-match "^#\\+STARTUP:.*"
  4529. (buffer-substring (point-at-bol) (point))))
  4530. (completion-ignore-case opt)
  4531. (type nil)
  4532. (tbl nil)
  4533. (table (cond
  4534. (opt
  4535. (setq type :opt)
  4536. (mapcar (lambda (x)
  4537. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  4538. (cons (match-string 2 x) (match-string 1 x)))
  4539. (org-split-string (org-get-current-options) "\n")))
  4540. (startup
  4541. (setq type :startup)
  4542. org-startup-options)
  4543. (texp
  4544. (setq type :tex)
  4545. org-html-entities)
  4546. ((string-match "\\`\\*+[ \t]*\\'"
  4547. (buffer-substring (point-at-bol) beg))
  4548. (setq type :todo)
  4549. (mapcar 'list org-todo-keywords))
  4550. (camel
  4551. (setq type :camel)
  4552. (save-excursion
  4553. (goto-char (point-min))
  4554. (while (re-search-forward org-todo-line-regexp nil t)
  4555. (push (list
  4556. (if org-file-link-context-use-camel-case
  4557. (org-make-org-heading-camel (match-string 3) t)
  4558. (org-make-org-heading-search-string
  4559. (match-string 3) t)))
  4560. tbl)))
  4561. tbl)
  4562. (tag (setq type :tag beg beg1)
  4563. (or org-tag-alist (org-get-buffer-tags)))
  4564. (t (progn (ispell-complete-word arg) (throw 'exit nil)))))
  4565. (pattern (buffer-substring-no-properties beg end))
  4566. (completion (try-completion pattern table confirm)))
  4567. (cond ((eq completion t)
  4568. (if (equal type :opt)
  4569. (insert (substring (cdr (assoc (upcase pattern) table))
  4570. (length pattern)))))
  4571. ((null completion)
  4572. (message "Can't find completion for \"%s\"" pattern)
  4573. (ding))
  4574. ((not (string= pattern completion))
  4575. (delete-region beg end)
  4576. (if (string-match " +$" completion)
  4577. (setq completion (replace-match "" t t completion)))
  4578. (insert completion)
  4579. (if (get-buffer-window "*Completions*")
  4580. (delete-window (get-buffer-window "*Completions*")))
  4581. (if (assoc completion table)
  4582. (if (eq type :todo) (insert " ")
  4583. (if (eq type :tag) (insert ":"))))
  4584. (if (and (equal type :opt) (assoc completion table))
  4585. (message "%s" (substitute-command-keys
  4586. "Press \\[org-complete] again to insert example settings"))))
  4587. (t
  4588. (message "Making completion list...")
  4589. (let ((list (sort (all-completions pattern table confirm)
  4590. 'string<)))
  4591. (with-output-to-temp-buffer "*Completions*"
  4592. (condition-case nil
  4593. ;; Protection needed for XEmacs and emacs 21
  4594. (display-completion-list list pattern)
  4595. (error (display-completion-list list)))))
  4596. (message "Making completion list...%s" "done"))))))
  4597. ;;; Comments, TODO and DEADLINE
  4598. (defun org-toggle-comment ()
  4599. "Change the COMMENT state of an entry."
  4600. (interactive)
  4601. (save-excursion
  4602. (org-back-to-heading)
  4603. (if (looking-at (concat outline-regexp
  4604. "\\( +\\<" org-comment-string "\\>\\)"))
  4605. (replace-match "" t t nil 1)
  4606. (if (looking-at outline-regexp)
  4607. (progn
  4608. (goto-char (match-end 0))
  4609. (insert " " org-comment-string))))))
  4610. (defvar org-last-todo-state-is-todo nil
  4611. "This is non-nil when the last TODO state change led to a TODO state.
  4612. If the last change removed the TODO tag or switched to DONE, then
  4613. this is nil.")
  4614. (defun org-todo (&optional arg)
  4615. "Change the TODO state of an item.
  4616. The state of an item is given by a keyword at the start of the heading,
  4617. like
  4618. *** TODO Write paper
  4619. *** DONE Call mom
  4620. The different keywords are specified in the variable `org-todo-keywords'.
  4621. By default the available states are \"TODO\" and \"DONE\".
  4622. So for this example: when the item starts with TODO, it is changed to DONE.
  4623. When it starts with DONE, the DONE is removed. And when neither TODO nor
  4624. DONE are present, add TODO at the beginning of the heading.
  4625. With prefix arg, use completion to determine the new state. With numeric
  4626. prefix arg, switch to that state."
  4627. (interactive "P")
  4628. (save-excursion
  4629. (org-back-to-heading)
  4630. (if (looking-at outline-regexp) (goto-char (match-end 0)))
  4631. (or (looking-at (concat " +" org-todo-regexp " *"))
  4632. (looking-at " *"))
  4633. (let* ((this (match-string 1))
  4634. (completion-ignore-case t)
  4635. (member (member this org-todo-keywords))
  4636. (tail (cdr member))
  4637. (state (cond
  4638. ((equal arg '(4))
  4639. ;; Read a state with completion
  4640. (completing-read "State: " (mapcar (lambda(x) (list x))
  4641. org-todo-keywords)
  4642. nil t))
  4643. ((eq arg 'right)
  4644. (if this
  4645. (if tail (car tail) nil)
  4646. (car org-todo-keywords)))
  4647. ((eq arg 'left)
  4648. (if (equal member org-todo-keywords)
  4649. nil
  4650. (if this
  4651. (nth (- (length org-todo-keywords) (length tail) 2)
  4652. org-todo-keywords)
  4653. org-done-string)))
  4654. (arg
  4655. ;; user requests a specific state
  4656. (nth (1- (prefix-numeric-value arg))
  4657. org-todo-keywords))
  4658. ((null member) (car org-todo-keywords))
  4659. ((null tail) nil) ;; -> first entry
  4660. ((eq org-todo-interpretation 'sequence)
  4661. (car tail))
  4662. ((memq org-todo-interpretation '(type priority))
  4663. (if (eq this-command last-command)
  4664. (car tail)
  4665. (if (> (length tail) 0) org-done-string nil)))
  4666. (t nil)))
  4667. (next (if state (concat " " state " ") " ")))
  4668. (replace-match next t t)
  4669. (setq org-last-todo-state-is-todo
  4670. (not (equal state org-done-string)))
  4671. (when org-log-done
  4672. (if (equal state org-done-string)
  4673. (org-add-planning-info 'closed (current-time) 'scheduled)
  4674. (if (not this)
  4675. (org-add-planning-info nil nil 'closed))))
  4676. ;; Fixup tag positioning
  4677. (and org-auto-align-tags (org-set-tags nil t))
  4678. (run-hooks 'org-after-todo-state-change-hook)))
  4679. ;; Fixup cursor location if close to the keyword
  4680. (if (and (outline-on-heading-p)
  4681. (not (bolp))
  4682. (save-excursion (beginning-of-line 1)
  4683. (looking-at org-todo-line-regexp))
  4684. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  4685. (progn
  4686. (goto-char (or (match-end 2) (match-end 1)))
  4687. (just-one-space))))
  4688. (defun org-log-done (&optional undone)
  4689. "Add a time stamp logging that a TODO entry has been closed.
  4690. When UNDONE is non-nil, remove such a time stamp again."
  4691. (interactive)
  4692. (let (beg end col)
  4693. (save-excursion
  4694. (org-back-to-heading t)
  4695. (setq beg (point))
  4696. (looking-at (concat outline-regexp " *"))
  4697. (goto-char (match-end 0))
  4698. (setq col (current-column))
  4699. (outline-next-heading)
  4700. (setq end (point))
  4701. (goto-char beg)
  4702. (when (re-search-forward (concat
  4703. "[\r\n]\\([ \t]*"
  4704. (regexp-quote org-closed-string)
  4705. " *\\[.*?\\][^\n\r]*[\n\r]?\\)") end t)
  4706. (delete-region (match-beginning 1) (match-end 1)))
  4707. (unless undone
  4708. (org-back-to-heading t)
  4709. (skip-chars-forward "^\n\r")
  4710. (goto-char (min (1+ (point)) (point-max)))
  4711. (when (not (member (char-before) '(?\r ?\n)))
  4712. (insert "\n"))
  4713. (indent-to col)
  4714. (insert org-closed-string " "
  4715. (format-time-string
  4716. (concat "[" (substring (cdr org-time-stamp-formats) 1 -1) "]")
  4717. (org-current-time))
  4718. "\n")))))
  4719. (defun org-show-todo-tree (arg)
  4720. "Make a compact tree which shows all headlines marked with TODO.
  4721. The tree will show the lines where the regexp matches, and all higher
  4722. headlines above the match.
  4723. With \\[universal-argument] prefix, also show the DONE entries.
  4724. With a numeric prefix N, construct a sparse tree for the Nth element
  4725. of `org-todo-keywords'."
  4726. (interactive "P")
  4727. (let ((case-fold-search nil)
  4728. (kwd-re
  4729. (cond ((null arg) org-not-done-regexp)
  4730. ((equal arg '(4)) org-todo-regexp)
  4731. ((<= (prefix-numeric-value arg) (length org-todo-keywords))
  4732. (regexp-quote (nth (1- (prefix-numeric-value arg))
  4733. org-todo-keywords)))
  4734. (t (error "Invalid prefix argument: %s" arg)))))
  4735. (message "%d TODO entries found"
  4736. (org-occur (concat "^" outline-regexp " +" kwd-re )))))
  4737. (defun org-deadline ()
  4738. "Insert the DEADLINE: string to make a deadline.
  4739. A timestamp is also inserted - use \\[org-timestamp-up] and \\[org-timestamp-down]
  4740. to modify it to the correct date."
  4741. (interactive)
  4742. (org-add-planning-info 'deadline nil 'closed))
  4743. (defun org-schedule ()
  4744. "Insert the SCHEDULED: string to schedule a TODO item.
  4745. A timestamp is also inserted - use \\[org-timestamp-up] and \\[org-timestamp-down]
  4746. to modify it to the correct date."
  4747. (interactive)
  4748. (org-add-planning-info 'scheduled nil 'closed))
  4749. (defun org-add-planning-info (what &optional time &rest remove)
  4750. "Insert new timestamp with keyword in the line directly after the headline.
  4751. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  4752. If non is given, the user is prompted for a date.
  4753. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  4754. be removed."
  4755. (interactive)
  4756. (when what (setq time (or time (org-read-date nil 'to-time))))
  4757. (when (and org-insert-labeled-timestamps-at-point
  4758. (member what '(scheduled deadline)))
  4759. (insert
  4760. (if (eq what 'scheduled) org-scheduled-string org-deadline-string)
  4761. " "
  4762. (format-time-string (car org-time-stamp-formats) time))
  4763. (setq what nil))
  4764. (save-excursion
  4765. (let (col list elt (buffer-invisibility-spec nil) ts)
  4766. (org-back-to-heading t)
  4767. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  4768. (goto-char (match-end 1))
  4769. (setq col (current-column))
  4770. (goto-char (1+ (match-end 0)))
  4771. (if (and (not (looking-at outline-regexp))
  4772. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  4773. "[^\r\n]*"))
  4774. (not (equal (match-string 1) org-clock-string)))
  4775. (narrow-to-region (match-beginning 0) (match-end 0))
  4776. (insert "\n")
  4777. (backward-char 1)
  4778. (narrow-to-region (point) (point))
  4779. (indent-to-column col))
  4780. ;; Check if we have to remove something.
  4781. (setq list (cons what remove))
  4782. (while list
  4783. (setq elt (pop list))
  4784. (goto-char (point-min))
  4785. (when (or (and (eq elt 'scheduled)
  4786. (re-search-forward org-scheduled-time-regexp nil t))
  4787. (and (eq elt 'deadline)
  4788. (re-search-forward org-deadline-time-regexp nil t))
  4789. (and (eq elt 'closed)
  4790. (re-search-forward org-closed-time-regexp nil t)))
  4791. (replace-match "")
  4792. (if (looking-at " +") (replace-match ""))))
  4793. (goto-char (point-max))
  4794. (when what
  4795. (insert
  4796. (if (not (equal (char-before) ?\ )) " " "")
  4797. (cond ((eq what 'scheduled) org-scheduled-string)
  4798. ((eq what 'deadline) org-deadline-string)
  4799. ((eq what 'closed) org-closed-string))
  4800. " ")
  4801. (insert
  4802. (setq ts
  4803. (format-time-string
  4804. (if (eq what 'closed)
  4805. (concat "[" (substring (cdr org-time-stamp-formats) 1 -1) "]")
  4806. (car org-time-stamp-formats))
  4807. time))))
  4808. (goto-char (point-min))
  4809. (widen)
  4810. (if (looking-at "[ \t]+\r?\n")
  4811. (replace-match ""))
  4812. ts)))
  4813. (defun org-occur (regexp &optional callback)
  4814. "Make a compact tree which shows all matches of REGEXP.
  4815. The tree will show the lines where the regexp matches, and all higher
  4816. headlines above the match. It will also show the heading after the match,
  4817. to make sure editing the matching entry is easy.
  4818. If CALLBACK is non-nil, it is a function which is called to confirm
  4819. that the match should indeed be shown."
  4820. (interactive "sRegexp: ")
  4821. (org-remove-occur-highlights nil nil t)
  4822. (let ((cnt 0))
  4823. (save-excursion
  4824. (goto-char (point-min))
  4825. (org-overview)
  4826. (while (re-search-forward regexp nil t)
  4827. (when (or (not callback)
  4828. (save-match-data (funcall callback)))
  4829. (setq cnt (1+ cnt))
  4830. (org-highlight-new-match (match-beginning 0) (match-end 0))
  4831. (org-show-hierarchy-above))))
  4832. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  4833. nil 'local)
  4834. (unless org-sparse-tree-open-archived-trees
  4835. (org-hide-archived-subtrees (point-min) (point-max)))
  4836. (run-hooks 'org-occur-hook)
  4837. (if (interactive-p)
  4838. (message "%d match(es) for regexp %s" cnt regexp))
  4839. cnt))
  4840. (defun org-show-hierarchy-above ()
  4841. "Make sure point and the headings hierarchy above is visible."
  4842. (catch 'exit
  4843. (if (org-on-heading-p t)
  4844. (org-flag-heading nil) ; only show the heading
  4845. (and (or (org-invisible-p) (org-invisible-p2))
  4846. (org-show-hidden-entry))) ; show entire entry
  4847. (save-excursion
  4848. (and org-show-following-heading
  4849. (outline-next-heading)
  4850. (org-flag-heading nil))) ; show the next heading
  4851. (when org-show-hierarchy-above
  4852. (save-excursion ; show all higher headings
  4853. (while (and (condition-case nil
  4854. (progn (org-up-heading-all 1) t)
  4855. (error nil))
  4856. (not (bobp)))
  4857. (org-flag-heading nil))))))
  4858. ;; Overlay compatibility functions
  4859. (defun org-make-overlay (beg end &optional buffer)
  4860. (if (featurep 'xemacs)
  4861. (make-extent beg end buffer)
  4862. (make-overlay beg end buffer)))
  4863. (defun org-delete-overlay (ovl)
  4864. (if (featurep 'xemacs) (delete-extent ovl) (delete-overlay ovl)))
  4865. (defun org-detatch-overlay (ovl)
  4866. (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
  4867. (defun org-move-overlay (ovl beg end &optional buffer)
  4868. (if (featurep 'xemacs)
  4869. (set-extent-endpoints ovl beg end buffer)
  4870. (move-overlay ovl beg end buffer)))
  4871. (defun org-overlay-put (ovl prop value)
  4872. (if (featurep 'xemacs)
  4873. (set-extent-property ovl prop value)
  4874. (overlay-put ovl prop value)))
  4875. (defun org-overlays-at (pos)
  4876. (if (featurep 'xemacs) (extents-at pos) (overlays-at pos)))
  4877. (defun org-overlay-start (o)
  4878. (if (featurep 'xemacs) (extent-start-position o) (overlay-start o)))
  4879. (defun org-overlay-end (o)
  4880. (if (featurep 'xemacs) (extent-end-position o) (overlay-end o)))
  4881. (defvar org-occur-highlights nil)
  4882. (make-variable-buffer-local 'org-occur-highlights)
  4883. (defun org-highlight-new-match (beg end)
  4884. "Highlight from BEG to END and mark the highlight is an occur headline."
  4885. (let ((ov (org-make-overlay beg end)))
  4886. (org-overlay-put ov 'face 'secondary-selection)
  4887. (push ov org-occur-highlights)))
  4888. (defvar org-inhibit-highlight-removal nil)
  4889. (defun org-remove-occur-highlights (&optional beg end noremove)
  4890. "Remove the occur highlights from the buffer.
  4891. BEG and END are ignored. If NOREMOVE is nil, remove this function
  4892. from the `before-change-functions' in the current buffer."
  4893. (interactive)
  4894. (unless org-inhibit-highlight-removal
  4895. (mapc 'org-delete-overlay org-occur-highlights)
  4896. (setq org-occur-highlights nil)
  4897. (unless noremove
  4898. (remove-hook 'before-change-functions
  4899. 'org-remove-occur-highlights 'local))))
  4900. ;;; Priorities
  4901. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z]\\)\\] ?\\)"
  4902. "Regular expression matching the priority indicator.")
  4903. (defvar org-remove-priority-next-time nil)
  4904. (defun org-priority-up ()
  4905. "Increase the priority of the current item."
  4906. (interactive)
  4907. (org-priority 'up))
  4908. (defun org-priority-down ()
  4909. "Decrease the priority of the current item."
  4910. (interactive)
  4911. (org-priority 'down))
  4912. (defun org-priority (&optional action)
  4913. "Change the priority of an item by ARG.
  4914. ACTION can be set, up, or down."
  4915. (interactive)
  4916. (setq action (or action 'set))
  4917. (let (current new news have remove)
  4918. (save-excursion
  4919. (org-back-to-heading)
  4920. (if (looking-at org-priority-regexp)
  4921. (setq current (string-to-char (match-string 2))
  4922. have t)
  4923. (setq current org-default-priority))
  4924. (cond
  4925. ((eq action 'set)
  4926. (message "Priority A-%c, SPC to remove: " org-lowest-priority)
  4927. (setq new (read-char-exclusive))
  4928. (cond ((equal new ?\ ) (setq remove t))
  4929. ((or (< (upcase new) ?A) (> (upcase new) org-lowest-priority))
  4930. (error "Priority must be between `%c' and `%c'"
  4931. ?A org-lowest-priority))))
  4932. ((eq action 'up)
  4933. (setq new (1- current)))
  4934. ((eq action 'down)
  4935. (setq new (1+ current)))
  4936. (t (error "Invalid action")))
  4937. (setq new (min (max ?A (upcase new)) org-lowest-priority))
  4938. (setq news (format "%c" new))
  4939. (if have
  4940. (if remove
  4941. (replace-match "" t t nil 1)
  4942. (replace-match news t t nil 2))
  4943. (if remove
  4944. (error "No priority cookie found in line")
  4945. (looking-at org-todo-line-regexp)
  4946. (if (match-end 2)
  4947. (progn
  4948. (goto-char (match-end 2))
  4949. (insert " [#" news "]"))
  4950. (goto-char (match-beginning 3))
  4951. (insert "[#" news "] ")))))
  4952. (if remove
  4953. (message "Priority removed")
  4954. (message "Priority of current item set to %s" news))))
  4955. (defun org-get-priority (s)
  4956. "Find priority cookie and return priority."
  4957. (save-match-data
  4958. (if (not (string-match org-priority-regexp s))
  4959. (* 1000 (- org-lowest-priority org-default-priority))
  4960. (* 1000 (- org-lowest-priority
  4961. (string-to-char (match-string 2 s)))))))
  4962. ;;; Timestamps
  4963. (defvar org-last-changed-timestamp nil)
  4964. (defun org-time-stamp (arg)
  4965. "Prompt for a date/time and insert a time stamp.
  4966. If the user specifies a time like HH:MM, or if this command is called
  4967. with a prefix argument, the time stamp will contain date and time.
  4968. Otherwise, only the date will be included. All parts of a date not
  4969. specified by the user will be filled in from the current date/time.
  4970. So if you press just return without typing anything, the time stamp
  4971. will represent the current date/time. If there is already a timestamp
  4972. at the cursor, it will be modified."
  4973. (interactive "P")
  4974. (let ((fmt (if arg (cdr org-time-stamp-formats)
  4975. (car org-time-stamp-formats)))
  4976. (org-time-was-given nil)
  4977. time)
  4978. (cond
  4979. ((and (org-at-timestamp-p)
  4980. (eq last-command 'org-time-stamp)
  4981. (eq this-command 'org-time-stamp))
  4982. (insert "--")
  4983. (setq time (let ((this-command this-command))
  4984. (org-read-date arg 'totime)))
  4985. (if org-time-was-given (setq fmt (cdr org-time-stamp-formats)))
  4986. (insert (format-time-string fmt time)))
  4987. ((org-at-timestamp-p)
  4988. (setq time (let ((this-command this-command))
  4989. (org-read-date arg 'totime)))
  4990. (and (org-at-timestamp-p) (replace-match
  4991. (setq org-last-changed-timestamp
  4992. (format-time-string fmt time))
  4993. t t))
  4994. (message "Timestamp updated"))
  4995. (t
  4996. (setq time (let ((this-command this-command))
  4997. (org-read-date arg 'totime)))
  4998. (if org-time-was-given (setq fmt (cdr org-time-stamp-formats)))
  4999. (insert (format-time-string fmt time))))))
  5000. (defun org-time-stamp-inactive (&optional arg)
  5001. "Insert an inactive time stamp.
  5002. An inactive time stamp is enclosed in square brackets instead of angle
  5003. brackets. It is inactive in the sense that it does not trigger agenda entries,
  5004. does not link to the calendar and cannot be changed with the S-cursor keys.
  5005. So these are more for recording a certain time/date."
  5006. (interactive "P")
  5007. (let ((fmt (if arg (cdr org-time-stamp-formats)
  5008. (car org-time-stamp-formats)))
  5009. (org-time-was-given nil)
  5010. time)
  5011. (setq time (org-read-date arg 'totime))
  5012. (if org-time-was-given (setq fmt (cdr org-time-stamp-formats)))
  5013. (setq fmt (concat "[" (substring fmt 1 -1) "]"))
  5014. (insert (format-time-string fmt time))))
  5015. (defvar org-date-ovl (org-make-overlay 1 1))
  5016. (org-overlay-put org-date-ovl 'face 'org-warning)
  5017. (org-detatch-overlay org-date-ovl)
  5018. (defun org-read-date (&optional with-time to-time)
  5019. "Read a date and make things smooth for the user.
  5020. The prompt will suggest to enter an ISO date, but you can also enter anything
  5021. which will at least partially be understood by `parse-time-string'.
  5022. Unrecognized parts of the date will default to the current day, month, year,
  5023. hour and minute. For example,
  5024. 3-2-5 --> 2003-02-05
  5025. feb 15 --> currentyear-02-15
  5026. sep 12 9 --> 2009-09-12
  5027. 12:45 --> today 12:45
  5028. 22 sept 0:34 --> currentyear-09-22 0:34
  5029. 12 --> currentyear-currentmonth-12
  5030. Fri --> nearest Friday (today or later)
  5031. etc.
  5032. The function understands only English month and weekday abbreviations,
  5033. but this can be configured with the variables `parse-time-months' and
  5034. `parse-time-weekdays'.
  5035. While prompting, a calendar is popped up - you can also select the
  5036. date with the mouse (button 1). The calendar shows a period of three
  5037. months. To scroll it to other months, use the keys `>' and `<'.
  5038. If you don't like the calendar, turn it off with
  5039. \(setq org-popup-calendar-for-date-prompt nil)
  5040. With optional argument TO-TIME, the date will immediately be converted
  5041. to an internal time.
  5042. With an optional argument WITH-TIME, the prompt will suggest to also
  5043. insert a time. Note that when WITH-TIME is not set, you can still
  5044. enter a time, and this function will inform the calling routine about
  5045. this change. The calling routine may then choose to change the format
  5046. used to insert the time stamp into the buffer to include the time."
  5047. (require 'parse-time)
  5048. (let* ((org-time-stamp-rounding-minutes
  5049. (if (equal with-time '(16)) 0 org-time-stamp-rounding-minutes))
  5050. (ct (org-current-time))
  5051. (default-time
  5052. ;; Default time is either today, or, when entering a range,
  5053. ;; the range start.
  5054. (if (save-excursion
  5055. (re-search-backward
  5056. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  5057. (- (point) 20) t))
  5058. (apply
  5059. 'encode-time
  5060. (mapcar (lambda(x) (or x 0))
  5061. (parse-time-string (match-string 1))))
  5062. ct))
  5063. (calendar-move-hook nil)
  5064. (view-diary-entries-initially nil)
  5065. (view-calendar-holidays-initially nil)
  5066. (timestr (format-time-string
  5067. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") default-time))
  5068. (prompt (format "YYYY-MM-DD [%s]: " timestr))
  5069. ans ans1 ans2
  5070. second minute hour day month year tl wday wday1)
  5071. (if org-popup-calendar-for-date-prompt
  5072. (save-excursion
  5073. (save-window-excursion
  5074. (calendar)
  5075. (calendar-forward-day (- (time-to-days default-time)
  5076. (calendar-absolute-from-gregorian
  5077. (calendar-current-date))))
  5078. (org-eval-in-calendar nil)
  5079. (let* ((old-map (current-local-map))
  5080. (map (copy-keymap calendar-mode-map))
  5081. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  5082. (define-key map (kbd "RET") 'org-calendar-select)
  5083. (define-key map (if (featurep 'xemacs) [button1] [mouse-1])
  5084. 'org-calendar-select-mouse)
  5085. (define-key map (if (featurep 'xemacs) [button2] [mouse-2])
  5086. 'org-calendar-select-mouse)
  5087. (define-key minibuffer-local-map [(meta shift left)]
  5088. (lambda () (interactive)
  5089. (org-eval-in-calendar '(calendar-backward-month 1))))
  5090. (define-key minibuffer-local-map [(meta shift right)]
  5091. (lambda () (interactive)
  5092. (org-eval-in-calendar '(calendar-forward-month 1))))
  5093. (define-key minibuffer-local-map [(shift up)]
  5094. (lambda () (interactive)
  5095. (org-eval-in-calendar '(calendar-backward-week 1))))
  5096. (define-key minibuffer-local-map [(shift down)]
  5097. (lambda () (interactive)
  5098. (org-eval-in-calendar '(calendar-forward-week 1))))
  5099. (define-key minibuffer-local-map [(shift left)]
  5100. (lambda () (interactive)
  5101. (org-eval-in-calendar '(calendar-backward-day 1))))
  5102. (define-key minibuffer-local-map [(shift right)]
  5103. (lambda () (interactive)
  5104. (org-eval-in-calendar '(calendar-forward-day 1))))
  5105. (define-key minibuffer-local-map ">"
  5106. (lambda () (interactive)
  5107. (org-eval-in-calendar '(scroll-calendar-left 1))))
  5108. (define-key minibuffer-local-map "<"
  5109. (lambda () (interactive)
  5110. (org-eval-in-calendar '(scroll-calendar-right 1))))
  5111. (unwind-protect
  5112. (progn
  5113. (use-local-map map)
  5114. (setq ans (read-string prompt "" nil nil))
  5115. (if (not (string-match "\\S-" ans)) (setq ans nil))
  5116. (setq ans (or ans1 ans ans2)))
  5117. (use-local-map old-map)))))
  5118. ;; Naked prompt only
  5119. (setq ans (read-string prompt "" nil timestr)))
  5120. (org-detatch-overlay org-date-ovl)
  5121. (if (string-match
  5122. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  5123. (progn
  5124. (setq year (if (match-end 2)
  5125. (string-to-number (match-string 2 ans))
  5126. (string-to-number (format-time-string "%Y")))
  5127. month (string-to-number (match-string 3 ans))
  5128. day (string-to-number (match-string 4 ans)))
  5129. (if (< year 100) (setq year (+ 2000 year)))
  5130. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  5131. t nil ans))))
  5132. (setq tl (parse-time-string ans)
  5133. year (or (nth 5 tl) (string-to-number (format-time-string "%Y" ct)))
  5134. month (or (nth 4 tl) (string-to-number (format-time-string "%m" ct)))
  5135. day (or (nth 3 tl) (string-to-number (format-time-string "%d" ct)))
  5136. hour (or (nth 2 tl) (string-to-number (format-time-string "%H" ct)))
  5137. minute (or (nth 1 tl) (string-to-number (format-time-string "%M" ct)))
  5138. second (or (nth 0 tl) 0)
  5139. wday (nth 6 tl))
  5140. (when (and wday (not (nth 3 tl)))
  5141. ;; Weekday was given, but no day, so pick that day in the week
  5142. ;; on or after the derived date.
  5143. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  5144. (unless (equal wday wday1)
  5145. (setq day (+ day (% (- wday wday1 -7) 7)))))
  5146. (if (and (boundp 'org-time-was-given)
  5147. (nth 2 tl))
  5148. (setq org-time-was-given t))
  5149. (if (< year 100) (setq year (+ 2000 year)))
  5150. (if to-time
  5151. (encode-time second minute hour day month year)
  5152. (if (or (nth 1 tl) (nth 2 tl))
  5153. (format "%04d-%02d-%02d %02d:%02d" year month day hour minute)
  5154. (format "%04d-%02d-%02d" year month day)))))
  5155. (defun org-eval-in-calendar (form)
  5156. "Eval FORM in the calendar window and return to current window.
  5157. Also, store the cursor date in variable ans2."
  5158. (let ((sw (selected-window)))
  5159. (select-window (get-buffer-window "*Calendar*"))
  5160. (eval form)
  5161. (when (calendar-cursor-to-date)
  5162. (let* ((date (calendar-cursor-to-date))
  5163. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  5164. (setq ans2 (format-time-string "%Y-%m-%d" time))))
  5165. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  5166. (select-window sw)))
  5167. (defun org-calendar-select ()
  5168. "Return to `org-read-date' with the date currently selected.
  5169. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  5170. (interactive)
  5171. (when (calendar-cursor-to-date)
  5172. (let* ((date (calendar-cursor-to-date))
  5173. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  5174. (setq ans1 (format-time-string "%Y-%m-%d" time)))
  5175. (if (active-minibuffer-window) (exit-minibuffer))))
  5176. (defun org-calendar-select-mouse (ev)
  5177. "Return to `org-read-date' with the date currently selected.
  5178. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  5179. (interactive "e")
  5180. (mouse-set-point ev)
  5181. (when (calendar-cursor-to-date)
  5182. (let* ((date (calendar-cursor-to-date))
  5183. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  5184. (setq ans1 (format-time-string "%Y-%m-%d" time)))
  5185. (if (active-minibuffer-window) (exit-minibuffer))))
  5186. (defun org-check-deadlines (ndays)
  5187. "Check if there are any deadlines due or past due.
  5188. A deadline is considered due if it happens within `org-deadline-warning-days'
  5189. days from today's date. If the deadline appears in an entry marked DONE,
  5190. it is not shown. The prefix arg NDAYS can be used to test that many
  5191. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  5192. (interactive "P")
  5193. (let* ((org-warn-days
  5194. (cond
  5195. ((equal ndays '(4)) 100000)
  5196. (ndays (prefix-numeric-value ndays))
  5197. (t org-deadline-warning-days)))
  5198. (case-fold-search nil)
  5199. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  5200. (callback
  5201. (lambda ()
  5202. (and (let ((d1 (time-to-days (current-time)))
  5203. (d2 (time-to-days
  5204. (org-time-string-to-time (match-string 1)))))
  5205. (< (- d2 d1) org-warn-days))
  5206. (not (org-entry-is-done-p))))))
  5207. (message "%d deadlines past-due or due within %d days"
  5208. (org-occur regexp callback)
  5209. org-warn-days)))
  5210. (defun org-evaluate-time-range (&optional to-buffer)
  5211. "Evaluate a time range by computing the difference between start and end.
  5212. Normally the result is just printed in the echo area, but with prefix arg
  5213. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  5214. If the time range is actually in a table, the result is inserted into the
  5215. next column.
  5216. For time difference computation, a year is assumed to be exactly 365
  5217. days in order to avoid rounding problems."
  5218. (interactive "P")
  5219. (or
  5220. (org-clock-update-time-maybe)
  5221. (save-excursion
  5222. (unless (org-at-date-range-p)
  5223. (goto-char (point-at-bol))
  5224. (re-search-forward org-tr-regexp (point-at-eol) t))
  5225. (if (not (org-at-date-range-p))
  5226. (error "Not at a time-stamp range, and none found in current line")))
  5227. (let* ((ts1 (match-string 1))
  5228. (ts2 (match-string 2))
  5229. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  5230. (match-end (match-end 0))
  5231. (time1 (org-time-string-to-time ts1))
  5232. (time2 (org-time-string-to-time ts2))
  5233. (t1 (time-to-seconds time1))
  5234. (t2 (time-to-seconds time2))
  5235. (diff (abs (- t2 t1)))
  5236. (negative (< (- t2 t1) 0))
  5237. ;; (ys (floor (* 365 24 60 60)))
  5238. (ds (* 24 60 60))
  5239. (hs (* 60 60))
  5240. (fy "%dy %dd %02d:%02d")
  5241. (fy1 "%dy %dd")
  5242. (fd "%dd %02d:%02d")
  5243. (fd1 "%dd")
  5244. (fh "%02d:%02d")
  5245. y d h m align)
  5246. (if havetime
  5247. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  5248. y 0
  5249. d (floor (/ diff ds)) diff (mod diff ds)
  5250. h (floor (/ diff hs)) diff (mod diff hs)
  5251. m (floor (/ diff 60)))
  5252. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  5253. y 0
  5254. d (floor (+ (/ diff ds) 0.5))
  5255. h 0 m 0))
  5256. (if (not to-buffer)
  5257. (message (org-make-tdiff-string y d h m))
  5258. (when (org-at-table-p)
  5259. (goto-char match-end)
  5260. (setq align t)
  5261. (and (looking-at " *|") (goto-char (match-end 0))))
  5262. (if (looking-at
  5263. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  5264. (replace-match ""))
  5265. (if negative (insert " -"))
  5266. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  5267. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  5268. (insert " " (format fh h m))))
  5269. (if align (org-table-align))
  5270. (message "Time difference inserted")))))
  5271. (defun org-make-tdiff-string (y d h m)
  5272. (let ((fmt "")
  5273. (l nil))
  5274. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  5275. l (push y l)))
  5276. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  5277. l (push d l)))
  5278. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  5279. l (push h l)))
  5280. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  5281. l (push m l)))
  5282. (apply 'format fmt (nreverse l))))
  5283. (defun org-time-string-to-time (s)
  5284. (apply 'encode-time (org-parse-time-string s)))
  5285. (defun org-parse-time-string (s &optional nodefault)
  5286. "Parse the standard Org-mode time string.
  5287. This should be a lot faster than the normal `parse-time-string'.
  5288. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  5289. hour and minute fields will be nil if not given."
  5290. (if (string-match org-ts-regexp1 s)
  5291. (list 0
  5292. (if (or (match-beginning 8) (not nodefault))
  5293. (string-to-number (or (match-string 8 s) "0")))
  5294. (if (or (match-beginning 7) (not nodefault))
  5295. (string-to-number (or (match-string 7 s) "0")))
  5296. (string-to-number (match-string 4 s))
  5297. (string-to-number (match-string 3 s))
  5298. (string-to-number (match-string 2 s))
  5299. nil nil nil)
  5300. (make-list 9 0)))
  5301. (defun org-timestamp-up (&optional arg)
  5302. "Increase the date item at the cursor by one.
  5303. If the cursor is on the year, change the year. If it is on the month or
  5304. the day, change that.
  5305. With prefix ARG, change by that many units."
  5306. (interactive "p")
  5307. (org-timestamp-change (prefix-numeric-value arg)))
  5308. (defun org-timestamp-down (&optional arg)
  5309. "Decrease the date item at the cursor by one.
  5310. If the cursor is on the year, change the year. If it is on the month or
  5311. the day, change that.
  5312. With prefix ARG, change by that many units."
  5313. (interactive "p")
  5314. (org-timestamp-change (- (prefix-numeric-value arg))))
  5315. (defun org-timestamp-up-day (&optional arg)
  5316. "Increase the date in the time stamp by one day.
  5317. With prefix ARG, change that many days."
  5318. (interactive "p")
  5319. (if (and (not (org-at-timestamp-p t))
  5320. (org-on-heading-p))
  5321. (org-todo 'up)
  5322. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  5323. (defun org-timestamp-down-day (&optional arg)
  5324. "Decrease the date in the time stamp by one day.
  5325. With prefix ARG, change that many days."
  5326. (interactive "p")
  5327. (if (and (not (org-at-timestamp-p t))
  5328. (org-on-heading-p))
  5329. (org-todo 'down)
  5330. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  5331. (defsubst org-pos-in-match-range (pos n)
  5332. (and (match-beginning n)
  5333. (<= (match-beginning n) pos)
  5334. (>= (match-end n) pos)))
  5335. (defun org-at-timestamp-p (&optional also-inactive)
  5336. "Determine if the cursor is in or at a timestamp."
  5337. (interactive)
  5338. (let* ((tsr (if also-inactive org-ts-regexp3 org-ts-regexp2))
  5339. (pos (point))
  5340. (ans (or (looking-at tsr)
  5341. (save-excursion
  5342. (skip-chars-backward "^[<\n\r\t")
  5343. (if (> (point) 1) (backward-char 1))
  5344. (and (looking-at tsr)
  5345. (> (- (match-end 0) pos) -1))))))
  5346. (and (boundp 'org-ts-what)
  5347. (setq org-ts-what
  5348. (cond
  5349. ((org-pos-in-match-range pos 2) 'year)
  5350. ((org-pos-in-match-range pos 3) 'month)
  5351. ((org-pos-in-match-range pos 7) 'hour)
  5352. ((org-pos-in-match-range pos 8) 'minute)
  5353. ((or (org-pos-in-match-range pos 4)
  5354. (org-pos-in-match-range pos 5)) 'day)
  5355. (t 'day))))
  5356. ans))
  5357. (defun org-timestamp-change (n &optional what)
  5358. "Change the date in the time stamp at point.
  5359. The date will be changed by N times WHAT. WHAT can be `day', `month',
  5360. `year', `minute', `second'. If WHAT is not given, the cursor position
  5361. in the timestamp determines what will be changed."
  5362. (let ((fmt (car org-time-stamp-formats))
  5363. org-ts-what
  5364. (pos (point))
  5365. ts time time0)
  5366. (if (not (org-at-timestamp-p t))
  5367. (error "Not at a timestamp"))
  5368. (setq org-ts-what (or what org-ts-what))
  5369. (setq fmt (if (<= (abs (- (cdr org-ts-lengths)
  5370. (- (match-end 0) (match-beginning 0))))
  5371. 1)
  5372. (cdr org-time-stamp-formats)
  5373. (car org-time-stamp-formats)))
  5374. (if (= (char-after (match-beginning 0)) ?\[)
  5375. (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  5376. (setq ts (match-string 0))
  5377. (replace-match "")
  5378. (setq time0 (org-parse-time-string ts))
  5379. (setq time
  5380. (apply 'encode-time
  5381. (append
  5382. (list (or (car time0) 0))
  5383. (list (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0)))
  5384. (list (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0)))
  5385. (list (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0)))
  5386. (list (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0)))
  5387. (list (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0)))
  5388. (nthcdr 6 time0))))
  5389. (if (eq what 'calendar)
  5390. (let ((cal-date
  5391. (save-excursion
  5392. (save-match-data
  5393. (set-buffer "*Calendar*")
  5394. (calendar-cursor-to-date)))))
  5395. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  5396. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  5397. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  5398. (setcar time0 (or (car time0) 0))
  5399. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  5400. (setcar (nthcdr 2 time0) (or (nth 1 time0) 0))
  5401. (setq time (apply 'encode-time time0))))
  5402. (insert (setq org-last-changed-timestamp (format-time-string fmt time)))
  5403. (org-clock-update-time-maybe)
  5404. (goto-char pos)
  5405. ;; Try to recenter the calendar window, if any
  5406. (if (and org-calendar-follow-timestamp-change
  5407. (get-buffer-window "*Calendar*" t)
  5408. (memq org-ts-what '(day month year)))
  5409. (org-recenter-calendar (time-to-days time)))))
  5410. (defun org-recenter-calendar (date)
  5411. "If the calendar is visible, recenter it to DATE."
  5412. (let* ((win (selected-window))
  5413. (cwin (get-buffer-window "*Calendar*" t))
  5414. (calendar-move-hook nil))
  5415. (when cwin
  5416. (select-window cwin)
  5417. (calendar-goto-date (if (listp date) date
  5418. (calendar-gregorian-from-absolute date)))
  5419. (select-window win))))
  5420. (defun org-goto-calendar (&optional arg)
  5421. "Go to the Emacs calendar at the current date.
  5422. If there is a time stamp in the current line, go to that date.
  5423. A prefix ARG can be used to force the current date."
  5424. (interactive "P")
  5425. (let ((tsr org-ts-regexp) diff
  5426. (calendar-move-hook nil)
  5427. (view-calendar-holidays-initially nil)
  5428. (view-diary-entries-initially nil))
  5429. (if (or (org-at-timestamp-p)
  5430. (save-excursion
  5431. (beginning-of-line 1)
  5432. (looking-at (concat ".*" tsr))))
  5433. (let ((d1 (time-to-days (current-time)))
  5434. (d2 (time-to-days
  5435. (org-time-string-to-time (match-string 1)))))
  5436. (setq diff (- d2 d1))))
  5437. (calendar)
  5438. (calendar-goto-today)
  5439. (if (and diff (not arg)) (calendar-forward-day diff))))
  5440. (defun org-date-from-calendar ()
  5441. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  5442. If there is already a time stamp at the cursor position, update it."
  5443. (interactive)
  5444. (org-timestamp-change 0 'calendar))
  5445. ;;; The clock for measuring work time.
  5446. (defvar org-clock-marker (make-marker)
  5447. "Marker recording the last clock-in.")
  5448. (defun org-clock-in ()
  5449. "Start the clock on the current item.
  5450. If necessary, clock-out of the currently active clock."
  5451. (interactive)
  5452. (org-clock-out t)
  5453. (let (ts)
  5454. (save-excursion
  5455. (org-back-to-heading t)
  5456. (beginning-of-line 2)
  5457. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  5458. (not (equal (match-string 1) org-clock-string)))
  5459. (beginning-of-line 1))
  5460. (insert "\n") (backward-char 1)
  5461. (indent-relative)
  5462. (insert org-clock-string " "
  5463. (setq ts (concat "[" (format-time-string
  5464. (substring
  5465. (cdr org-time-stamp-formats) 1 -1)
  5466. (current-time))
  5467. "]")))
  5468. (move-marker org-clock-marker (point))
  5469. (message "Clock started at %s" ts))))
  5470. (defun org-clock-out (&optional fail-quietly)
  5471. "Stop the currently running clock.
  5472. If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
  5473. (interactive)
  5474. (catch 'exit
  5475. (if (not (marker-buffer org-clock-marker))
  5476. (if fail-quietly (throw 'exit t) (error "No active clock")))
  5477. (let (ts te s h m)
  5478. (save-excursion
  5479. (set-buffer (marker-buffer org-clock-marker))
  5480. (goto-char org-clock-marker)
  5481. (beginning-of-line 1)
  5482. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  5483. (equal (match-string 1) org-clock-string))
  5484. (setq ts (match-string 2))
  5485. (if fail-quietly (throw 'exit nil) (error "Clock start time is gone")))
  5486. (goto-char org-clock-marker)
  5487. (setq te (concat "[" (format-time-string
  5488. (substring
  5489. (cdr org-time-stamp-formats) 1 -1)
  5490. (current-time))
  5491. "]"))
  5492. (setq s (- (time-to-seconds (apply 'encode-time (org-parse-time-string te)))
  5493. (time-to-seconds (apply 'encode-time (org-parse-time-string ts))))
  5494. h (floor (/ s 3600))
  5495. s (- s (* 3600 h))
  5496. m (floor (/ s 60))
  5497. s (- s (* 60 s)))
  5498. (insert "--" te " => " (format "%2d:%02d" h m))
  5499. (move-marker org-clock-marker nil)
  5500. (message "Clock stopped at %s after HH:MM = %d:%02d" te h m)))))
  5501. (defun org-clock-cancel ()
  5502. "Cancel the running clock be removing the start timestamp."
  5503. (interactive)
  5504. (if (not (marker-buffer org-clock-marker))
  5505. (error "No active clock"))
  5506. (save-excursion
  5507. (set-buffer (marker-buffer org-clock-marker))
  5508. (goto-char org-clock-marker)
  5509. (delete-region (1- (point-at-bol)) (point-at-eol)))
  5510. (message "Clock canceled"))
  5511. (defvar org-clock-file-total-minutes nil
  5512. "Holds the file total time in minutes, after a call to `org-clock-sum'.")
  5513. (make-variable-buffer-local 'org-clock-file-total-minutes)
  5514. (defun org-clock-sum (&optional tstart tend)
  5515. "Sum the times for each subtree.
  5516. Puts the resulting times in minutes as a text property on each headline."
  5517. (interactive)
  5518. (let* ((bmp (buffer-modified-p))
  5519. (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
  5520. org-clock-string
  5521. "[ \t]*\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)"))
  5522. (lmax 30)
  5523. (ltimes (make-vector lmax 0))
  5524. (t1 0)
  5525. (level 0)
  5526. ts te dt
  5527. time)
  5528. (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t))
  5529. (save-excursion
  5530. (goto-char (point-max))
  5531. (while (re-search-backward re nil t)
  5532. (if (match-end 2)
  5533. ;; A time
  5534. (setq ts (match-string 2)
  5535. te (match-string 3)
  5536. ts (time-to-seconds
  5537. (apply 'encode-time (org-parse-time-string ts)))
  5538. te (time-to-seconds
  5539. (apply 'encode-time (org-parse-time-string te)))
  5540. ts (if tstart (max ts tstart) ts)
  5541. te (if tend (min te tend) te)
  5542. dt (- te ts)
  5543. t1 (if (> dt 0) (+ t1 (floor (/ dt 60))) t1))
  5544. ;; A headline
  5545. (setq level (- (match-end 1) (match-beginning 1)))
  5546. (when (or (> t1 0) (> (aref ltimes level) 0))
  5547. (loop for l from 0 to level do
  5548. (aset ltimes l (+ (aref ltimes l) t1)))
  5549. (setq t1 0 time (aref ltimes level))
  5550. (loop for l from level to (1- lmax) do
  5551. (aset ltimes l 0))
  5552. (goto-char (match-beginning 0))
  5553. (put-text-property (point) (point-at-eol) :org-clock-minutes time))))
  5554. (setq org-clock-file-total-minutes (aref ltimes 0)))
  5555. (set-buffer-modified-p bmp)))
  5556. (defun org-clock-display (&optional total-only)
  5557. "Show subtree times in the entire buffer.
  5558. If TOTAL-ONLY is non-nil, only show the total time for the entire file
  5559. in the echo area."
  5560. (interactive)
  5561. (org-remove-clock-overlays)
  5562. (let (time h m p)
  5563. (org-clock-sum)
  5564. (unless total-only
  5565. (save-excursion
  5566. (goto-char (point-min))
  5567. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  5568. (goto-char p)
  5569. (when (setq time (get-text-property p :org-clock-minutes))
  5570. (org-put-clock-overlay time (funcall outline-level))))
  5571. (setq h (/ org-clock-file-total-minutes 60)
  5572. m (- org-clock-file-total-minutes (* 60 h)))
  5573. ;; Arrange to remove the overlays upon next change.
  5574. (org-add-hook 'before-change-functions 'org-remove-clock-overlays
  5575. nil 'local)))
  5576. (message "Total file time: %d:%02d (%d hours and %d minutes)" h m h m)))
  5577. (defvar org-clock-overlays nil)
  5578. (make-variable-buffer-local 'org-clock-overlays)
  5579. (defun org-put-clock-overlay (time &optional level)
  5580. "Put an overlays on the current line, displaying TIME.
  5581. If LEVEL is given, prefix time with a corresponding number of stars.
  5582. This creates a new overlay and stores it in `org-clock-overlays', so that it
  5583. will be easy to remove."
  5584. (let* ((c 60) (h (floor (/ time 60))) (m (- time (* 60 h)))
  5585. (l (if level (org-get-legal-level level 0) 0))
  5586. (off 0)
  5587. ov tx)
  5588. (move-to-column c)
  5589. (unless (eolp) (skip-chars-backward "^ \t"))
  5590. (skip-chars-backward " \t")
  5591. (setq ov (org-make-overlay (1- (point)) (point-at-eol))
  5592. tx (concat (buffer-substring (1- (point)) (point))
  5593. (make-string (+ off (max 0 (- c (current-column)))) ?.)
  5594. (org-add-props (format "%s %2d:%02d%s"
  5595. (make-string l ?*) h m
  5596. (make-string (- 10 l) ?\ ))
  5597. '(face secondary-selection))
  5598. ""))
  5599. (org-overlay-put ov 'display tx)
  5600. (push ov org-clock-overlays)))
  5601. (defun org-remove-clock-overlays (&optional beg end noremove)
  5602. "Remove the occur highlights from the buffer.
  5603. BEG and END are ignored. If NOREMOVE is nil, remove this function
  5604. from the `before-change-functions' in the current buffer."
  5605. (interactive)
  5606. (unless org-inhibit-highlight-removal
  5607. (mapc 'org-delete-overlay org-clock-overlays)
  5608. (setq org-clock-overlays nil)
  5609. (unless noremove
  5610. (remove-hook 'before-change-functions
  5611. 'org-remove-clock-overlays 'local))))
  5612. (defun org-clock-out-if-current ()
  5613. "Clock out if the current entry contains the running clock.
  5614. This is used to stop the clock after a TODO entry is marked DONE."
  5615. (when (and (equal state org-done-string)
  5616. (equal (marker-buffer org-clock-marker) (current-buffer))
  5617. (< (point) org-clock-marker)
  5618. (> (save-excursion (outline-next-heading) (point))
  5619. org-clock-marker))
  5620. (org-clock-out)))
  5621. (add-hook 'org-after-todo-state-change-hook
  5622. 'org-clock-out-if-current)
  5623. (defun org-check-running-clock ()
  5624. "Check if the current buffer contains the running clock.
  5625. If yes, offer to stop it and to save the buffer with the changes."
  5626. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  5627. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  5628. (buffer-name))))
  5629. (org-clock-out)
  5630. (when (y-or-n-p "Save changed buffer?")
  5631. (save-buffer))))
  5632. (defun org-clock-report ()
  5633. "Create a table containing a report about clocked time.
  5634. If the buffer contains lines
  5635. #+BEGIN: clocktable :maxlevel 3 :emphasize nil
  5636. #+END: clocktable
  5637. then the table will be inserted between these lines, replacing whatever
  5638. is was there before. If these lines are not in the buffer, the table
  5639. is inserted at point, surrounded by the special lines.
  5640. The BEGIN line can contain parameters. Allowed are:
  5641. :maxlevel The maximum level to be included in the table. Default is 3.
  5642. :emphasize t/nil, if levell 1 and level 2 should be bold/italic in the table."
  5643. (interactive)
  5644. (org-remove-clock-overlays)
  5645. (unless (org-find-dblock "clocktable")
  5646. (org-create-dblock (list :name "clocktable"
  5647. :maxlevel 2 :emphasize nil)))
  5648. (org-update-dblock))
  5649. (defun org-clock-update-time-maybe ()
  5650. "If this is a CLOCK line, update it and return t.
  5651. Otherwise, return nil."
  5652. (interactive)
  5653. (save-excursion
  5654. (beginning-of-line 1)
  5655. (skip-chars-forward " \t")
  5656. (when (looking-at org-clock-string)
  5657. (let ((re (concat "[ \t]*" org-clock-string
  5658. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  5659. "\\([ \t]*=>.*\\)?"))
  5660. ts te h m s)
  5661. (if (not (looking-at re))
  5662. nil
  5663. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  5664. (end-of-line 1)
  5665. (setq ts (match-string 1)
  5666. te (match-string 2))
  5667. (setq s (- (time-to-seconds
  5668. (apply 'encode-time (org-parse-time-string te)))
  5669. (time-to-seconds
  5670. (apply 'encode-time (org-parse-time-string ts))))
  5671. h (floor (/ s 3600))
  5672. s (- s (* 3600 h))
  5673. m (floor (/ s 60))
  5674. s (- s (* 60 s)))
  5675. (insert " => " (format "%2d:%02d" h m))
  5676. t)))))
  5677. (defun org-clock-special-range (key &optional time as-strings)
  5678. "Return two times bordering a special time range.
  5679. Key is a symbol specifying the range and can be one of `today', `yesterday',
  5680. `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
  5681. A week starts Monday 0:00 and ends Sunday 24:00.
  5682. The range is determined relative to TIME. TIME defaults to the current time.
  5683. The return value is a cons cell with two internal times like the ones
  5684. returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
  5685. the returned times will be formatted strings."
  5686. (let* ((tm (decode-time (or time (current-time))))
  5687. (s 0) (m (nth 1 tm)) (h (nth 2 tm))
  5688. (d (nth 3 tm)) (month (nth 4 tm)) (y (nth 5 tm))
  5689. (dow (nth 6 tm))
  5690. s1 m1 h1 d1 month1 y1 diff ts te fm)
  5691. (cond
  5692. ((eq key 'today)
  5693. (setq h 0 m 0 h1 24 m1 0))
  5694. ((eq key 'yesterday)
  5695. (setq d (1- d) h 0 m 0 h1 24 m1 0))
  5696. ((eq key 'thisweek)
  5697. (setq diff (if (= dow 0) 6 (1- dow))
  5698. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  5699. ((eq key 'lastweek)
  5700. (setq diff (+ 7 (if (= dow 0) 6 (1- dow)))
  5701. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  5702. ((eq key 'thismonth)
  5703. (setq d 1 h 0 m 0 d1 1 month1 (1+ month) h1 0 m1 0))
  5704. ((eq key 'lastmonth)
  5705. (setq d 1 h 0 m 0 d1 1 month (1- month) month1 (1+ month) h1 0 m1 0))
  5706. ((eq key 'thisyear)
  5707. (setq m 0 h 0 d 1 month 1 y1 (1+ y)))
  5708. ((eq key 'lastyear)
  5709. (setq m 0 h 0 d 1 month 1 y (1- y) y1 (1+ y)))
  5710. (t (error "No such time block %s" key)))
  5711. (setq ts (encode-time s m h d month y)
  5712. te (encode-time (or s1 s) (or m1 m) (or h1 h)
  5713. (or d1 d) (or month1 month) (or y1 y)))
  5714. (setq fm (cdr org-time-stamp-formats))
  5715. (if as-strings
  5716. (cons (format-time-string fm ts) (format-time-string fm te))
  5717. (cons ts te))))
  5718. (defun org-dblock-write:clocktable (params)
  5719. "Write the standard clocktable."
  5720. (let ((hlchars '((1 . "*") (2 . ?/)))
  5721. (emph nil)
  5722. (ins (make-marker))
  5723. ipos time h m p level hlc hdl maxlevel
  5724. ts te cc block)
  5725. (setq maxlevel (or (plist-get params :maxlevel) 3)
  5726. emph (plist-get params :emphasize)
  5727. ts (plist-get params :tstart)
  5728. te (plist-get params :tend)
  5729. block (plist-get params :block))
  5730. (when block
  5731. (setq cc (org-clock-special-range block nil t)
  5732. ts (car cc) te (cdr cc)))
  5733. (if ts (setq ts (time-to-seconds
  5734. (apply 'encode-time (org-parse-time-string ts)))))
  5735. (if te (setq te (time-to-seconds
  5736. (apply 'encode-time (org-parse-time-string te)))))
  5737. (move-marker ins (point))
  5738. (setq ipos (point))
  5739. (insert-before-markers "Clock summary at ["
  5740. (substring
  5741. (format-time-string (cdr org-time-stamp-formats))
  5742. 1 -1)
  5743. "]."
  5744. (if block
  5745. (format " Considered range is /%s/." block)
  5746. "")
  5747. "\n\n|L|Headline|Time|\n")
  5748. (org-clock-sum ts te)
  5749. (setq h (/ org-clock-file-total-minutes 60)
  5750. m (- org-clock-file-total-minutes (* 60 h)))
  5751. (insert-before-markers "|-\n|0|" "*Total file time*| "
  5752. (format "*%d:%02d*" h m)
  5753. "|\n")
  5754. (goto-char (point-min))
  5755. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  5756. (goto-char p)
  5757. (when (setq time (get-text-property p :org-clock-minutes))
  5758. (save-excursion
  5759. (beginning-of-line 1)
  5760. (when (and (looking-at "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[0-9a-zA-Z_@:]+:\\)?[ \t]*$")
  5761. (setq level (- (match-end 1) (match-beginning 1)))
  5762. (<= level maxlevel))
  5763. (setq hlc (if emph (or (cdr (assoc level hlchars)) "") "")
  5764. hdl (match-string 2)
  5765. h (/ time 60)
  5766. m (- time (* 60 h)))
  5767. (goto-char ins)
  5768. (if (= level 1) (insert-before-markers "|-\n"))
  5769. (insert-before-markers
  5770. "| " (int-to-string level) "|" hlc hdl hlc " |"
  5771. (make-string (1- level) ?|)
  5772. hlc
  5773. (format "%d:%02d" h m)
  5774. hlc
  5775. " |\n")))))
  5776. (goto-char ins)
  5777. (backward-delete-char 1)
  5778. (goto-char ipos)
  5779. (skip-chars-forward "^|")
  5780. (org-table-align)))
  5781. (defun org-collect-clock-time-entries ()
  5782. "Return an internal list with clocking information.
  5783. This list has one entry for each CLOCK interval.
  5784. FIXME: describe the elements."
  5785. (interactive)
  5786. (let ((re (concat "^[ \t]*" org-clock-string
  5787. " *\\[\\(.*?\\)\\]--\\[\\(.*?\\)\\]"))
  5788. rtn beg end next cont level title total closedp leafp
  5789. clockpos titlepos h m donep)
  5790. (save-excursion
  5791. (org-clock-sum)
  5792. (goto-char (point-min))
  5793. (while (re-search-forward re nil t)
  5794. (setq clockpos (match-beginning 0)
  5795. beg (match-string 1) end (match-string 2)
  5796. cont (match-end 0))
  5797. (setq beg (apply 'encode-time (org-parse-time-string beg))
  5798. end (apply 'encode-time (org-parse-time-string end)))
  5799. (org-back-to-heading t)
  5800. (setq donep (org-entry-is-done-p))
  5801. (setq titlepos (point)
  5802. total (or (get-text-property (1+ (point)) :org-clock-minutes) 0)
  5803. h (/ total 60) m (- total (* 60 h))
  5804. total (cons h m))
  5805. (looking-at "\\(\\*+\\) +\\(.*\\)")
  5806. (setq level (- (match-end 1) (match-beginning 1))
  5807. title (org-match-string-no-properties 2))
  5808. (save-excursion (outline-next-heading) (setq next (point)))
  5809. (setq closedp (re-search-forward org-closed-time-regexp next t))
  5810. (goto-char next)
  5811. (setq leafp (and (looking-at "^\\*+ ")
  5812. (<= (- (match-end 0) (point)) level)))
  5813. (push (list beg end clockpos closedp donep
  5814. total title titlepos level leafp)
  5815. rtn)
  5816. (goto-char cont)))
  5817. (nreverse rtn)))
  5818. ;;; Agenda, and Diary Integration
  5819. ;;; Define the mode
  5820. (defvar org-agenda-mode-map (make-sparse-keymap)
  5821. "Keymap for `org-agenda-mode'.")
  5822. (defvar org-agenda-menu) ; defined later in this file.
  5823. (defvar org-agenda-follow-mode nil)
  5824. (defvar org-agenda-show-log nil)
  5825. (defvar org-agenda-redo-command nil)
  5826. (defvar org-agenda-mode-hook nil)
  5827. (defvar org-agenda-type nil)
  5828. (defvar org-agenda-force-single-file nil)
  5829. (defun org-agenda-mode ()
  5830. "Mode for time-sorted view on action items in Org-mode files.
  5831. The following commands are available:
  5832. \\{org-agenda-mode-map}"
  5833. (interactive)
  5834. (kill-all-local-variables)
  5835. (setq major-mode 'org-agenda-mode)
  5836. (setq mode-name "Org-Agenda")
  5837. (use-local-map org-agenda-mode-map)
  5838. (easy-menu-add org-agenda-menu)
  5839. (if org-startup-truncated (setq truncate-lines t))
  5840. (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
  5841. (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
  5842. (unless org-agenda-keep-modes
  5843. (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
  5844. org-agenda-show-log nil))
  5845. (easy-menu-change
  5846. '("Agenda") "Agenda Files"
  5847. (append
  5848. (list
  5849. (vector
  5850. (if (get 'org-agenda-files 'org-restrict)
  5851. "Restricted to single file"
  5852. "Edit File List")
  5853. '(org-edit-agenda-file-list)
  5854. (not (get 'org-agenda-files 'org-restrict)))
  5855. "--")
  5856. (mapcar 'org-file-menu-entry (org-agenda-files))))
  5857. (org-agenda-set-mode-name)
  5858. (apply
  5859. (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
  5860. (list 'org-agenda-mode-hook)))
  5861. (define-key org-agenda-mode-map "\C-i" 'org-agenda-goto)
  5862. (define-key org-agenda-mode-map [(tab)] 'org-agenda-goto)
  5863. (define-key org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
  5864. (define-key org-agenda-mode-map " " 'org-agenda-show)
  5865. (define-key org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
  5866. (define-key org-agenda-mode-map "o" 'delete-other-windows)
  5867. (define-key org-agenda-mode-map "L" 'org-agenda-recenter)
  5868. (define-key org-agenda-mode-map "t" 'org-agenda-todo)
  5869. (define-key org-agenda-mode-map "a" 'org-agenda-toggle-archive-tag)
  5870. (define-key org-agenda-mode-map ":" 'org-agenda-set-tags)
  5871. (define-key org-agenda-mode-map "." 'org-agenda-goto-today)
  5872. (define-key org-agenda-mode-map "d" 'org-agenda-day-view)
  5873. (define-key org-agenda-mode-map "w" 'org-agenda-week-view)
  5874. (define-key org-agenda-mode-map (org-key 'S-right) 'org-agenda-date-later)
  5875. (define-key org-agenda-mode-map (org-key 'S-left) 'org-agenda-date-earlier)
  5876. (define-key org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-date-later)
  5877. (define-key org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-date-earlier)
  5878. (define-key org-agenda-mode-map ">" 'org-agenda-date-prompt)
  5879. (define-key org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
  5880. (define-key org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
  5881. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  5882. (while l (define-key org-agenda-mode-map
  5883. (int-to-string (pop l)) 'digit-argument)))
  5884. (define-key org-agenda-mode-map "f" 'org-agenda-follow-mode)
  5885. (define-key org-agenda-mode-map "l" 'org-agenda-log-mode)
  5886. (define-key org-agenda-mode-map "D" 'org-agenda-toggle-diary)
  5887. (define-key org-agenda-mode-map "g" 'org-agenda-toggle-time-grid)
  5888. (define-key org-agenda-mode-map "r" 'org-agenda-redo)
  5889. (define-key org-agenda-mode-map "q" 'org-agenda-quit)
  5890. (define-key org-agenda-mode-map "x" 'org-agenda-exit)
  5891. (define-key org-agenda-mode-map "s" 'org-save-all-org-buffers)
  5892. (define-key org-agenda-mode-map "P" 'org-agenda-show-priority)
  5893. (define-key org-agenda-mode-map "T" 'org-agenda-show-tags)
  5894. (define-key org-agenda-mode-map "n" 'next-line)
  5895. (define-key org-agenda-mode-map "p" 'previous-line)
  5896. (define-key org-agenda-mode-map "\C-n" 'org-agenda-next-date-line)
  5897. (define-key org-agenda-mode-map "\C-p" 'org-agenda-previous-date-line)
  5898. (define-key org-agenda-mode-map "," 'org-agenda-priority)
  5899. (define-key org-agenda-mode-map "\C-c," 'org-agenda-priority)
  5900. (define-key org-agenda-mode-map "i" 'org-agenda-diary-entry)
  5901. (define-key org-agenda-mode-map "c" 'org-agenda-goto-calendar)
  5902. (eval-after-load "calendar"
  5903. '(define-key calendar-mode-map org-calendar-to-agenda-key
  5904. 'org-calendar-goto-agenda))
  5905. (define-key org-agenda-mode-map "C" 'org-agenda-convert-date)
  5906. (define-key org-agenda-mode-map "m" 'org-agenda-phases-of-moon)
  5907. (define-key org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
  5908. (define-key org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
  5909. (define-key org-agenda-mode-map "h" 'org-agenda-holidays)
  5910. (define-key org-agenda-mode-map "H" 'org-agenda-holidays)
  5911. (define-key org-agenda-mode-map "+" 'org-agenda-priority-up)
  5912. (define-key org-agenda-mode-map "I" 'org-agenda-clock-in)
  5913. (define-key org-agenda-mode-map "O" 'org-clock-out)
  5914. (define-key org-agenda-mode-map "X" 'org-clock-cancel)
  5915. (define-key org-agenda-mode-map "-" 'org-agenda-priority-down)
  5916. (define-key org-agenda-mode-map (org-key 'S-up) 'org-agenda-priority-up)
  5917. (define-key org-agenda-mode-map (org-key 'S-down) 'org-agenda-priority-down)
  5918. (define-key org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
  5919. (define-key org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
  5920. (define-key org-agenda-mode-map [(right)] 'org-agenda-later)
  5921. (define-key org-agenda-mode-map [(left)] 'org-agenda-earlier)
  5922. (define-key org-agenda-mode-map "\C-c\C-x\C-c" 'org-export-icalendar-combine-agenda-files)
  5923. (defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
  5924. "Local keymap for agenda entries from Org-mode.")
  5925. (define-key org-agenda-keymap
  5926. (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
  5927. (define-key org-agenda-keymap
  5928. (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
  5929. (when org-agenda-mouse-1-follows-link
  5930. (define-key org-agenda-keymap [follow-link] 'mouse-face))
  5931. (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
  5932. '("Agenda"
  5933. ("Agenda Files")
  5934. "--"
  5935. ["Show" org-agenda-show t]
  5936. ["Go To (other window)" org-agenda-goto t]
  5937. ["Go To (one window)" org-agenda-switch-to t]
  5938. ["Follow Mode" org-agenda-follow-mode
  5939. :style toggle :selected org-agenda-follow-mode :active t]
  5940. "--"
  5941. ["Cycle TODO" org-agenda-todo t]
  5942. ("Tags"
  5943. ["Show all Tags" org-agenda-show-tags t]
  5944. ["Set Tags" org-agenda-set-tags t])
  5945. ("Schedule"
  5946. ["Schedule" org-agenda-schedule t]
  5947. ["Set Deadline" org-agenda-deadline t]
  5948. "--"
  5949. ["Reschedule +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
  5950. ["Reschedule -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
  5951. ["Reschedule to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
  5952. ("Priority"
  5953. ["Set Priority" org-agenda-priority t]
  5954. ["Increase Priority" org-agenda-priority-up t]
  5955. ["Decrease Priority" org-agenda-priority-down t]
  5956. ["Show Priority" org-agenda-show-priority t])
  5957. "--"
  5958. ;; ["New agenda command" org-agenda t]
  5959. ["Rebuild buffer" org-agenda-redo t]
  5960. ["Save all Org-mode Buffers" org-save-all-org-buffers t]
  5961. "--"
  5962. ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
  5963. ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
  5964. ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
  5965. "--"
  5966. ["Day View" org-agenda-day-view :active (org-agenda-check-type nil 'agenda)
  5967. :style radio :selected (equal org-agenda-ndays 1)]
  5968. ["Week View" org-agenda-week-view :active (org-agenda-check-type nil 'agenda)
  5969. :style radio :selected (equal org-agenda-ndays 7)]
  5970. "--"
  5971. ["Show Logbook entries" org-agenda-log-mode
  5972. :style toggle :selected org-agenda-show-log :active (org-agenda-check-type nil 'agenda 'timeline)]
  5973. ["Include Diary" org-agenda-toggle-diary
  5974. :style toggle :selected org-agenda-include-diary :active (org-agenda-check-type nil 'agenda)]
  5975. ["Use Time Grid" org-agenda-toggle-time-grid
  5976. :style toggle :selected org-agenda-use-time-grid :active (org-agenda-check-type nil 'agenda)]
  5977. "--"
  5978. ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
  5979. ("Calendar Commands"
  5980. ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
  5981. ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
  5982. ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
  5983. ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
  5984. ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)])
  5985. ["Create iCalendar file" org-export-icalendar-combine-agenda-files t]
  5986. "--"
  5987. ["Quit" org-agenda-quit t]
  5988. ["Exit and Release Buffers" org-agenda-exit t]
  5989. ))
  5990. (defvar org-agenda-restrict nil)
  5991. (defvar org-agenda-restrict-begin (make-marker))
  5992. (defvar org-agenda-restrict-end (make-marker))
  5993. (defvar org-agenda-last-dispatch-buffer nil)
  5994. ;;;###autoload
  5995. (defun org-agenda (arg)
  5996. "Dispatch agenda commands to collect entries to the agenda buffer.
  5997. Prompts for a character to select a command. Any prefix arg will be passed
  5998. on to the selected command. The default selections are:
  5999. g
  6000. a Call `org-agenda-list' to display the agenda for current day or week.
  6001. t Call `org-todo-list' to display the global todo list.
  6002. T Call `org-todo-list' to display the global todo list, select only
  6003. entries with a specific TODO keyword (the user gets a prompt).
  6004. m Call `org-tags-view' to display headlines with tags matching
  6005. a condition (the user is prompted for the condition).
  6006. M Like `m', but select only TODO entries, no ordinary headlines.
  6007. l Create a timeeline for the current buffer.
  6008. More commands can be added by configuring the variable
  6009. `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
  6010. searches can be pre-defined in this way.
  6011. If the current buffer is in Org-mode and visiting a file, you can also
  6012. first press `1' to indicate that the agenda should be temporarily (until the
  6013. next use of \\[org-agenda]) restricted to the current file."
  6014. (interactive "P")
  6015. (catch 'exit
  6016. (let ((restrict-ok (and buffer-file-name (org-mode-p)))
  6017. (buf (current-buffer))
  6018. (bfn buffer-file-name)
  6019. (custom org-agenda-custom-commands)
  6020. c entry key type match lprops)
  6021. ;; Turn off restriction
  6022. (put 'org-agenda-files 'org-restrict nil)
  6023. (setq org-agenda-restrict nil)
  6024. (move-marker org-agenda-restrict-begin nil)
  6025. (move-marker org-agenda-restrict-end nil)
  6026. ;; Remember where this call originated
  6027. (setq org-agenda-last-dispatch-buffer (current-buffer))
  6028. (save-window-excursion
  6029. (delete-other-windows)
  6030. (switch-to-buffer-other-window " *Agenda Commands*")
  6031. (erase-buffer)
  6032. (insert
  6033. "Press key for an agenda command:
  6034. --------------------------------
  6035. a Agenda for current week or day
  6036. t List of all TODO entries T Entries with special TODO kwd
  6037. m Match a TAGS query M Like m, but only TODO entries
  6038. L Timeline for current buffer C Configure custom agenda commands")
  6039. (while (setq entry (pop custom))
  6040. (setq key (car entry) type (nth 1 entry) match (nth 2 entry))
  6041. (insert (format "\n%-4s%-14s: %s"
  6042. key
  6043. (cond
  6044. ((stringp type) type)
  6045. ((eq type 'tags) "Tags query")
  6046. ((eq type 'todo) "TODO keyword")
  6047. ((eq type 'tags-tree) "Tags tree")
  6048. ((eq type 'todo-tree) "TODO kwd tree")
  6049. ((eq type 'occur-tree) "Occur tree")
  6050. (t "???"))
  6051. (if (stringp match)
  6052. (org-add-props match nil 'face 'org-warning)
  6053. (format "set of %d commands" (+ -2 (length entry)))))))
  6054. (if restrict-ok
  6055. (insert "\n"
  6056. (org-add-props "1 Restrict call to current buffer 0 Restrict call to region or subtree" nil 'face 'org-table)))
  6057. (goto-char (point-min))
  6058. (if (fboundp 'fit-window-to-buffer) (fit-window-to-buffer))
  6059. (message "Press key for agenda command%s"
  6060. (if restrict-ok ", or [1] or [0] to restrict" ""))
  6061. (setq c (read-char-exclusive))
  6062. (message "")
  6063. (when (memq c '(?L ?1 ?0))
  6064. (if restrict-ok
  6065. (put 'org-agenda-files 'org-restrict (list bfn))
  6066. (error "Cannot restrict agenda to current buffer"))
  6067. (with-current-buffer " *Agenda Commands*"
  6068. (goto-char (point-max))
  6069. (delete-region (point-at-bol) (point))
  6070. (goto-char (point-min)))
  6071. (when (eq c ?0)
  6072. (setq org-agenda-restrict t)
  6073. (with-current-buffer buf
  6074. (if (org-region-active-p)
  6075. (progn
  6076. (move-marker org-agenda-restrict-begin (region-beginning))
  6077. (move-marker org-agenda-restrict-end (region-end)))
  6078. (save-excursion
  6079. (org-back-to-heading t)
  6080. (move-marker org-agenda-restrict-begin (point))
  6081. (move-marker org-agenda-restrict-end
  6082. (progn (org-end-of-subtree t)))))))
  6083. (unless (eq c ?L)
  6084. (message "Press key for agenda command%s"
  6085. (if restrict-ok " (restricted to current file)" ""))
  6086. (setq c (read-char-exclusive)))
  6087. (message "")))
  6088. (require 'calendar) ; FIXME: can we avoid this for some commands?
  6089. ;; For example the todo list should not need it (but does...)
  6090. (cond
  6091. ((equal c ?C) (customize-variable 'org-agenda-custom-commands))
  6092. ((equal c ?a) (call-interactively 'org-agenda-list))
  6093. ((equal c ?t) (call-interactively 'org-todo-list))
  6094. ((equal c ?T) (org-call-with-arg 'org-todo-list (or arg '(4))))
  6095. ((equal c ?m) (call-interactively 'org-tags-view))
  6096. ((equal c ?M) (org-call-with-arg 'org-tags-view (or arg '(4))))
  6097. ((equal c ?L)
  6098. (unless restrict-ok
  6099. (error "This is not an Org-mode file"))
  6100. (org-call-with-arg 'org-timeline arg))
  6101. ((setq entry (assoc (char-to-string c) org-agenda-custom-commands))
  6102. (if (symbolp (nth 1 entry))
  6103. (progn
  6104. (setq type (nth 1 entry) match (nth 2 entry) lprops (nth 3 entry)
  6105. lprops (nth 3 entry))
  6106. (cond
  6107. ((eq type 'tags)
  6108. (org-let lprops '(org-tags-view current-prefix-arg match)))
  6109. ((eq type 'tags-todo)
  6110. (org-let lprops '(org-tags-view '(4) match)))
  6111. ((eq type 'todo)
  6112. (org-let lprops '(org-todo-list match)))
  6113. ((eq type 'tags-tree)
  6114. (org-check-for-org-mode)
  6115. (org-let lprops '(org-tags-sparse-tree current-prefix-arg match)))
  6116. ((eq type 'todo-tree)
  6117. (org-check-for-org-mode)
  6118. (org-let lprops
  6119. '(org-occur (concat "^" outline-regexp "[ \t]*"
  6120. (regexp-quote match) "\\>"))))
  6121. ((eq type 'occur-tree)
  6122. (org-check-for-org-mode)
  6123. (org-let lprops '(org-occur match)))
  6124. (t (error "Invalid custom agenda command type %s" type))))
  6125. (org-run-agenda-series (cddr entry))))
  6126. (t (error "Invalid key"))))))
  6127. (defun org-run-agenda-series (series &optional window)
  6128. (org-prepare-agenda)
  6129. (let* ((org-agenda-multi t)
  6130. (redo (list 'org-run-agenda-series (list 'quote series)))
  6131. (org-select-agenda-window t)
  6132. (cmds (car series))
  6133. (gprops (nth 1 series))
  6134. cmd type match lprops)
  6135. (while (setq cmd (pop cmds))
  6136. (setq type (car cmd) match (nth 1 cmd) lprops (nth 2 cmd))
  6137. (cond
  6138. ((eq type 'agenda)
  6139. (call-interactively 'org-agenda-list))
  6140. ((eq type 'alltodo)
  6141. (call-interactively 'org-todo-list))
  6142. ((eq type 'tags)
  6143. (org-let2 gprops lprops
  6144. '(org-tags-view current-prefix-arg match)))
  6145. ((eq type 'tags-todo)
  6146. (org-let2 gprops lprops
  6147. '(org-tags-view '(4) match)))
  6148. ((eq type 'todo)
  6149. (org-let2 gprops lprops
  6150. '(org-todo-list match)))
  6151. (t (error "Invalid type in command series"))))
  6152. (widen)
  6153. (setq org-agenda-redo-command redo)
  6154. (goto-char (point-min))))
  6155. ;;;###autoload
  6156. (defmacro org-batch-agenda (cmd-key &rest parameters)
  6157. "Run an agenda command in batch mode, send result to STDOUT.
  6158. CMD-KEY is a string that is also a key in `org-agenda-custom-commands'.
  6159. Paramters are alternating variable names and values that will be bound
  6160. before running the agenda command."
  6161. (let (pars)
  6162. (while parameters
  6163. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  6164. (flet ((read-char-exclusive () (string-to-char cmd-key)))
  6165. (eval (list 'let (nreverse pars) '(org-agenda nil))))
  6166. (set-buffer "*Org Agenda*")
  6167. (princ (buffer-string))))
  6168. (defun org-check-for-org-mode ()
  6169. "Make sure current buffer is in org-mode. Error if not."
  6170. (or (org-mode-p)
  6171. (error "Cannot execute org-mode agenda command on buffer in %s."
  6172. major-mode)))
  6173. (defun org-fit-agenda-window ()
  6174. "Fit the window to the buffer size."
  6175. (and org-fit-agenda-window
  6176. (fboundp 'fit-window-to-buffer)
  6177. (fit-window-to-buffer nil (/ (* (frame-height) 3) 4)
  6178. (/ (frame-height) 2))))
  6179. (defun org-agenda-files (&optional unrestricted)
  6180. "Get the list of agenda files.
  6181. Optional UNRESTRICTED means return the full list even if a restriction
  6182. is currently in place."
  6183. (cond
  6184. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  6185. ((stringp org-agenda-files) (org-read-agenda-file-list))
  6186. ((listp org-agenda-files) org-agenda-files)
  6187. (t (error "Invalid value of `org-agenda-files'"))))
  6188. (defvar org-window-configuration)
  6189. (defun org-edit-agenda-file-list ()
  6190. "Edit the list of agenda files.
  6191. Depending on setup, this either uses customize to edit the variable
  6192. `org-agenda-files', or it visits the file that is holding the list. In the
  6193. latter case, the buffer is set up in a way that saving it automatically kills
  6194. the buffer and restores the previous window configuration."
  6195. (interactive)
  6196. (if (stringp org-agenda-files)
  6197. (let ((cw (current-window-configuration)))
  6198. (find-file org-agenda-files)
  6199. (org-set-local 'org-window-configuration cw)
  6200. (org-add-hook 'after-save-hook
  6201. (lambda ()
  6202. (set-window-configuration
  6203. (prog1 org-window-configuration
  6204. (kill-buffer (current-buffer))))
  6205. (org-install-agenda-files-menu)
  6206. (message "New agenda file list installed"))
  6207. nil 'local)
  6208. (message (substitute-command-keys
  6209. "Edit list and finish with \\[save-buffer]")))
  6210. (customize-variable 'org-agenda-files)))
  6211. (defun org-store-new-agenda-file-list (list)
  6212. "Set new value for the agenda file list and save it correcly."
  6213. (if (stringp org-agenda-files)
  6214. (let ((f org-agenda-files) b)
  6215. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  6216. (with-temp-file f
  6217. (insert (mapconcat 'identity list "\n") "\n")))
  6218. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  6219. (setq org-agenda-files list)
  6220. (customize-save-variable 'org-agenda-files org-agenda-files))))
  6221. (defun org-read-agenda-file-list ()
  6222. "Read the list of agenda files from a file."
  6223. (when (stringp org-agenda-files)
  6224. (with-temp-buffer
  6225. (insert-file-contents org-agenda-files)
  6226. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  6227. (defvar org-agenda-markers nil
  6228. "List of all currently active markers created by `org-agenda'.")
  6229. (defvar org-agenda-last-marker-time (time-to-seconds (current-time))
  6230. "Creation time of the last agenda marker.")
  6231. (defun org-agenda-new-marker (&optional pos)
  6232. "Return a new agenda marker.
  6233. Org-mode keeps a list of these markers and resets them when they are
  6234. no longer in use."
  6235. (let ((m (copy-marker (or pos (point)))))
  6236. (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
  6237. (push m org-agenda-markers)
  6238. m))
  6239. (defun org-agenda-maybe-reset-markers (&optional force)
  6240. "Reset markers created by `org-agenda'. But only if they are old enough."
  6241. (if (or (and force (not org-agenda-multi))
  6242. (> (- (time-to-seconds (current-time))
  6243. org-agenda-last-marker-time)
  6244. 5))
  6245. (while org-agenda-markers
  6246. (move-marker (pop org-agenda-markers) nil))))
  6247. (defvar org-agenda-new-buffers nil
  6248. "Buffers created to visit agenda files.")
  6249. (defun org-get-agenda-file-buffer (file)
  6250. "Get a buffer visiting FILE. If the buffer needs to be created, add
  6251. it to the list of buffers which might be released later."
  6252. (let ((buf (find-buffer-visiting file)))
  6253. (if buf
  6254. buf ; just return it
  6255. ;; Make a new buffer and remember it
  6256. (setq buf (find-file-noselect file))
  6257. (if buf (push buf org-agenda-new-buffers))
  6258. buf)))
  6259. (defun org-release-buffers (blist)
  6260. "Release all buffers in list, asking the user for confirmation when needed.
  6261. When a buffer is unmodified, it is just killed. When modified, it is saved
  6262. \(if the user agrees) and then killed."
  6263. (let (buf file)
  6264. (while (setq buf (pop blist))
  6265. (setq file (buffer-file-name buf))
  6266. (when (and (buffer-modified-p buf)
  6267. file
  6268. (y-or-n-p (format "Save file %s? " file)))
  6269. (with-current-buffer buf (save-buffer)))
  6270. (kill-buffer buf))))
  6271. (defun org-timeline (&optional include-all keep-modes)
  6272. "Show a time-sorted view of the entries in the current org file.
  6273. Only entries with a time stamp of today or later will be listed. With
  6274. \\[universal-argument] prefix, all unfinished TODO items will also be shown,
  6275. under the current date.
  6276. If the buffer contains an active region, only check the region for
  6277. dates."
  6278. (interactive "P")
  6279. (require 'calendar)
  6280. (org-compile-prefix-format 'timeline)
  6281. (org-set-sorting-strategy 'timeline)
  6282. (let* ((dopast t)
  6283. (dotodo include-all)
  6284. (doclosed org-agenda-show-log)
  6285. (org-agenda-keep-modes keep-modes)
  6286. (entry buffer-file-name)
  6287. (date (calendar-current-date))
  6288. (win (selected-window))
  6289. (pos1 (point))
  6290. (beg (if (org-region-active-p) (region-beginning) (point-min)))
  6291. (end (if (org-region-active-p) (region-end) (point-max)))
  6292. (day-numbers (org-get-all-dates beg end 'no-ranges
  6293. t doclosed ; always include today
  6294. org-timeline-show-empty-dates))
  6295. (today (time-to-days (current-time)))
  6296. (past t)
  6297. args
  6298. s e rtn d emptyp)
  6299. (setq org-agenda-redo-command
  6300. (list 'progn
  6301. (list 'switch-to-buffer-other-window (current-buffer))
  6302. (list 'org-timeline (list 'quote include-all) t)))
  6303. (if (not dopast)
  6304. ;; Remove past dates from the list of dates.
  6305. (setq day-numbers (delq nil (mapcar (lambda(x)
  6306. (if (>= x today) x nil))
  6307. day-numbers))))
  6308. (org-prepare-agenda)
  6309. (if doclosed (push :closed args))
  6310. (push :timestamp args)
  6311. (if dotodo (push :todo args))
  6312. (while (setq d (pop day-numbers))
  6313. (if (and (listp d) (eq (car d) :omitted))
  6314. (progn
  6315. (setq s (point))
  6316. (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
  6317. (put-text-property s (1- (point)) 'face 'org-level-3))
  6318. (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
  6319. (if (and (>= d today)
  6320. dopast
  6321. past)
  6322. (progn
  6323. (setq past nil)
  6324. (insert (make-string 79 ?-) "\n")))
  6325. (setq date (calendar-gregorian-from-absolute d))
  6326. (setq s (point))
  6327. (setq rtn (and (not emptyp)
  6328. (apply 'org-agenda-get-day-entries
  6329. entry date args)))
  6330. (if (or rtn (equal d today) org-timeline-show-empty-dates)
  6331. (progn
  6332. (insert (calendar-day-name date) " "
  6333. (number-to-string (extract-calendar-day date)) " "
  6334. (calendar-month-name (extract-calendar-month date)) " "
  6335. (number-to-string (extract-calendar-year date)) "\n")
  6336. (put-text-property s (1- (point)) 'face
  6337. 'org-level-3)
  6338. (if (equal d today)
  6339. (put-text-property s (1- (point)) 'org-today t))
  6340. (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
  6341. (put-text-property s (1- (point)) 'day d)))))
  6342. (goto-char (point-min))
  6343. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  6344. (point-min)))
  6345. (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
  6346. (org-agenda-align-tags)
  6347. (setq buffer-read-only t)
  6348. (when (not org-select-agenda-window)
  6349. (select-window win)
  6350. (goto-char pos1))))
  6351. (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
  6352. (defvar org-agenda-last-arguments nil
  6353. "The arguments of the previous call to org-agenda")
  6354. ;;;###autoload
  6355. (defun org-agenda-list (&optional include-all start-day ndays keep-modes)
  6356. "Produce a weekly view from all files in variable `org-agenda-files'.
  6357. The view will be for the current week, but from the overview buffer you
  6358. will be able to go to other weeks.
  6359. With one \\[universal-argument] prefix argument INCLUDE-ALL, all unfinished TODO items will
  6360. also be shown, under the current date.
  6361. With two \\[universal-argument] prefix argument INCLUDE-ALL, all TODO entries marked DONE
  6362. on the days are also shown. See the variable `org-log-done' for how
  6363. to turn on logging.
  6364. START-DAY defaults to TODAY, or to the most recent match for the weekday
  6365. given in `org-agenda-start-on-weekday'.
  6366. NDAYS defaults to `org-agenda-ndays'."
  6367. (interactive "P")
  6368. (if org-agenda-overriding-arguments
  6369. (setq include-all (car org-agenda-overriding-arguments)
  6370. start-day (nth 1 org-agenda-overriding-arguments)
  6371. ndays (nth 2 org-agenda-overriding-arguments)
  6372. keep-modes (nth 3 org-agenda-overriding-arguments)))
  6373. (setq org-agenda-last-arguments (list include-all start-day ndays keep-modes))
  6374. (org-compile-prefix-format 'agenda)
  6375. (org-set-sorting-strategy 'agenda)
  6376. (require 'calendar)
  6377. (let* ((org-agenda-start-on-weekday
  6378. (if (or (equal ndays 1)
  6379. (and (null ndays) (equal 1 org-agenda-ndays)))
  6380. nil org-agenda-start-on-weekday))
  6381. (org-agenda-keep-modes keep-modes)
  6382. (thefiles (org-agenda-files))
  6383. (files thefiles)
  6384. (win (selected-window))
  6385. (today (time-to-days (current-time)))
  6386. (sd (or start-day today))
  6387. (start (if (or (null org-agenda-start-on-weekday)
  6388. (< org-agenda-ndays 7))
  6389. sd
  6390. (let* ((nt (calendar-day-of-week
  6391. (calendar-gregorian-from-absolute sd)))
  6392. (n1 org-agenda-start-on-weekday)
  6393. (d (- nt n1)))
  6394. (- sd (+ (if (< d 0) 7 0) d)))))
  6395. (day-numbers (list start))
  6396. (inhibit-redisplay t)
  6397. s e rtn rtnall file date d start-pos end-pos todayp nd)
  6398. (setq org-agenda-redo-command
  6399. (list 'org-agenda-list (list 'quote include-all) start-day ndays t))
  6400. ;; Make the list of days
  6401. (setq ndays (or ndays org-agenda-ndays)
  6402. nd ndays)
  6403. (while (> ndays 1)
  6404. (push (1+ (car day-numbers)) day-numbers)
  6405. (setq ndays (1- ndays)))
  6406. (setq day-numbers (nreverse day-numbers))
  6407. (org-prepare-agenda)
  6408. (org-set-local 'starting-day (car day-numbers))
  6409. (org-set-local 'include-all-loc include-all)
  6410. (when (and (or include-all org-agenda-include-all-todo)
  6411. (member today day-numbers))
  6412. (setq files thefiles
  6413. rtnall nil)
  6414. (while (setq file (pop files))
  6415. (catch 'nextfile
  6416. (org-check-agenda-file file)
  6417. (setq date (calendar-gregorian-from-absolute today)
  6418. rtn (org-agenda-get-day-entries
  6419. file date :todo))
  6420. (setq rtnall (append rtnall rtn))))
  6421. (when rtnall
  6422. (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
  6423. (add-text-properties (point-min) (1- (point))
  6424. (list 'face 'org-level-3))
  6425. (insert (org-finalize-agenda-entries rtnall) "\n")))
  6426. (setq s (point))
  6427. (insert (if (= nd 7) "Week-" "Day-") "agenda:\n")
  6428. (add-text-properties s (1- (point)) (list 'face 'org-level-3))
  6429. (while (setq d (pop day-numbers))
  6430. (setq date (calendar-gregorian-from-absolute d)
  6431. s (point))
  6432. (if (or (setq todayp (= d today))
  6433. (and (not start-pos) (= d sd)))
  6434. (setq start-pos (point))
  6435. (if (and start-pos (not end-pos))
  6436. (setq end-pos (point))))
  6437. (setq files thefiles
  6438. rtnall nil)
  6439. (while (setq file (pop files))
  6440. (catch 'nextfile
  6441. (org-check-agenda-file file)
  6442. (if org-agenda-show-log
  6443. (setq rtn (org-agenda-get-day-entries
  6444. file date
  6445. :deadline :scheduled :timestamp :closed))
  6446. (setq rtn (org-agenda-get-day-entries
  6447. file date
  6448. :deadline :scheduled :timestamp)))
  6449. (setq rtnall (append rtnall rtn))))
  6450. (if org-agenda-include-diary
  6451. (progn
  6452. (require 'diary-lib)
  6453. (setq rtn (org-get-entries-from-diary date))
  6454. (setq rtnall (append rtnall rtn))))
  6455. (if (or rtnall org-agenda-show-all-dates)
  6456. (progn
  6457. (insert (format "%-9s %2d %s %4d\n"
  6458. (calendar-day-name date)
  6459. (extract-calendar-day date)
  6460. (calendar-month-name (extract-calendar-month date))
  6461. (extract-calendar-year date)))
  6462. (put-text-property s (1- (point)) 'face
  6463. 'org-level-3)
  6464. (if todayp (put-text-property s (1- (point)) 'org-today t))
  6465. (if rtnall (insert
  6466. (org-finalize-agenda-entries
  6467. (org-agenda-add-time-grid-maybe
  6468. rtnall nd todayp))
  6469. "\n"))
  6470. (put-text-property s (1- (point)) 'day d))))
  6471. (goto-char (point-min))
  6472. (org-fit-agenda-window)
  6473. (unless (and (pos-visible-in-window-p (point-min))
  6474. (pos-visible-in-window-p (point-max)))
  6475. (goto-char (1- (point-max)))
  6476. (recenter -1)
  6477. (if (not (pos-visible-in-window-p (or start-pos 1)))
  6478. (progn
  6479. (goto-char (or start-pos 1))
  6480. (recenter 1))))
  6481. (goto-char (or start-pos 1))
  6482. (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
  6483. (org-agenda-align-tags)
  6484. (setq buffer-read-only t)
  6485. (if (not org-select-agenda-window) (select-window win))
  6486. (message "")))
  6487. (defvar org-select-this-todo-keyword nil)
  6488. ;;;###autoload
  6489. (defun org-todo-list (arg &optional keep-modes)
  6490. "Show all TODO entries from all agenda file in a single list.
  6491. The prefix arg can be used to select a specific TODO keyword and limit
  6492. the list to these. When using \\[universal-argument], you will be prompted
  6493. for a keyword. A numeric prefix directly selects the Nth keyword in
  6494. `org-todo-keywords'."
  6495. (interactive "P")
  6496. (org-compile-prefix-format 'todo)
  6497. (org-set-sorting-strategy 'todo)
  6498. (let* ((org-agenda-keep-modes keep-modes)
  6499. (today (time-to-days (current-time)))
  6500. (date (calendar-gregorian-from-absolute today))
  6501. (win (selected-window))
  6502. (kwds org-todo-keywords)
  6503. (completion-ignore-case t)
  6504. (org-select-this-todo-keyword
  6505. (if (stringp arg) arg
  6506. (and arg (integerp arg) (> arg 0)
  6507. (nth (1- arg) org-todo-keywords))))
  6508. rtn rtnall files file pos)
  6509. (when (equal arg '(4))
  6510. (setq org-select-this-todo-keyword
  6511. (completing-read "Keyword: " (mapcar 'list org-todo-keywords)
  6512. nil t)))
  6513. (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
  6514. (org-prepare-agenda)
  6515. (org-set-local 'last-arg arg)
  6516. (org-set-local 'org-todo-keywords kwds)
  6517. (setq org-agenda-redo-command
  6518. '(org-todo-list (or current-prefix-arg last-arg) t))
  6519. (setq files (org-agenda-files)
  6520. rtnall nil)
  6521. (while (setq file (pop files))
  6522. (catch 'nextfile
  6523. (org-check-agenda-file file)
  6524. (setq rtn (org-agenda-get-day-entries file date :todo))
  6525. (setq rtnall (append rtnall rtn))))
  6526. (insert "Global list of TODO items of type: ")
  6527. (add-text-properties (point-min) (1- (point))
  6528. (list 'face 'org-level-3))
  6529. (setq pos (point))
  6530. (insert (or org-select-this-todo-keyword "ALL") "\n")
  6531. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  6532. (setq pos (point))
  6533. (unless org-agenda-multi
  6534. (insert
  6535. "Available with `N r': (0)ALL "
  6536. (let ((n 0))
  6537. (mapconcat (lambda (x)
  6538. (format "(%d)%s" (setq n (1+ n)) x))
  6539. org-todo-keywords " "))
  6540. "\n"))
  6541. (add-text-properties pos (1- (point)) (list 'face 'org-level-3))
  6542. (when rtnall
  6543. (insert (org-finalize-agenda-entries rtnall) "\n"))
  6544. (goto-char (point-min))
  6545. (org-fit-agenda-window)
  6546. (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
  6547. (org-agenda-align-tags)
  6548. (setq buffer-read-only t)
  6549. (if (not org-select-agenda-window) (select-window win))))
  6550. (defun org-check-agenda-file (file)
  6551. "Make sure FILE exists. If not, ask user what to do."
  6552. (when (not (file-exists-p file))
  6553. (message "non-existent file %s. [R]emove from list or [A]bort?"
  6554. (abbreviate-file-name file))
  6555. (let ((r (downcase (read-char-exclusive))))
  6556. (cond
  6557. ((equal r ?r)
  6558. (org-remove-file file)
  6559. (throw 'nextfile t))
  6560. (t (error "Abort"))))))
  6561. (defun org-agenda-check-type (error &rest types)
  6562. "Check if agenda buffer is of allowed type.
  6563. If ERROR is non-nil, throw an error, otherwise just return nil."
  6564. (if (memq org-agenda-type types)
  6565. t
  6566. (if error
  6567. (error "Not allowed in %s-type agenda buffers" org-agenda-type)
  6568. nil)))
  6569. (defun org-agenda-quit ()
  6570. "Exit agenda by removing the window or the buffer."
  6571. (interactive)
  6572. (let ((buf (current-buffer)))
  6573. (if (not (one-window-p)) (delete-window))
  6574. (kill-buffer buf)
  6575. (org-agenda-maybe-reset-markers 'force)))
  6576. (defun org-agenda-exit ()
  6577. "Exit agenda by removing the window or the buffer.
  6578. Also kill all Org-mode buffers which have been loaded by `org-agenda'.
  6579. Org-mode buffers visited directly by the user will not be touched."
  6580. (interactive)
  6581. (org-release-buffers org-agenda-new-buffers)
  6582. (setq org-agenda-new-buffers nil)
  6583. (org-agenda-quit))
  6584. ;; FIXME: move this function.
  6585. (defun org-save-all-org-buffers ()
  6586. "Save all Org-mode buffers without user confirmation."
  6587. (interactive)
  6588. (message "Saving all Org-mode buffers...")
  6589. (save-some-buffers t 'org-mode-p)
  6590. (message "Saving all Org-mode buffers... done"))
  6591. (defun org-agenda-redo ()
  6592. "Rebuild Agenda.
  6593. When this is the global TODO list, a prefix argument will be interpreted."
  6594. (interactive)
  6595. (let* ((line (org-current-line))
  6596. (window-line (- line (org-current-line (window-start)))))
  6597. (message "Rebuilding agenda buffer...")
  6598. (eval org-agenda-redo-command)
  6599. (message "Rebuilding agenda buffer...done")
  6600. (goto-line line)
  6601. (recenter window-line)))
  6602. (defun org-agenda-goto-today ()
  6603. "Go to today."
  6604. (interactive)
  6605. (org-agenda-check-type t 'timeline 'agenda)
  6606. (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
  6607. (cond
  6608. (tdpos (goto-char tdpos))
  6609. ((eq org-agenda-type 'agenda)
  6610. (let ((org-agenda-overriding-arguments org-agenda-last-arguments))
  6611. (setf (nth 1 org-agenda-overriding-arguments) nil)
  6612. (org-agenda-redo)
  6613. (org-agenda-find-today-or-agenda)))
  6614. (t (error "Cannot find today")))))
  6615. (defun org-agenda-find-today-or-agenda ()
  6616. (goto-char
  6617. (or (text-property-any (point-min) (point-max) 'org-today t)
  6618. (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
  6619. (point-min))))
  6620. (defun org-agenda-later (arg)
  6621. "Go forward in time by `org-agenda-ndays' days.
  6622. With prefix ARG, go forward that many times `org-agenda-ndays'."
  6623. (interactive "p")
  6624. (org-agenda-check-type t 'agenda)
  6625. (let ((org-agenda-overriding-arguments
  6626. (list (car org-agenda-last-arguments)
  6627. (+ starting-day (* arg org-agenda-ndays))
  6628. nil t)))
  6629. (org-agenda-redo)
  6630. (org-agenda-find-today-or-agenda)))
  6631. (defun org-agenda-earlier (arg)
  6632. "Go back in time by `org-agenda-ndays' days.
  6633. With prefix ARG, go back that many times `org-agenda-ndays'."
  6634. (interactive "p")
  6635. (org-agenda-check-type t 'agenda)
  6636. (let ((org-agenda-overriding-arguments
  6637. (list (car org-agenda-last-arguments)
  6638. (- starting-day (* arg org-agenda-ndays))
  6639. nil t)))
  6640. (org-agenda-redo)
  6641. (org-agenda-find-today-or-agenda)))
  6642. (defun org-agenda-week-view ()
  6643. "Switch to weekly view for agenda."
  6644. (interactive)
  6645. (org-agenda-check-type t 'agenda)
  6646. (if (= org-agenda-ndays 7)
  6647. (error "This is already the week view"))
  6648. (setq org-agenda-ndays 7)
  6649. (let ((org-agenda-overriding-arguments
  6650. (list (car org-agenda-last-arguments)
  6651. (or (get-text-property (point) 'day)
  6652. starting-day)
  6653. nil t)))
  6654. (org-agenda-redo)
  6655. (org-agenda-find-today-or-agenda))
  6656. (org-agenda-set-mode-name)
  6657. (message "Switched to week view"))
  6658. (defun org-agenda-day-view ()
  6659. "Switch to daily view for agenda."
  6660. (interactive)
  6661. (org-agenda-check-type t 'agenda)
  6662. (if (= org-agenda-ndays 1)
  6663. (error "This is already the day view"))
  6664. (setq org-agenda-ndays 1)
  6665. (let ((org-agenda-overriding-arguments
  6666. (list (car org-agenda-last-arguments)
  6667. (or (get-text-property (point) 'day)
  6668. starting-day)
  6669. nil t)))
  6670. (org-agenda-redo)
  6671. (org-agenda-find-today-or-agenda))
  6672. (org-agenda-set-mode-name)
  6673. (message "Switched to day view"))
  6674. (defun org-agenda-next-date-line (&optional arg)
  6675. "Jump to the next line indicating a date in agenda buffer."
  6676. (interactive "p")
  6677. (org-agenda-check-type t 'agenda 'timeline)
  6678. (beginning-of-line 1)
  6679. (if (looking-at "^\\S-") (forward-char 1))
  6680. (if (not (re-search-forward "^\\S-" nil t arg))
  6681. (progn
  6682. (backward-char 1)
  6683. (error "No next date after this line in this buffer")))
  6684. (goto-char (match-beginning 0)))
  6685. (defun org-agenda-previous-date-line (&optional arg)
  6686. "Jump to the previous line indicating a date in agenda buffer."
  6687. (interactive "p")
  6688. (org-agenda-check-type t 'agenda 'timeline)
  6689. (beginning-of-line 1)
  6690. (if (not (re-search-backward "^\\S-" nil t arg))
  6691. (error "No previous date before this line in this buffer")))
  6692. ;; Initialize the highlight
  6693. (defvar org-hl (org-make-overlay 1 1))
  6694. (org-overlay-put org-hl 'face 'highlight)
  6695. (defun org-highlight (begin end &optional buffer)
  6696. "Highlight a region with overlay."
  6697. (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
  6698. org-hl begin end (or buffer (current-buffer))))
  6699. (defun org-unhighlight ()
  6700. "Detach overlay INDEX."
  6701. (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
  6702. (defun org-agenda-follow-mode ()
  6703. "Toggle follow mode in an agenda buffer."
  6704. (interactive)
  6705. (setq org-agenda-follow-mode (not org-agenda-follow-mode))
  6706. (org-agenda-set-mode-name)
  6707. (message "Follow mode is %s"
  6708. (if org-agenda-follow-mode "on" "off")))
  6709. (defun org-agenda-log-mode ()
  6710. "Toggle log mode in an agenda buffer."
  6711. (interactive)
  6712. (org-agenda-check-type t 'agenda 'timeline)
  6713. (setq org-agenda-show-log (not org-agenda-show-log))
  6714. (org-agenda-set-mode-name)
  6715. (org-agenda-redo)
  6716. (message "Log mode is %s"
  6717. (if org-agenda-show-log "on" "off")))
  6718. (defun org-agenda-toggle-diary ()
  6719. "Toggle diary inclusion in an agenda buffer."
  6720. (interactive)
  6721. (org-agenda-check-type t 'agenda)
  6722. (setq org-agenda-include-diary (not org-agenda-include-diary))
  6723. (org-agenda-redo)
  6724. (org-agenda-set-mode-name)
  6725. (message "Diary inclusion turned %s"
  6726. (if org-agenda-include-diary "on" "off")))
  6727. (defun org-agenda-toggle-time-grid ()
  6728. "Toggle time grid in an agenda buffer."
  6729. (interactive)
  6730. (org-agenda-check-type t 'agenda)
  6731. (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
  6732. (org-agenda-redo)
  6733. (org-agenda-set-mode-name)
  6734. (message "Time-grid turned %s"
  6735. (if org-agenda-use-time-grid "on" "off")))
  6736. (defun org-agenda-set-mode-name ()
  6737. "Set the mode name to indicate all the small mode settings."
  6738. (setq mode-name
  6739. (concat "Org-Agenda"
  6740. (if (equal org-agenda-ndays 1) " Day" "")
  6741. (if (equal org-agenda-ndays 7) " Week" "")
  6742. (if org-agenda-follow-mode " Follow" "")
  6743. (if org-agenda-include-diary " Diary" "")
  6744. (if org-agenda-use-time-grid " Grid" "")
  6745. (if org-agenda-show-log " Log" "")))
  6746. (force-mode-line-update))
  6747. (defun org-agenda-post-command-hook ()
  6748. (and (eolp) (not (bolp)) (backward-char 1))
  6749. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  6750. (if (and org-agenda-follow-mode
  6751. (get-text-property (point) 'org-marker))
  6752. (org-agenda-show)))
  6753. (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
  6754. (defun org-get-entries-from-diary (date)
  6755. "Get the (Emacs Calendar) diary entries for DATE."
  6756. (let* ((fancy-diary-buffer "*temporary-fancy-diary-buffer*")
  6757. (diary-display-hook '(fancy-diary-display))
  6758. (list-diary-entries-hook
  6759. (cons 'org-diary-default-entry list-diary-entries-hook))
  6760. (diary-file-name-prefix-function nil) ; turn this feature off
  6761. (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
  6762. entries
  6763. (org-disable-agenda-to-diary t))
  6764. (save-excursion
  6765. (save-window-excursion
  6766. (list-diary-entries date 1))) ;; Keep this name for now, compatibility
  6767. (if (not (get-buffer fancy-diary-buffer))
  6768. (setq entries nil)
  6769. (with-current-buffer fancy-diary-buffer
  6770. (setq buffer-read-only nil)
  6771. (if (= (point-max) 1)
  6772. ;; No entries
  6773. (setq entries nil)
  6774. ;; Omit the date and other unnecessary stuff
  6775. (org-agenda-cleanup-fancy-diary)
  6776. ;; Add prefix to each line and extend the text properties
  6777. (if (= (point-max) 1)
  6778. (setq entries nil)
  6779. (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
  6780. (set-buffer-modified-p nil)
  6781. (kill-buffer fancy-diary-buffer)))
  6782. (when entries
  6783. (setq entries (org-split-string entries "\n"))
  6784. (setq entries
  6785. (mapcar
  6786. (lambda (x)
  6787. (setq x (org-format-agenda-item "" x "Diary" nil 'time))
  6788. ;; Extend the text properties to the beginning of the line
  6789. (org-add-props x (text-properties-at (1- (length x)) x)))
  6790. entries)))))
  6791. (defun org-agenda-cleanup-fancy-diary ()
  6792. "Remove unwanted stuff in buffer created by `fancy-diary-display'.
  6793. This gets rid of the date, the underline under the date, and
  6794. the dummy entry installed by `org-mode' to ensure non-empty diary for each
  6795. date. It also removes lines that contain only whitespace."
  6796. (goto-char (point-min))
  6797. (if (looking-at ".*?:[ \t]*")
  6798. (progn
  6799. (replace-match "")
  6800. (re-search-forward "\n=+$" nil t)
  6801. (replace-match "")
  6802. (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
  6803. (re-search-forward "\n=+$" nil t)
  6804. (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
  6805. (goto-char (point-min))
  6806. (while (re-search-forward "^ +\n" nil t)
  6807. (replace-match ""))
  6808. (goto-char (point-min))
  6809. (if (re-search-forward "^Org-mode dummy\n?" nil t)
  6810. (replace-match "")))
  6811. ;; Make sure entries from the diary have the right text properties.
  6812. (eval-after-load "diary-lib"
  6813. '(if (boundp 'diary-modify-entry-list-string-function)
  6814. ;; We can rely on the hook, nothing to do
  6815. nil
  6816. ;; Hook not avaiable, must use advice to make this work
  6817. (defadvice add-to-diary-list (before org-mark-diary-entry activate)
  6818. "Make the position visible."
  6819. (if (and org-disable-agenda-to-diary ;; called from org-agenda
  6820. (stringp string)
  6821. buffer-file-name)
  6822. (setq string (org-modify-diary-entry-string string))))))
  6823. (defun org-modify-diary-entry-string (string)
  6824. "Add text properties to string, allowing org-mode to act on it."
  6825. (org-add-props string nil
  6826. 'mouse-face 'highlight
  6827. 'keymap org-agenda-keymap
  6828. 'help-echo (format "mouse-2 or RET jump to diary file %s"
  6829. (abbreviate-file-name buffer-file-name))
  6830. 'org-agenda-diary-link t
  6831. 'org-marker (org-agenda-new-marker (point-at-bol))))
  6832. (defun org-diary-default-entry ()
  6833. "Add a dummy entry to the diary.
  6834. Needed to avoid empty dates which mess up holiday display."
  6835. ;; Catch the error if dealing with the new add-to-diary-alist
  6836. (when org-disable-agenda-to-diary
  6837. (condition-case nil
  6838. (add-to-diary-list original-date "Org-mode dummy" "")
  6839. (error
  6840. (add-to-diary-list original-date "Org-mode dummy" "" nil)))))
  6841. (defun org-cycle-agenda-files ()
  6842. "Cycle through the files in `org-agenda-files'.
  6843. If the current buffer visits an agenda file, find the next one in the list.
  6844. If the current buffer does not, find the first agenda file."
  6845. (interactive)
  6846. (let* ((fs (org-agenda-files t))
  6847. (files (append fs (list (car fs))))
  6848. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  6849. file)
  6850. (unless files (error "No agenda files"))
  6851. (catch 'exit
  6852. (while (setq file (pop files))
  6853. (if (equal (file-truename file) tcf)
  6854. (when (car files)
  6855. (find-file (car files))
  6856. (throw 'exit t))))
  6857. (find-file (car fs)))))
  6858. (defun org-agenda-file-to-end ()
  6859. "Move/add the current file to the end of the agenda file list.
  6860. If the file is not present in the list, it is appended to the list. If it is
  6861. present, it is moved there."
  6862. (interactive)
  6863. (org-agenda-file-to-front 'to-end))
  6864. (defun org-agenda-file-to-front (&optional to-end)
  6865. "Move/add the current file to the top of the agenda file list.
  6866. If the file is not present in the list, it is added to the front. If it is
  6867. present, it is moved there. With optional argument TO-END, add/move to the
  6868. end of the list."
  6869. (interactive "P")
  6870. (let ((file-alist (mapcar (lambda (x)
  6871. (cons (file-truename x) x))
  6872. (org-agenda-files t)))
  6873. (ctf (file-truename buffer-file-name))
  6874. x had)
  6875. (setq x (assoc ctf file-alist) had x)
  6876. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  6877. (if to-end
  6878. (setq file-alist (append (delq x file-alist) (list x)))
  6879. (setq file-alist (cons x (delq x file-alist))))
  6880. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  6881. (org-install-agenda-files-menu)
  6882. (message "File %s to %s of agenda file list"
  6883. (if had "moved" "added") (if to-end "end" "front"))))
  6884. (defun org-remove-file (&optional file)
  6885. "Remove current file from the list of files in variable `org-agenda-files'.
  6886. These are the files which are being checked for agenda entries.
  6887. Optional argument FILE means, use this file instead of the current."
  6888. (interactive)
  6889. (let* ((file (or file buffer-file-name))
  6890. (true-file (file-truename file))
  6891. (afile (abbreviate-file-name file))
  6892. (files (delq nil (mapcar
  6893. (lambda (x)
  6894. (if (equal true-file
  6895. (file-truename x))
  6896. nil x))
  6897. (org-agenda-files t)))))
  6898. (if (not (= (length files) (length (org-agenda-files t))))
  6899. (progn
  6900. (org-store-new-agenda-file-list files)
  6901. (org-install-agenda-files-menu)
  6902. (message "Removed file: %s" afile))
  6903. (message "File was not in list: %s" afile))))
  6904. (defun org-file-menu-entry (file)
  6905. (vector file (list 'find-file file) t))
  6906. (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty)
  6907. "Return a list of all relevant day numbers from BEG to END buffer positions.
  6908. If NO-RANGES is non-nil, include only the start and end dates of a range,
  6909. not every single day in the range. If FORCE-TODAY is non-nil, make
  6910. sure that TODAY is included in the list. If INACTIVE is non-nil, also
  6911. inactive time stamps (those in square brackets) are included.
  6912. When EMPTY is non-nil, also include days without any entries."
  6913. (let ((re (if inactive org-ts-regexp-both org-ts-regexp))
  6914. dates dates1 date day day1 day2 ts1 ts2)
  6915. (if force-today
  6916. (setq dates (list (time-to-days (current-time)))))
  6917. (save-excursion
  6918. (goto-char beg)
  6919. (while (re-search-forward re end t)
  6920. (setq day (time-to-days (org-time-string-to-time
  6921. (substring (match-string 1) 0 10))))
  6922. (or (memq day dates) (push day dates)))
  6923. (unless no-ranges
  6924. (goto-char beg)
  6925. (while (re-search-forward org-tr-regexp end t)
  6926. (setq ts1 (substring (match-string 1) 0 10)
  6927. ts2 (substring (match-string 2) 0 10)
  6928. day1 (time-to-days (org-time-string-to-time ts1))
  6929. day2 (time-to-days (org-time-string-to-time ts2)))
  6930. (while (< (setq day1 (1+ day1)) day2)
  6931. (or (memq day1 dates) (push day1 dates)))))
  6932. (setq dates (sort dates '<))
  6933. (when empty
  6934. (while (setq day (pop dates))
  6935. (setq day2 (car dates))
  6936. (push day dates1)
  6937. (when (and day2 empty)
  6938. (if (or (eq empty t)
  6939. (and (numberp empty) (<= (- day2 day) empty)))
  6940. (while (< (setq day (1+ day)) day2)
  6941. (push (list day) dates1))
  6942. (push (cons :omitted (- day2 day)) dates1))))
  6943. (setq dates (nreverse dates1)))
  6944. dates)))
  6945. ;;;###autoload
  6946. (defun org-diary (&rest args)
  6947. "Return diary information from org-files.
  6948. This function can be used in a \"sexp\" diary entry in the Emacs calendar.
  6949. It accesses org files and extracts information from those files to be
  6950. listed in the diary. The function accepts arguments specifying what
  6951. items should be listed. The following arguments are allowed:
  6952. :timestamp List the headlines of items containing a date stamp or
  6953. date range matching the selected date. Deadlines will
  6954. also be listed, on the expiration day.
  6955. :deadline List any deadlines past due, or due within
  6956. `org-deadline-warning-days'. The listing occurs only
  6957. in the diary for *today*, not at any other date. If
  6958. an entry is marked DONE, it is no longer listed.
  6959. :scheduled List all items which are scheduled for the given date.
  6960. The diary for *today* also contains items which were
  6961. scheduled earlier and are not yet marked DONE.
  6962. :todo List all TODO items from the org-file. This may be a
  6963. long list - so this is not turned on by default.
  6964. Like deadlines, these entries only show up in the
  6965. diary for *today*, not at any other date.
  6966. The call in the diary file should look like this:
  6967. &%%(org-diary) ~/path/to/some/orgfile.org
  6968. Use a separate line for each org file to check. Or, if you omit the file name,
  6969. all files listed in `org-agenda-files' will be checked automatically:
  6970. &%%(org-diary)
  6971. If you don't give any arguments (as in the example above), the default
  6972. arguments (:deadline :scheduled :timestamp) are used. So the example above may
  6973. also be written as
  6974. &%%(org-diary :deadline :timestamp :scheduled)
  6975. The function expects the lisp variables `entry' and `date' to be provided
  6976. by the caller, because this is how the calendar works. Don't use this
  6977. function from a program - use `org-agenda-get-day-entries' instead."
  6978. (org-agenda-maybe-reset-markers) ;; FIXME: does this still do the right thing?
  6979. (org-compile-prefix-format 'agenda)
  6980. (org-set-sorting-strategy 'agenda)
  6981. (setq args (or args '(:deadline :scheduled :timestamp)))
  6982. (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
  6983. (list entry)
  6984. (org-agenda-files t)))
  6985. file rtn results)
  6986. ;; If this is called during org-agenda, don't return any entries to
  6987. ;; the calendar. Org Agenda will list these entries itself.
  6988. (if org-disable-agenda-to-diary (setq files nil))
  6989. (while (setq file (pop files))
  6990. (setq rtn (apply 'org-agenda-get-day-entries file date args))
  6991. (setq results (append results rtn)))
  6992. (if results
  6993. (concat (org-finalize-agenda-entries results) "\n"))))
  6994. (defvar org-category-table nil)
  6995. (defun org-get-category-table ()
  6996. "Get the table of categories and positions in current buffer."
  6997. (let (tbl)
  6998. (save-excursion
  6999. (goto-char (point-min))
  7000. (while (re-search-forward "\\(^\\|\r\\)#\\+CATEGORY:[ \t]*\\(.*\\)" nil t)
  7001. (push (cons (point) (org-trim (match-string 2))) tbl)))
  7002. tbl))
  7003. (defun org-get-category (&optional pos)
  7004. "Get the category applying to position POS."
  7005. (if (not org-category-table)
  7006. (cond
  7007. ((null org-category)
  7008. (setq org-category
  7009. (if buffer-file-name
  7010. (file-name-sans-extension
  7011. (file-name-nondirectory buffer-file-name))
  7012. "???")))
  7013. ((symbolp org-category) (symbol-name org-category))
  7014. (t org-category))
  7015. (let ((tbl org-category-table)
  7016. (pos (or pos (point))))
  7017. (while (and tbl (> (caar tbl) pos))
  7018. (pop tbl))
  7019. (or (cdar tbl) (cdr (nth (1- (length org-category-table))
  7020. org-category-table))))))
  7021. (defun org-agenda-get-day-entries (file date &rest args)
  7022. "Does the work for `org-diary' and `org-agenda'.
  7023. FILE is the path to a file to be checked for entries. DATE is date like
  7024. the one returned by `calendar-current-date'. ARGS are symbols indicating
  7025. which kind of entries should be extracted. For details about these, see
  7026. the documentation of `org-diary'."
  7027. (setq args (or args '(:deadline :scheduled :timestamp)))
  7028. (let* ((org-startup-with-deadline-check nil)
  7029. (org-startup-folded nil)
  7030. (org-startup-align-all-tables nil)
  7031. (buffer (if (file-exists-p file)
  7032. (org-get-agenda-file-buffer file)
  7033. (error "No such file %s" file)))
  7034. arg results rtn)
  7035. (if (not buffer)
  7036. ;; If file does not exist, make sure an error message ends up in diary
  7037. (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  7038. (with-current-buffer buffer
  7039. (unless (org-mode-p)
  7040. (error "Agenda file %s is not in `org-mode'" file))
  7041. (setq org-category-table (org-get-category-table))
  7042. (let ((case-fold-search nil))
  7043. (save-excursion
  7044. (save-restriction
  7045. (if org-agenda-restrict
  7046. (narrow-to-region org-agenda-restrict-begin
  7047. org-agenda-restrict-end)
  7048. (widen))
  7049. ;; The way we repeatedly append to `results' makes it O(n^2) :-(
  7050. (while (setq arg (pop args))
  7051. (cond
  7052. ((and (eq arg :todo)
  7053. (equal date (calendar-current-date)))
  7054. (setq rtn (org-agenda-get-todos))
  7055. (setq results (append results rtn)))
  7056. ((eq arg :timestamp)
  7057. (setq rtn (org-agenda-get-blocks))
  7058. (setq results (append results rtn))
  7059. (setq rtn (org-agenda-get-timestamps))
  7060. (setq results (append results rtn)))
  7061. ((eq arg :scheduled)
  7062. (setq rtn (org-agenda-get-scheduled))
  7063. (setq results (append results rtn)))
  7064. ((eq arg :closed)
  7065. (setq rtn (org-agenda-get-closed))
  7066. (setq results (append results rtn)))
  7067. ((and (eq arg :deadline)
  7068. (equal date (calendar-current-date)))
  7069. (setq rtn (org-agenda-get-deadlines))
  7070. (setq results (append results rtn))))))))
  7071. results))))
  7072. (defun org-entry-is-done-p ()
  7073. "Is the current entry marked DONE?"
  7074. (save-excursion
  7075. (and (re-search-backward "[\r\n]\\*" nil t)
  7076. (looking-at org-nl-done-regexp))))
  7077. (defun org-at-date-range-p ()
  7078. "Is the cursor inside a date range?"
  7079. (interactive)
  7080. (save-excursion
  7081. (catch 'exit
  7082. (let ((pos (point)))
  7083. (skip-chars-backward "^<\r\n")
  7084. (skip-chars-backward "<")
  7085. (and (looking-at org-tr-regexp)
  7086. (>= (match-end 0) pos)
  7087. (throw 'exit t))
  7088. (skip-chars-backward "^<\r\n")
  7089. (skip-chars-backward "<")
  7090. (and (looking-at org-tr-regexp)
  7091. (>= (match-end 0) pos)
  7092. (throw 'exit t)))
  7093. nil)))
  7094. (defun org-agenda-get-todos ()
  7095. "Return the TODO information for agenda display."
  7096. (let* ((props (list 'face nil
  7097. 'done-face 'org-done
  7098. 'org-not-done-regexp org-not-done-regexp
  7099. 'mouse-face 'highlight
  7100. 'keymap org-agenda-keymap
  7101. 'help-echo
  7102. (format "mouse-2 or RET jump to org file %s"
  7103. (abbreviate-file-name buffer-file-name))))
  7104. (regexp (concat "[\n\r]\\*+ *\\("
  7105. (if org-select-this-todo-keyword
  7106. (concat "\\<\\(" org-select-this-todo-keyword
  7107. "\\)\\>")
  7108. org-not-done-regexp)
  7109. "[^\n\r]*\\)"))
  7110. (sched-re (concat ".*\n?.*?" org-scheduled-time-regexp))
  7111. marker priority category tags
  7112. ee txt)
  7113. (goto-char (point-min))
  7114. (while (re-search-forward regexp nil t)
  7115. (catch :skip
  7116. (when (and org-agenda-todo-ignore-scheduled
  7117. (looking-at sched-re))
  7118. ;; FIXME: the following test also happens below, but we need it here
  7119. (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
  7120. (throw :skip nil))
  7121. (org-agenda-skip)
  7122. (goto-char (match-beginning 1))
  7123. (setq marker (org-agenda-new-marker (1+ (match-beginning 0)))
  7124. category (org-get-category)
  7125. tags (org-get-tags-at (point))
  7126. txt (org-format-agenda-item "" (match-string 1) category tags)
  7127. priority
  7128. (+ (org-get-priority txt)
  7129. (if org-todo-kwd-priority-p
  7130. (- org-todo-kwd-max-priority -2
  7131. (length
  7132. (member (match-string 2) org-todo-keywords)))
  7133. 1)))
  7134. (org-add-props txt props
  7135. 'org-marker marker 'org-hd-marker marker
  7136. 'priority priority 'category category)
  7137. (push txt ee)
  7138. (if org-agenda-todo-list-sublevels
  7139. (goto-char (match-end 1))
  7140. (org-end-of-subtree 'invisible))))
  7141. (nreverse ee)))
  7142. (defconst org-agenda-no-heading-message
  7143. "No heading for this item in buffer or region.")
  7144. (defun org-agenda-get-timestamps ()
  7145. "Return the date stamp information for agenda display."
  7146. (let* ((props (list 'face nil
  7147. 'org-not-done-regexp org-not-done-regexp
  7148. 'mouse-face 'highlight
  7149. 'keymap org-agenda-keymap
  7150. 'help-echo
  7151. (format "mouse-2 or RET jump to org file %s"
  7152. (abbreviate-file-name buffer-file-name))))
  7153. (regexp (regexp-quote
  7154. (substring
  7155. (format-time-string
  7156. (car org-time-stamp-formats)
  7157. (apply 'encode-time ; DATE bound by calendar
  7158. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  7159. 0 11)))
  7160. marker hdmarker deadlinep scheduledp donep tmp priority category
  7161. ee txt timestr tags)
  7162. (goto-char (point-min))
  7163. (while (re-search-forward regexp nil t)
  7164. (catch :skip
  7165. (and (save-match-data (org-at-date-range-p)) (throw :skip nil))
  7166. (org-agenda-skip)
  7167. (setq marker (org-agenda-new-marker (match-beginning 0))
  7168. category (org-get-category (match-beginning 0))
  7169. tmp (buffer-substring (max (point-min)
  7170. (- (match-beginning 0)
  7171. org-ds-keyword-length))
  7172. (match-beginning 0))
  7173. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  7174. deadlinep (string-match org-deadline-regexp tmp)
  7175. scheduledp (string-match org-scheduled-regexp tmp)
  7176. donep (org-entry-is-done-p))
  7177. (if (string-match ">" timestr)
  7178. ;; substring should only run to end of time stamp
  7179. (setq timestr (substring timestr 0 (match-end 0))))
  7180. (save-excursion
  7181. (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t)
  7182. (progn
  7183. (goto-char (match-end 1))
  7184. (setq hdmarker (org-agenda-new-marker)
  7185. tags (org-get-tags-at))
  7186. (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
  7187. (setq txt (org-format-agenda-item
  7188. (format "%s%s"
  7189. (if deadlinep "Deadline: " "")
  7190. (if scheduledp "Scheduled: " ""))
  7191. (match-string 1) category tags timestr)))
  7192. (setq txt org-agenda-no-heading-message))
  7193. (setq priority (org-get-priority txt))
  7194. (org-add-props txt props
  7195. 'org-marker marker 'org-hd-marker hdmarker)
  7196. (if deadlinep
  7197. (org-add-props txt nil
  7198. 'face (if donep 'org-done 'org-warning)
  7199. 'undone-face 'org-warning 'done-face 'org-done
  7200. 'category category 'priority (+ 100 priority))
  7201. (if scheduledp
  7202. (org-add-props txt nil
  7203. 'face 'org-scheduled-today
  7204. 'undone-face 'org-scheduled-today 'done-face 'org-done
  7205. 'category category 'priority (+ 99 priority))
  7206. (org-add-props txt nil 'priority priority 'category category)))
  7207. (push txt ee))
  7208. (outline-next-heading)))
  7209. (nreverse ee)))
  7210. (defun org-agenda-get-closed ()
  7211. "Return the logged TODO entries for agenda display."
  7212. (let* ((props (list 'mouse-face 'highlight
  7213. 'org-not-done-regexp org-not-done-regexp
  7214. 'keymap org-agenda-keymap
  7215. 'help-echo
  7216. (format "mouse-2 or RET jump to org file %s"
  7217. (abbreviate-file-name buffer-file-name))))
  7218. (regexp (concat
  7219. "\\<\\(" org-closed-string "\\|" org-clock-string "\\) *\\["
  7220. (regexp-quote
  7221. (substring
  7222. (format-time-string
  7223. (car org-time-stamp-formats)
  7224. (apply 'encode-time ; DATE bound by calendar
  7225. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  7226. 1 11))))
  7227. marker hdmarker priority category tags closedp
  7228. ee txt timestr)
  7229. (goto-char (point-min))
  7230. (while (re-search-forward regexp nil t)
  7231. (catch :skip
  7232. (org-agenda-skip)
  7233. (setq marker (org-agenda-new-marker (match-beginning 0))
  7234. closedp (equal (match-string 1) org-closed-string)
  7235. category (org-get-category (match-beginning 0))
  7236. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  7237. ;; donep (org-entry-is-done-p)
  7238. )
  7239. (if (string-match "\\]" timestr)
  7240. ;; substring should only run to end of time stamp
  7241. (setq timestr (substring timestr 0 (match-end 0))))
  7242. (save-excursion
  7243. (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t)
  7244. (progn
  7245. (goto-char (match-end 1))
  7246. (setq hdmarker (org-agenda-new-marker)
  7247. tags (org-get-tags-at))
  7248. (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
  7249. (setq txt (org-format-agenda-item
  7250. (if closedp "Closed: " "Clocked: ")
  7251. (match-string 1) category tags timestr)))
  7252. (setq txt org-agenda-no-heading-message))
  7253. (setq priority 100000)
  7254. (org-add-props txt props
  7255. 'org-marker marker 'org-hd-marker hdmarker 'face 'org-done
  7256. 'priority priority 'category category
  7257. 'undone-face 'org-warning 'done-face 'org-done)
  7258. (push txt ee))
  7259. (outline-next-heading)))
  7260. (nreverse ee)))
  7261. (defun org-agenda-get-deadlines ()
  7262. "Return the deadline information for agenda display."
  7263. (let* ((wdays org-deadline-warning-days)
  7264. (props (list 'mouse-face 'highlight
  7265. 'org-not-done-regexp org-not-done-regexp
  7266. 'keymap org-agenda-keymap
  7267. 'help-echo
  7268. (format "mouse-2 or RET jump to org file %s"
  7269. (abbreviate-file-name buffer-file-name))))
  7270. (regexp org-deadline-time-regexp)
  7271. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  7272. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  7273. d2 diff pos pos1 category tags
  7274. ee txt head face)
  7275. (goto-char (point-min))
  7276. (while (re-search-forward regexp nil t)
  7277. (catch :skip
  7278. (org-agenda-skip)
  7279. (setq pos (1- (match-beginning 1))
  7280. d2 (time-to-days
  7281. (org-time-string-to-time (match-string 1)))
  7282. diff (- d2 d1))
  7283. ;; When to show a deadline in the calendar:
  7284. ;; If the expiration is within wdays warning time.
  7285. ;; Past-due deadlines are only shown on the current date
  7286. (if (and (< diff wdays) todayp (not (= diff 0)))
  7287. (save-excursion
  7288. (setq category (org-get-category))
  7289. (if (re-search-backward "\\(^\\|\r\\)\\*+[ \t]*" nil t)
  7290. (progn
  7291. (goto-char (match-end 0))
  7292. (setq pos1 (match-end 1))
  7293. (setq tags (org-get-tags-at pos1))
  7294. (setq head (buffer-substring-no-properties
  7295. (point)
  7296. (progn (skip-chars-forward "^\r\n")
  7297. (point))))
  7298. (if (string-match org-looking-at-done-regexp head)
  7299. (setq txt nil)
  7300. (setq txt (org-format-agenda-item
  7301. (format "In %3d d.: " diff) head category tags))))
  7302. (setq txt org-agenda-no-heading-message))
  7303. (when txt
  7304. (setq face (cond ((<= diff 0) 'org-warning)
  7305. ((<= diff 5) 'org-upcoming-deadline)
  7306. (t nil)))
  7307. (org-add-props txt props
  7308. 'org-marker (org-agenda-new-marker pos)
  7309. 'org-hd-marker (org-agenda-new-marker pos1)
  7310. 'priority (+ (- 10 diff) (org-get-priority txt))
  7311. 'category category
  7312. 'face face 'undone-face face 'done-face 'org-done)
  7313. (push txt ee))))))
  7314. ee))
  7315. (defun org-agenda-get-scheduled ()
  7316. "Return the scheduled information for agenda display."
  7317. (let* ((props (list 'face 'org-scheduled-previously
  7318. 'org-not-done-regexp org-not-done-regexp
  7319. 'undone-face 'org-scheduled-previously
  7320. 'done-face 'org-done
  7321. 'mouse-face 'highlight
  7322. 'keymap org-agenda-keymap
  7323. 'help-echo
  7324. (format "mouse-2 or RET jump to org file %s"
  7325. (abbreviate-file-name buffer-file-name))))
  7326. (regexp org-scheduled-time-regexp)
  7327. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  7328. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  7329. d2 diff pos pos1 category tags
  7330. ee txt head)
  7331. (goto-char (point-min))
  7332. (while (re-search-forward regexp nil t)
  7333. (catch :skip
  7334. (org-agenda-skip)
  7335. (setq pos (1- (match-beginning 1))
  7336. d2 (time-to-days
  7337. (org-time-string-to-time (match-string 1)))
  7338. diff (- d2 d1))
  7339. ;; When to show a scheduled item in the calendar:
  7340. ;; If it is on or past the date.
  7341. (if (and (< diff 0) todayp)
  7342. (save-excursion
  7343. (setq category (org-get-category))
  7344. (if (re-search-backward "\\(^\\|\r\\)\\*+[ \t]*" nil t)
  7345. (progn
  7346. (goto-char (match-end 0))
  7347. (setq pos1 (match-end 1))
  7348. (setq tags (org-get-tags-at))
  7349. (setq head (buffer-substring-no-properties
  7350. (point)
  7351. (progn (skip-chars-forward "^\r\n") (point))))
  7352. (if (string-match org-looking-at-done-regexp head)
  7353. (setq txt nil)
  7354. (setq txt (org-format-agenda-item
  7355. (format "Sched.%2dx: " (- 1 diff)) head
  7356. category tags))))
  7357. (setq txt org-agenda-no-heading-message))
  7358. (when txt
  7359. (org-add-props txt props
  7360. 'org-marker (org-agenda-new-marker pos)
  7361. 'org-hd-marker (org-agenda-new-marker pos1)
  7362. 'priority (+ (- 5 diff) (org-get-priority txt))
  7363. 'category category)
  7364. (push txt ee))))))
  7365. ee))
  7366. (defun org-agenda-get-blocks ()
  7367. "Return the date-range information for agenda display."
  7368. (let* ((props (list 'face nil
  7369. 'org-not-done-regexp org-not-done-regexp
  7370. 'mouse-face 'highlight
  7371. 'keymap org-agenda-keymap
  7372. 'help-echo
  7373. (format "mouse-2 or RET jump to org file %s"
  7374. (abbreviate-file-name buffer-file-name))))
  7375. (regexp org-tr-regexp)
  7376. (d0 (calendar-absolute-from-gregorian date))
  7377. marker hdmarker ee txt d1 d2 s1 s2 timestr category tags pos)
  7378. (goto-char (point-min))
  7379. (while (re-search-forward regexp nil t)
  7380. (catch :skip
  7381. (org-agenda-skip)
  7382. (setq pos (point))
  7383. (setq timestr (match-string 0)
  7384. s1 (match-string 1)
  7385. s2 (match-string 2)
  7386. d1 (time-to-days (org-time-string-to-time s1))
  7387. d2 (time-to-days (org-time-string-to-time s2)))
  7388. (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
  7389. ;; Only allow days between the limits, because the normal
  7390. ;; date stamps will catch the limits.
  7391. (save-excursion
  7392. (setq marker (org-agenda-new-marker (point)))
  7393. (setq category (org-get-category))
  7394. (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t)
  7395. (progn
  7396. (setq hdmarker (org-agenda-new-marker (match-end 1)))
  7397. (goto-char (match-end 1))
  7398. (setq tags (org-get-tags-at))
  7399. (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
  7400. (setq txt (org-format-agenda-item
  7401. (format (if (= d1 d2) "" "(%d/%d): ")
  7402. (1+ (- d0 d1)) (1+ (- d2 d1)))
  7403. (match-string 1) category tags
  7404. (if (= d0 d1) timestr))))
  7405. (setq txt org-agenda-no-heading-message))
  7406. (org-add-props txt props
  7407. 'org-marker marker 'org-hd-marker hdmarker
  7408. 'priority (org-get-priority txt) 'category category)
  7409. (push txt ee)))
  7410. (goto-char pos)))
  7411. ; (outline-next-heading))) ;FIXME: correct to be removed??????
  7412. ;; Sort the entries by expiration date.
  7413. (nreverse ee)))
  7414. (defconst org-plain-time-of-day-regexp
  7415. (concat
  7416. "\\(\\<[012]?[0-9]"
  7417. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  7418. "\\(--?"
  7419. "\\(\\<[012]?[0-9]"
  7420. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  7421. "\\)?")
  7422. "Regular expression to match a plain time or time range.
  7423. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  7424. groups carry important information:
  7425. 0 the full match
  7426. 1 the first time, range or not
  7427. 8 the second time, if it is a range.")
  7428. (defconst org-stamp-time-of-day-regexp
  7429. (concat
  7430. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  7431. "\\([012][0-9]:[0-5][0-9]\\)>"
  7432. "\\(--?"
  7433. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  7434. "Regular expression to match a timestamp time or time range.
  7435. After a match, the following groups carry important information:
  7436. 0 the full match
  7437. 1 date plus weekday, for backreferencing to make sure both times on same day
  7438. 2 the first time, range or not
  7439. 4 the second time, if it is a range.")
  7440. (defvar org-prefix-has-time nil
  7441. "A flag, set by `org-compile-prefix-format'.
  7442. The flag is set if the currently compiled format contains a `%t'.")
  7443. (defvar org-prefix-has-tag nil
  7444. "A flag, set by `org-compile-prefix-format'.
  7445. The flag is set if the currently compiled format contains a `%T'.")
  7446. (defun org-format-agenda-item (extra txt &optional category tags dotime
  7447. noprefix)
  7448. "Format TXT to be inserted into the agenda buffer.
  7449. In particular, it adds the prefix and corresponding text properties. EXTRA
  7450. must be a string and replaces the `%s' specifier in the prefix format.
  7451. CATEGORY (string, symbol or nil) may be used to overrule the default
  7452. category taken from local variable or file name. It will replace the `%c'
  7453. specifier in the format. DOTIME, when non-nil, indicates that a
  7454. time-of-day should be extracted from TXT for sorting of this entry, and for
  7455. the `%t' specifier in the format. When DOTIME is a string, this string is
  7456. searched for a time before TXT is. NOPREFIX is a flag and indicates that
  7457. only the correctly processes TXT should be returned - this is used by
  7458. `org-agenda-change-all-lines'. TAGS can be the tags of the headline."
  7459. (save-match-data
  7460. ;; Diary entries sometimes have extra whitespace at the beginning
  7461. (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
  7462. (let* ((category (or category
  7463. org-category
  7464. (if buffer-file-name
  7465. (file-name-sans-extension
  7466. (file-name-nondirectory buffer-file-name))
  7467. "")))
  7468. (tag (if tags (nth (1- (length tags)) tags) ""))
  7469. time ;; needed for the eval of the prefix format
  7470. (ts (if dotime (concat (if (stringp dotime) dotime "") txt)))
  7471. (time-of-day (and dotime (org-get-time-of-day ts)))
  7472. stamp plain s0 s1 s2 rtn)
  7473. (when (and dotime time-of-day org-prefix-has-time)
  7474. ;; Extract starting and ending time and move them to prefix
  7475. (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
  7476. (setq plain (string-match org-plain-time-of-day-regexp ts)))
  7477. (setq s0 (match-string 0 ts)
  7478. s1 (match-string (if plain 1 2) ts)
  7479. s2 (match-string (if plain 8 4) ts))
  7480. ;; If the times are in TXT (not in DOTIMES), and the prefix will list
  7481. ;; them, we might want to remove them there to avoid duplication.
  7482. ;; The user can turn this off with a variable.
  7483. (if (and org-agenda-remove-times-when-in-prefix (or stamp plain)
  7484. (string-match (concat (regexp-quote s0) " *") txt)
  7485. (if (eq org-agenda-remove-times-when-in-prefix 'beg)
  7486. (= (match-beginning 0) 0)
  7487. t))
  7488. (setq txt (replace-match "" nil nil txt))))
  7489. ;; Normalize the time(s) to 24 hour
  7490. (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
  7491. (if s2 (setq s2 (org-get-time-of-day s2 'string t))))
  7492. (when (string-match "\\([ \t]+\\)\\(:[a-zA-Z_@0-9:]+:\\)[ \t]*$" txt)
  7493. ;; Tags are in the string
  7494. (if (or (eq org-agenda-remove-tags-when-in-prefix t)
  7495. (and org-agenda-remove-tags-when-in-prefix
  7496. org-prefix-has-tag))
  7497. (setq txt (replace-match "" t t txt))
  7498. (setq txt (replace-match
  7499. (concat (make-string (max (- 50 (length txt)) 1) ?\ )
  7500. (match-string 2 txt))
  7501. t t txt))))
  7502. ;; Create the final string
  7503. (if noprefix
  7504. (setq rtn txt)
  7505. ;; Prepare the variables needed in the eval of the compiled format
  7506. (setq time (cond (s2 (concat s1 "-" s2))
  7507. (s1 (concat s1 "......"))
  7508. (t ""))
  7509. extra (or extra "")
  7510. category (if (symbolp category) (symbol-name category) category))
  7511. ;; Evaluate the compiled format
  7512. (setq rtn (concat (eval org-prefix-format-compiled) txt)))
  7513. ;; And finally add the text properties
  7514. (org-add-props rtn nil
  7515. 'category (downcase category) 'tags tags
  7516. 'prefix-length (- (length rtn) (length txt))
  7517. 'time-of-day time-of-day
  7518. 'dotime dotime))))
  7519. (defvar org-agenda-sorting-strategy)
  7520. (defvar org-agenda-sorting-strategy-selected nil)
  7521. (defun org-agenda-add-time-grid-maybe (list ndays todayp)
  7522. (catch 'exit
  7523. (cond ((not org-agenda-use-time-grid) (throw 'exit list))
  7524. ((and todayp (member 'today (car org-agenda-time-grid))))
  7525. ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
  7526. ((member 'weekly (car org-agenda-time-grid)))
  7527. (t (throw 'exit list)))
  7528. (let* ((have (delq nil (mapcar
  7529. (lambda (x) (get-text-property 1 'time-of-day x))
  7530. list)))
  7531. (string (nth 1 org-agenda-time-grid))
  7532. (gridtimes (nth 2 org-agenda-time-grid))
  7533. (req (car org-agenda-time-grid))
  7534. (remove (member 'remove-match req))
  7535. new time)
  7536. (if (and (member 'require-timed req) (not have))
  7537. ;; don't show empty grid
  7538. (throw 'exit list))
  7539. (while (setq time (pop gridtimes))
  7540. (unless (and remove (member time have))
  7541. (setq time (int-to-string time))
  7542. (push (org-format-agenda-item
  7543. nil string "" nil
  7544. (concat (substring time 0 -2) ":" (substring time -2)))
  7545. new)
  7546. (put-text-property
  7547. 1 (length (car new)) 'face 'org-time-grid (car new))))
  7548. (if (member 'time-up org-agenda-sorting-strategy-selected)
  7549. (append new list)
  7550. (append list new)))))
  7551. (defun org-compile-prefix-format (key)
  7552. "Compile the prefix format into a Lisp form that can be evaluated.
  7553. The resulting form is returned and stored in the variable
  7554. `org-prefix-format-compiled'."
  7555. (setq org-prefix-has-time nil org-prefix-has-tag nil)
  7556. (let ((s (cond
  7557. ((stringp org-agenda-prefix-format)
  7558. org-agenda-prefix-format)
  7559. ((assq key org-agenda-prefix-format)
  7560. (cdr (assq key org-agenda-prefix-format)))
  7561. (t " %-12:c%?-12t% s")))
  7562. (start 0)
  7563. varform vars var e c f opt)
  7564. (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cts]\\)"
  7565. s start)
  7566. (setq var (cdr (assoc (match-string 4 s)
  7567. '(("c" . category) ("t" . time) ("s" . extra)
  7568. ("T" . tag))))
  7569. c (or (match-string 3 s) "")
  7570. opt (match-beginning 1)
  7571. start (1+ (match-beginning 0)))
  7572. (if (equal var 'time) (setq org-prefix-has-time t))
  7573. (if (equal var 'tag) (setq org-prefix-has-tag t))
  7574. (setq f (concat "%" (match-string 2 s) "s"))
  7575. (if opt
  7576. (setq varform
  7577. `(if (equal "" ,var)
  7578. ""
  7579. (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
  7580. (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
  7581. (setq s (replace-match "%s" t nil s))
  7582. (push varform vars))
  7583. (setq vars (nreverse vars))
  7584. (setq org-prefix-format-compiled `(format ,s ,@vars))))
  7585. (defun org-set-sorting-strategy (key)
  7586. (if (symbolp (car org-agenda-sorting-strategy))
  7587. ;; the old format
  7588. (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
  7589. (setq org-agenda-sorting-strategy-selected
  7590. (or (cdr (assq key org-agenda-sorting-strategy))
  7591. (cdr (assq 'agenda org-agenda-sorting-strategy))
  7592. '(time-up category-keep priority-down)))))
  7593. (defun org-get-time-of-day (s &optional string mod24)
  7594. "Check string S for a time of day.
  7595. If found, return it as a military time number between 0 and 2400.
  7596. If not found, return nil.
  7597. The optional STRING argument forces conversion into a 5 character wide string
  7598. HH:MM."
  7599. (save-match-data
  7600. (when
  7601. (or
  7602. (string-match
  7603. "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
  7604. (string-match
  7605. "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
  7606. (let* ((h (string-to-number (match-string 1 s)))
  7607. (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
  7608. (ampm (if (match-end 4) (downcase (match-string 4 s))))
  7609. (am-p (equal ampm "am"))
  7610. (h1 (cond ((not ampm) h)
  7611. ((= h 12) (if am-p 0 12))
  7612. (t (+ h (if am-p 0 12)))))
  7613. (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
  7614. (mod h1 24) h1))
  7615. (t0 (+ (* 100 h2) m))
  7616. (t1 (concat (if (>= h1 24) "+" " ")
  7617. (if (< t0 100) "0" "")
  7618. (if (< t0 10) "0" "")
  7619. (int-to-string t0))))
  7620. (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
  7621. (defun org-finalize-agenda-entries (list &optional nosort)
  7622. "Sort and concatenate the agenda items."
  7623. (setq list (mapcar 'org-agenda-highlight-todo list))
  7624. (if nosort
  7625. list
  7626. (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
  7627. (defun org-agenda-highlight-todo (x)
  7628. (let (re pl)
  7629. (if (eq x 'line)
  7630. (save-excursion
  7631. (beginning-of-line 1)
  7632. (setq re (get-text-property (point) 'org-not-done-regexp))
  7633. (goto-char (+ (point) (get-text-property (point) 'prefix-length)))
  7634. (and (looking-at (concat "[ \t]*" re))
  7635. (add-text-properties (match-beginning 0) (match-end 0)
  7636. '(face org-todo))))
  7637. (setq re (get-text-property 0 'org-not-done-regexp x)
  7638. pl (get-text-property 0 'prefix-length x))
  7639. (and re (equal (string-match re x pl) pl)
  7640. (add-text-properties (match-beginning 0) (match-end 0)
  7641. '(face org-todo) x))
  7642. x)))
  7643. (defsubst org-cmp-priority (a b)
  7644. "Compare the priorities of string A and B."
  7645. (let ((pa (or (get-text-property 1 'priority a) 0))
  7646. (pb (or (get-text-property 1 'priority b) 0)))
  7647. (cond ((> pa pb) +1)
  7648. ((< pa pb) -1)
  7649. (t nil))))
  7650. (defsubst org-cmp-category (a b)
  7651. "Compare the string values of categories of strings A and B."
  7652. (let ((ca (or (get-text-property 1 'category a) ""))
  7653. (cb (or (get-text-property 1 'category b) "")))
  7654. (cond ((string-lessp ca cb) -1)
  7655. ((string-lessp cb ca) +1)
  7656. (t nil))))
  7657. (defsubst org-cmp-tag (a b)
  7658. "Compare the string values of categories of strings A and B."
  7659. (let ((ta (car (last (get-text-property 1 'tags a))))
  7660. (tb (car (last (get-text-property 1 'tags b)))))
  7661. (cond ((not ta) +1)
  7662. ((not tb) -1)
  7663. ((string-lessp ta tb) -1)
  7664. ((string-lessp tb ta) +1)
  7665. (t nil))))
  7666. (defsubst org-cmp-time (a b)
  7667. "Compare the time-of-day values of strings A and B."
  7668. (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
  7669. (ta (or (get-text-property 1 'time-of-day a) def))
  7670. (tb (or (get-text-property 1 'time-of-day b) def)))
  7671. (cond ((< ta tb) -1)
  7672. ((< tb ta) +1)
  7673. (t nil))))
  7674. (defun org-entries-lessp (a b)
  7675. "Predicate for sorting agenda entries."
  7676. ;; The following variables will be used when the form is evaluated.
  7677. (let* ((time-up (org-cmp-time a b))
  7678. (time-down (if time-up (- time-up) nil))
  7679. (priority-up (org-cmp-priority a b))
  7680. (priority-down (if priority-up (- priority-up) nil))
  7681. (category-up (org-cmp-category a b))
  7682. (category-down (if category-up (- category-up) nil))
  7683. (category-keep (if category-up +1 nil))
  7684. (tag-up (org-cmp-tag a b))
  7685. (tag-down (if tag-up (- tag-up) nil)))
  7686. (cdr (assoc
  7687. (eval (cons 'or org-agenda-sorting-strategy-selected))
  7688. '((-1 . t) (1 . nil) (nil . nil))))))
  7689. (defun org-agenda-show-priority ()
  7690. "Show the priority of the current item.
  7691. This priority is composed of the main priority given with the [#A] cookies,
  7692. and by additional input from the age of a schedules or deadline entry."
  7693. (interactive)
  7694. (let* ((pri (get-text-property (point-at-bol) 'priority)))
  7695. (message "Priority is %d" (if pri pri -1000))))
  7696. (defun org-agenda-show-tags ()
  7697. "Show the tags applicable to the current item."
  7698. (interactive)
  7699. (let* ((tags (get-text-property (point-at-bol) 'tags)))
  7700. (if tags
  7701. (message "Tags are :%s:"
  7702. (org-no-properties (mapconcat 'identity tags ":")))
  7703. (message "No tags associated with this line"))))
  7704. (defun org-agenda-goto (&optional highlight)
  7705. "Go to the Org-mode file which contains the item at point."
  7706. (interactive)
  7707. (let* ((marker (or (get-text-property (point) 'org-marker)
  7708. (org-agenda-error)))
  7709. (buffer (marker-buffer marker))
  7710. (pos (marker-position marker)))
  7711. (switch-to-buffer-other-window buffer)
  7712. (widen)
  7713. (goto-char pos)
  7714. (when (org-mode-p)
  7715. (org-show-hidden-entry)
  7716. (save-excursion
  7717. (and (outline-next-heading)
  7718. (org-flag-heading nil)))) ; show the next heading
  7719. (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
  7720. (defun org-agenda-switch-to ()
  7721. "Go to the Org-mode file which contains the item at point."
  7722. (interactive)
  7723. (let* ((marker (or (get-text-property (point) 'org-marker)
  7724. (org-agenda-error)))
  7725. (buffer (marker-buffer marker))
  7726. (pos (marker-position marker)))
  7727. (switch-to-buffer buffer)
  7728. (delete-other-windows)
  7729. (widen)
  7730. (goto-char pos)
  7731. (when (org-mode-p)
  7732. (org-show-hidden-entry)
  7733. (save-excursion
  7734. (and (outline-next-heading)
  7735. (org-flag-heading nil)))))) ; show the next heading
  7736. (defun org-agenda-goto-mouse (ev)
  7737. "Go to the Org-mode file which contains the item at the mouse click."
  7738. (interactive "e")
  7739. (mouse-set-point ev)
  7740. (org-agenda-goto))
  7741. (defun org-agenda-show ()
  7742. "Display the Org-mode file which contains the item at point."
  7743. (interactive)
  7744. (let ((win (selected-window)))
  7745. (org-agenda-goto t)
  7746. (select-window win)))
  7747. (defun org-agenda-recenter (arg)
  7748. "Display the Org-mode file which contains the item at point and recenter."
  7749. (interactive "P")
  7750. (let ((win (selected-window)))
  7751. (org-agenda-goto t)
  7752. (recenter arg)
  7753. (select-window win)))
  7754. (defun org-agenda-show-mouse (ev)
  7755. "Display the Org-mode file which contains the item at the mouse click."
  7756. (interactive "e")
  7757. (mouse-set-point ev)
  7758. (org-agenda-show))
  7759. (defun org-agenda-check-no-diary ()
  7760. "Check if the entry is a diary link and abort if yes."
  7761. (if (get-text-property (point) 'org-agenda-diary-link)
  7762. (org-agenda-error)))
  7763. (defun org-agenda-error ()
  7764. (error "Command not allowed in this line"))
  7765. (defvar org-last-heading-marker (make-marker)
  7766. "Marker pointing to the headline that last changed its TODO state
  7767. by a remote command from the agenda.")
  7768. (defun org-agenda-todo (&optional arg)
  7769. "Cycle TODO state of line at point, also in Org-mode file.
  7770. This changes the line at point, all other lines in the agenda referring to
  7771. the same tree node, and the headline of the tree node in the Org-mode file."
  7772. (interactive "P")
  7773. (org-agenda-check-no-diary)
  7774. (let* ((col (current-column))
  7775. (marker (or (get-text-property (point) 'org-marker)
  7776. (org-agenda-error)))
  7777. (buffer (marker-buffer marker))
  7778. (pos (marker-position marker))
  7779. (hdmarker (get-text-property (point) 'org-hd-marker))
  7780. (buffer-read-only nil)
  7781. newhead)
  7782. (with-current-buffer buffer
  7783. (widen)
  7784. (goto-char pos)
  7785. (org-show-hidden-entry)
  7786. (save-excursion
  7787. (and (outline-next-heading)
  7788. (org-flag-heading nil))) ; show the next heading
  7789. (org-todo arg)
  7790. (and (bolp) (forward-char 1))
  7791. (setq newhead (org-get-heading))
  7792. (save-excursion
  7793. (org-back-to-heading)
  7794. (move-marker org-last-heading-marker (point))))
  7795. (beginning-of-line 1)
  7796. (save-excursion
  7797. (org-agenda-change-all-lines newhead hdmarker 'fixface))
  7798. (move-to-column col)))
  7799. (defun org-agenda-change-all-lines (newhead hdmarker &optional fixface)
  7800. "Change all lines in the agenda buffer which match HDMARKER.
  7801. The new content of the line will be NEWHEAD (as modified by
  7802. `org-format-agenda-item'). HDMARKER is checked with
  7803. `equal' against all `org-hd-marker' text properties in the file.
  7804. If FIXFACE is non-nil, the face of each item is modified acording to
  7805. the new TODO state."
  7806. (let* (props m pl undone-face done-face finish new dotime cat tags)
  7807. ; (setq newhead (org-format-agenda-item "x" newhead "x" nil 'noprefix))
  7808. (save-excursion
  7809. (goto-char (point-max))
  7810. (beginning-of-line 1)
  7811. (while (not finish)
  7812. (setq finish (bobp))
  7813. (when (and (setq m (get-text-property (point) 'org-hd-marker))
  7814. (equal m hdmarker))
  7815. (setq props (text-properties-at (point))
  7816. dotime (get-text-property (point) 'dotime)
  7817. cat (get-text-property (point) 'category)
  7818. tags (get-text-property (point) 'tags)
  7819. new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
  7820. pl (get-text-property (point) 'prefix-length)
  7821. undone-face (get-text-property (point) 'undone-face)
  7822. done-face (get-text-property (point) 'done-face))
  7823. (move-to-column pl)
  7824. (if (looking-at ".*")
  7825. (progn
  7826. (replace-match new t t)
  7827. (beginning-of-line 1)
  7828. (add-text-properties (point-at-bol) (point-at-eol) props)
  7829. (when fixface
  7830. (add-text-properties
  7831. (point-at-bol) (point-at-eol)
  7832. (list 'face
  7833. (if org-last-todo-state-is-todo
  7834. undone-face done-face)))
  7835. (org-agenda-highlight-todo 'line))
  7836. (org-agenda-align-tags 'line)
  7837. (beginning-of-line 1))
  7838. (error "Line update did not work")))
  7839. (beginning-of-line 0)))))
  7840. (defun org-agenda-align-tags (&optional line)
  7841. "Align all tags in agenda items to `org-agenda-align-tags-to-column'."
  7842. (let ((buffer-read-only))
  7843. (save-excursion
  7844. (goto-char (if line (point-at-bol) (point-min)))
  7845. (while (re-search-forward "\\([ \t]+\\):[a-zA-Z0-9_@:]+:[ \t]*$"
  7846. (if line (point-at-eol) nil) t)
  7847. (delete-region (match-beginning 1) (match-end 1))
  7848. (goto-char (match-beginning 1))
  7849. (insert (org-add-props
  7850. (make-string (max 1 (- org-agenda-align-tags-to-column
  7851. (current-column))) ?\ )
  7852. (text-properties-at (point))))))))
  7853. (defun org-agenda-priority-up ()
  7854. "Increase the priority of line at point, also in Org-mode file."
  7855. (interactive)
  7856. (org-agenda-priority 'up))
  7857. (defun org-agenda-priority-down ()
  7858. "Decrease the priority of line at point, also in Org-mode file."
  7859. (interactive)
  7860. (org-agenda-priority 'down))
  7861. (defun org-agenda-priority (&optional force-direction)
  7862. "Set the priority of line at point, also in Org-mode file.
  7863. This changes the line at point, all other lines in the agenda referring to
  7864. the same tree node, and the headline of the tree node in the Org-mode file."
  7865. (interactive)
  7866. (org-agenda-check-no-diary)
  7867. (let* ((marker (or (get-text-property (point) 'org-marker)
  7868. (org-agenda-error)))
  7869. (buffer (marker-buffer marker))
  7870. (pos (marker-position marker))
  7871. (hdmarker (get-text-property (point) 'org-hd-marker))
  7872. (buffer-read-only nil)
  7873. newhead)
  7874. (with-current-buffer buffer
  7875. (widen)
  7876. (goto-char pos)
  7877. (org-show-hidden-entry)
  7878. (save-excursion
  7879. (and (outline-next-heading)
  7880. (org-flag-heading nil))) ; show the next heading
  7881. (funcall 'org-priority force-direction)
  7882. (end-of-line 1)
  7883. (setq newhead (org-get-heading)))
  7884. (org-agenda-change-all-lines newhead hdmarker)
  7885. (beginning-of-line 1)))
  7886. (defun org-get-tags-at (&optional pos)
  7887. "Get a list of all headline tags applicable at POS.
  7888. POS defaults to point. If tags are inherited, the list contains
  7889. the targets in the same sequence as the headlines appear, i.e.
  7890. the tags of the current headline come last."
  7891. (interactive)
  7892. (let (tags)
  7893. (save-excursion
  7894. (goto-char (or pos (point)))
  7895. (save-match-data
  7896. (org-back-to-heading t)
  7897. (condition-case nil
  7898. (while t
  7899. (if (looking-at "[^\r\n]+?:\\([a-zA-Z_@0-9:]+\\):[ \t]*\\([\n\r]\\|\\'\\)")
  7900. (setq tags (append (org-split-string
  7901. (org-match-string-no-properties 1) ":")
  7902. tags)))
  7903. (or org-use-tag-inheritance (error ""))
  7904. (org-up-heading-all 1))
  7905. (error nil))))
  7906. tags))
  7907. (defun org-agenda-set-tags ()
  7908. "Set tags for the current headline."
  7909. (interactive)
  7910. (org-agenda-check-no-diary)
  7911. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  7912. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  7913. (org-agenda-error)))
  7914. (buffer (marker-buffer hdmarker))
  7915. (pos (marker-position hdmarker))
  7916. (buffer-read-only nil)
  7917. newhead)
  7918. (with-current-buffer buffer
  7919. (widen)
  7920. (goto-char pos)
  7921. (org-show-hidden-entry)
  7922. (save-excursion
  7923. (and (outline-next-heading)
  7924. (org-flag-heading nil))) ; show the next heading
  7925. (call-interactively 'org-set-tags)
  7926. (end-of-line 1)
  7927. (setq newhead (org-get-heading)))
  7928. (org-agenda-change-all-lines newhead hdmarker)
  7929. (beginning-of-line 1)))
  7930. (defun org-agenda-date-later (arg &optional what)
  7931. "Change the date of this item to one day later."
  7932. (interactive "p")
  7933. (org-agenda-check-type t 'agenda 'timeline)
  7934. (org-agenda-check-no-diary)
  7935. (let* ((marker (or (get-text-property (point) 'org-marker)
  7936. (org-agenda-error)))
  7937. (buffer (marker-buffer marker))
  7938. (pos (marker-position marker)))
  7939. (with-current-buffer buffer
  7940. (widen)
  7941. (goto-char pos)
  7942. (if (not (org-at-timestamp-p))
  7943. (error "Cannot find time stamp"))
  7944. (org-timestamp-change arg (or what 'day))
  7945. (message "Time stamp changed to %s" org-last-changed-timestamp))))
  7946. (defun org-agenda-date-earlier (arg &optional what)
  7947. "Change the date of this item to one day earlier."
  7948. (interactive "p")
  7949. (org-agenda-date-later (- arg) what))
  7950. (defun org-agenda-date-prompt (arg)
  7951. "Change the date of this item. Date is prompted for, with default today.
  7952. The prefix ARG is passed to the `org-time-stamp' command and can therefore
  7953. be used to request time specification in the time stamp."
  7954. (interactive "P")
  7955. (org-agenda-check-type t 'agenda 'timeline)
  7956. (org-agenda-check-no-diary)
  7957. (let* ((marker (or (get-text-property (point) 'org-marker)
  7958. (org-agenda-error)))
  7959. (buffer (marker-buffer marker))
  7960. (pos (marker-position marker)))
  7961. (with-current-buffer buffer
  7962. (widen)
  7963. (goto-char pos)
  7964. (if (not (org-at-timestamp-p))
  7965. (error "Cannot find time stamp"))
  7966. (org-time-stamp arg)
  7967. (message "Time stamp changed to %s" org-last-changed-timestamp))))
  7968. (defun org-agenda-schedule (arg)
  7969. "Schedule the item at point."
  7970. (interactive "P")
  7971. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  7972. (org-agenda-check-no-diary)
  7973. (let* ((marker (or (get-text-property (point) 'org-marker)
  7974. (org-agenda-error)))
  7975. (buffer (marker-buffer marker))
  7976. (pos (marker-position marker))
  7977. (org-insert-labeled-timestamps-at-point nil)
  7978. ts)
  7979. (with-current-buffer buffer
  7980. (widen)
  7981. (goto-char pos)
  7982. (setq ts (org-schedule))
  7983. (message "Item scheduled for %s" ts))))
  7984. (defun org-agenda-deadline (arg)
  7985. "Schedule the item at point."
  7986. (interactive "P")
  7987. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  7988. (org-agenda-check-no-diary)
  7989. (let* ((marker (or (get-text-property (point) 'org-marker)
  7990. (org-agenda-error)))
  7991. (buffer (marker-buffer marker))
  7992. (pos (marker-position marker))
  7993. (org-insert-labeled-timestamps-at-point nil)
  7994. ts)
  7995. (with-current-buffer buffer
  7996. (widen)
  7997. (goto-char pos)
  7998. (setq ts (org-deadline))
  7999. (message "Deadline for this item set to %s" ts))))
  8000. (defun org-get-heading ()
  8001. "Return the heading of the current entry, without the stars."
  8002. (save-excursion
  8003. (and (memq (char-before) '(?\n ?\r)) (skip-chars-forward "^\n\r"))
  8004. (if (and (re-search-backward "[\r\n]\\*" nil t)
  8005. (looking-at "[\r\n]\\*+[ \t]+\\([^\r\n]*\\)"))
  8006. (match-string 1)
  8007. "")))
  8008. (defun org-agenda-clock-in (&optional arg)
  8009. "Start the clock on the currently selected item."
  8010. (interactive "P")
  8011. (org-agenda-check-no-diary)
  8012. (let* ((marker (or (get-text-property (point) 'org-marker)
  8013. (org-agenda-error)))
  8014. (pos (marker-position marker)))
  8015. (with-current-buffer (marker-buffer marker)
  8016. (widen)
  8017. (goto-char pos)
  8018. (org-clock-in))))
  8019. (defun org-agenda-diary-entry ()
  8020. "Make a diary entry, like the `i' command from the calendar.
  8021. All the standard commands work: block, weekly etc."
  8022. (interactive)
  8023. (org-agenda-check-type t 'agenda 'timeline)
  8024. (require 'diary-lib)
  8025. (let* ((char (progn
  8026. (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
  8027. (read-char-exclusive)))
  8028. (cmd (cdr (assoc char
  8029. '((?d . insert-diary-entry)
  8030. (?w . insert-weekly-diary-entry)
  8031. (?m . insert-monthly-diary-entry)
  8032. (?y . insert-yearly-diary-entry)
  8033. (?a . insert-anniversary-diary-entry)
  8034. (?b . insert-block-diary-entry)
  8035. (?c . insert-cyclic-diary-entry)))))
  8036. (oldf (symbol-function 'calendar-cursor-to-date))
  8037. (point (point))
  8038. (mark (or (mark t) (point))))
  8039. (unless cmd
  8040. (error "No command associated with <%c>" char))
  8041. (unless (and (get-text-property point 'day)
  8042. (or (not (equal ?b char))
  8043. (get-text-property mark 'day)))
  8044. (error "Don't know which date to use for diary entry"))
  8045. ;; We implement this by hacking the `calendar-cursor-to-date' function
  8046. ;; and the `calendar-mark-ring' variable. Saves a lot of code.
  8047. (let ((calendar-mark-ring
  8048. (list (calendar-gregorian-from-absolute
  8049. (or (get-text-property mark 'day)
  8050. (get-text-property point 'day))))))
  8051. (unwind-protect
  8052. (progn
  8053. (fset 'calendar-cursor-to-date
  8054. (lambda (&optional error)
  8055. (calendar-gregorian-from-absolute
  8056. (get-text-property point 'day))))
  8057. (call-interactively cmd))
  8058. (fset 'calendar-cursor-to-date oldf)))))
  8059. (defun org-agenda-execute-calendar-command (cmd)
  8060. "Execute a calendar command from the agenda, with the date associated to
  8061. the cursor position."
  8062. (org-agenda-check-type t 'agenda 'timeline)
  8063. (require 'diary-lib)
  8064. (unless (get-text-property (point) 'day)
  8065. (error "Don't know which date to use for calendar command"))
  8066. (let* ((oldf (symbol-function 'calendar-cursor-to-date))
  8067. (point (point))
  8068. (date (calendar-gregorian-from-absolute
  8069. (get-text-property point 'day)))
  8070. (displayed-day (extract-calendar-day date))
  8071. (displayed-month (extract-calendar-month date))
  8072. (displayed-year (extract-calendar-year date)))
  8073. (unwind-protect
  8074. (progn
  8075. (fset 'calendar-cursor-to-date
  8076. (lambda (&optional error)
  8077. (calendar-gregorian-from-absolute
  8078. (get-text-property point 'day))))
  8079. (call-interactively cmd))
  8080. (fset 'calendar-cursor-to-date oldf))))
  8081. (defun org-agenda-phases-of-moon ()
  8082. "Display the phases of the moon for the 3 months around the cursor date."
  8083. (interactive)
  8084. (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
  8085. (defun org-agenda-holidays ()
  8086. "Display the holidays for the 3 months around the cursor date."
  8087. (interactive)
  8088. (org-agenda-execute-calendar-command 'list-calendar-holidays))
  8089. (defun org-agenda-sunrise-sunset (arg)
  8090. "Display sunrise and sunset for the cursor date.
  8091. Latitude and longitude can be specified with the variables
  8092. `calendar-latitude' and `calendar-longitude'. When called with prefix
  8093. argument, latitude and longitude will be prompted for."
  8094. (interactive "P")
  8095. (let ((calendar-longitude (if arg nil calendar-longitude))
  8096. (calendar-latitude (if arg nil calendar-latitude))
  8097. (calendar-location-name
  8098. (if arg "the given coordinates" calendar-location-name)))
  8099. (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
  8100. (defun org-agenda-goto-calendar ()
  8101. "Open the Emacs calendar with the date at the cursor."
  8102. (interactive)
  8103. (org-agenda-check-type t 'agenda 'timeline)
  8104. (let* ((day (or (get-text-property (point) 'day)
  8105. (error "Don't know which date to open in calendar")))
  8106. (date (calendar-gregorian-from-absolute day))
  8107. (calendar-move-hook nil)
  8108. (view-calendar-holidays-initially nil)
  8109. (view-diary-entries-initially nil))
  8110. (calendar)
  8111. (calendar-goto-date date)))
  8112. (defun org-calendar-goto-agenda ()
  8113. "Compute the Org-mode agenda for the calendar date displayed at the cursor.
  8114. This is a command that has to be installed in `calendar-mode-map'."
  8115. (interactive)
  8116. (org-agenda-list nil (calendar-absolute-from-gregorian
  8117. (calendar-cursor-to-date))
  8118. nil t))
  8119. (defun org-agenda-convert-date ()
  8120. (interactive)
  8121. (org-agenda-check-type t 'agenda 'timeline)
  8122. (let ((day (get-text-property (point) 'day))
  8123. date s)
  8124. (unless day
  8125. (error "Don't know which date to convert"))
  8126. (setq date (calendar-gregorian-from-absolute day))
  8127. (setq s (concat
  8128. "Gregorian: " (calendar-date-string date) "\n"
  8129. "ISO: " (calendar-iso-date-string date) "\n"
  8130. "Day of Yr: " (calendar-day-of-year-string date) "\n"
  8131. "Julian: " (calendar-julian-date-string date) "\n"
  8132. "Astron. JD: " (calendar-astro-date-string date)
  8133. " (Julian date number at noon UTC)\n"
  8134. "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
  8135. "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
  8136. "French: " (calendar-french-date-string date) "\n"
  8137. "Mayan: " (calendar-mayan-date-string date) "\n"
  8138. "Coptic: " (calendar-coptic-date-string date) "\n"
  8139. "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
  8140. "Persian: " (calendar-persian-date-string date) "\n"
  8141. "Chinese: " (calendar-chinese-date-string date) "\n"))
  8142. (with-output-to-temp-buffer "*Dates*"
  8143. (princ s))
  8144. (if (fboundp 'fit-window-to-buffer)
  8145. (fit-window-to-buffer (get-buffer-window "*Dates*")))))
  8146. ;;; Tags
  8147. (defun org-scan-tags (action matcher &optional todo-only)
  8148. "Scan headline tags with inheritance and produce output ACTION.
  8149. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  8150. evaluated, testing if a given set of tags qualifies a headline for
  8151. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  8152. are included in the output."
  8153. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8154. (mapconcat 'regexp-quote
  8155. (nreverse (cdr (reverse org-todo-keywords)))
  8156. "\\|")
  8157. "\\>\\)\\)? *\\(.*?\\)\\(:[A-Za-z_@0-9:]+:\\)?[ \t]*[\n\r]"))
  8158. (props (list 'face nil
  8159. 'done-face 'org-done
  8160. 'undone-face nil
  8161. 'mouse-face 'highlight
  8162. 'org-not-done-regexp org-not-done-regexp
  8163. 'keymap org-agenda-keymap
  8164. 'help-echo
  8165. (format "mouse-2 or RET jump to org file %s"
  8166. (abbreviate-file-name buffer-file-name))))
  8167. lspos
  8168. tags tags-list tags-alist (llast 0) rtn level category i txt
  8169. todo marker)
  8170. (save-excursion
  8171. (goto-char (point-min))
  8172. (when (eq action 'sparse-tree) (org-overview))
  8173. (while (re-search-forward re nil t)
  8174. (catch :skip
  8175. (and (eq action 'agenda) (org-agenda-skip))
  8176. (setq todo (if (match-end 1) (match-string 2))
  8177. tags (if (match-end 4) (match-string 4)))
  8178. (goto-char (setq lspos (1+ (match-beginning 0))))
  8179. (setq level (funcall outline-level)
  8180. category (org-get-category))
  8181. (setq i llast llast level)
  8182. ;; remove tag lists from same and sublevels
  8183. (while (>= i level)
  8184. (when (setq entry (assoc i tags-alist))
  8185. (setq tags-alist (delete entry tags-alist)))
  8186. (setq i (1- i)))
  8187. ;; add the nex tags
  8188. (when tags
  8189. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8190. tags-alist
  8191. (cons (cons level tags) tags-alist)))
  8192. ;; compile tags for current headline
  8193. (setq tags-list
  8194. (if org-use-tag-inheritance
  8195. (apply 'append (mapcar 'cdr tags-alist))
  8196. tags))
  8197. (when (and (or (not todo-only) todo)
  8198. (eval matcher)
  8199. (or (not org-agenda-skip-archived-trees)
  8200. (not (member org-archive-tag tags-list))))
  8201. ;; list this headline
  8202. (if (eq action 'sparse-tree)
  8203. (progn
  8204. (org-show-hierarchy-above))
  8205. (setq txt (org-format-agenda-item
  8206. ""
  8207. (concat
  8208. (if org-tags-match-list-sublevels
  8209. (make-string (1- level) ?.) "")
  8210. (org-get-heading))
  8211. category tags-list))
  8212. (goto-char lspos)
  8213. (setq marker (org-agenda-new-marker))
  8214. (org-add-props txt props
  8215. 'org-marker marker 'org-hd-marker marker 'category category)
  8216. (push txt rtn))
  8217. ;; if we are to skip sublevels, jump to end of subtree
  8218. (point)
  8219. (or org-tags-match-list-sublevels (org-end-of-subtree))))))
  8220. (when (and (eq action 'sparse-tree)
  8221. (not org-sparse-tree-open-archived-trees))
  8222. (org-hide-archived-subtrees (point-min) (point-max)))
  8223. (nreverse rtn)))
  8224. (defun org-tags-sparse-tree (&optional arg match)
  8225. "Create a sparse tree according to tags search string MATCH.
  8226. MATCH can contain positive and negative selection of tags, like
  8227. \"+WORK+URGENT-WITHBOSS\"."
  8228. (interactive "P")
  8229. (let ((org-show-following-heading nil)
  8230. (org-show-hierarchy-above nil))
  8231. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)))))
  8232. (defun org-make-tags-matcher (match)
  8233. "Create the TAGS matcher form for the tags-selecting string MATCH."
  8234. (unless match
  8235. ;; Get a new match request, with completion
  8236. (setq org-last-tags-completion-table
  8237. (or (org-get-buffer-tags)
  8238. org-last-tags-completion-table))
  8239. (setq match (completing-read
  8240. "Tags: " 'org-tags-completion-function nil nil nil
  8241. 'org-tags-history)))
  8242. ;; parse the string and create a lisp form
  8243. (let ((match0 match) minus tag mm matcher orterms term orlist)
  8244. (setq orterms (org-split-string match "|"))
  8245. (while (setq term (pop orterms))
  8246. (while (string-match "^&?\\([-+:]\\)?\\([A-Za-z_@0-9]+\\)" term)
  8247. (setq minus (and (match-end 1)
  8248. (equal (match-string 1 term) "-"))
  8249. tag (match-string 2 term)
  8250. term (substring term (match-end 0))
  8251. mm (list 'member (downcase tag) 'tags-list)
  8252. mm (if minus (list 'not mm) mm))
  8253. (push mm matcher))
  8254. (push (if (> (length matcher) 1) (cons 'and matcher) (car matcher))
  8255. orlist)
  8256. (setq matcher nil))
  8257. (setq matcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8258. ;; Return the string and lisp forms of the matcher
  8259. (cons match0 matcher)))
  8260. ;;;###autoload
  8261. (defun org-tags-view (&optional todo-only match keep-modes)
  8262. "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
  8263. The prefix arg TODO-ONLY limits the search to TODO entries."
  8264. (interactive "P")
  8265. (org-compile-prefix-format 'tags)
  8266. (org-set-sorting-strategy 'tags)
  8267. (let* ((org-agenda-keep-modes keep-modes)
  8268. (org-tags-match-list-sublevels
  8269. (if todo-only t org-tags-match-list-sublevels))
  8270. (win (selected-window))
  8271. (completion-ignore-case t)
  8272. rtn rtnall files file pos matcher
  8273. buffer)
  8274. (setq matcher (org-make-tags-matcher match)
  8275. match (car matcher) matcher (cdr matcher))
  8276. (org-prepare-agenda)
  8277. (setq org-agenda-redo-command
  8278. (list 'org-tags-view (list 'quote todo-only)
  8279. (list 'if 'current-prefix-arg nil match) t))
  8280. (setq files (org-agenda-files)
  8281. rtnall nil)
  8282. (while (setq file (pop files))
  8283. (catch 'nextfile
  8284. (org-check-agenda-file file)
  8285. (setq buffer (if (file-exists-p file)
  8286. (org-get-agenda-file-buffer file)
  8287. (error "No such file %s" file)))
  8288. (if (not buffer)
  8289. ;; If file does not exist, merror message to agenda
  8290. (setq rtn (list
  8291. (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  8292. rtnall (append rtnall rtn))
  8293. (with-current-buffer buffer
  8294. (unless (org-mode-p)
  8295. (error "Agenda file %s is not in `org-mode'" file))
  8296. (setq org-category-table (org-get-category-table))
  8297. (save-excursion
  8298. (save-restriction
  8299. (if org-agenda-restrict
  8300. (narrow-to-region org-agenda-restrict-begin
  8301. org-agenda-restrict-end)
  8302. (widen))
  8303. (setq rtn (org-scan-tags 'agenda matcher todo-only))
  8304. (setq rtnall (append rtnall rtn))))))))
  8305. (insert "Headlines with TAGS match: ")
  8306. (add-text-properties (point-min) (1- (point))
  8307. (list 'face 'org-level-3))
  8308. (setq pos (point))
  8309. (insert match "\n")
  8310. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  8311. (setq pos (point))
  8312. (unless org-agenda-multi
  8313. (insert "Press `C-u r' to search again with new search string\n"))
  8314. (add-text-properties pos (1- (point)) (list 'face 'org-level-3))
  8315. (when rtnall
  8316. (insert (org-finalize-agenda-entries rtnall) "\n"))
  8317. (goto-char (point-min))
  8318. (org-fit-agenda-window)
  8319. (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
  8320. (org-agenda-align-tags)
  8321. (setq buffer-read-only t)
  8322. (if (not org-select-agenda-window) (select-window win))))
  8323. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  8324. (defun org-set-tags (&optional arg just-align)
  8325. "Set the tags for the current headline.
  8326. With prefix ARG, realign all tags in headings in the current buffer."
  8327. (interactive "P")
  8328. (let* ((re (concat "^" outline-regexp))
  8329. (col (current-column))
  8330. (current (org-get-tags))
  8331. table current-tags inherited-tags ; computed below when needed
  8332. tags hd empty invis)
  8333. (if arg
  8334. (save-excursion
  8335. (goto-char (point-min))
  8336. (while (re-search-forward re nil t)
  8337. (org-set-tags nil t))
  8338. (message "All tags realigned to column %d" org-tags-column))
  8339. (if just-align
  8340. (setq tags current)
  8341. (setq table (or org-tag-alist (org-get-buffer-tags))
  8342. org-last-tags-completion-table table
  8343. current-tags (org-split-string current ":")
  8344. inherited-tags (nreverse
  8345. (nthcdr (length current-tags)
  8346. (nreverse (org-get-tags-at))))
  8347. tags
  8348. (if (or (eq t org-use-fast-tag-selection)
  8349. (and org-use-fast-tag-selection
  8350. (delq nil (mapcar 'cdr table))))
  8351. (org-fast-tag-selection current-tags inherited-tags table)
  8352. (let ((org-add-colon-after-tag-completion t))
  8353. (completing-read "Tags: " 'org-tags-completion-function
  8354. nil nil current 'org-tags-history))))
  8355. (while (string-match "[-+&]+" tags)
  8356. (setq tags (replace-match ":" t t tags))))
  8357. (unless (setq empty (string-match "\\`[\t ]*\\'" tags))
  8358. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  8359. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  8360. (if (equal current "")
  8361. (progn
  8362. (end-of-line 1)
  8363. (or empty (insert " ")))
  8364. (beginning-of-line 1)
  8365. (setq invis (org-invisible-p))
  8366. (looking-at (concat ".*?\\([ \t]*" (regexp-quote current) "\\)[ \t]*"))
  8367. (delete-region (match-beginning 1) (match-end 1))
  8368. (goto-char (match-beginning 1))
  8369. (insert (if empty "" " ")))
  8370. (if (equal tags "")
  8371. (save-excursion
  8372. (beginning-of-line 1)
  8373. (skip-chars-forward "*")
  8374. (if (= (char-after) ?\ ) (forward-char 1))
  8375. (and (re-search-forward "[ \t]+$" (point-at-eol) t)
  8376. (replace-match "")))
  8377. (move-to-column (max (current-column)
  8378. (if (> org-tags-column 0)
  8379. org-tags-column
  8380. (- (- org-tags-column) (length tags))))
  8381. t)
  8382. (insert tags)
  8383. (if (and (not invis) (org-invisible-p))
  8384. (outline-flag-region (point-at-bol) (point) nil)))
  8385. (move-to-column col))))
  8386. (defun org-tags-completion-function (string predicate &optional flag)
  8387. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  8388. (confirm (lambda (x) (stringp (car x)))))
  8389. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  8390. (setq s1 (match-string 1 string)
  8391. s2 (match-string 2 string))
  8392. (setq s1 "" s2 string))
  8393. (cond
  8394. ((eq flag nil)
  8395. ;; try completion
  8396. (setq rtn (try-completion s2 ctable confirm))
  8397. (if (stringp rtn)
  8398. (concat s1 s2 (substring rtn (length s2))
  8399. (if (and org-add-colon-after-tag-completion
  8400. (assoc rtn ctable))
  8401. ":" "")))
  8402. )
  8403. ((eq flag t)
  8404. ;; all-completions
  8405. (all-completions s2 ctable confirm)
  8406. )
  8407. ((eq flag 'lambda)
  8408. ;; exact match?
  8409. (assoc s2 ctable)))
  8410. ))
  8411. (defun org-fast-tag-insert (kwd tags face &optional end)
  8412. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  8413. (insert (format "%-12s" (concat kwd ":"))
  8414. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  8415. (or end "")))
  8416. (defun org-fast-tag-selection (current inherited table)
  8417. "Fast tag selection with single keys.
  8418. CURRENT is the current list of tags in the headline, INHERITED is the
  8419. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  8420. possibly with grouping information.
  8421. If the keys are nil, a-z are automatically assigned.
  8422. Returns the new tags string, or nil to not change the current settings."
  8423. (let* ((maxlen (apply 'max (mapcar
  8424. (lambda (x)
  8425. (if (stringp (car x)) (string-width (car x)) 0))
  8426. table)))
  8427. (fwidth (+ maxlen 3 1 3))
  8428. (ncol (/ (- (window-width) 4) fwidth))
  8429. (i-face 'org-done)
  8430. (c-face 'org-tag)
  8431. tg cnt e c char c1 c2 ntable tbl rtn
  8432. groups ingroup)
  8433. (save-window-excursion
  8434. (delete-other-windows)
  8435. (split-window-vertically)
  8436. (switch-to-buffer-other-window (get-buffer-create " *Org tags*"))
  8437. (erase-buffer)
  8438. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  8439. (org-fast-tag-insert "Current" current c-face "\n\n")
  8440. (setq tbl table char ?a cnt 0)
  8441. (while (setq e (pop tbl))
  8442. (cond
  8443. ((equal e '(:startgroup))
  8444. (push '() groups) (setq ingroup t)
  8445. (when (not (= cnt 0))
  8446. (setq cnt 0)
  8447. (insert "\n"))
  8448. (insert "{ "))
  8449. ((equal e '(:endgroup))
  8450. (setq ingroup nil cnt 0)
  8451. (insert "}\n"))
  8452. (t
  8453. (setq tg (car e) c2 nil)
  8454. (if (cdr e)
  8455. (setq c (cdr e))
  8456. ;; automatically assign a character.
  8457. (setq c1 (string-to-char
  8458. (downcase (substring
  8459. tg (if (= (string-to-char tg) ?@) 1 0)))))
  8460. (if (or (rassoc c1 ntable) (rassoc c1 table))
  8461. (while (or (rassoc char ntable) (rassoc char table))
  8462. (setq char (1+ char)))
  8463. (setq c2 c1))
  8464. (setq c (or c2 char)))
  8465. (if ingroup (push tg (car groups)))
  8466. (setq tg (org-add-props tg nil 'face
  8467. (cond
  8468. ((member tg current) c-face)
  8469. ((member tg inherited) i-face)
  8470. (t nil))))
  8471. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8472. (insert "[" c "] " tg (make-string
  8473. (- fwidth 4 (length tg)) ?\ ))
  8474. (push (cons tg c) ntable)
  8475. (when (= (setq cnt (1+ cnt)) ncol)
  8476. (insert "\n")
  8477. (if ingroup (insert " "))
  8478. (setq cnt 0)))))
  8479. (setq ntable (nreverse ntable))
  8480. (insert "\n")
  8481. (goto-char (point-min))
  8482. (if (fboundp 'fit-window-to-buffer) (fit-window-to-buffer))
  8483. (setq rtn
  8484. (catch 'exit
  8485. (while t
  8486. (message "[key]:Toggle SPC: clear current RET accept%s"
  8487. (if groups " [!] ignore goups" ""))
  8488. (setq c (read-char-exclusive))
  8489. (cond
  8490. ((= c ?\r) (throw 'exit t))
  8491. ((= c ?!)
  8492. (setq groups nil)
  8493. (goto-char (point-min))
  8494. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  8495. ((or (= c ?\C-g)
  8496. (and (= c ?q) (not (rassoc c ntable))))
  8497. (setq quit-flag t))
  8498. ((= c ?\ ) (setq current nil))
  8499. ((setq e (rassoc c ntable) tg (car e))
  8500. (if (member tg current)
  8501. (setq current (delete tg current))
  8502. (loop for g in groups do
  8503. (if (member tg g)
  8504. (mapcar (lambda (x)
  8505. (setq current (delete x current)))
  8506. g)))
  8507. (setq current (cons tg current)))))
  8508. ;; Create a sorted list
  8509. (setq current
  8510. (sort current
  8511. (lambda (a b)
  8512. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  8513. (goto-char (point-min))
  8514. (beginning-of-line 2)
  8515. (delete-region (point) (point-at-eol))
  8516. (org-fast-tag-insert "Current" current c-face)
  8517. (while (re-search-forward "\\[.\\] \\([a-zA-Z0-9_@]+\\)" nil t)
  8518. (setq tg (match-string 1))
  8519. (add-text-properties (match-beginning 1) (match-end 1)
  8520. (list 'face
  8521. (cond
  8522. ((member tg current) c-face)
  8523. ((member tg inherited) i-face)
  8524. (t nil)))))
  8525. (goto-char (point-min)))))
  8526. (if rtn
  8527. (mapconcat 'identity current ":")
  8528. nil))))
  8529. (defun org-get-tags ()
  8530. "Get the TAGS string in the current headline."
  8531. (unless (org-on-heading-p t)
  8532. (error "Not on a heading"))
  8533. (save-excursion
  8534. (beginning-of-line 1)
  8535. (if (looking-at ".*[ \t]\\(:[A-Za-z_@0-9:]+:\\)[ \t]*\\(\r\\|$\\)")
  8536. (org-match-string-no-properties 1)
  8537. "")))
  8538. (defun org-get-buffer-tags ()
  8539. "Get a table of all tags used in the buffer, for completion."
  8540. (let (tags)
  8541. (save-excursion
  8542. (goto-char (point-min))
  8543. (while (re-search-forward "[ \t]:\\([A-Za-z_@0-9:]+\\):[ \t\r\n]" nil t)
  8544. (mapc (lambda (x) (add-to-list 'tags x))
  8545. (org-split-string (org-match-string-no-properties 1) ":"))))
  8546. (mapcar 'list tags)))
  8547. ;;; Link Stuff
  8548. (defvar org-create-file-search-functions nil
  8549. "List of functions to construct the right search string for a file link.
  8550. These functions are called in turn with point at the location to
  8551. which the link should point.
  8552. A function in the hook should first test if it would like to
  8553. handle this file type, for example by checking the major-mode or
  8554. the file extension. If it decides not to handle this file, it
  8555. should just return nil to give other functions a chance. If it
  8556. does handle the file, it must return the search string to be used
  8557. when following the link. The search string will be part of the
  8558. file link, given after a double colon, and `org-open-at-point'
  8559. will automatically search for it. If special measures must be
  8560. taken to make the search successful, another function should be
  8561. added to the companion hook `org-execute-file-search-functions',
  8562. which see.
  8563. A function in this hook may also use `setq' to set the variable
  8564. `description' to provide a suggestion for the descriptive text to
  8565. be used for this link when it gets inserted into an Org-mode
  8566. buffer with \\[org-insert-link].")
  8567. (defvar org-execute-file-search-functions nil
  8568. "List of functions to execute a file search triggered by a link.
  8569. Functions added to this hook must accept a single argument, the
  8570. search string that was part of the file link, the part after the
  8571. double colon. The function must first check if it would like to
  8572. handle this search, for example by checking the major-mode or the
  8573. file extension. If it decides not to handle this search, it
  8574. should just return nil to give other functions a chance. If it
  8575. does handle the search, it must return a non-nil value to keep
  8576. other functions from trying.
  8577. Each function can access the current prefix argument through the
  8578. variable `current-prefix-argument'. Note that a single prefix is
  8579. used to force opening a link in Emacs, so it may be good to only
  8580. use a numeric or double prefix to guide the search function.
  8581. In case this is needed, a function in this hook can also restore
  8582. the window configuration before `org-open-at-point' was called using:
  8583. (set-window-configuration org-window-config-before-follow-link)")
  8584. (defun org-find-file-at-mouse (ev)
  8585. "Open file link or URL at mouse."
  8586. (interactive "e")
  8587. (mouse-set-point ev)
  8588. (org-open-at-point 'in-emacs))
  8589. (defun org-open-at-mouse (ev)
  8590. "Open file link or URL at mouse."
  8591. (interactive "e")
  8592. (mouse-set-point ev)
  8593. (org-open-at-point))
  8594. (defvar org-window-config-before-follow-link nil
  8595. "The window configuration before following a link.
  8596. This is saved in case the need arises to restore it.")
  8597. (defun org-open-at-point (&optional in-emacs)
  8598. "Open link at or after point.
  8599. If there is no link at point, this function will search forward up to
  8600. the end of the current subtree.
  8601. Normally, files will be opened by an appropriate application. If the
  8602. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  8603. (interactive "P")
  8604. (setq org-window-config-before-follow-link (current-window-configuration))
  8605. (org-remove-occur-highlights nil nil t)
  8606. (if (org-at-timestamp-p)
  8607. (org-agenda-list nil (time-to-days (org-time-string-to-time
  8608. (substring (match-string 1) 0 10)))
  8609. 1)
  8610. (let (type path link line search (pos (point)))
  8611. (catch 'match
  8612. (save-excursion
  8613. (skip-chars-forward "^]\n\r")
  8614. (when (and (re-search-backward "\\[\\[" nil t)
  8615. (looking-at org-bracket-link-regexp)
  8616. (<= (match-beginning 0) pos)
  8617. (>= (match-end 0) pos))
  8618. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  8619. (while (string-match " *\n *" link)
  8620. (setq link (replace-match " " t t link)))
  8621. (if (string-match org-link-re-with-space2 link)
  8622. (setq type (match-string 1 link)
  8623. path (match-string 2 link))
  8624. (setq type "thisfile"
  8625. path link))
  8626. (throw 'match t)))
  8627. (when (get-text-property (point) 'org-linked-text)
  8628. (setq type "thisfile"
  8629. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8630. (1+ (point)) (point))
  8631. path (buffer-substring
  8632. (previous-single-property-change pos 'org-linked-text)
  8633. (next-single-property-change pos 'org-linked-text)))
  8634. (throw 'match t))
  8635. (save-excursion
  8636. (skip-chars-backward (concat "^[]" org-non-link-chars " "))
  8637. (if (equal (char-before) ?<) (backward-char 1))
  8638. (when (or (looking-at org-angle-link-re)
  8639. (looking-at org-plain-link-re)
  8640. (and (or (re-search-forward org-angle-link-re (point-at-eol) t)
  8641. (re-search-forward org-plain-link-re (point-at-eol) t))
  8642. (<= (match-beginning 0) pos)
  8643. (>= (match-end 0) pos)))
  8644. (setq type (match-string 1)
  8645. path (match-string 2))
  8646. (throw 'match t)))
  8647. (save-excursion
  8648. (skip-chars-backward "^ \t\n\r")
  8649. (when (looking-at "\\(:[A-Za-z_@0-9:]+\\):[ \t\r\n]")
  8650. (setq type "tags"
  8651. path (match-string 1))
  8652. (while (string-match ":" path)
  8653. (setq path (replace-match "+" t t path)))
  8654. (throw 'match t)))
  8655. (save-excursion
  8656. (skip-chars-backward "a-zA-Z_")
  8657. (when (and (memq 'camel org-activate-links)
  8658. (looking-at org-camel-regexp))
  8659. (setq type "camel" path (match-string 0))
  8660. (if (equal (char-before) ?*)
  8661. (setq path (concat "*" path))))
  8662. (throw 'match t)))
  8663. (unless path
  8664. (error "No link found"))
  8665. ;; Remove any trailing spaces in path
  8666. (if (string-match " +\\'" path)
  8667. (setq path (replace-match "" t t path)))
  8668. (cond
  8669. ((equal type "mailto")
  8670. (let ((cmd (car org-link-mailto-program))
  8671. (args (cdr org-link-mailto-program)) args1
  8672. (address path) (subject "") a)
  8673. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  8674. (setq address (match-string 1 path)
  8675. subject (org-link-escape (match-string 2 path))))
  8676. (while args
  8677. (cond
  8678. ((not (stringp (car args))) (push (pop args) args1))
  8679. (t (setq a (pop args))
  8680. (if (string-match "%a" a)
  8681. (setq a (replace-match address t t a)))
  8682. (if (string-match "%s" a)
  8683. (setq a (replace-match subject t t a)))
  8684. (push a args1))))
  8685. (apply cmd (nreverse args1))))
  8686. ((member type '("http" "https" "ftp" "news"))
  8687. (browse-url (concat type ":" path)))
  8688. ((string= type "tags")
  8689. (org-tags-view in-emacs path))
  8690. ((or (string= type "camel")
  8691. (string= type "thisfile"))
  8692. (org-mark-ring-push)
  8693. (org-link-search
  8694. path
  8695. (cond ((equal in-emacs '(4)) 'occur)
  8696. ((equal in-emacs '(16)) 'org-occur)
  8697. (t nil))))
  8698. ((string= type "file")
  8699. (if (string-match "::\\([0-9]+\\)\\'" path)
  8700. (setq line (string-to-number (match-string 1 path))
  8701. path (substring path 0 (match-beginning 0)))
  8702. (if (string-match "::\\(.+\\)\\'" path)
  8703. (setq search (match-string 1 path)
  8704. path (substring path 0 (match-beginning 0)))))
  8705. (org-open-file path in-emacs line search))
  8706. ((string= type "news")
  8707. (org-follow-gnus-link path))
  8708. ((string= type "bbdb")
  8709. (org-follow-bbdb-link path))
  8710. ((string= type "info")
  8711. (org-follow-info-link path))
  8712. ((string= type "gnus")
  8713. (let (group article)
  8714. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  8715. (error "Error in Gnus link"))
  8716. (setq group (match-string 1 path)
  8717. article (match-string 3 path))
  8718. (org-follow-gnus-link group article)))
  8719. ((string= type "vm")
  8720. (let (folder article)
  8721. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  8722. (error "Error in VM link"))
  8723. (setq folder (match-string 1 path)
  8724. article (match-string 3 path))
  8725. ;; in-emacs is the prefix arg, will be interpreted as read-only
  8726. (org-follow-vm-link folder article in-emacs)))
  8727. ((string= type "wl")
  8728. (let (folder article)
  8729. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  8730. (error "Error in Wanderlust link"))
  8731. (setq folder (match-string 1 path)
  8732. article (match-string 3 path))
  8733. (org-follow-wl-link folder article)))
  8734. ((string= type "mhe")
  8735. (let (folder article)
  8736. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  8737. (error "Error in MHE link"))
  8738. (setq folder (match-string 1 path)
  8739. article (match-string 3 path))
  8740. (org-follow-mhe-link folder article)))
  8741. ((string= type "rmail")
  8742. (let (folder article)
  8743. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  8744. (error "Error in RMAIL link"))
  8745. (setq folder (match-string 1 path)
  8746. article (match-string 3 path))
  8747. (org-follow-rmail-link folder article)))
  8748. ((string= type "shell")
  8749. (let ((cmd path))
  8750. (while (string-match "@{" cmd)
  8751. (setq cmd (replace-match "<" t t cmd)))
  8752. (while (string-match "@}" cmd)
  8753. (setq cmd (replace-match ">" t t cmd)))
  8754. (if (or (not org-confirm-shell-link-function)
  8755. (funcall org-confirm-shell-link-function
  8756. (format "Execute \"%s\" in shell? "
  8757. (org-add-props cmd nil
  8758. 'face 'org-warning))))
  8759. (progn
  8760. (message "Executing %s" cmd)
  8761. (shell-command cmd))
  8762. (error "Abort"))))
  8763. ((string= type "elisp")
  8764. (let ((cmd path))
  8765. (if (or (not org-confirm-elisp-link-function)
  8766. (funcall org-confirm-elisp-link-function
  8767. (format "Execute \"%s\" as elisp? "
  8768. (org-add-props cmd nil
  8769. 'face 'org-warning))))
  8770. (message "%s => %s" cmd (eval (read cmd)))
  8771. (error "Abort"))))
  8772. (t
  8773. (browse-url-at-point))))))
  8774. (defun org-link-search (s &optional type)
  8775. "Search for a link search option.
  8776. When S is a CamelCaseWord, search for a target, or for a sentence containing
  8777. the words. If S is surrounded by forward slashes, it is interpreted as a
  8778. regular expression. In org-mode files, this will create an `org-occur'
  8779. sparse tree. In ordinary files, `occur' will be used to list matches.
  8780. If the current buffer is in `dired-mode', grep will be used to search
  8781. in all files."
  8782. (let ((case-fold-search t)
  8783. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  8784. (pos (point))
  8785. (pre "") (post "")
  8786. words re0 re1 re2 re3 re4 re5 re2a reall camel)
  8787. (cond
  8788. ;; First check if there are any special
  8789. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  8790. ;; Now try the builtin stuff
  8791. ((save-excursion
  8792. (goto-char (point-min))
  8793. (and
  8794. (re-search-forward
  8795. (concat "<<" (regexp-quote s0) ">>") nil t)
  8796. (setq pos (match-beginning 0))))
  8797. ;; There is an exact target for this
  8798. (goto-char pos))
  8799. ((string-match "^/\\(.*\\)/$" s)
  8800. ;; A regular expression
  8801. (cond
  8802. ((org-mode-p)
  8803. (org-occur (match-string 1 s)))
  8804. ;;((eq major-mode 'dired-mode)
  8805. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  8806. (t (org-do-occur (match-string 1 s)))))
  8807. ((or (setq camel (string-match (concat "^" org-camel-regexp "$") s))
  8808. t)
  8809. ;; A camel or a normal search string
  8810. (when (equal (string-to-char s) ?*)
  8811. ;; Anchor on headlines, post may include tags.
  8812. (setq pre "^\\*+[ \t]*\\(?:\\sw+\\)?[ \t]*"
  8813. post "[ \t]*\\(?:[ \t]+:[a-zA-Z_@0-9:+]:[ \t]*\\)?$"
  8814. s (substring s 1)))
  8815. (remove-text-properties
  8816. 0 (length s)
  8817. '(face nil mouse-face nil keymap nil fontified nil) s)
  8818. ;; Make a series of regular expressions to find a match
  8819. (setq words
  8820. (if camel
  8821. (org-camel-to-words s)
  8822. (org-split-string s "[ \n\r\t]+"))
  8823. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  8824. re2 (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t]+") "\\)[ \t\r\n]")
  8825. re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  8826. re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  8827. re1 (concat pre re2 post)
  8828. re3 (concat pre re4 post)
  8829. re5 (concat pre ".*" re4)
  8830. re2 (concat pre re2)
  8831. re2a (concat pre re2a)
  8832. re4 (concat pre re4)
  8833. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  8834. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  8835. re5 "\\)"
  8836. ))
  8837. (cond
  8838. ((eq type 'org-occur) (org-occur reall))
  8839. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  8840. (t (goto-char (point-min))
  8841. (if (or (org-search-not-link re0 nil t)
  8842. (org-search-not-link re1 nil t)
  8843. (org-search-not-link re2 nil t)
  8844. (org-search-not-link re2a nil t)
  8845. (org-search-not-link re3 nil t)
  8846. (org-search-not-link re4 nil t)
  8847. (org-search-not-link re5 nil t)
  8848. )
  8849. (goto-char (match-beginning 1))
  8850. (goto-char pos)
  8851. (error "No match")))))
  8852. (t
  8853. ;; Normal string-search
  8854. (goto-char (point-min))
  8855. (if (search-forward s nil t)
  8856. (goto-char (match-beginning 0))
  8857. (error "No match"))))
  8858. (and (org-mode-p) (org-show-hierarchy-above))))
  8859. (defun org-search-not-link (&rest args)
  8860. "Execute `re-search-forward', but only accept matches that are not a link."
  8861. (catch 'exit
  8862. (let (p1)
  8863. (while (apply 're-search-forward args)
  8864. (setq p1 (point))
  8865. (if (not (save-match-data
  8866. (and (re-search-backward "\\[\\[" nil t)
  8867. (looking-at org-bracket-link-regexp)
  8868. (<= (match-beginning 0) p1)
  8869. (>= (match-end 0) p1))))
  8870. (progn (goto-char (match-end 0))
  8871. (throw 'exit (point)))
  8872. (goto-char (match-end 0)))))))
  8873. (defun org-do-occur (regexp &optional cleanup)
  8874. "Call the Emacs command `occur'.
  8875. If CLEANUP is non-nil, remove the printout of the regular expression
  8876. in the *Occur* buffer. This is useful if the regex is long and not useful
  8877. to read."
  8878. (occur regexp)
  8879. (when cleanup
  8880. (let ((cwin (selected-window)) win beg end)
  8881. (when (setq win (get-buffer-window "*Occur*"))
  8882. (select-window win))
  8883. (goto-char (point-min))
  8884. (when (re-search-forward "match[a-z]+" nil t)
  8885. (setq beg (match-end 0))
  8886. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  8887. (setq end (1- (match-beginning 0)))))
  8888. (and beg end (let ((buffer-read-only)) (delete-region beg end)))
  8889. (goto-char (point-min))
  8890. (select-window cwin))))
  8891. (defvar org-mark-ring nil
  8892. "Mark ring for positions before jumps in Org-mode.")
  8893. (defvar org-mark-ring-last-goto nil
  8894. "Last position in the mark ring used to go back.")
  8895. ;; Fill and close the ring
  8896. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  8897. (loop for i from 1 to org-mark-ring-length do
  8898. (push (make-marker) org-mark-ring))
  8899. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  8900. org-mark-ring)
  8901. (defun org-mark-ring-push (&optional pos buffer)
  8902. "Put the current position or POS into the mark ring and rotate it."
  8903. (interactive)
  8904. (setq pos (or pos (point)))
  8905. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  8906. (move-marker (car org-mark-ring)
  8907. (or pos (point))
  8908. (or buffer (current-buffer)))
  8909. (message
  8910. (substitute-command-keys
  8911. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  8912. (defun org-mark-ring-goto (&optional n)
  8913. "Jump to the previous position in the mark ring.
  8914. With prefix arg N, jump back that many stored positions. When
  8915. called several times in succession, walk through the entire ring.
  8916. Org-mode commands jumping to a different position in the current file,
  8917. or to another Org-mode file, automatically push the old position
  8918. onto the ring."
  8919. (interactive "p")
  8920. (let (p m)
  8921. (if (eq last-command this-command)
  8922. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  8923. (setq p org-mark-ring))
  8924. (setq org-mark-ring-last-goto p)
  8925. (setq m (car p))
  8926. (switch-to-buffer (marker-buffer m))
  8927. (goto-char m)
  8928. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-hierarchy-above))))
  8929. (defun org-camel-to-words (s)
  8930. "Split \"CamelCaseWords\" to (\"Camel\" \"Case\" \"Words\")."
  8931. (let ((case-fold-search nil)
  8932. words)
  8933. (while (string-match "[a-z][A-Z]" s)
  8934. (push (substring s 0 (1+ (match-beginning 0))) words)
  8935. (setq s (substring s (1+ (match-beginning 0)))))
  8936. (nreverse (cons s words))))
  8937. (defun org-remove-angle-brackets (s)
  8938. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  8939. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  8940. s)
  8941. (defun org-add-angle-brackets (s)
  8942. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  8943. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  8944. s)
  8945. (defun org-follow-bbdb-link (name)
  8946. "Follow a BBDB link to NAME."
  8947. (require 'bbdb)
  8948. (let ((inhibit-redisplay t)
  8949. (bbdb-electric-p nil))
  8950. (catch 'exit
  8951. ;; Exact match on name
  8952. (bbdb-name (concat "\\`" name "\\'") nil)
  8953. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  8954. ;; Exact match on name
  8955. (bbdb-company (concat "\\`" name "\\'") nil)
  8956. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  8957. ;; Partial match on name
  8958. (bbdb-name name nil)
  8959. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  8960. ;; Partial match on company
  8961. (bbdb-company name nil)
  8962. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  8963. ;; General match including network address and notes
  8964. (bbdb name nil)
  8965. (when (= 0 (buffer-size (get-buffer "*BBDB*")))
  8966. (delete-window (get-buffer-window "*BBDB*"))
  8967. (error "No matching BBDB record")))))
  8968. (defun org-follow-info-link (name)
  8969. "Follow an info file & node link to NAME."
  8970. (if (or (string-match "\\(.*\\)::?\\(.*\\)" name)
  8971. (string-match "\\(.*\\)" name))
  8972. (progn
  8973. (require 'info)
  8974. (if (match-string 2 name) ; If there isn't a node, choose "Top"
  8975. (Info-find-node (match-string 1 name) (match-string 2 name))
  8976. (Info-find-node (match-string 1 name) "Top")))
  8977. (message (concat "Could not open: " name))))
  8978. (defun org-follow-gnus-link (&optional group article)
  8979. "Follow a Gnus link to GROUP and ARTICLE."
  8980. (require 'gnus)
  8981. (funcall (cdr (assq 'gnus org-link-frame-setup)))
  8982. (if gnus-other-frame-object (select-frame gnus-other-frame-object))
  8983. (if group (gnus-fetch-group group))
  8984. (if article
  8985. (or (gnus-summary-goto-article article nil 'force)
  8986. (if (fboundp 'gnus-summary-insert-cached-articles)
  8987. (progn
  8988. (gnus-summary-insert-cached-articles)
  8989. (gnus-summary-goto-article article nil 'force))
  8990. (message "Message could not be found.")))))
  8991. (defun org-follow-vm-link (&optional folder article readonly)
  8992. "Follow a VM link to FOLDER and ARTICLE."
  8993. (require 'vm)
  8994. (setq article (org-add-angle-brackets article))
  8995. (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
  8996. ;; ange-ftp or efs or tramp access
  8997. (let ((user (or (match-string 1 folder) (user-login-name)))
  8998. (host (match-string 2 folder))
  8999. (file (match-string 3 folder)))
  9000. (cond
  9001. ((featurep 'tramp)
  9002. ;; use tramp to access the file
  9003. (if (featurep 'xemacs)
  9004. (setq folder (format "[%s@%s]%s" user host file))
  9005. (setq folder (format "/%s@%s:%s" user host file))))
  9006. (t
  9007. ;; use ange-ftp or efs
  9008. (require (if (featurep 'xemacs) 'efs 'ange-ftp))
  9009. (setq folder (format "/%s@%s:%s" user host file))))))
  9010. (when folder
  9011. (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
  9012. (sit-for 0.1)
  9013. (when article
  9014. (vm-select-folder-buffer)
  9015. (widen)
  9016. (let ((case-fold-search t))
  9017. (goto-char (point-min))
  9018. (if (not (re-search-forward
  9019. (concat "^" "message-id: *" (regexp-quote article))))
  9020. (error "Could not find the specified message in this folder"))
  9021. (vm-isearch-update)
  9022. (vm-isearch-narrow)
  9023. (vm-beginning-of-message)
  9024. (vm-summarize)))))
  9025. (defun org-follow-wl-link (folder article)
  9026. "Follow a Wanderlust link to FOLDER and ARTICLE."
  9027. (setq article (org-add-angle-brackets article))
  9028. (wl-summary-goto-folder-subr folder 'no-sync t nil t)
  9029. (if article (wl-summary-jump-to-msg-by-message-id article ">"))
  9030. (wl-summary-redisplay))
  9031. (defun org-follow-rmail-link (folder article)
  9032. "Follow an RMAIL link to FOLDER and ARTICLE."
  9033. (setq article (org-add-angle-brackets article))
  9034. (let (message-number)
  9035. (save-excursion
  9036. (save-window-excursion
  9037. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  9038. (setq message-number
  9039. (save-restriction
  9040. (widen)
  9041. (goto-char (point-max))
  9042. (if (re-search-backward
  9043. (concat "^Message-ID:\\s-+" (regexp-quote
  9044. (or article "")))
  9045. nil t)
  9046. (rmail-what-message))))))
  9047. (if message-number
  9048. (progn
  9049. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  9050. (rmail-show-message message-number)
  9051. message-number)
  9052. (error "Message not found"))))
  9053. ;; mh-e integration based on planner-mode
  9054. (defun org-mhe-get-message-real-folder ()
  9055. "Return the name of the current message real folder, so if you use
  9056. sequences, it will now work."
  9057. (save-excursion
  9058. (let* ((folder
  9059. (if (equal major-mode 'mh-folder-mode)
  9060. mh-current-folder
  9061. ;; Refer to the show buffer
  9062. mh-show-folder-buffer))
  9063. (end-index
  9064. (if (boundp 'mh-index-folder)
  9065. (min (length mh-index-folder) (length folder))))
  9066. )
  9067. ;; a simple test on mh-index-data does not work, because
  9068. ;; mh-index-data is always nil in a show buffer.
  9069. (if (and (boundp 'mh-index-folder)
  9070. (string= mh-index-folder (substring folder 0 end-index)))
  9071. (if (equal major-mode 'mh-show-mode)
  9072. (save-window-excursion
  9073. (when (buffer-live-p (get-buffer folder))
  9074. (progn
  9075. (pop-to-buffer folder)
  9076. (org-mhe-get-message-folder-from-index)
  9077. )
  9078. ))
  9079. (org-mhe-get-message-folder-from-index)
  9080. )
  9081. folder
  9082. )
  9083. )))
  9084. (defun org-mhe-get-message-folder-from-index ()
  9085. "Returns the name of the message folder in a index folder buffer."
  9086. (save-excursion
  9087. (mh-index-previous-folder)
  9088. (re-search-forward "^\\(+.*\\)$" nil t)
  9089. (message (match-string 1))))
  9090. (defun org-mhe-get-message-folder ()
  9091. "Return the name of the current message folder. Be careful if you
  9092. use sequences."
  9093. (save-excursion
  9094. (if (equal major-mode 'mh-folder-mode)
  9095. mh-current-folder
  9096. ;; Refer to the show buffer
  9097. mh-show-folder-buffer)))
  9098. (defun org-mhe-get-message-num ()
  9099. "Return the number of the current message. Be careful if you
  9100. use sequences."
  9101. (save-excursion
  9102. (if (equal major-mode 'mh-folder-mode)
  9103. (mh-get-msg-num nil)
  9104. ;; Refer to the show buffer
  9105. (mh-show-buffer-message-number))))
  9106. (defun org-mhe-get-header (header)
  9107. "Return a header of the message in folder mode. This will create a
  9108. show buffer for the corresponding message. If you have a more clever
  9109. idea..."
  9110. (let* ((folder (org-mhe-get-message-folder))
  9111. (num (org-mhe-get-message-num))
  9112. (buffer (get-buffer-create (concat "show-" folder)))
  9113. (header-field))
  9114. (with-current-buffer buffer
  9115. (mh-display-msg num folder)
  9116. (if (equal major-mode 'mh-folder-mode)
  9117. (mh-header-display)
  9118. (mh-show-header-display))
  9119. (set-buffer buffer)
  9120. (setq header-field (mh-get-header-field header))
  9121. (if (equal major-mode 'mh-folder-mode)
  9122. (mh-show)
  9123. (mh-show-show))
  9124. header-field)))
  9125. (defun org-follow-mhe-link (folder article)
  9126. "Follow an MHE link to FOLDER and ARTICLE.
  9127. If ARTICLE is nil FOLDER is shown. If the configuration variable
  9128. `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
  9129. ARTICLE is searched in all folders. Indexed searches (swish++,
  9130. namazu, and others supported by MH-E) will always search in all
  9131. folders."
  9132. (require 'mh-e)
  9133. (require 'mh-search)
  9134. (require 'mh-utils)
  9135. (mh-find-path)
  9136. (if (not article)
  9137. (mh-visit-folder (mh-normalize-folder-name folder))
  9138. (setq article (org-add-angle-brackets article))
  9139. (mh-search-choose)
  9140. (if (equal mh-searcher 'pick)
  9141. (progn
  9142. (mh-search folder (list "--message-id" article))
  9143. (when (and org-mhe-search-all-folders
  9144. (not (org-mhe-get-message-real-folder)))
  9145. (kill-this-buffer)
  9146. (mh-search "+" (list "--message-id" article))))
  9147. (mh-search "+" article))
  9148. (if (org-mhe-get-message-real-folder)
  9149. (mh-show-msg 1)
  9150. (kill-this-buffer)
  9151. (error "Message not found"))))
  9152. ;; BibTeX links
  9153. ;; Use the custom search meachnism to construct and use search strings for
  9154. ;; file links to BibTeX database entries.
  9155. (defun org-create-file-search-in-bibtex ()
  9156. "Create the search string and description for a BibTeX database entry."
  9157. (when (eq major-mode 'bibtex-mode)
  9158. ;; yes, we want to construct this search string.
  9159. ;; Make a good description for this entry, using names, year and the title
  9160. ;; Put it into the `description' variable which is dynamically scoped.
  9161. (let ((bibtex-autokey-names 1)
  9162. (bibtex-autokey-names-stretch 1)
  9163. (bibtex-autokey-name-case-convert-function 'identity)
  9164. (bibtex-autokey-name-separator " & ")
  9165. (bibtex-autokey-additional-names " et al.")
  9166. (bibtex-autokey-year-length 4)
  9167. (bibtex-autokey-name-year-separator " ")
  9168. (bibtex-autokey-titlewords 3)
  9169. (bibtex-autokey-titleword-separator " ")
  9170. (bibtex-autokey-titleword-case-convert-function 'identity)
  9171. (bibtex-autokey-titleword-length 'infty)
  9172. (bibtex-autokey-year-title-separator ": "))
  9173. (setq description (bibtex-generate-autokey)))
  9174. ;; Now parse the entry, get the key and return it.
  9175. (save-excursion
  9176. (bibtex-beginning-of-entry)
  9177. (cdr (assoc "=key=" (bibtex-parse-entry))))))
  9178. (defun org-execute-file-search-in-bibtex (s)
  9179. "Find the link search string S as a key for a database entry."
  9180. (when (eq major-mode 'bibtex-mode)
  9181. ;; Yes, we want to do the search in this file.
  9182. ;; We construct a regexp that searches for "@entrytype{" followed by the key
  9183. (goto-char (point-min))
  9184. (and (re-search-forward (concat "@[a-zA-Z]+[ \t\n]*{[ \t\n]*"
  9185. (regexp-quote s) "[ \t\n]*,") nil t)
  9186. (goto-char (match-beginning 0)))
  9187. (if (and (match-beginning 0) (equal current-prefix-arg '(16)))
  9188. ;; Use double prefix to indicate that any web link should be browsed
  9189. (let ((b (current-buffer)) (p (point)))
  9190. ;; Restore the window configuration because we just use the web link
  9191. (set-window-configuration org-window-config-before-follow-link)
  9192. (save-excursion (set-buffer b) (goto-char p)
  9193. (bibtex-url)))
  9194. (recenter 0)) ; Move entry start to beginning of window
  9195. ;; return t to indicate that the search is done.
  9196. t))
  9197. ;; Finally add the functions to the right hooks.
  9198. (add-hook 'org-create-file-search-functions 'org-create-file-search-in-bibtex)
  9199. (add-hook 'org-execute-file-search-functions 'org-execute-file-search-in-bibtex)
  9200. ;; end of Bibtex link setup
  9201. (defun org-upgrade-old-links (&optional query-description)
  9202. "Transfer old <...> style links to new [[...]] style links.
  9203. With arg query-description, ask at each match for a description text to use
  9204. for this link."
  9205. (interactive (list (y-or-n-p "Would you like to be queried for a description at each link?")))
  9206. (save-excursion
  9207. (goto-char (point-min))
  9208. (let ((re (concat "\\([^[]\\)<\\("
  9209. "\\(" (mapconcat 'identity org-link-types "\\|")
  9210. "\\):"
  9211. "[^" org-non-link-chars "]+\\)>"))
  9212. l1 l2 (cnt 0))
  9213. (while (re-search-forward re nil t)
  9214. (setq cnt (1+ cnt)
  9215. l1 (org-match-string-no-properties 2)
  9216. l2 (save-match-data (org-link-escape l1)))
  9217. (when query-description (setq l1 (read-string "Desc: " l1)))
  9218. (if (equal l1 l2)
  9219. (replace-match (concat (match-string 1) "[[" l1 "]]") t t)
  9220. (replace-match (concat (match-string 1) "[[" l2 "][" l1 "]]") t t)))
  9221. (message "%d matches have beed treated" cnt))))
  9222. (defun org-open-file (path &optional in-emacs line search)
  9223. "Open the file at PATH.
  9224. First, this expands any special file name abbreviations. Then the
  9225. configuration variable `org-file-apps' is checked if it contains an
  9226. entry for this file type, and if yes, the corresponding command is launched.
  9227. If no application is found, Emacs simply visits the file.
  9228. With optional argument IN-EMACS, Emacs will visit the file.
  9229. Optional LINE specifies a line to go to, optional SEARCH a string to
  9230. search for. If LINE or SEARCH is given, the file will always be
  9231. opened in Emacs.
  9232. If the file does not exist, an error is thrown."
  9233. (setq in-emacs (or in-emacs line search))
  9234. (let* ((file (if (equal path "")
  9235. buffer-file-name
  9236. (substitute-in-file-name (expand-file-name path))))
  9237. (apps (append org-file-apps (org-default-apps)))
  9238. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9239. (dirp (if remp nil (file-directory-p file)))
  9240. (dfile (downcase file))
  9241. (old-buffer (current-buffer))
  9242. (old-pos (point))
  9243. (old-mode major-mode)
  9244. ext cmd)
  9245. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9246. (setq ext (match-string 1 dfile))
  9247. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9248. (setq ext (match-string 1 dfile))))
  9249. (if in-emacs
  9250. (setq cmd 'emacs)
  9251. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9252. (and dirp (cdr (assoc 'directory apps)))
  9253. (cdr (assoc ext apps))
  9254. (cdr (assoc t apps)))))
  9255. (when (eq cmd 'mailcap)
  9256. (require 'mailcap)
  9257. (mailcap-parse-mailcaps)
  9258. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9259. (command (mailcap-mime-info mime-type)))
  9260. (if (stringp command)
  9261. (setq cmd command)
  9262. (setq cmd 'emacs))))
  9263. (if (and (not (eq cmd 'emacs)) ; Emacs has not problems with non-ex files
  9264. (not (file-exists-p file))
  9265. (not org-open-non-existing-files))
  9266. (error "No such file: %s" file))
  9267. (cond
  9268. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9269. ;; Normalize use of quote, this can vary.
  9270. (if (string-match "['\"]%s['\"]" cmd)
  9271. (setq cmd (replace-match "'%s'" t t cmd)))
  9272. (setq cmd (format cmd file))
  9273. (save-window-excursion
  9274. (shell-command (concat cmd " &"))))
  9275. ((or (stringp cmd)
  9276. (eq cmd 'emacs))
  9277. ; (unless (equal (file-truename file) (file-truename (or buffer-file-name "")))
  9278. ; (funcall (cdr (assq 'file org-link-frame-setup)) file))
  9279. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9280. (if line (goto-line line)
  9281. (if search (org-link-search search))))
  9282. ((consp cmd)
  9283. (eval cmd))
  9284. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9285. (and (org-mode-p) (eq old-mode 'org-mode)
  9286. (or (not (equal old-buffer (current-buffer)))
  9287. (not (equal old-pos (point))))
  9288. (org-mark-ring-push old-pos old-buffer))))
  9289. (defun org-default-apps ()
  9290. "Return the default applications for this operating system."
  9291. (cond
  9292. ((eq system-type 'darwin)
  9293. org-file-apps-defaults-macosx)
  9294. ((eq system-type 'windows-nt)
  9295. org-file-apps-defaults-windowsnt)
  9296. (t org-file-apps-defaults-gnu)))
  9297. (defun org-expand-file-name (path)
  9298. "Replace special path abbreviations and expand the file name."
  9299. (expand-file-name path))
  9300. (defun org-file-remote-p (file)
  9301. "Test whether FILE specifies a location on a remote system.
  9302. Return non-nil if the location is indeed remote.
  9303. For example, the filename \"/user@host:/foo\" specifies a location
  9304. on the system \"/user@host:\"."
  9305. (cond ((fboundp 'file-remote-p)
  9306. (file-remote-p file))
  9307. ((fboundp 'tramp-handle-file-remote-p)
  9308. (tramp-handle-file-remote-p file))
  9309. ((and (boundp 'ange-ftp-name-format)
  9310. (string-match ange-ftp-name-format file))
  9311. t)
  9312. (t nil)))
  9313. (defvar org-insert-link-history nil
  9314. "Minibuffer history for links inserted with `org-insert-link'.")
  9315. (defvar org-stored-links nil
  9316. "Contains the links stored with `org-store-link'.")
  9317. ;;;###autoload
  9318. (defun org-store-link (arg)
  9319. "\\<org-mode-map>Store an org-link to the current location.
  9320. This link can later be inserted into an org-buffer with
  9321. \\[org-insert-link].
  9322. For some link types, a prefix arg is interpreted:
  9323. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  9324. For file links, arg negates `org-context-in-file-links'."
  9325. (interactive "P")
  9326. (let (link cpltxt desc description search txt (pos (point)))
  9327. (cond
  9328. ((eq major-mode 'bbdb-mode)
  9329. (setq cpltxt (concat
  9330. "bbdb:"
  9331. (or (bbdb-record-name (bbdb-current-record))
  9332. (bbdb-record-company (bbdb-current-record))))
  9333. link (org-make-link cpltxt)))
  9334. ((eq major-mode 'Info-mode)
  9335. (setq link (org-make-link "info:"
  9336. (file-name-nondirectory Info-current-file)
  9337. ":" Info-current-node))
  9338. (setq cpltxt (concat (file-name-nondirectory Info-current-file)
  9339. ":" Info-current-node)))
  9340. ((eq major-mode 'calendar-mode)
  9341. (let ((cd (calendar-cursor-to-date)))
  9342. (setq link
  9343. (format-time-string
  9344. (car org-time-stamp-formats)
  9345. (apply 'encode-time
  9346. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  9347. nil nil nil))))))
  9348. ((or (eq major-mode 'vm-summary-mode)
  9349. (eq major-mode 'vm-presentation-mode))
  9350. (and (eq major-mode 'vm-presentation-mode) (vm-summarize))
  9351. (vm-follow-summary-cursor)
  9352. (save-excursion
  9353. (vm-select-folder-buffer)
  9354. (let* ((message (car vm-message-pointer))
  9355. (folder buffer-file-name)
  9356. (subject (vm-su-subject message))
  9357. (author (vm-su-full-name message))
  9358. (message-id (vm-su-message-id message)))
  9359. (setq message-id (org-remove-angle-brackets message-id))
  9360. (setq folder (abbreviate-file-name folder))
  9361. (if (string-match (concat "^" (regexp-quote vm-folder-directory))
  9362. folder)
  9363. (setq folder (replace-match "" t t folder)))
  9364. (setq cpltxt (concat author " on: " subject))
  9365. (setq link (org-make-link "vm:" folder "#" message-id)))))
  9366. ((eq major-mode 'wl-summary-mode)
  9367. (let* ((msgnum (wl-summary-message-number))
  9368. (message-id (elmo-message-field wl-summary-buffer-elmo-folder
  9369. msgnum 'message-id))
  9370. (wl-message-entity (elmo-msgdb-overview-get-entity
  9371. msgnum (wl-summary-buffer-msgdb)))
  9372. (author (wl-summary-line-from)) ; FIXME: correct?
  9373. (subject "???")) ; FIXME:
  9374. (setq message-id (org-remove-angle-brackets message-id))
  9375. (setq cpltxt (concat author " on: " subject))
  9376. (setq link (org-make-link "wl:" wl-summary-buffer-folder-name
  9377. "#" message-id))))
  9378. ((or (equal major-mode 'mh-folder-mode)
  9379. (equal major-mode 'mh-show-mode))
  9380. (let ((from-header (org-mhe-get-header "From:"))
  9381. (to-header (org-mhe-get-header "To:"))
  9382. (subject (org-mhe-get-header "Subject:")))
  9383. (setq cpltxt (concat from-header " on: " subject))
  9384. (setq link (org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
  9385. (org-remove-angle-brackets
  9386. (org-mhe-get-header "Message-Id:"))))))
  9387. ((eq major-mode 'rmail-mode)
  9388. (save-excursion
  9389. (save-restriction
  9390. (rmail-narrow-to-non-pruned-header)
  9391. (let ((folder buffer-file-name)
  9392. (message-id (mail-fetch-field "message-id"))
  9393. (author (mail-fetch-field "from"))
  9394. (subject (mail-fetch-field "subject")))
  9395. (setq message-id (org-remove-angle-brackets message-id))
  9396. (setq cpltxt (concat author " on: " subject))
  9397. (setq link (org-make-link "rmail:" folder "#" message-id))))))
  9398. ((eq major-mode 'gnus-group-mode)
  9399. (let ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
  9400. (gnus-group-group-name)) ; version
  9401. ((fboundp 'gnus-group-name)
  9402. (gnus-group-name))
  9403. (t "???"))))
  9404. (setq cpltxt (concat
  9405. (if (org-xor arg org-usenet-links-prefer-google)
  9406. "http://groups.google.com/groups?group="
  9407. "gnus:")
  9408. group)
  9409. link (org-make-link cpltxt))))
  9410. ((memq major-mode '(gnus-summary-mode gnus-article-mode))
  9411. (and (eq major-mode 'gnus-article-mode) (gnus-article-show-summary))
  9412. (gnus-summary-beginning-of-article)
  9413. (let* ((group (car gnus-article-current))
  9414. (article (cdr gnus-article-current))
  9415. (header (gnus-summary-article-header article))
  9416. (author (mail-header-from header))
  9417. (message-id (mail-header-id header))
  9418. (date (mail-header-date header))
  9419. (subject (gnus-summary-subject-string)))
  9420. (setq cpltxt (concat author " on: " subject))
  9421. (if (org-xor arg org-usenet-links-prefer-google)
  9422. (setq link
  9423. (concat
  9424. cpltxt "\n "
  9425. (format "http://groups.google.com/groups?as_umsgid=%s"
  9426. (org-fixup-message-id-for-http message-id))))
  9427. (setq link (org-make-link "gnus:" group
  9428. "#" (number-to-string article))))))
  9429. ((eq major-mode 'w3-mode)
  9430. (setq cpltxt (url-view-url t)
  9431. link (org-make-link cpltxt)))
  9432. ((eq major-mode 'w3m-mode)
  9433. (setq cpltxt (or w3m-current-title w3m-current-url)
  9434. link (org-make-link w3m-current-url)))
  9435. ((setq search (run-hook-with-args-until-success
  9436. 'org-create-file-search-functions))
  9437. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  9438. "::" search))
  9439. (setq cpltxt (or description link)))
  9440. ((eq major-mode 'image-mode)
  9441. (setq cpltxt (concat "file:"
  9442. (abbreviate-file-name buffer-file-name))
  9443. link (org-make-link cpltxt)))
  9444. ((eq major-mode 'dired-mode)
  9445. ;; link to the file in the current line
  9446. (setq cpltxt (concat "file:"
  9447. (abbreviate-file-name
  9448. (expand-file-name
  9449. (dired-get-filename nil t))))
  9450. link (org-make-link cpltxt)))
  9451. ((org-mode-p)
  9452. ;; Just link to current headline
  9453. (setq cpltxt (concat "file:"
  9454. (abbreviate-file-name buffer-file-name)))
  9455. ;; Add a context search string
  9456. (when (org-xor org-context-in-file-links arg)
  9457. ;; Check if we are on a target
  9458. (if (save-excursion
  9459. (skip-chars-forward "^>\n\r")
  9460. (and (re-search-backward "<<" nil t)
  9461. (looking-at "<<\\(.*?\\)>>")
  9462. (<= (match-beginning 0) pos)
  9463. (>= (match-end 0) pos)))
  9464. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  9465. (setq txt (cond
  9466. ((org-on-heading-p) nil)
  9467. ((org-region-active-p)
  9468. (buffer-substring (region-beginning) (region-end)))
  9469. (t (buffer-substring (point-at-bol) (point-at-eol)))))
  9470. (when (or (null txt) (string-match "\\S-" txt))
  9471. (setq cpltxt
  9472. (concat cpltxt "::"
  9473. (if org-file-link-context-use-camel-case
  9474. (org-make-org-heading-camel txt)
  9475. (org-make-org-heading-search-string txt)))
  9476. desc "NONE"))))
  9477. (if (string-match "::\\'" cpltxt)
  9478. (setq cpltxt (substring cpltxt 0 -2)))
  9479. (setq link (org-make-link cpltxt)))
  9480. (buffer-file-name
  9481. ;; Just link to this file here.
  9482. (setq cpltxt (concat "file:"
  9483. (abbreviate-file-name buffer-file-name)))
  9484. ;; Add a context string
  9485. (when (org-xor org-context-in-file-links arg)
  9486. (setq txt (if (org-region-active-p)
  9487. (buffer-substring (region-beginning) (region-end))
  9488. (buffer-substring (point-at-bol) (point-at-eol))))
  9489. ;; Only use search option if there is some text.
  9490. (when (string-match "\\S-" txt)
  9491. (setq cpltxt
  9492. (concat cpltxt "::"
  9493. (if org-file-link-context-use-camel-case
  9494. (org-make-org-heading-camel txt)
  9495. (org-make-org-heading-search-string txt)))
  9496. desc "NONE")))
  9497. (setq link (org-make-link cpltxt)))
  9498. ((interactive-p)
  9499. (error "Cannot link to a buffer which is not visiting a file"))
  9500. (t (setq link nil)))
  9501. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  9502. (setq link (or link cpltxt)
  9503. desc (or desc cpltxt))
  9504. (if (equal desc "NONE") (setq desc nil))
  9505. (if (and (interactive-p) link)
  9506. (progn
  9507. (setq org-stored-links
  9508. (cons (list cpltxt link desc) org-stored-links))
  9509. (message "Stored: %s" (or cpltxt link)))
  9510. (org-make-link-string link desc))))
  9511. (defun org-make-org-heading-search-string (&optional string heading)
  9512. "Make search string for STRING or current headline."
  9513. (interactive)
  9514. (let ((s (or string (org-get-heading))))
  9515. (unless (and string (not heading))
  9516. ;; We are using a headline, clean up garbage in there.
  9517. (if (string-match org-todo-regexp s)
  9518. (setq s (replace-match "" t t s)))
  9519. (if (string-match ":[a-zA-Z_@0-9:]+:[ \t]*$" s)
  9520. (setq s (replace-match "" t t s)))
  9521. (setq s (org-trim s))
  9522. (if (string-match (concat "^\\(" org-quote-string "\\|"
  9523. org-comment-string "\\)") s)
  9524. (setq s (replace-match "" t t s)))
  9525. (while (string-match org-ts-regexp s)
  9526. (setq s (replace-match "" t t s))))
  9527. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  9528. (setq s (replace-match " " t t s)))
  9529. (or string (setq s (concat "*" s))) ; Add * for headlines
  9530. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  9531. (defun org-make-org-heading-camel (&optional string heading)
  9532. "Make a CamelCase string for STRING or the current headline."
  9533. (interactive)
  9534. (let ((s (or string (org-get-heading))))
  9535. (unless (and string (not heading))
  9536. ;; We are using a headline, clean up garbage in there.
  9537. (if (string-match org-todo-regexp s)
  9538. (setq s (replace-match "" t t s)))
  9539. (if (string-match ":[a-zA-Z_@0-9:]+:[ \t]*$" s)
  9540. (setq s (replace-match "" t t s)))
  9541. (setq s (org-trim s))
  9542. (if (string-match (concat "^\\(" org-quote-string "\\|"
  9543. org-comment-string "\\)") s)
  9544. (setq s (replace-match "" t t s)))
  9545. (while (string-match org-ts-regexp s)
  9546. (setq s (replace-match "" t t s))))
  9547. (while (string-match "[^a-zA-Z_ \t]+" s)
  9548. (setq s (replace-match " " t t s)))
  9549. (or string (setq s (concat "*" s))) ; Add * for headlines
  9550. (mapconcat 'capitalize (org-split-string s "[ \t]+") "")))
  9551. (defun org-make-link (&rest strings)
  9552. "Concatenate STRINGS, format resulting string with `org-link-format'."
  9553. (format org-link-format (apply 'concat strings)))
  9554. (defun org-make-link-string (link &optional description)
  9555. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  9556. (if (eq org-link-style 'plain)
  9557. (if (equal description link)
  9558. link
  9559. (concat description "\n" link))
  9560. (when (stringp description)
  9561. ;; Remove brackets from the description, they are fatal.
  9562. (while (string-match "\\[\\|\\]" description)
  9563. (setq description (replace-match "" t t description))))
  9564. (when (equal (org-link-escape link) description)
  9565. ;; No description needed, it is identical
  9566. (setq description nil))
  9567. (when (and (not description)
  9568. (not (equal link (org-link-escape link))))
  9569. (setq description link))
  9570. (concat "[[" (org-link-escape link) "]"
  9571. (if description (concat "[" description "]") "")
  9572. "]")))
  9573. (defconst org-link-escape-chars '(("[" . "%5B") ("]" . "%5D") (" " . "%20"))
  9574. "Association list of escapes for some characters problematic in links.")
  9575. (defun org-link-escape (text)
  9576. "Escape charaters in TEXT that are problematic for links."
  9577. (when text
  9578. (let ((re (mapconcat (lambda (x) (regexp-quote (car x)))
  9579. org-link-escape-chars "\\|")))
  9580. (while (string-match re text)
  9581. (setq text
  9582. (replace-match
  9583. (cdr (assoc (match-string 0 text) org-link-escape-chars))
  9584. t t text)))
  9585. text)))
  9586. (defun org-link-unescape (text)
  9587. "Reverse the action of `org-link-escape'."
  9588. (when text
  9589. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  9590. org-link-escape-chars "\\|")))
  9591. (while (string-match re text)
  9592. (setq text
  9593. (replace-match
  9594. (car (rassoc (match-string 0 text) org-link-escape-chars))
  9595. t t text)))
  9596. text)))
  9597. (defun org-xor (a b)
  9598. "Exclusive or."
  9599. (if a (not b) b))
  9600. (defun org-get-header (header)
  9601. "Find a header field in the current buffer."
  9602. (save-excursion
  9603. (goto-char (point-min))
  9604. (let ((case-fold-search t) s)
  9605. (cond
  9606. ((eq header 'from)
  9607. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  9608. (setq s (match-string 1)))
  9609. (while (string-match "\"" s)
  9610. (setq s (replace-match "" t t s)))
  9611. (if (string-match "[<(].*" s)
  9612. (setq s (replace-match "" t t s))))
  9613. ((eq header 'message-id)
  9614. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  9615. (setq s (match-string 1))))
  9616. ((eq header 'subject)
  9617. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  9618. (setq s (match-string 1)))))
  9619. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  9620. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  9621. s)))
  9622. (defun org-fixup-message-id-for-http (s)
  9623. "Replace special characters in a message id, so it can be used in an http query."
  9624. (while (string-match "<" s)
  9625. (setq s (replace-match "%3C" t t s)))
  9626. (while (string-match ">" s)
  9627. (setq s (replace-match "%3E" t t s)))
  9628. (while (string-match "@" s)
  9629. (setq s (replace-match "%40" t t s)))
  9630. s)
  9631. (defun org-insert-link (&optional complete-file)
  9632. "Insert a link. At the prompt, enter the link.
  9633. Completion can be used to select a link previously stored with
  9634. `org-store-link'. When the empty string is entered (i.e. if you just
  9635. press RET at the prompt), the link defaults to the most recently
  9636. stored link. As SPC triggers completion in the minibuffer, you need to
  9637. use M-SPC or C-q SPC to force the insertion of a space character.
  9638. You will also be prompted for a description, and if one is given, it will
  9639. be displayed in the buffer instead of the link.
  9640. If there is already a link at point, this command will allow you to edit link
  9641. and description parts.
  9642. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
  9643. selected using completion. The path to the file will be relative to
  9644. the current directory if the file is in the current directory or a
  9645. subdirectory. Otherwise, the link will be the absolute path as
  9646. completed in the minibuffer (i.e. normally ~/path/to/file).
  9647. With two \\[universal-argument] prefixes, enforce an absolute path even if the file
  9648. is in the current directory or below.
  9649. With three \\[universal-argument] prefixes, negate the meaning of
  9650. `org-keep-stored-link-after-insertion'."
  9651. (interactive "P")
  9652. (let (link desc entry remove file (pos (point)))
  9653. (cond
  9654. ((save-excursion
  9655. (skip-chars-forward "^]\n\r")
  9656. (and (re-search-backward "\\[\\[" nil t)
  9657. (looking-at org-bracket-link-regexp)
  9658. (<= (match-beginning 0) pos)
  9659. (>= (match-end 0) pos)))
  9660. ;; We do have a link at point, and we are going to edit it.
  9661. (setq remove (list (match-beginning 0) (match-end 0)))
  9662. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  9663. (setq link (read-string "Link: "
  9664. (org-link-unescape
  9665. (org-match-string-no-properties 1)))))
  9666. ((equal complete-file '(4))
  9667. ;; Completing read for file names.
  9668. (setq file (read-file-name "File: "))
  9669. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  9670. (pwd1 (file-name-as-directory (abbreviate-file-name
  9671. (expand-file-name ".")))))
  9672. (cond
  9673. ((equal complete-file '(16))
  9674. (setq link (org-make-link
  9675. "file:"
  9676. (abbreviate-file-name (expand-file-name file)))))
  9677. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9678. (setq link (org-make-link "file:" (match-string 1 file))))
  9679. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9680. (expand-file-name file))
  9681. (setq link (org-make-link
  9682. "file:" (match-string 1 (expand-file-name file)))))
  9683. (t (setq link (org-make-link "file:" file))))))
  9684. (t
  9685. ;; Read link, with completion for stored links.
  9686. (setq link (org-completing-read
  9687. "Link: " org-stored-links nil nil nil
  9688. org-insert-link-history
  9689. (or (car (car org-stored-links)))))
  9690. (setq entry (assoc link org-stored-links))
  9691. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  9692. (not org-keep-stored-link-after-insertion))
  9693. (setq org-stored-links (delq (assoc link org-stored-links)
  9694. org-stored-links)))
  9695. (setq link (if entry (nth 1 entry) link)
  9696. desc (or desc (nth 2 entry)))))
  9697. (if (string-match org-plain-link-re link)
  9698. ;; URL-like link, normalize the use of angular brackets.
  9699. (setq link (org-make-link (org-remove-angle-brackets link))))
  9700. ;; Check if we are linking to the current file with a search option
  9701. ;; If yes, simplify the link by using only the search option.
  9702. (when (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link)
  9703. (let* ((path (match-string 1 link))
  9704. (case-fold-search nil)
  9705. (search (match-string 2 link)))
  9706. (save-match-data
  9707. (if (equal (file-truename buffer-file-name) (file-truename path))
  9708. ;; We are linking to this same file, with a search option
  9709. (setq link search)))))
  9710. ;; Check if we can/should use a relative path. If yes, simplify the link
  9711. (when (string-match "\\<file:\\(.*\\)" link)
  9712. (let* ((path (match-string 1 link))
  9713. (case-fold-search nil))
  9714. (cond
  9715. ((eq org-link-file-path-type 'absolute)
  9716. (setq path (abbreviate-file-name (expand-file-name path))))
  9717. ((eq org-link-file-path-type 'noabbrev)
  9718. (setq path (expand-file-name path)))
  9719. ((eq org-link-file-path-type 'relative)
  9720. (setq path (file-relative-name path)))
  9721. (t
  9722. (save-match-data
  9723. (if (string-match (concat "^" (regexp-quote
  9724. (file-name-as-directory
  9725. (expand-file-name "."))))
  9726. (expand-file-name path))
  9727. ;; We are linking a file with relative path name.
  9728. (setq path (substring (expand-file-name path)
  9729. (match-end 0)))))))
  9730. (setq link (concat "file:" path))))
  9731. (setq desc (read-string "Description: " desc))
  9732. (unless (string-match "\\S-" desc) (setq desc nil))
  9733. (if remove (apply 'delete-region remove))
  9734. (insert (org-make-link-string link desc))))
  9735. (defun org-completing-read (&rest args)
  9736. (let ((minibuffer-local-completion-map
  9737. (copy-keymap minibuffer-local-completion-map)))
  9738. (define-key minibuffer-local-completion-map " " 'self-insert-command)
  9739. (apply 'completing-read args)))
  9740. ;;; Hooks for remember.el
  9741. (defvar org-finish-function nil)
  9742. ;;;###autoload
  9743. (defun org-remember-annotation ()
  9744. "Return a link to the current location as an annotation for remember.el.
  9745. If you are using Org-mode files as target for data storage with
  9746. remember.el, then the annotations should include a link compatible with the
  9747. conventions in Org-mode. This function returns such a link."
  9748. (org-store-link nil))
  9749. (defconst org-remember-help
  9750. "Select a destination location for the note.
  9751. UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
  9752. RET at beg-of-buf -> Append to file as level 2 headline
  9753. RET on headline -> Store as sublevel entry to current headline
  9754. <left>/<right> -> before/after current headline, same headings level")
  9755. ;;;###autoload
  9756. (defun org-remember-apply-template ()
  9757. "Initialize *remember* buffer with template, invoke `org-mode'.
  9758. This function should be placed into `remember-mode-hook' and in fact requires
  9759. to be run from that hook to fucntion properly."
  9760. (if org-remember-templates
  9761. (let* ((entry (if (= (length org-remember-templates) 1)
  9762. (cdar org-remember-templates)
  9763. (message "Select template: %s"
  9764. (mapconcat
  9765. (lambda (x) (char-to-string (car x)))
  9766. org-remember-templates " "))
  9767. (cdr (assoc (read-char-exclusive) org-remember-templates))))
  9768. (tpl (car entry))
  9769. (file (if (consp (cdr entry)) (nth 1 entry)))
  9770. (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
  9771. (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
  9772. (v-u (concat "[" (substring v-t 1 -1) "]"))
  9773. (v-U (concat "[" (substring v-T 1 -1) "]"))
  9774. (v-a annotation) ; defined in `remember-mode'
  9775. (v-i initial) ; defined in `remember-mode'
  9776. (v-n user-full-name)
  9777. )
  9778. (unless tpl (setq tpl "") (message "No template") (ding))
  9779. (insert tpl) (goto-char (point-min))
  9780. (while (re-search-forward "%\\([tTuTai]\\)" nil t)
  9781. (when (and initial (equal (match-string 0) "%i"))
  9782. (save-match-data
  9783. (let* ((lead (buffer-substring
  9784. (point-at-bol) (match-beginning 0))))
  9785. (setq v-i (mapconcat 'identity
  9786. (org-split-string initial "\n")
  9787. (concat "\n" lead))))))
  9788. (replace-match
  9789. (or (eval (intern (concat "v-" (match-string 1)))) "")
  9790. t t))
  9791. (let ((org-startup-folded nil)
  9792. (org-startup-with-deadline-check nil))
  9793. (org-mode))
  9794. (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
  9795. (org-set-local 'org-default-notes-file file))
  9796. (goto-char (point-min))
  9797. (if (re-search-forward "%\\?" nil t) (replace-match "")))
  9798. (let ((org-startup-folded nil)
  9799. (org-startup-with-deadline-check nil))
  9800. (org-mode)))
  9801. (org-set-local 'org-finish-function 'remember-buffer))
  9802. ;;;###autoload
  9803. (defun org-remember-handler ()
  9804. "Store stuff from remember.el into an org file.
  9805. First prompts for an org file. If the user just presses return, the value
  9806. of `org-default-notes-file' is used.
  9807. Then the command offers the headings tree of the selected file in order to
  9808. file the text at a specific location.
  9809. You can either immediately press RET to get the note appended to the
  9810. file, or you can use vertical cursor motion and visibility cycling (TAB) to
  9811. find a better place. Then press RET or <left> or <right> in insert the note.
  9812. Key Cursor position Note gets inserted
  9813. -----------------------------------------------------------------------------
  9814. RET buffer-start as level 2 heading at end of file
  9815. RET on headline as sublevel of the heading at cursor
  9816. RET no heading at cursor position, level taken from context.
  9817. Or use prefix arg to specify level manually.
  9818. <left> on headline as same level, before current heading
  9819. <right> on headline as same level, after current heading
  9820. So the fastest way to store the note is to press RET RET to append it to
  9821. the default file. This way your current train of thought is not
  9822. interrupted, in accordance with the principles of remember.el. But with
  9823. little extra effort, you can push it directly to the correct location.
  9824. Before being stored away, the function ensures that the text has a
  9825. headline, i.e. a first line that starts with a \"*\". If not, a headline
  9826. is constructed from the current date and some additional data.
  9827. If the variable `org-adapt-indentation' is non-nil, the entire text is
  9828. also indented so that it starts in the same column as the headline
  9829. \(i.e. after the stars).
  9830. See also the variable `org-reverse-note-order'."
  9831. (catch 'quit
  9832. (let* ((txt (buffer-substring (point-min) (point-max)))
  9833. (fastp current-prefix-arg)
  9834. (file (if fastp org-default-notes-file (org-get-org-file)))
  9835. (visiting (find-buffer-visiting file))
  9836. (org-startup-with-deadline-check nil)
  9837. (org-startup-folded nil)
  9838. (org-startup-align-all-tables nil)
  9839. spos level indent reversed)
  9840. ;; Modify text so that it becomes a nice subtree which can be inserted
  9841. ;; into an org tree.
  9842. (let* ((lines (split-string txt "\n"))
  9843. first)
  9844. ;; remove empty lines at the beginning
  9845. (while (and lines (string-match "^[ \t]*\n" (car lines)))
  9846. (setq lines (cdr lines)))
  9847. (setq first (car lines) lines (cdr lines))
  9848. (if (string-match "^\\*+" first)
  9849. ;; Is already a headline
  9850. (setq indent nil)
  9851. ;; We need to add a headline: Use time and first buffer line
  9852. (setq lines (cons first lines)
  9853. first (concat "* " (current-time-string)
  9854. " (" (remember-buffer-desc) ")")
  9855. indent " "))
  9856. (if (and org-adapt-indentation indent)
  9857. (setq lines (mapcar (lambda (x) (concat indent x)) lines)))
  9858. (setq txt (concat first "\n"
  9859. (mapconcat 'identity lines "\n"))))
  9860. ;; Find the file
  9861. (if (not visiting)
  9862. (find-file-noselect file))
  9863. (with-current-buffer (get-file-buffer file)
  9864. (save-excursion (and (goto-char (point-min))
  9865. (not (re-search-forward "^\\* " nil t))
  9866. (insert "\n* Notes\n")))
  9867. (setq reversed (org-notes-order-reversed-p))
  9868. (save-excursion
  9869. (save-restriction
  9870. (widen)
  9871. ;; Ask the User for a location
  9872. (setq spos (if fastp 1 (org-get-location
  9873. (current-buffer)
  9874. org-remember-help)))
  9875. (if (not spos) (throw 'quit nil)) ; return nil to show we did
  9876. ; not handle this note
  9877. (goto-char spos)
  9878. (cond ((bobp)
  9879. ;; Put it at the start or end, as level 2
  9880. (save-restriction
  9881. (widen)
  9882. (goto-char (if reversed (point-min) (point-max)))
  9883. (if (not (bolp)) (newline))
  9884. (org-paste-subtree 2 txt)))
  9885. ((and (org-on-heading-p nil) (not current-prefix-arg))
  9886. ;; Put it below this entry, at the beg/end of the subtree
  9887. (org-back-to-heading)
  9888. (setq level (funcall outline-level))
  9889. (if reversed
  9890. (outline-end-of-heading)
  9891. (outline-end-of-subtree))
  9892. (if (not (bolp)) (newline))
  9893. (beginning-of-line 1)
  9894. (org-paste-subtree (1+ level) txt))
  9895. (t
  9896. ;; Put it right there, with automatic level determined by
  9897. ;; org-paste-subtree or from prefix arg
  9898. (org-paste-subtree current-prefix-arg txt)))
  9899. (when remember-save-after-remembering
  9900. (save-buffer)
  9901. (if (not visiting) (kill-buffer (current-buffer)))))))))
  9902. t) ;; return t to indicate that we took care of this note.
  9903. (defun org-get-org-file ()
  9904. "Read a filename, with default directory `org-directory'."
  9905. (let ((default (or org-default-notes-file remember-data-file)))
  9906. (read-file-name (format "File name [%s]: " default)
  9907. (file-name-as-directory org-directory)
  9908. default)))
  9909. (defun org-notes-order-reversed-p ()
  9910. "Check if the current file should receive notes in reversed order."
  9911. (cond
  9912. ((not org-reverse-note-order) nil)
  9913. ((eq t org-reverse-note-order) t)
  9914. ((not (listp org-reverse-note-order)) nil)
  9915. (t (catch 'exit
  9916. (let ((all org-reverse-note-order)
  9917. entry)
  9918. (while (setq entry (pop all))
  9919. (if (string-match (car entry) buffer-file-name)
  9920. (throw 'exit (cdr entry))))
  9921. nil)))))
  9922. ;;; Tables
  9923. ;; Watch out: Here we are talking about two different kind of tables.
  9924. ;; Most of the code is for the tables created with the Org-mode table editor.
  9925. ;; Sometimes, we talk about tables created and edited with the table.el
  9926. ;; Emacs package. We call the former org-type tables, and the latter
  9927. ;; table.el-type tables.
  9928. (defun org-before-change-function (beg end)
  9929. "Every change indicates that a table might need an update."
  9930. (setq org-table-may-need-update t))
  9931. (defconst org-table-line-regexp "^[ \t]*|"
  9932. "Detects an org-type table line.")
  9933. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  9934. "Detects an org-type table line.")
  9935. (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
  9936. "Detects a table line marked for automatic recalculation.")
  9937. (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
  9938. "Detects a table line marked for automatic recalculation.")
  9939. (defconst org-table-calculate-mark-regexp "^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
  9940. "Detects a table line marked for automatic recalculation.")
  9941. (defconst org-table-hline-regexp "^[ \t]*|-"
  9942. "Detects an org-type table hline.")
  9943. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  9944. "Detects a table-type table hline.")
  9945. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  9946. "Detects an org-type or table-type table.")
  9947. (defconst org-table-border-regexp "^[ \t]*[^| \t]"
  9948. "Searching from within a table (any type) this finds the first line
  9949. outside the table.")
  9950. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  9951. "Searching from within a table (any type) this finds the first line
  9952. outside the table.")
  9953. (defun org-table-create-with-table.el ()
  9954. "Use the table.el package to insert a new table.
  9955. If there is already a table at point, convert between Org-mode tables
  9956. and table.el tables."
  9957. (interactive)
  9958. (require 'table)
  9959. (cond
  9960. ((org-at-table.el-p)
  9961. (if (y-or-n-p "Convert table to Org-mode table? ")
  9962. (org-table-convert)))
  9963. ((org-at-table-p)
  9964. (if (y-or-n-p "Convert table to table.el table? ")
  9965. (org-table-convert)))
  9966. (t (call-interactively 'table-insert))))
  9967. (defun org-table-create-or-convert-from-region (arg)
  9968. "Convert region to table, or create an empty table.
  9969. If there is an active region, convert it to a table. If there is no such
  9970. region, create an empty table."
  9971. (interactive "P")
  9972. (if (org-region-active-p)
  9973. (org-table-convert-region (region-beginning) (region-end) arg)
  9974. (org-table-create arg)))
  9975. (defun org-table-create (&optional size)
  9976. "Query for a size and insert a table skeleton.
  9977. SIZE is a string Columns x Rows like for example \"3x2\"."
  9978. (interactive "P")
  9979. (unless size
  9980. (setq size (read-string
  9981. (concat "Table size Columns x Rows [e.g. "
  9982. org-table-default-size "]: ")
  9983. "" nil org-table-default-size)))
  9984. (let* ((pos (point))
  9985. (indent (make-string (current-column) ?\ ))
  9986. (split (org-split-string size " *x *"))
  9987. (rows (string-to-number (nth 1 split)))
  9988. (columns (string-to-number (car split)))
  9989. (line (concat (apply 'concat indent "|" (make-list columns " |"))
  9990. "\n")))
  9991. (if (string-match "^[ \t]*$" (buffer-substring-no-properties
  9992. (point-at-bol) (point)))
  9993. (beginning-of-line 1)
  9994. (newline))
  9995. ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
  9996. (dotimes (i rows) (insert line))
  9997. (goto-char pos)
  9998. (if (> rows 1)
  9999. ;; Insert a hline after the first row.
  10000. (progn
  10001. (end-of-line 1)
  10002. (insert "\n|-")
  10003. (goto-char pos)))
  10004. (org-table-align)))
  10005. (defun org-table-convert-region (beg0 end0 &optional nspace)
  10006. "Convert region to a table.
  10007. The region goes from BEG0 to END0, but these borders will be moved
  10008. slightly, to make sure a beginning of line in the first line is included.
  10009. When NSPACE is non-nil, it indicates the minimum number of spaces that
  10010. separate columns (default: just one space)."
  10011. (interactive "rP")
  10012. (let* ((beg (min beg0 end0))
  10013. (end (max beg0 end0))
  10014. (tabsep t)
  10015. re)
  10016. (goto-char beg)
  10017. (beginning-of-line 1)
  10018. (setq beg (move-marker (make-marker) (point)))
  10019. (goto-char end)
  10020. (if (bolp) (backward-char 1) (end-of-line 1))
  10021. (setq end (move-marker (make-marker) (point)))
  10022. ;; Lets see if this is tab-separated material. If every nonempty line
  10023. ;; contains a tab, we will assume that it is tab-separated material
  10024. (if nspace
  10025. (setq tabsep nil)
  10026. (goto-char beg)
  10027. (and (re-search-forward "^[^\n\t]+$" end t) (setq tabsep nil)))
  10028. (if nspace (setq tabsep nil))
  10029. (if tabsep
  10030. (setq re "^\\|\t")
  10031. (setq re (format "^ *\\| *\t *\\| \\{%d,\\}"
  10032. (max 1 (prefix-numeric-value nspace)))))
  10033. (goto-char beg)
  10034. (while (re-search-forward re end t)
  10035. (replace-match "|" t t))
  10036. (goto-char beg)
  10037. (insert " ")
  10038. (org-table-align)))
  10039. (defun org-table-import (file arg)
  10040. "Import FILE as a table.
  10041. The file is assumed to be tab-separated. Such files can be produced by most
  10042. spreadsheet and database applications. If no tabs (at least one per line)
  10043. are found, lines will be split on whitespace into fields."
  10044. (interactive "f\nP")
  10045. (or (bolp) (newline))
  10046. (let ((beg (point))
  10047. (pm (point-max)))
  10048. (insert-file-contents file)
  10049. (org-table-convert-region beg (+ (point) (- (point-max) pm)) arg)))
  10050. (defun org-table-export ()
  10051. "Export table as a tab-separated file.
  10052. Such a file can be imported into a spreadsheet program like Excel."
  10053. (interactive)
  10054. (let* ((beg (org-table-begin))
  10055. (end (org-table-end))
  10056. (table (buffer-substring beg end))
  10057. (file (read-file-name "Export table to: "))
  10058. buf)
  10059. (unless (or (not (file-exists-p file))
  10060. (y-or-n-p (format "Overwrite file %s? " file)))
  10061. (error "Abort"))
  10062. (with-current-buffer (find-file-noselect file)
  10063. (setq buf (current-buffer))
  10064. (erase-buffer)
  10065. (fundamental-mode)
  10066. (insert table)
  10067. (goto-char (point-min))
  10068. (while (re-search-forward "^[ \t]*|[ \t]*" nil t)
  10069. (replace-match "" t t)
  10070. (end-of-line 1))
  10071. (goto-char (point-min))
  10072. (while (re-search-forward "[ \t]*|[ \t]*$" nil t)
  10073. (replace-match "" t t)
  10074. (goto-char (min (1+ (point)) (point-max))))
  10075. (goto-char (point-min))
  10076. (while (re-search-forward "^-[-+]*$" nil t)
  10077. (replace-match "")
  10078. (if (looking-at "\n")
  10079. (delete-char 1)))
  10080. (goto-char (point-min))
  10081. (while (re-search-forward "[ \t]*|[ \t]*" nil t)
  10082. (replace-match "\t" t t))
  10083. (save-buffer))
  10084. (kill-buffer buf)))
  10085. (defvar org-table-aligned-begin-marker (make-marker)
  10086. "Marker at the beginning of the table last aligned.
  10087. Used to check if cursor still is in that table, to minimize realignment.")
  10088. (defvar org-table-aligned-end-marker (make-marker)
  10089. "Marker at the end of the table last aligned.
  10090. Used to check if cursor still is in that table, to minimize realignment.")
  10091. (defvar org-table-last-alignment nil
  10092. "List of flags for flushright alignment, from the last re-alignment.
  10093. This is being used to correctly align a single field after TAB or RET.")
  10094. (defvar org-table-last-column-widths nil
  10095. "List of max width of fields in each column.
  10096. This is being used to correctly align a single field after TAB or RET.")
  10097. (defvar org-last-recalc-line nil)
  10098. (defconst org-narrow-column-arrow "=>"
  10099. "Used as display property in narrowed table columns.")
  10100. (defun org-table-align ()
  10101. "Align the table at point by aligning all vertical bars."
  10102. (interactive)
  10103. (let* (
  10104. ;; Limits of table
  10105. (beg (org-table-begin))
  10106. (end (org-table-end))
  10107. ;; Current cursor position
  10108. (linepos (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
  10109. (colpos (org-table-current-column))
  10110. (winstart (window-start))
  10111. lines (new "") lengths l typenums ty fields maxfields i
  10112. column
  10113. (indent "") cnt frac
  10114. rfmt hfmt
  10115. (spaces '(1 . 1))
  10116. (sp1 (car spaces))
  10117. (sp2 (cdr spaces))
  10118. (rfmt1 (concat
  10119. (make-string sp2 ?\ ) "%%%s%ds" (make-string sp1 ?\ ) "|"))
  10120. (hfmt1 (concat
  10121. (make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
  10122. emptystrings links narrow fmax f1 len c e)
  10123. (untabify beg end)
  10124. (remove-text-properties beg end '(org-cwidth t display t))
  10125. ;; Check if we have links
  10126. (goto-char beg)
  10127. (setq links (re-search-forward org-bracket-link-regexp end t))
  10128. ;; Make sure the link properties are right
  10129. (when links (goto-char beg) (while (org-activate-bracket-links end)))
  10130. ;; Check if we are narrowing any columns
  10131. (goto-char beg)
  10132. (setq narrow (and org-format-transports-properties-p
  10133. (re-search-forward "<[0-9]+>" end t)))
  10134. ;; Get the rows
  10135. (setq lines (org-split-string
  10136. (buffer-substring beg end) "\n"))
  10137. ;; Store the indentation of the first line
  10138. (if (string-match "^ *" (car lines))
  10139. (setq indent (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  10140. ;; Mark the hlines by setting the corresponding element to nil
  10141. ;; At the same time, we remove trailing space.
  10142. (setq lines (mapcar (lambda (l)
  10143. (if (string-match "^ *|-" l)
  10144. nil
  10145. (if (string-match "[ \t]+$" l)
  10146. (substring l 0 (match-beginning 0))
  10147. l)))
  10148. lines))
  10149. ;; Get the data fields by splitting the lines.
  10150. (setq fields (mapcar
  10151. (lambda (l)
  10152. (org-split-string l " *| *"))
  10153. (delq nil (copy-sequence lines))))
  10154. ;; How many fields in the longest line?
  10155. (condition-case nil
  10156. (setq maxfields (apply 'max (mapcar 'length fields)))
  10157. (error
  10158. (kill-region beg end)
  10159. (org-table-create org-table-default-size)
  10160. (error "Empty table - created default table")))
  10161. ;; A list of empty string to fill any short rows on output
  10162. (setq emptystrings (make-list maxfields ""))
  10163. ;; Check for special formatting.
  10164. (setq i -1)
  10165. (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns
  10166. (setq column (mapcar (lambda (x) (or (nth i x) "")) fields))
  10167. ;; Check if there is an explicit width specified
  10168. (when (and org-table-limit-column-width narrow)
  10169. (setq c column fmax nil)
  10170. (while c
  10171. (setq e (pop c))
  10172. (if (and (stringp e) (string-match "^<\\([0-9]+\\)>$" e))
  10173. (setq fmax (string-to-number (match-string 1 e)) c nil)))
  10174. ;; Find fields that are wider than fmax, and shorten them
  10175. (when fmax
  10176. (loop for xx in column do
  10177. (when (and (stringp xx)
  10178. (> (org-string-width xx) fmax))
  10179. (org-add-props xx nil
  10180. 'help-echo
  10181. (concat "Clipped table field, use C-c ` to edit. Full value is:\n" (org-no-properties (copy-sequence xx))))
  10182. (setq f1 (min fmax (or (string-match org-bracket-link-regexp xx) fmax)))
  10183. (unless (> f1 1)
  10184. (error "Cannot narrow field starting with wide link \"%s\""
  10185. (match-string 0 xx)))
  10186. (add-text-properties f1 (length xx) (list 'org-cwidth t) xx)
  10187. (add-text-properties (- f1 2) f1
  10188. (list 'display org-narrow-column-arrow)
  10189. xx)))))
  10190. ;; Get the maximum width for each column
  10191. (push (apply 'max 1 (mapcar 'org-string-width column)) lengths)
  10192. ;; Get the fraction of numbers, to decide about alignment of the column
  10193. (setq cnt 0 frac 0.0)
  10194. (loop for x in column do
  10195. (if (equal x "")
  10196. nil
  10197. (setq frac ( / (+ (* frac cnt)
  10198. (if (string-match org-table-number-regexp x) 1 0))
  10199. (setq cnt (1+ cnt))))))
  10200. (push (>= frac org-table-number-fraction) typenums))
  10201. (setq lengths (nreverse lengths) typenums (nreverse typenums))
  10202. ;; Store the alignment of this table, for later editing of single fields
  10203. (setq org-table-last-alignment typenums
  10204. org-table-last-column-widths lengths)
  10205. ;; With invisible characters, `format' does not get the field width right
  10206. ;; So we need to make these fields wide by hand.
  10207. (when links
  10208. (loop for i from 0 upto (1- maxfields) do
  10209. (setq len (nth i lengths))
  10210. (loop for j from 0 upto (1- (length fields)) do
  10211. (setq c (nthcdr i (car (nthcdr j fields))))
  10212. (if (and (stringp (car c))
  10213. (string-match org-bracket-link-regexp (car c))
  10214. (< (org-string-width (car c)) len))
  10215. (setcar c (concat (car c) (make-string (- len (org-string-width (car c))) ?\ )))))))
  10216. ;; Compute the formats needed for output of the table
  10217. (setq rfmt (concat indent "|") hfmt (concat indent "|"))
  10218. (while (setq l (pop lengths))
  10219. (setq ty (if (pop typenums) "" "-")) ; number types flushright
  10220. (setq rfmt (concat rfmt (format rfmt1 ty l))
  10221. hfmt (concat hfmt (format hfmt1 (make-string l ?-)))))
  10222. (setq rfmt (concat rfmt "\n")
  10223. hfmt (concat (substring hfmt 0 -1) "|\n"))
  10224. (setq new (mapconcat
  10225. (lambda (l)
  10226. (if l (apply 'format rfmt
  10227. (append (pop fields) emptystrings))
  10228. hfmt))
  10229. lines ""))
  10230. ;; Replace the old one
  10231. (delete-region beg end)
  10232. (move-marker end nil)
  10233. (move-marker org-table-aligned-begin-marker (point))
  10234. (insert new)
  10235. (move-marker org-table-aligned-end-marker (point))
  10236. (when (and orgtbl-mode (not (org-mode-p)))
  10237. (goto-char org-table-aligned-begin-marker)
  10238. (while (org-hide-wide-columns org-table-aligned-end-marker)))
  10239. ;; Try to move to the old location (approximately)
  10240. (goto-line linepos)
  10241. (set-window-start (selected-window) winstart 'noforce)
  10242. (org-table-goto-column colpos)
  10243. (setq org-table-may-need-update nil)
  10244. ))
  10245. (defun org-string-width (s)
  10246. "Compute width of string, ignoring invisible characters.
  10247. This ignores character with invisibility property `org-link', and also
  10248. characters with property `org-cwidth', because these will become invisible
  10249. upon the next fontification round."
  10250. (let (b)
  10251. (when (or (eq t buffer-invisibility-spec)
  10252. (assq 'org-link buffer-invisibility-spec))
  10253. (while (setq b (text-property-any 0 (length s)
  10254. 'invisible 'org-link s))
  10255. (setq s (concat (substring s 0 b)
  10256. (substring s (or (next-single-property-change
  10257. b 'invisible s) (length s)))))))
  10258. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  10259. (setq s (concat (substring s 0 b)
  10260. (substring s (or (next-single-property-change
  10261. b 'org-cwidth s) (length s))))))
  10262. (string-width s)))
  10263. (defun org-table-begin (&optional table-type)
  10264. "Find the beginning of the table and return its position.
  10265. With argument TABLE-TYPE, go to the beginning of a table.el-type table."
  10266. (save-excursion
  10267. (if (not (re-search-backward
  10268. (if table-type org-table-any-border-regexp
  10269. org-table-border-regexp)
  10270. nil t))
  10271. (progn (goto-char (point-min)) (point))
  10272. (goto-char (match-beginning 0))
  10273. (beginning-of-line 2)
  10274. (point))))
  10275. (defun org-table-end (&optional table-type)
  10276. "Find the end of the table and return its position.
  10277. With argument TABLE-TYPE, go to the end of a table.el-type table."
  10278. (save-excursion
  10279. (if (not (re-search-forward
  10280. (if table-type org-table-any-border-regexp
  10281. org-table-border-regexp)
  10282. nil t))
  10283. (goto-char (point-max))
  10284. (goto-char (match-beginning 0)))
  10285. (point-marker)))
  10286. (defun org-table-justify-field-maybe (&optional new)
  10287. "Justify the current field, text to left, number to right.
  10288. Optional argument NEW may specify text to replace the current field content."
  10289. (cond
  10290. ((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
  10291. ((org-at-table-hline-p))
  10292. ((and (not new)
  10293. (or (not (equal (marker-buffer org-table-aligned-begin-marker)
  10294. (current-buffer)))
  10295. (< (point) org-table-aligned-begin-marker)
  10296. (>= (point) org-table-aligned-end-marker)))
  10297. ;; This is not the same table, force a full re-align
  10298. (setq org-table-may-need-update t))
  10299. (t ;; realign the current field, based on previous full realign
  10300. (let* ((pos (point)) s
  10301. (col (org-table-current-column))
  10302. (num (if (> col 0) (nth (1- col) org-table-last-alignment)))
  10303. l f n o e)
  10304. (when (> col 0)
  10305. (skip-chars-backward "^|\n")
  10306. (if (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)")
  10307. (progn
  10308. (setq s (match-string 1)
  10309. o (match-string 0)
  10310. l (max 1 (- (match-end 0) (match-beginning 0) 3))
  10311. e (not (= (match-beginning 2) (match-end 2))))
  10312. (setq f (format (if num " %%%ds %s" " %%-%ds %s")
  10313. l (if e "|" (setq org-table-may-need-update t) ""))
  10314. n (format f s))
  10315. (if new
  10316. (if (<= (length new) l) ;; FIXME: length -> str-width?
  10317. (setq n (format f new))
  10318. (setq n (concat new "|") org-table-may-need-update t)))
  10319. (or (equal n o)
  10320. (let (org-table-may-need-update)
  10321. (replace-match n))))
  10322. (setq org-table-may-need-update t))
  10323. (goto-char pos))))))
  10324. (defun org-table-next-field ()
  10325. "Go to the next field in the current table, creating new lines as needed.
  10326. Before doing so, re-align the table if necessary."
  10327. (interactive)
  10328. (org-table-maybe-eval-formula)
  10329. (org-table-maybe-recalculate-line)
  10330. (if (and org-table-automatic-realign
  10331. org-table-may-need-update)
  10332. (org-table-align))
  10333. (let ((end (org-table-end)))
  10334. (if (org-at-table-hline-p)
  10335. (end-of-line 1))
  10336. (condition-case nil
  10337. (progn
  10338. (re-search-forward "|" end)
  10339. (if (looking-at "[ \t]*$")
  10340. (re-search-forward "|" end))
  10341. (if (and (looking-at "-")
  10342. org-table-tab-jumps-over-hlines
  10343. (re-search-forward "^[ \t]*|\\([^-]\\)" end t))
  10344. (goto-char (match-beginning 1)))
  10345. (if (looking-at "-")
  10346. (progn
  10347. (beginning-of-line 0)
  10348. (org-table-insert-row 'below))
  10349. (if (looking-at " ") (forward-char 1))))
  10350. (error
  10351. (org-table-insert-row 'below)))))
  10352. (defun org-table-previous-field ()
  10353. "Go to the previous field in the table.
  10354. Before doing so, re-align the table if necessary."
  10355. (interactive)
  10356. (org-table-justify-field-maybe)
  10357. (org-table-maybe-recalculate-line)
  10358. (if (and org-table-automatic-realign
  10359. org-table-may-need-update)
  10360. (org-table-align))
  10361. (if (org-at-table-hline-p)
  10362. (end-of-line 1))
  10363. (re-search-backward "|" (org-table-begin))
  10364. (re-search-backward "|" (org-table-begin))
  10365. (while (looking-at "|\\(-\\|[ \t]*$\\)")
  10366. (re-search-backward "|" (org-table-begin)))
  10367. (if (looking-at "| ?")
  10368. (goto-char (match-end 0))))
  10369. (defun org-table-next-row ()
  10370. "Go to the next row (same column) in the current table.
  10371. Before doing so, re-align the table if necessary."
  10372. (interactive)
  10373. (org-table-maybe-eval-formula)
  10374. (org-table-maybe-recalculate-line)
  10375. (if (or (looking-at "[ \t]*$")
  10376. (save-excursion (skip-chars-backward " \t") (bolp)))
  10377. (newline)
  10378. (if (and org-table-automatic-realign
  10379. org-table-may-need-update)
  10380. (org-table-align))
  10381. (let ((col (org-table-current-column)))
  10382. (beginning-of-line 2)
  10383. (if (or (not (org-at-table-p))
  10384. (org-at-table-hline-p))
  10385. (progn
  10386. (beginning-of-line 0)
  10387. (org-table-insert-row 'below)))
  10388. (org-table-goto-column col)
  10389. (skip-chars-backward "^|\n\r")
  10390. (if (looking-at " ") (forward-char 1)))))
  10391. (defun org-table-copy-down (n)
  10392. "Copy a field down in the current column.
  10393. If the field at the cursor is empty, copy into it the content of the nearest
  10394. non-empty field above. With argument N, use the Nth non-empty field.
  10395. If the current field is not empty, it is copied down to the next row, and
  10396. the cursor is moved with it. Therefore, repeating this command causes the
  10397. column to be filled row-by-row.
  10398. If the variable `org-table-copy-increment' is non-nil and the field is an
  10399. integer, it will be incremented while copying."
  10400. (interactive "p")
  10401. (let* ((colpos (org-table-current-column))
  10402. (field (org-table-get-field))
  10403. (non-empty (string-match "[^ \t]" field))
  10404. (beg (org-table-begin))
  10405. txt)
  10406. (org-table-check-inside-data-field)
  10407. (if non-empty
  10408. (progn
  10409. (setq txt (org-trim field))
  10410. (org-table-next-row)
  10411. (org-table-blank-field))
  10412. (save-excursion
  10413. (setq txt
  10414. (catch 'exit
  10415. (while (progn (beginning-of-line 1)
  10416. (re-search-backward org-table-dataline-regexp
  10417. beg t))
  10418. (org-table-goto-column colpos t)
  10419. (if (and (looking-at
  10420. "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
  10421. (= (setq n (1- n)) 0))
  10422. (throw 'exit (match-string 1))))))))
  10423. (if txt
  10424. (progn
  10425. (if (and org-table-copy-increment
  10426. (string-match "^[0-9]+$" txt))
  10427. (setq txt (format "%d" (+ (string-to-number txt) 1))))
  10428. (insert txt)
  10429. (org-table-maybe-recalculate-line)
  10430. (org-table-align))
  10431. (error "No non-empty field found"))))
  10432. (defun org-table-check-inside-data-field ()
  10433. "Is point inside a table data field?
  10434. I.e. not on a hline or before the first or after the last column?
  10435. This actually throws an error, so it aborts the current command."
  10436. (if (or (not (org-at-table-p))
  10437. (= (org-table-current-column) 0)
  10438. (org-at-table-hline-p)
  10439. (looking-at "[ \t]*$"))
  10440. (error "Not in table data field")))
  10441. (defvar org-table-clip nil
  10442. "Clipboard for table regions.")
  10443. (defun org-table-blank-field ()
  10444. "Blank the current table field or active region."
  10445. (interactive)
  10446. (org-table-check-inside-data-field)
  10447. (if (and (interactive-p) (org-region-active-p))
  10448. (let (org-table-clip)
  10449. (org-table-cut-region (region-beginning) (region-end)))
  10450. (skip-chars-backward "^|")
  10451. (backward-char 1)
  10452. (if (looking-at "|[^|\n]+")
  10453. (let* ((pos (match-beginning 0))
  10454. (match (match-string 0))
  10455. (len (org-string-width match)))
  10456. (replace-match (concat "|" (make-string (1- len) ?\ )))
  10457. (goto-char (+ 2 pos))
  10458. (substring match 1)))))
  10459. (defun org-table-get-field (&optional n replace)
  10460. "Return the value of the field in column N of current row.
  10461. N defaults to current field.
  10462. If REPLACE is a string, replace field with this value. The return value
  10463. is always the old value."
  10464. (and n (org-table-goto-column n))
  10465. (skip-chars-backward "^|\n")
  10466. (backward-char 1)
  10467. (if (looking-at "|[^|\r\n]*")
  10468. (let* ((pos (match-beginning 0))
  10469. (val (buffer-substring (1+ pos) (match-end 0))))
  10470. (if replace
  10471. (replace-match (concat "|" replace)))
  10472. (goto-char (min (point-at-eol) (+ 2 pos)))
  10473. val)
  10474. (forward-char 1) ""))
  10475. (defun org-table-current-column ()
  10476. "Find out which column we are in.
  10477. When called interactively, column is also displayed in echo area."
  10478. (interactive)
  10479. (if (interactive-p) (org-table-check-inside-data-field))
  10480. (save-excursion
  10481. (let ((cnt 0) (pos (point)))
  10482. (beginning-of-line 1)
  10483. (while (search-forward "|" pos t)
  10484. (setq cnt (1+ cnt)))
  10485. (if (interactive-p) (message "This is table column %d" cnt))
  10486. cnt)))
  10487. (defun org-table-goto-column (n &optional on-delim force)
  10488. "Move the cursor to the Nth column in the current table line.
  10489. With optional argument ON-DELIM, stop with point before the left delimiter
  10490. of the field.
  10491. If there are less than N fields, just go to after the last delimiter.
  10492. However, when FORCE is non-nil, create new columns if necessary."
  10493. (interactive "p")
  10494. (let ((pos (point-at-eol)))
  10495. (beginning-of-line 1)
  10496. (when (> n 0)
  10497. (while (and (> (setq n (1- n)) -1)
  10498. (or (search-forward "|" pos t)
  10499. (and force
  10500. (progn (end-of-line 1)
  10501. (skip-chars-backward "^|")
  10502. (insert " | "))))))
  10503. ; (backward-char 2) t)))))
  10504. (when (and force (not (looking-at ".*|")))
  10505. (save-excursion (end-of-line 1) (insert " | ")))
  10506. (if on-delim
  10507. (backward-char 1)
  10508. (if (looking-at " ") (forward-char 1))))))
  10509. (defun org-at-table-p (&optional table-type)
  10510. "Return t if the cursor is inside an org-type table.
  10511. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  10512. (if org-enable-table-editor
  10513. (save-excursion
  10514. (beginning-of-line 1)
  10515. (looking-at (if table-type org-table-any-line-regexp
  10516. org-table-line-regexp)))
  10517. nil))
  10518. (defun org-at-table.el-p ()
  10519. "Return t if and only if we are at a table.el table."
  10520. (and (org-at-table-p 'any)
  10521. (save-excursion
  10522. (goto-char (org-table-begin 'any))
  10523. (looking-at org-table1-hline-regexp))))
  10524. (defun org-table-recognize-table.el ()
  10525. "If there is a table.el table nearby, recognize it and move into it."
  10526. (if org-table-tab-recognizes-table.el
  10527. (if (org-at-table.el-p)
  10528. (progn
  10529. (beginning-of-line 1)
  10530. (if (looking-at org-table-dataline-regexp)
  10531. nil
  10532. (if (looking-at org-table1-hline-regexp)
  10533. (progn
  10534. (beginning-of-line 2)
  10535. (if (looking-at org-table-any-border-regexp)
  10536. (beginning-of-line -1)))))
  10537. (if (re-search-forward "|" (org-table-end t) t)
  10538. (progn
  10539. (require 'table)
  10540. (if (table--at-cell-p (point))
  10541. t
  10542. (message "recognizing table.el table...")
  10543. (table-recognize-table)
  10544. (message "recognizing table.el table...done")))
  10545. (error "This should not happen..."))
  10546. t)
  10547. nil)
  10548. nil))
  10549. (defun org-at-table-hline-p ()
  10550. "Return t if the cursor is inside a hline in a table."
  10551. (if org-enable-table-editor
  10552. (save-excursion
  10553. (beginning-of-line 1)
  10554. (looking-at org-table-hline-regexp))
  10555. nil))
  10556. (defun org-table-insert-column ()
  10557. "Insert a new column into the table."
  10558. (interactive)
  10559. (if (not (org-at-table-p))
  10560. (error "Not at a table"))
  10561. (org-table-find-dataline)
  10562. (let* ((col (max 1 (org-table-current-column)))
  10563. (beg (org-table-begin))
  10564. (end (org-table-end))
  10565. ;; Current cursor position
  10566. (linepos (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
  10567. (colpos col))
  10568. (goto-char beg)
  10569. (while (< (point) end)
  10570. (if (org-at-table-hline-p)
  10571. nil
  10572. (org-table-goto-column col t)
  10573. (insert "| "))
  10574. (beginning-of-line 2))
  10575. (move-marker end nil)
  10576. (goto-line linepos)
  10577. (org-table-goto-column colpos)
  10578. (org-table-align)
  10579. (org-table-modify-formulas 'insert col)))
  10580. (defun org-table-find-dataline ()
  10581. "Find a dataline in the current table, which is needed for column commands."
  10582. (if (and (org-at-table-p)
  10583. (not (org-at-table-hline-p)))
  10584. t
  10585. (let ((col (current-column))
  10586. (end (org-table-end)))
  10587. (move-to-column col)
  10588. (while (and (< (point) end)
  10589. (or (not (= (current-column) col))
  10590. (org-at-table-hline-p)))
  10591. (beginning-of-line 2)
  10592. (move-to-column col))
  10593. (if (and (org-at-table-p)
  10594. (not (org-at-table-hline-p)))
  10595. t
  10596. (error
  10597. "Please position cursor in a data line for column operations")))))
  10598. (defun org-table-delete-column ()
  10599. "Delete a column from the table."
  10600. (interactive)
  10601. (if (not (org-at-table-p))
  10602. (error "Not at a table"))
  10603. (org-table-find-dataline)
  10604. (org-table-check-inside-data-field)
  10605. (let* ((col (org-table-current-column))
  10606. (beg (org-table-begin))
  10607. (end (org-table-end))
  10608. ;; Current cursor position
  10609. (linepos (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
  10610. (colpos col))
  10611. (goto-char beg)
  10612. (while (< (point) end)
  10613. (if (org-at-table-hline-p)
  10614. nil
  10615. (org-table-goto-column col t)
  10616. (and (looking-at "|[^|\n]+|")
  10617. (replace-match "|")))
  10618. (beginning-of-line 2))
  10619. (move-marker end nil)
  10620. (goto-line linepos)
  10621. (org-table-goto-column colpos)
  10622. (org-table-align)
  10623. (org-table-modify-formulas 'remove col)))
  10624. (defun org-table-move-column-right ()
  10625. "Move column to the right."
  10626. (interactive)
  10627. (org-table-move-column nil))
  10628. (defun org-table-move-column-left ()
  10629. "Move column to the left."
  10630. (interactive)
  10631. (org-table-move-column 'left))
  10632. (defun org-table-move-column (&optional left)
  10633. "Move the current column to the right. With arg LEFT, move to the left."
  10634. (interactive "P")
  10635. (if (not (org-at-table-p))
  10636. (error "Not at a table"))
  10637. (org-table-find-dataline)
  10638. (org-table-check-inside-data-field)
  10639. (let* ((col (org-table-current-column))
  10640. (col1 (if left (1- col) col))
  10641. (beg (org-table-begin))
  10642. (end (org-table-end))
  10643. ;; Current cursor position
  10644. (linepos (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
  10645. (colpos (if left (1- col) (1+ col))))
  10646. (if (and left (= col 1))
  10647. (error "Cannot move column further left"))
  10648. (if (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
  10649. (error "Cannot move column further right"))
  10650. (goto-char beg)
  10651. (while (< (point) end)
  10652. (if (org-at-table-hline-p)
  10653. nil
  10654. (org-table-goto-column col1 t)
  10655. (and (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
  10656. (replace-match "|\\2|\\1|")))
  10657. (beginning-of-line 2))
  10658. (move-marker end nil)
  10659. (goto-line linepos)
  10660. (org-table-goto-column colpos)
  10661. (org-table-align)
  10662. (org-table-modify-formulas 'swap col (if left (1- col) (1+ col)))))
  10663. (defun org-table-move-row-down ()
  10664. "Move table row down."
  10665. (interactive)
  10666. (org-table-move-row nil))
  10667. (defun org-table-move-row-up ()
  10668. "Move table row up."
  10669. (interactive)
  10670. (org-table-move-row 'up))
  10671. (defun org-table-move-row (&optional up)
  10672. "Move the current table line down. With arg UP, move it up."
  10673. (interactive "P")
  10674. (let ((col (current-column))
  10675. (pos (point))
  10676. (tonew (if up 0 2))
  10677. txt)
  10678. (beginning-of-line tonew)
  10679. (if (not (org-at-table-p))
  10680. (progn
  10681. (goto-char pos)
  10682. (error "Cannot move row further")))
  10683. (goto-char pos)
  10684. (beginning-of-line 1)
  10685. (setq pos (point))
  10686. (setq txt (buffer-substring (point) (1+ (point-at-eol))))
  10687. (delete-region (point) (1+ (point-at-eol)))
  10688. (beginning-of-line tonew)
  10689. (insert txt)
  10690. (beginning-of-line 0)
  10691. (move-to-column col)))
  10692. (defun org-table-insert-row (&optional arg)
  10693. "Insert a new row above the current line into the table.
  10694. With prefix ARG, insert below the current line."
  10695. (interactive "P")
  10696. (if (not (org-at-table-p))
  10697. (error "Not at a table"))
  10698. (let* ((line (buffer-substring (point-at-bol) (point-at-eol)))
  10699. (new (org-table-clean-line line)))
  10700. ;; Fix the first field if necessary
  10701. (if (string-match "^[ \t]*| *[#$] *|" line)
  10702. (setq new (replace-match (match-string 0 line) t t new)))
  10703. (beginning-of-line (if arg 2 1))
  10704. (let (org-table-may-need-update) (insert-before-markers new "\n"))
  10705. (beginning-of-line 0)
  10706. (re-search-forward "| ?" (point-at-eol) t)
  10707. (and org-table-may-need-update (org-table-align))))
  10708. (defun org-table-insert-hline (&optional arg)
  10709. "Insert a horizontal-line below the current line into the table.
  10710. With prefix ARG, insert above the current line."
  10711. (interactive "P")
  10712. (if (not (org-at-table-p))
  10713. (error "Not at a table"))
  10714. (let ((line (org-table-clean-line
  10715. (buffer-substring (point-at-bol) (point-at-eol))))
  10716. (col (current-column)))
  10717. (while (string-match "|\\( +\\)|" line)
  10718. (setq line (replace-match
  10719. (concat "+" (make-string (- (match-end 1) (match-beginning 1))
  10720. ?-) "|") t t line)))
  10721. (and (string-match "\\+" line) (setq line (replace-match "|" t t line)))
  10722. (beginning-of-line (if arg 1 2))
  10723. (insert line "\n")
  10724. (beginning-of-line (if arg 1 -1))
  10725. (move-to-column col)))
  10726. (defun org-table-clean-line (s)
  10727. "Convert a table line S into a string with only \"|\" and space.
  10728. In particular, this does handle wide and invisible characters."
  10729. (if (string-match "^[ \t]*|-" s)
  10730. ;; It's a hline, just map the characters
  10731. (setq s (mapconcat (lambda (x) (if (member x '(?| ?+)) "|" " ")) s ""))
  10732. (while (string-match "|\\([ \t]*?[^ \t\r\n|][^\r\n|]*\\)|" s)
  10733. (setq s (replace-match
  10734. (concat "|" (make-string (org-string-width (match-string 1 s))
  10735. ?\ ) "|")
  10736. t t s)))
  10737. s))
  10738. (defun org-table-kill-row ()
  10739. "Delete the current row or horizontal line from the table."
  10740. (interactive)
  10741. (if (not (org-at-table-p))
  10742. (error "Not at a table"))
  10743. (let ((col (current-column)))
  10744. (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
  10745. (if (not (org-at-table-p)) (beginning-of-line 0))
  10746. (move-to-column col)))
  10747. (defun org-table-sort-lines (beg end numericp)
  10748. "Sort table lines in region.
  10749. Point and mark define the first and last line to include. Both point and
  10750. mark should be in the column that is used for sorting. For example, to
  10751. sort according to column 3, put the mark in the first line to sort, in
  10752. table column 3. Put point into the last line to be included in the sorting,
  10753. also in table column 3. The command will prompt for the sorting method
  10754. \(n for numerical, a for alphanumeric)."
  10755. (interactive "r\nsSorting method: [n]=numeric [a]=alpha: ")
  10756. (setq numericp (string-match "[nN]" numericp))
  10757. (org-table-align) ;; Just to be safe
  10758. (let* (bcol ecol cmp column lns)
  10759. (goto-char beg)
  10760. (org-table-check-inside-data-field)
  10761. (setq column (org-table-current-column)
  10762. beg (move-marker (make-marker) (point-at-bol)))
  10763. (goto-char end)
  10764. (org-table-check-inside-data-field)
  10765. (setq end (move-marker (make-marker) (1+ (point-at-eol))))
  10766. (untabify beg end)
  10767. (goto-char beg)
  10768. (org-table-goto-column column)
  10769. (skip-chars-backward "^|")
  10770. (setq bcol (current-column))
  10771. (org-table-goto-column (1+ column))
  10772. (skip-chars-backward "^|")
  10773. (setq ecol (1- (current-column)))
  10774. (setq cmp (if numericp
  10775. (lambda (a b) (< (car a) (car b)))
  10776. (lambda (a b) (string< (car a) (car b)))))
  10777. (setq lns (mapcar (lambda(x) (cons (org-trim (substring x bcol ecol)) x))
  10778. (org-split-string (buffer-substring beg end) "\n")))
  10779. (if numericp
  10780. (setq lns (mapcar (lambda(x)
  10781. (cons (string-to-number (car x)) (cdr x)))
  10782. lns)))
  10783. (delete-region beg end)
  10784. (move-marker beg nil)
  10785. (move-marker end nil)
  10786. (insert (mapconcat 'cdr (setq lns (sort lns cmp)) "\n") "\n")
  10787. (message "%d lines sorted %s based on column %d"
  10788. (length lns)
  10789. (if numericp "numerically" "alphabetically") column)))
  10790. (defun org-table-cut-region (beg end)
  10791. "Copy region in table to the clipboard and blank all relevant fields."
  10792. (interactive "r")
  10793. (org-table-copy-region beg end 'cut))
  10794. (defun org-table-copy-region (beg end &optional cut)
  10795. "Copy rectangular region in table to clipboard.
  10796. A special clipboard is used which can only be accessed
  10797. with `org-table-paste-rectangle'."
  10798. (interactive "rP")
  10799. (let* (l01 c01 l02 c02 l1 c1 l2 c2 ic1 ic2
  10800. region cols
  10801. (rpl (if cut " " nil)))
  10802. (goto-char beg)
  10803. (org-table-check-inside-data-field)
  10804. (setq l01 (count-lines (point-min) (point))
  10805. c01 (org-table-current-column))
  10806. (goto-char end)
  10807. (org-table-check-inside-data-field)
  10808. (setq l02 (count-lines (point-min) (point))
  10809. c02 (org-table-current-column))
  10810. (setq l1 (min l01 l02) l2 (max l01 l02)
  10811. c1 (min c01 c02) c2 (max c01 c02))
  10812. (catch 'exit
  10813. (while t
  10814. (catch 'nextline
  10815. (if (> l1 l2) (throw 'exit t))
  10816. (goto-line l1)
  10817. (if (org-at-table-hline-p) (throw 'nextline (setq l1 (1+ l1))))
  10818. (setq cols nil ic1 c1 ic2 c2)
  10819. (while (< ic1 (1+ ic2))
  10820. (push (org-table-get-field ic1 rpl) cols)
  10821. (setq ic1 (1+ ic1)))
  10822. (push (nreverse cols) region)
  10823. (setq l1 (1+ l1)))))
  10824. (setq org-table-clip (nreverse region))
  10825. (if cut (org-table-align))
  10826. org-table-clip))
  10827. (defun org-table-paste-rectangle ()
  10828. "Paste a rectangular region into a table.
  10829. The upper right corner ends up in the current field. All involved fields
  10830. will be overwritten. If the rectangle does not fit into the present table,
  10831. the table is enlarged as needed. The process ignores horizontal separator
  10832. lines."
  10833. (interactive)
  10834. (unless (and org-table-clip (listp org-table-clip))
  10835. (error "First cut/copy a region to paste!"))
  10836. (org-table-check-inside-data-field)
  10837. (let* ((clip org-table-clip)
  10838. (line (count-lines (point-min) (point)))
  10839. (col (org-table-current-column))
  10840. (org-enable-table-editor t)
  10841. (org-table-automatic-realign nil)
  10842. c cols field)
  10843. (while (setq cols (pop clip))
  10844. (while (org-at-table-hline-p) (beginning-of-line 2))
  10845. (if (not (org-at-table-p))
  10846. (progn (end-of-line 0) (org-table-next-field)))
  10847. (setq c col)
  10848. (while (setq field (pop cols))
  10849. (org-table-goto-column c nil 'force)
  10850. (org-table-get-field nil field)
  10851. (setq c (1+ c)))
  10852. (beginning-of-line 2))
  10853. (goto-line line)
  10854. (org-table-goto-column col)
  10855. (org-table-align)))
  10856. (defun org-table-convert ()
  10857. "Convert from `org-mode' table to table.el and back.
  10858. Obviously, this only works within limits. When an Org-mode table is
  10859. converted to table.el, all horizontal separator lines get lost, because
  10860. table.el uses these as cell boundaries and has no notion of horizontal lines.
  10861. A table.el table can be converted to an Org-mode table only if it does not
  10862. do row or column spanning. Multiline cells will become multiple cells.
  10863. Beware, Org-mode does not test if the table can be successfully converted - it
  10864. blindly applies a recipe that works for simple tables."
  10865. (interactive)
  10866. (require 'table)
  10867. (if (org-at-table.el-p)
  10868. ;; convert to Org-mode table
  10869. (let ((beg (move-marker (make-marker) (org-table-begin t)))
  10870. (end (move-marker (make-marker) (org-table-end t))))
  10871. (table-unrecognize-region beg end)
  10872. (goto-char beg)
  10873. (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
  10874. (replace-match ""))
  10875. (goto-char beg))
  10876. (if (org-at-table-p)
  10877. ;; convert to table.el table
  10878. (let ((beg (move-marker (make-marker) (org-table-begin)))
  10879. (end (move-marker (make-marker) (org-table-end))))
  10880. ;; first, get rid of all horizontal lines
  10881. (goto-char beg)
  10882. (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t)
  10883. (replace-match ""))
  10884. ;; insert a hline before first
  10885. (goto-char beg)
  10886. (org-table-insert-hline 'above)
  10887. (beginning-of-line -1)
  10888. ;; insert a hline after each line
  10889. (while (progn (beginning-of-line 3) (< (point) end))
  10890. (org-table-insert-hline))
  10891. (goto-char beg)
  10892. (setq end (move-marker end (org-table-end)))
  10893. ;; replace "+" at beginning and ending of hlines
  10894. (while (re-search-forward "^\\([ \t]*\\)|-" end t)
  10895. (replace-match "\\1+-"))
  10896. (goto-char beg)
  10897. (while (re-search-forward "-|[ \t]*$" end t)
  10898. (replace-match "-+"))
  10899. (goto-char beg)))))
  10900. (defun org-table-wrap-region (arg)
  10901. "Wrap several fields in a column like a paragraph.
  10902. This is useful if you'd like to spread the contents of a field over several
  10903. lines, in order to keep the table compact.
  10904. If there is an active region, and both point and mark are in the same column,
  10905. the text in the column is wrapped to minimum width for the given number of
  10906. lines. Generally, this makes the table more compact. A prefix ARG may be
  10907. used to change the number of desired lines. For example, `C-2 \\[org-table-wrap]'
  10908. formats the selected text to two lines. If the region was longer than two
  10909. lines, the remaining lines remain empty. A negative prefix argument reduces
  10910. the current number of lines by that amount. The wrapped text is pasted back
  10911. into the table. If you formatted it to more lines than it was before, fields
  10912. further down in the table get overwritten - so you might need to make space in
  10913. the table first.
  10914. If there is no region, the current field is split at the cursor position and
  10915. the text fragment to the right of the cursor is prepended to the field one
  10916. line down.
  10917. If there is no region, but you specify a prefix ARG, the current field gets
  10918. blank, and the content is appended to the field above."
  10919. (interactive "P")
  10920. (org-table-check-inside-data-field)
  10921. (if (org-region-active-p)
  10922. ;; There is a region: fill as a paragraph
  10923. (let ((beg (region-beginning))
  10924. nlines)
  10925. (org-table-cut-region (region-beginning) (region-end))
  10926. (if (> (length (car org-table-clip)) 1)
  10927. (error "Region must be limited to single column"))
  10928. (setq nlines (if arg
  10929. (if (< arg 1)
  10930. (+ (length org-table-clip) arg)
  10931. arg)
  10932. (length org-table-clip)))
  10933. (setq org-table-clip
  10934. (mapcar 'list (org-wrap (mapconcat 'car org-table-clip " ")
  10935. nil nlines)))
  10936. (goto-char beg)
  10937. (org-table-paste-rectangle))
  10938. ;; No region, split the current field at point
  10939. (if arg
  10940. ;; combine with field above
  10941. (let ((s (org-table-blank-field))
  10942. (col (org-table-current-column)))
  10943. (beginning-of-line 0)
  10944. (while (org-at-table-hline-p) (beginning-of-line 0))
  10945. (org-table-goto-column col)
  10946. (skip-chars-forward "^|")
  10947. (skip-chars-backward " ")
  10948. (insert " " (org-trim s))
  10949. (org-table-align))
  10950. ;; split field
  10951. (when (looking-at "\\([^|]+\\)+|")
  10952. (let ((s (match-string 1)))
  10953. (replace-match " |")
  10954. (goto-char (match-beginning 0))
  10955. (org-table-next-row)
  10956. (insert (org-trim s) " ")
  10957. (org-table-align))))))
  10958. (defvar org-field-marker nil)
  10959. (defun org-table-edit-field (arg)
  10960. "Edit table field in a different window.
  10961. This is mainly useful for fields that contain hidden parts.
  10962. When called with a \\[universal-argument] prefix, just make the full field visible so that
  10963. it can be edited in place."
  10964. (interactive "P")
  10965. (if arg
  10966. (let ((b (save-excursion (skip-chars-backward "^|") (point)))
  10967. (e (save-excursion (skip-chars-forward "^|\r\n") (point))))
  10968. (remove-text-properties b e '(org-cwidth t invisible t
  10969. display t intangible t))
  10970. (if (and (boundp 'font-lock-mode) font-lock-mode)
  10971. (font-lock-fontify-block)))
  10972. (let ((pos (move-marker (make-marker) (point)))
  10973. (field (org-table-get-field))
  10974. (cw (current-window-configuration))
  10975. p)
  10976. (switch-to-buffer-other-window "*Org tmp*")
  10977. (erase-buffer)
  10978. (insert "#\n# Edit field and finish with C-c C-c\n#\n")
  10979. (org-mode)
  10980. (goto-char (setq p (point-max)))
  10981. (insert (org-trim field))
  10982. (remove-text-properties p (point-max)
  10983. '(invisible t org-cwidth t display t
  10984. intangible t))
  10985. (goto-char p)
  10986. (org-set-local 'org-finish-function
  10987. 'org-table-finish-edit-field)
  10988. (org-set-local 'org-window-configuration cw)
  10989. (org-set-local 'org-field-marker pos)
  10990. (message "Edit and finish with C-c C-c"))))
  10991. (defun org-table-finish-edit-field ()
  10992. "Finish editing a table data field.
  10993. Remove all newline characters, insert the result into the table, realign
  10994. the table and kill the editing buffer."
  10995. (let ((pos org-field-marker)
  10996. (cw org-window-configuration)
  10997. (cb (current-buffer))
  10998. text)
  10999. (goto-char (point-min))
  11000. (while (re-search-forward "^#.*\n?" nil t) (replace-match ""))
  11001. (while (re-search-forward "\\([ \t]*\n[ \t]*\\)+" nil t)
  11002. (replace-match " "))
  11003. (setq text (org-trim (buffer-string)))
  11004. (set-window-configuration cw)
  11005. (kill-buffer cb)
  11006. (select-window (get-buffer-window (marker-buffer pos)))
  11007. (goto-char pos)
  11008. (move-marker pos nil)
  11009. (org-table-check-inside-data-field)
  11010. (org-table-get-field nil text)
  11011. (org-table-align)
  11012. (message "New field value inserted")))
  11013. (defun org-trim (s)
  11014. "Remove whitespace at beginning and end of string."
  11015. (if (string-match "^[ \t]+" s) (setq s (replace-match "" t t s)))
  11016. (if (string-match "[ \t]+$" s) (setq s (replace-match "" t t s)))
  11017. s)
  11018. (defun org-wrap (string &optional width lines)
  11019. "Wrap string to either a number of lines, or a width in characters.
  11020. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  11021. that costs. If there is a word longer than WIDTH, the text is actually
  11022. wrapped to the length of that word.
  11023. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  11024. many lines, whatever width that takes.
  11025. The return value is a list of lines, without newlines at the end."
  11026. (let* ((words (org-split-string string "[ \t\n]+"))
  11027. (maxword (apply 'max (mapcar 'org-string-width words)))
  11028. w ll)
  11029. (cond (width
  11030. (org-do-wrap words (max maxword width)))
  11031. (lines
  11032. (setq w maxword)
  11033. (setq ll (org-do-wrap words maxword))
  11034. (if (<= (length ll) lines)
  11035. ll
  11036. (setq ll words)
  11037. (while (> (length ll) lines)
  11038. (setq w (1+ w))
  11039. (setq ll (org-do-wrap words w)))
  11040. ll))
  11041. (t (error "Cannot wrap this")))))
  11042. (defun org-do-wrap (words width)
  11043. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  11044. (let (lines line)
  11045. (while words
  11046. (setq line (pop words))
  11047. (while (and words (< (+ (length line) (length (car words))) width))
  11048. (setq line (concat line " " (pop words))))
  11049. (setq lines (push line lines)))
  11050. (nreverse lines)))
  11051. (defun org-split-string (string &optional separators)
  11052. "Splits STRING into substrings at SEPARATORS.
  11053. No empty strings are returned if there are matches at the beginning
  11054. and end of string."
  11055. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  11056. (start 0)
  11057. notfirst
  11058. (list nil))
  11059. (while (and (string-match rexp string
  11060. (if (and notfirst
  11061. (= start (match-beginning 0))
  11062. (< start (length string)))
  11063. (1+ start) start))
  11064. (< (match-beginning 0) (length string)))
  11065. (setq notfirst t)
  11066. (or (eq (match-beginning 0) 0)
  11067. (and (eq (match-beginning 0) (match-end 0))
  11068. (eq (match-beginning 0) start))
  11069. (setq list
  11070. (cons (substring string start (match-beginning 0))
  11071. list)))
  11072. (setq start (match-end 0)))
  11073. (or (eq start (length string))
  11074. (setq list
  11075. (cons (substring string start)
  11076. list)))
  11077. (nreverse list)))
  11078. (defun org-table-map-tables (function)
  11079. "Apply FUNCTION to the start of all tables in the buffer."
  11080. (save-excursion
  11081. (save-restriction
  11082. (widen)
  11083. (goto-char (point-min))
  11084. (while (re-search-forward org-table-any-line-regexp nil t)
  11085. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  11086. (beginning-of-line 1)
  11087. (if (looking-at org-table-line-regexp)
  11088. (save-excursion (funcall function)))
  11089. (re-search-forward org-table-any-border-regexp nil 1))))
  11090. (message "Mapping tables: done"))
  11091. (defun org-table-sum (&optional beg end nlast)
  11092. "Sum numbers in region of current table column.
  11093. The result will be displayed in the echo area, and will be available
  11094. as kill to be inserted with \\[yank].
  11095. If there is an active region, it is interpreted as a rectangle and all
  11096. numbers in that rectangle will be summed. If there is no active
  11097. region and point is located in a table column, sum all numbers in that
  11098. column.
  11099. If at least one number looks like a time HH:MM or HH:MM:SS, all other
  11100. numbers are assumed to be times as well (in decimal hours) and the
  11101. numbers are added as such.
  11102. If NLAST is a number, only the NLAST fields will actually be summed."
  11103. (interactive)
  11104. (save-excursion
  11105. (let (col (timecnt 0) diff h m s org-table-clip)
  11106. (cond
  11107. ((and beg end)) ; beg and end given explicitly
  11108. ((org-region-active-p)
  11109. (setq beg (region-beginning) end (region-end)))
  11110. (t
  11111. (setq col (org-table-current-column))
  11112. (goto-char (org-table-begin))
  11113. (unless (re-search-forward "^[ \t]*|[^-]" nil t)
  11114. (error "No table data"))
  11115. (org-table-goto-column col)
  11116. ;not needed? (skip-chars-backward "^|")
  11117. (setq beg (point))
  11118. (goto-char (org-table-end))
  11119. (unless (re-search-backward "^[ \t]*|[^-]" nil t)
  11120. (error "No table data"))
  11121. (org-table-goto-column col)
  11122. ;not needed? (skip-chars-forward "^|")
  11123. (setq end (point))))
  11124. (let* ((items (apply 'append (org-table-copy-region beg end)))
  11125. (items1 (cond ((not nlast) items)
  11126. ((>= nlast (length items)) items)
  11127. (t (setq items (reverse items))
  11128. (setcdr (nthcdr (1- nlast) items) nil)
  11129. (nreverse items))))
  11130. (numbers (delq nil (mapcar 'org-table-get-number-for-summing
  11131. items1)))
  11132. (res (apply '+ numbers))
  11133. (sres (if (= timecnt 0)
  11134. (format "%g" res)
  11135. (setq diff (* 3600 res)
  11136. h (floor (/ diff 3600)) diff (mod diff 3600)
  11137. m (floor (/ diff 60)) diff (mod diff 60)
  11138. s diff)
  11139. (format "%d:%02d:%02d" h m s))))
  11140. (kill-new sres)
  11141. (if (interactive-p)
  11142. (message "%s"
  11143. (substitute-command-keys
  11144. (format "Sum of %d items: %-20s (\\[yank] will insert result into buffer)"
  11145. (length numbers) sres))))
  11146. sres))))
  11147. (defun org-table-get-number-for-summing (s)
  11148. (let (n)
  11149. (if (string-match "^ *|? *" s)
  11150. (setq s (replace-match "" nil nil s)))
  11151. (if (string-match " *|? *$" s)
  11152. (setq s (replace-match "" nil nil s)))
  11153. (setq n (string-to-number s))
  11154. (cond
  11155. ((and (string-match "0" s)
  11156. (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
  11157. ((string-match "\\`[ \t]+\\'" s) nil)
  11158. ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
  11159. (let ((h (string-to-number (or (match-string 1 s) "0")))
  11160. (m (string-to-number (or (match-string 2 s) "0")))
  11161. (s (string-to-number (or (match-string 4 s) "0"))))
  11162. (if (boundp 'timecnt) (setq timecnt (1+ timecnt)))
  11163. (* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
  11164. ((equal n 0) nil)
  11165. (t n))))
  11166. (defun org-table-get-vertical-vector (desc &optional tbeg col)
  11167. "Get a calc vector from a column, accorting to descriptor DESC.
  11168. Optional arguments TBEG and COL can give the beginning of the table and
  11169. the current column, to avoid unnecessary parsing."
  11170. (save-excursion
  11171. (or tbeg (setq tbeg (org-table-begin)))
  11172. (or col (setq col (org-table-current-column)))
  11173. (let (beg end nn n n1 n2 l (thisline (org-current-line)) hline-list)
  11174. (cond
  11175. ((string-match "\\(I+\\)\\(-\\(I+\\)\\)?" desc)
  11176. (setq n1 (- (match-end 1) (match-beginning 1)))
  11177. (if (match-beginning 3)
  11178. (setq n2 (- (match-end 2) (match-beginning 3))))
  11179. (setq n (if n2 (max n1 n2) n1))
  11180. (setq n1 (if n2 (min n1 n2)))
  11181. (setq nn n)
  11182. (while (and (> nn 0)
  11183. (re-search-backward org-table-hline-regexp tbeg t))
  11184. (push (org-current-line) hline-list)
  11185. (setq nn (1- nn)))
  11186. (setq hline-list (nreverse hline-list))
  11187. (goto-line (nth (1- n) hline-list))
  11188. (when (re-search-forward org-table-dataline-regexp)
  11189. (org-table-goto-column col)
  11190. (setq beg (point)))
  11191. (goto-line (if n1 (nth (1- n1) hline-list) thisline))
  11192. (when (re-search-backward org-table-dataline-regexp)
  11193. (org-table-goto-column col)
  11194. (setq end (point)))
  11195. (setq l (apply 'append (org-table-copy-region beg end)))
  11196. (concat "[" (mapconcat (lambda (x) (setq x (org-trim x))
  11197. (if (equal x "") "0" x))
  11198. l ",") "]"))
  11199. ((string-match "\\([0-9]+\\)-\\([0-9]+\\)" desc)
  11200. (setq n1 (string-to-number (match-string 1 desc))
  11201. n2 (string-to-number (match-string 2 desc)))
  11202. (beginning-of-line 1)
  11203. (save-excursion
  11204. (when (re-search-backward org-table-dataline-regexp tbeg t n1)
  11205. (org-table-goto-column col)
  11206. (setq beg (point))))
  11207. (when (re-search-backward org-table-dataline-regexp tbeg t n2)
  11208. (org-table-goto-column col)
  11209. (setq end (point)))
  11210. (setq l (apply 'append (org-table-copy-region beg end)))
  11211. (concat "[" (mapconcat
  11212. (lambda (x) (setq x (org-trim x))
  11213. (if (equal x "") "0" x))
  11214. l ",") "]"))
  11215. ((string-match "\\([0-9]+\\)" desc)
  11216. (beginning-of-line 1)
  11217. (when (re-search-backward org-table-dataline-regexp tbeg t
  11218. (string-to-number (match-string 0 desc)))
  11219. (org-table-goto-column col)
  11220. (org-trim (org-table-get-field))))))))
  11221. (defvar org-table-formula-history nil)
  11222. (defvar org-table-column-names nil
  11223. "Alist with column names, derived from the `!' line.")
  11224. (defvar org-table-column-name-regexp nil
  11225. "Regular expression matching the current column names.")
  11226. (defvar org-table-local-parameters nil
  11227. "Alist with parameter names, derived from the `$' line.")
  11228. (defvar org-table-named-field-locations nil
  11229. "Alist with locations of named fields.")
  11230. (defun org-table-get-formula (&optional equation named)
  11231. "Read a formula from the minibuffer, offer stored formula as default."
  11232. (let* ((name (car (rassoc (list (org-current-line)
  11233. (org-table-current-column))
  11234. org-table-named-field-locations)))
  11235. (scol (if named
  11236. (if name name
  11237. (error "Not in a named field"))
  11238. (int-to-string (org-table-current-column))))
  11239. (dummy (and name (not named)
  11240. (not (y-or-n-p "Replace named-field formula with column equation? " ))
  11241. (error "Abort")))
  11242. (org-table-may-need-update nil)
  11243. (stored-list (org-table-get-stored-formulas))
  11244. (stored (cdr (assoc scol stored-list)))
  11245. (eq (cond
  11246. ((and stored equation (string-match "^ *=? *$" equation))
  11247. stored)
  11248. ((stringp equation)
  11249. equation)
  11250. (t (read-string
  11251. (format "%s formula $%s=" (if named "Field" "Column") scol)
  11252. (or stored "") 'org-table-formula-history
  11253. ;stored
  11254. ))))
  11255. mustsave)
  11256. (when (not (string-match "\\S-" eq))
  11257. ;; remove formula
  11258. (setq stored-list (delq (assoc scol stored-list) stored-list))
  11259. (org-table-store-formulas stored-list)
  11260. (error "Formula removed"))
  11261. (if (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
  11262. (if (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
  11263. (if (and name (not named))
  11264. ;; We set the column equation, delete the named one.
  11265. (setq stored-list (delq (assoc name stored-list) stored-list)
  11266. mustsave t))
  11267. (if stored
  11268. (setcdr (assoc scol stored-list) eq)
  11269. (setq stored-list (cons (cons scol eq) stored-list)))
  11270. (if (or mustsave (not (equal stored eq)))
  11271. (org-table-store-formulas stored-list))
  11272. eq))
  11273. (defun org-table-store-formulas (alist)
  11274. "Store the list of formulas below the current table."
  11275. (setq alist (sort alist (lambda (a b) (string< (car a) (car b)))))
  11276. (save-excursion
  11277. (goto-char (org-table-end))
  11278. (if (looking-at "\\([ \t]*\n\\)*#\\+TBLFM:.*\n?")
  11279. (delete-region (point) (match-end 0)))
  11280. (insert "#+TBLFM: "
  11281. (mapconcat (lambda (x)
  11282. (concat "$" (car x) "=" (cdr x)))
  11283. alist "::")
  11284. "\n")))
  11285. (defun org-table-get-stored-formulas ()
  11286. "Return an alist with the stored formulas directly after current table."
  11287. (interactive)
  11288. (let (scol eq eq-alist strings string seen)
  11289. (save-excursion
  11290. (goto-char (org-table-end))
  11291. (when (looking-at "\\([ \t]*\n\\)*#\\+TBLFM: *\\(.*\\)")
  11292. (setq strings (org-split-string (match-string 2) " *:: *"))
  11293. (while (setq string (pop strings))
  11294. (when (string-match "\\$\\([a-zA-Z0-9]+\\) *= *\\(.*[^ \t]\\)" string)
  11295. (setq scol (match-string 1 string)
  11296. eq (match-string 2 string)
  11297. eq-alist (cons (cons scol eq) eq-alist))
  11298. (if (member scol seen)
  11299. (error "Double definition `$%s=' in TBLFM line, please fix by hand" scol)
  11300. (push scol seen))))))
  11301. (nreverse eq-alist)))
  11302. (defun org-table-modify-formulas (action &rest columns)
  11303. "Modify the formulas stored below the current table.
  11304. ACTION can be `remove', `insert', `swap'. For `swap', two column numbers are
  11305. expected, for the other actions only a single column number is needed."
  11306. (let ((list (org-table-get-stored-formulas))
  11307. (nmax (length (org-split-string
  11308. (buffer-substring (point-at-bol) (point-at-eol))
  11309. "|")))
  11310. col col1 col2 scol si sc1 sc2)
  11311. (cond
  11312. ((null list)) ; No action needed if there are no stored formulas
  11313. ((eq action 'remove)
  11314. (setq col (car columns)
  11315. scol (int-to-string col))
  11316. (org-table-replace-in-formulas list scol "INVALID")
  11317. (if (assoc scol list) (setq list (delq (assoc scol list) list)))
  11318. (loop for i from (1+ col) upto nmax by 1 do
  11319. (setq si (int-to-string i))
  11320. (org-table-replace-in-formulas list si (int-to-string (1- i)))
  11321. (if (assoc si list) (setcar (assoc si list)
  11322. (int-to-string (1- i))))))
  11323. ((eq action 'insert)
  11324. (setq col (car columns))
  11325. (loop for i from nmax downto col by 1 do
  11326. (setq si (int-to-string i))
  11327. (org-table-replace-in-formulas list si (int-to-string (1+ i)))
  11328. (if (assoc si list) (setcar (assoc si list)
  11329. (int-to-string (1+ i))))))
  11330. ((eq action 'swap)
  11331. (setq col1 (car columns) col2 (nth 1 columns)
  11332. sc1 (int-to-string col1) sc2 (int-to-string col2))
  11333. ;; Hopefully, ZqZtZ will never be a name in a table
  11334. (org-table-replace-in-formulas list sc1 "ZqZtZ")
  11335. (org-table-replace-in-formulas list sc2 sc1)
  11336. (org-table-replace-in-formulas list "ZqZtZ" sc2)
  11337. (if (assoc sc1 list) (setcar (assoc sc1 list) "ZqZtZ"))
  11338. (if (assoc sc2 list) (setcar (assoc sc2 list) sc1))
  11339. (if (assoc "ZqZtZ" list) (setcar (assoc "ZqZtZ" list) sc2)))
  11340. (t (error "Invalid action in `org-table-modify-formulas'")))
  11341. (if list (org-table-store-formulas list))))
  11342. (defun org-table-replace-in-formulas (list s1 s2)
  11343. (let (elt re s)
  11344. (setq s1 (concat "$" (if (integerp s1) (int-to-string s1) s1))
  11345. s2 (concat "$" (if (integerp s2) (int-to-string s2) s2))
  11346. re (concat (regexp-quote s1) "\\>"))
  11347. (while (setq elt (pop list))
  11348. (setq s (cdr elt))
  11349. (while (string-match re s)
  11350. (setq s (replace-match s2 t t s)))
  11351. (setcdr elt s))))
  11352. (defun org-table-get-specials ()
  11353. "Get the column names and local parameters for this table."
  11354. (save-excursion
  11355. (let ((beg (org-table-begin)) (end (org-table-end))
  11356. names name fields fields1 field cnt c v line col)
  11357. (setq org-table-column-names nil
  11358. org-table-local-parameters nil
  11359. org-table-named-field-locations nil)
  11360. (goto-char beg)
  11361. (when (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t)
  11362. (setq names (org-split-string (match-string 1) " *| *")
  11363. cnt 1)
  11364. (while (setq name (pop names))
  11365. (setq cnt (1+ cnt))
  11366. (if (string-match "^[a-zA-Z][a-zA-Z0-9]*$" name)
  11367. (push (cons name (int-to-string cnt)) org-table-column-names))))
  11368. (setq org-table-column-names (nreverse org-table-column-names))
  11369. (setq org-table-column-name-regexp
  11370. (concat "\\$\\(" (mapconcat 'car org-table-column-names "\\|") "\\)\\>"))
  11371. (goto-char beg)
  11372. (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t)
  11373. (setq fields (org-split-string (match-string 1) " *| *"))
  11374. (while (setq field (pop fields))
  11375. (if (string-match "^\\([a-zA-Z][a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field)
  11376. (push (cons (match-string 1 field) (match-string 2 field))
  11377. org-table-local-parameters))))
  11378. (goto-char beg)
  11379. (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t)
  11380. (setq c (match-string 1)
  11381. fields (org-split-string (match-string 2) " *| *"))
  11382. (save-excursion
  11383. (beginning-of-line (if (equal c "_") 2 0))
  11384. (setq line (org-current-line) col 1)
  11385. (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
  11386. (setq fields1 (org-split-string (match-string 1) " *| *"))))
  11387. (while (and fields1 (setq field (pop fields)))
  11388. (setq v (pop fields1) col (1+ col))
  11389. (when (and (stringp field) (stringp v)
  11390. (string-match "^[a-zA-Z][a-zA-Z0-9]*$" field))
  11391. (push (cons field v) org-table-local-parameters)
  11392. (push (list field line col) org-table-named-field-locations)))))))
  11393. (defun org-this-word ()
  11394. ;; Get the current word
  11395. (save-excursion
  11396. (let ((beg (progn (skip-chars-backward "^ \t\n") (point)))
  11397. (end (progn (skip-chars-forward "^ \t\n") (point))))
  11398. (buffer-substring-no-properties beg end))))
  11399. (defun org-table-maybe-eval-formula ()
  11400. "Check if the current field starts with \"=\" or \":=\".
  11401. If yes, store the formula and apply it."
  11402. ;; We already know we are in a table. Get field will only return a formula
  11403. ;; when appropriate. It might return a separator line, but no problem.
  11404. (when org-table-formula-evaluate-inline
  11405. (let* ((field (org-trim (or (org-table-get-field) "")))
  11406. named eq)
  11407. (when (string-match "^:?=\\(.*\\)" field)
  11408. (setq named (equal (string-to-char field) ?:)
  11409. eq (match-string 1 field))
  11410. (if (fboundp 'calc-eval)
  11411. (org-table-eval-formula (if named '(4) nil) eq))))))
  11412. (defvar org-recalc-commands nil
  11413. "List of commands triggering the recalculation of a line.
  11414. Will be filled automatically during use.")
  11415. (defvar org-recalc-marks
  11416. '((" " . "Unmarked: no special line, no automatic recalculation")
  11417. ("#" . "Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
  11418. ("*" . "Recalculate only when entire table is recalculated with `C-u C-c *'")
  11419. ("!" . "Column name definition line. Reference in formula as $name.")
  11420. ("$" . "Parameter definition line name=value. Reference in formula as $name.")
  11421. ("_" . "Names for values in row below this one.")
  11422. ("^" . "Names for values in row above this one.")))
  11423. (defun org-table-rotate-recalc-marks (&optional newchar)
  11424. "Rotate the recalculation mark in the first column.
  11425. If in any row, the first field is not consistent with a mark,
  11426. insert a new column for the markers.
  11427. When there is an active region, change all the lines in the region,
  11428. after prompting for the marking character.
  11429. After each change, a message will be displayed indicating the meaning
  11430. of the new mark."
  11431. (interactive)
  11432. (unless (org-at-table-p) (error "Not at a table"))
  11433. (let* ((marks (append (mapcar 'car org-recalc-marks) '(" ")))
  11434. (beg (org-table-begin))
  11435. (end (org-table-end))
  11436. (l (org-current-line))
  11437. (l1 (if (org-region-active-p) (org-current-line (region-beginning))))
  11438. (l2 (if (org-region-active-p) (org-current-line (region-end))))
  11439. (have-col
  11440. (save-excursion
  11441. (goto-char beg)
  11442. (not (re-search-forward "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" end t))))
  11443. (col (org-table-current-column))
  11444. (forcenew (car (assoc newchar org-recalc-marks)))
  11445. epos new)
  11446. (when l1
  11447. (message "Change region to what mark? Type # * ! $ or SPC: ")
  11448. (setq newchar (char-to-string (read-char-exclusive))
  11449. forcenew (car (assoc newchar org-recalc-marks))))
  11450. (if (and newchar (not forcenew))
  11451. (error "Invalid NEWCHAR `%s' in `org-table-rotate-recalc-marks'"
  11452. newchar))
  11453. (if l1 (goto-line l1))
  11454. (save-excursion
  11455. (beginning-of-line 1)
  11456. (unless (looking-at org-table-dataline-regexp)
  11457. (error "Not at a table data line")))
  11458. (unless have-col
  11459. (org-table-goto-column 1)
  11460. (org-table-insert-column)
  11461. (org-table-goto-column (1+ col)))
  11462. (setq epos (point-at-eol))
  11463. (save-excursion
  11464. (beginning-of-line 1)
  11465. (org-table-get-field
  11466. 1 (if (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")
  11467. (concat " "
  11468. (setq new (or forcenew
  11469. (cadr (member (match-string 1) marks))))
  11470. " ")
  11471. " # ")))
  11472. (if (and l1 l2)
  11473. (progn
  11474. (goto-line l1)
  11475. (while (progn (beginning-of-line 2) (not (= (org-current-line) l2)))
  11476. (and (looking-at org-table-dataline-regexp)
  11477. (org-table-get-field 1 (concat " " new " "))))
  11478. (goto-line l1)))
  11479. (if (not (= epos (point-at-eol))) (org-table-align))
  11480. (goto-line l)
  11481. (and (interactive-p) (message (cdr (assoc new org-recalc-marks))))))
  11482. (defun org-table-maybe-recalculate-line ()
  11483. "Recompute the current line if marked for it, and if we haven't just done it."
  11484. (interactive)
  11485. (and org-table-allow-automatic-line-recalculation
  11486. (not (and (memq last-command org-recalc-commands)
  11487. (equal org-last-recalc-line (org-current-line))))
  11488. (save-excursion (beginning-of-line 1)
  11489. (looking-at org-table-auto-recalculate-regexp))
  11490. (fboundp 'calc-eval)
  11491. (org-table-recalculate) t))
  11492. (defvar org-table-formula-debug nil
  11493. "Non-nil means, debug table formulas.
  11494. When nil, simply write \"#ERROR\" in corrupted fields.")
  11495. (defvar modes)
  11496. (defsubst org-set-calc-mode (var &optional value)
  11497. (if (stringp var)
  11498. (setq var (assoc var '(("D" calc-angle-mode deg)
  11499. ("R" calc-angle-mode rad)
  11500. ("F" calc-prefer-frac t)
  11501. ("S" calc-symbolic-mode t)))
  11502. value (nth 2 var) var (nth 1 var)))
  11503. (if (memq var modes)
  11504. (setcar (cdr (memq var modes)) value)
  11505. (cons var (cons value modes)))
  11506. modes)
  11507. (defun org-table-eval-formula (&optional arg equation
  11508. suppress-align suppress-const
  11509. suppress-store)
  11510. "Replace the table field value at the cursor by the result of a calculation.
  11511. This function makes use of Dave Gillespie's Calc package, in my view the
  11512. most exciting program ever written for GNU Emacs. So you need to have Calc
  11513. installed in order to use this function.
  11514. In a table, this command replaces the value in the current field with the
  11515. result of a formula. It also installs the formula as the \"current\" column
  11516. formula, by storing it in a special line below the table. When called
  11517. with a `C-u' prefix, the current field must ba a named field, and the
  11518. formula is installed as valid in only this specific field.
  11519. When called, the command first prompts for a formula, which is read in
  11520. the minibuffer. Previously entered formulas are available through the
  11521. history list, and the last used formula is offered as a default.
  11522. These stored formulas are adapted correctly when moving, inserting, or
  11523. deleting columns with the corresponding commands.
  11524. The formula can be any algebraic expression understood by the Calc package.
  11525. For details, see the Org-mode manual.
  11526. This function can also be called from Lisp programs and offers
  11527. additional arguments: EQUATION can be the formula to apply. If this
  11528. argument is given, the user will not be prompted. SUPPRESS-ALIGN is
  11529. used to speed-up recursive calls by by-passing unnecessary aligns.
  11530. SUPPRESS-CONST suppresses the interpretation of constants in the
  11531. formula, assuming that this has been done already outside the function.
  11532. SUPPRESS-STORE means the formula should not be stored, either because
  11533. it is already stored, or because it is a modified equation that should
  11534. not overwrite the stored one."
  11535. (interactive "P")
  11536. (require 'calc)
  11537. (org-table-check-inside-data-field)
  11538. (org-table-get-specials)
  11539. (let* (fields
  11540. (ndown (if (integerp arg) arg 1))
  11541. (org-table-automatic-realign nil)
  11542. (case-fold-search nil)
  11543. (down (> ndown 1))
  11544. (formula (if (and equation suppress-store)
  11545. equation
  11546. (org-table-get-formula equation (equal arg '(4)))))
  11547. (n0 (org-table-current-column))
  11548. (modes (copy-sequence org-calc-default-modes))
  11549. n form fmt x ev orig c lispp)
  11550. ;; Parse the format string. Since we have a lot of modes, this is
  11551. ;; a lot of work. However, I think calc still uses most of the time.
  11552. (if (string-match ";" formula)
  11553. (let ((tmp (org-split-string formula ";")))
  11554. (setq formula (car tmp)
  11555. fmt (concat (cdr (assoc "%" org-table-local-parameters))
  11556. (nth 1 tmp)))
  11557. (while (string-match "\\([pnfse]\\)\\(-?[0-9]+\\)" fmt)
  11558. (setq c (string-to-char (match-string 1 fmt))
  11559. n (string-to-number (match-string 2 fmt)))
  11560. (if (= c ?p)
  11561. (setq modes (org-set-calc-mode 'calc-internal-prec n))
  11562. (setq modes (org-set-calc-mode
  11563. 'calc-float-format
  11564. (list (cdr (assoc c '((?n . float) (?f . fix)
  11565. (?s . sci) (?e . eng))))
  11566. n))))
  11567. (setq fmt (replace-match "" t t fmt)))
  11568. (while (string-match "[DRFS]" fmt)
  11569. (setq modes (org-set-calc-mode (match-string 0 fmt)))
  11570. (setq fmt (replace-match "" t t fmt)))
  11571. (unless (string-match "\\S-" fmt)
  11572. (setq fmt nil))))
  11573. (if (and (not suppress-const) org-table-formula-use-constants)
  11574. (setq formula (org-table-formula-substitute-names formula)))
  11575. (setq orig (or (get-text-property 1 :orig-formula formula) "?"))
  11576. (while (> ndown 0)
  11577. (setq fields (org-split-string
  11578. (buffer-substring
  11579. (point-at-bol) (point-at-eol)) " *| *"))
  11580. (if org-table-formula-numbers-only
  11581. (setq fields (mapcar
  11582. (lambda (x) (number-to-string (string-to-number x)))
  11583. fields)))
  11584. (setq ndown (1- ndown))
  11585. (setq form (copy-sequence formula)
  11586. lispp (equal (substring form 0 2) "'("))
  11587. ;; Insert the references to fields in same row
  11588. (while (string-match "\\$\\([0-9]+\\)?" form)
  11589. (setq n (if (match-beginning 1)
  11590. (string-to-number (match-string 1 form))
  11591. n0)
  11592. x (nth (1- n) fields))
  11593. (unless x (error "Invalid field specifier \"%s\""
  11594. (match-string 0 form)))
  11595. (if (equal x "") (setq x "0"))
  11596. (setq form (replace-match
  11597. (if lispp x (concat "(" x ")"))
  11598. t t form)))
  11599. ;; Insert ranges in current column
  11600. (while (string-match "\\&[-I0-9]+" form)
  11601. (setq form (replace-match
  11602. (save-match-data
  11603. (org-table-get-vertical-vector (match-string 0 form)
  11604. nil n0))
  11605. t t form)))
  11606. (if lispp
  11607. (setq ev (eval (eval (read form)))
  11608. ev (if (numberp ev) (number-to-string ev) ev))
  11609. (setq ev (calc-eval (cons form modes)
  11610. (if org-table-formula-numbers-only 'num))))
  11611. (when org-table-formula-debug
  11612. (with-output-to-temp-buffer "*Help*"
  11613. (princ (format "Substitution history of formula
  11614. Orig: %s
  11615. $xyz-> %s
  11616. $1-> %s\n" orig formula form))
  11617. (if (listp ev)
  11618. (princ (format " %s^\nError: %s"
  11619. (make-string (car ev) ?\-) (nth 1 ev)))
  11620. (princ (format "Result: %s\nFormat: %s\nFinal: %s"
  11621. ev (or fmt "NONE")
  11622. (if fmt (format fmt (string-to-number ev)) ev)))))
  11623. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  11624. (unless (and (interactive-p) (not ndown))
  11625. (unless (let (inhibit-redisplay)
  11626. (y-or-n-p "Debugging Formula. Continue to next? "))
  11627. (org-table-align)
  11628. (error "Abort"))
  11629. (delete-window (get-buffer-window "*Help*"))
  11630. (message "")))
  11631. (if (listp ev) (setq fmt nil ev "#ERROR"))
  11632. (org-table-justify-field-maybe
  11633. (if fmt (format fmt (string-to-number ev)) ev))
  11634. (if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
  11635. (call-interactively 'org-return)
  11636. (setq ndown 0)))
  11637. (and down (org-table-maybe-recalculate-line))
  11638. (or suppress-align (and org-table-may-need-update
  11639. (org-table-align)))))
  11640. (defun org-table-recalculate (&optional all noalign)
  11641. "Recalculate the current table line by applying all stored formulas.
  11642. With prefix arg ALL, do this for all lines in the table."
  11643. (interactive "P")
  11644. (or (memq this-command org-recalc-commands)
  11645. (setq org-recalc-commands (cons this-command org-recalc-commands)))
  11646. (unless (org-at-table-p) (error "Not at a table"))
  11647. (org-table-get-specials)
  11648. (let* ((eqlist (sort (org-table-get-stored-formulas)
  11649. (lambda (a b) (string< (car a) (car b)))))
  11650. (inhibit-redisplay t)
  11651. (line-re org-table-dataline-regexp)
  11652. (thisline (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
  11653. (thiscol (org-table-current-column))
  11654. beg end entry eqlnum eqlname eql (cnt 0) eq a name)
  11655. ;; Insert constants in all formulas
  11656. (setq eqlist
  11657. (mapcar (lambda (x)
  11658. (setcdr x (org-table-formula-substitute-names (cdr x)))
  11659. x)
  11660. eqlist))
  11661. ;; Split the equation list
  11662. (while (setq eq (pop eqlist))
  11663. (if (<= (string-to-char (car eq)) ?9)
  11664. (push eq eqlnum)
  11665. (push eq eqlname)))
  11666. (setq eqlnum (nreverse eqlnum) eqlname (nreverse eqlname))
  11667. (if all
  11668. (progn
  11669. (setq end (move-marker (make-marker) (1+ (org-table-end))))
  11670. (goto-char (setq beg (org-table-begin)))
  11671. (if (re-search-forward org-table-calculate-mark-regexp end t)
  11672. ;; This is a table with marked lines, only compute selected lines
  11673. (setq line-re org-table-recalculate-regexp)
  11674. ;; Move forward to the first non-header line
  11675. (if (and (re-search-forward org-table-dataline-regexp end t)
  11676. (re-search-forward org-table-hline-regexp end t)
  11677. (re-search-forward org-table-dataline-regexp end t))
  11678. (setq beg (match-beginning 0))
  11679. nil))) ;; just leave beg where it is
  11680. (setq beg (point-at-bol)
  11681. end (move-marker (make-marker) (1+ (point-at-eol)))))
  11682. (goto-char beg)
  11683. (and all (message "Re-applying formulas to full table..."))
  11684. (while (re-search-forward line-re end t)
  11685. (unless (string-match "^ *[_^!$] *$" (org-table-get-field 1))
  11686. ;; Unprotected line, recalculate
  11687. (and all (message "Re-applying formulas to full table...(line %d)"
  11688. (setq cnt (1+ cnt))))
  11689. (setq org-last-recalc-line (org-current-line))
  11690. (setq eql eqlnum)
  11691. (while (setq entry (pop eql))
  11692. (goto-line org-last-recalc-line)
  11693. (org-table-goto-column (string-to-number (car entry)) nil 'force)
  11694. (org-table-eval-formula nil (cdr entry) 'noalign 'nocst 'nostore))))
  11695. (goto-line thisline)
  11696. (org-table-goto-column thiscol)
  11697. (or noalign (and org-table-may-need-update (org-table-align))
  11698. (and all (message "Re-applying formulas to %d lines...done" cnt)))
  11699. ;; Now do the names fields
  11700. (while (setq eq (pop eqlname))
  11701. (setq name (car eq)
  11702. a (assoc name org-table-named-field-locations))
  11703. (when a
  11704. (message "Re-applying formula to named field: %s" name)
  11705. (goto-line (nth 1 a))
  11706. (org-table-goto-column (nth 2 a))
  11707. (org-table-eval-formula nil (cdr eq) 'noalign 'nocst 'nostore)))
  11708. ;; back to initial position
  11709. (goto-line thisline)
  11710. (org-table-goto-column thiscol)
  11711. (or noalign (and org-table-may-need-update (org-table-align))
  11712. (and all (message "Re-applying formulas...done")))))
  11713. (defun org-table-formula-substitute-names (f)
  11714. "Replace $const with values in string F."
  11715. (let ((start 0) a n1 n2 nn1 nn2 s (f1 f))
  11716. ;; First, check for column names
  11717. (while (setq start (string-match org-table-column-name-regexp f start))
  11718. (setq start (1+ start))
  11719. (setq a (assoc (match-string 1 f) org-table-column-names))
  11720. (setq f (replace-match (concat "$" (cdr a)) t t f)))
  11721. ;; Expand ranges to vectors
  11722. (while (string-match "\\$\\([0-9]+\\)\\.\\.\\.?\\$\\([0-9]+\\)" f)
  11723. (setq n1 (string-to-number (match-string 1 f))
  11724. n2 (string-to-number (match-string 2 f))
  11725. nn1 (1+ (min n1 n2)) nn2 (max n1 n2)
  11726. s (concat "[($" (number-to-string (1- nn1)) ")"))
  11727. (loop for i from nn1 upto nn2 do
  11728. (setq s (concat s ",($" (int-to-string i) ")")))
  11729. (setq s (concat s "]"))
  11730. (if (< n2 n1) (setq s (concat "rev(" s ")")))
  11731. (setq f (replace-match s t t f)))
  11732. ;; Parameters and constants
  11733. (setq start 0)
  11734. (while (setq start (string-match "\\$\\([a-zA-Z][a-zA-Z0-9]*\\)" f start))
  11735. (setq start (1+ start))
  11736. (if (setq a (save-match-data
  11737. (org-table-get-constant (match-string 1 f))))
  11738. (setq f (replace-match (concat "(" a ")") t t f))))
  11739. (if org-table-formula-debug
  11740. (put-text-property 0 (length f) :orig-formula f1 f))
  11741. f))
  11742. (defun org-table-get-constant (const)
  11743. "Find the value for a parameter or constant in a formula.
  11744. Parameters get priority."
  11745. (or (cdr (assoc const org-table-local-parameters))
  11746. (cdr (assoc const org-table-formula-constants))
  11747. (and (fboundp 'constants-get) (constants-get const))
  11748. "#UNDEFINED_NAME"))
  11749. (defvar org-edit-formulas-map (make-sparse-keymap))
  11750. (define-key org-edit-formulas-map "\C-c\C-c" 'org-finish-edit-formulas)
  11751. (define-key org-edit-formulas-map "\C-c\C-q" 'org-abort-edit-formulas)
  11752. (define-key org-edit-formulas-map "\C-c?" 'org-show-variable)
  11753. (defvar org-pos)
  11754. (defun org-table-edit-formulas ()
  11755. "Edit the formulas of the current table in a separate buffer."
  11756. (interactive)
  11757. (unless (org-at-table-p)
  11758. (error "Not at a table"))
  11759. (org-table-get-specials)
  11760. (let ((eql (org-table-get-stored-formulas))
  11761. (pos (move-marker (make-marker) (point)))
  11762. (wc (current-window-configuration))
  11763. entry loc s)
  11764. (switch-to-buffer-other-window "*Edit Formulas*")
  11765. (erase-buffer)
  11766. (fundamental-mode)
  11767. (org-set-local 'org-pos pos)
  11768. (org-set-local 'org-window-configuration wc)
  11769. (use-local-map org-edit-formulas-map)
  11770. (setq s "# Edit formulas and finish with `C-c C-c'.
  11771. # Use `C-u C-c C-c' to also appy them immediately to the entire table.
  11772. # Use `C-c ?' to get information about $name at point.
  11773. # To cancel editing, press `C-c C-q'.\n")
  11774. (put-text-property 0 (length s) 'face 'font-lock-comment-face s)
  11775. (insert s)
  11776. (while (setq entry (pop eql))
  11777. (when (setq loc (assoc (car entry) org-table-named-field-locations))
  11778. (setq s (format "# Named formula, referring to column %d in line %d\n"
  11779. (nth 2 loc) (nth 1 loc)))
  11780. (put-text-property 0 (length s) 'face 'font-lock-comment-face s)
  11781. (insert s))
  11782. (setq s (concat "$" (car entry) "=" (cdr entry) "\n"))
  11783. (remove-text-properties 0 (length s) '(face nil) s)
  11784. (insert s))
  11785. (goto-char (point-min))
  11786. (message "Edit formulas and finish with `C-c C-c'.")))
  11787. (defun org-show-variable ()
  11788. "Show the location/value of the $ expression at point."
  11789. (interactive)
  11790. (let (var (pos org-pos) (win (selected-window)) e)
  11791. (save-excursion
  11792. (or (looking-at "\\$") (skip-chars-backward "$a-zA-Z0-9"))
  11793. (if (looking-at "\\$\\([a-zA-Z0-9]+\\)")
  11794. (setq var (match-string 1))
  11795. (error "No variable at point")))
  11796. (cond
  11797. ((setq e (assoc var org-table-named-field-locations))
  11798. (switch-to-buffer-other-window (marker-buffer pos))
  11799. (goto-line (nth 1 e))
  11800. (org-table-goto-column (nth 2 e))
  11801. (select-window win)
  11802. (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
  11803. ((setq e (assoc var org-table-column-names))
  11804. (switch-to-buffer-other-window (marker-buffer pos))
  11805. (goto-char pos)
  11806. (goto-char (org-table-begin))
  11807. (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
  11808. (org-table-end) t)
  11809. (progn
  11810. (goto-char (match-beginning 1))
  11811. (message "Named column (column %s)" (cdr e)))
  11812. (error "Column name not found"))
  11813. (select-window win))
  11814. ((string-match "^[0-9]$" var)
  11815. ;; column number
  11816. (switch-to-buffer-other-window (marker-buffer pos))
  11817. (goto-char pos)
  11818. (goto-char (org-table-begin))
  11819. (recenter 1)
  11820. (if (re-search-forward org-table-dataline-regexp
  11821. (org-table-end) t)
  11822. (progn
  11823. (goto-char (match-beginning 0))
  11824. (org-table-goto-column (string-to-number var))
  11825. (message "Column %s" var))
  11826. (error "Column name not found"))
  11827. (select-window win))
  11828. ((setq e (assoc var org-table-local-parameters))
  11829. (switch-to-buffer-other-window (marker-buffer pos))
  11830. (goto-char pos)
  11831. (goto-char (org-table-begin))
  11832. (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
  11833. (progn
  11834. (goto-char (match-beginning 1))
  11835. (message "Local parameter."))
  11836. (error "Parameter not found"))
  11837. (select-window win))
  11838. (t
  11839. (cond
  11840. ((setq e (assoc var org-table-formula-constants))
  11841. (message "Constant: $%s=%s in `org-table-formula-constants'." var (cdr e)))
  11842. ((setq e (and (fboundp 'constants-get) (constants-get var)))
  11843. (message "Constant: $%s=%s, retrieved from `constants.el'." var e))
  11844. (t (error "Undefined name $%s" var)))))))
  11845. (defun org-finish-edit-formulas (&optional arg)
  11846. "Parse the buffer for formula definitions and install them.
  11847. With prefix ARG, apply the new formulas to the table."
  11848. (interactive "P")
  11849. (let ((pos org-pos) eql)
  11850. (goto-char (point-min))
  11851. (while (re-search-forward "^\\$\\([a-zA-Z0-9]+\\) *= *\\(.*?\\) *$" nil t)
  11852. (push (cons (match-string 1) (match-string 2)) eql))
  11853. (set-window-configuration org-window-configuration)
  11854. (select-window (get-buffer-window (marker-buffer pos)))
  11855. (goto-char pos)
  11856. (unless (org-at-table-p)
  11857. (error "Lost table position - cannot install formulae"))
  11858. (org-table-store-formulas eql)
  11859. (move-marker pos nil)
  11860. (kill-buffer "*Edit Formulas*")
  11861. (if arg
  11862. (org-table-recalculate 'all)
  11863. (message "New formulas installed - press C-u C-c C-c to apply."))))
  11864. (defun org-abort-edit-formulas ()
  11865. "Abort editing formulas, without installing the changes."
  11866. (interactive)
  11867. (let ((pos org-pos))
  11868. (set-window-configuration org-window-configuration)
  11869. (select-window (get-buffer-window (marker-buffer pos)))
  11870. (goto-char pos)
  11871. (message "Formula editing aborted without installing changes")))
  11872. ;;; The orgtbl minor mode
  11873. ;; Define a minor mode which can be used in other modes in order to
  11874. ;; integrate the org-mode table editor.
  11875. ;; This is really a hack, because the org-mode table editor uses several
  11876. ;; keys which normally belong to the major mode, for example the TAB and
  11877. ;; RET keys. Here is how it works: The minor mode defines all the keys
  11878. ;; necessary to operate the table editor, but wraps the commands into a
  11879. ;; function which tests if the cursor is currently inside a table. If that
  11880. ;; is the case, the table editor command is executed. However, when any of
  11881. ;; those keys is used outside a table, the function uses `key-binding' to
  11882. ;; look up if the key has an associated command in another currently active
  11883. ;; keymap (minor modes, major mode, global), and executes that command.
  11884. ;; There might be problems if any of the keys used by the table editor is
  11885. ;; otherwise used as a prefix key.
  11886. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  11887. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  11888. ;; addresses this by checking explicitly for both bindings.
  11889. ;; The optimized version (see variable `orgtbl-optimized') takes over
  11890. ;; all keys which are bound to `self-insert-command' in the *global map*.
  11891. ;; Some modes bind other commands to simple characters, for example
  11892. ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
  11893. ;; active, this binding is ignored inside tables and replaced with a
  11894. ;; modified self-insert.
  11895. (defvar orgtbl-mode nil
  11896. "Variable controlling `orgtbl-mode', a minor mode enabling the `org-mode'
  11897. table editor in arbitrary modes.")
  11898. (make-variable-buffer-local 'orgtbl-mode)
  11899. (defvar orgtbl-mode-map (make-keymap)
  11900. "Keymap for `orgtbl-mode'.")
  11901. ;;;###autoload
  11902. (defun turn-on-orgtbl ()
  11903. "Unconditionally turn on `orgtbl-mode'."
  11904. (orgtbl-mode 1))
  11905. ;;;###autoload
  11906. (defun orgtbl-mode (&optional arg)
  11907. "The `org-mode' table editor as a minor mode for use in other modes."
  11908. (interactive)
  11909. (if (org-mode-p)
  11910. ;; Exit without error, in case some hook functions calls this
  11911. ;; by accident in org-mode.
  11912. (message "Orgtbl-mode is not useful in org-mode, command ignored")
  11913. (setq orgtbl-mode
  11914. (if arg (> (prefix-numeric-value arg) 0) (not orgtbl-mode)))
  11915. (if orgtbl-mode
  11916. (progn
  11917. (and (orgtbl-setup) (defun orgtbl-setup () nil))
  11918. ;; Make sure we are first in minor-mode-map-alist
  11919. (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
  11920. (and c (setq minor-mode-map-alist
  11921. (cons c (delq c minor-mode-map-alist)))))
  11922. (org-set-local (quote org-table-may-need-update) t)
  11923. (org-add-hook 'before-change-functions 'org-before-change-function
  11924. nil 'local)
  11925. (org-set-local 'org-old-auto-fill-inhibit-regexp
  11926. auto-fill-inhibit-regexp)
  11927. (org-set-local 'auto-fill-inhibit-regexp
  11928. (if auto-fill-inhibit-regexp
  11929. (concat "\\([ \t]*|\\|" auto-fill-inhibit-regexp)
  11930. "[ \t]*|"))
  11931. (org-add-to-invisibility-spec '(org-cwidth))
  11932. (easy-menu-add orgtbl-mode-menu)
  11933. (run-hooks 'orgtbl-mode-hook))
  11934. (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
  11935. (org-cleanup-narrow-column-properties)
  11936. (org-remove-from-invisibility-spec '(org-cwidth))
  11937. (remove-hook 'before-change-functions 'org-before-change-function t)
  11938. (easy-menu-remove orgtbl-mode-menu)
  11939. (force-mode-line-update 'all))))
  11940. (defun org-cleanup-narrow-column-properties ()
  11941. "Remove all properties related to narrow-column invisibility."
  11942. (let ((s 1))
  11943. (while (setq s (text-property-any s (point-max)
  11944. 'display org-narrow-column-arrow))
  11945. (remove-text-properties s (1+ s) '(display t)))
  11946. (setq s 1)
  11947. (while (setq s (text-property-any s (point-max) 'org-cwidth 1))
  11948. (remove-text-properties s (1+ s) '(org-cwidth t)))
  11949. (setq s 1)
  11950. (while (setq s (text-property-any s (point-max) 'invisible 'org-cwidth))
  11951. (remove-text-properties s (1+ s) '(invisible t)))))
  11952. ;; Install it as a minor mode.
  11953. (put 'orgtbl-mode :included t)
  11954. (put 'orgtbl-mode :menu-tag "Org Table Mode")
  11955. (add-minor-mode 'orgtbl-mode " OrgTbl" orgtbl-mode-map)
  11956. (defun orgtbl-make-binding (fun n &rest keys)
  11957. "Create a function for binding in the table minor mode.
  11958. FUN is the command to call inside a table. N is used to create a unique
  11959. command name. KEYS are keys that should be checked in for a command
  11960. to execute outside of tables."
  11961. (eval
  11962. (list 'defun
  11963. (intern (concat "orgtbl-hijacker-command-" (int-to-string n)))
  11964. '(arg)
  11965. (concat "In tables, run `" (symbol-name fun) "'.\n"
  11966. "Outside of tables, run the binding of `"
  11967. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  11968. "'.")
  11969. '(interactive "p")
  11970. (list 'if
  11971. '(org-at-table-p)
  11972. (list 'call-interactively (list 'quote fun))
  11973. (list 'let '(orgtbl-mode)
  11974. (list 'call-interactively
  11975. (append '(or)
  11976. (mapcar (lambda (k)
  11977. (list 'key-binding k))
  11978. keys)
  11979. '('orgtbl-error))))))))
  11980. (defun orgtbl-error ()
  11981. "Error when there is no default binding for a table key."
  11982. (interactive)
  11983. (error "This key is has no function outside tables"))
  11984. (defun orgtbl-setup ()
  11985. "Setup orgtbl keymaps."
  11986. (let ((nfunc 0)
  11987. (bindings
  11988. (list
  11989. '([(meta shift left)] org-table-delete-column)
  11990. '([(meta left)] org-table-move-column-left)
  11991. '([(meta right)] org-table-move-column-right)
  11992. '([(meta shift right)] org-table-insert-column)
  11993. '([(meta shift up)] org-table-kill-row)
  11994. '([(meta shift down)] org-table-insert-row)
  11995. '([(meta up)] org-table-move-row-up)
  11996. '([(meta down)] org-table-move-row-down)
  11997. '("\C-c\C-w" org-table-cut-region)
  11998. '("\C-c\M-w" org-table-copy-region)
  11999. '("\C-c\C-y" org-table-paste-rectangle)
  12000. '("\C-c-" org-table-insert-hline)
  12001. ; '([(shift tab)] org-table-previous-field)
  12002. '("\C-m" org-table-next-row)
  12003. (list (org-key 'S-return) 'org-table-copy-down)
  12004. '([(meta return)] org-table-wrap-region)
  12005. '("\C-c\C-q" org-table-wrap-region)
  12006. '("\C-c?" org-table-current-column)
  12007. '("\C-c " org-table-blank-field)
  12008. '("\C-c+" org-table-sum)
  12009. '("\C-c=" org-table-eval-formula)
  12010. '("\C-c'" org-table-edit-formulas)
  12011. '("\C-c`" org-table-edit-field)
  12012. '("\C-c*" org-table-recalculate)
  12013. '("\C-c|" org-table-create-or-convert-from-region)
  12014. '("\C-c^" org-table-sort-lines)
  12015. '([(control ?#)] org-table-rotate-recalc-marks)))
  12016. elt key fun cmd)
  12017. (while (setq elt (pop bindings))
  12018. (setq nfunc (1+ nfunc))
  12019. (setq key (car elt)
  12020. fun (nth 1 elt)
  12021. cmd (orgtbl-make-binding fun nfunc key))
  12022. (define-key orgtbl-mode-map key cmd))
  12023. ;; Special treatment needed for TAB and RET
  12024. (define-key orgtbl-mode-map [(return)]
  12025. (orgtbl-make-binding 'orgtbl-ret 100 [(return)] "\C-m"))
  12026. (define-key orgtbl-mode-map "\C-m"
  12027. (orgtbl-make-binding 'orgtbl-ret 101 "\C-m" [(return)]))
  12028. (define-key orgtbl-mode-map [(tab)]
  12029. (orgtbl-make-binding 'orgtbl-tab 102 [(tab)] "\C-i"))
  12030. (define-key orgtbl-mode-map "\C-i"
  12031. (orgtbl-make-binding 'orgtbl-tab 103 "\C-i" [(tab)])))
  12032. (define-key orgtbl-mode-map "\C-i"
  12033. (orgtbl-make-binding 'orgtbl-tab 104 [(shift tab)]))
  12034. (define-key orgtbl-mode-map "\C-c\C-c"
  12035. (orgtbl-make-binding 'org-ctrl-c-ctrl-c 105 "\C-c\C-c"))
  12036. (when orgtbl-optimized
  12037. ;; If the user wants maximum table support, we need to hijack
  12038. ;; some standard editing functions
  12039. (org-remap orgtbl-mode-map
  12040. 'self-insert-command 'orgtbl-self-insert-command
  12041. 'delete-char 'org-delete-char
  12042. 'delete-backward-char 'org-delete-backward-char)
  12043. (define-key orgtbl-mode-map "|" 'org-force-self-insert))
  12044. (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
  12045. '("OrgTbl"
  12046. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
  12047. ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
  12048. ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
  12049. ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
  12050. "--"
  12051. ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
  12052. ["Edit Field" org-table-edit-field :active (org-at-table-p) :keys "C-c ` "]
  12053. ["Copy Field from Above"
  12054. org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
  12055. "--"
  12056. ("Column"
  12057. ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
  12058. ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
  12059. ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
  12060. ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"]
  12061. "--"
  12062. ["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])
  12063. ("Row"
  12064. ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
  12065. ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
  12066. ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
  12067. ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
  12068. ["Sort lines in region" org-table-sort-lines (org-at-table-p) :keys "C-c ^"]
  12069. "--"
  12070. ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
  12071. ("Rectangle"
  12072. ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
  12073. ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
  12074. ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
  12075. ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
  12076. "--"
  12077. ["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
  12078. ["Set Named Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  12079. ["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
  12080. ["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
  12081. ["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
  12082. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
  12083. ["Sum Column/Rectangle" org-table-sum
  12084. :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
  12085. ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
  12086. ["Debug Formulas"
  12087. (setq org-table-formula-debug (not org-table-formula-debug))
  12088. :style toggle :selected org-table-formula-debug]
  12089. ))
  12090. t)
  12091. (defun orgtbl-tab (arg)
  12092. "Justification and field motion for `orgtbl-mode'."
  12093. (interactive "P")
  12094. (if arg (org-table-edit-field t)
  12095. (org-table-justify-field-maybe)
  12096. (org-table-next-field)))
  12097. (defun orgtbl-ret ()
  12098. "Justification and field motion for `orgtbl-mode'."
  12099. (interactive)
  12100. (org-table-justify-field-maybe)
  12101. (org-table-next-row))
  12102. (defun orgtbl-self-insert-command (N)
  12103. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  12104. If the cursor is in a table looking at whitespace, the whitespace is
  12105. overwritten, and the table is not marked as requiring realignment."
  12106. (interactive "p")
  12107. (if (and (org-at-table-p)
  12108. (or
  12109. (and org-table-auto-blank-field
  12110. (member last-command
  12111. '(orgtbl-hijacker-command-100
  12112. orgtbl-hijacker-command-101
  12113. orgtbl-hijacker-command-102
  12114. orgtbl-hijacker-command-103
  12115. orgtbl-hijacker-command-104
  12116. orgtbl-hijacker-command-105))
  12117. (org-table-blank-field))
  12118. t)
  12119. (eq N 1)
  12120. (looking-at "[^|\n]* +|"))
  12121. (let (org-table-may-need-update)
  12122. (goto-char (1- (match-end 0)))
  12123. (delete-backward-char 1)
  12124. (goto-char (match-beginning 0))
  12125. (self-insert-command N))
  12126. (setq org-table-may-need-update t)
  12127. (let (orgtbl-mode)
  12128. (call-interactively (key-binding (vector last-input-event))))))
  12129. (defun org-force-self-insert (N)
  12130. "Needed to enforce self-insert under remapping."
  12131. (interactive "p")
  12132. (self-insert-command N))
  12133. ;;; Exporting
  12134. (defconst org-level-max 20)
  12135. (defvar org-export-html-preamble nil
  12136. "Preamble, to be inserted just after <body>. Set by publishing functions.")
  12137. (defvar org-export-html-postamble nil
  12138. "Preamble, to be inserted just before </body>. Set by publishing functions.")
  12139. (defvar org-export-html-auto-preamble t
  12140. "Should default preamble be inserted? Set by publishing functions.")
  12141. (defvar org-export-html-auto-postamble t
  12142. "Should default postamble be inserted? Set by publishing functions.")
  12143. (defconst org-export-plist-vars
  12144. '((:language . org-export-default-language)
  12145. (:headline-levels . org-export-headline-levels)
  12146. (:section-numbers . org-export-with-section-numbers)
  12147. (:table-of-contents . org-export-with-toc)
  12148. (:archived-trees . org-export-with-archived-trees)
  12149. (:emphasize . org-export-with-emphasize)
  12150. (:sub-superscript . org-export-with-sub-superscripts)
  12151. (:TeX-macros . org-export-with-TeX-macros)
  12152. (:LaTeX-fragments . org-export-with-LaTeX-fragments)
  12153. (:fixed-width . org-export-with-fixed-width)
  12154. (:timestamps . org-export-with-timestamps)
  12155. (:tables . org-export-with-tables)
  12156. (:table-auto-headline . org-export-highlight-first-table-line)
  12157. (:style . org-export-html-style)
  12158. (:convert-org-links . org-export-html-link-org-files-as-html)
  12159. (:inline-images . org-export-html-inline-images)
  12160. (:expand-quoted-html . org-export-html-expand)
  12161. (:timestamp . org-export-html-with-timestamp)
  12162. (:publishing-directory . org-export-publishing-directory)
  12163. (:preamble . org-export-html-preamble)
  12164. (:postamble . org-export-html-postamble)
  12165. (:auto-preamble . org-export-html-auto-preamble)
  12166. (:auto-postamble . org-export-html-auto-postamble)
  12167. (:author . user-full-name)
  12168. (:email . user-mail-address)))
  12169. (defun org-default-export-plist ()
  12170. "Return the property list with default settings for the export variables."
  12171. (let ((l org-export-plist-vars) rtn e)
  12172. (while (setq e (pop l))
  12173. (setq rtn (cons (car e) (cons (symbol-value (cdr e)) rtn))))
  12174. rtn))
  12175. (defun org-infile-export-plist ()
  12176. "Return the property list with file-local settings for export."
  12177. (save-excursion
  12178. (goto-char 0)
  12179. (let ((re (org-make-options-regexp
  12180. '("TITLE" "AUTHOR" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE")))
  12181. p key val text options)
  12182. (while (re-search-forward re nil t)
  12183. (setq key (org-match-string-no-properties 1)
  12184. val (org-match-string-no-properties 2))
  12185. (cond
  12186. ((string-equal key "TITLE") (setq p (plist-put p :title val)))
  12187. ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
  12188. ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
  12189. ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
  12190. ((string-equal key "TEXT")
  12191. (setq text (if text (concat text "\n" val) val)))
  12192. ((string-equal key "OPTIONS") (setq options val))))
  12193. (setq p (plist-put p :text text))
  12194. (when options
  12195. (let ((op '(("H" . :headline-levels)
  12196. ("num" . :section-numbers)
  12197. ("toc" . :table-of-contents)
  12198. ("\\n" . :preserve-breaks)
  12199. ("@" . :expand-quoted-html)
  12200. (":" . :fixed-width)
  12201. ("|" . :tables)
  12202. ("^" . :sub-superscript)
  12203. ("*" . :emphasize)
  12204. ("TeX" . :TeX-macros)
  12205. ("LaTeX" . :LaTeX-fragments)))
  12206. o)
  12207. (while (setq o (pop op))
  12208. (if (string-match (concat (regexp-quote (car o))
  12209. ":\\([^ \t\n\r;,.]*\\)")
  12210. options)
  12211. (setq p (plist-put p (cdr o)
  12212. (car (read-from-string
  12213. (match-string 1 options)))))))))
  12214. p)))
  12215. (defun org-combine-plists (&rest plists)
  12216. "Create a single property list from all plists in PLISTS.
  12217. The process starts by copying the last list, and then setting properties
  12218. from the other lists. Settings in the first list are the most significant
  12219. ones and overrule settings in the other lists."
  12220. (let ((rtn (copy-sequence (pop plists)))
  12221. p v ls)
  12222. (while plists
  12223. (setq ls (pop plists))
  12224. (while ls
  12225. (setq p (pop ls) v (pop ls))
  12226. (setq rtn (plist-put rtn p v))))
  12227. rtn))
  12228. (defun org-export-directory (type plist)
  12229. (let* ((val (plist-get plist :publishing-directory))
  12230. (dir (if (listp val)
  12231. (or (cdr (assoc type val)) ".")
  12232. val)))
  12233. dir))
  12234. (defun org-export-find-first-heading-line (list)
  12235. "Remove all lines from LIST which are before the first headline."
  12236. (let ((orig-list list)
  12237. (re (concat "^" outline-regexp)))
  12238. (while (and list
  12239. (not (string-match re (car list))))
  12240. (pop list))
  12241. (or list orig-list)))
  12242. (defun org-skip-comments (lines)
  12243. "Skip lines starting with \"#\" and subtrees starting with COMMENT."
  12244. (let ((re1 (concat "^\\(\\*+\\)[ \t]+" org-comment-string))
  12245. (re2 "^\\(\\*+\\)[ \t\n\r]")
  12246. rtn line level)
  12247. (while (setq line (pop lines))
  12248. (cond
  12249. ((and (string-match re1 line)
  12250. (setq level (- (match-end 1) (match-beginning 1))))
  12251. ;; Beginning of a COMMENT subtree. Skip it.
  12252. (while (and (setq line (pop lines))
  12253. (or (not (string-match re2 line))
  12254. (> (- (match-end 1) (match-beginning 1)) level))))
  12255. (setq lines (cons line lines)))
  12256. ((string-match "^#" line)
  12257. ;; an ordinary comment line
  12258. )
  12259. ((and org-export-table-remove-special-lines
  12260. (string-match "^[ \t]*|" line)
  12261. (or (string-match "^[ \t]*| *[!_^] *|" line)
  12262. (and (string-match "| *<[0-9]+> *|" line)
  12263. (not (string-match "| *[^ <|]" line)))))
  12264. ;; a special table line that should be removed
  12265. )
  12266. (t (setq rtn (cons line rtn)))))
  12267. (nreverse rtn)))
  12268. (defun org-export (&optional arg)
  12269. (interactive)
  12270. (let ((help "[t] insert the export option template
  12271. \[v] limit export to visible part of outline tree
  12272. \[a] export as ASCII
  12273. \[h] export as HTML
  12274. \[b] export as HTML and browse immediately
  12275. \[x] export as XOXO
  12276. \[i] export current file as iCalendar file
  12277. \[I] export all agenda files as iCalendar files
  12278. \[c] export agenda files into combined iCalendar file
  12279. \[F] publish current file
  12280. \[P] publish current project
  12281. \[X] publish... (project will be prompted for)
  12282. \[A] publish all projects")
  12283. (cmds
  12284. '((?t . org-insert-export-options-template)
  12285. (?v . org-export-visible)
  12286. (?a . org-export-as-ascii)
  12287. (?h . org-export-as-html)
  12288. (?b . org-export-as-html-and-open)
  12289. (?x . org-export-as-xoxo)
  12290. (?i . org-export-icalendar-this-file)
  12291. (?I . org-export-icalendar-all-agenda-files)
  12292. (?c . org-export-icalendar-combine-agenda-files)
  12293. (?F . org-publish-current-file)
  12294. (?P . org-publish-current-project)
  12295. (?X . org-publish)
  12296. (?A . org-publish-all)))
  12297. r1 r2 ass)
  12298. (save-window-excursion
  12299. (delete-other-windows)
  12300. (with-output-to-temp-buffer "*Org Export/Publishing Help*"
  12301. (princ help))
  12302. (message "Select command: ")
  12303. (setq r1 (read-char-exclusive)))
  12304. (setq r2 (if (< r1 27) (+ r1 96) r1))
  12305. (if (setq ass (assq r2 cmds))
  12306. (call-interactively (cdr ass))
  12307. (error "No command associated with key %c" r1))))
  12308. ;; ASCII
  12309. (defconst org-html-entities
  12310. '(("nbsp")
  12311. ("iexcl")
  12312. ("cent")
  12313. ("pound")
  12314. ("curren")
  12315. ("yen")
  12316. ("brvbar")
  12317. ("sect")
  12318. ("uml")
  12319. ("copy")
  12320. ("ordf")
  12321. ("laquo")
  12322. ("not")
  12323. ("shy")
  12324. ("reg")
  12325. ("macr")
  12326. ("deg")
  12327. ("plusmn")
  12328. ("sup2")
  12329. ("sup3")
  12330. ("acute")
  12331. ("micro")
  12332. ("para")
  12333. ("middot")
  12334. ("odot"."o")
  12335. ("star"."*")
  12336. ("cedil")
  12337. ("sup1")
  12338. ("ordm")
  12339. ("raquo")
  12340. ("frac14")
  12341. ("frac12")
  12342. ("frac34")
  12343. ("iquest")
  12344. ("Agrave")
  12345. ("Aacute")
  12346. ("Acirc")
  12347. ("Atilde")
  12348. ("Auml")
  12349. ("Aring") ("AA"."&Aring;")
  12350. ("AElig")
  12351. ("Ccedil")
  12352. ("Egrave")
  12353. ("Eacute")
  12354. ("Ecirc")
  12355. ("Euml")
  12356. ("Igrave")
  12357. ("Iacute")
  12358. ("Icirc")
  12359. ("Iuml")
  12360. ("ETH")
  12361. ("Ntilde")
  12362. ("Ograve")
  12363. ("Oacute")
  12364. ("Ocirc")
  12365. ("Otilde")
  12366. ("Ouml")
  12367. ("times")
  12368. ("Oslash")
  12369. ("Ugrave")
  12370. ("Uacute")
  12371. ("Ucirc")
  12372. ("Uuml")
  12373. ("Yacute")
  12374. ("THORN")
  12375. ("szlig")
  12376. ("agrave")
  12377. ("aacute")
  12378. ("acirc")
  12379. ("atilde")
  12380. ("auml")
  12381. ("aring")
  12382. ("aelig")
  12383. ("ccedil")
  12384. ("egrave")
  12385. ("eacute")
  12386. ("ecirc")
  12387. ("euml")
  12388. ("igrave")
  12389. ("iacute")
  12390. ("icirc")
  12391. ("iuml")
  12392. ("eth")
  12393. ("ntilde")
  12394. ("ograve")
  12395. ("oacute")
  12396. ("ocirc")
  12397. ("otilde")
  12398. ("ouml")
  12399. ("divide")
  12400. ("oslash")
  12401. ("ugrave")
  12402. ("uacute")
  12403. ("ucirc")
  12404. ("uuml")
  12405. ("yacute")
  12406. ("thorn")
  12407. ("yuml")
  12408. ("fnof")
  12409. ("Alpha")
  12410. ("Beta")
  12411. ("Gamma")
  12412. ("Delta")
  12413. ("Epsilon")
  12414. ("Zeta")
  12415. ("Eta")
  12416. ("Theta")
  12417. ("Iota")
  12418. ("Kappa")
  12419. ("Lambda")
  12420. ("Mu")
  12421. ("Nu")
  12422. ("Xi")
  12423. ("Omicron")
  12424. ("Pi")
  12425. ("Rho")
  12426. ("Sigma")
  12427. ("Tau")
  12428. ("Upsilon")
  12429. ("Phi")
  12430. ("Chi")
  12431. ("Psi")
  12432. ("Omega")
  12433. ("alpha")
  12434. ("beta")
  12435. ("gamma")
  12436. ("delta")
  12437. ("epsilon")
  12438. ("varepsilon"."&epsilon;")
  12439. ("zeta")
  12440. ("eta")
  12441. ("theta")
  12442. ("iota")
  12443. ("kappa")
  12444. ("lambda")
  12445. ("mu")
  12446. ("nu")
  12447. ("xi")
  12448. ("omicron")
  12449. ("pi")
  12450. ("rho")
  12451. ("sigmaf") ("varsigma"."&sigmaf;")
  12452. ("sigma")
  12453. ("tau")
  12454. ("upsilon")
  12455. ("phi")
  12456. ("chi")
  12457. ("psi")
  12458. ("omega")
  12459. ("thetasym") ("vartheta"."&thetasym;")
  12460. ("upsih")
  12461. ("piv")
  12462. ("bull") ("bullet"."&bull;")
  12463. ("hellip") ("dots"."&hellip;")
  12464. ("prime")
  12465. ("Prime")
  12466. ("oline")
  12467. ("frasl")
  12468. ("weierp")
  12469. ("image")
  12470. ("real")
  12471. ("trade")
  12472. ("alefsym")
  12473. ("larr") ("leftarrow"."&larr;") ("gets"."&larr;")
  12474. ("uarr") ("uparrow"."&uarr;")
  12475. ("rarr") ("to"."&rarr;") ("rightarrow"."&rarr;")
  12476. ("darr")("downarrow"."&darr;")
  12477. ("harr") ("leftrightarrow"."&harr;")
  12478. ("crarr") ("hookleftarrow"."&crarr;") ; has round hook, not quite CR
  12479. ("lArr") ("Leftarrow"."&lArr;")
  12480. ("uArr") ("Uparrow"."&uArr;")
  12481. ("rArr") ("Rightarrow"."&rArr;")
  12482. ("dArr") ("Downarrow"."&dArr;")
  12483. ("hArr") ("Leftrightarrow"."&hArr;")
  12484. ("forall")
  12485. ("part") ("partial"."&part;")
  12486. ("exist") ("exists"."&exist;")
  12487. ("empty") ("emptyset"."&empty;")
  12488. ("nabla")
  12489. ("isin") ("in"."&isin;")
  12490. ("notin")
  12491. ("ni")
  12492. ("prod")
  12493. ("sum")
  12494. ("minus")
  12495. ("lowast") ("ast"."&lowast;")
  12496. ("radic")
  12497. ("prop") ("proptp"."&prop;")
  12498. ("infin") ("infty"."&infin;")
  12499. ("ang") ("angle"."&ang;")
  12500. ("and") ("vee"."&and;")
  12501. ("or") ("wedge"."&or;")
  12502. ("cap")
  12503. ("cup")
  12504. ("int")
  12505. ("there4")
  12506. ("sim")
  12507. ("cong") ("simeq"."&cong;")
  12508. ("asymp")("approx"."&asymp;")
  12509. ("ne") ("neq"."&ne;")
  12510. ("equiv")
  12511. ("le")
  12512. ("ge")
  12513. ("sub") ("subset"."&sub;")
  12514. ("sup") ("supset"."&sup;")
  12515. ("nsub")
  12516. ("sube")
  12517. ("supe")
  12518. ("oplus")
  12519. ("otimes")
  12520. ("perp")
  12521. ("sdot") ("cdot"."&sdot;")
  12522. ("lceil")
  12523. ("rceil")
  12524. ("lfloor")
  12525. ("rfloor")
  12526. ("lang")
  12527. ("rang")
  12528. ("loz") ("Diamond"."&loz;")
  12529. ("spades") ("spadesuit"."&spades;")
  12530. ("clubs") ("clubsuit"."&clubs;")
  12531. ("hearts") ("diamondsuit"."&hearts;")
  12532. ("diams") ("diamondsuit"."&diams;")
  12533. ("quot")
  12534. ("amp")
  12535. ("lt")
  12536. ("gt")
  12537. ("OElig")
  12538. ("oelig")
  12539. ("Scaron")
  12540. ("scaron")
  12541. ("Yuml")
  12542. ("circ")
  12543. ("tilde")
  12544. ("ensp")
  12545. ("emsp")
  12546. ("thinsp")
  12547. ("zwnj")
  12548. ("zwj")
  12549. ("lrm")
  12550. ("rlm")
  12551. ("ndash")
  12552. ("mdash")
  12553. ("lsquo")
  12554. ("rsquo")
  12555. ("sbquo")
  12556. ("ldquo")
  12557. ("rdquo")
  12558. ("bdquo")
  12559. ("dagger")
  12560. ("Dagger")
  12561. ("permil")
  12562. ("lsaquo")
  12563. ("rsaquo")
  12564. ("euro")
  12565. ("arccos"."arccos")
  12566. ("arcsin"."arcsin")
  12567. ("arctan"."arctan")
  12568. ("arg"."arg")
  12569. ("cos"."cos")
  12570. ("cosh"."cosh")
  12571. ("cot"."cot")
  12572. ("coth"."coth")
  12573. ("csc"."csc")
  12574. ("deg"."deg")
  12575. ("det"."det")
  12576. ("dim"."dim")
  12577. ("exp"."exp")
  12578. ("gcd"."gcd")
  12579. ("hom"."hom")
  12580. ("inf"."inf")
  12581. ("ker"."ker")
  12582. ("lg"."lg")
  12583. ("lim"."lim")
  12584. ("liminf"."liminf")
  12585. ("limsup"."limsup")
  12586. ("ln"."ln")
  12587. ("log"."log")
  12588. ("max"."max")
  12589. ("min"."min")
  12590. ("Pr"."Pr")
  12591. ("sec"."sec")
  12592. ("sin"."sin")
  12593. ("sinh"."sinh")
  12594. ("sup"."sup")
  12595. ("tan"."tan")
  12596. ("tanh"."tanh")
  12597. )
  12598. "Entities for TeX->HTML translation.
  12599. Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
  12600. \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
  12601. In that case, \"\\ent\" will be translated to \"&other;\".
  12602. The list contains HTML entities for Latin-1, Greek and other symbols.
  12603. It is supplemented by a number of commonly used TeX macros with appropriate
  12604. translations. There is currently no way for users to extend this.")
  12605. (defun org-cleaned-string-for-export (string &rest parameters)
  12606. "Cleanup a buffer substring so that links can be created safely."
  12607. (interactive)
  12608. (let* ((re-radio (and org-target-link-regexp
  12609. (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)")))
  12610. (re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
  12611. (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
  12612. (re-archive (concat ":" org-archive-tag ":"))
  12613. rtn)
  12614. (save-excursion
  12615. (set-buffer (get-buffer-create " org-mode-tmp"))
  12616. (erase-buffer)
  12617. (insert string)
  12618. (let ((org-inhibit-startup t)) (org-mode))
  12619. ;; Get rid of archived trees
  12620. (when (not (eq org-export-with-archived-trees t))
  12621. (goto-char (point-min))
  12622. (while (re-search-forward re-archive nil t)
  12623. (if (not (org-on-heading-p))
  12624. (org-end-of-subtree t)
  12625. (beginning-of-line 1)
  12626. (delete-region
  12627. (if org-export-with-archived-trees (1+ (point-at-eol)) (point))
  12628. (org-end-of-subtree)))))
  12629. ;; Find targets in comments and move them out of comments,
  12630. ;; but mark them as targets that should be invisible
  12631. (goto-char (point-min))
  12632. (while (re-search-forward "^#.*?\\(<<<?[^>\r\n]+>>>?\\).*" nil t)
  12633. (replace-match "\\1(INVISIBLE)"))
  12634. ;; Find matches for radio targets and turn them into internal links
  12635. (goto-char (point-min))
  12636. (when re-radio
  12637. (while (re-search-forward re-radio nil t)
  12638. (replace-match "\\1[[\\2]]")))
  12639. ;; Find all links that contain a newline and put them into a single line
  12640. (goto-char (point-min))
  12641. (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
  12642. (replace-match "\\1 \\3")
  12643. (goto-char (match-beginning 0)))
  12644. ;; Convert LaTeX fragments to images
  12645. (when (memq :LaTeX-fragments parameters)
  12646. (org-format-latex
  12647. (concat "ltxpng/" (file-name-sans-extension
  12648. (file-name-nondirectory
  12649. org-current-export-file)))
  12650. org-current-export-dir nil "Creating LaTeX image %s"))
  12651. (message "Exporting...")
  12652. ;; Normalize links: Convert angle and plain links into bracket links
  12653. (goto-char (point-min))
  12654. (while (re-search-forward re-plain-link nil t)
  12655. (replace-match
  12656. (concat
  12657. (match-string 1) "[[" (match-string 2) ":" (match-string 3) "]]")
  12658. t t))
  12659. (goto-char (point-min))
  12660. (while (re-search-forward re-angle-link nil t)
  12661. (replace-match
  12662. (concat
  12663. (match-string 1) "[[" (match-string 2) ":" (match-string 3) "]]")
  12664. t t))
  12665. ;; Find multiline emphasis and put them into single line
  12666. (when (memq :emph-multiline parameters)
  12667. (goto-char (point-min))
  12668. (while (re-search-forward org-emph-re nil t)
  12669. (subst-char-in-region (match-beginning 0) (match-end 0) ?\n ?\ t)
  12670. (goto-char (1- (match-end 0)))))
  12671. ;; Remove comments
  12672. (goto-char (point-min))
  12673. (while (re-search-forward "^#.*\n?" nil t)
  12674. (replace-match ""))
  12675. (setq rtn (buffer-string)))
  12676. (kill-buffer " org-mode-tmp")
  12677. rtn))
  12678. (defun org-solidify-link-text (s &optional alist)
  12679. "Take link text and make a safe target out of it."
  12680. (save-match-data
  12681. (let* ((rtn
  12682. (mapconcat
  12683. 'identity
  12684. (org-split-string s "[ \t\r\n]+") "--"))
  12685. (a (assoc rtn alist)))
  12686. (or (cdr a) rtn))))
  12687. (defun org-convert-to-odd-levels ()
  12688. "Convert an org-mode file with all levels allowed to one with odd levels.
  12689. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  12690. level 5 etc."
  12691. (interactive)
  12692. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  12693. (let ((org-odd-levels-only nil) n)
  12694. (save-excursion
  12695. (goto-char (point-min))
  12696. (while (re-search-forward "^\\*\\*+" nil t)
  12697. (setq n (1- (length (match-string 0))))
  12698. (while (>= (setq n (1- n)) 0)
  12699. (org-demote))
  12700. (end-of-line 1))))))
  12701. (defun org-convert-to-oddeven-levels ()
  12702. "Convert an org-mode file with only odd levels to one with odd and even levels.
  12703. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  12704. section with an even level, conversion would destroy the structure of the file. An error
  12705. is signaled in this case."
  12706. (interactive)
  12707. (goto-char (point-min))
  12708. ;; First check if there are no even levels
  12709. (when (re-search-forward "^\\(\\*\\*\\)+[^*]" nil t)
  12710. (org-show-hierarchy-above)
  12711. (error "Not all levels are odd in this file. Conversion not possible."))
  12712. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  12713. (let ((org-odd-levels-only nil) n)
  12714. (save-excursion
  12715. (goto-char (point-min))
  12716. (while (re-search-forward "^\\*\\*+" nil t)
  12717. (setq n (/ (length (match-string 0)) 2))
  12718. (while (>= (setq n (1- n)) 0)
  12719. (org-promote))
  12720. (end-of-line 1))))))
  12721. (defun org-tr-level (n)
  12722. "Make N odd if required."
  12723. (if org-odd-levels-only (1+ (/ n 2)) n))
  12724. (defvar org-last-level nil) ; dynamically scoped variable
  12725. (defvar org-ascii-current-indentation nil) ; For communication
  12726. (defun org-export-as-ascii (arg)
  12727. "Export the outline as a pretty ASCII file.
  12728. If there is an active region, export only the region.
  12729. The prefix ARG specifies how many levels of the outline should become
  12730. underlined headlines. The default is 3."
  12731. (interactive "P")
  12732. (setq-default org-todo-line-regexp org-todo-line-regexp)
  12733. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  12734. (org-infile-export-plist)))
  12735. (region
  12736. (buffer-substring
  12737. (if (org-region-active-p) (region-beginning) (point-min))
  12738. (if (org-region-active-p) (region-end) (point-max))))
  12739. (lines (org-export-find-first-heading-line
  12740. (org-skip-comments
  12741. (org-split-string
  12742. (org-cleaned-string-for-export region)
  12743. "[\r\n]"))))
  12744. (org-ascii-current-indentation '(0 . 0))
  12745. (org-startup-with-deadline-check nil)
  12746. (level 0) line txt
  12747. (umax nil)
  12748. (case-fold-search nil)
  12749. (filename (concat (file-name-as-directory
  12750. (org-export-directory :ascii opt-plist))
  12751. (file-name-sans-extension
  12752. (file-name-nondirectory buffer-file-name))
  12753. ".txt"))
  12754. (buffer (find-file-noselect filename))
  12755. (levels-open (make-vector org-level-max nil))
  12756. (odd org-odd-levels-only)
  12757. (date (format-time-string "%Y/%m/%d" (current-time)))
  12758. (time (format-time-string "%X" (org-current-time)))
  12759. (author (plist-get opt-plist :author))
  12760. (title (or (plist-get opt-plist :title)
  12761. (file-name-sans-extension
  12762. (file-name-nondirectory buffer-file-name))))
  12763. (email (plist-get opt-plist :email))
  12764. (language (plist-get opt-plist :language))
  12765. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  12766. (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
  12767. (text nil)
  12768. (todo nil)
  12769. (lang-words nil))
  12770. (setq org-last-level 1)
  12771. (org-init-section-numbers)
  12772. (find-file-noselect filename)
  12773. (setq lang-words (or (assoc language org-export-language-setup)
  12774. (assoc "en" org-export-language-setup)))
  12775. (if org-export-ascii-show-new-buffer
  12776. (switch-to-buffer-other-window buffer)
  12777. (set-buffer buffer))
  12778. (erase-buffer)
  12779. (fundamental-mode)
  12780. ;; create local variables for all options, to make sure all called
  12781. ;; functions get the correct information
  12782. (mapcar (lambda (x)
  12783. (set (make-local-variable (cdr x))
  12784. (plist-get opt-plist (car x))))
  12785. org-export-plist-vars)
  12786. (org-set-local 'org-odd-levels-only odd)
  12787. (setq umax (if arg (prefix-numeric-value arg)
  12788. org-export-headline-levels))
  12789. ;; File header
  12790. (if title (org-insert-centered title ?=))
  12791. (insert "\n")
  12792. (if (or author email)
  12793. (insert (concat (nth 1 lang-words) ": " (or author "")
  12794. (if email (concat " <" email ">") "")
  12795. "\n")))
  12796. (if (and date time)
  12797. (insert (concat (nth 2 lang-words) ": " date " " time "\n")))
  12798. (if text (insert (concat (org-html-expand-for-ascii text) "\n\n")))
  12799. (insert "\n\n")
  12800. (if org-export-with-toc
  12801. (progn
  12802. (insert (nth 3 lang-words) "\n"
  12803. (make-string (length (nth 3 lang-words)) ?=) "\n")
  12804. (mapcar '(lambda (line)
  12805. (if (string-match org-todo-line-regexp
  12806. line)
  12807. ;; This is a headline
  12808. (progn
  12809. (setq level (- (match-end 1) (match-beginning 1))
  12810. level (org-tr-level level)
  12811. txt (match-string 3 line)
  12812. todo
  12813. (or (and org-export-mark-todo-in-toc
  12814. (match-beginning 2)
  12815. (not (equal (match-string 2 line)
  12816. org-done-string)))
  12817. ; TODO, not DONE
  12818. (and org-export-mark-todo-in-toc
  12819. (= level umax)
  12820. (org-search-todo-below
  12821. line lines level))))
  12822. (setq txt (org-html-expand-for-ascii txt))
  12823. (if (and (memq org-export-with-tags '(not-in-toc nil))
  12824. (string-match "[ \t]+:[a-zA-Z0-9_@:]+:[ \t]*$" txt))
  12825. (setq txt (replace-match "" t t txt)))
  12826. (if (string-match quote-re0 txt)
  12827. (setq txt (replace-match "" t t txt)))
  12828. (if org-export-with-section-numbers
  12829. (setq txt (concat (org-section-number level)
  12830. " " txt)))
  12831. (if (<= level umax)
  12832. (progn
  12833. (insert
  12834. (make-string (* (1- level) 4) ?\ )
  12835. (format (if todo "%s (*)\n" "%s\n") txt))
  12836. (setq org-last-level level))
  12837. ))))
  12838. lines)))
  12839. (org-init-section-numbers)
  12840. (while (setq line (pop lines))
  12841. ;; Remove the quoted HTML tags.
  12842. (setq line (org-html-expand-for-ascii line))
  12843. ;; Remove targets
  12844. (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line)
  12845. (setq line (replace-match "" t t line)))
  12846. ;; Replace internal links
  12847. (while (string-match org-bracket-link-regexp line)
  12848. (setq line (replace-match
  12849. (if (match-end 3) "[\\3]" "[\\1]")
  12850. t nil line)))
  12851. (cond
  12852. ((string-match "^\\(\\*+\\)[ \t]*\\(.*\\)" line)
  12853. ;; a Headline
  12854. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  12855. txt (match-string 2 line))
  12856. (org-ascii-level-start level txt umax lines))
  12857. (t
  12858. (insert (org-fix-indentation line org-ascii-current-indentation) "\n"))))
  12859. (normal-mode)
  12860. (save-buffer)
  12861. ;; remove display and invisible chars
  12862. (let (beg end)
  12863. (goto-char (point-min))
  12864. (while (setq beg (next-single-property-change (point) 'display))
  12865. (setq end (next-single-property-change beg 'display))
  12866. (delete-region beg end)
  12867. (goto-char beg)
  12868. (insert "=>"))
  12869. (goto-char (point-min))
  12870. (while (setq beg (next-single-property-change (point) 'org-cwidth))
  12871. (setq end (next-single-property-change beg 'org-cwidth))
  12872. (delete-region beg end)
  12873. (goto-char beg)))
  12874. (goto-char (point-min))))
  12875. (defun org-search-todo-below (line lines level)
  12876. "Search the subtree below LINE for any TODO entries."
  12877. (let ((rest (cdr (memq line lines)))
  12878. (re org-todo-line-regexp)
  12879. line lv todo)
  12880. (catch 'exit
  12881. (while (setq line (pop rest))
  12882. (if (string-match re line)
  12883. (progn
  12884. (setq lv (- (match-end 1) (match-beginning 1))
  12885. todo (and (match-beginning 2)
  12886. (not (equal (match-string 2 line)
  12887. org-done-string))))
  12888. ; TODO, not DONE
  12889. (if (<= lv level) (throw 'exit nil))
  12890. (if todo (throw 'exit t))))))))
  12891. (defun org-html-expand-for-ascii (line)
  12892. "Handle quoted HTML for ASCII export."
  12893. (if org-export-html-expand
  12894. (while (string-match "@<[^<>\n]*>" line)
  12895. ;; We just remove the tags for now.
  12896. (setq line (replace-match "" nil nil line))))
  12897. line)
  12898. (defun org-insert-centered (s &optional underline)
  12899. "Insert the string S centered and underline it with character UNDERLINE."
  12900. (let ((ind (max (/ (- 80 (string-width s)) 2) 0)))
  12901. (insert (make-string ind ?\ ) s "\n")
  12902. (if underline
  12903. (insert (make-string ind ?\ )
  12904. (make-string (string-width s) underline)
  12905. "\n"))))
  12906. (defun org-ascii-level-start (level title umax &optional lines)
  12907. "Insert a new level in ASCII export."
  12908. (let (char (n (- level umax 1)) (ind 0))
  12909. (if (> level umax)
  12910. (progn
  12911. (insert (make-string (* 2 n) ?\ )
  12912. (char-to-string (nth (% n (length org-export-ascii-bullets))
  12913. org-export-ascii-bullets))
  12914. " " title "\n")
  12915. ;; find the indentation of the next non-empty line
  12916. (catch 'stop
  12917. (while lines
  12918. (if (string-match "^\\*" (car lines)) (throw 'stop nil))
  12919. (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
  12920. (throw 'stop (setq ind (org-get-indentation (car lines)))))
  12921. (pop lines)))
  12922. (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
  12923. (if (or (not (equal (char-before) ?\n))
  12924. (not (equal (char-before (1- (point))) ?\n)))
  12925. (insert "\n"))
  12926. (setq char (nth (- umax level) (reverse org-export-ascii-underline)))
  12927. (unless org-export-with-tags
  12928. (if (string-match "[ \t]+\\(:[a-zA-Z0-9_@:]+:\\)[ \t]*$" title)
  12929. (setq title (replace-match "" t t title))))
  12930. (if org-export-with-section-numbers
  12931. (setq title (concat (org-section-number level) " " title)))
  12932. (insert title "\n" (make-string (string-width title) char) "\n")
  12933. (setq org-ascii-current-indentation '(0 . 0)))))
  12934. (defun org-export-visible (type arg)
  12935. "Create a copy of the visible part of the current buffer, and export it.
  12936. The copy is created in a temporary buffer and removed after use.
  12937. TYPE is the final key (as a string) that also select the export command in
  12938. the `C-c C-e' export dispatcher.
  12939. As a special case, if the you type SPC at the prompt, the temporary
  12940. org-mode file will not be removed but presented to you so that you can
  12941. continue to use it. The prefix arg ARG is passed through to the exporting
  12942. command."
  12943. (interactive
  12944. (list (progn
  12945. (message "Export visible: [a]SCII [h]tml [b]rowse HTML [x]OXO [ ]keep buffer")
  12946. (read-char-exclusive))
  12947. current-prefix-arg))
  12948. (if (not (member type '(?a ?\C-a ?b ?\C-b ?h ?x ?\ )))
  12949. (error "Invalid export key"))
  12950. (let* ((binding (cdr (assoc type
  12951. '((?a . org-export-as-ascii)
  12952. (?\C-a . org-export-as-ascii)
  12953. (?b . org-export-as-html-and-open)
  12954. (?\C-b . org-export-as-html-and-open)
  12955. (?h . org-export-as-html)
  12956. (?x . org-export-as-xoxo)))))
  12957. (keepp (equal type ?\ ))
  12958. (file buffer-file-name)
  12959. (buffer (get-buffer-create "*Org Export Visible*"))
  12960. s e)
  12961. (with-current-buffer buffer (erase-buffer))
  12962. (save-excursion
  12963. (setq s (goto-char (point-min)))
  12964. (while (not (= (point) (point-max)))
  12965. (goto-char (org-find-invisible))
  12966. (append-to-buffer buffer s (point))
  12967. (setq s (goto-char (org-find-visible))))
  12968. (goto-char (point-min))
  12969. (unless keepp
  12970. ;; Copy all comment lines to the end, to make sure #+ settings are
  12971. ;; still available for the second export step. Kind of a hack, but
  12972. ;; does do the trick.
  12973. (if (looking-at "#[^\r\n]*")
  12974. (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
  12975. (while (re-search-forward "[\n\r]#[^\n\r]*" nil t)
  12976. (append-to-buffer buffer (1+ (match-beginning 0))
  12977. (min (point-max) (1+ (match-end 0))))))
  12978. (set-buffer buffer)
  12979. (let ((buffer-file-name file)
  12980. (org-inhibit-startup t))
  12981. (org-mode)
  12982. (show-all)
  12983. (unless keepp (funcall binding arg))))
  12984. (if (not keepp)
  12985. (kill-buffer buffer)
  12986. (switch-to-buffer-other-window buffer)
  12987. (goto-char (point-min)))))
  12988. (defun org-find-visible ()
  12989. (let ((s (point)))
  12990. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  12991. (get-char-property s 'invisible)))
  12992. s))
  12993. (defun org-find-invisible ()
  12994. (let ((s (point)))
  12995. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  12996. (not (get-char-property s 'invisible))))
  12997. s))
  12998. ;; HTML
  12999. (defun org-get-current-options ()
  13000. "Return a string with current options as keyword options.
  13001. Does include HTML export options as well as TODO and CATEGORY stuff."
  13002. (format
  13003. "#+TITLE: %s
  13004. #+AUTHOR: %s
  13005. #+EMAIL: %s
  13006. #+LANGUAGE: %s
  13007. #+TEXT: Some descriptive text to be emitted. Several lines OK.
  13008. #+OPTIONS: H:%d num:%s toc:%s \\n:%s @:%s ::%s |:%s ^:%s *:%s TeX:%s LaTeX:%s
  13009. #+CATEGORY: %s
  13010. #+SEQ_TODO: %s
  13011. #+TYP_TODO: %s
  13012. #+STARTUP: %s %s %s %s %s %s
  13013. #+TAGS: %s
  13014. #+ARCHIVE: %s
  13015. "
  13016. (buffer-name) (user-full-name) user-mail-address org-export-default-language
  13017. org-export-headline-levels
  13018. org-export-with-section-numbers
  13019. org-export-with-toc
  13020. org-export-preserve-breaks
  13021. org-export-html-expand
  13022. org-export-with-fixed-width
  13023. org-export-with-tables
  13024. org-export-with-sub-superscripts
  13025. org-export-with-emphasize
  13026. org-export-with-TeX-macros
  13027. org-export-with-LaTeX-fragments
  13028. (file-name-nondirectory buffer-file-name)
  13029. (if (equal org-todo-interpretation 'sequence)
  13030. (mapconcat 'identity org-todo-keywords " ")
  13031. "TODO FEEDBACK VERIFY DONE")
  13032. (if (equal org-todo-interpretation 'type)
  13033. (mapconcat 'identity org-todo-keywords " ")
  13034. "Me Jason Marie DONE")
  13035. (cdr (assoc org-startup-folded
  13036. '((nil . "showall") (t . "overview") (content . "content"))))
  13037. (if org-startup-with-deadline-check "dlcheck" "nodlcheck")
  13038. (if org-odd-levels-only "odd" "oddeven")
  13039. (if org-hide-leading-stars "hidestars" "showstars")
  13040. (if org-startup-align-all-tables "align" "noalign")
  13041. (if org-log-done "logging" "nologging")
  13042. (or (mapconcat (lambda (x)
  13043. (cond
  13044. ((equal '(:startgroup) x) "{")
  13045. ((equal '(:endgroup) x) "}")
  13046. ((cdr x) (format "%s(%c)" (car x) (cdr x)))
  13047. (t (car x))))
  13048. (or org-tag-alist (org-get-buffer-tags)) " ") "")
  13049. org-archive-location
  13050. ))
  13051. (defun org-insert-export-options-template ()
  13052. "Insert into the buffer a template with information for exporting."
  13053. (interactive)
  13054. (if (not (bolp)) (newline))
  13055. (let ((s (org-get-current-options)))
  13056. (and (string-match "#\\+CATEGORY" s)
  13057. (setq s (substring s 0 (match-beginning 0))))
  13058. (insert s)))
  13059. (defun org-toggle-fixed-width-section (arg)
  13060. "Toggle the fixed-width export.
  13061. If there is no active region, the QUOTE keyword at the current headline is
  13062. inserted or removed. When present, it causes the text between this headline
  13063. and the next to be exported as fixed-width text, and unmodified.
  13064. If there is an active region, this command adds or removes a colon as the
  13065. first character of this line. If the first character of a line is a colon,
  13066. this line is also exported in fixed-width font."
  13067. (interactive "P")
  13068. (let* ((cc 0)
  13069. (regionp (org-region-active-p))
  13070. (beg (if regionp (region-beginning) (point)))
  13071. (end (if regionp (region-end)))
  13072. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  13073. (re "[ \t]*\\(:\\)")
  13074. off)
  13075. (if regionp
  13076. (save-excursion
  13077. (goto-char beg)
  13078. (setq cc (current-column))
  13079. (beginning-of-line 1)
  13080. (setq off (looking-at re))
  13081. (while (> nlines 0)
  13082. (setq nlines (1- nlines))
  13083. (beginning-of-line 1)
  13084. (cond
  13085. (arg
  13086. (move-to-column cc t)
  13087. (insert ":\n")
  13088. (forward-line -1))
  13089. ((and off (looking-at re))
  13090. (replace-match "" t t nil 1))
  13091. ((not off) (move-to-column cc t) (insert ":")))
  13092. (forward-line 1)))
  13093. (save-excursion
  13094. (org-back-to-heading)
  13095. (if (looking-at (concat outline-regexp
  13096. "\\( +\\<" org-quote-string "\\>\\)"))
  13097. (replace-match "" t t nil 1)
  13098. (if (looking-at outline-regexp)
  13099. (progn
  13100. (goto-char (match-end 0))
  13101. (insert " " org-quote-string))))))))
  13102. (defun org-export-as-html-and-open (arg)
  13103. "Export the outline as HTML and immediately open it with a browser.
  13104. If there is an active region, export only the region.
  13105. The prefix ARG specifies how many levels of the outline should become
  13106. headlines. The default is 3. Lower levels will become bulleted lists."
  13107. (interactive "P")
  13108. (org-export-as-html arg 'hidden)
  13109. (org-open-file buffer-file-name))
  13110. (defun org-export-as-html-batch ()
  13111. "Call `org-export-as-html', may be used in batch processing as
  13112. emacs --batch
  13113. --load=$HOME/lib/emacs/org.el
  13114. --eval \"(setq org-export-headline-levels 2)\"
  13115. --visit=MyFile --funcall org-export-as-html-batch"
  13116. (org-export-as-html org-export-headline-levels 'hidden))
  13117. (defun org-export-as-html (arg &optional hidden ext-plist)
  13118. "Export the outline as a pretty HTML file.
  13119. If there is an active region, export only the region.
  13120. The prefix ARG specifies how many levels of the outline should become
  13121. headlines. The default is 3. Lower levels will become bulleted lists.
  13122. When HIDDEN is non-nil, don't display the HTML buffer.
  13123. EXT-PLIST is a property list with external parameters overriding
  13124. org-mode's default settings, but still inferior to file-local settings."
  13125. (interactive "P")
  13126. (message "Exporting...")
  13127. (setq-default org-todo-line-regexp org-todo-line-regexp)
  13128. (setq-default org-deadline-line-regexp org-deadline-line-regexp)
  13129. (setq-default org-done-string org-done-string)
  13130. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  13131. ext-plist
  13132. (org-infile-export-plist)))
  13133. (style (plist-get opt-plist :style))
  13134. (link-validate (plist-get opt-plist :link-validation-function))
  13135. valid
  13136. (odd org-odd-levels-only)
  13137. (region-p (org-region-active-p))
  13138. (region
  13139. (buffer-substring
  13140. (if region-p (region-beginning) (point-min))
  13141. (if region-p (region-end) (point-max))))
  13142. ;; The following two are dynamically scoped into other
  13143. ;; routines below.
  13144. (org-current-export-dir (org-export-directory :html opt-plist))
  13145. (org-current-export-file buffer-file-name)
  13146. (all_lines
  13147. (org-skip-comments (org-split-string
  13148. (org-cleaned-string-for-export
  13149. region :emph-multiline
  13150. (if (plist-get opt-plist :LaTeX-fragments)
  13151. :LaTeX-fragments))
  13152. "[\r\n]")))
  13153. (lines (org-export-find-first-heading-line all_lines))
  13154. (level 0) (line "") (origline "") txt todo
  13155. (umax nil)
  13156. (filename (concat (file-name-as-directory
  13157. (org-export-directory :html opt-plist))
  13158. (file-name-sans-extension
  13159. (file-name-nondirectory buffer-file-name))
  13160. ".html"))
  13161. (current-dir (file-name-directory buffer-file-name))
  13162. (buffer (find-file-noselect filename))
  13163. (levels-open (make-vector org-level-max nil))
  13164. (date (format-time-string "%Y/%m/%d" (current-time)))
  13165. (time (format-time-string "%X" (org-current-time)))
  13166. (author (plist-get opt-plist :author))
  13167. (title (or (plist-get opt-plist :title)
  13168. (file-name-sans-extension
  13169. (file-name-nondirectory buffer-file-name))))
  13170. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  13171. (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
  13172. (inquote nil)
  13173. (infixed nil)
  13174. (in-local-list nil)
  13175. (local-list-num nil)
  13176. (local-list-indent nil)
  13177. (llt org-plain-list-ordered-item-terminator)
  13178. (email (plist-get opt-plist :email))
  13179. (language (plist-get opt-plist :language))
  13180. (text (plist-get opt-plist :text))
  13181. (lang-words nil)
  13182. (target-alist nil) tg
  13183. (head-count 0) cnt
  13184. (start 0)
  13185. (coding-system (and (boundp 'buffer-file-coding-system)
  13186. buffer-file-coding-system))
  13187. (coding-system-for-write coding-system)
  13188. (save-buffer-coding-system coding-system)
  13189. (charset (and coding-system
  13190. (fboundp 'coding-system-get)
  13191. (coding-system-get coding-system 'mime-charset)))
  13192. table-open type
  13193. table-buffer table-orig-buffer
  13194. ind start-is-num starter
  13195. rpl path desc descp desc1 desc2 link
  13196. )
  13197. (message "Exporting...")
  13198. (setq org-last-level 1)
  13199. (org-init-section-numbers)
  13200. ;; Get the language-dependent settings
  13201. (setq lang-words (or (assoc language org-export-language-setup)
  13202. (assoc "en" org-export-language-setup)))
  13203. ;; Switch to the output buffer
  13204. (if (or hidden (not org-export-html-show-new-buffer))
  13205. (set-buffer buffer)
  13206. (switch-to-buffer-other-window buffer))
  13207. (erase-buffer)
  13208. (fundamental-mode)
  13209. (let ((case-fold-search nil)
  13210. (org-odd-levels-only odd))
  13211. ;; create local variables for all options, to make sure all called
  13212. ;; functions get the correct information
  13213. (mapcar (lambda (x)
  13214. (set (make-local-variable (cdr x))
  13215. (plist-get opt-plist (car x))))
  13216. org-export-plist-vars)
  13217. (setq umax (if arg (prefix-numeric-value arg)
  13218. org-export-headline-levels))
  13219. ;; File header
  13220. (insert (format
  13221. "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
  13222. \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
  13223. <html xmlns=\"http://www.w3.org/1999/xhtml\"
  13224. lang=\"%s\" xml:lang=\"%s\">
  13225. <head>
  13226. <title>%s</title>
  13227. <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
  13228. <meta name=\"generator\" content=\"Org-mode\"/>
  13229. <meta name=\"generated\" content=\"%s %s\"/>
  13230. <meta name=\"author\" content=\"%s\"/>
  13231. %s
  13232. </head><body>
  13233. "
  13234. language language (org-html-expand title) (or charset "iso-8859-1")
  13235. date time author style))
  13236. (insert (or (plist-get opt-plist :preamble) ""))
  13237. (when (plist-get opt-plist :auto-preamble)
  13238. (if title (insert (format org-export-html-title-format
  13239. (org-html-expand title))))
  13240. (if text (insert "<p>\n" (org-html-expand text) "</p>")))
  13241. (if org-export-with-toc
  13242. (progn
  13243. (insert (format "<h%d>%s</h%d>\n"
  13244. org-export-html-toplevel-hlevel
  13245. (nth 3 lang-words)
  13246. org-export-html-toplevel-hlevel))
  13247. (insert "<ul>\n<li>")
  13248. (setq lines
  13249. (mapcar '(lambda (line)
  13250. (if (string-match org-todo-line-regexp line)
  13251. ;; This is a headline
  13252. (progn
  13253. (setq level (- (match-end 1) (match-beginning 1))
  13254. level (org-tr-level level)
  13255. txt (save-match-data
  13256. (org-html-expand
  13257. (org-export-cleanup-toc-line
  13258. (match-string 3 line))))
  13259. todo
  13260. (or (and org-export-mark-todo-in-toc
  13261. (match-beginning 2)
  13262. (not (equal (match-string 2 line)
  13263. org-done-string)))
  13264. ; TODO, not DONE
  13265. (and org-export-mark-todo-in-toc
  13266. (= level umax)
  13267. (org-search-todo-below
  13268. line lines level))))
  13269. (if (and (memq org-export-with-tags '(not-in-toc nil))
  13270. (string-match "[ \t]+:[a-zA-Z0-9_@:]+:[ \t]*$" txt))
  13271. (setq txt (replace-match "" t t txt)))
  13272. (if (string-match quote-re0 txt)
  13273. (setq txt (replace-match "" t t txt)))
  13274. (if org-export-with-section-numbers
  13275. (setq txt (concat (org-section-number level)
  13276. " " txt)))
  13277. (if (<= level umax)
  13278. (progn
  13279. (setq head-count (+ head-count 1))
  13280. (if (> level org-last-level)
  13281. (progn
  13282. (setq cnt (- level org-last-level))
  13283. (while (>= (setq cnt (1- cnt)) 0)
  13284. (insert "\n<ul>\n<li>"))
  13285. (insert "\n")))
  13286. (if (< level org-last-level)
  13287. (progn
  13288. (setq cnt (- org-last-level level))
  13289. (while (>= (setq cnt (1- cnt)) 0)
  13290. (insert "</li>\n</ul>"))
  13291. (insert "\n")))
  13292. ;; Check for targets
  13293. (while (string-match org-target-regexp line)
  13294. (setq tg (match-string 1 line)
  13295. line (replace-match
  13296. (concat "@<span class=\"target\">" tg "@</span> ")
  13297. t t line))
  13298. (push (cons (org-solidify-link-text tg)
  13299. (format "sec-%d" head-count))
  13300. target-alist))
  13301. (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
  13302. (setq txt (replace-match "" t t txt)))
  13303. (insert
  13304. (format
  13305. (if todo
  13306. "</li>\n<li><a href=\"#sec-%d\"><span class=\"todo\">%s</span></a>"
  13307. "</li>\n<li><a href=\"#sec-%d\">%s</a>")
  13308. head-count txt))
  13309. (setq org-last-level level))
  13310. )))
  13311. line)
  13312. lines))
  13313. (while (> org-last-level 0)
  13314. (setq org-last-level (1- org-last-level))
  13315. (insert "</li>\n</ul>\n"))
  13316. ))
  13317. (setq head-count 0)
  13318. (org-init-section-numbers)
  13319. (while (setq line (pop lines) origline line)
  13320. (catch 'nextline
  13321. ;; end of quote section?
  13322. (when (and inquote (string-match "^\\*+" line))
  13323. (insert "</pre>\n")
  13324. (setq inquote nil))
  13325. ;; inside a quote section?
  13326. (when inquote
  13327. (insert (org-html-protect line) "\n")
  13328. (throw 'nextline nil))
  13329. ;; verbatim lines
  13330. (when (and org-export-with-fixed-width
  13331. (string-match "^[ \t]*:\\(.*\\)" line))
  13332. (when (not infixed)
  13333. (setq infixed t)
  13334. (insert "<pre>\n"))
  13335. (insert (org-html-protect (match-string 1 line)) "\n")
  13336. (when (and lines
  13337. (not (string-match "^[ \t]*\\(:.*\\)"
  13338. (car lines))))
  13339. (setq infixed nil)
  13340. (insert "</pre>\n"))
  13341. (throw 'nextline nil))
  13342. ;; make targets to anchors
  13343. (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
  13344. (cond
  13345. ((match-end 2)
  13346. (setq line (replace-match
  13347. (concat "@<a name=\""
  13348. (org-solidify-link-text (match-string 1 line))
  13349. "\">\\nbsp@</a>")
  13350. t t line)))
  13351. ((and org-export-with-toc (equal (string-to-char line) ?*))
  13352. (setq line (replace-match
  13353. (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
  13354. ; (concat "@<i>" (match-string 1 line) "@</i> ")
  13355. t t line)))
  13356. (t
  13357. (setq line (replace-match
  13358. (concat "@<a name=\""
  13359. (org-solidify-link-text (match-string 1 line))
  13360. "\" class=\"target\">" (match-string 1 line) "@</a> ")
  13361. t t line)))))
  13362. (setq line (org-html-handle-time-stamps line))
  13363. ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  13364. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  13365. ;; Also handle sub_superscripts and checkboxes
  13366. (setq line (org-html-expand line))
  13367. ;; Format the links
  13368. (setq start 0)
  13369. (while (string-match org-bracket-link-analytic-regexp line start)
  13370. (setq start (match-beginning 0))
  13371. (setq type (if (match-end 2) (match-string 2 line) "internal"))
  13372. (setq path (match-string 3 line))
  13373. (setq desc1 (if (match-end 5) (match-string 5 line))
  13374. desc2 (if (match-end 2) (concat type ":" path) path)
  13375. descp (and desc1 (not (equal desc1 desc2)))
  13376. desc (or desc1 desc2))
  13377. ;; FIXME: do we need to unescape here somewhere?
  13378. (cond
  13379. ((equal type "internal")
  13380. (setq rpl
  13381. (concat
  13382. "<a href=\"#"
  13383. (org-solidify-link-text path target-alist)
  13384. "\">" desc "</a>")))
  13385. ((member type '("http" "https" "ftp" "mailto" "news"))
  13386. ;; standard URL
  13387. (setq link (concat type ":" path))
  13388. (setq rpl (concat "<a href=\"" link "\">" desc "</a>")))
  13389. ((string= type "file")
  13390. ;; FILE link
  13391. (let* ((filename path)
  13392. (abs-p (file-name-absolute-p filename))
  13393. thefile file-is-image-p search)
  13394. (save-match-data
  13395. (if (string-match "::\\(.*\\)" filename)
  13396. (setq search (match-string 1 filename)
  13397. filename (replace-match "" t nil filename)))
  13398. (setq valid
  13399. (if (functionp link-validate)
  13400. (funcall link-validate filename current-dir)
  13401. t))
  13402. (setq file-is-image-p
  13403. (string-match (org-image-file-name-regexp) filename))
  13404. (setq thefile (if abs-p (expand-file-name filename) filename))
  13405. (when (and org-export-html-link-org-files-as-html
  13406. (string-match "\\.org$" thefile))
  13407. (setq thefile (concat (substring thefile 0
  13408. (match-beginning 0))
  13409. ".html"))
  13410. (if (and search
  13411. ;; make sure this is can be used as target search
  13412. (not (string-match "^[0-9]*$" search))
  13413. (not (string-match "^\\*" search))
  13414. (not (string-match "^/.*/$" search)))
  13415. (setq thefile (concat thefile "#"
  13416. (org-solidify-link-text
  13417. (org-link-unescape search)))))
  13418. (when (string-match "^file:" desc)
  13419. (setq desc (replace-match "" t t desc))
  13420. (if (string-match "\\.org$" desc)
  13421. (setq desc (replace-match "" t t desc))))))
  13422. (setq rpl (if (and file-is-image-p
  13423. (or (eq t org-export-html-inline-images)
  13424. (and org-export-html-inline-images
  13425. (not descp))))
  13426. (concat "<img src=\"" thefile "\"/>")
  13427. (concat "<a href=\"" thefile "\">" desc "</a>")))
  13428. (if (not valid) (setq rpl desc))))
  13429. ((member type '("bbdb" "vm" "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  13430. (setq rpl (concat "<i>&lt;" type ":"
  13431. (save-match-data (org-link-unescape path))
  13432. "&gt;</i>"))))
  13433. (setq line (replace-match rpl t t line)
  13434. start (+ start (length rpl))))
  13435. ;; TODO items
  13436. (if (and (string-match org-todo-line-regexp line)
  13437. (match-beginning 2))
  13438. (if (equal (match-string 2 line) org-done-string)
  13439. (setq line (replace-match
  13440. "<span class=\"done\">\\2</span>"
  13441. t nil line 2))
  13442. (setq line (replace-match "<span class=\"todo\">\\2</span>"
  13443. t nil line 2))))
  13444. (cond
  13445. ((string-match "^\\(\\*+\\)[ \t]*\\(.*\\)" line)
  13446. ;; This is a headline
  13447. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  13448. txt (match-string 2 line))
  13449. (if (string-match quote-re0 txt)
  13450. (setq txt (replace-match "" t t txt)))
  13451. (if (<= level umax) (setq head-count (+ head-count 1)))
  13452. (when in-local-list
  13453. ;; Close any local lists before inserting a new header line
  13454. (while local-list-num
  13455. (org-close-li)
  13456. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  13457. (pop local-list-num))
  13458. (setq local-list-indent nil
  13459. in-local-list nil))
  13460. (org-html-level-start level txt umax
  13461. (and org-export-with-toc (<= level umax))
  13462. head-count)
  13463. ;; QUOTES
  13464. (when (string-match quote-re line)
  13465. (insert "<pre>")
  13466. (setq inquote t)))
  13467. ((and org-export-with-tables
  13468. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  13469. (if (not table-open)
  13470. ;; New table starts
  13471. (setq table-open t table-buffer nil table-orig-buffer nil))
  13472. ;; Accumulate lines
  13473. (setq table-buffer (cons line table-buffer)
  13474. table-orig-buffer (cons origline table-orig-buffer))
  13475. (when (or (not lines)
  13476. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  13477. (car lines))))
  13478. (setq table-open nil
  13479. table-buffer (nreverse table-buffer)
  13480. table-orig-buffer (nreverse table-orig-buffer))
  13481. (org-close-par-maybe)
  13482. (insert (org-format-table-html table-buffer table-orig-buffer))))
  13483. (t
  13484. ;; Normal lines
  13485. (when (string-match
  13486. (cond
  13487. ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  13488. ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  13489. ((= llt ?\)) "^\\( \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  13490. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
  13491. line)
  13492. (setq ind (org-get-string-indentation line)
  13493. start-is-num (match-beginning 4)
  13494. starter (if (match-beginning 2)
  13495. (substring (match-string 2 line) 0 -1))
  13496. line (substring line (match-beginning 5)))
  13497. (unless (string-match "[^ \t]" line)
  13498. ;; empty line. Pretend indentation is large.
  13499. (setq ind (1+ (or (car local-list-indent) 1))))
  13500. (while (and in-local-list
  13501. (or (and (= ind (car local-list-indent))
  13502. (not starter))
  13503. (< ind (car local-list-indent))))
  13504. (org-close-li)
  13505. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  13506. (pop local-list-num) (pop local-list-indent)
  13507. (setq in-local-list local-list-indent))
  13508. (cond
  13509. ((and starter
  13510. (or (not in-local-list)
  13511. (> ind (car local-list-indent))))
  13512. ;; Start new (level of ) list
  13513. (org-close-par-maybe)
  13514. (insert (if start-is-num "<ol>\n<li>\n" "<ul>\n<li>\n"))
  13515. (push start-is-num local-list-num)
  13516. (push ind local-list-indent)
  13517. (setq in-local-list t))
  13518. (starter
  13519. ;; continue current list
  13520. (org-close-li)
  13521. (insert "<li>\n")))
  13522. (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line)
  13523. (setq line
  13524. (replace-match
  13525. (if (equal (match-string 1 line) "X")
  13526. "<b>[X]</b>"
  13527. "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
  13528. t t line))))
  13529. ;; Empty lines start a new paragraph. If hand-formatted lists
  13530. ;; are not fully interpreted, lines starting with "-", "+", "*"
  13531. ;; also start a new paragraph.
  13532. (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
  13533. ;; Check if the line break needs to be conserved
  13534. (cond
  13535. ((string-match "\\\\\\\\[ \t]*$" line)
  13536. (setq line (replace-match "<br/>" t t line)))
  13537. (org-export-preserve-breaks
  13538. (setq line (concat line "<br/>"))))
  13539. (insert line "\n")))))
  13540. ;; Properly close all local lists and other lists
  13541. (when inquote (insert "</pre>\n"))
  13542. (when in-local-list
  13543. ;; Close any local lists before inserting a new header line
  13544. (while local-list-num
  13545. (org-close-li)
  13546. (insert (if (car local-list-num) "</ol>\n" "</ul>\n"))
  13547. (pop local-list-num))
  13548. (setq local-list-indent nil
  13549. in-local-list nil))
  13550. (org-html-level-start 1 nil umax
  13551. (and org-export-with-toc (<= level umax))
  13552. head-count)
  13553. (when (plist-get opt-plist :auto-postamble)
  13554. (when author
  13555. (insert "<p class=\"author\"> "
  13556. (nth 1 lang-words) ": " author "\n")
  13557. (when email
  13558. (insert "<a href=\"mailto:" email "\">&lt;"
  13559. email "&gt;</a>\n"))
  13560. (insert "</p>\n"))
  13561. (when (and date time)
  13562. (insert "<p class=\"date\"> "
  13563. (nth 2 lang-words) ": "
  13564. date " " time "</p>\n")))
  13565. (if org-export-html-with-timestamp
  13566. (insert org-export-html-html-helper-timestamp))
  13567. (insert (or (plist-get opt-plist :postamble) ""))
  13568. (insert "</body>\n</html>\n")
  13569. (normal-mode)
  13570. ;; remove empty paragraphs and lists
  13571. (goto-char (point-min))
  13572. (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
  13573. (replace-match ""))
  13574. (goto-char (point-min))
  13575. (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t)
  13576. (replace-match ""))
  13577. (save-buffer)
  13578. (goto-char (point-min))
  13579. (message "Exporting... done"))))
  13580. (defun org-format-table-html (lines olines)
  13581. "Find out which HTML converter to use and return the HTML code."
  13582. (if (string-match "^[ \t]*|" (car lines))
  13583. ;; A normal org table
  13584. (org-format-org-table-html lines)
  13585. ;; Table made by table.el - test for spanning
  13586. (let* ((hlines (delq nil (mapcar
  13587. (lambda (x)
  13588. (if (string-match "^[ \t]*\\+-" x) x
  13589. nil))
  13590. lines)))
  13591. (first (car hlines))
  13592. (ll (and (string-match "\\S-+" first)
  13593. (match-string 0 first)))
  13594. (re (concat "^[ \t]*" (regexp-quote ll)))
  13595. (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
  13596. hlines))))
  13597. (if (and (not spanning)
  13598. (not org-export-prefer-native-exporter-for-tables))
  13599. ;; We can use my own converter with HTML conversions
  13600. (org-format-table-table-html lines)
  13601. ;; Need to use the code generator in table.el, with the original text.
  13602. (org-format-table-table-html-using-table-generate-source olines)))))
  13603. (defun org-format-org-table-html (lines)
  13604. "Format a table into HTML."
  13605. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  13606. (setq lines (nreverse lines))
  13607. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  13608. (setq lines (nreverse lines))
  13609. (when org-export-table-remove-special-lines
  13610. ;; Check if the table has a marking column. If yes remove the
  13611. ;; column and the special lines
  13612. (let* ((special
  13613. (not
  13614. (memq nil
  13615. (mapcar
  13616. (lambda (x)
  13617. (or (string-match "^[ \t]*|-" x)
  13618. (string-match "^[ \t]*| *\\([#!$*_^ ]\\) *|" x)))
  13619. lines)))))
  13620. (if special
  13621. (setq lines
  13622. (delq nil
  13623. (mapcar
  13624. (lambda (x)
  13625. (if (string-match "^[ \t]*| *[!_^] *|" x)
  13626. nil ; ignore this line
  13627. (and (or (string-match "^[ \t]*|-+\\+" x)
  13628. (string-match "^[ \t]*|[^|]*|" x))
  13629. (replace-match "|" t t x))))
  13630. lines))))))
  13631. (let ((head (and org-export-highlight-first-table-line
  13632. (delq nil (mapcar
  13633. (lambda (x) (string-match "^[ \t]*|-" x))
  13634. (cdr lines)))))
  13635. line fields html)
  13636. (setq html (concat org-export-html-table-tag "\n"))
  13637. (while (setq line (pop lines))
  13638. (catch 'next-line
  13639. (if (string-match "^[ \t]*|-" line)
  13640. (progn
  13641. (setq head nil) ;; head ends here, first time around
  13642. ;; ignore this line
  13643. (throw 'next-line t)))
  13644. ;; Break the line into fields
  13645. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  13646. (setq html (concat
  13647. html
  13648. "<tr>"
  13649. (mapconcat (lambda (x)
  13650. (if head
  13651. (concat "<th>" x "</th>")
  13652. (concat "<td>" x "</td>")))
  13653. fields "")
  13654. "</tr>\n"))))
  13655. (setq html (concat html "</table>\n"))
  13656. html))
  13657. (defun org-fake-empty-table-line (line)
  13658. "Replace everything except \"|\" with spaces."
  13659. (let ((i (length line))
  13660. (newstr (copy-sequence line)))
  13661. (while (> i 0)
  13662. (setq i (1- i))
  13663. (if (not (eq (aref newstr i) ?|))
  13664. (aset newstr i ?\ )))
  13665. newstr))
  13666. (defun org-format-table-table-html (lines)
  13667. "Format a table generated by table.el into HTML.
  13668. This conversion does *not* use `table-generate-source' from table.el.
  13669. This has the advantage that Org-mode's HTML conversions can be used.
  13670. But it has the disadvantage, that no cell- or row-spanning is allowed."
  13671. (let (line field-buffer
  13672. (head org-export-highlight-first-table-line)
  13673. fields html empty)
  13674. (setq html (concat org-export-html-table-tag "\n"))
  13675. (while (setq line (pop lines))
  13676. (setq empty "&nbsp;")
  13677. (catch 'next-line
  13678. (if (string-match "^[ \t]*\\+-" line)
  13679. (progn
  13680. (if field-buffer
  13681. (progn
  13682. (setq html (concat
  13683. html
  13684. "<tr>"
  13685. (mapconcat
  13686. (lambda (x)
  13687. (if (equal x "") (setq x empty))
  13688. (if head
  13689. (concat "<th>" x "</th>\n")
  13690. (concat "<td>" x "</td>\n")))
  13691. field-buffer "\n")
  13692. "</tr>\n"))
  13693. (setq head nil)
  13694. (setq field-buffer nil)))
  13695. ;; Ignore this line
  13696. (throw 'next-line t)))
  13697. ;; Break the line into fields and store the fields
  13698. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  13699. (if field-buffer
  13700. (setq field-buffer (mapcar
  13701. (lambda (x)
  13702. (concat x "<br/>" (pop fields)))
  13703. field-buffer))
  13704. (setq field-buffer fields))))
  13705. (setq html (concat html "</table>\n"))
  13706. html))
  13707. (defun org-format-table-table-html-using-table-generate-source (lines)
  13708. "Format a table into html, using `table-generate-source' from table.el.
  13709. This has the advantage that cell- or row-spanning is allowed.
  13710. But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
  13711. (require 'table)
  13712. (with-current-buffer (get-buffer-create " org-tmp1 ")
  13713. (erase-buffer)
  13714. (insert (mapconcat 'identity lines "\n"))
  13715. (goto-char (point-min))
  13716. (if (not (re-search-forward "|[^+]" nil t))
  13717. (error "Error processing table"))
  13718. (table-recognize-table)
  13719. (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
  13720. (table-generate-source 'html " org-tmp2 ")
  13721. (set-buffer " org-tmp2 ")
  13722. (buffer-substring (point-min) (point-max))))
  13723. (defun org-html-handle-time-stamps (s)
  13724. "Format time stamps in string S, or remove them."
  13725. (catch 'exit
  13726. (let (r b)
  13727. (while (string-match org-maybe-keyword-time-regexp s)
  13728. ;; FIXME: is it good to never export CLOCK, or do we need control?
  13729. (if (and (match-end 1) (equal (match-string 1 s) org-clock-string))
  13730. (throw 'exit ""))
  13731. (or b (setq b (substring s 0 (match-beginning 0))))
  13732. (if (not org-export-with-timestamps)
  13733. (setq r (concat r (substring s 0 (match-beginning 0)))
  13734. s (substring s (match-end 0)))
  13735. (setq r (concat
  13736. r (substring s 0 (match-beginning 0))
  13737. (if (match-end 1)
  13738. (format "@<span class=\"timestamp-kwd\">%s @</span>"
  13739. (match-string 1 s)))
  13740. (format " @<span class=\"timestamp\">%s@</span>"
  13741. (substring (match-string 3 s) 1 -1)))
  13742. s (substring s (match-end 0)))))
  13743. ;; Line break if line started and ended with time stamp stuff
  13744. (if (not r)
  13745. s
  13746. (setq r (concat r s))
  13747. (unless (string-match "\\S-" (concat b s))
  13748. (setq r (concat r "@<br/>")))
  13749. r))))
  13750. (defun org-html-protect (s)
  13751. ;; convert & to &amp;, < to &lt; and > to &gt;
  13752. (let ((start 0))
  13753. (while (string-match "&" s start)
  13754. (setq s (replace-match "&amp;" t t s)
  13755. start (1+ (match-beginning 0))))
  13756. (while (string-match "<" s)
  13757. (setq s (replace-match "&lt;" t t s)))
  13758. (while (string-match ">" s)
  13759. (setq s (replace-match "&gt;" t t s))))
  13760. s)
  13761. (defun org-export-cleanup-toc-line (s)
  13762. "Remove tags and time staps from lines going into the toc."
  13763. (if (string-match " +:[a-zA-Z0-9_@:]+: *$" s)
  13764. (setq s (replace-match "" t t s)))
  13765. (when org-export-remove-timestamps-from-toc
  13766. (while (string-match org-maybe-keyword-time-regexp s)
  13767. (setq s (replace-match "" t t s))))
  13768. s)
  13769. (defun org-html-expand (string)
  13770. "Prepare STRING for HTML export. Applies all active conversions.
  13771. If there are links in the string, don't modify these."
  13772. (let* (m s l res)
  13773. (while (setq m (string-match org-bracket-link-regexp string))
  13774. (setq s (substring string 0 m)
  13775. l (match-string 0 string)
  13776. string (substring string (match-end 0)))
  13777. (push (org-html-do-expand s) res)
  13778. (push l res))
  13779. (push (org-html-do-expand string) res)
  13780. (apply 'concat (nreverse res))))
  13781. (defun org-html-do-expand (s)
  13782. "Apply all active conversions to translate special ASCII to HTML."
  13783. (setq s (org-html-protect s))
  13784. (if org-export-html-expand
  13785. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  13786. (setq s (replace-match "<\\1>" t nil s))))
  13787. (if org-export-with-emphasize
  13788. (setq s (org-export-html-convert-emphasize s)))
  13789. (if org-export-with-sub-superscripts
  13790. (setq s (org-export-html-convert-sub-super s)))
  13791. (if org-export-with-TeX-macros
  13792. (let ((start 0) wd ass)
  13793. (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)" s start))
  13794. (setq wd (match-string 1 s))
  13795. (if (setq ass (assoc wd org-html-entities))
  13796. (setq s (replace-match (or (cdr ass)
  13797. (concat "&" (car ass) ";"))
  13798. t t s))
  13799. (setq start (+ start (length wd)))))))
  13800. s)
  13801. (defun org-create-multibrace-regexp (left right n)
  13802. "Create a regular expression which will match a balanced sexp.
  13803. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  13804. as single character strings.
  13805. The regexp returned will match the entire expression including the
  13806. delimiters. It will also define a single group which contains the
  13807. match except for the outermost delimiters. The maximum depth of
  13808. stacked delimiters is N. Escaping delimiters is not possible."
  13809. (let* ((nothing (concat "[^" "\\" left "\\" right "]*?"))
  13810. (or "\\|")
  13811. (re nothing)
  13812. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  13813. (while (> n 1)
  13814. (setq n (1- n)
  13815. re (concat re or next)
  13816. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  13817. (concat left "\\(" re "\\)" right)))
  13818. (defvar org-match-substring-regexp
  13819. (concat
  13820. "\\([^\\]\\)\\([_^]\\)\\("
  13821. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  13822. "\\|"
  13823. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  13824. "\\|"
  13825. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  13826. "The regular expression matching a sub- or superscript.")
  13827. (defun org-export-html-convert-sub-super (string)
  13828. "Convert sub- and superscripts in STRING to HTML."
  13829. (let (key c)
  13830. (while (string-match org-match-substring-regexp string)
  13831. (setq key (if (string= (match-string 2 string) "_") "sub" "sup"))
  13832. (setq c (or (match-string 8 string)
  13833. (match-string 6 string)
  13834. (match-string 5 string)))
  13835. (setq string (replace-match
  13836. (concat (match-string 1 string)
  13837. "<" key ">" c "</" key ">")
  13838. t t string)))
  13839. (while (string-match "\\\\\\([_^]\\)" string)
  13840. (setq string (replace-match (match-string 1 string) t t string))))
  13841. string)
  13842. (defun org-export-html-convert-emphasize (string)
  13843. "Apply emphasis."
  13844. (while (string-match org-emph-re string)
  13845. (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)))
  13846. string)
  13847. (defvar org-par-open nil)
  13848. (defun org-open-par ()
  13849. "Insert <p>, but first close previous paragraph if any."
  13850. (org-close-par-maybe)
  13851. (insert "\n<p>")
  13852. (setq org-par-open t))
  13853. (defun org-close-par-maybe ()
  13854. "Close paragraph if there is one open."
  13855. (when org-par-open
  13856. (insert "</p>")
  13857. (setq org-par-open nil)))
  13858. (defun org-close-li ()
  13859. "Close <li> if necessary."
  13860. (org-close-par-maybe)
  13861. (insert "</li>\n"))
  13862. ; (when (save-excursion
  13863. ; (re-search-backward "</?\\(ul\\|ol\\|li\\|[hH][0-9]\\)>" nil t))
  13864. ; (if (member (match-string 0) '("</ul>" "</ol>" "<li>"))
  13865. ; (insert "</li>"))))
  13866. (defun org-html-level-start (level title umax with-toc head-count)
  13867. "Insert a new level in HTML export.
  13868. When TITLE is nil, just close all open levels."
  13869. (org-close-par-maybe)
  13870. (let ((l (1+ (max level umax))))
  13871. (while (<= l org-level-max)
  13872. (if (aref levels-open (1- l))
  13873. (progn
  13874. (org-html-level-close l)
  13875. (aset levels-open (1- l) nil)))
  13876. (setq l (1+ l)))
  13877. (when title
  13878. ;; If title is nil, this means this function is called to close
  13879. ;; all levels, so the rest is done only if title is given
  13880. (when (string-match "\\(:[a-zA-Z0-9_@:]+:\\)[ \t]*$" title)
  13881. (setq title (replace-match
  13882. (if org-export-with-tags
  13883. (save-match-data
  13884. (concat
  13885. "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
  13886. (mapconcat 'identity (org-split-string
  13887. (match-string 1 title) ":")
  13888. "&nbsp;")
  13889. "</span>"))
  13890. "")
  13891. t t title)))
  13892. (if (> level umax)
  13893. (progn
  13894. (if (aref levels-open (1- level))
  13895. (progn
  13896. (org-close-li)
  13897. (insert "<li>" title "<br/>\n"))
  13898. (aset levels-open (1- level) t)
  13899. (org-close-par-maybe)
  13900. (insert "<ul>\n<li>" title "<br/>\n")))
  13901. (if org-export-with-section-numbers
  13902. (setq title (concat (org-section-number level) " " title)))
  13903. (setq level (+ level org-export-html-toplevel-hlevel -1))
  13904. (if with-toc
  13905. (insert (format "\n<h%d><a name=\"sec-%d\">%s</a></h%d>\n"
  13906. level head-count title level))
  13907. (insert (format "\n<h%d>%s</h%d>\n" level title level)))
  13908. (org-open-par)))))
  13909. (defun org-html-level-close (&rest args)
  13910. "Terminate one level in HTML export."
  13911. (org-close-li)
  13912. (insert "</ul>"))
  13913. ;; Variable holding the vector with section numbers
  13914. (defvar org-section-numbers (make-vector org-level-max 0))
  13915. (defun org-init-section-numbers ()
  13916. "Initialize the vector for the section numbers."
  13917. (let* ((level -1)
  13918. (numbers (nreverse (org-split-string "" "\\.")))
  13919. (depth (1- (length org-section-numbers)))
  13920. (i depth) number-string)
  13921. (while (>= i 0)
  13922. (if (> i level)
  13923. (aset org-section-numbers i 0)
  13924. (setq number-string (or (car numbers) "0"))
  13925. (if (string-match "\\`[A-Z]\\'" number-string)
  13926. (aset org-section-numbers i
  13927. (- (string-to-char number-string) ?A -1))
  13928. (aset org-section-numbers i (string-to-number number-string)))
  13929. (pop numbers))
  13930. (setq i (1- i)))))
  13931. (defun org-section-number (&optional level)
  13932. "Return a string with the current section number.
  13933. When LEVEL is non-nil, increase section numbers on that level."
  13934. (let* ((depth (1- (length org-section-numbers))) idx n (string ""))
  13935. (when level
  13936. (when (> level -1)
  13937. (aset org-section-numbers
  13938. level (1+ (aref org-section-numbers level))))
  13939. (setq idx (1+ level))
  13940. (while (<= idx depth)
  13941. (if (not (= idx 1))
  13942. (aset org-section-numbers idx 0))
  13943. (setq idx (1+ idx))))
  13944. (setq idx 0)
  13945. (while (<= idx depth)
  13946. (setq n (aref org-section-numbers idx))
  13947. (setq string (concat string (if (not (string= string "")) "." "")
  13948. (int-to-string n)))
  13949. (setq idx (1+ idx)))
  13950. (save-match-data
  13951. (if (string-match "\\`\\([@0]\\.\\)+" string)
  13952. (setq string (replace-match "" t nil string)))
  13953. (if (string-match "\\(\\.0\\)+\\'" string)
  13954. (setq string (replace-match "" t nil string))))
  13955. string))
  13956. ;;;###autoload
  13957. (defun org-export-icalendar-this-file ()
  13958. "Export current file as an iCalendar file.
  13959. The iCalendar file will be located in the same directory as the Org-mode
  13960. file, but with extension `.ics'."
  13961. (interactive)
  13962. (org-export-icalendar nil buffer-file-name))
  13963. (defun org-export-as-xoxo-insert-into (buffer &rest output)
  13964. (with-current-buffer buffer
  13965. (apply 'insert output)))
  13966. (put 'org-export-as-xoxo-insert-into 'lisp-indent-function 1)
  13967. (defun org-export-as-xoxo (&optional buffer)
  13968. "Export the org buffer as XOXO.
  13969. The XOXO buffer is named *xoxo-<source buffer name>*"
  13970. (interactive (list (current-buffer)))
  13971. ;; A quickie abstraction
  13972. ;; Output everything as XOXO
  13973. (with-current-buffer (get-buffer buffer)
  13974. (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
  13975. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  13976. (org-infile-export-plist)))
  13977. (filename (concat (file-name-as-directory
  13978. (org-export-directory :xoxo opt-plist))
  13979. (file-name-sans-extension
  13980. (file-name-nondirectory buffer-file-name))
  13981. ".html"))
  13982. (out (find-file-noselect filename))
  13983. (last-level 1)
  13984. (hanging-li nil))
  13985. ;; Check the output buffer is empty.
  13986. (with-current-buffer out (erase-buffer))
  13987. ;; Kick off the output
  13988. (org-export-as-xoxo-insert-into out "<ol class='xoxo'>\n")
  13989. (while (re-search-forward "^\\(\\*+\\) \\(.+\\)" (point-max) 't)
  13990. (let* ((hd (match-string-no-properties 1))
  13991. (level (length hd))
  13992. (text (concat
  13993. (match-string-no-properties 2)
  13994. (save-excursion
  13995. (goto-char (match-end 0))
  13996. (let ((str ""))
  13997. (catch 'loop
  13998. (while 't
  13999. (forward-line)
  14000. (if (looking-at "^[ \t]\\(.*\\)")
  14001. (setq str (concat str (match-string-no-properties 1)))
  14002. (throw 'loop str)))))))))
  14003. ;; Handle level rendering
  14004. (cond
  14005. ((> level last-level)
  14006. (org-export-as-xoxo-insert-into out "\n<ol>\n"))
  14007. ((< level last-level)
  14008. (dotimes (- (- last-level level) 1)
  14009. (if hanging-li
  14010. (org-export-as-xoxo-insert-into out "</li>\n"))
  14011. (org-export-as-xoxo-insert-into out "</ol>\n"))
  14012. (when hanging-li
  14013. (org-export-as-xoxo-insert-into out "</li>\n")
  14014. (setq hanging-li nil)))
  14015. ((equal level last-level)
  14016. (if hanging-li
  14017. (org-export-as-xoxo-insert-into out "</li>\n")))
  14018. )
  14019. (setq last-level level)
  14020. ;; And output the new li
  14021. (setq hanging-li 't)
  14022. (if (equal ?+ (elt text 0))
  14023. (org-export-as-xoxo-insert-into out "<li class='" (substring text 1) "'>")
  14024. (org-export-as-xoxo-insert-into out "<li>" text))))
  14025. ;; Finally finish off the ol
  14026. (dotimes (- last-level 1)
  14027. (if hanging-li
  14028. (org-export-as-xoxo-insert-into out "</li>\n"))
  14029. (org-export-as-xoxo-insert-into out "</ol>\n"))
  14030. ;; Finish the buffer off and clean it up.
  14031. (switch-to-buffer-other-window out)
  14032. (indent-region (point-min) (point-max) nil)
  14033. (save-buffer)
  14034. (goto-char (point-min))
  14035. )))
  14036. ;;;###autoload
  14037. (defun org-export-icalendar-all-agenda-files ()
  14038. "Export all files in `org-agenda-files' to iCalendar .ics files.
  14039. Each iCalendar file will be located in the same directory as the Org-mode
  14040. file, but with extension `.ics'."
  14041. (interactive)
  14042. (apply 'org-export-icalendar nil (org-agenda-files t)))
  14043. ;;;###autoload
  14044. (defun org-export-icalendar-combine-agenda-files ()
  14045. "Export all files in `org-agenda-files' to a single combined iCalendar file.
  14046. The file is stored under the name `org-combined-agenda-icalendar-file'."
  14047. (interactive)
  14048. (apply 'org-export-icalendar t (org-agenda-files t)))
  14049. (defun org-export-icalendar (combine &rest files)
  14050. "Create iCalendar files for all elements of FILES.
  14051. If COMBINE is non-nil, combine all calendar entries into a single large
  14052. file and store it under the name `org-combined-agenda-icalendar-file'."
  14053. (save-excursion
  14054. (let* ((dir (org-export-directory
  14055. :ical (list :publishing-directory
  14056. org-export-publishing-directory)))
  14057. file ical-file ical-buffer category started org-agenda-new-buffers)
  14058. (when combine
  14059. (setq ical-file
  14060. (if (file-name-absolute-p org-combined-agenda-icalendar-file)
  14061. org-combined-agenda-icalendar-file
  14062. (expand-file-name org-combined-agenda-icalendar-file dir))
  14063. ical-buffer (org-get-agenda-file-buffer ical-file))
  14064. (set-buffer ical-buffer) (erase-buffer))
  14065. (while (setq file (pop files))
  14066. (catch 'nextfile
  14067. (org-check-agenda-file file)
  14068. (set-buffer (org-get-agenda-file-buffer file))
  14069. (unless combine
  14070. (setq ical-file (concat (file-name-as-directory dir)
  14071. (file-name-sans-extension
  14072. (file-name-nondirectory buffer-file-name))
  14073. ".ics"))
  14074. (setq ical-buffer (org-get-agenda-file-buffer ical-file))
  14075. (with-current-buffer ical-buffer (erase-buffer)))
  14076. (setq category (or org-category
  14077. (file-name-sans-extension
  14078. (file-name-nondirectory buffer-file-name))))
  14079. (if (symbolp category) (setq category (symbol-name category)))
  14080. (let ((standard-output ical-buffer))
  14081. (if combine
  14082. (and (not started) (setq started t)
  14083. (org-start-icalendar-file org-icalendar-combined-name))
  14084. (org-start-icalendar-file category))
  14085. (org-print-icalendar-entries combine category)
  14086. (when (or (and combine (not files)) (not combine))
  14087. (org-finish-icalendar-file)
  14088. (set-buffer ical-buffer)
  14089. (save-buffer)
  14090. (run-hooks 'org-after-save-iCalendar-file-hook)))))
  14091. (org-release-buffers org-agenda-new-buffers))))
  14092. (defvar org-after-save-iCalendar-file-hook nil
  14093. "Hook run after an iCalendar file has been saved.
  14094. The iCalendar buffer is still current when this hook is run.
  14095. A good way to use this is to tell a desktop calenndar application to re-read
  14096. the iCalendar file.")
  14097. (defun org-print-icalendar-entries (&optional combine category)
  14098. "Print iCalendar entries for the current Org-mode file to `standard-output'.
  14099. When COMBINE is non nil, add the category to each line."
  14100. (let ((re2 (concat "--?-?\\(" org-ts-regexp "\\)"))
  14101. (dts (org-ical-ts-to-string
  14102. (format-time-string (cdr org-time-stamp-formats) (current-time))
  14103. "DTSTART"))
  14104. hd ts ts2 state (inc t) pos scheduledp deadlinep tmp pri)
  14105. (save-excursion
  14106. (goto-char (point-min))
  14107. (while (re-search-forward org-ts-regexp nil t)
  14108. (setq pos (match-beginning 0)
  14109. ts (match-string 0)
  14110. inc t
  14111. hd (org-get-heading))
  14112. (if (looking-at re2)
  14113. (progn
  14114. (goto-char (match-end 0))
  14115. (setq ts2 (match-string 1) inc nil))
  14116. (setq ts2 ts
  14117. tmp (buffer-substring (max (point-min)
  14118. (- pos org-ds-keyword-length))
  14119. pos)
  14120. deadlinep (string-match org-deadline-regexp tmp)
  14121. scheduledp (string-match org-scheduled-regexp tmp)
  14122. ;; donep (org-entry-is-done-p)
  14123. ))
  14124. (if (or (string-match org-tr-regexp hd)
  14125. (string-match org-ts-regexp hd))
  14126. (setq hd (replace-match "" t t hd)))
  14127. (if combine
  14128. (setq hd (concat hd " (category " category ")")))
  14129. (if deadlinep (setq hd (concat "DL: " hd " This is a deadline")))
  14130. (if scheduledp (setq hd (concat "S: " hd " Scheduled for this date")))
  14131. (princ (format "BEGIN:VEVENT
  14132. %s
  14133. %s
  14134. SUMMARY:%s
  14135. END:VEVENT\n"
  14136. (org-ical-ts-to-string ts "DTSTART")
  14137. (org-ical-ts-to-string ts2 "DTEND" inc)
  14138. hd)))
  14139. (when org-icalendar-include-todo
  14140. (goto-char (point-min))
  14141. (while (re-search-forward org-todo-line-regexp nil t)
  14142. (setq state (match-string 1))
  14143. (unless (equal state org-done-string)
  14144. (setq hd (match-string 3))
  14145. (if (string-match org-priority-regexp hd)
  14146. (setq pri (string-to-char (match-string 2 hd))
  14147. hd (concat (substring hd 0 (match-beginning 1))
  14148. (substring hd (- (match-end 1)))))
  14149. (setq pri org-default-priority))
  14150. (setq pri (floor (1+ (* 8. (/ (float (- org-lowest-priority pri))
  14151. (- org-lowest-priority ?A))))))
  14152. (princ (format "BEGIN:VTODO
  14153. %s
  14154. SUMMARY:%s
  14155. SEQUENCE:1
  14156. PRIORITY:%d
  14157. END:VTODO\n"
  14158. dts hd pri))))))))
  14159. (defun org-start-icalendar-file (name)
  14160. "Start an iCalendar file by inserting the header."
  14161. (let ((user user-full-name)
  14162. (name (or name "unknown"))
  14163. (timezone (cadr (current-time-zone))))
  14164. (princ
  14165. (format "BEGIN:VCALENDAR
  14166. VERSION:2.0
  14167. X-WR-CALNAME:%s
  14168. PRODID:-//%s//Emacs with Org-mode//EN
  14169. X-WR-TIMEZONE:%s
  14170. CALSCALE:GREGORIAN\n" name user timezone))))
  14171. (defun org-finish-icalendar-file ()
  14172. "Finish an iCalendar file by inserting the END statement."
  14173. (princ "END:VCALENDAR\n"))
  14174. (defun org-ical-ts-to-string (s keyword &optional inc)
  14175. "Take a time string S and convert it to iCalendar format.
  14176. KEYWORD is added in front, to make a complete line like DTSTART....
  14177. When INC is non-nil, increase the hour by two (if time string contains
  14178. a time), or the day by one (if it does not contain a time)."
  14179. (let ((t1 (org-parse-time-string s 'nodefault))
  14180. t2 fmt have-time time)
  14181. (if (and (car t1) (nth 1 t1) (nth 2 t1))
  14182. (setq t2 t1 have-time t)
  14183. (setq t2 (org-parse-time-string s)))
  14184. (let ((s (car t2)) (mi (nth 1 t2)) (h (nth 2 t2))
  14185. (d (nth 3 t2)) (m (nth 4 t2)) (y (nth 5 t2)))
  14186. (when inc
  14187. (if have-time (setq h (+ 2 h)) (setq d (1+ d))))
  14188. (setq time (encode-time s mi h d m y)))
  14189. (setq fmt (if have-time ":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
  14190. (concat keyword (format-time-string fmt time))))
  14191. ;;; LaTeX stuff
  14192. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  14193. "Keymap for the minor `org-cdlatex-mode'.")
  14194. (define-key org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  14195. (define-key org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  14196. (define-key org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  14197. (define-key org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  14198. (define-key org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  14199. (defvar org-cdlatex-texmathp-advice-is-done nil
  14200. "Flag remembering if we have applied the advice to texmathp already.")
  14201. (define-minor-mode org-cdlatex-mode
  14202. "Toggle the minor `org-cdlatex-mode'.
  14203. This mode supports entering LaTeX environment and math in LaTeX fragments
  14204. in Org-mode.
  14205. \\{org-cdlatex-mode-map}"
  14206. nil " OCDL" nil
  14207. (when org-cdlatex-mode (require 'cdlatex))
  14208. (unless org-cdlatex-texmathp-advice-is-done
  14209. (setq org-cdlatex-texmathp-advice-is-done t)
  14210. (defadvice texmathp (around org-math-always-on activate)
  14211. "Always return t in org-mode buffers.
  14212. This is because we want to insert math symbols without dollars even outside
  14213. the LaTeX math segments. If Orgmode thinks that point is actually inside
  14214. en embedded LaTeX fragement, let texmathp do its job.
  14215. \\[org-cdlatex-mode-map]"
  14216. (interactive)
  14217. (let (p)
  14218. (cond
  14219. ((not (org-mode-p)) ad-do-it)
  14220. ((eq this-command 'cdlatex-math-symbol)
  14221. (setq ad-return-value t
  14222. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  14223. (t
  14224. (let ((p (org-inside-LaTeX-fragment-p)))
  14225. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  14226. (setq ad-return-value t
  14227. texmathp-why '("Org-mode embedded math" . 0))
  14228. (if p ad-do-it)))))))))
  14229. (defun turn-on-org-cdlatex ()
  14230. "Unconditionally turn on `org-cdlatex-mode'."
  14231. (org-cdlatex-mode 1))
  14232. (defun org-inside-LaTeX-fragment-p ()
  14233. "Test if point is inside a LaTeX fragment.
  14234. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  14235. sequence appearing also before point.
  14236. Even though the matchers for math are configurable, this function assumes
  14237. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  14238. delimiters are skipped when they have been removed by customization.
  14239. The return value is nil, or a cons cell with the delimiter and
  14240. and the position of this delimiter.
  14241. This function does a reasonably good job, but can locally be fooled by
  14242. for example currency specifications. For example it will assume being in
  14243. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  14244. fragments that are properly closed, but during editing, we have to live
  14245. with the uncertainty caused by missing closing delimiters. This function
  14246. looks only before point, not after."
  14247. (catch 'exit
  14248. (let ((pos (point))
  14249. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  14250. (lim (progn
  14251. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  14252. (point)))
  14253. dd-on str (start 0) m re)
  14254. (goto-char pos)
  14255. (when dodollar
  14256. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  14257. re (nth 1 (assoc "$" org-latex-regexps)))
  14258. (while (string-match re str start)
  14259. (cond
  14260. ((= (match-end 0) (length str))
  14261. (throw 'exit (cons "$" (+ lim (match-beginning 0)))))
  14262. ((= (match-end 0) (- (length str) 5))
  14263. (throw 'exit nil))
  14264. (t (setq start (match-end 0))))))
  14265. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  14266. (goto-char pos)
  14267. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  14268. (and (match-beginning 2) (throw 'exit nil))
  14269. ;; count $$
  14270. (while (re-search-backward "\\$\\$" lim t)
  14271. (setq dd-on (not dd-on)))
  14272. (goto-char pos)
  14273. (if dd-on (cons "$$" m))))))
  14274. (defun org-try-cdlatex-tab ()
  14275. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  14276. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  14277. - inside a LaTeX fragment, or
  14278. - after the first word in a line, where an abbreviation expansion could
  14279. insert a LaTeX environment."
  14280. ;; FIXME: This may still need refinement.
  14281. (when org-cdlatex-mode
  14282. (cond
  14283. ((save-excursion
  14284. (skip-chars-backward "a-zA-Z0-9*")
  14285. (skip-chars-backward " \t")
  14286. (bolp))
  14287. (cdlatex-tab) t)
  14288. ((org-inside-LaTeX-fragment-p)
  14289. (cdlatex-tab) t)
  14290. (t nil))))
  14291. (defun org-cdlatex-underscore-caret (&optional arg)
  14292. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  14293. Revert to the normal definition outside of these fragments."
  14294. (interactive "P")
  14295. (if (org-inside-LaTeX-fragment-p)
  14296. (call-interactively 'cdlatex-sub-superscript)
  14297. (let (org-cdlatex-mode)
  14298. (call-interactively (key-binding (vector last-input-event))))))
  14299. (defun org-cdlatex-math-modify (&optional arg)
  14300. "Execute `cdlatex-math-modify' in LaTeX fragments.
  14301. Revert to the normal definition outside of these fragments."
  14302. (interactive "P")
  14303. (if (org-inside-LaTeX-fragment-p)
  14304. (call-interactively 'cdlatex-math-modify)
  14305. (let (org-cdlatex-mode)
  14306. (call-interactively (key-binding (vector last-input-event))))))
  14307. (defvar org-latex-fragment-image-overlays nil
  14308. "List of overlays carrying the images of latex fragments.")
  14309. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  14310. (defun org-remove-latex-fragment-image-overlays ()
  14311. "Remove all overlays with LaTeX fragment images in current buffer."
  14312. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  14313. (setq org-latex-fragment-image-overlays nil))
  14314. (defun org-preview-latex-fragment (&optional subtree)
  14315. "Preview the LaTeX fragment at point, or all locally or globally.
  14316. If the cursor is in a LaTeX fragment, create the image and overlay
  14317. it over the source code. If there is no fragment at point, display
  14318. all fragments in the current text, from one headline to the next. With
  14319. prefix SUBTREE, display all fragments in the current subtree. With a
  14320. double prefix `C-u C-u', or when the cursor is before the first headline,
  14321. display all fragments in the buffer.
  14322. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  14323. (interactive "P")
  14324. (org-remove-latex-fragment-image-overlays)
  14325. (save-excursion
  14326. (save-restriction
  14327. (let (beg end at msg)
  14328. (cond
  14329. ((or (equal subtree '(16))
  14330. (not (save-excursion
  14331. (re-search-backward (concat "^" outline-regexp) nil t))))
  14332. (setq beg (point-min) end (point-max)
  14333. msg "Creating images for buffer...%s"))
  14334. ((equal subtree '(4))
  14335. (org-back-to-heading)
  14336. (setq beg (point) end (org-end-of-subtree)
  14337. msg "Creating images for subtree...%s"))
  14338. (t
  14339. (if (setq at (org-inside-LaTeX-fragment-p))
  14340. (goto-char (max (point-min) (- (cdr at) 2)))
  14341. (org-back-to-heading))
  14342. (setq beg (point) end (progn (outline-next-heading) (point))
  14343. msg (if at "Creating image...%s"
  14344. "Creating images for entry...%s"))))
  14345. (message msg "")
  14346. (narrow-to-region beg end)
  14347. (org-format-latex
  14348. (concat "ltxpng/" (file-name-sans-extension
  14349. (file-name-nondirectory
  14350. buffer-file-name)))
  14351. default-directory 'overlays msg at)
  14352. (message msg "done. Use `C-c C-c' to remove images.")))))
  14353. (defvar org-latex-regexps
  14354. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  14355. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  14356. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  14357. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  14358. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  14359. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  14360. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  14361. "Regular expressions for matching embedded LaTeX.")
  14362. (defun org-format-latex (prefix &optional dir overlays msg at)
  14363. "Replace LaTeX fragments with links to an image, and produce images."
  14364. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  14365. (let* ((prefixnodir (file-name-nondirectory prefix))
  14366. (absprefix (expand-file-name prefix dir))
  14367. (todir (file-name-directory absprefix))
  14368. (opt org-format-latex-options)
  14369. (matchers (plist-get opt :matchers))
  14370. (re-list org-latex-regexps)
  14371. (cnt 0) txt link beg end re e oldfiles
  14372. m n block linkfile movefile ov)
  14373. ;; Make sure the directory exists
  14374. (or (file-directory-p todir) (make-directory todir))
  14375. ;; Check if there are old images files with this prefix, and remove them
  14376. (setq oldfiles (directory-files
  14377. todir 'full
  14378. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$")))
  14379. (while oldfiles (delete-file (pop oldfiles)))
  14380. ;; Check the different regular expressions
  14381. (while (setq e (pop re-list))
  14382. (setq m (car e) re (nth 1 e) n (nth 2 e)
  14383. block (if (nth 3 e) "\n\n" ""))
  14384. (when (member m matchers)
  14385. (goto-char (point-min))
  14386. (while (re-search-forward re nil t)
  14387. (when (or (not at) (equal (cdr at) (match-beginning n)))
  14388. (setq txt (match-string n)
  14389. beg (match-beginning n) end (match-end n)
  14390. cnt (1+ cnt)
  14391. linkfile (format "%s_%04d.png" prefix cnt)
  14392. movefile (format "%s_%04d.png" absprefix cnt)
  14393. link (concat block "[[file:" linkfile "]]" block))
  14394. (if msg (message msg cnt))
  14395. (goto-char beg)
  14396. (org-create-formula-image
  14397. txt movefile opt)
  14398. (if overlays
  14399. (progn
  14400. (setq ov (org-make-overlay beg end))
  14401. (if (featurep 'xemacs)
  14402. (progn
  14403. (org-overlay-put ov 'invisible t)
  14404. (org-overlay-put
  14405. ov 'end-glyph
  14406. (make-glyph (vector 'png :file movefile))))
  14407. (org-overlay-put
  14408. ov 'display
  14409. (list 'image :type 'png :file movefile :ascent 'center)))
  14410. (push ov org-latex-fragment-image-overlays)
  14411. (goto-char end))
  14412. (delete-region beg end)
  14413. (insert link))))))))
  14414. ;; This function borrows from Ganesh Swami's latex2png.el
  14415. (defun org-create-formula-image (string tofile options)
  14416. (let* ((tmpdir (if (featurep 'xemacs)
  14417. (temp-directory)
  14418. temporary-file-directory))
  14419. (texfilebase (make-temp-name
  14420. (expand-file-name "orgtex" tmpdir)))
  14421. ;(texfilebase (make-temp-file "orgtex"))
  14422. ; (dummy (delete-file texfilebase))
  14423. (texfile (concat texfilebase ".tex"))
  14424. (dvifile (concat texfilebase ".dvi"))
  14425. (pngfile (concat texfilebase ".png"))
  14426. (scale (number-to-string (* 1000 (or (plist-get options :scale) 1.0))))
  14427. (fg (or (plist-get options :foreground) "Black"))
  14428. (bg (or (plist-get options :background) "Transparent")))
  14429. (with-temp-file texfile
  14430. (insert "\\documentclass{article}
  14431. \\usepackage{fullpage}
  14432. \\usepackage{amssymb}
  14433. \\usepackage[usenames]{color}
  14434. \\usepackage{amsmath}
  14435. \\usepackage{latexsym}
  14436. \\usepackage[mathscr]{eucal}
  14437. \\pagestyle{empty}
  14438. \\begin{document}\n" string "\n\\end{document}\n"))
  14439. (let ((dir default-directory))
  14440. (condition-case nil
  14441. (progn
  14442. (cd tmpdir)
  14443. (call-process "latex" nil nil nil texfile))
  14444. (error nil))
  14445. (cd dir))
  14446. (if (not (file-exists-p dvifile))
  14447. (progn (message "Failed to create dvi file from %s" texfile) nil)
  14448. (call-process "dvipng" nil nil nil
  14449. "-E" "-fg" fg "-bg" bg
  14450. "-x" scale "-y" scale "-T" "tight"
  14451. "-o" pngfile
  14452. dvifile)
  14453. (if (not (file-exists-p pngfile))
  14454. (progn (message "Failed to create png file from %s" texfile) nil)
  14455. ;; Use the requested file name and clean up
  14456. (copy-file pngfile tofile 'replace)
  14457. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  14458. (delete-file (concat texfilebase e)))
  14459. pngfile))))
  14460. ;;; Key bindings
  14461. ;; - Bindings in Org-mode map are currently
  14462. ;; 0123456789abcdefghijklmnopqrstuvwxyz!?@#$%^&-+*/=()_{}[]:;"|,.<>~`'\t the alphabet
  14463. ;; abcd fgh j lmnopqrstuvwxyz!? #$ ^ -+*/= [] ; |,.<>~ '\t necessary bindings
  14464. ;; e (?) useful from outline-mode
  14465. ;; i k @ expendable from outline-mode
  14466. ;; 0123456789 % & ()_{} " ` free
  14467. ;; Make `C-c C-x' a prefix key
  14468. (define-key org-mode-map "\C-c\C-x" (make-sparse-keymap))
  14469. ;; TAB key with modifiers
  14470. (define-key org-mode-map "\C-i" 'org-cycle)
  14471. (define-key org-mode-map [(tab)] 'org-cycle)
  14472. (define-key org-mode-map [(control tab)] 'org-force-cycle-archived)
  14473. (define-key org-mode-map [(meta tab)] 'org-complete)
  14474. (define-key org-mode-map "\M-\C-i" 'org-complete) ; for tty emacs
  14475. ;; The following line is necessary under Suse GNU/Linux
  14476. (unless (featurep 'xemacs)
  14477. (define-key org-mode-map [S-iso-lefttab] 'org-shifttab))
  14478. (define-key org-mode-map [(shift tab)] 'org-shifttab)
  14479. (define-key org-mode-map (org-key 'S-return) 'org-table-copy-down)
  14480. (define-key org-mode-map "\C-c\C-xc" 'org-table-copy-down) ; tty
  14481. (define-key org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  14482. (define-key org-mode-map "\C-c\C-xM" 'org-insert-todo-heading) ; tty
  14483. (define-key org-mode-map [(meta return)] 'org-meta-return)
  14484. (define-key org-mode-map "\C-c\C-xm" 'org-meta-return) ; tty emacs
  14485. (define-key org-mode-map [?\e (return)] 'org-meta-return) ; tty emacs
  14486. ;; Cursor keys with modifiers
  14487. (define-key org-mode-map [(meta left)] 'org-metaleft)
  14488. (define-key org-mode-map [?\e (left)] 'org-metaleft) ; for tty emacs
  14489. (define-key org-mode-map "\C-c\C-xl" 'org-metaleft) ; for tty emacs
  14490. (define-key org-mode-map [(meta right)] 'org-metaright)
  14491. (define-key org-mode-map [?\e (right)] 'org-metaright) ; for tty emacs
  14492. (define-key org-mode-map "\C-c\C-xr" 'org-metaright) ; for tty emacs
  14493. (define-key org-mode-map [(meta up)] 'org-metaup)
  14494. (define-key org-mode-map [?\e (up)] 'org-metaup) ; for tty emacs
  14495. (define-key org-mode-map "\C-c\C-xu" 'org-metaup) ; for tty emacs
  14496. (define-key org-mode-map [(meta down)] 'org-metadown)
  14497. (define-key org-mode-map [?\e (down)] 'org-metadown) ; for tty emacs
  14498. (define-key org-mode-map "\C-c\C-xd" 'org-metadown) ; for tty emacs
  14499. (define-key org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  14500. (define-key org-mode-map "\C-c\C-xL" 'org-shiftmetaleft) ; tty
  14501. (define-key org-mode-map [(meta shift right)] 'org-shiftmetaright)
  14502. (define-key org-mode-map "\C-c\C-xR" 'org-shiftmetaright) ; tty
  14503. (define-key org-mode-map [(meta shift up)] 'org-shiftmetaup)
  14504. (define-key org-mode-map "\C-c\C-xU" 'org-shiftmetaup) ; tty
  14505. (define-key org-mode-map [(meta shift down)] 'org-shiftmetadown)
  14506. (define-key org-mode-map "\C-c\C-xD" 'org-shiftmetadown) ; tty
  14507. (define-key org-mode-map (org-key 'S-up) 'org-shiftup)
  14508. (define-key org-mode-map [?\C-c ?\C-x (up)] 'org-shiftup)
  14509. (define-key org-mode-map (org-key 'S-down) 'org-shiftdown)
  14510. (define-key org-mode-map [?\C-c ?\C-x (down)] 'org-shiftdown)
  14511. (define-key org-mode-map (org-key 'S-left) 'org-shiftleft)
  14512. (define-key org-mode-map [?\C-c ?\C-x (left)] 'org-shiftleft)
  14513. (define-key org-mode-map (org-key 'S-right) 'org-shiftright)
  14514. (define-key org-mode-map [?\C-c ?\C-x (right)] 'org-shiftright)
  14515. ;; All the other keys
  14516. (define-key org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  14517. (define-key org-mode-map "\C-xns" 'org-narrow-to-subtree)
  14518. (define-key org-mode-map "\C-c$" 'org-archive-subtree)
  14519. (define-key org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  14520. (define-key org-mode-map "\C-c\C-j" 'org-goto)
  14521. (define-key org-mode-map "\C-c\C-t" 'org-todo)
  14522. (define-key org-mode-map "\C-c\C-s" 'org-schedule)
  14523. (define-key org-mode-map "\C-c\C-d" 'org-deadline)
  14524. (define-key org-mode-map "\C-c;" 'org-toggle-comment)
  14525. (define-key org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  14526. (define-key org-mode-map "\C-c\C-w" 'org-check-deadlines)
  14527. (define-key org-mode-map "\C-c/" 'org-occur) ; Minor-mode reserved
  14528. (define-key org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  14529. (define-key org-mode-map "\C-c\C-m" 'org-insert-heading)
  14530. (define-key org-mode-map "\M-\C-m" 'org-insert-heading)
  14531. (define-key org-mode-map "\C-c\C-l" 'org-insert-link)
  14532. (define-key org-mode-map "\C-c\C-o" 'org-open-at-point)
  14533. (define-key org-mode-map "\C-c%" 'org-mark-ring-push)
  14534. (define-key org-mode-map "\C-c&" 'org-mark-ring-goto)
  14535. (define-key org-mode-map "\C-c\C-z" 'org-time-stamp) ; Alternative binding
  14536. (define-key org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  14537. (define-key org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  14538. (define-key org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  14539. (define-key org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  14540. (define-key org-mode-map "\C-c>" 'org-goto-calendar)
  14541. (define-key org-mode-map "\C-c<" 'org-date-from-calendar)
  14542. (define-key org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  14543. (define-key org-mode-map "\C-c[" 'org-agenda-file-to-front)
  14544. (define-key org-mode-map "\C-c]" 'org-remove-file)
  14545. (define-key org-mode-map "\C-c-" 'org-table-insert-hline)
  14546. (define-key org-mode-map "\C-c^" 'org-table-sort-lines)
  14547. (define-key org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  14548. (define-key org-mode-map "\C-c#" 'org-update-checkbox-count)
  14549. (define-key org-mode-map "\C-m" 'org-return)
  14550. (define-key org-mode-map "\C-c?" 'org-table-current-column)
  14551. (define-key org-mode-map "\C-c " 'org-table-blank-field)
  14552. (define-key org-mode-map "\C-c+" 'org-table-sum)
  14553. (define-key org-mode-map "\C-c=" 'org-table-eval-formula)
  14554. (define-key org-mode-map "\C-c'" 'org-table-edit-formulas)
  14555. (define-key org-mode-map "\C-c`" 'org-table-edit-field)
  14556. (define-key org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  14557. (define-key org-mode-map "\C-c*" 'org-table-recalculate)
  14558. (define-key org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  14559. (define-key org-mode-map "\C-c~" 'org-table-create-with-table.el)
  14560. (define-key org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  14561. (define-key org-mode-map "\C-c\C-e" 'org-export)
  14562. (define-key org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  14563. (define-key org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  14564. (define-key org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  14565. (define-key org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  14566. (define-key org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  14567. (define-key org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  14568. (define-key org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  14569. (define-key org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  14570. (define-key org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  14571. (define-key org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  14572. (define-key org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  14573. (define-key org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  14574. (define-key org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  14575. (when (featurep 'xemacs)
  14576. (define-key org-mode-map 'button3 'popup-mode-menu))
  14577. (defsubst org-table-p () (org-at-table-p))
  14578. (defun org-self-insert-command (N)
  14579. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  14580. If the cursor is in a table looking at whitespace, the whitespace is
  14581. overwritten, and the table is not marked as requiring realignment."
  14582. (interactive "p")
  14583. (if (and (org-table-p)
  14584. (progn
  14585. ;; check if we blank the field, and if that triggers align
  14586. (and org-table-auto-blank-field
  14587. (member last-command
  14588. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  14589. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  14590. ;; got extra space, this field does not determine column width
  14591. (let (org-table-may-need-update) (org-table-blank-field))
  14592. ;; no extra space, this field may determine column width
  14593. (org-table-blank-field)))
  14594. t)
  14595. (eq N 1)
  14596. (looking-at "[^|\n]* |"))
  14597. (let (org-table-may-need-update)
  14598. (goto-char (1- (match-end 0)))
  14599. (delete-backward-char 1)
  14600. (goto-char (match-beginning 0))
  14601. (self-insert-command N))
  14602. (setq org-table-may-need-update t)
  14603. (self-insert-command N)))
  14604. (defun org-delete-backward-char (N)
  14605. "Like `delete-backward-char', insert whitespace at field end in tables.
  14606. When deleting backwards, in tables this function will insert whitespace in
  14607. front of the next \"|\" separator, to keep the table aligned. The table will
  14608. still be marked for re-alignment if the field did fill the entire column,
  14609. because, in this case the deletion might narrow the column."
  14610. (interactive "p")
  14611. (if (and (org-table-p)
  14612. (eq N 1)
  14613. (string-match "|" (buffer-substring (point-at-bol) (point)))
  14614. (looking-at ".*?|"))
  14615. (let ((pos (point))
  14616. (noalign (looking-at "[^|\n\r]* |"))
  14617. (c org-table-may-need-update))
  14618. (backward-delete-char N)
  14619. (skip-chars-forward "^|")
  14620. (insert " ")
  14621. (goto-char (1- pos))
  14622. ;; noalign: if there were two spaces at the end, this field
  14623. ;; does not determine the width of the column.
  14624. (if noalign (setq org-table-may-need-update c)))
  14625. (backward-delete-char N)))
  14626. (defun org-delete-char (N)
  14627. "Like `delete-char', but insert whitespace at field end in tables.
  14628. When deleting characters, in tables this function will insert whitespace in
  14629. front of the next \"|\" separator, to keep the table aligned. The table will
  14630. still be marked for re-alignment if the field did fill the entire column,
  14631. because, in this case the deletion might narrow the column."
  14632. (interactive "p")
  14633. (if (and (org-table-p)
  14634. (not (bolp))
  14635. (not (= (char-after) ?|))
  14636. (eq N 1))
  14637. (if (looking-at ".*?|")
  14638. (let ((pos (point))
  14639. (noalign (looking-at "[^|\n\r]* |"))
  14640. (c org-table-may-need-update))
  14641. (replace-match (concat
  14642. (substring (match-string 0) 1 -1)
  14643. " |"))
  14644. (goto-char pos)
  14645. ;; noalign: if there were two spaces at the end, this field
  14646. ;; does not determine the width of the column.
  14647. (if noalign (setq org-table-may-need-update c)))
  14648. (delete-char N))
  14649. (delete-char N)))
  14650. ;; How to do this: Measure non-white length of current string
  14651. ;; If equal to column width, we should realign.
  14652. (defun org-remap (map &rest commands)
  14653. "In MAP, remap the functions given in COMMANDS.
  14654. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  14655. (let (new old)
  14656. (while commands
  14657. (setq old (pop commands) new (pop commands))
  14658. (if (fboundp 'command-remapping)
  14659. (define-key map (vector 'remap old) new)
  14660. (substitute-key-definition old new map global-map)))))
  14661. (when (eq org-enable-table-editor 'optimized)
  14662. ;; If the user wants maximum table support, we need to hijack
  14663. ;; some standard editing functions
  14664. (org-remap org-mode-map
  14665. 'self-insert-command 'org-self-insert-command
  14666. 'delete-char 'org-delete-char
  14667. 'delete-backward-char 'org-delete-backward-char)
  14668. (define-key org-mode-map "|" 'org-force-self-insert))
  14669. (defun org-shiftcursor-error ()
  14670. "Throw an error because Shift-Cursor command was applied in wrong context."
  14671. (error "This command is active in special context like tables, headlines or timestamps"))
  14672. (defun org-shifttab (&optional arg)
  14673. "Global visibility cycling or move to previous table field.
  14674. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  14675. on context.
  14676. See the individual commands for more information."
  14677. (interactive "P")
  14678. (cond
  14679. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  14680. (t (call-interactively 'org-global-cycle))))
  14681. (defun org-shiftmetaleft ()
  14682. "Promote subtree or delete table column.
  14683. Calls `org-promote-subtree' or `org-table-delete-column', depending on context.
  14684. See the individual commands for more information."
  14685. (interactive)
  14686. (cond
  14687. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  14688. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  14689. ((org-at-item-p) (call-interactively 'org-outdent-item))
  14690. (t (org-shiftcursor-error))))
  14691. (defun org-shiftmetaright ()
  14692. "Demote subtree or insert table column.
  14693. Calls `org-demote-subtree' or `org-table-insert-column', depending on context.
  14694. See the individual commands for more information."
  14695. (interactive)
  14696. (cond
  14697. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  14698. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  14699. ((org-at-item-p) (call-interactively 'org-indent-item))
  14700. (t (org-shiftcursor-error))))
  14701. (defun org-shiftmetaup (&optional arg)
  14702. "Move subtree up or kill table row.
  14703. Calls `org-move-subtree-up' or `org-table-kill-row' or
  14704. `org-move-item-up' depending on context. See the individual commands
  14705. for more information."
  14706. (interactive "P")
  14707. (cond
  14708. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  14709. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  14710. ((org-at-item-p) (call-interactively 'org-move-item-up))
  14711. (t (org-shiftcursor-error))))
  14712. (defun org-shiftmetadown (&optional arg)
  14713. "Move subtree down or insert table row.
  14714. Calls `org-move-subtree-down' or `org-table-insert-row' or
  14715. `org-move-item-down', depending on context. See the individual
  14716. commands for more information."
  14717. (interactive "P")
  14718. (cond
  14719. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  14720. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  14721. ((org-at-item-p) (call-interactively 'org-move-item-down))
  14722. (t (org-shiftcursor-error))))
  14723. (defun org-metaleft (&optional arg)
  14724. "Promote heading or move table column to left.
  14725. Calls `org-do-promote' or `org-table-move-column', depending on context.
  14726. With no specific context, calls the Emacs default `backward-word'.
  14727. See the individual commands for more information."
  14728. (interactive "P")
  14729. (cond
  14730. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  14731. ((or (org-on-heading-p) (org-region-active-p))
  14732. (call-interactively 'org-do-promote))
  14733. (t (call-interactively 'backward-word))))
  14734. (defun org-metaright (&optional arg)
  14735. "Demote subtree or move table column to right.
  14736. Calls `org-do-demote' or `org-table-move-column', depending on context.
  14737. With no specific context, calls the Emacs default `forward-word'.
  14738. See the individual commands for more information."
  14739. (interactive "P")
  14740. (cond
  14741. ((org-at-table-p) (call-interactively 'org-table-move-column))
  14742. ((or (org-on-heading-p) (org-region-active-p))
  14743. (call-interactively 'org-do-demote))
  14744. (t (call-interactively 'forward-word))))
  14745. (defun org-metaup (&optional arg)
  14746. "Move subtree up or move table row up.
  14747. Calls `org-move-subtree-up' or `org-table-move-row' or
  14748. `org-move-item-up', depending on context. See the individual commands
  14749. for more information."
  14750. (interactive "P")
  14751. (cond
  14752. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  14753. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  14754. ((org-at-item-p) (call-interactively 'org-move-item-up))
  14755. (t (org-shiftcursor-error))))
  14756. (defun org-metadown (&optional arg)
  14757. "Move subtree down or move table row down.
  14758. Calls `org-move-subtree-down' or `org-table-move-row' or
  14759. `org-move-item-down', depending on context. See the individual
  14760. commands for more information."
  14761. (interactive "P")
  14762. (cond
  14763. ((org-at-table-p) (call-interactively 'org-table-move-row))
  14764. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  14765. ((org-at-item-p) (call-interactively 'org-move-item-down))
  14766. (t (org-shiftcursor-error))))
  14767. (defun org-shiftup (&optional arg)
  14768. "Increase item in timestamp or increase priority of current headline.
  14769. Calls `org-timestamp-up' or `org-priority-up', depending on context.
  14770. See the individual commands for more information."
  14771. (interactive "P")
  14772. (cond
  14773. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up))
  14774. ((org-on-heading-p) (call-interactively 'org-priority-up))
  14775. ((org-at-item-p) (call-interactively 'org-previous-item))
  14776. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  14777. (defun org-shiftdown (&optional arg)
  14778. "Decrease item in timestamp or decrease priority of current headline.
  14779. Calls `org-timestamp-down' or `org-priority-down', depending on context.
  14780. See the individual commands for more information."
  14781. (interactive "P")
  14782. (cond
  14783. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down))
  14784. ((org-on-heading-p) (call-interactively 'org-priority-down))
  14785. (t (call-interactively 'org-next-item))))
  14786. (defun org-shiftright ()
  14787. "Next TODO keyword or timestamp one day later, depending on context."
  14788. (interactive)
  14789. (cond
  14790. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  14791. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  14792. (t (org-shiftcursor-error))))
  14793. (defun org-shiftleft ()
  14794. "Previous TODO keyword or timestamp one day earlier, depending on context."
  14795. (interactive)
  14796. (cond
  14797. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  14798. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  14799. (t (org-shiftcursor-error))))
  14800. (defun org-copy-special ()
  14801. "Copy region in table or copy current subtree.
  14802. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  14803. See the individual commands for more information."
  14804. (interactive)
  14805. (call-interactively
  14806. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  14807. (defun org-cut-special ()
  14808. "Cut region in table or cut current subtree.
  14809. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  14810. See the individual commands for more information."
  14811. (interactive)
  14812. (call-interactively
  14813. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  14814. (defun org-paste-special (arg)
  14815. "Paste rectangular region into table, or past subtree relative to level.
  14816. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  14817. See the individual commands for more information."
  14818. (interactive "P")
  14819. (if (org-at-table-p)
  14820. (org-table-paste-rectangle)
  14821. (org-paste-subtree arg)))
  14822. (defun org-ctrl-c-ctrl-c (&optional arg)
  14823. "Set tags in headline, or update according to changed information at point.
  14824. This command does many different things, depending on context:
  14825. - If the cursor is in a headline, prompt for tags and insert them
  14826. into the current line, aligned to `org-tags-column'. When called
  14827. with prefix arg, realign all tags in the current buffer.
  14828. - If the cursor is in one of the special #+KEYWORD lines, this
  14829. triggers scanning the buffer for these lines and updating the
  14830. information.
  14831. - If the cursor is inside a table, realign the table. This command
  14832. works even if the automatic table editor has been turned off.
  14833. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  14834. the entire table.
  14835. - If the cursor is inside a table created by the table.el package,
  14836. activate that table.
  14837. - If the current buffer is a remember buffer, close note and file it.
  14838. with a prefix argument, file it without further interaction to the default
  14839. location.
  14840. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  14841. links in this buffer.
  14842. - If the cursor is on a numbered item in a plain list, renumber the
  14843. ordered list."
  14844. (interactive "P")
  14845. (let ((org-enable-table-editor t))
  14846. (cond
  14847. ((or org-clock-overlays org-occur-highlights
  14848. org-latex-fragment-image-overlays)
  14849. (org-remove-clock-overlays)
  14850. (org-remove-occur-highlights)
  14851. (org-remove-latex-fragment-image-overlays)
  14852. (message "Temporary highlights/overlays removed from current buffer"))
  14853. ((and (local-variable-p 'org-finish-function (current-buffer))
  14854. (fboundp org-finish-function))
  14855. (funcall org-finish-function))
  14856. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  14857. ((org-on-heading-p) (call-interactively 'org-set-tags))
  14858. ((org-at-table.el-p)
  14859. (require 'table)
  14860. (beginning-of-line 1)
  14861. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  14862. (call-interactively 'table-recognize-table))
  14863. ((org-at-table-p)
  14864. (org-table-maybe-eval-formula)
  14865. (if arg
  14866. (call-interactively 'org-table-recalculate)
  14867. (org-table-maybe-recalculate-line))
  14868. (call-interactively 'org-table-align))
  14869. ((org-at-item-checkbox-p)
  14870. (call-interactively 'org-toggle-checkbox))
  14871. ((org-at-item-p)
  14872. (call-interactively 'org-renumber-ordered-list))
  14873. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  14874. (cond
  14875. ((equal (match-string 1) "TBLFM")
  14876. ;; Recalculate the table before this line
  14877. (save-excursion
  14878. (beginning-of-line 1)
  14879. (skip-chars-backward " \r\n\t")
  14880. (if (org-at-table-p)
  14881. (org-call-with-arg 'org-table-recalculate t))))
  14882. (t
  14883. (call-interactively 'org-mode-restart))))
  14884. (t (error "C-c C-c can do nothing useful at this location.")))))
  14885. (defun org-mode-restart ()
  14886. "Restart Org-mode, to scan again for special lines.
  14887. Also updates the keyword regular expressions."
  14888. (interactive)
  14889. (let ((org-inhibit-startup t)) (org-mode))
  14890. (message "Org-mode restarted to refresh keyword and special line setup"))
  14891. (defun org-return ()
  14892. "Goto next table row or insert a newline.
  14893. Calls `org-table-next-row' or `newline', depending on context.
  14894. See the individual commands for more information."
  14895. (interactive)
  14896. (cond
  14897. ((org-at-table-p)
  14898. (org-table-justify-field-maybe)
  14899. (call-interactively 'org-table-next-row))
  14900. (t (newline))))
  14901. (defun org-meta-return (&optional arg)
  14902. "Insert a new heading or wrap a region in a table.
  14903. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  14904. See the individual commands for more information."
  14905. (interactive "P")
  14906. (cond
  14907. ((org-at-table-p)
  14908. (call-interactively 'org-table-wrap-region))
  14909. (t (call-interactively 'org-insert-heading))))
  14910. ;;; Menu entries
  14911. ;; Define the Org-mode menus
  14912. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  14913. '("Tbl"
  14914. ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
  14915. ["Next Field" org-cycle (org-at-table-p)]
  14916. ["Previous Field" org-shifttab (org-at-table-p)]
  14917. ["Next Row" org-return (org-at-table-p)]
  14918. "--"
  14919. ["Blank Field" org-table-blank-field (org-at-table-p)]
  14920. ["Edit Field" org-table-edit-field (org-at-table-p)]
  14921. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  14922. "--"
  14923. ("Column"
  14924. ["Move Column Left" org-metaleft (org-at-table-p)]
  14925. ["Move Column Right" org-metaright (org-at-table-p)]
  14926. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  14927. ["Insert Column" org-shiftmetaright (org-at-table-p)]
  14928. "--"
  14929. ["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])
  14930. ("Row"
  14931. ["Move Row Up" org-metaup (org-at-table-p)]
  14932. ["Move Row Down" org-metadown (org-at-table-p)]
  14933. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  14934. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  14935. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  14936. "--"
  14937. ["Insert Hline" org-table-insert-hline (org-at-table-p)])
  14938. ("Rectangle"
  14939. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  14940. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  14941. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  14942. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  14943. "--"
  14944. ("Calculate"
  14945. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  14946. ["Set Named Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  14947. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  14948. "--"
  14949. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  14950. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  14951. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  14952. "--"
  14953. ["Sum Column/Rectangle" org-table-sum
  14954. (or (org-at-table-p) (org-region-active-p))]
  14955. ["Which Column?" org-table-current-column (org-at-table-p)])
  14956. ["Debug Formulas"
  14957. (setq org-table-formula-debug (not org-table-formula-debug))
  14958. :style toggle :selected org-table-formula-debug]
  14959. "--"
  14960. ["Create" org-table-create (and (not (org-at-table-p))
  14961. org-enable-table-editor)]
  14962. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  14963. ["Import from File" org-table-import (not (org-at-table-p))]
  14964. ["Export to File" org-table-export (org-at-table-p)]
  14965. "--"
  14966. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  14967. (easy-menu-define org-org-menu org-mode-map "Org menu"
  14968. '("Org"
  14969. ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
  14970. ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
  14971. ["Sparse Tree" org-occur t]
  14972. ["Show All" show-all t]
  14973. "--"
  14974. ["New Heading" org-insert-heading t]
  14975. ("Navigate Headings"
  14976. ["Up" outline-up-heading t]
  14977. ["Next" outline-next-visible-heading t]
  14978. ["Previous" outline-previous-visible-heading t]
  14979. ["Next Same Level" outline-forward-same-level t]
  14980. ["Previous Same Level" outline-backward-same-level t]
  14981. "--"
  14982. ["Jump" org-goto t])
  14983. ("Edit Structure"
  14984. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  14985. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  14986. "--"
  14987. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  14988. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  14989. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  14990. "--"
  14991. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  14992. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  14993. ["Demote Heading" org-metaright (not (org-at-table-p))]
  14994. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  14995. "--"
  14996. ["Convert to odd levels" org-convert-to-odd-levels t]
  14997. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  14998. ("Archive"
  14999. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  15000. ["Check and Tag Children" (org-toggle-archive-tag (4))
  15001. :active t :keys "C-u C-c C-x C-a"]
  15002. ["Sparse trees open ARCHIVE trees"
  15003. (setq org-sparse-tree-open-archived-trees
  15004. (not org-sparse-tree-open-archived-trees))
  15005. :style toggle :selected org-sparse-tree-open-archived-trees]
  15006. ["Cycling opens ARCHIVE trees"
  15007. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  15008. :style toggle :selected org-cycle-open-archived-trees]
  15009. ["Agenda includes ARCHIVE trees"
  15010. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  15011. :style toggle :selected (not org-agenda-skip-archived-trees)]
  15012. "--"
  15013. ["Move Subtree to Archive" org-archive-subtree t]
  15014. ["Check and Move Children" (org-archive-subtree '(4))
  15015. :active t :keys "C-u C-c $"])
  15016. "--"
  15017. ("TODO Lists"
  15018. ["TODO/DONE/-" org-todo t]
  15019. ("Select keyword"
  15020. ["Next keyword" org-shiftright (org-on-heading-p)]
  15021. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  15022. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))])
  15023. ["Show TODO Tree" org-show-todo-tree t]
  15024. ["Global TODO list" org-todo-list t]
  15025. "--"
  15026. ["Set Priority" org-priority t]
  15027. ["Priority Up" org-shiftup t]
  15028. ["Priority Down" org-shiftdown t]
  15029. "--"
  15030. ; ["Insert Checkbox" org-insert-todo-heading (org-in-item-p)]
  15031. ; ["Toggle Checkbox" org-ctrl-c-ctrl-c (org-at-item-checkbox-p)]
  15032. ; ["Insert [n/m] cookie" (progn (insert "[/]") (org-update-checkbox-count))
  15033. ; (or (org-on-heading-p) (org-at-item-p))]
  15034. ; ["Insert [%] cookie" (progn (insert "[%]") (org-update-checkbox-count))
  15035. ; (or (org-on-heading-p) (org-at-item-p))]
  15036. ; ["Update Statistics" org-update-checkbox-count t]
  15037. )
  15038. ("Dates and Scheduling"
  15039. ["Timestamp" org-time-stamp t]
  15040. ["Timestamp (inactive)" org-time-stamp-inactive t]
  15041. ("Change Date"
  15042. ["1 Day Later" org-timestamp-up-day t]
  15043. ["1 Day Earlier" org-timestamp-down-day t]
  15044. ["1 ... Later" org-shiftup t]
  15045. ["1 ... Earlier" org-shiftdown t])
  15046. ["Compute Time Range" org-evaluate-time-range t]
  15047. ["Schedule Item" org-schedule t]
  15048. ["Deadline" org-deadline t]
  15049. "--"
  15050. ["Goto Calendar" org-goto-calendar t]
  15051. ["Date from Calendar" org-date-from-calendar t])
  15052. ("Logging work"
  15053. ["Clock in" org-clock-in t]
  15054. ["Clock out" org-clock-out t]
  15055. ["Clock cancel" org-clock-cancel t]
  15056. ["Display times" org-clock-display t]
  15057. ["Create clock table" org-clock-report t]
  15058. "--"
  15059. ["Record DONE time"
  15060. (progn (setq org-log-done (not org-log-done))
  15061. (message "Switching to %s will %s record a timestamp"
  15062. org-done-string
  15063. (if org-log-done "automatically" "not")))
  15064. :style toggle :selected org-log-done])
  15065. "--"
  15066. ["Agenda Command..." org-agenda t]
  15067. ("File List for Agenda")
  15068. ("Special views current file"
  15069. ["TODO Tree" org-show-todo-tree t]
  15070. ["Check Deadlines" org-check-deadlines t]
  15071. ["Timeline" org-timeline t]
  15072. ["Tags Tree" org-tags-sparse-tree t])
  15073. "--"
  15074. ("Hyperlinks"
  15075. ["Store Link (Global)" org-store-link t]
  15076. ["Insert Link" org-insert-link t]
  15077. ["Follow Link" org-open-at-point t]
  15078. "--"
  15079. ["Descriptive Links"
  15080. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  15081. :style radio :selected (member '(org-link) buffer-invisibility-spec)]
  15082. ["Literal Links"
  15083. (progn
  15084. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  15085. :style radio :selected (not (member '(org-link) buffer-invisibility-spec))]
  15086. "--"
  15087. ["Upgrade all <link> to [[link][desc]]" org-upgrade-old-links
  15088. (save-excursion (goto-char (point-min))
  15089. (re-search-forward "<[a-z]+:" nil t))])
  15090. "--"
  15091. ["Export/Publish..." org-export t]
  15092. ("LaTeX"
  15093. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  15094. :selected org-cdlatex-mode]
  15095. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  15096. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  15097. ["Modify math symbol" org-cdlatex-math-modify
  15098. (org-inside-LaTeX-fragment-p)]
  15099. ["Export LaTeX fragments as images"
  15100. (setq org-export-with-LaTeX-fragments (not org-export-with-LaTeX-fragments))
  15101. :style toggle :selected org-export-with-LaTeX-fragments])
  15102. "--"
  15103. ("Documentation"
  15104. ["Show Version" org-version t]
  15105. ["Info Documentation" org-info t])
  15106. ("Customize"
  15107. ["Browse Org Group" org-customize t]
  15108. "--"
  15109. ["Expand This Menu" org-create-customize-menu
  15110. (fboundp 'customize-menu-create)])
  15111. "--"
  15112. ["Refresh setup" org-mode-restart t]
  15113. ))
  15114. (defun org-info (&optional node)
  15115. "Read documentation for Org-mode in the info system.
  15116. With optional NODE, go directly to that node."
  15117. (interactive)
  15118. (require 'info)
  15119. (Info-goto-node (format "(org)%s" (or node ""))))
  15120. (defun org-install-agenda-files-menu ()
  15121. (let ((bl (buffer-list)))
  15122. (save-excursion
  15123. (while bl
  15124. (set-buffer (pop bl))
  15125. (if (org-mode-p) (setq bl nil)))
  15126. (when (org-mode-p)
  15127. (easy-menu-change
  15128. '("Org") "File List for Agenda"
  15129. (append
  15130. (list
  15131. ["Edit File List" (org-edit-agenda-file-list) t]
  15132. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  15133. ["Remove Current File from List" org-remove-file t]
  15134. ["Cycle through agenda files" org-cycle-agenda-files t]
  15135. "--")
  15136. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  15137. ;;; Documentation
  15138. (defun org-customize ()
  15139. "Call the customize function with org as argument."
  15140. (interactive)
  15141. (customize-browse 'org))
  15142. (defun org-create-customize-menu ()
  15143. "Create a full customization menu for Org-mode, insert it into the menu."
  15144. (interactive)
  15145. (if (fboundp 'customize-menu-create)
  15146. (progn
  15147. (easy-menu-change
  15148. '("Org") "Customize"
  15149. `(["Browse Org group" org-customize t]
  15150. "--"
  15151. ,(customize-menu-create 'org)
  15152. ["Set" Custom-set t]
  15153. ["Save" Custom-save t]
  15154. ["Reset to Current" Custom-reset-current t]
  15155. ["Reset to Saved" Custom-reset-saved t]
  15156. ["Reset to Standard Settings" Custom-reset-standard t]))
  15157. (message "\"Org\"-menu now contains full customization menu"))
  15158. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  15159. ;;; Miscellaneous stuff
  15160. (defun org-context ()
  15161. "Return a list of contexts of the current cursor position.
  15162. If several contexts apply, all are returned.
  15163. Each context entry is a list with a symbol naming the context, and
  15164. two positions indicating start and end of the context. Possible
  15165. contexts are:
  15166. :headline anywhere in a headline
  15167. :headline-stars on the leading stars in a headline
  15168. :todo-keyword on a TODO keyword (including DONE) in a headline
  15169. :tags on the TAGS in a headline
  15170. :priority on the priority cookie in a headline
  15171. :item on the first line of a plain list item
  15172. :item-bullet on the bullet/number of a plain list item
  15173. :checkbox on the checkbox in a plain list item
  15174. :table in an org-mode table
  15175. :table-special on a special filed in a table
  15176. :table-table in a table.el table
  15177. :link on a hyperline
  15178. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  15179. :target on a <<target>>
  15180. :radio-target on a <<<radio-target>>>
  15181. :latex-fragment on a LaTeX fragment
  15182. :latex-preview on a LaTeX fragment with overlayed preview image
  15183. This function expects the position to be visible because it uses font-lock
  15184. faces as a help to recognize the following contexts: :table-special, :link,
  15185. and :keyword."
  15186. (let* ((f (get-text-property (point) 'face))
  15187. (faces (if (listp f) f (list f)))
  15188. (p (point)) clist o)
  15189. ;; First the large context
  15190. (cond
  15191. ((org-on-heading-p)
  15192. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  15193. (when (progn
  15194. (beginning-of-line 1)
  15195. (looking-at org-todo-line-tags-regexp))
  15196. (push (org-point-in-group p 1 :headline-stars) clist)
  15197. (push (org-point-in-group p 2 :todo-keyword) clist)
  15198. (push (org-point-in-group p 4 :tags) clist))
  15199. (goto-char p)
  15200. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  15201. (if (looking-at "\\[#[A-Z]\\]")
  15202. (push (org-point-in-group p 0 :priority) clist)))
  15203. ((org-at-item-p)
  15204. (push (org-point-in-group p 2 :item-bullet) clist)
  15205. (push (list :item (point-at-bol)
  15206. (save-excursion (org-end-of-item) (point)))
  15207. clist)
  15208. (and (org-at-item-checkbox-p)
  15209. (push (org-point-in-group p 0 :checkbox) clist)))
  15210. ((org-at-table-p)
  15211. (push (list :table (org-table-begin) (org-table-end)) clist)
  15212. (if (memq 'org-formula faces)
  15213. (push (list :table-special
  15214. (previous-single-property-change p 'face)
  15215. (next-single-property-change p 'face)) clist)))
  15216. ((org-at-table-p 'any)
  15217. (push (list :table-table) clist)))
  15218. (goto-char p)
  15219. ;; Now the small context
  15220. (cond
  15221. ((org-at-timestamp-p)
  15222. (push (org-point-in-group p 0 :timestamp) clist))
  15223. ((memq 'org-link faces)
  15224. (push (list :link
  15225. (previous-single-property-change p 'face)
  15226. (next-single-property-change p 'face)) clist))
  15227. ((memq 'org-special-keyword faces)
  15228. (push (list :keyword
  15229. (previous-single-property-change p 'face)
  15230. (next-single-property-change p 'face)) clist))
  15231. ((org-on-target-p)
  15232. (push (org-point-in-group p 0 :target) clist)
  15233. (goto-char (1- (match-beginning 0)))
  15234. (if (looking-at org-radio-target-regexp)
  15235. (push (org-point-in-group p 0 :radio-target) clist))
  15236. (goto-char p))
  15237. ((setq o (car (delq nil
  15238. (mapcar
  15239. (lambda (x)
  15240. (if (memq x org-latex-fragment-image-overlays) x))
  15241. (org-overlays-at (point))))))
  15242. (push (list :latex-fragment
  15243. (org-overlay-start o) (org-overlay-end o)) clist)
  15244. (push (list :latex-preview
  15245. (org-overlay-start o) (org-overlay-end o)) clist))
  15246. ((org-inside-LaTeX-fragment-p)
  15247. ;; FIXME: positions wring.
  15248. (push (list :latex-fragment (point) (point)) clist)))
  15249. (setq clist (nreverse (delq nil clist)))
  15250. clist))
  15251. (defun org-point-in-group (point group &optional context)
  15252. "Check if POINT is in match-group GROUP.
  15253. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  15254. match. If the match group does ot exist or point is not inside it,
  15255. return nil."
  15256. (and (match-beginning group)
  15257. (>= point (match-beginning group))
  15258. (<= point (match-end group))
  15259. (if context
  15260. (list context (match-beginning group) (match-end group))
  15261. t)))
  15262. (defun org-move-line-down (arg)
  15263. "Move the current line down. With prefix argument, move it past ARG lines."
  15264. (interactive "p")
  15265. (let ((col (current-column))
  15266. beg end pos)
  15267. (beginning-of-line 1) (setq beg (point))
  15268. (beginning-of-line 2) (setq end (point))
  15269. (beginning-of-line (+ 1 arg))
  15270. (setq pos (move-marker (make-marker) (point)))
  15271. (insert (delete-and-extract-region beg end))
  15272. (goto-char pos)
  15273. (move-to-column col)))
  15274. (defun org-move-line-up (arg)
  15275. "Move the current line up. With prefix argument, move it past ARG lines."
  15276. (interactive "p")
  15277. (let ((col (current-column))
  15278. beg end pos)
  15279. (beginning-of-line 1) (setq beg (point))
  15280. (beginning-of-line 2) (setq end (point))
  15281. (beginning-of-line (- arg))
  15282. (setq pos (move-marker (make-marker) (point)))
  15283. (insert (delete-and-extract-region beg end))
  15284. (goto-char pos)
  15285. (move-to-column col)))
  15286. ;; Paragraph filling stuff.
  15287. ;; We want this to be just right, so use the full arsenal.
  15288. (defun org-set-autofill-regexps ()
  15289. (interactive)
  15290. ;; In the paragraph separator we include headlines, because filling
  15291. ;; text in a line directly attached to a headline would otherwise
  15292. ;; fill the headline as well.
  15293. (org-set-local 'comment-start-skip "^#+[ \t]*")
  15294. (org-set-local 'paragraph-separate "\f\\|\\*\\|[ ]*$\\|[ \t]*[:|]")
  15295. ;; The paragraph starter includes hand-formatted lists.
  15296. (org-set-local 'paragraph-start
  15297. "\f\\|[ ]*$\\|\\([*\f]+\\)\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  15298. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  15299. ;; But only if the user has not turned off tables or fixed-width regions
  15300. (org-set-local
  15301. 'auto-fill-inhibit-regexp
  15302. (concat "\\*\\|#"
  15303. "\\|[ \t]*" org-keyword-time-regexp
  15304. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  15305. (concat
  15306. "\\|[ \t]*["
  15307. (if org-enable-table-editor "|" "")
  15308. (if org-enable-fixed-width-editor ":" "")
  15309. "]"))))
  15310. ;; We use our own fill-paragraph function, to make sure that tables
  15311. ;; and fixed-width regions are not wrapped. That function will pass
  15312. ;; through to `fill-paragraph' when appropriate.
  15313. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  15314. ; Adaptive filling: To get full control, first make sure that
  15315. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  15316. (org-set-local 'adaptive-fill-regexp "\000")
  15317. (org-set-local 'adaptive-fill-function
  15318. 'org-adaptive-fill-function))
  15319. (defun org-fill-paragraph (&optional justify)
  15320. "Re-align a table, pass through to fill-paragraph if no table."
  15321. (let ((table-p (org-at-table-p))
  15322. (table.el-p (org-at-table.el-p)))
  15323. (cond ((equal (char-after (point-at-bol)) ?*) t) ; skip headlines
  15324. (table.el-p t) ; skip table.el tables
  15325. (table-p (org-table-align) t) ; align org-mode tables
  15326. (t nil)))) ; call paragraph-fill
  15327. ;; For reference, this is the default value of adaptive-fill-regexp
  15328. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  15329. (defun org-adaptive-fill-function ()
  15330. "Return a fill prefix for org-mode files.
  15331. In particular, this makes sure hanging paragraphs for hand-formatted lists
  15332. work correctly."
  15333. (if (looking-at " *\\([-*+] \\|[0-9]+[.)] \\)?")
  15334. (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  15335. ;; Functions needed for Emacs/XEmacs region compatibility
  15336. (defun org-add-hook (hook function &optional append local)
  15337. "Add-hook, compatible with both Emacsen."
  15338. (if (and local (featurep 'xemacs))
  15339. (add-local-hook hook function append)
  15340. (add-hook hook function append local)))
  15341. (defun org-region-active-p ()
  15342. "Is `transient-mark-mode' on and the region active?
  15343. Works on both Emacs and XEmacs."
  15344. (if org-ignore-region
  15345. nil
  15346. (if (featurep 'xemacs)
  15347. (and zmacs-regions (region-active-p))
  15348. (and transient-mark-mode mark-active))))
  15349. (defun org-add-to-invisibility-spec (arg)
  15350. "Add elements to `buffer-invisibility-spec'.
  15351. See documentation for `buffer-invisibility-spec' for the kind of elements
  15352. that can be added."
  15353. (cond
  15354. ((fboundp 'add-to-invisibility-spec)
  15355. (add-to-invisibility-spec arg))
  15356. ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
  15357. (setq buffer-invisibility-spec (list arg)))
  15358. (t
  15359. (setq buffer-invisibility-spec
  15360. (cons arg buffer-invisibility-spec)))))
  15361. (defun org-remove-from-invisibility-spec (arg)
  15362. "Remove elements from `buffer-invisibility-spec'."
  15363. (if (fboundp 'remove-from-invisibility-spec)
  15364. (remove-from-invisibility-spec arg)
  15365. (if (consp buffer-invisibility-spec)
  15366. (setq buffer-invisibility-spec
  15367. (delete arg buffer-invisibility-spec)))))
  15368. (defun org-in-invisibility-spec-p (arg)
  15369. "Is ARG a member of `buffer-invisibility-spec'?"
  15370. (if (consp buffer-invisibility-spec)
  15371. (member arg buffer-invisibility-spec)
  15372. nil))
  15373. (defun org-image-file-name-regexp ()
  15374. "Return regexp matching the file names of images."
  15375. (if (fboundp 'image-file-name-regexp)
  15376. (image-file-name-regexp)
  15377. (let ((image-file-name-extensions
  15378. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  15379. "xbm" "xpm" "pbm" "pgm" "ppm")))
  15380. (concat "\\."
  15381. (regexp-opt (nconc (mapcar 'upcase
  15382. image-file-name-extensions)
  15383. image-file-name-extensions)
  15384. t)
  15385. "\\'"))))
  15386. ;; Functions extending outline functionality
  15387. ;; C-a should go to the beginning of a *visible* line, also in the
  15388. ;; new outline.el. I guess this should be patched into Emacs?
  15389. (defun org-beginning-of-line ()
  15390. "Go to the beginning of the current line. If that is invisible, continue
  15391. to a visible line beginning. This makes the function of C-a more intuitive."
  15392. (interactive)
  15393. (beginning-of-line 1)
  15394. (if (bobp)
  15395. nil
  15396. (backward-char 1)
  15397. (if (org-invisible-p)
  15398. (while (and (not (bobp)) (org-invisible-p))
  15399. (backward-char 1)
  15400. (beginning-of-line 1))
  15401. (forward-char 1))))
  15402. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  15403. (defun org-invisible-p ()
  15404. "Check if point is at a character currently not visible."
  15405. ;; Early versions of noutline don't have `outline-invisible-p'.
  15406. (if (fboundp 'outline-invisible-p)
  15407. (outline-invisible-p)
  15408. (get-char-property (point) 'invisible)))
  15409. (defun org-invisible-p2 ()
  15410. "Check if point is at a character currently not visible."
  15411. (save-excursion
  15412. (if (and (eolp) (not (bobp))) (backward-char 1))
  15413. ;; Early versions of noutline don't have `outline-invisible-p'.
  15414. (if (fboundp 'outline-invisible-p)
  15415. (outline-invisible-p)
  15416. (get-char-property (point) 'invisible))))
  15417. (defalias 'org-back-to-heading 'outline-back-to-heading)
  15418. (defalias 'org-on-heading-p 'outline-on-heading-p)
  15419. (defun org-on-target-p ()
  15420. (let ((pos (point)))
  15421. (save-excursion
  15422. (skip-chars-forward "<")
  15423. (and (re-search-backward "<<" nil t)
  15424. (or (looking-at org-radio-target-regexp)
  15425. (looking-at org-target-regexp))
  15426. (<= (match-beginning 0) pos)
  15427. (>= (1+ (match-end 0)) pos)))))
  15428. (defun org-up-heading-all (arg)
  15429. "Move to the heading line of which the present line is a subheading.
  15430. This function considers both visible and invisible heading lines.
  15431. With argument, move up ARG levels."
  15432. (if (fboundp 'outline-up-heading-all)
  15433. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  15434. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  15435. (defun org-show-hidden-entry ()
  15436. "Show an entry where even the heading is hidden."
  15437. (save-excursion
  15438. (org-show-entry)))
  15439. (defun org-flag-heading (flag &optional entry)
  15440. "Flag the current heading. FLAG non-nil means make invisible.
  15441. When ENTRY is non-nil, show the entire entry."
  15442. (save-excursion
  15443. (org-back-to-heading t)
  15444. ;; Check if we should show the entire entry
  15445. (if entry
  15446. (progn
  15447. (org-show-entry)
  15448. (save-excursion
  15449. (and (outline-next-heading)
  15450. (org-flag-heading nil))))
  15451. (outline-flag-region (max 1 (1- (point)))
  15452. (save-excursion (outline-end-of-heading) (point))
  15453. flag))))
  15454. (defun org-end-of-subtree (&optional invisible-OK)
  15455. ;; This is an exact copy of the original function, but it uses
  15456. ;; `org-back-to-heading', to make it work also in invisible
  15457. ;; trees. And is uses an invisible-OK argument.
  15458. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  15459. (org-back-to-heading invisible-OK)
  15460. (let ((first t)
  15461. (level (funcall outline-level)))
  15462. (while (and (not (eobp))
  15463. (or first (> (funcall outline-level) level)))
  15464. (setq first nil)
  15465. (outline-next-heading))
  15466. (if (memq (preceding-char) '(?\n ?\^M))
  15467. (progn
  15468. ;; Go to end of line before heading
  15469. (forward-char -1)
  15470. (if (memq (preceding-char) '(?\n ?\^M))
  15471. ;; leave blank line before heading
  15472. (forward-char -1)))))
  15473. (point))
  15474. (defun org-show-subtree ()
  15475. "Show everything after this heading at deeper levels."
  15476. (outline-flag-region
  15477. (point)
  15478. (save-excursion
  15479. (outline-end-of-subtree) (outline-next-heading) (point))
  15480. nil))
  15481. (defun org-show-entry ()
  15482. "Show the body directly following this heading.
  15483. Show the heading too, if it is currently invisible."
  15484. (interactive)
  15485. (save-excursion
  15486. (org-back-to-heading t)
  15487. (outline-flag-region
  15488. (max 1 (1- (point)))
  15489. (save-excursion
  15490. (re-search-forward (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  15491. (or (match-beginning 1) (point-max)))
  15492. nil)))
  15493. (defun org-make-options-regexp (kwds)
  15494. "Make a regular expression for keyword lines."
  15495. (concat
  15496. "^"
  15497. "#?[ \t]*\\+\\("
  15498. (mapconcat 'regexp-quote kwds "\\|")
  15499. "\\):[ \t]*"
  15500. "\\(.+\\)"))
  15501. ;; Make `bookmark-jump' show the jump location if it was hidden.
  15502. (eval-after-load "bookmark"
  15503. '(if (boundp 'bookmark-after-jump-hook)
  15504. ;; We can use the hook
  15505. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  15506. ;; Hook not available, use advice
  15507. (defadvice bookmark-jump (after org-make-visible activate)
  15508. "Make the position visible."
  15509. (org-bookmark-jump-unhide))))
  15510. (defun org-bookmark-jump-unhide ()
  15511. "Unhide the current position, to show the bookmark location."
  15512. (and (org-mode-p)
  15513. (or (org-invisible-p)
  15514. (save-excursion (goto-char (max (point-min) (1- (point))))
  15515. (org-invisible-p)))
  15516. (org-show-hierarchy-above)))
  15517. ;;; Experimental code
  15518. ;(add-hook 'org-load-hook
  15519. ; (lambda ()
  15520. ; (defun outline-show-heading ()
  15521. ; "Show the current heading and move to its end."
  15522. ; (outline-flag-region
  15523. ; (- (point)
  15524. ; (cond
  15525. ; ((bobp) 0)
  15526. ; ((equal (buffer-substring
  15527. ; (max (point-min) (- (point) 3)) (point))
  15528. ; "\n\n\n")
  15529. ; 2)
  15530. ; (t 1)))
  15531. ; (progn (outline-end-of-heading) (point))
  15532. ; nil))))
  15533. ;;; Finish up
  15534. (provide 'org)
  15535. (run-hooks 'org-load-hook)
  15536. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  15537. ;;; org.el ends here