org.el 603 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414
  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.44
  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. ;;
  47. ;; Installation
  48. ;; ------------
  49. ;; If Org-mode is part of the Emacs distribution or an XEmacs package, you
  50. ;; only need to copy the following lines to your .emacs file. The last two
  51. ;; lines define *global* keys for the commands `org-store-link' and
  52. ;; `org-agenda' - please choose suitable keys yourself.
  53. ;;
  54. ;; (add-to-list 'auto-mode-alist '("\\.org$" . org-mode))
  55. ;; (define-key global-map "\C-cl" 'org-store-link)
  56. ;; (define-key global-map "\C-ca" 'org-agenda)
  57. ;;
  58. ;; Furthermore you need to activate font-lock-mode in org-mode buffers.
  59. ;; either of the following two lins will do the trick:
  60. ;;
  61. ;; (global-font-lock-mode 1) ; for all buffers
  62. ;; (add-hook 'org-mode-hook 'turn-on-font-lock) ; org-mode buffers only
  63. ;;
  64. ;; If you have downloaded Org-mode from the Web, you have to take additional
  65. ;; action: Byte-compile org.el and org-publish.el and put them together with
  66. ;; org-install.el on your load path. Then also add to your .emacs file:
  67. ;;
  68. ;; (require 'org-install)
  69. ;;
  70. ;;
  71. ;; Activation
  72. ;; ----------
  73. ;; The setup above will put all files with extension ".org" into Org-mode.
  74. ;; As an alternative, make the first line of a file look like this:
  75. ;;
  76. ;; MY PROJECTS -*- mode: org; -*-
  77. ;;
  78. ;; which will select Org-mode for this buffer no matter what the file's
  79. ;; name is.
  80. ;;
  81. ;; Documentation
  82. ;; -------------
  83. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  84. ;; distribution also contains a PDF version of it. At the homepage of
  85. ;; Org-mode, you can read the same text online as HTML. There is also an
  86. ;; excellent reference card made by Philip Rooke. This card can be found
  87. ;; in the etc/ directory of Emacs 22.
  88. ;;
  89. ;; Recent changes
  90. ;; --------------
  91. ;; Version 4.44
  92. ;; - Clock table can be done for a limited time interval.
  93. ;; - Obsolete support for the old outline mode has been removed.
  94. ;; - Bug fixes and code cleaning.
  95. ;;
  96. ;; Version 4.43
  97. ;; - Bug fixes
  98. ;; - `s' key in the agenda saves all org-mode buffers.
  99. ;;
  100. ;; Version 4.41
  101. ;; - Shift-curser keys can modify inactive time stamps (inactive time
  102. ;; stamps are the ones in [...] brackets.
  103. ;; - Toggle all checkboxes in a region/below a headline.
  104. ;; - Bug fixes.
  105. ;;
  106. ;; Version 4.40
  107. ;; - Bug fixes.
  108. ;;
  109. ;; Version 4.39
  110. ;; - Special tag ARCHIVE keeps a subtree closed and away from agenda lists.
  111. ;; - LaTeX code in Org-mode files can be converted to images for HTML.
  112. ;; - Bug fixes.
  113. ;; - CDLaTeX-mode features can be used in Org-mode to help inserting
  114. ;; LaTeX environment and math.
  115. ;;
  116. ;; Version 4.38
  117. ;; - noutline.el is now required (important for XEmacs users only).
  118. ;; - Dynamic blocks.
  119. ;; - Archiving of all level 1 trees without open TODO items.
  120. ;; - Clock reports can be inserted into the file in a special section.
  121. ;; - FAQ removed from the manual, now only on the web.
  122. ;; - Bug fixes.
  123. ;;
  124. ;; Version 4.37
  125. ;; - Clock-feature for measuring time spent on specific items.
  126. ;; - Improved emphasizing allows configuration and stacking.
  127. ;;
  128. ;; Version 4.36
  129. ;; - Improved indentation of ASCII export, when headlines become items.
  130. ;; - Handling of 12am and 12pm fixed. Times beyond 24:00 can be used
  131. ;; and will not lead to conflicts.
  132. ;; - Support for mutually exclusive TAGS with the fast tags interface.
  133. ;; - Bug fixes.
  134. ;;
  135. ;; Version 4.35
  136. ;; - HTML export is now valid XHTML.
  137. ;; - Timeline can also show dates without entries. See new option
  138. ;; `org-timeline-show-empty-dates'.
  139. ;; - The bullets created by the ASCII exporter can now be configured.
  140. ;; See the new option `org-export-ascii-bullets'.
  141. ;; - New face `org-upcoming-deadline' (was `org-scheduled-previously').
  142. ;; - New function `org-context' to allow testing for local context.
  143. ;;
  144. ;; Version 4.34
  145. ;; - Bug fixes.
  146. ;;
  147. ;; Version 4.33
  148. ;; - New commands to move through plain lists: S-up and S-down.
  149. ;; - Bug fixes and documentation update.
  150. ;;
  151. ;; Version 4.32
  152. ;; - Fast (single-key-per-tag) interface for setting TAGS.
  153. ;; - The list of legal tags can be configured globally and locally.
  154. ;; - Elisp and Info links (thanks to Todd Neal).
  155. ;; - `org-export-publishing-directory' can be an alist, with different
  156. ;; directories for different export types.
  157. ;; - All context-sensitive commands use `call-interactively' to dispatch.
  158. ;; - `org-confirm-shell-links' renamed to `org-confirm-shell-link-function'.
  159. ;; - Bug fixes.
  160. ;;
  161. ;; Version 4.31
  162. ;; - Bug fixes.
  163. ;;
  164. ;; Version 4.30
  165. ;; - Modified installation: Autoloads have been collected in org-install.el.
  166. ;; - Logging (org-log-done) is now a #+STARTUP option.
  167. ;; - Checkboxes in plain list items, following up on Frank Ruell's idea.
  168. ;; - File links inserted with C-c C-l will use relative paths if the linked
  169. ;; file is in the current directory or a subdirectory of it.
  170. ;; - New variable `org-link-file-path-type' to specify preference for
  171. ;; relative and absolute paths.
  172. ;; - New CSS classes for tags, timestamps, timestamp keywords.
  173. ;; - Bug and typo fixes.
  174. ;;
  175. ;; Version 4.29
  176. ;; - Inlining images in HTML export now depends on wheather the link
  177. ;; contains a description or not.
  178. ;; - TODO items can be scheduled from the global TODO list using C-c C-s.
  179. ;; - TODO items already scheduled can be made to disappear from the global
  180. ;; todo list, see `org-agenda-todo-ignore-scheduled'.
  181. ;; - In Tables, formulas may also be Lisp forms.
  182. ;; - Exporting the visible part of an outline with `C-c C-x v' works now
  183. ;; for all available exporters.
  184. ;; - Bug fixes, lots of them :-(
  185. ;;
  186. ;; Version 4.28
  187. ;; - Bug fixes.
  188. ;;
  189. ;; Version 4.27
  190. ;; - HTML exporter generalized to receive external options.
  191. ;; As part of the process, author, email and date have been moved to the
  192. ;; end of the HTML file.
  193. ;; - Support for customizable file search in file links.
  194. ;; - BibTeX database links as first application of the above.
  195. ;; - New option `org-agenda-todo-list-sublevels' to turn off listing TODO
  196. ;; entries that are sublevels of another TODO entry.
  197. ;;
  198. ;;
  199. ;;; Code:
  200. (eval-when-compile
  201. (require 'cl)
  202. (require 'calendar))
  203. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  204. ;; the file noutline.el being loaded.
  205. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  206. ;; We require noutline, which might be provided in outline.el
  207. (require 'outline) (require 'noutline)
  208. ;; Other stuff we need.
  209. (require 'time-date)
  210. (require 'easymenu)
  211. ;;; Customization variables
  212. (defvar org-version "4.44"
  213. "The version number of the file org.el.")
  214. (defun org-version ()
  215. (interactive)
  216. (message "Org-mode version %s" org-version))
  217. ;; Compatibility constants
  218. (defconst org-xemacs-p (featurep 'xemacs)) ; not used by org.el itself
  219. (defconst org-format-transports-properties-p
  220. (let ((x "a"))
  221. (add-text-properties 0 1 '(test t) x)
  222. (get-text-property 0 'test (format "%s" x)))
  223. "Does format transport text properties?")
  224. (defgroup org nil
  225. "Outline-based notes management and organizer."
  226. :tag "Org"
  227. :group 'outlines
  228. :group 'hypermedia
  229. :group 'calendar)
  230. (defgroup org-startup nil
  231. "Options concerning startup of Org-mode."
  232. :tag "Org Startup"
  233. :group 'org)
  234. (defcustom org-startup-folded t
  235. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  236. This can also be configured on a per-file basis by adding one of
  237. the following lines anywhere in the buffer:
  238. #+STARTUP: fold
  239. #+STARTUP: nofold
  240. #+STARTUP: content"
  241. :group 'org-startup
  242. :type '(choice
  243. (const :tag "nofold: show all" nil)
  244. (const :tag "fold: overview" t)
  245. (const :tag "content: all headlines" content)))
  246. (defcustom org-startup-truncated t
  247. "Non-nil means, entering Org-mode will set `truncate-lines'.
  248. This is useful since some lines containing links can be very long and
  249. uninteresting. Also tables look terrible when wrapped."
  250. :group 'org-startup
  251. :type 'boolean)
  252. (defcustom org-startup-align-all-tables nil
  253. "Non-nil means, align all tables when visiting a file.
  254. This is useful when the column width in tables is forced with <N> cookies
  255. in table fields. Such tables will look correct only after the first re-align.
  256. This can also be configured on a per-file basis by adding one of
  257. the following lines anywhere in the buffer:
  258. #+STARTUP: align
  259. #+STARTUP: noalign"
  260. :group 'org-startup
  261. :type 'boolean)
  262. (defcustom org-startup-with-deadline-check nil
  263. "Non-nil means, entering Org-mode will run the deadline check.
  264. This means, if you start editing an org file, you will get an
  265. immediate reminder of any due deadlines.
  266. This can also be configured on a per-file basis by adding one of
  267. the following lines anywhere in the buffer:
  268. #+STARTUP: dlcheck
  269. #+STARTUP: nodlcheck"
  270. :group 'org-startup
  271. :type 'boolean)
  272. (defcustom org-insert-mode-line-in-empty-file nil
  273. "Non-nil means insert the first line setting Org-mode in empty files.
  274. When the function `org-mode' is called interactively in an empty file, this
  275. normally means that the file name does not automatically trigger Org-mode.
  276. To ensure that the file will always be in Org-mode in the future, a
  277. line enforcing Org-mode will be inserted into the buffer, if this option
  278. has been set."
  279. :group 'org-startup
  280. :type 'boolean)
  281. (defcustom org-CUA-compatible nil
  282. "Non-nil means use alternative key bindings for S-<cursor movement>.
  283. Org-mode used S-<cursor movement> for changing timestamps and priorities.
  284. S-<cursor movement> is also used for example by `CUA-mode' to select text.
  285. If you want to use Org-mode together with `CUA-mode', Org-mode needs to use
  286. alternative bindings. Setting this variable to t will replace the following
  287. keys both in Org-mode and in the Org-agenda buffer.
  288. S-RET -> C-S-RET
  289. S-up -> M-p
  290. S-down -> M-n
  291. S-left -> M--
  292. S-right -> M-+
  293. If you do not like the alternative keys, take a look at the variable
  294. `org-disputed-keys'.
  295. This option is only relevant at load-time of Org-mode. Changing it requires
  296. a restart of Emacs to become effective."
  297. :group 'org-startup
  298. :type 'boolean)
  299. (defvar org-disputed-keys
  300. '((S-up [(shift up)] [(meta ?p)])
  301. (S-down [(shift down)] [(meta ?n)])
  302. (S-left [(shift left)] [(meta ?-)])
  303. (S-right [(shift right)] [(meta ?+)])
  304. (S-return [(shift return)] [(control shift return)]))
  305. "Keys for which Org-mode and other modes compete.
  306. This is an alist, cars are symbols for lookup, 1st element is the default key,
  307. second element will be used when `org-CUA-compatible' is t.")
  308. (defun org-key (key)
  309. "Select a key according to `org-CUA-compatible'."
  310. (nth (if org-CUA-compatible 2 1)
  311. (or (assq key org-disputed-keys)
  312. (error "Invalid Key %s in `org-key'" key))))
  313. (defcustom org-ellipsis nil
  314. "The ellipsis to use in the Org-mode outline.
  315. When nil, just use the standard three dots. When a string, use that instead,
  316. and just in Org-mode (which will then use its own display table).
  317. Changing this requires executing `M-x org-mode' in a buffer to become
  318. effective."
  319. :group 'org-startup
  320. :type '(choice (const :tag "Default" nil)
  321. (string :tag "String" :value "...#")))
  322. (defvar org-display-table nil
  323. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  324. (defgroup org-keywords nil
  325. "Keywords in Org-mode."
  326. :tag "Org Keywords"
  327. :group 'org)
  328. (defcustom org-deadline-string "DEADLINE:"
  329. "String to mark deadline entries.
  330. A deadline is this string, followed by a time stamp. Should be a word,
  331. terminated by a colon. You can insert a schedule keyword and
  332. a timestamp with \\[org-deadline].
  333. Changes become only effective after restarting Emacs."
  334. :group 'org-keywords
  335. :type 'string)
  336. (defcustom org-scheduled-string "SCHEDULED:"
  337. "String to mark scheduled TODO entries.
  338. A schedule is this string, followed by a time stamp. Should be a word,
  339. terminated by a colon. You can insert a schedule keyword and
  340. a timestamp with \\[org-schedule].
  341. Changes become only effective after restarting Emacs."
  342. :group 'org-keywords
  343. :type 'string)
  344. (defcustom org-closed-string "CLOSED:"
  345. "String used as the prefix for timestamps logging closing a TODO entry."
  346. :group 'org-keywords
  347. :type 'string)
  348. (defcustom org-clock-string "CLOCK:"
  349. "String used as prefix for timestamps clocking work hours on an item."
  350. :group 'org-keywords
  351. :type 'string)
  352. (defcustom org-comment-string "COMMENT"
  353. "Entries starting with this keyword will never be exported.
  354. An entry can be toggled between COMMENT and normal with
  355. \\[org-toggle-comment].
  356. Changes become only effective after restarting Emacs."
  357. :group 'org-keywords
  358. :type 'string)
  359. (defcustom org-quote-string "QUOTE"
  360. "Entries starting with this keyword will be exported in fixed-width font.
  361. Quoting applies only to the text in the entry following the headline, and does
  362. not extend beyond the next headline, even if that is lower level.
  363. An entry can be toggled between QUOTE and normal with
  364. \\[org-toggle-fixed-width-section]."
  365. :group 'org-keywords
  366. :type 'string)
  367. (defgroup org-structure nil
  368. "Options concerning the general structure of Org-mode files."
  369. :tag "Org Structure"
  370. :group 'org)
  371. (defgroup org-cycle nil
  372. "Options concerning visibility cycling in Org-mode."
  373. :tag "Org Cycle"
  374. :group 'org-structure)
  375. (defcustom org-cycle-global-at-bob t
  376. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  377. This makes it possible to do global cycling without having to use S-TAB or
  378. C-u TAB. For this special case to work, the first line of the buffer
  379. must not be a headline - it may be empty ot some other text. When used in
  380. this way, `org-cycle-hook' is disables temporarily, to make sure the
  381. cursor stays at the beginning of the buffer.
  382. When this option is nil, don't do anything special at the beginning
  383. of the buffer."
  384. :group 'org-cycle
  385. :type 'boolean)
  386. (defcustom org-cycle-emulate-tab t
  387. "Where should `org-cycle' emulate TAB.
  388. nil Never
  389. white Only in completely white lines
  390. whitestart Only at the beginning of lines, before the first non-white char.
  391. t Everywhere except in headlines
  392. If TAB is used in a place where it does not emulate TAB, the current subtree
  393. visibility is cycled."
  394. :group 'org-cycle
  395. :type '(choice (const :tag "Never" nil)
  396. (const :tag "Only in completely white lines" white)
  397. (const :tag "Before first char in a line" whitestart)
  398. (const :tag "Everywhere except in headlines" t)
  399. ))
  400. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  401. org-optimize-window-after-visibility-change)
  402. "Hook that is run after `org-cycle' has changed the buffer visibility.
  403. The function(s) in this hook must accept a single argument which indicates
  404. the new state that was set by the most recent `org-cycle' command. The
  405. argument is a symbol. After a global state change, it can have the values
  406. `overview', `content', or `all'. After a local state change, it can have
  407. the values `folded', `children', or `subtree'."
  408. :group 'org-cycle
  409. :type 'hook)
  410. (defgroup org-edit-structure nil
  411. "Options concerning structure editing in Org-mode."
  412. :tag "Org Edit Structure"
  413. :group 'org-structure)
  414. (defcustom org-odd-levels-only nil
  415. "Non-nil means, skip even levels and only use odd levels for the outline.
  416. This has the effect that two stars are being added/taken away in
  417. promotion/demotion commands. It also influences how levels are
  418. handled by the exporters.
  419. Changing it requires restart of `font-lock-mode' to become effective
  420. for fontification also in regions already fontified.
  421. You may also set this on a per-file basis by adding one of the following
  422. lines to the buffer:
  423. #+STARTUP: odd
  424. #+STARTUP: oddeven"
  425. :group 'org-edit-structure
  426. :group 'org-font-lock
  427. :type 'boolean)
  428. (defcustom org-adapt-indentation t
  429. "Non-nil means, adapt indentation when promoting and demoting.
  430. When this is set and the *entire* text in an entry is indented, the
  431. indentation is increased by one space in a demotion command, and
  432. decreased by one in a promotion command. If any line in the entry
  433. body starts at column 0, indentation is not changed at all."
  434. :group 'org-edit-structure
  435. :type 'boolean)
  436. (defcustom org-insert-heading-hook nil
  437. "Hook being run after inserting a new heading."
  438. :group 'org-edit-structure
  439. :type 'boolean)
  440. (defcustom org-enable-fixed-width-editor t
  441. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  442. This currently only means, they are never auto-wrapped.
  443. When nil, such lines will be treated like ordinary lines.
  444. See also the QUOTE keyword."
  445. :group 'org-edit-structure
  446. :type 'boolean)
  447. (defgroup org-sparse-trees nil
  448. "Options concerning sparse trees in Org-mode."
  449. :tag "Org Sparse Trees"
  450. :group 'org-structure)
  451. (defcustom org-highlight-sparse-tree-matches t
  452. "Non-nil means, highlight all matches that define a sparse tree.
  453. The highlights will automatically disappear the next time the buffer is
  454. changed by an edit command."
  455. :group 'org-sparse-trees
  456. :type 'boolean)
  457. (defcustom org-show-hierarchy-above t
  458. "Non-nil means, show full hierarchy when showing a spot in the tree.
  459. Turning this off makes sparse trees more compact, but also less clear."
  460. :group 'org-sparse-trees
  461. :type 'boolean)
  462. (defcustom org-show-following-heading t
  463. "Non-nil means, show heading following match in `org-occur'.
  464. When doing an `org-occur' it is useful to show the headline which
  465. follows the match, even if they do not match the regexp. This makes it
  466. easier to edit directly inside the sparse tree. However, if you use
  467. `org-occur' mainly as an overview, the following headlines are
  468. unnecessary clutter."
  469. :group 'org-sparse-trees
  470. :type 'boolean)
  471. (defcustom org-occur-hook '(org-first-headline-recenter)
  472. "Hook that is run after `org-occur' has constructed a sparse tree.
  473. This can be used to recenter the window to show as much of the structure
  474. as possible."
  475. :group 'org-sparse-trees
  476. :type 'hook)
  477. (defgroup org-plain-lists nil
  478. "Options concerning plain lists in Org-mode."
  479. :tag "Org Plain lists"
  480. :group 'org-structure)
  481. (defcustom org-cycle-include-plain-lists nil
  482. "Non-nil means, include plain lists into visibility cycling.
  483. This means that during cycling, plain list items will *temporarily* be
  484. interpreted as outline headlines with a level given by 1000+i where i is the
  485. indentation of the bullet. In all other operations, plain list items are
  486. not seen as headlines. For example, you cannot assign a TODO keyword to
  487. such an item."
  488. :group 'org-plain-lists
  489. :type 'boolean)
  490. (defcustom org-plain-list-ordered-item-terminator t
  491. "The character that makes a line with leading number an ordered list item.
  492. Valid values are ?. and ?\). To get both terminators, use t. While
  493. ?. may look nicer, it creates the danger that a line with leading
  494. number may be incorrectly interpreted as an item. ?\) therefore is
  495. the safe choice."
  496. :group 'org-plain-lists
  497. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  498. (const :tag "paren like in \"2)\"" ?\))
  499. (const :tab "both" t)))
  500. (defcustom org-auto-renumber-ordered-lists t
  501. "Non-nil means, automatically renumber ordered plain lists.
  502. Renumbering happens when the sequence have been changed with
  503. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  504. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  505. :group 'org-plain-lists
  506. :type 'boolean)
  507. (defgroup org-archive nil
  508. "Options concerning archiving in Org-mode."
  509. :tag "Org Archive"
  510. :group 'org-structure)
  511. (defcustom org-archive-tag "ARCHIVE"
  512. "The tag that marks a subtree as archived.
  513. An archived subtree does not open during visibility cycling, and does
  514. not contribute to the agenda listings."
  515. :group 'org-archive
  516. :group 'org-keywords
  517. :type 'string)
  518. (defcustom org-agenda-skip-archived-trees t
  519. "Non-nil means, the agenda will skip any items located in archived trees.
  520. An archived tree is a tree marked with the tag ARCHIVE."
  521. :group 'org-archive
  522. :group 'org-agenda-display
  523. :type 'boolean)
  524. (defcustom org-cycle-open-archived-trees nil
  525. "Non-nil means, `org-cycle' will open archived trees.
  526. An archived tree is a tree marked with the tag ARCHIVE.
  527. When nil, archived trees will stay folded. You can still open them with
  528. normal outline commands like `show-all', but not with the cycling commands."
  529. :group 'org-archive
  530. :group 'org-cycle
  531. :type 'boolean)
  532. (defcustom org-sparse-tree-open-archived-trees nil
  533. "Non-nil means sparse tree construction shows matches in archived trees.
  534. When nil, matches in these trees are highlighted, but the trees are kept in
  535. collapsed state."
  536. :group 'org-archive
  537. :group 'org-sparse-trees
  538. :type 'boolean)
  539. (defcustom org-archive-location "%s_archive::"
  540. "The location where subtrees should be archived.
  541. This string consists of two parts, separated by a double-colon.
  542. The first part is a file name - when omitted, archiving happens in the same
  543. file. %s will be replaced by the current file name (without directory part).
  544. Archiving to a different file is useful to keep archived entries from
  545. contributing to the Org-mode Agenda.
  546. The part after the double colon is a headline. The archived entries will be
  547. filed under that headline. When omitted, the subtrees are simply filed away
  548. at the end of the file, as top-level entries.
  549. Here are a few examples:
  550. \"%s_archive::\"
  551. If the current file is Projects.org, archive in file
  552. Projects.org_archive, as top-level trees. This is the default.
  553. \"::* Archived Tasks\"
  554. Archive in the current file, under the top-level headline
  555. \"* Archived Tasks\".
  556. \"~/org/archive.org::\"
  557. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  558. \"basement::** Finished Tasks\"
  559. Archive in file ./basement (relative path), as level 3 trees
  560. below the level 2 heading \"** Finished Tasks\".
  561. You may set this option on a per-file basis by adding to the buffer a
  562. line like
  563. #+ARCHIVE: basement::** Finished Tasks"
  564. :group 'org-archive
  565. :type 'string)
  566. (defcustom org-archive-mark-done t
  567. "Non-nil means, mark entries as DONE when they are moved to the archive file."
  568. :group 'org-archive
  569. :type 'boolean)
  570. (defcustom org-archive-stamp-time t
  571. "Non-nil means, add a time stamp to entries moved to an archive file.
  572. The time stamp will be added directly after the TODO state keyword in the
  573. first line, so it is probably best to use this in combinations with
  574. `org-archive-mark-done'."
  575. :group 'org-archive
  576. :type 'boolean)
  577. (defgroup org-table nil
  578. "Options concerning tables in Org-mode."
  579. :tag "Org Table"
  580. :group 'org)
  581. (defcustom org-enable-table-editor 'optimized
  582. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  583. When nil, such lines will be treated like ordinary lines.
  584. When equal to the symbol `optimized', the table editor will be optimized to
  585. do the following:
  586. - Use automatic overwrite mode in front of whitespace in table fields.
  587. This make the structure of the table stay in tact as long as the edited
  588. field does not exceed the column width.
  589. - Minimize the number of realigns. Normally, the table is aligned each time
  590. TAB or RET are pressed to move to another field. With optimization this
  591. happens only if changes to a field might have changed the column width.
  592. Optimization requires replacing the functions `self-insert-command',
  593. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  594. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  595. very good at guessing when a re-align will be necessary, but you can always
  596. force one with \\[org-ctrl-c-ctrl-c].
  597. If you would like to use the optimized version in Org-mode, but the
  598. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  599. This variable can be used to turn on and off the table editor during a session,
  600. but in order to toggle optimization, a restart is required.
  601. See also the variable `org-table-auto-blank-field'."
  602. :group 'org-table
  603. :type '(choice
  604. (const :tag "off" nil)
  605. (const :tag "on" t)
  606. (const :tag "on, optimized" optimized)))
  607. (defcustom orgtbl-optimized (eq org-enable-table-editor 'optimized)
  608. "Non-nil means, use the optimized table editor version for `orgtbl-mode'.
  609. In the optimized version, the table editor takes over all simple keys that
  610. normally just insert a character. In tables, the characters are inserted
  611. in a way to minimize disturbing the table structure (i.e. in overwrite mode
  612. for empty fields). Outside tables, the correct binding of the keys is
  613. restored.
  614. The default for this option is t if the optimized version is also used in
  615. Org-mode. See the variable `org-enable-table-editor' for details. Changing
  616. this variable requires a restart of Emacs to become effective."
  617. :group 'org-table
  618. :type 'boolean)
  619. (defgroup org-table-settings nil
  620. "Settings for tables in Org-mode."
  621. :tag "Org Table Settings"
  622. :group 'org-table)
  623. (defcustom org-table-default-size "5x2"
  624. "The default size for newly created tables, Columns x Rows."
  625. :group 'org-table-settings
  626. :type 'string)
  627. (defcustom org-table-number-regexp "^[<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*$"
  628. "Regular expression for recognizing numbers in table columns.
  629. If a table column contains mostly numbers, it will be aligned to the
  630. right. If not, it will be aligned to the left.
  631. The default value of this option is a regular expression which allows
  632. anything which looks remotely like a number as used in scientific
  633. context. For example, all of the following will be considered a
  634. number:
  635. 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
  636. Other options offered by the customize interface are more restrictive."
  637. :group 'org-table-settings
  638. :type '(choice
  639. (const :tag "Positive Integers"
  640. "^[0-9]+$")
  641. (const :tag "Integers"
  642. "^[-+]?[0-9]+$")
  643. (const :tag "Floating Point Numbers"
  644. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
  645. (const :tag "Floating Point Number or Integer"
  646. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
  647. (const :tag "Exponential, Floating point, Integer"
  648. "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
  649. (const :tag "Very General Number-Like"
  650. "^[<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*$")
  651. (string :tag "Regexp:")))
  652. (defcustom org-table-number-fraction 0.5
  653. "Fraction of numbers in a column required to make the column align right.
  654. In a column all non-white fields are considered. If at least this
  655. fraction of fields is matched by `org-table-number-fraction',
  656. alignment to the right border applies."
  657. :group 'org-table-settings
  658. :type 'number)
  659. (defgroup org-table-editing nil
  660. "Bahavior of tables during editing in Org-mode."
  661. :tag "Org Table Editing"
  662. :group 'org-table)
  663. (defcustom org-table-automatic-realign t
  664. "Non-nil means, automatically re-align table when pressing TAB or RETURN.
  665. When nil, aligning is only done with \\[org-table-align], or after column
  666. removal/insertion."
  667. :group 'org-table-editing
  668. :type 'boolean)
  669. (defcustom org-table-limit-column-width t ;kw
  670. "Non-nil means, allow to limit the width of table columns with <N> fields."
  671. :group 'org-table-editing
  672. :type 'boolean)
  673. (defcustom org-table-auto-blank-field t
  674. "Non-nil means, automatically blank table field when starting to type into it.
  675. This only happens when typing immediately after a field motion
  676. command (TAB, S-TAB or RET).
  677. Only relevant when `org-enable-table-editor' is equal to `optimized'."
  678. :group 'org-table-editing
  679. :type 'boolean)
  680. (defcustom org-table-tab-jumps-over-hlines t
  681. "Non-nil means, tab in the last column of a table with jump over a hline.
  682. If a horizontal separator line is following the current line,
  683. `org-table-next-field' can either create a new row before that line, or jump
  684. over the line. When this option is nil, a new line will be created before
  685. this line."
  686. :group 'org-table-editing
  687. :type 'boolean)
  688. (defcustom org-table-tab-recognizes-table.el t
  689. "Non-nil means, TAB will automatically notice a table.el table.
  690. When it sees such a table, it moves point into it and - if necessary -
  691. calls `table-recognize-table'."
  692. :group 'org-table-editing
  693. :type 'boolean)
  694. (defgroup org-table-calculation nil
  695. "Options concerning tables in Org-mode."
  696. :tag "Org Table Calculation"
  697. :group 'org-table)
  698. (defcustom org-table-copy-increment t
  699. "Non-nil means, increment when copying current field with \\[org-table-copy-down]."
  700. :group 'org-table-calculation
  701. :type 'boolean)
  702. (defcustom org-calc-default-modes
  703. '(calc-internal-prec 12
  704. calc-float-format (float 5)
  705. calc-angle-mode deg
  706. calc-prefer-frac nil
  707. calc-symbolic-mode nil
  708. calc-date-format (YYYY "-" MM "-" DD " " Www (" " HH ":" mm))
  709. calc-display-working-message t
  710. )
  711. "List with Calc mode settings for use in calc-eval for table formulas.
  712. The list must contain alternating symbols (Calc modes variables and values).
  713. Don't remove any of the default settings, just change the values. Org-mode
  714. relies on the variables to be present in the list."
  715. :group 'org-table-calculation
  716. :type 'plist)
  717. (defcustom org-table-formula-evaluate-inline t
  718. "Non-nil means, TAB and RET evaluate a formula in current table field.
  719. If the current field starts with an equal sign, it is assumed to be a formula
  720. which should be evaluated as described in the manual and in the documentation
  721. string of the command `org-table-eval-formula'. This feature requires the
  722. Emacs calc package.
  723. When this variable is nil, formula calculation is only available through
  724. the command \\[org-table-eval-formula]."
  725. :group 'org-table-calculation
  726. :type 'boolean)
  727. (defcustom org-table-formula-use-constants t
  728. "Non-nil means, interpret constants in formulas in tables.
  729. A constant looks like `$c' or `$Grav' and will be replaced before evaluation
  730. by the value given in `org-table-formula-constants', or by a value obtained
  731. from the `constants.el' package."
  732. :group 'org-table-calculation
  733. :type 'boolean)
  734. (defcustom org-table-formula-constants nil
  735. "Alist with constant names and values, for use in table formulas.
  736. The car of each element is a name of a constant, without the `$' before it.
  737. The cdr is the value as a string. For example, if you'd like to use the
  738. speed of light in a formula, you would configure
  739. (setq org-table-formula-constants '((\"c\" . \"299792458.\")))
  740. and then use it in an equation like `$1*$c'."
  741. :group 'org-table-calculation
  742. :type '(repeat
  743. (cons (string :tag "name")
  744. (string :tag "value"))))
  745. (defcustom org-table-formula-numbers-only nil
  746. "Non-nil means, calculate only with numbers in table formulas.
  747. Then all input fields will be converted to a number, and the result
  748. must also be a number. When nil, calc's full potential is available
  749. in table calculations, including symbolics etc."
  750. :group 'org-table-calculation
  751. :type 'boolean)
  752. (defcustom org-table-allow-automatic-line-recalculation t
  753. "Non-nil means, lines marked with |#| or |*| will be recomputed automatically.
  754. Automatically means, when TAB or RET or C-c C-c are pressed in the line."
  755. :group 'org-table-calculation
  756. :type 'boolean)
  757. (defgroup org-link nil
  758. "Options concerning links in Org-mode."
  759. :tag "Org Link"
  760. :group 'org)
  761. (defcustom org-descriptive-links t
  762. "Non-nil means, hide link part and only show description of bracket links.
  763. Bracket links are like [[link][descritpion]]. This variable sets the initial
  764. state in new org-mode buffers. The setting can then be toggled on a
  765. per-buffer basis from the Org->Hyperlinks menu."
  766. :group 'org-link
  767. :type 'boolean)
  768. (defcustom org-link-style 'bracket
  769. "The style of links to be inserted with \\[org-insert-link].
  770. Possible values are:
  771. bracket [[link][description]]. This is recommended
  772. plain Description \\n link. The old way, no longer recommended."
  773. :group 'org-link
  774. :type '(choice
  775. (const :tag "Bracket (recommended)" bracket)
  776. (const :tag "Plain (no longer recommended)" plain)))
  777. (defcustom org-link-format "%s"
  778. "Default format for external, URL-like linkes in the buffer.
  779. This is a format string for printf, %s will be replaced by the link text.
  780. The recommended value is just \"%s\", since links will be protected by
  781. enclosing them in double brackets. If you prefer plain links (see variable
  782. `org-link-style'), \"<%s>\" is useful. Some people also recommend an
  783. additional URL: prefix, so the format would be \"<URL:%s>\"."
  784. :group 'org-link
  785. :type '(choice
  786. (const :tag "\"%s\" (e.g. http://www.there.com)" "%s")
  787. (const :tag "\"<%s>\" (e.g. <http://www.there.com>)" "<%s>")
  788. (const :tag "\"<URL:%s>\" (e.g. <URL:http://www.there.com>)" "<URL:%s>")
  789. (string :tag "Other" :value "<%s>")))
  790. (defcustom org-link-file-path-type 'adaptive
  791. "How the path name in file links should be stored.
  792. Valid values are:
  793. relative relative to the current directory, i.e. the directory of the file
  794. into which the link is being inserted.
  795. absolute absolute path, if possible with ~ for home directory.
  796. noabbrev absolute path, no abbreviation of home directory.
  797. adaptive Use relative path for files in the current directory and sub-
  798. directories of it. For other files, use an absolute path."
  799. :group 'org-link
  800. :type '(choice
  801. (const relative)
  802. (const absolute)
  803. (const noabbrev)
  804. (const adaptive)))
  805. (defcustom org-activate-links '(bracket angle plain radio tag date)
  806. "Types of links that should be activated in Org-mode files.
  807. This is a list of symbols, each leading to the activation of a certain link
  808. type. In principle, it does not hurt to turn on most link types - there may
  809. be a small gain when turning off unused link types. The types are:
  810. bracket The recommended [[link][description]] or [[link]] links with hiding.
  811. angular Links in angular brackes that may contain whitespace like
  812. <bbdb:Carsten Dominik>.
  813. plain Plain links in normal text, no whitespace, like http://google.com.
  814. radio Text that is matched by a radio target, see manual for details.
  815. tag Tag settings in a headline (link to tag search).
  816. date Time stamps (link to calendar).
  817. camel CamelCase words defining text searches.
  818. Changing this variable requires a restart of Emacs to become effective."
  819. :group 'org-link
  820. :type '(set (const :tag "Double bracket links (new style)" bracket)
  821. (const :tag "Angular bracket links (old style)" angular)
  822. (const :tag "plain text links" plain)
  823. (const :tag "Radio target matches" radio)
  824. (const :tag "Tags" tag)
  825. (const :tag "Timestamps" date)
  826. (const :tag "CamelCase words" camel)))
  827. (defgroup org-link-store nil
  828. "Options concerning storing links in Org-mode"
  829. :tag "Org Store Link"
  830. :group 'org-link)
  831. (defcustom org-context-in-file-links t
  832. "Non-nil means, file links from `org-store-link' contain context.
  833. A search string will be added to the file name with :: as separator and
  834. used to find the context when the link is activated by the command
  835. `org-open-at-point'.
  836. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  837. negates this setting for the duration of the command."
  838. :group 'org-link-store
  839. :type 'boolean)
  840. (defcustom org-file-link-context-use-camel-case nil
  841. "Non-nil means, use CamelCase to store a search context in a file link.
  842. When nil, the search string simply consists of the words of the string.
  843. CamelCase is deprecated, and support for it may be dropped in the future."
  844. :group 'org-link-store
  845. :type 'boolean)
  846. (defcustom org-keep-stored-link-after-insertion nil
  847. "Non-nil means, keep link in list for entire session.
  848. The command `org-store-link' adds a link pointing to the current
  849. location to an internal list. These links accumulate during a session.
  850. The command `org-insert-link' can be used to insert links into any
  851. Org-mode file (offering completion for all stored links). When this
  852. option is nil, every link which has been inserted once using \\[org-insert-link]
  853. will be removed from the list, to make completing the unused links
  854. more efficient."
  855. :group 'org-link-store
  856. :type 'boolean)
  857. (defcustom org-usenet-links-prefer-google nil
  858. "Non-nil means, `org-store-link' will create web links to Google groups.
  859. When nil, Gnus will be used for such links.
  860. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  861. negates this setting for the duration of the command."
  862. :group 'org-link-store
  863. :type 'boolean)
  864. (defgroup org-link-follow nil
  865. "Options concerning following links in Org-mode"
  866. :tag "Org Follow Link"
  867. :group 'org-link)
  868. (defcustom org-tab-follows-link nil
  869. "Non-nil means, on links TAB will follow the link.
  870. Needs to be set before org.el is loaded."
  871. :group 'org-link-follow
  872. :type 'boolean)
  873. (defcustom org-return-follows-link nil
  874. "Non-nil means, on links RET will follow the link.
  875. Needs to be set before org.el is loaded."
  876. :group 'org-link-follow
  877. :type 'boolean)
  878. (defcustom org-mouse-1-follows-link t
  879. "Non-nil means, mouse-1 on a link will follow the link.
  880. A longer mouse click will still set point. Does not wortk on XEmacs.
  881. Needs to be set before org.el is loaded."
  882. :group 'org-link-follow
  883. :type 'boolean)
  884. (defcustom org-mark-ring-length 4
  885. "Number of different positions to be recorded in the ring
  886. Changing this requires a restart of Emacs to work correctly."
  887. :group 'org-link-follow
  888. :type 'interger)
  889. (defcustom org-link-frame-setup
  890. '((vm . vm-visit-folder-other-frame)
  891. (gnus . gnus-other-frame)
  892. (file . find-file-other-window))
  893. "Setup the frame configuration for following links.
  894. When following a link with Emacs, it may often be useful to display
  895. this link in another window or frame. This variable can be used to
  896. set this up for the different types of links.
  897. For VM, use any of
  898. `vm-visit-folder'
  899. `vm-visit-folder-other-frame'
  900. For Gnus, use any of
  901. `gnus'
  902. `gnus-other-frame'
  903. For FILE, use any of
  904. `find-file'
  905. `find-file-other-window'
  906. `find-file-other-frame'
  907. For the calendar, use the variable `calendar-setup'.
  908. For BBDB, it is currently only possible to display the matches in
  909. another window."
  910. :group 'org-link-follow
  911. :type '(list
  912. (cons (const vm)
  913. (choice
  914. (const vm-visit-folder)
  915. (const vm-visit-folder-other-window)
  916. (const vm-visit-folder-other-frame)))
  917. (cons (const gnus)
  918. (choice
  919. (const gnus)
  920. (const gnus-other-frame)))
  921. (cons (const file)
  922. (choice
  923. (const find-file)
  924. (const find-file-other-window)
  925. (const find-file-other-frame)))))
  926. (defcustom org-open-non-existing-files nil
  927. "Non-nil means, `org-open-file' will open non-existing file.
  928. When nil, an error will be generated."
  929. :group 'org-link-follow
  930. :type 'boolean)
  931. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  932. "Non-nil means, ask for confirmation before executing shell links.
  933. Shell links can be dangerous, just thing about a link
  934. [[shell:rm -rf ~/*][Google Search]]
  935. This link would show up in your Org-mode document as \"Google Search\"
  936. but really it would remove your entire home directory.
  937. Therefore I *definitely* advise against setting this variable to nil.
  938. Just change it to `y-or-n-p' of you want to confirm with a single key press
  939. rather than having to type \"yes\"."
  940. :group 'org-link-follow
  941. :type '(choice
  942. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  943. (const :tag "with y-or-n (faster)" y-or-n-p)
  944. (const :tag "no confirmation (dangerous)" nil)))
  945. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  946. "Non-nil means, ask for confirmation before executing elisp links.
  947. Elisp links can be dangerous, just thing about a link
  948. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  949. This link would show up in your Org-mode document as \"Google Search\"
  950. but really it would remove your entire home directory.
  951. Therefore I *definitely* advise against setting this variable to nil.
  952. Just change it to `y-or-n-p' of you want to confirm with a single key press
  953. rather than having to type \"yes\"."
  954. :group 'org-link-follow
  955. :type '(choice
  956. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  957. (const :tag "with y-or-n (faster)" y-or-n-p)
  958. (const :tag "no confirmation (dangerous)" nil)))
  959. (defconst org-file-apps-defaults-gnu
  960. '((remote . emacs)
  961. (t . mailcap))
  962. "Default file applications on a UNIX or GNU/Linux system.
  963. See `org-file-apps'.")
  964. (defconst org-file-apps-defaults-macosx
  965. '((remote . emacs)
  966. (t . "open %s")
  967. ("ps" . "gv %s")
  968. ("ps.gz" . "gv %s")
  969. ("eps" . "gv %s")
  970. ("eps.gz" . "gv %s")
  971. ("dvi" . "xdvi %s")
  972. ("fig" . "xfig %s"))
  973. "Default file applications on a MacOS X system.
  974. The system \"open\" is known as a default, but we use X11 applications
  975. for some files for which the OS does not have a good default.
  976. See `org-file-apps'.")
  977. (defconst org-file-apps-defaults-windowsnt
  978. (list
  979. '(remote . emacs)
  980. (cons t
  981. (list (if (featurep 'xemacs)
  982. 'mswindows-shell-execute
  983. 'w32-shell-execute)
  984. "open" 'file)))
  985. "Default file applications on a Windows NT system.
  986. The system \"open\" is used for most files.
  987. See `org-file-apps'.")
  988. (defcustom org-file-apps
  989. '(
  990. ("txt" . emacs)
  991. ("tex" . emacs)
  992. ("ltx" . emacs)
  993. ("org" . emacs)
  994. ("el" . emacs)
  995. ("bib" . emacs)
  996. )
  997. "External applications for opening `file:path' items in a document.
  998. Org-mode uses system defaults for different file types, but
  999. you can use this variable to set the application for a given file
  1000. extension. The entries in this list are cons cells where the car identifies
  1001. files and the cdr the corresponding command. Possible values for the
  1002. file identifier are
  1003. \"ext\" A string identifying an extension
  1004. `directory' Matches a directory
  1005. `remote' Matches a remote file, accessible through tramp or efs.
  1006. Remote files most likely should be visited through emacs
  1007. because external applications cannot handle such paths.
  1008. t Default for all remaining files
  1009. Possible values for the command are:
  1010. `emacs' The file will be visited by the current Emacs process.
  1011. `default' Use the default application for this file type.
  1012. string A command to be executed by a shell; %s will be replaced
  1013. by the path to the file.
  1014. sexp A Lisp form which will be evaluated. The file path will
  1015. be available in the Lisp variable `file'.
  1016. For more examples, see the system specific constants
  1017. `org-file-apps-defaults-macosx'
  1018. `org-file-apps-defaults-windowsnt'
  1019. `org-file-apps-defaults-gnu'."
  1020. :group 'org-link-follow
  1021. :type '(repeat
  1022. (cons (choice :value ""
  1023. (string :tag "Extension")
  1024. (const :tag "Default for unrecognized files" t)
  1025. (const :tag "Remote file" remote)
  1026. (const :tag "Links to a directory" directory))
  1027. (choice :value ""
  1028. (const :tag "Visit with Emacs" emacs)
  1029. (const :tag "Use system default" default)
  1030. (string :tag "Command")
  1031. (sexp :tag "Lisp form")))))
  1032. (defcustom org-mhe-search-all-folders nil
  1033. "Non-nil means, that the search for the mh-message will be extended to
  1034. all folders if the message cannot be found in the folder given in the link.
  1035. Searching all folders is very effective with one of the search engines
  1036. supported by MH-E, but will be slow with pick."
  1037. :group 'org-link-follow
  1038. :type 'boolean)
  1039. (defgroup org-remember nil
  1040. "Options concerning interaction with remember.el."
  1041. :tag "Org Remember"
  1042. :group 'org)
  1043. (defcustom org-directory "~/org"
  1044. "Directory with org files.
  1045. This directory will be used as default to prompt for org files.
  1046. Used by the hooks for remember.el."
  1047. :group 'org-remember
  1048. :type 'directory)
  1049. (defcustom org-default-notes-file "~/.notes"
  1050. "Default target for storing notes.
  1051. Used by the hooks for remember.el. This can be a string, or nil to mean
  1052. the value of `remember-data-file'."
  1053. :group 'org-remember
  1054. :type '(choice
  1055. (const :tag "Default from remember-data-file" nil)
  1056. file))
  1057. (defcustom org-remember-templates nil
  1058. "Templates for the creation of remember buffers.
  1059. When nil, just let remember make the buffer.
  1060. When not nil, this is a list of 3-element lists. In each entry, the first
  1061. element is a character, a unique key to select this template.
  1062. The second element is the template. The third element is optional and can
  1063. specify a destination file for remember items created with this template.
  1064. The default file is given by `org-default-notes-file'.
  1065. The template specifies the structure of the remember buffer. It should have
  1066. a first line starting with a star, to act as the org-mode headline.
  1067. Furthermore, the following %-escapes will be replaced with content:
  1068. %t time stamp, date only
  1069. %T time stamp with date and time
  1070. %u inactive time stamp, date only
  1071. %U inactive time stamp with date and time
  1072. %n user name
  1073. %a annotation, normally the link created with org-store-link
  1074. %i initial content, the region when remember is called with C-u.
  1075. If %i is indented, the entire inserted text will be indented as well.
  1076. %? This will be removed, and the cursor placed at this position."
  1077. :group 'org-remember
  1078. :type '(repeat :tag "enabled"
  1079. (list :value (?a "\n" nil)
  1080. (character :tag "Selection Key")
  1081. (string :tag "Template")
  1082. (file :tag "Destination file (optional)"))))
  1083. (defcustom org-reverse-note-order nil
  1084. "Non-nil means, store new notes at the beginning of a file or entry.
  1085. When nil, new notes will be filed to the end of a file or entry."
  1086. :group 'org-remember
  1087. :type '(choice
  1088. (const :tag "Reverse always" t)
  1089. (const :tag "Reverse never" nil)
  1090. (repeat :tag "By file name regexp"
  1091. (cons regexp boolean))))
  1092. (defgroup org-todo nil
  1093. "Options concerning TODO items in Org-mode."
  1094. :tag "Org TODO"
  1095. :group 'org)
  1096. (defcustom org-todo-keywords '("TODO" "DONE")
  1097. "List of TODO entry keywords.
  1098. \\<org-mode-map>By default, this is '(\"TODO\" \"DONE\"). The last entry in the list is
  1099. considered to mean that the entry is \"done\". All the other mean that
  1100. action is required, and will make the entry show up in todo lists, diaries
  1101. etc.
  1102. The command \\[org-todo] cycles an entry through these states, and an
  1103. additional state where no keyword is present. For details about this
  1104. cycling, see also the variable `org-todo-interpretation'
  1105. Changes become only effective after restarting Emacs."
  1106. :group 'org-todo
  1107. :group 'org-keywords
  1108. :type '(repeat (string :tag "Keyword")))
  1109. (defcustom org-todo-interpretation 'sequence
  1110. "Controls how TODO keywords are interpreted.
  1111. This variable is only relevant if `org-todo-keywords' contains more than two
  1112. states. \\<org-mode-map>Possible values are `sequence' and `type'.
  1113. When `sequence', \\[org-todo] will always switch to the next state in the
  1114. `org-todo-keywords' list. When `type', \\[org-todo] only cycles from state
  1115. to state when executed several times in direct succession. Otherwise, it
  1116. switches directly to DONE from any state.
  1117. See the manual for more information."
  1118. :group 'org-todo
  1119. :group 'org-keywords
  1120. :type '(choice (const sequence)
  1121. (const type)))
  1122. (defcustom org-after-todo-state-change-hook nil
  1123. "Hook which is run after the state of a TODO item was changed.
  1124. The new state (a string with a TODO keyword, or nil) is available in the
  1125. Lisp variable `state'."
  1126. :group 'org-todo
  1127. :type 'hook)
  1128. (defcustom org-log-done nil
  1129. "When set, insert a (non-active) time stamp when TODO entry is marked DONE.
  1130. When the state of an entry is changed from nothing to TODO, remove a previous
  1131. closing date.
  1132. This can also be configured on a per-file basis by adding one of
  1133. the following lines anywhere in the buffer:
  1134. #+STARTUP: logging
  1135. #+STARTUP: nologging"
  1136. :group 'org-todo
  1137. :type 'boolean)
  1138. (defgroup org-priorities nil
  1139. "Priorities in Org-mode."
  1140. :tag "Org Priorities"
  1141. :group 'org-todo)
  1142. (defcustom org-default-priority ?B
  1143. "The default priority of TODO items.
  1144. This is the priority an item get if no explicit priority is given."
  1145. :group 'org-priorities
  1146. :type 'character)
  1147. (defcustom org-lowest-priority ?C
  1148. "The lowest priority of TODO items. A character like ?A, ?B etc."
  1149. :group 'org-priorities
  1150. :type 'character)
  1151. (defgroup org-time nil
  1152. "Options concerning time stamps and deadlines in Org-mode."
  1153. :tag "Org Time"
  1154. :group 'org)
  1155. (defcustom org-insert-labeled-timestamps-at-point nil
  1156. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1157. When nil, these labeled time stamps are forces into the second line of an
  1158. entry, just after the headline. When scheduling from the global TODO list,
  1159. the time stamp will always be forced into the second line."
  1160. :group 'org-time
  1161. :type 'boolean)
  1162. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1163. "Formats for `format-time-string' which are used for time stamps.
  1164. It is not recommended to change this constant.")
  1165. (defcustom org-time-stamp-rounding-minutes 0
  1166. "Number of minutes to round time stamps to upon insertion.
  1167. When zero, insert the time unmodified. Useful rounding numbers
  1168. should be factors of 60, so for example 5, 10, 15.
  1169. When this is not zero, you can still force an exact time-stamp by using
  1170. a double prefix argument to a time-stamp command like `C-c .' or `C-c !'."
  1171. :group 'org-time
  1172. :type 'integer)
  1173. (defcustom org-deadline-warning-days 30
  1174. "No. of days before expiration during which a deadline becomes active.
  1175. This variable governs the display in the org file."
  1176. :group 'org-time
  1177. :type 'number)
  1178. (defcustom org-popup-calendar-for-date-prompt t
  1179. "Non-nil means, pop up a calendar when prompting for a date.
  1180. In the calendar, the date can be selected with mouse-1. However, the
  1181. minibuffer will also be active, and you can simply enter the date as well.
  1182. When nil, only the minibuffer will be available."
  1183. :group 'org-time
  1184. :type 'boolean)
  1185. (defcustom org-calendar-follow-timestamp-change t
  1186. "Non-nil means, make the calendar window follow timestamp changes.
  1187. When a timestamp is modified and the calendar window is visible, it will be
  1188. moved to the new date."
  1189. :group 'org-time
  1190. :type 'boolean)
  1191. (defgroup org-tags nil
  1192. "Options concerning tags in Org-mode."
  1193. :tag "Org Tags"
  1194. :group 'org)
  1195. (defcustom org-tag-alist nil
  1196. "List of tags allowed in Org-mode files.
  1197. When this list is nil, Org-mode will base TAG input on what is already in the
  1198. buffer.
  1199. The value of this variable is an alist, the car may be (and should) be a
  1200. character that is used to select that tag through the fast-tag-selection
  1201. interface. See the manual for details."
  1202. :group 'org-tags
  1203. :type '(repeat
  1204. (choice
  1205. (cons (string :tag "Tag name")
  1206. (character :tag "Access char"))
  1207. (const :tag "Start radio group" (:startgroup))
  1208. (const :tag "End radio group" (:endgroup)))))
  1209. (defcustom org-use-fast-tag-selection 'auto
  1210. "Non-nil means, use fast tag selection scheme.
  1211. This is a special interface to select and deselect tags with single keys.
  1212. When nil, fast selection is never used.
  1213. When the symbol `auto', fast selection is used if and only if selection
  1214. characters for tags have been configured, either through the variable
  1215. `org-tag-alist' or through a #+TAGS line in the buffer.
  1216. When t, fast selection is always used and selection keys are assigned
  1217. automatically if necessary."
  1218. :group 'org-tags
  1219. :type '(choice
  1220. (const :tag "Always" t)
  1221. (const :tag "Never" nil)
  1222. (const :tag "When selection characters are configured" 'auto)))
  1223. (defcustom org-tags-column 48
  1224. "The column to which tags should be indented in a headline.
  1225. If this number is positive, it specifies the column. If it is negative,
  1226. it means that the tags should be flushright to that column. For example,
  1227. -79 works well for a normal 80 character screen."
  1228. :group 'org-tags
  1229. :type 'integer)
  1230. (defcustom org-auto-align-tags t
  1231. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1232. These operations change the length of a headline and therefore shift
  1233. the tags around. With this options turned on, after each such operation
  1234. the tags are again aligned to `org-tags-column'."
  1235. :group 'org-tags
  1236. :type 'boolean)
  1237. (defcustom org-use-tag-inheritance t
  1238. "Non-nil means, tags in levels apply also for sublevels.
  1239. When nil, only the tags directly given in a specific line apply there.
  1240. If you turn off this option, you very likely want to turn on the
  1241. companion option `org-tags-match-list-sublevels'."
  1242. :group 'org-tags
  1243. :type 'boolean)
  1244. (defcustom org-tags-match-list-sublevels nil
  1245. "Non-nil means list also sublevels of headlines matching tag search.
  1246. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1247. the sublevels of a headline matching a tag search often also match
  1248. the same search. Listing all of them can create very long lists.
  1249. Setting this variable to nil causes subtrees of a match to be skipped.
  1250. This option is off by default, because inheritance in on. If you turn
  1251. inheritance off, you very likely want to turn this option on.
  1252. As a special case, if the tag search is restricted to TODO items, the
  1253. value of this variable is ignored and sublevels are always checked, to
  1254. make sure all corresponding TODO items find their way into the list."
  1255. :group 'org-tags
  1256. :type 'boolean)
  1257. (defvar org-tags-history nil
  1258. "History of minibuffer reads for tags.")
  1259. (defvar org-last-tags-completion-table nil
  1260. "The last used completion table for tags.")
  1261. (defgroup org-agenda nil
  1262. "Options concerning agenda display Org-mode."
  1263. :tag "Org Agenda"
  1264. :group 'org)
  1265. (defvar org-category nil
  1266. "Variable used by org files to set a category for agenda display.
  1267. Such files should use a file variable to set it, for example
  1268. -*- mode: org; org-category: \"ELisp\"
  1269. or contain a special line
  1270. #+CATEGORY: ELisp
  1271. If the file does not specify a category, then file's base name
  1272. is used instead.")
  1273. (make-variable-buffer-local 'org-category)
  1274. (defcustom org-agenda-files nil
  1275. "The files to be used for agenda display.
  1276. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1277. \\[org-remove-file]. You can also use customize to edit the list.
  1278. If the value of the variable is not a list but a single file name, then
  1279. the list of agenda files is actually stored and maintained in that file, one
  1280. agenda file per line."
  1281. :group 'org-agenda
  1282. :type '(choice
  1283. (repeat :tag "List of files" file)
  1284. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1285. (defcustom org-agenda-custom-commands '(("w" todo "WAITING"))
  1286. "Custom commands for the agenda.
  1287. These commands will be offered on the splash screen displayed by the
  1288. agenda dispatcher \\[org-agenda]. Each entry is a list of 3 items:
  1289. key The key (a single char as a string) to be associated with the command.
  1290. type The command type, any of the following symbols:
  1291. todo Entries with a specific TODO keyword, in all agenda files.
  1292. tags Tags match in all agenda files.
  1293. tags-todo Tags match in all agenda files, TODO entries only.
  1294. todo-tree Sparse tree of specific TODO keyword in *current* file.
  1295. tags-tree Sparse tree with all tags matches in *current* file.
  1296. occur-tree Occur sparse tree for current file.
  1297. match What to search for:
  1298. - a single keyword for TODO keyword searches
  1299. - a tags match expression for tags searches
  1300. - a regular expression for occur searches"
  1301. :group 'org-agenda
  1302. :type '(repeat
  1303. (list (string :tag "Key")
  1304. (choice :tag "Type"
  1305. (const :tag "Tags search in all agenda files" tags)
  1306. (const :tag "Tags search of TODO entries, all agenda files" tags-todo)
  1307. (const :tag "TODO keyword search in all agenda files" todo)
  1308. (const :tag "Tags sparse tree in current buffer" tags-tree)
  1309. (const :tag "TODO keyword tree in current buffer" todo-tree)
  1310. (const :tag "Occur tree in current buffer" occur-tree))
  1311. (string :tag "Match"))))
  1312. (defcustom org-agenda-todo-list-sublevels t
  1313. "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
  1314. When nil, the sublevels of a TODO entry are not checked, resulting in
  1315. potentially much shorter TODO lists."
  1316. :group 'org-agenda
  1317. :group 'org-todo
  1318. :type 'boolean)
  1319. (defcustom org-agenda-todo-ignore-scheduled nil
  1320. "Non-nil means, don't show scheduled entries in the global todo list.
  1321. The idea behind this is that by scheduling it, you have already taken care
  1322. of this item."
  1323. :group 'org-agenda
  1324. :group 'org-todo
  1325. :type 'boolean)
  1326. (defcustom org-agenda-include-all-todo nil
  1327. "Non-nil means, the agenda will always contain all TODO entries.
  1328. When nil, date-less entries will only be shown if `org-agenda' is called
  1329. with a prefix argument.
  1330. When non-nil, the TODO entries will be listed at the top of the agenda, before
  1331. the entries for specific days."
  1332. :group 'org-agenda
  1333. :type 'boolean)
  1334. (defcustom org-agenda-include-diary nil
  1335. "If non-nil, include in the agenda entries from the Emacs Calendar's diary."
  1336. :group 'org-agenda
  1337. :type 'boolean)
  1338. (defcustom org-calendar-to-agenda-key [?c]
  1339. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1340. The command `org-calendar-goto-agenda' will be bound to this key. The
  1341. default is the character `c' because then `c' can be used to switch back and
  1342. forth between agenda and calendar."
  1343. :group 'org-agenda
  1344. :type 'sexp)
  1345. (defgroup org-agenda-setup nil
  1346. "Options concerning setting up the Agenda window in Org Mode."
  1347. :tag "Org Agenda Window Setup"
  1348. :group 'org-agenda)
  1349. (defcustom org-agenda-mouse-1-follows-link nil
  1350. "Non-nil means, mouse-1 on a link will follow the link in the agenda.
  1351. A longer mouse click will still set point. Does not wortk on XEmacs.
  1352. Needs to be set before org.el is loaded."
  1353. :group 'org-agenda-setup
  1354. :type 'boolean)
  1355. (defcustom org-agenda-start-with-follow-mode nil
  1356. "The initial value of follwo-mode in a newly created agenda window."
  1357. :group 'org-agenda-setup
  1358. :type 'boolean)
  1359. (defcustom org-select-agenda-window t
  1360. "Non-nil means, after creating an agenda, move cursor into Agenda window.
  1361. When nil, cursor will remain in the current window."
  1362. :group 'org-agenda-setup
  1363. :type 'boolean)
  1364. (defcustom org-fit-agenda-window t
  1365. "Non-nil means, change window size of agenda to fit content."
  1366. :group 'org-agenda-setup
  1367. :type 'boolean)
  1368. (defgroup org-agenda-display nil
  1369. "Options concerning what to display initially in Agenda."
  1370. :tag "Org Agenda Display"
  1371. :group 'org-agenda)
  1372. (defcustom org-agenda-show-all-dates t
  1373. "Non-nil means, `org-agenda' shows every day in the selected range.
  1374. When nil, only the days which actually have entries are shown."
  1375. :group 'org-agenda-display
  1376. :type 'boolean)
  1377. (defcustom org-agenda-start-on-weekday 1
  1378. "Non-nil means, start the overview always on the specified weekday.
  1379. 0 denotes Sunday, 1 denotes Monday etc.
  1380. When nil, always start on the current day."
  1381. :group 'org-agenda-display
  1382. :type '(choice (const :tag "Today" nil)
  1383. (number :tag "Weekday No.")))
  1384. (defcustom org-agenda-ndays 7
  1385. "Number of days to include in overview display.
  1386. Should be 1 or 7."
  1387. :group 'org-agenda-display
  1388. :type 'number)
  1389. (defcustom org-agenda-use-time-grid t
  1390. "Non-nil means, show a time grid in the agenda schedule.
  1391. A time grid is a set of lines for specific times (like every two hours between
  1392. 8:00 and 20:00). The items scheduled for a day at specific times are
  1393. sorted in between these lines.
  1394. For details about when the grid will be shown, and what it will look like, see
  1395. the variable `org-agenda-time-grid'."
  1396. :group 'org-agenda-display
  1397. :type 'boolean)
  1398. (defcustom org-agenda-time-grid
  1399. '((daily today require-timed)
  1400. "----------------"
  1401. (800 1000 1200 1400 1600 1800 2000))
  1402. "The settings for time grid for agenda display.
  1403. This is a list of three items. The first item is again a list. It contains
  1404. symbols specifying conditions when the grid should be displayed:
  1405. daily if the agenda shows a single day
  1406. weekly if the agenda shows an entire week
  1407. today show grid on current date, independent of daily/weekly display
  1408. require-timed show grid only if at least on item has a time specification
  1409. The second item is a string which will be places behing the grid time.
  1410. The third item is a list of integers, indicating the times that should have
  1411. a grid line."
  1412. :group 'org-agenda-display
  1413. :type
  1414. '(list
  1415. (set :greedy t :tag "Grid Display Options"
  1416. (const :tag "Show grid in single day agenda display" daily)
  1417. (const :tag "Show grid in weekly agenda display" weekly)
  1418. (const :tag "Always show grid for today" today)
  1419. (const :tag "Show grid only if any timed entries are present"
  1420. require-timed)
  1421. (const :tag "Skip grid times already present in an entry"
  1422. remove-match))
  1423. (string :tag "Grid String")
  1424. (repeat :tag "Grid Times" (integer :tag "Time"))))
  1425. (defcustom org-agenda-sorting-strategy '(time-up category-keep priority-down)
  1426. "Sorting structure for the agenda items of a single day.
  1427. This is a list of symbols which will be used in sequence to determine
  1428. if an entry should be listed before another entry. The following
  1429. symbols are recognized:
  1430. time-up Put entries with time-of-day indications first, early first
  1431. time-down Put entries with time-of-day indications first, late first
  1432. category-keep Keep the default order of categories, corresponding to the
  1433. sequence in `org-agenda-files'.
  1434. category-up Sort alphabetically by category, A-Z.
  1435. category-down Sort alphabetically by category, 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 '(repeat
  1451. (choice
  1452. (const time-up)
  1453. (const time-down)
  1454. (const category-keep)
  1455. (const category-up)
  1456. (const category-down)
  1457. (const priority-up)
  1458. (const priority-down))))
  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 " %-12:c%?-12t% s"
  1473. "Format specification for the prefix of items in the agenda buffer.
  1474. This format works similar to a printf format, with the following meaning:
  1475. %c the category of the item, \"Diary\" for entries from the diary, or
  1476. as given by the CATEGORY keyword or derived from the file name.
  1477. %T the first tag of the item.
  1478. %t the time-of-day specification if one applies to the entry, in the
  1479. format HH:MM
  1480. %s Scheduling/Deadline information, a short string
  1481. All specifiers work basically like the standard `%s' of printf, but may
  1482. contain two additional characters: A question mark just after the `%' and
  1483. a whitespace/punctuation character just before the final letter.
  1484. If the first character after `%' is a question mark, the entire field
  1485. will only be included if the corresponding value applies to the
  1486. current entry. This is useful for fields which should have fixed
  1487. width when present, but zero width when absent. For example,
  1488. \"%?-12t\" will result in a 12 character time field if a time of the
  1489. day is specified, but will completely disappear in entries which do
  1490. not contain a time.
  1491. If there is punctuation or whitespace character just before the final
  1492. format letter, this character will be appended to the field value if
  1493. the value is not empty. For example, the format \"%-12:c\" leads to
  1494. \"Diary: \" if the category is \"Diary\". If the category were be
  1495. empty, no additional colon would be interted.
  1496. The default value of this option is \" %-12:c%?-12t% s\", meaning:
  1497. - Indent the line with two space characters
  1498. - Give the category in a 12 chars wide field, padded with whitespace on
  1499. the right (because of `-'). Append a colon if there is a category
  1500. (because of `:').
  1501. - If there is a time-of-day, put it into a 12 chars wide field. If no
  1502. time, don't put in an empty field, just skip it (because of '?').
  1503. - Finally, put the scheduling information and append a whitespace.
  1504. As another example, if you don't want the time-of-day of entries in
  1505. the prefix, you could use:
  1506. (setq org-agenda-prefix-format \" %-11:c% s\")
  1507. See also the variables `org-agenda-remove-times-when-in-prefix' and
  1508. `org-agenda-remove-tags-when-in-prefix'."
  1509. :type 'string
  1510. :group 'org-agenda-prefix)
  1511. (defvar org-prefix-format-compiled nil
  1512. "The compiled version of the most recently used prefix format.
  1513. Depending on which command was used last, this may be the compiled version
  1514. of `org-agenda-prefix-format' or `org-timeline-prefix-format'.")
  1515. (defcustom org-agenda-remove-times-when-in-prefix t
  1516. "Non-nil means, remove duplicate time specifications in agenda items.
  1517. When the format `org-agenda-prefix-format' contains a `%t' specifier, a
  1518. time-of-day specification in a headline or diary entry is extracted and
  1519. placed into the prefix. If this option is non-nil, the original specification
  1520. \(a timestamp or -range, or just a plain time(range) specification like
  1521. 11:30-4pm) will be removed for agenda display. This makes the agenda less
  1522. cluttered.
  1523. The option can be t or nil. It may also be the symbol `beg', indicating
  1524. that the time should only be removed what it is located at the beginning of
  1525. the headline/diary entry."
  1526. :group 'org-agenda-prefix
  1527. :type '(choice
  1528. (const :tag "Always" t)
  1529. (const :tag "Never" nil)
  1530. (const :tag "When at beginning of entry" beg)))
  1531. (defcustom org-agenda-remove-tags-when-in-prefix nil
  1532. "Non-nil means, remove the tags from the headline copy in the agenda.
  1533. When this is the symbol `prefix', only remove tags when
  1534. `org-agenda-prefix-format' contains a `%T' specifier."
  1535. :group 'org-agenda-prefix
  1536. :type '(choice
  1537. (const :tag "Always" t)
  1538. (const :tag "Never" nil)
  1539. (const :tag "When prefix format contains %T" prefix)))
  1540. (defgroup org-agenda-timeline nil
  1541. "Options concerning the timeline buffer in Org Mode."
  1542. :tag "Org Agenda Timeline"
  1543. :group 'org-agenda)
  1544. (defcustom org-timeline-prefix-format " % s"
  1545. "Like `org-agenda-prefix-format', but for the timeline of a single file."
  1546. :type 'string
  1547. :group 'org-agenda-timeline)
  1548. (defcustom org-select-timeline-window t
  1549. "Non-nil means, after creating a timeline, move cursor into Timeline window.
  1550. When nil, cursor will remain in the current window."
  1551. :group 'org-agenda-timeline
  1552. :type 'boolean)
  1553. (defcustom org-timeline-show-empty-dates 3
  1554. "Non-nil means, `org-timeline' also shows dates without an entry.
  1555. When nil, only the days which actually have entries are shown.
  1556. When t, all days between the first and the last date are shown.
  1557. When an integer, show also empty dates, but if there is a gap of more than
  1558. N days, just insert a special line indicating the size of the gap."
  1559. :group 'org-agenda-timeline
  1560. :type '(choice
  1561. (const :tag "None" nil)
  1562. (const :tag "All" t)
  1563. (number :tag "at most")))
  1564. (defgroup org-latex nil
  1565. "Options for embedding LaTeX code into Org-mode"
  1566. :tag "Org LaTeX"
  1567. :group 'org)
  1568. (defcustom org-format-latex-options
  1569. '(:foreground "Black" :background "Transparent" :scale 1.0
  1570. :matchers ("begin" "$" "$$" "\\(" "\\["))
  1571. "Options for creating images from LaTeX fragments.
  1572. This is a property list with the following properties:
  1573. :foreground the foreground color, for example \"Black\".
  1574. :background the background color, or \"Transparent\".
  1575. :scale a scaling factor for the size of the images
  1576. :matchers a list indicating which matchers should be used to
  1577. find LaTeX fragments. Valid members of this list are:
  1578. \"begin\" find environments
  1579. \"$\" find math expressions surrounded by $...$
  1580. \"$$\" find math expressions surrounded by $$....$$
  1581. \"\\(\" find math expressions surrounded by \\(...\\)
  1582. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  1583. :group 'org-latex
  1584. :type 'plist)
  1585. (defgroup org-export nil
  1586. "Options for exporting org-listings."
  1587. :tag "Org Export"
  1588. :group 'org)
  1589. (defgroup org-export-general nil
  1590. "General options for exporting Org-mode files."
  1591. :tag "Org Export General"
  1592. :group 'org-export)
  1593. (defcustom org-export-publishing-directory "."
  1594. "Path to the location where exported files should be located.
  1595. This path may be relative to the directory where the Org-mode file lives.
  1596. The default is to put them into the same directory as the Org-mode file.
  1597. The variable may also be an alist with export types `:html', `:ascii',
  1598. `:ical', or `:xoxo' and the corresponding directories. If a direcoty path
  1599. is relative, it is interpreted relative to the directory where the exported
  1600. Org-mode files lives."
  1601. :group 'org-export-general
  1602. :type '(choice
  1603. (directory)
  1604. (repeat
  1605. (cons
  1606. (choice :tag "Type"
  1607. (const :html) (const :ascii) (const :ical) (const :xoxo))
  1608. (directory)))))
  1609. (defcustom org-export-language-setup
  1610. '(("en" "Author" "Date" "Table of Contents")
  1611. ("cs" "Autor" "Datum" "Obsah")
  1612. ("da" "Ophavsmand" "Dato" "Indhold")
  1613. ("de" "Autor" "Datum" "Inhaltsverzeichnis")
  1614. ("es" "Autor" "Fecha" "\xccndice")
  1615. ("fr" "Auteur" "Date" "Table des Mati\xe8res")
  1616. ("it" "Autore" "Data" "Indice")
  1617. ("nl" "Auteur" "Datum" "Inhoudsopgave")
  1618. ("nn" "Forfattar" "Dato" "Innhold") ;; nn = Norsk (nynorsk)
  1619. ("sv" "F\xf6rfattarens" "Datum" "Inneh\xe5ll"))
  1620. "Terms used in export text, translated to different languages.
  1621. Use the variable `org-export-default-language' to set the language,
  1622. or use the +OPTION lines for a per-file setting."
  1623. :group 'org-export-general
  1624. :type '(repeat
  1625. (list
  1626. (string :tag "HTML language tag")
  1627. (string :tag "Author")
  1628. (string :tag "Date")
  1629. (string :tag "Table of Contents"))))
  1630. (defcustom org-export-default-language "en"
  1631. "The default language of HTML export, as a string.
  1632. This should have an association in `org-export-language-setup'."
  1633. :group 'org-export-general
  1634. :type 'string)
  1635. (defcustom org-export-headline-levels 3
  1636. "The last level which is still exported as a headline.
  1637. Inferior levels will produce itemize lists when exported.
  1638. Note that a numeric prefix argument to an exporter function overrides
  1639. this setting.
  1640. This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
  1641. :group 'org-export-general
  1642. :type 'number)
  1643. (defcustom org-export-with-section-numbers t
  1644. "Non-nil means, add section numbers to headlines when exporting.
  1645. This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
  1646. :group 'org-export-general
  1647. :type 'boolean)
  1648. (defcustom org-export-with-toc t
  1649. "Non-nil means, create a table of contents in exported files.
  1650. The TOC contains headlines with levels up to`org-export-headline-levels'.
  1651. Headlines which contain any TODO items will be marked with \"(*)\" in
  1652. ASCII export, and with red color in HTML output.
  1653. In HTML output, the TOC will be clickable.
  1654. This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"."
  1655. :group 'org-export-general
  1656. :type 'boolean)
  1657. (defcustom org-export-mark-todo-in-toc nil
  1658. "Non-nil means, mark TOC lines that contain any open TODO items."
  1659. :group 'org-export-general
  1660. :type 'boolean)
  1661. (defcustom org-export-preserve-breaks nil
  1662. "Non-nil means, preserve all line breaks when exporting.
  1663. Normally, in HTML output paragraphs will be reformatted. In ASCII
  1664. export, line breaks will always be preserved, regardless of this variable.
  1665. This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
  1666. :group 'org-export-general
  1667. :type 'boolean)
  1668. (defcustom org-export-with-archived-trees 'headline
  1669. "Whether subtrees with the ARCHIVE tag should be exported.
  1670. This can have three different values
  1671. nil Do not export, pretend this tree is not present
  1672. t Do export the entire tree
  1673. headline Only export the headline, but skip the tree below it."
  1674. :group 'org-export-general
  1675. :group 'org-archive
  1676. :type '(choice
  1677. (const :tag "not at all" nil)
  1678. (const :tag "headline only" 'headline)
  1679. (const :tag "entirely" t)))
  1680. (defcustom org-export-with-timestamps t
  1681. "Nil means, do not export time stamps and associated keywords."
  1682. :group 'org-export
  1683. :type 'boolean)
  1684. (defcustom org-export-remove-timestamps-from-toc t
  1685. "Nil means, remove timestamps from the table of contents entries."
  1686. :group 'org-export
  1687. :type 'boolean)
  1688. (defcustom org-export-with-tags t
  1689. "Nil means, do not export tags, just remove them from headlines."
  1690. :group 'org-export-general
  1691. :type 'boolean)
  1692. (defcustom org-export-with-timestamps t
  1693. "Nil means, do not export timestamps and associated keywords."
  1694. :group 'org-export-general
  1695. :type 'boolean)
  1696. (defgroup org-export-translation nil
  1697. "Options for translating special ascii sequences for the export backends."
  1698. :tag "Org Export Translation"
  1699. :group 'org-export)
  1700. (defcustom org-export-with-emphasize t
  1701. "Non-nil means, interpret *word*, /word/, and _word_ as emphasized text.
  1702. If the export target supports emphasizing text, the word will be
  1703. typeset in bold, italic, or underlined, respectively. Works only for
  1704. single words, but you can say: I *really* *mean* *this*.
  1705. Not all export backends support this.
  1706. This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
  1707. :group 'org-export-translation
  1708. :type 'boolean)
  1709. (defcustom org-export-with-sub-superscripts t
  1710. "Non-nil means, interpret \"_\" and \"^\" for export.
  1711. When this option is turned on, you can use TeX-like syntax for sub- and
  1712. superscripts. Several characters after \"_\" or \"^\" will be
  1713. considered as a single item - so grouping with {} is normally not
  1714. needed. For example, the following things will be parsed as single
  1715. sub- or superscripts.
  1716. 10^24 or 10^tau several digits will be considered 1 item.
  1717. 10^-12 or 10^-tau a leading sign with digits or a word
  1718. x^2-y^3 will be read as x^2 - y^3, because items are
  1719. terminated by almost any nonword/nondigit char.
  1720. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  1721. Still, ambiguity is possible - so when in doubt use {} to enclose the
  1722. sub/superscript.
  1723. Not all export backends support this, but HTML does.
  1724. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  1725. :group 'org-export-translation
  1726. :type 'boolean)
  1727. (defcustom org-export-with-TeX-macros t
  1728. "Non-nil means, interpret simple TeX-like macros when exporting.
  1729. For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
  1730. No only real TeX macros will work here, but the standard HTML entities
  1731. for math can be used as macro names as well. For a list of supported
  1732. names in HTML export, see the constant `org-html-entities'.
  1733. Not all export backends support this.
  1734. This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
  1735. :group 'org-export-translation
  1736. :group 'org-latex
  1737. :type 'boolean)
  1738. (defcustom org-export-with-LaTeX-fragments nil
  1739. "Non-nil means, convert LaTeX fragments to images when exporting to HTML.
  1740. When set, the exporter will find LaTeX environments if the \\begin line is
  1741. the first non-white thing on a line. It will also find the math delimiters
  1742. like $a=b$ and \\( a=b \\) for inline math, $$a=b$$ and \\[ a=b \\] for
  1743. display math.
  1744. This option can also be set with the +OPTIONS line, e.g. \"LaTeX:t\"."
  1745. :group 'org-export-translation
  1746. :group 'org-latex
  1747. :type 'boolean)
  1748. (defcustom org-export-with-fixed-width t
  1749. "Non-nil means, lines starting with \":\" will be in fixed width font.
  1750. This can be used to have pre-formatted text, fragments of code etc. For
  1751. example:
  1752. : ;; Some Lisp examples
  1753. : (while (defc cnt)
  1754. : (ding))
  1755. will be looking just like this in also HTML. See also the QUOTE keyword.
  1756. Not all export backends support this.
  1757. This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
  1758. :group 'org-export-translation
  1759. :type 'boolean)
  1760. (defcustom org-match-sexp-depth 3
  1761. "Number of stacked braces for sub/superscript matching.
  1762. This has to be set before loading org.el to be effective."
  1763. :group 'org-export-translation
  1764. :type 'integer)
  1765. (defgroup org-export-tables nil
  1766. "Options for exporting tables in Org-mode."
  1767. :tag "Org Export Tables"
  1768. :group 'org-export)
  1769. (defcustom org-export-with-tables t
  1770. "If non-nil, lines starting with \"|\" define a table.
  1771. For example:
  1772. | Name | Address | Birthday |
  1773. |-------------+----------+-----------|
  1774. | Arthur Dent | England | 29.2.2100 |
  1775. Not all export backends support this.
  1776. This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
  1777. :group 'org-export-tables
  1778. :type 'boolean)
  1779. (defcustom org-export-highlight-first-table-line t
  1780. "Non-nil means, highlight the first table line.
  1781. In HTML export, this means use <th> instead of <td>.
  1782. In tables created with table.el, this applies to the first table line.
  1783. In Org-mode tables, all lines before the first horizontal separator
  1784. line will be formatted with <th> tags."
  1785. :group 'org-export-tables
  1786. :type 'boolean)
  1787. (defcustom org-export-table-remove-special-lines t
  1788. "Remove special lines and marking characters in calculating tables.
  1789. This removes the special marking character column from tables that are set
  1790. up for spreadsheet calculations. It also removes the entire lines
  1791. marked with `!', `_', or `^'. The lines with `$' are kept, because
  1792. the values of constants may be useful to have."
  1793. :group 'org-export-tables
  1794. :type 'boolean)
  1795. (defcustom org-export-prefer-native-exporter-for-tables nil
  1796. "Non-nil means, always export tables created with table.el natively.
  1797. Natively means, use the HTML code generator in table.el.
  1798. When nil, Org-mode's own HTML generator is used when possible (i.e. if
  1799. the table does not use row- or column-spanning). This has the
  1800. advantage, that the automatic HTML conversions for math symbols and
  1801. sub/superscripts can be applied. Org-mode's HTML generator is also
  1802. much faster."
  1803. :group 'org-export-tables
  1804. :type 'boolean)
  1805. (defgroup org-export-ascii nil
  1806. "Options specific for ASCII export of Org-mode files."
  1807. :tag "Org Export ASCII"
  1808. :group 'org-export)
  1809. (defcustom org-export-ascii-underline '(?\$ ?\# ?^ ?\~ ?\= ?\-)
  1810. "Characters for underlining headings in ASCII export.
  1811. In the given sequence, these characters will be used for level 1, 2, ..."
  1812. :group 'org-export-ascii
  1813. :type '(repeat character))
  1814. (defcustom org-export-ascii-bullets '(?* ?+ ?-)
  1815. "Bullet characters for headlines converted to lists in ASCII export.
  1816. The first character is is used for the first lest level generated in this
  1817. way, and so on. If there are more levels than characters given here,
  1818. the list will be repeated.
  1819. Note that plain lists will keep the same bullets as the have in the
  1820. Org-mode file."
  1821. :group 'org-export-ascii
  1822. :type '(repeat character))
  1823. (defcustom org-export-ascii-show-new-buffer t
  1824. "Non-nil means, popup buffer containing the exported ASCII text.
  1825. Otherwise the buffer will just be saved to a file and stay hidden."
  1826. :group 'org-export-ascii
  1827. :type 'boolean)
  1828. (defgroup org-export-xml nil
  1829. "Options specific for XML export of Org-mode files."
  1830. :tag "Org Export XML"
  1831. :group 'org-export)
  1832. (defgroup org-export-html nil
  1833. "Options specific for HTML export of Org-mode files."
  1834. :tag "Org Export HTML"
  1835. :group 'org-export)
  1836. (defcustom org-export-html-style
  1837. "<style type=\"text/css\">
  1838. html {
  1839. font-family: Times, serif;
  1840. font-size: 12pt;
  1841. }
  1842. .title { text-align: center; }
  1843. .todo { color: red; }
  1844. .done { color: green; }
  1845. .timestamp { color: grey }
  1846. .timestamp-kwd { color: CadetBlue }
  1847. .tag { background-color:lightblue; font-weight:normal }
  1848. .target { background-color: lavender; }
  1849. pre {
  1850. border: 1pt solid #AEBDCC;
  1851. background-color: #F3F5F7;
  1852. padding: 5pt;
  1853. font-family: courier, monospace;
  1854. }
  1855. table { border-collapse: collapse; }
  1856. td, th {
  1857. vertical-align: top;
  1858. border: 1pt solid #ADB9CC;
  1859. }
  1860. </style>"
  1861. "The default style specification for exported HTML files.
  1862. Since there are different ways of setting style information, this variable
  1863. needs to contain the full HTML structure to provide a style, including the
  1864. surrounding HTML tags. The style specifications should include definitions
  1865. for new classes todo, done, title, and deadline. For example, legal values
  1866. would be:
  1867. <style type=\"text/css\">
  1868. p { font-weight: normal; color: gray; }
  1869. h1 { color: black; }
  1870. .title { text-align: center; }
  1871. .todo, .deadline { color: red; }
  1872. .done { color: green; }
  1873. </style>
  1874. or, if you want to keep the style in a file,
  1875. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  1876. As the value of this option simply gets inserted into the HTML <head> header,
  1877. you can \"misuse\" it to add arbitrary text to the header."
  1878. :group 'org-export-html
  1879. :type 'string)
  1880. (defcustom org-export-html-title-format "<h1 class=\"title\">%s</h1>\n"
  1881. "Format for typesetting the document title in HTML export."
  1882. :group 'org-export-html
  1883. :type 'string)
  1884. (defcustom org-export-html-toplevel-hlevel 2
  1885. "The <H> level for level 1 headings in HTML export."
  1886. :group 'org-export-html
  1887. :type 'string)
  1888. (defcustom org-export-html-link-org-files-as-html t
  1889. "Non-nil means, make file links to `file.org' point to `file.html'.
  1890. When org-mode is exporting an org-mode file to HTML, links to
  1891. non-html files are directly put into a href tag in HTML.
  1892. However, links to other Org-mode files (recognized by the
  1893. extension `.org.) should become links to the corresponding html
  1894. file, assuming that the linked org-mode file will also be
  1895. converted to HTML.
  1896. When nil, the links still point to the plain `.org' file."
  1897. :group 'org-export-html
  1898. :type 'boolean)
  1899. (defcustom org-export-html-inline-images 'maybe
  1900. "Non-nil means, inline images into exported HTML pages.
  1901. This is done using an <img> tag. When nil, an anchor with href is used to
  1902. link to the image. If this option is `maybe', then images in links with
  1903. an empty description will be inlined, while images with a description will
  1904. be linked only."
  1905. :group 'org-export-html
  1906. :type '(choice (const :tag "Never" nil)
  1907. (const :tag "Always" t)
  1908. (const :tag "When there is no description" maybe)))
  1909. (defcustom org-export-html-expand t
  1910. "Non-nil means, for HTML export, treat @<...> as HTML tag.
  1911. When nil, these tags will be exported as plain text and therefore
  1912. not be interpreted by a browser.
  1913. This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
  1914. :group 'org-export-html
  1915. :type 'boolean)
  1916. (defcustom org-export-html-table-tag
  1917. "<table border=\"1\" cellspacing=\"0\" cellpadding=\"6\">"
  1918. "The HTML tag used to start a table.
  1919. This must be a <table> tag, but you may change the options like
  1920. borders and spacing."
  1921. :group 'org-export-html
  1922. :type 'string)
  1923. (defcustom org-export-html-with-timestamp nil
  1924. "If non-nil, write `org-export-html-html-helper-timestamp'
  1925. into the exported HTML text. Otherwise, the buffer will just be saved
  1926. to a file."
  1927. :group 'org-export-html
  1928. :type 'boolean)
  1929. (defcustom org-export-html-html-helper-timestamp
  1930. "<br/><br/><hr><p><!-- hhmts start --> <!-- hhmts end --></p>\n"
  1931. "The HTML tag used as timestamp delimiter for HTML-helper-mode."
  1932. :group 'org-export-html
  1933. :type 'string)
  1934. (defcustom org-export-html-show-new-buffer nil
  1935. "Non-nil means, popup buffer containing the exported html text.
  1936. Otherwise, the buffer will just be saved to a file and stay hidden."
  1937. :group 'org-export-html
  1938. :type 'boolean)
  1939. (defgroup org-export-icalendar nil
  1940. "Options specific for iCalendar export of Org-mode files."
  1941. :tag "Org Export iCalendar"
  1942. :group 'org-export)
  1943. (defcustom org-combined-agenda-icalendar-file "~/org.ics"
  1944. "The file name for the iCalendar file covering all agenda files.
  1945. This file is created with the command \\[org-export-icalendar-all-agenda-files].
  1946. The file name should be absolute."
  1947. :group 'org-export-icalendar
  1948. :type 'file)
  1949. (defcustom org-icalendar-include-todo nil
  1950. "Non-nil means, export to iCalendar files should also cover TODO items."
  1951. :group 'org-export-icalendar
  1952. :type 'boolean)
  1953. (defcustom org-icalendar-combined-name "OrgMode"
  1954. "Calendar name for the combined iCalendar representing all agenda files."
  1955. :group 'org-export-icalendar
  1956. :type 'string)
  1957. (defgroup org-font-lock nil
  1958. "Font-lock settings for highlighting in Org-mode."
  1959. :tag "Org Font Lock"
  1960. :group 'org)
  1961. (defcustom org-level-color-stars-only nil
  1962. "Non-nil means fontify only the stars in each headline.
  1963. When nil, the entire headline is fontified.
  1964. Changing it requires restart of `font-lock-mode' to become effective
  1965. also in regions already fontified."
  1966. :group 'org-font-lock
  1967. :type 'boolean)
  1968. (defcustom org-hide-leading-stars nil
  1969. "Non-nil means, hide the first N-1 stars in a headline.
  1970. This works by using the face `org-hide' for these stars. This
  1971. face is white for a light background, and black for a dark
  1972. background. You may have to customize the face `org-hide' to
  1973. make this work.
  1974. Changing it requires restart of `font-lock-mode' to become effective
  1975. also in regions already fontified.
  1976. You may also set this on a per-file basis by adding one of the following
  1977. lines to the buffer:
  1978. #+STARTUP: hidestars
  1979. #+STARTUP: showstars"
  1980. :group 'org-font-lock
  1981. :type 'boolean)
  1982. (defcustom org-fontify-done-headline nil
  1983. "Non-nil means, change the face of a headline if it is marked DONE.
  1984. Normally, only the TODO/DONE keyword indicates the state of a headline.
  1985. When this is non-nil, the headline after the keyword is set to the
  1986. `org-headline-done' as an additional indication."
  1987. :group 'org-font-lock
  1988. :type 'boolean)
  1989. (defcustom org-fontify-emphasized-text t
  1990. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  1991. Changing this variable requires a restart of Emacs to take effect."
  1992. :group 'org-font-lock
  1993. :type 'boolean)
  1994. (defvar org-emph-re nil
  1995. "Regular expression for matching emphasis.")
  1996. (defvar org-emphasis-regexp-components) ; defined just below
  1997. (defvar org-emphasis-alist) ; defined just below
  1998. (defun org-set-emph-re (var val)
  1999. "Set variable and compute the emphasis regular expression."
  2000. (set var val)
  2001. (when (and (boundp 'org-emphasis-alist)
  2002. (boundp 'org-emphasis-regexp-components)
  2003. org-emphasis-alist org-emphasis-regexp-components)
  2004. (let* ((e org-emphasis-regexp-components)
  2005. (pre (car e))
  2006. (post (nth 1 e))
  2007. (border (nth 2 e))
  2008. (body (nth 3 e))
  2009. (nl (nth 4 e))
  2010. (stacked (nth 5 e))
  2011. (body1 (concat body "*?"))
  2012. (markers (mapconcat 'car org-emphasis-alist "")))
  2013. ;; make sure special characters appear at the right position in the class
  2014. (if (string-match "\\^" markers)
  2015. (setq markers (concat (replace-match "" t t markers) "^")))
  2016. (if (string-match "-" markers)
  2017. (setq markers (concat (replace-match "" t t markers) "-")))
  2018. (while (>= (setq nl (1- nl)) 0) (setq body1 (concat body1 "\n?" body "*?")))
  2019. ;; Make the regexp
  2020. (setq org-emph-re
  2021. (concat "\\([" pre (if stacked markers) "]\\|^\\)"
  2022. "\\("
  2023. "\\([" markers "]\\)"
  2024. "\\("
  2025. "[^" border markers "]"
  2026. body1
  2027. "[^" border markers "]"
  2028. "\\)"
  2029. "\\3\\)"
  2030. "\\([" post (if stacked markers) "]\\|$\\)")))))
  2031. (defcustom org-emphasis-regexp-components
  2032. '(" \t(" " \t.,?;:'\")" " \t\r\n,." "." 1 nil)
  2033. "Components used to build the reqular expression for emphasis.
  2034. This is a list with 6 entries. Terminology: In an emphasis string
  2035. like \" *strong word* \", we call the initial space PREMATCH, the final
  2036. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2037. and \"trong wor\" is the body. The different components in this variable
  2038. specify what is allowed/forbidden in each part:
  2039. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2040. post Chars allowed as postmatch. End of line will be allowed too.
  2041. border The chars *forbidden* as border characters. In addition to the
  2042. characters given here, all marker characters are forbidden too.
  2043. body-regexp A regexp like \".\" to match a body character. Don't use
  2044. non-shy groups here, and don't allow newline here.
  2045. newline The maximum number of newlines allowed in an emphasis exp.
  2046. stacked Non-nil means, allow stacked styles. This works only in HTML
  2047. export. When this is set, all marker characters (as given in
  2048. `org-emphasis-alist') will be allowed as pre/post, aiding
  2049. inside-out matching.
  2050. Use customize to modify this, or restart emacs after changing it."
  2051. :group 'org-font-lock
  2052. :set 'org-set-emph-re
  2053. :type '(list
  2054. (sexp :tag "Allowed chars in pre ")
  2055. (sexp :tag "Allowed chars in post ")
  2056. (sexp :tag "Forbidden chars in border ")
  2057. (sexp :tag "Regexp for body ")
  2058. (integer :tag "number of newlines allowed")
  2059. (boolean :tag "Stacking allowed ")))
  2060. (defcustom org-emphasis-alist
  2061. '(("*" bold "<b>" "</b>")
  2062. ("/" italic "<i>" "</i>")
  2063. ("_" underline "<u>" "</u>")
  2064. ("=" shadow "<code>" "</code>")
  2065. ("+" (:strike-through t) "<del>" "</del>")
  2066. )
  2067. "Special syntax for emphasised text.
  2068. Text starting and ending with a special character will be emphasized, for
  2069. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2070. characters, the face to bbe used by font-lock for highlighting in Org-mode
  2071. emacs buffers, and the HTML tags to be used for this.
  2072. Use customize to modify this, or restart emacs after changing it."
  2073. :group 'org-font-lock
  2074. :set 'org-set-emph-re
  2075. :type '(repeat
  2076. (list
  2077. (string :tag "Marker character")
  2078. (choice
  2079. (face :tag "Font-lock-face")
  2080. (plist :tag "Face property list"))
  2081. (string :tag "HTML start tag")
  2082. (string :tag "HTML end tag"))))
  2083. (defgroup org-faces nil
  2084. "Faces in Org-mode."
  2085. :tag "Org Faces"
  2086. :group 'org-font-lock)
  2087. (defun org-compatible-face (specs)
  2088. "Make a compatible face specification.
  2089. XEmacs and Emacs 21 do not know about the `min-colors' attribute.
  2090. For them we convert a (min-colors 8) entry to a `tty' entry and move it
  2091. to the top of the list. The `min-colors' attribute will be removed from
  2092. any other entries, and any resulting duplicates will be removed entirely."
  2093. (if (or (featurep 'xemacs) (< emacs-major-version 22))
  2094. (let (r e a)
  2095. (while (setq e (pop specs))
  2096. (cond
  2097. ((memq (car e) '(t default)) (push e r))
  2098. ((setq a (member '(min-colors 8) (car e)))
  2099. (nconc r (list (cons (cons '(type tty) (delq (car a) (car e)))
  2100. (cdr e)))))
  2101. ((setq a (assq 'min-colors (car e)))
  2102. (setq e (cons (delq a (car e)) (cdr e)))
  2103. (or (assoc (car e) r) (push e r)))
  2104. (t (or (assoc (car e) r) (push e r)))))
  2105. (nreverse r))
  2106. specs))
  2107. (defface org-hide
  2108. '((((background light)) (:foreground "white"))
  2109. (((background dark)) (:foreground "black")))
  2110. "Face used to hide leading stars in headlines.
  2111. The forground color of this face should be equal to the background
  2112. color of the frame."
  2113. :group 'org-faces)
  2114. (defface org-level-1 ;; font-lock-function-name-face
  2115. (org-compatible-face
  2116. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2117. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2118. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2119. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2120. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  2121. (t (:bold t))))
  2122. "Face used for level 1 headlines."
  2123. :group 'org-faces)
  2124. (defface org-level-2 ;; font-lock-variable-name-face
  2125. (org-compatible-face
  2126. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  2127. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  2128. (((class color) (min-colors 8) (background light)) (:foreground "yellow"))
  2129. (((class color) (min-colors 8) (background dark)) (:foreground "yellow" :bold t))
  2130. (t (:bold t))))
  2131. "Face used for level 2 headlines."
  2132. :group 'org-faces)
  2133. (defface org-level-3 ;; font-lock-keyword-face
  2134. (org-compatible-face
  2135. '((((class color) (min-colors 88) (background light)) (:foreground "Purple"))
  2136. (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
  2137. (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
  2138. (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
  2139. (((class color) (min-colors 8) (background light)) (:foreground "purple" :bold t))
  2140. (((class color) (min-colors 8) (background dark)) (:foreground "cyan" :bold t))
  2141. (t (:bold t))))
  2142. "Face used for level 3 headlines."
  2143. :group 'org-faces)
  2144. (defface org-level-4 ;; font-lock-comment-face
  2145. (org-compatible-face
  2146. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2147. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2148. (((class color) (min-colors 16) (background light)) (:foreground "red"))
  2149. (((class color) (min-colors 16) (background dark)) (:foreground "red1"))
  2150. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2151. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2152. (t (:bold t))))
  2153. "Face used for level 4 headlines."
  2154. :group 'org-faces)
  2155. (defface org-level-5 ;; font-lock-type-face
  2156. (org-compatible-face
  2157. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
  2158. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
  2159. (((class color) (min-colors 8)) (:foreground "green"))))
  2160. "Face used for level 5 headlines."
  2161. :group 'org-faces)
  2162. (defface org-level-6 ;; font-lock-constant-face
  2163. (org-compatible-face
  2164. '((((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
  2165. (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
  2166. (((class color) (min-colors 8)) (:foreground "magenta"))))
  2167. "Face used for level 6 headlines."
  2168. :group 'org-faces)
  2169. (defface org-level-7 ;; font-lock-builtin-face
  2170. (org-compatible-face
  2171. '((((class color) (min-colors 16) (background light)) (:foreground "Orchid"))
  2172. (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue"))
  2173. (((class color) (min-colors 8)) (:foreground "blue"))))
  2174. "Face used for level 7 headlines."
  2175. :group 'org-faces)
  2176. (defface org-level-8 ;; font-lock-string-face
  2177. (org-compatible-face
  2178. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2179. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2180. (((class color) (min-colors 8)) (:foreground "green"))))
  2181. "Face used for level 8 headlines."
  2182. :group 'org-faces)
  2183. (defface org-special-keyword ;; font-lock-string-face
  2184. (org-compatible-face
  2185. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2186. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2187. (t (:italic t))))
  2188. "Face used for special keywords."
  2189. :group 'org-faces)
  2190. (defface org-warning ;; font-lock-warning-face
  2191. (org-compatible-face
  2192. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  2193. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  2194. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2195. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2196. (t (:bold t))))
  2197. "Face for deadlines and TODO keywords."
  2198. :group 'org-faces)
  2199. (defface org-headline-done ;; font-lock-string-face
  2200. (org-compatible-face
  2201. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2202. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2203. (((class color) (min-colors 8) (background light)) (:bold nil))))
  2204. "Face used to indicate that a headline is DONE.
  2205. This face is only used if `org-fontify-done-headline' is set."
  2206. :group 'org-faces)
  2207. (defface org-archived ; similar to shadow
  2208. (org-compatible-face
  2209. '((((class color grayscale) (min-colors 88) (background light))
  2210. (:foreground "grey50"))
  2211. (((class color grayscale) (min-colors 88) (background dark))
  2212. (:foreground "grey70"))
  2213. (((class color) (min-colors 8) (background light))
  2214. (:foreground "green"))
  2215. (((class color) (min-colors 8) (background dark))
  2216. (:foreground "yellow"))))
  2217. "Face for headline with the ARCHIVE tag."
  2218. :group 'org-faces)
  2219. (defface org-link
  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-date
  2226. '((((class color) (background light)) (:foreground "Purple" :underline t))
  2227. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  2228. (t (:underline t)))
  2229. "Face for links."
  2230. :group 'org-faces)
  2231. (defface org-tag
  2232. '((t (:bold t)))
  2233. "Face for tags."
  2234. :group 'org-faces)
  2235. (defface org-todo ;; font-lock-warning-face
  2236. (org-compatible-face
  2237. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  2238. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  2239. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2240. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2241. (t (:inverse-video t :bold t))))
  2242. "Face for TODO keywords."
  2243. :group 'org-faces)
  2244. (defface org-done ;; font-lock-type-face
  2245. (org-compatible-face
  2246. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
  2247. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
  2248. (((class color) (min-colors 8)) (:foreground "green"))
  2249. (t (:bold t))))
  2250. "Face used for DONE."
  2251. :group 'org-faces)
  2252. (defface org-table ;; font-lock-function-name-face
  2253. (org-compatible-face
  2254. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2255. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2256. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2257. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2258. (((class color) (min-colors 8) (background light)) (:foreground "blue"))
  2259. (((class color) (min-colors 8) (background dark)))))
  2260. "Face used for tables."
  2261. :group 'org-faces)
  2262. (defface org-formula
  2263. (org-compatible-face
  2264. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2265. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2266. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2267. (((class color) (min-colors 8) (background dark)) (:foreground "red"))
  2268. (t (:bold t :italic t))))
  2269. "Face for formulas."
  2270. :group 'org-faces)
  2271. (defface org-scheduled-today
  2272. (org-compatible-face
  2273. '((((class color) (min-colors 88) (background light)) (:foreground "DarkGreen"))
  2274. (((class color) (min-colors 88) (background dark)) (:foreground "PaleGreen"))
  2275. (((class color) (min-colors 8)) (:foreground "green"))
  2276. (t (:bold t :italic t))))
  2277. "Face for items scheduled for a certain day."
  2278. :group 'org-faces)
  2279. (defface org-scheduled-previously
  2280. (org-compatible-face
  2281. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2282. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2283. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2284. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2285. (t (:bold t))))
  2286. "Face for items scheduled previously, and not yet done."
  2287. :group 'org-faces)
  2288. (defface org-upcoming-deadline
  2289. (org-compatible-face
  2290. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2291. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2292. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2293. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2294. (t (:bold t))))
  2295. "Face for items scheduled previously, and not yet done."
  2296. :group 'org-faces)
  2297. (defface org-time-grid ;; font-lock-variable-name-face
  2298. (org-compatible-face
  2299. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  2300. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  2301. (((class color) (min-colors 8)) (:foreground "yellow" :weight light))))
  2302. "Face used for time grids."
  2303. :group 'org-faces)
  2304. (defconst org-level-faces
  2305. '(org-level-1 org-level-2 org-level-3 org-level-4
  2306. org-level-5 org-level-6 org-level-7 org-level-8
  2307. ))
  2308. (defconst org-n-levels (length org-level-faces))
  2309. ;; Variables for pre-computed regular expressions, all buffer local
  2310. (defvar org-done-string nil
  2311. "The last string in `org-todo-keywords', indicating an item is DONE.")
  2312. (make-variable-buffer-local 'org-done-string)
  2313. (defvar org-todo-regexp nil
  2314. "Matches any of the TODO state keywords.")
  2315. (make-variable-buffer-local 'org-todo-regexp)
  2316. (defvar org-not-done-regexp nil
  2317. "Matches any of the TODO state keywords except the last one.")
  2318. (make-variable-buffer-local 'org-not-done-regexp)
  2319. (defvar org-todo-line-regexp nil
  2320. "Matches a headline and puts TODO state into group 2 if present.")
  2321. (make-variable-buffer-local 'org-todo-line-regexp)
  2322. (defvar org-todo-line-tags-regexp nil
  2323. "Matches a headline and puts TODO state into group 2 if present.
  2324. Also put tags into group 4 if tags are present.")
  2325. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2326. (defvar org-nl-done-regexp nil
  2327. "Matches newline followed by a headline with the DONE keyword.")
  2328. (make-variable-buffer-local 'org-nl-done-regexp)
  2329. (defvar org-looking-at-done-regexp nil
  2330. "Matches the DONE keyword a point.")
  2331. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2332. (defvar org-todo-kwd-priority-p nil
  2333. "Do TODO items have priorities?")
  2334. (make-variable-buffer-local 'org-todo-kwd-priority-p)
  2335. (defvar org-todo-kwd-max-priority nil
  2336. "Maximum priority of TODO items.")
  2337. (make-variable-buffer-local 'org-todo-kwd-max-priority)
  2338. (defvar org-ds-keyword-length 12
  2339. "Maximum length of the Deadline and SCHEDULED keywords.")
  2340. (make-variable-buffer-local 'org-ds-keyword-length)
  2341. (defvar org-deadline-regexp nil
  2342. "Matches the DEADLINE keyword.")
  2343. (make-variable-buffer-local 'org-deadline-regexp)
  2344. (defvar org-deadline-time-regexp nil
  2345. "Matches the DEADLINE keyword together with a time stamp.")
  2346. (make-variable-buffer-local 'org-deadline-time-regexp)
  2347. (defvar org-deadline-line-regexp nil
  2348. "Matches the DEADLINE keyword and the rest of the line.")
  2349. (make-variable-buffer-local 'org-deadline-line-regexp)
  2350. (defvar org-scheduled-regexp nil
  2351. "Matches the SCHEDULED keyword.")
  2352. (make-variable-buffer-local 'org-scheduled-regexp)
  2353. (defvar org-scheduled-time-regexp nil
  2354. "Matches the SCHEDULED keyword together with a time stamp.")
  2355. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2356. (defvar org-closed-time-regexp nil
  2357. "Matches the CLOSED keyword together with a time stamp.")
  2358. (make-variable-buffer-local 'org-closed-time-regexp)
  2359. (defvar org-keyword-time-regexp nil
  2360. "Matches any of the 3 keywords, together with the time stamp.")
  2361. (make-variable-buffer-local 'org-keyword-time-regexp)
  2362. (defvar org-maybe-keyword-time-regexp nil
  2363. "Matches a timestamp, possibly preceeded by a keyword.")
  2364. (make-variable-buffer-local 'org-keyword-time-regexp)
  2365. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  2366. mouse-map t)
  2367. "Properties to remove when a string without properties is wanted.")
  2368. (defsubst org-match-string-no-properties (num &optional string)
  2369. (if (featurep 'xemacs)
  2370. (let ((s (match-string num string)))
  2371. (remove-text-properties 0 (length s) org-rm-props s)
  2372. s)
  2373. (match-string-no-properties num string)))
  2374. (defsubst org-no-properties (s)
  2375. (remove-text-properties 0 (length s) org-rm-props s)
  2376. s)
  2377. (defsubst org-set-local (var value)
  2378. "Make VAR local in current buffer and set it to VALUE."
  2379. (set (make-variable-buffer-local var) value))
  2380. (defsubst org-mode-p ()
  2381. "Check if the current buffer is in Org-mode."
  2382. (eq major-mode 'org-mode))
  2383. (defun org-set-regexps-and-options ()
  2384. "Precompute regular expressions for current buffer."
  2385. (when (org-mode-p)
  2386. (let ((re (org-make-options-regexp
  2387. '("CATEGORY" "SEQ_TODO" "PRI_TODO" "TYP_TODO"
  2388. "STARTUP" "ARCHIVE" "TAGS" "CALC")))
  2389. (splitre "[ \t]+")
  2390. kwds int key value cat arch tags)
  2391. (save-excursion
  2392. (save-restriction
  2393. (widen)
  2394. (goto-char (point-min))
  2395. (while (re-search-forward re nil t)
  2396. (setq key (match-string 1) value (org-match-string-no-properties 2))
  2397. (cond
  2398. ((equal key "CATEGORY")
  2399. (if (string-match "[ \t]+$" value)
  2400. (setq value (replace-match "" t t value)))
  2401. (setq cat (intern value)))
  2402. ((equal key "SEQ_TODO")
  2403. (setq int 'sequence
  2404. kwds (append kwds (org-split-string value splitre))))
  2405. ((equal key "PRI_TODO")
  2406. (setq int 'priority
  2407. kwds (append kwds (org-split-string value splitre))))
  2408. ((equal key "TYP_TODO")
  2409. (setq int 'type
  2410. kwds (append kwds (org-split-string value splitre))))
  2411. ((equal key "TAGS")
  2412. (setq tags (append tags (org-split-string value splitre))))
  2413. ((equal key "STARTUP")
  2414. (let ((opts (org-split-string value splitre))
  2415. (set '(("fold" org-startup-folded t)
  2416. ("overview" org-startup-folded t)
  2417. ("nofold" org-startup-folded nil)
  2418. ("showall" org-startup-folded nil)
  2419. ("content" org-startup-folded content)
  2420. ("hidestars" org-hide-leading-stars t)
  2421. ("showstars" org-hide-leading-stars nil)
  2422. ("odd" org-odd-levels-only t)
  2423. ("oddeven" org-odd-levels-only nil)
  2424. ("align" org-startup-align-all-tables t)
  2425. ("noalign" org-startup-align-all-tables nil)
  2426. ("logging" org-log-done t)
  2427. ("nologging" org-log-done nil)
  2428. ("dlcheck" org-startup-with-deadline-check t)
  2429. ("nodlcheck" org-startup-with-deadline-check nil)))
  2430. l var val)
  2431. (while (setq l (assoc (pop opts) set))
  2432. (setq var (nth 1 l) val (nth 2 l))
  2433. (set (make-local-variable var) val))))
  2434. ((equal key "ARCHIVE")
  2435. (string-match " *$" value)
  2436. (setq arch (replace-match "" t t value))
  2437. (remove-text-properties 0 (length arch)
  2438. '(face t fontified t) arch)))
  2439. )))
  2440. (and cat (org-set-local 'org-category cat))
  2441. (and kwds (org-set-local 'org-todo-keywords kwds))
  2442. (and arch (org-set-local 'org-archive-location arch))
  2443. (and int (org-set-local 'org-todo-interpretation int))
  2444. (when tags
  2445. (let (e tgs)
  2446. (while (setq e (pop tags))
  2447. (cond
  2448. ((equal e "{") (push '(:startgroup) tgs))
  2449. ((equal e "}") (push '(:endgroup) tgs))
  2450. ((string-match "^\\([0-9a-zA-Z_@]+\\)(\\(.\\))$" e)
  2451. (push (cons (match-string 1 e)
  2452. (string-to-char (match-string 2 e)))
  2453. tgs))
  2454. (t (push (list e) tgs))))
  2455. (org-set-local 'org-tag-alist nil)
  2456. (while (setq e (pop tgs))
  2457. (or (and (stringp (car e))
  2458. (assoc (car e) org-tag-alist))
  2459. (push e org-tag-alist))))))
  2460. ;; Compute the regular expressions and other local variables
  2461. (setq org-todo-kwd-priority-p (equal org-todo-interpretation 'priority)
  2462. org-todo-kwd-max-priority (1- (length org-todo-keywords))
  2463. org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2464. (length org-scheduled-string)))
  2465. org-done-string
  2466. (nth (1- (length org-todo-keywords)) org-todo-keywords)
  2467. org-todo-regexp
  2468. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords
  2469. "\\|") "\\)\\>")
  2470. org-not-done-regexp
  2471. (concat "\\<\\("
  2472. (mapconcat 'regexp-quote
  2473. (nreverse (cdr (reverse org-todo-keywords)))
  2474. "\\|")
  2475. "\\)\\>")
  2476. org-todo-line-regexp
  2477. (concat "^\\(\\*+\\)[ \t]*\\("
  2478. (mapconcat 'regexp-quote org-todo-keywords "\\|")
  2479. "\\)? *\\(.*\\)")
  2480. org-nl-done-regexp
  2481. (concat "[\r\n]\\*+[ \t]+" org-done-string "\\>")
  2482. org-todo-line-tags-regexp
  2483. (concat "^\\(\\*+\\)[ \t]*\\("
  2484. (mapconcat 'regexp-quote org-todo-keywords "\\|")
  2485. "\\)? *\\(.*?\\([ \t]:[a-zA-Z0-9:_@]+:[ \t]*\\)?$\\)")
  2486. org-looking-at-done-regexp (concat "^" org-done-string "\\>")
  2487. org-deadline-regexp (concat "\\<" org-deadline-string)
  2488. org-deadline-time-regexp
  2489. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2490. org-deadline-line-regexp
  2491. (concat "\\<\\(" org-deadline-string "\\).*")
  2492. org-scheduled-regexp
  2493. (concat "\\<" org-scheduled-string)
  2494. org-scheduled-time-regexp
  2495. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2496. org-closed-time-regexp
  2497. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2498. org-keyword-time-regexp
  2499. (concat "\\<\\(" org-scheduled-string
  2500. "\\|" org-deadline-string
  2501. "\\|" org-closed-string
  2502. "\\|" org-clock-string "\\)"
  2503. " *[[<]\\([^]>]+\\)[]>]")
  2504. org-maybe-keyword-time-regexp
  2505. (concat "\\(\\<\\(" org-scheduled-string
  2506. "\\|" org-deadline-string
  2507. "\\|" org-closed-string
  2508. "\\|" org-clock-string "\\)\\)?"
  2509. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}[^]\r\n>]*?[]>]\\)"))
  2510. (org-set-font-lock-defaults)))
  2511. ;; Tell the compiler about dynamically scoped variables,
  2512. ;; and variables from other packages
  2513. (defvar calc-embedded-close-formula) ; defined by the calc package
  2514. (defvar calc-embedded-open-formula) ; defined by the calc package
  2515. (defvar font-lock-unfontify-region-function) ; defined by font-lock.el
  2516. (defvar zmacs-regions) ; XEmacs regions
  2517. (defvar original-date) ; dynamically scoped in calendar
  2518. (defvar org-old-auto-fill-inhibit-regexp) ; local variable used by `orgtbl-mode'
  2519. (defvar orgtbl-mode-menu) ; defined when orgtbl mode get initialized
  2520. (defvar org-html-entities) ; defined later in this file
  2521. (defvar org-goto-start-pos) ; dynamically scoped parameter
  2522. (defvar org-time-was-given) ; dynamically scoped parameter
  2523. (defvar org-ts-what) ; dynamically scoped parameter
  2524. (defvar org-current-export-file) ; dynamically scoped parameter
  2525. (defvar org-current-export-dir) ; dynamically scoped parameter
  2526. (defvar mark-active) ; Emacs only, not available in XEmacs.
  2527. (defvar timecnt) ; dynamically scoped parameter
  2528. (defvar levels-open) ; dynamically scoped parameter
  2529. (defvar entry) ; dynamically scoped parameter
  2530. (defvar state) ; dynamically scoped into `org-after-todo-state-change-hook'
  2531. (defvar date) ; dynamically scoped parameter
  2532. (defvar description) ; dynamically scoped parameter
  2533. (defvar ans1) ; dynamically scoped parameter
  2534. (defvar ans2) ; dynamically scoped parameter
  2535. (defvar starting-day) ; local variable
  2536. (defvar include-all-loc) ; local variable
  2537. (defvar vm-message-pointer) ; from vm
  2538. (defvar vm-folder-directory) ; from vm
  2539. (defvar wl-summary-buffer-elmo-folder) ; from wanderlust
  2540. (defvar wl-summary-buffer-folder-name) ; from wanderlust
  2541. (defvar gnus-group-name) ; from gnus
  2542. (defvar gnus-article-current) ; from gnus
  2543. (defvar w3m-current-url) ; from w3m
  2544. (defvar w3m-current-title) ; from w3m
  2545. (defvar mh-progs) ; from MH-E
  2546. (defvar mh-current-folder) ; from MH-E
  2547. (defvar mh-show-folder-buffer) ; from MH-E
  2548. (defvar mh-index-folder) ; from MH-E
  2549. (defvar mh-searcher) ; from MH-E
  2550. (defvar org-selected-point) ; dynamically scoped parameter
  2551. (defvar calendar-mode-map) ; from calendar.el
  2552. (defvar last-arg) ; local variable
  2553. (defvar remember-save-after-remembering) ; from remember.el
  2554. (defvar remember-data-file) ; from remember.el
  2555. (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
  2556. (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
  2557. (defvar orgtbl-mode) ; defined later in this file
  2558. (defvar Info-current-file) ; from info.el
  2559. (defvar Info-current-node) ; from info.el
  2560. (defvar texmathp-why) ; from texmathp.el
  2561. (defvar org-latex-regexps)
  2562. ;;; Define the mode
  2563. (defvar org-mode-map
  2564. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  2565. (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.")
  2566. (copy-keymap outline-mode-map))
  2567. "Keymap for Org-mode.")
  2568. (defvar org-struct-menu) ; defined later in this file
  2569. (defvar org-org-menu) ; defined later in this file
  2570. (defvar org-tbl-menu) ; defined later in this file
  2571. ;; We use a before-change function to check if a table might need
  2572. ;; an update.
  2573. (defvar org-table-may-need-update t
  2574. "Indicates that a table might need an update.
  2575. This variable is set by `org-before-change-function'.
  2576. `org-table-align' sets it back to nil.")
  2577. (defvar org-mode-hook nil)
  2578. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  2579. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  2580. ;;;###autoload
  2581. (define-derived-mode org-mode outline-mode "Org"
  2582. "Outline-based notes management and organizer, alias
  2583. \"Carsten's outline-mode for keeping track of everything.\"
  2584. Org-mode develops organizational tasks around a NOTES file which
  2585. contains information about projects as plain text. Org-mode is
  2586. implemented on top of outline-mode, which is ideal to keep the content
  2587. of large files well structured. It supports ToDo items, deadlines and
  2588. time stamps, which magically appear in the diary listing of the Emacs
  2589. calendar. Tables are easily created with a built-in table editor.
  2590. Plain text URL-like links connect to websites, emails (VM), Usenet
  2591. messages (Gnus), BBDB entries, and any files related to the project.
  2592. For printing and sharing of notes, an Org-mode file (or a part of it)
  2593. can be exported as a structured ASCII or HTML file.
  2594. The following commands are available:
  2595. \\{org-mode-map}"
  2596. ;; Get rid of Outline menus, they are not needed
  2597. ;; Need to do this here because define-derived-mode sets up
  2598. ;; the keymap so late.
  2599. (if (featurep 'xemacs)
  2600. (progn
  2601. ;; Assume this is Greg's port, it used easymenu
  2602. (easy-menu-remove outline-mode-menu-heading)
  2603. (easy-menu-remove outline-mode-menu-show)
  2604. (easy-menu-remove outline-mode-menu-hide))
  2605. (define-key org-mode-map [menu-bar headings] 'undefined)
  2606. (define-key org-mode-map [menu-bar hide] 'undefined)
  2607. (define-key org-mode-map [menu-bar show] 'undefined))
  2608. (easy-menu-add org-org-menu)
  2609. (easy-menu-add org-tbl-menu)
  2610. (org-install-agenda-files-menu)
  2611. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  2612. (org-add-to-invisibility-spec '(org-cwidth))
  2613. (when (featurep 'xemacs)
  2614. (org-set-local 'line-move-ignore-invisible t))
  2615. (setq outline-regexp "\\*+")
  2616. ;;(setq outline-regexp "\\(?:\\*+\\|[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\) \\)")
  2617. (setq outline-level 'org-outline-level)
  2618. (when (and org-ellipsis (stringp org-ellipsis))
  2619. (unless org-display-table
  2620. (setq org-display-table (make-display-table)))
  2621. (set-display-table-slot org-display-table
  2622. 4 (string-to-vector org-ellipsis))
  2623. (setq buffer-display-table org-display-table))
  2624. (org-set-regexps-and-options)
  2625. ;; Calc embedded
  2626. (org-set-local 'calc-embedded-open-mode "# ")
  2627. (modify-syntax-entry ?# "<")
  2628. (if org-startup-truncated (setq truncate-lines t))
  2629. (org-set-local 'font-lock-unfontify-region-function
  2630. 'org-unfontify-region)
  2631. ;; Activate before-change-function
  2632. (org-set-local 'org-table-may-need-update t)
  2633. (org-add-hook 'before-change-functions 'org-before-change-function nil
  2634. 'local)
  2635. ;; Check for running clock before killing a buffer
  2636. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  2637. ;; Paragraphs and auto-filling
  2638. (org-set-autofill-regexps)
  2639. (org-update-radio-target-regexp)
  2640. (if (and org-insert-mode-line-in-empty-file
  2641. (interactive-p)
  2642. (= (point-min) (point-max)))
  2643. (insert " -*- mode: org -*-\n\n"))
  2644. (unless org-inhibit-startup
  2645. (when org-startup-align-all-tables
  2646. (let ((bmp (buffer-modified-p)))
  2647. (org-table-map-tables 'org-table-align)
  2648. (set-buffer-modified-p bmp)))
  2649. (if org-startup-with-deadline-check
  2650. (call-interactively 'org-check-deadlines)
  2651. (cond
  2652. ((eq org-startup-folded t)
  2653. (org-cycle '(4)))
  2654. ((eq org-startup-folded 'content)
  2655. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  2656. (org-cycle '(4)) (org-cycle '(4))))))))
  2657. (defsubst org-call-with-arg (command arg)
  2658. "Call COMMAND interactively, but pretend prefix are was ARG."
  2659. (let ((current-prefix-arg arg)) (call-interactively command)))
  2660. (defsubst org-current-line (&optional pos)
  2661. (+ (if (bolp) 1 0) (count-lines (point-min) (or pos (point)))))
  2662. (defun org-current-time ()
  2663. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  2664. (if (> org-time-stamp-rounding-minutes 0)
  2665. (let ((r org-time-stamp-rounding-minutes)
  2666. (time (decode-time)))
  2667. (apply 'encode-time
  2668. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  2669. (nthcdr 2 time))))
  2670. (current-time)))
  2671. (defun org-add-props (string plist &rest props)
  2672. "Add text properties to entire string, from beginning to end.
  2673. PLIST may be a list of properties, PROPS are individual properties and values
  2674. that will be added to PLIST. Returns the string that was modified."
  2675. (add-text-properties
  2676. 0 (length string) (if props (append plist props) plist) string)
  2677. string)
  2678. (put 'org-add-props 'lisp-indent-function 2)
  2679. ;;; Font-Lock stuff
  2680. (defvar org-mouse-map (make-sparse-keymap))
  2681. (define-key org-mouse-map
  2682. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  2683. (define-key org-mouse-map
  2684. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  2685. (when org-mouse-1-follows-link
  2686. (define-key org-mouse-map [follow-link] 'mouse-face))
  2687. (when org-tab-follows-link
  2688. (define-key org-mouse-map [(tab)] 'org-open-at-point)
  2689. (define-key org-mouse-map "\C-i" 'org-open-at-point))
  2690. (when org-return-follows-link
  2691. (define-key org-mouse-map [(return)] 'org-open-at-point)
  2692. (define-key org-mouse-map "\C-m" 'org-open-at-point))
  2693. (require 'font-lock)
  2694. (defconst org-non-link-chars "]\t\n\r<>")
  2695. (defconst org-link-types '("https?" "ftp" "mailto" "file" "news" "bbdb" "vm"
  2696. "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  2697. (defconst org-link-re-with-space
  2698. (concat
  2699. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2700. "\\([^" org-non-link-chars " ]"
  2701. "[^" org-non-link-chars "]*"
  2702. "[^" org-non-link-chars " ]\\)>?")
  2703. "Matches a link with spaces, optional angular brackets around it.")
  2704. (defconst org-link-re-with-space2
  2705. (concat
  2706. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2707. "\\([^" org-non-link-chars " ]"
  2708. "[^]\t\n\r]*"
  2709. "[^" org-non-link-chars " ]\\)>?")
  2710. "Matches a link with spaces, optional angular brackets around it.")
  2711. (defconst org-angle-link-re
  2712. (concat
  2713. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2714. "\\([^" org-non-link-chars " ]"
  2715. "[^" org-non-link-chars "]*"
  2716. "\\)>")
  2717. "Matches link with angular brackets, spaces are allowed.")
  2718. (defconst org-plain-link-re
  2719. (concat
  2720. "\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2721. "\\([^]\t\n\r<>,;() ]+\\)")
  2722. "Matches plain link, without spaces.")
  2723. (defconst org-bracket-link-regexp
  2724. "\\[\\[\\([^]]+\\)\\]\\(\\[\\([^]]+\\)\\]\\)?\\]"
  2725. "Matches a link in double brackets.")
  2726. (defconst org-bracket-link-analytic-regexp
  2727. (concat
  2728. "\\[\\["
  2729. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  2730. "\\([^]]+\\)"
  2731. "\\]"
  2732. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  2733. "\\]"))
  2734. ; 1: http:
  2735. ; 2: http
  2736. ; 3: path
  2737. ; 4: [desc]
  2738. ; 5: desc
  2739. (defconst org-ts-lengths
  2740. (cons (length (format-time-string (car org-time-stamp-formats)))
  2741. (length (format-time-string (cdr org-time-stamp-formats))))
  2742. "This holds the lengths of the two different time formats.")
  2743. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}[^\r\n>]*?\\)>"
  2744. "Regular expression for fast time stamp matching.")
  2745. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}[^\r\n>]*?\\)[]>]"
  2746. "Regular expression for fast time stamp matching.")
  2747. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  2748. "Regular expression matching time strings for analysis.")
  2749. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 ">")
  2750. "Regular expression matching time stamps, with groups.")
  2751. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[]>]")
  2752. "Regular expression matching time stamps (also [..]), with groups.")
  2753. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  2754. "Regular expression matching a time stamp range.")
  2755. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  2756. org-ts-regexp "\\)?")
  2757. "Regular expression matching a time stamp or time stamp range.")
  2758. (defvar org-�§emph-face nil)
  2759. (defun org-do-emphasis-faces (limit)
  2760. "Run through the buffer and add overlays to links."
  2761. (if (re-search-forward org-emph-re limit t)
  2762. (progn
  2763. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  2764. 'face
  2765. (nth 1 (assoc (match-string 3)
  2766. org-emphasis-alist)))
  2767. (add-text-properties (match-beginning 2) (match-end 2)
  2768. '(font-lock-multiline t))
  2769. (backward-char 1)
  2770. t)))
  2771. (defun org-activate-plain-links (limit)
  2772. "Run through the buffer and add overlays to links."
  2773. (if (re-search-forward org-plain-link-re limit t)
  2774. (progn
  2775. (add-text-properties (match-beginning 0) (match-end 0)
  2776. (list 'mouse-face 'highlight
  2777. 'keymap org-mouse-map
  2778. ))
  2779. t)))
  2780. (defun org-activate-angle-links (limit)
  2781. "Run through the buffer and add overlays to links."
  2782. (if (re-search-forward org-angle-link-re limit t)
  2783. (progn
  2784. (add-text-properties (match-beginning 0) (match-end 0)
  2785. (list 'mouse-face 'highlight
  2786. 'keymap org-mouse-map
  2787. ))
  2788. t)))
  2789. (defun org-activate-bracket-links (limit)
  2790. "Run through the buffer and add overlays to bracketed links."
  2791. (if (re-search-forward org-bracket-link-regexp limit t)
  2792. (let* ((help (concat "LINK: "
  2793. (org-match-string-no-properties 1)))
  2794. ;; FIXME: above we should remove the escapes.
  2795. ;; but that requires another match, protecting match data,
  2796. ;; a lot of overhead for font-lock.
  2797. (ip (list 'invisible 'org-link 'intangible t 'rear-nonsticky t
  2798. 'keymap org-mouse-map 'mouse-face 'highlight
  2799. 'help-echo help))
  2800. (vp (list 'rear-nonsticky t
  2801. 'keymap org-mouse-map 'mouse-face 'highlight
  2802. 'help-echo help)))
  2803. ;; We need to remove the invisible property here. Table narrowing
  2804. ;; may have made some of this invisible.
  2805. (remove-text-properties (match-beginning 0) (match-end 0)
  2806. '(invisible nil))
  2807. (if (match-end 3)
  2808. (progn
  2809. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  2810. (add-text-properties (match-beginning 3) (match-end 3) vp)
  2811. (add-text-properties (match-end 3) (match-end 0) ip))
  2812. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  2813. (add-text-properties (match-beginning 1) (match-end 1) vp)
  2814. (add-text-properties (match-end 1) (match-end 0) ip))
  2815. t)))
  2816. (defun org-activate-dates (limit)
  2817. "Run through the buffer and add overlays to dates."
  2818. (if (re-search-forward org-tsr-regexp limit t)
  2819. (progn
  2820. (add-text-properties (match-beginning 0) (match-end 0)
  2821. (list 'mouse-face 'highlight
  2822. 'keymap org-mouse-map))
  2823. t)))
  2824. (defvar org-target-link-regexp nil
  2825. "Regular expression matching radio targets in plain text.")
  2826. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  2827. "Regular expression matching a link target.")
  2828. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  2829. "Regular expression matching a link target.")
  2830. (defun org-activate-target-links (limit)
  2831. "Run through the buffer and add overlays to target matches."
  2832. (when org-target-link-regexp
  2833. (let ((case-fold-search t))
  2834. (if (re-search-forward org-target-link-regexp limit t)
  2835. (progn
  2836. (add-text-properties (match-beginning 0) (match-end 0)
  2837. (list 'mouse-face 'highlight
  2838. 'keymap org-mouse-map
  2839. 'help-echo "Radio target link"
  2840. 'org-linked-text t))
  2841. t)))))
  2842. (defun org-update-radio-target-regexp ()
  2843. "Find all radio targets in this file and update the regular expression."
  2844. (interactive)
  2845. (when (memq 'radio org-activate-links)
  2846. (setq org-target-link-regexp
  2847. (org-make-target-link-regexp (org-all-targets 'radio)))
  2848. (org-restart-font-lock)))
  2849. (defun org-hide-wide-columns (limit)
  2850. (let (s e)
  2851. (setq s (text-property-any (point) (or limit (point-max))
  2852. 'org-cwidth t))
  2853. (when s
  2854. (setq e (next-single-property-change s 'org-cwidth))
  2855. (add-text-properties s e '(invisible org-cwidth intangible t))
  2856. (goto-char e)
  2857. t)))
  2858. (defun org-restart-font-lock ()
  2859. "Restart font-lock-mode, to force refontification."
  2860. (when (and (boundp 'font-lock-mode) font-lock-mode)
  2861. (font-lock-mode -1)
  2862. (font-lock-mode 1)))
  2863. (defun org-all-targets (&optional radio)
  2864. "Return a list of all targets in this file.
  2865. With optional argument RADIO, only find radio targets."
  2866. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  2867. rtn)
  2868. (save-excursion
  2869. (goto-char (point-min))
  2870. (while (re-search-forward re nil t)
  2871. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  2872. rtn)))
  2873. (defun org-make-target-link-regexp (targets)
  2874. "Make regular expression matching all strings in TARGETS.
  2875. The regular expression finds the targets also if there is a line break
  2876. between words."
  2877. (and targets
  2878. (concat
  2879. "\\<\\("
  2880. (mapconcat
  2881. (lambda (x)
  2882. (while (string-match " +" x)
  2883. (setq x (replace-match "\\s-+" t t x)))
  2884. x)
  2885. targets
  2886. "\\|")
  2887. "\\)\\>")))
  2888. (defvar org-camel-regexp "\\*?\\<[A-Z]+[a-z]+[A-Z][a-zA-Z]*\\>"
  2889. "Matches CamelCase words, possibly with a star before it.")
  2890. (defun org-activate-camels (limit)
  2891. "Run through the buffer and add overlays to dates."
  2892. (if (re-search-forward org-camel-regexp limit t)
  2893. (progn
  2894. (add-text-properties (match-beginning 0) (match-end 0)
  2895. (list 'mouse-face 'highlight
  2896. 'keymap org-mouse-map))
  2897. t)))
  2898. (defun org-activate-tags (limit)
  2899. (if (re-search-forward "[ \t]\\(:[A-Za-z_@0-9:]+:\\)[ \r\n]" limit t)
  2900. (progn
  2901. (add-text-properties (match-beginning 1) (match-end 1)
  2902. (list 'mouse-face 'highlight
  2903. 'keymap org-mouse-map))
  2904. t)))
  2905. (defun org-font-lock-level ()
  2906. (save-excursion
  2907. (org-back-to-heading t)
  2908. (- (match-end 0) (match-beginning 0))))
  2909. (defun org-outline-level ()
  2910. (save-excursion
  2911. (looking-at outline-regexp)
  2912. (if (match-beginning 1)
  2913. (+ (org-get-string-indentation (match-string 1)) 1000)
  2914. (- (match-end 0) (match-beginning 0)))))
  2915. (defvar org-font-lock-keywords nil)
  2916. (defun org-set-font-lock-defaults ()
  2917. (let* ((em org-fontify-emphasized-text)
  2918. (lk org-activate-links)
  2919. (org-font-lock-extra-keywords
  2920. ;; Headlines
  2921. (list
  2922. '("^\\(\\**\\)\\(\\*\\)\\(.*\\)" (1 (org-get-level-face 1))
  2923. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  2924. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  2925. (1 'org-table))
  2926. ;; Links
  2927. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  2928. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  2929. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  2930. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  2931. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  2932. (if (memq 'camel lk) '(org-activate-camels (0 'org-link t)))
  2933. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  2934. (if org-table-limit-column-width
  2935. '(org-hide-wide-columns (0 nil append)))
  2936. ;; TODO lines
  2937. (list (concat "^\\*+[ \t]*" org-not-done-regexp)
  2938. '(1 'org-todo t))
  2939. ;; Priorities
  2940. (list (concat "\\[#[A-Z]\\]") '(0 'org-special-keyword t))
  2941. ;; Special keywords
  2942. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  2943. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  2944. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  2945. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  2946. ;; Emphasis
  2947. (if em '(org-do-emphasis-faces))
  2948. ;; Checkboxes, similar to Frank Ruell's org-checklet.el
  2949. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[ X]\\]\\)"
  2950. 2 'bold prepend)
  2951. ;; COMMENT
  2952. (list (concat "^\\*+[ \t]*\\<\\(" org-comment-string
  2953. "\\|" org-quote-string "\\)\\>")
  2954. '(1 'org-special-keyword t))
  2955. '("^#.*" (0 'font-lock-comment-face t))
  2956. ;; DONE
  2957. (if org-fontify-done-headline
  2958. (list (concat "^[*]+ +\\<\\(" org-done-string "\\)\\(.*\\)\\>")
  2959. '(1 'org-done t) '(2 'org-headline-done t))
  2960. (list (concat "^[*]+ +\\<\\(" org-done-string "\\)\\>")
  2961. '(1 'org-done t)))
  2962. ;; Table stuff
  2963. '("^[ \t]*\\(:.*\\)" (1 'org-table t))
  2964. '("| *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  2965. '("^[ \t]*| *\\([#!$*_^]\\) *|" (1 'org-formula t))
  2966. (if org-format-transports-properties-p
  2967. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  2968. '("^\\*+ \\(.*:ARCHIVE:.*\\)" (1 'org-archived prepend))
  2969. )))
  2970. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  2971. ;; Now set the full font-lock-keywords
  2972. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  2973. (org-set-local 'font-lock-defaults
  2974. '(org-font-lock-keywords t nil nil backward-paragraph))
  2975. (kill-local-variable 'font-lock-keywords) nil))
  2976. (defvar org-m nil)
  2977. (defvar org-l nil)
  2978. (defvar org-f nil)
  2979. (defun org-get-level-face (n)
  2980. "Get the right face for match N in font-lock matching of healdines."
  2981. (setq org-l (- (match-end 2) (match-beginning 1)))
  2982. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  2983. ; (setq org-f (nth (1- (% org-l org-n-levels)) org-level-faces))
  2984. (setq org-f (nth (% (1- org-l) org-n-levels) org-level-faces))
  2985. (cond
  2986. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  2987. ((eq n 2) org-f)
  2988. (t (if org-level-color-stars-only nil org-f))))
  2989. (defun org-unfontify-region (beg end &optional maybe_loudly)
  2990. "Remove fontification and activation overlays from links."
  2991. (font-lock-default-unfontify-region beg end)
  2992. (let* ((buffer-undo-list t)
  2993. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  2994. (inhibit-modification-hooks t)
  2995. deactivate-mark buffer-file-name buffer-file-truename)
  2996. (remove-text-properties beg end
  2997. '(mouse-face nil keymap nil org-linked-text nil
  2998. invisible nil intangible nil))))
  2999. ;;; Visibility cycling
  3000. (defvar org-cycle-global-status nil)
  3001. (make-variable-buffer-local 'org-cycle-global-status)
  3002. (defvar org-cycle-subtree-status nil)
  3003. (make-variable-buffer-local 'org-cycle-subtree-status)
  3004. ;;;###autoload
  3005. (defun org-cycle (&optional arg)
  3006. "Visibility cycling for Org-mode.
  3007. - When this function is called with a prefix argument, rotate the entire
  3008. buffer through 3 states (global cycling)
  3009. 1. OVERVIEW: Show only top-level headlines.
  3010. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3011. 3. SHOW ALL: Show everything.
  3012. - When point is at the beginning of a headline, rotate the subtree started
  3013. by this line through 3 different states (local cycling)
  3014. 1. FOLDED: Only the main headline is shown.
  3015. 2. CHILDREN: The main headline and the direct children are shown.
  3016. From this state, you can move to one of the children
  3017. and zoom in further.
  3018. 3. SUBTREE: Show the entire subtree, including body text.
  3019. - When there is a numeric prefix, go up to a heading with level ARG, do
  3020. a `show-subtree' and return to the previous cursor position. If ARG
  3021. is negative, go up that many levels.
  3022. - When point is not at the beginning of a headline, execute
  3023. `indent-relative', like TAB normally does. See the option
  3024. `org-cycle-emulate-tab' for details.
  3025. - Special case: if point is the the beginning of the buffer and there is
  3026. no headline in line 1, this function will act as if called with prefix arg."
  3027. (interactive "P")
  3028. (let* ((outline-regexp
  3029. (if org-cycle-include-plain-lists
  3030. "\\*+\\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) "
  3031. outline-regexp))
  3032. (bob-special (and org-cycle-global-at-bob (bobp)
  3033. (not (looking-at outline-regexp))))
  3034. (org-cycle-hook
  3035. (if bob-special
  3036. (delq 'org-optimize-window-after-visibility-change
  3037. (copy-sequence org-cycle-hook))
  3038. org-cycle-hook))
  3039. (pos (point)))
  3040. (if (or bob-special (equal arg '(4)))
  3041. ;; special case: use global cycling
  3042. (setq arg t))
  3043. (cond
  3044. ((org-at-table-p 'any)
  3045. ;; Enter the table or move to the next field in the table
  3046. (or (org-table-recognize-table.el)
  3047. (progn
  3048. (if arg (org-table-edit-field t)
  3049. (org-table-justify-field-maybe)
  3050. (call-interactively 'org-table-next-field)))))
  3051. ((eq arg t) ;; Global cycling
  3052. (cond
  3053. ((and (eq last-command this-command)
  3054. (eq org-cycle-global-status 'overview))
  3055. ;; We just created the overview - now do table of contents
  3056. ;; This can be slow in very large buffers, so indicate action
  3057. (message "CONTENTS...")
  3058. (org-content)
  3059. (message "CONTENTS...done")
  3060. (setq org-cycle-global-status 'contents)
  3061. (run-hook-with-args 'org-cycle-hook 'contents))
  3062. ((and (eq last-command this-command)
  3063. (eq org-cycle-global-status 'contents))
  3064. ;; We just showed the table of contents - now show everything
  3065. (show-all)
  3066. (message "SHOW ALL")
  3067. (setq org-cycle-global-status 'all)
  3068. (run-hook-with-args 'org-cycle-hook 'all))
  3069. (t
  3070. ;; Default action: go to overview
  3071. (org-overview)
  3072. (message "OVERVIEW")
  3073. (setq org-cycle-global-status 'overview)
  3074. (run-hook-with-args 'org-cycle-hook 'overview))))
  3075. ((integerp arg)
  3076. ;; Show-subtree, ARG levels up from here.
  3077. (save-excursion
  3078. (org-back-to-heading)
  3079. (outline-up-heading (if (< arg 0) (- arg)
  3080. (- (funcall outline-level) arg)))
  3081. (org-show-subtree)))
  3082. ((save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3083. ;; At a heading: rotate between three different views
  3084. (org-back-to-heading)
  3085. (let ((goal-column 0) eoh eol eos)
  3086. ;; First, some boundaries
  3087. (save-excursion
  3088. (org-back-to-heading)
  3089. (save-excursion
  3090. (beginning-of-line 2)
  3091. (while (and (not (eobp)) ;; this is like `next-line'
  3092. (get-char-property (1- (point)) 'invisible))
  3093. (beginning-of-line 2)) (setq eol (point)))
  3094. (outline-end-of-heading) (setq eoh (point))
  3095. (org-end-of-subtree t) (setq eos (point))
  3096. (outline-next-heading))
  3097. ;; Find out what to do next and set `this-command'
  3098. (cond
  3099. ((and (= eos eoh)
  3100. ;; Nothing is hidden behind this heading
  3101. (message "EMPTY ENTRY")
  3102. (setq org-cycle-subtree-status nil)))
  3103. ((>= eol eos)
  3104. ;; Entire subtree is hidden in one line: open it
  3105. (org-show-entry)
  3106. (show-children)
  3107. (message "CHILDREN")
  3108. (setq org-cycle-subtree-status 'children)
  3109. (run-hook-with-args 'org-cycle-hook 'children))
  3110. ((and (eq last-command this-command)
  3111. (eq org-cycle-subtree-status 'children))
  3112. ;; We just showed the children, now show everything.
  3113. (org-show-subtree)
  3114. (message "SUBTREE")
  3115. (setq org-cycle-subtree-status 'subtree)
  3116. (run-hook-with-args 'org-cycle-hook 'subtree))
  3117. (t
  3118. ;; Default action: hide the subtree.
  3119. (hide-subtree)
  3120. (message "FOLDED")
  3121. (setq org-cycle-subtree-status 'folded)
  3122. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3123. ;; TAB emulation
  3124. (buffer-read-only (org-back-to-heading))
  3125. ((org-try-cdlatex-tab))
  3126. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3127. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3128. (or (and (eq org-cycle-emulate-tab 'white)
  3129. (= (match-end 0) (point-at-eol)))
  3130. (and (eq org-cycle-emulate-tab 'whitestart)
  3131. (>= (match-end 0) pos))))
  3132. t
  3133. (eq org-cycle-emulate-tab t))
  3134. (if (and (looking-at "[ \n\r\t]")
  3135. (string-match "^[ \t]*$" (buffer-substring
  3136. (point-at-bol) (point))))
  3137. (progn
  3138. (beginning-of-line 1)
  3139. (and (looking-at "[ \t]+") (replace-match ""))))
  3140. (indent-relative))
  3141. (t (save-excursion
  3142. (org-back-to-heading)
  3143. (org-cycle))))))
  3144. ;;;###autoload
  3145. (defun org-global-cycle (&optional arg)
  3146. "Cycle the global visibility. For details see `org-cycle'."
  3147. (interactive "P")
  3148. (if (integerp arg)
  3149. (progn
  3150. (show-all)
  3151. (hide-sublevels arg)
  3152. (setq org-cycle-global-status 'contents))
  3153. (org-cycle '(4))))
  3154. (defun org-overview ()
  3155. "Switch to overview mode, shoing only top-level headlines.
  3156. Really, this shows all headlines with level equal or greater than the level
  3157. of the first headline in the buffer. This is important, because if the
  3158. first headline is not level one, then (hide-sublevels 1) gives confusing
  3159. results."
  3160. (interactive)
  3161. (hide-sublevels (save-excursion
  3162. (goto-char (point-min))
  3163. (if (re-search-forward (concat "^" outline-regexp) nil t)
  3164. (progn
  3165. (goto-char (match-beginning 0))
  3166. (funcall outline-level))
  3167. 1))))
  3168. ;; FIXME: allow an argument to give a limiting level for this.
  3169. (defun org-content ()
  3170. "Show all headlines in the buffer, like a table of contents"
  3171. (interactive)
  3172. (save-excursion
  3173. ;; Visit all headings and show their offspring
  3174. (goto-char (point-max))
  3175. (catch 'exit
  3176. (while (and (progn (condition-case nil
  3177. (outline-previous-visible-heading 1)
  3178. (error (goto-char (point-min))))
  3179. t)
  3180. (looking-at outline-regexp))
  3181. (show-branches)
  3182. (if (bobp) (throw 'exit nil))))))
  3183. (defun org-optimize-window-after-visibility-change (state)
  3184. "Adjust the window after a change in outline visibility.
  3185. This function is the default value of the hook `org-cycle-hook'."
  3186. (when (get-buffer-window (current-buffer))
  3187. (cond
  3188. ((eq state 'overview) (org-first-headline-recenter 1))
  3189. ((eq state 'content) nil)
  3190. ((eq state 'all) nil)
  3191. ((eq state 'folded) nil)
  3192. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  3193. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  3194. (defun org-subtree-end-visible-p ()
  3195. "Is the end of the current subtree visible?"
  3196. (pos-visible-in-window-p
  3197. (save-excursion (org-end-of-subtree t) (point))))
  3198. (defun org-first-headline-recenter (&optional N)
  3199. "Move cursor to the first headline and recenter the headline.
  3200. Optional argument N means, put the headline into the Nth line of the window."
  3201. (goto-char (point-min))
  3202. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  3203. (beginning-of-line)
  3204. (recenter (prefix-numeric-value N))))
  3205. (defvar org-goto-window-configuration nil)
  3206. (defvar org-goto-marker nil)
  3207. (defvar org-goto-map (make-sparse-keymap))
  3208. (let ((cmds '(isearch-forward isearch-backward)) cmd)
  3209. (while (setq cmd (pop cmds))
  3210. (substitute-key-definition cmd cmd org-goto-map global-map)))
  3211. (define-key org-goto-map "\C-m" 'org-goto-ret)
  3212. (define-key org-goto-map [(left)] 'org-goto-left)
  3213. (define-key org-goto-map [(right)] 'org-goto-right)
  3214. (define-key org-goto-map [(?q)] 'org-goto-quit)
  3215. (define-key org-goto-map [(control ?g)] 'org-goto-quit)
  3216. (define-key org-goto-map "\C-i" 'org-cycle)
  3217. (define-key org-goto-map [(tab)] 'org-cycle)
  3218. (define-key org-goto-map [(down)] 'outline-next-visible-heading)
  3219. (define-key org-goto-map [(up)] 'outline-previous-visible-heading)
  3220. (define-key org-goto-map "n" 'outline-next-visible-heading)
  3221. (define-key org-goto-map "p" 'outline-previous-visible-heading)
  3222. (define-key org-goto-map "f" 'outline-forward-same-level)
  3223. (define-key org-goto-map "b" 'outline-backward-same-level)
  3224. (define-key org-goto-map "u" 'outline-up-heading)
  3225. (define-key org-goto-map "\C-c\C-n" 'outline-next-visible-heading)
  3226. (define-key org-goto-map "\C-c\C-p" 'outline-previous-visible-heading)
  3227. (define-key org-goto-map "\C-c\C-f" 'outline-forward-same-level)
  3228. (define-key org-goto-map "\C-c\C-b" 'outline-backward-same-level)
  3229. (define-key org-goto-map "\C-c\C-u" 'outline-up-heading)
  3230. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  3231. (while l (define-key org-goto-map (int-to-string (pop l)) 'digit-argument)))
  3232. (defconst org-goto-help
  3233. "Select a location to jump to, press RET
  3234. \[Up]/[Down]=next/prev headline TAB=cycle visibility RET=select [Q]uit")
  3235. (defun org-goto ()
  3236. "Go to a different location of the document, keeping current visibility.
  3237. When you want to go to a different location in a document, the fastest way
  3238. is often to fold the entire buffer and then dive into the tree. This
  3239. method has the disadvantage, that the previous location will be folded,
  3240. which may not be what you want.
  3241. This command works around this by showing a copy of the current buffer in
  3242. overview mode. You can dive into the tree in that copy, to find the
  3243. location you want to reach. When pressing RET, the command returns to the
  3244. original buffer in which the visibility is still unchanged. It then jumps
  3245. to the new location, making it and the headline hierarchy above it visible."
  3246. (interactive)
  3247. (let* ((org-goto-start-pos (point))
  3248. (selected-point
  3249. (org-get-location (current-buffer) org-goto-help)))
  3250. (if selected-point
  3251. (progn
  3252. (org-mark-ring-push org-goto-start-pos)
  3253. (goto-char selected-point)
  3254. (if (or (org-invisible-p) (org-invisible-p2))
  3255. (org-show-hierarchy-above)))
  3256. (error "Quit"))))
  3257. (defun org-get-location (buf help)
  3258. "Let the user select a location in the Org-mode buffer BUF.
  3259. This function uses a recursive edit. It returns the selected position
  3260. or nil."
  3261. (let (org-selected-point)
  3262. (save-excursion
  3263. (save-window-excursion
  3264. (delete-other-windows)
  3265. (switch-to-buffer (get-buffer-create "*org-goto*"))
  3266. (with-output-to-temp-buffer "*Help*"
  3267. (princ help))
  3268. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  3269. (setq buffer-read-only nil)
  3270. (erase-buffer)
  3271. (insert-buffer-substring buf)
  3272. (let ((org-startup-truncated t)
  3273. (org-startup-folded t)
  3274. (org-startup-align-all-tables nil)
  3275. (org-startup-with-deadline-check nil))
  3276. (org-mode))
  3277. (setq buffer-read-only t)
  3278. (if (boundp 'org-goto-start-pos)
  3279. (goto-char org-goto-start-pos)
  3280. (goto-char (point-min)))
  3281. (org-beginning-of-line)
  3282. (message "Select location and press RET")
  3283. ;; now we make sure that during selection, ony very few keys work
  3284. ;; and that it is impossible to switch to another window.
  3285. (let ((gm (current-global-map))
  3286. (overriding-local-map org-goto-map))
  3287. (unwind-protect
  3288. (progn
  3289. (use-global-map org-goto-map)
  3290. (recursive-edit))
  3291. (use-global-map gm)))))
  3292. (kill-buffer "*org-goto*")
  3293. org-selected-point))
  3294. (defun org-goto-ret (&optional arg)
  3295. "Finish `org-goto' by going to the new location."
  3296. (interactive "P")
  3297. (setq org-selected-point (point)
  3298. current-prefix-arg arg)
  3299. (throw 'exit nil))
  3300. (defun org-goto-left ()
  3301. "Finish `org-goto' by going to the new location."
  3302. (interactive)
  3303. (if (org-on-heading-p)
  3304. (progn
  3305. (beginning-of-line 1)
  3306. (setq org-selected-point (point)
  3307. current-prefix-arg (- (match-end 0) (match-beginning 0)))
  3308. (throw 'exit nil))
  3309. (error "Not on a heading")))
  3310. (defun org-goto-right ()
  3311. "Finish `org-goto' by going to the new location."
  3312. (interactive)
  3313. (if (org-on-heading-p)
  3314. (progn
  3315. (outline-end-of-subtree)
  3316. (or (eobp) (forward-char 1))
  3317. (setq org-selected-point (point)
  3318. current-prefix-arg (- (match-end 0) (match-beginning 0)))
  3319. (throw 'exit nil))
  3320. (error "Not on a heading")))
  3321. (defun org-goto-quit ()
  3322. "Finish `org-goto' without cursor motion."
  3323. (interactive)
  3324. (setq org-selected-point nil)
  3325. (throw 'exit nil))
  3326. ;;; Promotion, Demotion, Inserting new headlines
  3327. (defvar org-ignore-region nil
  3328. "To temporarily disable the active region.")
  3329. (defun org-insert-heading (&optional force-heading)
  3330. "Insert a new heading or item with same depth at point.
  3331. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  3332. If point is at the beginning of a headline, insert a sibling before the
  3333. current headline. If point is in the middle of a headline, split the headline
  3334. at that position and make the rest of the headline part of the sibling below
  3335. the current headline."
  3336. (interactive "P")
  3337. (if (= (buffer-size) 0)
  3338. (insert "\n* ")
  3339. (when (or force-heading (not (org-insert-item)))
  3340. (let* ((head (save-excursion
  3341. (condition-case nil
  3342. (progn
  3343. (org-back-to-heading)
  3344. (match-string 0))
  3345. (error "*"))))
  3346. pos)
  3347. (cond
  3348. ((and (org-on-heading-p) (bolp)
  3349. (save-excursion (backward-char 1) (not (org-invisible-p))))
  3350. (open-line 1))
  3351. ((and (bolp) (save-excursion
  3352. (backward-char 1) (not (org-invisible-p))))
  3353. nil)
  3354. (t (newline)))
  3355. (insert head) (just-one-space)
  3356. (setq pos (point))
  3357. (end-of-line 1)
  3358. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  3359. (run-hooks 'org-insert-heading-hook)))))
  3360. (defun org-insert-item (&optional checkbox)
  3361. "Insert a new item at the current level.
  3362. Return t when things worked, nil when we are not in an item."
  3363. (when (save-excursion
  3364. (condition-case nil
  3365. (progn
  3366. (org-beginning-of-item)
  3367. (org-at-item-p)
  3368. t)
  3369. (error nil)))
  3370. (let* ((bul (match-string 0))
  3371. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  3372. (match-end 0)))
  3373. pos)
  3374. (cond
  3375. ((and (org-at-item-p) (<= (point) eow))
  3376. ;; before the bullet
  3377. (beginning-of-line 1)
  3378. (open-line 1))
  3379. ((<= (point) eow)
  3380. (beginning-of-line 1))
  3381. (t (newline)))
  3382. (insert bul (if checkbox "[ ]" ""))
  3383. (just-one-space)
  3384. (setq pos (point))
  3385. (end-of-line 1)
  3386. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  3387. (org-maybe-renumber-ordered-list)
  3388. t))
  3389. (defun org-insert-todo-heading (arg)
  3390. "Insert a new heading with the same level and TODO state as current heading.
  3391. If the heading has no TODO state, or if the state is DONE, use the first
  3392. state (TODO by default). Also with prefix arg, force first state."
  3393. (interactive "P")
  3394. (when (not (org-insert-item 'checkbox))
  3395. (org-insert-heading)
  3396. (save-excursion
  3397. (org-back-to-heading)
  3398. (outline-previous-heading)
  3399. (looking-at org-todo-line-regexp))
  3400. (if (or arg
  3401. (not (match-beginning 2))
  3402. (equal (match-string 2) org-done-string))
  3403. (insert (car org-todo-keywords) " ")
  3404. (insert (match-string 2) " "))))
  3405. (defun org-promote-subtree ()
  3406. "Promote the entire subtree.
  3407. See also `org-promote'."
  3408. (interactive)
  3409. (save-excursion
  3410. (org-map-tree 'org-promote)))
  3411. (defun org-demote-subtree ()
  3412. "Demote the entire subtree. See `org-demote'.
  3413. See also `org-promote'."
  3414. (interactive)
  3415. (save-excursion
  3416. (org-map-tree 'org-demote)))
  3417. (defun org-do-promote ()
  3418. "Promote the current heading higher up the tree.
  3419. If the region is active in `transient-mark-mode', promote all headings
  3420. in the region."
  3421. (interactive)
  3422. (save-excursion
  3423. (if (org-region-active-p)
  3424. (org-map-region 'org-promote (region-beginning) (region-end))
  3425. (org-promote)))
  3426. (org-fix-position-after-promote))
  3427. (defun org-do-demote ()
  3428. "Demote the current heading lower down the tree.
  3429. If the region is active in `transient-mark-mode', demote all headings
  3430. in the region."
  3431. (interactive)
  3432. (save-excursion
  3433. (if (org-region-active-p)
  3434. (org-map-region 'org-demote (region-beginning) (region-end))
  3435. (org-demote)))
  3436. (org-fix-position-after-promote))
  3437. (defun org-fix-position-after-promote ()
  3438. "Make sure that after pro/demotion cursor position is right."
  3439. (and (equal (char-after) ?\ )
  3440. (equal (char-before) ?*)
  3441. (forward-char 1)))
  3442. (defun org-get-legal-level (level change)
  3443. "Rectify a level change under the influence of `org-odd-levels-only'
  3444. LEVEL is a current level, CHANGE is by how much the level should be
  3445. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  3446. even level numbers will become the next higher odd number."
  3447. (if org-odd-levels-only
  3448. (cond ((not change) (1+ (* 2 (/ level 2))))
  3449. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  3450. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  3451. (max 1 (+ level change))))
  3452. (defun org-promote ()
  3453. "Promote the current heading higher up the tree.
  3454. If the region is active in `transient-mark-mode', promote all headings
  3455. in the region."
  3456. (org-back-to-heading t)
  3457. (let* ((level (save-match-data (funcall outline-level)))
  3458. (up-head (make-string (org-get-legal-level level -1) ?*))
  3459. (diff (abs (- level (length up-head)))))
  3460. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover"))
  3461. (replace-match up-head nil t)
  3462. ;; Fixup tag positioning
  3463. (and org-auto-align-tags (org-set-tags nil t))
  3464. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  3465. (defun org-demote ()
  3466. "Demote the current heading lower down the tree.
  3467. If the region is active in `transient-mark-mode', demote all headings
  3468. in the region."
  3469. (org-back-to-heading t)
  3470. (let* ((level (save-match-data (funcall outline-level)))
  3471. (down-head (make-string (org-get-legal-level level 1) ?*))
  3472. (diff (abs (- level (length down-head)))))
  3473. (replace-match down-head nil t)
  3474. ;; Fixup tag positioning
  3475. (and org-auto-align-tags (org-set-tags nil t))
  3476. (if org-adapt-indentation (org-fixup-indentation diff))))
  3477. (defun org-map-tree (fun)
  3478. "Call FUN for every heading underneath the current one."
  3479. (org-back-to-heading)
  3480. (let ((level (funcall outline-level)))
  3481. (save-excursion
  3482. (funcall fun)
  3483. (while (and (progn
  3484. (outline-next-heading)
  3485. (> (funcall outline-level) level))
  3486. (not (eobp)))
  3487. (funcall fun)))))
  3488. (defun org-map-region (fun beg end)
  3489. "Call FUN for every heading between BEG and END."
  3490. (let ((org-ignore-region t))
  3491. (save-excursion
  3492. (setq end (copy-marker end))
  3493. (goto-char beg)
  3494. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  3495. (< (point) end))
  3496. (funcall fun))
  3497. (while (and (progn
  3498. (outline-next-heading)
  3499. (< (point) end))
  3500. (not (eobp)))
  3501. (funcall fun)))))
  3502. (defun org-fixup-indentation (diff)
  3503. "Change the indentation in the current entry by DIFF
  3504. However, if any line in the current entry has no indentation, or if it
  3505. would end up with no indentation after the change, nothing at all is done."
  3506. (save-excursion
  3507. (let ((end (save-excursion (outline-next-heading)
  3508. (point-marker)))
  3509. (prohibit (if (> diff 0)
  3510. "^\\S-"
  3511. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  3512. col)
  3513. (unless (save-excursion (re-search-forward prohibit end t))
  3514. (while (re-search-forward "^[ \t]+" end t)
  3515. (goto-char (match-end 0))
  3516. (setq col (current-column))
  3517. (if (< diff 0) (replace-match ""))
  3518. (indent-to (+ diff col))))
  3519. (move-marker end nil))))
  3520. ;;; Vertical tree motion, cutting and pasting of subtrees
  3521. (defun org-move-subtree-up (&optional arg)
  3522. "Move the current subtree up past ARG headlines of the same level."
  3523. (interactive "p")
  3524. (org-move-subtree-down (- (prefix-numeric-value arg))))
  3525. (defun org-move-subtree-down (&optional arg)
  3526. "Move the current subtree down past ARG headlines of the same level."
  3527. (interactive "p")
  3528. (setq arg (prefix-numeric-value arg))
  3529. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  3530. 'outline-get-last-sibling))
  3531. (ins-point (make-marker))
  3532. (cnt (abs arg))
  3533. beg end txt folded)
  3534. ;; Select the tree
  3535. (org-back-to-heading)
  3536. (setq beg (point))
  3537. (save-match-data
  3538. (save-excursion (outline-end-of-heading)
  3539. (setq folded (org-invisible-p)))
  3540. (outline-end-of-subtree))
  3541. (outline-next-heading)
  3542. (setq end (point))
  3543. ;; Find insertion point, with error handling
  3544. (goto-char beg)
  3545. (while (> cnt 0)
  3546. (or (and (funcall movfunc) (looking-at outline-regexp))
  3547. (progn (goto-char beg)
  3548. (error "Cannot move past superior level or buffer limit")))
  3549. (setq cnt (1- cnt)))
  3550. (if (> arg 0)
  3551. ;; Moving forward - still need to move over subtree
  3552. (progn (outline-end-of-subtree)
  3553. (outline-next-heading)
  3554. (if (not (or (looking-at (concat "^" outline-regexp))
  3555. (bolp)))
  3556. (newline))))
  3557. (move-marker ins-point (point))
  3558. (setq txt (buffer-substring beg end))
  3559. (delete-region beg end)
  3560. (insert txt)
  3561. (goto-char ins-point)
  3562. (if folded (hide-subtree))
  3563. (move-marker ins-point nil)))
  3564. (defvar org-subtree-clip ""
  3565. "Clipboard for cut and paste of subtrees.
  3566. This is actually only a copy of the kill, because we use the normal kill
  3567. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  3568. (defvar org-subtree-clip-folded nil
  3569. "Was the last copied subtree folded?
  3570. This is used to fold the tree back after pasting.")
  3571. (defun org-cut-subtree ()
  3572. "Cut the current subtree into the clipboard.
  3573. This is a short-hand for marking the subtree and then cutting it."
  3574. (interactive)
  3575. (org-copy-subtree 'cut))
  3576. (defun org-copy-subtree (&optional cut)
  3577. "Cut the current subtree into the clipboard.
  3578. This is a short-hand for marking the subtree and then copying it.
  3579. If CUT is non nil, actually cut the subtree."
  3580. (interactive)
  3581. (let (beg end folded)
  3582. (org-back-to-heading)
  3583. (setq beg (point))
  3584. (save-match-data
  3585. (save-excursion (outline-end-of-heading)
  3586. (setq folded (org-invisible-p)))
  3587. (outline-end-of-subtree))
  3588. (if (equal (char-after) ?\n) (forward-char 1))
  3589. (setq end (point))
  3590. (goto-char beg)
  3591. (when (> end beg)
  3592. (setq org-subtree-clip-folded folded)
  3593. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  3594. (setq org-subtree-clip (current-kill 0))
  3595. (message "%s: Subtree with %d characters"
  3596. (if cut "Cut" "Copied")
  3597. (length org-subtree-clip)))))
  3598. (defun org-paste-subtree (&optional level tree)
  3599. "Paste the clipboard as a subtree, with modification of headline level.
  3600. The entire subtree is promoted or demoted in order to match a new headline
  3601. level. By default, the new level is derived from the visible headings
  3602. before and after the insertion point, and taken to be the inferior headline
  3603. level of the two. So if the previous visible heading is level 3 and the
  3604. next is level 4 (or vice versa), level 4 will be used for insertion.
  3605. This makes sure that the subtree remains an independent subtree and does
  3606. not swallow low level entries.
  3607. You can also force a different level, either by using a numeric prefix
  3608. argument, or by inserting the heading marker by hand. For example, if the
  3609. cursor is after \"*****\", then the tree will be shifted to level 5.
  3610. If you want to insert the tree as is, just use \\[yank].
  3611. If optional TREE is given, use this text instead of the kill ring."
  3612. (interactive "P")
  3613. (unless (org-kill-is-subtree-p tree)
  3614. (error
  3615. (substitute-command-keys
  3616. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  3617. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  3618. (^re (concat "^\\(" outline-regexp "\\)"))
  3619. (re (concat "\\(" outline-regexp "\\)"))
  3620. (^re_ (concat "\\(" outline-regexp "\\)[ \t]*"))
  3621. (old-level (if (string-match ^re txt)
  3622. (- (match-end 0) (match-beginning 0))
  3623. -1))
  3624. (force-level (cond (level (prefix-numeric-value level))
  3625. ((string-match
  3626. ^re_ (buffer-substring (point-at-bol) (point)))
  3627. (- (match-end 0) (match-beginning 0)))
  3628. (t nil)))
  3629. (previous-level (save-excursion
  3630. (condition-case nil
  3631. (progn
  3632. (outline-previous-visible-heading 1)
  3633. (if (looking-at re)
  3634. (- (match-end 0) (match-beginning 0))
  3635. 1))
  3636. (error 1))))
  3637. (next-level (save-excursion
  3638. (condition-case nil
  3639. (progn
  3640. (outline-next-visible-heading 1)
  3641. (if (looking-at re)
  3642. (- (match-end 0) (match-beginning 0))
  3643. 1))
  3644. (error 1))))
  3645. (new-level (or force-level (max previous-level next-level)))
  3646. (shift (if (or (= old-level -1)
  3647. (= new-level -1)
  3648. (= old-level new-level))
  3649. 0
  3650. (- new-level old-level)))
  3651. (shift1 shift)
  3652. (delta (if (> shift 0) -1 1))
  3653. (func (if (> shift 0) 'org-demote 'org-promote))
  3654. (org-odd-levels-only nil)
  3655. beg end)
  3656. ;; Remove the forces level indicator
  3657. (if force-level
  3658. (delete-region (point-at-bol) (point)))
  3659. ;; Make sure we start at the beginning of an empty line
  3660. (if (not (bolp)) (insert "\n"))
  3661. (if (not (looking-at "[ \t]*$"))
  3662. (progn (insert "\n") (backward-char 1)))
  3663. ;; Paste
  3664. (setq beg (point))
  3665. (if (string-match "[ \t\r\n]+\\'" txt)
  3666. (setq txt (replace-match "\n" t t txt)))
  3667. (insert txt)
  3668. (setq end (point))
  3669. (if (looking-at "[ \t\r\n]+")
  3670. (replace-match "\n"))
  3671. (goto-char beg)
  3672. ;; Shift if necessary
  3673. (if (= shift 0)
  3674. (message "Pasted at level %d, without shift" new-level)
  3675. (save-restriction
  3676. (narrow-to-region beg end)
  3677. (while (not (= shift 0))
  3678. (org-map-region func (point-min) (point-max))
  3679. (setq shift (+ delta shift)))
  3680. (goto-char (point-min))
  3681. (message "Pasted at level %d, with shift by %d levels"
  3682. new-level shift1)))
  3683. (if (and kill-ring
  3684. (eq org-subtree-clip (current-kill 0))
  3685. org-subtree-clip-folded)
  3686. ;; The tree was folded before it was killed/copied
  3687. (hide-subtree))))
  3688. (defun org-kill-is-subtree-p (&optional txt)
  3689. "Check if the current kill is an outline subtree, or a set of trees.
  3690. Returns nil if kill does not start with a headline, or if the first
  3691. headline level is not the largest headline level in the tree.
  3692. So this will actually accept several entries of equal levels as well,
  3693. which is OK for `org-paste-subtree'.
  3694. If optional TXT is given, check this string instead of the current kill."
  3695. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  3696. (start-level (and kill
  3697. (string-match (concat "\\`" outline-regexp) kill)
  3698. (- (match-end 0) (match-beginning 0))))
  3699. (re (concat "^" outline-regexp))
  3700. (start 1))
  3701. (if (not start-level)
  3702. nil ;; does not even start with a heading
  3703. (catch 'exit
  3704. (while (setq start (string-match re kill (1+ start)))
  3705. (if (< (- (match-end 0) (match-beginning 0)) start-level)
  3706. (throw 'exit nil)))
  3707. t))))
  3708. (defun org-narrow-to-subtree ()
  3709. "Narrow buffer to the current subtree."
  3710. (interactive)
  3711. (save-excursion
  3712. (narrow-to-region
  3713. (progn (org-back-to-heading) (point))
  3714. (progn (org-end-of-subtree t) (point)))))
  3715. ;;; Plain list items
  3716. (defun org-at-item-p ()
  3717. "Is point in a line starting a hand-formatted item?"
  3718. (let ((llt org-plain-list-ordered-item-terminator))
  3719. (save-excursion
  3720. (goto-char (point-at-bol))
  3721. (looking-at
  3722. (cond
  3723. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  3724. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  3725. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  3726. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  3727. (defun org-at-item-checkbox-p ()
  3728. "Is point at a line starting a plain-list item with a checklet?"
  3729. (and (org-at-item-p)
  3730. (save-excursion
  3731. (goto-char (match-end 0))
  3732. (skip-chars-forward " \t")
  3733. (looking-at "\\[[ X]\\]"))))
  3734. (defun org-toggle-checkbox (&optional arg)
  3735. "Toggle the checkbox in the current line."
  3736. (interactive "P")
  3737. (catch 'exit
  3738. (let (beg end status (firstnew 'unknown))
  3739. (cond
  3740. ((org-region-active-p)
  3741. (setq beg (region-beginning) end (region-end)))
  3742. ((org-on-heading-p)
  3743. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  3744. ((org-at-item-checkbox-p)
  3745. (save-excursion
  3746. (replace-match (if (equal (match-string 0) "[ ]") "[X]" "[ ]") t t))
  3747. (throw 'exit t))
  3748. (t (error "Not at a checkbox or heading, and no active region")))
  3749. (save-excursion
  3750. (goto-char beg)
  3751. (while (< (point) end)
  3752. (when (org-at-item-checkbox-p)
  3753. (setq status (equal (match-string 0) "[X]"))
  3754. (when (eq firstnew 'unknown)
  3755. (setq firstnew (not status)))
  3756. (replace-match
  3757. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  3758. (beginning-of-line 2))))))
  3759. (defun org-get-indentation (&optional line)
  3760. "Get the indentation of the current line, interpreting tabs.
  3761. When LINE is given, assume it represents a line and compute its indentation."
  3762. (if line
  3763. (if (string-match "^ *" (org-remove-tabs line))
  3764. (match-end 0))
  3765. (save-excursion
  3766. (beginning-of-line 1)
  3767. (skip-chars-forward " \t")
  3768. (current-column))))
  3769. (defun org-remove-tabs (s &optional width)
  3770. "Replace tabulators in S with spaces.
  3771. Assumes that s is a single line, starting in column 0."
  3772. (setq width (or width tab-width))
  3773. (while (string-match "\t" s)
  3774. (setq s (replace-match
  3775. (make-string
  3776. (- (* width (/ (+ (match-beginning 0) width) width))
  3777. (match-beginning 0)) ?\ )
  3778. t t s)))
  3779. s)
  3780. ;; FIXME: document properly.
  3781. (defun org-fix-indentation (line ind)
  3782. "If the current indenation is smaller than ind1, leave it alone.
  3783. If it is larger than ind, reduce it by ind."
  3784. (let* ((l (org-remove-tabs line))
  3785. (i (org-get-indentation l))
  3786. (i1 (car ind)) (i2 (cdr ind)))
  3787. (if (>= i i2) (setq l (substring line i2)))
  3788. (if (> i1 0)
  3789. (concat (make-string i1 ?\ ) l)
  3790. l)))
  3791. (defun org-beginning-of-item ()
  3792. "Go to the beginning of the current hand-formatted item.
  3793. If the cursor is not in an item, throw an error."
  3794. (interactive)
  3795. (let ((pos (point))
  3796. (limit (save-excursion (org-back-to-heading)
  3797. (beginning-of-line 2) (point)))
  3798. ind ind1)
  3799. (if (org-at-item-p)
  3800. (beginning-of-line 1)
  3801. (beginning-of-line 1)
  3802. (skip-chars-forward " \t")
  3803. (setq ind (current-column))
  3804. (if (catch 'exit
  3805. (while t
  3806. (beginning-of-line 0)
  3807. (if (< (point) limit) (throw 'exit nil))
  3808. (unless (looking-at "[ \t]*$")
  3809. (skip-chars-forward " \t")
  3810. (setq ind1 (current-column))
  3811. (if (< ind1 ind)
  3812. (throw 'exit (org-at-item-p))))))
  3813. nil
  3814. (goto-char pos)
  3815. (error "Not in an item")))))
  3816. (defun org-end-of-item ()
  3817. "Go to the end of the current hand-formatted item.
  3818. If the cursor is not in an item, throw an error."
  3819. (interactive)
  3820. (let ((pos (point))
  3821. (limit (save-excursion (outline-next-heading) (point)))
  3822. (ind (save-excursion
  3823. (org-beginning-of-item)
  3824. (skip-chars-forward " \t")
  3825. (current-column)))
  3826. ind1)
  3827. (if (catch 'exit
  3828. (while t
  3829. (beginning-of-line 2)
  3830. (if (>= (point) limit) (throw 'exit t))
  3831. (unless (looking-at "[ \t]*$")
  3832. (skip-chars-forward " \t")
  3833. (setq ind1 (current-column))
  3834. (if (<= ind1 ind) (throw 'exit t)))))
  3835. (beginning-of-line 1)
  3836. (goto-char pos)
  3837. (error "Not in an item"))))
  3838. (defun org-next-item ()
  3839. "Move to the beginning of the next item in the current plain list.
  3840. Error if not at a plain list, or if this is the last item in the list."
  3841. (interactive)
  3842. (let (ind ind1 (pos (point)))
  3843. (org-beginning-of-item)
  3844. (setq ind (org-get-indentation))
  3845. (org-end-of-item)
  3846. (setq ind1 (org-get-indentation))
  3847. (unless (and (org-at-item-p) (= ind ind1))
  3848. (goto-char pos)
  3849. (error "On last item"))))
  3850. (defun org-previous-item ()
  3851. "Move to the beginning of the previous item in the current plain list.
  3852. Error if not at a plain list, or if this is the last item in the list."
  3853. (interactive)
  3854. (let (beg ind (pos (point)))
  3855. (org-beginning-of-item)
  3856. (setq beg (point))
  3857. (setq ind (org-get-indentation))
  3858. (goto-char beg)
  3859. (catch 'exit
  3860. (while t
  3861. (beginning-of-line 0)
  3862. (if (looking-at "[ \t]*$")
  3863. nil
  3864. (if (<= (org-get-indentation) ind)
  3865. (throw 'exit t)))))
  3866. (condition-case nil
  3867. (org-beginning-of-item)
  3868. (error (goto-char pos)
  3869. (error "On first item")))))
  3870. (defun org-move-item-down ()
  3871. "Move the plain list item at point down, i.e. swap with following item.
  3872. Subitems (items with larger indentation) are considered part of the item,
  3873. so this really moves item trees."
  3874. (interactive)
  3875. (let (beg end ind ind1 (pos (point)) txt)
  3876. (org-beginning-of-item)
  3877. (setq beg (point))
  3878. (setq ind (org-get-indentation))
  3879. (org-end-of-item)
  3880. (setq end (point))
  3881. (setq ind1 (org-get-indentation))
  3882. (if (and (org-at-item-p) (= ind ind1))
  3883. (progn
  3884. (org-end-of-item)
  3885. (setq txt (buffer-substring beg end))
  3886. (save-excursion
  3887. (delete-region beg end))
  3888. (setq pos (point))
  3889. (insert txt)
  3890. (goto-char pos)
  3891. (org-maybe-renumber-ordered-list))
  3892. (goto-char pos)
  3893. (error "Cannot move this item further down"))))
  3894. (defun org-move-item-up (arg)
  3895. "Move the plain list item at point up, i.e. swap with previous item.
  3896. Subitems (items with larger indentation) are considered part of the item,
  3897. so this really moves item trees."
  3898. (interactive "p")
  3899. (let (beg end ind ind1 (pos (point)) txt)
  3900. (org-beginning-of-item)
  3901. (setq beg (point))
  3902. (setq ind (org-get-indentation))
  3903. (org-end-of-item)
  3904. (setq end (point))
  3905. (goto-char beg)
  3906. (catch 'exit
  3907. (while t
  3908. (beginning-of-line 0)
  3909. (if (looking-at "[ \t]*$")
  3910. nil
  3911. (if (<= (setq ind1 (org-get-indentation)) ind)
  3912. (throw 'exit t)))))
  3913. (condition-case nil
  3914. (org-beginning-of-item)
  3915. (error (goto-char beg)
  3916. (error "Cannot move this item further up")))
  3917. (setq ind1 (org-get-indentation))
  3918. (if (and (org-at-item-p) (= ind ind1))
  3919. (progn
  3920. (setq txt (buffer-substring beg end))
  3921. (save-excursion
  3922. (delete-region beg end))
  3923. (setq pos (point))
  3924. (insert txt)
  3925. (goto-char pos)
  3926. (org-maybe-renumber-ordered-list))
  3927. (goto-char pos)
  3928. (error "Cannot move this item further up"))))
  3929. (defun org-maybe-renumber-ordered-list ()
  3930. "Renumber the ordered list at point if setup allows it.
  3931. This tests the user option `org-auto-renumber-ordered-lists' before
  3932. doing the renumbering."
  3933. (and org-auto-renumber-ordered-lists
  3934. (org-at-item-p)
  3935. (match-beginning 3)
  3936. (org-renumber-ordered-list 1)))
  3937. (defun org-get-string-indentation (s)
  3938. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  3939. (let ((n -1) (i 0) (w tab-width) c)
  3940. (catch 'exit
  3941. (while (< (setq n (1+ n)) (length s))
  3942. (setq c (aref s n))
  3943. (cond ((= c ?\ ) (setq i (1+ i)))
  3944. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  3945. (t (throw 'exit t)))))
  3946. i))
  3947. (defun org-renumber-ordered-list (arg)
  3948. "Renumber an ordered plain list.
  3949. Cursor needs to be in the first line of an item, the line that starts
  3950. with something like \"1.\" or \"2)\"."
  3951. (interactive "p")
  3952. (unless (and (org-at-item-p)
  3953. (match-beginning 3))
  3954. (error "This is not an ordered list"))
  3955. (let ((line (org-current-line))
  3956. (col (current-column))
  3957. (ind (org-get-string-indentation
  3958. (buffer-substring (point-at-bol) (match-beginning 3))))
  3959. ;; (term (substring (match-string 3) -1))
  3960. ind1 (n (1- arg)))
  3961. ;; find where this list begins
  3962. (catch 'exit
  3963. (while t
  3964. (catch 'next
  3965. (beginning-of-line 0)
  3966. (if (looking-at "[ \t]*$") (throw 'next t))
  3967. (skip-chars-forward " \t") (setq ind1 (current-column))
  3968. (if (or (< ind1 ind)
  3969. (and (= ind1 ind)
  3970. (not (org-at-item-p))))
  3971. (throw 'exit t)))))
  3972. ;; Walk forward and replace these numbers
  3973. (catch 'exit
  3974. (while t
  3975. (catch 'next
  3976. (beginning-of-line 2)
  3977. (if (eobp) (throw 'exit nil))
  3978. (if (looking-at "[ \t]*$") (throw 'next nil))
  3979. (skip-chars-forward " \t") (setq ind1 (current-column))
  3980. (if (> ind1 ind) (throw 'next t))
  3981. (if (< ind1 ind) (throw 'exit t))
  3982. (if (not (org-at-item-p)) (throw 'exit nil))
  3983. (if (not (match-beginning 3))
  3984. (error "unordered bullet in ordered list. Press \\[undo] to recover"))
  3985. (delete-region (match-beginning 3) (1- (match-end 3)))
  3986. (goto-char (match-beginning 3))
  3987. (insert (format "%d" (setq n (1+ n)))))))
  3988. (goto-line line)
  3989. (move-to-column col)))
  3990. (defvar org-last-indent-begin-marker (make-marker))
  3991. (defvar org-last-indent-end-marker (make-marker))
  3992. (defun org-outdent-item (arg)
  3993. "Outdent a local list item."
  3994. (interactive "p")
  3995. (org-indent-item (- arg)))
  3996. (defun org-indent-item (arg)
  3997. "Indent a local list item."
  3998. (interactive "p")
  3999. (unless (org-at-item-p)
  4000. (error "Not on an item"))
  4001. (save-excursion
  4002. (let (beg end ind ind1)
  4003. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  4004. (setq beg org-last-indent-begin-marker
  4005. end org-last-indent-end-marker)
  4006. (org-beginning-of-item)
  4007. (setq beg (move-marker org-last-indent-begin-marker (point)))
  4008. (org-end-of-item)
  4009. (setq end (move-marker org-last-indent-end-marker (point))))
  4010. (goto-char beg)
  4011. (skip-chars-forward " \t") (setq ind (current-column))
  4012. (if (< (+ arg ind) 0) (error "Cannot outdent beyond margin"))
  4013. (while (< (point) end)
  4014. (beginning-of-line 1)
  4015. (skip-chars-forward " \t") (setq ind1 (current-column))
  4016. (delete-region (point-at-bol) (point))
  4017. (indent-to-column (+ ind1 arg))
  4018. (beginning-of-line 2)))))
  4019. ;;; Archiving
  4020. (defun org-archive-subtree (&optional find-done)
  4021. "Move the current subtree to the archive.
  4022. The archive can be a certain top-level heading in the current file, or in
  4023. a different file. The tree will be moved to that location, the subtree
  4024. heading be marked DONE, and the current time will be added.
  4025. When called with prefix argument FIND-DONE, find whole trees without any
  4026. open TODO items and archive them (after getting confirmation from the user).
  4027. If the cursor is not at a headline when this comand is called, try all level
  4028. 1 trees. If the cursor is on a headline, only try the direct children of
  4029. this heading. "
  4030. (interactive "P")
  4031. (if find-done
  4032. (org-archive-all-done)
  4033. ;; Save all relevant TODO keyword-relatex variables
  4034. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  4035. (tr-org-todo-keywords org-todo-keywords)
  4036. (tr-org-todo-interpretation org-todo-interpretation)
  4037. (tr-org-done-string org-done-string)
  4038. (tr-org-todo-regexp org-todo-regexp)
  4039. (tr-org-todo-line-regexp org-todo-line-regexp)
  4040. (this-buffer (current-buffer))
  4041. file heading buffer level newfile-p)
  4042. (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
  4043. (progn
  4044. (setq file (format (match-string 1 org-archive-location)
  4045. (file-name-nondirectory buffer-file-name))
  4046. heading (match-string 2 org-archive-location)))
  4047. (error "Invalid `org-archive-location'"))
  4048. (if (> (length file) 0)
  4049. (setq newfile-p (not (file-exists-p file))
  4050. buffer (find-file-noselect file))
  4051. (setq buffer (current-buffer)))
  4052. (unless buffer
  4053. (error "Cannot access file \"%s\"" file))
  4054. (if (and (> (length heading) 0)
  4055. (string-match "^\\*+" heading))
  4056. (setq level (match-end 0))
  4057. (setq heading nil level 0))
  4058. (save-excursion
  4059. ;; We first only copy, in case something goes wrong
  4060. ;; we need to protect this-command, to avoid kill-region sets it,
  4061. ;; which would lead to duplication of subtrees
  4062. (let (this-command) (org-copy-subtree))
  4063. (set-buffer buffer)
  4064. ;; Enforce org-mode for the archive buffer
  4065. (if (not (org-mode-p))
  4066. ;; Force the mode for future visits.
  4067. (let ((org-insert-mode-line-in-empty-file t))
  4068. (call-interactively 'org-mode)))
  4069. (when newfile-p
  4070. (goto-char (point-max))
  4071. (insert (format "\nArchived entries from file %s\n\n"
  4072. (buffer-file-name this-buffer))))
  4073. ;; Force the TODO keywords of the original buffer
  4074. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  4075. (org-todo-keywords tr-org-todo-keywords)
  4076. (org-todo-interpretation tr-org-todo-interpretation)
  4077. (org-done-string tr-org-done-string)
  4078. (org-todo-regexp tr-org-todo-regexp)
  4079. (org-todo-line-regexp tr-org-todo-line-regexp))
  4080. (goto-char (point-min))
  4081. (if heading
  4082. (progn
  4083. (if (re-search-forward
  4084. (concat "\\(^\\|\r\\)"
  4085. (regexp-quote heading) "[ \t]*\\($\\|\r\\)")
  4086. nil t)
  4087. (goto-char (match-end 0))
  4088. ;; Heading not found, just insert it at the end
  4089. (goto-char (point-max))
  4090. (or (bolp) (insert "\n"))
  4091. (insert "\n" heading "\n")
  4092. (end-of-line 0))
  4093. ;; Make the subtree visible
  4094. (show-subtree)
  4095. (org-end-of-subtree t)
  4096. (skip-chars-backward " \t\r\n]")
  4097. (and (looking-at "[ \t\r\n]*")
  4098. (replace-match "\n\n")))
  4099. ;; No specific heading, just go to end of file.
  4100. (goto-char (point-max)) (insert "\n"))
  4101. ;; Paste
  4102. (org-paste-subtree (1+ level))
  4103. ;; Mark the entry as done, i.e. set to last work in org-todo-keywords
  4104. (if org-archive-mark-done
  4105. (org-todo (length org-todo-keywords)))
  4106. ;; Move cursor to right after the TODO keyword
  4107. (when org-archive-stamp-time
  4108. (beginning-of-line 1)
  4109. (looking-at org-todo-line-regexp)
  4110. (goto-char (or (match-end 2) (match-beginning 3)))
  4111. (insert "(" (format-time-string (cdr org-time-stamp-formats)
  4112. (org-current-time))
  4113. ")"))
  4114. ;; Save the buffer, if it is not the same buffer.
  4115. (if (not (eq this-buffer buffer)) (save-buffer))))
  4116. ;; Here we are back in the original buffer. Everything seems to have
  4117. ;; worked. So now cut the tree and finish up.
  4118. (let (this-command) (org-cut-subtree))
  4119. (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
  4120. (message "Subtree archived %s"
  4121. (if (eq this-buffer buffer)
  4122. (concat "under heading: " heading)
  4123. (concat "in file: " (abbreviate-file-name file)))))))
  4124. (defun org-archive-all-done (&optional tag)
  4125. "Archive sublevels of the current tree without open TODO items.
  4126. If the cursor is not on a headline, try all level 1 trees. If
  4127. it is on a headline, try all direct children.
  4128. When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
  4129. (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
  4130. (rea (concat ".*:" org-archive-tag ":"))
  4131. (begm (make-marker))
  4132. (endm (make-marker))
  4133. (question (if tag "Set ARCHIVE tag (no open TODO items)? "
  4134. "Move subtree to archive (no open TODO items)? "))
  4135. beg end (cntarch 0))
  4136. (if (org-on-heading-p)
  4137. (progn
  4138. (setq re1 (concat "^" (regexp-quote
  4139. (make-string
  4140. (1+ (- (match-end 0) (match-beginning 0)))
  4141. ?*))
  4142. " "))
  4143. (move-marker begm (point))
  4144. (move-marker endm (org-end-of-subtree)))
  4145. (setq re1 "^* ")
  4146. (move-marker begm (point-min))
  4147. (move-marker endm (point-max)))
  4148. (save-excursion
  4149. (goto-char begm)
  4150. (while (re-search-forward re1 endm t)
  4151. (setq beg (match-beginning 0)
  4152. end (save-excursion (org-end-of-subtree t) (point)))
  4153. (goto-char beg)
  4154. (if (re-search-forward re end t)
  4155. (goto-char end)
  4156. (goto-char beg)
  4157. (if (and (or (not tag) (not (looking-at rea)))
  4158. (y-or-n-p question))
  4159. (progn
  4160. (if tag
  4161. (org-toggle-tag org-archive-tag 'on)
  4162. (org-archive-subtree))
  4163. (setq cntarch (1+ cntarch)))
  4164. (goto-char end)))))
  4165. (message "%d trees archived" cntarch)))
  4166. (defun org-cycle-hide-archived-subtrees (state)
  4167. "Re-hide all archived subtrees after a visibility state change."
  4168. (when (and (not org-cycle-open-archived-trees)
  4169. (not (memq state '(overview folded))))
  4170. (save-excursion
  4171. (let* ((globalp (memq state '(contents all)))
  4172. (beg (if globalp (point-min) (point)))
  4173. (end (if globalp (point-max) (org-end-of-subtree))))
  4174. (org-hide-archived-subtrees beg end)))))
  4175. (defun org-hide-archived-subtrees (beg end)
  4176. "Re-hide all archived subtrees after a visibility state change."
  4177. (save-excursion
  4178. (let* ((re (concat ":" org-archive-tag ":")))
  4179. (goto-char beg)
  4180. (while (re-search-forward re end t)
  4181. (and (org-on-heading-p) (hide-subtree))
  4182. (org-end-of-subtree)))))
  4183. (defun org-toggle-tag (tag &optional onoff)
  4184. "Toggle the tag TAG for the current line.
  4185. If ONOFF is `on' or `off', don't toggle but set to this state."
  4186. (unless (org-on-heading-p) (error "Not on headling"))
  4187. (let (res current)
  4188. (save-excursion
  4189. (beginning-of-line)
  4190. (if (re-search-forward "[ \t]:\\([a-zA-Z0-9_@:]+\\):[ \t]*$"
  4191. (point-at-eol) t)
  4192. (progn
  4193. (setq current (match-string 1))
  4194. (replace-match ""))
  4195. (setq current ""))
  4196. (setq current (nreverse (org-split-string current ":")))
  4197. (cond
  4198. ((eq onoff 'on)
  4199. (setq res t)
  4200. (or (member tag current) (push tag current)))
  4201. ((eq onoff 'off)
  4202. (or (not (member tag current)) (setq current (delete tag current))))
  4203. (t (if (member tag current)
  4204. (setq current (delete tag current))
  4205. (setq res t)
  4206. (push tag current))))
  4207. (end-of-line 1)
  4208. (when current
  4209. (insert " :" (mapconcat 'identity (nreverse current) ":") ":"))
  4210. (org-set-tags nil t))
  4211. res))
  4212. (defun org-toggle-archive-tag (&optional arg)
  4213. "Toggle the archive tag for the current headline.
  4214. With prefix ARG, check all children of current headline and offer tagging
  4215. the children that do not contain any open TODO items."
  4216. (interactive "P")
  4217. (if arg
  4218. (org-archive-all-done 'tag)
  4219. (let (set)
  4220. (save-excursion
  4221. (org-back-to-heading t)
  4222. (setq set (org-toggle-tag org-archive-tag))
  4223. (when set (hide-subtree)))
  4224. (and set (beginning-of-line 1))
  4225. (message "Subtree %s" (if set "archived" "unarchived")))))
  4226. (defun org-prepare-agenda-buffers (files)
  4227. "Create buffers for all agenda files, protect archived trees and comments."
  4228. (interactive)
  4229. (let ((pa '(:org-archived t))
  4230. (pc '(:org-comment t))
  4231. (pall '(:org-archived t :org-comment t))
  4232. (rea (concat ":" org-archive-tag ":"))
  4233. bmp file re)
  4234. (save-excursion
  4235. (while (setq file (pop files))
  4236. (org-check-agenda-file file)
  4237. (set-buffer (org-get-agenda-file-buffer file))
  4238. (widen)
  4239. (setq bmp (buffer-modified-p))
  4240. (save-excursion
  4241. (remove-text-properties (point-min) (point-max) pall)
  4242. (when org-agenda-skip-archived-trees
  4243. (goto-char (point-min))
  4244. (while (re-search-forward rea nil t)
  4245. (if (org-on-heading-p)
  4246. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  4247. (goto-char (point-min))
  4248. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  4249. (while (re-search-forward re nil t)
  4250. (add-text-properties
  4251. (match-beginning 0) (org-end-of-subtree t) pc)))
  4252. (set-buffer-modified-p bmp)))))
  4253. (defun org-agenda-skip ()
  4254. "Throw to `:skip' in places that should be skipped."
  4255. (let ((p (point-at-bol)))
  4256. (and org-agenda-skip-archived-trees
  4257. (get-text-property p :org-archived)
  4258. (org-end-of-subtree)
  4259. (throw :skip t))
  4260. (and (get-text-property p :org-comment)
  4261. (org-end-of-subtree)
  4262. (throw :skip t))
  4263. (if (equal (char-after p) ?#) (throw :skip t))))
  4264. (defun org-agenda-toggle-archive-tag ()
  4265. "Toggle the archive tag for the current entry."
  4266. (interactive)
  4267. (org-agenda-check-no-diary)
  4268. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  4269. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  4270. (org-agenda-error)))
  4271. (buffer (marker-buffer hdmarker))
  4272. (pos (marker-position hdmarker))
  4273. (buffer-read-only nil)
  4274. newhead)
  4275. (with-current-buffer buffer
  4276. (widen)
  4277. (goto-char pos)
  4278. (org-show-hidden-entry)
  4279. (save-excursion
  4280. (and (outline-next-heading)
  4281. (org-flag-heading nil))) ; show the next heading
  4282. (call-interactively 'org-toggle-archive-tag)
  4283. (end-of-line 1)
  4284. (setq newhead (org-get-heading)))
  4285. (org-agenda-change-all-lines newhead hdmarker)
  4286. (beginning-of-line 1)))
  4287. ;;; Dynamic blocks
  4288. (defun org-find-dblock (name)
  4289. "Find the first dynamic block with name NAME in the buffer.
  4290. If not found, stay at current position and return nil."
  4291. (let (pos)
  4292. (save-excursion
  4293. (goto-char (point-min))
  4294. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  4295. nil t)
  4296. (match-beginning 0))))
  4297. (if pos (goto-char pos))
  4298. pos))
  4299. (defconst org-dblock-start-re
  4300. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  4301. "Matches the startline of a dynamic block, with parameters.")
  4302. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  4303. "Matches the end of a dyhamic block.")
  4304. (defun org-create-dblock (plist)
  4305. "Create a dynamic block section, with parameters taken from PLIST.
  4306. PLIST must containe a :name entry which is used as name of the block."
  4307. (unless (bolp) (newline))
  4308. (let ((name (plist-get plist :name)))
  4309. (insert "#+BEGIN: " name)
  4310. (while plist
  4311. (if (eq (car plist) :name)
  4312. (setq plist (cddr plist))
  4313. (insert " " (prin1-to-string (pop plist)))))
  4314. (insert "\n\n#+END:\n")
  4315. (beginning-of-line -2)))
  4316. (defun org-prepare-dblock ()
  4317. "Prepare dynamic block for refresh.
  4318. This empties the block, puts the cursor at the insert position and returns
  4319. the property list including an extra property :name with the block name."
  4320. (unless (looking-at org-dblock-start-re)
  4321. (error "Not at a dynamic block"))
  4322. (let* ((begdel (1+ (match-end 0)))
  4323. (name (match-string 1))
  4324. (params (append (list :name name)
  4325. (read (concat "(" (match-string 3) ")")))))
  4326. (unless (re-search-forward org-dblock-end-re nil t)
  4327. (error "Dynamic block not terminated"))
  4328. (delete-region begdel (match-beginning 0))
  4329. (goto-char begdel)
  4330. (open-line 1)
  4331. params))
  4332. (defun org-map-dblocks (&optional command)
  4333. "Apply COMMAND to all dynamic blocks in the current buffer.
  4334. If COMMAND is not given, use `org-update-dblock'."
  4335. (let ((cmd (or command 'org-update-dblock))
  4336. pos)
  4337. (save-excursion
  4338. (goto-char (point-min))
  4339. (while (re-search-forward org-dblock-start-re nil t)
  4340. (goto-char (setq pos (match-beginning 0)))
  4341. (condition-case nil
  4342. (funcall cmd)
  4343. (error (message "Error during update of dynamic block")))
  4344. (goto-char pos)
  4345. (unless (re-search-forward org-dblock-end-re nil t)
  4346. (error "Dynamic block not terminated"))))))
  4347. (defun org-dblock-update (&optional arg)
  4348. "User command for updating dynamic blocks.
  4349. Update the dynamic block at point. With prefix ARG, update all dynamic
  4350. blocks in the buffer."
  4351. (interactive "P")
  4352. (if arg
  4353. (org-update-all-dblocks)
  4354. (or (looking-at org-dblock-start-re)
  4355. (org-beginning-of-dblock))
  4356. (org-update-dblock)))
  4357. (defun org-update-dblock ()
  4358. "Update the dynamic block at point
  4359. This means to empty the block, parse for parameters and then call
  4360. the correct writing function."
  4361. (let* ((pos (point))
  4362. (params (org-prepare-dblock))
  4363. (name (plist-get params :name))
  4364. (cmd (intern (concat "org-dblock-write:" name))))
  4365. (funcall cmd params)
  4366. (goto-char pos)))
  4367. (defun org-beginning-of-dblock ()
  4368. "Find the beginning of the dynamic block at point.
  4369. Error if there is no scuh block at point."
  4370. (let ((pos (point))
  4371. beg)
  4372. (end-of-line 1)
  4373. (if (and (re-search-backward org-dblock-start-re nil t)
  4374. (setq beg (match-beginning 0))
  4375. (re-search-forward org-dblock-end-re nil t)
  4376. (> (match-end 0) pos))
  4377. (goto-char beg)
  4378. (goto-char pos)
  4379. (error "Not in a dynamic block"))))
  4380. (defun org-update-all-dblocks ()
  4381. "Update all dynamic blocks in the buffer.
  4382. This function can be used in a hook."
  4383. (when (org-mode-p)
  4384. (org-map-dblocks 'org-update-dblock)))
  4385. ;;; Completion
  4386. (defun org-complete (&optional arg)
  4387. "Perform completion on word at point.
  4388. At the beginning of a headline, this completes TODO keywords as given in
  4389. `org-todo-keywords'.
  4390. If the current word is preceded by a backslash, completes the TeX symbols
  4391. that are supported for HTML support.
  4392. If the current word is preceded by \"#+\", completes special words for
  4393. setting file options.
  4394. At all other locations, this simply calls `ispell-complete-word'."
  4395. (interactive "P")
  4396. (catch 'exit
  4397. (let* ((end (point))
  4398. (beg1 (save-excursion
  4399. (if (equal (char-before (point)) ?\ ) (backward-char 1))
  4400. (skip-chars-backward "a-zA-Z_@0-9")
  4401. (point)))
  4402. (beg (save-excursion
  4403. (if (equal (char-before (point)) ?\ ) (backward-char 1))
  4404. (skip-chars-backward "a-zA-Z0-9_:$")
  4405. (point)))
  4406. (confirm (lambda (x) (stringp (car x))))
  4407. (camel (equal (char-before beg) ?*))
  4408. (tag (equal (char-before beg1) ?:))
  4409. (texp (equal (char-before beg) ?\\))
  4410. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  4411. beg)
  4412. "#+"))
  4413. (completion-ignore-case opt)
  4414. (type nil)
  4415. (tbl nil)
  4416. (table (cond
  4417. (opt
  4418. (setq type :opt)
  4419. (mapcar (lambda (x)
  4420. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  4421. (cons (match-string 2 x) (match-string 1 x)))
  4422. (org-split-string (org-get-current-options) "\n")))
  4423. (texp
  4424. (setq type :tex)
  4425. org-html-entities)
  4426. ((string-match "\\`\\*+[ \t]*\\'"
  4427. (buffer-substring (point-at-bol) beg))
  4428. (setq type :todo)
  4429. (mapcar 'list org-todo-keywords))
  4430. (camel
  4431. (setq type :camel)
  4432. (save-excursion
  4433. (goto-char (point-min))
  4434. (while (re-search-forward org-todo-line-regexp nil t)
  4435. (push (list
  4436. (if org-file-link-context-use-camel-case
  4437. (org-make-org-heading-camel (match-string 3) t)
  4438. (org-make-org-heading-search-string
  4439. (match-string 3) t)))
  4440. tbl)))
  4441. tbl)
  4442. (tag (setq type :tag beg beg1)
  4443. (or org-tag-alist (org-get-buffer-tags)))
  4444. (t (progn (ispell-complete-word arg) (throw 'exit nil)))))
  4445. (pattern (buffer-substring-no-properties beg end))
  4446. (completion (try-completion pattern table confirm)))
  4447. (cond ((eq completion t)
  4448. (if (equal type :opt)
  4449. (insert (substring (cdr (assoc (upcase pattern) table))
  4450. (length pattern)))))
  4451. ((null completion)
  4452. (message "Can't find completion for \"%s\"" pattern)
  4453. (ding))
  4454. ((not (string= pattern completion))
  4455. (delete-region beg end)
  4456. (if (string-match " +$" completion)
  4457. (setq completion (replace-match "" t t completion)))
  4458. (insert completion)
  4459. (if (get-buffer-window "*Completions*")
  4460. (delete-window (get-buffer-window "*Completions*")))
  4461. (if (assoc completion table)
  4462. (if (eq type :todo) (insert " ")
  4463. (if (eq type :tag) (insert ":"))))
  4464. (if (and (equal type :opt) (assoc completion table))
  4465. (message "%s" (substitute-command-keys
  4466. "Press \\[org-complete] again to insert example settings"))))
  4467. (t
  4468. (message "Making completion list...")
  4469. (let ((list (sort (all-completions pattern table confirm)
  4470. 'string<)))
  4471. (with-output-to-temp-buffer "*Completions*"
  4472. (condition-case nil
  4473. ;; Protection needed for XEmacs and emacs 21
  4474. (display-completion-list list pattern)
  4475. (error (display-completion-list list)))))
  4476. (message "Making completion list...%s" "done"))))))
  4477. ;;; Comments, TODO and DEADLINE
  4478. (defun org-toggle-comment ()
  4479. "Change the COMMENT state of an entry."
  4480. (interactive)
  4481. (save-excursion
  4482. (org-back-to-heading)
  4483. (if (looking-at (concat outline-regexp
  4484. "\\( +\\<" org-comment-string "\\>\\)"))
  4485. (replace-match "" t t nil 1)
  4486. (if (looking-at outline-regexp)
  4487. (progn
  4488. (goto-char (match-end 0))
  4489. (insert " " org-comment-string))))))
  4490. (defvar org-last-todo-state-is-todo nil
  4491. "This is non-nil when the last TODO state change led to a TODO state.
  4492. If the last change removed the TODO tag or switched to DONE, then
  4493. this is nil.")
  4494. (defun org-todo (&optional arg)
  4495. "Change the TODO state of an item.
  4496. The state of an item is given by a keyword at the start of the heading,
  4497. like
  4498. *** TODO Write paper
  4499. *** DONE Call mom
  4500. The different keywords are specified in the variable `org-todo-keywords'.
  4501. By default the available states are \"TODO\" and \"DONE\".
  4502. So for this example: when the item starts with TODO, it is changed to DONE.
  4503. When it starts with DONE, the DONE is removed. And when neither TODO nor
  4504. DONE are present, add TODO at the beginning of the heading.
  4505. With prefix arg, use completion to determine the new state. With numeric
  4506. prefix arg, switch to that state."
  4507. (interactive "P")
  4508. (save-excursion
  4509. (org-back-to-heading)
  4510. (if (looking-at outline-regexp) (goto-char (match-end 0)))
  4511. (or (looking-at (concat " +" org-todo-regexp " *"))
  4512. (looking-at " *"))
  4513. (let* ((this (match-string 1))
  4514. (completion-ignore-case t)
  4515. (member (member this org-todo-keywords))
  4516. (tail (cdr member))
  4517. (state (cond
  4518. ((equal arg '(4))
  4519. ;; Read a state with completion
  4520. (completing-read "State: " (mapcar (lambda(x) (list x))
  4521. org-todo-keywords)
  4522. nil t))
  4523. ((eq arg 'right)
  4524. (if this
  4525. (if tail (car tail) nil)
  4526. (car org-todo-keywords)))
  4527. ((eq arg 'left)
  4528. (if (equal member org-todo-keywords)
  4529. nil
  4530. (if this
  4531. (nth (- (length org-todo-keywords) (length tail) 2)
  4532. org-todo-keywords)
  4533. org-done-string)))
  4534. (arg
  4535. ;; user requests a specific state
  4536. (nth (1- (prefix-numeric-value arg))
  4537. org-todo-keywords))
  4538. ((null member) (car org-todo-keywords))
  4539. ((null tail) nil) ;; -> first entry
  4540. ((eq org-todo-interpretation 'sequence)
  4541. (car tail))
  4542. ((memq org-todo-interpretation '(type priority))
  4543. (if (eq this-command last-command)
  4544. (car tail)
  4545. (if (> (length tail) 0) org-done-string nil)))
  4546. (t nil)))
  4547. (next (if state (concat " " state " ") " ")))
  4548. (replace-match next t t)
  4549. (setq org-last-todo-state-is-todo
  4550. (not (equal state org-done-string)))
  4551. (when org-log-done
  4552. (if (equal state org-done-string)
  4553. (org-add-planning-info 'closed (current-time) 'scheduled)
  4554. (if (not this)
  4555. (org-add-planning-info nil nil 'closed))))
  4556. ;; Fixup tag positioning
  4557. (and org-auto-align-tags (org-set-tags nil t))
  4558. (run-hooks 'org-after-todo-state-change-hook)))
  4559. ;; Fixup cursor location if close to the keyword
  4560. (if (and (outline-on-heading-p)
  4561. (not (bolp))
  4562. (save-excursion (beginning-of-line 1)
  4563. (looking-at org-todo-line-regexp))
  4564. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  4565. (progn
  4566. (goto-char (or (match-end 2) (match-end 1)))
  4567. (just-one-space))))
  4568. (defun org-log-done (&optional undone)
  4569. "Add a time stamp logging that a TODO entry has been closed.
  4570. When UNDONE is non-nil, remove such a time stamp again."
  4571. (interactive)
  4572. (let (beg end col)
  4573. (save-excursion
  4574. (org-back-to-heading t)
  4575. (setq beg (point))
  4576. (looking-at (concat outline-regexp " *"))
  4577. (goto-char (match-end 0))
  4578. (setq col (current-column))
  4579. (outline-next-heading)
  4580. (setq end (point))
  4581. (goto-char beg)
  4582. (when (re-search-forward (concat
  4583. "[\r\n]\\([ \t]*"
  4584. (regexp-quote org-closed-string)
  4585. " *\\[.*?\\][^\n\r]*[\n\r]?\\)") end t)
  4586. (delete-region (match-beginning 1) (match-end 1)))
  4587. (unless undone
  4588. (org-back-to-heading t)
  4589. (skip-chars-forward "^\n\r")
  4590. (goto-char (min (1+ (point)) (point-max)))
  4591. (when (not (member (char-before) '(?\r ?\n)))
  4592. (insert "\n"))
  4593. (indent-to col)
  4594. (insert org-closed-string " "
  4595. (format-time-string
  4596. (concat "[" (substring (cdr org-time-stamp-formats) 1 -1) "]")
  4597. (org-current-time))
  4598. "\n")))))
  4599. (defun org-show-todo-tree (arg)
  4600. "Make a compact tree which shows all headlines marked with TODO.
  4601. The tree will show the lines where the regexp matches, and all higher
  4602. headlines above the match.
  4603. With \\[universal-argument] prefix, also show the DONE entries.
  4604. With a numeric prefix N, construct a sparse tree for the Nth element
  4605. of `org-todo-keywords'."
  4606. (interactive "P")
  4607. (let ((case-fold-search nil)
  4608. (kwd-re
  4609. (cond ((null arg) org-not-done-regexp)
  4610. ((equal arg '(4)) org-todo-regexp)
  4611. ((<= (prefix-numeric-value arg) (length org-todo-keywords))
  4612. (regexp-quote (nth (1- (prefix-numeric-value arg))
  4613. org-todo-keywords)))
  4614. (t (error "Invalid prefix argument: %s" arg)))))
  4615. (message "%d TODO entries found"
  4616. (org-occur (concat "^" outline-regexp " +" kwd-re )))))
  4617. (defun org-deadline ()
  4618. "Insert the DEADLINE: string to make a deadline.
  4619. A timestamp is also inserted - use \\[org-timestamp-up] and \\[org-timestamp-down]
  4620. to modify it to the correct date."
  4621. (interactive)
  4622. (org-add-planning-info 'deadline nil 'closed))
  4623. (defun org-schedule ()
  4624. "Insert the SCHEDULED: string to schedule a TODO item.
  4625. A timestamp is also inserted - use \\[org-timestamp-up] and \\[org-timestamp-down]
  4626. to modify it to the correct date."
  4627. (interactive)
  4628. (org-add-planning-info 'scheduled nil 'closed))
  4629. (defun org-add-planning-info (what &optional time &rest remove)
  4630. "Insert new timestamp with keyword in the line directly after the headline.
  4631. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  4632. If non is given, the user is prompted for a date.
  4633. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  4634. be removed."
  4635. (interactive)
  4636. (when what (setq time (or time (org-read-date nil 'to-time))))
  4637. (when (and org-insert-labeled-timestamps-at-point
  4638. (member what '(scheduled deadline)))
  4639. (insert
  4640. (if (eq what 'scheduled) org-scheduled-string org-deadline-string)
  4641. " "
  4642. (format-time-string (car org-time-stamp-formats) time))
  4643. (setq what nil))
  4644. (save-excursion
  4645. (let (col list elt (buffer-invisibility-spec nil) ts)
  4646. (org-back-to-heading t)
  4647. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  4648. (goto-char (match-end 1))
  4649. (setq col (current-column))
  4650. (goto-char (1+ (match-end 0)))
  4651. (if (and (not (looking-at outline-regexp))
  4652. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  4653. "[^\r\n]*"))
  4654. (not (equal (match-string 1) org-clock-string)))
  4655. (narrow-to-region (match-beginning 0) (match-end 0))
  4656. (insert "\n")
  4657. (backward-char 1)
  4658. (narrow-to-region (point) (point))
  4659. (indent-to-column col))
  4660. ;; Check if we have to remove something.
  4661. (setq list (cons what remove))
  4662. (while list
  4663. (setq elt (pop list))
  4664. (goto-char (point-min))
  4665. (when (or (and (eq elt 'scheduled)
  4666. (re-search-forward org-scheduled-time-regexp nil t))
  4667. (and (eq elt 'deadline)
  4668. (re-search-forward org-deadline-time-regexp nil t))
  4669. (and (eq elt 'closed)
  4670. (re-search-forward org-closed-time-regexp nil t)))
  4671. (replace-match "")
  4672. (if (looking-at " +") (replace-match ""))))
  4673. (goto-char (point-max))
  4674. (when what
  4675. (insert
  4676. (if (not (equal (char-before) ?\ )) " " "")
  4677. (cond ((eq what 'scheduled) org-scheduled-string)
  4678. ((eq what 'deadline) org-deadline-string)
  4679. ((eq what 'closed) org-closed-string))
  4680. " ")
  4681. (insert
  4682. (setq ts
  4683. (format-time-string
  4684. (if (eq what 'closed)
  4685. (concat "[" (substring (cdr org-time-stamp-formats) 1 -1) "]")
  4686. (car org-time-stamp-formats))
  4687. time))))
  4688. (goto-char (point-min))
  4689. (widen)
  4690. (if (looking-at "[ \t]+\r?\n")
  4691. (replace-match ""))
  4692. ts)))
  4693. (defun org-occur (regexp &optional callback)
  4694. "Make a compact tree which shows all matches of REGEXP.
  4695. The tree will show the lines where the regexp matches, and all higher
  4696. headlines above the match. It will also show the heading after the match,
  4697. to make sure editing the matching entry is easy.
  4698. If CALLBACK is non-nil, it is a function which is called to confirm
  4699. that the match should indeed be shown."
  4700. (interactive "sRegexp: ")
  4701. (org-remove-occur-highlights nil nil t)
  4702. (let ((cnt 0))
  4703. (save-excursion
  4704. (goto-char (point-min))
  4705. (org-overview)
  4706. (while (re-search-forward regexp nil t)
  4707. (when (or (not callback)
  4708. (save-match-data (funcall callback)))
  4709. (setq cnt (1+ cnt))
  4710. (org-highlight-new-match (match-beginning 0) (match-end 0))
  4711. (org-show-hierarchy-above))))
  4712. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  4713. nil 'local)
  4714. (unless org-sparse-tree-open-archived-trees
  4715. (org-hide-archived-subtrees (point-min) (point-max)))
  4716. (run-hooks 'org-occur-hook)
  4717. (if (interactive-p)
  4718. (message "%d match(es) for regexp %s" cnt regexp))
  4719. cnt))
  4720. (defun org-show-hierarchy-above ()
  4721. "Make sure point and the headings hierarchy above is visible."
  4722. (catch 'exit
  4723. (if (org-on-heading-p t)
  4724. (org-flag-heading nil) ; only show the heading
  4725. (and (or (org-invisible-p) (org-invisible-p2))
  4726. (org-show-hidden-entry))) ; show entire entry
  4727. (save-excursion
  4728. (and org-show-following-heading
  4729. (outline-next-heading)
  4730. (org-flag-heading nil))) ; show the next heading
  4731. (when org-show-hierarchy-above
  4732. (save-excursion ; show all higher headings
  4733. (while (and (condition-case nil
  4734. (progn (org-up-heading-all 1) t)
  4735. (error nil))
  4736. (not (bobp)))
  4737. (org-flag-heading nil))))))
  4738. ;; Overlay compatibility functions
  4739. (defun org-make-overlay (beg end &optional buffer)
  4740. (if (featurep 'xemacs)
  4741. (make-extent beg end buffer)
  4742. (make-overlay beg end buffer)))
  4743. (defun org-delete-overlay (ovl)
  4744. (if (featurep 'xemacs) (delete-extent ovl) (delete-overlay ovl)))
  4745. (defun org-detatch-overlay (ovl)
  4746. (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
  4747. (defun org-move-overlay (ovl beg end &optional buffer)
  4748. (if (featurep 'xemacs)
  4749. (set-extent-endpoints ovl beg end buffer)
  4750. (move-overlay ovl beg end buffer)))
  4751. (defun org-overlay-put (ovl prop value)
  4752. (if (featurep 'xemacs)
  4753. (set-extent-property ovl prop value)
  4754. (overlay-put ovl prop value)))
  4755. (defun org-overlays-at (pos)
  4756. (if (featurep 'xemacs) (extents-at pos) (overlays-at pos)))
  4757. (defun org-overlay-start (o)
  4758. (if (featurep 'xemacs) (extent-start-position o) (overlay-start o)))
  4759. (defun org-overlay-end (o)
  4760. (if (featurep 'xemacs) (extent-end-position o) (overlay-end o)))
  4761. (defvar org-occur-highlights nil)
  4762. (make-variable-buffer-local 'org-occur-highlights)
  4763. (defun org-highlight-new-match (beg end)
  4764. "Highlight from BEG to END and mark the highlight is an occur headline."
  4765. (let ((ov (org-make-overlay beg end)))
  4766. (org-overlay-put ov 'face 'secondary-selection)
  4767. (push ov org-occur-highlights)))
  4768. (defvar org-inhibit-highlight-removal nil)
  4769. (defun org-remove-occur-highlights (&optional beg end noremove)
  4770. "Remove the occur highlights from the buffer.
  4771. BEG and END are ignored. If NOREMOVE is nil, remove this function
  4772. from the `before-change-functions' in the current buffer."
  4773. (interactive)
  4774. (unless org-inhibit-highlight-removal
  4775. (mapc 'org-delete-overlay org-occur-highlights)
  4776. (setq org-occur-highlights nil)
  4777. (unless noremove
  4778. (remove-hook 'before-change-functions
  4779. 'org-remove-occur-highlights 'local))))
  4780. ;;; Priorities
  4781. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z]\\)\\] ?\\)"
  4782. "Regular expression matching the priority indicator.")
  4783. (defvar org-remove-priority-next-time nil)
  4784. (defun org-priority-up ()
  4785. "Increase the priority of the current item."
  4786. (interactive)
  4787. (org-priority 'up))
  4788. (defun org-priority-down ()
  4789. "Decrease the priority of the current item."
  4790. (interactive)
  4791. (org-priority 'down))
  4792. (defun org-priority (&optional action)
  4793. "Change the priority of an item by ARG.
  4794. ACTION can be set, up, or down."
  4795. (interactive)
  4796. (setq action (or action 'set))
  4797. (let (current new news have remove)
  4798. (save-excursion
  4799. (org-back-to-heading)
  4800. (if (looking-at org-priority-regexp)
  4801. (setq current (string-to-char (match-string 2))
  4802. have t)
  4803. (setq current org-default-priority))
  4804. (cond
  4805. ((eq action 'set)
  4806. (message "Priority A-%c, SPC to remove: " org-lowest-priority)
  4807. (setq new (read-char-exclusive))
  4808. (cond ((equal new ?\ ) (setq remove t))
  4809. ((or (< (upcase new) ?A) (> (upcase new) org-lowest-priority))
  4810. (error "Priority must be between `%c' and `%c'"
  4811. ?A org-lowest-priority))))
  4812. ((eq action 'up)
  4813. (setq new (1- current)))
  4814. ((eq action 'down)
  4815. (setq new (1+ current)))
  4816. (t (error "Invalid action")))
  4817. (setq new (min (max ?A (upcase new)) org-lowest-priority))
  4818. (setq news (format "%c" new))
  4819. (if have
  4820. (if remove
  4821. (replace-match "" t t nil 1)
  4822. (replace-match news t t nil 2))
  4823. (if remove
  4824. (error "No priority cookie found in line")
  4825. (looking-at org-todo-line-regexp)
  4826. (if (match-end 2)
  4827. (progn
  4828. (goto-char (match-end 2))
  4829. (insert " [#" news "]"))
  4830. (goto-char (match-beginning 3))
  4831. (insert "[#" news "] ")))))
  4832. (if remove
  4833. (message "Priority removed")
  4834. (message "Priority of current item set to %s" news))))
  4835. (defun org-get-priority (s)
  4836. "Find priority cookie and return priority."
  4837. (save-match-data
  4838. (if (not (string-match org-priority-regexp s))
  4839. (* 1000 (- org-lowest-priority org-default-priority))
  4840. (* 1000 (- org-lowest-priority
  4841. (string-to-char (match-string 2 s)))))))
  4842. ;;; Timestamps
  4843. (defvar org-last-changed-timestamp nil)
  4844. (defun org-time-stamp (arg)
  4845. "Prompt for a date/time and insert a time stamp.
  4846. If the user specifies a time like HH:MM, or if this command is called
  4847. with a prefix argument, the time stamp will contain date and time.
  4848. Otherwise, only the date will be included. All parts of a date not
  4849. specified by the user will be filled in from the current date/time.
  4850. So if you press just return without typing anything, the time stamp
  4851. will represent the current date/time. If there is already a timestamp
  4852. at the cursor, it will be modified."
  4853. (interactive "P")
  4854. (let ((fmt (if arg (cdr org-time-stamp-formats)
  4855. (car org-time-stamp-formats)))
  4856. (org-time-was-given nil)
  4857. time)
  4858. (cond
  4859. ((and (org-at-timestamp-p)
  4860. (eq last-command 'org-time-stamp)
  4861. (eq this-command 'org-time-stamp))
  4862. (insert "--")
  4863. (setq time (let ((this-command this-command))
  4864. (org-read-date arg 'totime)))
  4865. (if org-time-was-given (setq fmt (cdr org-time-stamp-formats)))
  4866. (insert (format-time-string fmt time)))
  4867. ((org-at-timestamp-p)
  4868. (setq time (let ((this-command this-command))
  4869. (org-read-date arg 'totime)))
  4870. (and (org-at-timestamp-p) (replace-match
  4871. (setq org-last-changed-timestamp
  4872. (format-time-string fmt time))
  4873. t t))
  4874. (message "Timestamp updated"))
  4875. (t
  4876. (setq time (let ((this-command this-command))
  4877. (org-read-date arg 'totime)))
  4878. (if org-time-was-given (setq fmt (cdr org-time-stamp-formats)))
  4879. (insert (format-time-string fmt time))))))
  4880. (defun org-time-stamp-inactive (&optional arg)
  4881. "Insert an inactive time stamp.
  4882. An inactive time stamp is enclosed in square brackets instead of angle
  4883. brackets. It is inactive in the sense that it does not trigger agenda entries,
  4884. does not link to the calendar and cannot be changed with the S-cursor keys.
  4885. So these are more for recording a certain time/date."
  4886. (interactive "P")
  4887. (let ((fmt (if arg (cdr org-time-stamp-formats)
  4888. (car org-time-stamp-formats)))
  4889. (org-time-was-given nil)
  4890. time)
  4891. (setq time (org-read-date arg 'totime))
  4892. (if org-time-was-given (setq fmt (cdr org-time-stamp-formats)))
  4893. (setq fmt (concat "[" (substring fmt 1 -1) "]"))
  4894. (insert (format-time-string fmt time))))
  4895. (defvar org-date-ovl (org-make-overlay 1 1))
  4896. (org-overlay-put org-date-ovl 'face 'org-warning)
  4897. (org-detatch-overlay org-date-ovl)
  4898. (defun org-read-date (&optional with-time to-time)
  4899. "Read a date and make things smooth for the user.
  4900. The prompt will suggest to enter an ISO date, but you can also enter anything
  4901. which will at least partially be understood by `parse-time-string'.
  4902. Unrecognized parts of the date will default to the current day, month, year,
  4903. hour and minute. For example,
  4904. 3-2-5 --> 2003-02-05
  4905. feb 15 --> currentyear-02-15
  4906. sep 12 9 --> 2009-09-12
  4907. 12:45 --> today 12:45
  4908. 22 sept 0:34 --> currentyear-09-22 0:34
  4909. 12 --> currentyear-currentmonth-12
  4910. Fri --> nearest Friday (today or later)
  4911. etc.
  4912. The function understands only English month and weekday abbreviations,
  4913. but this can be configured with the variables `parse-time-months' and
  4914. `parse-time-weekdays'.
  4915. While prompting, a calendar is popped up - you can also select the
  4916. date with the mouse (button 1). The calendar shows a period of three
  4917. months. To scroll it to other months, use the keys `>' and `<'.
  4918. If you don't like the calendar, turn it off with
  4919. \(setq org-popup-calendar-for-date-prompt nil)
  4920. With optional argument TO-TIME, the date will immediately be converted
  4921. to an internal time.
  4922. With an optional argument WITH-TIME, the prompt will suggest to also
  4923. insert a time. Note that when WITH-TIME is not set, you can still
  4924. enter a time, and this function will inform the calling routine about
  4925. this change. The calling routine may then choose to change the format
  4926. used to insert the time stamp into the buffer to include the time."
  4927. (require 'parse-time)
  4928. (let* ((org-time-stamp-rounding-minutes
  4929. (if (equal with-time '(16)) 0 org-time-stamp-rounding-minutes))
  4930. (ct (org-current-time))
  4931. (default-time
  4932. ;; Default time is either today, or, when entering a range,
  4933. ;; the range start.
  4934. (if (save-excursion
  4935. (re-search-backward
  4936. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  4937. (- (point) 20) t))
  4938. (apply
  4939. 'encode-time
  4940. (mapcar (lambda(x) (or x 0))
  4941. (parse-time-string (match-string 1))))
  4942. ct))
  4943. (calendar-move-hook nil)
  4944. (view-diary-entries-initially nil)
  4945. (view-calendar-holidays-initially nil)
  4946. (timestr (format-time-string
  4947. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") default-time))
  4948. (prompt (format "YYYY-MM-DD [%s]: " timestr))
  4949. ans ans1 ans2
  4950. second minute hour day month year tl wday wday1)
  4951. (if org-popup-calendar-for-date-prompt
  4952. (save-excursion
  4953. (save-window-excursion
  4954. (calendar)
  4955. (calendar-forward-day (- (time-to-days default-time)
  4956. (calendar-absolute-from-gregorian
  4957. (calendar-current-date))))
  4958. (org-eval-in-calendar nil)
  4959. (let* ((old-map (current-local-map))
  4960. (map (copy-keymap calendar-mode-map))
  4961. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  4962. (define-key map (kbd "RET") 'org-calendar-select)
  4963. (define-key map (if (featurep 'xemacs) [button1] [mouse-1])
  4964. 'org-calendar-select-mouse)
  4965. (define-key map (if (featurep 'xemacs) [button2] [mouse-2])
  4966. 'org-calendar-select-mouse)
  4967. (define-key minibuffer-local-map [(meta shift left)]
  4968. (lambda () (interactive)
  4969. (org-eval-in-calendar '(calendar-backward-month 1))))
  4970. (define-key minibuffer-local-map [(meta shift right)]
  4971. (lambda () (interactive)
  4972. (org-eval-in-calendar '(calendar-forward-month 1))))
  4973. (define-key minibuffer-local-map [(shift up)]
  4974. (lambda () (interactive)
  4975. (org-eval-in-calendar '(calendar-backward-week 1))))
  4976. (define-key minibuffer-local-map [(shift down)]
  4977. (lambda () (interactive)
  4978. (org-eval-in-calendar '(calendar-forward-week 1))))
  4979. (define-key minibuffer-local-map [(shift left)]
  4980. (lambda () (interactive)
  4981. (org-eval-in-calendar '(calendar-backward-day 1))))
  4982. (define-key minibuffer-local-map [(shift right)]
  4983. (lambda () (interactive)
  4984. (org-eval-in-calendar '(calendar-forward-day 1))))
  4985. (define-key minibuffer-local-map ">"
  4986. (lambda () (interactive)
  4987. (org-eval-in-calendar '(scroll-calendar-left 1))))
  4988. (define-key minibuffer-local-map "<"
  4989. (lambda () (interactive)
  4990. (org-eval-in-calendar '(scroll-calendar-right 1))))
  4991. (unwind-protect
  4992. (progn
  4993. (use-local-map map)
  4994. (setq ans (read-string prompt "" nil nil))
  4995. (if (not (string-match "\\S-" ans)) (setq ans nil))
  4996. (setq ans (or ans1 ans ans2)))
  4997. (use-local-map old-map)))))
  4998. ;; Naked prompt only
  4999. (setq ans (read-string prompt "" nil timestr)))
  5000. (org-detatch-overlay org-date-ovl)
  5001. (if (string-match
  5002. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  5003. (progn
  5004. (setq year (if (match-end 2)
  5005. (string-to-number (match-string 2 ans))
  5006. (string-to-number (format-time-string "%Y")))
  5007. month (string-to-number (match-string 3 ans))
  5008. day (string-to-number (match-string 4 ans)))
  5009. (if (< year 100) (setq year (+ 2000 year)))
  5010. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  5011. t nil ans))))
  5012. (setq tl (parse-time-string ans)
  5013. year (or (nth 5 tl) (string-to-number (format-time-string "%Y" ct)))
  5014. month (or (nth 4 tl) (string-to-number (format-time-string "%m" ct)))
  5015. day (or (nth 3 tl) (string-to-number (format-time-string "%d" ct)))
  5016. hour (or (nth 2 tl) (string-to-number (format-time-string "%H" ct)))
  5017. minute (or (nth 1 tl) (string-to-number (format-time-string "%M" ct)))
  5018. second (or (nth 0 tl) 0)
  5019. wday (nth 6 tl))
  5020. (when (and wday (not (nth 3 tl)))
  5021. ;; Weekday was given, but no day, so pick that day in the week
  5022. ;; on or after the derived date.
  5023. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  5024. (unless (equal wday wday1)
  5025. (setq day (+ day (% (- wday wday1 -7) 7)))))
  5026. (if (and (boundp 'org-time-was-given)
  5027. (nth 2 tl))
  5028. (setq org-time-was-given t))
  5029. (if (< year 100) (setq year (+ 2000 year)))
  5030. (if to-time
  5031. (encode-time second minute hour day month year)
  5032. (if (or (nth 1 tl) (nth 2 tl))
  5033. (format "%04d-%02d-%02d %02d:%02d" year month day hour minute)
  5034. (format "%04d-%02d-%02d" year month day)))))
  5035. (defun org-eval-in-calendar (form)
  5036. "Eval FORM in the calendar window and return to current window.
  5037. Also, store the cursor date in variable ans2."
  5038. (let ((sw (selected-window)))
  5039. (select-window (get-buffer-window "*Calendar*"))
  5040. (eval form)
  5041. (when (calendar-cursor-to-date)
  5042. (let* ((date (calendar-cursor-to-date))
  5043. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  5044. (setq ans2 (format-time-string "%Y-%m-%d" time))))
  5045. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  5046. (select-window sw)))
  5047. (defun org-calendar-select ()
  5048. "Return to `org-read-date' with the date currently selected.
  5049. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  5050. (interactive)
  5051. (when (calendar-cursor-to-date)
  5052. (let* ((date (calendar-cursor-to-date))
  5053. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  5054. (setq ans1 (format-time-string "%Y-%m-%d" time)))
  5055. (if (active-minibuffer-window) (exit-minibuffer))))
  5056. (defun org-calendar-select-mouse (ev)
  5057. "Return to `org-read-date' with the date currently selected.
  5058. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  5059. (interactive "e")
  5060. (mouse-set-point ev)
  5061. (when (calendar-cursor-to-date)
  5062. (let* ((date (calendar-cursor-to-date))
  5063. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  5064. (setq ans1 (format-time-string "%Y-%m-%d" time)))
  5065. (if (active-minibuffer-window) (exit-minibuffer))))
  5066. (defun org-check-deadlines (ndays)
  5067. "Check if there are any deadlines due or past due.
  5068. A deadline is considered due if it happens within `org-deadline-warning-days'
  5069. days from today's date. If the deadline appears in an entry marked DONE,
  5070. it is not shown. The prefix arg NDAYS can be used to test that many
  5071. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  5072. (interactive "P")
  5073. (let* ((org-warn-days
  5074. (cond
  5075. ((equal ndays '(4)) 100000)
  5076. (ndays (prefix-numeric-value ndays))
  5077. (t org-deadline-warning-days)))
  5078. (case-fold-search nil)
  5079. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  5080. (callback
  5081. (lambda ()
  5082. (and (let ((d1 (time-to-days (current-time)))
  5083. (d2 (time-to-days
  5084. (org-time-string-to-time (match-string 1)))))
  5085. (< (- d2 d1) org-warn-days))
  5086. (not (org-entry-is-done-p))))))
  5087. (message "%d deadlines past-due or due within %d days"
  5088. (org-occur regexp callback)
  5089. org-warn-days)))
  5090. (defun org-evaluate-time-range (&optional to-buffer)
  5091. "Evaluate a time range by computing the difference between start and end.
  5092. Normally the result is just printed in the echo area, but with prefix arg
  5093. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  5094. If the time range is actually in a table, the result is inserted into the
  5095. next column.
  5096. For time difference computation, a year is assumed to be exactly 365
  5097. days in order to avoid rounding problems."
  5098. (interactive "P")
  5099. (or
  5100. (org-clock-update-time-maybe)
  5101. (save-excursion
  5102. (unless (org-at-date-range-p)
  5103. (goto-char (point-at-bol))
  5104. (re-search-forward org-tr-regexp (point-at-eol) t))
  5105. (if (not (org-at-date-range-p))
  5106. (error "Not at a time-stamp range, and none found in current line")))
  5107. (let* ((ts1 (match-string 1))
  5108. (ts2 (match-string 2))
  5109. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  5110. (match-end (match-end 0))
  5111. (time1 (org-time-string-to-time ts1))
  5112. (time2 (org-time-string-to-time ts2))
  5113. (t1 (time-to-seconds time1))
  5114. (t2 (time-to-seconds time2))
  5115. (diff (abs (- t2 t1)))
  5116. (negative (< (- t2 t1) 0))
  5117. ;; (ys (floor (* 365 24 60 60)))
  5118. (ds (* 24 60 60))
  5119. (hs (* 60 60))
  5120. (fy "%dy %dd %02d:%02d")
  5121. (fy1 "%dy %dd")
  5122. (fd "%dd %02d:%02d")
  5123. (fd1 "%dd")
  5124. (fh "%02d:%02d")
  5125. y d h m align)
  5126. (if havetime
  5127. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  5128. y 0
  5129. d (floor (/ diff ds)) diff (mod diff ds)
  5130. h (floor (/ diff hs)) diff (mod diff hs)
  5131. m (floor (/ diff 60)))
  5132. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  5133. y 0
  5134. d (floor (+ (/ diff ds) 0.5))
  5135. h 0 m 0))
  5136. (if (not to-buffer)
  5137. (message (org-make-tdiff-string y d h m))
  5138. (when (org-at-table-p)
  5139. (goto-char match-end)
  5140. (setq align t)
  5141. (and (looking-at " *|") (goto-char (match-end 0))))
  5142. (if (looking-at
  5143. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  5144. (replace-match ""))
  5145. (if negative (insert " -"))
  5146. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  5147. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  5148. (insert " " (format fh h m))))
  5149. (if align (org-table-align))
  5150. (message "Time difference inserted")))))
  5151. (defun org-make-tdiff-string (y d h m)
  5152. (let ((fmt "")
  5153. (l nil))
  5154. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  5155. l (push y l)))
  5156. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  5157. l (push d l)))
  5158. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  5159. l (push h l)))
  5160. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  5161. l (push m l)))
  5162. (apply 'format fmt (nreverse l))))
  5163. (defun org-time-string-to-time (s)
  5164. (apply 'encode-time (org-parse-time-string s)))
  5165. (defun org-parse-time-string (s &optional nodefault)
  5166. "Parse the standard Org-mode time string.
  5167. This should be a lot faster than the normal `parse-time-string'.
  5168. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  5169. hour and minute fields will be nil if not given."
  5170. (if (string-match org-ts-regexp1 s)
  5171. (list 0
  5172. (if (or (match-beginning 8) (not nodefault))
  5173. (string-to-number (or (match-string 8 s) "0")))
  5174. (if (or (match-beginning 7) (not nodefault))
  5175. (string-to-number (or (match-string 7 s) "0")))
  5176. (string-to-number (match-string 4 s))
  5177. (string-to-number (match-string 3 s))
  5178. (string-to-number (match-string 2 s))
  5179. nil nil nil)
  5180. (make-list 9 0)))
  5181. (defun org-timestamp-up (&optional arg)
  5182. "Increase the date item at the cursor by one.
  5183. If the cursor is on the year, change the year. If it is on the month or
  5184. the day, change that.
  5185. With prefix ARG, change by that many units."
  5186. (interactive "p")
  5187. (org-timestamp-change (prefix-numeric-value arg)))
  5188. (defun org-timestamp-down (&optional arg)
  5189. "Decrease the date item at the cursor by one.
  5190. If the cursor is on the year, change the year. If it is on the month or
  5191. the day, change that.
  5192. With prefix ARG, change by that many units."
  5193. (interactive "p")
  5194. (org-timestamp-change (- (prefix-numeric-value arg))))
  5195. (defun org-timestamp-up-day (&optional arg)
  5196. "Increase the date in the time stamp by one day.
  5197. With prefix ARG, change that many days."
  5198. (interactive "p")
  5199. (if (and (not (org-at-timestamp-p t))
  5200. (org-on-heading-p))
  5201. (org-todo 'up)
  5202. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  5203. (defun org-timestamp-down-day (&optional arg)
  5204. "Decrease the date in the time stamp by one day.
  5205. With prefix ARG, change that many days."
  5206. (interactive "p")
  5207. (if (and (not (org-at-timestamp-p t))
  5208. (org-on-heading-p))
  5209. (org-todo 'down)
  5210. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  5211. (defsubst org-pos-in-match-range (pos n)
  5212. (and (match-beginning n)
  5213. (<= (match-beginning n) pos)
  5214. (>= (match-end n) pos)))
  5215. (defun org-at-timestamp-p (&optional also-inactive)
  5216. "Determine if the cursor is in or at a timestamp."
  5217. (interactive)
  5218. (let* ((tsr (if also-inactive org-ts-regexp3 org-ts-regexp2))
  5219. (pos (point))
  5220. (ans (or (looking-at tsr)
  5221. (save-excursion
  5222. (skip-chars-backward "^[<\n\r\t")
  5223. (if (> (point) 1) (backward-char 1))
  5224. (and (looking-at tsr)
  5225. (> (- (match-end 0) pos) -1))))))
  5226. (and (boundp 'org-ts-what)
  5227. (setq org-ts-what
  5228. (cond
  5229. ((org-pos-in-match-range pos 2) 'year)
  5230. ((org-pos-in-match-range pos 3) 'month)
  5231. ((org-pos-in-match-range pos 7) 'hour)
  5232. ((org-pos-in-match-range pos 8) 'minute)
  5233. ((or (org-pos-in-match-range pos 4)
  5234. (org-pos-in-match-range pos 5)) 'day)
  5235. (t 'day))))
  5236. ans))
  5237. (defun org-timestamp-change (n &optional what)
  5238. "Change the date in the time stamp at point.
  5239. The date will be changed by N times WHAT. WHAT can be `day', `month',
  5240. `year', `minute', `second'. If WHAT is not given, the cursor position
  5241. in the timestamp determines what will be changed."
  5242. (let ((fmt (car org-time-stamp-formats))
  5243. org-ts-what
  5244. (pos (point))
  5245. ts time time0)
  5246. (if (not (org-at-timestamp-p t))
  5247. (error "Not at a timestamp"))
  5248. (setq org-ts-what (or what org-ts-what))
  5249. (setq fmt (if (<= (abs (- (cdr org-ts-lengths)
  5250. (- (match-end 0) (match-beginning 0))))
  5251. 1)
  5252. (cdr org-time-stamp-formats)
  5253. (car org-time-stamp-formats)))
  5254. (if (= (char-after (match-beginning 0)) ?\[)
  5255. (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  5256. (setq ts (match-string 0))
  5257. (replace-match "")
  5258. (setq time0 (org-parse-time-string ts))
  5259. (setq time
  5260. (apply 'encode-time
  5261. (append
  5262. (list (or (car time0) 0))
  5263. (list (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0)))
  5264. (list (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0)))
  5265. (list (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0)))
  5266. (list (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0)))
  5267. (list (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0)))
  5268. (nthcdr 6 time0))))
  5269. (if (eq what 'calendar)
  5270. (let ((cal-date
  5271. (save-excursion
  5272. (save-match-data
  5273. (set-buffer "*Calendar*")
  5274. (calendar-cursor-to-date)))))
  5275. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  5276. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  5277. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  5278. (setcar time0 (or (car time0) 0))
  5279. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  5280. (setcar (nthcdr 2 time0) (or (nth 1 time0) 0))
  5281. (setq time (apply 'encode-time time0))))
  5282. (insert (setq org-last-changed-timestamp (format-time-string fmt time)))
  5283. (org-clock-update-time-maybe)
  5284. (goto-char pos)
  5285. ;; Try to recenter the calendar window, if any
  5286. (if (and org-calendar-follow-timestamp-change
  5287. (get-buffer-window "*Calendar*" t)
  5288. (memq org-ts-what '(day month year)))
  5289. (org-recenter-calendar (time-to-days time)))))
  5290. (defun org-recenter-calendar (date)
  5291. "If the calendar is visible, recenter it to DATE."
  5292. (let* ((win (selected-window))
  5293. (cwin (get-buffer-window "*Calendar*" t))
  5294. (calendar-move-hook nil))
  5295. (when cwin
  5296. (select-window cwin)
  5297. (calendar-goto-date (if (listp date) date
  5298. (calendar-gregorian-from-absolute date)))
  5299. (select-window win))))
  5300. (defun org-goto-calendar (&optional arg)
  5301. "Go to the Emacs calendar at the current date.
  5302. If there is a time stamp in the current line, go to that date.
  5303. A prefix ARG can be used to force the current date."
  5304. (interactive "P")
  5305. (let ((tsr org-ts-regexp) diff
  5306. (calendar-move-hook nil)
  5307. (view-calendar-holidays-initially nil)
  5308. (view-diary-entries-initially nil))
  5309. (if (or (org-at-timestamp-p)
  5310. (save-excursion
  5311. (beginning-of-line 1)
  5312. (looking-at (concat ".*" tsr))))
  5313. (let ((d1 (time-to-days (current-time)))
  5314. (d2 (time-to-days
  5315. (org-time-string-to-time (match-string 1)))))
  5316. (setq diff (- d2 d1))))
  5317. (calendar)
  5318. (calendar-goto-today)
  5319. (if (and diff (not arg)) (calendar-forward-day diff))))
  5320. (defun org-date-from-calendar ()
  5321. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  5322. If there is already a time stamp at the cursor position, update it."
  5323. (interactive)
  5324. (org-timestamp-change 0 'calendar))
  5325. ;;; The clock for measuring work time.
  5326. (defvar org-clock-marker (make-marker)
  5327. "Marker recording the last clock-in.")
  5328. (defun org-clock-in ()
  5329. "Start the clock on the current item.
  5330. If necessary, clock-out of the currently active clock."
  5331. (interactive)
  5332. (org-clock-out t)
  5333. (let (ts)
  5334. (save-excursion
  5335. (org-back-to-heading t)
  5336. (beginning-of-line 2)
  5337. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  5338. (not (equal (match-string 1) org-clock-string)))
  5339. (beginning-of-line 1))
  5340. (insert "\n") (backward-char 1)
  5341. (indent-relative)
  5342. (insert org-clock-string " "
  5343. (setq ts (concat "[" (format-time-string
  5344. (substring
  5345. (cdr org-time-stamp-formats) 1 -1)
  5346. (current-time))
  5347. "]")))
  5348. (move-marker org-clock-marker (point))
  5349. (message "Clock started at %s" ts))))
  5350. (defun org-clock-out (&optional fail-quietly)
  5351. "Stop the currently running clock.
  5352. If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
  5353. (interactive)
  5354. (catch 'exit
  5355. (if (not (marker-buffer org-clock-marker))
  5356. (if fail-quietly (throw 'exit t) (error "No active clock")))
  5357. (let (ts te s h m)
  5358. (save-excursion
  5359. (set-buffer (marker-buffer org-clock-marker))
  5360. (goto-char org-clock-marker)
  5361. (beginning-of-line 1)
  5362. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  5363. (equal (match-string 1) org-clock-string))
  5364. (setq ts (match-string 2))
  5365. (if fail-quietly (throw 'exit nil) (error "Clock start time is gone")))
  5366. (goto-char org-clock-marker)
  5367. (setq te (concat "[" (format-time-string
  5368. (substring
  5369. (cdr org-time-stamp-formats) 1 -1)
  5370. (current-time))
  5371. "]"))
  5372. (setq s (- (time-to-seconds (apply 'encode-time (org-parse-time-string te)))
  5373. (time-to-seconds (apply 'encode-time (org-parse-time-string ts))))
  5374. h (floor (/ s 3600))
  5375. s (- s (* 3600 h))
  5376. m (floor (/ s 60))
  5377. s (- s (* 60 s)))
  5378. (insert "--" te " => " (format "%2d:%02d" h m))
  5379. (move-marker org-clock-marker nil)
  5380. (message "Clock stopped at %s after HH:MM = %d:%02d" te h m)))))
  5381. (defun org-clock-cancel ()
  5382. "Cancel the running clock be removing the start timestamp."
  5383. (interactive)
  5384. (if (not (marker-buffer org-clock-marker))
  5385. (error "No active clock"))
  5386. (save-excursion
  5387. (set-buffer (marker-buffer org-clock-marker))
  5388. (goto-char org-clock-marker)
  5389. (delete-region (1- (point-at-bol)) (point-at-eol)))
  5390. (message "Clock canceled"))
  5391. (defvar org-clock-file-total-minutes nil
  5392. "Holds the file total time in minutes, after a call to `org-clock-sum'.")
  5393. (make-variable-buffer-local 'org-clock-file-total-minutes)
  5394. (defun org-clock-sum (&optional tstart tend)
  5395. "Sum the times for each subtree.
  5396. Puts the resulting times in minutes as a text property on each headline."
  5397. (interactive)
  5398. (let* ((bmp (buffer-modified-p))
  5399. (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
  5400. org-clock-string
  5401. "[ \t]*\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)"))
  5402. (lmax 30)
  5403. (ltimes (make-vector lmax 0))
  5404. (t1 0)
  5405. (level 0)
  5406. ts te dt
  5407. time)
  5408. (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t))
  5409. (save-excursion
  5410. (goto-char (point-max))
  5411. (while (re-search-backward re nil t)
  5412. (if (match-end 2)
  5413. ;; A time
  5414. (setq ts (match-string 2)
  5415. te (match-string 3)
  5416. ts (time-to-seconds
  5417. (apply 'encode-time (org-parse-time-string ts)))
  5418. te (time-to-seconds
  5419. (apply 'encode-time (org-parse-time-string te)))
  5420. ts (if tstart (max ts tstart) ts)
  5421. te (if tend (min te tend) te)
  5422. dt (- te ts)
  5423. t1 (if (> dt 0) (+ t1 (floor (/ dt 60))) t1))
  5424. ;; A headline
  5425. (setq level (- (match-end 1) (match-beginning 1)))
  5426. (when (or (> t1 0) (> (aref ltimes level) 0))
  5427. (loop for l from 0 to level do
  5428. (aset ltimes l (+ (aref ltimes l) t1)))
  5429. (setq t1 0 time (aref ltimes level))
  5430. (loop for l from level to (1- lmax) do
  5431. (aset ltimes l 0))
  5432. (goto-char (match-beginning 0))
  5433. (put-text-property (point) (point-at-eol) :org-clock-minutes time))))
  5434. (setq org-clock-file-total-minutes (aref ltimes 0)))
  5435. (set-buffer-modified-p bmp)))
  5436. (defun org-clock-display (&optional total-only)
  5437. "Show subtree times in the entire buffer.
  5438. If TOTAL-ONLY is non-nil, only show the total time for the entire file
  5439. in the echo area."
  5440. (interactive)
  5441. (org-remove-clock-overlays)
  5442. (let (time h m p)
  5443. (org-clock-sum)
  5444. (unless total-only
  5445. (save-excursion
  5446. (goto-char (point-min))
  5447. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  5448. (goto-char p)
  5449. (when (setq time (get-text-property p :org-clock-minutes))
  5450. (org-put-clock-overlay time (funcall outline-level))))
  5451. (setq h (/ org-clock-file-total-minutes 60)
  5452. m (- org-clock-file-total-minutes (* 60 h)))
  5453. ;; Arrange to remove the overlays upon next change.
  5454. (org-add-hook 'before-change-functions 'org-remove-clock-overlays
  5455. nil 'local)))
  5456. (message "Total file time: %d:%02d (%d hours and %d minutes)" h m h m)))
  5457. (defvar org-clock-overlays nil)
  5458. (make-variable-buffer-local 'org-clock-overlays)
  5459. (defun org-put-clock-overlay (time &optional level)
  5460. "Put an overlays on the current line, displaying TIME.
  5461. If LEVEL is given, prefix time with a corresponding number of stars.
  5462. This creates a new overlay and stores it in `org-clock-overlays', so that it
  5463. will be easy to remove."
  5464. (let* ((c 60) (h (floor (/ time 60))) (m (- time (* 60 h)))
  5465. (l (if level (org-get-legal-level level 0) 0))
  5466. (off 0)
  5467. ov tx)
  5468. (move-to-column c)
  5469. (unless (eolp) (skip-chars-backward "^ \t"))
  5470. (skip-chars-backward " \t")
  5471. (setq ov (org-make-overlay (1- (point)) (point-at-eol))
  5472. tx (concat (buffer-substring (1- (point)) (point))
  5473. (make-string (+ off (max 0 (- c (current-column)))) ?.)
  5474. (org-add-props (format "%s %2d:%02d%s"
  5475. (make-string l ?*) h m
  5476. (make-string (- 10 l) ?\ ))
  5477. '(face secondary-selection))
  5478. ""))
  5479. (org-overlay-put ov 'display tx)
  5480. (push ov org-clock-overlays)))
  5481. (defun org-remove-clock-overlays (&optional beg end noremove)
  5482. "Remove the occur highlights from the buffer.
  5483. BEG and END are ignored. If NOREMOVE is nil, remove this function
  5484. from the `before-change-functions' in the current buffer."
  5485. (interactive)
  5486. (unless org-inhibit-highlight-removal
  5487. (mapc 'org-delete-overlay org-clock-overlays)
  5488. (setq org-clock-overlays nil)
  5489. (unless noremove
  5490. (remove-hook 'before-change-functions
  5491. 'org-remove-clock-overlays 'local))))
  5492. (defun org-clock-out-if-current ()
  5493. "Clock out if the current entry contains the running clock.
  5494. This is used to stop the clock after a TODO entry is marked DONE."
  5495. (when (and (equal state org-done-string)
  5496. (equal (marker-buffer org-clock-marker) (current-buffer))
  5497. (< (point) org-clock-marker)
  5498. (> (save-excursion (outline-next-heading) (point))
  5499. org-clock-marker))
  5500. (org-clock-out)))
  5501. (add-hook 'org-after-todo-state-change-hook
  5502. 'org-clock-out-if-current)
  5503. (defun org-check-running-clock ()
  5504. "Check if the current buffer contains the running clock.
  5505. If yes, offer to stop it and to save the buffer with the changes."
  5506. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  5507. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  5508. (buffer-name))))
  5509. (org-clock-out)
  5510. (when (y-or-n-p "Save changed buffer?")
  5511. (save-buffer))))
  5512. (defun org-clock-report ()
  5513. "Create a table containing a report about clocked time.
  5514. If the buffer contains lines
  5515. #+BEGIN: clocktable :maxlevel 3 :emphasize nil
  5516. #+END: clocktable
  5517. then the table will be inserted between these lines, replacing whatever
  5518. is was there before. If these lines are not in the buffer, the table
  5519. is inserted at point, surrounded by the special lines.
  5520. The BEGIN line can contain parameters. Allowed are:
  5521. :maxlevel The maximum level to be included in the table. Default is 3.
  5522. :emphasize t/nil, if levell 1 and level 2 should be bold/italic in the table."
  5523. (interactive)
  5524. (org-remove-clock-overlays)
  5525. (unless (org-find-dblock "clocktable")
  5526. (org-create-dblock (list :name "clocktable"
  5527. :maxlevel 2 :emphasize nil)))
  5528. (org-update-dblock))
  5529. (defun org-clock-update-time-maybe ()
  5530. "If this is a CLOCK line, update it and return t.
  5531. Otherwise, return nil."
  5532. (interactive)
  5533. (save-excursion
  5534. (beginning-of-line 1)
  5535. (skip-chars-forward " \t")
  5536. (when (looking-at org-clock-string)
  5537. (let ((re (concat "[ \t]*" org-clock-string
  5538. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  5539. "\\([ \t]*=>.*\\)?"))
  5540. ts te h m s)
  5541. (if (not (looking-at re))
  5542. nil
  5543. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  5544. (end-of-line 1)
  5545. (setq ts (match-string 1)
  5546. te (match-string 2))
  5547. (setq s (- (time-to-seconds
  5548. (apply 'encode-time (org-parse-time-string te)))
  5549. (time-to-seconds
  5550. (apply 'encode-time (org-parse-time-string ts))))
  5551. h (floor (/ s 3600))
  5552. s (- s (* 3600 h))
  5553. m (floor (/ s 60))
  5554. s (- s (* 60 s)))
  5555. (insert " => " (format "%2d:%02d" h m))
  5556. t)))))
  5557. (defun org-clock-special-range (key &optional time as-strings)
  5558. "Return two times bordering a special time range.
  5559. Key is a symbol specifying the range and can be one of `today', `yesterday',
  5560. `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
  5561. A week starts Monday 0:00 and ends Sunday 24:00.
  5562. The range is determined relative to TIME. TIME defaults to the current time.
  5563. The return value is a cons cell with two internal times like the ones
  5564. returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
  5565. the returned times will be formatted strings."
  5566. (let* ((tm (decode-time (or time (current-time))))
  5567. (s 0) (m (nth 1 tm)) (h (nth 2 tm))
  5568. (d (nth 3 tm)) (month (nth 4 tm)) (y (nth 5 tm))
  5569. (dow (nth 6 tm))
  5570. s1 m1 h1 d1 month1 y1 diff ts te fm)
  5571. (cond
  5572. ((eq key 'today)
  5573. (setq h 0 m 0 h1 24 m1 0))
  5574. ((eq key 'yesterday)
  5575. (setq d (1- d) h 0 m 0 h1 24 m1 0))
  5576. ((eq key 'thisweek)
  5577. (setq diff (if (= dow 0) 6 (1- dow))
  5578. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  5579. ((eq key 'lastweek)
  5580. (setq diff (+ 7 (if (= dow 0) 6 (1- dow)))
  5581. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  5582. ((eq key 'thismonth)
  5583. (setq d 1 h 0 m 0 d1 1 month1 (1+ month) h1 0 m1 0))
  5584. ((eq key 'lastmonth)
  5585. (setq d 1 h 0 m 0 d1 1 month (1- month) month1 (1+ month) h1 0 m1 0))
  5586. ((eq key 'thisyear)
  5587. (setq m 0 h 0 d 1 month 1 y1 (1+ y)))
  5588. ((eq key 'lastyear)
  5589. (setq m 0 h 0 d 1 month 1 y (1- y) y1 (1+ y)))
  5590. (t (error "No such time block %s" key)))
  5591. (setq ts (encode-time s m h d month y)
  5592. te (encode-time (or s1 s) (or m1 m) (or h1 h)
  5593. (or d1 d) (or month1 month) (or y1 y)))
  5594. (setq fm (cdr org-time-stamp-formats))
  5595. (if as-strings
  5596. (cons (format-time-string fm ts) (format-time-string fm te))
  5597. (cons ts te))))
  5598. (defun org-dblock-write:clocktable (params)
  5599. "Write the standard clocktable."
  5600. (let ((hlchars '((1 . "*") (2 . ?/)))
  5601. (emph nil)
  5602. (ins (make-marker))
  5603. ipos time h m p level hlc hdl maxlevel
  5604. ts te cc block)
  5605. (setq maxlevel (or (plist-get params :maxlevel) 3)
  5606. emph (plist-get params :emphasize)
  5607. ts (plist-get params :tstart)
  5608. te (plist-get params :tend)
  5609. block (plist-get params :block))
  5610. (when block
  5611. (setq cc (org-clock-special-range block nil t)
  5612. ts (car cc) te (cdr cc)))
  5613. (if ts (setq ts (time-to-seconds
  5614. (apply 'encode-time (org-parse-time-string ts)))))
  5615. (if te (setq te (time-to-seconds
  5616. (apply 'encode-time (org-parse-time-string te)))))
  5617. (move-marker ins (point))
  5618. (setq ipos (point))
  5619. (insert-before-markers "Clock summary at ["
  5620. (substring
  5621. (format-time-string (cdr org-time-stamp-formats))
  5622. 1 -1)
  5623. "]."
  5624. (if block
  5625. (format " Considered range is /%s/." block)
  5626. "")
  5627. "\n\n|L|Headline|Time|\n")
  5628. (org-clock-sum ts te)
  5629. (setq h (/ org-clock-file-total-minutes 60)
  5630. m (- org-clock-file-total-minutes (* 60 h)))
  5631. (insert-before-markers "|-\n|0|" "*Total file time*| "
  5632. (format "*%d:%02d*" h m)
  5633. "|\n")
  5634. (goto-char (point-min))
  5635. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  5636. (goto-char p)
  5637. (when (setq time (get-text-property p :org-clock-minutes))
  5638. (beginning-of-line 1)
  5639. (when (and (looking-at "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[0-9a-zA-Z_@:]+:\\)?[ \t]*$")
  5640. (setq level (- (match-end 1) (match-beginning 1)))
  5641. (<= level maxlevel))
  5642. (setq hlc (if emph (or (cdr (assoc level hlchars)) "") "")
  5643. hdl (match-string 2)
  5644. h (/ time 60)
  5645. m (- time (* 60 h)))
  5646. (save-excursion
  5647. (goto-char ins)
  5648. (if (= level 1) (insert-before-markers "|-\n"))
  5649. (insert-before-markers
  5650. "| " (int-to-string level) "|" hlc hdl hlc " |"
  5651. (make-string (1- level) ?|)
  5652. hlc
  5653. (format "%d:%02d" h m)
  5654. hlc
  5655. " |\n")))))
  5656. (goto-char ins)
  5657. (backward-delete-char 1)
  5658. (goto-char ipos)
  5659. (skip-chars-forward "^|")
  5660. (org-table-align)))
  5661. (defun org-collect-clock-time-entries ()
  5662. "Return an internal list with clocking information.
  5663. This list has one entry for each CLOCK interval.
  5664. FIXME: describe the elements."
  5665. (interactive)
  5666. (let ((re (concat "^[ \t]*" org-clock-string
  5667. " *\\[\\(.*?\\)\\]--\\[\\(.*?\\)\\]"))
  5668. rtn beg end next cont level title total closedp leafp
  5669. clockpos titlepos h m donep)
  5670. (save-excursion
  5671. (org-clock-sum)
  5672. (goto-char (point-min))
  5673. (while (re-search-forward re nil t)
  5674. (setq clockpos (match-beginning 0)
  5675. beg (match-string 1) end (match-string 2)
  5676. cont (match-end 0))
  5677. (setq beg (apply 'encode-time (org-parse-time-string beg))
  5678. end (apply 'encode-time (org-parse-time-string end)))
  5679. (org-back-to-heading t)
  5680. (setq donep (org-entry-is-done-p))
  5681. (setq titlepos (point)
  5682. total (or (get-text-property (1+ (point)) :org-clock-minutes) 0)
  5683. h (/ total 60) m (- total (* 60 h))
  5684. total (cons h m))
  5685. (looking-at "\\(\\*+\\) +\\(.*\\)")
  5686. (setq level (- (match-end 1) (match-beginning 1))
  5687. title (org-match-string-no-properties 2))
  5688. (save-excursion (outline-next-heading) (setq next (point)))
  5689. (setq closedp (re-search-forward org-closed-time-regexp next t))
  5690. (goto-char next)
  5691. (setq leafp (and (looking-at "^\\*+ ")
  5692. (<= (- (match-end 0) (point)) level)))
  5693. (push (list beg end clockpos closedp donep
  5694. total title titlepos level leafp)
  5695. rtn)
  5696. (goto-char cont)))
  5697. (nreverse rtn)))
  5698. ;;; Agenda, and Diary Integration
  5699. ;;; Define the mode
  5700. (defvar org-agenda-mode-map (make-sparse-keymap)
  5701. "Keymap for `org-agenda-mode'.")
  5702. (defvar org-agenda-menu) ; defined later in this file.
  5703. (defvar org-agenda-follow-mode nil)
  5704. (defvar org-agenda-show-log nil)
  5705. (defvar org-agenda-buffer-name "*Org Agenda*")
  5706. (defvar org-agenda-redo-command nil)
  5707. (defvar org-agenda-mode-hook nil)
  5708. (defvar org-agenda-type nil)
  5709. (defvar org-agenda-force-single-file nil)
  5710. (defun org-agenda-mode ()
  5711. "Mode for time-sorted view on action items in Org-mode files.
  5712. The following commands are available:
  5713. \\{org-agenda-mode-map}"
  5714. (interactive)
  5715. (kill-all-local-variables)
  5716. (setq major-mode 'org-agenda-mode)
  5717. (setq mode-name "Org-Agenda")
  5718. (use-local-map org-agenda-mode-map)
  5719. (easy-menu-add org-agenda-menu)
  5720. (if org-startup-truncated (setq truncate-lines t))
  5721. (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
  5722. (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
  5723. (unless org-agenda-keep-modes
  5724. (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
  5725. org-agenda-show-log nil))
  5726. (easy-menu-change
  5727. '("Agenda") "Agenda Files"
  5728. (append
  5729. (list
  5730. (vector
  5731. (if (get 'org-agenda-files 'org-restrict)
  5732. "Restricted to single file"
  5733. "Edit File List")
  5734. '(org-edit-agenda-file-list)
  5735. (not (get 'org-agenda-files 'org-restrict)))
  5736. "--")
  5737. (mapcar 'org-file-menu-entry (org-agenda-files))))
  5738. (org-agenda-set-mode-name)
  5739. (apply
  5740. (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
  5741. (list 'org-agenda-mode-hook)))
  5742. (define-key org-agenda-mode-map "\C-i" 'org-agenda-goto)
  5743. (define-key org-agenda-mode-map [(tab)] 'org-agenda-goto)
  5744. (define-key org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
  5745. (define-key org-agenda-mode-map " " 'org-agenda-show)
  5746. (define-key org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
  5747. (define-key org-agenda-mode-map "o" 'delete-other-windows)
  5748. (define-key org-agenda-mode-map "L" 'org-agenda-recenter)
  5749. (define-key org-agenda-mode-map "t" 'org-agenda-todo)
  5750. (define-key org-agenda-mode-map "a" 'org-agenda-toggle-archive-tag)
  5751. (define-key org-agenda-mode-map ":" 'org-agenda-set-tags)
  5752. (define-key org-agenda-mode-map "." 'org-agenda-goto-today)
  5753. (define-key org-agenda-mode-map "d" 'org-agenda-day-view)
  5754. (define-key org-agenda-mode-map "w" 'org-agenda-week-view)
  5755. (define-key org-agenda-mode-map (org-key 'S-right) 'org-agenda-date-later)
  5756. (define-key org-agenda-mode-map (org-key 'S-left) 'org-agenda-date-earlier)
  5757. (define-key org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-date-later)
  5758. (define-key org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-date-earlier)
  5759. (define-key org-agenda-mode-map ">" 'org-agenda-date-prompt)
  5760. (define-key org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
  5761. (define-key org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
  5762. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  5763. (while l (define-key org-agenda-mode-map
  5764. (int-to-string (pop l)) 'digit-argument)))
  5765. (define-key org-agenda-mode-map "f" 'org-agenda-follow-mode)
  5766. (define-key org-agenda-mode-map "l" 'org-agenda-log-mode)
  5767. (define-key org-agenda-mode-map "D" 'org-agenda-toggle-diary)
  5768. (define-key org-agenda-mode-map "g" 'org-agenda-toggle-time-grid)
  5769. (define-key org-agenda-mode-map "r" 'org-agenda-redo)
  5770. (define-key org-agenda-mode-map "q" 'org-agenda-quit)
  5771. (define-key org-agenda-mode-map "x" 'org-agenda-exit)
  5772. (define-key org-agenda-mode-map "s" 'org-save-all-org-buffers)
  5773. (define-key org-agenda-mode-map "P" 'org-agenda-show-priority)
  5774. (define-key org-agenda-mode-map "T" 'org-agenda-show-tags)
  5775. (define-key org-agenda-mode-map "n" 'next-line)
  5776. (define-key org-agenda-mode-map "p" 'previous-line)
  5777. (define-key org-agenda-mode-map "\C-n" 'org-agenda-next-date-line)
  5778. (define-key org-agenda-mode-map "\C-p" 'org-agenda-previous-date-line)
  5779. (define-key org-agenda-mode-map "," 'org-agenda-priority)
  5780. (define-key org-agenda-mode-map "\C-c," 'org-agenda-priority)
  5781. (define-key org-agenda-mode-map "i" 'org-agenda-diary-entry)
  5782. (define-key org-agenda-mode-map "c" 'org-agenda-goto-calendar)
  5783. (eval-after-load "calendar"
  5784. '(define-key calendar-mode-map org-calendar-to-agenda-key
  5785. 'org-calendar-goto-agenda))
  5786. (define-key org-agenda-mode-map "C" 'org-agenda-convert-date)
  5787. (define-key org-agenda-mode-map "m" 'org-agenda-phases-of-moon)
  5788. (define-key org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
  5789. (define-key org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
  5790. (define-key org-agenda-mode-map "h" 'org-agenda-holidays)
  5791. (define-key org-agenda-mode-map "H" 'org-agenda-holidays)
  5792. (define-key org-agenda-mode-map "+" 'org-agenda-priority-up)
  5793. (define-key org-agenda-mode-map "I" 'org-agenda-clock-in)
  5794. (define-key org-agenda-mode-map "O" 'org-clock-out)
  5795. (define-key org-agenda-mode-map "X" 'org-clock-cancel)
  5796. (define-key org-agenda-mode-map "-" 'org-agenda-priority-down)
  5797. (define-key org-agenda-mode-map (org-key 'S-up) 'org-agenda-priority-up)
  5798. (define-key org-agenda-mode-map (org-key 'S-down) 'org-agenda-priority-down)
  5799. (define-key org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
  5800. (define-key org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
  5801. (define-key org-agenda-mode-map [(right)] 'org-agenda-later)
  5802. (define-key org-agenda-mode-map [(left)] 'org-agenda-earlier)
  5803. (define-key org-agenda-mode-map "\C-c\C-x\C-c" 'org-export-icalendar-combine-agenda-files)
  5804. (defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
  5805. "Local keymap for agenda entries from Org-mode.")
  5806. (define-key org-agenda-keymap
  5807. (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
  5808. (define-key org-agenda-keymap
  5809. (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
  5810. (when org-agenda-mouse-1-follows-link
  5811. (define-key org-agenda-keymap [follow-link] 'mouse-face))
  5812. (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
  5813. '("Agenda"
  5814. ("Agenda Files")
  5815. "--"
  5816. ["Show" org-agenda-show t]
  5817. ["Go To (other window)" org-agenda-goto t]
  5818. ["Go To (one window)" org-agenda-switch-to t]
  5819. ["Follow Mode" org-agenda-follow-mode
  5820. :style toggle :selected org-agenda-follow-mode :active t]
  5821. "--"
  5822. ["Cycle TODO" org-agenda-todo t]
  5823. ("Tags"
  5824. ["Show all Tags" org-agenda-show-tags t]
  5825. ["Set Tags" org-agenda-set-tags t])
  5826. ("Schedule"
  5827. ["Schedule" org-agenda-schedule t]
  5828. ["Set Deadline" org-agenda-deadline t]
  5829. "--"
  5830. ["Reschedule +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
  5831. ["Reschedule -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
  5832. ["Reschedule to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
  5833. ("Priority"
  5834. ["Set Priority" org-agenda-priority t]
  5835. ["Increase Priority" org-agenda-priority-up t]
  5836. ["Decrease Priority" org-agenda-priority-down t]
  5837. ["Show Priority" org-agenda-show-priority t])
  5838. "--"
  5839. ;; ["New agenda command" org-agenda t]
  5840. ["Rebuild buffer" org-agenda-redo t]
  5841. ["Save all Org-mode Buffers" org-save-all-org-buffers t]
  5842. "--"
  5843. ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
  5844. ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
  5845. ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
  5846. "--"
  5847. ["Day View" org-agenda-day-view :active (org-agenda-check-type nil 'agenda)
  5848. :style radio :selected (equal org-agenda-ndays 1)]
  5849. ["Week View" org-agenda-week-view :active (org-agenda-check-type nil 'agenda)
  5850. :style radio :selected (equal org-agenda-ndays 7)]
  5851. "--"
  5852. ["Show Logbook entries" org-agenda-log-mode
  5853. :style toggle :selected org-agenda-show-log :active (org-agenda-check-type nil 'agenda 'timeline)]
  5854. ["Include Diary" org-agenda-toggle-diary
  5855. :style toggle :selected org-agenda-include-diary :active (org-agenda-check-type nil 'agenda)]
  5856. ["Use Time Grid" org-agenda-toggle-time-grid
  5857. :style toggle :selected org-agenda-use-time-grid :active (org-agenda-check-type nil 'agenda)]
  5858. "--"
  5859. ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
  5860. ("Calendar Commands"
  5861. ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
  5862. ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
  5863. ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
  5864. ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
  5865. ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)])
  5866. ["Create iCalendar file" org-export-icalendar-combine-agenda-files t]
  5867. "--"
  5868. ["Quit" org-agenda-quit t]
  5869. ["Exit and Release Buffers" org-agenda-exit t]
  5870. ))
  5871. ;;;###autoload
  5872. (defun org-agenda (arg)
  5873. "Dispatch agenda commands to collect entries to the agenda buffer.
  5874. Prompts for a character to select a command. Any prefix arg will be passed
  5875. on to the selected command. The default selections are:
  5876. a Call `org-agenda' to display the agenda for the current day or week.
  5877. t Call `org-todo-list' to display the global todo list.
  5878. T Call `org-todo-list' to display the global todo list, select only
  5879. entries with a specific TODO keyword (the user gets a prompt).
  5880. m Call `org-tags-view' to display headlines with tags matching
  5881. a condition (the user is prompted for the condition).
  5882. M Like `m', but select only TODO entries, no ordinary headlines.
  5883. More commands can be added by configuring the variable
  5884. `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
  5885. searches can be pre-defined in this way.
  5886. If the current buffer is in Org-mode and visiting a file, you can also
  5887. first press `1' to indicate that the agenda should be temporarily (until the
  5888. next use of \\[org-agenda]) restricted to the current file."
  5889. (interactive "P")
  5890. (catch 'exit
  5891. (let ((restrict-ok (and buffer-file-name (org-mode-p)))
  5892. (bfn buffer-file-name)
  5893. (custom org-agenda-custom-commands)
  5894. c entry key type string)
  5895. (put 'org-agenda-files 'org-restrict nil)
  5896. (save-window-excursion
  5897. (delete-other-windows)
  5898. (switch-to-buffer-other-window " *Agenda Commands*")
  5899. (erase-buffer)
  5900. (insert
  5901. "Press key for an agenda command:
  5902. --------------------------------
  5903. a Agenda for current week or day
  5904. t List of all TODO entries T Entries with special TODO kwd
  5905. m Match a TAGS query M Like m, but only TODO entries
  5906. C Configure your own agenda commands")
  5907. (while (setq entry (pop custom))
  5908. (setq key (car entry) type (nth 1 entry) string (nth 2 entry))
  5909. (insert (format "\n%-4s%-14s: %s"
  5910. key
  5911. (cond
  5912. ((eq type 'tags) "Tags query")
  5913. ((eq type 'todo) "TODO keyword")
  5914. ((eq type 'tags-tree) "Tags tree")
  5915. ((eq type 'todo-tree) "TODO kwd tree")
  5916. ((eq type 'occur-tree) "Occur tree")
  5917. (t "???"))
  5918. (org-add-props string nil 'face 'org-warning))))
  5919. (goto-char (point-min))
  5920. (if (fboundp 'fit-window-to-buffer) (fit-window-to-buffer))
  5921. (message "Press key for agenda command%s"
  5922. (if restrict-ok ", or [1] to restrict to current file" ""))
  5923. (setq c (read-char-exclusive))
  5924. (message "")
  5925. (when (equal c ?1)
  5926. (if restrict-ok
  5927. (put 'org-agenda-files 'org-restrict (list bfn))
  5928. (error "Cannot restrict agenda to current buffer"))
  5929. (message "Press key for agenda command%s"
  5930. (if restrict-ok " (restricted to current file)" ""))
  5931. (setq c (read-char-exclusive))
  5932. (message "")))
  5933. (require 'calendar) ; FIXME: can we avoid this for some commands?
  5934. ;; For example the todo list should not need it (but does...)
  5935. (cond
  5936. ((equal c ?C) (customize-variable 'org-agenda-custom-commands))
  5937. ((equal c ?a) (call-interactively 'org-agenda-list))
  5938. ((equal c ?t) (call-interactively 'org-todo-list))
  5939. ((equal c ?T) (org-call-with-arg 'org-todo-list (or arg '(4))))
  5940. ((equal c ?m) (call-interactively 'org-tags-view))
  5941. ((equal c ?M) (org-call-with-arg 'org-tags-view (or arg '(4))))
  5942. ((setq entry (assoc (char-to-string c) org-agenda-custom-commands))
  5943. (setq type (nth 1 entry) string (nth 2 entry))
  5944. (cond
  5945. ((eq type 'tags)
  5946. (org-tags-view current-prefix-arg string))
  5947. ((eq type 'tags-todo)
  5948. (org-tags-view '(4) string))
  5949. ((eq type 'todo)
  5950. (org-todo-list string))
  5951. ((eq type 'tags-tree)
  5952. (org-check-for-org-mode)
  5953. (org-tags-sparse-tree current-prefix-arg string))
  5954. ((eq type 'todo-tree)
  5955. (org-check-for-org-mode)
  5956. (org-occur (concat "^" outline-regexp "[ \t]*"
  5957. (regexp-quote string) "\\>")))
  5958. ((eq type 'occur-tree)
  5959. (org-check-for-org-mode)
  5960. (org-occur string))
  5961. (t (error "Invalid custom agenda command type %s" type))))
  5962. (t (error "Invalid key"))))))
  5963. (defun org-check-for-org-mode ()
  5964. "Make sure current buffer is in org-mode. Error if not."
  5965. (or (org-mode-p)
  5966. (error "Cannot execute org-mode agenda command on buffer in %s."
  5967. major-mode)))
  5968. (defun org-fit-agenda-window ()
  5969. "Fit the window to the buffer size."
  5970. (and org-fit-agenda-window
  5971. (fboundp 'fit-window-to-buffer)
  5972. (fit-window-to-buffer nil (/ (* (frame-height) 3) 4)
  5973. (/ (frame-height) 2))))
  5974. (defun org-agenda-files (&optional unrestricted)
  5975. "Get the list of agenda files.
  5976. Optional UNRESTRICTED means return the full list even if a restriction
  5977. is currently in place."
  5978. (cond
  5979. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  5980. ((stringp org-agenda-files) (org-read-agenda-file-list))
  5981. ((listp org-agenda-files) org-agenda-files)
  5982. (t (error "Invalid value of `org-agenda-files'"))))
  5983. (defvar org-window-configuration)
  5984. (defun org-edit-agenda-file-list ()
  5985. "Edit the list of agenda files.
  5986. Depending on setup, this either uses customize to edit the variable
  5987. `org-agenda-files', or it visits the file that is holding the list. In the
  5988. latter case, the buffer is set up in a way that saving it automatically kills
  5989. the buffer and restores the previous window configuration."
  5990. (interactive)
  5991. (if (stringp org-agenda-files)
  5992. (let ((cw (current-window-configuration)))
  5993. (find-file org-agenda-files)
  5994. (org-set-local 'org-window-configuration cw)
  5995. (org-add-hook 'after-save-hook
  5996. (lambda ()
  5997. (set-window-configuration
  5998. (prog1 org-window-configuration
  5999. (kill-buffer (current-buffer))))
  6000. (org-install-agenda-files-menu)
  6001. (message "New agenda file list installed"))
  6002. nil 'local)
  6003. (message (substitute-command-keys
  6004. "Edit list and finish with \\[save-buffer]")))
  6005. (customize-variable 'org-agenda-files)))
  6006. (defun org-store-new-agenda-file-list (list)
  6007. "Set new value for the agenda file list and save it correcly."
  6008. (if (stringp org-agenda-files)
  6009. (let ((f org-agenda-files) b)
  6010. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  6011. (with-temp-file f
  6012. (insert (mapconcat 'identity list "\n") "\n")))
  6013. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  6014. (setq org-agenda-files list)
  6015. (customize-save-variable 'org-agenda-files org-agenda-files))))
  6016. (defun org-read-agenda-file-list ()
  6017. "Read the list of agenda files from a file."
  6018. (when (stringp org-agenda-files)
  6019. (with-temp-buffer
  6020. (insert-file-contents org-agenda-files)
  6021. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  6022. (defvar org-agenda-markers nil
  6023. "List of all currently active markers created by `org-agenda'.")
  6024. (defvar org-agenda-last-marker-time (time-to-seconds (current-time))
  6025. "Creation time of the last agenda marker.")
  6026. (defun org-agenda-new-marker (&optional pos)
  6027. "Return a new agenda marker.
  6028. Org-mode keeps a list of these markers and resets them when they are
  6029. no longer in use."
  6030. (let ((m (copy-marker (or pos (point)))))
  6031. (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
  6032. (push m org-agenda-markers)
  6033. m))
  6034. (defun org-agenda-maybe-reset-markers (&optional force)
  6035. "Reset markers created by `org-agenda'. But only if they are old enough."
  6036. (if (or force
  6037. (> (- (time-to-seconds (current-time))
  6038. org-agenda-last-marker-time)
  6039. 5))
  6040. (while org-agenda-markers
  6041. (move-marker (pop org-agenda-markers) nil))))
  6042. (defvar org-agenda-new-buffers nil
  6043. "Buffers created to visit agenda files.")
  6044. (defun org-get-agenda-file-buffer (file)
  6045. "Get a buffer visiting FILE. If the buffer needs to be created, add
  6046. it to the list of buffers which might be released later."
  6047. (let ((buf (find-buffer-visiting file)))
  6048. (if buf
  6049. buf ; just return it
  6050. ;; Make a new buffer and remember it
  6051. (setq buf (find-file-noselect file))
  6052. (if buf (push buf org-agenda-new-buffers))
  6053. buf)))
  6054. (defun org-release-buffers (blist)
  6055. "Release all buffers in list, asking the user for confirmation when needed.
  6056. When a buffer is unmodified, it is just killed. When modified, it is saved
  6057. \(if the user agrees) and then killed."
  6058. (let (buf file)
  6059. (while (setq buf (pop blist))
  6060. (setq file (buffer-file-name buf))
  6061. (when (and (buffer-modified-p buf)
  6062. file
  6063. (y-or-n-p (format "Save file %s? " file)))
  6064. (with-current-buffer buf (save-buffer)))
  6065. (kill-buffer buf))))
  6066. (defvar org-respect-restriction nil) ; Dynamically-scoped param.
  6067. (defun org-timeline (&optional include-all keep-modes)
  6068. "Show a time-sorted view of the entries in the current org file.
  6069. Only entries with a time stamp of today or later will be listed. With
  6070. \\[universal-argument] prefix, all unfinished TODO items will also be shown,
  6071. under the current date.
  6072. If the buffer contains an active region, only check the region for
  6073. dates."
  6074. (interactive "P")
  6075. (require 'calendar)
  6076. (org-agenda-maybe-reset-markers 'force)
  6077. (org-compile-prefix-format org-timeline-prefix-format)
  6078. (let* ((dopast t)
  6079. (dotodo include-all)
  6080. (doclosed org-agenda-show-log)
  6081. (org-agenda-keep-modes keep-modes)
  6082. (entry buffer-file-name)
  6083. (org-agenda-files (list buffer-file-name))
  6084. (date (calendar-current-date))
  6085. (win (selected-window))
  6086. (pos1 (point))
  6087. (beg (if (org-region-active-p) (region-beginning) (point-min)))
  6088. (end (if (org-region-active-p) (region-end) (point-max)))
  6089. (day-numbers (org-get-all-dates beg end 'no-ranges
  6090. t doclosed ; always include today
  6091. org-timeline-show-empty-dates))
  6092. (today (time-to-days (current-time)))
  6093. (org-respect-restriction t)
  6094. (past t)
  6095. args
  6096. s e rtn d emptyp)
  6097. (org-prepare-agenda-buffers org-agenda-files)
  6098. (setq org-agenda-redo-command
  6099. (list 'progn
  6100. (list 'switch-to-buffer-other-window (current-buffer))
  6101. (list 'org-timeline (list 'quote include-all) t)))
  6102. (if (not dopast)
  6103. ;; Remove past dates from the list of dates.
  6104. (setq day-numbers (delq nil (mapcar (lambda(x)
  6105. (if (>= x today) x nil))
  6106. day-numbers))))
  6107. (switch-to-buffer-other-window
  6108. (get-buffer-create org-agenda-buffer-name))
  6109. (setq buffer-read-only nil)
  6110. (erase-buffer)
  6111. (org-agenda-mode) (setq buffer-read-only nil)
  6112. (org-set-local 'org-agenda-type 'timeline)
  6113. (if doclosed (push :closed args))
  6114. (push :timestamp args)
  6115. (if dotodo (push :todo args))
  6116. (while (setq d (pop day-numbers))
  6117. (if (and (listp d) (eq (car d) :omitted))
  6118. (progn
  6119. (setq s (point))
  6120. (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
  6121. (put-text-property s (1- (point)) 'face 'org-level-3))
  6122. (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
  6123. (if (and (>= d today)
  6124. dopast
  6125. past)
  6126. (progn
  6127. (setq past nil)
  6128. (insert (make-string 79 ?-) "\n")))
  6129. (setq date (calendar-gregorian-from-absolute d))
  6130. (setq s (point))
  6131. (setq rtn (and (not emptyp)
  6132. (apply 'org-agenda-get-day-entries
  6133. entry date args)))
  6134. (if (or rtn (equal d today) org-timeline-show-empty-dates)
  6135. (progn
  6136. (insert (calendar-day-name date) " "
  6137. (number-to-string (extract-calendar-day date)) " "
  6138. (calendar-month-name (extract-calendar-month date)) " "
  6139. (number-to-string (extract-calendar-year date)) "\n")
  6140. (put-text-property s (1- (point)) 'face
  6141. 'org-level-3)
  6142. (if (equal d today)
  6143. (put-text-property s (1- (point)) 'org-today t))
  6144. (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
  6145. (put-text-property s (1- (point)) 'day d)))))
  6146. (goto-char (point-min))
  6147. (setq buffer-read-only t)
  6148. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  6149. (point-min)))
  6150. (when (not org-select-timeline-window)
  6151. (select-window win)
  6152. (goto-char pos1))))
  6153. ;;;###autoload
  6154. (defun org-agenda-list (&optional include-all start-day ndays keep-modes)
  6155. "Produce a weekly view from all files in variable `org-agenda-files'.
  6156. The view will be for the current week, but from the overview buffer you
  6157. will be able to go to other weeks.
  6158. With one \\[universal-argument] prefix argument INCLUDE-ALL, all unfinished TODO items will
  6159. also be shown, under the current date.
  6160. With two \\[universal-argument] prefix argument INCLUDE-ALL, all TODO entries marked DONE
  6161. on the days are also shown. See the variable `org-log-done' for how
  6162. to turn on logging.
  6163. START-DAY defaults to TODAY, or to the most recent match for the weekday
  6164. given in `org-agenda-start-on-weekday'.
  6165. NDAYS defaults to `org-agenda-ndays'."
  6166. (interactive "P")
  6167. (org-agenda-maybe-reset-markers 'force)
  6168. (org-compile-prefix-format org-agenda-prefix-format)
  6169. (require 'calendar)
  6170. (let* ((org-agenda-start-on-weekday
  6171. (if (or (equal ndays 1)
  6172. (and (null ndays) (equal 1 org-agenda-ndays)))
  6173. nil org-agenda-start-on-weekday))
  6174. (org-agenda-keep-modes keep-modes)
  6175. (thefiles (org-agenda-files))
  6176. (files thefiles)
  6177. (win (selected-window))
  6178. (today (time-to-days (current-time)))
  6179. (sd (or start-day today))
  6180. (start (if (or (null org-agenda-start-on-weekday)
  6181. (< org-agenda-ndays 7))
  6182. sd
  6183. (let* ((nt (calendar-day-of-week
  6184. (calendar-gregorian-from-absolute sd)))
  6185. (n1 org-agenda-start-on-weekday)
  6186. (d (- nt n1)))
  6187. (- sd (+ (if (< d 0) 7 0) d)))))
  6188. (day-numbers (list start))
  6189. (inhibit-redisplay t)
  6190. s e rtn rtnall file date d start-pos end-pos todayp nd)
  6191. (org-prepare-agenda-buffers files)
  6192. (setq org-agenda-redo-command
  6193. (list 'org-agenda-list (list 'quote include-all) start-day ndays t))
  6194. ;; Make the list of days
  6195. (setq ndays (or ndays org-agenda-ndays)
  6196. nd ndays)
  6197. (while (> ndays 1)
  6198. (push (1+ (car day-numbers)) day-numbers)
  6199. (setq ndays (1- ndays)))
  6200. (setq day-numbers (nreverse day-numbers))
  6201. (if (not (equal (current-buffer) (get-buffer org-agenda-buffer-name)))
  6202. (progn
  6203. (delete-other-windows)
  6204. (switch-to-buffer-other-window
  6205. (get-buffer-create org-agenda-buffer-name))))
  6206. (setq buffer-read-only nil)
  6207. (erase-buffer)
  6208. (org-agenda-mode) (setq buffer-read-only nil)
  6209. (org-set-local 'org-agenda-type 'agenda)
  6210. (org-set-local 'starting-day (car day-numbers))
  6211. (org-set-local 'include-all-loc include-all)
  6212. (when (and (or include-all org-agenda-include-all-todo)
  6213. (member today day-numbers))
  6214. (setq files thefiles
  6215. rtnall nil)
  6216. (while (setq file (pop files))
  6217. (catch 'nextfile
  6218. (org-check-agenda-file file)
  6219. (setq date (calendar-gregorian-from-absolute today)
  6220. rtn (org-agenda-get-day-entries
  6221. file date :todo))
  6222. (setq rtnall (append rtnall rtn))))
  6223. (when rtnall
  6224. (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
  6225. (add-text-properties (point-min) (1- (point))
  6226. (list 'face 'org-level-3))
  6227. (insert (org-finalize-agenda-entries rtnall) "\n")))
  6228. (while (setq d (pop day-numbers))
  6229. (setq date (calendar-gregorian-from-absolute d)
  6230. s (point))
  6231. (if (or (setq todayp (= d today))
  6232. (and (not start-pos) (= d sd)))
  6233. (setq start-pos (point))
  6234. (if (and start-pos (not end-pos))
  6235. (setq end-pos (point))))
  6236. (setq files thefiles
  6237. rtnall nil)
  6238. (while (setq file (pop files))
  6239. (catch 'nextfile
  6240. (org-check-agenda-file file)
  6241. (if org-agenda-show-log
  6242. (setq rtn (org-agenda-get-day-entries
  6243. file date
  6244. :deadline :scheduled :timestamp :closed))
  6245. (setq rtn (org-agenda-get-day-entries
  6246. file date
  6247. :deadline :scheduled :timestamp)))
  6248. (setq rtnall (append rtnall rtn))))
  6249. (if org-agenda-include-diary
  6250. (progn
  6251. (require 'diary-lib)
  6252. (setq rtn (org-get-entries-from-diary date))
  6253. (setq rtnall (append rtnall rtn))))
  6254. (if (or rtnall org-agenda-show-all-dates)
  6255. (progn
  6256. (insert (format "%-9s %2d %s %4d\n"
  6257. (calendar-day-name date)
  6258. (extract-calendar-day date)
  6259. (calendar-month-name (extract-calendar-month date))
  6260. (extract-calendar-year date)))
  6261. (put-text-property s (1- (point)) 'face
  6262. 'org-level-3)
  6263. (if rtnall (insert
  6264. (org-finalize-agenda-entries
  6265. (org-agenda-add-time-grid-maybe
  6266. rtnall nd todayp))
  6267. "\n"))
  6268. (put-text-property s (1- (point)) 'day d))))
  6269. (goto-char (point-min))
  6270. (setq buffer-read-only t)
  6271. (org-fit-agenda-window)
  6272. (unless (and (pos-visible-in-window-p (point-min))
  6273. (pos-visible-in-window-p (point-max)))
  6274. (goto-char (1- (point-max)))
  6275. (recenter -1)
  6276. (if (not (pos-visible-in-window-p (or start-pos 1)))
  6277. (progn
  6278. (goto-char (or start-pos 1))
  6279. (recenter 1))))
  6280. (goto-char (or start-pos 1))
  6281. (if (not org-select-agenda-window) (select-window win))
  6282. (message "")))
  6283. (defvar org-select-this-todo-keyword nil)
  6284. ;;;###autoload
  6285. (defun org-todo-list (arg &optional keep-modes)
  6286. "Show all TODO entries from all agenda file in a single list.
  6287. The prefix arg can be used to select a specific TODO keyword and limit
  6288. the list to these. When using \\[universal-argument], you will be prompted
  6289. for a keyword. A numeric prefix directly selects the Nth keyword in
  6290. `org-todo-keywords'."
  6291. (interactive "P")
  6292. (org-agenda-maybe-reset-markers 'force)
  6293. (org-compile-prefix-format org-agenda-prefix-format)
  6294. (let* ((org-agenda-keep-modes keep-modes)
  6295. (today (time-to-days (current-time)))
  6296. (date (calendar-gregorian-from-absolute today))
  6297. (win (selected-window))
  6298. (kwds org-todo-keywords)
  6299. (completion-ignore-case t)
  6300. (org-select-this-todo-keyword
  6301. (if (stringp arg) arg
  6302. (and arg (integerp arg) (> arg 0)
  6303. (nth (1- arg) org-todo-keywords))))
  6304. rtn rtnall files file pos)
  6305. (when (equal arg '(4))
  6306. (setq org-select-this-todo-keyword
  6307. (completing-read "Keyword: " (mapcar 'list org-todo-keywords)
  6308. nil t)))
  6309. (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
  6310. (if (not (equal (current-buffer) (get-buffer org-agenda-buffer-name)))
  6311. (progn
  6312. (delete-other-windows)
  6313. (switch-to-buffer-other-window
  6314. (get-buffer-create org-agenda-buffer-name))))
  6315. (setq buffer-read-only nil)
  6316. (erase-buffer)
  6317. (org-agenda-mode) (setq buffer-read-only nil)
  6318. (org-set-local 'org-agenda-type 'todo)
  6319. (org-set-local 'last-arg arg)
  6320. (org-set-local 'org-todo-keywords kwds)
  6321. (org-set-local 'org-agenda-redo-command
  6322. '(org-todo-list (or current-prefix-arg last-arg) t))
  6323. (setq files (org-agenda-files)
  6324. rtnall nil)
  6325. (org-prepare-agenda-buffers files)
  6326. (while (setq file (pop files))
  6327. (catch 'nextfile
  6328. (org-check-agenda-file file)
  6329. (setq rtn (org-agenda-get-day-entries file date :todo))
  6330. (setq rtnall (append rtnall rtn))))
  6331. (insert "Global list of TODO items of type: ")
  6332. (add-text-properties (point-min) (1- (point))
  6333. (list 'face 'org-level-3))
  6334. (setq pos (point))
  6335. (insert (or org-select-this-todo-keyword "ALL") "\n")
  6336. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  6337. (setq pos (point))
  6338. (insert
  6339. "Available with `N r': (0)ALL "
  6340. (let ((n 0))
  6341. (mapconcat (lambda (x)
  6342. (format "(%d)%s" (setq n (1+ n)) x))
  6343. org-todo-keywords " "))
  6344. "\n")
  6345. (add-text-properties pos (1- (point)) (list 'face 'org-level-3))
  6346. (when rtnall
  6347. (insert (org-finalize-agenda-entries rtnall) "\n"))
  6348. (goto-char (point-min))
  6349. (setq buffer-read-only t)
  6350. (org-fit-agenda-window)
  6351. (if (not org-select-agenda-window) (select-window win))))
  6352. (defun org-check-agenda-file (file)
  6353. "Make sure FILE exists. If not, ask user what to do."
  6354. (when (not (file-exists-p file))
  6355. (message "non-existent file %s. [R]emove from list or [A]bort?"
  6356. (abbreviate-file-name file))
  6357. (let ((r (downcase (read-char-exclusive))))
  6358. (cond
  6359. ((equal r ?r)
  6360. (org-remove-file file)
  6361. (throw 'nextfile t))
  6362. (t (error "Abort"))))))
  6363. (defun org-agenda-check-type (error &rest types)
  6364. "Check if agenda buffer is of allowed type.
  6365. If ERROR is non-nil, throw an error, otherwise just return nil."
  6366. (if (memq org-agenda-type types)
  6367. t
  6368. (if error
  6369. (error "Not allowed in %s-type agenda buffers" org-agenda-type)
  6370. nil)))
  6371. (defun org-agenda-quit ()
  6372. "Exit agenda by removing the window or the buffer."
  6373. (interactive)
  6374. (let ((buf (current-buffer)))
  6375. (if (not (one-window-p)) (delete-window))
  6376. (kill-buffer buf)
  6377. (org-agenda-maybe-reset-markers 'force)))
  6378. (defun org-agenda-exit ()
  6379. "Exit agenda by removing the window or the buffer.
  6380. Also kill all Org-mode buffers which have been loaded by `org-agenda'.
  6381. Org-mode buffers visited directly by the user will not be touched."
  6382. (interactive)
  6383. (org-release-buffers org-agenda-new-buffers)
  6384. (setq org-agenda-new-buffers nil)
  6385. (org-agenda-quit))
  6386. ;; FIXME: move this function.
  6387. (defun org-save-all-org-buffers ()
  6388. "Save all Org-mode buffers without user confirmation."
  6389. (interactive)
  6390. (message "Saving all Org-mode buffers...")
  6391. (save-some-buffers t 'org-mode-p)
  6392. (message "Saving all Org-mode buffers... done"))
  6393. (defun org-agenda-redo ()
  6394. "Rebuild Agenda.
  6395. When this is the global TODO list, a prefix argument will be interpreted."
  6396. (interactive)
  6397. (message "Rebuilding agenda buffer...")
  6398. (eval org-agenda-redo-command)
  6399. (message "Rebuilding agenda buffer...done"))
  6400. (defun org-agenda-goto-today ()
  6401. "Go to today."
  6402. (interactive)
  6403. (org-agenda-check-type t 'timeline 'agenda)
  6404. (if (boundp 'starting-day)
  6405. (let ((cmd (car org-agenda-redo-command))
  6406. (iall (nth 1 org-agenda-redo-command))
  6407. (nday (nth 3 org-agenda-redo-command))
  6408. (keep (nth 4 org-agenda-redo-command)))
  6409. (eval (list cmd iall nil nday keep)))
  6410. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  6411. (point-min)))))
  6412. (defun org-agenda-later (arg)
  6413. "Go forward in time by `org-agenda-ndays' days.
  6414. With prefix ARG, go forward that many times `org-agenda-ndays'."
  6415. (interactive "p")
  6416. (org-agenda-check-type t 'agenda)
  6417. (org-agenda-list (if (boundp 'include-all-loc) include-all-loc nil)
  6418. (+ starting-day (* arg org-agenda-ndays)) nil t))
  6419. (defun org-agenda-earlier (arg)
  6420. "Go back in time by `org-agenda-ndays' days.
  6421. With prefix ARG, go back that many times `org-agenda-ndays'."
  6422. (interactive "p")
  6423. (org-agenda-check-type t 'agenda)
  6424. (org-agenda-list (if (boundp 'include-all-loc) include-all-loc nil)
  6425. (- starting-day (* arg org-agenda-ndays)) nil t))
  6426. (defun org-agenda-week-view ()
  6427. "Switch to weekly view for agenda."
  6428. (interactive)
  6429. (org-agenda-check-type t 'agenda)
  6430. (setq org-agenda-ndays 7)
  6431. (org-agenda-list include-all-loc
  6432. (or (get-text-property (point) 'day)
  6433. starting-day)
  6434. nil t)
  6435. (org-agenda-set-mode-name)
  6436. (message "Switched to week view"))
  6437. (defun org-agenda-day-view ()
  6438. "Switch to daily view for agenda."
  6439. (interactive)
  6440. (org-agenda-check-type t 'agenda)
  6441. (setq org-agenda-ndays 1)
  6442. (org-agenda-list include-all-loc
  6443. (or (get-text-property (point) 'day)
  6444. starting-day)
  6445. nil t)
  6446. (org-agenda-set-mode-name)
  6447. (message "Switched to day view"))
  6448. (defun org-agenda-next-date-line (&optional arg)
  6449. "Jump to the next line indicating a date in agenda buffer."
  6450. (interactive "p")
  6451. (org-agenda-check-type t 'agenda 'timeline)
  6452. (beginning-of-line 1)
  6453. (if (looking-at "^\\S-") (forward-char 1))
  6454. (if (not (re-search-forward "^\\S-" nil t arg))
  6455. (progn
  6456. (backward-char 1)
  6457. (error "No next date after this line in this buffer")))
  6458. (goto-char (match-beginning 0)))
  6459. (defun org-agenda-previous-date-line (&optional arg)
  6460. "Jump to the previous line indicating a date in agenda buffer."
  6461. (interactive "p")
  6462. (org-agenda-check-type t 'agenda 'timeline)
  6463. (beginning-of-line 1)
  6464. (if (not (re-search-backward "^\\S-" nil t arg))
  6465. (error "No previous date before this line in this buffer")))
  6466. ;; Initialize the highlight
  6467. (defvar org-hl (org-make-overlay 1 1))
  6468. (org-overlay-put org-hl 'face 'highlight)
  6469. (defun org-highlight (begin end &optional buffer)
  6470. "Highlight a region with overlay."
  6471. (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
  6472. org-hl begin end (or buffer (current-buffer))))
  6473. (defun org-unhighlight ()
  6474. "Detach overlay INDEX."
  6475. (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
  6476. (defun org-agenda-follow-mode ()
  6477. "Toggle follow mode in an agenda buffer."
  6478. (interactive)
  6479. (setq org-agenda-follow-mode (not org-agenda-follow-mode))
  6480. (org-agenda-set-mode-name)
  6481. (message "Follow mode is %s"
  6482. (if org-agenda-follow-mode "on" "off")))
  6483. (defun org-agenda-log-mode ()
  6484. "Toggle log mode in an agenda buffer."
  6485. (interactive)
  6486. (org-agenda-check-type t 'agenda 'timeline)
  6487. (setq org-agenda-show-log (not org-agenda-show-log))
  6488. (org-agenda-set-mode-name)
  6489. (org-agenda-redo)
  6490. (message "Log mode is %s"
  6491. (if org-agenda-show-log "on" "off")))
  6492. (defun org-agenda-toggle-diary ()
  6493. "Toggle diary inclusion in an agenda buffer."
  6494. (interactive)
  6495. (org-agenda-check-type t 'agenda)
  6496. (setq org-agenda-include-diary (not org-agenda-include-diary))
  6497. (org-agenda-redo)
  6498. (org-agenda-set-mode-name)
  6499. (message "Diary inclusion turned %s"
  6500. (if org-agenda-include-diary "on" "off")))
  6501. (defun org-agenda-toggle-time-grid ()
  6502. "Toggle time grid in an agenda buffer."
  6503. (interactive)
  6504. (org-agenda-check-type t 'agenda)
  6505. (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
  6506. (org-agenda-redo)
  6507. (org-agenda-set-mode-name)
  6508. (message "Time-grid turned %s"
  6509. (if org-agenda-use-time-grid "on" "off")))
  6510. (defun org-agenda-set-mode-name ()
  6511. "Set the mode name to indicate all the small mode settings."
  6512. (setq mode-name
  6513. (concat "Org-Agenda"
  6514. (if (equal org-agenda-ndays 1) " Day" "")
  6515. (if (equal org-agenda-ndays 7) " Week" "")
  6516. (if org-agenda-follow-mode " Follow" "")
  6517. (if org-agenda-include-diary " Diary" "")
  6518. (if org-agenda-use-time-grid " Grid" "")
  6519. (if org-agenda-show-log " Log" "")))
  6520. (force-mode-line-update))
  6521. (defun org-agenda-post-command-hook ()
  6522. (and (eolp) (not (bolp)) (backward-char 1))
  6523. (if (and org-agenda-follow-mode
  6524. (get-text-property (point) 'org-marker))
  6525. (org-agenda-show)))
  6526. (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
  6527. (defun org-get-entries-from-diary (date)
  6528. "Get the (Emacs Calendar) diary entries for DATE."
  6529. (let* ((fancy-diary-buffer "*temporary-fancy-diary-buffer*")
  6530. (diary-display-hook '(fancy-diary-display))
  6531. (list-diary-entries-hook
  6532. (cons 'org-diary-default-entry list-diary-entries-hook))
  6533. (diary-file-name-prefix-function nil) ; turn this feature off
  6534. (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
  6535. entries
  6536. (org-disable-agenda-to-diary t))
  6537. (save-excursion
  6538. (save-window-excursion
  6539. (list-diary-entries date 1))) ;; Keep this name for now, compatibility
  6540. (if (not (get-buffer fancy-diary-buffer))
  6541. (setq entries nil)
  6542. (with-current-buffer fancy-diary-buffer
  6543. (setq buffer-read-only nil)
  6544. (if (= (point-max) 1)
  6545. ;; No entries
  6546. (setq entries nil)
  6547. ;; Omit the date and other unnecessary stuff
  6548. (org-agenda-cleanup-fancy-diary)
  6549. ;; Add prefix to each line and extend the text properties
  6550. (if (= (point-max) 1)
  6551. (setq entries nil)
  6552. (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
  6553. (set-buffer-modified-p nil)
  6554. (kill-buffer fancy-diary-buffer)))
  6555. (when entries
  6556. (setq entries (org-split-string entries "\n"))
  6557. (setq entries
  6558. (mapcar
  6559. (lambda (x)
  6560. (setq x (org-format-agenda-item "" x "Diary" nil 'time))
  6561. ;; Extend the text properties to the beginning of the line
  6562. (org-add-props x (text-properties-at (1- (length x)) x)))
  6563. entries)))))
  6564. (defun org-agenda-cleanup-fancy-diary ()
  6565. "Remove unwanted stuff in buffer created by `fancy-diary-display'.
  6566. This gets rid of the date, the underline under the date, and
  6567. the dummy entry installed by `org-mode' to ensure non-empty diary for each
  6568. date. It also removes lines that contain only whitespace."
  6569. (goto-char (point-min))
  6570. (if (looking-at ".*?:[ \t]*")
  6571. (progn
  6572. (replace-match "")
  6573. (re-search-forward "\n=+$" nil t)
  6574. (replace-match "")
  6575. (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
  6576. (re-search-forward "\n=+$" nil t)
  6577. (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
  6578. (goto-char (point-min))
  6579. (while (re-search-forward "^ +\n" nil t)
  6580. (replace-match ""))
  6581. (goto-char (point-min))
  6582. (if (re-search-forward "^Org-mode dummy\n?" nil t)
  6583. (replace-match "")))
  6584. ;; Make sure entries from the diary have the right text properties.
  6585. (eval-after-load "diary-lib"
  6586. '(if (boundp 'diary-modify-entry-list-string-function)
  6587. ;; We can rely on the hook, nothing to do
  6588. nil
  6589. ;; Hook not avaiable, must use advice to make this work
  6590. (defadvice add-to-diary-list (before org-mark-diary-entry activate)
  6591. "Make the position visible."
  6592. (if (and org-disable-agenda-to-diary ;; called from org-agenda
  6593. (stringp string)
  6594. buffer-file-name)
  6595. (setq string (org-modify-diary-entry-string string))))))
  6596. (defun org-modify-diary-entry-string (string)
  6597. "Add text properties to string, allowing org-mode to act on it."
  6598. (org-add-props string nil
  6599. 'mouse-face 'highlight
  6600. 'keymap org-agenda-keymap
  6601. 'help-echo (format "mouse-2 or RET jump to diary file %s"
  6602. (abbreviate-file-name buffer-file-name))
  6603. 'org-agenda-diary-link t
  6604. 'org-marker (org-agenda-new-marker (point-at-bol))))
  6605. (defun org-diary-default-entry ()
  6606. "Add a dummy entry to the diary.
  6607. Needed to avoid empty dates which mess up holiday display."
  6608. ;; Catch the error if dealing with the new add-to-diary-alist
  6609. (when org-disable-agenda-to-diary
  6610. (condition-case nil
  6611. (add-to-diary-list original-date "Org-mode dummy" "")
  6612. (error
  6613. (add-to-diary-list original-date "Org-mode dummy" "" nil)))))
  6614. (defun org-cycle-agenda-files ()
  6615. "Cycle through the files in `org-agenda-files'.
  6616. If the current buffer visits an agenda file, find the next one in the list.
  6617. If the current buffer does not, find the first agenda file."
  6618. (interactive)
  6619. (let* ((fs (org-agenda-files t))
  6620. (files (append fs (list (car fs))))
  6621. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  6622. file)
  6623. (unless files (error "No agenda files"))
  6624. (catch 'exit
  6625. (while (setq file (pop files))
  6626. (if (equal (file-truename file) tcf)
  6627. (when (car files)
  6628. (find-file (car files))
  6629. (throw 'exit t))))
  6630. (find-file (car fs)))))
  6631. (defun org-agenda-file-to-end ()
  6632. "Move/add the current file to the end of the agenda file list.
  6633. If the file is not present in the list, it is appended to the list. If it is
  6634. present, it is moved there."
  6635. (interactive)
  6636. (org-agenda-file-to-front 'to-end))
  6637. (defun org-agenda-file-to-front (&optional to-end)
  6638. "Move/add the current file to the top of the agenda file list.
  6639. If the file is not present in the list, it is added to the front. If it is
  6640. present, it is moved there. With optional argument TO-END, add/move to the
  6641. end of the list."
  6642. (interactive "P")
  6643. (let ((file-alist (mapcar (lambda (x)
  6644. (cons (file-truename x) x))
  6645. (org-agenda-files t)))
  6646. (ctf (file-truename buffer-file-name))
  6647. x had)
  6648. (setq x (assoc ctf file-alist) had x)
  6649. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  6650. (if to-end
  6651. (setq file-alist (append (delq x file-alist) (list x)))
  6652. (setq file-alist (cons x (delq x file-alist))))
  6653. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  6654. (org-install-agenda-files-menu)
  6655. (message "File %s to %s of agenda file list"
  6656. (if had "moved" "added") (if to-end "end" "front"))))
  6657. (defun org-remove-file (&optional file)
  6658. "Remove current file from the list of files in variable `org-agenda-files'.
  6659. These are the files which are being checked for agenda entries.
  6660. Optional argument FILE means, use this file instead of the current."
  6661. (interactive)
  6662. (let* ((file (or file buffer-file-name))
  6663. (true-file (file-truename file))
  6664. (afile (abbreviate-file-name file))
  6665. (files (delq nil (mapcar
  6666. (lambda (x)
  6667. (if (equal true-file
  6668. (file-truename x))
  6669. nil x))
  6670. (org-agenda-files t)))))
  6671. (if (not (= (length files) (length (org-agenda-files t))))
  6672. (progn
  6673. (org-store-new-agenda-file-list files)
  6674. (org-install-agenda-files-menu)
  6675. (message "Removed file: %s" afile))
  6676. (message "File was not in list: %s" afile))))
  6677. (defun org-file-menu-entry (file)
  6678. (vector file (list 'find-file file) t))
  6679. (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty)
  6680. "Return a list of all relevant day numbers from BEG to END buffer positions.
  6681. If NO-RANGES is non-nil, include only the start and end dates of a range,
  6682. not every single day in the range. If FORCE-TODAY is non-nil, make
  6683. sure that TODAY is included in the list. If INACTIVE is non-nil, also
  6684. inactive time stamps (those in square brackets) are included.
  6685. When EMPTY is non-nil, also include days without any entries."
  6686. (let ((re (if inactive org-ts-regexp-both org-ts-regexp))
  6687. dates dates1 date day day1 day2 ts1 ts2)
  6688. (if force-today
  6689. (setq dates (list (time-to-days (current-time)))))
  6690. (save-excursion
  6691. (goto-char beg)
  6692. (while (re-search-forward re end t)
  6693. (setq day (time-to-days (org-time-string-to-time
  6694. (substring (match-string 1) 0 10))))
  6695. (or (memq day dates) (push day dates)))
  6696. (unless no-ranges
  6697. (goto-char beg)
  6698. (while (re-search-forward org-tr-regexp end t)
  6699. (setq ts1 (substring (match-string 1) 0 10)
  6700. ts2 (substring (match-string 2) 0 10)
  6701. day1 (time-to-days (org-time-string-to-time ts1))
  6702. day2 (time-to-days (org-time-string-to-time ts2)))
  6703. (while (< (setq day1 (1+ day1)) day2)
  6704. (or (memq day1 dates) (push day1 dates)))))
  6705. (setq dates (sort dates '<))
  6706. (when empty
  6707. (while (setq day (pop dates))
  6708. (setq day2 (car dates))
  6709. (push day dates1)
  6710. (when (and day2 empty)
  6711. (if (or (eq empty t)
  6712. (and (numberp empty) (<= (- day2 day) empty)))
  6713. (while (< (setq day (1+ day)) day2)
  6714. (push (list day) dates1))
  6715. (push (cons :omitted (- day2 day)) dates1))))
  6716. (setq dates (nreverse dates1)))
  6717. dates)))
  6718. ;;;###autoload
  6719. (defun org-diary (&rest args)
  6720. "Return diary information from org-files.
  6721. This function can be used in a \"sexp\" diary entry in the Emacs calendar.
  6722. It accesses org files and extracts information from those files to be
  6723. listed in the diary. The function accepts arguments specifying what
  6724. items should be listed. The following arguments are allowed:
  6725. :timestamp List the headlines of items containing a date stamp or
  6726. date range matching the selected date. Deadlines will
  6727. also be listed, on the expiration day.
  6728. :deadline List any deadlines past due, or due within
  6729. `org-deadline-warning-days'. The listing occurs only
  6730. in the diary for *today*, not at any other date. If
  6731. an entry is marked DONE, it is no longer listed.
  6732. :scheduled List all items which are scheduled for the given date.
  6733. The diary for *today* also contains items which were
  6734. scheduled earlier and are not yet marked DONE.
  6735. :todo List all TODO items from the org-file. This may be a
  6736. long list - so this is not turned on by default.
  6737. Like deadlines, these entries only show up in the
  6738. diary for *today*, not at any other date.
  6739. The call in the diary file should look like this:
  6740. &%%(org-diary) ~/path/to/some/orgfile.org
  6741. Use a separate line for each org file to check. Or, if you omit the file name,
  6742. all files listed in `org-agenda-files' will be checked automatically:
  6743. &%%(org-diary)
  6744. If you don't give any arguments (as in the example above), the default
  6745. arguments (:deadline :scheduled :timestamp) are used. So the example above may
  6746. also be written as
  6747. &%%(org-diary :deadline :timestamp :scheduled)
  6748. The function expects the lisp variables `entry' and `date' to be provided
  6749. by the caller, because this is how the calendar works. Don't use this
  6750. function from a program - use `org-agenda-get-day-entries' instead."
  6751. (org-agenda-maybe-reset-markers)
  6752. (org-compile-prefix-format org-agenda-prefix-format)
  6753. (setq args (or args '(:deadline :scheduled :timestamp)))
  6754. (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
  6755. (list entry)
  6756. (org-agenda-files t)))
  6757. file rtn results)
  6758. ;; If this is called during org-agenda, don't return any entries to
  6759. ;; the calendar. Org Agenda will list these entries itself.
  6760. (if org-disable-agenda-to-diary (setq files nil))
  6761. (while (setq file (pop files))
  6762. (setq rtn (apply 'org-agenda-get-day-entries file date args))
  6763. (setq results (append results rtn)))
  6764. (if results
  6765. (concat (org-finalize-agenda-entries results) "\n"))))
  6766. (defvar org-category-table nil)
  6767. (defun org-get-category-table ()
  6768. "Get the table of categories and positions in current buffer."
  6769. (let (tbl)
  6770. (save-excursion
  6771. (goto-char (point-min))
  6772. (while (re-search-forward "\\(^\\|\r\\)#\\+CATEGORY:[ \t]*\\(.*\\)" nil t)
  6773. (push (cons (point) (org-trim (match-string 2))) tbl)))
  6774. tbl))
  6775. (defun org-get-category (&optional pos)
  6776. "Get the category applying to position POS."
  6777. (if (not org-category-table)
  6778. (cond
  6779. ((null org-category)
  6780. (setq org-category
  6781. (if buffer-file-name
  6782. (file-name-sans-extension
  6783. (file-name-nondirectory buffer-file-name))
  6784. "???")))
  6785. ((symbolp org-category) (symbol-name org-category))
  6786. (t org-category))
  6787. (let ((tbl org-category-table)
  6788. (pos (or pos (point))))
  6789. (while (and tbl (> (caar tbl) pos))
  6790. (pop tbl))
  6791. (or (cdar tbl) (cdr (nth (1- (length org-category-table))
  6792. org-category-table))))))
  6793. (defun org-agenda-get-day-entries (file date &rest args)
  6794. "Does the work for `org-diary' and `org-agenda'.
  6795. FILE is the path to a file to be checked for entries. DATE is date like
  6796. the one returned by `calendar-current-date'. ARGS are symbols indicating
  6797. which kind of entries should be extracted. For details about these, see
  6798. the documentation of `org-diary'."
  6799. (setq args (or args '(:deadline :scheduled :timestamp)))
  6800. (let* ((org-startup-with-deadline-check nil)
  6801. (org-startup-folded nil)
  6802. (org-startup-align-all-tables nil)
  6803. (buffer (if (file-exists-p file)
  6804. (org-get-agenda-file-buffer file)
  6805. (error "No such file %s" file)))
  6806. arg results rtn)
  6807. (if (not buffer)
  6808. ;; If file does not exist, make sure an error message ends up in diary
  6809. (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  6810. (with-current-buffer buffer
  6811. (unless (org-mode-p)
  6812. (error "Agenda file %s is not in `org-mode'" file))
  6813. (setq org-category-table (org-get-category-table))
  6814. (let ((case-fold-search nil))
  6815. (save-excursion
  6816. (save-restriction
  6817. (if org-respect-restriction
  6818. (if (org-region-active-p)
  6819. ;; Respect a region to restrict search
  6820. (narrow-to-region (region-beginning) (region-end)))
  6821. ;; If we work for the calendar or many files,
  6822. ;; get rid of any restriction
  6823. (widen))
  6824. ;; The way we repeatedly append to `results' makes it O(n^2) :-(
  6825. (while (setq arg (pop args))
  6826. (cond
  6827. ((and (eq arg :todo)
  6828. (equal date (calendar-current-date)))
  6829. (setq rtn (org-agenda-get-todos))
  6830. (setq results (append results rtn)))
  6831. ((eq arg :timestamp)
  6832. (setq rtn (org-agenda-get-blocks))
  6833. (setq results (append results rtn))
  6834. (setq rtn (org-agenda-get-timestamps))
  6835. (setq results (append results rtn)))
  6836. ((eq arg :scheduled)
  6837. (setq rtn (org-agenda-get-scheduled))
  6838. (setq results (append results rtn)))
  6839. ((eq arg :closed)
  6840. (setq rtn (org-agenda-get-closed))
  6841. (setq results (append results rtn)))
  6842. ((and (eq arg :deadline)
  6843. (equal date (calendar-current-date)))
  6844. (setq rtn (org-agenda-get-deadlines))
  6845. (setq results (append results rtn))))))))
  6846. results))))
  6847. (defun org-entry-is-done-p ()
  6848. "Is the current entry marked DONE?"
  6849. (save-excursion
  6850. (and (re-search-backward "[\r\n]\\*" nil t)
  6851. (looking-at org-nl-done-regexp))))
  6852. (defun org-at-date-range-p ()
  6853. "Is the cursor inside a date range?"
  6854. (interactive)
  6855. (save-excursion
  6856. (catch 'exit
  6857. (let ((pos (point)))
  6858. (skip-chars-backward "^<\r\n")
  6859. (skip-chars-backward "<")
  6860. (and (looking-at org-tr-regexp)
  6861. (>= (match-end 0) pos)
  6862. (throw 'exit t))
  6863. (skip-chars-backward "^<\r\n")
  6864. (skip-chars-backward "<")
  6865. (and (looking-at org-tr-regexp)
  6866. (>= (match-end 0) pos)
  6867. (throw 'exit t)))
  6868. nil)))
  6869. (defun org-agenda-get-todos ()
  6870. "Return the TODO information for agenda display."
  6871. (let* ((props (list 'face nil
  6872. 'done-face 'org-done
  6873. 'org-not-done-regexp org-not-done-regexp
  6874. 'mouse-face 'highlight
  6875. 'keymap org-agenda-keymap
  6876. 'help-echo
  6877. (format "mouse-2 or RET jump to org file %s"
  6878. (abbreviate-file-name buffer-file-name))))
  6879. (regexp (concat "[\n\r]\\*+ *\\("
  6880. (if org-select-this-todo-keyword
  6881. (concat "\\<\\(" org-select-this-todo-keyword
  6882. "\\)\\>")
  6883. org-not-done-regexp)
  6884. "[^\n\r]*\\)"))
  6885. (sched-re (concat ".*\n?.*?" org-scheduled-time-regexp))
  6886. marker priority category tags
  6887. ee txt)
  6888. (goto-char (point-min))
  6889. (while (re-search-forward regexp nil t)
  6890. (catch :skip
  6891. (when (and org-agenda-todo-ignore-scheduled
  6892. (looking-at sched-re))
  6893. ;; FIXME: the following test also happens below, but we need it here
  6894. (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
  6895. (throw :skip nil))
  6896. (org-agenda-skip)
  6897. (goto-char (match-beginning 1))
  6898. (setq marker (org-agenda-new-marker (1+ (match-beginning 0)))
  6899. category (org-get-category)
  6900. tags (org-get-tags-at (point))
  6901. txt (org-format-agenda-item "" (match-string 1) category tags)
  6902. priority
  6903. (+ (org-get-priority txt)
  6904. (if org-todo-kwd-priority-p
  6905. (- org-todo-kwd-max-priority -2
  6906. (length
  6907. (member (match-string 2) org-todo-keywords)))
  6908. 1)))
  6909. (org-add-props txt props
  6910. 'org-marker marker 'org-hd-marker marker
  6911. 'priority priority 'category category)
  6912. (push txt ee)
  6913. (if org-agenda-todo-list-sublevels
  6914. (goto-char (match-end 1))
  6915. (org-end-of-subtree 'invisible))))
  6916. (nreverse ee)))
  6917. (defconst org-agenda-no-heading-message
  6918. "No heading for this item in buffer or region.")
  6919. (defun org-agenda-get-timestamps ()
  6920. "Return the date stamp information for agenda display."
  6921. (let* ((props (list 'face nil
  6922. 'org-not-done-regexp org-not-done-regexp
  6923. 'mouse-face 'highlight
  6924. 'keymap org-agenda-keymap
  6925. 'help-echo
  6926. (format "mouse-2 or RET jump to org file %s"
  6927. (abbreviate-file-name buffer-file-name))))
  6928. (regexp (regexp-quote
  6929. (substring
  6930. (format-time-string
  6931. (car org-time-stamp-formats)
  6932. (apply 'encode-time ; DATE bound by calendar
  6933. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  6934. 0 11)))
  6935. marker hdmarker deadlinep scheduledp donep tmp priority category
  6936. ee txt timestr tags)
  6937. (goto-char (point-min))
  6938. (while (re-search-forward regexp nil t)
  6939. (catch :skip
  6940. (and (save-match-data (org-at-date-range-p)) (throw :skip nil))
  6941. (org-agenda-skip)
  6942. (setq marker (org-agenda-new-marker (match-beginning 0))
  6943. category (org-get-category (match-beginning 0))
  6944. tmp (buffer-substring (max (point-min)
  6945. (- (match-beginning 0)
  6946. org-ds-keyword-length))
  6947. (match-beginning 0))
  6948. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  6949. deadlinep (string-match org-deadline-regexp tmp)
  6950. scheduledp (string-match org-scheduled-regexp tmp)
  6951. donep (org-entry-is-done-p))
  6952. (if (string-match ">" timestr)
  6953. ;; substring should only run to end of time stamp
  6954. (setq timestr (substring timestr 0 (match-end 0))))
  6955. (save-excursion
  6956. (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t)
  6957. (progn
  6958. (goto-char (match-end 1))
  6959. (setq hdmarker (org-agenda-new-marker)
  6960. tags (org-get-tags-at))
  6961. (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
  6962. (setq txt (org-format-agenda-item
  6963. (format "%s%s"
  6964. (if deadlinep "Deadline: " "")
  6965. (if scheduledp "Scheduled: " ""))
  6966. (match-string 1) category tags timestr)))
  6967. (setq txt org-agenda-no-heading-message))
  6968. (setq priority (org-get-priority txt))
  6969. (org-add-props txt props
  6970. 'org-marker marker 'org-hd-marker hdmarker)
  6971. (if deadlinep
  6972. (org-add-props txt nil
  6973. 'face (if donep 'org-done 'org-warning)
  6974. 'undone-face 'org-warning 'done-face 'org-done
  6975. 'category category 'priority (+ 100 priority))
  6976. (if scheduledp
  6977. (org-add-props txt nil
  6978. 'face 'org-scheduled-today
  6979. 'undone-face 'org-scheduled-today 'done-face 'org-done
  6980. 'category category 'priority (+ 99 priority))
  6981. (org-add-props txt nil 'priority priority 'category category)))
  6982. (push txt ee))
  6983. (outline-next-heading)))
  6984. (nreverse ee)))
  6985. (defun org-agenda-get-closed ()
  6986. "Return the logged TODO entries for agenda display."
  6987. (let* ((props (list 'mouse-face 'highlight
  6988. 'org-not-done-regexp org-not-done-regexp
  6989. 'keymap org-agenda-keymap
  6990. 'help-echo
  6991. (format "mouse-2 or RET jump to org file %s"
  6992. (abbreviate-file-name buffer-file-name))))
  6993. (regexp (concat
  6994. "\\<\\(" org-closed-string "\\|" org-clock-string "\\) *\\["
  6995. (regexp-quote
  6996. (substring
  6997. (format-time-string
  6998. (car org-time-stamp-formats)
  6999. (apply 'encode-time ; DATE bound by calendar
  7000. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  7001. 1 11))))
  7002. marker hdmarker priority category tags closedp
  7003. ee txt timestr)
  7004. (goto-char (point-min))
  7005. (while (re-search-forward regexp nil t)
  7006. (catch :skip
  7007. (org-agenda-skip)
  7008. (setq marker (org-agenda-new-marker (match-beginning 0))
  7009. closedp (equal (match-string 1) org-closed-string)
  7010. category (org-get-category (match-beginning 0))
  7011. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  7012. ;; donep (org-entry-is-done-p)
  7013. )
  7014. (if (string-match "\\]" timestr)
  7015. ;; substring should only run to end of time stamp
  7016. (setq timestr (substring timestr 0 (match-end 0))))
  7017. (save-excursion
  7018. (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t)
  7019. (progn
  7020. (goto-char (match-end 1))
  7021. (setq hdmarker (org-agenda-new-marker)
  7022. tags (org-get-tags-at))
  7023. (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
  7024. (setq txt (org-format-agenda-item
  7025. (if closedp "Closed: " "Clocked: ")
  7026. (match-string 1) category tags timestr)))
  7027. (setq txt org-agenda-no-heading-message))
  7028. (setq priority 100000)
  7029. (org-add-props txt props
  7030. 'org-marker marker 'org-hd-marker hdmarker 'face 'org-done
  7031. 'priority priority 'category category
  7032. 'undone-face 'org-warning 'done-face 'org-done)
  7033. (push txt ee))
  7034. (outline-next-heading)))
  7035. (nreverse ee)))
  7036. (defun org-agenda-get-deadlines ()
  7037. "Return the deadline information for agenda display."
  7038. (let* ((wdays org-deadline-warning-days)
  7039. (props (list 'mouse-face 'highlight
  7040. 'org-not-done-regexp org-not-done-regexp
  7041. 'keymap org-agenda-keymap
  7042. 'help-echo
  7043. (format "mouse-2 or RET jump to org file %s"
  7044. (abbreviate-file-name buffer-file-name))))
  7045. (regexp org-deadline-time-regexp)
  7046. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  7047. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  7048. d2 diff pos pos1 category tags
  7049. ee txt head face)
  7050. (goto-char (point-min))
  7051. (while (re-search-forward regexp nil t)
  7052. (catch :skip
  7053. (org-agenda-skip)
  7054. (setq pos (1- (match-beginning 1))
  7055. d2 (time-to-days
  7056. (org-time-string-to-time (match-string 1)))
  7057. diff (- d2 d1))
  7058. ;; When to show a deadline in the calendar:
  7059. ;; If the expiration is within wdays warning time.
  7060. ;; Past-due deadlines are only shown on the current date
  7061. (if (and (< diff wdays) todayp (not (= diff 0)))
  7062. (save-excursion
  7063. (setq category (org-get-category))
  7064. (if (re-search-backward "\\(^\\|\r\\)\\*+[ \t]*" nil t)
  7065. (progn
  7066. (goto-char (match-end 0))
  7067. (setq pos1 (match-end 1))
  7068. (setq tags (org-get-tags-at pos1))
  7069. (setq head (buffer-substring-no-properties
  7070. (point)
  7071. (progn (skip-chars-forward "^\r\n")
  7072. (point))))
  7073. (if (string-match org-looking-at-done-regexp head)
  7074. (setq txt nil)
  7075. (setq txt (org-format-agenda-item
  7076. (format "In %3d d.: " diff) head category tags))))
  7077. (setq txt org-agenda-no-heading-message))
  7078. (when txt
  7079. (setq face (cond ((<= diff 0) 'org-warning)
  7080. ((<= diff 5) 'org-upcoming-deadline)
  7081. (t nil)))
  7082. (org-add-props txt props
  7083. 'org-marker (org-agenda-new-marker pos)
  7084. 'org-hd-marker (org-agenda-new-marker pos1)
  7085. 'priority (+ (- 10 diff) (org-get-priority txt))
  7086. 'category category
  7087. 'face face 'undone-face face 'done-face 'org-done)
  7088. (push txt ee))))))
  7089. ee))
  7090. (defun org-agenda-get-scheduled ()
  7091. "Return the scheduled information for agenda display."
  7092. (let* ((props (list 'face 'org-scheduled-previously
  7093. 'org-not-done-regexp org-not-done-regexp
  7094. 'undone-face 'org-scheduled-previously
  7095. 'done-face 'org-done
  7096. 'mouse-face 'highlight
  7097. 'keymap org-agenda-keymap
  7098. 'help-echo
  7099. (format "mouse-2 or RET jump to org file %s"
  7100. (abbreviate-file-name buffer-file-name))))
  7101. (regexp org-scheduled-time-regexp)
  7102. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  7103. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  7104. d2 diff pos pos1 category tags
  7105. ee txt head)
  7106. (goto-char (point-min))
  7107. (while (re-search-forward regexp nil t)
  7108. (catch :skip
  7109. (org-agenda-skip)
  7110. (setq pos (1- (match-beginning 1))
  7111. d2 (time-to-days
  7112. (org-time-string-to-time (match-string 1)))
  7113. diff (- d2 d1))
  7114. ;; When to show a scheduled item in the calendar:
  7115. ;; If it is on or past the date.
  7116. (if (and (< diff 0) todayp)
  7117. (save-excursion
  7118. (setq category (org-get-category))
  7119. (if (re-search-backward "\\(^\\|\r\\)\\*+[ \t]*" nil t)
  7120. (progn
  7121. (goto-char (match-end 0))
  7122. (setq pos1 (match-end 1))
  7123. (setq tags (org-get-tags-at))
  7124. (setq head (buffer-substring-no-properties
  7125. (point)
  7126. (progn (skip-chars-forward "^\r\n") (point))))
  7127. (if (string-match org-looking-at-done-regexp head)
  7128. (setq txt nil)
  7129. (setq txt (org-format-agenda-item
  7130. (format "Sched.%2dx: " (- 1 diff)) head
  7131. category tags))))
  7132. (setq txt org-agenda-no-heading-message))
  7133. (when txt
  7134. (org-add-props txt props
  7135. 'org-marker (org-agenda-new-marker pos)
  7136. 'org-hd-marker (org-agenda-new-marker pos1)
  7137. 'priority (+ (- 5 diff) (org-get-priority txt))
  7138. 'category category)
  7139. (push txt ee))))))
  7140. ee))
  7141. (defun org-agenda-get-blocks ()
  7142. "Return the date-range information for agenda display."
  7143. (let* ((props (list 'face nil
  7144. 'org-not-done-regexp org-not-done-regexp
  7145. 'mouse-face 'highlight
  7146. 'keymap org-agenda-keymap
  7147. 'help-echo
  7148. (format "mouse-2 or RET jump to org file %s"
  7149. (abbreviate-file-name buffer-file-name))))
  7150. (regexp org-tr-regexp)
  7151. (d0 (calendar-absolute-from-gregorian date))
  7152. marker hdmarker ee txt d1 d2 s1 s2 timestr category tags pos)
  7153. (goto-char (point-min))
  7154. (while (re-search-forward regexp nil t)
  7155. (catch :skip
  7156. (org-agenda-skip)
  7157. (setq pos (point))
  7158. (setq timestr (match-string 0)
  7159. s1 (match-string 1)
  7160. s2 (match-string 2)
  7161. d1 (time-to-days (org-time-string-to-time s1))
  7162. d2 (time-to-days (org-time-string-to-time s2)))
  7163. (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
  7164. ;; Only allow days between the limits, because the normal
  7165. ;; date stamps will catch the limits.
  7166. (save-excursion
  7167. (setq marker (org-agenda-new-marker (point)))
  7168. (setq category (org-get-category))
  7169. (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t)
  7170. (progn
  7171. (setq hdmarker (org-agenda-new-marker (match-end 1)))
  7172. (goto-char (match-end 1))
  7173. (setq tags (org-get-tags-at))
  7174. (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
  7175. (setq txt (org-format-agenda-item
  7176. (format (if (= d1 d2) "" "(%d/%d): ")
  7177. (1+ (- d0 d1)) (1+ (- d2 d1)))
  7178. (match-string 1) category tags
  7179. (if (= d0 d1) timestr))))
  7180. (setq txt org-agenda-no-heading-message))
  7181. (org-add-props txt props
  7182. 'org-marker marker 'org-hd-marker hdmarker
  7183. 'priority (org-get-priority txt) 'category category)
  7184. (push txt ee)))
  7185. (goto-char pos)))
  7186. ; (outline-next-heading))) ;FIXME: correct to be removed??????
  7187. ;; Sort the entries by expiration date.
  7188. (nreverse ee)))
  7189. (defconst org-plain-time-of-day-regexp
  7190. (concat
  7191. "\\(\\<[012]?[0-9]"
  7192. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  7193. "\\(--?"
  7194. "\\(\\<[012]?[0-9]"
  7195. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  7196. "\\)?")
  7197. "Regular expression to match a plain time or time range.
  7198. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  7199. groups carry important information:
  7200. 0 the full match
  7201. 1 the first time, range or not
  7202. 8 the second time, if it is a range.")
  7203. (defconst org-stamp-time-of-day-regexp
  7204. (concat
  7205. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  7206. "\\([012][0-9]:[0-5][0-9]\\)>"
  7207. "\\(--?"
  7208. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  7209. "Regular expression to match a timestamp time or time range.
  7210. After a match, the following groups carry important information:
  7211. 0 the full match
  7212. 1 date plus weekday, for backreferencing to make sure both times on same day
  7213. 2 the first time, range or not
  7214. 4 the second time, if it is a range.")
  7215. (defvar org-prefix-has-time nil
  7216. "A flag, set by `org-compile-prefix-format'.
  7217. The flag is set if the currently compiled format contains a `%t'.")
  7218. (defvar org-prefix-has-tag nil
  7219. "A flag, set by `org-compile-prefix-format'.
  7220. The flag is set if the currently compiled format contains a `%T'.")
  7221. (defun org-format-agenda-item (extra txt &optional category tags dotime noprefix)
  7222. "Format TXT to be inserted into the agenda buffer.
  7223. In particular, it adds the prefix and corresponding text properties. EXTRA
  7224. must be a string and replaces the `%s' specifier in the prefix format.
  7225. CATEGORY (string, symbol or nil) may be used to overrule the default
  7226. category taken from local variable or file name. It will replace the `%c'
  7227. specifier in the format. DOTIME, when non-nil, indicates that a
  7228. time-of-day should be extracted from TXT for sorting of this entry, and for
  7229. the `%t' specifier in the format. When DOTIME is a string, this string is
  7230. searched for a time before TXT is. NOPREFIX is a flag and indicates that
  7231. only the correctly processes TXT should be returned - this is used by
  7232. `org-agenda-change-all-lines'. TAG can be the tag of the headline."
  7233. (save-match-data
  7234. ;; Diary entries sometimes have extra whitespace at the beginning
  7235. (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
  7236. (let* ((category (or category
  7237. org-category
  7238. (if buffer-file-name
  7239. (file-name-sans-extension
  7240. (file-name-nondirectory buffer-file-name))
  7241. "")))
  7242. (tag (if tags (nth (1- (length tags)) tags) ""))
  7243. time ;; needed for the eval of the prefix format
  7244. (ts (if dotime (concat (if (stringp dotime) dotime "") txt)))
  7245. (time-of-day (and dotime (org-get-time-of-day ts)))
  7246. stamp plain s0 s1 s2 rtn)
  7247. (when (and dotime time-of-day org-prefix-has-time)
  7248. ;; Extract starting and ending time and move them to prefix
  7249. (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
  7250. (setq plain (string-match org-plain-time-of-day-regexp ts)))
  7251. (setq s0 (match-string 0 ts)
  7252. s1 (match-string (if plain 1 2) ts)
  7253. s2 (match-string (if plain 8 4) ts))
  7254. ;; If the times are in TXT (not in DOTIMES), and the prefix will list
  7255. ;; them, we might want to remove them there to avoid duplication.
  7256. ;; The user can turn this off with a variable.
  7257. (if (and org-agenda-remove-times-when-in-prefix (or stamp plain)
  7258. (string-match (concat (regexp-quote s0) " *") txt)
  7259. (if (eq org-agenda-remove-times-when-in-prefix 'beg)
  7260. (= (match-beginning 0) 0)
  7261. t))
  7262. (setq txt (replace-match "" nil nil txt))))
  7263. ;; Normalize the time(s) to 24 hour
  7264. (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
  7265. (if s2 (setq s2 (org-get-time-of-day s2 'string t))))
  7266. (when (string-match "\\([ \t]+\\)\\(:[a-zA-Z_@0-9:]+:\\)[ \t]*$" txt)
  7267. ;; Tags are in the string
  7268. (if (or (eq org-agenda-remove-tags-when-in-prefix t)
  7269. (and org-agenda-remove-tags-when-in-prefix
  7270. org-prefix-has-tag))
  7271. (setq txt (replace-match "" t t txt))
  7272. (setq txt (replace-match
  7273. (concat (make-string (max (- 50 (length txt)) 1) ?\ )
  7274. (match-string 2 txt))
  7275. t t txt))))
  7276. ;; Create the final string
  7277. (if noprefix
  7278. (setq rtn txt)
  7279. ;; Prepare the variables needed in the eval of the compiled format
  7280. (setq time (cond (s2 (concat s1 "-" s2))
  7281. (s1 (concat s1 "......"))
  7282. (t ""))
  7283. extra (or extra "")
  7284. category (if (symbolp category) (symbol-name category) category))
  7285. ;; Evaluate the compiled format
  7286. (setq rtn (concat (eval org-prefix-format-compiled) txt)))
  7287. ;; And finally add the text properties
  7288. (org-add-props rtn nil
  7289. 'category (downcase category) 'tags tags
  7290. 'prefix-length (- (length rtn) (length txt))
  7291. 'time-of-day time-of-day
  7292. 'dotime dotime))))
  7293. (defun org-agenda-add-time-grid-maybe (list ndays todayp)
  7294. (catch 'exit
  7295. (cond ((not org-agenda-use-time-grid) (throw 'exit list))
  7296. ((and todayp (member 'today (car org-agenda-time-grid))))
  7297. ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
  7298. ((member 'weekly (car org-agenda-time-grid)))
  7299. (t (throw 'exit list)))
  7300. (let* ((have (delq nil (mapcar
  7301. (lambda (x) (get-text-property 1 'time-of-day x))
  7302. list)))
  7303. (string (nth 1 org-agenda-time-grid))
  7304. (gridtimes (nth 2 org-agenda-time-grid))
  7305. (req (car org-agenda-time-grid))
  7306. (remove (member 'remove-match req))
  7307. new time)
  7308. (if (and (member 'require-timed req) (not have))
  7309. ;; don't show empty grid
  7310. (throw 'exit list))
  7311. (while (setq time (pop gridtimes))
  7312. (unless (and remove (member time have))
  7313. (setq time (int-to-string time))
  7314. (push (org-format-agenda-item
  7315. nil string "" nil
  7316. (concat (substring time 0 -2) ":" (substring time -2)))
  7317. new)
  7318. (put-text-property
  7319. 1 (length (car new)) 'face 'org-time-grid (car new))))
  7320. (if (member 'time-up org-agenda-sorting-strategy)
  7321. (append new list)
  7322. (append list new)))))
  7323. (defun org-compile-prefix-format (format)
  7324. "Compile the prefix format into a Lisp form that can be evaluated.
  7325. The resulting form is returned and stored in the variable
  7326. `org-prefix-format-compiled'."
  7327. (setq org-prefix-has-time nil org-prefix-has-tag nil)
  7328. (let ((start 0) varform vars var (s format)e c f opt)
  7329. (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cts]\\)"
  7330. s start)
  7331. (setq var (cdr (assoc (match-string 4 s)
  7332. '(("c" . category) ("t" . time) ("s" . extra)
  7333. ("T" . tag))))
  7334. c (or (match-string 3 s) "")
  7335. opt (match-beginning 1)
  7336. start (1+ (match-beginning 0)))
  7337. (if (equal var 'time) (setq org-prefix-has-time t))
  7338. (if (equal var 'tag) (setq org-prefix-has-tag t))
  7339. (setq f (concat "%" (match-string 2 s) "s"))
  7340. (if opt
  7341. (setq varform
  7342. `(if (equal "" ,var)
  7343. ""
  7344. (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
  7345. (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
  7346. (setq s (replace-match "%s" t nil s))
  7347. (push varform vars))
  7348. (setq vars (nreverse vars))
  7349. (setq org-prefix-format-compiled `(format ,s ,@vars))))
  7350. (defun org-get-time-of-day (s &optional string mod24)
  7351. "Check string S for a time of day.
  7352. If found, return it as a military time number between 0 and 2400.
  7353. If not found, return nil.
  7354. The optional STRING argument forces conversion into a 5 character wide string
  7355. HH:MM."
  7356. (save-match-data
  7357. (when
  7358. (or
  7359. (string-match
  7360. "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
  7361. (string-match
  7362. "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
  7363. (let* ((h (string-to-number (match-string 1 s)))
  7364. (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
  7365. (ampm (if (match-end 4) (downcase (match-string 4 s))))
  7366. (am-p (equal ampm "am"))
  7367. (h1 (cond ((not ampm) h)
  7368. ((= h 12) (if am-p 0 12))
  7369. (t (+ h (if am-p 0 12)))))
  7370. (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
  7371. (mod h1 24) h1))
  7372. (t0 (+ (* 100 h2) m))
  7373. (t1 (concat (if (>= h1 24) "+" " ")
  7374. (if (< t0 100) "0" "")
  7375. (if (< t0 10) "0" "")
  7376. (int-to-string t0))))
  7377. (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
  7378. (defun org-finalize-agenda-entries (list)
  7379. "Sort and concatenate the agenda items."
  7380. (setq list (mapcar 'org-agenda-highlight-todo list))
  7381. (mapconcat 'identity (sort list 'org-entries-lessp) "\n"))
  7382. (defun org-agenda-highlight-todo (x)
  7383. (let (re pl)
  7384. (if (eq x 'line)
  7385. (save-excursion
  7386. (beginning-of-line 1)
  7387. (setq re (get-text-property (point) 'org-not-done-regexp))
  7388. (goto-char (+ (point) (get-text-property (point) 'prefix-length)))
  7389. (and (looking-at (concat "[ \t]*" re))
  7390. (add-text-properties (match-beginning 0) (match-end 0)
  7391. '(face org-todo))))
  7392. (setq re (get-text-property 0 'org-not-done-regexp x)
  7393. pl (get-text-property 0 'prefix-length x))
  7394. (and re (equal (string-match re x pl) pl)
  7395. (add-text-properties (match-beginning 0) (match-end 0)
  7396. '(face org-todo) x))
  7397. x)))
  7398. (defsubst org-cmp-priority (a b)
  7399. "Compare the priorities of string A and B."
  7400. (let ((pa (or (get-text-property 1 'priority a) 0))
  7401. (pb (or (get-text-property 1 'priority b) 0)))
  7402. (cond ((> pa pb) +1)
  7403. ((< pa pb) -1)
  7404. (t nil))))
  7405. (defsubst org-cmp-category (a b)
  7406. "Compare the string values of categories of strings A and B."
  7407. (let ((ca (or (get-text-property 1 'category a) ""))
  7408. (cb (or (get-text-property 1 'category b) "")))
  7409. (cond ((string-lessp ca cb) -1)
  7410. ((string-lessp cb ca) +1)
  7411. (t nil))))
  7412. (defsubst org-cmp-time (a b)
  7413. "Compare the time-of-day values of strings A and B."
  7414. (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
  7415. (ta (or (get-text-property 1 'time-of-day a) def))
  7416. (tb (or (get-text-property 1 'time-of-day b) def)))
  7417. (cond ((< ta tb) -1)
  7418. ((< tb ta) +1)
  7419. (t nil))))
  7420. (defun org-entries-lessp (a b)
  7421. "Predicate for sorting agenda entries."
  7422. ;; The following variables will be used when the form is evaluated.
  7423. (let* ((time-up (org-cmp-time a b))
  7424. (time-down (if time-up (- time-up) nil))
  7425. (priority-up (org-cmp-priority a b))
  7426. (priority-down (if priority-up (- priority-up) nil))
  7427. (category-up (org-cmp-category a b))
  7428. (category-down (if category-up (- category-up) nil))
  7429. (category-keep (if category-up +1 nil)))
  7430. (cdr (assoc
  7431. (eval (cons 'or org-agenda-sorting-strategy))
  7432. '((-1 . t) (1 . nil) (nil . nil))))))
  7433. (defun org-agenda-show-priority ()
  7434. "Show the priority of the current item.
  7435. This priority is composed of the main priority given with the [#A] cookies,
  7436. and by additional input from the age of a schedules or deadline entry."
  7437. (interactive)
  7438. (let* ((pri (get-text-property (point-at-bol) 'priority)))
  7439. (message "Priority is %d" (if pri pri -1000))))
  7440. (defun org-agenda-show-tags ()
  7441. "Show the tags applicable to the current item."
  7442. (interactive)
  7443. (let* ((tags (get-text-property (point-at-bol) 'tags)))
  7444. (if tags
  7445. (message "Tags are :%s:"
  7446. (org-no-properties (mapconcat 'identity tags ":")))
  7447. (message "No tags associated with this line"))))
  7448. (defun org-agenda-goto (&optional highlight)
  7449. "Go to the Org-mode file which contains the item at point."
  7450. (interactive)
  7451. (let* ((marker (or (get-text-property (point) 'org-marker)
  7452. (org-agenda-error)))
  7453. (buffer (marker-buffer marker))
  7454. (pos (marker-position marker)))
  7455. (switch-to-buffer-other-window buffer)
  7456. (widen)
  7457. (goto-char pos)
  7458. (when (org-mode-p)
  7459. (org-show-hidden-entry)
  7460. (save-excursion
  7461. (and (outline-next-heading)
  7462. (org-flag-heading nil)))) ; show the next heading
  7463. (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
  7464. (defun org-agenda-switch-to ()
  7465. "Go to the Org-mode file which contains the item at point."
  7466. (interactive)
  7467. (let* ((marker (or (get-text-property (point) 'org-marker)
  7468. (org-agenda-error)))
  7469. (buffer (marker-buffer marker))
  7470. (pos (marker-position marker)))
  7471. (switch-to-buffer buffer)
  7472. (delete-other-windows)
  7473. (widen)
  7474. (goto-char pos)
  7475. (when (org-mode-p)
  7476. (org-show-hidden-entry)
  7477. (save-excursion
  7478. (and (outline-next-heading)
  7479. (org-flag-heading nil)))))) ; show the next heading
  7480. (defun org-agenda-goto-mouse (ev)
  7481. "Go to the Org-mode file which contains the item at the mouse click."
  7482. (interactive "e")
  7483. (mouse-set-point ev)
  7484. (org-agenda-goto))
  7485. (defun org-agenda-show ()
  7486. "Display the Org-mode file which contains the item at point."
  7487. (interactive)
  7488. (let ((win (selected-window)))
  7489. (org-agenda-goto t)
  7490. (select-window win)))
  7491. (defun org-agenda-recenter (arg)
  7492. "Display the Org-mode file which contains the item at point and recenter."
  7493. (interactive "P")
  7494. (let ((win (selected-window)))
  7495. (org-agenda-goto t)
  7496. (recenter arg)
  7497. (select-window win)))
  7498. (defun org-agenda-show-mouse (ev)
  7499. "Display the Org-mode file which contains the item at the mouse click."
  7500. (interactive "e")
  7501. (mouse-set-point ev)
  7502. (org-agenda-show))
  7503. (defun org-agenda-check-no-diary ()
  7504. "Check if the entry is a diary link and abort if yes."
  7505. (if (get-text-property (point) 'org-agenda-diary-link)
  7506. (org-agenda-error)))
  7507. (defun org-agenda-error ()
  7508. (error "Command not allowed in this line"))
  7509. (defvar org-last-heading-marker (make-marker)
  7510. "Marker pointing to the headline that last changed its TODO state
  7511. by a remote command from the agenda.")
  7512. (defun org-agenda-todo (&optional arg)
  7513. "Cycle TODO state of line at point, also in Org-mode file.
  7514. This changes the line at point, all other lines in the agenda referring to
  7515. the same tree node, and the headline of the tree node in the Org-mode file."
  7516. (interactive "P")
  7517. (org-agenda-check-no-diary)
  7518. (let* ((col (current-column))
  7519. (marker (or (get-text-property (point) 'org-marker)
  7520. (org-agenda-error)))
  7521. (buffer (marker-buffer marker))
  7522. (pos (marker-position marker))
  7523. (hdmarker (get-text-property (point) 'org-hd-marker))
  7524. (buffer-read-only nil)
  7525. newhead)
  7526. (with-current-buffer buffer
  7527. (widen)
  7528. (goto-char pos)
  7529. (org-show-hidden-entry)
  7530. (save-excursion
  7531. (and (outline-next-heading)
  7532. (org-flag-heading nil))) ; show the next heading
  7533. (org-todo arg)
  7534. (and (bolp) (forward-char 1))
  7535. (setq newhead (org-get-heading))
  7536. (save-excursion
  7537. (org-back-to-heading)
  7538. (move-marker org-last-heading-marker (point))))
  7539. (beginning-of-line 1)
  7540. (save-excursion
  7541. (org-agenda-change-all-lines newhead hdmarker 'fixface))
  7542. (move-to-column col)))
  7543. (defun org-agenda-change-all-lines (newhead hdmarker &optional fixface)
  7544. "Change all lines in the agenda buffer which match HDMARKER.
  7545. The new content of the line will be NEWHEAD (as modified by
  7546. `org-format-agenda-item'). HDMARKER is checked with
  7547. `equal' against all `org-hd-marker' text properties in the file.
  7548. If FIXFACE is non-nil, the face of each item is modified acording to
  7549. the new TODO state."
  7550. (let* (props m pl undone-face done-face finish new dotime cat tags)
  7551. ; (setq newhead (org-format-agenda-item "x" newhead "x" nil 'noprefix))
  7552. (save-excursion
  7553. (goto-char (point-max))
  7554. (beginning-of-line 1)
  7555. (while (not finish)
  7556. (setq finish (bobp))
  7557. (when (and (setq m (get-text-property (point) 'org-hd-marker))
  7558. (equal m hdmarker))
  7559. (setq props (text-properties-at (point))
  7560. dotime (get-text-property (point) 'dotime)
  7561. cat (get-text-property (point) 'category)
  7562. tags (get-text-property (point) 'tags)
  7563. new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
  7564. pl (get-text-property (point) 'prefix-length)
  7565. undone-face (get-text-property (point) 'undone-face)
  7566. done-face (get-text-property (point) 'done-face))
  7567. (move-to-column pl)
  7568. (if (looking-at ".*")
  7569. (progn
  7570. (replace-match new t t)
  7571. (beginning-of-line 1)
  7572. (add-text-properties (point-at-bol) (point-at-eol) props)
  7573. (when fixface
  7574. (add-text-properties
  7575. (point-at-bol) (point-at-eol)
  7576. (list 'face
  7577. (if org-last-todo-state-is-todo
  7578. undone-face done-face)))
  7579. (org-agenda-highlight-todo 'line))
  7580. (beginning-of-line 1))
  7581. (error "Line update did not work")))
  7582. (beginning-of-line 0)))))
  7583. (defun org-agenda-priority-up ()
  7584. "Increase the priority of line at point, also in Org-mode file."
  7585. (interactive)
  7586. (org-agenda-priority 'up))
  7587. (defun org-agenda-priority-down ()
  7588. "Decrease the priority of line at point, also in Org-mode file."
  7589. (interactive)
  7590. (org-agenda-priority 'down))
  7591. (defun org-agenda-priority (&optional force-direction)
  7592. "Set the priority of line at point, also in Org-mode file.
  7593. This changes the line at point, all other lines in the agenda referring to
  7594. the same tree node, and the headline of the tree node in the Org-mode file."
  7595. (interactive)
  7596. (org-agenda-check-no-diary)
  7597. (let* ((marker (or (get-text-property (point) 'org-marker)
  7598. (org-agenda-error)))
  7599. (buffer (marker-buffer marker))
  7600. (pos (marker-position marker))
  7601. (hdmarker (get-text-property (point) 'org-hd-marker))
  7602. (buffer-read-only nil)
  7603. newhead)
  7604. (with-current-buffer buffer
  7605. (widen)
  7606. (goto-char pos)
  7607. (org-show-hidden-entry)
  7608. (save-excursion
  7609. (and (outline-next-heading)
  7610. (org-flag-heading nil))) ; show the next heading
  7611. (funcall 'org-priority force-direction)
  7612. (end-of-line 1)
  7613. (setq newhead (org-get-heading)))
  7614. (org-agenda-change-all-lines newhead hdmarker)
  7615. (beginning-of-line 1)))
  7616. (defun org-get-tags-at (&optional pos)
  7617. "Get a list of all headline tags applicable at POS.
  7618. POS defaults to point. If tags are inherited, the list contains
  7619. the targets in the same sequence as the headlines appear, i.e.
  7620. the tags of the current headline come last."
  7621. (interactive)
  7622. (let (tags)
  7623. (save-excursion
  7624. (goto-char (or pos (point)))
  7625. (save-match-data
  7626. (org-back-to-heading t)
  7627. (condition-case nil
  7628. (while t
  7629. (if (looking-at "[^\r\n]+?:\\([a-zA-Z_@0-9:]+\\):[ \t]*\\([\n\r]\\|\\'\\)")
  7630. (setq tags (append (org-split-string
  7631. (org-match-string-no-properties 1) ":")
  7632. tags)))
  7633. (or org-use-tag-inheritance (error ""))
  7634. (org-up-heading-all 1))
  7635. (error nil))))
  7636. (message "%s" tags)
  7637. tags))
  7638. (defun org-agenda-set-tags ()
  7639. "Set tags for the current headline."
  7640. (interactive)
  7641. (org-agenda-check-no-diary)
  7642. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  7643. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  7644. (org-agenda-error)))
  7645. (buffer (marker-buffer hdmarker))
  7646. (pos (marker-position hdmarker))
  7647. (buffer-read-only nil)
  7648. newhead)
  7649. (with-current-buffer buffer
  7650. (widen)
  7651. (goto-char pos)
  7652. (org-show-hidden-entry)
  7653. (save-excursion
  7654. (and (outline-next-heading)
  7655. (org-flag-heading nil))) ; show the next heading
  7656. (call-interactively 'org-set-tags)
  7657. (end-of-line 1)
  7658. (setq newhead (org-get-heading)))
  7659. (org-agenda-change-all-lines newhead hdmarker)
  7660. (beginning-of-line 1)))
  7661. (defun org-agenda-date-later (arg &optional what)
  7662. "Change the date of this item to one day later."
  7663. (interactive "p")
  7664. (org-agenda-check-type t 'agenda 'timeline)
  7665. (org-agenda-check-no-diary)
  7666. (let* ((marker (or (get-text-property (point) 'org-marker)
  7667. (org-agenda-error)))
  7668. (buffer (marker-buffer marker))
  7669. (pos (marker-position marker)))
  7670. (with-current-buffer buffer
  7671. (widen)
  7672. (goto-char pos)
  7673. (if (not (org-at-timestamp-p))
  7674. (error "Cannot find time stamp"))
  7675. (org-timestamp-change arg (or what 'day))
  7676. (message "Time stamp changed to %s" org-last-changed-timestamp))))
  7677. (defun org-agenda-date-earlier (arg &optional what)
  7678. "Change the date of this item to one day earlier."
  7679. (interactive "p")
  7680. (org-agenda-date-later (- arg) what))
  7681. (defun org-agenda-date-prompt (arg)
  7682. "Change the date of this item. Date is prompted for, with default today.
  7683. The prefix ARG is passed to the `org-time-stamp' command and can therefore
  7684. be used to request time specification in the time stamp."
  7685. (interactive "P")
  7686. (org-agenda-check-type t 'agenda 'timeline)
  7687. (org-agenda-check-no-diary)
  7688. (let* ((marker (or (get-text-property (point) 'org-marker)
  7689. (org-agenda-error)))
  7690. (buffer (marker-buffer marker))
  7691. (pos (marker-position marker)))
  7692. (with-current-buffer buffer
  7693. (widen)
  7694. (goto-char pos)
  7695. (if (not (org-at-timestamp-p))
  7696. (error "Cannot find time stamp"))
  7697. (org-time-stamp arg)
  7698. (message "Time stamp changed to %s" org-last-changed-timestamp))))
  7699. (defun org-agenda-schedule (arg)
  7700. "Schedule the item at point."
  7701. (interactive "P")
  7702. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  7703. (org-agenda-check-no-diary)
  7704. (let* ((marker (or (get-text-property (point) 'org-marker)
  7705. (org-agenda-error)))
  7706. (buffer (marker-buffer marker))
  7707. (pos (marker-position marker))
  7708. (org-insert-labeled-timestamps-at-point nil)
  7709. ts)
  7710. (with-current-buffer buffer
  7711. (widen)
  7712. (goto-char pos)
  7713. (setq ts (org-schedule))
  7714. (message "Item scheduled for %s" ts))))
  7715. (defun org-agenda-deadline (arg)
  7716. "Schedule the item at point."
  7717. (interactive "P")
  7718. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  7719. (org-agenda-check-no-diary)
  7720. (let* ((marker (or (get-text-property (point) 'org-marker)
  7721. (org-agenda-error)))
  7722. (buffer (marker-buffer marker))
  7723. (pos (marker-position marker))
  7724. (org-insert-labeled-timestamps-at-point nil)
  7725. ts)
  7726. (with-current-buffer buffer
  7727. (widen)
  7728. (goto-char pos)
  7729. (setq ts (org-deadline))
  7730. (message "Deadline for this item set to %s" ts))))
  7731. (defun org-get-heading ()
  7732. "Return the heading of the current entry, without the stars."
  7733. (save-excursion
  7734. (and (memq (char-before) '(?\n ?\r)) (skip-chars-forward "^\n\r"))
  7735. (if (and (re-search-backward "[\r\n]\\*" nil t)
  7736. (looking-at "[\r\n]\\*+[ \t]+\\([^\r\n]*\\)"))
  7737. (match-string 1)
  7738. "")))
  7739. (defun org-agenda-clock-in (&optional arg)
  7740. "Start the clock on the currently selected item."
  7741. (interactive "P")
  7742. (org-agenda-check-no-diary)
  7743. (let* ((marker (or (get-text-property (point) 'org-marker)
  7744. (org-agenda-error)))
  7745. (pos (marker-position marker)))
  7746. (with-current-buffer (marker-buffer marker)
  7747. (widen)
  7748. (goto-char pos)
  7749. (org-clock-in))))
  7750. (defun org-agenda-diary-entry ()
  7751. "Make a diary entry, like the `i' command from the calendar.
  7752. All the standard commands work: block, weekly etc."
  7753. (interactive)
  7754. (org-agenda-check-type t 'agenda 'timeline)
  7755. (require 'diary-lib)
  7756. (let* ((char (progn
  7757. (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
  7758. (read-char-exclusive)))
  7759. (cmd (cdr (assoc char
  7760. '((?d . insert-diary-entry)
  7761. (?w . insert-weekly-diary-entry)
  7762. (?m . insert-monthly-diary-entry)
  7763. (?y . insert-yearly-diary-entry)
  7764. (?a . insert-anniversary-diary-entry)
  7765. (?b . insert-block-diary-entry)
  7766. (?c . insert-cyclic-diary-entry)))))
  7767. (oldf (symbol-function 'calendar-cursor-to-date))
  7768. (point (point))
  7769. (mark (or (mark t) (point))))
  7770. (unless cmd
  7771. (error "No command associated with <%c>" char))
  7772. (unless (and (get-text-property point 'day)
  7773. (or (not (equal ?b char))
  7774. (get-text-property mark 'day)))
  7775. (error "Don't know which date to use for diary entry"))
  7776. ;; We implement this by hacking the `calendar-cursor-to-date' function
  7777. ;; and the `calendar-mark-ring' variable. Saves a lot of code.
  7778. (let ((calendar-mark-ring
  7779. (list (calendar-gregorian-from-absolute
  7780. (or (get-text-property mark 'day)
  7781. (get-text-property point 'day))))))
  7782. (unwind-protect
  7783. (progn
  7784. (fset 'calendar-cursor-to-date
  7785. (lambda (&optional error)
  7786. (calendar-gregorian-from-absolute
  7787. (get-text-property point 'day))))
  7788. (call-interactively cmd))
  7789. (fset 'calendar-cursor-to-date oldf)))))
  7790. (defun org-agenda-execute-calendar-command (cmd)
  7791. "Execute a calendar command from the agenda, with the date associated to
  7792. the cursor position."
  7793. (org-agenda-check-type t 'agenda 'timeline)
  7794. (require 'diary-lib)
  7795. (unless (get-text-property (point) 'day)
  7796. (error "Don't know which date to use for calendar command"))
  7797. (let* ((oldf (symbol-function 'calendar-cursor-to-date))
  7798. (point (point))
  7799. (date (calendar-gregorian-from-absolute
  7800. (get-text-property point 'day)))
  7801. (displayed-day (extract-calendar-day date))
  7802. (displayed-month (extract-calendar-month date))
  7803. (displayed-year (extract-calendar-year date)))
  7804. (unwind-protect
  7805. (progn
  7806. (fset 'calendar-cursor-to-date
  7807. (lambda (&optional error)
  7808. (calendar-gregorian-from-absolute
  7809. (get-text-property point 'day))))
  7810. (call-interactively cmd))
  7811. (fset 'calendar-cursor-to-date oldf))))
  7812. (defun org-agenda-phases-of-moon ()
  7813. "Display the phases of the moon for the 3 months around the cursor date."
  7814. (interactive)
  7815. (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
  7816. (defun org-agenda-holidays ()
  7817. "Display the holidays for the 3 months around the cursor date."
  7818. (interactive)
  7819. (org-agenda-execute-calendar-command 'list-calendar-holidays))
  7820. (defun org-agenda-sunrise-sunset (arg)
  7821. "Display sunrise and sunset for the cursor date.
  7822. Latitude and longitude can be specified with the variables
  7823. `calendar-latitude' and `calendar-longitude'. When called with prefix
  7824. argument, latitude and longitude will be prompted for."
  7825. (interactive "P")
  7826. (let ((calendar-longitude (if arg nil calendar-longitude))
  7827. (calendar-latitude (if arg nil calendar-latitude))
  7828. (calendar-location-name
  7829. (if arg "the given coordinates" calendar-location-name)))
  7830. (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
  7831. (defun org-agenda-goto-calendar ()
  7832. "Open the Emacs calendar with the date at the cursor."
  7833. (interactive)
  7834. (org-agenda-check-type t 'agenda 'timeline)
  7835. (let* ((day (or (get-text-property (point) 'day)
  7836. (error "Don't know which date to open in calendar")))
  7837. (date (calendar-gregorian-from-absolute day))
  7838. (calendar-move-hook nil)
  7839. (view-calendar-holidays-initially nil)
  7840. (view-diary-entries-initially nil))
  7841. (calendar)
  7842. (calendar-goto-date date)))
  7843. (defun org-calendar-goto-agenda ()
  7844. "Compute the Org-mode agenda for the calendar date displayed at the cursor.
  7845. This is a command that has to be installed in `calendar-mode-map'."
  7846. (interactive)
  7847. (org-agenda-list nil (calendar-absolute-from-gregorian
  7848. (calendar-cursor-to-date))
  7849. nil t))
  7850. (defun org-agenda-convert-date ()
  7851. (interactive)
  7852. (org-agenda-check-type t 'agenda 'timeline)
  7853. (let ((day (get-text-property (point) 'day))
  7854. date s)
  7855. (unless day
  7856. (error "Don't know which date to convert"))
  7857. (setq date (calendar-gregorian-from-absolute day))
  7858. (setq s (concat
  7859. "Gregorian: " (calendar-date-string date) "\n"
  7860. "ISO: " (calendar-iso-date-string date) "\n"
  7861. "Day of Yr: " (calendar-day-of-year-string date) "\n"
  7862. "Julian: " (calendar-julian-date-string date) "\n"
  7863. "Astron. JD: " (calendar-astro-date-string date)
  7864. " (Julian date number at noon UTC)\n"
  7865. "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
  7866. "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
  7867. "French: " (calendar-french-date-string date) "\n"
  7868. "Mayan: " (calendar-mayan-date-string date) "\n"
  7869. "Coptic: " (calendar-coptic-date-string date) "\n"
  7870. "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
  7871. "Persian: " (calendar-persian-date-string date) "\n"
  7872. "Chinese: " (calendar-chinese-date-string date) "\n"))
  7873. (with-output-to-temp-buffer "*Dates*"
  7874. (princ s))
  7875. (if (fboundp 'fit-window-to-buffer)
  7876. (fit-window-to-buffer (get-buffer-window "*Dates*")))))
  7877. ;;; Tags
  7878. (defun org-scan-tags (action matcher &optional todo-only)
  7879. "Scan headline tags with inheritance and produce output ACTION.
  7880. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  7881. evaluated, testing if a given set of tags qualifies a headline for
  7882. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  7883. are included in the output."
  7884. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  7885. (mapconcat 'regexp-quote
  7886. (nreverse (cdr (reverse org-todo-keywords)))
  7887. "\\|")
  7888. "\\>\\)\\)? *\\(.*?\\)\\(:[A-Za-z_@0-9:]+:\\)?[ \t]*[\n\r]"))
  7889. (props (list 'face nil
  7890. 'done-face 'org-done
  7891. 'undone-face nil
  7892. 'mouse-face 'highlight
  7893. 'keymap org-agenda-keymap
  7894. 'help-echo
  7895. (format "mouse-2 or RET jump to org file %s"
  7896. (abbreviate-file-name buffer-file-name))))
  7897. lspos
  7898. tags tags-list tags-alist (llast 0) rtn level category i txt
  7899. todo marker)
  7900. (save-excursion
  7901. (goto-char (point-min))
  7902. (when (eq action 'sparse-tree) (org-overview))
  7903. (while (re-search-forward re nil t)
  7904. (catch :skip
  7905. (and (eq action 'agenda) (org-agenda-skip))
  7906. (setq todo (if (match-end 1) (match-string 2))
  7907. tags (if (match-end 4) (match-string 4)))
  7908. (goto-char (setq lspos (1+ (match-beginning 0))))
  7909. (setq level (funcall outline-level)
  7910. category (org-get-category))
  7911. (setq i llast llast level)
  7912. ;; remove tag lists from same and sublevels
  7913. (while (>= i level)
  7914. (when (setq entry (assoc i tags-alist))
  7915. (setq tags-alist (delete entry tags-alist)))
  7916. (setq i (1- i)))
  7917. ;; add the nex tags
  7918. (when tags
  7919. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  7920. tags-alist
  7921. (cons (cons level tags) tags-alist)))
  7922. ;; compile tags for current headline
  7923. (setq tags-list
  7924. (if org-use-tag-inheritance
  7925. (apply 'append (mapcar 'cdr tags-alist))
  7926. tags))
  7927. (when (and (or (not todo-only) todo)
  7928. (eval matcher)
  7929. (or (not org-agenda-skip-archived-trees)
  7930. (not (member org-archive-tag tags-list))))
  7931. ;; list this headline
  7932. (if (eq action 'sparse-tree)
  7933. (progn
  7934. (org-show-hierarchy-above))
  7935. (setq txt (org-format-agenda-item
  7936. ""
  7937. (concat
  7938. (if org-tags-match-list-sublevels
  7939. (make-string (1- level) ?.) "")
  7940. (org-get-heading))
  7941. category tags-list))
  7942. (goto-char lspos)
  7943. (setq marker (org-agenda-new-marker))
  7944. (org-add-props txt props
  7945. 'org-marker marker 'org-hd-marker marker 'category category)
  7946. (push txt rtn))
  7947. ;; if we are to skip sublevels, jump to end of subtree
  7948. (point)
  7949. (or org-tags-match-list-sublevels (org-end-of-subtree))))))
  7950. (when (and (eq action 'sparse-tree)
  7951. (not org-sparse-tree-open-archived-trees))
  7952. (org-hide-archived-subtrees (point-min) (point-max)))
  7953. (nreverse rtn)))
  7954. (defun org-tags-sparse-tree (&optional arg match)
  7955. "Create a sparse tree according to tags search string MATCH.
  7956. MATCH can contain positive and negative selection of tags, like
  7957. \"+WORK+URGENT-WITHBOSS\"."
  7958. (interactive "P")
  7959. (let ((org-show-following-heading nil)
  7960. (org-show-hierarchy-above nil))
  7961. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)))))
  7962. (defun org-make-tags-matcher (match)
  7963. "Create the TAGS matcher form for the tags-selecting string MATCH."
  7964. (unless match
  7965. ;; Get a new match request, with completion
  7966. (setq org-last-tags-completion-table
  7967. (or (org-get-buffer-tags)
  7968. org-last-tags-completion-table))
  7969. (setq match (completing-read
  7970. "Tags: " 'org-tags-completion-function nil nil nil
  7971. 'org-tags-history)))
  7972. ;; parse the string and create a lisp form
  7973. (let ((match0 match) minus tag mm matcher orterms term orlist)
  7974. (setq orterms (org-split-string match "|"))
  7975. (while (setq term (pop orterms))
  7976. (while (string-match "^&?\\([-+:]\\)?\\([A-Za-z_@0-9]+\\)" term)
  7977. (setq minus (and (match-end 1)
  7978. (equal (match-string 1 term) "-"))
  7979. tag (match-string 2 term)
  7980. term (substring term (match-end 0))
  7981. mm (list 'member (downcase tag) 'tags-list)
  7982. mm (if minus (list 'not mm) mm))
  7983. (push mm matcher))
  7984. (push (if (> (length matcher) 1) (cons 'and matcher) (car matcher))
  7985. orlist)
  7986. (setq matcher nil))
  7987. (setq matcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  7988. ;; Return the string and lisp forms of the matcher
  7989. (cons match0 matcher)))
  7990. ;;;###autoload
  7991. (defun org-tags-view (&optional todo-only match keep-modes)
  7992. "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
  7993. The prefix arg TODO-ONLY limits the search to TODO entries."
  7994. (interactive "P")
  7995. (org-agenda-maybe-reset-markers 'force)
  7996. (org-compile-prefix-format org-agenda-prefix-format)
  7997. (let* ((org-agenda-keep-modes keep-modes)
  7998. (org-tags-match-list-sublevels
  7999. (if todo-only t org-tags-match-list-sublevels))
  8000. (win (selected-window))
  8001. (completion-ignore-case t)
  8002. rtn rtnall files file pos matcher
  8003. buffer)
  8004. (setq matcher (org-make-tags-matcher match)
  8005. match (car matcher) matcher (cdr matcher))
  8006. (if (not (equal (current-buffer) (get-buffer org-agenda-buffer-name)))
  8007. (progn
  8008. (delete-other-windows)
  8009. (switch-to-buffer-other-window
  8010. (get-buffer-create org-agenda-buffer-name))))
  8011. (setq buffer-read-only nil)
  8012. (erase-buffer)
  8013. (org-agenda-mode) (setq buffer-read-only nil)
  8014. (org-set-local 'org-agenda-type 'tags)
  8015. (org-set-local 'org-agenda-redo-command
  8016. (list 'org-tags-view (list 'quote todo-only)
  8017. (list 'if 'current-prefix-arg nil match) t))
  8018. (setq files (org-agenda-files)
  8019. rtnall nil)
  8020. (org-prepare-agenda-buffers files)
  8021. (while (setq file (pop files))
  8022. (catch 'nextfile
  8023. (org-check-agenda-file file)
  8024. (setq buffer (if (file-exists-p file)
  8025. (org-get-agenda-file-buffer file)
  8026. (error "No such file %s" file)))
  8027. (if (not buffer)
  8028. ;; If file does not exist, merror message to agenda
  8029. (setq rtn (list
  8030. (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  8031. rtnall (append rtnall rtn))
  8032. (with-current-buffer buffer
  8033. (unless (org-mode-p)
  8034. (error "Agenda file %s is not in `org-mode'" file))
  8035. (setq org-category-table (org-get-category-table))
  8036. (save-excursion
  8037. (save-restriction
  8038. (if org-respect-restriction
  8039. (if (org-region-active-p)
  8040. ;; Respect a region to restrict search
  8041. (narrow-to-region (region-beginning) (region-end)))
  8042. ;; If we work for the calendar or many files,
  8043. ;; get rid of any restriction
  8044. (widen))
  8045. (setq rtn (org-scan-tags 'agenda matcher todo-only))
  8046. (setq rtnall (append rtnall rtn))))))))
  8047. (insert "Headlines with TAGS match: ")
  8048. (add-text-properties (point-min) (1- (point))
  8049. (list 'face 'org-level-3))
  8050. (setq pos (point))
  8051. (insert match "\n")
  8052. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  8053. (setq pos (point))
  8054. (insert "Press `C-u r' to search again with new search string\n")
  8055. (add-text-properties pos (1- (point)) (list 'face 'org-level-3))
  8056. (when rtnall
  8057. (insert (mapconcat 'identity rtnall "\n")))
  8058. (goto-char (point-min))
  8059. (setq buffer-read-only t)
  8060. (org-fit-agenda-window)
  8061. (if (not org-select-agenda-window) (select-window win))))
  8062. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  8063. (defun org-set-tags (&optional arg just-align)
  8064. "Set the tags for the current headline.
  8065. With prefix ARG, realign all tags in headings in the current buffer."
  8066. (interactive "P")
  8067. (let* ((re (concat "^" outline-regexp))
  8068. (col (current-column))
  8069. (current (org-get-tags))
  8070. table current-tags inherited-tags ; computed below when needed
  8071. tags hd empty invis)
  8072. (if arg
  8073. (save-excursion
  8074. (goto-char (point-min))
  8075. (while (re-search-forward re nil t)
  8076. (org-set-tags nil t))
  8077. (message "All tags realigned to column %d" org-tags-column))
  8078. (if just-align
  8079. (setq tags current)
  8080. (setq table (or org-tag-alist (org-get-buffer-tags))
  8081. org-last-tags-completion-table table
  8082. current-tags (org-split-string current ":")
  8083. inherited-tags (nreverse
  8084. (nthcdr (length current-tags)
  8085. (nreverse (org-get-tags-at))))
  8086. tags
  8087. (if (or (eq t org-use-fast-tag-selection)
  8088. (and org-use-fast-tag-selection
  8089. (delq nil (mapcar 'cdr table))))
  8090. (org-fast-tag-selection current-tags inherited-tags table)
  8091. (let ((org-add-colon-after-tag-completion t))
  8092. (completing-read "Tags: " 'org-tags-completion-function
  8093. nil nil current 'org-tags-history))))
  8094. (while (string-match "[-+&]+" tags)
  8095. (setq tags (replace-match ":" t t tags))))
  8096. (unless (setq empty (string-match "\\`[\t ]*\\'" tags))
  8097. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  8098. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  8099. (if (equal current "")
  8100. (progn
  8101. (end-of-line 1)
  8102. (or empty (insert-before-markers " ")))
  8103. (beginning-of-line 1)
  8104. (setq invis (org-invisible-p))
  8105. (looking-at (concat "\\(.*\\)\\(" (regexp-quote current) "\\)[ \t]*"))
  8106. (setq hd (match-string 1))
  8107. (delete-region (match-beginning 0) (match-end 0))
  8108. (insert-before-markers (org-trim hd) (if empty "" " ")))
  8109. (if (equal tags "")
  8110. (save-excursion
  8111. (beginning-of-line 1)
  8112. (skip-chars-forward "*")
  8113. (if (= (char-after) ?\ ) (forward-char 1))
  8114. (and (re-search-forward "[ \t]+$" (point-at-eol) t)
  8115. (replace-match "")))
  8116. (move-to-column (max (current-column)
  8117. (if (> org-tags-column 0)
  8118. org-tags-column
  8119. (- (- org-tags-column) (length tags))))
  8120. t)
  8121. (insert-before-markers tags)
  8122. (if (and (not invis) (org-invisible-p))
  8123. (outline-flag-region (point-at-bol) (point) nil)))
  8124. (move-to-column col))))
  8125. (defun org-tags-completion-function (string predicate &optional flag)
  8126. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  8127. (confirm (lambda (x) (stringp (car x)))))
  8128. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  8129. (setq s1 (match-string 1 string)
  8130. s2 (match-string 2 string))
  8131. (setq s1 "" s2 string))
  8132. (cond
  8133. ((eq flag nil)
  8134. ;; try completion
  8135. (setq rtn (try-completion s2 ctable confirm))
  8136. (if (stringp rtn)
  8137. (concat s1 s2 (substring rtn (length s2))
  8138. (if (and org-add-colon-after-tag-completion
  8139. (assoc rtn ctable))
  8140. ":" "")))
  8141. )
  8142. ((eq flag t)
  8143. ;; all-completions
  8144. (all-completions s2 ctable confirm)
  8145. )
  8146. ((eq flag 'lambda)
  8147. ;; exact match?
  8148. (assoc s2 ctable)))
  8149. ))
  8150. (defun org-fast-tag-insert (kwd tags face &optional end)
  8151. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  8152. (insert (format "%-12s" (concat kwd ":"))
  8153. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  8154. (or end "")))
  8155. (defun org-fast-tag-selection (current inherited table)
  8156. "Fast tag selection with single keys.
  8157. CURRENT is the current list of tags in the headline, INHERITED is the
  8158. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  8159. possibly with grouping information.
  8160. If the keys are nil, a-z are automatically assigned.
  8161. Returns the new tags string, or nil to not change the current settings."
  8162. (let* ((maxlen (apply 'max (mapcar
  8163. (lambda (x)
  8164. (if (stringp (car x)) (string-width (car x)) 0))
  8165. table)))
  8166. (fwidth (+ maxlen 3 1 3))
  8167. (ncol (/ (- (window-width) 4) fwidth))
  8168. (i-face 'org-done)
  8169. (c-face 'org-tag)
  8170. tg cnt e c char c1 c2 ntable tbl rtn
  8171. groups ingroup)
  8172. (save-window-excursion
  8173. (delete-other-windows)
  8174. (split-window-vertically)
  8175. (switch-to-buffer-other-window (get-buffer-create " *Org tags*"))
  8176. (erase-buffer)
  8177. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  8178. (org-fast-tag-insert "Current" current c-face "\n\n")
  8179. (setq tbl table char ?a cnt 0)
  8180. (while (setq e (pop tbl))
  8181. (cond
  8182. ((equal e '(:startgroup))
  8183. (push '() groups) (setq ingroup t)
  8184. (when (not (= cnt 0))
  8185. (setq cnt 0)
  8186. (insert "\n"))
  8187. (insert "{ "))
  8188. ((equal e '(:endgroup))
  8189. (setq ingroup nil cnt 0)
  8190. (insert "}\n"))
  8191. (t
  8192. (setq tg (car e) c2 nil)
  8193. (if (cdr e)
  8194. (setq c (cdr e))
  8195. ;; automatically assign a character.
  8196. (setq c1 (string-to-char
  8197. (downcase (substring
  8198. tg (if (= (string-to-char tg) ?@) 1 0)))))
  8199. (if (or (rassoc c1 ntable) (rassoc c1 table))
  8200. (while (or (rassoc char ntable) (rassoc char table))
  8201. (setq char (1+ char)))
  8202. (setq c2 c1))
  8203. (setq c (or c2 char)))
  8204. (if ingroup (push tg (car groups)))
  8205. (setq tg (org-add-props tg nil 'face
  8206. (cond
  8207. ((member tg current) c-face)
  8208. ((member tg inherited) i-face)
  8209. (t nil))))
  8210. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8211. (insert "[" c "] " tg (make-string
  8212. (- fwidth 4 (length tg)) ?\ ))
  8213. (push (cons tg c) ntable)
  8214. (when (= (setq cnt (1+ cnt)) ncol)
  8215. (insert "\n")
  8216. (if ingroup (insert " "))
  8217. (setq cnt 0)))))
  8218. (setq ntable (nreverse ntable))
  8219. (insert "\n")
  8220. (goto-char (point-min))
  8221. (if (fboundp 'fit-window-to-buffer) (fit-window-to-buffer))
  8222. (setq rtn
  8223. (catch 'exit
  8224. (while t
  8225. (message "[key]:Toggle SPC: clear current RET accept%s"
  8226. (if groups " [!] ignore goups" ""))
  8227. (setq c (read-char-exclusive))
  8228. (cond
  8229. ((= c ?\r) (throw 'exit t))
  8230. ((= c ?!)
  8231. (setq groups nil)
  8232. (goto-char (point-min))
  8233. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  8234. ((or (= c ?\C-g)
  8235. (and (= c ?q) (not (rassoc c ntable))))
  8236. (setq quit-flag t))
  8237. ((= c ?\ ) (setq current nil))
  8238. ((setq e (rassoc c ntable) tg (car e))
  8239. (if (member tg current)
  8240. (setq current (delete tg current))
  8241. (loop for g in groups do
  8242. (if (member tg g)
  8243. (mapcar (lambda (x)
  8244. (setq current (delete x current)))
  8245. g)))
  8246. (setq current (cons tg current)))))
  8247. ;; Create a sorted list
  8248. (setq current
  8249. (sort current
  8250. (lambda (a b)
  8251. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  8252. (goto-char (point-min))
  8253. (beginning-of-line 2)
  8254. (delete-region (point) (point-at-eol))
  8255. (org-fast-tag-insert "Current" current c-face)
  8256. (while (re-search-forward "\\[.\\] \\([a-zA-Z0-9_@]+\\)" nil t)
  8257. (setq tg (match-string 1))
  8258. (add-text-properties (match-beginning 1) (match-end 1)
  8259. (list 'face
  8260. (cond
  8261. ((member tg current) c-face)
  8262. ((member tg inherited) i-face)
  8263. (t nil)))))
  8264. (goto-char (point-min)))))
  8265. (if rtn
  8266. (mapconcat 'identity current ":")
  8267. nil))))
  8268. (defun org-get-tags ()
  8269. "Get the TAGS string in the current headline."
  8270. (unless (org-on-heading-p)
  8271. (error "Not on a heading"))
  8272. (save-excursion
  8273. (beginning-of-line 1)
  8274. (if (looking-at ".*[ \t]\\(:[A-Za-z_@0-9:]+:\\)[ \t]*\\(\r\\|$\\)")
  8275. (org-match-string-no-properties 1)
  8276. "")))
  8277. (defun org-get-buffer-tags ()
  8278. "Get a table of all tags used in the buffer, for completion."
  8279. (let (tags)
  8280. (save-excursion
  8281. (goto-char (point-min))
  8282. (while (re-search-forward "[ \t]:\\([A-Za-z_@0-9:]+\\):[ \t\r\n]" nil t)
  8283. (mapc (lambda (x) (add-to-list 'tags x))
  8284. (org-split-string (org-match-string-no-properties 1) ":"))))
  8285. (mapcar 'list tags)))
  8286. ;;; Link Stuff
  8287. (defvar org-create-file-search-functions nil
  8288. "List of functions to construct the right search string for a file link.
  8289. These functions are called in turn with point at the location to
  8290. which the link should point.
  8291. A function in the hook should first test if it would like to
  8292. handle this file type, for example by checking the major-mode or
  8293. the file extension. If it decides not to handle this file, it
  8294. should just return nil to give other functions a chance. If it
  8295. does handle the file, it must return the search string to be used
  8296. when following the link. The search string will be part of the
  8297. file link, given after a double colon, and `org-open-at-point'
  8298. will automatically search for it. If special measures must be
  8299. taken to make the search successful, another function should be
  8300. added to the companion hook `org-execute-file-search-functions',
  8301. which see.
  8302. A function in this hook may also use `setq' to set the variable
  8303. `description' to provide a suggestion for the descriptive text to
  8304. be used for this link when it gets inserted into an Org-mode
  8305. buffer with \\[org-insert-link].")
  8306. (defvar org-execute-file-search-functions nil
  8307. "List of functions to execute a file search triggered by a link.
  8308. Functions added to this hook must accept a single argument, the
  8309. search string that was part of the file link, the part after the
  8310. double colon. The function must first check if it would like to
  8311. handle this search, for example by checking the major-mode or the
  8312. file extension. If it decides not to handle this search, it
  8313. should just return nil to give other functions a chance. If it
  8314. does handle the search, it must return a non-nil value to keep
  8315. other functions from trying.
  8316. Each function can access the current prefix argument through the
  8317. variable `current-prefix-argument'. Note that a single prefix is
  8318. used to force opening a link in Emacs, so it may be good to only
  8319. use a numeric or double prefix to guide the search function.
  8320. In case this is needed, a function in this hook can also restore
  8321. the window configuration before `org-open-at-point' was called using:
  8322. (set-window-configuration org-window-config-before-follow-link)")
  8323. (defun org-find-file-at-mouse (ev)
  8324. "Open file link or URL at mouse."
  8325. (interactive "e")
  8326. (mouse-set-point ev)
  8327. (org-open-at-point 'in-emacs))
  8328. (defun org-open-at-mouse (ev)
  8329. "Open file link or URL at mouse."
  8330. (interactive "e")
  8331. (mouse-set-point ev)
  8332. (org-open-at-point))
  8333. (defvar org-window-config-before-follow-link nil
  8334. "The window configuration before following a link.
  8335. This is saved in case the need arises to restore it.")
  8336. (defun org-open-at-point (&optional in-emacs)
  8337. "Open link at or after point.
  8338. If there is no link at point, this function will search forward up to
  8339. the end of the current subtree.
  8340. Normally, files will be opened by an appropriate application. If the
  8341. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  8342. (interactive "P")
  8343. (setq org-window-config-before-follow-link (current-window-configuration))
  8344. (org-remove-occur-highlights nil nil t)
  8345. (if (org-at-timestamp-p)
  8346. (org-agenda-list nil (time-to-days (org-time-string-to-time
  8347. (substring (match-string 1) 0 10)))
  8348. 1)
  8349. (let (type path link line search (pos (point)))
  8350. (catch 'match
  8351. (save-excursion
  8352. (skip-chars-forward "^]\n\r")
  8353. (when (and (re-search-backward "\\[\\[" nil t)
  8354. (looking-at org-bracket-link-regexp)
  8355. (<= (match-beginning 0) pos)
  8356. (>= (match-end 0) pos))
  8357. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  8358. (while (string-match " *\n *" link)
  8359. (setq link (replace-match " " t t link)))
  8360. (if (string-match org-link-re-with-space2 link)
  8361. (setq type (match-string 1 link)
  8362. path (match-string 2 link))
  8363. (setq type "thisfile"
  8364. path link))
  8365. (throw 'match t)))
  8366. (when (get-text-property (point) 'org-linked-text)
  8367. (setq type "thisfile"
  8368. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8369. (1+ (point)) (point))
  8370. path (buffer-substring
  8371. (previous-single-property-change pos 'org-linked-text)
  8372. (next-single-property-change pos 'org-linked-text)))
  8373. (throw 'match t))
  8374. (save-excursion
  8375. (skip-chars-backward (concat "^[]" org-non-link-chars " "))
  8376. (if (equal (char-before) ?<) (backward-char 1))
  8377. (when (or (looking-at org-angle-link-re)
  8378. (looking-at org-plain-link-re)
  8379. (and (or (re-search-forward org-angle-link-re (point-at-eol) t)
  8380. (re-search-forward org-plain-link-re (point-at-eol) t))
  8381. (<= (match-beginning 0) pos)
  8382. (>= (match-end 0) pos)))
  8383. (setq type (match-string 1)
  8384. path (match-string 2))
  8385. (throw 'match t)))
  8386. (save-excursion
  8387. (skip-chars-backward "^ \t\n\r")
  8388. (when (looking-at "\\(:[A-Za-z_@0-9:]+\\):[ \t\r\n]")
  8389. (setq type "tags"
  8390. path (match-string 1))
  8391. (while (string-match ":" path)
  8392. (setq path (replace-match "+" t t path)))
  8393. (throw 'match t)))
  8394. (save-excursion
  8395. (skip-chars-backward "a-zA-Z_")
  8396. (when (and (memq 'camel org-activate-links)
  8397. (looking-at org-camel-regexp))
  8398. (setq type "camel" path (match-string 0))
  8399. (if (equal (char-before) ?*)
  8400. (setq path (concat "*" path))))
  8401. (throw 'match t)))
  8402. (unless path
  8403. (error "No link found"))
  8404. ;; Remove any trailing spaces in path
  8405. (if (string-match " +\\'" path)
  8406. (setq path (replace-match "" t t path)))
  8407. (cond
  8408. ((member type '("http" "https" "ftp" "mailto" "news"))
  8409. (browse-url (concat type ":" path)))
  8410. ((string= type "tags")
  8411. (org-tags-view in-emacs path))
  8412. ((or (string= type "camel")
  8413. (string= type "thisfile"))
  8414. (org-mark-ring-push)
  8415. (org-link-search
  8416. path
  8417. (cond ((equal in-emacs '(4)) 'occur)
  8418. ((equal in-emacs '(16)) 'org-occur)
  8419. (t nil))))
  8420. ((string= type "file")
  8421. (if (string-match "::\\([0-9]+\\)\\'" path)
  8422. (setq line (string-to-number (match-string 1 path))
  8423. path (substring path 0 (match-beginning 0)))
  8424. (if (string-match "::\\(.+\\)\\'" path)
  8425. (setq search (match-string 1 path)
  8426. path (substring path 0 (match-beginning 0)))))
  8427. (org-open-file path in-emacs line search))
  8428. ((string= type "news")
  8429. (org-follow-gnus-link path))
  8430. ((string= type "bbdb")
  8431. (org-follow-bbdb-link path))
  8432. ((string= type "info")
  8433. (org-follow-info-link path))
  8434. ((string= type "gnus")
  8435. (let (group article)
  8436. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  8437. (error "Error in Gnus link"))
  8438. (setq group (match-string 1 path)
  8439. article (match-string 3 path))
  8440. (org-follow-gnus-link group article)))
  8441. ((string= type "vm")
  8442. (let (folder article)
  8443. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  8444. (error "Error in VM link"))
  8445. (setq folder (match-string 1 path)
  8446. article (match-string 3 path))
  8447. ;; in-emacs is the prefix arg, will be interpreted as read-only
  8448. (org-follow-vm-link folder article in-emacs)))
  8449. ((string= type "wl")
  8450. (let (folder article)
  8451. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  8452. (error "Error in Wanderlust link"))
  8453. (setq folder (match-string 1 path)
  8454. article (match-string 3 path))
  8455. (org-follow-wl-link folder article)))
  8456. ((string= type "mhe")
  8457. (let (folder article)
  8458. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  8459. (error "Error in MHE link"))
  8460. (setq folder (match-string 1 path)
  8461. article (match-string 3 path))
  8462. (org-follow-mhe-link folder article)))
  8463. ((string= type "rmail")
  8464. (let (folder article)
  8465. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  8466. (error "Error in RMAIL link"))
  8467. (setq folder (match-string 1 path)
  8468. article (match-string 3 path))
  8469. (org-follow-rmail-link folder article)))
  8470. ((string= type "shell")
  8471. (let ((cmd path))
  8472. (while (string-match "@{" cmd)
  8473. (setq cmd (replace-match "<" t t cmd)))
  8474. (while (string-match "@}" cmd)
  8475. (setq cmd (replace-match ">" t t cmd)))
  8476. (if (or (not org-confirm-shell-link-function)
  8477. (funcall org-confirm-shell-link-function
  8478. (format "Execute \"%s\" in shell? "
  8479. (org-add-props cmd nil
  8480. 'face 'org-warning))))
  8481. (progn
  8482. (message "Executing %s" cmd)
  8483. (shell-command cmd))
  8484. (error "Abort"))))
  8485. ((string= type "elisp")
  8486. (let ((cmd path))
  8487. (if (or (not org-confirm-elisp-link-function)
  8488. (funcall org-confirm-elisp-link-function
  8489. (format "Execute \"%s\" as elisp? "
  8490. (org-add-props cmd nil
  8491. 'face 'org-warning))))
  8492. (message "%s => %s" cmd (eval (read cmd)))
  8493. (error "Abort"))))
  8494. (t
  8495. (browse-url-at-point))))))
  8496. (defun org-link-search (s &optional type)
  8497. "Search for a link search option.
  8498. When S is a CamelCaseWord, search for a target, or for a sentence containing
  8499. the words. If S is surrounded by forward slashes, it is interpreted as a
  8500. regular expression. In org-mode files, this will create an `org-occur'
  8501. sparse tree. In ordinary files, `occur' will be used to list matches.
  8502. If the current buffer is in `dired-mode', grep will be used to search
  8503. in all files."
  8504. (let ((case-fold-search t)
  8505. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  8506. (pos (point))
  8507. (pre "") (post "")
  8508. words re0 re1 re2 re3 re4 re5 re2a reall camel)
  8509. (cond
  8510. ;; First check if there are any special
  8511. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  8512. ;; Now try the builtin stuff
  8513. ((save-excursion
  8514. (goto-char (point-min))
  8515. (and
  8516. (re-search-forward
  8517. (concat "<<" (regexp-quote s0) ">>") nil t)
  8518. (setq pos (match-beginning 0))))
  8519. ;; There is an exact target for this
  8520. (goto-char pos))
  8521. ((string-match "^/\\(.*\\)/$" s)
  8522. ;; A regular expression
  8523. (cond
  8524. ((org-mode-p)
  8525. (org-occur (match-string 1 s)))
  8526. ;;((eq major-mode 'dired-mode)
  8527. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  8528. (t (org-do-occur (match-string 1 s)))))
  8529. ((or (setq camel (string-match (concat "^" org-camel-regexp "$") s))
  8530. t)
  8531. ;; A camel or a normal search string
  8532. (when (equal (string-to-char s) ?*)
  8533. ;; Anchor on headlines, post may include tags.
  8534. (setq pre "^\\*+[ \t]*\\(?:\\sw+\\)?[ \t]*"
  8535. post "[ \t]*\\(?:[ \t]+:[a-zA-Z_@0-9:+]:[ \t]*\\)?$"
  8536. s (substring s 1)))
  8537. (remove-text-properties
  8538. 0 (length s)
  8539. '(face nil mouse-face nil keymap nil fontified nil) s)
  8540. ;; Make a series of regular expressions to find a match
  8541. (setq words
  8542. (if camel
  8543. (org-camel-to-words s)
  8544. (org-split-string s "[ \n\r\t]+"))
  8545. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  8546. re2 (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t]+") "\\)[ \t\r\n]")
  8547. re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  8548. re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  8549. re1 (concat pre re2 post)
  8550. re3 (concat pre re4 post)
  8551. re5 (concat pre ".*" re4)
  8552. re2 (concat pre re2)
  8553. re2a (concat pre re2a)
  8554. re4 (concat pre re4)
  8555. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  8556. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  8557. re5 "\\)"
  8558. ))
  8559. (cond
  8560. ((eq type 'org-occur) (org-occur reall))
  8561. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  8562. (t (goto-char (point-min))
  8563. (if (or (org-search-not-link re0 nil t)
  8564. (org-search-not-link re1 nil t)
  8565. (org-search-not-link re2 nil t)
  8566. (org-search-not-link re2a nil t)
  8567. (org-search-not-link re3 nil t)
  8568. (org-search-not-link re4 nil t)
  8569. (org-search-not-link re5 nil t)
  8570. )
  8571. (goto-char (match-beginning 1))
  8572. (goto-char pos)
  8573. (error "No match")))))
  8574. (t
  8575. ;; Normal string-search
  8576. (goto-char (point-min))
  8577. (if (search-forward s nil t)
  8578. (goto-char (match-beginning 0))
  8579. (error "No match"))))
  8580. (and (org-mode-p) (org-show-hierarchy-above))))
  8581. (defun org-search-not-link (&rest args)
  8582. "Execute `re-search-forward', but only accept matches that are not a link."
  8583. (catch 'exit
  8584. (let (p1)
  8585. (while (apply 're-search-forward args)
  8586. (setq p1 (point))
  8587. (if (not (save-match-data
  8588. (and (re-search-backward "\\[\\[" nil t)
  8589. (looking-at org-bracket-link-regexp)
  8590. (<= (match-beginning 0) p1)
  8591. (>= (match-end 0) p1))))
  8592. (progn (goto-char (match-end 0))
  8593. (throw 'exit (point)))
  8594. (goto-char (match-end 0)))))))
  8595. (defun org-do-occur (regexp &optional cleanup)
  8596. "Call the Emacs command `occur'.
  8597. If CLEANUP is non-nil, remove the printout of the regular expression
  8598. in the *Occur* buffer. This is useful if the regex is long and not useful
  8599. to read."
  8600. (occur regexp)
  8601. (when cleanup
  8602. (let ((cwin (selected-window)) win beg end)
  8603. (when (setq win (get-buffer-window "*Occur*"))
  8604. (select-window win))
  8605. (goto-char (point-min))
  8606. (when (re-search-forward "match[a-z]+" nil t)
  8607. (setq beg (match-end 0))
  8608. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  8609. (setq end (1- (match-beginning 0)))))
  8610. (and beg end (let ((buffer-read-only)) (delete-region beg end)))
  8611. (goto-char (point-min))
  8612. (select-window cwin))))
  8613. (defvar org-mark-ring nil
  8614. "Mark ring for positions before jumps in Org-mode.")
  8615. (defvar org-mark-ring-last-goto nil
  8616. "Last position in the mark ring used to go back.")
  8617. ;; Fill and close the ring
  8618. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  8619. (loop for i from 1 to org-mark-ring-length do
  8620. (push (make-marker) org-mark-ring))
  8621. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  8622. org-mark-ring)
  8623. (defun org-mark-ring-push (&optional pos buffer)
  8624. "Put the current position or POS into the mark ring and rotate it."
  8625. (interactive)
  8626. (setq pos (or pos (point)))
  8627. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  8628. (move-marker (car org-mark-ring)
  8629. (or pos (point))
  8630. (or buffer (current-buffer)))
  8631. (message
  8632. (substitute-command-keys
  8633. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  8634. (defun org-mark-ring-goto (&optional n)
  8635. "Jump to the previous position in the mark ring.
  8636. With prefix arg N, jump back that many stored positions. When
  8637. called several times in succession, walk through the entire ring.
  8638. Org-mode commands jumping to a different position in the current file,
  8639. or to another Org-mode file, automatically push the old position
  8640. onto the ring."
  8641. (interactive "p")
  8642. (let (p m)
  8643. (if (eq last-command this-command)
  8644. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  8645. (setq p org-mark-ring))
  8646. (setq org-mark-ring-last-goto p)
  8647. (setq m (car p))
  8648. (switch-to-buffer (marker-buffer m))
  8649. (goto-char m)
  8650. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-hierarchy-above))))
  8651. (defun org-camel-to-words (s)
  8652. "Split \"CamelCaseWords\" to (\"Camel\" \"Case\" \"Words\")."
  8653. (let ((case-fold-search nil)
  8654. words)
  8655. (while (string-match "[a-z][A-Z]" s)
  8656. (push (substring s 0 (1+ (match-beginning 0))) words)
  8657. (setq s (substring s (1+ (match-beginning 0)))))
  8658. (nreverse (cons s words))))
  8659. (defun org-remove-angle-brackets (s)
  8660. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  8661. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  8662. s)
  8663. (defun org-add-angle-brackets (s)
  8664. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  8665. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  8666. s)
  8667. (defun org-follow-bbdb-link (name)
  8668. "Follow a BBDB link to NAME."
  8669. (require 'bbdb)
  8670. (let ((inhibit-redisplay t)
  8671. (bbdb-electric-p nil))
  8672. (catch 'exit
  8673. ;; Exact match on name
  8674. (bbdb-name (concat "\\`" name "\\'") nil)
  8675. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  8676. ;; Exact match on name
  8677. (bbdb-company (concat "\\`" name "\\'") nil)
  8678. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  8679. ;; Partial match on name
  8680. (bbdb-name name nil)
  8681. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  8682. ;; Partial match on company
  8683. (bbdb-company name nil)
  8684. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  8685. ;; General match including network address and notes
  8686. (bbdb name nil)
  8687. (when (= 0 (buffer-size (get-buffer "*BBDB*")))
  8688. (delete-window (get-buffer-window "*BBDB*"))
  8689. (error "No matching BBDB record")))))
  8690. (defun org-follow-info-link (name)
  8691. "Follow an info file & node link to NAME."
  8692. (if (or (string-match "\\(.*\\)::?\\(.*\\)" name)
  8693. (string-match "\\(.*\\)" name))
  8694. (progn
  8695. (require 'info)
  8696. (if (match-string 2 name) ; If there isn't a node, choose "Top"
  8697. (Info-find-node (match-string 1 name) (match-string 2 name))
  8698. (Info-find-node (match-string 1 name) "Top")))
  8699. (message (concat "Could not open: " name))))
  8700. (defun org-follow-gnus-link (&optional group article)
  8701. "Follow a Gnus link to GROUP and ARTICLE."
  8702. (require 'gnus)
  8703. (funcall (cdr (assq 'gnus org-link-frame-setup)))
  8704. (if group (gnus-fetch-group group))
  8705. (if article
  8706. (or (gnus-summary-goto-article article nil 'force)
  8707. (if (fboundp 'gnus-summary-insert-cached-articles)
  8708. (progn
  8709. (gnus-summary-insert-cached-articles)
  8710. (gnus-summary-goto-article article nil 'force))
  8711. (message "Message could not be found.")))))
  8712. (defun org-follow-vm-link (&optional folder article readonly)
  8713. "Follow a VM link to FOLDER and ARTICLE."
  8714. (require 'vm)
  8715. (setq article (org-add-angle-brackets article))
  8716. (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
  8717. ;; ange-ftp or efs or tramp access
  8718. (let ((user (or (match-string 1 folder) (user-login-name)))
  8719. (host (match-string 2 folder))
  8720. (file (match-string 3 folder)))
  8721. (cond
  8722. ((featurep 'tramp)
  8723. ;; use tramp to access the file
  8724. (if (featurep 'xemacs)
  8725. (setq folder (format "[%s@%s]%s" user host file))
  8726. (setq folder (format "/%s@%s:%s" user host file))))
  8727. (t
  8728. ;; use ange-ftp or efs
  8729. (require (if (featurep 'xemacs) 'efs 'ange-ftp))
  8730. (setq folder (format "/%s@%s:%s" user host file))))))
  8731. (when folder
  8732. (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
  8733. (sit-for 0.1)
  8734. (when article
  8735. (vm-select-folder-buffer)
  8736. (widen)
  8737. (let ((case-fold-search t))
  8738. (goto-char (point-min))
  8739. (if (not (re-search-forward
  8740. (concat "^" "message-id: *" (regexp-quote article))))
  8741. (error "Could not find the specified message in this folder"))
  8742. (vm-isearch-update)
  8743. (vm-isearch-narrow)
  8744. (vm-beginning-of-message)
  8745. (vm-summarize)))))
  8746. (defun org-follow-wl-link (folder article)
  8747. "Follow a Wanderlust link to FOLDER and ARTICLE."
  8748. (setq article (org-add-angle-brackets article))
  8749. (wl-summary-goto-folder-subr folder 'no-sync t nil t)
  8750. (if article (wl-summary-jump-to-msg-by-message-id article ">"))
  8751. (wl-summary-redisplay))
  8752. (defun org-follow-rmail-link (folder article)
  8753. "Follow an RMAIL link to FOLDER and ARTICLE."
  8754. (setq article (org-add-angle-brackets article))
  8755. (let (message-number)
  8756. (save-excursion
  8757. (save-window-excursion
  8758. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  8759. (setq message-number
  8760. (save-restriction
  8761. (widen)
  8762. (goto-char (point-max))
  8763. (if (re-search-backward
  8764. (concat "^Message-ID:\\s-+" (regexp-quote
  8765. (or article "")))
  8766. nil t)
  8767. (rmail-what-message))))))
  8768. (if message-number
  8769. (progn
  8770. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  8771. (rmail-show-message message-number)
  8772. message-number)
  8773. (error "Message not found"))))
  8774. ;; mh-e integration based on planner-mode
  8775. (defun org-mhe-get-message-real-folder ()
  8776. "Return the name of the current message real folder, so if you use
  8777. sequences, it will now work."
  8778. (save-excursion
  8779. (let* ((folder
  8780. (if (equal major-mode 'mh-folder-mode)
  8781. mh-current-folder
  8782. ;; Refer to the show buffer
  8783. mh-show-folder-buffer))
  8784. (end-index
  8785. (if (boundp 'mh-index-folder)
  8786. (min (length mh-index-folder) (length folder))))
  8787. )
  8788. ;; a simple test on mh-index-data does not work, because
  8789. ;; mh-index-data is always nil in a show buffer.
  8790. (if (and (boundp 'mh-index-folder)
  8791. (string= mh-index-folder (substring folder 0 end-index)))
  8792. (if (equal major-mode 'mh-show-mode)
  8793. (save-window-excursion
  8794. (when (buffer-live-p (get-buffer folder))
  8795. (progn
  8796. (pop-to-buffer folder)
  8797. (org-mhe-get-message-folder-from-index)
  8798. )
  8799. ))
  8800. (org-mhe-get-message-folder-from-index)
  8801. )
  8802. folder
  8803. )
  8804. )))
  8805. (defun org-mhe-get-message-folder-from-index ()
  8806. "Returns the name of the message folder in a index folder buffer."
  8807. (save-excursion
  8808. (mh-index-previous-folder)
  8809. (re-search-forward "^\\(+.*\\)$" nil t)
  8810. (message (match-string 1))))
  8811. (defun org-mhe-get-message-folder ()
  8812. "Return the name of the current message folder. Be careful if you
  8813. use sequences."
  8814. (save-excursion
  8815. (if (equal major-mode 'mh-folder-mode)
  8816. mh-current-folder
  8817. ;; Refer to the show buffer
  8818. mh-show-folder-buffer)))
  8819. (defun org-mhe-get-message-num ()
  8820. "Return the number of the current message. Be careful if you
  8821. use sequences."
  8822. (save-excursion
  8823. (if (equal major-mode 'mh-folder-mode)
  8824. (mh-get-msg-num nil)
  8825. ;; Refer to the show buffer
  8826. (mh-show-buffer-message-number))))
  8827. (defun org-mhe-get-header (header)
  8828. "Return a header of the message in folder mode. This will create a
  8829. show buffer for the corresponding message. If you have a more clever
  8830. idea..."
  8831. (let* ((folder (org-mhe-get-message-folder))
  8832. (num (org-mhe-get-message-num))
  8833. (buffer (get-buffer-create (concat "show-" folder)))
  8834. (header-field))
  8835. (with-current-buffer buffer
  8836. (mh-display-msg num folder)
  8837. (if (equal major-mode 'mh-folder-mode)
  8838. (mh-header-display)
  8839. (mh-show-header-display))
  8840. (set-buffer buffer)
  8841. (setq header-field (mh-get-header-field header))
  8842. (if (equal major-mode 'mh-folder-mode)
  8843. (mh-show)
  8844. (mh-show-show))
  8845. header-field)))
  8846. (defun org-follow-mhe-link (folder article)
  8847. "Follow an MHE link to FOLDER and ARTICLE.
  8848. If ARTICLE is nil FOLDER is shown. If the configuration variable
  8849. `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
  8850. ARTICLE is searched in all folders. Indexed searches (swish++,
  8851. namazu, and others supported by MH-E) will always search in all
  8852. folders."
  8853. (require 'mh-e)
  8854. (require 'mh-search)
  8855. (require 'mh-utils)
  8856. (mh-find-path)
  8857. (if (not article)
  8858. (mh-visit-folder (mh-normalize-folder-name folder))
  8859. (setq article (org-add-angle-brackets article))
  8860. (mh-search-choose)
  8861. (if (equal mh-searcher 'pick)
  8862. (progn
  8863. (mh-search folder (list "--message-id" article))
  8864. (when (and org-mhe-search-all-folders
  8865. (not (org-mhe-get-message-real-folder)))
  8866. (kill-this-buffer)
  8867. (mh-search "+" (list "--message-id" article))))
  8868. (mh-search "+" article))
  8869. (if (org-mhe-get-message-real-folder)
  8870. (mh-show-msg 1)
  8871. (kill-this-buffer)
  8872. (error "Message not found"))))
  8873. ;; BibTeX links
  8874. ;; Use the custom search meachnism to construct and use search strings for
  8875. ;; file links to BibTeX database entries.
  8876. (defun org-create-file-search-in-bibtex ()
  8877. "Create the search string and description for a BibTeX database entry."
  8878. (when (eq major-mode 'bibtex-mode)
  8879. ;; yes, we want to construct this search string.
  8880. ;; Make a good description for this entry, using names, year and the title
  8881. ;; Put it into the `description' variable which is dynamically scoped.
  8882. (let ((bibtex-autokey-names 1)
  8883. (bibtex-autokey-names-stretch 1)
  8884. (bibtex-autokey-name-case-convert-function 'identity)
  8885. (bibtex-autokey-name-separator " & ")
  8886. (bibtex-autokey-additional-names " et al.")
  8887. (bibtex-autokey-year-length 4)
  8888. (bibtex-autokey-name-year-separator " ")
  8889. (bibtex-autokey-titlewords 3)
  8890. (bibtex-autokey-titleword-separator " ")
  8891. (bibtex-autokey-titleword-case-convert-function 'identity)
  8892. (bibtex-autokey-titleword-length 'infty)
  8893. (bibtex-autokey-year-title-separator ": "))
  8894. (setq description (bibtex-generate-autokey)))
  8895. ;; Now parse the entry, get the key and return it.
  8896. (save-excursion
  8897. (bibtex-beginning-of-entry)
  8898. (cdr (assoc "=key=" (bibtex-parse-entry))))))
  8899. (defun org-execute-file-search-in-bibtex (s)
  8900. "Find the link search string S as a key for a database entry."
  8901. (when (eq major-mode 'bibtex-mode)
  8902. ;; Yes, we want to do the search in this file.
  8903. ;; We construct a regexp that searches for "@entrytype{" followed by the key
  8904. (goto-char (point-min))
  8905. (and (re-search-forward (concat "@[a-zA-Z]+[ \t\n]*{[ \t\n]*"
  8906. (regexp-quote s) "[ \t\n]*,") nil t)
  8907. (goto-char (match-beginning 0)))
  8908. (if (and (match-beginning 0) (equal current-prefix-arg '(16)))
  8909. ;; Use double prefix to indicate that any web link should be browsed
  8910. (let ((b (current-buffer)) (p (point)))
  8911. ;; Restore the window configuration because we just use the web link
  8912. (set-window-configuration org-window-config-before-follow-link)
  8913. (save-excursion (set-buffer b) (goto-char p)
  8914. (bibtex-url)))
  8915. (recenter 0)) ; Move entry start to beginning of window
  8916. ;; return t to indicate that the search is done.
  8917. t))
  8918. ;; Finally add the functions to the right hooks.
  8919. (add-hook 'org-create-file-search-functions 'org-create-file-search-in-bibtex)
  8920. (add-hook 'org-execute-file-search-functions 'org-execute-file-search-in-bibtex)
  8921. ;; end of Bibtex link setup
  8922. (defun org-upgrade-old-links (&optional query-description)
  8923. "Transfer old <...> style links to new [[...]] style links.
  8924. With arg query-description, ask at each match for a description text to use
  8925. for this link."
  8926. (interactive (list (y-or-n-p "Would you like to be queried for a description at each link?")))
  8927. (save-excursion
  8928. (goto-char (point-min))
  8929. (let ((re (concat "\\([^[]\\)<\\("
  8930. "\\(" (mapconcat 'identity org-link-types "\\|")
  8931. "\\):"
  8932. "[^" org-non-link-chars "]+\\)>"))
  8933. l1 l2 (cnt 0))
  8934. (while (re-search-forward re nil t)
  8935. (setq cnt (1+ cnt)
  8936. l1 (org-match-string-no-properties 2)
  8937. l2 (save-match-data (org-link-escape l1)))
  8938. (when query-description (setq l1 (read-string "Desc: " l1)))
  8939. (if (equal l1 l2)
  8940. (replace-match (concat (match-string 1) "[[" l1 "]]") t t)
  8941. (replace-match (concat (match-string 1) "[[" l2 "][" l1 "]]") t t)))
  8942. (message "%d matches have beed treated" cnt))))
  8943. (defun org-open-file (path &optional in-emacs line search)
  8944. "Open the file at PATH.
  8945. First, this expands any special file name abbreviations. Then the
  8946. configuration variable `org-file-apps' is checked if it contains an
  8947. entry for this file type, and if yes, the corresponding command is launched.
  8948. If no application is found, Emacs simply visits the file.
  8949. With optional argument IN-EMACS, Emacs will visit the file.
  8950. Optional LINE specifies a line to go to, optional SEARCH a string to
  8951. search for. If LINE or SEARCH is given, the file will always be
  8952. opened in Emacs.
  8953. If the file does not exist, an error is thrown."
  8954. (setq in-emacs (or in-emacs line search))
  8955. (let* ((file (if (equal path "")
  8956. buffer-file-name
  8957. path))
  8958. (apps (append org-file-apps (org-default-apps)))
  8959. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  8960. (dirp (if remp nil (file-directory-p file)))
  8961. (dfile (downcase file))
  8962. (old-buffer (current-buffer))
  8963. (old-pos (point))
  8964. (old-mode major-mode)
  8965. ext cmd)
  8966. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  8967. (setq ext (match-string 1 dfile))
  8968. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  8969. (setq ext (match-string 1 dfile))))
  8970. (if in-emacs
  8971. (setq cmd 'emacs)
  8972. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  8973. (and dirp (cdr (assoc 'directory apps)))
  8974. (cdr (assoc ext apps))
  8975. (cdr (assoc t apps)))))
  8976. (when (eq cmd 'mailcap)
  8977. (require 'mailcap)
  8978. (mailcap-parse-mailcaps)
  8979. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  8980. (command (mailcap-mime-info mime-type)))
  8981. (if (stringp command)
  8982. (setq cmd command)
  8983. (setq cmd 'emacs))))
  8984. (if (and (not (eq cmd 'emacs)) ; Emacs has not problems with non-ex files
  8985. (not (file-exists-p file))
  8986. (not org-open-non-existing-files))
  8987. (error "No such file: %s" file))
  8988. (cond
  8989. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  8990. ;; Normalize use of quote, this can vary.
  8991. (if (string-match "['\"]%s['\"]" cmd)
  8992. (setq cmd (replace-match "'%s'" t t cmd)))
  8993. (setq cmd (format cmd file))
  8994. (save-window-excursion
  8995. (shell-command (concat cmd " &"))))
  8996. ((or (stringp cmd)
  8997. (eq cmd 'emacs))
  8998. ; (unless (equal (file-truename file) (file-truename (or buffer-file-name "")))
  8999. ; (funcall (cdr (assq 'file org-link-frame-setup)) file))
  9000. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9001. (if line (goto-line line)
  9002. (if search (org-link-search search))))
  9003. ((consp cmd)
  9004. (eval cmd))
  9005. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9006. (and (org-mode-p) (eq old-mode 'org-mode)
  9007. (or (not (equal old-buffer (current-buffer)))
  9008. (not (equal old-pos (point))))
  9009. (org-mark-ring-push old-pos old-buffer))))
  9010. (defun org-default-apps ()
  9011. "Return the default applications for this operating system."
  9012. (cond
  9013. ((eq system-type 'darwin)
  9014. org-file-apps-defaults-macosx)
  9015. ((eq system-type 'windows-nt)
  9016. org-file-apps-defaults-windowsnt)
  9017. (t org-file-apps-defaults-gnu)))
  9018. (defun org-expand-file-name (path)
  9019. "Replace special path abbreviations and expand the file name."
  9020. (expand-file-name path))
  9021. (defun org-file-remote-p (file)
  9022. "Test whether FILE specifies a location on a remote system.
  9023. Return non-nil if the location is indeed remote.
  9024. For example, the filename \"/user@host:/foo\" specifies a location
  9025. on the system \"/user@host:\"."
  9026. (cond ((fboundp 'file-remote-p)
  9027. (file-remote-p file))
  9028. ((fboundp 'tramp-handle-file-remote-p)
  9029. (tramp-handle-file-remote-p file))
  9030. ((and (boundp 'ange-ftp-name-format)
  9031. (string-match ange-ftp-name-format file))
  9032. t)
  9033. (t nil)))
  9034. (defvar org-insert-link-history nil
  9035. "Minibuffer history for links inserted with `org-insert-link'.")
  9036. (defvar org-stored-links nil
  9037. "Contains the links stored with `org-store-link'.")
  9038. ;;;###autoload
  9039. (defun org-store-link (arg)
  9040. "\\<org-mode-map>Store an org-link to the current location.
  9041. This link can later be inserted into an org-buffer with
  9042. \\[org-insert-link].
  9043. For some link types, a prefix arg is interpreted:
  9044. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  9045. For file links, arg negates `org-context-in-file-links'."
  9046. (interactive "P")
  9047. (let (link cpltxt desc description search txt (pos (point)))
  9048. (cond
  9049. ((eq major-mode 'bbdb-mode)
  9050. (setq cpltxt (concat
  9051. "bbdb:"
  9052. (or (bbdb-record-name (bbdb-current-record))
  9053. (bbdb-record-company (bbdb-current-record))))
  9054. link (org-make-link cpltxt)))
  9055. ((eq major-mode 'Info-mode)
  9056. (setq link (org-make-link "info:"
  9057. (file-name-nondirectory Info-current-file)
  9058. ":" Info-current-node))
  9059. (setq cpltxt (concat (file-name-nondirectory Info-current-file)
  9060. ":" Info-current-node)))
  9061. ((eq major-mode 'calendar-mode)
  9062. (let ((cd (calendar-cursor-to-date)))
  9063. (setq link
  9064. (format-time-string
  9065. (car org-time-stamp-formats)
  9066. (apply 'encode-time
  9067. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  9068. nil nil nil))))))
  9069. ((or (eq major-mode 'vm-summary-mode)
  9070. (eq major-mode 'vm-presentation-mode))
  9071. (and (eq major-mode 'vm-presentation-mode) (vm-summarize))
  9072. (vm-follow-summary-cursor)
  9073. (save-excursion
  9074. (vm-select-folder-buffer)
  9075. (let* ((message (car vm-message-pointer))
  9076. (folder buffer-file-name)
  9077. (subject (vm-su-subject message))
  9078. (author (vm-su-full-name message))
  9079. (message-id (vm-su-message-id message)))
  9080. (setq message-id (org-remove-angle-brackets message-id))
  9081. (setq folder (abbreviate-file-name folder))
  9082. (if (string-match (concat "^" (regexp-quote vm-folder-directory))
  9083. folder)
  9084. (setq folder (replace-match "" t t folder)))
  9085. (setq cpltxt (concat author " on: " subject))
  9086. (setq link (org-make-link "vm:" folder "#" message-id)))))
  9087. ((eq major-mode 'wl-summary-mode)
  9088. (let* ((msgnum (wl-summary-message-number))
  9089. (message-id (elmo-message-field wl-summary-buffer-elmo-folder
  9090. msgnum 'message-id))
  9091. (wl-message-entity (elmo-msgdb-overview-get-entity
  9092. msgnum (wl-summary-buffer-msgdb)))
  9093. (author (wl-summary-line-from)) ; FIXME: correct?
  9094. (subject "???")) ; FIXME:
  9095. (setq message-id (org-remove-angle-brackets message-id))
  9096. (setq cpltxt (concat author " on: " subject))
  9097. (setq link (org-make-link "wl:" wl-summary-buffer-folder-name
  9098. "#" message-id))))
  9099. ((or (equal major-mode 'mh-folder-mode)
  9100. (equal major-mode 'mh-show-mode))
  9101. (let ((from-header (org-mhe-get-header "From:"))
  9102. (to-header (org-mhe-get-header "To:"))
  9103. (subject (org-mhe-get-header "Subject:")))
  9104. (setq cpltxt (concat from-header " on: " subject))
  9105. (setq link (org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
  9106. (org-remove-angle-brackets
  9107. (org-mhe-get-header "Message-Id:"))))))
  9108. ((eq major-mode 'rmail-mode)
  9109. (save-excursion
  9110. (save-restriction
  9111. (rmail-narrow-to-non-pruned-header)
  9112. (let ((folder buffer-file-name)
  9113. (message-id (mail-fetch-field "message-id"))
  9114. (author (mail-fetch-field "from"))
  9115. (subject (mail-fetch-field "subject")))
  9116. (setq message-id (org-remove-angle-brackets message-id))
  9117. (setq cpltxt (concat author " on: " subject))
  9118. (setq link (org-make-link "rmail:" folder "#" message-id))))))
  9119. ((eq major-mode 'gnus-group-mode)
  9120. (let ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
  9121. (gnus-group-group-name)) ; version
  9122. ((fboundp 'gnus-group-name)
  9123. (gnus-group-name))
  9124. (t "???"))))
  9125. (setq cpltxt (concat
  9126. (if (org-xor arg org-usenet-links-prefer-google)
  9127. "http://groups.google.com/groups?group="
  9128. "gnus:")
  9129. group)
  9130. link (org-make-link cpltxt))))
  9131. ((memq major-mode '(gnus-summary-mode gnus-article-mode))
  9132. (and (eq major-mode 'gnus-article-mode) (gnus-article-show-summary))
  9133. (gnus-summary-beginning-of-article)
  9134. (let* ((group (car gnus-article-current))
  9135. (article (cdr gnus-article-current))
  9136. (header (gnus-summary-article-header article))
  9137. (author (mail-header-from header))
  9138. (message-id (mail-header-id header))
  9139. (date (mail-header-date header))
  9140. (subject (gnus-summary-subject-string)))
  9141. (setq cpltxt (concat author " on: " subject))
  9142. (if (org-xor arg org-usenet-links-prefer-google)
  9143. (setq link
  9144. (concat
  9145. cpltxt "\n "
  9146. (format "http://groups.google.com/groups?as_umsgid=%s"
  9147. (org-fixup-message-id-for-http message-id))))
  9148. (setq link (org-make-link "gnus:" group
  9149. "#" (number-to-string article))))))
  9150. ((eq major-mode 'w3-mode)
  9151. (setq cpltxt (url-view-url t)
  9152. link (org-make-link cpltxt)))
  9153. ((eq major-mode 'w3m-mode)
  9154. (setq cpltxt (or w3m-current-title w3m-current-url)
  9155. link (org-make-link w3m-current-url)))
  9156. ((setq search (run-hook-with-args-until-success
  9157. 'org-create-file-search-functions))
  9158. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  9159. "::" search))
  9160. (setq cpltxt (or description link)))
  9161. ((eq major-mode 'image-mode)
  9162. (setq cpltxt (concat "file:"
  9163. (abbreviate-file-name buffer-file-name))
  9164. link (org-make-link cpltxt)))
  9165. ((org-mode-p)
  9166. ;; Just link to current headline
  9167. (setq cpltxt (concat "file:"
  9168. (abbreviate-file-name buffer-file-name)))
  9169. ;; Add a context search string
  9170. (when (org-xor org-context-in-file-links arg)
  9171. ;; Check if we are on a target
  9172. (if (save-excursion
  9173. (skip-chars-forward "^>\n\r")
  9174. (and (re-search-backward "<<" nil t)
  9175. (looking-at "<<\\(.*?\\)>>")
  9176. (<= (match-beginning 0) pos)
  9177. (>= (match-end 0) pos)))
  9178. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  9179. (setq txt (cond
  9180. ((org-on-heading-p) nil)
  9181. ((org-region-active-p)
  9182. (buffer-substring (region-beginning) (region-end)))
  9183. (t (buffer-substring (point-at-bol) (point-at-eol)))))
  9184. (when (or (null txt) (string-match "\\S-" txt))
  9185. (setq cpltxt
  9186. (concat cpltxt "::"
  9187. (if org-file-link-context-use-camel-case
  9188. (org-make-org-heading-camel txt)
  9189. (org-make-org-heading-search-string txt)))
  9190. desc "NONE"))))
  9191. (if (string-match "::\\'" cpltxt)
  9192. (setq cpltxt (substring cpltxt 0 -2)))
  9193. (setq link (org-make-link cpltxt)))
  9194. (buffer-file-name
  9195. ;; Just link to this file here.
  9196. (setq cpltxt (concat "file:"
  9197. (abbreviate-file-name buffer-file-name)))
  9198. ;; Add a context string
  9199. (when (org-xor org-context-in-file-links arg)
  9200. (setq txt (if (org-region-active-p)
  9201. (buffer-substring (region-beginning) (region-end))
  9202. (buffer-substring (point-at-bol) (point-at-eol))))
  9203. ;; Only use search option if there is some text.
  9204. (when (string-match "\\S-" txt)
  9205. (setq cpltxt
  9206. (concat cpltxt "::"
  9207. (if org-file-link-context-use-camel-case
  9208. (org-make-org-heading-camel txt)
  9209. (org-make-org-heading-search-string txt)))
  9210. desc "NONE")))
  9211. (setq link (org-make-link cpltxt)))
  9212. ((interactive-p)
  9213. (error "Cannot link to a buffer which is not visiting a file"))
  9214. (t (setq link nil)))
  9215. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  9216. (setq link (or link cpltxt)
  9217. desc (or desc cpltxt))
  9218. (if (equal desc "NONE") (setq desc nil))
  9219. (if (and (interactive-p) link)
  9220. (progn
  9221. (setq org-stored-links
  9222. (cons (list cpltxt link desc) org-stored-links))
  9223. (message "Stored: %s" (or cpltxt link)))
  9224. (org-make-link-string link desc))))
  9225. (defun org-make-org-heading-search-string (&optional string heading)
  9226. "Make search string for STRING or current headline."
  9227. (interactive)
  9228. (let ((s (or string (org-get-heading))))
  9229. (unless (and string (not heading))
  9230. ;; We are using a headline, clean up garbage in there.
  9231. (if (string-match org-todo-regexp s)
  9232. (setq s (replace-match "" t t s)))
  9233. (if (string-match ":[a-zA-Z_@0-9:]+:[ \t]*$" s)
  9234. (setq s (replace-match "" t t s)))
  9235. (setq s (org-trim s))
  9236. (if (string-match (concat "^\\(" org-quote-string "\\|"
  9237. org-comment-string "\\)") s)
  9238. (setq s (replace-match "" t t s)))
  9239. (while (string-match org-ts-regexp s)
  9240. (setq s (replace-match "" t t s))))
  9241. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  9242. (setq s (replace-match " " t t s)))
  9243. (or string (setq s (concat "*" s))) ; Add * for headlines
  9244. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  9245. (defun org-make-org-heading-camel (&optional string heading)
  9246. "Make a CamelCase string for STRING or the current headline."
  9247. (interactive)
  9248. (let ((s (or string (org-get-heading))))
  9249. (unless (and string (not heading))
  9250. ;; We are using a headline, clean up garbage in there.
  9251. (if (string-match org-todo-regexp s)
  9252. (setq s (replace-match "" t t s)))
  9253. (if (string-match ":[a-zA-Z_@0-9:]+:[ \t]*$" s)
  9254. (setq s (replace-match "" t t s)))
  9255. (setq s (org-trim s))
  9256. (if (string-match (concat "^\\(" org-quote-string "\\|"
  9257. org-comment-string "\\)") s)
  9258. (setq s (replace-match "" t t s)))
  9259. (while (string-match org-ts-regexp s)
  9260. (setq s (replace-match "" t t s))))
  9261. (while (string-match "[^a-zA-Z_ \t]+" s)
  9262. (setq s (replace-match " " t t s)))
  9263. (or string (setq s (concat "*" s))) ; Add * for headlines
  9264. (mapconcat 'capitalize (org-split-string s "[ \t]+") "")))
  9265. (defun org-make-link (&rest strings)
  9266. "Concatenate STRINGS, format resulting string with `org-link-format'."
  9267. (format org-link-format (apply 'concat strings)))
  9268. (defun org-make-link-string (link &optional description)
  9269. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  9270. (if (eq org-link-style 'plain)
  9271. (if (equal description link)
  9272. link
  9273. (concat description "\n" link))
  9274. (when (stringp description)
  9275. ;; Remove brackets from the description, they are fatal.
  9276. (while (string-match "\\[\\|\\]" description)
  9277. (setq description (replace-match "" t t description))))
  9278. (when (equal (org-link-escape link) description)
  9279. ;; No description needed, it is identical
  9280. (setq description nil))
  9281. (when (and (not description)
  9282. (not (equal link (org-link-escape link))))
  9283. (setq description link))
  9284. (concat "[[" (org-link-escape link) "]"
  9285. (if description (concat "[" description "]") "")
  9286. "]")))
  9287. (defconst org-link-escape-chars '(("[" . "%5B") ("]" . "%5D") (" " . "%20"))
  9288. "Association list of escapes for some characters problematic in links.")
  9289. (defun org-link-escape (text)
  9290. "Escape charaters in TEXT that are problematic for links."
  9291. (when text
  9292. (let ((re (mapconcat (lambda (x) (regexp-quote (car x)))
  9293. org-link-escape-chars "\\|")))
  9294. (while (string-match re text)
  9295. (setq text
  9296. (replace-match
  9297. (cdr (assoc (match-string 0 text) org-link-escape-chars))
  9298. t t text)))
  9299. text)))
  9300. (defun org-link-unescape (text)
  9301. "Reverse the action of `org-link-escape'."
  9302. (when text
  9303. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  9304. org-link-escape-chars "\\|")))
  9305. (while (string-match re text)
  9306. (setq text
  9307. (replace-match
  9308. (car (rassoc (match-string 0 text) org-link-escape-chars))
  9309. t t text)))
  9310. text)))
  9311. (defun org-xor (a b)
  9312. "Exclusive or."
  9313. (if a (not b) b))
  9314. (defun org-get-header (header)
  9315. "Find a header field in the current buffer."
  9316. (save-excursion
  9317. (goto-char (point-min))
  9318. (let ((case-fold-search t) s)
  9319. (cond
  9320. ((eq header 'from)
  9321. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  9322. (setq s (match-string 1)))
  9323. (while (string-match "\"" s)
  9324. (setq s (replace-match "" t t s)))
  9325. (if (string-match "[<(].*" s)
  9326. (setq s (replace-match "" t t s))))
  9327. ((eq header 'message-id)
  9328. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  9329. (setq s (match-string 1))))
  9330. ((eq header 'subject)
  9331. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  9332. (setq s (match-string 1)))))
  9333. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  9334. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  9335. s)))
  9336. (defun org-fixup-message-id-for-http (s)
  9337. "Replace special characters in a message id, so it can be used in an http query."
  9338. (while (string-match "<" s)
  9339. (setq s (replace-match "%3C" t t s)))
  9340. (while (string-match ">" s)
  9341. (setq s (replace-match "%3E" t t s)))
  9342. (while (string-match "@" s)
  9343. (setq s (replace-match "%40" t t s)))
  9344. s)
  9345. (defun org-insert-link (&optional complete-file)
  9346. "Insert a link. At the prompt, enter the link.
  9347. Completion can be used to select a link previously stored with
  9348. `org-store-link'. When the empty string is entered (i.e. if you just
  9349. press RET at the prompt), the link defaults to the most recently
  9350. stored link. As SPC triggers completion in the minibuffer, you need to
  9351. use M-SPC or C-q SPC to force the insertion of a space character.
  9352. You will also be prompted for a description, and if one is given, it will
  9353. be displayed in the buffer instead of the link.
  9354. If there is already a link at point, this command will allow you to edit link
  9355. and description parts.
  9356. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
  9357. selected using completion. The path to the file will be relative to
  9358. the current directory if the file is in the current directory or a
  9359. subdirectory. Otherwise, the link will be the absolute path as
  9360. completed in the minibuffer (i.e. normally ~/path/to/file).
  9361. With two \\[universal-argument] prefixes, enforce an absolute path even if the file
  9362. is in the current directory or below.
  9363. With three \\[universal-argument] prefixes, negate the meaning of
  9364. `org-keep-stored-link-after-insertion'."
  9365. (interactive "P")
  9366. (let (link desc entry remove file (pos (point)))
  9367. (cond
  9368. ((save-excursion
  9369. (skip-chars-forward "^]\n\r")
  9370. (and (re-search-backward "\\[\\[" nil t)
  9371. (looking-at org-bracket-link-regexp)
  9372. (<= (match-beginning 0) pos)
  9373. (>= (match-end 0) pos)))
  9374. ;; We do have a link at point, and we are going to edit it.
  9375. (setq remove (list (match-beginning 0) (match-end 0)))
  9376. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  9377. (setq link (read-string "Link: "
  9378. (org-link-unescape
  9379. (org-match-string-no-properties 1)))))
  9380. ((equal complete-file '(4))
  9381. ;; Completing read for file names.
  9382. (setq file (read-file-name "File: "))
  9383. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  9384. (pwd1 (file-name-as-directory (abbreviate-file-name
  9385. (expand-file-name ".")))))
  9386. (cond
  9387. ((equal complete-file '(16))
  9388. (setq link (org-make-link
  9389. "file:"
  9390. (abbreviate-file-name (expand-file-name file)))))
  9391. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9392. (setq link (org-make-link "file:" (match-string 1 file))))
  9393. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9394. (expand-file-name file))
  9395. (setq link (org-make-link
  9396. "file:" (match-string 1 (expand-file-name file)))))
  9397. (t (setq link (org-make-link "file:" file))))))
  9398. (t
  9399. ;; Read link, with completion for stored links.
  9400. (setq link (org-completing-read
  9401. "Link: " org-stored-links nil nil nil
  9402. org-insert-link-history
  9403. (or (car (car org-stored-links)))))
  9404. (setq entry (assoc link org-stored-links))
  9405. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  9406. (not org-keep-stored-link-after-insertion))
  9407. (setq org-stored-links (delq (assoc link org-stored-links)
  9408. org-stored-links)))
  9409. (setq link (if entry (nth 1 entry) link)
  9410. desc (or desc (nth 2 entry)))))
  9411. (if (string-match org-plain-link-re link)
  9412. ;; URL-like link, normalize the use of angular brackets.
  9413. (setq link (org-make-link (org-remove-angle-brackets link))))
  9414. ;; Check if we are linking to the current file with a search option
  9415. ;; If yes, simplify the link by using only the search option.
  9416. (when (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link)
  9417. (let* ((path (match-string 1 link))
  9418. (case-fold-search nil)
  9419. (search (match-string 2 link)))
  9420. (save-match-data
  9421. (if (equal (file-truename buffer-file-name) (file-truename path))
  9422. ;; We are linking to this same file, with a search option
  9423. (setq link search)))))
  9424. ;; Check if we can/should use a relative path. If yes, simplify the link
  9425. (when (string-match "\\<file:\\(.*\\)" link)
  9426. (let* ((path (match-string 1 link))
  9427. (case-fold-search nil))
  9428. (cond
  9429. ((eq org-link-file-path-type 'absolute)
  9430. (setq path (abbreviate-file-name (expand-file-name path))))
  9431. ((eq org-link-file-path-type 'noabbrev)
  9432. (setq path (expand-file-name path)))
  9433. ((eq org-link-file-path-type 'relative)
  9434. (setq path (file-relative-name path)))
  9435. (t
  9436. (save-match-data
  9437. (if (string-match (concat "^" (regexp-quote
  9438. (file-name-as-directory
  9439. (expand-file-name "."))))
  9440. (expand-file-name path))
  9441. ;; We are linking a file with relative path name.
  9442. (setq path (substring (expand-file-name path)
  9443. (match-end 0)))))))
  9444. (setq link (concat "file:" path))))
  9445. (setq desc (read-string "Description: " desc))
  9446. (unless (string-match "\\S-" desc) (setq desc nil))
  9447. (if remove (apply 'delete-region remove))
  9448. (insert (org-make-link-string link desc))))
  9449. (defun org-completing-read (&rest args)
  9450. (let ((minibuffer-local-completion-map
  9451. (copy-keymap minibuffer-local-completion-map)))
  9452. (define-key minibuffer-local-completion-map " " 'self-insert-command)
  9453. (apply 'completing-read args)))
  9454. ;;; Hooks for remember.el
  9455. (defvar org-finish-function nil)
  9456. ;;;###autoload
  9457. (defun org-remember-annotation ()
  9458. "Return a link to the current location as an annotation for remember.el.
  9459. If you are using Org-mode files as target for data storage with
  9460. remember.el, then the annotations should include a link compatible with the
  9461. conventions in Org-mode. This function returns such a link."
  9462. (org-store-link nil))
  9463. (defconst org-remember-help
  9464. "Select a destination location for the note.
  9465. UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
  9466. RET at beg-of-buf -> Append to file as level 2 headline
  9467. RET on headline -> Store as sublevel entry to current headline
  9468. <left>/<right> -> before/after current headline, same headings level")
  9469. ;;;###autoload
  9470. (defun org-remember-apply-template ()
  9471. "Initialize *remember* buffer with template, invoke `org-mode'.
  9472. This function should be placed into `remember-mode-hook' and in fact requires
  9473. to be run from that hook to fucntion properly."
  9474. (if org-remember-templates
  9475. (let* ((entry (if (= (length org-remember-templates) 1)
  9476. (cdar org-remember-templates)
  9477. (message "Select template: %s"
  9478. (mapconcat
  9479. (lambda (x) (char-to-string (car x)))
  9480. org-remember-templates " "))
  9481. (cdr (assoc (read-char-exclusive) org-remember-templates))))
  9482. (tpl (car entry))
  9483. (file (if (consp (cdr entry)) (nth 1 entry)))
  9484. (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
  9485. (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
  9486. (v-u (concat "[" (substring v-t 1 -1) "]"))
  9487. (v-U (concat "[" (substring v-T 1 -1) "]"))
  9488. (v-a annotation) ; defined in `remember-mode'
  9489. (v-i initial) ; defined in `remember-mode'
  9490. (v-n user-full-name)
  9491. )
  9492. (unless tpl (setq tpl "") (message "No template") (ding))
  9493. (insert tpl) (goto-char (point-min))
  9494. (while (re-search-forward "%\\([tTuTai]\\)" nil t)
  9495. (when (and initial (equal (match-string 0) "%i"))
  9496. (save-match-data
  9497. (let* ((lead (buffer-substring
  9498. (point-at-bol) (match-beginning 0))))
  9499. (setq v-i (mapconcat 'identity
  9500. (org-split-string initial "\n")
  9501. (concat "\n" lead))))))
  9502. (replace-match
  9503. (or (eval (intern (concat "v-" (match-string 1)))) "")
  9504. t t))
  9505. (let ((org-startup-folded nil)
  9506. (org-startup-with-deadline-check nil))
  9507. (org-mode))
  9508. (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
  9509. (org-set-local 'org-default-notes-file file))
  9510. (goto-char (point-min))
  9511. (if (re-search-forward "%\\?" nil t) (replace-match "")))
  9512. (let ((org-startup-folded nil)
  9513. (org-startup-with-deadline-check nil))
  9514. (org-mode)))
  9515. (org-set-local 'org-finish-function 'remember-buffer))
  9516. ;;;###autoload
  9517. (defun org-remember-handler ()
  9518. "Store stuff from remember.el into an org file.
  9519. First prompts for an org file. If the user just presses return, the value
  9520. of `org-default-notes-file' is used.
  9521. Then the command offers the headings tree of the selected file in order to
  9522. file the text at a specific location.
  9523. You can either immediately press RET to get the note appended to the
  9524. file, or you can use vertical cursor motion and visibility cycling (TAB) to
  9525. find a better place. Then press RET or <left> or <right> in insert the note.
  9526. Key Cursor position Note gets inserted
  9527. -----------------------------------------------------------------------------
  9528. RET buffer-start as level 2 heading at end of file
  9529. RET on headline as sublevel of the heading at cursor
  9530. RET no heading at cursor position, level taken from context.
  9531. Or use prefix arg to specify level manually.
  9532. <left> on headline as same level, before current heading
  9533. <right> on headline as same level, after current heading
  9534. So the fastest way to store the note is to press RET RET to append it to
  9535. the default file. This way your current train of thought is not
  9536. interrupted, in accordance with the principles of remember.el. But with
  9537. little extra effort, you can push it directly to the correct location.
  9538. Before being stored away, the function ensures that the text has a
  9539. headline, i.e. a first line that starts with a \"*\". If not, a headline
  9540. is constructed from the current date and some additional data.
  9541. If the variable `org-adapt-indentation' is non-nil, the entire text is
  9542. also indented so that it starts in the same column as the headline
  9543. \(i.e. after the stars).
  9544. See also the variable `org-reverse-note-order'."
  9545. (catch 'quit
  9546. (let* ((txt (buffer-substring (point-min) (point-max)))
  9547. (fastp current-prefix-arg)
  9548. (file (if fastp org-default-notes-file (org-get-org-file)))
  9549. (visiting (find-buffer-visiting file))
  9550. (org-startup-with-deadline-check nil)
  9551. (org-startup-folded nil)
  9552. (org-startup-align-all-tables nil)
  9553. spos level indent reversed)
  9554. ;; Modify text so that it becomes a nice subtree which can be inserted
  9555. ;; into an org tree.
  9556. (let* ((lines (split-string txt "\n"))
  9557. first)
  9558. ;; remove empty lines at the beginning
  9559. (while (and lines (string-match "^[ \t]*\n" (car lines)))
  9560. (setq lines (cdr lines)))
  9561. (setq first (car lines) lines (cdr lines))
  9562. (if (string-match "^\\*+" first)
  9563. ;; Is already a headline
  9564. (setq indent nil)
  9565. ;; We need to add a headline: Use time and first buffer line
  9566. (setq lines (cons first lines)
  9567. first (concat "* " (current-time-string)
  9568. " (" (remember-buffer-desc) ")")
  9569. indent " "))
  9570. (if (and org-adapt-indentation indent)
  9571. (setq lines (mapcar (lambda (x) (concat indent x)) lines)))
  9572. (setq txt (concat first "\n"
  9573. (mapconcat 'identity lines "\n"))))
  9574. ;; Find the file
  9575. (if (not visiting)
  9576. (find-file-noselect file))
  9577. (with-current-buffer (get-file-buffer file)
  9578. (save-excursion (and (goto-char (point-min))
  9579. (not (re-search-forward "^\\* " nil t))
  9580. (insert "\n* Notes\n")))
  9581. (setq reversed (org-notes-order-reversed-p))
  9582. (save-excursion
  9583. (save-restriction
  9584. (widen)
  9585. ;; Ask the User for a location
  9586. (setq spos (if fastp 1 (org-get-location
  9587. (current-buffer)
  9588. org-remember-help)))
  9589. (if (not spos) (throw 'quit nil)) ; return nil to show we did
  9590. ; not handle this note
  9591. (goto-char spos)
  9592. (cond ((bobp)
  9593. ;; Put it at the start or end, as level 2
  9594. (save-restriction
  9595. (widen)
  9596. (goto-char (if reversed (point-min) (point-max)))
  9597. (if (not (bolp)) (newline))
  9598. (org-paste-subtree 2 txt)))
  9599. ((and (org-on-heading-p nil) (not current-prefix-arg))
  9600. ;; Put it below this entry, at the beg/end of the subtree
  9601. (org-back-to-heading)
  9602. (setq level (funcall outline-level))
  9603. (if reversed
  9604. (outline-end-of-heading)
  9605. (outline-end-of-subtree))
  9606. (if (not (bolp)) (newline))
  9607. (beginning-of-line 1)
  9608. (org-paste-subtree (1+ level) txt))
  9609. (t
  9610. ;; Put it right there, with automatic level determined by
  9611. ;; org-paste-subtree or from prefix arg
  9612. (org-paste-subtree current-prefix-arg txt)))
  9613. (when remember-save-after-remembering
  9614. (save-buffer)
  9615. (if (not visiting) (kill-buffer (current-buffer)))))))))
  9616. t) ;; return t to indicate that we took care of this note.
  9617. (defun org-get-org-file ()
  9618. "Read a filename, with default directory `org-directory'."
  9619. (let ((default (or org-default-notes-file remember-data-file)))
  9620. (read-file-name (format "File name [%s]: " default)
  9621. (file-name-as-directory org-directory)
  9622. default)))
  9623. (defun org-notes-order-reversed-p ()
  9624. "Check if the current file should receive notes in reversed order."
  9625. (cond
  9626. ((not org-reverse-note-order) nil)
  9627. ((eq t org-reverse-note-order) t)
  9628. ((not (listp org-reverse-note-order)) nil)
  9629. (t (catch 'exit
  9630. (let ((all org-reverse-note-order)
  9631. entry)
  9632. (while (setq entry (pop all))
  9633. (if (string-match (car entry) buffer-file-name)
  9634. (throw 'exit (cdr entry))))
  9635. nil)))))
  9636. ;;; Tables
  9637. ;; Watch out: Here we are talking about two different kind of tables.
  9638. ;; Most of the code is for the tables created with the Org-mode table editor.
  9639. ;; Sometimes, we talk about tables created and edited with the table.el
  9640. ;; Emacs package. We call the former org-type tables, and the latter
  9641. ;; table.el-type tables.
  9642. (defun org-before-change-function (beg end)
  9643. "Every change indicates that a table might need an update."
  9644. (setq org-table-may-need-update t))
  9645. (defconst org-table-line-regexp "^[ \t]*|"
  9646. "Detects an org-type table line.")
  9647. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  9648. "Detects an org-type table line.")
  9649. (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
  9650. "Detects a table line marked for automatic recalculation.")
  9651. (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
  9652. "Detects a table line marked for automatic recalculation.")
  9653. (defconst org-table-calculate-mark-regexp "^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
  9654. "Detects a table line marked for automatic recalculation.")
  9655. (defconst org-table-hline-regexp "^[ \t]*|-"
  9656. "Detects an org-type table hline.")
  9657. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  9658. "Detects a table-type table hline.")
  9659. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  9660. "Detects an org-type or table-type table.")
  9661. (defconst org-table-border-regexp "^[ \t]*[^| \t]"
  9662. "Searching from within a table (any type) this finds the first line
  9663. outside the table.")
  9664. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  9665. "Searching from within a table (any type) this finds the first line
  9666. outside the table.")
  9667. (defun org-table-create-with-table.el ()
  9668. "Use the table.el package to insert a new table.
  9669. If there is already a table at point, convert between Org-mode tables
  9670. and table.el tables."
  9671. (interactive)
  9672. (require 'table)
  9673. (cond
  9674. ((org-at-table.el-p)
  9675. (if (y-or-n-p "Convert table to Org-mode table? ")
  9676. (org-table-convert)))
  9677. ((org-at-table-p)
  9678. (if (y-or-n-p "Convert table to table.el table? ")
  9679. (org-table-convert)))
  9680. (t (call-interactively 'table-insert))))
  9681. (defun org-table-create-or-convert-from-region (arg)
  9682. "Convert region to table, or create an empty table.
  9683. If there is an active region, convert it to a table. If there is no such
  9684. region, create an empty table."
  9685. (interactive "P")
  9686. (if (org-region-active-p)
  9687. (org-table-convert-region (region-beginning) (region-end) arg)
  9688. (org-table-create arg)))
  9689. (defun org-table-create (&optional size)
  9690. "Query for a size and insert a table skeleton.
  9691. SIZE is a string Columns x Rows like for example \"3x2\"."
  9692. (interactive "P")
  9693. (unless size
  9694. (setq size (read-string
  9695. (concat "Table size Columns x Rows [e.g. "
  9696. org-table-default-size "]: ")
  9697. "" nil org-table-default-size)))
  9698. (let* ((pos (point))
  9699. (indent (make-string (current-column) ?\ ))
  9700. (split (org-split-string size " *x *"))
  9701. (rows (string-to-number (nth 1 split)))
  9702. (columns (string-to-number (car split)))
  9703. (line (concat (apply 'concat indent "|" (make-list columns " |"))
  9704. "\n")))
  9705. (if (string-match "^[ \t]*$" (buffer-substring-no-properties
  9706. (point-at-bol) (point)))
  9707. (beginning-of-line 1)
  9708. (newline))
  9709. ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
  9710. (dotimes (i rows) (insert line))
  9711. (goto-char pos)
  9712. (if (> rows 1)
  9713. ;; Insert a hline after the first row.
  9714. (progn
  9715. (end-of-line 1)
  9716. (insert "\n|-")
  9717. (goto-char pos)))
  9718. (org-table-align)))
  9719. (defun org-table-convert-region (beg0 end0 &optional nspace)
  9720. "Convert region to a table.
  9721. The region goes from BEG0 to END0, but these borders will be moved
  9722. slightly, to make sure a beginning of line in the first line is included.
  9723. When NSPACE is non-nil, it indicates the minimum number of spaces that
  9724. separate columns (default: just one space)."
  9725. (interactive "rP")
  9726. (let* ((beg (min beg0 end0))
  9727. (end (max beg0 end0))
  9728. (tabsep t)
  9729. re)
  9730. (goto-char beg)
  9731. (beginning-of-line 1)
  9732. (setq beg (move-marker (make-marker) (point)))
  9733. (goto-char end)
  9734. (if (bolp) (backward-char 1) (end-of-line 1))
  9735. (setq end (move-marker (make-marker) (point)))
  9736. ;; Lets see if this is tab-separated material. If every nonempty line
  9737. ;; contains a tab, we will assume that it is tab-separated material
  9738. (if nspace
  9739. (setq tabsep nil)
  9740. (goto-char beg)
  9741. (and (re-search-forward "^[^\n\t]+$" end t) (setq tabsep nil)))
  9742. (if nspace (setq tabsep nil))
  9743. (if tabsep
  9744. (setq re "^\\|\t")
  9745. (setq re (format "^ *\\| *\t *\\| \\{%d,\\}"
  9746. (max 1 (prefix-numeric-value nspace)))))
  9747. (goto-char beg)
  9748. (while (re-search-forward re end t)
  9749. (replace-match "|" t t))
  9750. (goto-char beg)
  9751. (insert " ")
  9752. (org-table-align)))
  9753. (defun org-table-import (file arg)
  9754. "Import FILE as a table.
  9755. The file is assumed to be tab-separated. Such files can be produced by most
  9756. spreadsheet and database applications. If no tabs (at least one per line)
  9757. are found, lines will be split on whitespace into fields."
  9758. (interactive "f\nP")
  9759. (or (bolp) (newline))
  9760. (let ((beg (point))
  9761. (pm (point-max)))
  9762. (insert-file-contents file)
  9763. (org-table-convert-region beg (+ (point) (- (point-max) pm)) arg)))
  9764. (defun org-table-export ()
  9765. "Export table as a tab-separated file.
  9766. Such a file can be imported into a spreadsheet program like Excel."
  9767. (interactive)
  9768. (let* ((beg (org-table-begin))
  9769. (end (org-table-end))
  9770. (table (buffer-substring beg end))
  9771. (file (read-file-name "Export table to: "))
  9772. buf)
  9773. (unless (or (not (file-exists-p file))
  9774. (y-or-n-p (format "Overwrite file %s? " file)))
  9775. (error "Abort"))
  9776. (with-current-buffer (find-file-noselect file)
  9777. (setq buf (current-buffer))
  9778. (erase-buffer)
  9779. (fundamental-mode)
  9780. (insert table)
  9781. (goto-char (point-min))
  9782. (while (re-search-forward "^[ \t]*|[ \t]*" nil t)
  9783. (replace-match "" t t)
  9784. (end-of-line 1))
  9785. (goto-char (point-min))
  9786. (while (re-search-forward "[ \t]*|[ \t]*$" nil t)
  9787. (replace-match "" t t)
  9788. (goto-char (min (1+ (point)) (point-max))))
  9789. (goto-char (point-min))
  9790. (while (re-search-forward "^-[-+]*$" nil t)
  9791. (replace-match "")
  9792. (if (looking-at "\n")
  9793. (delete-char 1)))
  9794. (goto-char (point-min))
  9795. (while (re-search-forward "[ \t]*|[ \t]*" nil t)
  9796. (replace-match "\t" t t))
  9797. (save-buffer))
  9798. (kill-buffer buf)))
  9799. (defvar org-table-aligned-begin-marker (make-marker)
  9800. "Marker at the beginning of the table last aligned.
  9801. Used to check if cursor still is in that table, to minimize realignment.")
  9802. (defvar org-table-aligned-end-marker (make-marker)
  9803. "Marker at the end of the table last aligned.
  9804. Used to check if cursor still is in that table, to minimize realignment.")
  9805. (defvar org-table-last-alignment nil
  9806. "List of flags for flushright alignment, from the last re-alignment.
  9807. This is being used to correctly align a single field after TAB or RET.")
  9808. (defvar org-table-last-column-widths nil
  9809. "List of max width of fields in each column.
  9810. This is being used to correctly align a single field after TAB or RET.")
  9811. (defvar org-last-recalc-line nil)
  9812. (defconst org-narrow-column-arrow "=>"
  9813. "Used as display property in narrowed table columns.")
  9814. (defun org-table-align ()
  9815. "Align the table at point by aligning all vertical bars."
  9816. (interactive)
  9817. (let* (
  9818. ;; Limits of table
  9819. (beg (org-table-begin))
  9820. (end (org-table-end))
  9821. ;; Current cursor position
  9822. (linepos (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
  9823. (colpos (org-table-current-column))
  9824. (winstart (window-start))
  9825. lines (new "") lengths l typenums ty fields maxfields i
  9826. column
  9827. (indent "") cnt frac
  9828. rfmt hfmt
  9829. (spaces '(1 . 1))
  9830. (sp1 (car spaces))
  9831. (sp2 (cdr spaces))
  9832. (rfmt1 (concat
  9833. (make-string sp2 ?\ ) "%%%s%ds" (make-string sp1 ?\ ) "|"))
  9834. (hfmt1 (concat
  9835. (make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
  9836. emptystrings links narrow fmax f1 len c e)
  9837. (untabify beg end)
  9838. (remove-text-properties beg end '(org-cwidth t display t))
  9839. ;; Check if we have links
  9840. (goto-char beg)
  9841. (setq links (re-search-forward org-bracket-link-regexp end t))
  9842. ;; Make sure the link properties are right
  9843. (when links (goto-char beg) (while (org-activate-bracket-links end)))
  9844. ;; Check if we are narrowing any columns
  9845. (goto-char beg)
  9846. (setq narrow (and org-format-transports-properties-p
  9847. (re-search-forward "<[0-9]+>" end t)))
  9848. ;; Get the rows
  9849. (setq lines (org-split-string
  9850. (buffer-substring beg end) "\n"))
  9851. ;; Store the indentation of the first line
  9852. (if (string-match "^ *" (car lines))
  9853. (setq indent (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  9854. ;; Mark the hlines by setting the corresponding element to nil
  9855. ;; At the same time, we remove trailing space.
  9856. (setq lines (mapcar (lambda (l)
  9857. (if (string-match "^ *|-" l)
  9858. nil
  9859. (if (string-match "[ \t]+$" l)
  9860. (substring l 0 (match-beginning 0))
  9861. l)))
  9862. lines))
  9863. ;; Get the data fields by splitting the lines.
  9864. (setq fields (mapcar
  9865. (lambda (l)
  9866. (org-split-string l " *| *"))
  9867. (delq nil (copy-sequence lines))))
  9868. ;; How many fields in the longest line?
  9869. (condition-case nil
  9870. (setq maxfields (apply 'max (mapcar 'length fields)))
  9871. (error
  9872. (kill-region beg end)
  9873. (org-table-create org-table-default-size)
  9874. (error "Empty table - created default table")))
  9875. ;; A list of empty string to fill any short rows on output
  9876. (setq emptystrings (make-list maxfields ""))
  9877. ;; Check for special formatting.
  9878. (setq i -1)
  9879. (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns
  9880. (setq column (mapcar (lambda (x) (or (nth i x) "")) fields))
  9881. ;; Check if there is an explicit width specified
  9882. (when (and org-table-limit-column-width narrow)
  9883. (setq c column fmax nil)
  9884. (while c
  9885. (setq e (pop c))
  9886. (if (and (stringp e) (string-match "^<\\([0-9]+\\)>$" e))
  9887. (setq fmax (string-to-number (match-string 1 e)) c nil)))
  9888. ;; Find fields that are wider than fmax, and shorten them
  9889. (when fmax
  9890. (loop for xx in column do
  9891. (when (and (stringp xx)
  9892. (> (org-string-width xx) fmax))
  9893. (org-add-props xx nil
  9894. 'help-echo
  9895. (concat "Clipped table field, use C-c ` to edit. Full value is:\n" (org-no-properties (copy-sequence xx))))
  9896. (setq f1 (min fmax (or (string-match org-bracket-link-regexp xx) fmax)))
  9897. (unless (> f1 1)
  9898. (error "Cannot narrow field starting with wide link \"%s\""
  9899. (match-string 0 xx)))
  9900. (add-text-properties f1 (length xx) (list 'org-cwidth t) xx)
  9901. (add-text-properties (- f1 2) f1
  9902. (list 'display org-narrow-column-arrow)
  9903. xx)))))
  9904. ;; Get the maximum width for each column
  9905. (push (apply 'max 1 (mapcar 'org-string-width column)) lengths)
  9906. ;; Get the fraction of numbers, to decide about alignment of the column
  9907. (setq cnt 0 frac 0.0)
  9908. (loop for x in column do
  9909. (if (equal x "")
  9910. nil
  9911. (setq frac ( / (+ (* frac cnt)
  9912. (if (string-match org-table-number-regexp x) 1 0))
  9913. (setq cnt (1+ cnt))))))
  9914. (push (>= frac org-table-number-fraction) typenums))
  9915. (setq lengths (nreverse lengths) typenums (nreverse typenums))
  9916. ;; Store the alignment of this table, for later editing of single fields
  9917. (setq org-table-last-alignment typenums
  9918. org-table-last-column-widths lengths)
  9919. ;; With invisible characters, `format' does not get the field width right
  9920. ;; So we need to make these fields wide by hand.
  9921. (when links
  9922. (loop for i from 0 upto (1- maxfields) do
  9923. (setq len (nth i lengths))
  9924. (loop for j from 0 upto (1- (length fields)) do
  9925. (setq c (nthcdr i (car (nthcdr j fields))))
  9926. (if (and (stringp (car c))
  9927. (string-match org-bracket-link-regexp (car c))
  9928. (< (org-string-width (car c)) len))
  9929. (setcar c (concat (car c) (make-string (- len (org-string-width (car c))) ?\ )))))))
  9930. ;; Compute the formats needed for output of the table
  9931. (setq rfmt (concat indent "|") hfmt (concat indent "|"))
  9932. (while (setq l (pop lengths))
  9933. (setq ty (if (pop typenums) "" "-")) ; number types flushright
  9934. (setq rfmt (concat rfmt (format rfmt1 ty l))
  9935. hfmt (concat hfmt (format hfmt1 (make-string l ?-)))))
  9936. (setq rfmt (concat rfmt "\n")
  9937. hfmt (concat (substring hfmt 0 -1) "|\n"))
  9938. (setq new (mapconcat
  9939. (lambda (l)
  9940. (if l (apply 'format rfmt
  9941. (append (pop fields) emptystrings))
  9942. hfmt))
  9943. lines ""))
  9944. ;; Replace the old one
  9945. (delete-region beg end)
  9946. (move-marker end nil)
  9947. (move-marker org-table-aligned-begin-marker (point))
  9948. (insert new)
  9949. (move-marker org-table-aligned-end-marker (point))
  9950. (when (and orgtbl-mode (not (org-mode-p)))
  9951. (goto-char org-table-aligned-begin-marker)
  9952. (while (org-hide-wide-columns org-table-aligned-end-marker)))
  9953. ;; Try to move to the old location (approximately)
  9954. (goto-line linepos)
  9955. (set-window-start (selected-window) winstart 'noforce)
  9956. (org-table-goto-column colpos)
  9957. (setq org-table-may-need-update nil)
  9958. ))
  9959. (defun org-string-width (s)
  9960. "Compute width of string, ignoring invisible characters.
  9961. This ignores character with invisibility property `org-link', and also
  9962. characters with property `org-cwidth', because these will become invisible
  9963. upon the next fontification round."
  9964. (let (b)
  9965. (when (or (eq t buffer-invisibility-spec)
  9966. (assq 'org-link buffer-invisibility-spec))
  9967. (while (setq b (text-property-any 0 (length s)
  9968. 'invisible 'org-link s))
  9969. (setq s (concat (substring s 0 b)
  9970. (substring s (or (next-single-property-change
  9971. b 'invisible s) (length s)))))))
  9972. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  9973. (setq s (concat (substring s 0 b)
  9974. (substring s (or (next-single-property-change
  9975. b 'org-cwidth s) (length s))))))
  9976. (string-width s)))
  9977. (defun org-table-begin (&optional table-type)
  9978. "Find the beginning of the table and return its position.
  9979. With argument TABLE-TYPE, go to the beginning of a table.el-type table."
  9980. (save-excursion
  9981. (if (not (re-search-backward
  9982. (if table-type org-table-any-border-regexp
  9983. org-table-border-regexp)
  9984. nil t))
  9985. (progn (goto-char (point-min)) (point))
  9986. (goto-char (match-beginning 0))
  9987. (beginning-of-line 2)
  9988. (point))))
  9989. (defun org-table-end (&optional table-type)
  9990. "Find the end of the table and return its position.
  9991. With argument TABLE-TYPE, go to the end of a table.el-type table."
  9992. (save-excursion
  9993. (if (not (re-search-forward
  9994. (if table-type org-table-any-border-regexp
  9995. org-table-border-regexp)
  9996. nil t))
  9997. (goto-char (point-max))
  9998. (goto-char (match-beginning 0)))
  9999. (point-marker)))
  10000. (defun org-table-justify-field-maybe (&optional new)
  10001. "Justify the current field, text to left, number to right.
  10002. Optional argument NEW may specify text to replace the current field content."
  10003. (cond
  10004. ((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
  10005. ((org-at-table-hline-p))
  10006. ((and (not new)
  10007. (or (not (equal (marker-buffer org-table-aligned-begin-marker)
  10008. (current-buffer)))
  10009. (< (point) org-table-aligned-begin-marker)
  10010. (>= (point) org-table-aligned-end-marker)))
  10011. ;; This is not the same table, force a full re-align
  10012. (setq org-table-may-need-update t))
  10013. (t ;; realign the current field, based on previous full realign
  10014. (let* ((pos (point)) s
  10015. (col (org-table-current-column))
  10016. (num (if (> col 0) (nth (1- col) org-table-last-alignment)))
  10017. l f n o e)
  10018. (when (> col 0)
  10019. (skip-chars-backward "^|\n")
  10020. (if (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)")
  10021. (progn
  10022. (setq s (match-string 1)
  10023. o (match-string 0)
  10024. l (max 1 (- (match-end 0) (match-beginning 0) 3))
  10025. e (not (= (match-beginning 2) (match-end 2))))
  10026. (setq f (format (if num " %%%ds %s" " %%-%ds %s")
  10027. l (if e "|" (setq org-table-may-need-update t) ""))
  10028. n (format f s))
  10029. (if new
  10030. (if (<= (length new) l) ;; FIXME: length -> str-width?
  10031. (setq n (format f new))
  10032. (setq n (concat new "|") org-table-may-need-update t)))
  10033. (or (equal n o)
  10034. (let (org-table-may-need-update)
  10035. (replace-match n))))
  10036. (setq org-table-may-need-update t))
  10037. (goto-char pos))))))
  10038. (defun org-table-next-field ()
  10039. "Go to the next field in the current table, creating new lines as needed.
  10040. Before doing so, re-align the table if necessary."
  10041. (interactive)
  10042. (org-table-maybe-eval-formula)
  10043. (org-table-maybe-recalculate-line)
  10044. (if (and org-table-automatic-realign
  10045. org-table-may-need-update)
  10046. (org-table-align))
  10047. (let ((end (org-table-end)))
  10048. (if (org-at-table-hline-p)
  10049. (end-of-line 1))
  10050. (condition-case nil
  10051. (progn
  10052. (re-search-forward "|" end)
  10053. (if (looking-at "[ \t]*$")
  10054. (re-search-forward "|" end))
  10055. (if (and (looking-at "-")
  10056. org-table-tab-jumps-over-hlines
  10057. (re-search-forward "^[ \t]*|\\([^-]\\)" end t))
  10058. (goto-char (match-beginning 1)))
  10059. (if (looking-at "-")
  10060. (progn
  10061. (beginning-of-line 0)
  10062. (org-table-insert-row 'below))
  10063. (if (looking-at " ") (forward-char 1))))
  10064. (error
  10065. (org-table-insert-row 'below)))))
  10066. (defun org-table-previous-field ()
  10067. "Go to the previous field in the table.
  10068. Before doing so, re-align the table if necessary."
  10069. (interactive)
  10070. (org-table-justify-field-maybe)
  10071. (org-table-maybe-recalculate-line)
  10072. (if (and org-table-automatic-realign
  10073. org-table-may-need-update)
  10074. (org-table-align))
  10075. (if (org-at-table-hline-p)
  10076. (end-of-line 1))
  10077. (re-search-backward "|" (org-table-begin))
  10078. (re-search-backward "|" (org-table-begin))
  10079. (while (looking-at "|\\(-\\|[ \t]*$\\)")
  10080. (re-search-backward "|" (org-table-begin)))
  10081. (if (looking-at "| ?")
  10082. (goto-char (match-end 0))))
  10083. (defun org-table-next-row ()
  10084. "Go to the next row (same column) in the current table.
  10085. Before doing so, re-align the table if necessary."
  10086. (interactive)
  10087. (org-table-maybe-eval-formula)
  10088. (org-table-maybe-recalculate-line)
  10089. (if (or (looking-at "[ \t]*$")
  10090. (save-excursion (skip-chars-backward " \t") (bolp)))
  10091. (newline)
  10092. (if (and org-table-automatic-realign
  10093. org-table-may-need-update)
  10094. (org-table-align))
  10095. (let ((col (org-table-current-column)))
  10096. (beginning-of-line 2)
  10097. (if (or (not (org-at-table-p))
  10098. (org-at-table-hline-p))
  10099. (progn
  10100. (beginning-of-line 0)
  10101. (org-table-insert-row 'below)))
  10102. (org-table-goto-column col)
  10103. (skip-chars-backward "^|\n\r")
  10104. (if (looking-at " ") (forward-char 1)))))
  10105. (defun org-table-copy-down (n)
  10106. "Copy a field down in the current column.
  10107. If the field at the cursor is empty, copy into it the content of the nearest
  10108. non-empty field above. With argument N, use the Nth non-empty field.
  10109. If the current field is not empty, it is copied down to the next row, and
  10110. the cursor is moved with it. Therefore, repeating this command causes the
  10111. column to be filled row-by-row.
  10112. If the variable `org-table-copy-increment' is non-nil and the field is an
  10113. integer, it will be incremented while copying."
  10114. (interactive "p")
  10115. (let* ((colpos (org-table-current-column))
  10116. (field (org-table-get-field))
  10117. (non-empty (string-match "[^ \t]" field))
  10118. (beg (org-table-begin))
  10119. txt)
  10120. (org-table-check-inside-data-field)
  10121. (if non-empty
  10122. (progn
  10123. (setq txt (org-trim field))
  10124. (org-table-next-row)
  10125. (org-table-blank-field))
  10126. (save-excursion
  10127. (setq txt
  10128. (catch 'exit
  10129. (while (progn (beginning-of-line 1)
  10130. (re-search-backward org-table-dataline-regexp
  10131. beg t))
  10132. (org-table-goto-column colpos t)
  10133. (if (and (looking-at
  10134. "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
  10135. (= (setq n (1- n)) 0))
  10136. (throw 'exit (match-string 1))))))))
  10137. (if txt
  10138. (progn
  10139. (if (and org-table-copy-increment
  10140. (string-match "^[0-9]+$" txt))
  10141. (setq txt (format "%d" (+ (string-to-number txt) 1))))
  10142. (insert txt)
  10143. (org-table-maybe-recalculate-line)
  10144. (org-table-align))
  10145. (error "No non-empty field found"))))
  10146. (defun org-table-check-inside-data-field ()
  10147. "Is point inside a table data field?
  10148. I.e. not on a hline or before the first or after the last column?
  10149. This actually throws an error, so it aborts the current command."
  10150. (if (or (not (org-at-table-p))
  10151. (= (org-table-current-column) 0)
  10152. (org-at-table-hline-p)
  10153. (looking-at "[ \t]*$"))
  10154. (error "Not in table data field")))
  10155. (defvar org-table-clip nil
  10156. "Clipboard for table regions.")
  10157. (defun org-table-blank-field ()
  10158. "Blank the current table field or active region."
  10159. (interactive)
  10160. (org-table-check-inside-data-field)
  10161. (if (and (interactive-p) (org-region-active-p))
  10162. (let (org-table-clip)
  10163. (org-table-cut-region (region-beginning) (region-end)))
  10164. (skip-chars-backward "^|")
  10165. (backward-char 1)
  10166. (if (looking-at "|[^|\n]+")
  10167. (let* ((pos (match-beginning 0))
  10168. (match (match-string 0))
  10169. (len (org-string-width match)))
  10170. (replace-match (concat "|" (make-string (1- len) ?\ )))
  10171. (goto-char (+ 2 pos))
  10172. (substring match 1)))))
  10173. (defun org-table-get-field (&optional n replace)
  10174. "Return the value of the field in column N of current row.
  10175. N defaults to current field.
  10176. If REPLACE is a string, replace field with this value. The return value
  10177. is always the old value."
  10178. (and n (org-table-goto-column n))
  10179. (skip-chars-backward "^|\n")
  10180. (backward-char 1)
  10181. (if (looking-at "|[^|\r\n]*")
  10182. (let* ((pos (match-beginning 0))
  10183. (val (buffer-substring (1+ pos) (match-end 0))))
  10184. (if replace
  10185. (replace-match (concat "|" replace)))
  10186. (goto-char (min (point-at-eol) (+ 2 pos)))
  10187. val)
  10188. (forward-char 1) ""))
  10189. (defun org-table-current-column ()
  10190. "Find out which column we are in.
  10191. When called interactively, column is also displayed in echo area."
  10192. (interactive)
  10193. (if (interactive-p) (org-table-check-inside-data-field))
  10194. (save-excursion
  10195. (let ((cnt 0) (pos (point)))
  10196. (beginning-of-line 1)
  10197. (while (search-forward "|" pos t)
  10198. (setq cnt (1+ cnt)))
  10199. (if (interactive-p) (message "This is table column %d" cnt))
  10200. cnt)))
  10201. (defun org-table-goto-column (n &optional on-delim force)
  10202. "Move the cursor to the Nth column in the current table line.
  10203. With optional argument ON-DELIM, stop with point before the left delimiter
  10204. of the field.
  10205. If there are less than N fields, just go to after the last delimiter.
  10206. However, when FORCE is non-nil, create new columns if necessary."
  10207. (interactive "p")
  10208. (let ((pos (point-at-eol)))
  10209. (beginning-of-line 1)
  10210. (when (> n 0)
  10211. (while (and (> (setq n (1- n)) -1)
  10212. (or (search-forward "|" pos t)
  10213. (and force
  10214. (progn (end-of-line 1)
  10215. (skip-chars-backward "^|")
  10216. (insert " | "))))))
  10217. ; (backward-char 2) t)))))
  10218. (when (and force (not (looking-at ".*|")))
  10219. (save-excursion (end-of-line 1) (insert " | ")))
  10220. (if on-delim
  10221. (backward-char 1)
  10222. (if (looking-at " ") (forward-char 1))))))
  10223. (defun org-at-table-p (&optional table-type)
  10224. "Return t if the cursor is inside an org-type table.
  10225. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  10226. (if org-enable-table-editor
  10227. (save-excursion
  10228. (beginning-of-line 1)
  10229. (looking-at (if table-type org-table-any-line-regexp
  10230. org-table-line-regexp)))
  10231. nil))
  10232. (defun org-at-table.el-p ()
  10233. "Return t if and only if we are at a table.el table."
  10234. (and (org-at-table-p 'any)
  10235. (save-excursion
  10236. (goto-char (org-table-begin 'any))
  10237. (looking-at org-table1-hline-regexp))))
  10238. (defun org-table-recognize-table.el ()
  10239. "If there is a table.el table nearby, recognize it and move into it."
  10240. (if org-table-tab-recognizes-table.el
  10241. (if (org-at-table.el-p)
  10242. (progn
  10243. (beginning-of-line 1)
  10244. (if (looking-at org-table-dataline-regexp)
  10245. nil
  10246. (if (looking-at org-table1-hline-regexp)
  10247. (progn
  10248. (beginning-of-line 2)
  10249. (if (looking-at org-table-any-border-regexp)
  10250. (beginning-of-line -1)))))
  10251. (if (re-search-forward "|" (org-table-end t) t)
  10252. (progn
  10253. (require 'table)
  10254. (if (table--at-cell-p (point))
  10255. t
  10256. (message "recognizing table.el table...")
  10257. (table-recognize-table)
  10258. (message "recognizing table.el table...done")))
  10259. (error "This should not happen..."))
  10260. t)
  10261. nil)
  10262. nil))
  10263. (defun org-at-table-hline-p ()
  10264. "Return t if the cursor is inside a hline in a table."
  10265. (if org-enable-table-editor
  10266. (save-excursion
  10267. (beginning-of-line 1)
  10268. (looking-at org-table-hline-regexp))
  10269. nil))
  10270. (defun org-table-insert-column ()
  10271. "Insert a new column into the table."
  10272. (interactive)
  10273. (if (not (org-at-table-p))
  10274. (error "Not at a table"))
  10275. (org-table-find-dataline)
  10276. (let* ((col (max 1 (org-table-current-column)))
  10277. (beg (org-table-begin))
  10278. (end (org-table-end))
  10279. ;; Current cursor position
  10280. (linepos (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
  10281. (colpos col))
  10282. (goto-char beg)
  10283. (while (< (point) end)
  10284. (if (org-at-table-hline-p)
  10285. nil
  10286. (org-table-goto-column col t)
  10287. (insert "| "))
  10288. (beginning-of-line 2))
  10289. (move-marker end nil)
  10290. (goto-line linepos)
  10291. (org-table-goto-column colpos)
  10292. (org-table-align)
  10293. (org-table-modify-formulas 'insert col)))
  10294. (defun org-table-find-dataline ()
  10295. "Find a dataline in the current table, which is needed for column commands."
  10296. (if (and (org-at-table-p)
  10297. (not (org-at-table-hline-p)))
  10298. t
  10299. (let ((col (current-column))
  10300. (end (org-table-end)))
  10301. (move-to-column col)
  10302. (while (and (< (point) end)
  10303. (or (not (= (current-column) col))
  10304. (org-at-table-hline-p)))
  10305. (beginning-of-line 2)
  10306. (move-to-column col))
  10307. (if (and (org-at-table-p)
  10308. (not (org-at-table-hline-p)))
  10309. t
  10310. (error
  10311. "Please position cursor in a data line for column operations")))))
  10312. (defun org-table-delete-column ()
  10313. "Delete a column from the table."
  10314. (interactive)
  10315. (if (not (org-at-table-p))
  10316. (error "Not at a table"))
  10317. (org-table-find-dataline)
  10318. (org-table-check-inside-data-field)
  10319. (let* ((col (org-table-current-column))
  10320. (beg (org-table-begin))
  10321. (end (org-table-end))
  10322. ;; Current cursor position
  10323. (linepos (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
  10324. (colpos col))
  10325. (goto-char beg)
  10326. (while (< (point) end)
  10327. (if (org-at-table-hline-p)
  10328. nil
  10329. (org-table-goto-column col t)
  10330. (and (looking-at "|[^|\n]+|")
  10331. (replace-match "|")))
  10332. (beginning-of-line 2))
  10333. (move-marker end nil)
  10334. (goto-line linepos)
  10335. (org-table-goto-column colpos)
  10336. (org-table-align)
  10337. (org-table-modify-formulas 'remove col)))
  10338. (defun org-table-move-column-right ()
  10339. "Move column to the right."
  10340. (interactive)
  10341. (org-table-move-column nil))
  10342. (defun org-table-move-column-left ()
  10343. "Move column to the left."
  10344. (interactive)
  10345. (org-table-move-column 'left))
  10346. (defun org-table-move-column (&optional left)
  10347. "Move the current column to the right. With arg LEFT, move to the left."
  10348. (interactive "P")
  10349. (if (not (org-at-table-p))
  10350. (error "Not at a table"))
  10351. (org-table-find-dataline)
  10352. (org-table-check-inside-data-field)
  10353. (let* ((col (org-table-current-column))
  10354. (col1 (if left (1- col) col))
  10355. (beg (org-table-begin))
  10356. (end (org-table-end))
  10357. ;; Current cursor position
  10358. (linepos (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
  10359. (colpos (if left (1- col) (1+ col))))
  10360. (if (and left (= col 1))
  10361. (error "Cannot move column further left"))
  10362. (if (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
  10363. (error "Cannot move column further right"))
  10364. (goto-char beg)
  10365. (while (< (point) end)
  10366. (if (org-at-table-hline-p)
  10367. nil
  10368. (org-table-goto-column col1 t)
  10369. (and (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
  10370. (replace-match "|\\2|\\1|")))
  10371. (beginning-of-line 2))
  10372. (move-marker end nil)
  10373. (goto-line linepos)
  10374. (org-table-goto-column colpos)
  10375. (org-table-align)
  10376. (org-table-modify-formulas 'swap col (if left (1- col) (1+ col)))))
  10377. (defun org-table-move-row-down ()
  10378. "Move table row down."
  10379. (interactive)
  10380. (org-table-move-row nil))
  10381. (defun org-table-move-row-up ()
  10382. "Move table row up."
  10383. (interactive)
  10384. (org-table-move-row 'up))
  10385. (defun org-table-move-row (&optional up)
  10386. "Move the current table line down. With arg UP, move it up."
  10387. (interactive "P")
  10388. (let ((col (current-column))
  10389. (pos (point))
  10390. (tonew (if up 0 2))
  10391. txt)
  10392. (beginning-of-line tonew)
  10393. (if (not (org-at-table-p))
  10394. (progn
  10395. (goto-char pos)
  10396. (error "Cannot move row further")))
  10397. (goto-char pos)
  10398. (beginning-of-line 1)
  10399. (setq pos (point))
  10400. (setq txt (buffer-substring (point) (1+ (point-at-eol))))
  10401. (delete-region (point) (1+ (point-at-eol)))
  10402. (beginning-of-line tonew)
  10403. (insert txt)
  10404. (beginning-of-line 0)
  10405. (move-to-column col)))
  10406. (defun org-table-insert-row (&optional arg)
  10407. "Insert a new row above the current line into the table.
  10408. With prefix ARG, insert below the current line."
  10409. (interactive "P")
  10410. (if (not (org-at-table-p))
  10411. (error "Not at a table"))
  10412. (let* ((line (buffer-substring (point-at-bol) (point-at-eol)))
  10413. (new (org-table-clean-line line)))
  10414. ;; Fix the first field if necessary
  10415. (if (string-match "^[ \t]*| *[#$] *|" line)
  10416. (setq new (replace-match (match-string 0 line) t t new)))
  10417. (beginning-of-line (if arg 2 1))
  10418. (let (org-table-may-need-update) (insert-before-markers new "\n"))
  10419. (beginning-of-line 0)
  10420. (re-search-forward "| ?" (point-at-eol) t)
  10421. (and org-table-may-need-update (org-table-align))))
  10422. (defun org-table-insert-hline (&optional arg)
  10423. "Insert a horizontal-line below the current line into the table.
  10424. With prefix ARG, insert above the current line."
  10425. (interactive "P")
  10426. (if (not (org-at-table-p))
  10427. (error "Not at a table"))
  10428. (let ((line (org-table-clean-line
  10429. (buffer-substring (point-at-bol) (point-at-eol))))
  10430. (col (current-column)))
  10431. (while (string-match "|\\( +\\)|" line)
  10432. (setq line (replace-match
  10433. (concat "+" (make-string (- (match-end 1) (match-beginning 1))
  10434. ?-) "|") t t line)))
  10435. (and (string-match "\\+" line) (setq line (replace-match "|" t t line)))
  10436. (beginning-of-line (if arg 1 2))
  10437. (insert line "\n")
  10438. (beginning-of-line (if arg 1 -1))
  10439. (move-to-column col)))
  10440. (defun org-table-clean-line (s)
  10441. "Convert a table line S into a string with only \"|\" and space.
  10442. In particular, this does handle wide and invisible characters."
  10443. (if (string-match "^[ \t]*|-" s)
  10444. ;; It's a hline, just map the characters
  10445. (setq s (mapconcat (lambda (x) (if (member x '(?| ?+)) "|" " ")) s ""))
  10446. (while (string-match "|\\([ \t]*?[^ \t\r\n|][^\r\n|]*\\)|" s)
  10447. (setq s (replace-match
  10448. (concat "|" (make-string (org-string-width (match-string 1 s))
  10449. ?\ ) "|")
  10450. t t s)))
  10451. s))
  10452. (defun org-table-kill-row ()
  10453. "Delete the current row or horizontal line from the table."
  10454. (interactive)
  10455. (if (not (org-at-table-p))
  10456. (error "Not at a table"))
  10457. (let ((col (current-column)))
  10458. (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
  10459. (if (not (org-at-table-p)) (beginning-of-line 0))
  10460. (move-to-column col)))
  10461. (defun org-table-sort-lines (beg end numericp)
  10462. "Sort table lines in region.
  10463. Point and mark define the first and last line to include. Both point and
  10464. mark should be in the column that is used for sorting. For example, to
  10465. sort according to column 3, put the mark in the first line to sort, in
  10466. table column 3. Put point into the last line to be included in the sorting,
  10467. also in table column 3. The command will prompt for the sorting method
  10468. \(n for numerical, a for alphanumeric)."
  10469. (interactive "r\nsSorting method: [n]=numeric [a]=alpha: ")
  10470. (setq numericp (string-match "[nN]" numericp))
  10471. (org-table-align) ;; Just to be safe
  10472. (let* (bcol ecol cmp column lns)
  10473. (goto-char beg)
  10474. (org-table-check-inside-data-field)
  10475. (setq column (org-table-current-column)
  10476. beg (move-marker (make-marker) (point-at-bol)))
  10477. (goto-char end)
  10478. (org-table-check-inside-data-field)
  10479. (setq end (move-marker (make-marker) (1+ (point-at-eol))))
  10480. (untabify beg end)
  10481. (goto-char beg)
  10482. (org-table-goto-column column)
  10483. (skip-chars-backward "^|")
  10484. (setq bcol (current-column))
  10485. (org-table-goto-column (1+ column))
  10486. (skip-chars-backward "^|")
  10487. (setq ecol (1- (current-column)))
  10488. (setq cmp (if numericp
  10489. (lambda (a b) (< (car a) (car b)))
  10490. (lambda (a b) (string< (car a) (car b)))))
  10491. (setq lns (mapcar (lambda(x) (cons (org-trim (substring x bcol ecol)) x))
  10492. (org-split-string (buffer-substring beg end) "\n")))
  10493. (if numericp
  10494. (setq lns (mapcar (lambda(x)
  10495. (cons (string-to-number (car x)) (cdr x)))
  10496. lns)))
  10497. (delete-region beg end)
  10498. (move-marker beg nil)
  10499. (move-marker end nil)
  10500. (insert (mapconcat 'cdr (setq lns (sort lns cmp)) "\n") "\n")
  10501. (message "%d lines sorted %s based on column %d"
  10502. (length lns)
  10503. (if numericp "numerically" "alphabetically") column)))
  10504. (defun org-table-cut-region (beg end)
  10505. "Copy region in table to the clipboard and blank all relevant fields."
  10506. (interactive "r")
  10507. (org-table-copy-region beg end 'cut))
  10508. (defun org-table-copy-region (beg end &optional cut)
  10509. "Copy rectangular region in table to clipboard.
  10510. A special clipboard is used which can only be accessed
  10511. with `org-table-paste-rectangle'."
  10512. (interactive "rP")
  10513. (let* (l01 c01 l02 c02 l1 c1 l2 c2 ic1 ic2
  10514. region cols
  10515. (rpl (if cut " " nil)))
  10516. (goto-char beg)
  10517. (org-table-check-inside-data-field)
  10518. (setq l01 (count-lines (point-min) (point))
  10519. c01 (org-table-current-column))
  10520. (goto-char end)
  10521. (org-table-check-inside-data-field)
  10522. (setq l02 (count-lines (point-min) (point))
  10523. c02 (org-table-current-column))
  10524. (setq l1 (min l01 l02) l2 (max l01 l02)
  10525. c1 (min c01 c02) c2 (max c01 c02))
  10526. (catch 'exit
  10527. (while t
  10528. (catch 'nextline
  10529. (if (> l1 l2) (throw 'exit t))
  10530. (goto-line l1)
  10531. (if (org-at-table-hline-p) (throw 'nextline (setq l1 (1+ l1))))
  10532. (setq cols nil ic1 c1 ic2 c2)
  10533. (while (< ic1 (1+ ic2))
  10534. (push (org-table-get-field ic1 rpl) cols)
  10535. (setq ic1 (1+ ic1)))
  10536. (push (nreverse cols) region)
  10537. (setq l1 (1+ l1)))))
  10538. (setq org-table-clip (nreverse region))
  10539. (if cut (org-table-align))
  10540. org-table-clip))
  10541. (defun org-table-paste-rectangle ()
  10542. "Paste a rectangular region into a table.
  10543. The upper right corner ends up in the current field. All involved fields
  10544. will be overwritten. If the rectangle does not fit into the present table,
  10545. the table is enlarged as needed. The process ignores horizontal separator
  10546. lines."
  10547. (interactive)
  10548. (unless (and org-table-clip (listp org-table-clip))
  10549. (error "First cut/copy a region to paste!"))
  10550. (org-table-check-inside-data-field)
  10551. (let* ((clip org-table-clip)
  10552. (line (count-lines (point-min) (point)))
  10553. (col (org-table-current-column))
  10554. (org-enable-table-editor t)
  10555. (org-table-automatic-realign nil)
  10556. c cols field)
  10557. (while (setq cols (pop clip))
  10558. (while (org-at-table-hline-p) (beginning-of-line 2))
  10559. (if (not (org-at-table-p))
  10560. (progn (end-of-line 0) (org-table-next-field)))
  10561. (setq c col)
  10562. (while (setq field (pop cols))
  10563. (org-table-goto-column c nil 'force)
  10564. (org-table-get-field nil field)
  10565. (setq c (1+ c)))
  10566. (beginning-of-line 2))
  10567. (goto-line line)
  10568. (org-table-goto-column col)
  10569. (org-table-align)))
  10570. (defun org-table-convert ()
  10571. "Convert from `org-mode' table to table.el and back.
  10572. Obviously, this only works within limits. When an Org-mode table is
  10573. converted to table.el, all horizontal separator lines get lost, because
  10574. table.el uses these as cell boundaries and has no notion of horizontal lines.
  10575. A table.el table can be converted to an Org-mode table only if it does not
  10576. do row or column spanning. Multiline cells will become multiple cells.
  10577. Beware, Org-mode does not test if the table can be successfully converted - it
  10578. blindly applies a recipe that works for simple tables."
  10579. (interactive)
  10580. (require 'table)
  10581. (if (org-at-table.el-p)
  10582. ;; convert to Org-mode table
  10583. (let ((beg (move-marker (make-marker) (org-table-begin t)))
  10584. (end (move-marker (make-marker) (org-table-end t))))
  10585. (table-unrecognize-region beg end)
  10586. (goto-char beg)
  10587. (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
  10588. (replace-match ""))
  10589. (goto-char beg))
  10590. (if (org-at-table-p)
  10591. ;; convert to table.el table
  10592. (let ((beg (move-marker (make-marker) (org-table-begin)))
  10593. (end (move-marker (make-marker) (org-table-end))))
  10594. ;; first, get rid of all horizontal lines
  10595. (goto-char beg)
  10596. (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t)
  10597. (replace-match ""))
  10598. ;; insert a hline before first
  10599. (goto-char beg)
  10600. (org-table-insert-hline 'above)
  10601. (beginning-of-line -1)
  10602. ;; insert a hline after each line
  10603. (while (progn (beginning-of-line 3) (< (point) end))
  10604. (org-table-insert-hline))
  10605. (goto-char beg)
  10606. (setq end (move-marker end (org-table-end)))
  10607. ;; replace "+" at beginning and ending of hlines
  10608. (while (re-search-forward "^\\([ \t]*\\)|-" end t)
  10609. (replace-match "\\1+-"))
  10610. (goto-char beg)
  10611. (while (re-search-forward "-|[ \t]*$" end t)
  10612. (replace-match "-+"))
  10613. (goto-char beg)))))
  10614. (defun org-table-wrap-region (arg)
  10615. "Wrap several fields in a column like a paragraph.
  10616. This is useful if you'd like to spread the contents of a field over several
  10617. lines, in order to keep the table compact.
  10618. If there is an active region, and both point and mark are in the same column,
  10619. the text in the column is wrapped to minimum width for the given number of
  10620. lines. Generally, this makes the table more compact. A prefix ARG may be
  10621. used to change the number of desired lines. For example, `C-2 \\[org-table-wrap]'
  10622. formats the selected text to two lines. If the region was longer than two
  10623. lines, the remaining lines remain empty. A negative prefix argument reduces
  10624. the current number of lines by that amount. The wrapped text is pasted back
  10625. into the table. If you formatted it to more lines than it was before, fields
  10626. further down in the table get overwritten - so you might need to make space in
  10627. the table first.
  10628. If there is no region, the current field is split at the cursor position and
  10629. the text fragment to the right of the cursor is prepended to the field one
  10630. line down.
  10631. If there is no region, but you specify a prefix ARG, the current field gets
  10632. blank, and the content is appended to the field above."
  10633. (interactive "P")
  10634. (org-table-check-inside-data-field)
  10635. (if (org-region-active-p)
  10636. ;; There is a region: fill as a paragraph
  10637. (let ((beg (region-beginning))
  10638. nlines)
  10639. (org-table-cut-region (region-beginning) (region-end))
  10640. (if (> (length (car org-table-clip)) 1)
  10641. (error "Region must be limited to single column"))
  10642. (setq nlines (if arg
  10643. (if (< arg 1)
  10644. (+ (length org-table-clip) arg)
  10645. arg)
  10646. (length org-table-clip)))
  10647. (setq org-table-clip
  10648. (mapcar 'list (org-wrap (mapconcat 'car org-table-clip " ")
  10649. nil nlines)))
  10650. (goto-char beg)
  10651. (org-table-paste-rectangle))
  10652. ;; No region, split the current field at point
  10653. (if arg
  10654. ;; combine with field above
  10655. (let ((s (org-table-blank-field))
  10656. (col (org-table-current-column)))
  10657. (beginning-of-line 0)
  10658. (while (org-at-table-hline-p) (beginning-of-line 0))
  10659. (org-table-goto-column col)
  10660. (skip-chars-forward "^|")
  10661. (skip-chars-backward " ")
  10662. (insert " " (org-trim s))
  10663. (org-table-align))
  10664. ;; split field
  10665. (when (looking-at "\\([^|]+\\)+|")
  10666. (let ((s (match-string 1)))
  10667. (replace-match " |")
  10668. (goto-char (match-beginning 0))
  10669. (org-table-next-row)
  10670. (insert (org-trim s) " ")
  10671. (org-table-align))))))
  10672. (defvar org-field-marker nil)
  10673. (defun org-table-edit-field (arg)
  10674. "Edit table field in a different window.
  10675. This is mainly useful for fields that contain hidden parts.
  10676. When called with a \\[universal-argument] prefix, just make the full field visible so that
  10677. it can be edited in place."
  10678. (interactive "P")
  10679. (if arg
  10680. (let ((b (save-excursion (skip-chars-backward "^|") (point)))
  10681. (e (save-excursion (skip-chars-forward "^|\r\n") (point))))
  10682. (remove-text-properties b e '(org-cwidth t invisible t
  10683. display t intangible t))
  10684. (if (and (boundp 'font-lock-mode) font-lock-mode)
  10685. (font-lock-fontify-block)))
  10686. (let ((pos (move-marker (make-marker) (point)))
  10687. (field (org-table-get-field))
  10688. (cw (current-window-configuration))
  10689. p)
  10690. (switch-to-buffer-other-window "*Org tmp*")
  10691. (erase-buffer)
  10692. (insert "#\n# Edit field and finish with C-c C-c\n#\n")
  10693. (org-mode)
  10694. (goto-char (setq p (point-max)))
  10695. (insert (org-trim field))
  10696. (remove-text-properties p (point-max)
  10697. '(invisible t org-cwidth t display t
  10698. intangible t))
  10699. (goto-char p)
  10700. (org-set-local 'org-finish-function
  10701. 'org-table-finish-edit-field)
  10702. (org-set-local 'org-window-configuration cw)
  10703. (org-set-local 'org-field-marker pos)
  10704. (message "Edit and finish with C-c C-c"))))
  10705. (defun org-table-finish-edit-field ()
  10706. "Finish editing a table data field.
  10707. Remove all newline characters, insert the result into the table, realign
  10708. the table and kill the editing buffer."
  10709. (let ((pos org-field-marker)
  10710. (cw org-window-configuration)
  10711. (cb (current-buffer))
  10712. text)
  10713. (goto-char (point-min))
  10714. (while (re-search-forward "^#.*\n?" nil t) (replace-match ""))
  10715. (while (re-search-forward "\\([ \t]*\n[ \t]*\\)+" nil t)
  10716. (replace-match " "))
  10717. (setq text (org-trim (buffer-string)))
  10718. (set-window-configuration cw)
  10719. (kill-buffer cb)
  10720. (select-window (get-buffer-window (marker-buffer pos)))
  10721. (goto-char pos)
  10722. (move-marker pos nil)
  10723. (org-table-check-inside-data-field)
  10724. (org-table-get-field nil text)
  10725. (org-table-align)
  10726. (message "New field value inserted")))
  10727. (defun org-trim (s)
  10728. "Remove whitespace at beginning and end of string."
  10729. (if (string-match "^[ \t]+" s) (setq s (replace-match "" t t s)))
  10730. (if (string-match "[ \t]+$" s) (setq s (replace-match "" t t s)))
  10731. s)
  10732. (defun org-wrap (string &optional width lines)
  10733. "Wrap string to either a number of lines, or a width in characters.
  10734. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  10735. that costs. If there is a word longer than WIDTH, the text is actually
  10736. wrapped to the length of that word.
  10737. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  10738. many lines, whatever width that takes.
  10739. The return value is a list of lines, without newlines at the end."
  10740. (let* ((words (org-split-string string "[ \t\n]+"))
  10741. (maxword (apply 'max (mapcar 'org-string-width words)))
  10742. w ll)
  10743. (cond (width
  10744. (org-do-wrap words (max maxword width)))
  10745. (lines
  10746. (setq w maxword)
  10747. (setq ll (org-do-wrap words maxword))
  10748. (if (<= (length ll) lines)
  10749. ll
  10750. (setq ll words)
  10751. (while (> (length ll) lines)
  10752. (setq w (1+ w))
  10753. (setq ll (org-do-wrap words w)))
  10754. ll))
  10755. (t (error "Cannot wrap this")))))
  10756. (defun org-do-wrap (words width)
  10757. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  10758. (let (lines line)
  10759. (while words
  10760. (setq line (pop words))
  10761. (while (and words (< (+ (length line) (length (car words))) width))
  10762. (setq line (concat line " " (pop words))))
  10763. (setq lines (push line lines)))
  10764. (nreverse lines)))
  10765. (defun org-split-string (string &optional separators)
  10766. "Splits STRING into substrings at SEPARATORS.
  10767. No empty strings are returned if there are matches at the beginning
  10768. and end of string."
  10769. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  10770. (start 0)
  10771. notfirst
  10772. (list nil))
  10773. (while (and (string-match rexp string
  10774. (if (and notfirst
  10775. (= start (match-beginning 0))
  10776. (< start (length string)))
  10777. (1+ start) start))
  10778. (< (match-beginning 0) (length string)))
  10779. (setq notfirst t)
  10780. (or (eq (match-beginning 0) 0)
  10781. (and (eq (match-beginning 0) (match-end 0))
  10782. (eq (match-beginning 0) start))
  10783. (setq list
  10784. (cons (substring string start (match-beginning 0))
  10785. list)))
  10786. (setq start (match-end 0)))
  10787. (or (eq start (length string))
  10788. (setq list
  10789. (cons (substring string start)
  10790. list)))
  10791. (nreverse list)))
  10792. (defun org-table-map-tables (function)
  10793. "Apply FUNCTION to the start of all tables in the buffer."
  10794. (save-excursion
  10795. (save-restriction
  10796. (widen)
  10797. (goto-char (point-min))
  10798. (while (re-search-forward org-table-any-line-regexp nil t)
  10799. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  10800. (beginning-of-line 1)
  10801. (if (looking-at org-table-line-regexp)
  10802. (save-excursion (funcall function)))
  10803. (re-search-forward org-table-any-border-regexp nil 1))))
  10804. (message "Mapping tables: done"))
  10805. (defun org-table-sum (&optional beg end nlast)
  10806. "Sum numbers in region of current table column.
  10807. The result will be displayed in the echo area, and will be available
  10808. as kill to be inserted with \\[yank].
  10809. If there is an active region, it is interpreted as a rectangle and all
  10810. numbers in that rectangle will be summed. If there is no active
  10811. region and point is located in a table column, sum all numbers in that
  10812. column.
  10813. If at least one number looks like a time HH:MM or HH:MM:SS, all other
  10814. numbers are assumed to be times as well (in decimal hours) and the
  10815. numbers are added as such.
  10816. If NLAST is a number, only the NLAST fields will actually be summed."
  10817. (interactive)
  10818. (save-excursion
  10819. (let (col (timecnt 0) diff h m s org-table-clip)
  10820. (cond
  10821. ((and beg end)) ; beg and end given explicitly
  10822. ((org-region-active-p)
  10823. (setq beg (region-beginning) end (region-end)))
  10824. (t
  10825. (setq col (org-table-current-column))
  10826. (goto-char (org-table-begin))
  10827. (unless (re-search-forward "^[ \t]*|[^-]" nil t)
  10828. (error "No table data"))
  10829. (org-table-goto-column col)
  10830. ;not needed? (skip-chars-backward "^|")
  10831. (setq beg (point))
  10832. (goto-char (org-table-end))
  10833. (unless (re-search-backward "^[ \t]*|[^-]" nil t)
  10834. (error "No table data"))
  10835. (org-table-goto-column col)
  10836. ;not needed? (skip-chars-forward "^|")
  10837. (setq end (point))))
  10838. (let* ((items (apply 'append (org-table-copy-region beg end)))
  10839. (items1 (cond ((not nlast) items)
  10840. ((>= nlast (length items)) items)
  10841. (t (setq items (reverse items))
  10842. (setcdr (nthcdr (1- nlast) items) nil)
  10843. (nreverse items))))
  10844. (numbers (delq nil (mapcar 'org-table-get-number-for-summing
  10845. items1)))
  10846. (res (apply '+ numbers))
  10847. (sres (if (= timecnt 0)
  10848. (format "%g" res)
  10849. (setq diff (* 3600 res)
  10850. h (floor (/ diff 3600)) diff (mod diff 3600)
  10851. m (floor (/ diff 60)) diff (mod diff 60)
  10852. s diff)
  10853. (format "%d:%02d:%02d" h m s))))
  10854. (kill-new sres)
  10855. (if (interactive-p)
  10856. (message "%s"
  10857. (substitute-command-keys
  10858. (format "Sum of %d items: %-20s (\\[yank] will insert result into buffer)"
  10859. (length numbers) sres))))
  10860. sres))))
  10861. (defun org-table-get-number-for-summing (s)
  10862. (let (n)
  10863. (if (string-match "^ *|? *" s)
  10864. (setq s (replace-match "" nil nil s)))
  10865. (if (string-match " *|? *$" s)
  10866. (setq s (replace-match "" nil nil s)))
  10867. (setq n (string-to-number s))
  10868. (cond
  10869. ((and (string-match "0" s)
  10870. (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
  10871. ((string-match "\\`[ \t]+\\'" s) nil)
  10872. ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
  10873. (let ((h (string-to-number (or (match-string 1 s) "0")))
  10874. (m (string-to-number (or (match-string 2 s) "0")))
  10875. (s (string-to-number (or (match-string 4 s) "0"))))
  10876. (if (boundp 'timecnt) (setq timecnt (1+ timecnt)))
  10877. (* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
  10878. ((equal n 0) nil)
  10879. (t n))))
  10880. (defun org-table-get-vertical-vector (desc &optional tbeg col)
  10881. "Get a calc vector from a column, accorting to descriptor DESC.
  10882. Optional arguments TBEG and COL can give the beginning of the table and
  10883. the current column, to avoid unnecessary parsing."
  10884. (save-excursion
  10885. (or tbeg (setq tbeg (org-table-begin)))
  10886. (or col (setq col (org-table-current-column)))
  10887. (let (beg end nn n n1 n2 l (thisline (org-current-line)) hline-list)
  10888. (cond
  10889. ((string-match "\\(I+\\)\\(-\\(I+\\)\\)?" desc)
  10890. (setq n1 (- (match-end 1) (match-beginning 1)))
  10891. (if (match-beginning 3)
  10892. (setq n2 (- (match-end 2) (match-beginning 3))))
  10893. (setq n (if n2 (max n1 n2) n1))
  10894. (setq n1 (if n2 (min n1 n2)))
  10895. (setq nn n)
  10896. (while (and (> nn 0)
  10897. (re-search-backward org-table-hline-regexp tbeg t))
  10898. (push (org-current-line) hline-list)
  10899. (setq nn (1- nn)))
  10900. (setq hline-list (nreverse hline-list))
  10901. (goto-line (nth (1- n) hline-list))
  10902. (when (re-search-forward org-table-dataline-regexp)
  10903. (org-table-goto-column col)
  10904. (setq beg (point)))
  10905. (goto-line (if n1 (nth (1- n1) hline-list) thisline))
  10906. (when (re-search-backward org-table-dataline-regexp)
  10907. (org-table-goto-column col)
  10908. (setq end (point)))
  10909. (setq l (apply 'append (org-table-copy-region beg end)))
  10910. (concat "[" (mapconcat (lambda (x) (setq x (org-trim x))
  10911. (if (equal x "") "0" x))
  10912. l ",") "]"))
  10913. ((string-match "\\([0-9]+\\)-\\([0-9]+\\)" desc)
  10914. (setq n1 (string-to-number (match-string 1 desc))
  10915. n2 (string-to-number (match-string 2 desc)))
  10916. (beginning-of-line 1)
  10917. (save-excursion
  10918. (when (re-search-backward org-table-dataline-regexp tbeg t n1)
  10919. (org-table-goto-column col)
  10920. (setq beg (point))))
  10921. (when (re-search-backward org-table-dataline-regexp tbeg t n2)
  10922. (org-table-goto-column col)
  10923. (setq end (point)))
  10924. (setq l (apply 'append (org-table-copy-region beg end)))
  10925. (concat "[" (mapconcat
  10926. (lambda (x) (setq x (org-trim x))
  10927. (if (equal x "") "0" x))
  10928. l ",") "]"))
  10929. ((string-match "\\([0-9]+\\)" desc)
  10930. (beginning-of-line 1)
  10931. (when (re-search-backward org-table-dataline-regexp tbeg t
  10932. (string-to-number (match-string 0 desc)))
  10933. (org-table-goto-column col)
  10934. (org-trim (org-table-get-field))))))))
  10935. (defvar org-table-formula-history nil)
  10936. (defvar org-table-column-names nil
  10937. "Alist with column names, derived from the `!' line.")
  10938. (defvar org-table-column-name-regexp nil
  10939. "Regular expression matching the current column names.")
  10940. (defvar org-table-local-parameters nil
  10941. "Alist with parameter names, derived from the `$' line.")
  10942. (defvar org-table-named-field-locations nil
  10943. "Alist with locations of named fields.")
  10944. (defun org-table-get-formula (&optional equation named)
  10945. "Read a formula from the minibuffer, offer stored formula as default."
  10946. (let* ((name (car (rassoc (list (org-current-line)
  10947. (org-table-current-column))
  10948. org-table-named-field-locations)))
  10949. (scol (if named
  10950. (if name name
  10951. (error "Not in a named field"))
  10952. (int-to-string (org-table-current-column))))
  10953. (dummy (and name (not named)
  10954. (not (y-or-n-p "Replace named-field formula with column equation? " ))
  10955. (error "Abort")))
  10956. (org-table-may-need-update nil)
  10957. (stored-list (org-table-get-stored-formulas))
  10958. (stored (cdr (assoc scol stored-list)))
  10959. (eq (cond
  10960. ((and stored equation (string-match "^ *=? *$" equation))
  10961. stored)
  10962. ((stringp equation)
  10963. equation)
  10964. (t (read-string
  10965. (format "%s formula $%s=" (if named "Field" "Column") scol)
  10966. (or stored "") 'org-table-formula-history
  10967. ;stored
  10968. ))))
  10969. mustsave)
  10970. (when (not (string-match "\\S-" eq))
  10971. ;; remove formula
  10972. (setq stored-list (delq (assoc scol stored-list) stored-list))
  10973. (org-table-store-formulas stored-list)
  10974. (error "Formula removed"))
  10975. (if (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
  10976. (if (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
  10977. (if (and name (not named))
  10978. ;; We set the column equation, delete the named one.
  10979. (setq stored-list (delq (assoc name stored-list) stored-list)
  10980. mustsave t))
  10981. (if stored
  10982. (setcdr (assoc scol stored-list) eq)
  10983. (setq stored-list (cons (cons scol eq) stored-list)))
  10984. (if (or mustsave (not (equal stored eq)))
  10985. (org-table-store-formulas stored-list))
  10986. eq))
  10987. (defun org-table-store-formulas (alist)
  10988. "Store the list of formulas below the current table."
  10989. (setq alist (sort alist (lambda (a b) (string< (car a) (car b)))))
  10990. (save-excursion
  10991. (goto-char (org-table-end))
  10992. (if (looking-at "\\([ \t]*\n\\)*#\\+TBLFM:.*\n?")
  10993. (delete-region (point) (match-end 0)))
  10994. (insert "#+TBLFM: "
  10995. (mapconcat (lambda (x)
  10996. (concat "$" (car x) "=" (cdr x)))
  10997. alist "::")
  10998. "\n")))
  10999. (defun org-table-get-stored-formulas ()
  11000. "Return an alist with the stored formulas directly after current table."
  11001. (interactive)
  11002. (let (scol eq eq-alist strings string seen)
  11003. (save-excursion
  11004. (goto-char (org-table-end))
  11005. (when (looking-at "\\([ \t]*\n\\)*#\\+TBLFM: *\\(.*\\)")
  11006. (setq strings (org-split-string (match-string 2) " *:: *"))
  11007. (while (setq string (pop strings))
  11008. (when (string-match "\\$\\([a-zA-Z0-9]+\\) *= *\\(.*[^ \t]\\)" string)
  11009. (setq scol (match-string 1 string)
  11010. eq (match-string 2 string)
  11011. eq-alist (cons (cons scol eq) eq-alist))
  11012. (if (member scol seen)
  11013. (error "Double definition `$%s=' in TBLFM line, please fix by hand" scol)
  11014. (push scol seen))))))
  11015. (nreverse eq-alist)))
  11016. (defun org-table-modify-formulas (action &rest columns)
  11017. "Modify the formulas stored below the current table.
  11018. ACTION can be `remove', `insert', `swap'. For `swap', two column numbers are
  11019. expected, for the other actions only a single column number is needed."
  11020. (let ((list (org-table-get-stored-formulas))
  11021. (nmax (length (org-split-string
  11022. (buffer-substring (point-at-bol) (point-at-eol))
  11023. "|")))
  11024. col col1 col2 scol si sc1 sc2)
  11025. (cond
  11026. ((null list)) ; No action needed if there are no stored formulas
  11027. ((eq action 'remove)
  11028. (setq col (car columns)
  11029. scol (int-to-string col))
  11030. (org-table-replace-in-formulas list scol "INVALID")
  11031. (if (assoc scol list) (setq list (delq (assoc scol list) list)))
  11032. (loop for i from (1+ col) upto nmax by 1 do
  11033. (setq si (int-to-string i))
  11034. (org-table-replace-in-formulas list si (int-to-string (1- i)))
  11035. (if (assoc si list) (setcar (assoc si list)
  11036. (int-to-string (1- i))))))
  11037. ((eq action 'insert)
  11038. (setq col (car columns))
  11039. (loop for i from nmax downto col by 1 do
  11040. (setq si (int-to-string i))
  11041. (org-table-replace-in-formulas list si (int-to-string (1+ i)))
  11042. (if (assoc si list) (setcar (assoc si list)
  11043. (int-to-string (1+ i))))))
  11044. ((eq action 'swap)
  11045. (setq col1 (car columns) col2 (nth 1 columns)
  11046. sc1 (int-to-string col1) sc2 (int-to-string col2))
  11047. ;; Hopefully, ZqZtZ will never be a name in a table
  11048. (org-table-replace-in-formulas list sc1 "ZqZtZ")
  11049. (org-table-replace-in-formulas list sc2 sc1)
  11050. (org-table-replace-in-formulas list "ZqZtZ" sc2)
  11051. (if (assoc sc1 list) (setcar (assoc sc1 list) "ZqZtZ"))
  11052. (if (assoc sc2 list) (setcar (assoc sc2 list) sc1))
  11053. (if (assoc "ZqZtZ" list) (setcar (assoc "ZqZtZ" list) sc2)))
  11054. (t (error "Invalid action in `org-table-modify-formulas'")))
  11055. (if list (org-table-store-formulas list))))
  11056. (defun org-table-replace-in-formulas (list s1 s2)
  11057. (let (elt re s)
  11058. (setq s1 (concat "$" (if (integerp s1) (int-to-string s1) s1))
  11059. s2 (concat "$" (if (integerp s2) (int-to-string s2) s2))
  11060. re (concat (regexp-quote s1) "\\>"))
  11061. (while (setq elt (pop list))
  11062. (setq s (cdr elt))
  11063. (while (string-match re s)
  11064. (setq s (replace-match s2 t t s)))
  11065. (setcdr elt s))))
  11066. (defun org-table-get-specials ()
  11067. "Get the column names and local parameters for this table."
  11068. (save-excursion
  11069. (let ((beg (org-table-begin)) (end (org-table-end))
  11070. names name fields fields1 field cnt c v line col)
  11071. (setq org-table-column-names nil
  11072. org-table-local-parameters nil
  11073. org-table-named-field-locations nil)
  11074. (goto-char beg)
  11075. (when (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t)
  11076. (setq names (org-split-string (match-string 1) " *| *")
  11077. cnt 1)
  11078. (while (setq name (pop names))
  11079. (setq cnt (1+ cnt))
  11080. (if (string-match "^[a-zA-Z][a-zA-Z0-9]*$" name)
  11081. (push (cons name (int-to-string cnt)) org-table-column-names))))
  11082. (setq org-table-column-names (nreverse org-table-column-names))
  11083. (setq org-table-column-name-regexp
  11084. (concat "\\$\\(" (mapconcat 'car org-table-column-names "\\|") "\\)\\>"))
  11085. (goto-char beg)
  11086. (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t)
  11087. (setq fields (org-split-string (match-string 1) " *| *"))
  11088. (while (setq field (pop fields))
  11089. (if (string-match "^\\([a-zA-Z][a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field)
  11090. (push (cons (match-string 1 field) (match-string 2 field))
  11091. org-table-local-parameters))))
  11092. (goto-char beg)
  11093. (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t)
  11094. (setq c (match-string 1)
  11095. fields (org-split-string (match-string 2) " *| *"))
  11096. (save-excursion
  11097. (beginning-of-line (if (equal c "_") 2 0))
  11098. (setq line (org-current-line) col 1)
  11099. (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
  11100. (setq fields1 (org-split-string (match-string 1) " *| *"))))
  11101. (while (and fields1 (setq field (pop fields)))
  11102. (setq v (pop fields1) col (1+ col))
  11103. (when (and (stringp field) (stringp v)
  11104. (string-match "^[a-zA-Z][a-zA-Z0-9]*$" field))
  11105. (push (cons field v) org-table-local-parameters)
  11106. (push (list field line col) org-table-named-field-locations)))))))
  11107. (defun org-this-word ()
  11108. ;; Get the current word
  11109. (save-excursion
  11110. (let ((beg (progn (skip-chars-backward "^ \t\n") (point)))
  11111. (end (progn (skip-chars-forward "^ \t\n") (point))))
  11112. (buffer-substring-no-properties beg end))))
  11113. (defun org-table-maybe-eval-formula ()
  11114. "Check if the current field starts with \"=\" or \":=\".
  11115. If yes, store the formula and apply it."
  11116. ;; We already know we are in a table. Get field will only return a formula
  11117. ;; when appropriate. It might return a separator line, but no problem.
  11118. (when org-table-formula-evaluate-inline
  11119. (let* ((field (org-trim (or (org-table-get-field) "")))
  11120. named eq)
  11121. (when (string-match "^:?=\\(.*\\)" field)
  11122. (setq named (equal (string-to-char field) ?:)
  11123. eq (match-string 1 field))
  11124. (if (fboundp 'calc-eval)
  11125. (org-table-eval-formula (if named '(4) nil) eq))))))
  11126. (defvar org-recalc-commands nil
  11127. "List of commands triggering the recalculation of a line.
  11128. Will be filled automatically during use.")
  11129. (defvar org-recalc-marks
  11130. '((" " . "Unmarked: no special line, no automatic recalculation")
  11131. ("#" . "Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
  11132. ("*" . "Recalculate only when entire table is recalculated with `C-u C-c *'")
  11133. ("!" . "Column name definition line. Reference in formula as $name.")
  11134. ("$" . "Parameter definition line name=value. Reference in formula as $name.")
  11135. ("_" . "Names for values in row below this one.")
  11136. ("^" . "Names for values in row above this one.")))
  11137. (defun org-table-rotate-recalc-marks (&optional newchar)
  11138. "Rotate the recalculation mark in the first column.
  11139. If in any row, the first field is not consistent with a mark,
  11140. insert a new column for the markers.
  11141. When there is an active region, change all the lines in the region,
  11142. after prompting for the marking character.
  11143. After each change, a message will be displayed indicating the meaning
  11144. of the new mark."
  11145. (interactive)
  11146. (unless (org-at-table-p) (error "Not at a table"))
  11147. (let* ((marks (append (mapcar 'car org-recalc-marks) '(" ")))
  11148. (beg (org-table-begin))
  11149. (end (org-table-end))
  11150. (l (org-current-line))
  11151. (l1 (if (org-region-active-p) (org-current-line (region-beginning))))
  11152. (l2 (if (org-region-active-p) (org-current-line (region-end))))
  11153. (have-col
  11154. (save-excursion
  11155. (goto-char beg)
  11156. (not (re-search-forward "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" end t))))
  11157. (col (org-table-current-column))
  11158. (forcenew (car (assoc newchar org-recalc-marks)))
  11159. epos new)
  11160. (when l1
  11161. (message "Change region to what mark? Type # * ! $ or SPC: ")
  11162. (setq newchar (char-to-string (read-char-exclusive))
  11163. forcenew (car (assoc newchar org-recalc-marks))))
  11164. (if (and newchar (not forcenew))
  11165. (error "Invalid NEWCHAR `%s' in `org-table-rotate-recalc-marks'"
  11166. newchar))
  11167. (if l1 (goto-line l1))
  11168. (save-excursion
  11169. (beginning-of-line 1)
  11170. (unless (looking-at org-table-dataline-regexp)
  11171. (error "Not at a table data line")))
  11172. (unless have-col
  11173. (org-table-goto-column 1)
  11174. (org-table-insert-column)
  11175. (org-table-goto-column (1+ col)))
  11176. (setq epos (point-at-eol))
  11177. (save-excursion
  11178. (beginning-of-line 1)
  11179. (org-table-get-field
  11180. 1 (if (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")
  11181. (concat " "
  11182. (setq new (or forcenew
  11183. (cadr (member (match-string 1) marks))))
  11184. " ")
  11185. " # ")))
  11186. (if (and l1 l2)
  11187. (progn
  11188. (goto-line l1)
  11189. (while (progn (beginning-of-line 2) (not (= (org-current-line) l2)))
  11190. (and (looking-at org-table-dataline-regexp)
  11191. (org-table-get-field 1 (concat " " new " "))))
  11192. (goto-line l1)))
  11193. (if (not (= epos (point-at-eol))) (org-table-align))
  11194. (goto-line l)
  11195. (and (interactive-p) (message (cdr (assoc new org-recalc-marks))))))
  11196. (defun org-table-maybe-recalculate-line ()
  11197. "Recompute the current line if marked for it, and if we haven't just done it."
  11198. (interactive)
  11199. (and org-table-allow-automatic-line-recalculation
  11200. (not (and (memq last-command org-recalc-commands)
  11201. (equal org-last-recalc-line (org-current-line))))
  11202. (save-excursion (beginning-of-line 1)
  11203. (looking-at org-table-auto-recalculate-regexp))
  11204. (fboundp 'calc-eval)
  11205. (org-table-recalculate) t))
  11206. (defvar org-table-formula-debug nil
  11207. "Non-nil means, debug table formulas.
  11208. When nil, simply write \"#ERROR\" in corrupted fields.")
  11209. (defvar modes)
  11210. (defsubst org-set-calc-mode (var &optional value)
  11211. (if (stringp var)
  11212. (setq var (assoc var '(("D" calc-angle-mode deg)
  11213. ("R" calc-angle-mode rad)
  11214. ("F" calc-prefer-frac t)
  11215. ("S" calc-symbolic-mode t)))
  11216. value (nth 2 var) var (nth 1 var)))
  11217. (if (memq var modes)
  11218. (setcar (cdr (memq var modes)) value)
  11219. (cons var (cons value modes)))
  11220. modes)
  11221. (defun org-table-eval-formula (&optional arg equation
  11222. suppress-align suppress-const
  11223. suppress-store)
  11224. "Replace the table field value at the cursor by the result of a calculation.
  11225. This function makes use of Dave Gillespie's Calc package, in my view the
  11226. most exciting program ever written for GNU Emacs. So you need to have Calc
  11227. installed in order to use this function.
  11228. In a table, this command replaces the value in the current field with the
  11229. result of a formula. It also installs the formula as the \"current\" column
  11230. formula, by storing it in a special line below the table. When called
  11231. with a `C-u' prefix, the current field must ba a named field, and the
  11232. formula is installed as valid in only this specific field.
  11233. When called, the command first prompts for a formula, which is read in
  11234. the minibuffer. Previously entered formulas are available through the
  11235. history list, and the last used formula is offered as a default.
  11236. These stored formulas are adapted correctly when moving, inserting, or
  11237. deleting columns with the corresponding commands.
  11238. The formula can be any algebraic expression understood by the Calc package.
  11239. For details, see the Org-mode manual.
  11240. This function can also be called from Lisp programs and offers
  11241. additional arguments: EQUATION can be the formula to apply. If this
  11242. argument is given, the user will not be prompted. SUPPRESS-ALIGN is
  11243. used to speed-up recursive calls by by-passing unnecessary aligns.
  11244. SUPPRESS-CONST suppresses the interpretation of constants in the
  11245. formula, assuming that this has been done already outside the function.
  11246. SUPPRESS-STORE means the formula should not be stored, either because
  11247. it is already stored, or because it is a modified equation that should
  11248. not overwrite the stored one."
  11249. (interactive "P")
  11250. (require 'calc)
  11251. (org-table-check-inside-data-field)
  11252. (org-table-get-specials)
  11253. (let* (fields
  11254. (ndown (if (integerp arg) arg 1))
  11255. (org-table-automatic-realign nil)
  11256. (case-fold-search nil)
  11257. (down (> ndown 1))
  11258. (formula (if (and equation suppress-store)
  11259. equation
  11260. (org-table-get-formula equation (equal arg '(4)))))
  11261. (n0 (org-table-current-column))
  11262. (modes (copy-sequence org-calc-default-modes))
  11263. n form fmt x ev orig c lispp)
  11264. ;; Parse the format string. Since we have a lot of modes, this is
  11265. ;; a lot of work. However, I think calc still uses most of the time.
  11266. (if (string-match ";" formula)
  11267. (let ((tmp (org-split-string formula ";")))
  11268. (setq formula (car tmp)
  11269. fmt (concat (cdr (assoc "%" org-table-local-parameters))
  11270. (nth 1 tmp)))
  11271. (while (string-match "\\([pnfse]\\)\\(-?[0-9]+\\)" fmt)
  11272. (setq c (string-to-char (match-string 1 fmt))
  11273. n (string-to-number (match-string 2 fmt)))
  11274. (if (= c ?p)
  11275. (setq modes (org-set-calc-mode 'calc-internal-prec n))
  11276. (setq modes (org-set-calc-mode
  11277. 'calc-float-format
  11278. (list (cdr (assoc c '((?n . float) (?f . fix)
  11279. (?s . sci) (?e . eng))))
  11280. n))))
  11281. (setq fmt (replace-match "" t t fmt)))
  11282. (while (string-match "[DRFS]" fmt)
  11283. (setq modes (org-set-calc-mode (match-string 0 fmt)))
  11284. (setq fmt (replace-match "" t t fmt)))
  11285. (unless (string-match "\\S-" fmt)
  11286. (setq fmt nil))))
  11287. (if (and (not suppress-const) org-table-formula-use-constants)
  11288. (setq formula (org-table-formula-substitute-names formula)))
  11289. (setq orig (or (get-text-property 1 :orig-formula formula) "?"))
  11290. (while (> ndown 0)
  11291. (setq fields (org-split-string
  11292. (buffer-substring
  11293. (point-at-bol) (point-at-eol)) " *| *"))
  11294. (if org-table-formula-numbers-only
  11295. (setq fields (mapcar
  11296. (lambda (x) (number-to-string (string-to-number x)))
  11297. fields)))
  11298. (setq ndown (1- ndown))
  11299. (setq form (copy-sequence formula)
  11300. lispp (equal (substring form 0 2) "'("))
  11301. ;; Insert the references to fields in same row
  11302. (while (string-match "\\$\\([0-9]+\\)?" form)
  11303. (setq n (if (match-beginning 1)
  11304. (string-to-number (match-string 1 form))
  11305. n0)
  11306. x (nth (1- n) fields))
  11307. (unless x (error "Invalid field specifier \"%s\""
  11308. (match-string 0 form)))
  11309. (if (equal x "") (setq x "0"))
  11310. (setq form (replace-match
  11311. (if lispp x (concat "(" x ")"))
  11312. t t form)))
  11313. ;; Insert ranges in current column
  11314. (while (string-match "\\&[-I0-9]+" form)
  11315. (setq form (replace-match
  11316. (save-match-data
  11317. (org-table-get-vertical-vector (match-string 0 form)
  11318. nil n0))
  11319. t t form)))
  11320. (if lispp
  11321. (setq ev (eval (eval (read form)))
  11322. ev (if (numberp ev) (number-to-string ev) ev))
  11323. (setq ev (calc-eval (cons form modes)
  11324. (if org-table-formula-numbers-only 'num))))
  11325. (when org-table-formula-debug
  11326. (with-output-to-temp-buffer "*Help*"
  11327. (princ (format "Substitution history of formula
  11328. Orig: %s
  11329. $xyz-> %s
  11330. $1-> %s\n" orig formula form))
  11331. (if (listp ev)
  11332. (princ (format " %s^\nError: %s"
  11333. (make-string (car ev) ?\-) (nth 1 ev)))
  11334. (princ (format "Result: %s\nFormat: %s\nFinal: %s"
  11335. ev (or fmt "NONE")
  11336. (if fmt (format fmt (string-to-number ev)) ev)))))
  11337. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  11338. (unless (and (interactive-p) (not ndown))
  11339. (unless (let (inhibit-redisplay)
  11340. (y-or-n-p "Debugging Formula. Continue to next? "))
  11341. (org-table-align)
  11342. (error "Abort"))
  11343. (delete-window (get-buffer-window "*Help*"))
  11344. (message "")))
  11345. (if (listp ev) (setq fmt nil ev "#ERROR"))
  11346. (org-table-justify-field-maybe
  11347. (if fmt (format fmt (string-to-number ev)) ev))
  11348. (if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
  11349. (call-interactively 'org-return)
  11350. (setq ndown 0)))
  11351. (and down (org-table-maybe-recalculate-line))
  11352. (or suppress-align (and org-table-may-need-update
  11353. (org-table-align)))))
  11354. (defun org-table-recalculate (&optional all noalign)
  11355. "Recalculate the current table line by applying all stored formulas.
  11356. With prefix arg ALL, do this for all lines in the table."
  11357. (interactive "P")
  11358. (or (memq this-command org-recalc-commands)
  11359. (setq org-recalc-commands (cons this-command org-recalc-commands)))
  11360. (unless (org-at-table-p) (error "Not at a table"))
  11361. (org-table-get-specials)
  11362. (let* ((eqlist (sort (org-table-get-stored-formulas)
  11363. (lambda (a b) (string< (car a) (car b)))))
  11364. (inhibit-redisplay t)
  11365. (line-re org-table-dataline-regexp)
  11366. (thisline (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
  11367. (thiscol (org-table-current-column))
  11368. beg end entry eqlnum eqlname eql (cnt 0) eq a name)
  11369. ;; Insert constants in all formulas
  11370. (setq eqlist
  11371. (mapcar (lambda (x)
  11372. (setcdr x (org-table-formula-substitute-names (cdr x)))
  11373. x)
  11374. eqlist))
  11375. ;; Split the equation list
  11376. (while (setq eq (pop eqlist))
  11377. (if (<= (string-to-char (car eq)) ?9)
  11378. (push eq eqlnum)
  11379. (push eq eqlname)))
  11380. (setq eqlnum (nreverse eqlnum) eqlname (nreverse eqlname))
  11381. (if all
  11382. (progn
  11383. (setq end (move-marker (make-marker) (1+ (org-table-end))))
  11384. (goto-char (setq beg (org-table-begin)))
  11385. (if (re-search-forward org-table-calculate-mark-regexp end t)
  11386. ;; This is a table with marked lines, only compute selected lines
  11387. (setq line-re org-table-recalculate-regexp)
  11388. ;; Move forward to the first non-header line
  11389. (if (and (re-search-forward org-table-dataline-regexp end t)
  11390. (re-search-forward org-table-hline-regexp end t)
  11391. (re-search-forward org-table-dataline-regexp end t))
  11392. (setq beg (match-beginning 0))
  11393. nil))) ;; just leave beg where it is
  11394. (setq beg (point-at-bol)
  11395. end (move-marker (make-marker) (1+ (point-at-eol)))))
  11396. (goto-char beg)
  11397. (and all (message "Re-applying formulas to full table..."))
  11398. (while (re-search-forward line-re end t)
  11399. (unless (string-match "^ *[_^!$] *$" (org-table-get-field 1))
  11400. ;; Unprotected line, recalculate
  11401. (and all (message "Re-applying formulas to full table...(line %d)"
  11402. (setq cnt (1+ cnt))))
  11403. (setq org-last-recalc-line (org-current-line))
  11404. (setq eql eqlnum)
  11405. (while (setq entry (pop eql))
  11406. (goto-line org-last-recalc-line)
  11407. (org-table-goto-column (string-to-number (car entry)) nil 'force)
  11408. (org-table-eval-formula nil (cdr entry) 'noalign 'nocst 'nostore))))
  11409. (goto-line thisline)
  11410. (org-table-goto-column thiscol)
  11411. (or noalign (and org-table-may-need-update (org-table-align))
  11412. (and all (message "Re-applying formulas to %d lines...done" cnt)))
  11413. ;; Now do the names fields
  11414. (while (setq eq (pop eqlname))
  11415. (setq name (car eq)
  11416. a (assoc name org-table-named-field-locations))
  11417. (when a
  11418. (message "Re-applying formula to named field: %s" name)
  11419. (goto-line (nth 1 a))
  11420. (org-table-goto-column (nth 2 a))
  11421. (org-table-eval-formula nil (cdr eq) 'noalign 'nocst 'nostore)))
  11422. ;; back to initial position
  11423. (goto-line thisline)
  11424. (org-table-goto-column thiscol)
  11425. (or noalign (and org-table-may-need-update (org-table-align))
  11426. (and all (message "Re-applying formulas...done")))))
  11427. (defun org-table-formula-substitute-names (f)
  11428. "Replace $const with values in string F."
  11429. (let ((start 0) a n1 n2 nn1 nn2 s (f1 f))
  11430. ;; First, check for column names
  11431. (while (setq start (string-match org-table-column-name-regexp f start))
  11432. (setq start (1+ start))
  11433. (setq a (assoc (match-string 1 f) org-table-column-names))
  11434. (setq f (replace-match (concat "$" (cdr a)) t t f)))
  11435. ;; Expand ranges to vectors
  11436. (while (string-match "\\$\\([0-9]+\\)\\.\\.\\.?\\$\\([0-9]+\\)" f)
  11437. (setq n1 (string-to-number (match-string 1 f))
  11438. n2 (string-to-number (match-string 2 f))
  11439. nn1 (1+ (min n1 n2)) nn2 (max n1 n2)
  11440. s (concat "[($" (number-to-string (1- nn1)) ")"))
  11441. (loop for i from nn1 upto nn2 do
  11442. (setq s (concat s ",($" (int-to-string i) ")")))
  11443. (setq s (concat s "]"))
  11444. (if (< n2 n1) (setq s (concat "rev(" s ")")))
  11445. (setq f (replace-match s t t f)))
  11446. ;; Parameters and constants
  11447. (setq start 0)
  11448. (while (setq start (string-match "\\$\\([a-zA-Z][a-zA-Z0-9]*\\)" f start))
  11449. (setq start (1+ start))
  11450. (if (setq a (save-match-data
  11451. (org-table-get-constant (match-string 1 f))))
  11452. (setq f (replace-match (concat "(" a ")") t t f))))
  11453. (if org-table-formula-debug
  11454. (put-text-property 0 (length f) :orig-formula f1 f))
  11455. f))
  11456. (defun org-table-get-constant (const)
  11457. "Find the value for a parameter or constant in a formula.
  11458. Parameters get priority."
  11459. (or (cdr (assoc const org-table-local-parameters))
  11460. (cdr (assoc const org-table-formula-constants))
  11461. (and (fboundp 'constants-get) (constants-get const))
  11462. "#UNDEFINED_NAME"))
  11463. (defvar org-edit-formulas-map (make-sparse-keymap))
  11464. (define-key org-edit-formulas-map "\C-c\C-c" 'org-finish-edit-formulas)
  11465. (define-key org-edit-formulas-map "\C-c\C-q" 'org-abort-edit-formulas)
  11466. (define-key org-edit-formulas-map "\C-c?" 'org-show-variable)
  11467. (defvar org-pos)
  11468. (defun org-table-edit-formulas ()
  11469. "Edit the formulas of the current table in a separate buffer."
  11470. (interactive)
  11471. (unless (org-at-table-p)
  11472. (error "Not at a table"))
  11473. (org-table-get-specials)
  11474. (let ((eql (org-table-get-stored-formulas))
  11475. (pos (move-marker (make-marker) (point)))
  11476. (wc (current-window-configuration))
  11477. entry loc s)
  11478. (switch-to-buffer-other-window "*Edit Formulas*")
  11479. (erase-buffer)
  11480. (fundamental-mode)
  11481. (org-set-local 'org-pos pos)
  11482. (org-set-local 'org-window-configuration wc)
  11483. (use-local-map org-edit-formulas-map)
  11484. (setq s "# Edit formulas and finish with `C-c C-c'.
  11485. # Use `C-u C-c C-c' to also appy them immediately to the entire table.
  11486. # Use `C-c ?' to get information about $name at point.
  11487. # To cancel editing, press `C-c C-q'.\n")
  11488. (put-text-property 0 (length s) 'face 'font-lock-comment-face s)
  11489. (insert s)
  11490. (while (setq entry (pop eql))
  11491. (when (setq loc (assoc (car entry) org-table-named-field-locations))
  11492. (setq s (format "# Named formula, referring to column %d in line %d\n"
  11493. (nth 2 loc) (nth 1 loc)))
  11494. (put-text-property 0 (length s) 'face 'font-lock-comment-face s)
  11495. (insert s))
  11496. (setq s (concat "$" (car entry) "=" (cdr entry) "\n"))
  11497. (remove-text-properties 0 (length s) '(face nil) s)
  11498. (insert s))
  11499. (goto-char (point-min))
  11500. (message "Edit formulas and finish with `C-c C-c'.")))
  11501. (defun org-show-variable ()
  11502. "Show the location/value of the $ expression at point."
  11503. (interactive)
  11504. (let (var (pos org-pos) (win (selected-window)) e)
  11505. (save-excursion
  11506. (or (looking-at "\\$") (skip-chars-backward "$a-zA-Z0-9"))
  11507. (if (looking-at "\\$\\([a-zA-Z0-9]+\\)")
  11508. (setq var (match-string 1))
  11509. (error "No variable at point")))
  11510. (cond
  11511. ((setq e (assoc var org-table-named-field-locations))
  11512. (switch-to-buffer-other-window (marker-buffer pos))
  11513. (goto-line (nth 1 e))
  11514. (org-table-goto-column (nth 2 e))
  11515. (select-window win)
  11516. (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
  11517. ((setq e (assoc var org-table-column-names))
  11518. (switch-to-buffer-other-window (marker-buffer pos))
  11519. (goto-char pos)
  11520. (goto-char (org-table-begin))
  11521. (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
  11522. (org-table-end) t)
  11523. (progn
  11524. (goto-char (match-beginning 1))
  11525. (message "Named column (column %s)" (cdr e)))
  11526. (error "Column name not found"))
  11527. (select-window win))
  11528. ((string-match "^[0-9]$" var)
  11529. ;; column number
  11530. (switch-to-buffer-other-window (marker-buffer pos))
  11531. (goto-char pos)
  11532. (goto-char (org-table-begin))
  11533. (recenter 1)
  11534. (if (re-search-forward org-table-dataline-regexp
  11535. (org-table-end) t)
  11536. (progn
  11537. (goto-char (match-beginning 0))
  11538. (org-table-goto-column (string-to-number var))
  11539. (message "Column %s" var))
  11540. (error "Column name not found"))
  11541. (select-window win))
  11542. ((setq e (assoc var org-table-local-parameters))
  11543. (switch-to-buffer-other-window (marker-buffer pos))
  11544. (goto-char pos)
  11545. (goto-char (org-table-begin))
  11546. (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
  11547. (progn
  11548. (goto-char (match-beginning 1))
  11549. (message "Local parameter."))
  11550. (error "Parameter not found"))
  11551. (select-window win))
  11552. (t
  11553. (cond
  11554. ((setq e (assoc var org-table-formula-constants))
  11555. (message "Constant: $%s=%s in `org-table-formula-constants'." var (cdr e)))
  11556. ((setq e (and (fboundp 'constants-get) (constants-get var)))
  11557. (message "Constant: $%s=%s, retrieved from `constants.el'." var e))
  11558. (t (error "Undefined name $%s" var)))))))
  11559. (defun org-finish-edit-formulas (&optional arg)
  11560. "Parse the buffer for formula definitions and install them.
  11561. With prefix ARG, apply the new formulas to the table."
  11562. (interactive "P")
  11563. (let ((pos org-pos) eql)
  11564. (goto-char (point-min))
  11565. (while (re-search-forward "^\\$\\([a-zA-Z0-9]+\\) *= *\\(.*?\\) *$" nil t)
  11566. (push (cons (match-string 1) (match-string 2)) eql))
  11567. (set-window-configuration org-window-configuration)
  11568. (select-window (get-buffer-window (marker-buffer pos)))
  11569. (goto-char pos)
  11570. (unless (org-at-table-p)
  11571. (error "Lost table position - cannot install formulae"))
  11572. (org-table-store-formulas eql)
  11573. (move-marker pos nil)
  11574. (kill-buffer "*Edit Formulas*")
  11575. (if arg
  11576. (org-table-recalculate 'all)
  11577. (message "New formulas installed - press C-u C-c C-c to apply."))))
  11578. (defun org-abort-edit-formulas ()
  11579. "Abort editing formulas, without installing the changes."
  11580. (interactive)
  11581. (let ((pos org-pos))
  11582. (set-window-configuration org-window-configuration)
  11583. (select-window (get-buffer-window (marker-buffer pos)))
  11584. (goto-char pos)
  11585. (message "Formula editing aborted without installing changes")))
  11586. ;;; The orgtbl minor mode
  11587. ;; Define a minor mode which can be used in other modes in order to
  11588. ;; integrate the org-mode table editor.
  11589. ;; This is really a hack, because the org-mode table editor uses several
  11590. ;; keys which normally belong to the major mode, for example the TAB and
  11591. ;; RET keys. Here is how it works: The minor mode defines all the keys
  11592. ;; necessary to operate the table editor, but wraps the commands into a
  11593. ;; function which tests if the cursor is currently inside a table. If that
  11594. ;; is the case, the table editor command is executed. However, when any of
  11595. ;; those keys is used outside a table, the function uses `key-binding' to
  11596. ;; look up if the key has an associated command in another currently active
  11597. ;; keymap (minor modes, major mode, global), and executes that command.
  11598. ;; There might be problems if any of the keys used by the table editor is
  11599. ;; otherwise used as a prefix key.
  11600. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  11601. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  11602. ;; addresses this by checking explicitly for both bindings.
  11603. ;; The optimized version (see variable `orgtbl-optimized') takes over
  11604. ;; all keys which are bound to `self-insert-command' in the *global map*.
  11605. ;; Some modes bind other commands to simple characters, for example
  11606. ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
  11607. ;; active, this binding is ignored inside tables and replaced with a
  11608. ;; modified self-insert.
  11609. (defvar orgtbl-mode nil
  11610. "Variable controlling `orgtbl-mode', a minor mode enabling the `org-mode'
  11611. table editor in arbitrary modes.")
  11612. (make-variable-buffer-local 'orgtbl-mode)
  11613. (defvar orgtbl-mode-map (make-keymap)
  11614. "Keymap for `orgtbl-mode'.")
  11615. ;;;###autoload
  11616. (defun turn-on-orgtbl ()
  11617. "Unconditionally turn on `orgtbl-mode'."
  11618. (orgtbl-mode 1))
  11619. ;;;###autoload
  11620. (defun orgtbl-mode (&optional arg)
  11621. "The `org-mode' table editor as a minor mode for use in other modes."
  11622. (interactive)
  11623. (if (org-mode-p)
  11624. ;; Exit without error, in case some hook functions calls this
  11625. ;; by accident in org-mode.
  11626. (message "Orgtbl-mode is not useful in org-mode, command ignored")
  11627. (setq orgtbl-mode
  11628. (if arg (> (prefix-numeric-value arg) 0) (not orgtbl-mode)))
  11629. (if orgtbl-mode
  11630. (progn
  11631. (and (orgtbl-setup) (defun orgtbl-setup () nil))
  11632. ;; Make sure we are first in minor-mode-map-alist
  11633. (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
  11634. (and c (setq minor-mode-map-alist
  11635. (cons c (delq c minor-mode-map-alist)))))
  11636. (org-set-local (quote org-table-may-need-update) t)
  11637. (org-add-hook 'before-change-functions 'org-before-change-function
  11638. nil 'local)
  11639. (org-set-local 'org-old-auto-fill-inhibit-regexp
  11640. auto-fill-inhibit-regexp)
  11641. (org-set-local 'auto-fill-inhibit-regexp
  11642. (if auto-fill-inhibit-regexp
  11643. (concat "\\([ \t]*|\\|" auto-fill-inhibit-regexp)
  11644. "[ \t]*|"))
  11645. (org-add-to-invisibility-spec '(org-cwidth))
  11646. (easy-menu-add orgtbl-mode-menu)
  11647. (run-hooks 'orgtbl-mode-hook))
  11648. (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
  11649. (org-cleanup-narrow-column-properties)
  11650. (org-remove-from-invisibility-spec '(org-cwidth))
  11651. (remove-hook 'before-change-functions 'org-before-change-function t)
  11652. (easy-menu-remove orgtbl-mode-menu)
  11653. (force-mode-line-update 'all))))
  11654. (defun org-cleanup-narrow-column-properties ()
  11655. "Remove all properties related to narrow-column invisibility."
  11656. (let ((s 1))
  11657. (while (setq s (text-property-any s (point-max)
  11658. 'display org-narrow-column-arrow))
  11659. (remove-text-properties s (1+ s) '(display t)))
  11660. (setq s 1)
  11661. (while (setq s (text-property-any s (point-max) 'org-cwidth 1))
  11662. (remove-text-properties s (1+ s) '(org-cwidth t)))
  11663. (setq s 1)
  11664. (while (setq s (text-property-any s (point-max) 'invisible 'org-cwidth))
  11665. (remove-text-properties s (1+ s) '(invisible t)))))
  11666. ;; Install it as a minor mode.
  11667. (put 'orgtbl-mode :included t)
  11668. (put 'orgtbl-mode :menu-tag "Org Table Mode")
  11669. (add-minor-mode 'orgtbl-mode " OrgTbl" orgtbl-mode-map)
  11670. (defun orgtbl-make-binding (fun n &rest keys)
  11671. "Create a function for binding in the table minor mode.
  11672. FUN is the command to call inside a table. N is used to create a unique
  11673. command name. KEYS are keys that should be checked in for a command
  11674. to execute outside of tables."
  11675. (eval
  11676. (list 'defun
  11677. (intern (concat "orgtbl-hijacker-command-" (int-to-string n)))
  11678. '(arg)
  11679. (concat "In tables, run `" (symbol-name fun) "'.\n"
  11680. "Outside of tables, run the binding of `"
  11681. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  11682. "'.")
  11683. '(interactive "p")
  11684. (list 'if
  11685. '(org-at-table-p)
  11686. (list 'call-interactively (list 'quote fun))
  11687. (list 'let '(orgtbl-mode)
  11688. (list 'call-interactively
  11689. (append '(or)
  11690. (mapcar (lambda (k)
  11691. (list 'key-binding k))
  11692. keys)
  11693. '('orgtbl-error))))))))
  11694. (defun orgtbl-error ()
  11695. "Error when there is no default binding for a table key."
  11696. (interactive)
  11697. (error "This key is has no function outside tables"))
  11698. (defun orgtbl-setup ()
  11699. "Setup orgtbl keymaps."
  11700. (let ((nfunc 0)
  11701. (bindings
  11702. (list
  11703. '([(meta shift left)] org-table-delete-column)
  11704. '([(meta left)] org-table-move-column-left)
  11705. '([(meta right)] org-table-move-column-right)
  11706. '([(meta shift right)] org-table-insert-column)
  11707. '([(meta shift up)] org-table-kill-row)
  11708. '([(meta shift down)] org-table-insert-row)
  11709. '([(meta up)] org-table-move-row-up)
  11710. '([(meta down)] org-table-move-row-down)
  11711. '("\C-c\C-w" org-table-cut-region)
  11712. '("\C-c\M-w" org-table-copy-region)
  11713. '("\C-c\C-y" org-table-paste-rectangle)
  11714. '("\C-c-" org-table-insert-hline)
  11715. ; '([(shift tab)] org-table-previous-field)
  11716. '("\C-m" org-table-next-row)
  11717. (list (org-key 'S-return) 'org-table-copy-down)
  11718. '([(meta return)] org-table-wrap-region)
  11719. '("\C-c\C-q" org-table-wrap-region)
  11720. '("\C-c?" org-table-current-column)
  11721. '("\C-c " org-table-blank-field)
  11722. '("\C-c+" org-table-sum)
  11723. '("\C-c=" org-table-eval-formula)
  11724. '("\C-c'" org-table-edit-formulas)
  11725. '("\C-c`" org-table-edit-field)
  11726. '("\C-c*" org-table-recalculate)
  11727. '("\C-c|" org-table-create-or-convert-from-region)
  11728. '("\C-c^" org-table-sort-lines)
  11729. '([(control ?#)] org-table-rotate-recalc-marks)))
  11730. elt key fun cmd)
  11731. (while (setq elt (pop bindings))
  11732. (setq nfunc (1+ nfunc))
  11733. (setq key (car elt)
  11734. fun (nth 1 elt)
  11735. cmd (orgtbl-make-binding fun nfunc key))
  11736. (define-key orgtbl-mode-map key cmd))
  11737. ;; Special treatment needed for TAB and RET
  11738. (define-key orgtbl-mode-map [(return)]
  11739. (orgtbl-make-binding 'orgtbl-ret 100 [(return)] "\C-m"))
  11740. (define-key orgtbl-mode-map "\C-m"
  11741. (orgtbl-make-binding 'orgtbl-ret 101 "\C-m" [(return)]))
  11742. (define-key orgtbl-mode-map [(tab)]
  11743. (orgtbl-make-binding 'orgtbl-tab 102 [(tab)] "\C-i"))
  11744. (define-key orgtbl-mode-map "\C-i"
  11745. (orgtbl-make-binding 'orgtbl-tab 103 "\C-i" [(tab)])))
  11746. (define-key orgtbl-mode-map "\C-i"
  11747. (orgtbl-make-binding 'orgtbl-tab 104 [(shift tab)]))
  11748. (define-key orgtbl-mode-map "\C-c\C-c"
  11749. (orgtbl-make-binding 'org-ctrl-c-ctrl-c 105 "\C-c\C-c"))
  11750. (when orgtbl-optimized
  11751. ;; If the user wants maximum table support, we need to hijack
  11752. ;; some standard editing functions
  11753. (org-remap orgtbl-mode-map
  11754. 'self-insert-command 'orgtbl-self-insert-command
  11755. 'delete-char 'org-delete-char
  11756. 'delete-backward-char 'org-delete-backward-char)
  11757. (define-key orgtbl-mode-map "|" 'org-force-self-insert))
  11758. (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
  11759. '("OrgTbl"
  11760. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
  11761. ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
  11762. ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
  11763. ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
  11764. "--"
  11765. ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
  11766. ["Edit Field" org-table-edit-field :active (org-at-table-p) :keys "C-c ` "]
  11767. ["Copy Field from Above"
  11768. org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
  11769. "--"
  11770. ("Column"
  11771. ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
  11772. ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
  11773. ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
  11774. ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"]
  11775. "--"
  11776. ["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])
  11777. ("Row"
  11778. ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
  11779. ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
  11780. ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
  11781. ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
  11782. ["Sort lines in region" org-table-sort-lines (org-at-table-p) :keys "C-c ^"]
  11783. "--"
  11784. ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
  11785. ("Rectangle"
  11786. ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
  11787. ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
  11788. ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
  11789. ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
  11790. "--"
  11791. ["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
  11792. ["Set Named Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  11793. ["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
  11794. ["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
  11795. ["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
  11796. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
  11797. ["Sum Column/Rectangle" org-table-sum
  11798. :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
  11799. ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
  11800. ["Debug Formulas"
  11801. (setq org-table-formula-debug (not org-table-formula-debug))
  11802. :style toggle :selected org-table-formula-debug]
  11803. ))
  11804. t)
  11805. (defun orgtbl-tab (arg)
  11806. "Justification and field motion for `orgtbl-mode'."
  11807. (interactive "P")
  11808. (if arg (org-table-edit-field t)
  11809. (org-table-justify-field-maybe)
  11810. (org-table-next-field)))
  11811. (defun orgtbl-ret ()
  11812. "Justification and field motion for `orgtbl-mode'."
  11813. (interactive)
  11814. (org-table-justify-field-maybe)
  11815. (org-table-next-row))
  11816. (defun orgtbl-self-insert-command (N)
  11817. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  11818. If the cursor is in a table looking at whitespace, the whitespace is
  11819. overwritten, and the table is not marked as requiring realignment."
  11820. (interactive "p")
  11821. (if (and (org-at-table-p)
  11822. (or
  11823. (and org-table-auto-blank-field
  11824. (member last-command
  11825. '(orgtbl-hijacker-command-100
  11826. orgtbl-hijacker-command-101
  11827. orgtbl-hijacker-command-102
  11828. orgtbl-hijacker-command-103
  11829. orgtbl-hijacker-command-104
  11830. orgtbl-hijacker-command-105))
  11831. (org-table-blank-field))
  11832. t)
  11833. (eq N 1)
  11834. (looking-at "[^|\n]* +|"))
  11835. (let (org-table-may-need-update)
  11836. (goto-char (1- (match-end 0)))
  11837. (delete-backward-char 1)
  11838. (goto-char (match-beginning 0))
  11839. (self-insert-command N))
  11840. (setq org-table-may-need-update t)
  11841. (let (orgtbl-mode)
  11842. (call-interactively (key-binding (vector last-input-event))))))
  11843. (defun org-force-self-insert (N)
  11844. "Needed to enforce self-insert under remapping."
  11845. (interactive "p")
  11846. (self-insert-command N))
  11847. ;;; Exporting
  11848. (defconst org-level-max 20)
  11849. (defvar org-export-html-preamble nil
  11850. "Preamble, to be inserted just after <body>. Set by publishing functions.")
  11851. (defvar org-export-html-postamble nil
  11852. "Preamble, to be inserted just before </body>. Set by publishing functions.")
  11853. (defvar org-export-html-auto-preamble t
  11854. "Should default preamble be inserted? Set by publishing functions.")
  11855. (defvar org-export-html-auto-postamble t
  11856. "Should default postamble be inserted? Set by publishing functions.")
  11857. (defconst org-export-plist-vars
  11858. '((:language . org-export-default-language)
  11859. (:headline-levels . org-export-headline-levels)
  11860. (:section-numbers . org-export-with-section-numbers)
  11861. (:table-of-contents . org-export-with-toc)
  11862. (:archived-trees . org-export-with-archived-trees)
  11863. (:emphasize . org-export-with-emphasize)
  11864. (:sub-superscript . org-export-with-sub-superscripts)
  11865. (:TeX-macros . org-export-with-TeX-macros)
  11866. (:LaTeX-fragments . org-export-with-LaTeX-fragments)
  11867. (:fixed-width . org-export-with-fixed-width)
  11868. (:timestamps . org-export-with-timestamps)
  11869. (:tables . org-export-with-tables)
  11870. (:table-auto-headline . org-export-highlight-first-table-line)
  11871. (:style . org-export-html-style)
  11872. (:convert-org-links . org-export-html-link-org-files-as-html)
  11873. (:inline-images . org-export-html-inline-images)
  11874. (:expand-quoted-html . org-export-html-expand)
  11875. (:timestamp . org-export-html-with-timestamp)
  11876. (:publishing-directory . org-export-publishing-directory)
  11877. (:preamble . org-export-html-preamble)
  11878. (:postamble . org-export-html-postamble)
  11879. (:auto-preamble . org-export-html-auto-preamble)
  11880. (:auto-postamble . org-export-html-auto-postamble)
  11881. (:author . user-full-name)
  11882. (:email . user-mail-address)))
  11883. (defun org-default-export-plist ()
  11884. "Return the property list with default settings for the export variables."
  11885. (let ((l org-export-plist-vars) rtn e)
  11886. (while (setq e (pop l))
  11887. (setq rtn (cons (car e) (cons (symbol-value (cdr e)) rtn))))
  11888. rtn))
  11889. (defun org-infile-export-plist ()
  11890. "Return the property list with file-local settings for export."
  11891. (save-excursion
  11892. (goto-char 0)
  11893. (let ((re (org-make-options-regexp
  11894. '("TITLE" "AUTHOR" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE")))
  11895. p key val text options)
  11896. (while (re-search-forward re nil t)
  11897. (setq key (org-match-string-no-properties 1)
  11898. val (org-match-string-no-properties 2))
  11899. (cond
  11900. ((string-equal key "TITLE") (setq p (plist-put p :title val)))
  11901. ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
  11902. ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
  11903. ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
  11904. ((string-equal key "TEXT")
  11905. (setq text (if text (concat text "\n" val) val)))
  11906. ((string-equal key "OPTIONS") (setq options val))))
  11907. (setq p (plist-put p :text text))
  11908. (when options
  11909. (let ((op '(("H" . :headline-levels)
  11910. ("num" . :section-numbers)
  11911. ("toc" . :table-of-contents)
  11912. ("\\n" . :preserve-breaks)
  11913. ("@" . :expand-quoted-html)
  11914. (":" . :fixed-width)
  11915. ("|" . :tables)
  11916. ("^" . :sub-superscript)
  11917. ("*" . :emphasize)
  11918. ("TeX" . :TeX-macros)
  11919. ("LaTeX" . :LaTeX-fragments)))
  11920. o)
  11921. (while (setq o (pop op))
  11922. (if (string-match (concat (regexp-quote (car o))
  11923. ":\\([^ \t\n\r;,.]*\\)")
  11924. options)
  11925. (setq p (plist-put p (cdr o)
  11926. (car (read-from-string
  11927. (match-string 1 options)))))))))
  11928. p)))
  11929. (defun org-combine-plists (&rest plists)
  11930. "Create a single property list from all plists in PLISTS.
  11931. The process starts by copying the last list, and then setting properties
  11932. from the other lists. Settings in the first list are the most significant
  11933. ones and overrule settings in the other lists."
  11934. (let ((rtn (copy-sequence (pop plists)))
  11935. p v ls)
  11936. (while plists
  11937. (setq ls (pop plists))
  11938. (while ls
  11939. (setq p (pop ls) v (pop ls))
  11940. (setq rtn (plist-put rtn p v))))
  11941. rtn))
  11942. (defun org-export-directory (type plist)
  11943. (let* ((val (plist-get plist :publishing-directory))
  11944. (dir (if (listp val)
  11945. (or (cdr (assoc type val)) ".")
  11946. val)))
  11947. dir))
  11948. (defun org-export-find-first-heading-line (list)
  11949. "Remove all lines from LIST which are before the first headline."
  11950. (let ((orig-list list)
  11951. (re (concat "^" outline-regexp)))
  11952. (while (and list
  11953. (not (string-match re (car list))))
  11954. (pop list))
  11955. (or list orig-list)))
  11956. (defun org-skip-comments (lines)
  11957. "Skip lines starting with \"#\" and subtrees starting with COMMENT."
  11958. (let ((re1 (concat "^\\(\\*+\\)[ \t]+" org-comment-string))
  11959. (re2 "^\\(\\*+\\)[ \t\n\r]")
  11960. rtn line level)
  11961. (while (setq line (pop lines))
  11962. (cond
  11963. ((and (string-match re1 line)
  11964. (setq level (- (match-end 1) (match-beginning 1))))
  11965. ;; Beginning of a COMMENT subtree. Skip it.
  11966. (while (and (setq line (pop lines))
  11967. (or (not (string-match re2 line))
  11968. (> (- (match-end 1) (match-beginning 1)) level))))
  11969. (setq lines (cons line lines)))
  11970. ((string-match "^#" line)
  11971. ;; an ordinary comment line
  11972. )
  11973. ((and org-export-table-remove-special-lines
  11974. (string-match "^[ \t]*|" line)
  11975. (or (string-match "^[ \t]*| *[!_^] *|" line)
  11976. (and (string-match "| *<[0-9]+> *|" line)
  11977. (not (string-match "| *[^ <|]" line)))))
  11978. ;; a special table line that should be removed
  11979. )
  11980. (t (setq rtn (cons line rtn)))))
  11981. (nreverse rtn)))
  11982. (defun org-export (&optional arg)
  11983. (interactive)
  11984. (let ((help "[t] insert the export option template
  11985. \[v] limit export to visible part of outline tree
  11986. \[a] export as ASCII
  11987. \[h] export as HTML
  11988. \[b] export as HTML and browse immediately
  11989. \[x] export as XOXO
  11990. \[i] export current file as iCalendar file
  11991. \[I] export all agenda files as iCalendar files
  11992. \[c] export agenda files into combined iCalendar file
  11993. \[F] publish current file
  11994. \[P] publish current project
  11995. \[X] publish... (project will be prompted for)
  11996. \[A] publish all projects")
  11997. (cmds
  11998. '((?t . org-insert-export-options-template)
  11999. (?v . org-export-visible)
  12000. (?a . org-export-as-ascii)
  12001. (?h . org-export-as-html)
  12002. (?b . org-export-as-html-and-open)
  12003. (?x . org-export-as-xoxo)
  12004. (?i . org-export-icalendar-this-file)
  12005. (?I . org-export-icalendar-all-agenda-files)
  12006. (?c . org-export-icalendar-combine-agenda-files)
  12007. (?F . org-publish-current-file)
  12008. (?P . org-publish-current-project)
  12009. (?X . org-publish)
  12010. (?A . org-publish-all)))
  12011. r1 r2 ass)
  12012. (save-window-excursion
  12013. (delete-other-windows)
  12014. (with-output-to-temp-buffer "*Org Export/Publishing Help*"
  12015. (princ help))
  12016. (message "Select command: ")
  12017. (setq r1 (read-char-exclusive)))
  12018. (setq r2 (if (< r1 27) (+ r1 96) r1))
  12019. (if (setq ass (assq r2 cmds))
  12020. (call-interactively (cdr ass))
  12021. (error "No command associated with key %c" r1))))
  12022. ;; ASCII
  12023. (defconst org-html-entities
  12024. '(("nbsp")
  12025. ("iexcl")
  12026. ("cent")
  12027. ("pound")
  12028. ("curren")
  12029. ("yen")
  12030. ("brvbar")
  12031. ("sect")
  12032. ("uml")
  12033. ("copy")
  12034. ("ordf")
  12035. ("laquo")
  12036. ("not")
  12037. ("shy")
  12038. ("reg")
  12039. ("macr")
  12040. ("deg")
  12041. ("plusmn")
  12042. ("sup2")
  12043. ("sup3")
  12044. ("acute")
  12045. ("micro")
  12046. ("para")
  12047. ("middot")
  12048. ("odot"."o")
  12049. ("star"."*")
  12050. ("cedil")
  12051. ("sup1")
  12052. ("ordm")
  12053. ("raquo")
  12054. ("frac14")
  12055. ("frac12")
  12056. ("frac34")
  12057. ("iquest")
  12058. ("Agrave")
  12059. ("Aacute")
  12060. ("Acirc")
  12061. ("Atilde")
  12062. ("Auml")
  12063. ("Aring") ("AA"."&Aring;")
  12064. ("AElig")
  12065. ("Ccedil")
  12066. ("Egrave")
  12067. ("Eacute")
  12068. ("Ecirc")
  12069. ("Euml")
  12070. ("Igrave")
  12071. ("Iacute")
  12072. ("Icirc")
  12073. ("Iuml")
  12074. ("ETH")
  12075. ("Ntilde")
  12076. ("Ograve")
  12077. ("Oacute")
  12078. ("Ocirc")
  12079. ("Otilde")
  12080. ("Ouml")
  12081. ("times")
  12082. ("Oslash")
  12083. ("Ugrave")
  12084. ("Uacute")
  12085. ("Ucirc")
  12086. ("Uuml")
  12087. ("Yacute")
  12088. ("THORN")
  12089. ("szlig")
  12090. ("agrave")
  12091. ("aacute")
  12092. ("acirc")
  12093. ("atilde")
  12094. ("auml")
  12095. ("aring")
  12096. ("aelig")
  12097. ("ccedil")
  12098. ("egrave")
  12099. ("eacute")
  12100. ("ecirc")
  12101. ("euml")
  12102. ("igrave")
  12103. ("iacute")
  12104. ("icirc")
  12105. ("iuml")
  12106. ("eth")
  12107. ("ntilde")
  12108. ("ograve")
  12109. ("oacute")
  12110. ("ocirc")
  12111. ("otilde")
  12112. ("ouml")
  12113. ("divide")
  12114. ("oslash")
  12115. ("ugrave")
  12116. ("uacute")
  12117. ("ucirc")
  12118. ("uuml")
  12119. ("yacute")
  12120. ("thorn")
  12121. ("yuml")
  12122. ("fnof")
  12123. ("Alpha")
  12124. ("Beta")
  12125. ("Gamma")
  12126. ("Delta")
  12127. ("Epsilon")
  12128. ("Zeta")
  12129. ("Eta")
  12130. ("Theta")
  12131. ("Iota")
  12132. ("Kappa")
  12133. ("Lambda")
  12134. ("Mu")
  12135. ("Nu")
  12136. ("Xi")
  12137. ("Omicron")
  12138. ("Pi")
  12139. ("Rho")
  12140. ("Sigma")
  12141. ("Tau")
  12142. ("Upsilon")
  12143. ("Phi")
  12144. ("Chi")
  12145. ("Psi")
  12146. ("Omega")
  12147. ("alpha")
  12148. ("beta")
  12149. ("gamma")
  12150. ("delta")
  12151. ("epsilon")
  12152. ("varepsilon"."&epsilon;")
  12153. ("zeta")
  12154. ("eta")
  12155. ("theta")
  12156. ("iota")
  12157. ("kappa")
  12158. ("lambda")
  12159. ("mu")
  12160. ("nu")
  12161. ("xi")
  12162. ("omicron")
  12163. ("pi")
  12164. ("rho")
  12165. ("sigmaf") ("varsigma"."&sigmaf;")
  12166. ("sigma")
  12167. ("tau")
  12168. ("upsilon")
  12169. ("phi")
  12170. ("chi")
  12171. ("psi")
  12172. ("omega")
  12173. ("thetasym") ("vartheta"."&thetasym;")
  12174. ("upsih")
  12175. ("piv")
  12176. ("bull") ("bullet"."&bull;")
  12177. ("hellip") ("dots"."&hellip;")
  12178. ("prime")
  12179. ("Prime")
  12180. ("oline")
  12181. ("frasl")
  12182. ("weierp")
  12183. ("image")
  12184. ("real")
  12185. ("trade")
  12186. ("alefsym")
  12187. ("larr") ("leftarrow"."&larr;") ("gets"."&larr;")
  12188. ("uarr") ("uparrow"."&uarr;")
  12189. ("rarr") ("to"."&rarr;") ("rightarrow"."&rarr;")
  12190. ("darr")("downarrow"."&darr;")
  12191. ("harr") ("leftrightarrow"."&harr;")
  12192. ("crarr") ("hookleftarrow"."&crarr;") ; has round hook, not quite CR
  12193. ("lArr") ("Leftarrow"."&lArr;")
  12194. ("uArr") ("Uparrow"."&uArr;")
  12195. ("rArr") ("Rightarrow"."&rArr;")
  12196. ("dArr") ("Downarrow"."&dArr;")
  12197. ("hArr") ("Leftrightarrow"."&hArr;")
  12198. ("forall")
  12199. ("part") ("partial"."&part;")
  12200. ("exist") ("exists"."&exist;")
  12201. ("empty") ("emptyset"."&empty;")
  12202. ("nabla")
  12203. ("isin") ("in"."&isin;")
  12204. ("notin")
  12205. ("ni")
  12206. ("prod")
  12207. ("sum")
  12208. ("minus")
  12209. ("lowast") ("ast"."&lowast;")
  12210. ("radic")
  12211. ("prop") ("proptp"."&prop;")
  12212. ("infin") ("infty"."&infin;")
  12213. ("ang") ("angle"."&ang;")
  12214. ("and") ("vee"."&and;")
  12215. ("or") ("wedge"."&or;")
  12216. ("cap")
  12217. ("cup")
  12218. ("int")
  12219. ("there4")
  12220. ("sim")
  12221. ("cong") ("simeq"."&cong;")
  12222. ("asymp")("approx"."&asymp;")
  12223. ("ne") ("neq"."&ne;")
  12224. ("equiv")
  12225. ("le")
  12226. ("ge")
  12227. ("sub") ("subset"."&sub;")
  12228. ("sup") ("supset"."&sup;")
  12229. ("nsub")
  12230. ("sube")
  12231. ("supe")
  12232. ("oplus")
  12233. ("otimes")
  12234. ("perp")
  12235. ("sdot") ("cdot"."&sdot;")
  12236. ("lceil")
  12237. ("rceil")
  12238. ("lfloor")
  12239. ("rfloor")
  12240. ("lang")
  12241. ("rang")
  12242. ("loz") ("Diamond"."&loz;")
  12243. ("spades") ("spadesuit"."&spades;")
  12244. ("clubs") ("clubsuit"."&clubs;")
  12245. ("hearts") ("diamondsuit"."&hearts;")
  12246. ("diams") ("diamondsuit"."&diams;")
  12247. ("quot")
  12248. ("amp")
  12249. ("lt")
  12250. ("gt")
  12251. ("OElig")
  12252. ("oelig")
  12253. ("Scaron")
  12254. ("scaron")
  12255. ("Yuml")
  12256. ("circ")
  12257. ("tilde")
  12258. ("ensp")
  12259. ("emsp")
  12260. ("thinsp")
  12261. ("zwnj")
  12262. ("zwj")
  12263. ("lrm")
  12264. ("rlm")
  12265. ("ndash")
  12266. ("mdash")
  12267. ("lsquo")
  12268. ("rsquo")
  12269. ("sbquo")
  12270. ("ldquo")
  12271. ("rdquo")
  12272. ("bdquo")
  12273. ("dagger")
  12274. ("Dagger")
  12275. ("permil")
  12276. ("lsaquo")
  12277. ("rsaquo")
  12278. ("euro")
  12279. ("arccos"."arccos")
  12280. ("arcsin"."arcsin")
  12281. ("arctan"."arctan")
  12282. ("arg"."arg")
  12283. ("cos"."cos")
  12284. ("cosh"."cosh")
  12285. ("cot"."cot")
  12286. ("coth"."coth")
  12287. ("csc"."csc")
  12288. ("deg"."deg")
  12289. ("det"."det")
  12290. ("dim"."dim")
  12291. ("exp"."exp")
  12292. ("gcd"."gcd")
  12293. ("hom"."hom")
  12294. ("inf"."inf")
  12295. ("ker"."ker")
  12296. ("lg"."lg")
  12297. ("lim"."lim")
  12298. ("liminf"."liminf")
  12299. ("limsup"."limsup")
  12300. ("ln"."ln")
  12301. ("log"."log")
  12302. ("max"."max")
  12303. ("min"."min")
  12304. ("Pr"."Pr")
  12305. ("sec"."sec")
  12306. ("sin"."sin")
  12307. ("sinh"."sinh")
  12308. ("sup"."sup")
  12309. ("tan"."tan")
  12310. ("tanh"."tanh")
  12311. )
  12312. "Entities for TeX->HTML translation.
  12313. Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
  12314. \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
  12315. In that case, \"\\ent\" will be translated to \"&other;\".
  12316. The list contains HTML entities for Latin-1, Greek and other symbols.
  12317. It is supplemented by a number of commonly used TeX macros with appropriate
  12318. translations. There is currently no way for users to extend this.")
  12319. (defun org-cleaned-string-for-export (string &rest parameters)
  12320. "Cleanup a buffer substring so that links can be created safely."
  12321. (interactive)
  12322. (let* ((re-radio (and org-target-link-regexp
  12323. (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)")))
  12324. (re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
  12325. (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
  12326. (re-archive (concat ":" org-archive-tag ":"))
  12327. rtn)
  12328. (save-excursion
  12329. (set-buffer (get-buffer-create " org-mode-tmp"))
  12330. (erase-buffer)
  12331. (insert string)
  12332. (let ((org-inhibit-startup t)) (org-mode))
  12333. ;; Get rid of archived trees
  12334. (when (not (eq org-export-with-archived-trees t))
  12335. (goto-char (point-min))
  12336. (while (re-search-forward re-archive nil t)
  12337. (if (not (org-on-heading-p))
  12338. (org-end-of-subtree t)
  12339. (beginning-of-line 1)
  12340. (delete-region
  12341. (if org-export-with-archived-trees (1+ (point-at-eol)) (point))
  12342. (org-end-of-subtree)))))
  12343. ;; Find targets in comments and move them out of comments,
  12344. ;; but mark them as targets that should be invisible
  12345. (goto-char (point-min))
  12346. (while (re-search-forward "^#.*?\\(<<<?[^>\r\n]+>>>?\\).*" nil t)
  12347. (replace-match "\\1(INVISIBLE)"))
  12348. ;; Find matches for radio targets and turn them into internal links
  12349. (goto-char (point-min))
  12350. (when re-radio
  12351. (while (re-search-forward re-radio nil t)
  12352. (replace-match "\\1[[\\2]]")))
  12353. ;; Find all links that contain a newline and put them into a single line
  12354. (goto-char (point-min))
  12355. (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
  12356. (replace-match "\\1 \\3")
  12357. (goto-char (match-beginning 0)))
  12358. ;; Convert LaTeX fragments to images
  12359. (when (memq :LaTeX-fragments parameters)
  12360. (org-format-latex
  12361. (concat "ltxpng/" (file-name-sans-extension
  12362. (file-name-nondirectory
  12363. org-current-export-file)))
  12364. org-current-export-dir nil "Creating LaTeX image %s"))
  12365. (message "Exporting...")
  12366. ;; Normalize links: Convert angle and plain links into bracket links
  12367. (goto-char (point-min))
  12368. (while (re-search-forward re-plain-link nil t)
  12369. (replace-match
  12370. (concat
  12371. (match-string 1) "[[" (match-string 2) ":" (match-string 3) "]]")
  12372. t t))
  12373. (goto-char (point-min))
  12374. (while (re-search-forward re-angle-link nil t)
  12375. (replace-match
  12376. (concat
  12377. (match-string 1) "[[" (match-string 2) ":" (match-string 3) "]]")
  12378. t t))
  12379. ;; Find multiline emphasis and put them into single line
  12380. (when (memq :emph-multiline parameters)
  12381. (goto-char (point-min))
  12382. (while (re-search-forward org-emph-re nil t)
  12383. (subst-char-in-region (match-beginning 0) (match-end 0) ?\n ?\ t)
  12384. (goto-char (1- (match-end 0)))))
  12385. ;; Remove comments
  12386. (goto-char (point-min))
  12387. (while (re-search-forward "^#.*\n?" nil t)
  12388. (replace-match ""))
  12389. (setq rtn (buffer-string)))
  12390. (kill-buffer " org-mode-tmp")
  12391. rtn))
  12392. (defun org-solidify-link-text (s &optional alist)
  12393. "Take link text and make a safe target out of it."
  12394. (save-match-data
  12395. (let* ((rtn
  12396. (mapconcat
  12397. 'identity
  12398. (org-split-string s "[ \t\r\n]+") "--"))
  12399. (a (assoc rtn alist)))
  12400. (or (cdr a) rtn))))
  12401. (defun org-convert-to-odd-levels ()
  12402. "Convert an org-mode file with all levels allowed to one with odd levels.
  12403. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  12404. level 5 etc."
  12405. (interactive)
  12406. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  12407. (let ((org-odd-levels-only nil) n)
  12408. (save-excursion
  12409. (goto-char (point-min))
  12410. (while (re-search-forward "^\\*\\*+" nil t)
  12411. (setq n (1- (length (match-string 0))))
  12412. (while (>= (setq n (1- n)) 0)
  12413. (org-demote))
  12414. (end-of-line 1))))))
  12415. (defun org-convert-to-oddeven-levels ()
  12416. "Convert an org-mode file with only odd levels to one with odd and even levels.
  12417. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  12418. section with an even level, conversion would destroy the structure of the file. An error
  12419. is signaled in this case."
  12420. (interactive)
  12421. (goto-char (point-min))
  12422. ;; First check if there are no even levels
  12423. (when (re-search-forward "^\\(\\*\\*\\)+[^*]" nil t)
  12424. (org-show-hierarchy-above)
  12425. (error "Not all levels are odd in this file. Conversion not possible."))
  12426. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  12427. (let ((org-odd-levels-only nil) n)
  12428. (save-excursion
  12429. (goto-char (point-min))
  12430. (while (re-search-forward "^\\*\\*+" nil t)
  12431. (setq n (/ (length (match-string 0)) 2))
  12432. (while (>= (setq n (1- n)) 0)
  12433. (org-promote))
  12434. (end-of-line 1))))))
  12435. (defun org-tr-level (n)
  12436. "Make N odd if required."
  12437. (if org-odd-levels-only (1+ (/ n 2)) n))
  12438. (defvar org-last-level nil) ; dynamically scoped variable
  12439. (defvar org-ascii-current-indentation nil) ; For communication
  12440. (defun org-export-as-ascii (arg)
  12441. "Export the outline as a pretty ASCII file.
  12442. If there is an active region, export only the region.
  12443. The prefix ARG specifies how many levels of the outline should become
  12444. underlined headlines. The default is 3."
  12445. (interactive "P")
  12446. (setq-default org-todo-line-regexp org-todo-line-regexp)
  12447. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  12448. (org-infile-export-plist)))
  12449. (region
  12450. (buffer-substring
  12451. (if (org-region-active-p) (region-beginning) (point-min))
  12452. (if (org-region-active-p) (region-end) (point-max))))
  12453. (lines (org-export-find-first-heading-line
  12454. (org-skip-comments
  12455. (org-split-string
  12456. (org-cleaned-string-for-export region)
  12457. "[\r\n]"))))
  12458. (org-ascii-current-indentation '(0 . 0))
  12459. (org-startup-with-deadline-check nil)
  12460. (level 0) line txt
  12461. (umax nil)
  12462. (case-fold-search nil)
  12463. (filename (concat (file-name-as-directory
  12464. (org-export-directory :ascii opt-plist))
  12465. (file-name-sans-extension
  12466. (file-name-nondirectory buffer-file-name))
  12467. ".txt"))
  12468. (buffer (find-file-noselect filename))
  12469. (levels-open (make-vector org-level-max nil))
  12470. (odd org-odd-levels-only)
  12471. (date (format-time-string "%Y/%m/%d" (current-time)))
  12472. (time (format-time-string "%X" (org-current-time)))
  12473. (author (plist-get opt-plist :author))
  12474. (title (or (plist-get opt-plist :title)
  12475. (file-name-sans-extension
  12476. (file-name-nondirectory buffer-file-name))))
  12477. (email (plist-get opt-plist :email))
  12478. (language (plist-get opt-plist :language))
  12479. (text nil)
  12480. (todo nil)
  12481. (lang-words nil))
  12482. (setq org-last-level 1)
  12483. (org-init-section-numbers)
  12484. (find-file-noselect filename)
  12485. (setq lang-words (or (assoc language org-export-language-setup)
  12486. (assoc "en" org-export-language-setup)))
  12487. (if org-export-ascii-show-new-buffer
  12488. (switch-to-buffer-other-window buffer)
  12489. (set-buffer buffer))
  12490. (erase-buffer)
  12491. (fundamental-mode)
  12492. ;; create local variables for all options, to make sure all called
  12493. ;; functions get the correct information
  12494. (mapcar (lambda (x)
  12495. (set (make-local-variable (cdr x))
  12496. (plist-get opt-plist (car x))))
  12497. org-export-plist-vars)
  12498. (org-set-local 'org-odd-levels-only odd)
  12499. (setq umax (if arg (prefix-numeric-value arg)
  12500. org-export-headline-levels))
  12501. ;; File header
  12502. (if title (org-insert-centered title ?=))
  12503. (insert "\n")
  12504. (if (or author email)
  12505. (insert (concat (nth 1 lang-words) ": " (or author "")
  12506. (if email (concat " <" email ">") "")
  12507. "\n")))
  12508. (if (and date time)
  12509. (insert (concat (nth 2 lang-words) ": " date " " time "\n")))
  12510. (if text (insert (concat (org-html-expand-for-ascii text) "\n\n")))
  12511. (insert "\n\n")
  12512. (if org-export-with-toc
  12513. (progn
  12514. (insert (nth 3 lang-words) "\n"
  12515. (make-string (length (nth 3 lang-words)) ?=) "\n")
  12516. (mapcar '(lambda (line)
  12517. (if (string-match org-todo-line-regexp
  12518. line)
  12519. ;; This is a headline
  12520. (progn
  12521. (setq level (- (match-end 1) (match-beginning 1))
  12522. level (org-tr-level level)
  12523. txt (match-string 3 line)
  12524. todo
  12525. (or (and org-export-mark-todo-in-toc
  12526. (match-beginning 2)
  12527. (not (equal (match-string 2 line)
  12528. org-done-string)))
  12529. ; TODO, not DONE
  12530. (and org-export-mark-todo-in-toc
  12531. (= level umax)
  12532. (org-search-todo-below
  12533. line lines level))))
  12534. (setq txt (org-html-expand-for-ascii txt))
  12535. (if org-export-with-section-numbers
  12536. (setq txt (concat (org-section-number level)
  12537. " " txt)))
  12538. (if (<= level umax)
  12539. (progn
  12540. (insert
  12541. (make-string (* (1- level) 4) ?\ )
  12542. (format (if todo "%s (*)\n" "%s\n") txt))
  12543. (setq org-last-level level))
  12544. ))))
  12545. lines)))
  12546. (org-init-section-numbers)
  12547. (while (setq line (pop lines))
  12548. ;; Remove the quoted HTML tags.
  12549. (setq line (org-html-expand-for-ascii line))
  12550. ;; Remove targets
  12551. (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line)
  12552. (setq line (replace-match "" t t line)))
  12553. ;; Replace internal links
  12554. (while (string-match org-bracket-link-regexp line)
  12555. (setq line (replace-match
  12556. (if (match-end 3) "[\\3]" "[\\1]")
  12557. t nil line)))
  12558. (cond
  12559. ((string-match "^\\(\\*+\\)[ \t]*\\(.*\\)" line)
  12560. ;; a Headline
  12561. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  12562. txt (match-string 2 line))
  12563. (org-ascii-level-start level txt umax lines))
  12564. (t
  12565. (insert (org-fix-indentation line org-ascii-current-indentation) "\n"))))
  12566. (normal-mode)
  12567. (save-buffer)
  12568. ;; remove display and invisible chars
  12569. (let (beg end)
  12570. (goto-char (point-min))
  12571. (while (setq beg (next-single-property-change (point) 'display))
  12572. (setq end (next-single-property-change beg 'display))
  12573. (delete-region beg end)
  12574. (goto-char beg)
  12575. (insert "=>"))
  12576. (goto-char (point-min))
  12577. (while (setq beg (next-single-property-change (point) 'org-cwidth))
  12578. (setq end (next-single-property-change beg 'org-cwidth))
  12579. (delete-region beg end)
  12580. (goto-char beg)))
  12581. (goto-char (point-min))))
  12582. (defun org-search-todo-below (line lines level)
  12583. "Search the subtree below LINE for any TODO entries."
  12584. (let ((rest (cdr (memq line lines)))
  12585. (re org-todo-line-regexp)
  12586. line lv todo)
  12587. (catch 'exit
  12588. (while (setq line (pop rest))
  12589. (if (string-match re line)
  12590. (progn
  12591. (setq lv (- (match-end 1) (match-beginning 1))
  12592. todo (and (match-beginning 2)
  12593. (not (equal (match-string 2 line)
  12594. org-done-string))))
  12595. ; TODO, not DONE
  12596. (if (<= lv level) (throw 'exit nil))
  12597. (if todo (throw 'exit t))))))))
  12598. (defun org-html-expand-for-ascii (line)
  12599. "Handle quoted HTML for ASCII export."
  12600. (if org-export-html-expand
  12601. (while (string-match "@<[^<>\n]*>" line)
  12602. ;; We just remove the tags for now.
  12603. (setq line (replace-match "" nil nil line))))
  12604. line)
  12605. (defun org-insert-centered (s &optional underline)
  12606. "Insert the string S centered and underline it with character UNDERLINE."
  12607. (let ((ind (max (/ (- 80 (string-width s)) 2) 0)))
  12608. (insert (make-string ind ?\ ) s "\n")
  12609. (if underline
  12610. (insert (make-string ind ?\ )
  12611. (make-string (string-width s) underline)
  12612. "\n"))))
  12613. (defun org-ascii-level-start (level title umax &optional lines)
  12614. "Insert a new level in ASCII export."
  12615. (let (char (n (- level umax 1)) (ind 0))
  12616. (if (> level umax)
  12617. (progn
  12618. (insert (make-string (* 2 n) ?\ )
  12619. (char-to-string (nth (% n (length org-export-ascii-bullets))
  12620. org-export-ascii-bullets))
  12621. " " title "\n")
  12622. ;; find the indentation of the next non-empty line
  12623. (catch 'stop
  12624. (while lines
  12625. (if (string-match "^\\*" (car lines)) (throw 'stop nil))
  12626. (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
  12627. (throw 'stop (setq ind (org-get-indentation (car lines)))))
  12628. (pop lines)))
  12629. (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
  12630. (if (or (not (equal (char-before) ?\n))
  12631. (not (equal (char-before (1- (point))) ?\n)))
  12632. (insert "\n"))
  12633. (setq char (nth (- umax level) (reverse org-export-ascii-underline)))
  12634. (if org-export-with-section-numbers
  12635. (setq title (concat (org-section-number level) " " title)))
  12636. (insert title "\n" (make-string (string-width title) char) "\n")
  12637. (setq org-ascii-current-indentation '(0 . 0)))))
  12638. (defun org-export-visible (type arg)
  12639. "Create a copy of the visible part of the current buffer, and export it.
  12640. The copy is created in a temporary buffer and removed after use.
  12641. TYPE is the final key (as a string) that also select the export command in
  12642. the `C-c C-e' export dispatcher.
  12643. As a special case, if the you type SPC at the prompt, the temporary
  12644. org-mode file will not be removed but presented to you so that you can
  12645. continue to use it. The prefix arg ARG is passed through to the exporting
  12646. command."
  12647. (interactive
  12648. (list (progn
  12649. (message "Export visible: [a]SCII [h]tml [b]rowse HTML [x]OXO [ ]keep buffer")
  12650. (read-char-exclusive))
  12651. current-prefix-arg))
  12652. (if (not (member type '(?a ?\C-a ?b ?\C-b ?h ?x ?\ )))
  12653. (error "Invalid export key"))
  12654. (let* ((binding (cdr (assoc type
  12655. '((?a . org-export-as-ascii)
  12656. (?\C-a . org-export-as-ascii)
  12657. (?b . org-export-as-html-and-open)
  12658. (?\C-b . org-export-as-html-and-open)
  12659. (?h . org-export-as-html)
  12660. (?x . org-export-as-xoxo)))))
  12661. (keepp (equal type ?\ ))
  12662. (file buffer-file-name)
  12663. (buffer (get-buffer-create "*Org Export Visible*"))
  12664. s e)
  12665. (with-current-buffer buffer (erase-buffer))
  12666. (save-excursion
  12667. (setq s (goto-char (point-min)))
  12668. (while (not (= (point) (point-max)))
  12669. (goto-char (org-find-invisible))
  12670. (append-to-buffer buffer s (point))
  12671. (setq s (goto-char (org-find-visible))))
  12672. (goto-char (point-min))
  12673. (unless keepp
  12674. ;; Copy all comment lines to the end, to make sure #+ settings are
  12675. ;; still available for the second export step. Kind of a hack, but
  12676. ;; does do the trick.
  12677. (if (looking-at "#[^\r\n]*")
  12678. (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
  12679. (while (re-search-forward "[\n\r]#[^\n\r]*" nil t)
  12680. (append-to-buffer buffer (1+ (match-beginning 0))
  12681. (min (point-max) (1+ (match-end 0))))))
  12682. (set-buffer buffer)
  12683. (let ((buffer-file-name file)
  12684. (org-inhibit-startup t))
  12685. (org-mode)
  12686. (show-all)
  12687. (unless keepp (funcall binding arg))))
  12688. (if (not keepp)
  12689. (kill-buffer buffer)
  12690. (switch-to-buffer-other-window buffer)
  12691. (goto-char (point-min)))))
  12692. (defun org-find-visible ()
  12693. (let ((s (point)))
  12694. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  12695. (get-char-property s 'invisible)))
  12696. s))
  12697. (defun org-find-invisible ()
  12698. (let ((s (point)))
  12699. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  12700. (not (get-char-property s 'invisible))))
  12701. s))
  12702. ;; HTML
  12703. (defun org-get-current-options ()
  12704. "Return a string with current options as keyword options.
  12705. Does include HTML export options as well as TODO and CATEGORY stuff."
  12706. (format
  12707. "#+TITLE: %s
  12708. #+AUTHOR: %s
  12709. #+EMAIL: %s
  12710. #+LANGUAGE: %s
  12711. #+TEXT: Some descriptive text to be emitted. Several lines OK.
  12712. #+OPTIONS: H:%d num:%s toc:%s \\n:%s @:%s ::%s |:%s ^:%s *:%s TeX:%s LaTeX:%s
  12713. #+CATEGORY: %s
  12714. #+SEQ_TODO: %s
  12715. #+TYP_TODO: %s
  12716. #+STARTUP: %s %s %s %s %s %s
  12717. #+TAGS: %s
  12718. #+ARCHIVE: %s
  12719. "
  12720. (buffer-name) (user-full-name) user-mail-address org-export-default-language
  12721. org-export-headline-levels
  12722. org-export-with-section-numbers
  12723. org-export-with-toc
  12724. org-export-preserve-breaks
  12725. org-export-html-expand
  12726. org-export-with-fixed-width
  12727. org-export-with-tables
  12728. org-export-with-sub-superscripts
  12729. org-export-with-emphasize
  12730. org-export-with-TeX-macros
  12731. org-export-with-LaTeX-fragments
  12732. (file-name-nondirectory buffer-file-name)
  12733. (if (equal org-todo-interpretation 'sequence)
  12734. (mapconcat 'identity org-todo-keywords " ")
  12735. "TODO FEEDBACK VERIFY DONE")
  12736. (if (equal org-todo-interpretation 'type)
  12737. (mapconcat 'identity org-todo-keywords " ")
  12738. "Me Jason Marie DONE")
  12739. (cdr (assoc org-startup-folded
  12740. '((nil . "showall") (t . "overview") (content . "content"))))
  12741. (if org-startup-with-deadline-check "dlcheck" "nodlcheck")
  12742. (if org-odd-levels-only "odd" "oddeven")
  12743. (if org-hide-leading-stars "hidestars" "showstars")
  12744. (if org-startup-align-all-tables "align" "noalign")
  12745. (if org-log-done "logging" "nologging")
  12746. (if org-tag-alist (mapconcat 'car org-tag-alist " ") "")
  12747. org-archive-location
  12748. ))
  12749. (defun org-insert-export-options-template ()
  12750. "Insert into the buffer a template with information for exporting."
  12751. (interactive)
  12752. (if (not (bolp)) (newline))
  12753. (let ((s (org-get-current-options)))
  12754. (and (string-match "#\\+CATEGORY" s)
  12755. (setq s (substring s 0 (match-beginning 0))))
  12756. (insert s)))
  12757. (defun org-toggle-fixed-width-section (arg)
  12758. "Toggle the fixed-width export.
  12759. If there is no active region, the QUOTE keyword at the current headline is
  12760. inserted or removed. When present, it causes the text between this headline
  12761. and the next to be exported as fixed-width text, and unmodified.
  12762. If there is an active region, this command adds or removes a colon as the
  12763. first character of this line. If the first character of a line is a colon,
  12764. this line is also exported in fixed-width font."
  12765. (interactive "P")
  12766. (let* ((cc 0)
  12767. (regionp (org-region-active-p))
  12768. (beg (if regionp (region-beginning) (point)))
  12769. (end (if regionp (region-end)))
  12770. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  12771. (re "[ \t]*\\(:\\)")
  12772. off)
  12773. (if regionp
  12774. (save-excursion
  12775. (goto-char beg)
  12776. (setq cc (current-column))
  12777. (beginning-of-line 1)
  12778. (setq off (looking-at re))
  12779. (while (> nlines 0)
  12780. (setq nlines (1- nlines))
  12781. (beginning-of-line 1)
  12782. (cond
  12783. (arg
  12784. (move-to-column cc t)
  12785. (insert ":\n")
  12786. (forward-line -1))
  12787. ((and off (looking-at re))
  12788. (replace-match "" t t nil 1))
  12789. ((not off) (move-to-column cc t) (insert ":")))
  12790. (forward-line 1)))
  12791. (save-excursion
  12792. (org-back-to-heading)
  12793. (if (looking-at (concat outline-regexp
  12794. "\\( +\\<" org-quote-string "\\>\\)"))
  12795. (replace-match "" t t nil 1)
  12796. (if (looking-at outline-regexp)
  12797. (progn
  12798. (goto-char (match-end 0))
  12799. (insert " " org-quote-string))))))))
  12800. (defun org-export-as-html-and-open (arg)
  12801. "Export the outline as HTML and immediately open it with a browser.
  12802. If there is an active region, export only the region.
  12803. The prefix ARG specifies how many levels of the outline should become
  12804. headlines. The default is 3. Lower levels will become bulleted lists."
  12805. (interactive "P")
  12806. (org-export-as-html arg 'hidden)
  12807. (org-open-file buffer-file-name))
  12808. (defun org-export-as-html-batch ()
  12809. "Call `org-export-as-html', may be used in batch processing as
  12810. emacs --batch
  12811. --load=$HOME/lib/emacs/org.el
  12812. --eval \"(setq org-export-headline-levels 2)\"
  12813. --visit=MyFile --funcall org-export-as-html-batch"
  12814. (org-export-as-html org-export-headline-levels 'hidden))
  12815. (defun org-export-as-html (arg &optional hidden ext-plist)
  12816. "Export the outline as a pretty HTML file.
  12817. If there is an active region, export only the region.
  12818. The prefix ARG specifies how many levels of the outline should become
  12819. headlines. The default is 3. Lower levels will become bulleted lists.
  12820. When HIDDEN is non-nil, don't display the HTML buffer.
  12821. EXT-PLIST is a property list with external parameters overriding
  12822. org-mode's default settings, but still inferior to file-local settings."
  12823. (interactive "P")
  12824. (message "Exporting...")
  12825. (setq-default org-todo-line-regexp org-todo-line-regexp)
  12826. (setq-default org-deadline-line-regexp org-deadline-line-regexp)
  12827. (setq-default org-done-string org-done-string)
  12828. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  12829. ext-plist
  12830. (org-infile-export-plist)))
  12831. (style (plist-get opt-plist :style))
  12832. (link-validate (plist-get opt-plist :link-validation-function))
  12833. valid
  12834. (odd org-odd-levels-only)
  12835. (region-p (org-region-active-p))
  12836. (region
  12837. (buffer-substring
  12838. (if region-p (region-beginning) (point-min))
  12839. (if region-p (region-end) (point-max))))
  12840. ;; The following two are dynamically scoped into other
  12841. ;; routines below.
  12842. (org-current-export-dir (org-export-directory :html opt-plist))
  12843. (org-current-export-file buffer-file-name)
  12844. (all_lines
  12845. (org-skip-comments (org-split-string
  12846. (org-cleaned-string-for-export
  12847. region :emph-multiline
  12848. (if (plist-get opt-plist :LaTeX-fragments)
  12849. :LaTeX-fragments))
  12850. "[\r\n]")))
  12851. (lines (org-export-find-first-heading-line all_lines))
  12852. (level 0) (line "") (origline "") txt todo
  12853. (umax nil)
  12854. (filename (concat (file-name-as-directory
  12855. (org-export-directory :html opt-plist))
  12856. (file-name-sans-extension
  12857. (file-name-nondirectory buffer-file-name))
  12858. ".html"))
  12859. (current-dir (file-name-directory buffer-file-name))
  12860. (buffer (find-file-noselect filename))
  12861. (levels-open (make-vector org-level-max nil))
  12862. (date (format-time-string "%Y/%m/%d" (current-time)))
  12863. (time (format-time-string "%X" (org-current-time)))
  12864. (author (plist-get opt-plist :author))
  12865. (title (or (plist-get opt-plist :title)
  12866. (file-name-sans-extension
  12867. (file-name-nondirectory buffer-file-name))))
  12868. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  12869. (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
  12870. (inquote nil)
  12871. (infixed nil)
  12872. (in-local-list nil)
  12873. (local-list-num nil)
  12874. (local-list-indent nil)
  12875. (llt org-plain-list-ordered-item-terminator)
  12876. (email (plist-get opt-plist :email))
  12877. (language (plist-get opt-plist :language))
  12878. (text (plist-get opt-plist :text))
  12879. (lang-words nil)
  12880. (target-alist nil) tg
  12881. (head-count 0) cnt
  12882. (start 0)
  12883. (coding-system (and (fboundp 'coding-system-get)
  12884. (boundp 'buffer-file-coding-system)
  12885. buffer-file-coding-system))
  12886. (coding-system-for-write (or coding-system coding-system-for-write))
  12887. (save-buffer-coding-system (or coding-system save-buffer-coding-system))
  12888. (charset (and coding-system
  12889. (coding-system-get coding-system 'mime-charset)))
  12890. table-open type
  12891. table-buffer table-orig-buffer
  12892. ind start-is-num starter
  12893. rpl path desc descp desc1 desc2 link
  12894. )
  12895. (message "Exporting...")
  12896. (setq org-last-level 1)
  12897. (org-init-section-numbers)
  12898. ;; Get the language-dependent settings
  12899. (setq lang-words (or (assoc language org-export-language-setup)
  12900. (assoc "en" org-export-language-setup)))
  12901. ;; Switch to the output buffer
  12902. (if (or hidden (not org-export-html-show-new-buffer))
  12903. (set-buffer buffer)
  12904. (switch-to-buffer-other-window buffer))
  12905. (erase-buffer)
  12906. (fundamental-mode)
  12907. (let ((case-fold-search nil)
  12908. (org-odd-levels-only odd))
  12909. ;; create local variables for all options, to make sure all called
  12910. ;; functions get the correct information
  12911. (mapcar (lambda (x)
  12912. (set (make-local-variable (cdr x))
  12913. (plist-get opt-plist (car x))))
  12914. org-export-plist-vars)
  12915. (setq umax (if arg (prefix-numeric-value arg)
  12916. org-export-headline-levels))
  12917. ;; File header
  12918. (insert (format
  12919. "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
  12920. \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
  12921. <html xmlns=\"http://www.w3.org/1999/xhtml\"
  12922. lang=\"%s\" xml:lang=\"%s\">
  12923. <head>
  12924. <title>%s</title>
  12925. <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
  12926. <meta name=\"generator\" content=\"Org-mode\"/>
  12927. <meta name=\"generated\" content=\"%s %s\"/>
  12928. <meta name=\"author\" content=\"%s\"/>
  12929. %s
  12930. </head><body>
  12931. "
  12932. language language (org-html-expand title) (or charset "iso-8859-1")
  12933. date time author style))
  12934. (insert (or (plist-get opt-plist :preamble) ""))
  12935. (when (plist-get opt-plist :auto-preamble)
  12936. (if title (insert (format org-export-html-title-format
  12937. (org-html-expand title))))
  12938. (if text (insert "<p>\n" (org-html-expand text) "</p>")))
  12939. (if org-export-with-toc
  12940. (progn
  12941. (insert (format "<h%d>%s</h%d>\n"
  12942. org-export-html-toplevel-hlevel
  12943. (nth 3 lang-words)
  12944. org-export-html-toplevel-hlevel))
  12945. (insert "<ul>\n<li>")
  12946. (setq lines
  12947. (mapcar '(lambda (line)
  12948. (if (string-match org-todo-line-regexp line)
  12949. ;; This is a headline
  12950. (progn
  12951. (setq level (- (match-end 1) (match-beginning 1))
  12952. level (org-tr-level level)
  12953. txt (save-match-data
  12954. (org-html-expand
  12955. (org-export-cleanup-toc-line
  12956. (match-string 3 line))))
  12957. todo
  12958. (or (and org-export-mark-todo-in-toc
  12959. (match-beginning 2)
  12960. (not (equal (match-string 2 line)
  12961. org-done-string)))
  12962. ; TODO, not DONE
  12963. (and org-export-mark-todo-in-toc
  12964. (= level umax)
  12965. (org-search-todo-below
  12966. line lines level))))
  12967. (if org-export-with-section-numbers
  12968. (setq txt (concat (org-section-number level)
  12969. " " txt)))
  12970. (if (<= level umax)
  12971. (progn
  12972. (setq head-count (+ head-count 1))
  12973. (if (> level org-last-level)
  12974. (progn
  12975. (setq cnt (- level org-last-level))
  12976. (while (>= (setq cnt (1- cnt)) 0)
  12977. (insert "\n<ul>\n<li>"))
  12978. (insert "\n")))
  12979. (if (< level org-last-level)
  12980. (progn
  12981. (setq cnt (- org-last-level level))
  12982. (while (>= (setq cnt (1- cnt)) 0)
  12983. (insert "</li>\n</ul>"))
  12984. (insert "\n")))
  12985. ;; Check for targets
  12986. (while (string-match org-target-regexp line)
  12987. (setq tg (match-string 1 line)
  12988. line (replace-match
  12989. (concat "@<span class=\"target\">" tg "@</span> ")
  12990. t t line))
  12991. (push (cons (org-solidify-link-text tg)
  12992. (format "sec-%d" head-count))
  12993. target-alist))
  12994. (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
  12995. (setq txt (replace-match "" t t txt)))
  12996. (insert
  12997. (format
  12998. (if todo
  12999. "</li>\n<li><a href=\"#sec-%d\"><span class=\"todo\">%s</span></a>"
  13000. "</li>\n<li><a href=\"#sec-%d\">%s</a>")
  13001. head-count txt))
  13002. (setq org-last-level level))
  13003. )))
  13004. line)
  13005. lines))
  13006. (while (> org-last-level 0)
  13007. (setq org-last-level (1- org-last-level))
  13008. (insert "</li>\n</ul>\n"))
  13009. ))
  13010. (setq head-count 0)
  13011. (org-init-section-numbers)
  13012. (while (setq line (pop lines) origline line)
  13013. (catch 'nextline
  13014. ;; end of quote section?
  13015. (when (and inquote (string-match "^\\*+" line))
  13016. (insert "</pre>\n")
  13017. (setq inquote nil))
  13018. ;; inside a quote section?
  13019. (when inquote
  13020. (insert (org-html-protect line) "\n")
  13021. (throw 'nextline nil))
  13022. ;; verbatim lines
  13023. (when (and org-export-with-fixed-width
  13024. (string-match "^[ \t]*:\\(.*\\)" line))
  13025. (when (not infixed)
  13026. (setq infixed t)
  13027. (insert "<pre>\n"))
  13028. (insert (org-html-protect (match-string 1 line)) "\n")
  13029. (when (and lines
  13030. (not (string-match "^[ \t]*\\(:.*\\)"
  13031. (car lines))))
  13032. (setq infixed nil)
  13033. (insert "</pre>\n"))
  13034. (throw 'nextline nil))
  13035. ;; make targets to anchors
  13036. (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
  13037. (cond
  13038. ((match-end 2)
  13039. (setq line (replace-match
  13040. (concat "@<a name=\""
  13041. (org-solidify-link-text (match-string 1 line))
  13042. "\">\\nbsp@</a>")
  13043. t t line)))
  13044. ((and org-export-with-toc (equal (string-to-char line) ?*))
  13045. (setq line (replace-match
  13046. (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
  13047. ; (concat "@<i>" (match-string 1 line) "@</i> ")
  13048. t t line)))
  13049. (t
  13050. (setq line (replace-match
  13051. (concat "@<a name=\""
  13052. (org-solidify-link-text (match-string 1 line))
  13053. "\" class=\"target\">" (match-string 1 line) "@</a> ")
  13054. t t line)))))
  13055. (setq line (org-html-handle-time-stamps line))
  13056. ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  13057. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  13058. ;; Also handle sub_superscripts and checkboxes
  13059. (setq line (org-html-expand line))
  13060. ;; Format the links
  13061. (setq start 0)
  13062. (while (string-match org-bracket-link-analytic-regexp line start)
  13063. (setq start (match-beginning 0))
  13064. (setq type (if (match-end 2) (match-string 2 line) "internal"))
  13065. (setq path (match-string 3 line))
  13066. (setq desc1 (if (match-end 5) (match-string 5 line))
  13067. desc2 (if (match-end 2) (concat type ":" path) path)
  13068. descp (and desc1 (not (equal desc1 desc2)))
  13069. desc (or desc1 desc2))
  13070. ;; FIXME: do we need to unescape here somewhere?
  13071. (cond
  13072. ((equal type "internal")
  13073. (setq rpl
  13074. (concat
  13075. "<a href=\"#"
  13076. (org-solidify-link-text path target-alist)
  13077. "\">" desc "</a>")))
  13078. ((member type '("http" "https" "ftp" "mailto" "news"))
  13079. ;; standard URL
  13080. (setq link (concat type ":" path))
  13081. (setq rpl (concat "<a href=\"" link "\">" desc "</a>")))
  13082. ((string= type "file")
  13083. ;; FILE link
  13084. (let* ((filename path)
  13085. (abs-p (file-name-absolute-p filename))
  13086. thefile file-is-image-p search)
  13087. (save-match-data
  13088. (if (string-match "::\\(.*\\)" filename)
  13089. (setq search (match-string 1 filename)
  13090. filename (replace-match "" t nil filename)))
  13091. (setq valid
  13092. (if (functionp link-validate)
  13093. (funcall link-validate filename current-dir)
  13094. t))
  13095. (setq file-is-image-p
  13096. (string-match (org-image-file-name-regexp) filename))
  13097. (setq thefile (if abs-p (expand-file-name filename) filename))
  13098. (when (and org-export-html-link-org-files-as-html
  13099. (string-match "\\.org$" thefile))
  13100. (setq thefile (concat (substring thefile 0
  13101. (match-beginning 0))
  13102. ".html"))
  13103. (if (and search
  13104. ;; make sure this is can be used as target search
  13105. (not (string-match "^[0-9]*$" search))
  13106. (not (string-match "^\\*" search))
  13107. (not (string-match "^/.*/$" search)))
  13108. (setq thefile (concat thefile "#"
  13109. (org-solidify-link-text
  13110. (org-link-unescape search)))))
  13111. (when (string-match "^file:" desc)
  13112. (setq desc (replace-match "" t t desc))
  13113. (if (string-match "\\.org$" desc)
  13114. (setq desc (replace-match "" t t desc))))))
  13115. (setq rpl (if (and file-is-image-p
  13116. (or (eq t org-export-html-inline-images)
  13117. (and org-export-html-inline-images
  13118. (not descp))))
  13119. (concat "<img src=\"" thefile "\"/>")
  13120. (concat "<a href=\"" thefile "\">" desc "</a>")))
  13121. (if (not valid) (setq rpl desc))))
  13122. ((member type '("bbdb" "vm" "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  13123. (setq rpl (concat "<i>&lt;" type ":"
  13124. (save-match-data (org-link-unescape path))
  13125. "&gt;</i>"))))
  13126. (setq line (replace-match rpl t t line)
  13127. start (+ start (length rpl))))
  13128. ;; TODO items
  13129. (if (and (string-match org-todo-line-regexp line)
  13130. (match-beginning 2))
  13131. (if (equal (match-string 2 line) org-done-string)
  13132. (setq line (replace-match
  13133. "<span class=\"done\">\\2</span>"
  13134. t nil line 2))
  13135. (setq line (replace-match "<span class=\"todo\">\\2</span>"
  13136. t nil line 2))))
  13137. (cond
  13138. ((string-match "^\\(\\*+\\)[ \t]*\\(.*\\)" line)
  13139. ;; This is a headline
  13140. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  13141. txt (match-string 2 line))
  13142. (if (string-match quote-re0 txt)
  13143. (setq txt (replace-match "" t t txt)))
  13144. (if (<= level umax) (setq head-count (+ head-count 1)))
  13145. (when in-local-list
  13146. ;; Close any local lists before inserting a new header line
  13147. (while local-list-num
  13148. (org-close-li)
  13149. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  13150. (pop local-list-num))
  13151. (setq local-list-indent nil
  13152. in-local-list nil))
  13153. (org-html-level-start level txt umax
  13154. (and org-export-with-toc (<= level umax))
  13155. head-count)
  13156. ;; QUOTES
  13157. (when (string-match quote-re line)
  13158. (insert "<pre>")
  13159. (setq inquote t)))
  13160. ((and org-export-with-tables
  13161. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  13162. (if (not table-open)
  13163. ;; New table starts
  13164. (setq table-open t table-buffer nil table-orig-buffer nil))
  13165. ;; Accumulate lines
  13166. (setq table-buffer (cons line table-buffer)
  13167. table-orig-buffer (cons origline table-orig-buffer))
  13168. (when (or (not lines)
  13169. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  13170. (car lines))))
  13171. (setq table-open nil
  13172. table-buffer (nreverse table-buffer)
  13173. table-orig-buffer (nreverse table-orig-buffer))
  13174. (org-close-par-maybe)
  13175. (insert (org-format-table-html table-buffer table-orig-buffer))))
  13176. (t
  13177. ;; Normal lines
  13178. (when (string-match
  13179. (cond
  13180. ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  13181. ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  13182. ((= llt ?\)) "^\\( \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  13183. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
  13184. line)
  13185. (setq ind (org-get-string-indentation line)
  13186. start-is-num (match-beginning 4)
  13187. starter (if (match-beginning 2)
  13188. (substring (match-string 2 line) 0 -1))
  13189. line (substring line (match-beginning 5)))
  13190. (unless (string-match "[^ \t]" line)
  13191. ;; empty line. Pretend indentation is large.
  13192. (setq ind (1+ (or (car local-list-indent) 1))))
  13193. (while (and in-local-list
  13194. (or (and (= ind (car local-list-indent))
  13195. (not starter))
  13196. (< ind (car local-list-indent))))
  13197. (org-close-li)
  13198. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  13199. (pop local-list-num) (pop local-list-indent)
  13200. (setq in-local-list local-list-indent))
  13201. (cond
  13202. ((and starter
  13203. (or (not in-local-list)
  13204. (> ind (car local-list-indent))))
  13205. ;; Start new (level of ) list
  13206. (org-close-par-maybe)
  13207. (insert (if start-is-num "<ol>\n<li>\n" "<ul>\n<li>\n"))
  13208. (push start-is-num local-list-num)
  13209. (push ind local-list-indent)
  13210. (setq in-local-list t))
  13211. (starter
  13212. ;; continue current list
  13213. (org-close-li)
  13214. (insert "<li>\n")))
  13215. (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line)
  13216. (setq line
  13217. (replace-match
  13218. (if (equal (match-string 1 line) "X")
  13219. "<b>[X]</b>"
  13220. "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
  13221. t t line))))
  13222. ;; Empty lines start a new paragraph. If hand-formatted lists
  13223. ;; are not fully interpreted, lines starting with "-", "+", "*"
  13224. ;; also start a new paragraph.
  13225. (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
  13226. ;; Check if the line break needs to be conserved
  13227. (cond
  13228. ((string-match "\\\\\\\\[ \t]*$" line)
  13229. (setq line (replace-match "<br/>" t t line)))
  13230. (org-export-preserve-breaks
  13231. (setq line (concat line "<br/>"))))
  13232. (insert line "\n")))))
  13233. ;; Properly close all local lists and other lists
  13234. (when inquote (insert "</pre>\n"))
  13235. (when in-local-list
  13236. ;; Close any local lists before inserting a new header line
  13237. (while local-list-num
  13238. (org-close-li)
  13239. (insert (if (car local-list-num) "</ol>\n" "</ul>\n"))
  13240. (pop local-list-num))
  13241. (setq local-list-indent nil
  13242. in-local-list nil))
  13243. (org-html-level-start 1 nil umax
  13244. (and org-export-with-toc (<= level umax))
  13245. head-count)
  13246. (when (plist-get opt-plist :auto-postamble)
  13247. (when author
  13248. (insert "<p class=\"author\"> "
  13249. (nth 1 lang-words) ": " author "\n")
  13250. (when email
  13251. (insert "<a href=\"mailto:" email "\">&lt;"
  13252. email "&gt;</a>\n"))
  13253. (insert "</p>\n"))
  13254. (when (and date time)
  13255. (insert "<p class=\"date\"> "
  13256. (nth 2 lang-words) ": "
  13257. date " " time "</p>\n")))
  13258. (if org-export-html-with-timestamp
  13259. (insert org-export-html-html-helper-timestamp))
  13260. (insert (or (plist-get opt-plist :postamble) ""))
  13261. (insert "</body>\n</html>\n")
  13262. (normal-mode)
  13263. ;; remove empty paragraphs and lists
  13264. (goto-char (point-min))
  13265. (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
  13266. (replace-match ""))
  13267. (goto-char (point-min))
  13268. (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t)
  13269. (replace-match ""))
  13270. (save-buffer)
  13271. (goto-char (point-min))
  13272. (message "Exporting... done"))))
  13273. (defun org-format-table-html (lines olines)
  13274. "Find out which HTML converter to use and return the HTML code."
  13275. (if (string-match "^[ \t]*|" (car lines))
  13276. ;; A normal org table
  13277. (org-format-org-table-html lines)
  13278. ;; Table made by table.el - test for spanning
  13279. (let* ((hlines (delq nil (mapcar
  13280. (lambda (x)
  13281. (if (string-match "^[ \t]*\\+-" x) x
  13282. nil))
  13283. lines)))
  13284. (first (car hlines))
  13285. (ll (and (string-match "\\S-+" first)
  13286. (match-string 0 first)))
  13287. (re (concat "^[ \t]*" (regexp-quote ll)))
  13288. (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
  13289. hlines))))
  13290. (if (and (not spanning)
  13291. (not org-export-prefer-native-exporter-for-tables))
  13292. ;; We can use my own converter with HTML conversions
  13293. (org-format-table-table-html lines)
  13294. ;; Need to use the code generator in table.el, with the original text.
  13295. (org-format-table-table-html-using-table-generate-source olines)))))
  13296. (defun org-format-org-table-html (lines)
  13297. "Format a table into HTML."
  13298. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  13299. (setq lines (nreverse lines))
  13300. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  13301. (setq lines (nreverse lines))
  13302. (when org-export-table-remove-special-lines
  13303. ;; Check if the table has a marking column. If yes remove the
  13304. ;; column and the special lines
  13305. (let* ((special
  13306. (not
  13307. (memq nil
  13308. (mapcar
  13309. (lambda (x)
  13310. (or (string-match "^[ \t]*|-" x)
  13311. (string-match "^[ \t]*| *\\([#!$*_^ ]\\) *|" x)))
  13312. lines)))))
  13313. (if special
  13314. (setq lines
  13315. (delq nil
  13316. (mapcar
  13317. (lambda (x)
  13318. (if (string-match "^[ \t]*| *[!_^] *|" x)
  13319. nil ; ignore this line
  13320. (and (or (string-match "^[ \t]*|-+\\+" x)
  13321. (string-match "^[ \t]*|[^|]*|" x))
  13322. (replace-match "|" t t x))))
  13323. lines))))))
  13324. (let ((head (and org-export-highlight-first-table-line
  13325. (delq nil (mapcar
  13326. (lambda (x) (string-match "^[ \t]*|-" x))
  13327. (cdr lines)))))
  13328. line fields html)
  13329. (setq html (concat org-export-html-table-tag "\n"))
  13330. (while (setq line (pop lines))
  13331. (catch 'next-line
  13332. (if (string-match "^[ \t]*|-" line)
  13333. (progn
  13334. (setq head nil) ;; head ends here, first time around
  13335. ;; ignore this line
  13336. (throw 'next-line t)))
  13337. ;; Break the line into fields
  13338. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  13339. (setq html (concat
  13340. html
  13341. "<tr>"
  13342. (mapconcat (lambda (x)
  13343. (if head
  13344. (concat "<th>" x "</th>")
  13345. (concat "<td>" x "</td>")))
  13346. fields "")
  13347. "</tr>\n"))))
  13348. (setq html (concat html "</table>\n"))
  13349. html))
  13350. (defun org-fake-empty-table-line (line)
  13351. "Replace everything except \"|\" with spaces."
  13352. (let ((i (length line))
  13353. (newstr (copy-sequence line)))
  13354. (while (> i 0)
  13355. (setq i (1- i))
  13356. (if (not (eq (aref newstr i) ?|))
  13357. (aset newstr i ?\ )))
  13358. newstr))
  13359. (defun org-format-table-table-html (lines)
  13360. "Format a table generated by table.el into HTML.
  13361. This conversion does *not* use `table-generate-source' from table.el.
  13362. This has the advantage that Org-mode's HTML conversions can be used.
  13363. But it has the disadvantage, that no cell- or row-spanning is allowed."
  13364. (let (line field-buffer
  13365. (head org-export-highlight-first-table-line)
  13366. fields html empty)
  13367. (setq html (concat org-export-html-table-tag "\n"))
  13368. (while (setq line (pop lines))
  13369. (setq empty "&nbsp;")
  13370. (catch 'next-line
  13371. (if (string-match "^[ \t]*\\+-" line)
  13372. (progn
  13373. (if field-buffer
  13374. (progn
  13375. (setq html (concat
  13376. html
  13377. "<tr>"
  13378. (mapconcat
  13379. (lambda (x)
  13380. (if (equal x "") (setq x empty))
  13381. (if head
  13382. (concat "<th>" x "</th>\n")
  13383. (concat "<td>" x "</td>\n")))
  13384. field-buffer "\n")
  13385. "</tr>\n"))
  13386. (setq head nil)
  13387. (setq field-buffer nil)))
  13388. ;; Ignore this line
  13389. (throw 'next-line t)))
  13390. ;; Break the line into fields and store the fields
  13391. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  13392. (if field-buffer
  13393. (setq field-buffer (mapcar
  13394. (lambda (x)
  13395. (concat x "<br/>" (pop fields)))
  13396. field-buffer))
  13397. (setq field-buffer fields))))
  13398. (setq html (concat html "</table>\n"))
  13399. html))
  13400. (defun org-format-table-table-html-using-table-generate-source (lines)
  13401. "Format a table into html, using `table-generate-source' from table.el.
  13402. This has the advantage that cell- or row-spanning is allowed.
  13403. But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
  13404. (require 'table)
  13405. (with-current-buffer (get-buffer-create " org-tmp1 ")
  13406. (erase-buffer)
  13407. (insert (mapconcat 'identity lines "\n"))
  13408. (goto-char (point-min))
  13409. (if (not (re-search-forward "|[^+]" nil t))
  13410. (error "Error processing table"))
  13411. (table-recognize-table)
  13412. (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
  13413. (table-generate-source 'html " org-tmp2 ")
  13414. (set-buffer " org-tmp2 ")
  13415. (buffer-substring (point-min) (point-max))))
  13416. (defun org-html-handle-time-stamps (s)
  13417. "Format time stamps in string S, or remove them."
  13418. (catch 'exit
  13419. (let (r b)
  13420. (while (string-match org-maybe-keyword-time-regexp s)
  13421. ;; FIXME: is it good to never export CLOCK, or do we need control?
  13422. (if (and (match-end 1) (equal (match-string 1 s) org-clock-string))
  13423. (throw 'exit ""))
  13424. (or b (setq b (substring s 0 (match-beginning 0))))
  13425. (if (not org-export-with-timestamps)
  13426. (setq r (concat r (substring s 0 (match-beginning 0)))
  13427. s (substring s (match-end 0)))
  13428. (setq r (concat
  13429. r (substring s 0 (match-beginning 0))
  13430. (if (match-end 1)
  13431. (format "@<span class=\"timestamp-kwd\">%s @</span>"
  13432. (match-string 1 s)))
  13433. (format " @<span class=\"timestamp\">%s@</span>"
  13434. (substring (match-string 3 s) 1 -1)))
  13435. s (substring s (match-end 0)))))
  13436. ;; Line break if line started and ended with time stamp stuff
  13437. (if (not r)
  13438. s
  13439. (setq r (concat r s))
  13440. (unless (string-match "\\S-" (concat b s))
  13441. (setq r (concat r "@<br/>")))
  13442. r))))
  13443. (defun org-html-protect (s)
  13444. ;; convert & to &amp;, < to &lt; and > to &gt;
  13445. (let ((start 0))
  13446. (while (string-match "&" s start)
  13447. (setq s (replace-match "&amp;" t t s)
  13448. start (1+ (match-beginning 0))))
  13449. (while (string-match "<" s)
  13450. (setq s (replace-match "&lt;" t t s)))
  13451. (while (string-match ">" s)
  13452. (setq s (replace-match "&gt;" t t s))))
  13453. s)
  13454. (defun org-export-cleanup-toc-line (s)
  13455. "Remove tags and time staps from lines going into the toc."
  13456. (if (string-match " +:[a-zA-Z0-9_@:]+: *$" s)
  13457. (setq s (replace-match "" t t s)))
  13458. (when org-export-remove-timestamps-from-toc
  13459. (while (string-match org-maybe-keyword-time-regexp s)
  13460. (setq s (replace-match "" t t s))))
  13461. s)
  13462. (defun org-html-expand (string)
  13463. "Prepare STRING for HTML export. Applies all active conversions.
  13464. If there are links in the string, don't modify these."
  13465. (let* (m s l res)
  13466. (while (setq m (string-match org-bracket-link-regexp string))
  13467. (setq s (substring string 0 m)
  13468. l (match-string 0 string)
  13469. string (substring string (match-end 0)))
  13470. (push (org-html-do-expand s) res)
  13471. (push l res))
  13472. (push (org-html-do-expand string) res)
  13473. (apply 'concat (nreverse res))))
  13474. (defun org-html-do-expand (s)
  13475. "Apply all active conversions to translate special ASCII to HTML."
  13476. (setq s (org-html-protect s))
  13477. (if org-export-html-expand
  13478. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  13479. (setq s (replace-match "<\\1>" t nil s))))
  13480. (if org-export-with-emphasize
  13481. (setq s (org-export-html-convert-emphasize s)))
  13482. (if org-export-with-sub-superscripts
  13483. (setq s (org-export-html-convert-sub-super s)))
  13484. (if org-export-with-TeX-macros
  13485. (let ((start 0) wd ass)
  13486. (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)" s start))
  13487. (setq wd (match-string 1 s))
  13488. (if (setq ass (assoc wd org-html-entities))
  13489. (setq s (replace-match (or (cdr ass)
  13490. (concat "&" (car ass) ";"))
  13491. t t s))
  13492. (setq start (+ start (length wd)))))))
  13493. s)
  13494. (defun org-create-multibrace-regexp (left right n)
  13495. "Create a regular expression which will match a balanced sexp.
  13496. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  13497. as single character strings.
  13498. The regexp returned will match the entire expression including the
  13499. delimiters. It will also define a single group which contains the
  13500. match except for the outermost delimiters. The maximum depth of
  13501. stacked delimiters is N. Escaping delimiters is not possible."
  13502. (let* ((nothing (concat "[^" "\\" left "\\" right "]*?"))
  13503. (or "\\|")
  13504. (re nothing)
  13505. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  13506. (while (> n 1)
  13507. (setq n (1- n)
  13508. re (concat re or next)
  13509. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  13510. (concat left "\\(" re "\\)" right)))
  13511. (defvar org-match-substring-regexp
  13512. (concat
  13513. "\\([^\\]\\)\\([_^]\\)\\("
  13514. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  13515. "\\|"
  13516. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  13517. "\\|"
  13518. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  13519. "The regular expression matching a sub- or superscript.")
  13520. (defun org-export-html-convert-sub-super (string)
  13521. "Convert sub- and superscripts in STRING to HTML."
  13522. (let (key c)
  13523. (while (string-match org-match-substring-regexp string)
  13524. (setq key (if (string= (match-string 2 string) "_") "sub" "sup"))
  13525. (setq c (or (match-string 8 string)
  13526. (match-string 6 string)
  13527. (match-string 5 string)))
  13528. (setq string (replace-match
  13529. (concat (match-string 1 string)
  13530. "<" key ">" c "</" key ">")
  13531. t t string)))
  13532. (while (string-match "\\\\\\([_^]\\)" string)
  13533. (setq string (replace-match (match-string 1 string) t t string))))
  13534. string)
  13535. (defun org-export-html-convert-emphasize (string)
  13536. "Apply emphasis."
  13537. (while (string-match org-emph-re string)
  13538. (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)))
  13539. string)
  13540. (defvar org-par-open nil)
  13541. (defun org-open-par ()
  13542. "Insert <p>, but first close previous paragraph if any."
  13543. (org-close-par-maybe)
  13544. (insert "\n<p>")
  13545. (setq org-par-open t))
  13546. (defun org-close-par-maybe ()
  13547. "Close paragraph if there is one open."
  13548. (when org-par-open
  13549. (insert "</p>")
  13550. (setq org-par-open nil)))
  13551. (defun org-close-li ()
  13552. "Close <li> if necessary."
  13553. (org-close-par-maybe)
  13554. (insert "</li>\n"))
  13555. ; (when (save-excursion
  13556. ; (re-search-backward "</?\\(ul\\|ol\\|li\\|[hH][0-9]\\)>" nil t))
  13557. ; (if (member (match-string 0) '("</ul>" "</ol>" "<li>"))
  13558. ; (insert "</li>"))))
  13559. (defun org-html-level-start (level title umax with-toc head-count)
  13560. "Insert a new level in HTML export.
  13561. When TITLE is nil, just close all open levels."
  13562. (org-close-par-maybe)
  13563. (let ((l (1+ (max level umax))))
  13564. (while (<= l org-level-max)
  13565. (if (aref levels-open (1- l))
  13566. (progn
  13567. (org-html-level-close l)
  13568. (aset levels-open (1- l) nil)))
  13569. (setq l (1+ l)))
  13570. (when title
  13571. ;; If title is nil, this means this function is called to close
  13572. ;; all levels, so the rest is done only if title is given
  13573. (when (string-match "\\(:[a-zA-Z0-9_@:]+:\\)[ \t]*$" title)
  13574. (setq title (replace-match
  13575. (if org-export-with-tags
  13576. (save-match-data
  13577. (concat
  13578. "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
  13579. (mapconcat 'identity (org-split-string
  13580. (match-string 1 title) ":")
  13581. "&nbsp;")
  13582. "</span>"))
  13583. "")
  13584. t t title)))
  13585. (if (> level umax)
  13586. (progn
  13587. (if (aref levels-open (1- level))
  13588. (progn
  13589. (org-close-li)
  13590. (insert "<li>" title "<br/>\n"))
  13591. (aset levels-open (1- level) t)
  13592. (org-close-par-maybe)
  13593. (insert "<ul>\n<li>" title "<br/>\n")))
  13594. (if org-export-with-section-numbers
  13595. (setq title (concat (org-section-number level) " " title)))
  13596. (setq level (+ level org-export-html-toplevel-hlevel -1))
  13597. (if with-toc
  13598. (insert (format "\n<h%d><a name=\"sec-%d\">%s</a></h%d>\n"
  13599. level head-count title level))
  13600. (insert (format "\n<h%d>%s</h%d>\n" level title level)))
  13601. (org-open-par)))))
  13602. (defun org-html-level-close (&rest args)
  13603. "Terminate one level in HTML export."
  13604. (org-close-li)
  13605. (insert "</ul>"))
  13606. ;; Variable holding the vector with section numbers
  13607. (defvar org-section-numbers (make-vector org-level-max 0))
  13608. (defun org-init-section-numbers ()
  13609. "Initialize the vector for the section numbers."
  13610. (let* ((level -1)
  13611. (numbers (nreverse (org-split-string "" "\\.")))
  13612. (depth (1- (length org-section-numbers)))
  13613. (i depth) number-string)
  13614. (while (>= i 0)
  13615. (if (> i level)
  13616. (aset org-section-numbers i 0)
  13617. (setq number-string (or (car numbers) "0"))
  13618. (if (string-match "\\`[A-Z]\\'" number-string)
  13619. (aset org-section-numbers i
  13620. (- (string-to-char number-string) ?A -1))
  13621. (aset org-section-numbers i (string-to-number number-string)))
  13622. (pop numbers))
  13623. (setq i (1- i)))))
  13624. (defun org-section-number (&optional level)
  13625. "Return a string with the current section number.
  13626. When LEVEL is non-nil, increase section numbers on that level."
  13627. (let* ((depth (1- (length org-section-numbers))) idx n (string ""))
  13628. (when level
  13629. (when (> level -1)
  13630. (aset org-section-numbers
  13631. level (1+ (aref org-section-numbers level))))
  13632. (setq idx (1+ level))
  13633. (while (<= idx depth)
  13634. (if (not (= idx 1))
  13635. (aset org-section-numbers idx 0))
  13636. (setq idx (1+ idx))))
  13637. (setq idx 0)
  13638. (while (<= idx depth)
  13639. (setq n (aref org-section-numbers idx))
  13640. (setq string (concat string (if (not (string= string "")) "." "")
  13641. (int-to-string n)))
  13642. (setq idx (1+ idx)))
  13643. (save-match-data
  13644. (if (string-match "\\`\\([@0]\\.\\)+" string)
  13645. (setq string (replace-match "" t nil string)))
  13646. (if (string-match "\\(\\.0\\)+\\'" string)
  13647. (setq string (replace-match "" t nil string))))
  13648. string))
  13649. ;;;###autoload
  13650. (defun org-export-icalendar-this-file ()
  13651. "Export current file as an iCalendar file.
  13652. The iCalendar file will be located in the same directory as the Org-mode
  13653. file, but with extension `.ics'."
  13654. (interactive)
  13655. (org-export-icalendar nil buffer-file-name))
  13656. (defun org-export-as-xoxo-insert-into (buffer &rest output)
  13657. (with-current-buffer buffer
  13658. (apply 'insert output)))
  13659. (defun org-export-as-xoxo (&optional buffer)
  13660. "Export the org buffer as XOXO.
  13661. The XOXO buffer is named *xoxo-<source buffer name>*"
  13662. (interactive (list (current-buffer)))
  13663. ;; A quickie abstraction
  13664. ;; Output everything as XOXO
  13665. (with-current-buffer (get-buffer buffer)
  13666. (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
  13667. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  13668. (org-infile-export-plist)))
  13669. (filename (concat (file-name-as-directory
  13670. (org-export-directory :xoxo opt-plist))
  13671. (file-name-sans-extension
  13672. (file-name-nondirectory buffer-file-name))
  13673. ".html"))
  13674. (out (find-file-noselect filename))
  13675. (last-level 1)
  13676. (hanging-li nil))
  13677. ;; Check the output buffer is empty.
  13678. (with-current-buffer out (erase-buffer))
  13679. ;; Kick off the output
  13680. (org-export-as-xoxo-insert-into out "<ol class='xoxo'>\n")
  13681. (while (re-search-forward "^\\(\\*+\\) \\(.+\\)" (point-max) 't)
  13682. (let* ((hd (match-string-no-properties 1))
  13683. (level (length hd))
  13684. (text (concat
  13685. (match-string-no-properties 2)
  13686. (save-excursion
  13687. (goto-char (match-end 0))
  13688. (let ((str ""))
  13689. (catch 'loop
  13690. (while 't
  13691. (forward-line)
  13692. (if (looking-at "^[ \t]\\(.*\\)")
  13693. (setq str (concat str (match-string-no-properties 1)))
  13694. (throw 'loop str)))))))))
  13695. ;; Handle level rendering
  13696. (cond
  13697. ((> level last-level)
  13698. (org-export-as-xoxo-insert-into out "\n<ol>\n"))
  13699. ((< level last-level)
  13700. (dotimes (- (- last-level level) 1)
  13701. (if hanging-li
  13702. (org-export-as-xoxo-insert-into out "</li>\n"))
  13703. (org-export-as-xoxo-insert-into out "</ol>\n"))
  13704. (when hanging-li
  13705. (org-export-as-xoxo-insert-into out "</li>\n")
  13706. (setq hanging-li nil)))
  13707. ((equal level last-level)
  13708. (if hanging-li
  13709. (org-export-as-xoxo-insert-into out "</li>\n")))
  13710. )
  13711. (setq last-level level)
  13712. ;; And output the new li
  13713. (setq hanging-li 't)
  13714. (if (equal ?+ (elt text 0))
  13715. (org-export-as-xoxo-insert-into out "<li class='" (substring text 1) "'>")
  13716. (org-export-as-xoxo-insert-into out "<li>" text))))
  13717. ;; Finally finish off the ol
  13718. (dotimes (- last-level 1)
  13719. (if hanging-li
  13720. (org-export-as-xoxo-insert-into out "</li>\n"))
  13721. (org-export-as-xoxo-insert-into out "</ol>\n"))
  13722. ;; Finish the buffer off and clean it up.
  13723. (switch-to-buffer-other-window out)
  13724. (indent-region (point-min) (point-max) nil)
  13725. (save-buffer)
  13726. (goto-char (point-min))
  13727. )))
  13728. ;;;###autoload
  13729. (defun org-export-icalendar-all-agenda-files ()
  13730. "Export all files in `org-agenda-files' to iCalendar .ics files.
  13731. Each iCalendar file will be located in the same directory as the Org-mode
  13732. file, but with extension `.ics'."
  13733. (interactive)
  13734. (apply 'org-export-icalendar nil (org-agenda-files t)))
  13735. ;;;###autoload
  13736. (defun org-export-icalendar-combine-agenda-files ()
  13737. "Export all files in `org-agenda-files' to a single combined iCalendar file.
  13738. The file is stored under the name `org-combined-agenda-icalendar-file'."
  13739. (interactive)
  13740. (apply 'org-export-icalendar t (org-agenda-files t)))
  13741. (defun org-export-icalendar (combine &rest files)
  13742. "Create iCalendar files for all elements of FILES.
  13743. If COMBINE is non-nil, combine all calendar entries into a single large
  13744. file and store it under the name `org-combined-agenda-icalendar-file'."
  13745. (save-excursion
  13746. (let* ((dir (org-export-directory
  13747. :ical (list :publishing-directory
  13748. org-export-publishing-directory)))
  13749. file ical-file ical-buffer category started org-agenda-new-buffers)
  13750. (when combine
  13751. (setq ical-file
  13752. (if (file-name-absolute-p org-combined-agenda-icalendar-file)
  13753. org-combined-agenda-icalendar-file
  13754. (expand-file-name org-combined-agenda-icalendar-file dir))
  13755. ical-buffer (org-get-agenda-file-buffer ical-file))
  13756. (set-buffer ical-buffer) (erase-buffer))
  13757. (while (setq file (pop files))
  13758. (catch 'nextfile
  13759. (org-check-agenda-file file)
  13760. (set-buffer (org-get-agenda-file-buffer file))
  13761. (unless combine
  13762. (setq ical-file (concat (file-name-as-directory dir)
  13763. (file-name-sans-extension
  13764. (file-name-nondirectory buffer-file-name))
  13765. ".ics"))
  13766. (setq ical-buffer (org-get-agenda-file-buffer ical-file))
  13767. (with-current-buffer ical-buffer (erase-buffer)))
  13768. (setq category (or org-category
  13769. (file-name-sans-extension
  13770. (file-name-nondirectory buffer-file-name))))
  13771. (if (symbolp category) (setq category (symbol-name category)))
  13772. (let ((standard-output ical-buffer))
  13773. (if combine
  13774. (and (not started) (setq started t)
  13775. (org-start-icalendar-file org-icalendar-combined-name))
  13776. (org-start-icalendar-file category))
  13777. (org-print-icalendar-entries combine category)
  13778. (when (or (and combine (not files)) (not combine))
  13779. (org-finish-icalendar-file)
  13780. (set-buffer ical-buffer)
  13781. (save-buffer)
  13782. (run-hooks 'org-after-save-iCalendar-file-hook)))))
  13783. (org-release-buffers org-agenda-new-buffers))))
  13784. (defvar org-after-save-iCalendar-file-hook nil
  13785. "Hook run after an iCalendar file has been saved.
  13786. The iCalendar buffer is still current when this hook is run.
  13787. A good way to use this is to tell a desktop calenndar application to re-read
  13788. the iCalendar file.")
  13789. (defun org-print-icalendar-entries (&optional combine category)
  13790. "Print iCalendar entries for the current Org-mode file to `standard-output'.
  13791. When COMBINE is non nil, add the category to each line."
  13792. (let ((re2 (concat "--?-?\\(" org-ts-regexp "\\)"))
  13793. (dts (org-ical-ts-to-string
  13794. (format-time-string (cdr org-time-stamp-formats) (current-time))
  13795. "DTSTART"))
  13796. hd ts ts2 state (inc t) pos scheduledp deadlinep tmp pri)
  13797. (save-excursion
  13798. (goto-char (point-min))
  13799. (while (re-search-forward org-ts-regexp nil t)
  13800. (setq pos (match-beginning 0)
  13801. ts (match-string 0)
  13802. inc t
  13803. hd (org-get-heading))
  13804. (if (looking-at re2)
  13805. (progn
  13806. (goto-char (match-end 0))
  13807. (setq ts2 (match-string 1) inc nil))
  13808. (setq ts2 ts
  13809. tmp (buffer-substring (max (point-min)
  13810. (- pos org-ds-keyword-length))
  13811. pos)
  13812. deadlinep (string-match org-deadline-regexp tmp)
  13813. scheduledp (string-match org-scheduled-regexp tmp)
  13814. ;; donep (org-entry-is-done-p)
  13815. ))
  13816. (if (or (string-match org-tr-regexp hd)
  13817. (string-match org-ts-regexp hd))
  13818. (setq hd (replace-match "" t t hd)))
  13819. (if combine
  13820. (setq hd (concat hd " (category " category ")")))
  13821. (if deadlinep (setq hd (concat "DL: " hd " This is a deadline")))
  13822. (if scheduledp (setq hd (concat "S: " hd " Scheduled for this date")))
  13823. (princ (format "BEGIN:VEVENT
  13824. %s
  13825. %s
  13826. SUMMARY:%s
  13827. END:VEVENT\n"
  13828. (org-ical-ts-to-string ts "DTSTART")
  13829. (org-ical-ts-to-string ts2 "DTEND" inc)
  13830. hd)))
  13831. (when org-icalendar-include-todo
  13832. (goto-char (point-min))
  13833. (while (re-search-forward org-todo-line-regexp nil t)
  13834. (setq state (match-string 1))
  13835. (unless (equal state org-done-string)
  13836. (setq hd (match-string 3))
  13837. (if (string-match org-priority-regexp hd)
  13838. (setq pri (string-to-char (match-string 2 hd))
  13839. hd (concat (substring hd 0 (match-beginning 1))
  13840. (substring hd (- (match-end 1)))))
  13841. (setq pri org-default-priority))
  13842. (setq pri (floor (1+ (* 8. (/ (float (- org-lowest-priority pri))
  13843. (- org-lowest-priority ?A))))))
  13844. (princ (format "BEGIN:VTODO
  13845. %s
  13846. SUMMARY:%s
  13847. SEQUENCE:1
  13848. PRIORITY:%d
  13849. END:VTODO\n"
  13850. dts hd pri))))))))
  13851. (defun org-start-icalendar-file (name)
  13852. "Start an iCalendar file by inserting the header."
  13853. (let ((user user-full-name)
  13854. (name (or name "unknown"))
  13855. (timezone (cadr (current-time-zone))))
  13856. (princ
  13857. (format "BEGIN:VCALENDAR
  13858. VERSION:2.0
  13859. X-WR-CALNAME:%s
  13860. PRODID:-//%s//Emacs with Org-mode//EN
  13861. X-WR-TIMEZONE:%s
  13862. CALSCALE:GREGORIAN\n" name user timezone))))
  13863. (defun org-finish-icalendar-file ()
  13864. "Finish an iCalendar file by inserting the END statement."
  13865. (princ "END:VCALENDAR\n"))
  13866. (defun org-ical-ts-to-string (s keyword &optional inc)
  13867. "Take a time string S and convert it to iCalendar format.
  13868. KEYWORD is added in front, to make a complete line like DTSTART....
  13869. When INC is non-nil, increase the hour by two (if time string contains
  13870. a time), or the day by one (if it does not contain a time)."
  13871. (let ((t1 (org-parse-time-string s 'nodefault))
  13872. t2 fmt have-time time)
  13873. (if (and (car t1) (nth 1 t1) (nth 2 t1))
  13874. (setq t2 t1 have-time t)
  13875. (setq t2 (org-parse-time-string s)))
  13876. (let ((s (car t2)) (mi (nth 1 t2)) (h (nth 2 t2))
  13877. (d (nth 3 t2)) (m (nth 4 t2)) (y (nth 5 t2)))
  13878. (when inc
  13879. (if have-time (setq h (+ 2 h)) (setq d (1+ d))))
  13880. (setq time (encode-time s mi h d m y)))
  13881. (setq fmt (if have-time ":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
  13882. (concat keyword (format-time-string fmt time))))
  13883. ;;; LaTeX stuff
  13884. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  13885. "Keymap for the minor `org-cdlatex-mode'.")
  13886. (define-key org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  13887. (define-key org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  13888. (define-key org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  13889. (define-key org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  13890. (define-key org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  13891. (defvar org-cdlatex-texmathp-advice-is-done nil
  13892. "Flag remembering if we have applied the advice to texmathp already.")
  13893. (define-minor-mode org-cdlatex-mode
  13894. "Toggle the minor `org-cdlatex-mode'.
  13895. This mode supports entering LaTeX environment and math in LaTeX fragments
  13896. in Org-mode.
  13897. \\{org-cdlatex-mode-map}"
  13898. nil " OCDL" nil
  13899. (when org-cdlatex-mode (require 'cdlatex))
  13900. (unless org-cdlatex-texmathp-advice-is-done
  13901. (setq org-cdlatex-texmathp-advice-is-done t)
  13902. (defadvice texmathp (around org-math-always-on activate)
  13903. "Always return t in org-mode buffers.
  13904. This is because we want to insert math symbols without dollars even outside
  13905. the LaTeX math segments. If Orgmode thinks that point is actually inside
  13906. en embedded LaTeX fragement, let texmathp do its job.
  13907. \\[org-cdlatex-mode-map]"
  13908. (interactive)
  13909. (let (p)
  13910. (cond
  13911. ((not (org-mode-p)) ad-do-it)
  13912. ((eq this-command 'cdlatex-math-symbol)
  13913. (setq ad-return-value t
  13914. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  13915. (t
  13916. (let ((p (org-inside-LaTeX-fragment-p)))
  13917. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  13918. (setq ad-return-value t
  13919. texmathp-why '("Org-mode embedded math" . 0))
  13920. (if p ad-do-it)))))))))
  13921. (defun turn-on-org-cdlatex ()
  13922. "Unconditionally turn on `org-cdlatex-mode'."
  13923. (org-cdlatex-mode 1))
  13924. (defun org-inside-LaTeX-fragment-p ()
  13925. "Test if point is inside a LaTeX fragment.
  13926. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  13927. sequence appearing also before point.
  13928. Even though the matchers for math are configurable, this function assumes
  13929. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  13930. delimiters are skipped when they have been removed by customization.
  13931. The return value is nil, or a cons cell with the delimiter and
  13932. and the position of this delimiter.
  13933. This function does a reasonably good job, but can locally be fooled by
  13934. for example currency specifications. For example it will assume being in
  13935. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  13936. fragments that are properly closed, but during editing, we have to live
  13937. with the uncertainty caused by missing closing delimiters. This function
  13938. looks only before point, not after."
  13939. (catch 'exit
  13940. (let ((pos (point))
  13941. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  13942. (lim (progn
  13943. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  13944. (point)))
  13945. dd-on str (start 0) m re)
  13946. (goto-char pos)
  13947. (when dodollar
  13948. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  13949. re (nth 1 (assoc "$" org-latex-regexps)))
  13950. (while (string-match re str start)
  13951. (cond
  13952. ((= (match-end 0) (length str))
  13953. (throw 'exit (cons "$" (+ lim (match-beginning 0)))))
  13954. ((= (match-end 0) (- (length str) 5))
  13955. (throw 'exit nil))
  13956. (t (setq start (match-end 0))))))
  13957. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  13958. (goto-char pos)
  13959. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  13960. (and (match-beginning 2) (throw 'exit nil))
  13961. ;; count $$
  13962. (while (re-search-backward "\\$\\$" lim t)
  13963. (setq dd-on (not dd-on)))
  13964. (goto-char pos)
  13965. (if dd-on (cons "$$" m))))))
  13966. (defun org-try-cdlatex-tab ()
  13967. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  13968. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  13969. - inside a LaTeX fragment, or
  13970. - after the first word in a line, where an abbreviation expansion could
  13971. insert a LaTeX environment."
  13972. ;; FIXME: This may still need refinement.
  13973. (when org-cdlatex-mode
  13974. (cond
  13975. ((save-excursion
  13976. (skip-chars-backward "a-zA-Z0-9*")
  13977. (skip-chars-backward " \t")
  13978. (bolp))
  13979. (cdlatex-tab) t)
  13980. ((org-inside-LaTeX-fragment-p)
  13981. (cdlatex-tab) t)
  13982. (t nil))))
  13983. (defun org-cdlatex-underscore-caret (&optional arg)
  13984. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  13985. Revert to the normal definition outside of these fragments."
  13986. (interactive "P")
  13987. (if (org-inside-LaTeX-fragment-p)
  13988. (call-interactively 'cdlatex-sub-superscript)
  13989. (let (org-cdlatex-mode)
  13990. (call-interactively (key-binding (vector last-input-event))))))
  13991. (defun org-cdlatex-math-modify (&optional arg)
  13992. "Execute `cdlatex-math-modify' in LaTeX fragments.
  13993. Revert to the normal definition outside of these fragments."
  13994. (interactive "P")
  13995. (if (org-inside-LaTeX-fragment-p)
  13996. (call-interactively 'cdlatex-math-modify)
  13997. (let (org-cdlatex-mode)
  13998. (call-interactively (key-binding (vector last-input-event))))))
  13999. (defvar org-latex-fragment-image-overlays nil
  14000. "List of overlays carrying the images of latex fragments.")
  14001. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  14002. (defun org-remove-latex-fragment-image-overlays ()
  14003. "Remove all overlays with LaTeX fragment images in current buffer."
  14004. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  14005. (setq org-latex-fragment-image-overlays nil))
  14006. (defun org-preview-latex-fragment (&optional subtree)
  14007. "Preview the LaTeX fragment at point, or all locally or globally.
  14008. If the cursor is in a LaTeX fragment, create the image and overlay
  14009. it over the source code. If there is no fragment at point, display
  14010. all fragments in the current text, from one headline to the next. With
  14011. prefix SUBTREE, display all fragments in the current subtree. With a
  14012. double prefix `C-u C-u', or when the cursor is before the first headline,
  14013. display all fragments in the buffer.
  14014. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  14015. (interactive "P")
  14016. (org-remove-latex-fragment-image-overlays)
  14017. (save-excursion
  14018. (save-restriction
  14019. (let (beg end at msg)
  14020. (cond
  14021. ((or (equal subtree '(16))
  14022. (not (save-excursion
  14023. (re-search-backward (concat "^" outline-regexp) nil t))))
  14024. (setq beg (point-min) end (point-max)
  14025. msg "Creating images for buffer...%s"))
  14026. ((equal subtree '(4))
  14027. (org-back-to-heading)
  14028. (setq beg (point) end (org-end-of-subtree)
  14029. msg "Creating images for subtree...%s"))
  14030. (t
  14031. (if (setq at (org-inside-LaTeX-fragment-p))
  14032. (goto-char (max (point-min) (- (cdr at) 2)))
  14033. (org-back-to-heading))
  14034. (setq beg (point) end (progn (outline-next-heading) (point))
  14035. msg (if at "Creating image...%s"
  14036. "Creating images for entry...%s"))))
  14037. (message msg "")
  14038. (narrow-to-region beg end)
  14039. (org-format-latex
  14040. (concat "ltxpng/" (file-name-sans-extension
  14041. (file-name-nondirectory
  14042. buffer-file-name)))
  14043. default-directory 'overlays msg at)
  14044. (message msg "done. Use `C-c C-c' to remove images.")))))
  14045. (defvar org-latex-regexps
  14046. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  14047. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  14048. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  14049. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  14050. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  14051. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  14052. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  14053. "Regular expressions for matching embedded LaTeX.")
  14054. (defun org-format-latex (prefix &optional dir overlays msg at)
  14055. "Replace LaTeX fragments with links to an image, and produce images."
  14056. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  14057. (let* ((prefixnodir (file-name-nondirectory prefix))
  14058. (absprefix (expand-file-name prefix dir))
  14059. (todir (file-name-directory absprefix))
  14060. (opt org-format-latex-options)
  14061. (matchers (plist-get opt :matchers))
  14062. (re-list org-latex-regexps)
  14063. (cnt 0) txt link beg end re e oldfiles
  14064. m n block linkfile movefile ov)
  14065. ;; Make sure the directory exists
  14066. (or (file-directory-p todir) (make-directory todir))
  14067. ;; Check if there are old images files with this prefix, and remove them
  14068. (setq oldfiles (directory-files
  14069. todir 'full
  14070. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$")))
  14071. (while oldfiles (delete-file (pop oldfiles)))
  14072. ;; Check the different regular expressions
  14073. (while (setq e (pop re-list))
  14074. (setq m (car e) re (nth 1 e) n (nth 2 e)
  14075. block (if (nth 3 e) "\n\n" ""))
  14076. (when (member m matchers)
  14077. (goto-char (point-min))
  14078. (while (re-search-forward re nil t)
  14079. (when (or (not at) (equal (cdr at) (match-beginning n)))
  14080. (setq txt (match-string n)
  14081. beg (match-beginning n) end (match-end n)
  14082. cnt (1+ cnt)
  14083. linkfile (format "%s_%04d.png" prefix cnt)
  14084. movefile (format "%s_%04d.png" absprefix cnt)
  14085. link (concat block "[[file:" linkfile "]]" block))
  14086. (if msg (message msg cnt))
  14087. (goto-char beg)
  14088. (org-create-formula-image
  14089. txt movefile opt)
  14090. (if overlays
  14091. (progn
  14092. (setq ov (org-make-overlay beg end))
  14093. (if (featurep 'xemacs)
  14094. (progn
  14095. (org-overlay-put ov 'invisible t)
  14096. (org-overlay-put
  14097. ov 'end-glyph
  14098. (make-glyph (vector 'png :file movefile))))
  14099. (org-overlay-put
  14100. ov 'display
  14101. (list 'image :type 'png :file movefile :ascent 'center)))
  14102. (push ov org-latex-fragment-image-overlays)
  14103. (goto-char end))
  14104. (delete-region beg end)
  14105. (insert link))))))))
  14106. ;; This function borrows from Ganesh Swami's latex2png.el
  14107. (defun org-create-formula-image (string tofile options)
  14108. (let* ((tmpdir (if (featurep 'xemacs)
  14109. (temp-directory)
  14110. temporary-file-directory))
  14111. (texfilebase (make-temp-name
  14112. (expand-file-name "orgtex" tmpdir)))
  14113. ;(texfilebase (make-temp-file "orgtex"))
  14114. ; (dummy (delete-file texfilebase))
  14115. (texfile (concat texfilebase ".tex"))
  14116. (dvifile (concat texfilebase ".dvi"))
  14117. (pngfile (concat texfilebase ".png"))
  14118. (scale (number-to-string (* 1000 (or (plist-get options :scale) 1.0))))
  14119. (fg (or (plist-get options :foreground) "Black"))
  14120. (bg (or (plist-get options :background) "Transparent")))
  14121. (with-temp-file texfile
  14122. (insert "\\documentclass{article}
  14123. \\usepackage{fullpage}
  14124. \\usepackage{amssymb}
  14125. \\usepackage[usenames]{color}
  14126. \\usepackage{amsmath}
  14127. \\usepackage{latexsym}
  14128. \\usepackage[mathscr]{eucal}
  14129. \\pagestyle{empty}
  14130. \\begin{document}\n" string "\n\\end{document}\n"))
  14131. (let ((dir default-directory))
  14132. (condition-case nil
  14133. (progn
  14134. (cd tmpdir)
  14135. (call-process "latex" nil nil nil texfile))
  14136. (error nil))
  14137. (cd dir))
  14138. (if (not (file-exists-p dvifile))
  14139. (progn (message "Failed to create dvi file from %s" texfile) nil)
  14140. (call-process "dvipng" nil nil nil
  14141. "-E" "-fg" fg "-bg" bg
  14142. "-x" scale "-y" scale "-T" "tight"
  14143. "-o" pngfile
  14144. dvifile)
  14145. (if (not (file-exists-p pngfile))
  14146. (progn (message "Failed to create png file from %s" texfile) nil)
  14147. ;; Use the requested file name and clean up
  14148. (copy-file pngfile tofile 'replace)
  14149. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  14150. (delete-file (concat texfilebase e)))
  14151. pngfile))))
  14152. ;;; Key bindings
  14153. ;; - Bindings in Org-mode map are currently
  14154. ;; 0123456789abcdefghijklmnopqrstuvwxyz!?@#$%^&-+*/=()_{}[]:;"|,.<>~`'\t the alphabet
  14155. ;; abcd fgh j lmnopqrstuvwxyz!? #$ ^ -+*/= [] ; |,.<>~ '\t necessary bindings
  14156. ;; e (?) useful from outline-mode
  14157. ;; i k @ expendable from outline-mode
  14158. ;; 0123456789 % & ()_{} " ` free
  14159. ;; Make `C-c C-x' a prefix key
  14160. (define-key org-mode-map "\C-c\C-x" (make-sparse-keymap))
  14161. ;; TAB key with modifiers
  14162. (define-key org-mode-map "\C-i" 'org-cycle)
  14163. (define-key org-mode-map [(tab)] 'org-cycle)
  14164. (define-key org-mode-map [(meta tab)] 'org-complete)
  14165. (define-key org-mode-map "\M-\C-i" 'org-complete) ; for tty emacs
  14166. ;; The following line is necessary under Suse GNU/Linux
  14167. (unless (featurep 'xemacs)
  14168. (define-key org-mode-map [S-iso-lefttab] 'org-shifttab))
  14169. (define-key org-mode-map [(shift tab)] 'org-shifttab)
  14170. (define-key org-mode-map (org-key 'S-return) 'org-table-copy-down)
  14171. (define-key org-mode-map "\C-c\C-xc" 'org-table-copy-down) ; tty
  14172. (define-key org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  14173. (define-key org-mode-map "\C-c\C-xM" 'org-insert-todo-heading) ; tty
  14174. (define-key org-mode-map [(meta return)] 'org-meta-return)
  14175. (define-key org-mode-map "\C-c\C-xm" 'org-meta-return) ; tty emacs
  14176. (define-key org-mode-map [?\e (return)] 'org-meta-return) ; tty emacs
  14177. ;; Cursor keys with modifiers
  14178. (define-key org-mode-map [(meta left)] 'org-metaleft)
  14179. (define-key org-mode-map [?\e (left)] 'org-metaleft) ; for tty emacs
  14180. (define-key org-mode-map "\C-c\C-xl" 'org-metaleft) ; for tty emacs
  14181. (define-key org-mode-map [(meta right)] 'org-metaright)
  14182. (define-key org-mode-map [?\e (right)] 'org-metaright) ; for tty emacs
  14183. (define-key org-mode-map "\C-c\C-xr" 'org-metaright) ; for tty emacs
  14184. (define-key org-mode-map [(meta up)] 'org-metaup)
  14185. (define-key org-mode-map [?\e (up)] 'org-metaup) ; for tty emacs
  14186. (define-key org-mode-map "\C-c\C-xu" 'org-metaup) ; for tty emacs
  14187. (define-key org-mode-map [(meta down)] 'org-metadown)
  14188. (define-key org-mode-map [?\e (down)] 'org-metadown) ; for tty emacs
  14189. (define-key org-mode-map "\C-c\C-xd" 'org-metadown) ; for tty emacs
  14190. (define-key org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  14191. (define-key org-mode-map "\C-c\C-xL" 'org-shiftmetaleft) ; tty
  14192. (define-key org-mode-map [(meta shift right)] 'org-shiftmetaright)
  14193. (define-key org-mode-map "\C-c\C-xR" 'org-shiftmetaright) ; tty
  14194. (define-key org-mode-map [(meta shift up)] 'org-shiftmetaup)
  14195. (define-key org-mode-map "\C-c\C-xU" 'org-shiftmetaup) ; tty
  14196. (define-key org-mode-map [(meta shift down)] 'org-shiftmetadown)
  14197. (define-key org-mode-map "\C-c\C-xD" 'org-shiftmetadown) ; tty
  14198. (define-key org-mode-map (org-key 'S-up) 'org-shiftup)
  14199. (define-key org-mode-map [?\C-c ?\C-x (up)] 'org-shiftup)
  14200. (define-key org-mode-map (org-key 'S-down) 'org-shiftdown)
  14201. (define-key org-mode-map [?\C-c ?\C-x (down)] 'org-shiftdown)
  14202. (define-key org-mode-map (org-key 'S-left) 'org-shiftleft)
  14203. (define-key org-mode-map [?\C-c ?\C-x (left)] 'org-shiftleft)
  14204. (define-key org-mode-map (org-key 'S-right) 'org-shiftright)
  14205. (define-key org-mode-map [?\C-c ?\C-x (right)] 'org-shiftright)
  14206. ;; All the other keys
  14207. (define-key org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  14208. (define-key org-mode-map "\C-xns" 'org-narrow-to-subtree)
  14209. (define-key org-mode-map "\C-c$" 'org-archive-subtree)
  14210. (define-key org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  14211. (define-key org-mode-map "\C-c\C-j" 'org-goto)
  14212. (define-key org-mode-map "\C-c\C-t" 'org-todo)
  14213. (define-key org-mode-map "\C-c\C-s" 'org-schedule)
  14214. (define-key org-mode-map "\C-c\C-d" 'org-deadline)
  14215. (define-key org-mode-map "\C-c;" 'org-toggle-comment)
  14216. (define-key org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  14217. (define-key org-mode-map "\C-c\C-w" 'org-check-deadlines)
  14218. (define-key org-mode-map "\C-c/" 'org-occur) ; Minor-mode reserved
  14219. (define-key org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  14220. (define-key org-mode-map "\C-c\C-m" 'org-insert-heading)
  14221. (define-key org-mode-map "\M-\C-m" 'org-insert-heading)
  14222. (define-key org-mode-map "\C-c\C-l" 'org-insert-link)
  14223. (define-key org-mode-map "\C-c\C-o" 'org-open-at-point)
  14224. (define-key org-mode-map "\C-c%" 'org-mark-ring-push)
  14225. (define-key org-mode-map "\C-c&" 'org-mark-ring-goto)
  14226. (define-key org-mode-map "\C-c\C-z" 'org-time-stamp) ; Alternative binding
  14227. (define-key org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  14228. (define-key org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  14229. (define-key org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  14230. (define-key org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  14231. (define-key org-mode-map "\C-c>" 'org-goto-calendar)
  14232. (define-key org-mode-map "\C-c<" 'org-date-from-calendar)
  14233. (define-key org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  14234. (define-key org-mode-map "\C-c[" 'org-agenda-file-to-front)
  14235. (define-key org-mode-map "\C-c]" 'org-remove-file)
  14236. (define-key org-mode-map "\C-c\C-r" 'org-timeline)
  14237. (define-key org-mode-map "\C-c-" 'org-table-insert-hline)
  14238. (define-key org-mode-map "\C-c^" 'org-table-sort-lines)
  14239. (define-key org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  14240. (define-key org-mode-map "\C-m" 'org-return)
  14241. (define-key org-mode-map "\C-c?" 'org-table-current-column)
  14242. (define-key org-mode-map "\C-c " 'org-table-blank-field)
  14243. (define-key org-mode-map "\C-c+" 'org-table-sum)
  14244. (define-key org-mode-map "\C-c=" 'org-table-eval-formula)
  14245. (define-key org-mode-map "\C-c'" 'org-table-edit-formulas)
  14246. (define-key org-mode-map "\C-c`" 'org-table-edit-field)
  14247. (define-key org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  14248. (define-key org-mode-map "\C-c*" 'org-table-recalculate)
  14249. (define-key org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  14250. (define-key org-mode-map "\C-c~" 'org-table-create-with-table.el)
  14251. (define-key org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  14252. (define-key org-mode-map "\C-c\C-e" 'org-export)
  14253. (define-key org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  14254. (define-key org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  14255. (define-key org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  14256. (define-key org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  14257. (define-key org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  14258. (define-key org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  14259. (define-key org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  14260. (define-key org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  14261. (define-key org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  14262. (define-key org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  14263. (define-key org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  14264. (define-key org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  14265. (define-key org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  14266. (when (featurep 'xemacs)
  14267. (define-key org-mode-map 'button3 'popup-mode-menu))
  14268. (defsubst org-table-p () (org-at-table-p))
  14269. (defun org-self-insert-command (N)
  14270. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  14271. If the cursor is in a table looking at whitespace, the whitespace is
  14272. overwritten, and the table is not marked as requiring realignment."
  14273. (interactive "p")
  14274. (if (and (org-table-p)
  14275. (progn
  14276. ;; check if we blank the field, and if that triggers align
  14277. (and org-table-auto-blank-field
  14278. (member last-command
  14279. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  14280. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  14281. ;; got extra space, this field does not determine column width
  14282. (let (org-table-may-need-update) (org-table-blank-field))
  14283. ;; no extra space, this field may determine column width
  14284. (org-table-blank-field)))
  14285. t)
  14286. (eq N 1)
  14287. (looking-at "[^|\n]* |"))
  14288. (let (org-table-may-need-update)
  14289. (goto-char (1- (match-end 0)))
  14290. (delete-backward-char 1)
  14291. (goto-char (match-beginning 0))
  14292. (self-insert-command N))
  14293. (setq org-table-may-need-update t)
  14294. (self-insert-command N)))
  14295. (defun org-delete-backward-char (N)
  14296. "Like `delete-backward-char', insert whitespace at field end in tables.
  14297. When deleting backwards, in tables this function will insert whitespace in
  14298. front of the next \"|\" separator, to keep the table aligned. The table will
  14299. still be marked for re-alignment if the field did fill the entire column,
  14300. because, in this case the deletion might narrow the column."
  14301. (interactive "p")
  14302. (if (and (org-table-p)
  14303. (eq N 1)
  14304. (string-match "|" (buffer-substring (point-at-bol) (point)))
  14305. (looking-at ".*?|"))
  14306. (let ((pos (point))
  14307. (noalign (looking-at "[^|\n\r]* |"))
  14308. (c org-table-may-need-update))
  14309. (backward-delete-char N)
  14310. (skip-chars-forward "^|")
  14311. (insert " ")
  14312. (goto-char (1- pos))
  14313. ;; noalign: if there were two spaces at the end, this field
  14314. ;; does not determine the width of the column.
  14315. (if noalign (setq org-table-may-need-update c)))
  14316. (backward-delete-char N)))
  14317. (defun org-delete-char (N)
  14318. "Like `delete-char', but insert whitespace at field end in tables.
  14319. When deleting characters, in tables this function will insert whitespace in
  14320. front of the next \"|\" separator, to keep the table aligned. The table will
  14321. still be marked for re-alignment if the field did fill the entire column,
  14322. because, in this case the deletion might narrow the column."
  14323. (interactive "p")
  14324. (if (and (org-table-p)
  14325. (not (bolp))
  14326. (not (= (char-after) ?|))
  14327. (eq N 1))
  14328. (if (looking-at ".*?|")
  14329. (let ((pos (point))
  14330. (noalign (looking-at "[^|\n\r]* |"))
  14331. (c org-table-may-need-update))
  14332. (replace-match (concat
  14333. (substring (match-string 0) 1 -1)
  14334. " |"))
  14335. (goto-char pos)
  14336. ;; noalign: if there were two spaces at the end, this field
  14337. ;; does not determine the width of the column.
  14338. (if noalign (setq org-table-may-need-update c)))
  14339. (delete-char N))
  14340. (delete-char N)))
  14341. ;; How to do this: Measure non-white length of current string
  14342. ;; If equal to column width, we should realign.
  14343. (defun org-remap (map &rest commands)
  14344. "In MAP, remap the functions given in COMMANDS.
  14345. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  14346. (let (new old)
  14347. (while commands
  14348. (setq old (pop commands) new (pop commands))
  14349. (if (fboundp 'command-remapping)
  14350. (define-key map (vector 'remap old) new)
  14351. (substitute-key-definition old new map global-map)))))
  14352. (when (eq org-enable-table-editor 'optimized)
  14353. ;; If the user wants maximum table support, we need to hijack
  14354. ;; some standard editing functions
  14355. (org-remap org-mode-map
  14356. 'self-insert-command 'org-self-insert-command
  14357. 'delete-char 'org-delete-char
  14358. 'delete-backward-char 'org-delete-backward-char)
  14359. (define-key org-mode-map "|" 'org-force-self-insert))
  14360. (defun org-shiftcursor-error ()
  14361. "Throw an error because Shift-Cursor command was applied in wrong context."
  14362. (error "This command is active in special context like tables, headlines or timestamps"))
  14363. (defun org-shifttab (&optional arg)
  14364. "Global visibility cycling or move to previous table field.
  14365. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  14366. on context.
  14367. See the individual commands for more information."
  14368. (interactive "P")
  14369. (cond
  14370. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  14371. (t (call-interactively 'org-global-cycle))))
  14372. (defun org-shiftmetaleft ()
  14373. "Promote subtree or delete table column.
  14374. Calls `org-promote-subtree' or `org-table-delete-column', depending on context.
  14375. See the individual commands for more information."
  14376. (interactive)
  14377. (cond
  14378. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  14379. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  14380. ((org-at-item-p) (call-interactively 'org-outdent-item))
  14381. (t (org-shiftcursor-error))))
  14382. (defun org-shiftmetaright ()
  14383. "Demote subtree or insert table column.
  14384. Calls `org-demote-subtree' or `org-table-insert-column', depending on context.
  14385. See the individual commands for more information."
  14386. (interactive)
  14387. (cond
  14388. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  14389. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  14390. ((org-at-item-p) (call-interactively 'org-indent-item))
  14391. (t (org-shiftcursor-error))))
  14392. (defun org-shiftmetaup (&optional arg)
  14393. "Move subtree up or kill table row.
  14394. Calls `org-move-subtree-up' or `org-table-kill-row' or
  14395. `org-move-item-up' depending on context. See the individual commands
  14396. for more information."
  14397. (interactive "P")
  14398. (cond
  14399. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  14400. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  14401. ((org-at-item-p) (call-interactively 'org-move-item-up))
  14402. (t (org-shiftcursor-error))))
  14403. (defun org-shiftmetadown (&optional arg)
  14404. "Move subtree down or insert table row.
  14405. Calls `org-move-subtree-down' or `org-table-insert-row' or
  14406. `org-move-item-down', depending on context. See the individual
  14407. commands for more information."
  14408. (interactive "P")
  14409. (cond
  14410. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  14411. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  14412. ((org-at-item-p) (call-interactively 'org-move-item-down))
  14413. (t (org-shiftcursor-error))))
  14414. (defun org-metaleft (&optional arg)
  14415. "Promote heading or move table column to left.
  14416. Calls `org-do-promote' or `org-table-move-column', depending on context.
  14417. With no specific context, calls the Emacs default `backward-word'.
  14418. See the individual commands for more information."
  14419. (interactive "P")
  14420. (cond
  14421. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  14422. ((or (org-on-heading-p) (org-region-active-p))
  14423. (call-interactively 'org-do-promote))
  14424. (t (call-interactively 'backward-word))))
  14425. (defun org-metaright (&optional arg)
  14426. "Demote subtree or move table column to right.
  14427. Calls `org-do-demote' or `org-table-move-column', depending on context.
  14428. With no specific context, calls the Emacs default `forward-word'.
  14429. See the individual commands for more information."
  14430. (interactive "P")
  14431. (cond
  14432. ((org-at-table-p) (call-interactively 'org-table-move-column))
  14433. ((or (org-on-heading-p) (org-region-active-p))
  14434. (call-interactively 'org-do-demote))
  14435. (t (call-interactively 'forward-word))))
  14436. (defun org-metaup (&optional arg)
  14437. "Move subtree up or move table row up.
  14438. Calls `org-move-subtree-up' or `org-table-move-row' or
  14439. `org-move-item-up', depending on context. See the individual commands
  14440. for more information."
  14441. (interactive "P")
  14442. (cond
  14443. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  14444. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  14445. ((org-at-item-p) (call-interactively 'org-move-item-up))
  14446. (t (org-shiftcursor-error))))
  14447. (defun org-metadown (&optional arg)
  14448. "Move subtree down or move table row down.
  14449. Calls `org-move-subtree-down' or `org-table-move-row' or
  14450. `org-move-item-down', depending on context. See the individual
  14451. commands for more information."
  14452. (interactive "P")
  14453. (cond
  14454. ((org-at-table-p) (call-interactively 'org-table-move-row))
  14455. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  14456. ((org-at-item-p) (call-interactively 'org-move-item-down))
  14457. (t (org-shiftcursor-error))))
  14458. (defun org-shiftup (&optional arg)
  14459. "Increase item in timestamp or increase priority of current headline.
  14460. Calls `org-timestamp-up' or `org-priority-up', depending on context.
  14461. See the individual commands for more information."
  14462. (interactive "P")
  14463. (cond
  14464. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up))
  14465. ((org-on-heading-p) (call-interactively 'org-priority-up))
  14466. ((org-at-item-p) (call-interactively 'org-previous-item))
  14467. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  14468. (defun org-shiftdown (&optional arg)
  14469. "Decrease item in timestamp or decrease priority of current headline.
  14470. Calls `org-timestamp-down' or `org-priority-down', depending on context.
  14471. See the individual commands for more information."
  14472. (interactive "P")
  14473. (cond
  14474. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down))
  14475. ((org-on-heading-p) (call-interactively 'org-priority-down))
  14476. (t (call-interactively 'org-next-item))))
  14477. (defun org-shiftright ()
  14478. "Next TODO keyword or timestamp one day later, depending on context."
  14479. (interactive)
  14480. (cond
  14481. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  14482. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  14483. (t (org-shiftcursor-error))))
  14484. (defun org-shiftleft ()
  14485. "Previous TODO keyword or timestamp one day earlier, depending on context."
  14486. (interactive)
  14487. (cond
  14488. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  14489. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  14490. (t (org-shiftcursor-error))))
  14491. (defun org-copy-special ()
  14492. "Copy region in table or copy current subtree.
  14493. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  14494. See the individual commands for more information."
  14495. (interactive)
  14496. (call-interactively
  14497. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  14498. (defun org-cut-special ()
  14499. "Cut region in table or cut current subtree.
  14500. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  14501. See the individual commands for more information."
  14502. (interactive)
  14503. (call-interactively
  14504. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  14505. (defun org-paste-special (arg)
  14506. "Paste rectangular region into table, or past subtree relative to level.
  14507. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  14508. See the individual commands for more information."
  14509. (interactive "P")
  14510. (if (org-at-table-p)
  14511. (org-table-paste-rectangle)
  14512. (org-paste-subtree arg)))
  14513. (defun org-ctrl-c-ctrl-c (&optional arg)
  14514. "Set tags in headline, or update according to changed information at point.
  14515. This command does many different things, depending on context:
  14516. - If the cursor is in a headline, prompt for tags and insert them
  14517. into the current line, aligned to `org-tags-column'. When called
  14518. with prefix arg, realign all tags in the current buffer.
  14519. - If the cursor is in one of the special #+KEYWORD lines, this
  14520. triggers scanning the buffer for these lines and updating the
  14521. information.
  14522. - If the cursor is inside a table, realign the table. This command
  14523. works even if the automatic table editor has been turned off.
  14524. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  14525. the entire table.
  14526. - If the cursor is inside a table created by the table.el package,
  14527. activate that table.
  14528. - If the current buffer is a remember buffer, close note and file it.
  14529. with a prefix argument, file it without further interaction to the default
  14530. location.
  14531. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  14532. links in this buffer.
  14533. - If the cursor is on a numbered item in a plain list, renumber the
  14534. ordered list."
  14535. (interactive "P")
  14536. (let ((org-enable-table-editor t))
  14537. (cond
  14538. ((or org-clock-overlays org-occur-highlights
  14539. org-latex-fragment-image-overlays)
  14540. (org-remove-clock-overlays)
  14541. (org-remove-occur-highlights)
  14542. (org-remove-latex-fragment-image-overlays)
  14543. (message "Temporary highlights/overlays removed from current buffer"))
  14544. ((and (local-variable-p 'org-finish-function (current-buffer))
  14545. (fboundp org-finish-function))
  14546. (funcall org-finish-function))
  14547. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  14548. ((org-on-heading-p) (call-interactively 'org-set-tags))
  14549. ((org-at-table.el-p)
  14550. (require 'table)
  14551. (beginning-of-line 1)
  14552. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  14553. (call-interactively 'table-recognize-table))
  14554. ((org-at-table-p)
  14555. (org-table-maybe-eval-formula)
  14556. (if arg
  14557. (call-interactively 'org-table-recalculate)
  14558. (org-table-maybe-recalculate-line))
  14559. (call-interactively 'org-table-align))
  14560. ((org-at-item-checkbox-p)
  14561. (call-interactively 'org-toggle-checkbox))
  14562. ((org-at-item-p)
  14563. (call-interactively 'org-renumber-ordered-list))
  14564. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  14565. (cond
  14566. ((equal (match-string 1) "TBLFM")
  14567. ;; Recalculate the table before this line
  14568. (save-excursion
  14569. (beginning-of-line 1)
  14570. (skip-chars-backward " \r\n\t")
  14571. (if (org-at-table-p)
  14572. (org-call-with-arg 'org-table-recalculate t))))
  14573. (t
  14574. (call-interactively 'org-mode-restart))))
  14575. (t (error "C-c C-c can do nothing useful at this location.")))))
  14576. (defun org-mode-restart ()
  14577. "Restart Org-mode, to scan again for special lines.
  14578. Also updates the keyword regular expressions."
  14579. (interactive)
  14580. (let ((org-inhibit-startup t)) (org-mode))
  14581. (message "Org-mode restarted to refresh keyword and special line setup"))
  14582. (defun org-return ()
  14583. "Goto next table row or insert a newline.
  14584. Calls `org-table-next-row' or `newline', depending on context.
  14585. See the individual commands for more information."
  14586. (interactive)
  14587. (cond
  14588. ((org-at-table-p)
  14589. (org-table-justify-field-maybe)
  14590. (call-interactively 'org-table-next-row))
  14591. (t (newline))))
  14592. (defun org-meta-return (&optional arg)
  14593. "Insert a new heading or wrap a region in a table.
  14594. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  14595. See the individual commands for more information."
  14596. (interactive "P")
  14597. (cond
  14598. ((org-at-table-p)
  14599. (call-interactively 'org-table-wrap-region))
  14600. (t (call-interactively 'org-insert-heading))))
  14601. ;;; Menu entries
  14602. ;; Define the Org-mode menus
  14603. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  14604. '("Tbl"
  14605. ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
  14606. ["Next Field" org-cycle (org-at-table-p)]
  14607. ["Previous Field" org-shifttab (org-at-table-p)]
  14608. ["Next Row" org-return (org-at-table-p)]
  14609. "--"
  14610. ["Blank Field" org-table-blank-field (org-at-table-p)]
  14611. ["Edit Field" org-table-edit-field (org-at-table-p)]
  14612. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  14613. "--"
  14614. ("Column"
  14615. ["Move Column Left" org-metaleft (org-at-table-p)]
  14616. ["Move Column Right" org-metaright (org-at-table-p)]
  14617. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  14618. ["Insert Column" org-shiftmetaright (org-at-table-p)]
  14619. "--"
  14620. ["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])
  14621. ("Row"
  14622. ["Move Row Up" org-metaup (org-at-table-p)]
  14623. ["Move Row Down" org-metadown (org-at-table-p)]
  14624. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  14625. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  14626. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  14627. "--"
  14628. ["Insert Hline" org-table-insert-hline (org-at-table-p)])
  14629. ("Rectangle"
  14630. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  14631. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  14632. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  14633. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  14634. "--"
  14635. ("Calculate"
  14636. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  14637. ["Set Named Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  14638. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  14639. "--"
  14640. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  14641. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  14642. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  14643. "--"
  14644. ["Sum Column/Rectangle" org-table-sum
  14645. (or (org-at-table-p) (org-region-active-p))]
  14646. ["Which Column?" org-table-current-column (org-at-table-p)])
  14647. ["Debug Formulas"
  14648. (setq org-table-formula-debug (not org-table-formula-debug))
  14649. :style toggle :selected org-table-formula-debug]
  14650. "--"
  14651. ["Create" org-table-create (and (not (org-at-table-p))
  14652. org-enable-table-editor)]
  14653. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  14654. ["Import from File" org-table-import (not (org-at-table-p))]
  14655. ["Export to File" org-table-export (org-at-table-p)]
  14656. "--"
  14657. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  14658. (easy-menu-define org-org-menu org-mode-map "Org menu"
  14659. '("Org"
  14660. ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
  14661. ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
  14662. ["Sparse Tree" org-occur t]
  14663. ["Show All" show-all t]
  14664. "--"
  14665. ["New Heading" org-insert-heading t]
  14666. ("Navigate Headings"
  14667. ["Up" outline-up-heading t]
  14668. ["Next" outline-next-visible-heading t]
  14669. ["Previous" outline-previous-visible-heading t]
  14670. ["Next Same Level" outline-forward-same-level t]
  14671. ["Previous Same Level" outline-backward-same-level t]
  14672. "--"
  14673. ["Jump" org-goto t])
  14674. ("Edit Structure"
  14675. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  14676. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  14677. "--"
  14678. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  14679. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  14680. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  14681. "--"
  14682. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  14683. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  14684. ["Demote Heading" org-metaright (not (org-at-table-p))]
  14685. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  14686. "--"
  14687. ["Convert to odd levels" org-convert-to-odd-levels t]
  14688. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  14689. ("Archive"
  14690. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  14691. ["Check and Tag Children" (org-toggle-archive-tag (4))
  14692. :active t :keys "C-u C-c C-x C-a"]
  14693. ["Sparse trees open ARCHIVE trees"
  14694. (setq org-sparse-tree-open-archived-trees
  14695. (not org-sparse-tree-open-archived-trees))
  14696. :style toggle :selected org-sparse-tree-open-archived-trees]
  14697. ["Cycling opens ARCHIVE trees"
  14698. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  14699. :style toggle :selected org-cycle-open-archived-trees]
  14700. ["Agenda includes ARCHIVE trees"
  14701. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  14702. :style toggle :selected (not org-agenda-skip-archived-trees)]
  14703. "--"
  14704. ["Move Subtree to Archive" org-archive-subtree t]
  14705. ["Check and Move Children" (org-archive-subtree '(4))
  14706. :active t :keys "C-u C-c $"])
  14707. "--"
  14708. ("TODO Lists"
  14709. ["TODO/DONE/-" org-todo t]
  14710. ("Select keyword"
  14711. ["Next keyword" org-shiftright (org-on-heading-p)]
  14712. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  14713. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))])
  14714. ["Show TODO Tree" org-show-todo-tree t]
  14715. ["Global TODO list" org-todo-list t]
  14716. "--"
  14717. ["Set Priority" org-priority t]
  14718. ["Priority Up" org-shiftup t]
  14719. ["Priority Down" org-shiftdown t])
  14720. ("Dates and Scheduling"
  14721. ["Timestamp" org-time-stamp t]
  14722. ["Timestamp (inactive)" org-time-stamp-inactive t]
  14723. ("Change Date"
  14724. ["1 Day Later" org-timestamp-up-day t]
  14725. ["1 Day Earlier" org-timestamp-down-day t]
  14726. ["1 ... Later" org-shiftup t]
  14727. ["1 ... Earlier" org-shiftdown t])
  14728. ["Compute Time Range" org-evaluate-time-range t]
  14729. ["Schedule Item" org-schedule t]
  14730. ["Deadline" org-deadline t]
  14731. "--"
  14732. ["Goto Calendar" org-goto-calendar t]
  14733. ["Date from Calendar" org-date-from-calendar t])
  14734. ("Logging work"
  14735. ["Clock in" org-clock-in t]
  14736. ["Clock out" org-clock-out t]
  14737. ["Clock cancel" org-clock-cancel t]
  14738. ["Display times" org-clock-display t]
  14739. ["Create clock table" org-clock-report t]
  14740. "--"
  14741. ["Record DONE time"
  14742. (progn (setq org-log-done (not org-log-done))
  14743. (message "Switching to %s will %s record a timestamp"
  14744. org-done-string
  14745. (if org-log-done "automatically" "not")))
  14746. :style toggle :selected org-log-done])
  14747. "--"
  14748. ["Agenda Command" org-agenda t]
  14749. ("File List for Agenda")
  14750. ("Special views current file"
  14751. ["TODO Tree" org-show-todo-tree t]
  14752. ["Check Deadlines" org-check-deadlines t]
  14753. ["Timeline" org-timeline t]
  14754. ["Tags Tree" org-tags-sparse-tree t])
  14755. "--"
  14756. ("Hyperlinks"
  14757. ["Store Link (Global)" org-store-link t]
  14758. ["Insert Link" org-insert-link t]
  14759. ["Follow Link" org-open-at-point t]
  14760. "--"
  14761. ["Descriptive Links"
  14762. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  14763. :style radio :selected (member '(org-link) buffer-invisibility-spec)]
  14764. ["Literal Links"
  14765. (progn
  14766. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  14767. :style radio :selected (not (member '(org-link) buffer-invisibility-spec))]
  14768. "--"
  14769. ["Upgrade all <link> to [[link][desc]]" org-upgrade-old-links
  14770. (save-excursion (goto-char (point-min))
  14771. (re-search-forward "<[a-z]+:" nil t))])
  14772. "--"
  14773. ["Export/Publish" org-export t]
  14774. ("LaTeX"
  14775. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  14776. :selected org-cdlatex-mode]
  14777. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  14778. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  14779. ["Modify math symbol" org-cdlatex-math-modify
  14780. (org-inside-LaTeX-fragment-p)]
  14781. ["Export LaTeX fragments as images"
  14782. (setq org-export-with-LaTeX-fragments (not org-export-with-LaTeX-fragments))
  14783. :style toggle :selected org-export-with-LaTeX-fragments])
  14784. "--"
  14785. ("Documentation"
  14786. ["Show Version" org-version t]
  14787. ["Info Documentation" org-info t])
  14788. ("Customize"
  14789. ["Browse Org Group" org-customize t]
  14790. "--"
  14791. ["Expand This Menu" org-create-customize-menu
  14792. (fboundp 'customize-menu-create)])
  14793. "--"
  14794. ["Refresh setup" org-mode-restart t]
  14795. ))
  14796. (defun org-info (&optional node)
  14797. "Read documentation for Org-mode in the info system.
  14798. With optional NODE, go directly to that node."
  14799. (interactive)
  14800. (require 'info)
  14801. (Info-goto-node (format "(org)%s" (or node ""))))
  14802. (defun org-install-agenda-files-menu ()
  14803. (let ((bl (buffer-list)))
  14804. (save-excursion
  14805. (while bl
  14806. (set-buffer (pop bl))
  14807. (if (org-mode-p) (setq bl nil)))
  14808. (when (org-mode-p)
  14809. (easy-menu-change
  14810. '("Org") "File List for Agenda"
  14811. (append
  14812. (list
  14813. ["Edit File List" (org-edit-agenda-file-list) t]
  14814. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  14815. ["Remove Current File from List" org-remove-file t]
  14816. ["Cycle through agenda files" org-cycle-agenda-files t]
  14817. "--")
  14818. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  14819. ;;; Documentation
  14820. (defun org-customize ()
  14821. "Call the customize function with org as argument."
  14822. (interactive)
  14823. (customize-browse 'org))
  14824. (defun org-create-customize-menu ()
  14825. "Create a full customization menu for Org-mode, insert it into the menu."
  14826. (interactive)
  14827. (if (fboundp 'customize-menu-create)
  14828. (progn
  14829. (easy-menu-change
  14830. '("Org") "Customize"
  14831. `(["Browse Org group" org-customize t]
  14832. "--"
  14833. ,(customize-menu-create 'org)
  14834. ["Set" Custom-set t]
  14835. ["Save" Custom-save t]
  14836. ["Reset to Current" Custom-reset-current t]
  14837. ["Reset to Saved" Custom-reset-saved t]
  14838. ["Reset to Standard Settings" Custom-reset-standard t]))
  14839. (message "\"Org\"-menu now contains full customization menu"))
  14840. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  14841. ;;; Miscellaneous stuff
  14842. (defun org-context ()
  14843. "Return a list of contexts of the current cursor position.
  14844. If several contexts apply, all are returned.
  14845. Each context entry is a list with a symbol naming the context, and
  14846. two positions indicating start and end of the context. Possible
  14847. contexts are:
  14848. :headline anywhere in a headline
  14849. :headline-stars on the leading stars in a headline
  14850. :todo-keyword on a TODO keyword (including DONE) in a headline
  14851. :tags on the TAGS in a headline
  14852. :priority on the priority cookie in a headline
  14853. :item on the first line of a plain list item
  14854. :item-bullet on the bullet/number of a plain list item
  14855. :checkbox on the checkbox in a plain list item
  14856. :table in an org-mode table
  14857. :table-special on a special filed in a table
  14858. :table-table in a table.el table
  14859. :link on a hyperline
  14860. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  14861. :target on a <<target>>
  14862. :radio-target on a <<<radio-target>>>
  14863. :latex-fragment on a LaTeX fragment
  14864. :latex-preview on a LaTeX fragment with overlayed preview image
  14865. This function expects the position to be visible because it uses font-lock
  14866. faces as a help to recognize the following contexts: :table-special, :link,
  14867. and :keyword."
  14868. (let* ((f (get-text-property (point) 'face))
  14869. (faces (if (listp f) f (list f)))
  14870. (p (point)) clist o)
  14871. ;; First the large context
  14872. (cond
  14873. ((org-on-heading-p)
  14874. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  14875. (when (progn
  14876. (beginning-of-line 1)
  14877. (looking-at org-todo-line-tags-regexp))
  14878. (push (org-point-in-group p 1 :headline-stars) clist)
  14879. (push (org-point-in-group p 2 :todo-keyword) clist)
  14880. (push (org-point-in-group p 4 :tags) clist))
  14881. (goto-char p)
  14882. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  14883. (if (looking-at "\\[#[A-Z]\\]")
  14884. (push (org-point-in-group p 0 :priority) clist)))
  14885. ((org-at-item-p)
  14886. (push (org-point-in-group p 2 :item-bullet) clist)
  14887. (push (list :item (point-at-bol)
  14888. (save-excursion (org-end-of-item) (point)))
  14889. clist)
  14890. (and (org-at-item-checkbox-p)
  14891. (push (org-point-in-group p 0 :checkbox) clist)))
  14892. ((org-at-table-p)
  14893. (push (list :table (org-table-begin) (org-table-end)) clist)
  14894. (if (memq 'org-formula faces)
  14895. (push (list :table-special
  14896. (previous-single-property-change p 'face)
  14897. (next-single-property-change p 'face)) clist)))
  14898. ((org-at-table-p 'any)
  14899. (push (list :table-table) clist)))
  14900. (goto-char p)
  14901. ;; Now the small context
  14902. (cond
  14903. ((org-at-timestamp-p)
  14904. (push (org-point-in-group p 0 :timestamp) clist))
  14905. ((memq 'org-link faces)
  14906. (push (list :link
  14907. (previous-single-property-change p 'face)
  14908. (next-single-property-change p 'face)) clist))
  14909. ((memq 'org-special-keyword faces)
  14910. (push (list :keyword
  14911. (previous-single-property-change p 'face)
  14912. (next-single-property-change p 'face)) clist))
  14913. ((org-on-target-p)
  14914. (push (org-point-in-group p 0 :target) clist)
  14915. (goto-char (1- (match-beginning 0)))
  14916. (if (looking-at org-radio-target-regexp)
  14917. (push (org-point-in-group p 0 :radio-target) clist))
  14918. (goto-char p))
  14919. ((setq o (car (delq nil
  14920. (mapcar
  14921. (lambda (x)
  14922. (if (memq x org-latex-fragment-image-overlays) x))
  14923. (org-overlays-at (point))))))
  14924. (push (list :latex-fragment
  14925. (org-overlay-start o) (org-overlay-end o)) clist)
  14926. (push (list :latex-preview
  14927. (org-overlay-start o) (org-overlay-end o)) clist))
  14928. ((org-inside-LaTeX-fragment-p)
  14929. ;; FIXME: positions wring.
  14930. (push (list :latex-fragment (point) (point)) clist)))
  14931. (setq clist (nreverse (delq nil clist)))
  14932. clist))
  14933. (defun org-point-in-group (point group &optional context)
  14934. "Check if POINT is in match-group GROUP.
  14935. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  14936. match. If the match group does ot exist or point is not inside it,
  14937. return nil."
  14938. (and (match-beginning group)
  14939. (>= point (match-beginning group))
  14940. (<= point (match-end group))
  14941. (if context
  14942. (list context (match-beginning group) (match-end group))
  14943. t)))
  14944. (defun org-move-line-down (arg)
  14945. "Move the current line down. With prefix argument, move it past ARG lines."
  14946. (interactive "p")
  14947. (let ((col (current-column))
  14948. beg end pos)
  14949. (beginning-of-line 1) (setq beg (point))
  14950. (beginning-of-line 2) (setq end (point))
  14951. (beginning-of-line (+ 1 arg))
  14952. (setq pos (move-marker (make-marker) (point)))
  14953. (insert (delete-and-extract-region beg end))
  14954. (goto-char pos)
  14955. (move-to-column col)))
  14956. (defun org-move-line-up (arg)
  14957. "Move the current line up. With prefix argument, move it past ARG lines."
  14958. (interactive "p")
  14959. (let ((col (current-column))
  14960. beg end pos)
  14961. (beginning-of-line 1) (setq beg (point))
  14962. (beginning-of-line 2) (setq end (point))
  14963. (beginning-of-line (- arg))
  14964. (setq pos (move-marker (make-marker) (point)))
  14965. (insert (delete-and-extract-region beg end))
  14966. (goto-char pos)
  14967. (move-to-column col)))
  14968. ;; Paragraph filling stuff.
  14969. ;; We want this to be just right, so use the full arsenal.
  14970. (defun org-set-autofill-regexps ()
  14971. (interactive)
  14972. ;; In the paragraph separator we include headlines, because filling
  14973. ;; text in a line directly attached to a headline would otherwise
  14974. ;; fill the headline as well.
  14975. (org-set-local 'comment-start-skip "^#+[ \t]*")
  14976. (org-set-local 'paragraph-separate "\f\\|\\*\\|[ ]*$\\|[ \t]*[:|]")
  14977. ;; The paragraph starter includes hand-formatted lists.
  14978. (org-set-local 'paragraph-start
  14979. "\f\\|[ ]*$\\|\\([*\f]+\\)\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  14980. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  14981. ;; But only if the user has not turned off tables or fixed-width regions
  14982. (org-set-local
  14983. 'auto-fill-inhibit-regexp
  14984. (concat "\\*\\|#"
  14985. "\\|[ \t]*" org-keyword-time-regexp
  14986. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  14987. (concat
  14988. "\\|[ \t]*["
  14989. (if org-enable-table-editor "|" "")
  14990. (if org-enable-fixed-width-editor ":" "")
  14991. "]"))))
  14992. ;; We use our own fill-paragraph function, to make sure that tables
  14993. ;; and fixed-width regions are not wrapped. That function will pass
  14994. ;; through to `fill-paragraph' when appropriate.
  14995. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  14996. ; Adaptive filling: To get full control, first make sure that
  14997. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  14998. (org-set-local 'adaptive-fill-regexp "\000")
  14999. (org-set-local 'adaptive-fill-function
  15000. 'org-adaptive-fill-function))
  15001. (defun org-fill-paragraph (&optional justify)
  15002. "Re-align a table, pass through to fill-paragraph if no table."
  15003. (let ((table-p (org-at-table-p))
  15004. (table.el-p (org-at-table.el-p)))
  15005. (cond ((equal (char-after (point-at-bol)) ?*) t) ; skip headlines
  15006. (table.el-p t) ; skip table.el tables
  15007. (table-p (org-table-align) t) ; align org-mode tables
  15008. (t nil)))) ; call paragraph-fill
  15009. ;; For reference, this is the default value of adaptive-fill-regexp
  15010. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  15011. (defun org-adaptive-fill-function ()
  15012. "Return a fill prefix for org-mode files.
  15013. In particular, this makes sure hanging paragraphs for hand-formatted lists
  15014. work correctly."
  15015. (if (looking-at " *\\([-*+] \\|[0-9]+[.)] \\)?")
  15016. (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  15017. ;; Functions needed for Emacs/XEmacs region compatibility
  15018. (defun org-add-hook (hook function &optional append local)
  15019. "Add-hook, compatible with both Emacsen."
  15020. (if (and local (featurep 'xemacs))
  15021. (add-local-hook hook function append)
  15022. (add-hook hook function append local)))
  15023. (defun org-region-active-p ()
  15024. "Is `transient-mark-mode' on and the region active?
  15025. Works on both Emacs and XEmacs."
  15026. (if org-ignore-region
  15027. nil
  15028. (if (featurep 'xemacs)
  15029. (and zmacs-regions (region-active-p))
  15030. (and transient-mark-mode mark-active))))
  15031. (defun org-add-to-invisibility-spec (arg)
  15032. "Add elements to `buffer-invisibility-spec'.
  15033. See documentation for `buffer-invisibility-spec' for the kind of elements
  15034. that can be added."
  15035. (cond
  15036. ((fboundp 'add-to-invisibility-spec)
  15037. (add-to-invisibility-spec arg))
  15038. ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
  15039. (setq buffer-invisibility-spec (list arg)))
  15040. (t
  15041. (setq buffer-invisibility-spec
  15042. (cons arg buffer-invisibility-spec)))))
  15043. (defun org-remove-from-invisibility-spec (arg)
  15044. "Remove elements from `buffer-invisibility-spec'."
  15045. (if (fboundp 'remove-from-invisibility-spec)
  15046. (remove-from-invisibility-spec arg)
  15047. (if (consp buffer-invisibility-spec)
  15048. (setq buffer-invisibility-spec
  15049. (delete arg buffer-invisibility-spec)))))
  15050. (defun org-in-invisibility-spec-p (arg)
  15051. "Is ARG a member of `buffer-invisibility-spec'?"
  15052. (if (consp buffer-invisibility-spec)
  15053. (member arg buffer-invisibility-spec)
  15054. nil))
  15055. (defun org-image-file-name-regexp ()
  15056. "Return regexp matching the file names of images."
  15057. (if (fboundp 'image-file-name-regexp)
  15058. (image-file-name-regexp)
  15059. (let ((image-file-name-extensions
  15060. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  15061. "xbm" "xpm" "pbm" "pgm" "ppm")))
  15062. (concat "\\."
  15063. (regexp-opt (nconc (mapcar 'upcase
  15064. image-file-name-extensions)
  15065. image-file-name-extensions)
  15066. t)
  15067. "\\'"))))
  15068. ;; Functions extending outline functionality
  15069. ;; C-a should go to the beginning of a *visible* line, also in the
  15070. ;; new outline.el. I guess this should be patched into Emacs?
  15071. (defun org-beginning-of-line ()
  15072. "Go to the beginning of the current line. If that is invisible, continue
  15073. to a visible line beginning. This makes the function of C-a more intuitive."
  15074. (interactive)
  15075. (beginning-of-line 1)
  15076. (if (bobp)
  15077. nil
  15078. (backward-char 1)
  15079. (if (org-invisible-p)
  15080. (while (and (not (bobp)) (org-invisible-p))
  15081. (backward-char 1)
  15082. (beginning-of-line 1))
  15083. (forward-char 1))))
  15084. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  15085. (defun org-invisible-p ()
  15086. "Check if point is at a character currently not visible."
  15087. ;; Early versions of noutline don't have `outline-invisible-p'.
  15088. (if (fboundp 'outline-invisible-p)
  15089. (outline-invisible-p)
  15090. (get-char-property (point) 'invisible)))
  15091. (defun org-invisible-p2 ()
  15092. "Check if point is at a character currently not visible."
  15093. (save-excursion
  15094. (if (and (eolp) (not (bobp))) (backward-char 1))
  15095. ;; Early versions of noutline don't have `outline-invisible-p'.
  15096. (if (fboundp 'outline-invisible-p)
  15097. (outline-invisible-p)
  15098. (get-char-property (point) 'invisible))))
  15099. (defalias 'org-back-to-heading 'outline-back-to-heading)
  15100. (defalias 'org-on-heading-p 'outline-on-heading-p)
  15101. (defun org-on-target-p ()
  15102. (let ((pos (point)))
  15103. (save-excursion
  15104. (skip-chars-forward "<")
  15105. (and (re-search-backward "<<" nil t)
  15106. (or (looking-at org-radio-target-regexp)
  15107. (looking-at org-target-regexp))
  15108. (<= (match-beginning 0) pos)
  15109. (>= (1+ (match-end 0)) pos)))))
  15110. (defun org-up-heading-all (arg)
  15111. "Move to the heading line of which the present line is a subheading.
  15112. This function considers both visible and invisible heading lines.
  15113. With argument, move up ARG levels."
  15114. (if (fboundp 'outline-up-heading-all)
  15115. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  15116. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  15117. (defun org-show-hidden-entry ()
  15118. "Show an entry where even the heading is hidden."
  15119. (save-excursion
  15120. (org-show-entry)))
  15121. (defun org-flag-heading (flag &optional entry)
  15122. "Flag the current heading. FLAG non-nil means make invisible.
  15123. When ENTRY is non-nil, show the entire entry."
  15124. (save-excursion
  15125. (org-back-to-heading t)
  15126. ;; Check if we should show the entire entry
  15127. (if entry
  15128. (progn
  15129. (org-show-entry)
  15130. (save-excursion
  15131. (and (outline-next-heading)
  15132. (org-flag-heading nil))))
  15133. (outline-flag-region (max 1 (1- (point)))
  15134. (save-excursion (outline-end-of-heading) (point))
  15135. flag))))
  15136. (defun org-end-of-subtree (&optional invisible-OK)
  15137. ;; This is an exact copy of the original function, but it uses
  15138. ;; `org-back-to-heading', to make it work also in invisible
  15139. ;; trees. And is uses an invisible-OK argument.
  15140. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  15141. (org-back-to-heading invisible-OK)
  15142. (let ((first t)
  15143. (level (funcall outline-level)))
  15144. (while (and (not (eobp))
  15145. (or first (> (funcall outline-level) level)))
  15146. (setq first nil)
  15147. (outline-next-heading))
  15148. (if (memq (preceding-char) '(?\n ?\^M))
  15149. (progn
  15150. ;; Go to end of line before heading
  15151. (forward-char -1)
  15152. (if (memq (preceding-char) '(?\n ?\^M))
  15153. ;; leave blank line before heading
  15154. (forward-char -1)))))
  15155. (point))
  15156. (defun org-show-subtree ()
  15157. "Show everything after this heading at deeper levels."
  15158. (outline-flag-region
  15159. (point)
  15160. (save-excursion
  15161. (outline-end-of-subtree) (outline-next-heading) (point))
  15162. nil))
  15163. (defun org-show-entry ()
  15164. "Show the body directly following this heading.
  15165. Show the heading too, if it is currently invisible."
  15166. (interactive)
  15167. (save-excursion
  15168. (org-back-to-heading t)
  15169. (outline-flag-region
  15170. (max 1 (1- (point)))
  15171. (save-excursion
  15172. (re-search-forward (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  15173. (or (match-beginning 1) (point-max)))
  15174. nil)))
  15175. (defun org-make-options-regexp (kwds)
  15176. "Make a regular expression for keyword lines."
  15177. (concat
  15178. "^"
  15179. "#?[ \t]*\\+\\("
  15180. (mapconcat 'regexp-quote kwds "\\|")
  15181. "\\):[ \t]*"
  15182. "\\(.+\\)"))
  15183. ;; Make `bookmark-jump' show the jump location if it was hidden.
  15184. (eval-after-load "bookmark"
  15185. '(if (boundp 'bookmark-after-jump-hook)
  15186. ;; We can use the hook
  15187. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  15188. ;; Hook not available, use advice
  15189. (defadvice bookmark-jump (after org-make-visible activate)
  15190. "Make the position visible."
  15191. (org-bookmark-jump-unhide))))
  15192. (defun org-bookmark-jump-unhide ()
  15193. "Unhide the current position, to show the bookmark location."
  15194. (and (org-mode-p)
  15195. (or (org-invisible-p)
  15196. (save-excursion (goto-char (max (point-min) (1- (point))))
  15197. (org-invisible-p)))
  15198. (org-show-hierarchy-above)))
  15199. ;;; Experimental code
  15200. ;;; Finish up
  15201. (provide 'org)
  15202. (run-hooks 'org-load-hook)
  15203. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  15204. ;;; org.el ends here