org.el 656 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832
  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.57a
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software; you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation; either version 2, or (at your option)
  15. ;; any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  22. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  23. ;; Boston, MA 02110-1301, USA.
  24. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  25. ;;
  26. ;;; Commentary:
  27. ;;
  28. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  29. ;; project planning with a fast and effective plain-text system.
  30. ;;
  31. ;; Org-mode develops organizational tasks around NOTES files that contain
  32. ;; information about projects as plain text. Org-mode is implemented on
  33. ;; top of outline-mode, which makes it possible to keep the content of
  34. ;; large files well structured. Visibility cycling and structure editing
  35. ;; help to work with the tree. Tables are easily created with a built-in
  36. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  37. ;; and scheduling. It dynamically compiles entries into an agenda that
  38. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  39. ;; Plain text URL-like links connect to websites, emails, Usenet
  40. ;; messages, BBDB entries, and any files related to the projects. For
  41. ;; printing and sharing of notes, an Org-mode file can be exported as a
  42. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  43. ;; iCalendar file. It can also serve as a publishing tool for a set of
  44. ;; linked webpages.
  45. ;;
  46. ;; Installation and Activation
  47. ;; ---------------------------
  48. ;; See the corresponding sections in the manual at
  49. ;;
  50. ;; http://staff.science.uva.nl/~dominik/Tools/org/org.html#Installation
  51. ;;
  52. ;; Documentation
  53. ;; -------------
  54. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  55. ;; distribution also contains a PDF version of it. At the homepage of
  56. ;; Org-mode, you can read the same text online as HTML. There is also an
  57. ;; excellent reference card made by Philip Rooke. This card can be found
  58. ;; in the etc/ directory of Emacs 22.
  59. ;;
  60. ;; Recent changes
  61. ;; --------------
  62. ;; Version 4.57a
  63. ;; - Bug fixes for XEmacs.
  64. ;;
  65. ;; Version 4.57
  66. ;; - Sorting of outline items on same level.
  67. ;; - Sorting tables automatically selects line range between hlines.
  68. ;; - Changes in Agenda buffer
  69. ;; - `C-c C-o' follows a link in the current line.
  70. ;; - `C-c $' archives the subtree corresponding to the line.
  71. ;; - Changing dates with S-left and S-right show new date in agenda,
  72. ;; but still do not move the entry to the new date.
  73. ;; - new option `org-agenda-skip-scheduled-if-done'.
  74. ;; - Agenda and sparse tree construction using tag matches can now
  75. ;; use regular expressions.
  76. ;; - When prompted for a date/time, entering "+7" indicates a date
  77. ;; 7 days from now - but only this is the only thing you give.
  78. ;; - Custom time formats also apply to exported html and ascii.
  79. ;; - Bug fixes.
  80. ;;
  81. ;; Version 4.56
  82. ;; - `C-k' in agenda kills current line and corresponding subtree in file.
  83. ;; - XEmacs compatibility issues fixed, in particular tag alignment.
  84. ;; - M-left/right now in/outdents plain list items, no Shift needed.
  85. ;; - Bug fixes.
  86. ;;
  87. ;; Version 4.55
  88. ;; - Bug fixes.
  89. ;;
  90. ;; Version 4.54
  91. ;; - Improvements to fast tag selection
  92. ;; + show status also in target line.
  93. ;; + option to auto-exit after first change to tags list (see manual).
  94. ;; - Tags sparse trees now also respect the settings in
  95. ;; `org-show-hierarchy-above' and `org-show-following-heading'.
  96. ;; - Bug fixes.
  97. ;;
  98. ;; Version 4.53
  99. ;; - Custom time formats can be overlayed over time stamps.
  100. ;; - New option `org-agenda-todo-ignore-deadlines'.
  101. ;; - Work-around for flyspell bug (CVS Emacs has this fixed in flyspell.el).
  102. ;; - Work-around for session.el problem with circular data structures.
  103. ;; - Bug fixes.
  104. ;;
  105. ;; Version 4.52
  106. ;; - TAG matches can also specify conditions on TODO keywords.
  107. ;; - The fast tag interface allows setting tags that are not in the
  108. ;; predefined list.
  109. ;; - Bug fixes.
  110. ;;
  111. ;; Version 4.51
  112. ;; - Link abbreviations (manual section 4.5).
  113. ;; - More control over how agenda is displayed. See the new variables
  114. ;; `org-agenda-window-setup', `org-agenda-restore-windows-after-quit'.
  115. ;; - Bug fixes.
  116. ;;
  117. ;;; Code:
  118. (eval-when-compile
  119. (require 'cl)
  120. (require 'gnus-sum)
  121. (require 'calendar))
  122. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  123. ;; the file noutline.el being loaded.
  124. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  125. ;; We require noutline, which might be provided in outline.el
  126. (require 'outline) (require 'noutline)
  127. ;; Other stuff we need.
  128. (require 'time-date)
  129. (require 'easymenu)
  130. ;;; Customization variables
  131. (defvar org-version "4.57a"
  132. "The version number of the file org.el.")
  133. (defun org-version ()
  134. (interactive)
  135. (message "Org-mode version %s" org-version))
  136. ;; Compatibility constants
  137. (defconst org-xemacs-p (featurep 'xemacs)) ; not used by org.el itself
  138. (defconst org-format-transports-properties-p
  139. (let ((x "a"))
  140. (add-text-properties 0 1 '(test t) x)
  141. (get-text-property 0 'test (format "%s" x)))
  142. "Does format transport text properties?")
  143. (defgroup org nil
  144. "Outline-based notes management and organizer."
  145. :tag "Org"
  146. :group 'outlines
  147. :group 'hypermedia
  148. :group 'calendar)
  149. (defgroup org-startup nil
  150. "Options concerning startup of Org-mode."
  151. :tag "Org Startup"
  152. :group 'org)
  153. (defcustom org-startup-folded t
  154. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  155. This can also be configured on a per-file basis by adding one of
  156. the following lines anywhere in the buffer:
  157. #+STARTUP: fold
  158. #+STARTUP: nofold
  159. #+STARTUP: content"
  160. :group 'org-startup
  161. :type '(choice
  162. (const :tag "nofold: show all" nil)
  163. (const :tag "fold: overview" t)
  164. (const :tag "content: all headlines" content)))
  165. (defcustom org-startup-truncated t
  166. "Non-nil means, entering Org-mode will set `truncate-lines'.
  167. This is useful since some lines containing links can be very long and
  168. uninteresting. Also tables look terrible when wrapped."
  169. :group 'org-startup
  170. :type 'boolean)
  171. (defcustom org-startup-align-all-tables nil
  172. "Non-nil means, align all tables when visiting a file.
  173. This is useful when the column width in tables is forced with <N> cookies
  174. in table fields. Such tables will look correct only after the first re-align.
  175. This can also be configured on a per-file basis by adding one of
  176. the following lines anywhere in the buffer:
  177. #+STARTUP: align
  178. #+STARTUP: noalign"
  179. :group 'org-startup
  180. :type 'boolean)
  181. (defcustom org-startup-with-deadline-check nil
  182. "Non-nil means, entering Org-mode will run the deadline check.
  183. This means, if you start editing an org file, you will get an
  184. immediate reminder of any due deadlines.
  185. This can also be configured on a per-file basis by adding one of
  186. the following lines anywhere in the buffer:
  187. #+STARTUP: dlcheck
  188. #+STARTUP: nodlcheck"
  189. :group 'org-startup
  190. :type 'boolean)
  191. (defcustom org-insert-mode-line-in-empty-file nil
  192. "Non-nil means insert the first line setting Org-mode in empty files.
  193. When the function `org-mode' is called interactively in an empty file, this
  194. normally means that the file name does not automatically trigger Org-mode.
  195. To ensure that the file will always be in Org-mode in the future, a
  196. line enforcing Org-mode will be inserted into the buffer, if this option
  197. has been set."
  198. :group 'org-startup
  199. :type 'boolean)
  200. (defcustom org-CUA-compatible nil
  201. "Non-nil means use alternative key bindings for S-<cursor movement>.
  202. Org-mode used S-<cursor movement> for changing timestamps and priorities.
  203. S-<cursor movement> is also used for example by `CUA-mode' to select text.
  204. If you want to use Org-mode together with `CUA-mode', Org-mode needs to use
  205. alternative bindings. Setting this variable to t will replace the following
  206. keys both in Org-mode and in the Org-agenda buffer.
  207. S-RET -> C-S-RET
  208. S-up -> M-p
  209. S-down -> M-n
  210. S-left -> M--
  211. S-right -> M-+
  212. If you do not like the alternative keys, take a look at the variable
  213. `org-disputed-keys'.
  214. This option is only relevant at load-time of Org-mode. Changing it requires
  215. a restart of Emacs to become effective."
  216. :group 'org-startup
  217. :type 'boolean)
  218. (defvar org-disputed-keys
  219. '((S-up [(shift up)] [(meta ?p)])
  220. (S-down [(shift down)] [(meta ?n)])
  221. (S-left [(shift left)] [(meta ?-)])
  222. (S-right [(shift right)] [(meta ?+)])
  223. (S-return [(shift return)] [(control shift return)]))
  224. "Keys for which Org-mode and other modes compete.
  225. This is an alist, cars are symbols for lookup, 1st element is the default key,
  226. second element will be used when `org-CUA-compatible' is t.")
  227. (defun org-key (key)
  228. "Select a key according to `org-CUA-compatible'."
  229. (nth (if org-CUA-compatible 2 1)
  230. (or (assq key org-disputed-keys)
  231. (error "Invalid Key %s in `org-key'" key))))
  232. (defcustom org-ellipsis nil
  233. "The ellipsis to use in the Org-mode outline.
  234. When nil, just use the standard three dots. When a string, use that instead,
  235. and just in Org-mode (which will then use its own display table).
  236. Changing this requires executing `M-x org-mode' in a buffer to become
  237. effective."
  238. :group 'org-startup
  239. :type '(choice (const :tag "Default" nil)
  240. (string :tag "String" :value "...#")))
  241. (defvar org-display-table nil
  242. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  243. (defgroup org-keywords nil
  244. "Keywords in Org-mode."
  245. :tag "Org Keywords"
  246. :group 'org)
  247. (defcustom org-deadline-string "DEADLINE:"
  248. "String to mark deadline entries.
  249. A deadline is this string, followed by a time stamp. Should be a word,
  250. terminated by a colon. You can insert a schedule keyword and
  251. a timestamp with \\[org-deadline].
  252. Changes become only effective after restarting Emacs."
  253. :group 'org-keywords
  254. :type 'string)
  255. (defcustom org-scheduled-string "SCHEDULED:"
  256. "String to mark scheduled TODO entries.
  257. A schedule is this string, followed by a time stamp. Should be a word,
  258. terminated by a colon. You can insert a schedule keyword and
  259. a timestamp with \\[org-schedule].
  260. Changes become only effective after restarting Emacs."
  261. :group 'org-keywords
  262. :type 'string)
  263. (defcustom org-closed-string "CLOSED:"
  264. "String used as the prefix for timestamps logging closing a TODO entry."
  265. :group 'org-keywords
  266. :type 'string)
  267. (defcustom org-clock-string "CLOCK:"
  268. "String used as prefix for timestamps clocking work hours on an item."
  269. :group 'org-keywords
  270. :type 'string)
  271. (defcustom org-comment-string "COMMENT"
  272. "Entries starting with this keyword will never be exported.
  273. An entry can be toggled between COMMENT and normal with
  274. \\[org-toggle-comment].
  275. Changes become only effective after restarting Emacs."
  276. :group 'org-keywords
  277. :type 'string)
  278. (defcustom org-quote-string "QUOTE"
  279. "Entries starting with this keyword will be exported in fixed-width font.
  280. Quoting applies only to the text in the entry following the headline, and does
  281. not extend beyond the next headline, even if that is lower level.
  282. An entry can be toggled between QUOTE and normal with
  283. \\[org-toggle-fixed-width-section]."
  284. :group 'org-keywords
  285. :type 'string)
  286. (defgroup org-structure nil
  287. "Options concerning the general structure of Org-mode files."
  288. :tag "Org Structure"
  289. :group 'org)
  290. (defgroup org-cycle nil
  291. "Options concerning visibility cycling in Org-mode."
  292. :tag "Org Cycle"
  293. :group 'org-structure)
  294. (defcustom org-cycle-global-at-bob t
  295. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  296. This makes it possible to do global cycling without having to use S-TAB or
  297. C-u TAB. For this special case to work, the first line of the buffer
  298. must not be a headline - it may be empty ot some other text. When used in
  299. this way, `org-cycle-hook' is disables temporarily, to make sure the
  300. cursor stays at the beginning of the buffer.
  301. When this option is nil, don't do anything special at the beginning
  302. of the buffer."
  303. :group 'org-cycle
  304. :type 'boolean)
  305. (defcustom org-cycle-emulate-tab t
  306. "Where should `org-cycle' emulate TAB.
  307. nil Never
  308. white Only in completely white lines
  309. whitestart Only at the beginning of lines, before the first non-white char.
  310. t Everywhere except in headlines
  311. If TAB is used in a place where it does not emulate TAB, the current subtree
  312. visibility is cycled."
  313. :group 'org-cycle
  314. :type '(choice (const :tag "Never" nil)
  315. (const :tag "Only in completely white lines" white)
  316. (const :tag "Before first char in a line" whitestart)
  317. (const :tag "Everywhere except in headlines" t)
  318. ))
  319. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  320. org-optimize-window-after-visibility-change)
  321. "Hook that is run after `org-cycle' has changed the buffer visibility.
  322. The function(s) in this hook must accept a single argument which indicates
  323. the new state that was set by the most recent `org-cycle' command. The
  324. argument is a symbol. After a global state change, it can have the values
  325. `overview', `content', or `all'. After a local state change, it can have
  326. the values `folded', `children', or `subtree'."
  327. :group 'org-cycle
  328. :type 'hook)
  329. (defgroup org-edit-structure nil
  330. "Options concerning structure editing in Org-mode."
  331. :tag "Org Edit Structure"
  332. :group 'org-structure)
  333. (defcustom org-odd-levels-only nil
  334. "Non-nil means, skip even levels and only use odd levels for the outline.
  335. This has the effect that two stars are being added/taken away in
  336. promotion/demotion commands. It also influences how levels are
  337. handled by the exporters.
  338. Changing it requires restart of `font-lock-mode' to become effective
  339. for fontification also in regions already fontified.
  340. You may also set this on a per-file basis by adding one of the following
  341. lines to the buffer:
  342. #+STARTUP: odd
  343. #+STARTUP: oddeven"
  344. :group 'org-edit-structure
  345. :group 'org-font-lock
  346. :type 'boolean)
  347. (defcustom org-adapt-indentation t
  348. "Non-nil means, adapt indentation when promoting and demoting.
  349. When this is set and the *entire* text in an entry is indented, the
  350. indentation is increased by one space in a demotion command, and
  351. decreased by one in a promotion command. If any line in the entry
  352. body starts at column 0, indentation is not changed at all."
  353. :group 'org-edit-structure
  354. :type 'boolean)
  355. (defcustom org-blank-before-new-entry '((heading . nil)
  356. (plain-list-item . nil))
  357. "Should `org-insert-heading' leave a blank line before new heading/item?
  358. The value is an alist, with `heading' and `plain-list-item' as car,
  359. and a boolean flag as cdr."
  360. :group 'org-edit-structure
  361. :type '(list
  362. (cons (const heading) (boolean))
  363. (cons (const plain-list-item) (boolean))))
  364. (defcustom org-insert-heading-hook nil
  365. "Hook being run after inserting a new heading."
  366. :group 'org-edit-structure
  367. :type 'boolean)
  368. (defcustom org-enable-fixed-width-editor t
  369. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  370. This currently only means, they are never auto-wrapped.
  371. When nil, such lines will be treated like ordinary lines.
  372. See also the QUOTE keyword."
  373. :group 'org-edit-structure
  374. :type 'boolean)
  375. (defgroup org-sparse-trees nil
  376. "Options concerning sparse trees in Org-mode."
  377. :tag "Org Sparse Trees"
  378. :group 'org-structure)
  379. (defcustom org-highlight-sparse-tree-matches t
  380. "Non-nil means, highlight all matches that define a sparse tree.
  381. The highlights will automatically disappear the next time the buffer is
  382. changed by an edit command."
  383. :group 'org-sparse-trees
  384. :type 'boolean)
  385. (defcustom org-remove-highlights-with-change t
  386. "Non-nil means, any change to the buffer will remove temporary highlights.
  387. Such highlights are created by `org-occur' and `org-clock-display'.
  388. When nil, `C-c C-c needs to be used to get rid of the highlights.
  389. The highlights created by `org-preview-latex-fragment' always need
  390. `C-c C-c' to be removed."
  391. :group 'org-sparse-trees
  392. :group 'org-time
  393. :type 'boolean)
  394. (defcustom org-show-hierarchy-above '((default . t))
  395. "Non-nil means, show full hierarchy when showing a spot in the tree.
  396. Turning this off makes sparse trees more compact, but also less clear.
  397. Instead of t, this can also be an alist specifying this option for different
  398. contexts. Valid contexts are
  399. agenda when exposing an entry from the agenda
  400. org-goto when using the command `org-goto' on key C-c C-j
  401. occur-tree when using the command `org-occur' on key C-c /
  402. tags-tree when constructing a sparse tree based on tags matches
  403. link-search when exposing search matches associated with a link
  404. mark-goto when exposing the jump goal of a mark
  405. bookmark-jump when exposing a bookmark location
  406. default default for all contexts not set explicitly"
  407. :group 'org-sparse-trees
  408. :type '(choice
  409. (const :tag "Always" t)
  410. (const :tag "Never" nil)
  411. (repeat :greedy t :tag "Individual contexts"
  412. (cons
  413. (choice :tag "Context"
  414. (const agenda)
  415. (const org-goto)
  416. (const occur-tree)
  417. (const tags-tree)
  418. (const link-search)
  419. (const mark-goto)
  420. (const bookmark-jump)
  421. (const default))
  422. (boolean)))))
  423. (defcustom org-show-following-heading '((default . t))
  424. "Non-nil means, show heading following match in `org-occur'.
  425. When doing an `org-occur' it is useful to show the headline which
  426. follows the match, even if they do not match the regexp. This makes it
  427. easier to edit directly inside the sparse tree. However, if you use
  428. `org-occur' mainly as an overview, the following headlines are
  429. unnecessary clutter.
  430. Instead of t, this can also be an alist specifying this option for different
  431. contexts. See `org-show-hierarchy-above' for valid contexts."
  432. :group 'org-sparse-trees
  433. :type '(choice
  434. (const :tag "Always" t)
  435. (const :tag "Never" nil)
  436. (repeat :greedy t :tag "Individual contexts"
  437. (cons
  438. (choice :tag "Context"
  439. (const agenda)
  440. (const org-goto)
  441. (const occur-tree)
  442. (const tags-tree)
  443. (const link-search)
  444. (const mark-goto)
  445. (const bookmark-jump)
  446. (const default))
  447. (boolean)))))
  448. (defcustom org-occur-hook '(org-first-headline-recenter)
  449. "Hook that is run after `org-occur' has constructed a sparse tree.
  450. This can be used to recenter the window to show as much of the structure
  451. as possible."
  452. :group 'org-sparse-trees
  453. :type 'hook)
  454. (defgroup org-plain-lists nil
  455. "Options concerning plain lists in Org-mode."
  456. :tag "Org Plain lists"
  457. :group 'org-structure)
  458. (defcustom org-cycle-include-plain-lists nil
  459. "Non-nil means, include plain lists into visibility cycling.
  460. This means that during cycling, plain list items will *temporarily* be
  461. interpreted as outline headlines with a level given by 1000+i where i is the
  462. indentation of the bullet. In all other operations, plain list items are
  463. not seen as headlines. For example, you cannot assign a TODO keyword to
  464. such an item."
  465. :group 'org-plain-lists
  466. :type 'boolean)
  467. (defcustom org-plain-list-ordered-item-terminator t
  468. "The character that makes a line with leading number an ordered list item.
  469. Valid values are ?. and ?\). To get both terminators, use t. While
  470. ?. may look nicer, it creates the danger that a line with leading
  471. number may be incorrectly interpreted as an item. ?\) therefore is
  472. the safe choice."
  473. :group 'org-plain-lists
  474. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  475. (const :tag "paren like in \"2)\"" ?\))
  476. (const :tab "both" t)))
  477. (defcustom org-auto-renumber-ordered-lists t
  478. "Non-nil means, automatically renumber ordered plain lists.
  479. Renumbering happens when the sequence have been changed with
  480. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  481. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  482. :group 'org-plain-lists
  483. :type 'boolean)
  484. (defcustom org-provide-checkbox-statistics t
  485. "Non-nil means, update checkbox statistics after insert and toggle.
  486. When this is set, checkbox statistics is updated each time you either insert
  487. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  488. with \\[org-ctrl-c-ctrl-c\\]."
  489. :group 'org
  490. :type 'boolean)
  491. (defgroup org-archive nil
  492. "Options concerning archiving in Org-mode."
  493. :tag "Org Archive"
  494. :group 'org-structure)
  495. (defcustom org-archive-tag "ARCHIVE"
  496. "The tag that marks a subtree as archived.
  497. An archived subtree does not open during visibility cycling, and does
  498. not contribute to the agenda listings."
  499. :group 'org-archive
  500. :group 'org-keywords
  501. :type 'string)
  502. (defcustom org-agenda-skip-archived-trees t
  503. "Non-nil means, the agenda will skip any items located in archived trees.
  504. An archived tree is a tree marked with the tag ARCHIVE."
  505. :group 'org-archive
  506. :group 'org-agenda-display
  507. :type 'boolean)
  508. (defcustom org-cycle-open-archived-trees nil
  509. "Non-nil means, `org-cycle' will open archived trees.
  510. An archived tree is a tree marked with the tag ARCHIVE.
  511. When nil, archived trees will stay folded. You can still open them with
  512. normal outline commands like `show-all', but not with the cycling commands."
  513. :group 'org-archive
  514. :group 'org-cycle
  515. :type 'boolean)
  516. (defcustom org-sparse-tree-open-archived-trees nil
  517. "Non-nil means sparse tree construction shows matches in archived trees.
  518. When nil, matches in these trees are highlighted, but the trees are kept in
  519. collapsed state."
  520. :group 'org-archive
  521. :group 'org-sparse-trees
  522. :type 'boolean)
  523. (defcustom org-archive-location "%s_archive::"
  524. "The location where subtrees should be archived.
  525. This string consists of two parts, separated by a double-colon.
  526. The first part is a file name - when omitted, archiving happens in the same
  527. file. %s will be replaced by the current file name (without directory part).
  528. Archiving to a different file is useful to keep archived entries from
  529. contributing to the Org-mode Agenda.
  530. The part after the double colon is a headline. The archived entries will be
  531. filed under that headline. When omitted, the subtrees are simply filed away
  532. at the end of the file, as top-level entries.
  533. Here are a few examples:
  534. \"%s_archive::\"
  535. If the current file is Projects.org, archive in file
  536. Projects.org_archive, as top-level trees. This is the default.
  537. \"::* Archived Tasks\"
  538. Archive in the current file, under the top-level headline
  539. \"* Archived Tasks\".
  540. \"~/org/archive.org::\"
  541. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  542. \"basement::** Finished Tasks\"
  543. Archive in file ./basement (relative path), as level 3 trees
  544. below the level 2 heading \"** Finished Tasks\".
  545. You may set this option on a per-file basis by adding to the buffer a
  546. line like
  547. #+ARCHIVE: basement::** Finished Tasks"
  548. :group 'org-archive
  549. :type 'string)
  550. (defcustom org-archive-mark-done t
  551. "Non-nil means, mark entries as DONE when they are moved to the archive file."
  552. :group 'org-archive
  553. :type 'boolean)
  554. (defcustom org-archive-stamp-time t
  555. "Non-nil means, add a time stamp to entries moved to an archive file.
  556. The time stamp will be added directly after the TODO state keyword in the
  557. first line, so it is probably best to use this in combinations with
  558. `org-archive-mark-done'."
  559. :group 'org-archive
  560. :type 'boolean)
  561. (defgroup org-table nil
  562. "Options concerning tables in Org-mode."
  563. :tag "Org Table"
  564. :group 'org)
  565. (defcustom org-enable-table-editor 'optimized
  566. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  567. When nil, such lines will be treated like ordinary lines.
  568. When equal to the symbol `optimized', the table editor will be optimized to
  569. do the following:
  570. - Automatic overwrite mode in front of whitespace in table fields.
  571. This makes the structure of the table stay in tact as long as the edited
  572. field does not exceed the column width.
  573. - Minimize the number of realigns. Normally, the table is aligned each time
  574. TAB or RET are pressed to move to another field. With optimization this
  575. happens only if changes to a field might have changed the column width.
  576. Optimization requires replacing the functions `self-insert-command',
  577. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  578. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  579. very good at guessing when a re-align will be necessary, but you can always
  580. force one with \\[org-ctrl-c-ctrl-c].
  581. If you would like to use the optimized version in Org-mode, but the
  582. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  583. This variable can be used to turn on and off the table editor during a session,
  584. but in order to toggle optimization, a restart is required.
  585. See also the variable `org-table-auto-blank-field'."
  586. :group 'org-table
  587. :type '(choice
  588. (const :tag "off" nil)
  589. (const :tag "on" t)
  590. (const :tag "on, optimized" optimized)))
  591. (defcustom orgtbl-optimized (eq org-enable-table-editor 'optimized)
  592. "Non-nil means, use the optimized table editor version for `orgtbl-mode'.
  593. In the optimized version, the table editor takes over all simple keys that
  594. normally just insert a character. In tables, the characters are inserted
  595. in a way to minimize disturbing the table structure (i.e. in overwrite mode
  596. for empty fields). Outside tables, the correct binding of the keys is
  597. restored.
  598. The default for this option is t if the optimized version is also used in
  599. Org-mode. See the variable `org-enable-table-editor' for details. Changing
  600. this variable requires a restart of Emacs to become effective."
  601. :group 'org-table
  602. :type 'boolean)
  603. (defgroup org-table-settings nil
  604. "Settings for tables in Org-mode."
  605. :tag "Org Table Settings"
  606. :group 'org-table)
  607. (defcustom org-table-default-size "5x2"
  608. "The default size for newly created tables, Columns x Rows."
  609. :group 'org-table-settings
  610. :type 'string)
  611. ;; FIXME: I am no longer sure if including HEX is a good idea.
  612. (defcustom org-table-number-regexp
  613. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*\\|\\(0[xX]\\)?[0-9a-fA-F]+\\)$"
  614. "Regular expression for recognizing numbers in table columns.
  615. If a table column contains mostly numbers, it will be aligned to the
  616. right. If not, it will be aligned to the left.
  617. The default value of this option is a regular expression which allows
  618. anything which looks remotely like a number as used in scientific
  619. context. For example, all of the following will be considered a
  620. number:
  621. 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
  622. Other options offered by the customize interface are more restrictive."
  623. :group 'org-table-settings
  624. :type '(choice
  625. (const :tag "Positive Integers"
  626. "^[0-9]+$")
  627. (const :tag "Integers"
  628. "^[-+]?[0-9]+$")
  629. (const :tag "Floating Point Numbers"
  630. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
  631. (const :tag "Floating Point Number or Integer"
  632. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
  633. (const :tag "Exponential, Floating point, Integer"
  634. "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
  635. (const :tag "Very General Number-Like, including hex"
  636. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*\\|\\(0[xX]\\)?[0-9a-fA-F]+\\)$")
  637. (string :tag "Regexp:")))
  638. (defcustom org-table-number-fraction 0.5
  639. "Fraction of numbers in a column required to make the column align right.
  640. In a column all non-white fields are considered. If at least this
  641. fraction of fields is matched by `org-table-number-fraction',
  642. alignment to the right border applies."
  643. :group 'org-table-settings
  644. :type 'number)
  645. (defgroup org-table-editing nil
  646. "Bahavior of tables during editing in Org-mode."
  647. :tag "Org Table Editing"
  648. :group 'org-table)
  649. (defcustom org-table-automatic-realign t
  650. "Non-nil means, automatically re-align table when pressing TAB or RETURN.
  651. When nil, aligning is only done with \\[org-table-align], or after column
  652. removal/insertion."
  653. :group 'org-table-editing
  654. :type 'boolean)
  655. (defcustom org-table-limit-column-width t ;kw
  656. "Non-nil means, allow to limit the width of table columns with <N> fields."
  657. :group 'org-table-editing
  658. :type 'boolean)
  659. (defcustom org-table-auto-blank-field t
  660. "Non-nil means, automatically blank table field when starting to type into it.
  661. This only happens when typing immediately after a field motion
  662. command (TAB, S-TAB or RET).
  663. Only relevant when `org-enable-table-editor' is equal to `optimized'."
  664. :group 'org-table-editing
  665. :type 'boolean)
  666. (defcustom org-table-tab-jumps-over-hlines t
  667. "Non-nil means, tab in the last column of a table with jump over a hline.
  668. If a horizontal separator line is following the current line,
  669. `org-table-next-field' can either create a new row before that line, or jump
  670. over the line. When this option is nil, a new line will be created before
  671. this line."
  672. :group 'org-table-editing
  673. :type 'boolean)
  674. (defcustom org-table-tab-recognizes-table.el t
  675. "Non-nil means, TAB will automatically notice a table.el table.
  676. When it sees such a table, it moves point into it and - if necessary -
  677. calls `table-recognize-table'."
  678. :group 'org-table-editing
  679. :type 'boolean)
  680. (defgroup org-table-calculation nil
  681. "Options concerning tables in Org-mode."
  682. :tag "Org Table Calculation"
  683. :group 'org-table)
  684. (defcustom org-table-copy-increment t
  685. "Non-nil means, increment when copying current field with \\[org-table-copy-down]."
  686. :group 'org-table-calculation
  687. :type 'boolean)
  688. (defcustom org-calc-default-modes
  689. '(calc-internal-prec 12
  690. calc-float-format (float 5)
  691. calc-angle-mode deg
  692. calc-prefer-frac nil
  693. calc-symbolic-mode nil
  694. calc-date-format (YYYY "-" MM "-" DD " " Www (" " HH ":" mm))
  695. calc-display-working-message t
  696. )
  697. "List with Calc mode settings for use in calc-eval for table formulas.
  698. The list must contain alternating symbols (Calc modes variables and values).
  699. Don't remove any of the default settings, just change the values. Org-mode
  700. relies on the variables to be present in the list."
  701. :group 'org-table-calculation
  702. :type 'plist)
  703. (defcustom org-table-formula-evaluate-inline t
  704. "Non-nil means, TAB and RET evaluate a formula in current table field.
  705. If the current field starts with an equal sign, it is assumed to be a formula
  706. which should be evaluated as described in the manual and in the documentation
  707. string of the command `org-table-eval-formula'. This feature requires the
  708. Emacs calc package.
  709. When this variable is nil, formula calculation is only available through
  710. the command \\[org-table-eval-formula]."
  711. :group 'org-table-calculation
  712. :type 'boolean)
  713. (defcustom org-table-formula-use-constants t
  714. "Non-nil means, interpret constants in formulas in tables.
  715. A constant looks like `$c' or `$Grav' and will be replaced before evaluation
  716. by the value given in `org-table-formula-constants', or by a value obtained
  717. from the `constants.el' package."
  718. :group 'org-table-calculation
  719. :type 'boolean)
  720. (defcustom org-table-formula-constants nil
  721. "Alist with constant names and values, for use in table formulas.
  722. The car of each element is a name of a constant, without the `$' before it.
  723. The cdr is the value as a string. For example, if you'd like to use the
  724. speed of light in a formula, you would configure
  725. (setq org-table-formula-constants '((\"c\" . \"299792458.\")))
  726. and then use it in an equation like `$1*$c'."
  727. :group 'org-table-calculation
  728. :type '(repeat
  729. (cons (string :tag "name")
  730. (string :tag "value"))))
  731. (defcustom org-table-formula-numbers-only nil
  732. "Non-nil means, calculate only with numbers in table formulas.
  733. Then all input fields will be converted to a number, and the result
  734. must also be a number. When nil, calc's full potential is available
  735. in table calculations, including symbolics etc."
  736. :group 'org-table-calculation
  737. :type 'boolean)
  738. (defcustom org-table-allow-automatic-line-recalculation t
  739. "Non-nil means, lines marked with |#| or |*| will be recomputed automatically.
  740. Automatically means, when TAB or RET or C-c C-c are pressed in the line."
  741. :group 'org-table-calculation
  742. :type 'boolean)
  743. (defgroup org-link nil
  744. "Options concerning links in Org-mode."
  745. :tag "Org Link"
  746. :group 'org)
  747. (defvar org-link-abbrev-alist-local nil
  748. "buffer-local version of `org-link-abbrev-alist', which see.
  749. The value of this is taken from the #+LINK lines.")
  750. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  751. (defcustom org-link-abbrev-alist nil
  752. "Alist of link abbreviations.
  753. The car of each element is a string, to be replaced at the start of a link.
  754. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  755. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  756. [[linkkey::tag][description]]
  757. If REPLACE is a string, the tag will simply be appended to create the link.
  758. If the string contains \"%s\", the tag will be inserted there. REPLACE may
  759. also be a function that will be called with the tag as the only argument to
  760. create the link. See the manual for examples."
  761. :group 'org-link
  762. :type 'alist)
  763. (defcustom org-descriptive-links t
  764. "Non-nil means, hide link part and only show description of bracket links.
  765. Bracket links are like [[link][descritpion]]. This variable sets the initial
  766. state in new org-mode buffers. The setting can then be toggled on a
  767. per-buffer basis from the Org->Hyperlinks menu."
  768. :group 'org-link
  769. :type 'boolean)
  770. (defcustom org-link-style 'bracket
  771. "The style of links to be inserted with \\[org-insert-link].
  772. Possible values are:
  773. bracket [[link][description]]. This is recommended
  774. plain Description \\n link. The old way, no longer recommended."
  775. :group 'org-link
  776. :type '(choice
  777. (const :tag "Bracket (recommended)" bracket)
  778. (const :tag "Plain (no longer recommended)" plain)))
  779. (defcustom org-link-format "%s"
  780. "Default format for external, URL-like linkes in the buffer.
  781. This is a format string for printf, %s will be replaced by the link text.
  782. The recommended value is just \"%s\", since links will be protected by
  783. enclosing them in double brackets. If you prefer plain links (see variable
  784. `org-link-style'), \"<%s>\" is useful. Some people also recommend an
  785. additional URL: prefix, so the format would be \"<URL:%s>\"."
  786. :group 'org-link
  787. :type '(choice
  788. (const :tag "\"%s\" (e.g. http://www.there.com)" "%s")
  789. (const :tag "\"<%s>\" (e.g. <http://www.there.com>)" "<%s>")
  790. (const :tag "\"<URL:%s>\" (e.g. <URL:http://www.there.com>)" "<URL:%s>")
  791. (string :tag "Other" :value "<%s>")))
  792. (defcustom org-link-file-path-type 'adaptive
  793. "How the path name in file links should be stored.
  794. Valid values are:
  795. relative relative to the current directory, i.e. the directory of the file
  796. into which the link is being inserted.
  797. absolute absolute path, if possible with ~ for home directory.
  798. noabbrev absolute path, no abbreviation of home directory.
  799. adaptive Use relative path for files in the current directory and sub-
  800. directories of it. For other files, use an absolute path."
  801. :group 'org-link
  802. :type '(choice
  803. (const relative)
  804. (const absolute)
  805. (const noabbrev)
  806. (const adaptive)))
  807. (defcustom org-activate-links '(bracket angle plain radio tag date)
  808. "Types of links that should be activated in Org-mode files.
  809. This is a list of symbols, each leading to the activation of a certain link
  810. type. In principle, it does not hurt to turn on most link types - there may
  811. be a small gain when turning off unused link types. The types are:
  812. bracket The recommended [[link][description]] or [[link]] links with hiding.
  813. angular Links in angular brackes that may contain whitespace like
  814. <bbdb:Carsten Dominik>.
  815. plain Plain links in normal text, no whitespace, like http://google.com.
  816. radio Text that is matched by a radio target, see manual for details.
  817. tag Tag settings in a headline (link to tag search).
  818. date Time stamps (link to calendar).
  819. camel CamelCase words defining text searches.
  820. Changing this variable requires a restart of Emacs to become effective."
  821. :group 'org-link
  822. :type '(set (const :tag "Double bracket links (new style)" bracket)
  823. (const :tag "Angular bracket links (old style)" angular)
  824. (const :tag "plain text links" plain)
  825. (const :tag "Radio target matches" radio)
  826. (const :tag "Tags" tag)
  827. (const :tag "Timestamps" date)
  828. (const :tag "CamelCase words" camel)))
  829. (defgroup org-link-store nil
  830. "Options concerning storing links in Org-mode"
  831. :tag "Org Store Link"
  832. :group 'org-link)
  833. (defcustom org-context-in-file-links t
  834. "Non-nil means, file links from `org-store-link' contain context.
  835. A search string will be added to the file name with :: as separator and
  836. used to find the context when the link is activated by the command
  837. `org-open-at-point'.
  838. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  839. negates this setting for the duration of the command."
  840. :group 'org-link-store
  841. :type 'boolean)
  842. (defcustom org-file-link-context-use-camel-case nil
  843. "Non-nil means, use CamelCase to store a search context in a file link.
  844. When nil, the search string simply consists of the words of the string.
  845. CamelCase is deprecated, and support for it may be dropped in the future."
  846. :group 'org-link-store
  847. :type 'boolean)
  848. (defcustom org-keep-stored-link-after-insertion nil
  849. "Non-nil means, keep link in list for entire session.
  850. The command `org-store-link' adds a link pointing to the current
  851. location to an internal list. These links accumulate during a session.
  852. The command `org-insert-link' can be used to insert links into any
  853. Org-mode file (offering completion for all stored links). When this
  854. option is nil, every link which has been inserted once using \\[org-insert-link]
  855. will be removed from the list, to make completing the unused links
  856. more efficient."
  857. :group 'org-link-store
  858. :type 'boolean)
  859. (defcustom org-usenet-links-prefer-google nil
  860. "Non-nil means, `org-store-link' will create web links to Google groups.
  861. When nil, Gnus will be used for such links.
  862. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  863. negates this setting for the duration of the command."
  864. :group 'org-link-store
  865. :type 'boolean)
  866. (defgroup org-link-follow nil
  867. "Options concerning following links in Org-mode"
  868. :tag "Org Follow Link"
  869. :group 'org-link)
  870. (defcustom org-tab-follows-link nil
  871. "Non-nil means, on links TAB will follow the link.
  872. Needs to be set before org.el is loaded."
  873. :group 'org-link-follow
  874. :type 'boolean)
  875. (defcustom org-return-follows-link nil
  876. "Non-nil means, on links RET will follow the link.
  877. Needs to be set before org.el is loaded."
  878. :group 'org-link-follow
  879. :type 'boolean)
  880. (defcustom org-mouse-1-follows-link t
  881. "Non-nil means, mouse-1 on a link will follow the link.
  882. A longer mouse click will still set point. Does not wortk on XEmacs.
  883. Needs to be set before org.el is loaded."
  884. :group 'org-link-follow
  885. :type 'boolean)
  886. (defcustom org-mark-ring-length 4
  887. "Number of different positions to be recorded in the ring
  888. Changing this requires a restart of Emacs to work correctly."
  889. :group 'org-link-follow
  890. :type 'interger)
  891. (defcustom org-link-frame-setup
  892. '((vm . vm-visit-folder-other-frame)
  893. (gnus . gnus-other-frame)
  894. (file . find-file-other-window))
  895. "Setup the frame configuration for following links.
  896. When following a link with Emacs, it may often be useful to display
  897. this link in another window or frame. This variable can be used to
  898. set this up for the different types of links.
  899. For VM, use any of
  900. `vm-visit-folder'
  901. `vm-visit-folder-other-frame'
  902. For Gnus, use any of
  903. `gnus'
  904. `gnus-other-frame'
  905. For FILE, use any of
  906. `find-file'
  907. `find-file-other-window'
  908. `find-file-other-frame'
  909. For the calendar, use the variable `calendar-setup'.
  910. For BBDB, it is currently only possible to display the matches in
  911. another window."
  912. :group 'org-link-follow
  913. :type '(list
  914. (cons (const vm)
  915. (choice
  916. (const vm-visit-folder)
  917. (const vm-visit-folder-other-window)
  918. (const vm-visit-folder-other-frame)))
  919. (cons (const gnus)
  920. (choice
  921. (const gnus)
  922. (const gnus-other-frame)))
  923. (cons (const file)
  924. (choice
  925. (const find-file)
  926. (const find-file-other-window)
  927. (const find-file-other-frame)))))
  928. (defcustom org-display-internal-link-with-indirect-buffer nil
  929. "Non-nil means, use indirect buffer to display infile links.
  930. Activating internal links (from one location in a file to another location
  931. in the same file) normally just jumps to the location. When the link is
  932. activated with a C-u prefix (or with mouse-3), the link is displayed in
  933. another window. When this option is set, the other window actually displays
  934. an indirect buffer clone of the current buffer, to avoid any visibility
  935. changes to the current buffer."
  936. :group 'org-link-follow
  937. :type 'boolean)
  938. (defcustom org-open-non-existing-files nil
  939. "Non-nil means, `org-open-file' will open non-existing file.
  940. When nil, an error will be generated."
  941. :group 'org-link-follow
  942. :type 'boolean)
  943. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  944. "Function and arguments to call for following mailto links.
  945. This is a list with the first element being a lisp function, and the
  946. remaining elements being arguments to the function. In string arguments,
  947. %a will be replaced by the address, and %s will be replaced by the subject
  948. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  949. :group 'org-link-follow
  950. :type '(choice
  951. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  952. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  953. (const :tag "message-mail" (message-mail "%a" "%s"))
  954. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  955. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  956. "Non-nil means, ask for confirmation before executing shell links.
  957. Shell links can be dangerous, just thing about a link
  958. [[shell:rm -rf ~/*][Google Search]]
  959. This link would show up in your Org-mode document as \"Google Search\"
  960. but really it would remove your entire home directory.
  961. Therefore I *definitely* advise against setting this variable to nil.
  962. Just change it to `y-or-n-p' of you want to confirm with a single key press
  963. rather than having to type \"yes\"."
  964. :group 'org-link-follow
  965. :type '(choice
  966. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  967. (const :tag "with y-or-n (faster)" y-or-n-p)
  968. (const :tag "no confirmation (dangerous)" nil)))
  969. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  970. "Non-nil means, ask for confirmation before executing elisp links.
  971. Elisp links can be dangerous, just think about a link
  972. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  973. This link would show up in your Org-mode document as \"Google Search\"
  974. but really it would remove your entire home directory.
  975. Therefore I *definitely* advise against setting this variable to nil.
  976. Just change it to `y-or-n-p' of you want to confirm with a single key press
  977. rather than having to type \"yes\"."
  978. :group 'org-link-follow
  979. :type '(choice
  980. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  981. (const :tag "with y-or-n (faster)" y-or-n-p)
  982. (const :tag "no confirmation (dangerous)" nil)))
  983. (defconst org-file-apps-defaults-gnu
  984. '((remote . emacs)
  985. (t . mailcap))
  986. "Default file applications on a UNIX or GNU/Linux system.
  987. See `org-file-apps'.")
  988. (defconst org-file-apps-defaults-macosx
  989. '((remote . emacs)
  990. (t . "open %s")
  991. ("ps" . "gv %s")
  992. ("ps.gz" . "gv %s")
  993. ("eps" . "gv %s")
  994. ("eps.gz" . "gv %s")
  995. ("dvi" . "xdvi %s")
  996. ("fig" . "xfig %s"))
  997. "Default file applications on a MacOS X system.
  998. The system \"open\" is known as a default, but we use X11 applications
  999. for some files for which the OS does not have a good default.
  1000. See `org-file-apps'.")
  1001. (defconst org-file-apps-defaults-windowsnt
  1002. (list
  1003. '(remote . emacs)
  1004. (cons t
  1005. (list (if (featurep 'xemacs)
  1006. 'mswindows-shell-execute
  1007. 'w32-shell-execute)
  1008. "open" 'file)))
  1009. "Default file applications on a Windows NT system.
  1010. The system \"open\" is used for most files.
  1011. See `org-file-apps'.")
  1012. (defcustom org-file-apps
  1013. '(
  1014. ("txt" . emacs)
  1015. ("tex" . emacs)
  1016. ("ltx" . emacs)
  1017. ("org" . emacs)
  1018. ("el" . emacs)
  1019. ("bib" . emacs)
  1020. )
  1021. "External applications for opening `file:path' items in a document.
  1022. Org-mode uses system defaults for different file types, but
  1023. you can use this variable to set the application for a given file
  1024. extension. The entries in this list are cons cells where the car identifies
  1025. files and the cdr the corresponding command. Possible values for the
  1026. file identifier are
  1027. \"ext\" A string identifying an extension
  1028. `directory' Matches a directory
  1029. `remote' Matches a remote file, accessible through tramp or efs.
  1030. Remote files most likely should be visited through Emacs
  1031. because external applications cannot handle such paths.
  1032. t Default for all remaining files
  1033. Possible values for the command are:
  1034. `emacs' The file will be visited by the current Emacs process.
  1035. `default' Use the default application for this file type.
  1036. string A command to be executed by a shell; %s will be replaced
  1037. by the path to the file.
  1038. sexp A Lisp form which will be evaluated. The file path will
  1039. be available in the Lisp variable `file'.
  1040. For more examples, see the system specific constants
  1041. `org-file-apps-defaults-macosx'
  1042. `org-file-apps-defaults-windowsnt'
  1043. `org-file-apps-defaults-gnu'."
  1044. :group 'org-link-follow
  1045. :type '(repeat
  1046. (cons (choice :value ""
  1047. (string :tag "Extension")
  1048. (const :tag "Default for unrecognized files" t)
  1049. (const :tag "Remote file" remote)
  1050. (const :tag "Links to a directory" directory))
  1051. (choice :value ""
  1052. (const :tag "Visit with Emacs" emacs)
  1053. (const :tag "Use system default" default)
  1054. (string :tag "Command")
  1055. (sexp :tag "Lisp form")))))
  1056. (defcustom org-mhe-search-all-folders nil
  1057. "Non-nil means, that the search for the mh-message will be extended to
  1058. all folders if the message cannot be found in the folder given in the link.
  1059. Searching all folders is very effective with one of the search engines
  1060. supported by MH-E, but will be slow with pick."
  1061. :group 'org-link-follow
  1062. :type 'boolean)
  1063. (defgroup org-remember nil
  1064. "Options concerning interaction with remember.el."
  1065. :tag "Org Remember"
  1066. :group 'org)
  1067. (defcustom org-directory "~/org"
  1068. "Directory with org files.
  1069. This directory will be used as default to prompt for org files.
  1070. Used by the hooks for remember.el."
  1071. :group 'org-remember
  1072. :type 'directory)
  1073. (defcustom org-default-notes-file "~/.notes"
  1074. "Default target for storing notes.
  1075. Used by the hooks for remember.el. This can be a string, or nil to mean
  1076. the value of `remember-data-file'."
  1077. :group 'org-remember
  1078. :type '(choice
  1079. (const :tag "Default from remember-data-file" nil)
  1080. file))
  1081. (defcustom org-remember-templates nil
  1082. "Templates for the creation of remember buffers.
  1083. When nil, just let remember make the buffer.
  1084. When not nil, this is a list of 3-element lists. In each entry, the first
  1085. element is a character, a unique key to select this template.
  1086. The second element is the template. The third element is optional and can
  1087. specify a destination file for remember items created with this template.
  1088. The default file is given by `org-default-notes-file'.
  1089. The template specifies the structure of the remember buffer. It should have
  1090. a first line starting with a star, to act as the org-mode headline.
  1091. Furthermore, the following %-escapes will be replaced with content:
  1092. %t time stamp, date only
  1093. %T time stamp with date and time
  1094. %u inactive time stamp, date only
  1095. %U inactive time stamp with date and time
  1096. %n user name
  1097. %a annotation, normally the link created with org-store-link
  1098. %i initial content, the region when remember is called with C-u.
  1099. If %i is indented, the entire inserted text will be indented as well.
  1100. %? This will be removed, and the cursor placed at this position."
  1101. :group 'org-remember
  1102. :type '(repeat :tag "enabled"
  1103. (list :value (?a "\n" nil)
  1104. (character :tag "Selection Key")
  1105. (string :tag "Template")
  1106. (file :tag "Destination file (optional)"))))
  1107. (defcustom org-reverse-note-order nil
  1108. "Non-nil means, store new notes at the beginning of a file or entry.
  1109. When nil, new notes will be filed to the end of a file or entry."
  1110. :group 'org-remember
  1111. :type '(choice
  1112. (const :tag "Reverse always" t)
  1113. (const :tag "Reverse never" nil)
  1114. (repeat :tag "By file name regexp"
  1115. (cons regexp boolean))))
  1116. (defgroup org-todo nil
  1117. "Options concerning TODO items in Org-mode."
  1118. :tag "Org TODO"
  1119. :group 'org)
  1120. (defcustom org-todo-keywords '("TODO" "DONE")
  1121. "List of TODO entry keywords.
  1122. \\<org-mode-map>By default, this is '(\"TODO\" \"DONE\"). The last entry in the list is
  1123. considered to mean that the entry is \"done\". All the other mean that
  1124. action is required, and will make the entry show up in todo lists, diaries
  1125. etc.
  1126. The command \\[org-todo] cycles an entry through these states, and an
  1127. additional state where no keyword is present. For details about this
  1128. cycling, see also the variable `org-todo-interpretation'
  1129. Changes become only effective after restarting Emacs."
  1130. :group 'org-todo
  1131. :group 'org-keywords
  1132. :type '(repeat (string :tag "Keyword")))
  1133. (defcustom org-todo-interpretation 'sequence
  1134. "Controls how TODO keywords are interpreted.
  1135. This variable is only relevant if `org-todo-keywords' contains more than two
  1136. states. \\<org-mode-map>Possible values are `sequence' and `type'.
  1137. When `sequence', \\[org-todo] will always switch to the next state in the
  1138. `org-todo-keywords' list. When `type', \\[org-todo] only cycles from state
  1139. to state when executed several times in direct succession. Otherwise, it
  1140. switches directly to DONE from any state.
  1141. See the manual for more information."
  1142. :group 'org-todo
  1143. :group 'org-keywords
  1144. :type '(choice (const sequence)
  1145. (const type)))
  1146. (defcustom org-after-todo-state-change-hook nil
  1147. "Hook which is run after the state of a TODO item was changed.
  1148. The new state (a string with a TODO keyword, or nil) is available in the
  1149. Lisp variable `state'."
  1150. :group 'org-todo
  1151. :type 'hook)
  1152. (defcustom org-log-done nil
  1153. "When set, insert a (non-active) time stamp when TODO entry is marked DONE.
  1154. When the state of an entry is changed from nothing to TODO, remove a previous
  1155. closing date.
  1156. This can also be a list of symbols indicating under which conditions
  1157. the time stamp recording the action should be annotated with a short note.
  1158. Valid members of this list are
  1159. done Offer to record a note when marking entries done
  1160. clock-out Offer to record a note when clocking out of an item.
  1161. A separate window will then pop up and allow you to type a note.
  1162. After finishing with C-c C-c, the note will be added directly after the
  1163. timestamp, as a plain list item. See also the variable
  1164. `org-log-note-headings'.
  1165. Logging can also be configured on a per-file basis by adding one of
  1166. the following lines anywhere in the buffer:
  1167. #+STARTUP: logging
  1168. #+STARTUP: nologging"
  1169. ;; FIXME: in-buffer words for notes???????
  1170. :group 'org-todo
  1171. :type '(choice
  1172. (const :tag "off" nil)
  1173. (const :tag "on" t)
  1174. (set :tag "on, with notes" :greedy t :value (done)
  1175. (const done) (const clock-out))))
  1176. (defcustom org-log-note-headings '((done . "CLOSING NOTE %t") (clock-out . ""))
  1177. "Headings for notes added when clocking out or closing TODO items.
  1178. The value is an alist, with the car being a sympol indicating the note
  1179. context, and the cdr is the heading to be used. The heading may also be the
  1180. empty string.
  1181. %t in the heading will be replaced by a time stamp
  1182. %u will be replaced by the user name
  1183. %U will be replaced by the full user name."
  1184. :group 'org-todo
  1185. :type '(list :greedy t
  1186. (cons (const :tag "Heading when closing an item" done) string)
  1187. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1188. (defgroup org-priorities nil
  1189. "Priorities in Org-mode."
  1190. :tag "Org Priorities"
  1191. :group 'org-todo)
  1192. (defcustom org-default-priority ?B
  1193. "The default priority of TODO items.
  1194. This is the priority an item get if no explicit priority is given."
  1195. :group 'org-priorities
  1196. :type 'character)
  1197. (defcustom org-lowest-priority ?C
  1198. "The lowest priority of TODO items. A character like ?A, ?B etc."
  1199. :group 'org-priorities
  1200. :type 'character)
  1201. (defgroup org-time nil
  1202. "Options concerning time stamps and deadlines in Org-mode."
  1203. :tag "Org Time"
  1204. :group 'org)
  1205. (defcustom org-insert-labeled-timestamps-at-point nil
  1206. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1207. When nil, these labeled time stamps are forces into the second line of an
  1208. entry, just after the headline. When scheduling from the global TODO list,
  1209. the time stamp will always be forced into the second line."
  1210. :group 'org-time
  1211. :type 'boolean)
  1212. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1213. "Formats for `format-time-string' which are used for time stamps.
  1214. It is not recommended to change this constant.")
  1215. (defcustom org-time-stamp-rounding-minutes 0
  1216. "Number of minutes to round time stamps to upon insertion.
  1217. When zero, insert the time unmodified. Useful rounding numbers
  1218. should be factors of 60, so for example 5, 10, 15.
  1219. When this is not zero, you can still force an exact time-stamp by using
  1220. a double prefix argument to a time-stamp command like `C-c .' or `C-c !'."
  1221. :group 'org-time
  1222. :type 'integer)
  1223. (defcustom org-display-custom-times nil
  1224. "Non-nil means, overlay custom formats over all time stamps.
  1225. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1226. To turn this on on a per-file basis, insert anywhere in the file:
  1227. #+STARTUP: customtime"
  1228. :group 'org-time
  1229. :set 'set-default
  1230. :type 'sexp)
  1231. (make-variable-buffer-local 'org-display-custom-times)
  1232. (defcustom org-time-stamp-custom-formats
  1233. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1234. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1235. These are overlayed over the default ISO format if the variable
  1236. `org-display-custom-times' is set."
  1237. :group 'org-time
  1238. :type 'sexp)
  1239. (defun org-time-stamp-format (&optional long inactive)
  1240. "Get the right format for a time string."
  1241. (let ((f (if long (cdr org-time-stamp-formats)
  1242. (car org-time-stamp-formats))))
  1243. (if inactive
  1244. (concat "[" (substring f 1 -1) "]")
  1245. f)))
  1246. (defcustom org-deadline-warning-days 30
  1247. "No. of days before expiration during which a deadline becomes active.
  1248. This variable governs the display in sparse trees and in the agenda."
  1249. :group 'org-time
  1250. :type 'number)
  1251. (defcustom org-popup-calendar-for-date-prompt t
  1252. "Non-nil means, pop up a calendar when prompting for a date.
  1253. In the calendar, the date can be selected with mouse-1. However, the
  1254. minibuffer will also be active, and you can simply enter the date as well.
  1255. When nil, only the minibuffer will be available."
  1256. :group 'org-time
  1257. :type 'boolean)
  1258. (defcustom org-calendar-follow-timestamp-change t
  1259. "Non-nil means, make the calendar window follow timestamp changes.
  1260. When a timestamp is modified and the calendar window is visible, it will be
  1261. moved to the new date."
  1262. :group 'org-time
  1263. :type 'boolean)
  1264. (defgroup org-tags nil
  1265. "Options concerning tags in Org-mode."
  1266. :tag "Org Tags"
  1267. :group 'org)
  1268. (defcustom org-tag-alist nil
  1269. "List of tags allowed in Org-mode files.
  1270. When this list is nil, Org-mode will base TAG input on what is already in the
  1271. buffer.
  1272. The value of this variable is an alist, the car may be (and should) be a
  1273. character that is used to select that tag through the fast-tag-selection
  1274. interface. See the manual for details."
  1275. :group 'org-tags
  1276. :type '(repeat
  1277. (choice
  1278. (cons (string :tag "Tag name")
  1279. (character :tag "Access char"))
  1280. (const :tag "Start radio group" (:startgroup))
  1281. (const :tag "End radio group" (:endgroup)))))
  1282. (defcustom org-use-fast-tag-selection 'auto
  1283. "Non-nil means, use fast tag selection scheme.
  1284. This is a special interface to select and deselect tags with single keys.
  1285. When nil, fast selection is never used.
  1286. When the symbol `auto', fast selection is used if and only if selection
  1287. characters for tags have been configured, either through the variable
  1288. `org-tag-alist' or through a #+TAGS line in the buffer.
  1289. When t, fast selection is always used and selection keys are assigned
  1290. automatically if necessary."
  1291. :group 'org-tags
  1292. :type '(choice
  1293. (const :tag "Always" t)
  1294. (const :tag "Never" nil)
  1295. (const :tag "When selection characters are configured" 'auto)))
  1296. (defcustom org-fast-tag-selection-single-key nil
  1297. "Non-nil means, fast tag selection exits after first change.
  1298. When nil, you have to press RET to exit it.
  1299. During fast tag selection, you can toggle this flag with `C-c'."
  1300. :group 'org-tags
  1301. :type 'boolean)
  1302. (defcustom org-tags-column 48
  1303. "The column to which tags should be indented in a headline.
  1304. If this number is positive, it specifies the column. If it is negative,
  1305. it means that the tags should be flushright to that column. For example,
  1306. -79 works well for a normal 80 character screen."
  1307. :group 'org-tags
  1308. :type 'integer)
  1309. (defcustom org-auto-align-tags t
  1310. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1311. These operations change the length of a headline and therefore shift
  1312. the tags around. With this options turned on, after each such operation
  1313. the tags are again aligned to `org-tags-column'."
  1314. :group 'org-tags
  1315. :type 'boolean)
  1316. (defcustom org-use-tag-inheritance t
  1317. "Non-nil means, tags in levels apply also for sublevels.
  1318. When nil, only the tags directly given in a specific line apply there.
  1319. If you turn off this option, you very likely want to turn on the
  1320. companion option `org-tags-match-list-sublevels'."
  1321. :group 'org-tags
  1322. :type 'boolean)
  1323. (defcustom org-tags-match-list-sublevels nil
  1324. "Non-nil means list also sublevels of headlines matching tag search.
  1325. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1326. the sublevels of a headline matching a tag search often also match
  1327. the same search. Listing all of them can create very long lists.
  1328. Setting this variable to nil causes subtrees of a match to be skipped.
  1329. This option is off by default, because inheritance in on. If you turn
  1330. inheritance off, you very likely want to turn this option on.
  1331. As a special case, if the tag search is restricted to TODO items, the
  1332. value of this variable is ignored and sublevels are always checked, to
  1333. make sure all corresponding TODO items find their way into the list."
  1334. :group 'org-tags
  1335. :type 'boolean)
  1336. (defvar org-tags-history nil
  1337. "History of minibuffer reads for tags.")
  1338. (defvar org-last-tags-completion-table nil
  1339. "The last used completion table for tags.")
  1340. (defgroup org-agenda nil
  1341. "Options concerning agenda display Org-mode."
  1342. :tag "Org Agenda"
  1343. :group 'org)
  1344. (defvar org-category nil
  1345. "Variable used by org files to set a category for agenda display.
  1346. Such files should use a file variable to set it, for example
  1347. -*- mode: org; org-category: \"ELisp\"
  1348. or contain a special line
  1349. #+CATEGORY: ELisp
  1350. If the file does not specify a category, then file's base name
  1351. is used instead.")
  1352. (make-variable-buffer-local 'org-category)
  1353. (defcustom org-agenda-files nil
  1354. "The files to be used for agenda display.
  1355. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1356. \\[org-remove-file]. You can also use customize to edit the list.
  1357. If the value of the variable is not a list but a single file name, then
  1358. the list of agenda files is actually stored and maintained in that file, one
  1359. agenda file per line."
  1360. :group 'org-agenda
  1361. :type '(choice
  1362. (repeat :tag "List of files" file)
  1363. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1364. (defcustom org-agenda-custom-commands '(("w" todo "WAITING"))
  1365. "Custom commands for the agenda.
  1366. These commands will be offered on the splash screen displayed by the
  1367. agenda dispatcher \\[org-agenda]. Each entry is a list like this:
  1368. (key type match options)
  1369. key The key (a single char as a string) to be associated with the command.
  1370. type The command type, any of the following symbols:
  1371. todo Entries with a specific TODO keyword, in all agenda files.
  1372. tags Tags match in all agenda files.
  1373. tags-todo Tags match in all agenda files, TODO entries only.
  1374. todo-tree Sparse tree of specific TODO keyword in *current* file.
  1375. tags-tree Sparse tree with all tags matches in *current* file.
  1376. occur-tree Occur sparse tree for *current* file.
  1377. match What to search for:
  1378. - a single keyword for TODO keyword searches
  1379. - a tags match expression for tags searches
  1380. - a regular expression for occur searches
  1381. options A list of option setttings, similar to that in a let form, so like
  1382. this: ((opt1 val1) (opt2 val2) ...)
  1383. You can also define a set of commands, to create a composite agenda buffer.
  1384. In this case, an entry looks like this:
  1385. (key desc (cmd1 cmd2 ...) general-options)
  1386. where
  1387. desc A description string to be displayed in the dispatcher menu.
  1388. cmd An agenda command, similar to the above. However, tree commands
  1389. are no allowed, but instead you can get agenda and global todo list.
  1390. So valid commands for a set are:
  1391. (agenda)
  1392. (alltodo)
  1393. (todo \"match\" options)
  1394. (tags \"match\" options )
  1395. (tags-todo \"match\" options)
  1396. Each command can carry a list of options, and another set of options can be
  1397. given for the whole set of commands. Individual command options take
  1398. precedence over the general options."
  1399. :group 'org-agenda
  1400. :type '(repeat
  1401. (choice
  1402. (list :tag "Single command"
  1403. (string :tag "Key")
  1404. (choice
  1405. (const :tag "Tags search (all agenda files)" tags)
  1406. (const :tag "Tags search of TODO entries (all agenda files)" tags-todo)
  1407. (const :tag "TODO keyword search (all agenda files)" todo)
  1408. (const :tag "Tags sparse tree (current buffer)" tags-tree)
  1409. (const :tag "TODO keyword tree (current buffer)" todo-tree)
  1410. (const :tag "Occur tree (current buffer)" occur-tree))
  1411. (string :tag "Match")
  1412. (repeat :tag "Local options"
  1413. (list (variable :tag "Option") (sexp :tag "Value"))))
  1414. (list :tag "Command series, all agenda files"
  1415. (string :tag "Key")
  1416. (string :tag "Description")
  1417. (repeat
  1418. (choice
  1419. (const :tag "Agenda" (agenda))
  1420. (const :tag "TODO list" (alltodo))
  1421. (list :tag "Tags search"
  1422. (const :format "" tags)
  1423. (string :tag "Match")
  1424. (repeat :tag "Local options"
  1425. (list (variable :tag "Option")
  1426. (sexp :tag "Value"))))
  1427. (list :tag "Tags search, TODO entries only"
  1428. (const :format "" tags-todo)
  1429. (string :tag "Match")
  1430. (repeat :tag "Local options"
  1431. (list (variable :tag "Option")
  1432. (sexp :tag "Value"))))
  1433. (list :tag "TODO keyword search"
  1434. (const :format "" todo)
  1435. (string :tag "Match")
  1436. (repeat :tag "Local options"
  1437. (list (variable :tag "Option")
  1438. (sexp :tag "Value"))))))
  1439. (repeat :tag "General options"
  1440. (list (variable :tag "Option")
  1441. (sexp :tag "Value")))))))
  1442. (defcustom org-agenda-todo-list-sublevels t
  1443. "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
  1444. When nil, the sublevels of a TODO entry are not checked, resulting in
  1445. potentially much shorter TODO lists."
  1446. :group 'org-agenda
  1447. :group 'org-todo
  1448. :type 'boolean)
  1449. (defcustom org-agenda-todo-ignore-scheduled nil
  1450. "Non-nil means, don't show scheduled entries in the global todo list.
  1451. The idea behind this is that by scheduling it, you have already taken care
  1452. of this item."
  1453. :group 'org-agenda
  1454. :group 'org-todo
  1455. :type 'boolean)
  1456. (defcustom org-agenda-todo-ignore-deadlines nil
  1457. "Non-nil means, don't show near deadline entries in the global todo list.
  1458. Near means closer than `org-deadline-warning-days' days.
  1459. The idea behind this is that such items will appear in the agenda anyway."
  1460. :group 'org-agenda
  1461. :group 'org-todo
  1462. :type 'boolean)
  1463. (defcustom org-agenda-skip-scheduled-if-done nil
  1464. "Non-nil means don't show scheduled items in agenda when they are done.
  1465. This is relevant for the daily/weekly agenda, not for the TODO list."
  1466. :group 'org-agenda
  1467. :type 'boolean)
  1468. (defcustom org-timeline-show-empty-dates 3
  1469. "Non-nil means, `org-timeline' also shows dates without an entry.
  1470. When nil, only the days which actually have entries are shown.
  1471. When t, all days between the first and the last date are shown.
  1472. When an integer, show also empty dates, but if there is a gap of more than
  1473. N days, just insert a special line indicating the size of the gap."
  1474. :group 'org-agenda
  1475. :type '(choice
  1476. (const :tag "None" nil)
  1477. (const :tag "All" t)
  1478. (number :tag "at most")))
  1479. (defcustom org-agenda-confirm-kill 1
  1480. "When set, remote killing from the agenda buffer needs confirmation.
  1481. When t, a confirmation is always needed. When a number N, confirmation is
  1482. only needed when the text to be killed contains more than N non-white lines."
  1483. :group 'org-agenda
  1484. :type '(choice
  1485. (const :tag "Never" nil)
  1486. (const :tag "Always" t)
  1487. (number :tag "When more than N lines")))
  1488. ;; FIXME: This variable could be removed
  1489. (defcustom org-agenda-include-all-todo nil
  1490. "Set means weekly/daily agenda will always contain all TODO entries.
  1491. The TODO entries will be listed at the top of the agenda, before
  1492. the entries for specific days."
  1493. :group 'org-agenda
  1494. :type 'boolean)
  1495. (defcustom org-agenda-include-diary nil
  1496. "If non-nil, include in the agenda entries from the Emacs Calendar's diary."
  1497. :group 'org-agenda
  1498. :type 'boolean)
  1499. (defcustom org-calendar-to-agenda-key [?c]
  1500. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1501. The command `org-calendar-goto-agenda' will be bound to this key. The
  1502. default is the character `c' because then `c' can be used to switch back and
  1503. forth between agenda and calendar."
  1504. :group 'org-agenda
  1505. :type 'sexp)
  1506. (defgroup org-agenda-setup nil
  1507. "Options concerning setting up the Agenda window in Org Mode."
  1508. :tag "Org Agenda Window Setup"
  1509. :group 'org-agenda)
  1510. (defcustom org-agenda-window-setup 'reorganize-frame
  1511. "How the agenda buffer should be displayed.
  1512. Possible values for this option are:
  1513. current-window Show agenda in the current window, keeping all other windows.
  1514. other-frame Use `switch-to-buffer-other-frame' to display agenda.
  1515. other-window Use `switch-to-buffer-other-window' to display agenda.
  1516. reorganize-frame Show only two windows on the current frame, the current
  1517. window and the agenda. Also, if the option
  1518. `org-fit-agenda-window' is set, resize the agenda window to
  1519. try to show as much as possible of the buffer content.
  1520. See also the variable `org-agenda-restore-windows-after-quit'."
  1521. :group 'org-agenda-setup
  1522. :type '(choice
  1523. (const current-window)
  1524. (const other-frame)
  1525. (const other-window)
  1526. (const reorganize-frame)))
  1527. (defcustom org-agenda-restore-windows-after-quit nil
  1528. "Non-nil means, restore window configuration open exiting agenda.
  1529. Before the window configuration is changed for displaying the agenda,
  1530. the current status is recorded. When the agenda is exited with
  1531. `q' or `x' and this option is set, the old state is restored. If
  1532. `org-agenda-window-setup' is `other-frame', the value of this
  1533. option will be ignored.."
  1534. :group 'org-agenda-setup
  1535. :type 'boolean)
  1536. ;; FIXME: I think this variable could be removed.
  1537. (defcustom org-select-agenda-window t
  1538. "Non-nil means, after creating an agenda, move cursor into Agenda window.
  1539. When nil, cursor will remain in the current window."
  1540. :group 'org-agenda-setup
  1541. :type 'boolean)
  1542. ;; FIXME: I think this variable could be removed.
  1543. (defcustom org-fit-agenda-window t
  1544. "Non-nil means, change window size of agenda to fit content.
  1545. This is only effective if `org-agenda-window-setup' is `reorganize-frame'."
  1546. :group 'org-agenda-setup
  1547. :type 'boolean)
  1548. (defcustom org-finalize-agenda-hook nil
  1549. "Hook run just before displaying an agenda buffer."
  1550. :group 'org-agenda-setup
  1551. :type 'hook)
  1552. (defcustom org-agenda-mouse-1-follows-link nil
  1553. "Non-nil means, mouse-1 on a link will follow the link in the agenda.
  1554. A longer mouse click will still set point. Does not wortk on XEmacs.
  1555. Needs to be set before org.el is loaded."
  1556. :group 'org-agenda-setup
  1557. :type 'boolean)
  1558. (defcustom org-agenda-start-with-follow-mode nil
  1559. "The initial value of follwo-mode in a newly created agenda window."
  1560. :group 'org-agenda-setup
  1561. :type 'boolean)
  1562. (defgroup org-agenda-display nil
  1563. "Options concerning what to display initially in Agenda."
  1564. :tag "Org Agenda Display"
  1565. :group 'org-agenda)
  1566. (defcustom org-agenda-show-all-dates t
  1567. "Non-nil means, `org-agenda' shows every day in the selected range.
  1568. When nil, only the days which actually have entries are shown."
  1569. :group 'org-agenda-display
  1570. :type 'boolean)
  1571. (defcustom org-agenda-start-on-weekday 1
  1572. "Non-nil means, start the overview always on the specified weekday.
  1573. 0 denotes Sunday, 1 denotes Monday etc.
  1574. When nil, always start on the current day."
  1575. :group 'org-agenda-display
  1576. :type '(choice (const :tag "Today" nil)
  1577. (number :tag "Weekday No.")))
  1578. (defcustom org-agenda-ndays 7
  1579. "Number of days to include in overview display.
  1580. Should be 1 or 7."
  1581. :group 'org-agenda-display
  1582. :type 'number)
  1583. (defcustom org-agenda-use-time-grid t
  1584. "Non-nil means, show a time grid in the agenda schedule.
  1585. A time grid is a set of lines for specific times (like every two hours between
  1586. 8:00 and 20:00). The items scheduled for a day at specific times are
  1587. sorted in between these lines.
  1588. For details about when the grid will be shown, and what it will look like, see
  1589. the variable `org-agenda-time-grid'."
  1590. :group 'org-agenda-display
  1591. :type 'boolean)
  1592. (defcustom org-agenda-time-grid
  1593. '((daily today require-timed)
  1594. "----------------"
  1595. (800 1000 1200 1400 1600 1800 2000))
  1596. "The settings for time grid for agenda display.
  1597. This is a list of three items. The first item is again a list. It contains
  1598. symbols specifying conditions when the grid should be displayed:
  1599. daily if the agenda shows a single day
  1600. weekly if the agenda shows an entire week
  1601. today show grid on current date, independent of daily/weekly display
  1602. require-timed show grid only if at least one item has a time specification
  1603. The second item is a string which will be places behing the grid time.
  1604. The third item is a list of integers, indicating the times that should have
  1605. a grid line."
  1606. :group 'org-agenda-display
  1607. :type
  1608. '(list
  1609. (set :greedy t :tag "Grid Display Options"
  1610. (const :tag "Show grid in single day agenda display" daily)
  1611. (const :tag "Show grid in weekly agenda display" weekly)
  1612. (const :tag "Always show grid for today" today)
  1613. (const :tag "Show grid only if any timed entries are present"
  1614. require-timed)
  1615. (const :tag "Skip grid times already present in an entry"
  1616. remove-match))
  1617. (string :tag "Grid String")
  1618. (repeat :tag "Grid Times" (integer :tag "Time"))))
  1619. (let ((sorting-choice
  1620. '(choice
  1621. (const time-up) (const time-down)
  1622. (const category-keep) (const category-up) (const category-down)
  1623. (const tag-down) (const tag-up)
  1624. (const priority-up) (const priority-down))))
  1625. (defcustom org-agenda-sorting-strategy
  1626. '((agenda time-up category-keep priority-down)
  1627. (todo category-keep priority-down)
  1628. (tags category-keep))
  1629. "Sorting structure for the agenda items of a single day.
  1630. This is a list of symbols which will be used in sequence to determine
  1631. if an entry should be listed before another entry. The following
  1632. symbols are recognized:
  1633. time-up Put entries with time-of-day indications first, early first
  1634. time-down Put entries with time-of-day indications first, late first
  1635. category-keep Keep the default order of categories, corresponding to the
  1636. sequence in `org-agenda-files'.
  1637. category-up Sort alphabetically by category, A-Z.
  1638. category-down Sort alphabetically by category, Z-A.
  1639. tag-up Sort alphabetically by last tag, A-Z.
  1640. tag-down Sort alphabetically by last tag, Z-A.
  1641. priority-up Sort numerically by priority, high priority last.
  1642. priority-down Sort numerically by priority, high priority first.
  1643. The different possibilities will be tried in sequence, and testing stops
  1644. if one comparison returns a \"not-equal\". For example, the default
  1645. '(time-up category-keep priority-down)
  1646. means: Pull out all entries having a specified time of day and sort them,
  1647. in order to make a time schedule for the current day the first thing in the
  1648. agenda listing for the day. Of the entries without a time indication, keep
  1649. the grouped in categories, don't sort the categories, but keep them in
  1650. the sequence given in `org-agenda-files'. Within each category sort by
  1651. priority.
  1652. Leaving out `category-keep' would mean that items will be sorted across
  1653. categories by priority."
  1654. :group 'org-agenda-display
  1655. :type `(choice
  1656. (repeat :tag "General" ,sorting-choice)
  1657. (list :tag "Individually"
  1658. (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
  1659. (repeat ,sorting-choice))
  1660. (cons (const :tag "Strategy for TODO lists" todo)
  1661. (repeat ,sorting-choice))
  1662. (cons (const :tag "Strategy for Tags matches" tags)
  1663. (repeat ,sorting-choice))))))
  1664. (defcustom org-sort-agenda-notime-is-late t
  1665. "Non-nil means, items without time are considered late.
  1666. This is only relevant for sorting. When t, items which have no explicit
  1667. time like 15:30 will be considered as 99:01, i.e. later than any items which
  1668. do have a time. When nil, the default time is before 0:00. You can use this
  1669. option to decide if the schedule for today should come before or after timeless
  1670. agenda entries."
  1671. :group 'org-agenda-display
  1672. :type 'boolean)
  1673. (defgroup org-agenda-prefix nil
  1674. "Options concerning the entry prefix in the Org-mode agenda display."
  1675. :tag "Org Agenda Prefix"
  1676. :group 'org-agenda)
  1677. (defcustom org-agenda-prefix-format
  1678. '((agenda . " %-12:c%?-12t% s")
  1679. (timeline . " % s")
  1680. (todo . " %-12:c")
  1681. (tags . " %-12:c"))
  1682. "Format specifications for the prefix of items in the agenda views.
  1683. An alist with four entries, for the different agenda types. The keys to the
  1684. sublists are `agenda', `timeline', `todo', and `tags'. The values
  1685. are format strings.
  1686. This format works similar to a printf format, with the following meaning:
  1687. %c the category of the item, \"Diary\" for entries from the diary, or
  1688. as given by the CATEGORY keyword or derived from the file name.
  1689. %T the *last* tag of the item. Last because inherited tags come
  1690. first in the list.
  1691. %t the time-of-day specification if one applies to the entry, in the
  1692. format HH:MM
  1693. %s Scheduling/Deadline information, a short string
  1694. All specifiers work basically like the standard `%s' of printf, but may
  1695. contain two additional characters: A question mark just after the `%' and
  1696. a whitespace/punctuation character just before the final letter.
  1697. If the first character after `%' is a question mark, the entire field
  1698. will only be included if the corresponding value applies to the
  1699. current entry. This is useful for fields which should have fixed
  1700. width when present, but zero width when absent. For example,
  1701. \"%?-12t\" will result in a 12 character time field if a time of the
  1702. day is specified, but will completely disappear in entries which do
  1703. not contain a time.
  1704. If there is punctuation or whitespace character just before the final
  1705. format letter, this character will be appended to the field value if
  1706. the value is not empty. For example, the format \"%-12:c\" leads to
  1707. \"Diary: \" if the category is \"Diary\". If the category were be
  1708. empty, no additional colon would be interted.
  1709. The default value of this option is \" %-12:c%?-12t% s\", meaning:
  1710. - Indent the line with two space characters
  1711. - Give the category in a 12 chars wide field, padded with whitespace on
  1712. the right (because of `-'). Append a colon if there is a category
  1713. (because of `:').
  1714. - If there is a time-of-day, put it into a 12 chars wide field. If no
  1715. time, don't put in an empty field, just skip it (because of '?').
  1716. - Finally, put the scheduling information and append a whitespace.
  1717. As another example, if you don't want the time-of-day of entries in
  1718. the prefix, you could use:
  1719. (setq org-agenda-prefix-format \" %-11:c% s\")
  1720. See also the variables `org-agenda-remove-times-when-in-prefix' and
  1721. `org-agenda-remove-tags-when-in-prefix'."
  1722. :type '(choice
  1723. (string :tag "General format")
  1724. (list :greedy t :tag "View dependent"
  1725. (cons (const agenda) (string :tag "Format"))
  1726. (cons (const timeline) (string :tag "Format"))
  1727. (cons (const todo) (string :tag "Format"))
  1728. (cons (const tags) (string :tag "Format"))))
  1729. :group 'org-agenda-prefix)
  1730. (defvar org-prefix-format-compiled nil
  1731. "The compiled version of the most recently used prefix format.
  1732. See the variable `org-agenda-prefix-format'.")
  1733. (defcustom org-agenda-remove-times-when-in-prefix t
  1734. "Non-nil means, remove duplicate time specifications in agenda items.
  1735. When the format `org-agenda-prefix-format' contains a `%t' specifier, a
  1736. time-of-day specification in a headline or diary entry is extracted and
  1737. placed into the prefix. If this option is non-nil, the original specification
  1738. \(a timestamp or -range, or just a plain time(range) specification like
  1739. 11:30-4pm) will be removed for agenda display. This makes the agenda less
  1740. cluttered.
  1741. The option can be t or nil. It may also be the symbol `beg', indicating
  1742. that the time should only be removed what it is located at the beginning of
  1743. the headline/diary entry."
  1744. :group 'org-agenda-prefix
  1745. :type '(choice
  1746. (const :tag "Always" t)
  1747. (const :tag "Never" nil)
  1748. (const :tag "When at beginning of entry" beg)))
  1749. (defcustom org-agenda-remove-tags-when-in-prefix nil
  1750. "Non-nil means, remove the tags from the headline copy in the agenda.
  1751. When this is the symbol `prefix', only remove tags when
  1752. `org-agenda-prefix-format' contains a `%T' specifier."
  1753. :group 'org-agenda-prefix
  1754. :type '(choice
  1755. (const :tag "Always" t)
  1756. (const :tag "Never" nil)
  1757. (const :tag "When prefix format contains %T" prefix)))
  1758. (defcustom org-agenda-align-tags-to-column 65
  1759. "Shift tags in agenda items to this column."
  1760. :group 'org-agenda-prefix
  1761. :type 'integer)
  1762. (defgroup org-latex nil
  1763. "Options for embedding LaTeX code into Org-mode"
  1764. :tag "Org LaTeX"
  1765. :group 'org)
  1766. (defcustom org-format-latex-options
  1767. '(:foreground "Black" :background "Transparent" :scale 1.0
  1768. :matchers ("begin" "$" "$$" "\\(" "\\["))
  1769. "Options for creating images from LaTeX fragments.
  1770. This is a property list with the following properties:
  1771. :foreground the foreground color, for example \"Black\".
  1772. :background the background color, or \"Transparent\".
  1773. :scale a scaling factor for the size of the images
  1774. :matchers a list indicating which matchers should be used to
  1775. find LaTeX fragments. Valid members of this list are:
  1776. \"begin\" find environments
  1777. \"$\" find math expressions surrounded by $...$
  1778. \"$$\" find math expressions surrounded by $$....$$
  1779. \"\\(\" find math expressions surrounded by \\(...\\)
  1780. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  1781. :group 'org-latex
  1782. :type 'plist)
  1783. (defgroup org-export nil
  1784. "Options for exporting org-listings."
  1785. :tag "Org Export"
  1786. :group 'org)
  1787. (defgroup org-export-general nil
  1788. "General options for exporting Org-mode files."
  1789. :tag "Org Export General"
  1790. :group 'org-export)
  1791. (defcustom org-export-publishing-directory "."
  1792. "Path to the location where exported files should be located.
  1793. This path may be relative to the directory where the Org-mode file lives.
  1794. The default is to put them into the same directory as the Org-mode file.
  1795. The variable may also be an alist with export types `:html', `:ascii',
  1796. `:ical', or `:xoxo' and the corresponding directories. If a direcoty path
  1797. is relative, it is interpreted relative to the directory where the exported
  1798. Org-mode files lives."
  1799. :group 'org-export-general
  1800. :type '(choice
  1801. (directory)
  1802. (repeat
  1803. (cons
  1804. (choice :tag "Type"
  1805. (const :html) (const :ascii) (const :ical) (const :xoxo))
  1806. (directory)))))
  1807. (defcustom org-export-language-setup
  1808. '(("en" "Author" "Date" "Table of Contents")
  1809. ("cs" "Autor" "Datum" "Obsah")
  1810. ("da" "Ophavsmand" "Dato" "Indhold")
  1811. ("de" "Autor" "Datum" "Inhaltsverzeichnis")
  1812. ("es" "Autor" "Fecha" "\xccndice")
  1813. ("fr" "Auteur" "Date" "Table des Mati\xe8res")
  1814. ("it" "Autore" "Data" "Indice")
  1815. ("nl" "Auteur" "Datum" "Inhoudsopgave")
  1816. ("nn" "Forfattar" "Dato" "Innhold") ;; nn = Norsk (nynorsk)
  1817. ("sv" "F\xf6rfattarens" "Datum" "Inneh\xe5ll"))
  1818. "Terms used in export text, translated to different languages.
  1819. Use the variable `org-export-default-language' to set the language,
  1820. or use the +OPTION lines for a per-file setting."
  1821. :group 'org-export-general
  1822. :type '(repeat
  1823. (list
  1824. (string :tag "HTML language tag")
  1825. (string :tag "Author")
  1826. (string :tag "Date")
  1827. (string :tag "Table of Contents"))))
  1828. (defcustom org-export-default-language "en"
  1829. "The default language of HTML export, as a string.
  1830. This should have an association in `org-export-language-setup'."
  1831. :group 'org-export-general
  1832. :type 'string)
  1833. (defcustom org-export-headline-levels 3
  1834. "The last level which is still exported as a headline.
  1835. Inferior levels will produce itemize lists when exported.
  1836. Note that a numeric prefix argument to an exporter function overrides
  1837. this setting.
  1838. This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
  1839. :group 'org-export-general
  1840. :type 'number)
  1841. (defcustom org-export-with-section-numbers t
  1842. "Non-nil means, add section numbers to headlines when exporting.
  1843. This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
  1844. :group 'org-export-general
  1845. :type 'boolean)
  1846. (defcustom org-export-with-toc t
  1847. "Non-nil means, create a table of contents in exported files.
  1848. The TOC contains headlines with levels up to`org-export-headline-levels'.
  1849. Headlines which contain any TODO items will be marked with \"(*)\" in
  1850. ASCII export, and with red color in HTML output.
  1851. In HTML output, the TOC will be clickable.
  1852. This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"."
  1853. :group 'org-export-general
  1854. :type 'boolean)
  1855. (defcustom org-export-mark-todo-in-toc nil
  1856. "Non-nil means, mark TOC lines that contain any open TODO items."
  1857. :group 'org-export-general
  1858. :type 'boolean)
  1859. (defcustom org-export-preserve-breaks nil
  1860. "Non-nil means, preserve all line breaks when exporting.
  1861. Normally, in HTML output paragraphs will be reformatted. In ASCII
  1862. export, line breaks will always be preserved, regardless of this variable.
  1863. This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
  1864. :group 'org-export-general
  1865. :type 'boolean)
  1866. (defcustom org-export-with-archived-trees 'headline
  1867. "Whether subtrees with the ARCHIVE tag should be exported.
  1868. This can have three different values
  1869. nil Do not export, pretend this tree is not present
  1870. t Do export the entire tree
  1871. headline Only export the headline, but skip the tree below it."
  1872. :group 'org-export-general
  1873. :group 'org-archive
  1874. :type '(choice
  1875. (const :tag "not at all" nil)
  1876. (const :tag "headline only" 'headline)
  1877. (const :tag "entirely" t)))
  1878. (defcustom org-export-with-timestamps t
  1879. "If nil, do not export time stamps and associated keywords."
  1880. :group 'org-export-general
  1881. :type 'boolean)
  1882. (defcustom org-export-remove-timestamps-from-toc t
  1883. "If nil, remove timestamps from the table of contents entries."
  1884. :group 'org-export-general
  1885. :type 'boolean)
  1886. (defcustom org-export-with-tags 'not-in-toc
  1887. "If nil, do not export tags, just remove them from headlines.
  1888. If this is the symbol `not-in-toc', tags will be removed from table of
  1889. contents entries, but still be shown in the headlines of the document."
  1890. :group 'org-export-general
  1891. :type '(choice
  1892. (const :tag "Off" nil)
  1893. (const :tag "Not in TOC" not-in-toc)
  1894. (const :tag "On" t)))
  1895. (defgroup org-export-translation nil
  1896. "Options for translating special ascii sequences for the export backends."
  1897. :tag "Org Export Translation"
  1898. :group 'org-export)
  1899. (defcustom org-export-with-emphasize t
  1900. "Non-nil means, interpret *word*, /word/, and _word_ as emphasized text.
  1901. If the export target supports emphasizing text, the word will be
  1902. typeset in bold, italic, or underlined, respectively. Works only for
  1903. single words, but you can say: I *really* *mean* *this*.
  1904. Not all export backends support this.
  1905. This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
  1906. :group 'org-export-translation
  1907. :type 'boolean)
  1908. (defcustom org-export-with-sub-superscripts t
  1909. "Non-nil means, interpret \"_\" and \"^\" for export.
  1910. When this option is turned on, you can use TeX-like syntax for sub- and
  1911. superscripts. Several characters after \"_\" or \"^\" will be
  1912. considered as a single item - so grouping with {} is normally not
  1913. needed. For example, the following things will be parsed as single
  1914. sub- or superscripts.
  1915. 10^24 or 10^tau several digits will be considered 1 item.
  1916. 10^-12 or 10^-tau a leading sign with digits or a word
  1917. x^2-y^3 will be read as x^2 - y^3, because items are
  1918. terminated by almost any nonword/nondigit char.
  1919. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  1920. Still, ambiguity is possible - so when in doubt use {} to enclose the
  1921. sub/superscript.
  1922. Not all export backends support this, but HTML does.
  1923. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  1924. :group 'org-export-translation
  1925. :type 'boolean)
  1926. (defcustom org-export-with-TeX-macros t
  1927. "Non-nil means, interpret simple TeX-like macros when exporting.
  1928. For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
  1929. No only real TeX macros will work here, but the standard HTML entities
  1930. for math can be used as macro names as well. For a list of supported
  1931. names in HTML export, see the constant `org-html-entities'.
  1932. Not all export backends support this.
  1933. This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
  1934. :group 'org-export-translation
  1935. :group 'org-latex
  1936. :type 'boolean)
  1937. (defcustom org-export-with-LaTeX-fragments nil
  1938. "Non-nil means, convert LaTeX fragments to images when exporting to HTML.
  1939. When set, the exporter will find LaTeX environments if the \\begin line is
  1940. the first non-white thing on a line. It will also find the math delimiters
  1941. like $a=b$ and \\( a=b \\) for inline math, $$a=b$$ and \\[ a=b \\] for
  1942. display math.
  1943. This option can also be set with the +OPTIONS line, e.g. \"LaTeX:t\"."
  1944. :group 'org-export-translation
  1945. :group 'org-latex
  1946. :type 'boolean)
  1947. (defcustom org-export-with-fixed-width t
  1948. "Non-nil means, lines starting with \":\" will be in fixed width font.
  1949. This can be used to have pre-formatted text, fragments of code etc. For
  1950. example:
  1951. : ;; Some Lisp examples
  1952. : (while (defc cnt)
  1953. : (ding))
  1954. will be looking just like this in also HTML. See also the QUOTE keyword.
  1955. Not all export backends support this.
  1956. This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
  1957. :group 'org-export-translation
  1958. :type 'boolean)
  1959. (defcustom org-match-sexp-depth 3
  1960. "Number of stacked braces for sub/superscript matching.
  1961. This has to be set before loading org.el to be effective."
  1962. :group 'org-export-translation
  1963. :type 'integer)
  1964. (defgroup org-export-tables nil
  1965. "Options for exporting tables in Org-mode."
  1966. :tag "Org Export Tables"
  1967. :group 'org-export)
  1968. (defcustom org-export-with-tables t
  1969. "If non-nil, lines starting with \"|\" define a table.
  1970. For example:
  1971. | Name | Address | Birthday |
  1972. |-------------+----------+-----------|
  1973. | Arthur Dent | England | 29.2.2100 |
  1974. Not all export backends support this.
  1975. This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
  1976. :group 'org-export-tables
  1977. :type 'boolean)
  1978. (defcustom org-export-highlight-first-table-line t
  1979. "Non-nil means, highlight the first table line.
  1980. In HTML export, this means use <th> instead of <td>.
  1981. In tables created with table.el, this applies to the first table line.
  1982. In Org-mode tables, all lines before the first horizontal separator
  1983. line will be formatted with <th> tags."
  1984. :group 'org-export-tables
  1985. :type 'boolean)
  1986. (defcustom org-export-table-remove-special-lines t
  1987. "Remove special lines and marking characters in calculating tables.
  1988. This removes the special marking character column from tables that are set
  1989. up for spreadsheet calculations. It also removes the entire lines
  1990. marked with `!', `_', or `^'. The lines with `$' are kept, because
  1991. the values of constants may be useful to have."
  1992. :group 'org-export-tables
  1993. :type 'boolean)
  1994. (defcustom org-export-prefer-native-exporter-for-tables nil
  1995. "Non-nil means, always export tables created with table.el natively.
  1996. Natively means, use the HTML code generator in table.el.
  1997. When nil, Org-mode's own HTML generator is used when possible (i.e. if
  1998. the table does not use row- or column-spanning). This has the
  1999. advantage, that the automatic HTML conversions for math symbols and
  2000. sub/superscripts can be applied. Org-mode's HTML generator is also
  2001. much faster."
  2002. :group 'org-export-tables
  2003. :type 'boolean)
  2004. (defgroup org-export-ascii nil
  2005. "Options specific for ASCII export of Org-mode files."
  2006. :tag "Org Export ASCII"
  2007. :group 'org-export)
  2008. (defcustom org-export-ascii-underline '(?\$ ?\# ?^ ?\~ ?\= ?\-)
  2009. "Characters for underlining headings in ASCII export.
  2010. In the given sequence, these characters will be used for level 1, 2, ..."
  2011. :group 'org-export-ascii
  2012. :type '(repeat character))
  2013. (defcustom org-export-ascii-bullets '(?* ?+ ?-)
  2014. "Bullet characters for headlines converted to lists in ASCII export.
  2015. The first character is is used for the first lest level generated in this
  2016. way, and so on. If there are more levels than characters given here,
  2017. the list will be repeated.
  2018. Note that plain lists will keep the same bullets as the have in the
  2019. Org-mode file."
  2020. :group 'org-export-ascii
  2021. :type '(repeat character))
  2022. (defcustom org-export-ascii-show-new-buffer t
  2023. "Non-nil means, popup buffer containing the exported ASCII text.
  2024. Otherwise the buffer will just be saved to a file and stay hidden."
  2025. :group 'org-export-ascii
  2026. :type 'boolean)
  2027. (defgroup org-export-xml nil
  2028. "Options specific for XML export of Org-mode files."
  2029. :tag "Org Export XML"
  2030. :group 'org-export)
  2031. (defgroup org-export-html nil
  2032. "Options specific for HTML export of Org-mode files."
  2033. :tag "Org Export HTML"
  2034. :group 'org-export)
  2035. (defcustom org-export-html-style
  2036. "<style type=\"text/css\">
  2037. html {
  2038. font-family: Times, serif;
  2039. font-size: 12pt;
  2040. }
  2041. .title { text-align: center; }
  2042. .todo { color: red; }
  2043. .done { color: green; }
  2044. .timestamp { color: grey }
  2045. .timestamp-kwd { color: CadetBlue }
  2046. .tag { background-color:lightblue; font-weight:normal }
  2047. .target { background-color: lavender; }
  2048. pre {
  2049. border: 1pt solid #AEBDCC;
  2050. background-color: #F3F5F7;
  2051. padding: 5pt;
  2052. font-family: courier, monospace;
  2053. }
  2054. table { border-collapse: collapse; }
  2055. td, th {
  2056. vertical-align: top;
  2057. border: 1pt solid #ADB9CC;
  2058. }
  2059. </style>"
  2060. "The default style specification for exported HTML files.
  2061. Since there are different ways of setting style information, this variable
  2062. needs to contain the full HTML structure to provide a style, including the
  2063. surrounding HTML tags. The style specifications should include definitions
  2064. for new classes todo, done, title, and deadline. For example, legal values
  2065. would be:
  2066. <style type=\"text/css\">
  2067. p { font-weight: normal; color: gray; }
  2068. h1 { color: black; }
  2069. .title { text-align: center; }
  2070. .todo, .deadline { color: red; }
  2071. .done { color: green; }
  2072. </style>
  2073. or, if you want to keep the style in a file,
  2074. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  2075. As the value of this option simply gets inserted into the HTML <head> header,
  2076. you can \"misuse\" it to add arbitrary text to the header."
  2077. :group 'org-export-html
  2078. :type 'string)
  2079. (defcustom org-export-html-title-format "<h1 class=\"title\">%s</h1>\n"
  2080. "Format for typesetting the document title in HTML export."
  2081. :group 'org-export-html
  2082. :type 'string)
  2083. (defcustom org-export-html-toplevel-hlevel 2
  2084. "The <H> level for level 1 headings in HTML export."
  2085. :group 'org-export-html
  2086. :type 'string)
  2087. (defcustom org-export-html-link-org-files-as-html t
  2088. "Non-nil means, make file links to `file.org' point to `file.html'.
  2089. When org-mode is exporting an org-mode file to HTML, links to
  2090. non-html files are directly put into a href tag in HTML.
  2091. However, links to other Org-mode files (recognized by the
  2092. extension `.org.) should become links to the corresponding html
  2093. file, assuming that the linked org-mode file will also be
  2094. converted to HTML.
  2095. When nil, the links still point to the plain `.org' file."
  2096. :group 'org-export-html
  2097. :type 'boolean)
  2098. (defcustom org-export-html-inline-images 'maybe
  2099. "Non-nil means, inline images into exported HTML pages.
  2100. This is done using an <img> tag. When nil, an anchor with href is used to
  2101. link to the image. If this option is `maybe', then images in links with
  2102. an empty description will be inlined, while images with a description will
  2103. be linked only."
  2104. :group 'org-export-html
  2105. :type '(choice (const :tag "Never" nil)
  2106. (const :tag "Always" t)
  2107. (const :tag "When there is no description" maybe)))
  2108. (defcustom org-export-html-expand t
  2109. "Non-nil means, for HTML export, treat @<...> as HTML tag.
  2110. When nil, these tags will be exported as plain text and therefore
  2111. not be interpreted by a browser.
  2112. This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
  2113. :group 'org-export-html
  2114. :type 'boolean)
  2115. (defcustom org-export-html-table-tag
  2116. "<table border=\"1\" cellspacing=\"0\" cellpadding=\"6\">"
  2117. "The HTML tag used to start a table.
  2118. This must be a <table> tag, but you may change the options like
  2119. borders and spacing."
  2120. :group 'org-export-html
  2121. :type 'string)
  2122. (defcustom org-export-html-with-timestamp nil
  2123. "If non-nil, write `org-export-html-html-helper-timestamp'
  2124. into the exported HTML text. Otherwise, the buffer will just be saved
  2125. to a file."
  2126. :group 'org-export-html
  2127. :type 'boolean)
  2128. (defcustom org-export-html-html-helper-timestamp
  2129. "<br/><br/><hr><p><!-- hhmts start --> <!-- hhmts end --></p>\n"
  2130. "The HTML tag used as timestamp delimiter for HTML-helper-mode."
  2131. :group 'org-export-html
  2132. :type 'string)
  2133. (defcustom org-export-html-show-new-buffer nil
  2134. "Non-nil means, popup buffer containing the exported html text.
  2135. Otherwise, the buffer will just be saved to a file and stay hidden."
  2136. :group 'org-export-html
  2137. :type 'boolean)
  2138. (defgroup org-export-icalendar nil
  2139. "Options specific for iCalendar export of Org-mode files."
  2140. :tag "Org Export iCalendar"
  2141. :group 'org-export)
  2142. (defcustom org-combined-agenda-icalendar-file "~/org.ics"
  2143. "The file name for the iCalendar file covering all agenda files.
  2144. This file is created with the command \\[org-export-icalendar-all-agenda-files].
  2145. The file name should be absolute."
  2146. :group 'org-export-icalendar
  2147. :type 'file)
  2148. (defcustom org-icalendar-include-todo nil
  2149. "Non-nil means, export to iCalendar files should also cover TODO items."
  2150. :group 'org-export-icalendar
  2151. :type 'boolean)
  2152. (defcustom org-icalendar-combined-name "OrgMode"
  2153. "Calendar name for the combined iCalendar representing all agenda files."
  2154. :group 'org-export-icalendar
  2155. :type 'string)
  2156. (defgroup org-font-lock nil
  2157. "Font-lock settings for highlighting in Org-mode."
  2158. :tag "Org Font Lock"
  2159. :group 'org)
  2160. (defcustom org-level-color-stars-only nil
  2161. "Non-nil means fontify only the stars in each headline.
  2162. When nil, the entire headline is fontified.
  2163. Changing it requires restart of `font-lock-mode' to become effective
  2164. also in regions already fontified."
  2165. :group 'org-font-lock
  2166. :type 'boolean)
  2167. (defcustom org-hide-leading-stars nil
  2168. "Non-nil means, hide the first N-1 stars in a headline.
  2169. This works by using the face `org-hide' for these stars. This
  2170. face is white for a light background, and black for a dark
  2171. background. You may have to customize the face `org-hide' to
  2172. make this work.
  2173. Changing it requires restart of `font-lock-mode' to become effective
  2174. also in regions already fontified.
  2175. You may also set this on a per-file basis by adding one of the following
  2176. lines to the buffer:
  2177. #+STARTUP: hidestars
  2178. #+STARTUP: showstars"
  2179. :group 'org-font-lock
  2180. :type 'boolean)
  2181. (defcustom org-fontify-done-headline nil
  2182. "Non-nil means, change the face of a headline if it is marked DONE.
  2183. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2184. When this is non-nil, the headline after the keyword is set to the
  2185. `org-headline-done' as an additional indication."
  2186. :group 'org-font-lock
  2187. :type 'boolean)
  2188. (defcustom org-fontify-emphasized-text t
  2189. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2190. Changing this variable requires a restart of Emacs to take effect."
  2191. :group 'org-font-lock
  2192. :type 'boolean)
  2193. (defvar org-emph-re nil
  2194. "Regular expression for matching emphasis.")
  2195. (defvar org-emphasis-regexp-components) ; defined just below
  2196. (defvar org-emphasis-alist) ; defined just below
  2197. (defun org-set-emph-re (var val)
  2198. "Set variable and compute the emphasis regular expression."
  2199. (set var val)
  2200. (when (and (boundp 'org-emphasis-alist)
  2201. (boundp 'org-emphasis-regexp-components)
  2202. org-emphasis-alist org-emphasis-regexp-components)
  2203. (let* ((e org-emphasis-regexp-components)
  2204. (pre (car e))
  2205. (post (nth 1 e))
  2206. (border (nth 2 e))
  2207. (body (nth 3 e))
  2208. (nl (nth 4 e))
  2209. (stacked (nth 5 e))
  2210. (body1 (concat body "*?"))
  2211. (markers (mapconcat 'car org-emphasis-alist "")))
  2212. ;; make sure special characters appear at the right position in the class
  2213. (if (string-match "\\^" markers)
  2214. (setq markers (concat (replace-match "" t t markers) "^")))
  2215. (if (string-match "-" markers)
  2216. (setq markers (concat (replace-match "" t t markers) "-")))
  2217. ; (while (>= (setq nl (1- nl)) 0) (setq body1 (concat body1 "\n?" body "*?")))
  2218. ; (while (>= (setq nl (1- nl)) 0) (setq body1 (concat body1 "\\(?:\n?" body "*?\\)?")))
  2219. (if (> nl 0)
  2220. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2221. (int-to-string nl) "\\}")))
  2222. ;; Make the regexp
  2223. (setq org-emph-re
  2224. (concat "\\([" pre (if stacked markers) "]\\|^\\)"
  2225. "\\("
  2226. "\\([" markers "]\\)"
  2227. "\\("
  2228. "[^" border markers "]"
  2229. body1
  2230. "[^" border markers "]"
  2231. "\\)"
  2232. "\\3\\)"
  2233. "\\([" post (if stacked markers) "]\\|$\\)")))))
  2234. (defcustom org-emphasis-regexp-components
  2235. '(" \t(" " \t.,?;'\")" " \t\r\n," "." 1 nil)
  2236. "Components used to build the reqular expression for emphasis.
  2237. This is a list with 6 entries. Terminology: In an emphasis string
  2238. like \" *strong word* \", we call the initial space PREMATCH, the final
  2239. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2240. and \"trong wor\" is the body. The different components in this variable
  2241. specify what is allowed/forbidden in each part:
  2242. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2243. post Chars allowed as postmatch. End of line will be allowed too.
  2244. border The chars *forbidden* as border characters. In addition to the
  2245. characters given here, all marker characters are forbidden too.
  2246. body-regexp A regexp like \".\" to match a body character. Don't use
  2247. non-shy groups here, and don't allow newline here.
  2248. newline The maximum number of newlines allowed in an emphasis exp.
  2249. stacked Non-nil means, allow stacked styles. This works only in HTML
  2250. export. When this is set, all marker characters (as given in
  2251. `org-emphasis-alist') will be allowed as pre/post, aiding
  2252. inside-out matching.
  2253. Use customize to modify this, or restart Emacs after changing it."
  2254. :group 'org-font-lock
  2255. :set 'org-set-emph-re
  2256. :type '(list
  2257. (sexp :tag "Allowed chars in pre ")
  2258. (sexp :tag "Allowed chars in post ")
  2259. (sexp :tag "Forbidden chars in border ")
  2260. (sexp :tag "Regexp for body ")
  2261. (integer :tag "number of newlines allowed")
  2262. (boolean :tag "Stacking allowed ")))
  2263. (defcustom org-emphasis-alist
  2264. '(("*" bold "<b>" "</b>")
  2265. ("/" italic "<i>" "</i>")
  2266. ("_" underline "<u>" "</u>")
  2267. ("=" shadow "<code>" "</code>")
  2268. ("+" (:strike-through t) "<del>" "</del>")
  2269. )
  2270. "Special syntax for emphasized text.
  2271. Text starting and ending with a special character will be emphasized, for
  2272. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2273. characters, the face to bbe used by font-lock for highlighting in Org-mode
  2274. Emacs buffers, and the HTML tags to be used for this.
  2275. Use customize to modify this, or restart Emacs after changing it."
  2276. :group 'org-font-lock
  2277. :set 'org-set-emph-re
  2278. :type '(repeat
  2279. (list
  2280. (string :tag "Marker character")
  2281. (choice
  2282. (face :tag "Font-lock-face")
  2283. (plist :tag "Face property list"))
  2284. (string :tag "HTML start tag")
  2285. (string :tag "HTML end tag"))))
  2286. (defgroup org-faces nil
  2287. "Faces in Org-mode."
  2288. :tag "Org Faces"
  2289. :group 'org-font-lock)
  2290. (defun org-compatible-face (specs)
  2291. "Make a compatible face specification.
  2292. XEmacs and Emacs 21 do not know about the `min-colors' attribute.
  2293. For them we convert a (min-colors 8) entry to a `tty' entry and move it
  2294. to the top of the list. The `min-colors' attribute will be removed from
  2295. any other entries, and any resulting duplicates will be removed entirely."
  2296. (if (or (featurep 'xemacs) (< emacs-major-version 22))
  2297. (let (r e a)
  2298. (while (setq e (pop specs))
  2299. (cond
  2300. ((memq (car e) '(t default)) (push e r))
  2301. ((setq a (member '(min-colors 8) (car e)))
  2302. (nconc r (list (cons (cons '(type tty) (delq (car a) (car e)))
  2303. (cdr e)))))
  2304. ((setq a (assq 'min-colors (car e)))
  2305. (setq e (cons (delq a (car e)) (cdr e)))
  2306. (or (assoc (car e) r) (push e r)))
  2307. (t (or (assoc (car e) r) (push e r)))))
  2308. (nreverse r))
  2309. specs))
  2310. (defface org-hide
  2311. '((((background light)) (:foreground "white"))
  2312. (((background dark)) (:foreground "black")))
  2313. "Face used to hide leading stars in headlines.
  2314. The forground color of this face should be equal to the background
  2315. color of the frame."
  2316. :group 'org-faces)
  2317. (defface org-level-1 ;; font-lock-function-name-face
  2318. (org-compatible-face
  2319. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2320. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2321. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2322. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2323. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  2324. (t (:bold t))))
  2325. "Face used for level 1 headlines."
  2326. :group 'org-faces)
  2327. (defface org-level-2 ;; font-lock-variable-name-face
  2328. (org-compatible-face
  2329. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  2330. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  2331. (((class color) (min-colors 8) (background light)) (:foreground "yellow"))
  2332. (((class color) (min-colors 8) (background dark)) (:foreground "yellow" :bold t))
  2333. (t (:bold t))))
  2334. "Face used for level 2 headlines."
  2335. :group 'org-faces)
  2336. (defface org-level-3 ;; font-lock-keyword-face
  2337. (org-compatible-face
  2338. '((((class color) (min-colors 88) (background light)) (:foreground "Purple"))
  2339. (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
  2340. (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
  2341. (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
  2342. (((class color) (min-colors 8) (background light)) (:foreground "purple" :bold t))
  2343. (((class color) (min-colors 8) (background dark)) (:foreground "cyan" :bold t))
  2344. (t (:bold t))))
  2345. "Face used for level 3 headlines."
  2346. :group 'org-faces)
  2347. (defface org-level-4 ;; font-lock-comment-face
  2348. (org-compatible-face
  2349. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2350. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2351. (((class color) (min-colors 16) (background light)) (:foreground "red"))
  2352. (((class color) (min-colors 16) (background dark)) (:foreground "red1"))
  2353. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2354. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2355. (t (:bold t))))
  2356. "Face used for level 4 headlines."
  2357. :group 'org-faces)
  2358. (defface org-level-5 ;; font-lock-type-face
  2359. (org-compatible-face
  2360. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
  2361. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
  2362. (((class color) (min-colors 8)) (:foreground "green"))))
  2363. "Face used for level 5 headlines."
  2364. :group 'org-faces)
  2365. (defface org-level-6 ;; font-lock-constant-face
  2366. (org-compatible-face
  2367. '((((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
  2368. (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
  2369. (((class color) (min-colors 8)) (:foreground "magenta"))))
  2370. "Face used for level 6 headlines."
  2371. :group 'org-faces)
  2372. (defface org-level-7 ;; font-lock-builtin-face
  2373. (org-compatible-face
  2374. '((((class color) (min-colors 16) (background light)) (:foreground "Orchid"))
  2375. (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue"))
  2376. (((class color) (min-colors 8)) (:foreground "blue"))))
  2377. "Face used for level 7 headlines."
  2378. :group 'org-faces)
  2379. (defface org-level-8 ;; font-lock-string-face
  2380. (org-compatible-face
  2381. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2382. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2383. (((class color) (min-colors 8)) (:foreground "green"))))
  2384. "Face used for level 8 headlines."
  2385. :group 'org-faces)
  2386. (defface org-special-keyword ;; font-lock-string-face
  2387. (org-compatible-face
  2388. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2389. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2390. (t (:italic t))))
  2391. "Face used for special keywords."
  2392. :group 'org-faces)
  2393. (defface org-warning ;; font-lock-warning-face
  2394. (org-compatible-face
  2395. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  2396. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  2397. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2398. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2399. (t (:bold t))))
  2400. "Face for deadlines and TODO keywords."
  2401. :group 'org-faces)
  2402. (defface org-headline-done ;; font-lock-string-face
  2403. (org-compatible-face
  2404. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2405. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2406. (((class color) (min-colors 8) (background light)) (:bold nil))))
  2407. "Face used to indicate that a headline is DONE.
  2408. This face is only used if `org-fontify-done-headline' is set."
  2409. :group 'org-faces)
  2410. (defface org-archived ; similar to shadow
  2411. (org-compatible-face
  2412. '((((class color grayscale) (min-colors 88) (background light))
  2413. (:foreground "grey50"))
  2414. (((class color grayscale) (min-colors 88) (background dark))
  2415. (:foreground "grey70"))
  2416. (((class color) (min-colors 8) (background light))
  2417. (:foreground "green"))
  2418. (((class color) (min-colors 8) (background dark))
  2419. (:foreground "yellow"))))
  2420. "Face for headline with the ARCHIVE tag."
  2421. :group 'org-faces)
  2422. (defface org-link
  2423. '((((class color) (background light)) (:foreground "Purple" :underline t))
  2424. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  2425. (t (:underline t)))
  2426. "Face for links."
  2427. :group 'org-faces)
  2428. (defface org-date
  2429. '((((class color) (background light)) (:foreground "Purple" :underline t))
  2430. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  2431. (t (:underline t)))
  2432. "Face for links."
  2433. :group 'org-faces)
  2434. (defface org-tag
  2435. '((t (:bold t)))
  2436. "Face for tags."
  2437. :group 'org-faces)
  2438. (defface org-todo ;; font-lock-warning-face
  2439. (org-compatible-face
  2440. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  2441. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  2442. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2443. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2444. (t (:inverse-video t :bold t))))
  2445. "Face for TODO keywords."
  2446. :group 'org-faces)
  2447. (defface org-done ;; font-lock-type-face
  2448. (org-compatible-face
  2449. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen" :bold t))
  2450. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen" :bold t))
  2451. (((class color) (min-colors 8)) (:foreground "green"))
  2452. (t (:bold t))))
  2453. "Face used for DONE."
  2454. :group 'org-faces)
  2455. (defface org-table ;; font-lock-function-name-face
  2456. (org-compatible-face
  2457. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2458. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2459. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2460. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2461. (((class color) (min-colors 8) (background light)) (:foreground "blue"))
  2462. (((class color) (min-colors 8) (background dark)))))
  2463. "Face used for tables."
  2464. :group 'org-faces)
  2465. (defface org-formula
  2466. (org-compatible-face
  2467. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2468. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2469. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2470. (((class color) (min-colors 8) (background dark)) (:foreground "red"))
  2471. (t (:bold t :italic t))))
  2472. "Face for formulas."
  2473. :group 'org-faces)
  2474. (defface org-scheduled-today
  2475. (org-compatible-face
  2476. '((((class color) (min-colors 88) (background light)) (:foreground "DarkGreen"))
  2477. (((class color) (min-colors 88) (background dark)) (:foreground "PaleGreen"))
  2478. (((class color) (min-colors 8)) (:foreground "green"))
  2479. (t (:bold t :italic t))))
  2480. "Face for items scheduled for a certain day."
  2481. :group 'org-faces)
  2482. (defface org-scheduled-previously
  2483. (org-compatible-face
  2484. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2485. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2486. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2487. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2488. (t (:bold t))))
  2489. "Face for items scheduled previously, and not yet done."
  2490. :group 'org-faces)
  2491. (defface org-upcoming-deadline
  2492. (org-compatible-face
  2493. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2494. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2495. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2496. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2497. (t (:bold t))))
  2498. "Face for items scheduled previously, and not yet done."
  2499. :group 'org-faces)
  2500. (defface org-time-grid ;; font-lock-variable-name-face
  2501. (org-compatible-face
  2502. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  2503. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  2504. (((class color) (min-colors 8)) (:foreground "yellow" :weight light))))
  2505. "Face used for time grids."
  2506. :group 'org-faces)
  2507. (defconst org-level-faces
  2508. '(org-level-1 org-level-2 org-level-3 org-level-4
  2509. org-level-5 org-level-6 org-level-7 org-level-8
  2510. ))
  2511. (defconst org-n-levels (length org-level-faces))
  2512. ;; Variables for pre-computed regular expressions, all buffer local
  2513. (defvar org-done-string nil
  2514. "The last string in `org-todo-keywords', indicating an item is DONE.")
  2515. (make-variable-buffer-local 'org-done-string)
  2516. (defvar org-todo-regexp nil
  2517. "Matches any of the TODO state keywords.")
  2518. (make-variable-buffer-local 'org-todo-regexp)
  2519. (defvar org-not-done-regexp nil
  2520. "Matches any of the TODO state keywords except the last one.")
  2521. (make-variable-buffer-local 'org-not-done-regexp)
  2522. (defvar org-todo-line-regexp nil
  2523. "Matches a headline and puts TODO state into group 2 if present.")
  2524. (make-variable-buffer-local 'org-todo-line-regexp)
  2525. (defvar org-todo-line-tags-regexp nil
  2526. "Matches a headline and puts TODO state into group 2 if present.
  2527. Also put tags into group 4 if tags are present.")
  2528. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2529. (defvar org-nl-done-regexp nil
  2530. "Matches newline followed by a headline with the DONE keyword.")
  2531. (make-variable-buffer-local 'org-nl-done-regexp)
  2532. (defvar org-looking-at-done-regexp nil
  2533. "Matches the DONE keyword a point.")
  2534. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2535. (defvar org-todo-kwd-priority-p nil
  2536. "Do TODO items have priorities?")
  2537. (make-variable-buffer-local 'org-todo-kwd-priority-p)
  2538. (defvar org-todo-kwd-max-priority nil
  2539. "Maximum priority of TODO items.")
  2540. (make-variable-buffer-local 'org-todo-kwd-max-priority)
  2541. (defvar org-ds-keyword-length 12
  2542. "Maximum length of the Deadline and SCHEDULED keywords.")
  2543. (make-variable-buffer-local 'org-ds-keyword-length)
  2544. (defvar org-deadline-regexp nil
  2545. "Matches the DEADLINE keyword.")
  2546. (make-variable-buffer-local 'org-deadline-regexp)
  2547. (defvar org-deadline-time-regexp nil
  2548. "Matches the DEADLINE keyword together with a time stamp.")
  2549. (make-variable-buffer-local 'org-deadline-time-regexp)
  2550. (defvar org-deadline-line-regexp nil
  2551. "Matches the DEADLINE keyword and the rest of the line.")
  2552. (make-variable-buffer-local 'org-deadline-line-regexp)
  2553. (defvar org-scheduled-regexp nil
  2554. "Matches the SCHEDULED keyword.")
  2555. (make-variable-buffer-local 'org-scheduled-regexp)
  2556. (defvar org-scheduled-time-regexp nil
  2557. "Matches the SCHEDULED keyword together with a time stamp.")
  2558. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2559. (defvar org-closed-time-regexp nil
  2560. "Matches the CLOSED keyword together with a time stamp.")
  2561. (make-variable-buffer-local 'org-closed-time-regexp)
  2562. (defvar org-keyword-time-regexp nil
  2563. "Matches any of the 3 keywords, together with the time stamp.")
  2564. (make-variable-buffer-local 'org-keyword-time-regexp)
  2565. (defvar org-maybe-keyword-time-regexp nil
  2566. "Matches a timestamp, possibly preceeded by a keyword.")
  2567. (make-variable-buffer-local 'org-keyword-time-regexp)
  2568. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  2569. rear-nonsticky t mouse-map t)
  2570. "Properties to remove when a string without properties is wanted.")
  2571. (defsubst org-match-string-no-properties (num &optional string)
  2572. (if (featurep 'xemacs)
  2573. (let ((s (match-string num string)))
  2574. (remove-text-properties 0 (length s) org-rm-props s)
  2575. s)
  2576. (match-string-no-properties num string)))
  2577. (defsubst org-no-properties (s)
  2578. (remove-text-properties 0 (length s) org-rm-props s)
  2579. s)
  2580. (defsubst org-get-alist-option (option key)
  2581. (cond ((eq key t) t)
  2582. ((eq option t) t)
  2583. ((assoc key option) (cdr (assoc key option)))
  2584. (t (cdr (assq 'default option)))))
  2585. (defsubst org-set-local (var value)
  2586. "Make VAR local in current buffer and set it to VALUE."
  2587. (set (make-variable-buffer-local var) value))
  2588. (defsubst org-mode-p ()
  2589. "Check if the current buffer is in Org-mode."
  2590. (eq major-mode 'org-mode))
  2591. (defsubst org-last (list)
  2592. "Return the last element of LIST."
  2593. (car (last list)))
  2594. (defun org-let (list &rest body)
  2595. (eval (cons 'let (cons list body))))
  2596. (put 'org-let 'lisp-indent-function 1)
  2597. (defun org-let2 (list1 list2 &rest body)
  2598. (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
  2599. (put 'org-let2 'lisp-indent-function 2)
  2600. (defconst org-startup-options
  2601. '(("fold" org-startup-folded t)
  2602. ("overview" org-startup-folded t)
  2603. ("nofold" org-startup-folded nil)
  2604. ("showall" org-startup-folded nil)
  2605. ("content" org-startup-folded content)
  2606. ("hidestars" org-hide-leading-stars t)
  2607. ("showstars" org-hide-leading-stars nil)
  2608. ("odd" org-odd-levels-only t)
  2609. ("oddeven" org-odd-levels-only nil)
  2610. ("align" org-startup-align-all-tables t)
  2611. ("noalign" org-startup-align-all-tables nil)
  2612. ("customtime" org-display-custom-times t)
  2613. ("logging" org-log-done t)
  2614. ("nologging" org-log-done nil)
  2615. ("dlcheck" org-startup-with-deadline-check t)
  2616. ("nodlcheck" org-startup-with-deadline-check nil)))
  2617. (defun org-set-regexps-and-options ()
  2618. "Precompute regular expressions for current buffer."
  2619. (when (org-mode-p)
  2620. (let ((re (org-make-options-regexp
  2621. '("CATEGORY" "SEQ_TODO" "PRI_TODO" "TYP_TODO"
  2622. "STARTUP" "ARCHIVE" "TAGS" "LINK")))
  2623. (splitre "[ \t]+")
  2624. kwds int key value cat arch tags links)
  2625. (save-excursion
  2626. (save-restriction
  2627. (widen)
  2628. (goto-char (point-min))
  2629. (while (re-search-forward re nil t)
  2630. (setq key (match-string 1) value (org-match-string-no-properties 2))
  2631. (cond
  2632. ((equal key "CATEGORY")
  2633. (if (string-match "[ \t]+$" value)
  2634. (setq value (replace-match "" t t value)))
  2635. (setq cat (intern value)))
  2636. ((equal key "SEQ_TODO")
  2637. (setq int 'sequence
  2638. kwds (append kwds (org-split-string value splitre))))
  2639. ((equal key "PRI_TODO")
  2640. (setq int 'priority
  2641. kwds (append kwds (org-split-string value splitre))))
  2642. ((equal key "TYP_TODO")
  2643. (setq int 'type
  2644. kwds (append kwds (org-split-string value splitre))))
  2645. ((equal key "TAGS")
  2646. (setq tags (append tags (org-split-string value splitre))))
  2647. ((equal key "LINK")
  2648. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2649. (push (cons (match-string 1 value)
  2650. (org-trim (match-string 2 value)))
  2651. links)))
  2652. ((equal key "STARTUP")
  2653. (let ((opts (org-split-string value splitre))
  2654. l var val)
  2655. (while (setq l (assoc (pop opts) org-startup-options))
  2656. (setq var (nth 1 l) val (nth 2 l))
  2657. (set (make-local-variable var) val))))
  2658. ((equal key "ARCHIVE")
  2659. (string-match " *$" value)
  2660. (setq arch (replace-match "" t t value))
  2661. (remove-text-properties 0 (length arch)
  2662. '(face t fontified t) arch)))
  2663. )))
  2664. (and cat (org-set-local 'org-category cat))
  2665. (and kwds (org-set-local 'org-todo-keywords kwds))
  2666. (and arch (org-set-local 'org-archive-location arch))
  2667. (and int (org-set-local 'org-todo-interpretation int))
  2668. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2669. (when tags
  2670. (let (e tgs)
  2671. (while (setq e (pop tags))
  2672. (cond
  2673. ((equal e "{") (push '(:startgroup) tgs))
  2674. ((equal e "}") (push '(:endgroup) tgs))
  2675. ((string-match "^\\([0-9a-zA-Z_@]+\\)(\\(.\\))$" e)
  2676. (push (cons (match-string 1 e)
  2677. (string-to-char (match-string 2 e)))
  2678. tgs))
  2679. (t (push (list e) tgs))))
  2680. (org-set-local 'org-tag-alist nil)
  2681. (while (setq e (pop tgs))
  2682. (or (and (stringp (car e))
  2683. (assoc (car e) org-tag-alist))
  2684. (push e org-tag-alist))))))
  2685. ;; Compute the regular expressions and other local variables
  2686. (setq org-todo-kwd-priority-p (equal org-todo-interpretation 'priority)
  2687. org-todo-kwd-max-priority (1- (length org-todo-keywords))
  2688. org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2689. (length org-scheduled-string)))
  2690. org-done-string
  2691. (nth (1- (length org-todo-keywords)) org-todo-keywords)
  2692. org-todo-regexp
  2693. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords
  2694. "\\|") "\\)\\>")
  2695. org-not-done-regexp
  2696. (concat "\\<\\("
  2697. (mapconcat 'regexp-quote
  2698. (nreverse (cdr (reverse org-todo-keywords)))
  2699. "\\|")
  2700. "\\)\\>")
  2701. org-todo-line-regexp
  2702. (concat "^\\(\\*+\\)[ \t]*\\(?:\\("
  2703. (mapconcat 'regexp-quote org-todo-keywords "\\|")
  2704. "\\)\\>\\)? *\\(.*\\)")
  2705. org-nl-done-regexp
  2706. (concat "[\r\n]\\*+[ \t]+" org-done-string "\\>")
  2707. org-todo-line-tags-regexp
  2708. (concat "^\\(\\*+\\)[ \t]*\\(?:\\("
  2709. (mapconcat 'regexp-quote org-todo-keywords "\\|")
  2710. "\\)\\>\\)? *\\(.*?\\([ \t]:[a-zA-Z0-9:_@]+:[ \t]*\\)?$\\)")
  2711. org-looking-at-done-regexp (concat "^" org-done-string "\\>")
  2712. org-deadline-regexp (concat "\\<" org-deadline-string)
  2713. org-deadline-time-regexp
  2714. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2715. org-deadline-line-regexp
  2716. (concat "\\<\\(" org-deadline-string "\\).*")
  2717. org-scheduled-regexp
  2718. (concat "\\<" org-scheduled-string)
  2719. org-scheduled-time-regexp
  2720. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2721. org-closed-time-regexp
  2722. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2723. org-keyword-time-regexp
  2724. (concat "\\<\\(" org-scheduled-string
  2725. "\\|" org-deadline-string
  2726. "\\|" org-closed-string
  2727. "\\|" org-clock-string "\\)"
  2728. " *[[<]\\([^]>]+\\)[]>]")
  2729. org-maybe-keyword-time-regexp
  2730. (concat "\\(\\<\\(" org-scheduled-string
  2731. "\\|" org-deadline-string
  2732. "\\|" org-closed-string
  2733. "\\|" org-clock-string "\\)\\)?"
  2734. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}[^]\r\n>]*?[]>]\\)"))
  2735. (org-set-font-lock-defaults)))
  2736. ;; Tell the compiler about dynamically scoped variables,
  2737. ;; and variables from other packages
  2738. (defvar calc-embedded-close-formula) ; defined by the calc package
  2739. (defvar calc-embedded-open-formula) ; defined by the calc package
  2740. (defvar font-lock-unfontify-region-function) ; defined by font-lock.el
  2741. (defvar zmacs-regions) ; XEmacs regions
  2742. (defvar original-date) ; dynamically scoped in calendar
  2743. (defvar org-old-auto-fill-inhibit-regexp) ; local variable used by `orgtbl-mode'
  2744. (defvar orgtbl-mode-menu) ; defined when orgtbl mode get initialized
  2745. (defvar org-html-entities) ; defined later in this file
  2746. (defvar org-goto-start-pos) ; dynamically scoped parameter
  2747. (defvar org-time-was-given) ; dynamically scoped parameter
  2748. (defvar org-ts-what) ; dynamically scoped parameter
  2749. (defvar org-current-export-file) ; dynamically scoped parameter
  2750. (defvar org-current-export-dir) ; dynamically scoped parameter
  2751. (defvar mark-active) ; Emacs only, not available in XEmacs.
  2752. (defvar timecnt) ; dynamically scoped parameter
  2753. (defvar levels-open) ; dynamically scoped parameter
  2754. (defvar entry) ; dynamically scoped parameter
  2755. (defvar state) ; dynamically scoped into `org-after-todo-state-change-hook'
  2756. (defvar date) ; dynamically scoped parameter
  2757. (defvar description) ; dynamically scoped parameter
  2758. (defvar ans1) ; dynamically scoped parameter
  2759. (defvar ans2) ; dynamically scoped parameter
  2760. (defvar starting-day) ; local variable
  2761. (defvar include-all-loc) ; local variable
  2762. (defvar vm-message-pointer) ; from vm
  2763. (defvar vm-folder-directory) ; from vm
  2764. (defvar gnus-other-frame-object) ; from gnus
  2765. (defvar wl-summary-buffer-elmo-folder) ; from wanderlust
  2766. (defvar wl-summary-buffer-folder-name) ; from wanderlust
  2767. (defvar gnus-group-name) ; from gnus
  2768. (defvar gnus-article-current) ; from gnus
  2769. (defvar w3m-current-url) ; from w3m
  2770. (defvar w3m-current-title) ; from w3m
  2771. (defvar mh-progs) ; from MH-E
  2772. (defvar mh-current-folder) ; from MH-E
  2773. (defvar mh-show-folder-buffer) ; from MH-E
  2774. (defvar mh-index-folder) ; from MH-E
  2775. (defvar mh-searcher) ; from MH-E
  2776. (defvar org-selected-point) ; dynamically scoped parameter
  2777. (defvar calendar-mode-map) ; from calendar.el
  2778. (defvar last-arg) ; local variable
  2779. (defvar remember-save-after-remembering) ; from remember.el
  2780. (defvar remember-data-file) ; from remember.el
  2781. (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
  2782. (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
  2783. (defvar orgtbl-mode) ; defined later in this file
  2784. (defvar Info-current-file) ; from info.el
  2785. (defvar Info-current-node) ; from info.el
  2786. (defvar texmathp-why) ; from texmathp.el
  2787. (defvar org-latex-regexps)
  2788. (defvar outline-mode-menu-heading)
  2789. (defvar outline-mode-menu-show)
  2790. (defvar outline-mode-menu-hide)
  2791. ;;; Define the mode
  2792. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  2793. (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."))
  2794. (defvar org-struct-menu) ; defined later in this file
  2795. (defvar org-org-menu) ; defined later in this file
  2796. (defvar org-tbl-menu) ; defined later in this file
  2797. ;; We use a before-change function to check if a table might need
  2798. ;; an update.
  2799. (defvar org-table-may-need-update t
  2800. "Indicates that a table might need an update.
  2801. This variable is set by `org-before-change-function'.
  2802. `org-table-align' sets it back to nil.")
  2803. (defvar org-mode-map)
  2804. (defvar org-mode-hook nil)
  2805. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  2806. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  2807. ;;;###autoload
  2808. (define-derived-mode org-mode outline-mode "Org"
  2809. "Outline-based notes management and organizer, alias
  2810. \"Carsten's outline-mode for keeping track of everything.\"
  2811. Org-mode develops organizational tasks around a NOTES file which
  2812. contains information about projects as plain text. Org-mode is
  2813. implemented on top of outline-mode, which is ideal to keep the content
  2814. of large files well structured. It supports ToDo items, deadlines and
  2815. time stamps, which magically appear in the diary listing of the Emacs
  2816. calendar. Tables are easily created with a built-in table editor.
  2817. Plain text URL-like links connect to websites, emails (VM), Usenet
  2818. messages (Gnus), BBDB entries, and any files related to the project.
  2819. For printing and sharing of notes, an Org-mode file (or a part of it)
  2820. can be exported as a structured ASCII or HTML file.
  2821. The following commands are available:
  2822. \\{org-mode-map}"
  2823. ;; Get rid of Outline menus, they are not needed
  2824. ;; Need to do this here because define-derived-mode sets up
  2825. ;; the keymap so late. Still, it is a waste to call this each time
  2826. ;; we switch another buffer into org-mode.
  2827. (if (featurep 'xemacs)
  2828. (when (boundp 'outline-mode-menu-heading)
  2829. ;; Assume this is Greg's port, it used easymenu
  2830. (easy-menu-remove outline-mode-menu-heading)
  2831. (easy-menu-remove outline-mode-menu-show)
  2832. (easy-menu-remove outline-mode-menu-hide))
  2833. (define-key org-mode-map [menu-bar headings] 'undefined)
  2834. (define-key org-mode-map [menu-bar hide] 'undefined)
  2835. (define-key org-mode-map [menu-bar show] 'undefined))
  2836. (easy-menu-add org-org-menu)
  2837. (easy-menu-add org-tbl-menu)
  2838. (org-install-agenda-files-menu)
  2839. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  2840. (org-add-to-invisibility-spec '(org-cwidth))
  2841. (when (featurep 'xemacs)
  2842. (org-set-local 'line-move-ignore-invisible t))
  2843. (setq outline-regexp "\\*+")
  2844. ;;(setq outline-regexp "\\(?:\\*+\\|[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\) \\)")
  2845. (setq outline-level 'org-outline-level)
  2846. (when (and org-ellipsis (stringp org-ellipsis))
  2847. (unless org-display-table
  2848. (setq org-display-table (make-display-table)))
  2849. (set-display-table-slot org-display-table
  2850. 4 (string-to-vector org-ellipsis))
  2851. (setq buffer-display-table org-display-table))
  2852. (org-set-regexps-and-options)
  2853. ;; Calc embedded
  2854. (org-set-local 'calc-embedded-open-mode "# ")
  2855. (modify-syntax-entry ?# "<")
  2856. (if org-startup-truncated (setq truncate-lines t))
  2857. (org-set-local 'font-lock-unfontify-region-function
  2858. 'org-unfontify-region)
  2859. ;; Activate before-change-function
  2860. (org-set-local 'org-table-may-need-update t)
  2861. (org-add-hook 'before-change-functions 'org-before-change-function nil
  2862. 'local)
  2863. ;; Check for running clock before killing a buffer
  2864. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  2865. ;; Paragraphs and auto-filling
  2866. (org-set-autofill-regexps)
  2867. (org-update-radio-target-regexp)
  2868. (if (and org-insert-mode-line-in-empty-file
  2869. (interactive-p)
  2870. (= (point-min) (point-max)))
  2871. (insert " -*- mode: org -*-\n\n"))
  2872. (unless org-inhibit-startup
  2873. (when org-startup-align-all-tables
  2874. (let ((bmp (buffer-modified-p)))
  2875. (org-table-map-tables 'org-table-align)
  2876. (set-buffer-modified-p bmp)))
  2877. (if org-startup-with-deadline-check
  2878. (call-interactively 'org-check-deadlines)
  2879. (cond
  2880. ((eq org-startup-folded t)
  2881. (org-cycle '(4)))
  2882. ((eq org-startup-folded 'content)
  2883. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  2884. (org-cycle '(4)) (org-cycle '(4))))))))
  2885. (defsubst org-call-with-arg (command arg)
  2886. "Call COMMAND interactively, but pretend prefix are was ARG."
  2887. (let ((current-prefix-arg arg)) (call-interactively command)))
  2888. (defsubst org-current-line (&optional pos)
  2889. (save-excursion
  2890. (and pos (goto-char pos))
  2891. (+ (if (bolp) 1 0) (count-lines (point-min) (point)))))
  2892. (defun org-current-time ()
  2893. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  2894. (if (> org-time-stamp-rounding-minutes 0)
  2895. (let ((r org-time-stamp-rounding-minutes)
  2896. (time (decode-time)))
  2897. (apply 'encode-time
  2898. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  2899. (nthcdr 2 time))))
  2900. (current-time)))
  2901. (defun org-add-props (string plist &rest props)
  2902. "Add text properties to entire string, from beginning to end.
  2903. PLIST may be a list of properties, PROPS are individual properties and values
  2904. that will be added to PLIST. Returns the string that was modified."
  2905. (add-text-properties
  2906. 0 (length string) (if props (append plist props) plist) string)
  2907. string)
  2908. (put 'org-add-props 'lisp-indent-function 2)
  2909. ;;; Font-Lock stuff
  2910. (defvar org-mouse-map (make-sparse-keymap))
  2911. (define-key org-mouse-map
  2912. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  2913. (define-key org-mouse-map
  2914. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  2915. (when org-mouse-1-follows-link
  2916. (define-key org-mouse-map [follow-link] 'mouse-face))
  2917. (when org-tab-follows-link
  2918. (define-key org-mouse-map [(tab)] 'org-open-at-point)
  2919. (define-key org-mouse-map "\C-i" 'org-open-at-point))
  2920. (when org-return-follows-link
  2921. (define-key org-mouse-map [(return)] 'org-open-at-point)
  2922. (define-key org-mouse-map "\C-m" 'org-open-at-point))
  2923. (require 'font-lock)
  2924. (defconst org-non-link-chars "]\t\n\r<>")
  2925. (defconst org-link-types '("https?" "ftp" "mailto" "file" "news" "bbdb" "vm"
  2926. "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  2927. (defconst org-link-re-with-space
  2928. (concat
  2929. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2930. "\\([^" org-non-link-chars " ]"
  2931. "[^" org-non-link-chars "]*"
  2932. "[^" org-non-link-chars " ]\\)>?")
  2933. "Matches a link with spaces, optional angular brackets around it.")
  2934. (defconst org-link-re-with-space2
  2935. (concat
  2936. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2937. "\\([^" org-non-link-chars " ]"
  2938. "[^]\t\n\r]*"
  2939. "[^" org-non-link-chars " ]\\)>?")
  2940. "Matches a link with spaces, optional angular brackets around it.")
  2941. (defconst org-angle-link-re
  2942. (concat
  2943. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2944. "\\([^" org-non-link-chars " ]"
  2945. "[^" org-non-link-chars "]*"
  2946. "\\)>")
  2947. "Matches link with angular brackets, spaces are allowed.")
  2948. (defconst org-plain-link-re
  2949. (concat
  2950. "\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2951. "\\([^]\t\n\r<>,;() ]+\\)")
  2952. "Matches plain link, without spaces.")
  2953. (defconst org-bracket-link-regexp
  2954. "\\[\\[\\([^]]+\\)\\]\\(\\[\\([^]]+\\)\\]\\)?\\]"
  2955. "Matches a link in double brackets.")
  2956. (defconst org-bracket-link-analytic-regexp
  2957. (concat
  2958. "\\[\\["
  2959. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  2960. "\\([^]]+\\)"
  2961. "\\]"
  2962. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  2963. "\\]"))
  2964. ; 1: http:
  2965. ; 2: http
  2966. ; 3: path
  2967. ; 4: [desc]
  2968. ; 5: desc
  2969. (defconst org-ts-lengths
  2970. (cons (length (format-time-string (car org-time-stamp-formats)))
  2971. (length (format-time-string (cdr org-time-stamp-formats))))
  2972. "This holds the lengths of the two different time formats.")
  2973. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}[^\r\n>]*?\\)>"
  2974. "Regular expression for fast time stamp matching.")
  2975. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}[^\r\n>]*?\\)[]>]"
  2976. "Regular expression for fast time stamp matching.")
  2977. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  2978. "Regular expression matching time strings for analysis.")
  2979. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 ">")
  2980. "Regular expression matching time stamps, with groups.")
  2981. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[]>]")
  2982. "Regular expression matching time stamps (also [..]), with groups.")
  2983. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  2984. "Regular expression matching a time stamp range.")
  2985. (defconst org-tr-regexp-both
  2986. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  2987. "Regular expression matching a time stamp range.")
  2988. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  2989. org-ts-regexp "\\)?")
  2990. "Regular expression matching a time stamp or time stamp range.")
  2991. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  2992. org-ts-regexp-both "\\)?")
  2993. "Regular expression matching a time stamp or time stamp range.
  2994. The time stamps may be either active or inactive.")
  2995. (defvar org-emph-face nil)
  2996. (defun org-do-emphasis-faces (limit)
  2997. "Run through the buffer and add overlays to links."
  2998. (if (re-search-forward org-emph-re limit t)
  2999. (progn
  3000. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3001. 'face
  3002. (nth 1 (assoc (match-string 3)
  3003. org-emphasis-alist)))
  3004. (add-text-properties (match-beginning 2) (match-end 2)
  3005. '(font-lock-multiline t))
  3006. (backward-char 1)
  3007. t)))
  3008. (defun org-activate-plain-links (limit)
  3009. "Run through the buffer and add overlays to links."
  3010. (if (re-search-forward org-plain-link-re limit t)
  3011. (progn
  3012. (add-text-properties (match-beginning 0) (match-end 0)
  3013. (list 'mouse-face 'highlight
  3014. 'rear-nonsticky t
  3015. 'keymap org-mouse-map
  3016. ))
  3017. t)))
  3018. (defun org-activate-angle-links (limit)
  3019. "Run through the buffer and add overlays to links."
  3020. (if (re-search-forward org-angle-link-re limit t)
  3021. (progn
  3022. (add-text-properties (match-beginning 0) (match-end 0)
  3023. (list 'mouse-face 'highlight
  3024. 'rear-nonsticky t
  3025. 'keymap org-mouse-map
  3026. ))
  3027. t)))
  3028. (defmacro org-maybe-intangible (props)
  3029. "Add '(intangigble t) to PROPS if Emacs version is earlier than Emacs 22.
  3030. In emacs 21, invisible text is not avoided by the command loop, so the
  3031. intangible property is needed to make sure point skips this text.
  3032. In Emacs 22, this is not necessary. The intangible text property has
  3033. led to problems with flyspell. These problems are fixed in flyspell.el,
  3034. but we still avoid setting the property in Emacs 22 and later.
  3035. We use a macro so that the test can happen at compilation time."
  3036. (if (< emacs-major-version 22)
  3037. `(append '(intangible t) ,props)
  3038. props))
  3039. (defun org-activate-bracket-links (limit)
  3040. "Run through the buffer and add overlays to bracketed links."
  3041. (if (re-search-forward org-bracket-link-regexp limit t)
  3042. (let* ((help (concat "LINK: "
  3043. (org-match-string-no-properties 1)))
  3044. ;; FIXME: above we should remove the escapes.
  3045. ;; but that requires another match, protecting match data,
  3046. ;; a lot of overhead for font-lock.
  3047. (ip (org-maybe-intangible
  3048. (list 'invisible 'org-link 'rear-nonsticky t
  3049. 'keymap org-mouse-map 'mouse-face 'highlight
  3050. 'help-echo help)))
  3051. (vp (list 'rear-nonsticky t
  3052. 'keymap org-mouse-map 'mouse-face 'highlight
  3053. 'help-echo help)))
  3054. ;; We need to remove the invisible property here. Table narrowing
  3055. ;; may have made some of this invisible.
  3056. (remove-text-properties (match-beginning 0) (match-end 0)
  3057. '(invisible nil))
  3058. (if (match-end 3)
  3059. (progn
  3060. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3061. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3062. (add-text-properties (match-end 3) (match-end 0) ip))
  3063. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3064. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3065. (add-text-properties (match-end 1) (match-end 0) ip))
  3066. t)))
  3067. (defun org-activate-dates (limit)
  3068. "Run through the buffer and add overlays to dates."
  3069. ; (if (re-search-forward org-tsr-regexp limit t)
  3070. ; (if (re-search-forward
  3071. ; (if org-display-custom-times org-ts-regexp-both org-tsr-regexp-both)
  3072. ; limit t)
  3073. (if (re-search-forward org-tsr-regexp-both limit t)
  3074. (progn
  3075. (add-text-properties (match-beginning 0) (match-end 0)
  3076. (list 'mouse-face 'highlight
  3077. 'rear-nonsticky t
  3078. 'keymap org-mouse-map))
  3079. (when org-display-custom-times
  3080. (if (match-end 3)
  3081. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3082. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3083. t)))
  3084. (defvar org-target-link-regexp nil
  3085. "Regular expression matching radio targets in plain text.")
  3086. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3087. "Regular expression matching a link target.")
  3088. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3089. "Regular expression matching a link target.")
  3090. (defun org-activate-target-links (limit)
  3091. "Run through the buffer and add overlays to target matches."
  3092. (when org-target-link-regexp
  3093. (let ((case-fold-search t))
  3094. (if (re-search-forward org-target-link-regexp limit t)
  3095. (progn
  3096. (add-text-properties (match-beginning 0) (match-end 0)
  3097. (list 'mouse-face 'highlight
  3098. 'rear-nonsticky t
  3099. 'keymap org-mouse-map
  3100. 'help-echo "Radio target link"
  3101. 'org-linked-text t))
  3102. t)))))
  3103. (defun org-update-radio-target-regexp ()
  3104. "Find all radio targets in this file and update the regular expression."
  3105. (interactive)
  3106. (when (memq 'radio org-activate-links)
  3107. (setq org-target-link-regexp
  3108. (org-make-target-link-regexp (org-all-targets 'radio)))
  3109. (org-restart-font-lock)))
  3110. (defun org-hide-wide-columns (limit)
  3111. (let (s e)
  3112. (setq s (text-property-any (point) (or limit (point-max))
  3113. 'org-cwidth t))
  3114. (when s
  3115. (setq e (next-single-property-change s 'org-cwidth))
  3116. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3117. (goto-char e)
  3118. t)))
  3119. (defun org-restart-font-lock ()
  3120. "Restart font-lock-mode, to force refontification."
  3121. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3122. (font-lock-mode -1)
  3123. (font-lock-mode 1)))
  3124. (defun org-all-targets (&optional radio)
  3125. "Return a list of all targets in this file.
  3126. With optional argument RADIO, only find radio targets."
  3127. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3128. rtn)
  3129. (save-excursion
  3130. (goto-char (point-min))
  3131. (while (re-search-forward re nil t)
  3132. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3133. rtn)))
  3134. (defun org-make-target-link-regexp (targets)
  3135. "Make regular expression matching all strings in TARGETS.
  3136. The regular expression finds the targets also if there is a line break
  3137. between words."
  3138. (and targets
  3139. (concat
  3140. "\\<\\("
  3141. (mapconcat
  3142. (lambda (x)
  3143. (while (string-match " +" x)
  3144. (setq x (replace-match "\\s-+" t t x)))
  3145. x)
  3146. targets
  3147. "\\|")
  3148. "\\)\\>")))
  3149. (defvar org-camel-regexp "\\*?\\<[A-Z]+[a-z]+[A-Z][a-zA-Z]*\\>"
  3150. "Matches CamelCase words, possibly with a star before it.")
  3151. (defun org-activate-camels (limit)
  3152. "Run through the buffer and add overlays to dates."
  3153. (if (re-search-forward org-camel-regexp limit t)
  3154. (progn
  3155. (add-text-properties (match-beginning 0) (match-end 0)
  3156. (list 'mouse-face 'highlight
  3157. 'rear-nonsticky t
  3158. 'keymap org-mouse-map))
  3159. t)))
  3160. (defun org-activate-tags (limit)
  3161. (if (re-search-forward "[ \t]\\(:[A-Za-z_@0-9:]+:\\)[ \r\n]" limit t)
  3162. (progn
  3163. (add-text-properties (match-beginning 1) (match-end 1)
  3164. (list 'mouse-face 'highlight
  3165. 'rear-nonsticky t
  3166. 'keymap org-mouse-map))
  3167. t)))
  3168. (defun org-font-lock-level ()
  3169. (save-excursion
  3170. (org-back-to-heading t)
  3171. (- (match-end 0) (match-beginning 0))))
  3172. (defun org-outline-level ()
  3173. (save-excursion
  3174. (looking-at outline-regexp)
  3175. (if (match-beginning 1)
  3176. (+ (org-get-string-indentation (match-string 1)) 1000)
  3177. (- (match-end 0) (match-beginning 0)))))
  3178. (defvar org-font-lock-keywords nil)
  3179. (defun org-set-font-lock-defaults ()
  3180. (let* ((em org-fontify-emphasized-text)
  3181. (lk org-activate-links)
  3182. (org-font-lock-extra-keywords
  3183. ;; Headlines
  3184. (list
  3185. '("^\\(\\**\\)\\(\\*\\)\\(.*\\)" (1 (org-get-level-face 1))
  3186. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3187. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3188. (1 'org-table))
  3189. ;; Links
  3190. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3191. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3192. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3193. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3194. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3195. (if (memq 'camel lk) '(org-activate-camels (0 'org-link t)))
  3196. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3197. (if org-table-limit-column-width
  3198. '(org-hide-wide-columns (0 nil append)))
  3199. ;; TODO lines
  3200. (list (concat "^\\*+[ \t]*" org-not-done-regexp)
  3201. '(1 'org-todo t))
  3202. ;; Priorities
  3203. (list (concat "\\[#[A-Z]\\]") '(0 'org-special-keyword t))
  3204. ;; Special keywords
  3205. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3206. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3207. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3208. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3209. ;; Emphasis
  3210. (if em
  3211. (if (featurep 'xemacs)
  3212. '(org-do-emphasis-faces (0 nil append))
  3213. '(org-do-emphasis-faces)))
  3214. ;; Checkboxes, similar to Frank Ruell's org-checklet.el
  3215. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[ X]\\]\\)"
  3216. 2 'bold prepend)
  3217. (if org-provide-checkbox-statistics
  3218. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3219. (0 (org-get-checkbox-statistics-face) t)))
  3220. ;; COMMENT
  3221. (list (concat "^\\*+[ \t]*\\<\\(" org-comment-string
  3222. "\\|" org-quote-string "\\)\\>")
  3223. '(1 'org-special-keyword t))
  3224. '("^#.*" (0 'font-lock-comment-face t))
  3225. ;; DONE
  3226. (if org-fontify-done-headline
  3227. (list (concat "^[*]+ +\\<\\(" org-done-string "\\)\\(.*\\)\\>")
  3228. '(1 'org-done t) '(2 'org-headline-done t))
  3229. (list (concat "^[*]+ +\\<\\(" org-done-string "\\)\\>")
  3230. '(1 'org-done t)))
  3231. ;; Table stuff
  3232. '("^[ \t]*\\(:.*\\)" (1 'org-table t))
  3233. '("| *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3234. '("^[ \t]*| *\\([#!$*_^]\\) *|" (1 'org-formula t))
  3235. (if org-format-transports-properties-p
  3236. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3237. '("^\\*+ \\(.*:ARCHIVE:.*\\)" (1 'org-archived prepend))
  3238. )))
  3239. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3240. ;; Now set the full font-lock-keywords
  3241. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3242. (org-set-local 'font-lock-defaults
  3243. '(org-font-lock-keywords t nil nil backward-paragraph))
  3244. (kill-local-variable 'font-lock-keywords) nil))
  3245. (defvar org-m nil)
  3246. (defvar org-l nil)
  3247. (defvar org-f nil)
  3248. (defun org-get-level-face (n)
  3249. "Get the right face for match N in font-lock matching of healdines."
  3250. (setq org-l (- (match-end 2) (match-beginning 1)))
  3251. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3252. ; (setq org-f (nth (1- (% org-l org-n-levels)) org-level-faces))
  3253. (setq org-f (nth (% (1- org-l) org-n-levels) org-level-faces))
  3254. (cond
  3255. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3256. ((eq n 2) org-f)
  3257. (t (if org-level-color-stars-only nil org-f))))
  3258. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3259. "Remove fontification and activation overlays from links."
  3260. (font-lock-default-unfontify-region beg end)
  3261. (let* ((buffer-undo-list t)
  3262. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3263. (inhibit-modification-hooks t)
  3264. deactivate-mark buffer-file-name buffer-file-truename)
  3265. (remove-text-properties beg end
  3266. '(mouse-face t keymap t org-linked-text t
  3267. rear-nonsticky t
  3268. invisible t intangible t))))
  3269. ;;; Visibility cycling
  3270. (defvar org-cycle-global-status nil)
  3271. (make-variable-buffer-local 'org-cycle-global-status)
  3272. (defvar org-cycle-subtree-status nil)
  3273. (make-variable-buffer-local 'org-cycle-subtree-status)
  3274. ;;;###autoload
  3275. (defun org-cycle (&optional arg)
  3276. "Visibility cycling for Org-mode.
  3277. - When this function is called with a prefix argument, rotate the entire
  3278. buffer through 3 states (global cycling)
  3279. 1. OVERVIEW: Show only top-level headlines.
  3280. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3281. 3. SHOW ALL: Show everything.
  3282. - When point is at the beginning of a headline, rotate the subtree started
  3283. by this line through 3 different states (local cycling)
  3284. 1. FOLDED: Only the main headline is shown.
  3285. 2. CHILDREN: The main headline and the direct children are shown.
  3286. From this state, you can move to one of the children
  3287. and zoom in further.
  3288. 3. SUBTREE: Show the entire subtree, including body text.
  3289. - When there is a numeric prefix, go up to a heading with level ARG, do
  3290. a `show-subtree' and return to the previous cursor position. If ARG
  3291. is negative, go up that many levels.
  3292. - When point is not at the beginning of a headline, execute
  3293. `indent-relative', like TAB normally does. See the option
  3294. `org-cycle-emulate-tab' for details.
  3295. - Special case: if point is the the beginning of the buffer and there is
  3296. no headline in line 1, this function will act as if called with prefix arg."
  3297. (interactive "P")
  3298. (let* ((outline-regexp
  3299. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3300. "\\(?:\\*+\\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3301. outline-regexp))
  3302. (bob-special (and org-cycle-global-at-bob (bobp)
  3303. (not (looking-at outline-regexp))))
  3304. (org-cycle-hook
  3305. (if bob-special
  3306. (delq 'org-optimize-window-after-visibility-change
  3307. (copy-sequence org-cycle-hook))
  3308. org-cycle-hook))
  3309. (pos (point)))
  3310. (if (or bob-special (equal arg '(4)))
  3311. ;; special case: use global cycling
  3312. (setq arg t))
  3313. (cond
  3314. ((org-at-table-p 'any)
  3315. ;; Enter the table or move to the next field in the table
  3316. (or (org-table-recognize-table.el)
  3317. (progn
  3318. (if arg (org-table-edit-field t)
  3319. (org-table-justify-field-maybe)
  3320. (call-interactively 'org-table-next-field)))))
  3321. ((eq arg t) ;; Global cycling
  3322. (cond
  3323. ((and (eq last-command this-command)
  3324. (eq org-cycle-global-status 'overview))
  3325. ;; We just created the overview - now do table of contents
  3326. ;; This can be slow in very large buffers, so indicate action
  3327. (message "CONTENTS...")
  3328. (org-content)
  3329. (message "CONTENTS...done")
  3330. (setq org-cycle-global-status 'contents)
  3331. (run-hook-with-args 'org-cycle-hook 'contents))
  3332. ((and (eq last-command this-command)
  3333. (eq org-cycle-global-status 'contents))
  3334. ;; We just showed the table of contents - now show everything
  3335. (show-all)
  3336. (message "SHOW ALL")
  3337. (setq org-cycle-global-status 'all)
  3338. (run-hook-with-args 'org-cycle-hook 'all))
  3339. (t
  3340. ;; Default action: go to overview
  3341. (org-overview)
  3342. (message "OVERVIEW")
  3343. (setq org-cycle-global-status 'overview)
  3344. (run-hook-with-args 'org-cycle-hook 'overview))))
  3345. ((integerp arg)
  3346. ;; Show-subtree, ARG levels up from here.
  3347. (save-excursion
  3348. (org-back-to-heading)
  3349. (outline-up-heading (if (< arg 0) (- arg)
  3350. (- (funcall outline-level) arg)))
  3351. (org-show-subtree)))
  3352. ((save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3353. ;; At a heading: rotate between three different views
  3354. (org-back-to-heading)
  3355. (let ((goal-column 0) eoh eol eos)
  3356. ;; First, some boundaries
  3357. (save-excursion
  3358. (org-back-to-heading)
  3359. (save-excursion
  3360. (beginning-of-line 2)
  3361. (while (and (not (eobp)) ;; this is like `next-line'
  3362. (get-char-property (1- (point)) 'invisible))
  3363. (beginning-of-line 2)) (setq eol (point)))
  3364. (outline-end-of-heading) (setq eoh (point))
  3365. (org-end-of-subtree t) (setq eos (point))
  3366. (outline-next-heading))
  3367. ;; Find out what to do next and set `this-command'
  3368. (cond
  3369. ((and (= eos eoh)
  3370. ;; Nothing is hidden behind this heading
  3371. (message "EMPTY ENTRY")
  3372. (setq org-cycle-subtree-status nil)))
  3373. ((>= eol eos)
  3374. ;; Entire subtree is hidden in one line: open it
  3375. (org-show-entry)
  3376. (show-children)
  3377. (message "CHILDREN")
  3378. (setq org-cycle-subtree-status 'children)
  3379. (run-hook-with-args 'org-cycle-hook 'children))
  3380. ((and (eq last-command this-command)
  3381. (eq org-cycle-subtree-status 'children))
  3382. ;; We just showed the children, now show everything.
  3383. (org-show-subtree)
  3384. (message "SUBTREE")
  3385. (setq org-cycle-subtree-status 'subtree)
  3386. (run-hook-with-args 'org-cycle-hook 'subtree))
  3387. (t
  3388. ;; Default action: hide the subtree.
  3389. (hide-subtree)
  3390. (message "FOLDED")
  3391. (setq org-cycle-subtree-status 'folded)
  3392. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3393. ;; TAB emulation
  3394. (buffer-read-only (org-back-to-heading))
  3395. ((org-try-cdlatex-tab))
  3396. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3397. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3398. (or (and (eq org-cycle-emulate-tab 'white)
  3399. (= (match-end 0) (point-at-eol)))
  3400. (and (eq org-cycle-emulate-tab 'whitestart)
  3401. (>= (match-end 0) pos))))
  3402. t
  3403. (eq org-cycle-emulate-tab t))
  3404. (if (and (looking-at "[ \n\r\t]")
  3405. (string-match "^[ \t]*$" (buffer-substring
  3406. (point-at-bol) (point))))
  3407. (progn
  3408. (beginning-of-line 1)
  3409. (and (looking-at "[ \t]+") (replace-match ""))))
  3410. (indent-relative))
  3411. (t (save-excursion
  3412. (org-back-to-heading)
  3413. (org-cycle))))))
  3414. ;;;###autoload
  3415. (defun org-global-cycle (&optional arg)
  3416. "Cycle the global visibility. For details see `org-cycle'."
  3417. (interactive "P")
  3418. (let ((org-cycle-include-plain-lists
  3419. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  3420. (if (integerp arg)
  3421. (progn
  3422. (show-all)
  3423. (hide-sublevels arg)
  3424. (setq org-cycle-global-status 'contents))
  3425. (org-cycle '(4)))))
  3426. (defun org-overview ()
  3427. "Switch to overview mode, shoing only top-level headlines.
  3428. Really, this shows all headlines with level equal or greater than the level
  3429. of the first headline in the buffer. This is important, because if the
  3430. first headline is not level one, then (hide-sublevels 1) gives confusing
  3431. results."
  3432. (interactive)
  3433. (hide-sublevels (save-excursion
  3434. (goto-char (point-min))
  3435. (if (re-search-forward (concat "^" outline-regexp) nil t)
  3436. (progn
  3437. (goto-char (match-beginning 0))
  3438. (funcall outline-level))
  3439. 1))))
  3440. ;; FIXME: allow an argument to give a limiting level for this.
  3441. (defun org-content ()
  3442. "Show all headlines in the buffer, like a table of contents"
  3443. (interactive)
  3444. (save-excursion
  3445. ;; Visit all headings and show their offspring
  3446. (goto-char (point-max))
  3447. (catch 'exit
  3448. (while (and (progn (condition-case nil
  3449. (outline-previous-visible-heading 1)
  3450. (error (goto-char (point-min))))
  3451. t)
  3452. (looking-at outline-regexp))
  3453. (show-branches)
  3454. (if (bobp) (throw 'exit nil))))))
  3455. (defun org-optimize-window-after-visibility-change (state)
  3456. "Adjust the window after a change in outline visibility.
  3457. This function is the default value of the hook `org-cycle-hook'."
  3458. (when (get-buffer-window (current-buffer))
  3459. (cond
  3460. ((eq state 'overview) (org-first-headline-recenter 1))
  3461. ((eq state 'content) nil)
  3462. ((eq state 'all) nil)
  3463. ((eq state 'folded) nil)
  3464. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  3465. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  3466. (defun org-subtree-end-visible-p ()
  3467. "Is the end of the current subtree visible?"
  3468. (pos-visible-in-window-p
  3469. (save-excursion (org-end-of-subtree t) (point))))
  3470. (defun org-first-headline-recenter (&optional N)
  3471. "Move cursor to the first headline and recenter the headline.
  3472. Optional argument N means, put the headline into the Nth line of the window."
  3473. (goto-char (point-min))
  3474. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  3475. (beginning-of-line)
  3476. (recenter (prefix-numeric-value N))))
  3477. (defvar org-goto-window-configuration nil)
  3478. (defvar org-goto-marker nil)
  3479. (defvar org-goto-map (make-sparse-keymap))
  3480. (let ((cmds '(isearch-forward isearch-backward)) cmd)
  3481. (while (setq cmd (pop cmds))
  3482. (substitute-key-definition cmd cmd org-goto-map global-map)))
  3483. (define-key org-goto-map "\C-m" 'org-goto-ret)
  3484. (define-key org-goto-map [(left)] 'org-goto-left)
  3485. (define-key org-goto-map [(right)] 'org-goto-right)
  3486. (define-key org-goto-map [(?q)] 'org-goto-quit)
  3487. (define-key org-goto-map [(control ?g)] 'org-goto-quit)
  3488. (define-key org-goto-map "\C-i" 'org-cycle)
  3489. (define-key org-goto-map [(tab)] 'org-cycle)
  3490. (define-key org-goto-map [(down)] 'outline-next-visible-heading)
  3491. (define-key org-goto-map [(up)] 'outline-previous-visible-heading)
  3492. (define-key org-goto-map "n" 'outline-next-visible-heading)
  3493. (define-key org-goto-map "p" 'outline-previous-visible-heading)
  3494. (define-key org-goto-map "f" 'outline-forward-same-level)
  3495. (define-key org-goto-map "b" 'outline-backward-same-level)
  3496. (define-key org-goto-map "u" 'outline-up-heading)
  3497. (define-key org-goto-map "\C-c\C-n" 'outline-next-visible-heading)
  3498. (define-key org-goto-map "\C-c\C-p" 'outline-previous-visible-heading)
  3499. (define-key org-goto-map "\C-c\C-f" 'outline-forward-same-level)
  3500. (define-key org-goto-map "\C-c\C-b" 'outline-backward-same-level)
  3501. (define-key org-goto-map "\C-c\C-u" 'outline-up-heading)
  3502. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  3503. (while l (define-key org-goto-map (int-to-string (pop l)) 'digit-argument)))
  3504. (defconst org-goto-help
  3505. "Select a location to jump to, press RET
  3506. \[Up]/[Down]=next/prev headline TAB=cycle visibility RET=select [Q]uit")
  3507. (defun org-goto ()
  3508. "Go to a different location of the document, keeping current visibility.
  3509. When you want to go to a different location in a document, the fastest way
  3510. is often to fold the entire buffer and then dive into the tree. This
  3511. method has the disadvantage, that the previous location will be folded,
  3512. which may not be what you want.
  3513. This command works around this by showing a copy of the current buffer in
  3514. overview mode. You can dive into the tree in that copy, to find the
  3515. location you want to reach. When pressing RET, the command returns to the
  3516. original buffer in which the visibility is still unchanged. It then jumps
  3517. to the new location, making it and the headline hierarchy above it visible."
  3518. (interactive)
  3519. (let* ((org-goto-start-pos (point))
  3520. (selected-point
  3521. (org-get-location (current-buffer) org-goto-help)))
  3522. (if selected-point
  3523. (progn
  3524. (org-mark-ring-push org-goto-start-pos)
  3525. (goto-char selected-point)
  3526. (if (or (org-invisible-p) (org-invisible-p2))
  3527. (org-show-context 'org-goto)))
  3528. (error "Quit"))))
  3529. (defun org-get-location (buf help)
  3530. "Let the user select a location in the Org-mode buffer BUF.
  3531. This function uses a recursive edit. It returns the selected position
  3532. or nil."
  3533. (let (org-selected-point)
  3534. (save-excursion
  3535. (save-window-excursion
  3536. (delete-other-windows)
  3537. (switch-to-buffer (get-buffer-create "*org-goto*"))
  3538. (with-output-to-temp-buffer "*Help*"
  3539. (princ help))
  3540. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  3541. (setq buffer-read-only nil)
  3542. (erase-buffer)
  3543. (insert-buffer-substring buf)
  3544. (let ((org-startup-truncated t)
  3545. (org-startup-folded t)
  3546. (org-startup-align-all-tables nil)
  3547. (org-startup-with-deadline-check nil))
  3548. (org-mode))
  3549. (setq buffer-read-only t)
  3550. (if (boundp 'org-goto-start-pos)
  3551. (goto-char org-goto-start-pos)
  3552. (goto-char (point-min)))
  3553. (org-beginning-of-line)
  3554. (message "Select location and press RET")
  3555. ;; now we make sure that during selection, ony very few keys work
  3556. ;; and that it is impossible to switch to another window.
  3557. (let ((gm (current-global-map))
  3558. (overriding-local-map org-goto-map))
  3559. (unwind-protect
  3560. (progn
  3561. (use-global-map org-goto-map)
  3562. (recursive-edit))
  3563. (use-global-map gm)))))
  3564. (kill-buffer "*org-goto*")
  3565. org-selected-point))
  3566. (defun org-goto-ret (&optional arg)
  3567. "Finish `org-goto' by going to the new location."
  3568. (interactive "P")
  3569. (setq org-selected-point (point)
  3570. current-prefix-arg arg)
  3571. (throw 'exit nil))
  3572. (defun org-goto-left ()
  3573. "Finish `org-goto' by going to the new location."
  3574. (interactive)
  3575. (if (org-on-heading-p)
  3576. (progn
  3577. (beginning-of-line 1)
  3578. (setq org-selected-point (point)
  3579. current-prefix-arg (- (match-end 0) (match-beginning 0)))
  3580. (throw 'exit nil))
  3581. (error "Not on a heading")))
  3582. (defun org-goto-right ()
  3583. "Finish `org-goto' by going to the new location."
  3584. (interactive)
  3585. (if (org-on-heading-p)
  3586. (progn
  3587. (outline-end-of-subtree)
  3588. (or (eobp) (forward-char 1))
  3589. (setq org-selected-point (point)
  3590. current-prefix-arg (- (match-end 0) (match-beginning 0)))
  3591. (throw 'exit nil))
  3592. (error "Not on a heading")))
  3593. (defun org-goto-quit ()
  3594. "Finish `org-goto' without cursor motion."
  3595. (interactive)
  3596. (setq org-selected-point nil)
  3597. (throw 'exit nil))
  3598. ;;; Promotion, Demotion, Inserting new headlines
  3599. (defvar org-ignore-region nil
  3600. "To temporarily disable the active region.")
  3601. (defun org-insert-heading (&optional force-heading)
  3602. "Insert a new heading or item with same depth at point.
  3603. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  3604. If point is at the beginning of a headline, insert a sibling before the
  3605. current headline. If point is in the middle of a headline, split the headline
  3606. at that position and make the rest of the headline part of the sibling below
  3607. the current headline."
  3608. (interactive "P")
  3609. (if (= (buffer-size) 0)
  3610. (insert "\n* ")
  3611. (when (or force-heading (not (org-insert-item)))
  3612. (let* ((head (save-excursion
  3613. (condition-case nil
  3614. (progn
  3615. (org-back-to-heading)
  3616. (match-string 0))
  3617. (error "*"))))
  3618. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  3619. pos)
  3620. (cond
  3621. ((and (org-on-heading-p) (bolp)
  3622. (save-excursion (backward-char 1) (not (org-invisible-p))))
  3623. (open-line (if blank 2 1)))
  3624. ((and (bolp) (save-excursion
  3625. (backward-char 1) (not (org-invisible-p))))
  3626. nil)
  3627. (t (newline (if blank 2 1))))
  3628. (insert head) (just-one-space)
  3629. (setq pos (point))
  3630. (end-of-line 1)
  3631. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  3632. (run-hooks 'org-insert-heading-hook)))))
  3633. (defun org-in-item-p ()
  3634. "It the cursor inside a plain list item.
  3635. Does not have to be the first line."
  3636. (save-excursion
  3637. (condition-case nil
  3638. (progn
  3639. (org-beginning-of-item)
  3640. (org-at-item-p)
  3641. t)
  3642. (error nil))))
  3643. (defun org-insert-item (&optional checkbox)
  3644. "Insert a new item at the current level.
  3645. Return t when things worked, nil when we are not in an item."
  3646. (when (save-excursion
  3647. (condition-case nil
  3648. (progn
  3649. (org-beginning-of-item)
  3650. (org-at-item-p)
  3651. (if (org-invisible-p) (error "Invisible item"))
  3652. t)
  3653. (error nil)))
  3654. (let* ((bul (match-string 0))
  3655. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  3656. (match-end 0)))
  3657. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  3658. pos)
  3659. (cond
  3660. ((and (org-at-item-p) (<= (point) eow))
  3661. ;; before the bullet
  3662. (beginning-of-line 1)
  3663. (open-line (if blank 2 1)))
  3664. ((<= (point) eow)
  3665. (beginning-of-line 1))
  3666. (t (newline (if blank 2 1))))
  3667. (insert bul (if checkbox "[ ]" ""))
  3668. (just-one-space)
  3669. (setq pos (point))
  3670. (end-of-line 1)
  3671. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  3672. (org-maybe-renumber-ordered-list)
  3673. (and checkbox (org-update-checkbox-count-maybe))
  3674. t))
  3675. (defun org-insert-todo-heading (arg)
  3676. "Insert a new heading with the same level and TODO state as current heading.
  3677. If the heading has no TODO state, or if the state is DONE, use the first
  3678. state (TODO by default). Also with prefix arg, force first state."
  3679. (interactive "P")
  3680. (when (not (org-insert-item 'checkbox))
  3681. (org-insert-heading)
  3682. (save-excursion
  3683. (org-back-to-heading)
  3684. (outline-previous-heading)
  3685. (looking-at org-todo-line-regexp))
  3686. (if (or arg
  3687. (not (match-beginning 2))
  3688. (equal (match-string 2) org-done-string))
  3689. (insert (car org-todo-keywords) " ")
  3690. (insert (match-string 2) " "))))
  3691. (defun org-promote-subtree ()
  3692. "Promote the entire subtree.
  3693. See also `org-promote'."
  3694. (interactive)
  3695. (save-excursion
  3696. (org-map-tree 'org-promote))
  3697. (org-fix-position-after-promote))
  3698. (defun org-demote-subtree ()
  3699. "Demote the entire subtree. See `org-demote'.
  3700. See also `org-promote'."
  3701. (interactive)
  3702. (save-excursion
  3703. (org-map-tree 'org-demote))
  3704. (org-fix-position-after-promote))
  3705. (defun org-do-promote ()
  3706. "Promote the current heading higher up the tree.
  3707. If the region is active in `transient-mark-mode', promote all headings
  3708. in the region."
  3709. (interactive)
  3710. (save-excursion
  3711. (if (org-region-active-p)
  3712. (org-map-region 'org-promote (region-beginning) (region-end))
  3713. (org-promote)))
  3714. (org-fix-position-after-promote))
  3715. (defun org-do-demote ()
  3716. "Demote the current heading lower down the tree.
  3717. If the region is active in `transient-mark-mode', demote all headings
  3718. in the region."
  3719. (interactive)
  3720. (save-excursion
  3721. (if (org-region-active-p)
  3722. (org-map-region 'org-demote (region-beginning) (region-end))
  3723. (org-demote)))
  3724. (org-fix-position-after-promote))
  3725. (defun org-fix-position-after-promote ()
  3726. "Make sure that after pro/demotion cursor position is right."
  3727. (if (and (equal (char-after) ?\n)
  3728. (save-excursion
  3729. (skip-chars-backward "a-zA-Z0-9_@")
  3730. (looking-at org-todo-regexp)))
  3731. (insert " "))
  3732. (and (equal (char-after) ?\ )
  3733. (equal (char-before) ?*)
  3734. (forward-char 1)))
  3735. (defun org-get-legal-level (level &optional change)
  3736. "Rectify a level change under the influence of `org-odd-levels-only'
  3737. LEVEL is a current level, CHANGE is by how much the level should be
  3738. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  3739. even level numbers will become the next higher odd number."
  3740. (if org-odd-levels-only
  3741. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  3742. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  3743. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  3744. (max 1 (+ level change))))
  3745. (defun org-promote ()
  3746. "Promote the current heading higher up the tree.
  3747. If the region is active in `transient-mark-mode', promote all headings
  3748. in the region."
  3749. (org-back-to-heading t)
  3750. (let* ((level (save-match-data (funcall outline-level)))
  3751. (up-head (make-string (org-get-legal-level level -1) ?*))
  3752. (diff (abs (- level (length up-head)))))
  3753. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  3754. (replace-match up-head nil t)
  3755. ;; Fixup tag positioning
  3756. (and org-auto-align-tags (org-set-tags nil t))
  3757. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  3758. (defun org-demote ()
  3759. "Demote the current heading lower down the tree.
  3760. If the region is active in `transient-mark-mode', demote all headings
  3761. in the region."
  3762. (org-back-to-heading t)
  3763. (let* ((level (save-match-data (funcall outline-level)))
  3764. (down-head (make-string (org-get-legal-level level 1) ?*))
  3765. (diff (abs (- level (length down-head)))))
  3766. (replace-match down-head nil t)
  3767. ;; Fixup tag positioning
  3768. (and org-auto-align-tags (org-set-tags nil t))
  3769. (if org-adapt-indentation (org-fixup-indentation diff))))
  3770. (defun org-sort (with-case)
  3771. "Call `org-sort-entries' or `org-table-sort-lines', depending on context."
  3772. (interactive "P")
  3773. (if (org-at-table-p)
  3774. (org-call-with-arg 'org-table-sort-lines with-case)
  3775. (org-call-with-arg 'org-sort-entries with-case)))
  3776. (defun org-sort-entries (&optional with-case sorting-type)
  3777. "Sort entries on a certain level of an outline tree.
  3778. If there is an active region, the entries in the region are sorted.
  3779. If not, the children of the entry at point are sorted.
  3780. Sorting can be alphabetically, numerically, and by date/time as given by
  3781. the first time stamp in the entry. The command prompts for the sorting
  3782. type unless it has been given to the function through the SORTING-TYPE
  3783. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T).
  3784. Comparing entries ignores case by default. However, with an optional argument
  3785. WITH-CASE, the sorting considers case as well. With two prefix arguments
  3786. `C-u C-u', sorting is case-sensitive and duplicate entries will be removed."
  3787. (interactive "P")
  3788. (let ((unique (equal with-case '(16)))
  3789. start beg end entries stars re re2 p nentries (nremoved 0) last txt)
  3790. ;; Find beginning and end of region to sort
  3791. (if (org-region-active-p)
  3792. (progn
  3793. ;; we will sort the region
  3794. (setq end (region-end))
  3795. (goto-char (1- (setq start (region-beginning)))))
  3796. ;; we will sort the children of the current headline
  3797. (setq start (point) end (org-end-of-subtree))
  3798. (goto-char start)
  3799. (show-subtree))
  3800. (outline-next-heading) ; this is the first heading to be included
  3801. (setq beg (point))
  3802. (if (>= (point) end) (error "Nothing to sort"))
  3803. (looking-at "\\(\\*+\\)")
  3804. (setq stars (match-string 1)
  3805. re (concat "^" (regexp-quote stars) " +")
  3806. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  3807. txt (buffer-substring beg end))
  3808. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  3809. (if (string-match re2 txt)
  3810. (error "Region to sort contains a level above the first entry"))
  3811. ;; Make a list that can be sorted.
  3812. ;; The car is the string for comparison, the cdr is the subtree
  3813. (message "Sorting entries...")
  3814. (setq entries
  3815. (mapcar
  3816. (lambda (x)
  3817. (string-match "^.*\\(\n.*\\)?" x) ; take two lines
  3818. (cons (match-string 0 x) x))
  3819. (org-split-string txt re)))
  3820. ;; Sort the list
  3821. (setq entries (org-do-sort
  3822. entries
  3823. (if (org-region-active-p) "region" "children")
  3824. with-case sorting-type))
  3825. ;; Delete the old stuff
  3826. (goto-char beg)
  3827. (kill-region beg end)
  3828. (setq nentries (length entries))
  3829. ;; Insert the sorted entries, and remove duplicates if this is required
  3830. (while (setq p (pop entries))
  3831. (if (and unique (equal last (setq last (org-trim (cdr p)))))
  3832. (setq nremoved (1+ nremoved)) ; same entry as before, skip it
  3833. (insert stars " " (cdr p))))
  3834. (goto-char start)
  3835. (message "Sorting entries...done (%d entries%s)"
  3836. nentries
  3837. (if unique (format ", %d duplicates removed" nremoved) ""))))
  3838. (defun org-do-sort (table what &optional with-case sorting-type)
  3839. "Sort TABLE of WHAT according to SORTING-TYPE.
  3840. The user will be prompted for the SORTING-TYPE if the call to this
  3841. function does not specify it. WHAT is only for the prompt, to indicate
  3842. what is being sorted. The sorting key will be extracted from
  3843. the car of the elements of the table.
  3844. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  3845. (unless sorting-type
  3846. (message
  3847. "Sort %s:[a]lphabetically [n]umerically [t]ime. A/N/T means reversed:"
  3848. what)
  3849. (setq sorting-type (read-char-exclusive)))
  3850. (let ((dcst (downcase sorting-type))
  3851. extractfun comparefun)
  3852. ;; Define the appropriate functions
  3853. (cond
  3854. ((= dcst ?n)
  3855. (setq extractfun 'string-to-number
  3856. comparefun (if (= dcst sorting-type) '< '>)))
  3857. ((= dcst ?a)
  3858. (setq extractfun (if with-case 'identity 'downcase)
  3859. comparefun (if (= dcst sorting-type)
  3860. 'string<
  3861. (lambda (a b) (and (not (string< a b))
  3862. (not (string= a b)))))))
  3863. ((= dcst ?t)
  3864. (setq extractfun
  3865. (lambda (x)
  3866. (if (string-match org-ts-regexp x)
  3867. (time-to-seconds
  3868. (org-time-string-to-time (match-string 0 x)))
  3869. 0))
  3870. comparefun (if (= dcst sorting-type) '< '>)))
  3871. (t (error "Invalid sorting type `%c'" sorting-type)))
  3872. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  3873. table)
  3874. (lambda (a b) (funcall comparefun (car a) (car b))))))
  3875. (defun org-map-tree (fun)
  3876. "Call FUN for every heading underneath the current one."
  3877. (org-back-to-heading)
  3878. (let ((level (funcall outline-level)))
  3879. (save-excursion
  3880. (funcall fun)
  3881. (while (and (progn
  3882. (outline-next-heading)
  3883. (> (funcall outline-level) level))
  3884. (not (eobp)))
  3885. (funcall fun)))))
  3886. (defun org-map-region (fun beg end)
  3887. "Call FUN for every heading between BEG and END."
  3888. (let ((org-ignore-region t))
  3889. (save-excursion
  3890. (setq end (copy-marker end))
  3891. (goto-char beg)
  3892. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  3893. (< (point) end))
  3894. (funcall fun))
  3895. (while (and (progn
  3896. (outline-next-heading)
  3897. (< (point) end))
  3898. (not (eobp)))
  3899. (funcall fun)))))
  3900. (defun org-fixup-indentation (diff)
  3901. "Change the indentation in the current entry by DIFF
  3902. However, if any line in the current entry has no indentation, or if it
  3903. would end up with no indentation after the change, nothing at all is done."
  3904. (save-excursion
  3905. (let ((end (save-excursion (outline-next-heading)
  3906. (point-marker)))
  3907. (prohibit (if (> diff 0)
  3908. "^\\S-"
  3909. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  3910. col)
  3911. (unless (save-excursion (re-search-forward prohibit end t))
  3912. (while (re-search-forward "^[ \t]+" end t)
  3913. (goto-char (match-end 0))
  3914. (setq col (current-column))
  3915. (if (< diff 0) (replace-match ""))
  3916. (indent-to (+ diff col))))
  3917. (move-marker end nil))))
  3918. ;;; Vertical tree motion, cutting and pasting of subtrees
  3919. (defun org-move-subtree-up (&optional arg)
  3920. "Move the current subtree up past ARG headlines of the same level."
  3921. (interactive "p")
  3922. (org-move-subtree-down (- (prefix-numeric-value arg))))
  3923. (defun org-move-subtree-down (&optional arg)
  3924. "Move the current subtree down past ARG headlines of the same level."
  3925. (interactive "p")
  3926. (setq arg (prefix-numeric-value arg))
  3927. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  3928. 'outline-get-last-sibling))
  3929. (ins-point (make-marker))
  3930. (cnt (abs arg))
  3931. beg end txt folded)
  3932. ;; Select the tree
  3933. (org-back-to-heading)
  3934. (setq beg (point))
  3935. (save-match-data
  3936. (save-excursion (outline-end-of-heading)
  3937. (setq folded (org-invisible-p)))
  3938. (outline-end-of-subtree))
  3939. (outline-next-heading)
  3940. (setq end (point))
  3941. ;; Find insertion point, with error handling
  3942. (goto-char beg)
  3943. (while (> cnt 0)
  3944. (or (and (funcall movfunc) (looking-at outline-regexp))
  3945. (progn (goto-char beg)
  3946. (error "Cannot move past superior level or buffer limit")))
  3947. (setq cnt (1- cnt)))
  3948. (if (> arg 0)
  3949. ;; Moving forward - still need to move over subtree
  3950. (progn (outline-end-of-subtree)
  3951. (outline-next-heading)
  3952. (if (not (or (looking-at (concat "^" outline-regexp))
  3953. (bolp)))
  3954. (newline))))
  3955. (move-marker ins-point (point))
  3956. (setq txt (buffer-substring beg end))
  3957. (delete-region beg end)
  3958. (insert txt)
  3959. (goto-char ins-point)
  3960. (if folded (hide-subtree))
  3961. (move-marker ins-point nil)))
  3962. (defvar org-subtree-clip ""
  3963. "Clipboard for cut and paste of subtrees.
  3964. This is actually only a copy of the kill, because we use the normal kill
  3965. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  3966. (defvar org-subtree-clip-folded nil
  3967. "Was the last copied subtree folded?
  3968. This is used to fold the tree back after pasting.")
  3969. (defun org-cut-subtree ()
  3970. "Cut the current subtree into the clipboard.
  3971. This is a short-hand for marking the subtree and then cutting it."
  3972. (interactive)
  3973. (org-copy-subtree 'cut))
  3974. (defun org-copy-subtree (&optional cut)
  3975. "Cut the current subtree into the clipboard.
  3976. This is a short-hand for marking the subtree and then copying it.
  3977. If CUT is non-nil, actually cut the subtree."
  3978. (interactive)
  3979. (let (beg end folded)
  3980. (org-back-to-heading)
  3981. (setq beg (point))
  3982. (save-match-data
  3983. (save-excursion (outline-end-of-heading)
  3984. (setq folded (org-invisible-p)))
  3985. (outline-end-of-subtree))
  3986. (if (equal (char-after) ?\n) (forward-char 1))
  3987. (setq end (point))
  3988. (goto-char beg)
  3989. (when (> end beg)
  3990. (setq org-subtree-clip-folded folded)
  3991. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  3992. (setq org-subtree-clip (current-kill 0))
  3993. (message "%s: Subtree with %d characters"
  3994. (if cut "Cut" "Copied")
  3995. (length org-subtree-clip)))))
  3996. (defun org-paste-subtree (&optional level tree)
  3997. "Paste the clipboard as a subtree, with modification of headline level.
  3998. The entire subtree is promoted or demoted in order to match a new headline
  3999. level. By default, the new level is derived from the visible headings
  4000. before and after the insertion point, and taken to be the inferior headline
  4001. level of the two. So if the previous visible heading is level 3 and the
  4002. next is level 4 (or vice versa), level 4 will be used for insertion.
  4003. This makes sure that the subtree remains an independent subtree and does
  4004. not swallow low level entries.
  4005. You can also force a different level, either by using a numeric prefix
  4006. argument, or by inserting the heading marker by hand. For example, if the
  4007. cursor is after \"*****\", then the tree will be shifted to level 5.
  4008. If you want to insert the tree as is, just use \\[yank].
  4009. If optional TREE is given, use this text instead of the kill ring."
  4010. (interactive "P")
  4011. (unless (org-kill-is-subtree-p tree)
  4012. (error
  4013. (substitute-command-keys
  4014. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4015. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  4016. (^re (concat "^\\(" outline-regexp "\\)"))
  4017. (re (concat "\\(" outline-regexp "\\)"))
  4018. (^re_ (concat "\\(" outline-regexp "\\)[ \t]*"))
  4019. (old-level (if (string-match ^re txt)
  4020. (- (match-end 0) (match-beginning 0))
  4021. -1))
  4022. (force-level (cond (level (prefix-numeric-value level))
  4023. ((string-match
  4024. ^re_ (buffer-substring (point-at-bol) (point)))
  4025. (- (match-end 0) (match-beginning 0)))
  4026. (t nil)))
  4027. (previous-level (save-excursion
  4028. (condition-case nil
  4029. (progn
  4030. (outline-previous-visible-heading 1)
  4031. (if (looking-at re)
  4032. (- (match-end 0) (match-beginning 0))
  4033. 1))
  4034. (error 1))))
  4035. (next-level (save-excursion
  4036. (condition-case nil
  4037. (progn
  4038. (outline-next-visible-heading 1)
  4039. (if (looking-at re)
  4040. (- (match-end 0) (match-beginning 0))
  4041. 1))
  4042. (error 1))))
  4043. (new-level (or force-level (max previous-level next-level)))
  4044. (shift (if (or (= old-level -1)
  4045. (= new-level -1)
  4046. (= old-level new-level))
  4047. 0
  4048. (- new-level old-level)))
  4049. (shift1 shift)
  4050. (delta (if (> shift 0) -1 1))
  4051. (func (if (> shift 0) 'org-demote 'org-promote))
  4052. (org-odd-levels-only nil)
  4053. beg end)
  4054. ;; Remove the forces level indicator
  4055. (if force-level
  4056. (delete-region (point-at-bol) (point)))
  4057. ;; Make sure we start at the beginning of an empty line
  4058. (if (not (bolp)) (insert "\n"))
  4059. (if (not (looking-at "[ \t]*$"))
  4060. (progn (insert "\n") (backward-char 1)))
  4061. ;; Paste
  4062. (setq beg (point))
  4063. (if (string-match "[ \t\r\n]+\\'" txt)
  4064. (setq txt (replace-match "\n" t t txt)))
  4065. (insert txt)
  4066. (setq end (point))
  4067. (if (looking-at "[ \t\r\n]+")
  4068. (replace-match "\n"))
  4069. (goto-char beg)
  4070. ;; Shift if necessary
  4071. (if (= shift 0)
  4072. (message "Pasted at level %d, without shift" new-level)
  4073. (save-restriction
  4074. (narrow-to-region beg end)
  4075. (while (not (= shift 0))
  4076. (org-map-region func (point-min) (point-max))
  4077. (setq shift (+ delta shift)))
  4078. (goto-char (point-min))
  4079. (message "Pasted at level %d, with shift by %d levels"
  4080. new-level shift1)))
  4081. (if (and kill-ring
  4082. (eq org-subtree-clip (current-kill 0))
  4083. org-subtree-clip-folded)
  4084. ;; The tree was folded before it was killed/copied
  4085. (hide-subtree))))
  4086. (defun org-kill-is-subtree-p (&optional txt)
  4087. "Check if the current kill is an outline subtree, or a set of trees.
  4088. Returns nil if kill does not start with a headline, or if the first
  4089. headline level is not the largest headline level in the tree.
  4090. So this will actually accept several entries of equal levels as well,
  4091. which is OK for `org-paste-subtree'.
  4092. If optional TXT is given, check this string instead of the current kill."
  4093. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  4094. (start-level (and kill
  4095. (string-match (concat "\\`" outline-regexp) kill)
  4096. (- (match-end 0) (match-beginning 0))))
  4097. (re (concat "^" outline-regexp))
  4098. (start 1))
  4099. (if (not start-level)
  4100. nil ;; does not even start with a heading
  4101. (catch 'exit
  4102. (while (setq start (string-match re kill (1+ start)))
  4103. (if (< (- (match-end 0) (match-beginning 0)) start-level)
  4104. (throw 'exit nil)))
  4105. t))))
  4106. (defun org-narrow-to-subtree ()
  4107. "Narrow buffer to the current subtree."
  4108. (interactive)
  4109. (save-excursion
  4110. (narrow-to-region
  4111. (progn (org-back-to-heading) (point))
  4112. (progn (org-end-of-subtree t) (point)))))
  4113. ;;; Plain list items
  4114. (defun org-at-item-p ()
  4115. "Is point in a line starting a hand-formatted item?"
  4116. (let ((llt org-plain-list-ordered-item-terminator))
  4117. (save-excursion
  4118. (goto-char (point-at-bol))
  4119. (looking-at
  4120. (cond
  4121. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4122. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4123. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4124. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  4125. (defun org-at-item-checkbox-p ()
  4126. "Is point at a line starting a plain-list item with a checklet?"
  4127. (and (org-at-item-p)
  4128. (save-excursion
  4129. (goto-char (match-end 0))
  4130. (skip-chars-forward " \t")
  4131. (looking-at "\\[[ X]\\]"))))
  4132. (defun org-toggle-checkbox (&optional arg)
  4133. "Toggle the checkbox in the current line."
  4134. (interactive "P")
  4135. (catch 'exit
  4136. (let (beg end status (firstnew 'unknown))
  4137. (cond
  4138. ((org-region-active-p)
  4139. (setq beg (region-beginning) end (region-end)))
  4140. ((org-on-heading-p)
  4141. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  4142. ((org-at-item-checkbox-p)
  4143. (save-excursion
  4144. (replace-match (if (equal (match-string 0) "[ ]") "[X]" "[ ]") t t))
  4145. (throw 'exit t))
  4146. (t (error "Not at a checkbox or heading, and no active region")))
  4147. (save-excursion
  4148. (goto-char beg)
  4149. (while (< (point) end)
  4150. (when (org-at-item-checkbox-p)
  4151. (setq status (equal (match-string 0) "[X]"))
  4152. (when (eq firstnew 'unknown)
  4153. (setq firstnew (not status)))
  4154. (replace-match
  4155. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  4156. (beginning-of-line 2)))))
  4157. (org-update-checkbox-count-maybe))
  4158. (defun org-update-checkbox-count-maybe ()
  4159. "Update checkbox statistics unless turned off by user."
  4160. (when org-provide-checkbox-statistics
  4161. (org-update-checkbox-count)))
  4162. (defun org-update-checkbox-count (&optional all)
  4163. "Update the checkbox statistics in the current section.
  4164. This will find all statistic cookies like [57%] and [6/12] and update them
  4165. with the current numbers. With optional prefix argument ALL, do this for
  4166. the whole buffer."
  4167. (interactive "P")
  4168. (save-excursion
  4169. (let* ((buffer-invisibility-spec nil) ; Emacs 21 compatibility
  4170. (beg (progn (outline-back-to-heading) (point)))
  4171. (end (move-marker (make-marker)
  4172. (progn (outline-next-heading) (point))))
  4173. (re "\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)")
  4174. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[ X]\\]\\)")
  4175. b1 e1 f1 c-on c-off lim (cstat 0))
  4176. (when all
  4177. (goto-char (point-min))
  4178. (outline-next-heading)
  4179. (setq beg (point) end (point-max)))
  4180. (goto-char beg)
  4181. (while (re-search-forward re end t)
  4182. (setq cstat (1+ cstat)
  4183. b1 (match-beginning 0)
  4184. e1 (match-end 0)
  4185. f1 (match-beginning 1)
  4186. lim (cond
  4187. ((org-on-heading-p) (outline-next-heading) (point))
  4188. ((org-at-item-p) (org-end-of-item) (point))
  4189. (t nil))
  4190. c-on 0 c-off 0)
  4191. (goto-char e1)
  4192. (when lim
  4193. (while (re-search-forward re-box lim t)
  4194. (if (equal (match-string 2) "[ ]")
  4195. (setq c-off (1+ c-off))
  4196. (setq c-on (1+ c-on))))
  4197. (delete-region b1 e1)
  4198. (goto-char b1)
  4199. (insert (if f1
  4200. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  4201. (format "[%d/%d]" c-on (+ c-on c-off))))))
  4202. (when (interactive-p)
  4203. (message "Checkbox satistics updated %s (%d places)"
  4204. (if all "in entire file" "in current outline entry") cstat)))))
  4205. (defun org-get-checkbox-statistics-face ()
  4206. "Select the face for checkbox statistics.
  4207. The face will be `org-done' when all relevant boxes are checked. Otherwise
  4208. it will be `org-todo'."
  4209. (if (match-end 1)
  4210. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  4211. (if (and (> (match-end 2) (match-beginning 2))
  4212. (equal (match-string 2) (match-string 3)))
  4213. 'org-done
  4214. 'org-todo)))
  4215. (defun org-get-indentation (&optional line)
  4216. "Get the indentation of the current line, interpreting tabs.
  4217. When LINE is given, assume it represents a line and compute its indentation."
  4218. (if line
  4219. (if (string-match "^ *" (org-remove-tabs line))
  4220. (match-end 0))
  4221. (save-excursion
  4222. (beginning-of-line 1)
  4223. (skip-chars-forward " \t")
  4224. (current-column))))
  4225. (defun org-remove-tabs (s &optional width)
  4226. "Replace tabulators in S with spaces.
  4227. Assumes that s is a single line, starting in column 0."
  4228. (setq width (or width tab-width))
  4229. (while (string-match "\t" s)
  4230. (setq s (replace-match
  4231. (make-string
  4232. (- (* width (/ (+ (match-beginning 0) width) width))
  4233. (match-beginning 0)) ?\ )
  4234. t t s)))
  4235. s)
  4236. ;; FIXME: document properly.
  4237. (defun org-fix-indentation (line ind)
  4238. "If the current indenation is smaller than ind1, leave it alone.
  4239. If it is larger than ind, reduce it by ind."
  4240. (let* ((l (org-remove-tabs line))
  4241. (i (org-get-indentation l))
  4242. (i1 (car ind)) (i2 (cdr ind)))
  4243. (if (>= i i2) (setq l (substring line i2)))
  4244. (if (> i1 0)
  4245. (concat (make-string i1 ?\ ) l)
  4246. l)))
  4247. (defun org-beginning-of-item ()
  4248. "Go to the beginning of the current hand-formatted item.
  4249. If the cursor is not in an item, throw an error."
  4250. (interactive)
  4251. (let ((pos (point))
  4252. (limit (save-excursion (org-back-to-heading)
  4253. (beginning-of-line 2) (point)))
  4254. ind ind1)
  4255. (if (org-at-item-p)
  4256. (beginning-of-line 1)
  4257. (beginning-of-line 1)
  4258. (skip-chars-forward " \t")
  4259. (setq ind (current-column))
  4260. (if (catch 'exit
  4261. (while t
  4262. (beginning-of-line 0)
  4263. (if (< (point) limit) (throw 'exit nil))
  4264. (unless (looking-at "[ \t]*$")
  4265. (skip-chars-forward " \t")
  4266. (setq ind1 (current-column))
  4267. (if (< ind1 ind)
  4268. (throw 'exit (org-at-item-p))))))
  4269. nil
  4270. (goto-char pos)
  4271. (error "Not in an item")))))
  4272. (defun org-end-of-item ()
  4273. "Go to the end of the current hand-formatted item.
  4274. If the cursor is not in an item, throw an error."
  4275. (interactive)
  4276. (let ((pos (point))
  4277. (limit (save-excursion (outline-next-heading) (point)))
  4278. (ind (save-excursion
  4279. (org-beginning-of-item)
  4280. (skip-chars-forward " \t")
  4281. (current-column)))
  4282. ind1)
  4283. (if (catch 'exit
  4284. (while t
  4285. (beginning-of-line 2)
  4286. (if (>= (point) limit) (throw 'exit t))
  4287. (unless (looking-at "[ \t]*$")
  4288. (skip-chars-forward " \t")
  4289. (setq ind1 (current-column))
  4290. (if (<= ind1 ind) (throw 'exit t)))))
  4291. (beginning-of-line 1)
  4292. (goto-char pos)
  4293. (error "Not in an item"))))
  4294. (defun org-next-item ()
  4295. "Move to the beginning of the next item in the current plain list.
  4296. Error if not at a plain list, or if this is the last item in the list."
  4297. (interactive)
  4298. (let (ind ind1 (pos (point)))
  4299. (org-beginning-of-item)
  4300. (setq ind (org-get-indentation))
  4301. (org-end-of-item)
  4302. (setq ind1 (org-get-indentation))
  4303. (unless (and (org-at-item-p) (= ind ind1))
  4304. (goto-char pos)
  4305. (error "On last item"))))
  4306. (defun org-previous-item ()
  4307. "Move to the beginning of the previous item in the current plain list.
  4308. Error if not at a plain list, or if this is the last item in the list."
  4309. (interactive)
  4310. (let (beg ind (pos (point)))
  4311. (org-beginning-of-item)
  4312. (setq beg (point))
  4313. (setq ind (org-get-indentation))
  4314. (goto-char beg)
  4315. (catch 'exit
  4316. (while t
  4317. (beginning-of-line 0)
  4318. (if (looking-at "[ \t]*$")
  4319. nil
  4320. (if (<= (org-get-indentation) ind)
  4321. (throw 'exit t)))))
  4322. (condition-case nil
  4323. (org-beginning-of-item)
  4324. (error (goto-char pos)
  4325. (error "On first item")))))
  4326. (defun org-move-item-down ()
  4327. "Move the plain list item at point down, i.e. swap with following item.
  4328. Subitems (items with larger indentation) are considered part of the item,
  4329. so this really moves item trees."
  4330. (interactive)
  4331. (let (beg end ind ind1 (pos (point)) txt)
  4332. (org-beginning-of-item)
  4333. (setq beg (point))
  4334. (setq ind (org-get-indentation))
  4335. (org-end-of-item)
  4336. (setq end (point))
  4337. (setq ind1 (org-get-indentation))
  4338. (if (and (org-at-item-p) (= ind ind1))
  4339. (progn
  4340. (org-end-of-item)
  4341. (setq txt (buffer-substring beg end))
  4342. (save-excursion
  4343. (delete-region beg end))
  4344. (setq pos (point))
  4345. (insert txt)
  4346. (goto-char pos)
  4347. (org-maybe-renumber-ordered-list))
  4348. (goto-char pos)
  4349. (error "Cannot move this item further down"))))
  4350. (defun org-move-item-up (arg)
  4351. "Move the plain list item at point up, i.e. swap with previous item.
  4352. Subitems (items with larger indentation) are considered part of the item,
  4353. so this really moves item trees."
  4354. (interactive "p")
  4355. (let (beg end ind ind1 (pos (point)) txt)
  4356. (org-beginning-of-item)
  4357. (setq beg (point))
  4358. (setq ind (org-get-indentation))
  4359. (org-end-of-item)
  4360. (setq end (point))
  4361. (goto-char beg)
  4362. (catch 'exit
  4363. (while t
  4364. (beginning-of-line 0)
  4365. (if (looking-at "[ \t]*$")
  4366. nil
  4367. (if (<= (setq ind1 (org-get-indentation)) ind)
  4368. (throw 'exit t)))))
  4369. (condition-case nil
  4370. (org-beginning-of-item)
  4371. (error (goto-char beg)
  4372. (error "Cannot move this item further up")))
  4373. (setq ind1 (org-get-indentation))
  4374. (if (and (org-at-item-p) (= ind ind1))
  4375. (progn
  4376. (setq txt (buffer-substring beg end))
  4377. (save-excursion
  4378. (delete-region beg end))
  4379. (setq pos (point))
  4380. (insert txt)
  4381. (goto-char pos)
  4382. (org-maybe-renumber-ordered-list))
  4383. (goto-char pos)
  4384. (error "Cannot move this item further up"))))
  4385. (defun org-maybe-renumber-ordered-list ()
  4386. "Renumber the ordered list at point if setup allows it.
  4387. This tests the user option `org-auto-renumber-ordered-lists' before
  4388. doing the renumbering."
  4389. (and org-auto-renumber-ordered-lists
  4390. (org-at-item-p)
  4391. (match-beginning 3)
  4392. (org-renumber-ordered-list 1)))
  4393. (defun org-get-string-indentation (s)
  4394. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  4395. (let ((n -1) (i 0) (w tab-width) c)
  4396. (catch 'exit
  4397. (while (< (setq n (1+ n)) (length s))
  4398. (setq c (aref s n))
  4399. (cond ((= c ?\ ) (setq i (1+ i)))
  4400. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  4401. (t (throw 'exit t)))))
  4402. i))
  4403. (defun org-renumber-ordered-list (arg)
  4404. "Renumber an ordered plain list.
  4405. Cursor needs to be in the first line of an item, the line that starts
  4406. with something like \"1.\" or \"2)\"."
  4407. (interactive "p")
  4408. (unless (and (org-at-item-p)
  4409. (match-beginning 3))
  4410. (error "This is not an ordered list"))
  4411. (let ((line (org-current-line))
  4412. (col (current-column))
  4413. (ind (org-get-string-indentation
  4414. (buffer-substring (point-at-bol) (match-beginning 3))))
  4415. ;; (term (substring (match-string 3) -1))
  4416. ind1 (n (1- arg)))
  4417. ;; find where this list begins
  4418. (catch 'exit
  4419. (while t
  4420. (catch 'next
  4421. (beginning-of-line 0)
  4422. (if (looking-at "[ \t]*$") (throw 'next t))
  4423. (skip-chars-forward " \t") (setq ind1 (current-column))
  4424. (if (or (< ind1 ind)
  4425. (and (= ind1 ind)
  4426. (not (org-at-item-p))))
  4427. (throw 'exit t)))))
  4428. ;; Walk forward and replace these numbers
  4429. (catch 'exit
  4430. (while t
  4431. (catch 'next
  4432. (beginning-of-line 2)
  4433. (if (eobp) (throw 'exit nil))
  4434. (if (looking-at "[ \t]*$") (throw 'next nil))
  4435. (skip-chars-forward " \t") (setq ind1 (current-column))
  4436. (if (> ind1 ind) (throw 'next t))
  4437. (if (< ind1 ind) (throw 'exit t))
  4438. (if (not (org-at-item-p)) (throw 'exit nil))
  4439. (if (not (match-beginning 3))
  4440. (error "unordered bullet in ordered list. Press \\[undo] to recover"))
  4441. (delete-region (match-beginning 3) (1- (match-end 3)))
  4442. (goto-char (match-beginning 3))
  4443. (insert (format "%d" (setq n (1+ n)))))))
  4444. (goto-line line)
  4445. (move-to-column col)))
  4446. (defvar org-last-indent-begin-marker (make-marker))
  4447. (defvar org-last-indent-end-marker (make-marker))
  4448. (defun org-outdent-item (arg)
  4449. "Outdent a local list item."
  4450. (interactive "p")
  4451. (org-indent-item (- arg)))
  4452. (defun org-indent-item (arg)
  4453. "Indent a local list item."
  4454. (interactive "p")
  4455. (unless (org-at-item-p)
  4456. (error "Not on an item"))
  4457. (save-excursion
  4458. (let (beg end ind ind1)
  4459. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  4460. (setq beg org-last-indent-begin-marker
  4461. end org-last-indent-end-marker)
  4462. (org-beginning-of-item)
  4463. (setq beg (move-marker org-last-indent-begin-marker (point)))
  4464. (org-end-of-item)
  4465. (setq end (move-marker org-last-indent-end-marker (point))))
  4466. (goto-char beg)
  4467. (skip-chars-forward " \t") (setq ind (current-column))
  4468. (if (< (+ arg ind) 0) (error "Cannot outdent beyond margin"))
  4469. (while (< (point) end)
  4470. (beginning-of-line 1)
  4471. (skip-chars-forward " \t") (setq ind1 (current-column))
  4472. (delete-region (point-at-bol) (point))
  4473. (indent-to-column (+ ind1 arg))
  4474. (beginning-of-line 2)))))
  4475. ;;; Archiving
  4476. (defun org-archive-subtree (&optional find-done)
  4477. "Move the current subtree to the archive.
  4478. The archive can be a certain top-level heading in the current file, or in
  4479. a different file. The tree will be moved to that location, the subtree
  4480. heading be marked DONE, and the current time will be added.
  4481. When called with prefix argument FIND-DONE, find whole trees without any
  4482. open TODO items and archive them (after getting confirmation from the user).
  4483. If the cursor is not at a headline when this comand is called, try all level
  4484. 1 trees. If the cursor is on a headline, only try the direct children of
  4485. this heading. "
  4486. (interactive "P")
  4487. (if find-done
  4488. (org-archive-all-done)
  4489. ;; Save all relevant TODO keyword-relatex variables
  4490. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  4491. (tr-org-todo-keywords org-todo-keywords)
  4492. (tr-org-todo-interpretation org-todo-interpretation)
  4493. (tr-org-done-string org-done-string)
  4494. (tr-org-todo-regexp org-todo-regexp)
  4495. (tr-org-todo-line-regexp org-todo-line-regexp)
  4496. (this-buffer (current-buffer))
  4497. file heading buffer level newfile-p)
  4498. (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
  4499. (progn
  4500. (setq file (format (match-string 1 org-archive-location)
  4501. (file-name-nondirectory buffer-file-name))
  4502. heading (match-string 2 org-archive-location)))
  4503. (error "Invalid `org-archive-location'"))
  4504. (if (> (length file) 0)
  4505. (setq newfile-p (not (file-exists-p file))
  4506. buffer (find-file-noselect file))
  4507. (setq buffer (current-buffer)))
  4508. (unless buffer
  4509. (error "Cannot access file \"%s\"" file))
  4510. (if (and (> (length heading) 0)
  4511. (string-match "^\\*+" heading))
  4512. (setq level (match-end 0))
  4513. (setq heading nil level 0))
  4514. (save-excursion
  4515. ;; We first only copy, in case something goes wrong
  4516. ;; we need to protect this-command, to avoid kill-region sets it,
  4517. ;; which would lead to duplication of subtrees
  4518. (let (this-command) (org-copy-subtree))
  4519. (set-buffer buffer)
  4520. ;; Enforce org-mode for the archive buffer
  4521. (if (not (org-mode-p))
  4522. ;; Force the mode for future visits.
  4523. (let ((org-insert-mode-line-in-empty-file t))
  4524. (call-interactively 'org-mode)))
  4525. (when newfile-p
  4526. (goto-char (point-max))
  4527. (insert (format "\nArchived entries from file %s\n\n"
  4528. (buffer-file-name this-buffer))))
  4529. ;; Force the TODO keywords of the original buffer
  4530. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  4531. (org-todo-keywords tr-org-todo-keywords)
  4532. (org-todo-interpretation tr-org-todo-interpretation)
  4533. (org-done-string tr-org-done-string)
  4534. (org-todo-regexp tr-org-todo-regexp)
  4535. (org-todo-line-regexp tr-org-todo-line-regexp))
  4536. (goto-char (point-min))
  4537. (if heading
  4538. (progn
  4539. (if (re-search-forward
  4540. (concat "\\(^\\|\r\\)"
  4541. (regexp-quote heading) "[ \t]*\\($\\|\r\\)")
  4542. nil t)
  4543. (goto-char (match-end 0))
  4544. ;; Heading not found, just insert it at the end
  4545. (goto-char (point-max))
  4546. (or (bolp) (insert "\n"))
  4547. (insert "\n" heading "\n")
  4548. (end-of-line 0))
  4549. ;; Make the subtree visible
  4550. (show-subtree)
  4551. (org-end-of-subtree t)
  4552. (skip-chars-backward " \t\r\n")
  4553. (and (looking-at "[ \t\r\n]*")
  4554. (replace-match "\n\n")))
  4555. ;; No specific heading, just go to end of file.
  4556. (goto-char (point-max)) (insert "\n"))
  4557. ;; Paste
  4558. (org-paste-subtree (1+ level))
  4559. ;; Mark the entry as done, i.e. set to last work in org-todo-keywords
  4560. (if org-archive-mark-done
  4561. (let (org-log-done)
  4562. (org-todo (length org-todo-keywords))))
  4563. ;; Move cursor to right after the TODO keyword
  4564. (when org-archive-stamp-time
  4565. (beginning-of-line 1)
  4566. (looking-at org-todo-line-regexp)
  4567. (goto-char (or (match-end 2) (match-beginning 3)))
  4568. (org-insert-time-stamp (org-current-time) t t "(" ")"))
  4569. ;; Save the buffer, if it is not the same buffer.
  4570. (if (not (eq this-buffer buffer)) (save-buffer))))
  4571. ;; Here we are back in the original buffer. Everything seems to have
  4572. ;; worked. So now cut the tree and finish up.
  4573. (let (this-command) (org-cut-subtree))
  4574. (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
  4575. (message "Subtree archived %s"
  4576. (if (eq this-buffer buffer)
  4577. (concat "under heading: " heading)
  4578. (concat "in file: " (abbreviate-file-name file)))))))
  4579. (defun org-archive-all-done (&optional tag)
  4580. "Archive sublevels of the current tree without open TODO items.
  4581. If the cursor is not on a headline, try all level 1 trees. If
  4582. it is on a headline, try all direct children.
  4583. When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
  4584. (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
  4585. (rea (concat ".*:" org-archive-tag ":"))
  4586. (begm (make-marker))
  4587. (endm (make-marker))
  4588. (question (if tag "Set ARCHIVE tag (no open TODO items)? "
  4589. "Move subtree to archive (no open TODO items)? "))
  4590. beg end (cntarch 0))
  4591. (if (org-on-heading-p)
  4592. (progn
  4593. (setq re1 (concat "^" (regexp-quote
  4594. (make-string
  4595. (1+ (- (match-end 0) (match-beginning 0)))
  4596. ?*))
  4597. " "))
  4598. (move-marker begm (point))
  4599. (move-marker endm (org-end-of-subtree t)))
  4600. (setq re1 "^* ")
  4601. (move-marker begm (point-min))
  4602. (move-marker endm (point-max)))
  4603. (save-excursion
  4604. (goto-char begm)
  4605. (while (re-search-forward re1 endm t)
  4606. (setq beg (match-beginning 0)
  4607. end (save-excursion (org-end-of-subtree t) (point)))
  4608. (goto-char beg)
  4609. (if (re-search-forward re end t)
  4610. (goto-char end)
  4611. (goto-char beg)
  4612. (if (and (or (not tag) (not (looking-at rea)))
  4613. (y-or-n-p question))
  4614. (progn
  4615. (if tag
  4616. (org-toggle-tag org-archive-tag 'on)
  4617. (org-archive-subtree))
  4618. (setq cntarch (1+ cntarch)))
  4619. (goto-char end)))))
  4620. (message "%d trees archived" cntarch)))
  4621. (defun org-cycle-hide-archived-subtrees (state)
  4622. "Re-hide all archived subtrees after a visibility state change."
  4623. (when (and (not org-cycle-open-archived-trees)
  4624. (not (memq state '(overview folded))))
  4625. (save-excursion
  4626. (let* ((globalp (memq state '(contents all)))
  4627. (beg (if globalp (point-min) (point)))
  4628. (end (if globalp (point-max) (org-end-of-subtree t))))
  4629. (org-hide-archived-subtrees beg end)
  4630. (goto-char beg)
  4631. (if (looking-at (concat ".*:" org-archive-tag ":"))
  4632. (message (substitute-command-keys
  4633. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  4634. (defun org-force-cycle-archived ()
  4635. "Cycle subtree even if it is archived."
  4636. (interactive)
  4637. (setq this-command 'org-cycle)
  4638. (let ((org-cycle-open-archived-trees t))
  4639. (call-interactively 'org-cycle)))
  4640. (defun org-hide-archived-subtrees (beg end)
  4641. "Re-hide all archived subtrees after a visibility state change."
  4642. (save-excursion
  4643. (let* ((re (concat ":" org-archive-tag ":")))
  4644. (goto-char beg)
  4645. (while (re-search-forward re end t)
  4646. (and (org-on-heading-p) (hide-subtree))
  4647. (org-end-of-subtree t)))))
  4648. (defun org-toggle-tag (tag &optional onoff)
  4649. "Toggle the tag TAG for the current line.
  4650. If ONOFF is `on' or `off', don't toggle but set to this state."
  4651. (unless (org-on-heading-p) (error "Not on headling"))
  4652. (let (res current)
  4653. (save-excursion
  4654. (beginning-of-line)
  4655. (if (re-search-forward "[ \t]:\\([a-zA-Z0-9_@:]+\\):[ \t]*$"
  4656. (point-at-eol) t)
  4657. (progn
  4658. (setq current (match-string 1))
  4659. (replace-match ""))
  4660. (setq current ""))
  4661. (setq current (nreverse (org-split-string current ":")))
  4662. (cond
  4663. ((eq onoff 'on)
  4664. (setq res t)
  4665. (or (member tag current) (push tag current)))
  4666. ((eq onoff 'off)
  4667. (or (not (member tag current)) (setq current (delete tag current))))
  4668. (t (if (member tag current)
  4669. (setq current (delete tag current))
  4670. (setq res t)
  4671. (push tag current))))
  4672. (end-of-line 1)
  4673. (when current
  4674. (insert " :" (mapconcat 'identity (nreverse current) ":") ":"))
  4675. (org-set-tags nil t))
  4676. res))
  4677. (defun org-toggle-archive-tag (&optional arg)
  4678. "Toggle the archive tag for the current headline.
  4679. With prefix ARG, check all children of current headline and offer tagging
  4680. the children that do not contain any open TODO items."
  4681. (interactive "P")
  4682. (if arg
  4683. (org-archive-all-done 'tag)
  4684. (let (set)
  4685. (save-excursion
  4686. (org-back-to-heading t)
  4687. (setq set (org-toggle-tag org-archive-tag))
  4688. (when set (hide-subtree)))
  4689. (and set (beginning-of-line 1))
  4690. (message "Subtree %s" (if set "archived" "unarchived")))))
  4691. (defvar org-agenda-multi nil) ; dynammically scoped
  4692. (defvar org-agenda-buffer-name "*Org Agenda*")
  4693. (defvar org-pre-agenda-window-conf nil)
  4694. (defun org-prepare-agenda ()
  4695. (if org-agenda-multi
  4696. (progn
  4697. (setq buffer-read-only nil)
  4698. (goto-char (point-max))
  4699. (unless (= (point) 1)
  4700. (insert "\n" (make-string (window-width) ?=) "\n"))
  4701. (narrow-to-region (point) (point-max)))
  4702. (org-agenda-maybe-reset-markers 'force)
  4703. (org-prepare-agenda-buffers (org-agenda-files))
  4704. (let* ((abuf (get-buffer-create org-agenda-buffer-name))
  4705. (awin (get-buffer-window abuf)))
  4706. (cond
  4707. ((equal (current-buffer) abuf) nil)
  4708. (awin (select-window awin))
  4709. ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
  4710. ((equal org-agenda-window-setup 'current-window)
  4711. (switch-to-buffer abuf))
  4712. ((equal org-agenda-window-setup 'other-window)
  4713. (switch-to-buffer-other-window abuf))
  4714. ((equal org-agenda-window-setup 'other-frame)
  4715. (switch-to-buffer-other-frame abuf))
  4716. ((equal org-agenda-window-setup 'reorganize-frame)
  4717. (delete-other-windows)
  4718. (switch-to-buffer-other-window abuf))))
  4719. (setq buffer-read-only nil)
  4720. (erase-buffer)
  4721. (org-agenda-mode))
  4722. (setq buffer-read-only nil))
  4723. (defun org-finalize-agenda ()
  4724. "Finishing touch for the agenda buffer, called just before displaying it."
  4725. (unless org-agenda-multi
  4726. (org-agenda-align-tags)
  4727. (save-excursion
  4728. (let ((buffer-read-only))
  4729. (goto-char (point-min))
  4730. (while (org-activate-bracket-links (point-max))
  4731. (add-text-properties (match-beginning 0) (match-end 0)
  4732. '(face org-link))))
  4733. (run-hooks 'org-finalize-agenda-hook))))
  4734. (defun org-prepare-agenda-buffers (files)
  4735. "Create buffers for all agenda files, protect archived trees and comments."
  4736. (interactive)
  4737. (let ((pa '(:org-archived t))
  4738. (pc '(:org-comment t))
  4739. (pall '(:org-archived t :org-comment t))
  4740. (rea (concat ":" org-archive-tag ":"))
  4741. bmp file re)
  4742. (save-excursion
  4743. (while (setq file (pop files))
  4744. (org-check-agenda-file file)
  4745. (set-buffer (org-get-agenda-file-buffer file))
  4746. (widen)
  4747. (setq bmp (buffer-modified-p))
  4748. (save-excursion
  4749. (remove-text-properties (point-min) (point-max) pall)
  4750. (when org-agenda-skip-archived-trees
  4751. (goto-char (point-min))
  4752. (while (re-search-forward rea nil t)
  4753. (if (org-on-heading-p)
  4754. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  4755. (goto-char (point-min))
  4756. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  4757. (while (re-search-forward re nil t)
  4758. (add-text-properties
  4759. (match-beginning 0) (org-end-of-subtree t) pc)))
  4760. (set-buffer-modified-p bmp)))))
  4761. (defun org-agenda-skip ()
  4762. "Throw to `:skip' in places that should be skipped."
  4763. (let ((p (point-at-bol)))
  4764. (and org-agenda-skip-archived-trees
  4765. (get-text-property p :org-archived)
  4766. (org-end-of-subtree t)
  4767. (throw :skip t))
  4768. (and (get-text-property p :org-comment)
  4769. (org-end-of-subtree t)
  4770. (throw :skip t))
  4771. (if (equal (char-after p) ?#) (throw :skip t))))
  4772. (defun org-agenda-toggle-archive-tag ()
  4773. "Toggle the archive tag for the current entry."
  4774. (interactive)
  4775. (org-agenda-check-no-diary)
  4776. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  4777. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  4778. (org-agenda-error)))
  4779. (buffer (marker-buffer hdmarker))
  4780. (pos (marker-position hdmarker))
  4781. (buffer-read-only nil)
  4782. newhead)
  4783. (with-current-buffer buffer
  4784. (widen)
  4785. (goto-char pos)
  4786. (org-show-context 'agenda)
  4787. (save-excursion
  4788. (and (outline-next-heading)
  4789. (org-flag-heading nil))) ; show the next heading
  4790. (call-interactively 'org-toggle-archive-tag)
  4791. (end-of-line 1)
  4792. (setq newhead (org-get-heading)))
  4793. (org-agenda-change-all-lines newhead hdmarker)
  4794. (beginning-of-line 1)))
  4795. ;;; Dynamic blocks
  4796. (defun org-find-dblock (name)
  4797. "Find the first dynamic block with name NAME in the buffer.
  4798. If not found, stay at current position and return nil."
  4799. (let (pos)
  4800. (save-excursion
  4801. (goto-char (point-min))
  4802. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  4803. nil t)
  4804. (match-beginning 0))))
  4805. (if pos (goto-char pos))
  4806. pos))
  4807. (defconst org-dblock-start-re
  4808. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  4809. "Matches the startline of a dynamic block, with parameters.")
  4810. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  4811. "Matches the end of a dyhamic block.")
  4812. (defun org-create-dblock (plist)
  4813. "Create a dynamic block section, with parameters taken from PLIST.
  4814. PLIST must containe a :name entry which is used as name of the block."
  4815. (unless (bolp) (newline))
  4816. (let ((name (plist-get plist :name)))
  4817. (insert "#+BEGIN: " name)
  4818. (while plist
  4819. (if (eq (car plist) :name)
  4820. (setq plist (cddr plist))
  4821. (insert " " (prin1-to-string (pop plist)))))
  4822. (insert "\n\n#+END:\n")
  4823. (beginning-of-line -2)))
  4824. (defun org-prepare-dblock ()
  4825. "Prepare dynamic block for refresh.
  4826. This empties the block, puts the cursor at the insert position and returns
  4827. the property list including an extra property :name with the block name."
  4828. (unless (looking-at org-dblock-start-re)
  4829. (error "Not at a dynamic block"))
  4830. (let* ((begdel (1+ (match-end 0)))
  4831. (name (match-string 1))
  4832. (params (append (list :name name)
  4833. (read (concat "(" (match-string 3) ")")))))
  4834. (unless (re-search-forward org-dblock-end-re nil t)
  4835. (error "Dynamic block not terminated"))
  4836. (delete-region begdel (match-beginning 0))
  4837. (goto-char begdel)
  4838. (open-line 1)
  4839. params))
  4840. (defun org-map-dblocks (&optional command)
  4841. "Apply COMMAND to all dynamic blocks in the current buffer.
  4842. If COMMAND is not given, use `org-update-dblock'."
  4843. (let ((cmd (or command 'org-update-dblock))
  4844. pos)
  4845. (save-excursion
  4846. (goto-char (point-min))
  4847. (while (re-search-forward org-dblock-start-re nil t)
  4848. (goto-char (setq pos (match-beginning 0)))
  4849. (condition-case nil
  4850. (funcall cmd)
  4851. (error (message "Error during update of dynamic block")))
  4852. (goto-char pos)
  4853. (unless (re-search-forward org-dblock-end-re nil t)
  4854. (error "Dynamic block not terminated"))))))
  4855. (defun org-dblock-update (&optional arg)
  4856. "User command for updating dynamic blocks.
  4857. Update the dynamic block at point. With prefix ARG, update all dynamic
  4858. blocks in the buffer."
  4859. (interactive "P")
  4860. (if arg
  4861. (org-update-all-dblocks)
  4862. (or (looking-at org-dblock-start-re)
  4863. (org-beginning-of-dblock))
  4864. (org-update-dblock)))
  4865. (defun org-update-dblock ()
  4866. "Update the dynamic block at point
  4867. This means to empty the block, parse for parameters and then call
  4868. the correct writing function."
  4869. (let* ((pos (point))
  4870. (params (org-prepare-dblock))
  4871. (name (plist-get params :name))
  4872. (cmd (intern (concat "org-dblock-write:" name))))
  4873. (funcall cmd params)
  4874. (goto-char pos)))
  4875. (defun org-beginning-of-dblock ()
  4876. "Find the beginning of the dynamic block at point.
  4877. Error if there is no scuh block at point."
  4878. (let ((pos (point))
  4879. beg)
  4880. (end-of-line 1)
  4881. (if (and (re-search-backward org-dblock-start-re nil t)
  4882. (setq beg (match-beginning 0))
  4883. (re-search-forward org-dblock-end-re nil t)
  4884. (> (match-end 0) pos))
  4885. (goto-char beg)
  4886. (goto-char pos)
  4887. (error "Not in a dynamic block"))))
  4888. (defun org-update-all-dblocks ()
  4889. "Update all dynamic blocks in the buffer.
  4890. This function can be used in a hook."
  4891. (when (org-mode-p)
  4892. (org-map-dblocks 'org-update-dblock)))
  4893. ;;; Completion
  4894. (defun org-complete (&optional arg)
  4895. "Perform completion on word at point.
  4896. At the beginning of a headline, this completes TODO keywords as given in
  4897. `org-todo-keywords'.
  4898. If the current word is preceded by a backslash, completes the TeX symbols
  4899. that are supported for HTML support.
  4900. If the current word is preceded by \"#+\", completes special words for
  4901. setting file options.
  4902. In the line after \"#+STARTUP:, complete valid keywords.\"
  4903. At all other locations, this simply calls `ispell-complete-word'."
  4904. (interactive "P")
  4905. (catch 'exit
  4906. (let* ((end (point))
  4907. (beg1 (save-excursion
  4908. (skip-chars-backward "a-zA-Z_@0-9")
  4909. (point)))
  4910. (beg (save-excursion
  4911. (skip-chars-backward "a-zA-Z0-9_:$")
  4912. (point)))
  4913. (confirm (lambda (x) (stringp (car x))))
  4914. (camel (equal (char-before beg) ?*))
  4915. (tag (equal (char-before beg1) ?:))
  4916. (texp (equal (char-before beg) ?\\))
  4917. (link (equal (char-before beg) ?\[))
  4918. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  4919. beg)
  4920. "#+"))
  4921. (startup (string-match "^#\\+STARTUP:.*"
  4922. (buffer-substring (point-at-bol) (point))))
  4923. (completion-ignore-case opt)
  4924. (type nil)
  4925. (tbl nil)
  4926. (table (cond
  4927. (opt
  4928. (setq type :opt)
  4929. (mapcar (lambda (x)
  4930. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  4931. (cons (match-string 2 x) (match-string 1 x)))
  4932. (org-split-string (org-get-current-options) "\n")))
  4933. (startup
  4934. (setq type :startup)
  4935. org-startup-options)
  4936. (link (append org-link-abbrev-alist-local
  4937. org-link-abbrev-alist))
  4938. (texp
  4939. (setq type :tex)
  4940. org-html-entities)
  4941. ((string-match "\\`\\*+[ \t]*\\'"
  4942. (buffer-substring (point-at-bol) beg))
  4943. (setq type :todo)
  4944. (mapcar 'list org-todo-keywords))
  4945. (camel
  4946. (setq type :camel)
  4947. (save-excursion
  4948. (goto-char (point-min))
  4949. (while (re-search-forward org-todo-line-regexp nil t)
  4950. (push (list
  4951. (if org-file-link-context-use-camel-case
  4952. (org-make-org-heading-camel (match-string 3) t)
  4953. (org-make-org-heading-search-string
  4954. (match-string 3) t)))
  4955. tbl)))
  4956. tbl)
  4957. (tag (setq type :tag beg beg1)
  4958. (or org-tag-alist (org-get-buffer-tags)))
  4959. (t (progn (ispell-complete-word arg) (throw 'exit nil)))))
  4960. (pattern (buffer-substring-no-properties beg end))
  4961. (completion (try-completion pattern table confirm)))
  4962. (cond ((eq completion t)
  4963. (if (equal type :opt)
  4964. (insert (substring (cdr (assoc (upcase pattern) table))
  4965. (length pattern)))
  4966. (if (equal type :tag) (insert ":"))))
  4967. ((null completion)
  4968. (message "Can't find completion for \"%s\"" pattern)
  4969. (ding))
  4970. ((not (string= pattern completion))
  4971. (delete-region beg end)
  4972. (if (string-match " +$" completion)
  4973. (setq completion (replace-match "" t t completion)))
  4974. (insert completion)
  4975. (if (get-buffer-window "*Completions*")
  4976. (delete-window (get-buffer-window "*Completions*")))
  4977. (if (assoc completion table)
  4978. (if (eq type :todo) (insert " ")
  4979. (if (eq type :tag) (insert ":"))))
  4980. (if (and (equal type :opt) (assoc completion table))
  4981. (message "%s" (substitute-command-keys
  4982. "Press \\[org-complete] again to insert example settings"))))
  4983. (t
  4984. (message "Making completion list...")
  4985. (let ((list (sort (all-completions pattern table confirm)
  4986. 'string<)))
  4987. (with-output-to-temp-buffer "*Completions*"
  4988. (condition-case nil
  4989. ;; Protection needed for XEmacs and emacs 21
  4990. (display-completion-list list pattern)
  4991. (error (display-completion-list list)))))
  4992. (message "Making completion list...%s" "done"))))))
  4993. ;;; Comments, TODO and DEADLINE
  4994. (defun org-toggle-comment ()
  4995. "Change the COMMENT state of an entry."
  4996. (interactive)
  4997. (save-excursion
  4998. (org-back-to-heading)
  4999. (if (looking-at (concat outline-regexp
  5000. "\\( +\\<" org-comment-string "\\>\\)"))
  5001. (replace-match "" t t nil 1)
  5002. (if (looking-at outline-regexp)
  5003. (progn
  5004. (goto-char (match-end 0))
  5005. (insert " " org-comment-string))))))
  5006. (defvar org-last-todo-state-is-todo nil
  5007. "This is non-nil when the last TODO state change led to a TODO state.
  5008. If the last change removed the TODO tag or switched to DONE, then
  5009. this is nil.")
  5010. (defun org-todo (&optional arg)
  5011. "Change the TODO state of an item.
  5012. The state of an item is given by a keyword at the start of the heading,
  5013. like
  5014. *** TODO Write paper
  5015. *** DONE Call mom
  5016. The different keywords are specified in the variable `org-todo-keywords'.
  5017. By default the available states are \"TODO\" and \"DONE\".
  5018. So for this example: when the item starts with TODO, it is changed to DONE.
  5019. When it starts with DONE, the DONE is removed. And when neither TODO nor
  5020. DONE are present, add TODO at the beginning of the heading.
  5021. With prefix arg, use completion to determine the new state. With numeric
  5022. prefix arg, switch to that state."
  5023. (interactive "P")
  5024. (save-excursion
  5025. (org-back-to-heading)
  5026. (if (looking-at outline-regexp) (goto-char (match-end 0)))
  5027. (or (looking-at (concat " +" org-todo-regexp " *"))
  5028. (looking-at " *"))
  5029. (let* ((this (match-string 1))
  5030. (completion-ignore-case t)
  5031. (member (member this org-todo-keywords))
  5032. (tail (cdr member))
  5033. (state (cond
  5034. ((equal arg '(4))
  5035. ;; Read a state with completion
  5036. (completing-read "State: " (mapcar (lambda(x) (list x))
  5037. org-todo-keywords)
  5038. nil t))
  5039. ((eq arg 'right)
  5040. (if this
  5041. (if tail (car tail) nil)
  5042. (car org-todo-keywords)))
  5043. ((eq arg 'left)
  5044. (if (equal member org-todo-keywords)
  5045. nil
  5046. (if this
  5047. (nth (- (length org-todo-keywords) (length tail) 2)
  5048. org-todo-keywords)
  5049. org-done-string)))
  5050. (arg
  5051. ;; user requests a specific state
  5052. (nth (1- (prefix-numeric-value arg))
  5053. org-todo-keywords))
  5054. ((null member) (car org-todo-keywords))
  5055. ((null tail) nil) ;; -> first entry
  5056. ((eq org-todo-interpretation 'sequence)
  5057. (car tail))
  5058. ((memq org-todo-interpretation '(type priority))
  5059. (if (eq this-command last-command)
  5060. (car tail)
  5061. (if (> (length tail) 0) org-done-string nil)))
  5062. (t nil)))
  5063. (next (if state (concat " " state " ") " ")))
  5064. (replace-match next t t)
  5065. (setq org-last-todo-state-is-todo
  5066. (not (equal state org-done-string)))
  5067. (when org-log-done
  5068. (if (equal state org-done-string)
  5069. (org-add-planning-info 'closed (org-current-time) 'scheduled)
  5070. (if (not this)
  5071. (org-add-planning-info nil nil 'closed))))
  5072. ;; Fixup tag positioning
  5073. (and org-auto-align-tags (org-set-tags nil t))
  5074. (run-hooks 'org-after-todo-state-change-hook)))
  5075. ;; Fixup cursor location if close to the keyword
  5076. (if (and (outline-on-heading-p)
  5077. (not (bolp))
  5078. (save-excursion (beginning-of-line 1)
  5079. (looking-at org-todo-line-regexp))
  5080. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  5081. (progn
  5082. (goto-char (or (match-end 2) (match-end 1)))
  5083. (just-one-space))))
  5084. (defun org-show-todo-tree (arg)
  5085. "Make a compact tree which shows all headlines marked with TODO.
  5086. The tree will show the lines where the regexp matches, and all higher
  5087. headlines above the match.
  5088. With \\[universal-argument] prefix, also show the DONE entries.
  5089. With a numeric prefix N, construct a sparse tree for the Nth element
  5090. of `org-todo-keywords'."
  5091. (interactive "P")
  5092. (let ((case-fold-search nil)
  5093. (kwd-re
  5094. (cond ((null arg) org-not-done-regexp)
  5095. ((equal arg '(4)) org-todo-regexp)
  5096. ((<= (prefix-numeric-value arg) (length org-todo-keywords))
  5097. (regexp-quote (nth (1- (prefix-numeric-value arg))
  5098. org-todo-keywords)))
  5099. (t (error "Invalid prefix argument: %s" arg)))))
  5100. (message "%d TODO entries found"
  5101. (org-occur (concat "^" outline-regexp " +" kwd-re )))))
  5102. (defun org-deadline ()
  5103. "Insert the DEADLINE: string to make a deadline.
  5104. A timestamp is also inserted - use \\[org-timestamp-up] and \\[org-timestamp-down]
  5105. to modify it to the correct date."
  5106. (interactive)
  5107. (org-add-planning-info 'deadline nil 'closed))
  5108. (defun org-schedule ()
  5109. "Insert the SCHEDULED: string to schedule a TODO item.
  5110. A timestamp is also inserted - use \\[org-timestamp-up] and \\[org-timestamp-down]
  5111. to modify it to the correct date."
  5112. (interactive)
  5113. (org-add-planning-info 'scheduled nil 'closed))
  5114. (defun org-add-planning-info (what &optional time &rest remove)
  5115. "Insert new timestamp with keyword in the line directly after the headline.
  5116. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  5117. If non is given, the user is prompted for a date.
  5118. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  5119. be removed."
  5120. (interactive)
  5121. (when what (setq time (or time (org-read-date nil 'to-time))))
  5122. (when (and org-insert-labeled-timestamps-at-point
  5123. (member what '(scheduled deadline)))
  5124. (insert
  5125. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  5126. (org-insert-time-stamp time)
  5127. (setq what nil))
  5128. (save-excursion
  5129. (save-restriction
  5130. (let (col list elt ts buffer-invisibility-spec)
  5131. (org-back-to-heading t)
  5132. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  5133. (goto-char (match-end 1))
  5134. (setq col (current-column))
  5135. (goto-char (1+ (match-end 0)))
  5136. (if (and (not (looking-at outline-regexp))
  5137. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  5138. "[^\r\n]*"))
  5139. (not (equal (match-string 1) org-clock-string)))
  5140. (narrow-to-region (match-beginning 0) (match-end 0))
  5141. (insert "\n")
  5142. (backward-char 1)
  5143. (narrow-to-region (point) (point))
  5144. (indent-to-column col))
  5145. ;; Check if we have to remove something.
  5146. (setq list (cons what remove))
  5147. (while list
  5148. (setq elt (pop list))
  5149. (goto-char (point-min))
  5150. (when (or (and (eq elt 'scheduled)
  5151. (re-search-forward org-scheduled-time-regexp nil t))
  5152. (and (eq elt 'deadline)
  5153. (re-search-forward org-deadline-time-regexp nil t))
  5154. (and (eq elt 'closed)
  5155. (re-search-forward org-closed-time-regexp nil t)))
  5156. (replace-match "")
  5157. (if (looking-at "--+<[^>]+>") (replace-match ""))
  5158. (if (looking-at " +") (replace-match ""))))
  5159. (goto-char (point-max))
  5160. (when what
  5161. (insert
  5162. (if (not (equal (char-before) ?\ )) " " "")
  5163. (cond ((eq what 'scheduled) org-scheduled-string)
  5164. ((eq what 'deadline) org-deadline-string)
  5165. ((eq what 'closed) org-closed-string))
  5166. " ")
  5167. (org-insert-time-stamp time nil (eq what 'closed))
  5168. (end-of-line 1)
  5169. (and (eq what 'closed) (org-add-log-maybe 'done)))
  5170. (goto-char (point-min))
  5171. (widen)
  5172. (if (looking-at "[ \t]+\r?\n")
  5173. (replace-match ""))
  5174. ts))))
  5175. (defvar org-log-note-marker (make-marker))
  5176. (defvar org-log-note-purpose nil)
  5177. (defvar org-log-note-window-configuration nil)
  5178. (defun org-add-log-maybe (&optional purpose)
  5179. (when (and (listp org-log-done)
  5180. (memq purpose org-log-done))
  5181. (move-marker org-log-note-marker (point))
  5182. (setq org-log-note-purpose purpose)
  5183. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  5184. (defun org-add-log-note (&optional purpose)
  5185. "Pop up a window for taking a note, and add this note later at point."
  5186. (remove-hook 'post-command-hook 'org-add-log-note)
  5187. (setq org-log-note-window-configuration (current-window-configuration))
  5188. (delete-other-windows)
  5189. (switch-to-buffer (marker-buffer org-log-note-marker))
  5190. (goto-char org-log-note-marker)
  5191. (switch-to-buffer-other-window "*Org Note*")
  5192. (erase-buffer)
  5193. (org-mode)
  5194. (insert (format "# Insert note for %s, finish with C-c C-c.\n\n"
  5195. (cond
  5196. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  5197. ((eq org-log-note-purpose 'done) "closed todo item")
  5198. (t (error "This should not happen")))))
  5199. (org-set-local 'org-finish-function 'org-store-log-note))
  5200. (defun org-store-log-note ()
  5201. "Finish taking a log note, and insert it to where it belongs."
  5202. (let ((txt (buffer-string))
  5203. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  5204. lines ind)
  5205. (kill-buffer (current-buffer))
  5206. (if (string-match "^#.*\n[ \t\\n]*" txt)
  5207. (setq txt (replace-match "" t t txt)))
  5208. (when (string-match "\\S-" txt)
  5209. (if (string-match "\\s-+\\'" txt)
  5210. (setq txt (replace-match "" t t txt)))
  5211. (setq lines (org-split-string txt "\n"))
  5212. (when (and note (string-match "\\S-" note))
  5213. (setq note
  5214. (org-replace-escapes
  5215. note
  5216. (list (cons "%u" user-login-name)
  5217. (cons "%U" user-full-name)
  5218. (cons "%t" (format-time-string
  5219. (org-time-stamp-format 'long 'inactive)
  5220. (current-time))))))
  5221. (push note lines))
  5222. (save-excursion
  5223. (set-buffer (marker-buffer org-log-note-marker))
  5224. (save-excursion
  5225. (goto-char org-log-note-marker)
  5226. (if (not (bolp)) (newline))
  5227. (indent-relative t)
  5228. (setq ind (concat (buffer-substring (point-at-bol) (point)) " "))
  5229. (insert " - " (pop lines))
  5230. (while lines
  5231. (insert "\n" ind (pop lines))))))
  5232. (set-window-configuration org-log-note-window-configuration)))
  5233. (defvar org-occur-highlights nil)
  5234. (make-variable-buffer-local 'org-occur-highlights)
  5235. (defun org-occur (regexp &optional keep-previous callback)
  5236. "Make a compact tree which shows all matches of REGEXP.
  5237. The tree will show the lines where the regexp matches, and all higher
  5238. headlines above the match. It will also show the heading after the match,
  5239. to make sure editing the matching entry is easy.
  5240. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  5241. call to `org-occur' will be kept, to allow stacking of calls to this
  5242. command.
  5243. If CALLBACK is non-nil, it is a function which is called to confirm
  5244. that the match should indeed be shown."
  5245. (interactive "sRegexp: \nP")
  5246. (or keep-previous (org-remove-occur-highlights nil nil t))
  5247. (let ((cnt 0))
  5248. (save-excursion
  5249. (goto-char (point-min))
  5250. (if (or (not keep-previous) ; do not want to keep
  5251. (not org-occur-highlights)) ; no previous matches
  5252. ;; hide everything
  5253. (org-overview))
  5254. (while (re-search-forward regexp nil t)
  5255. (when (or (not callback)
  5256. (save-match-data (funcall callback)))
  5257. (setq cnt (1+ cnt))
  5258. (org-highlight-new-match (match-beginning 0) (match-end 0))
  5259. (org-show-context 'occur-tree))))
  5260. (when org-remove-highlights-with-change
  5261. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  5262. nil 'local))
  5263. (unless org-sparse-tree-open-archived-trees
  5264. (org-hide-archived-subtrees (point-min) (point-max)))
  5265. (run-hooks 'org-occur-hook)
  5266. (if (interactive-p)
  5267. (message "%d match(es) for regexp %s" cnt regexp))
  5268. cnt))
  5269. (defun org-show-context (&optional key siblings)
  5270. "Make sure point and context and visible.
  5271. How much context is shown depends upon the variables
  5272. `org-show-hierarchy-above' and `org-show-following-heading'.
  5273. When SIBLINGS is non-nil, show all siblings on each hierarchy level."
  5274. (let ((heading-p (org-on-heading-p t))
  5275. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  5276. (following-p (org-get-alist-option org-show-following-heading key)))
  5277. (catch 'exit
  5278. ;; Show heading or entry text
  5279. (if heading-p
  5280. (org-flag-heading nil) ; only show the heading
  5281. (and (or (org-invisible-p) (org-invisible-p2))
  5282. (org-show-hidden-entry))) ; show entire entry
  5283. (when following-p
  5284. ;; Show next sibling, or heading below text
  5285. (save-excursion
  5286. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  5287. (org-flag-heading nil))))
  5288. (when hierarchy-p
  5289. ;; show all higher headings, possibly with siblings
  5290. (save-excursion
  5291. (while (and (condition-case nil
  5292. (progn (org-up-heading-all 1) t)
  5293. (error nil))
  5294. (not (bobp)))
  5295. (org-flag-heading nil)
  5296. (when siblings
  5297. (save-excursion
  5298. (while (org-goto-sibling) (org-flag-heading nil)))
  5299. (save-excursion
  5300. (while (org-goto-sibling 'previous)
  5301. (org-flag-heading nil))))))))))
  5302. (defun org-reveal (&optional siblings)
  5303. "Show current entry, hierarchy above it, and the following headline.
  5304. This can be used to show a consistent set of context around locations
  5305. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  5306. not t for the search context.
  5307. With optional argument SIBLINGS, on each level of the hierarchy all
  5308. siblings are shown. This repairs the tree structure so what it would
  5309. look like when opend with successive calls to `org-cycle'."
  5310. (interactive "P")
  5311. (let ((org-show-hierarchy-above t)
  5312. (org-show-following-heading t))
  5313. (org-show-context nil siblings)))
  5314. ;; Overlay compatibility functions
  5315. (defun org-make-overlay (beg end &optional buffer)
  5316. (if (featurep 'xemacs)
  5317. (make-extent beg end buffer)
  5318. (make-overlay beg end buffer)))
  5319. (defun org-delete-overlay (ovl)
  5320. (if (featurep 'xemacs) (delete-extent ovl) (delete-overlay ovl)))
  5321. (defun org-detach-overlay (ovl)
  5322. (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
  5323. (defun org-move-overlay (ovl beg end &optional buffer)
  5324. (if (featurep 'xemacs)
  5325. (set-extent-endpoints ovl beg end (or buffer (current-buffer)))
  5326. (move-overlay ovl beg end buffer)))
  5327. (defun org-overlay-put (ovl prop value)
  5328. (if (featurep 'xemacs)
  5329. (set-extent-property ovl prop value)
  5330. (overlay-put ovl prop value)))
  5331. (defun org-overlay-display (ovl text &optional face)
  5332. "Make overlay OVL display TEXT with face FACE."
  5333. (if (featurep 'xemacs)
  5334. (let ((gl (make-glyph text)))
  5335. (and face (set-glyph-face gl face))
  5336. (set-extent-property ovl 'invisible t)
  5337. (set-extent-property ovl 'end-glyph gl))
  5338. (overlay-put ovl 'display text)
  5339. (if face (overlay-put ovl 'face face))))
  5340. (defun org-overlay-get (ovl prop)
  5341. (if (featurep 'xemacs)
  5342. (extent-property ovl prop)
  5343. (overlay-get ovl prop)))
  5344. (defun org-overlays-at (pos)
  5345. (if (featurep 'xemacs) (extents-at pos) (overlays-at pos)))
  5346. (defun org-overlays-in (&optional start end)
  5347. (if (featurep 'xemacs)
  5348. (extent-list nil start end)
  5349. (overlays-in start end)))
  5350. (defun org-overlay-start (o)
  5351. (if (featurep 'xemacs) (extent-start-position o) (overlay-start o)))
  5352. (defun org-overlay-end (o)
  5353. (if (featurep 'xemacs) (extent-end-position o) (overlay-end o)))
  5354. (defun org-find-overlays (prop &optional pos delete)
  5355. "Find all overlays specifying PROP at POS or point.
  5356. If DELETE is non-nil, delete all those overlays."
  5357. (let ((overlays (org-overlays-at (or pos (point))))
  5358. ov found)
  5359. (while (setq ov (pop overlays))
  5360. (if (org-overlay-get ov prop)
  5361. (if delete (org-delete-overlay ov) (push ov found))))
  5362. found))
  5363. (defun org-highlight-new-match (beg end)
  5364. "Highlight from BEG to END and mark the highlight is an occur headline."
  5365. (let ((ov (org-make-overlay beg end)))
  5366. (org-overlay-put ov 'face 'secondary-selection)
  5367. (push ov org-occur-highlights)))
  5368. (defvar org-inhibit-highlight-removal nil)
  5369. (defun org-remove-occur-highlights (&optional beg end noremove)
  5370. "Remove the occur highlights from the buffer.
  5371. BEG and END are ignored. If NOREMOVE is nil, remove this function
  5372. from the `before-change-functions' in the current buffer."
  5373. (interactive)
  5374. (unless org-inhibit-highlight-removal
  5375. (mapc 'org-delete-overlay org-occur-highlights)
  5376. (setq org-occur-highlights nil)
  5377. (unless noremove
  5378. (remove-hook 'before-change-functions
  5379. 'org-remove-occur-highlights 'local))))
  5380. ;;; Priorities
  5381. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z]\\)\\] ?\\)"
  5382. "Regular expression matching the priority indicator.")
  5383. (defvar org-remove-priority-next-time nil)
  5384. (defun org-priority-up ()
  5385. "Increase the priority of the current item."
  5386. (interactive)
  5387. (org-priority 'up))
  5388. (defun org-priority-down ()
  5389. "Decrease the priority of the current item."
  5390. (interactive)
  5391. (org-priority 'down))
  5392. (defun org-priority (&optional action)
  5393. "Change the priority of an item by ARG.
  5394. ACTION can be set, up, or down."
  5395. (interactive)
  5396. (setq action (or action 'set))
  5397. (let (current new news have remove)
  5398. (save-excursion
  5399. (org-back-to-heading)
  5400. (if (looking-at org-priority-regexp)
  5401. (setq current (string-to-char (match-string 2))
  5402. have t)
  5403. (setq current org-default-priority))
  5404. (cond
  5405. ((eq action 'set)
  5406. (message "Priority A-%c, SPC to remove: " org-lowest-priority)
  5407. (setq new (read-char-exclusive))
  5408. (cond ((equal new ?\ ) (setq remove t))
  5409. ((or (< (upcase new) ?A) (> (upcase new) org-lowest-priority))
  5410. (error "Priority must be between `%c' and `%c'"
  5411. ?A org-lowest-priority))))
  5412. ((eq action 'up)
  5413. (setq new (1- current)))
  5414. ((eq action 'down)
  5415. (setq new (1+ current)))
  5416. (t (error "Invalid action")))
  5417. (setq new (min (max ?A (upcase new)) org-lowest-priority))
  5418. (setq news (format "%c" new))
  5419. (if have
  5420. (if remove
  5421. (replace-match "" t t nil 1)
  5422. (replace-match news t t nil 2))
  5423. (if remove
  5424. (error "No priority cookie found in line")
  5425. (looking-at org-todo-line-regexp)
  5426. (if (match-end 2)
  5427. (progn
  5428. (goto-char (match-end 2))
  5429. (insert " [#" news "]"))
  5430. (goto-char (match-beginning 3))
  5431. (insert "[#" news "] ")))))
  5432. (if remove
  5433. (message "Priority removed")
  5434. (message "Priority of current item set to %s" news))))
  5435. (defun org-get-priority (s)
  5436. "Find priority cookie and return priority."
  5437. (save-match-data
  5438. (if (not (string-match org-priority-regexp s))
  5439. (* 1000 (- org-lowest-priority org-default-priority))
  5440. (* 1000 (- org-lowest-priority
  5441. (string-to-char (match-string 2 s)))))))
  5442. ;;; Timestamps
  5443. (defvar org-last-changed-timestamp nil)
  5444. (defun org-time-stamp (arg)
  5445. "Prompt for a date/time and insert a time stamp.
  5446. If the user specifies a time like HH:MM, or if this command is called
  5447. with a prefix argument, the time stamp will contain date and time.
  5448. Otherwise, only the date will be included. All parts of a date not
  5449. specified by the user will be filled in from the current date/time.
  5450. So if you press just return without typing anything, the time stamp
  5451. will represent the current date/time. If there is already a timestamp
  5452. at the cursor, it will be modified."
  5453. (interactive "P")
  5454. (let (org-time-was-given time)
  5455. (cond
  5456. ((and (org-at-timestamp-p)
  5457. (eq last-command 'org-time-stamp)
  5458. (eq this-command 'org-time-stamp))
  5459. (insert "--")
  5460. (setq time (let ((this-command this-command))
  5461. (org-read-date arg 'totime)))
  5462. (org-insert-time-stamp time (or org-time-was-given arg)))
  5463. ((org-at-timestamp-p)
  5464. (setq time (let ((this-command this-command))
  5465. (org-read-date arg 'totime)))
  5466. (when (org-at-timestamp-p) ; just to get the match data
  5467. (replace-match "")
  5468. (setq org-last-changed-timestamp
  5469. (org-insert-time-stamp time (or org-time-was-given arg))))
  5470. (message "Timestamp updated"))
  5471. (t
  5472. (setq time (let ((this-command this-command))
  5473. (org-read-date arg 'totime)))
  5474. (org-insert-time-stamp time (or org-time-was-given arg))))))
  5475. (defun org-time-stamp-inactive (&optional arg)
  5476. "Insert an inactive time stamp.
  5477. An inactive time stamp is enclosed in square brackets instead of angle
  5478. brackets. It is inactive in the sense that it does not trigger agenda entries,
  5479. does not link to the calendar and cannot be changed with the S-cursor keys.
  5480. So these are more for recording a certain time/date."
  5481. (interactive "P")
  5482. (let (org-time-was-given time)
  5483. (setq time (org-read-date arg 'totime))
  5484. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive)))
  5485. (defvar org-date-ovl (org-make-overlay 1 1))
  5486. (org-overlay-put org-date-ovl 'face 'org-warning)
  5487. (org-detach-overlay org-date-ovl)
  5488. (defun org-read-date (&optional with-time to-time from-string)
  5489. "Read a date and make things smooth for the user.
  5490. The prompt will suggest to enter an ISO date, but you can also enter anything
  5491. which will at least partially be understood by `parse-time-string'.
  5492. Unrecognized parts of the date will default to the current day, month, year,
  5493. hour and minute. For example,
  5494. 3-2-5 --> 2003-02-05
  5495. feb 15 --> currentyear-02-15
  5496. sep 12 9 --> 2009-09-12
  5497. 12:45 --> today 12:45
  5498. 22 sept 0:34 --> currentyear-09-22 0:34
  5499. 12 --> currentyear-currentmonth-12
  5500. Fri --> nearest Friday (today or later)
  5501. +4 --> four days from today (only if +N is the only thing given)
  5502. etc.
  5503. The function understands only English month and weekday abbreviations,
  5504. but this can be configured with the variables `parse-time-months' and
  5505. `parse-time-weekdays'.
  5506. While prompting, a calendar is popped up - you can also select the
  5507. date with the mouse (button 1). The calendar shows a period of three
  5508. months. To scroll it to other months, use the keys `>' and `<'.
  5509. If you don't like the calendar, turn it off with
  5510. \(setq org-popup-calendar-for-date-prompt nil)
  5511. With optional argument TO-TIME, the date will immediately be converted
  5512. to an internal time.
  5513. With an optional argument WITH-TIME, the prompt will suggest to also
  5514. insert a time. Note that when WITH-TIME is not set, you can still
  5515. enter a time, and this function will inform the calling routine about
  5516. this change. The calling routine may then choose to change the format
  5517. used to insert the time stamp into the buffer to include the time."
  5518. (require 'parse-time)
  5519. (let* ((org-time-stamp-rounding-minutes
  5520. (if (equal with-time '(16)) 0 org-time-stamp-rounding-minutes))
  5521. (ct (org-current-time))
  5522. (default-time
  5523. ;; Default time is either today, or, when entering a range,
  5524. ;; the range start.
  5525. (if (save-excursion
  5526. (re-search-backward
  5527. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  5528. (- (point) 20) t))
  5529. (apply
  5530. 'encode-time
  5531. (mapcar (lambda(x) (or x 0))
  5532. (parse-time-string (match-string 1))))
  5533. ct))
  5534. (calendar-move-hook nil)
  5535. (view-diary-entries-initially nil)
  5536. (view-calendar-holidays-initially nil)
  5537. (timestr (format-time-string
  5538. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") default-time))
  5539. (prompt (format "YYYY-MM-DD [%s]: " timestr))
  5540. ans ans1 ans2 (deltadays 0)
  5541. second minute hour day month year tl wday wday1)
  5542. (cond
  5543. (from-string (setq ans from-string))
  5544. (org-popup-calendar-for-date-prompt
  5545. (save-excursion
  5546. (save-window-excursion
  5547. (calendar)
  5548. (calendar-forward-day (- (time-to-days default-time)
  5549. (calendar-absolute-from-gregorian
  5550. (calendar-current-date))))
  5551. (org-eval-in-calendar nil)
  5552. (let* ((old-map (current-local-map))
  5553. (map (copy-keymap calendar-mode-map))
  5554. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  5555. (define-key map (kbd "RET") 'org-calendar-select)
  5556. (define-key map (if (featurep 'xemacs) [button1] [mouse-1])
  5557. 'org-calendar-select-mouse)
  5558. (define-key map (if (featurep 'xemacs) [button2] [mouse-2])
  5559. 'org-calendar-select-mouse)
  5560. (define-key minibuffer-local-map [(meta shift left)]
  5561. (lambda () (interactive)
  5562. (org-eval-in-calendar '(calendar-backward-month 1))))
  5563. (define-key minibuffer-local-map [(meta shift right)]
  5564. (lambda () (interactive)
  5565. (org-eval-in-calendar '(calendar-forward-month 1))))
  5566. (define-key minibuffer-local-map [(shift up)]
  5567. (lambda () (interactive)
  5568. (org-eval-in-calendar '(calendar-backward-week 1))))
  5569. (define-key minibuffer-local-map [(shift down)]
  5570. (lambda () (interactive)
  5571. (org-eval-in-calendar '(calendar-forward-week 1))))
  5572. (define-key minibuffer-local-map [(shift left)]
  5573. (lambda () (interactive)
  5574. (org-eval-in-calendar '(calendar-backward-day 1))))
  5575. (define-key minibuffer-local-map [(shift right)]
  5576. (lambda () (interactive)
  5577. (org-eval-in-calendar '(calendar-forward-day 1))))
  5578. (define-key minibuffer-local-map ">"
  5579. (lambda () (interactive)
  5580. (org-eval-in-calendar '(scroll-calendar-left 1))))
  5581. (define-key minibuffer-local-map "<"
  5582. (lambda () (interactive)
  5583. (org-eval-in-calendar '(scroll-calendar-right 1))))
  5584. (unwind-protect
  5585. (progn
  5586. (use-local-map map)
  5587. (setq ans (read-string prompt "" nil nil))
  5588. (if (not (string-match "\\S-" ans)) (setq ans nil))
  5589. (setq ans (or ans1 ans ans2)))
  5590. (use-local-map old-map))))))
  5591. (t ; Naked prompt only
  5592. (setq ans (read-string prompt "" nil timestr))))
  5593. (org-detach-overlay org-date-ovl)
  5594. (if (string-match "^[ \t]*[-+][0-9]+[ \t]*$" ans)
  5595. (setq deltadays (string-to-number ans) ans ""))
  5596. (if (string-match
  5597. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  5598. (progn
  5599. (setq year (if (match-end 2)
  5600. (string-to-number (match-string 2 ans))
  5601. (string-to-number (format-time-string "%Y")))
  5602. month (string-to-number (match-string 3 ans))
  5603. day (string-to-number (match-string 4 ans)))
  5604. (if (< year 100) (setq year (+ 2000 year)))
  5605. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  5606. t nil ans))))
  5607. (setq tl (parse-time-string ans)
  5608. year (or (nth 5 tl) (string-to-number (format-time-string "%Y" ct)))
  5609. month (or (nth 4 tl) (string-to-number (format-time-string "%m" ct)))
  5610. day (or (nth 3 tl) (string-to-number (format-time-string "%d" ct)))
  5611. hour (or (nth 2 tl) (string-to-number (format-time-string "%H" ct)))
  5612. minute (or (nth 1 tl) (string-to-number (format-time-string "%M" ct)))
  5613. second (or (nth 0 tl) 0)
  5614. wday (nth 6 tl))
  5615. (setq day (+ day deltadays))
  5616. (when (and wday (not (nth 3 tl)))
  5617. ;; Weekday was given, but no day, so pick that day in the week
  5618. ;; on or after the derived date.
  5619. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  5620. (unless (equal wday wday1)
  5621. (setq day (+ day (% (- wday wday1 -7) 7)))))
  5622. (if (and (boundp 'org-time-was-given)
  5623. (nth 2 tl))
  5624. (setq org-time-was-given t))
  5625. (if (< year 100) (setq year (+ 2000 year)))
  5626. (if to-time
  5627. (encode-time second minute hour day month year)
  5628. (if (or (nth 1 tl) (nth 2 tl))
  5629. (format "%04d-%02d-%02d %02d:%02d" year month day hour minute)
  5630. (format "%04d-%02d-%02d" year month day)))))
  5631. (defun org-eval-in-calendar (form)
  5632. "Eval FORM in the calendar window and return to current window.
  5633. Also, store the cursor date in variable ans2."
  5634. (let ((sw (selected-window)))
  5635. (select-window (get-buffer-window "*Calendar*"))
  5636. (eval form)
  5637. (when (calendar-cursor-to-date)
  5638. (let* ((date (calendar-cursor-to-date))
  5639. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  5640. (setq ans2 (format-time-string "%Y-%m-%d" time))))
  5641. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  5642. (select-window sw)))
  5643. (defun org-calendar-select ()
  5644. "Return to `org-read-date' with the date currently selected.
  5645. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  5646. (interactive)
  5647. (when (calendar-cursor-to-date)
  5648. (let* ((date (calendar-cursor-to-date))
  5649. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  5650. (setq ans1 (format-time-string "%Y-%m-%d" time)))
  5651. (if (active-minibuffer-window) (exit-minibuffer))))
  5652. (defun org-insert-time-stamp (time &optional with-hm inactive pre post)
  5653. "Insert a date stamp for the date given by the internal TIME.
  5654. WITH-HM means, use the stamp format that includes the time of the day.
  5655. INACTIVE means use square brackets instead of angular ones, so that the
  5656. stamp will not contribute to the agenda.
  5657. PRE and POST are optional strings to be inserted before and after the
  5658. stamp.
  5659. The command returns the inserted time stamp."
  5660. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  5661. stamp)
  5662. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  5663. (insert (or pre ""))
  5664. (insert (setq stamp (format-time-string fmt time)))
  5665. (insert (or post ""))
  5666. stamp))
  5667. (defun org-toggle-time-stamp-overlays ()
  5668. "Toggle the use of custom time stamp formats."
  5669. (interactive)
  5670. (setq org-display-custom-times (not org-display-custom-times))
  5671. (unless org-display-custom-times
  5672. (let ((p (point-min)) (bmp (buffer-modified-p)))
  5673. (while (setq p (next-single-property-change p 'display))
  5674. (if (and (get-text-property p 'display)
  5675. (eq (get-text-property p 'face) 'org-date))
  5676. (remove-text-properties
  5677. p (setq p (next-single-property-change p 'display))
  5678. '(display t))))
  5679. (set-buffer-modified-p bmp)))
  5680. (if (featurep 'xemacs)
  5681. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  5682. (org-restart-font-lock)
  5683. (setq org-table-may-need-update t)
  5684. (if org-display-custom-times
  5685. (message "Time stamps are overlayed with custom format")
  5686. (message "Time stamp overlays removed")))
  5687. (defun org-display-custom-time (beg end)
  5688. "Overlay modified time stamp format over timestamp between BED and END."
  5689. (let* ((t1 (save-match-data
  5690. (org-parse-time-string (buffer-substring beg end) t)))
  5691. (w1 (- end beg))
  5692. (with-hm (and (nth 1 t1) (nth 2 t1)))
  5693. (inactive (= (char-before (1- beg)) ?\[))
  5694. (tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats))
  5695. (time (org-fix-decoded-time t1))
  5696. (str (org-add-props
  5697. (format-time-string
  5698. (substring tf 1 -1) (apply 'encode-time time))
  5699. nil 'mouse-face 'highlight))
  5700. (w2 (length str)))
  5701. (if (not (= w2 w1))
  5702. (add-text-properties (1+ beg) (+ 2 beg)
  5703. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  5704. (if (featurep 'xemacs)
  5705. (progn
  5706. (put-text-property beg end 'invisible t)
  5707. (put-text-property beg end 'end-glyph (make-glyph str)))
  5708. (put-text-property beg end 'display str))))
  5709. (defun org-translate-time (string)
  5710. "Translate all timestamps in STRING to custom format.
  5711. But do this only if the variable `org-display-custom-times' is set."
  5712. (when org-display-custom-times
  5713. (save-match-data
  5714. (let* ((start 0)
  5715. (re org-ts-regexp-both)
  5716. t1 with-hm inactive tf time str beg end)
  5717. (while (setq start (string-match re string start))
  5718. (setq beg (match-beginning 0)
  5719. end (match-end 0)
  5720. t1 (save-match-data
  5721. (org-parse-time-string (substring string beg end) t))
  5722. with-hm (and (nth 1 t1) (nth 2 t1))
  5723. inactive (equal (substring string beg (1+ beg)) "[")
  5724. tf (funcall (if with-hm 'cdr 'car)
  5725. org-time-stamp-custom-formats)
  5726. time (org-fix-decoded-time t1)
  5727. str (format-time-string
  5728. (concat
  5729. (if inactive "[" "<") (substring tf 1 -1)
  5730. (if inactive "]" ">"))
  5731. (apply 'encode-time time))
  5732. string (replace-match str t t string)
  5733. start (+ start (length str)))))))
  5734. string)
  5735. (defun org-fix-decoded-time (time)
  5736. "Set 0 instead of nil for the first 6 elements of time.
  5737. Don't touch the rest."
  5738. (let ((n 0))
  5739. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  5740. (defun org-days-to-time (timestamp-string)
  5741. "Difference between TIMESTAMP-STRING and now in days."
  5742. (- (time-to-days (org-time-string-to-time timestamp-string))
  5743. (time-to-days (current-time))))
  5744. (defun org-deadline-close (timestamp-string &optional ndays)
  5745. "Is the time in TIMESTAMP-STRING close to the current date?"
  5746. (and (< (org-days-to-time timestamp-string)
  5747. (or ndays org-deadline-warning-days))
  5748. (not (org-entry-is-done-p))))
  5749. (defun org-calendar-select-mouse (ev)
  5750. "Return to `org-read-date' with the date currently selected.
  5751. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  5752. (interactive "e")
  5753. (mouse-set-point ev)
  5754. (when (calendar-cursor-to-date)
  5755. (let* ((date (calendar-cursor-to-date))
  5756. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  5757. (setq ans1 (format-time-string "%Y-%m-%d" time)))
  5758. (if (active-minibuffer-window) (exit-minibuffer))))
  5759. (defun org-check-deadlines (ndays)
  5760. "Check if there are any deadlines due or past due.
  5761. A deadline is considered due if it happens within `org-deadline-warning-days'
  5762. days from today's date. If the deadline appears in an entry marked DONE,
  5763. it is not shown. The prefix arg NDAYS can be used to test that many
  5764. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  5765. (interactive "P")
  5766. (let* ((org-warn-days
  5767. (cond
  5768. ((equal ndays '(4)) 100000)
  5769. (ndays (prefix-numeric-value ndays))
  5770. (t org-deadline-warning-days)))
  5771. (case-fold-search nil)
  5772. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  5773. (callback
  5774. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  5775. (message "%d deadlines past-due or due within %d days"
  5776. (org-occur regexp nil callback)
  5777. org-warn-days)))
  5778. (defun org-evaluate-time-range (&optional to-buffer)
  5779. "Evaluate a time range by computing the difference between start and end.
  5780. Normally the result is just printed in the echo area, but with prefix arg
  5781. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  5782. If the time range is actually in a table, the result is inserted into the
  5783. next column.
  5784. For time difference computation, a year is assumed to be exactly 365
  5785. days in order to avoid rounding problems."
  5786. (interactive "P")
  5787. (or
  5788. (org-clock-update-time-maybe)
  5789. (save-excursion
  5790. (unless (org-at-date-range-p)
  5791. (goto-char (point-at-bol))
  5792. (re-search-forward org-tr-regexp (point-at-eol) t))
  5793. (if (not (org-at-date-range-p))
  5794. (error "Not at a time-stamp range, and none found in current line")))
  5795. (let* ((ts1 (match-string 1))
  5796. (ts2 (match-string 2))
  5797. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  5798. (match-end (match-end 0))
  5799. (time1 (org-time-string-to-time ts1))
  5800. (time2 (org-time-string-to-time ts2))
  5801. (t1 (time-to-seconds time1))
  5802. (t2 (time-to-seconds time2))
  5803. (diff (abs (- t2 t1)))
  5804. (negative (< (- t2 t1) 0))
  5805. ;; (ys (floor (* 365 24 60 60)))
  5806. (ds (* 24 60 60))
  5807. (hs (* 60 60))
  5808. (fy "%dy %dd %02d:%02d")
  5809. (fy1 "%dy %dd")
  5810. (fd "%dd %02d:%02d")
  5811. (fd1 "%dd")
  5812. (fh "%02d:%02d")
  5813. y d h m align)
  5814. (if havetime
  5815. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  5816. y 0
  5817. d (floor (/ diff ds)) diff (mod diff ds)
  5818. h (floor (/ diff hs)) diff (mod diff hs)
  5819. m (floor (/ diff 60)))
  5820. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  5821. y 0
  5822. d (floor (+ (/ diff ds) 0.5))
  5823. h 0 m 0))
  5824. (if (not to-buffer)
  5825. (message (org-make-tdiff-string y d h m))
  5826. (when (org-at-table-p)
  5827. (goto-char match-end)
  5828. (setq align t)
  5829. (and (looking-at " *|") (goto-char (match-end 0))))
  5830. (if (looking-at
  5831. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  5832. (replace-match ""))
  5833. (if negative (insert " -"))
  5834. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  5835. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  5836. (insert " " (format fh h m))))
  5837. (if align (org-table-align))
  5838. (message "Time difference inserted")))))
  5839. (defun org-make-tdiff-string (y d h m)
  5840. (let ((fmt "")
  5841. (l nil))
  5842. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  5843. l (push y l)))
  5844. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  5845. l (push d l)))
  5846. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  5847. l (push h l)))
  5848. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  5849. l (push m l)))
  5850. (apply 'format fmt (nreverse l))))
  5851. (defun org-time-string-to-time (s)
  5852. (apply 'encode-time (org-parse-time-string s)))
  5853. (defun org-parse-time-string (s &optional nodefault)
  5854. "Parse the standard Org-mode time string.
  5855. This should be a lot faster than the normal `parse-time-string'.
  5856. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  5857. hour and minute fields will be nil if not given."
  5858. (if (string-match org-ts-regexp1 s)
  5859. (list 0
  5860. (if (or (match-beginning 8) (not nodefault))
  5861. (string-to-number (or (match-string 8 s) "0")))
  5862. (if (or (match-beginning 7) (not nodefault))
  5863. (string-to-number (or (match-string 7 s) "0")))
  5864. (string-to-number (match-string 4 s))
  5865. (string-to-number (match-string 3 s))
  5866. (string-to-number (match-string 2 s))
  5867. nil nil nil)
  5868. (make-list 9 0)))
  5869. (defun org-timestamp-up (&optional arg)
  5870. "Increase the date item at the cursor by one.
  5871. If the cursor is on the year, change the year. If it is on the month or
  5872. the day, change that.
  5873. With prefix ARG, change by that many units."
  5874. (interactive "p")
  5875. (org-timestamp-change (prefix-numeric-value arg)))
  5876. (defun org-timestamp-down (&optional arg)
  5877. "Decrease the date item at the cursor by one.
  5878. If the cursor is on the year, change the year. If it is on the month or
  5879. the day, change that.
  5880. With prefix ARG, change by that many units."
  5881. (interactive "p")
  5882. (org-timestamp-change (- (prefix-numeric-value arg))))
  5883. (defun org-timestamp-up-day (&optional arg)
  5884. "Increase the date in the time stamp by one day.
  5885. With prefix ARG, change that many days."
  5886. (interactive "p")
  5887. (if (and (not (org-at-timestamp-p t))
  5888. (org-on-heading-p))
  5889. (org-todo 'up)
  5890. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  5891. (defun org-timestamp-down-day (&optional arg)
  5892. "Decrease the date in the time stamp by one day.
  5893. With prefix ARG, change that many days."
  5894. (interactive "p")
  5895. (if (and (not (org-at-timestamp-p t))
  5896. (org-on-heading-p))
  5897. (org-todo 'down)
  5898. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  5899. (defsubst org-pos-in-match-range (pos n)
  5900. (and (match-beginning n)
  5901. (<= (match-beginning n) pos)
  5902. (>= (match-end n) pos)))
  5903. (defun org-at-timestamp-p (&optional inactive-ok)
  5904. "Determine if the cursor is in or at a timestamp."
  5905. (interactive)
  5906. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  5907. (pos (point))
  5908. (ans (or (looking-at tsr)
  5909. (save-excursion
  5910. (skip-chars-backward "^[<\n\r\t")
  5911. (if (> (point) 1) (backward-char 1))
  5912. (and (looking-at tsr)
  5913. (> (- (match-end 0) pos) -1))))))
  5914. (and (boundp 'org-ts-what)
  5915. (setq org-ts-what
  5916. (cond
  5917. ((org-pos-in-match-range pos 2) 'year)
  5918. ((org-pos-in-match-range pos 3) 'month)
  5919. ((org-pos-in-match-range pos 7) 'hour)
  5920. ((org-pos-in-match-range pos 8) 'minute)
  5921. ((or (org-pos-in-match-range pos 4)
  5922. (org-pos-in-match-range pos 5)) 'day)
  5923. (t 'day))))
  5924. ans))
  5925. (defun org-timestamp-change (n &optional what)
  5926. "Change the date in the time stamp at point.
  5927. The date will be changed by N times WHAT. WHAT can be `day', `month',
  5928. `year', `minute', `second'. If WHAT is not given, the cursor position
  5929. in the timestamp determines what will be changed."
  5930. (let ((pos (point))
  5931. with-hm inactive
  5932. org-ts-what
  5933. ts time time0)
  5934. (if (not (org-at-timestamp-p t))
  5935. (error "Not at a timestamp"))
  5936. (if (and (not what) (not (eq org-ts-what 'day))
  5937. org-display-custom-times
  5938. (get-text-property (point) 'display)
  5939. (not (get-text-property (1- (point)) 'display)))
  5940. (setq org-ts-what 'day))
  5941. (setq org-ts-what (or what org-ts-what)
  5942. with-hm (<= (abs (- (cdr org-ts-lengths)
  5943. (- (match-end 0) (match-beginning 0))))
  5944. 1)
  5945. inactive (= (char-after (match-beginning 0)) ?\[)
  5946. ts (match-string 0))
  5947. (replace-match "")
  5948. (setq time0 (org-parse-time-string ts))
  5949. (setq time
  5950. (apply 'encode-time
  5951. (append
  5952. (list (or (car time0) 0))
  5953. (list (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0)))
  5954. (list (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0)))
  5955. (list (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0)))
  5956. (list (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0)))
  5957. (list (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0)))
  5958. (nthcdr 6 time0))))
  5959. (if (eq what 'calendar)
  5960. (let ((cal-date
  5961. (save-excursion
  5962. (save-match-data
  5963. (set-buffer "*Calendar*")
  5964. (calendar-cursor-to-date)))))
  5965. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  5966. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  5967. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  5968. (setcar time0 (or (car time0) 0))
  5969. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  5970. (setcar (nthcdr 2 time0) (or (nth 1 time0) 0))
  5971. (setq time (apply 'encode-time time0))))
  5972. (setq org-last-changed-timestamp
  5973. (org-insert-time-stamp time with-hm inactive))
  5974. (org-clock-update-time-maybe)
  5975. (goto-char pos)
  5976. ;; Try to recenter the calendar window, if any
  5977. (if (and org-calendar-follow-timestamp-change
  5978. (get-buffer-window "*Calendar*" t)
  5979. (memq org-ts-what '(day month year)))
  5980. (org-recenter-calendar (time-to-days time)))))
  5981. (defun org-recenter-calendar (date)
  5982. "If the calendar is visible, recenter it to DATE."
  5983. (let* ((win (selected-window))
  5984. (cwin (get-buffer-window "*Calendar*" t))
  5985. (calendar-move-hook nil))
  5986. (when cwin
  5987. (select-window cwin)
  5988. (calendar-goto-date (if (listp date) date
  5989. (calendar-gregorian-from-absolute date)))
  5990. (select-window win))))
  5991. (defun org-goto-calendar (&optional arg)
  5992. "Go to the Emacs calendar at the current date.
  5993. If there is a time stamp in the current line, go to that date.
  5994. A prefix ARG can be used to force the current date."
  5995. (interactive "P")
  5996. (let ((tsr org-ts-regexp) diff
  5997. (calendar-move-hook nil)
  5998. (view-calendar-holidays-initially nil)
  5999. (view-diary-entries-initially nil))
  6000. (if (or (org-at-timestamp-p)
  6001. (save-excursion
  6002. (beginning-of-line 1)
  6003. (looking-at (concat ".*" tsr))))
  6004. (let ((d1 (time-to-days (current-time)))
  6005. (d2 (time-to-days
  6006. (org-time-string-to-time (match-string 1)))))
  6007. (setq diff (- d2 d1))))
  6008. (calendar)
  6009. (calendar-goto-today)
  6010. (if (and diff (not arg)) (calendar-forward-day diff))))
  6011. (defun org-date-from-calendar ()
  6012. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  6013. If there is already a time stamp at the cursor position, update it."
  6014. (interactive)
  6015. (org-timestamp-change 0 'calendar))
  6016. ;;; The clock for measuring work time.
  6017. (defvar org-clock-marker (make-marker)
  6018. "Marker recording the last clock-in.")
  6019. (defun org-clock-in ()
  6020. "Start the clock on the current item.
  6021. If necessary, clock-out of the currently active clock."
  6022. (interactive)
  6023. (org-clock-out t)
  6024. (let (ts)
  6025. (save-excursion
  6026. (org-back-to-heading t)
  6027. (beginning-of-line 2)
  6028. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  6029. (not (equal (match-string 1) org-clock-string)))
  6030. (beginning-of-line 1))
  6031. (insert "\n") (backward-char 1)
  6032. (indent-relative)
  6033. (insert org-clock-string " ")
  6034. (setq ts (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  6035. (move-marker org-clock-marker (point))
  6036. (message "Clock started at %s" ts))))
  6037. (defun org-clock-out (&optional fail-quietly)
  6038. "Stop the currently running clock.
  6039. If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
  6040. (interactive)
  6041. (catch 'exit
  6042. (if (not (marker-buffer org-clock-marker))
  6043. (if fail-quietly (throw 'exit t) (error "No active clock")))
  6044. (let (ts te s h m)
  6045. (save-excursion
  6046. (set-buffer (marker-buffer org-clock-marker))
  6047. (goto-char org-clock-marker)
  6048. (beginning-of-line 1)
  6049. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  6050. (equal (match-string 1) org-clock-string))
  6051. (setq ts (match-string 2))
  6052. (if fail-quietly (throw 'exit nil) (error "Clock start time is gone")))
  6053. (goto-char org-clock-marker)
  6054. (insert "--")
  6055. (setq te (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  6056. (setq s (- (time-to-seconds (apply 'encode-time (org-parse-time-string te)))
  6057. (time-to-seconds (apply 'encode-time (org-parse-time-string ts))))
  6058. h (floor (/ s 3600))
  6059. s (- s (* 3600 h))
  6060. m (floor (/ s 60))
  6061. s (- s (* 60 s)))
  6062. (insert " => " (format "%2d:%02d" h m))
  6063. (move-marker org-clock-marker nil)
  6064. (org-add-log-maybe 'clock-out)
  6065. (message "Clock stopped at %s after HH:MM = %d:%02d" te h m)))))
  6066. (defun org-clock-cancel ()
  6067. "Cancel the running clock be removing the start timestamp."
  6068. (interactive)
  6069. (if (not (marker-buffer org-clock-marker))
  6070. (error "No active clock"))
  6071. (save-excursion
  6072. (set-buffer (marker-buffer org-clock-marker))
  6073. (goto-char org-clock-marker)
  6074. (delete-region (1- (point-at-bol)) (point-at-eol)))
  6075. (message "Clock canceled"))
  6076. (defvar org-clock-file-total-minutes nil
  6077. "Holds the file total time in minutes, after a call to `org-clock-sum'.")
  6078. (make-variable-buffer-local 'org-clock-file-total-minutes)
  6079. (defun org-clock-sum (&optional tstart tend)
  6080. "Sum the times for each subtree.
  6081. Puts the resulting times in minutes as a text property on each headline."
  6082. (interactive)
  6083. (let* ((bmp (buffer-modified-p))
  6084. (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
  6085. org-clock-string
  6086. "[ \t]*\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)"))
  6087. (lmax 30)
  6088. (ltimes (make-vector lmax 0))
  6089. (t1 0)
  6090. (level 0)
  6091. ts te dt
  6092. time)
  6093. (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t))
  6094. (save-excursion
  6095. (goto-char (point-max))
  6096. (while (re-search-backward re nil t)
  6097. (if (match-end 2)
  6098. ;; A time
  6099. (setq ts (match-string 2)
  6100. te (match-string 3)
  6101. ts (time-to-seconds
  6102. (apply 'encode-time (org-parse-time-string ts)))
  6103. te (time-to-seconds
  6104. (apply 'encode-time (org-parse-time-string te)))
  6105. ts (if tstart (max ts tstart) ts)
  6106. te (if tend (min te tend) te)
  6107. dt (- te ts)
  6108. t1 (if (> dt 0) (+ t1 (floor (/ dt 60))) t1))
  6109. ;; A headline
  6110. (setq level (- (match-end 1) (match-beginning 1)))
  6111. (when (or (> t1 0) (> (aref ltimes level) 0))
  6112. (loop for l from 0 to level do
  6113. (aset ltimes l (+ (aref ltimes l) t1)))
  6114. (setq t1 0 time (aref ltimes level))
  6115. (loop for l from level to (1- lmax) do
  6116. (aset ltimes l 0))
  6117. (goto-char (match-beginning 0))
  6118. (put-text-property (point) (point-at-eol) :org-clock-minutes time))))
  6119. (setq org-clock-file-total-minutes (aref ltimes 0)))
  6120. (set-buffer-modified-p bmp)))
  6121. (defun org-clock-display (&optional total-only)
  6122. "Show subtree times in the entire buffer.
  6123. If TOTAL-ONLY is non-nil, only show the total time for the entire file
  6124. in the echo area."
  6125. (interactive)
  6126. (org-remove-clock-overlays)
  6127. (let (time h m p)
  6128. (org-clock-sum)
  6129. (unless total-only
  6130. (save-excursion
  6131. (goto-char (point-min))
  6132. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  6133. (goto-char p)
  6134. (when (setq time (get-text-property p :org-clock-minutes))
  6135. (org-put-clock-overlay time (funcall outline-level))))
  6136. (setq h (/ org-clock-file-total-minutes 60)
  6137. m (- org-clock-file-total-minutes (* 60 h)))
  6138. ;; Arrange to remove the overlays upon next change.
  6139. (when org-remove-highlights-with-change
  6140. (org-add-hook 'before-change-functions 'org-remove-clock-overlays
  6141. nil 'local))))
  6142. (message "Total file time: %d:%02d (%d hours and %d minutes)" h m h m)))
  6143. (defvar org-clock-overlays nil)
  6144. (make-variable-buffer-local 'org-clock-overlays)
  6145. (defun org-put-clock-overlay (time &optional level)
  6146. "Put an overlays on the current line, displaying TIME.
  6147. If LEVEL is given, prefix time with a corresponding number of stars.
  6148. This creates a new overlay and stores it in `org-clock-overlays', so that it
  6149. will be easy to remove."
  6150. (let* ((c 60) (h (floor (/ time 60))) (m (- time (* 60 h)))
  6151. (l (if level (org-get-legal-level level 0) 0))
  6152. (off 0)
  6153. ov tx)
  6154. (move-to-column c)
  6155. (unless (eolp) (skip-chars-backward "^ \t"))
  6156. (skip-chars-backward " \t")
  6157. (setq ov (org-make-overlay (1- (point)) (point-at-eol))
  6158. tx (concat (buffer-substring (1- (point)) (point))
  6159. (make-string (+ off (max 0 (- c (current-column)))) ?.)
  6160. (org-add-props (format "%s %2d:%02d%s"
  6161. (make-string l ?*) h m
  6162. (make-string (- 10 l) ?\ ))
  6163. '(face secondary-selection))
  6164. ""))
  6165. (if (not (featurep 'xemacs))
  6166. (org-overlay-put ov 'display tx)
  6167. (org-overlay-put ov 'invisible t)
  6168. (org-overlay-put ov 'end-glyph (make-glyph tx)))
  6169. (push ov org-clock-overlays)))
  6170. (defun org-remove-clock-overlays (&optional beg end noremove)
  6171. "Remove the occur highlights from the buffer.
  6172. BEG and END are ignored. If NOREMOVE is nil, remove this function
  6173. from the `before-change-functions' in the current buffer."
  6174. (interactive)
  6175. (unless org-inhibit-highlight-removal
  6176. (mapc 'org-delete-overlay org-clock-overlays)
  6177. (setq org-clock-overlays nil)
  6178. (unless noremove
  6179. (remove-hook 'before-change-functions
  6180. 'org-remove-clock-overlays 'local))))
  6181. (defun org-clock-out-if-current ()
  6182. "Clock out if the current entry contains the running clock.
  6183. This is used to stop the clock after a TODO entry is marked DONE."
  6184. (when (and (equal state org-done-string)
  6185. (equal (marker-buffer org-clock-marker) (current-buffer))
  6186. (< (point) org-clock-marker)
  6187. (> (save-excursion (outline-next-heading) (point))
  6188. org-clock-marker))
  6189. ;; Clock out, but don't accept a logging message for this.
  6190. (let ((org-log-done (if (and (listp org-log-done)
  6191. (member 'clock-out org-log-done))
  6192. '(done)
  6193. org-log-done)))
  6194. (org-clock-out))))
  6195. (add-hook 'org-after-todo-state-change-hook
  6196. 'org-clock-out-if-current)
  6197. (defun org-check-running-clock ()
  6198. "Check if the current buffer contains the running clock.
  6199. If yes, offer to stop it and to save the buffer with the changes."
  6200. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  6201. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  6202. (buffer-name))))
  6203. (org-clock-out)
  6204. (when (y-or-n-p "Save changed buffer?")
  6205. (save-buffer))))
  6206. (defun org-clock-report ()
  6207. "Create a table containing a report about clocked time.
  6208. If the buffer contains lines
  6209. #+BEGIN: clocktable :maxlevel 3 :emphasize nil
  6210. #+END: clocktable
  6211. then the table will be inserted between these lines, replacing whatever
  6212. is was there before. If these lines are not in the buffer, the table
  6213. is inserted at point, surrounded by the special lines.
  6214. The BEGIN line can contain parameters. Allowed are:
  6215. :maxlevel The maximum level to be included in the table. Default is 3.
  6216. :emphasize t/nil, if levell 1 and level 2 should be bold/italic in the table."
  6217. (interactive)
  6218. (org-remove-clock-overlays)
  6219. (unless (org-find-dblock "clocktable")
  6220. (org-create-dblock (list :name "clocktable"
  6221. :maxlevel 2 :emphasize nil)))
  6222. (org-update-dblock))
  6223. (defun org-clock-update-time-maybe ()
  6224. "If this is a CLOCK line, update it and return t.
  6225. Otherwise, return nil."
  6226. (interactive)
  6227. (save-excursion
  6228. (beginning-of-line 1)
  6229. (skip-chars-forward " \t")
  6230. (when (looking-at org-clock-string)
  6231. (let ((re (concat "[ \t]*" org-clock-string
  6232. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  6233. "\\([ \t]*=>.*\\)?"))
  6234. ts te h m s)
  6235. (if (not (looking-at re))
  6236. nil
  6237. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  6238. (end-of-line 1)
  6239. (setq ts (match-string 1)
  6240. te (match-string 2))
  6241. (setq s (- (time-to-seconds
  6242. (apply 'encode-time (org-parse-time-string te)))
  6243. (time-to-seconds
  6244. (apply 'encode-time (org-parse-time-string ts))))
  6245. h (floor (/ s 3600))
  6246. s (- s (* 3600 h))
  6247. m (floor (/ s 60))
  6248. s (- s (* 60 s)))
  6249. (insert " => " (format "%2d:%02d" h m))
  6250. t)))))
  6251. (defun org-clock-special-range (key &optional time as-strings)
  6252. "Return two times bordering a special time range.
  6253. Key is a symbol specifying the range and can be one of `today', `yesterday',
  6254. `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
  6255. A week starts Monday 0:00 and ends Sunday 24:00.
  6256. The range is determined relative to TIME. TIME defaults to the current time.
  6257. The return value is a cons cell with two internal times like the ones
  6258. returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
  6259. the returned times will be formatted strings."
  6260. (let* ((tm (decode-time (or time (current-time))))
  6261. (s 0) (m (nth 1 tm)) (h (nth 2 tm))
  6262. (d (nth 3 tm)) (month (nth 4 tm)) (y (nth 5 tm))
  6263. (dow (nth 6 tm))
  6264. s1 m1 h1 d1 month1 y1 diff ts te fm)
  6265. (cond
  6266. ((eq key 'today)
  6267. (setq h 0 m 0 h1 24 m1 0))
  6268. ((eq key 'yesterday)
  6269. (setq d (1- d) h 0 m 0 h1 24 m1 0))
  6270. ((eq key 'thisweek)
  6271. (setq diff (if (= dow 0) 6 (1- dow))
  6272. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  6273. ((eq key 'lastweek)
  6274. (setq diff (+ 7 (if (= dow 0) 6 (1- dow)))
  6275. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  6276. ((eq key 'thismonth)
  6277. (setq d 1 h 0 m 0 d1 1 month1 (1+ month) h1 0 m1 0))
  6278. ((eq key 'lastmonth)
  6279. (setq d 1 h 0 m 0 d1 1 month (1- month) month1 (1+ month) h1 0 m1 0))
  6280. ((eq key 'thisyear)
  6281. (setq m 0 h 0 d 1 month 1 y1 (1+ y)))
  6282. ((eq key 'lastyear)
  6283. (setq m 0 h 0 d 1 month 1 y (1- y) y1 (1+ y)))
  6284. (t (error "No such time block %s" key)))
  6285. (setq ts (encode-time s m h d month y)
  6286. te (encode-time (or s1 s) (or m1 m) (or h1 h)
  6287. (or d1 d) (or month1 month) (or y1 y)))
  6288. (setq fm (cdr org-time-stamp-formats))
  6289. (if as-strings
  6290. (cons (format-time-string fm ts) (format-time-string fm te))
  6291. (cons ts te))))
  6292. (defun org-dblock-write:clocktable (params)
  6293. "Write the standard clocktable."
  6294. (let ((hlchars '((1 . "*") (2 . ?/)))
  6295. (emph nil)
  6296. (ins (make-marker))
  6297. ipos time h m p level hlc hdl maxlevel
  6298. ts te cc block)
  6299. (setq maxlevel (or (plist-get params :maxlevel) 3)
  6300. emph (plist-get params :emphasize)
  6301. ts (plist-get params :tstart)
  6302. te (plist-get params :tend)
  6303. block (plist-get params :block))
  6304. (when block
  6305. (setq cc (org-clock-special-range block nil t)
  6306. ts (car cc) te (cdr cc)))
  6307. (if ts (setq ts (time-to-seconds
  6308. (apply 'encode-time (org-parse-time-string ts)))))
  6309. (if te (setq te (time-to-seconds
  6310. (apply 'encode-time (org-parse-time-string te)))))
  6311. (move-marker ins (point))
  6312. (setq ipos (point))
  6313. ;; FIXME: does not yet use org-insert-time-stamp
  6314. (insert-before-markers "Clock summary at ["
  6315. (substring
  6316. (format-time-string (cdr org-time-stamp-formats))
  6317. 1 -1)
  6318. "]."
  6319. (if block
  6320. (format " Considered range is /%s/." block)
  6321. "")
  6322. "\n\n|L|Headline|Time|\n")
  6323. (org-clock-sum ts te)
  6324. (setq h (/ org-clock-file-total-minutes 60)
  6325. m (- org-clock-file-total-minutes (* 60 h)))
  6326. (insert-before-markers "|-\n|0|" "*Total file time*| "
  6327. (format "*%d:%02d*" h m)
  6328. "|\n")
  6329. (goto-char (point-min))
  6330. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  6331. (goto-char p)
  6332. (when (setq time (get-text-property p :org-clock-minutes))
  6333. (save-excursion
  6334. (beginning-of-line 1)
  6335. (when (and (looking-at "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[0-9a-zA-Z_@:]+:\\)?[ \t]*$")
  6336. (setq level (- (match-end 1) (match-beginning 1)))
  6337. (<= level maxlevel))
  6338. (setq hlc (if emph (or (cdr (assoc level hlchars)) "") "")
  6339. hdl (match-string 2)
  6340. h (/ time 60)
  6341. m (- time (* 60 h)))
  6342. (goto-char ins)
  6343. (if (= level 1) (insert-before-markers "|-\n"))
  6344. (insert-before-markers
  6345. "| " (int-to-string level) "|" hlc hdl hlc " |"
  6346. (make-string (1- level) ?|)
  6347. hlc
  6348. (format "%d:%02d" h m)
  6349. hlc
  6350. " |\n")))))
  6351. (goto-char ins)
  6352. (backward-delete-char 1)
  6353. (goto-char ipos)
  6354. (skip-chars-forward "^|")
  6355. (org-table-align)))
  6356. (defun org-collect-clock-time-entries ()
  6357. "Return an internal list with clocking information.
  6358. This list has one entry for each CLOCK interval.
  6359. FIXME: describe the elements."
  6360. (interactive)
  6361. (let ((re (concat "^[ \t]*" org-clock-string
  6362. " *\\[\\(.*?\\)\\]--\\[\\(.*?\\)\\]"))
  6363. rtn beg end next cont level title total closedp leafp
  6364. clockpos titlepos h m donep)
  6365. (save-excursion
  6366. (org-clock-sum)
  6367. (goto-char (point-min))
  6368. (while (re-search-forward re nil t)
  6369. (setq clockpos (match-beginning 0)
  6370. beg (match-string 1) end (match-string 2)
  6371. cont (match-end 0))
  6372. (setq beg (apply 'encode-time (org-parse-time-string beg))
  6373. end (apply 'encode-time (org-parse-time-string end)))
  6374. (org-back-to-heading t)
  6375. (setq donep (org-entry-is-done-p))
  6376. (setq titlepos (point)
  6377. total (or (get-text-property (1+ (point)) :org-clock-minutes) 0)
  6378. h (/ total 60) m (- total (* 60 h))
  6379. total (cons h m))
  6380. (looking-at "\\(\\*+\\) +\\(.*\\)")
  6381. (setq level (- (match-end 1) (match-beginning 1))
  6382. title (org-match-string-no-properties 2))
  6383. (save-excursion (outline-next-heading) (setq next (point)))
  6384. (setq closedp (re-search-forward org-closed-time-regexp next t))
  6385. (goto-char next)
  6386. (setq leafp (and (looking-at "^\\*+ ")
  6387. (<= (- (match-end 0) (point)) level)))
  6388. (push (list beg end clockpos closedp donep
  6389. total title titlepos level leafp)
  6390. rtn)
  6391. (goto-char cont)))
  6392. (nreverse rtn)))
  6393. ;;; Agenda, and Diary Integration
  6394. ;;; Define the mode
  6395. (defvar org-agenda-mode-map (make-sparse-keymap)
  6396. "Keymap for `org-agenda-mode'.")
  6397. (defvar org-agenda-menu) ; defined later in this file.
  6398. (defvar org-agenda-follow-mode nil)
  6399. (defvar org-agenda-show-log nil)
  6400. (defvar org-agenda-redo-command nil)
  6401. (defvar org-agenda-mode-hook nil)
  6402. (defvar org-agenda-type nil)
  6403. (defvar org-agenda-force-single-file nil)
  6404. (defun org-agenda-mode ()
  6405. "Mode for time-sorted view on action items in Org-mode files.
  6406. The following commands are available:
  6407. \\{org-agenda-mode-map}"
  6408. (interactive)
  6409. (kill-all-local-variables)
  6410. (setq major-mode 'org-agenda-mode)
  6411. (setq mode-name "Org-Agenda")
  6412. (use-local-map org-agenda-mode-map)
  6413. (easy-menu-add org-agenda-menu)
  6414. (if org-startup-truncated (setq truncate-lines t))
  6415. (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
  6416. (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
  6417. (unless org-agenda-keep-modes
  6418. (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
  6419. org-agenda-show-log nil))
  6420. (easy-menu-change
  6421. '("Agenda") "Agenda Files"
  6422. (append
  6423. (list
  6424. (vector
  6425. (if (get 'org-agenda-files 'org-restrict)
  6426. "Restricted to single file"
  6427. "Edit File List")
  6428. '(org-edit-agenda-file-list)
  6429. (not (get 'org-agenda-files 'org-restrict)))
  6430. "--")
  6431. (mapcar 'org-file-menu-entry (org-agenda-files))))
  6432. (org-agenda-set-mode-name)
  6433. (apply
  6434. (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
  6435. (list 'org-agenda-mode-hook)))
  6436. ;(substitute-key-definition 'undo 'org-agenda-undo
  6437. ; org-agenda-mode-map global-map)
  6438. (define-key org-agenda-mode-map "\C-i" 'org-agenda-goto)
  6439. (define-key org-agenda-mode-map [(tab)] 'org-agenda-goto)
  6440. (define-key org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
  6441. (define-key org-agenda-mode-map "\C-k" 'org-agenda-kill)
  6442. (define-key org-agenda-mode-map "\C-c$" 'org-agenda-archive)
  6443. (define-key org-agenda-mode-map "$" 'org-agenda-archive)
  6444. (define-key org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
  6445. (define-key org-agenda-mode-map " " 'org-agenda-show)
  6446. (define-key org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
  6447. (define-key org-agenda-mode-map "o" 'delete-other-windows)
  6448. (define-key org-agenda-mode-map "L" 'org-agenda-recenter)
  6449. (define-key org-agenda-mode-map "t" 'org-agenda-todo)
  6450. (define-key org-agenda-mode-map "a" 'org-agenda-toggle-archive-tag)
  6451. (define-key org-agenda-mode-map ":" 'org-agenda-set-tags)
  6452. (define-key org-agenda-mode-map "." 'org-agenda-goto-today)
  6453. (define-key org-agenda-mode-map "d" 'org-agenda-day-view)
  6454. (define-key org-agenda-mode-map "w" 'org-agenda-week-view)
  6455. (define-key org-agenda-mode-map (org-key 'S-right) 'org-agenda-date-later)
  6456. (define-key org-agenda-mode-map (org-key 'S-left) 'org-agenda-date-earlier)
  6457. (define-key org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-date-later)
  6458. (define-key org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-date-earlier)
  6459. (define-key org-agenda-mode-map ">" 'org-agenda-date-prompt)
  6460. (define-key org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
  6461. (define-key org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
  6462. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  6463. (while l (define-key org-agenda-mode-map
  6464. (int-to-string (pop l)) 'digit-argument)))
  6465. (define-key org-agenda-mode-map "f" 'org-agenda-follow-mode)
  6466. (define-key org-agenda-mode-map "l" 'org-agenda-log-mode)
  6467. (define-key org-agenda-mode-map "D" 'org-agenda-toggle-diary)
  6468. (define-key org-agenda-mode-map "g" 'org-agenda-toggle-time-grid)
  6469. (define-key org-agenda-mode-map "r" 'org-agenda-redo)
  6470. (define-key org-agenda-mode-map "q" 'org-agenda-quit)
  6471. (define-key org-agenda-mode-map "x" 'org-agenda-exit)
  6472. (define-key org-agenda-mode-map "s" 'org-save-all-org-buffers)
  6473. (define-key org-agenda-mode-map "P" 'org-agenda-show-priority)
  6474. (define-key org-agenda-mode-map "T" 'org-agenda-show-tags)
  6475. (define-key org-agenda-mode-map "n" 'next-line)
  6476. (define-key org-agenda-mode-map "p" 'previous-line)
  6477. (define-key org-agenda-mode-map "\C-n" 'org-agenda-next-date-line)
  6478. (define-key org-agenda-mode-map "\C-p" 'org-agenda-previous-date-line)
  6479. (define-key org-agenda-mode-map "," 'org-agenda-priority)
  6480. (define-key org-agenda-mode-map "\C-c," 'org-agenda-priority)
  6481. (define-key org-agenda-mode-map "i" 'org-agenda-diary-entry)
  6482. (define-key org-agenda-mode-map "c" 'org-agenda-goto-calendar)
  6483. (eval-after-load "calendar"
  6484. '(define-key calendar-mode-map org-calendar-to-agenda-key
  6485. 'org-calendar-goto-agenda))
  6486. (define-key org-agenda-mode-map "C" 'org-agenda-convert-date)
  6487. (define-key org-agenda-mode-map "m" 'org-agenda-phases-of-moon)
  6488. (define-key org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
  6489. (define-key org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
  6490. (define-key org-agenda-mode-map "h" 'org-agenda-holidays)
  6491. (define-key org-agenda-mode-map "H" 'org-agenda-holidays)
  6492. (define-key org-agenda-mode-map "+" 'org-agenda-priority-up)
  6493. (define-key org-agenda-mode-map "I" 'org-agenda-clock-in)
  6494. (define-key org-agenda-mode-map "O" 'org-clock-out)
  6495. (define-key org-agenda-mode-map "X" 'org-clock-cancel)
  6496. (define-key org-agenda-mode-map "-" 'org-agenda-priority-down)
  6497. (define-key org-agenda-mode-map (org-key 'S-up) 'org-agenda-priority-up)
  6498. (define-key org-agenda-mode-map (org-key 'S-down) 'org-agenda-priority-down)
  6499. (define-key org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
  6500. (define-key org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
  6501. (define-key org-agenda-mode-map [(right)] 'org-agenda-later)
  6502. (define-key org-agenda-mode-map [(left)] 'org-agenda-earlier)
  6503. (define-key org-agenda-mode-map "\C-c\C-x\C-c" 'org-export-icalendar-combine-agenda-files)
  6504. (defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
  6505. "Local keymap for agenda entries from Org-mode.")
  6506. (define-key org-agenda-keymap
  6507. (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
  6508. (define-key org-agenda-keymap
  6509. (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
  6510. (when org-agenda-mouse-1-follows-link
  6511. (define-key org-agenda-keymap [follow-link] 'mouse-face))
  6512. (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
  6513. '("Agenda"
  6514. ("Agenda Files")
  6515. "--"
  6516. ["Show" org-agenda-show t]
  6517. ["Go To (other window)" org-agenda-goto t]
  6518. ["Go To (this window)" org-agenda-switch-to t]
  6519. ["Follow Mode" org-agenda-follow-mode
  6520. :style toggle :selected org-agenda-follow-mode :active t]
  6521. "--"
  6522. ["Cycle TODO" org-agenda-todo t]
  6523. ["Archive subtree" org-agenda-archive t]
  6524. ["Delete subtree" org-agenda-kill t]
  6525. "--"
  6526. ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
  6527. ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
  6528. ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
  6529. "--"
  6530. ("Tags"
  6531. ["Show all Tags" org-agenda-show-tags t]
  6532. ["Set Tags" org-agenda-set-tags t])
  6533. ("Date/Schedule"
  6534. ["Schedule" org-agenda-schedule t]
  6535. ["Set Deadline" org-agenda-deadline t]
  6536. "--"
  6537. ["Change date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
  6538. ["Change date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
  6539. ["Change date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
  6540. ("Priority"
  6541. ["Set Priority" org-agenda-priority t]
  6542. ["Increase Priority" org-agenda-priority-up t]
  6543. ["Decrease Priority" org-agenda-priority-down t]
  6544. ["Show Priority" org-agenda-show-priority t])
  6545. ("Calendar/Diary"
  6546. ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
  6547. ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
  6548. ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
  6549. ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
  6550. ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
  6551. ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
  6552. "--"
  6553. ["Create iCalendar file" org-export-icalendar-combine-agenda-files t])
  6554. "--"
  6555. ("View"
  6556. ["Day View" org-agenda-day-view :active (org-agenda-check-type nil 'agenda)
  6557. :style radio :selected (equal org-agenda-ndays 1)]
  6558. ["Week View" org-agenda-week-view :active (org-agenda-check-type nil 'agenda)
  6559. :style radio :selected (equal org-agenda-ndays 7)]
  6560. "--"
  6561. ["Show Logbook entries" org-agenda-log-mode
  6562. :style toggle :selected org-agenda-show-log :active (org-agenda-check-type nil 'agenda 'timeline)]
  6563. ["Include Diary" org-agenda-toggle-diary
  6564. :style toggle :selected org-agenda-include-diary :active (org-agenda-check-type nil 'agenda)]
  6565. ["Use Time Grid" org-agenda-toggle-time-grid
  6566. :style toggle :selected org-agenda-use-time-grid :active (org-agenda-check-type nil 'agenda)])
  6567. ["Rebuild buffer" org-agenda-redo t]
  6568. ["Save all Org-mode Buffers" org-save-all-org-buffers t]
  6569. "--"
  6570. ; ["Undo Remote Editing" org-agenda-undo org-agenda-multi-buffer-undo-list]
  6571. ; "--"
  6572. ["Quit" org-agenda-quit t]
  6573. ["Exit and Release Buffers" org-agenda-exit t]
  6574. ))
  6575. (defvar org-agenda-restrict nil)
  6576. (defvar org-agenda-restrict-begin (make-marker))
  6577. (defvar org-agenda-restrict-end (make-marker))
  6578. (defvar org-agenda-last-dispatch-buffer nil)
  6579. ;;;###autoload
  6580. (defun org-agenda (arg)
  6581. "Dispatch agenda commands to collect entries to the agenda buffer.
  6582. Prompts for a character to select a command. Any prefix arg will be passed
  6583. on to the selected command. The default selections are:
  6584. g
  6585. a Call `org-agenda-list' to display the agenda for current day or week.
  6586. t Call `org-todo-list' to display the global todo list.
  6587. T Call `org-todo-list' to display the global todo list, select only
  6588. entries with a specific TODO keyword (the user gets a prompt).
  6589. m Call `org-tags-view' to display headlines with tags matching
  6590. a condition (the user is prompted for the condition).
  6591. M Like `m', but select only TODO entries, no ordinary headlines.
  6592. l Create a timeeline for the current buffer.
  6593. More commands can be added by configuring the variable
  6594. `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
  6595. searches can be pre-defined in this way.
  6596. If the current buffer is in Org-mode and visiting a file, you can also
  6597. first press `1' to indicate that the agenda should be temporarily (until the
  6598. next use of \\[org-agenda]) restricted to the current file."
  6599. (interactive "P")
  6600. (catch 'exit
  6601. (let* ((buf (current-buffer))
  6602. (bfn (buffer-file-name (buffer-base-buffer)))
  6603. (restrict-ok (and bfn (org-mode-p)))
  6604. (custom org-agenda-custom-commands)
  6605. c entry key type match lprops)
  6606. ;; Turn off restriction
  6607. (put 'org-agenda-files 'org-restrict nil)
  6608. (setq org-agenda-restrict nil)
  6609. (move-marker org-agenda-restrict-begin nil)
  6610. (move-marker org-agenda-restrict-end nil)
  6611. ;; Remember where this call originated
  6612. (setq org-agenda-last-dispatch-buffer (current-buffer))
  6613. (save-window-excursion
  6614. (delete-other-windows)
  6615. (switch-to-buffer-other-window " *Agenda Commands*")
  6616. (erase-buffer)
  6617. (insert
  6618. "Press key for an agenda command:
  6619. --------------------------------
  6620. a Agenda for current week or day
  6621. t List of all TODO entries T Entries with special TODO kwd
  6622. m Match a TAGS query M Like m, but only TODO entries
  6623. L Timeline for current buffer C Configure custom agenda commands")
  6624. (while (setq entry (pop custom))
  6625. (setq key (car entry) type (nth 1 entry) match (nth 2 entry))
  6626. (insert (format "\n%-4s%-14s: %s"
  6627. key
  6628. (cond
  6629. ((stringp type) type)
  6630. ((eq type 'tags) "Tags query")
  6631. ((eq type 'todo) "TODO keyword")
  6632. ((eq type 'tags-tree) "Tags tree")
  6633. ((eq type 'todo-tree) "TODO kwd tree")
  6634. ((eq type 'occur-tree) "Occur tree")
  6635. (t "???"))
  6636. (if (stringp match)
  6637. (org-add-props match nil 'face 'org-warning)
  6638. (format "set of %d commands" (+ -2 (length entry)))))))
  6639. (if restrict-ok
  6640. (insert "\n"
  6641. (org-add-props "1 Restrict call to current buffer 0 Restrict call to region or subtree" nil 'face 'org-table)))
  6642. (goto-char (point-min))
  6643. (if (fboundp 'fit-window-to-buffer) (fit-window-to-buffer))
  6644. (message "Press key for agenda command%s"
  6645. (if restrict-ok ", or [1] or [0] to restrict" ""))
  6646. (setq c (read-char-exclusive))
  6647. (message "")
  6648. (when (memq c '(?L ?1 ?0))
  6649. (if restrict-ok
  6650. (put 'org-agenda-files 'org-restrict (list bfn))
  6651. (error "Cannot restrict agenda to current buffer"))
  6652. (with-current-buffer " *Agenda Commands*"
  6653. (goto-char (point-max))
  6654. (delete-region (point-at-bol) (point))
  6655. (goto-char (point-min)))
  6656. (when (eq c ?0)
  6657. (setq org-agenda-restrict t)
  6658. (with-current-buffer buf
  6659. (if (org-region-active-p)
  6660. (progn
  6661. (move-marker org-agenda-restrict-begin (region-beginning))
  6662. (move-marker org-agenda-restrict-end (region-end)))
  6663. (save-excursion
  6664. (org-back-to-heading t)
  6665. (move-marker org-agenda-restrict-begin (point))
  6666. (move-marker org-agenda-restrict-end
  6667. (progn (org-end-of-subtree t)))))))
  6668. (unless (eq c ?L)
  6669. (message "Press key for agenda command%s"
  6670. (if restrict-ok " (restricted to current file)" ""))
  6671. (setq c (read-char-exclusive)))
  6672. (message "")))
  6673. (require 'calendar) ; FIXME: can we avoid this for some commands?
  6674. ;; For example the todo list should not need it (but does...)
  6675. (cond
  6676. ((equal c ?C) (customize-variable 'org-agenda-custom-commands))
  6677. ((equal c ?a) (call-interactively 'org-agenda-list))
  6678. ((equal c ?t) (call-interactively 'org-todo-list))
  6679. ((equal c ?T) (org-call-with-arg 'org-todo-list (or arg '(4))))
  6680. ((equal c ?m) (call-interactively 'org-tags-view))
  6681. ((equal c ?M) (org-call-with-arg 'org-tags-view (or arg '(4))))
  6682. ((equal c ?L)
  6683. (unless restrict-ok
  6684. (error "This is not an Org-mode file"))
  6685. (org-call-with-arg 'org-timeline arg))
  6686. ((setq entry (assoc (char-to-string c) org-agenda-custom-commands))
  6687. (if (symbolp (nth 1 entry))
  6688. (progn
  6689. (setq type (nth 1 entry) match (nth 2 entry) lprops (nth 3 entry)
  6690. lprops (nth 3 entry))
  6691. (cond
  6692. ((eq type 'tags)
  6693. (org-let lprops '(org-tags-view current-prefix-arg match)))
  6694. ((eq type 'tags-todo)
  6695. (org-let lprops '(org-tags-view '(4) match)))
  6696. ((eq type 'todo)
  6697. (org-let lprops '(org-todo-list match)))
  6698. ((eq type 'tags-tree)
  6699. (org-check-for-org-mode)
  6700. (org-let lprops '(org-tags-sparse-tree current-prefix-arg match)))
  6701. ((eq type 'todo-tree)
  6702. (org-check-for-org-mode)
  6703. (org-let lprops
  6704. '(org-occur (concat "^" outline-regexp "[ \t]*"
  6705. (regexp-quote match) "\\>"))))
  6706. ((eq type 'occur-tree)
  6707. (org-check-for-org-mode)
  6708. (org-let lprops '(org-occur match)))
  6709. (t (error "Invalid custom agenda command type %s" type))))
  6710. (org-run-agenda-series (cddr entry))))
  6711. (t (error "Invalid key"))))))
  6712. ;; FIXME: what is the meaning of WINDOW?????
  6713. (defun org-run-agenda-series (series &optional window)
  6714. (org-prepare-agenda)
  6715. (let* ((org-agenda-multi t)
  6716. (redo (list 'org-run-agenda-series (list 'quote series)))
  6717. (org-select-agenda-window t)
  6718. (cmds (car series))
  6719. (gprops (nth 1 series))
  6720. match ;; The byte compiler incorrectly complains about this. Keep it!
  6721. cmd type lprops)
  6722. (while (setq cmd (pop cmds))
  6723. (setq type (car cmd) match (nth 1 cmd) lprops (nth 2 cmd))
  6724. (cond
  6725. ((eq type 'agenda)
  6726. (call-interactively 'org-agenda-list))
  6727. ((eq type 'alltodo)
  6728. (call-interactively 'org-todo-list))
  6729. ((eq type 'tags)
  6730. (org-let2 gprops lprops
  6731. '(org-tags-view current-prefix-arg match)))
  6732. ((eq type 'tags-todo)
  6733. (org-let2 gprops lprops
  6734. '(org-tags-view '(4) match)))
  6735. ((eq type 'todo)
  6736. (org-let2 gprops lprops
  6737. '(org-todo-list match)))
  6738. (t (error "Invalid type in command series"))))
  6739. (widen)
  6740. (setq org-agenda-redo-command redo)
  6741. (goto-char (point-min)))
  6742. (org-finalize-agenda))
  6743. ;;;###autoload
  6744. (defmacro org-batch-agenda (cmd-key &rest parameters)
  6745. "Run an agenda command in batch mode, send result to STDOUT.
  6746. CMD-KEY is a string that is also a key in `org-agenda-custom-commands'.
  6747. Paramters are alternating variable names and values that will be bound
  6748. before running the agenda command."
  6749. (let (pars)
  6750. (while parameters
  6751. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  6752. (flet ((read-char-exclusive () (string-to-char cmd-key)))
  6753. (eval (list 'let (nreverse pars) '(org-agenda nil))))
  6754. (set-buffer "*Org Agenda*")
  6755. (princ (buffer-string))))
  6756. (defmacro org-no-read-only (&rest body)
  6757. "Inhibit read-only for BODY."
  6758. `(let ((inhibit-read-only t)) ,@body))
  6759. (defun org-check-for-org-mode ()
  6760. "Make sure current buffer is in org-mode. Error if not."
  6761. (or (org-mode-p)
  6762. (error "Cannot execute org-mode agenda command on buffer in %s."
  6763. major-mode)))
  6764. (defun org-fit-agenda-window ()
  6765. "Fit the window to the buffer size."
  6766. (and org-fit-agenda-window
  6767. (memq org-agenda-window-setup '(reorganize-frame))
  6768. (fboundp 'fit-window-to-buffer)
  6769. (fit-window-to-buffer nil (/ (* (frame-height) 3) 4)
  6770. (/ (frame-height) 2))))
  6771. (defun org-agenda-files (&optional unrestricted)
  6772. "Get the list of agenda files.
  6773. Optional UNRESTRICTED means return the full list even if a restriction
  6774. is currently in place."
  6775. (cond
  6776. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  6777. ((stringp org-agenda-files) (org-read-agenda-file-list))
  6778. ((listp org-agenda-files) org-agenda-files)
  6779. (t (error "Invalid value of `org-agenda-files'"))))
  6780. (defvar org-window-configuration)
  6781. (defun org-edit-agenda-file-list ()
  6782. "Edit the list of agenda files.
  6783. Depending on setup, this either uses customize to edit the variable
  6784. `org-agenda-files', or it visits the file that is holding the list. In the
  6785. latter case, the buffer is set up in a way that saving it automatically kills
  6786. the buffer and restores the previous window configuration."
  6787. (interactive)
  6788. (if (stringp org-agenda-files)
  6789. (let ((cw (current-window-configuration)))
  6790. (find-file org-agenda-files)
  6791. (org-set-local 'org-window-configuration cw)
  6792. (org-add-hook 'after-save-hook
  6793. (lambda ()
  6794. (set-window-configuration
  6795. (prog1 org-window-configuration
  6796. (kill-buffer (current-buffer))))
  6797. (org-install-agenda-files-menu)
  6798. (message "New agenda file list installed"))
  6799. nil 'local)
  6800. (message (substitute-command-keys
  6801. "Edit list and finish with \\[save-buffer]")))
  6802. (customize-variable 'org-agenda-files)))
  6803. (defun org-store-new-agenda-file-list (list)
  6804. "Set new value for the agenda file list and save it correcly."
  6805. (if (stringp org-agenda-files)
  6806. (let ((f org-agenda-files) b)
  6807. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  6808. (with-temp-file f
  6809. (insert (mapconcat 'identity list "\n") "\n")))
  6810. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  6811. (setq org-agenda-files list)
  6812. (customize-save-variable 'org-agenda-files org-agenda-files))))
  6813. (defun org-read-agenda-file-list ()
  6814. "Read the list of agenda files from a file."
  6815. (when (stringp org-agenda-files)
  6816. (with-temp-buffer
  6817. (insert-file-contents org-agenda-files)
  6818. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  6819. (defvar org-agenda-markers nil
  6820. "List of all currently active markers created by `org-agenda'.")
  6821. (defvar org-agenda-last-marker-time (time-to-seconds (current-time))
  6822. "Creation time of the last agenda marker.")
  6823. (defun org-agenda-new-marker (&optional pos)
  6824. "Return a new agenda marker.
  6825. Org-mode keeps a list of these markers and resets them when they are
  6826. no longer in use."
  6827. (let ((m (copy-marker (or pos (point)))))
  6828. (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
  6829. (push m org-agenda-markers)
  6830. m))
  6831. (defun org-agenda-maybe-reset-markers (&optional force)
  6832. "Reset markers created by `org-agenda'. But only if they are old enough."
  6833. (if (or (and force (not org-agenda-multi))
  6834. (> (- (time-to-seconds (current-time))
  6835. org-agenda-last-marker-time)
  6836. 5))
  6837. (while org-agenda-markers
  6838. (move-marker (pop org-agenda-markers) nil))))
  6839. (defvar org-agenda-new-buffers nil
  6840. "Buffers created to visit agenda files.")
  6841. (defun org-get-agenda-file-buffer (file)
  6842. "Get a buffer visiting FILE. If the buffer needs to be created, add
  6843. it to the list of buffers which might be released later."
  6844. (let ((buf (find-buffer-visiting file)))
  6845. (if buf
  6846. buf ; just return it
  6847. ;; Make a new buffer and remember it
  6848. (setq buf (find-file-noselect file))
  6849. (if buf (push buf org-agenda-new-buffers))
  6850. buf)))
  6851. (defun org-release-buffers (blist)
  6852. "Release all buffers in list, asking the user for confirmation when needed.
  6853. When a buffer is unmodified, it is just killed. When modified, it is saved
  6854. \(if the user agrees) and then killed."
  6855. (let (buf file)
  6856. (while (setq buf (pop blist))
  6857. (setq file (buffer-file-name buf))
  6858. (when (and (buffer-modified-p buf)
  6859. file
  6860. (y-or-n-p (format "Save file %s? " file)))
  6861. (with-current-buffer buf (save-buffer)))
  6862. (kill-buffer buf))))
  6863. (defun org-timeline (&optional include-all)
  6864. "Show a time-sorted view of the entries in the current org file.
  6865. Only entries with a time stamp of today or later will be listed. With
  6866. \\[universal-argument] prefix, all unfinished TODO items will also be shown,
  6867. under the current date.
  6868. If the buffer contains an active region, only check the region for
  6869. dates."
  6870. (interactive "P")
  6871. (require 'calendar)
  6872. (org-compile-prefix-format 'timeline)
  6873. (org-set-sorting-strategy 'timeline)
  6874. (let* ((dopast t)
  6875. (dotodo include-all)
  6876. (doclosed org-agenda-show-log)
  6877. (entry buffer-file-name)
  6878. (date (calendar-current-date))
  6879. (win (selected-window))
  6880. (pos1 (point))
  6881. (beg (if (org-region-active-p) (region-beginning) (point-min)))
  6882. (end (if (org-region-active-p) (region-end) (point-max)))
  6883. (day-numbers (org-get-all-dates beg end 'no-ranges
  6884. t doclosed ; always include today
  6885. org-timeline-show-empty-dates))
  6886. (today (time-to-days (current-time)))
  6887. (past t)
  6888. args
  6889. s e rtn d emptyp)
  6890. (setq org-agenda-redo-command
  6891. (list 'progn
  6892. (list 'switch-to-buffer-other-window (current-buffer))
  6893. (list 'org-timeline (list 'quote include-all))))
  6894. (if (not dopast)
  6895. ;; Remove past dates from the list of dates.
  6896. (setq day-numbers (delq nil (mapcar (lambda(x)
  6897. (if (>= x today) x nil))
  6898. day-numbers))))
  6899. (org-prepare-agenda)
  6900. (if doclosed (push :closed args))
  6901. (push :timestamp args)
  6902. (if dotodo (push :todo args))
  6903. (while (setq d (pop day-numbers))
  6904. (if (and (listp d) (eq (car d) :omitted))
  6905. (progn
  6906. (setq s (point))
  6907. (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
  6908. (put-text-property s (1- (point)) 'face 'org-level-3))
  6909. (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
  6910. (if (and (>= d today)
  6911. dopast
  6912. past)
  6913. (progn
  6914. (setq past nil)
  6915. (insert (make-string 79 ?-) "\n")))
  6916. (setq date (calendar-gregorian-from-absolute d))
  6917. (setq s (point))
  6918. (setq rtn (and (not emptyp)
  6919. (apply 'org-agenda-get-day-entries
  6920. entry date args)))
  6921. (if (or rtn (equal d today) org-timeline-show-empty-dates)
  6922. (progn
  6923. (insert (calendar-day-name date) " "
  6924. (number-to-string (extract-calendar-day date)) " "
  6925. (calendar-month-name (extract-calendar-month date)) " "
  6926. (number-to-string (extract-calendar-year date)) "\n")
  6927. (put-text-property s (1- (point)) 'face
  6928. 'org-level-3)
  6929. (if (equal d today)
  6930. (put-text-property s (1- (point)) 'org-today t))
  6931. (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
  6932. (put-text-property s (1- (point)) 'day d)))))
  6933. (goto-char (point-min))
  6934. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  6935. (point-min)))
  6936. (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
  6937. (org-finalize-agenda)
  6938. (setq buffer-read-only t)
  6939. (when (not org-select-agenda-window)
  6940. (select-window win)
  6941. (goto-char pos1))))
  6942. (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
  6943. (defvar org-agenda-last-arguments nil
  6944. "The arguments of the previous call to org-agenda")
  6945. ;;;###autoload
  6946. (defun org-agenda-list (&optional include-all start-day ndays)
  6947. "Produce a weekly view from all files in variable `org-agenda-files'.
  6948. The view will be for the current week, but from the overview buffer you
  6949. will be able to go to other weeks.
  6950. With one \\[universal-argument] prefix argument INCLUDE-ALL, all unfinished TODO items will
  6951. also be shown, under the current date.
  6952. With two \\[universal-argument] prefix argument INCLUDE-ALL, all TODO entries marked DONE
  6953. on the days are also shown. See the variable `org-log-done' for how
  6954. to turn on logging.
  6955. START-DAY defaults to TODAY, or to the most recent match for the weekday
  6956. given in `org-agenda-start-on-weekday'.
  6957. NDAYS defaults to `org-agenda-ndays'."
  6958. (interactive "P")
  6959. (if org-agenda-overriding-arguments
  6960. (setq include-all (car org-agenda-overriding-arguments)
  6961. start-day (nth 1 org-agenda-overriding-arguments)
  6962. ndays (nth 2 org-agenda-overriding-arguments)))
  6963. (setq org-agenda-last-arguments (list include-all start-day ndays))
  6964. (org-compile-prefix-format 'agenda)
  6965. (org-set-sorting-strategy 'agenda)
  6966. (require 'calendar)
  6967. (let* ((org-agenda-start-on-weekday
  6968. (if (or (equal ndays 1)
  6969. (and (null ndays) (equal 1 org-agenda-ndays)))
  6970. nil org-agenda-start-on-weekday))
  6971. (thefiles (org-agenda-files))
  6972. (files thefiles)
  6973. (win (selected-window))
  6974. (today (time-to-days (current-time)))
  6975. (sd (or start-day today))
  6976. (start (if (or (null org-agenda-start-on-weekday)
  6977. (< org-agenda-ndays 7))
  6978. sd
  6979. (let* ((nt (calendar-day-of-week
  6980. (calendar-gregorian-from-absolute sd)))
  6981. (n1 org-agenda-start-on-weekday)
  6982. (d (- nt n1)))
  6983. (- sd (+ (if (< d 0) 7 0) d)))))
  6984. (day-numbers (list start))
  6985. (inhibit-redisplay t)
  6986. s e rtn rtnall file date d start-pos end-pos todayp nd)
  6987. (setq org-agenda-redo-command
  6988. (list 'org-agenda-list (list 'quote include-all) start-day ndays))
  6989. ;; Make the list of days
  6990. (setq ndays (or ndays org-agenda-ndays)
  6991. nd ndays)
  6992. (while (> ndays 1)
  6993. (push (1+ (car day-numbers)) day-numbers)
  6994. (setq ndays (1- ndays)))
  6995. (setq day-numbers (nreverse day-numbers))
  6996. (org-prepare-agenda)
  6997. (org-set-local 'starting-day (car day-numbers))
  6998. (org-set-local 'include-all-loc include-all)
  6999. (when (and (or include-all org-agenda-include-all-todo)
  7000. (member today day-numbers))
  7001. (setq files thefiles
  7002. rtnall nil)
  7003. (while (setq file (pop files))
  7004. (catch 'nextfile
  7005. (org-check-agenda-file file)
  7006. (setq date (calendar-gregorian-from-absolute today)
  7007. rtn (org-agenda-get-day-entries
  7008. file date :todo))
  7009. (setq rtnall (append rtnall rtn))))
  7010. (when rtnall
  7011. (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
  7012. (add-text-properties (point-min) (1- (point))
  7013. (list 'face 'org-level-3))
  7014. (insert (org-finalize-agenda-entries rtnall) "\n")))
  7015. (setq s (point))
  7016. (insert (if (= nd 7) "Week-" "Day-") "agenda:\n")
  7017. (add-text-properties s (1- (point)) (list 'face 'org-level-3))
  7018. (while (setq d (pop day-numbers))
  7019. (setq date (calendar-gregorian-from-absolute d)
  7020. s (point))
  7021. (if (or (setq todayp (= d today))
  7022. (and (not start-pos) (= d sd)))
  7023. (setq start-pos (point))
  7024. (if (and start-pos (not end-pos))
  7025. (setq end-pos (point))))
  7026. (setq files thefiles
  7027. rtnall nil)
  7028. (while (setq file (pop files))
  7029. (catch 'nextfile
  7030. (org-check-agenda-file file)
  7031. (if org-agenda-show-log
  7032. (setq rtn (org-agenda-get-day-entries
  7033. file date
  7034. :deadline :scheduled :timestamp :closed))
  7035. (setq rtn (org-agenda-get-day-entries
  7036. file date
  7037. :deadline :scheduled :timestamp)))
  7038. (setq rtnall (append rtnall rtn))))
  7039. (if org-agenda-include-diary
  7040. (progn
  7041. (require 'diary-lib)
  7042. (setq rtn (org-get-entries-from-diary date))
  7043. (setq rtnall (append rtnall rtn))))
  7044. (if (or rtnall org-agenda-show-all-dates)
  7045. (progn
  7046. (insert (format "%-9s %2d %s %4d\n"
  7047. (calendar-day-name date)
  7048. (extract-calendar-day date)
  7049. (calendar-month-name (extract-calendar-month date))
  7050. (extract-calendar-year date)))
  7051. (put-text-property s (1- (point)) 'face
  7052. 'org-level-3)
  7053. (if todayp (put-text-property s (1- (point)) 'org-today t))
  7054. (if rtnall (insert
  7055. (org-finalize-agenda-entries
  7056. (org-agenda-add-time-grid-maybe
  7057. rtnall nd todayp))
  7058. "\n"))
  7059. (put-text-property s (1- (point)) 'day d))))
  7060. (goto-char (point-min))
  7061. (org-fit-agenda-window)
  7062. (unless (and (pos-visible-in-window-p (point-min))
  7063. (pos-visible-in-window-p (point-max)))
  7064. (goto-char (1- (point-max)))
  7065. (recenter -1)
  7066. (if (not (pos-visible-in-window-p (or start-pos 1)))
  7067. (progn
  7068. (goto-char (or start-pos 1))
  7069. (recenter 1))))
  7070. (goto-char (or start-pos 1))
  7071. (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
  7072. (org-finalize-agenda)
  7073. (setq buffer-read-only t)
  7074. (if (not org-select-agenda-window) (select-window win))
  7075. (message "")))
  7076. (defvar org-select-this-todo-keyword nil)
  7077. ;;;###autoload
  7078. (defun org-todo-list (arg)
  7079. "Show all TODO entries from all agenda file in a single list.
  7080. The prefix arg can be used to select a specific TODO keyword and limit
  7081. the list to these. When using \\[universal-argument], you will be prompted
  7082. for a keyword. A numeric prefix directly selects the Nth keyword in
  7083. `org-todo-keywords'."
  7084. (interactive "P")
  7085. (require 'calendar)
  7086. (org-compile-prefix-format 'todo)
  7087. (org-set-sorting-strategy 'todo)
  7088. (let* ((today (time-to-days (current-time)))
  7089. (date (calendar-gregorian-from-absolute today))
  7090. (win (selected-window))
  7091. (kwds org-todo-keywords)
  7092. (completion-ignore-case t)
  7093. (org-select-this-todo-keyword
  7094. (if (stringp arg) arg
  7095. (and arg (integerp arg) (> arg 0)
  7096. (nth (1- arg) org-todo-keywords))))
  7097. rtn rtnall files file pos)
  7098. (when (equal arg '(4))
  7099. (setq org-select-this-todo-keyword
  7100. (completing-read "Keyword: " (mapcar 'list org-todo-keywords)
  7101. nil t)))
  7102. (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
  7103. (org-prepare-agenda)
  7104. (org-set-local 'last-arg arg)
  7105. (org-set-local 'org-todo-keywords kwds)
  7106. (setq org-agenda-redo-command
  7107. '(org-todo-list (or current-prefix-arg last-arg)))
  7108. (setq files (org-agenda-files)
  7109. rtnall nil)
  7110. (while (setq file (pop files))
  7111. (catch 'nextfile
  7112. (org-check-agenda-file file)
  7113. (setq rtn (org-agenda-get-day-entries file date :todo))
  7114. (setq rtnall (append rtnall rtn))))
  7115. (insert "Global list of TODO items of type: ")
  7116. (add-text-properties (point-min) (1- (point))
  7117. (list 'face 'org-level-3))
  7118. (setq pos (point))
  7119. (insert (or org-select-this-todo-keyword "ALL") "\n")
  7120. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  7121. (setq pos (point))
  7122. (unless org-agenda-multi
  7123. (insert
  7124. "Available with `N r': (0)ALL "
  7125. (let ((n 0))
  7126. (mapconcat (lambda (x)
  7127. (format "(%d)%s" (setq n (1+ n)) x))
  7128. org-todo-keywords " "))
  7129. "\n"))
  7130. (add-text-properties pos (1- (point)) (list 'face 'org-level-3))
  7131. (when rtnall
  7132. (insert (org-finalize-agenda-entries rtnall) "\n"))
  7133. (goto-char (point-min))
  7134. (org-fit-agenda-window)
  7135. (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
  7136. (org-finalize-agenda)
  7137. (setq buffer-read-only t)
  7138. (if (not org-select-agenda-window) (select-window win))))
  7139. (defun org-check-agenda-file (file)
  7140. "Make sure FILE exists. If not, ask user what to do."
  7141. (when (not (file-exists-p file))
  7142. (message "non-existent file %s. [R]emove from list or [A]bort?"
  7143. (abbreviate-file-name file))
  7144. (let ((r (downcase (read-char-exclusive))))
  7145. (cond
  7146. ((equal r ?r)
  7147. (org-remove-file file)
  7148. (throw 'nextfile t))
  7149. (t (error "Abort"))))))
  7150. (defun org-agenda-check-type (error &rest types)
  7151. "Check if agenda buffer is of allowed type.
  7152. If ERROR is non-nil, throw an error, otherwise just return nil."
  7153. (if (memq org-agenda-type types)
  7154. t
  7155. (if error
  7156. (error "Not allowed in %s-type agenda buffers" org-agenda-type)
  7157. nil)))
  7158. (defun org-agenda-quit ()
  7159. "Exit agenda by removing the window or the buffer."
  7160. (interactive)
  7161. (let ((buf (current-buffer)))
  7162. (if (not (one-window-p)) (delete-window))
  7163. (kill-buffer buf)
  7164. (org-agenda-maybe-reset-markers 'force))
  7165. ;; Maybe restore the pre-agenda window configuration.
  7166. (and org-agenda-restore-windows-after-quit
  7167. (not (eq org-agenda-window-setup 'other-frame))
  7168. org-pre-agenda-window-conf
  7169. (set-window-configuration org-pre-agenda-window-conf)))
  7170. (defun org-agenda-exit ()
  7171. "Exit agenda by removing the window or the buffer.
  7172. Also kill all Org-mode buffers which have been loaded by `org-agenda'.
  7173. Org-mode buffers visited directly by the user will not be touched."
  7174. (interactive)
  7175. (org-release-buffers org-agenda-new-buffers)
  7176. (setq org-agenda-new-buffers nil)
  7177. (org-agenda-quit))
  7178. (defun org-save-all-org-buffers ()
  7179. "Save all Org-mode buffers without user confirmation."
  7180. (interactive)
  7181. (message "Saving all Org-mode buffers...")
  7182. (save-some-buffers t 'org-mode-p)
  7183. (message "Saving all Org-mode buffers... done"))
  7184. (defun org-agenda-redo ()
  7185. "Rebuild Agenda.
  7186. When this is the global TODO list, a prefix argument will be interpreted."
  7187. (interactive)
  7188. (let* ((org-agenda-keep-modes t)
  7189. (line (org-current-line))
  7190. (window-line (- line (org-current-line (window-start)))))
  7191. (message "Rebuilding agenda buffer...")
  7192. (eval org-agenda-redo-command)
  7193. (message "Rebuilding agenda buffer...done")
  7194. (goto-line line)
  7195. (recenter window-line)))
  7196. (defun org-agenda-goto-today ()
  7197. "Go to today."
  7198. (interactive)
  7199. (org-agenda-check-type t 'timeline 'agenda)
  7200. (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
  7201. (cond
  7202. (tdpos (goto-char tdpos))
  7203. ((eq org-agenda-type 'agenda)
  7204. (let ((org-agenda-overriding-arguments org-agenda-last-arguments))
  7205. (setf (nth 1 org-agenda-overriding-arguments) nil)
  7206. (org-agenda-redo)
  7207. (org-agenda-find-today-or-agenda)))
  7208. (t (error "Cannot find today")))))
  7209. (defun org-agenda-find-today-or-agenda ()
  7210. (goto-char
  7211. (or (text-property-any (point-min) (point-max) 'org-today t)
  7212. (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
  7213. (point-min))))
  7214. (defun org-agenda-later (arg)
  7215. "Go forward in time by `org-agenda-ndays' days.
  7216. With prefix ARG, go forward that many times `org-agenda-ndays'."
  7217. (interactive "p")
  7218. (org-agenda-check-type t 'agenda)
  7219. (let ((org-agenda-overriding-arguments
  7220. (list (car org-agenda-last-arguments)
  7221. (+ starting-day (* arg org-agenda-ndays))
  7222. nil t)))
  7223. (org-agenda-redo)
  7224. (org-agenda-find-today-or-agenda)))
  7225. (defun org-agenda-earlier (arg)
  7226. "Go back in time by `org-agenda-ndays' days.
  7227. With prefix ARG, go back that many times `org-agenda-ndays'."
  7228. (interactive "p")
  7229. (org-agenda-check-type t 'agenda)
  7230. (let ((org-agenda-overriding-arguments
  7231. (list (car org-agenda-last-arguments)
  7232. (- starting-day (* arg org-agenda-ndays))
  7233. nil t)))
  7234. (org-agenda-redo)
  7235. (org-agenda-find-today-or-agenda)))
  7236. (defun org-agenda-week-view ()
  7237. "Switch to weekly view for agenda."
  7238. (interactive)
  7239. (org-agenda-check-type t 'agenda)
  7240. (if (= org-agenda-ndays 7)
  7241. (error "This is already the week view"))
  7242. (setq org-agenda-ndays 7)
  7243. (let ((org-agenda-overriding-arguments
  7244. (list (car org-agenda-last-arguments)
  7245. (or (get-text-property (point) 'day)
  7246. starting-day)
  7247. nil t)))
  7248. (org-agenda-redo)
  7249. (org-agenda-find-today-or-agenda))
  7250. (org-agenda-set-mode-name)
  7251. (message "Switched to week view"))
  7252. (defun org-agenda-day-view ()
  7253. "Switch to daily view for agenda."
  7254. (interactive)
  7255. (org-agenda-check-type t 'agenda)
  7256. (if (= org-agenda-ndays 1)
  7257. (error "This is already the day view"))
  7258. (setq org-agenda-ndays 1)
  7259. (let ((org-agenda-overriding-arguments
  7260. (list (car org-agenda-last-arguments)
  7261. (or (get-text-property (point) 'day)
  7262. starting-day)
  7263. nil t)))
  7264. (org-agenda-redo)
  7265. (org-agenda-find-today-or-agenda))
  7266. (org-agenda-set-mode-name)
  7267. (message "Switched to day view"))
  7268. (defun org-agenda-next-date-line (&optional arg)
  7269. "Jump to the next line indicating a date in agenda buffer."
  7270. (interactive "p")
  7271. (org-agenda-check-type t 'agenda 'timeline)
  7272. (beginning-of-line 1)
  7273. (if (looking-at "^\\S-") (forward-char 1))
  7274. (if (not (re-search-forward "^\\S-" nil t arg))
  7275. (progn
  7276. (backward-char 1)
  7277. (error "No next date after this line in this buffer")))
  7278. (goto-char (match-beginning 0)))
  7279. (defun org-agenda-previous-date-line (&optional arg)
  7280. "Jump to the previous line indicating a date in agenda buffer."
  7281. (interactive "p")
  7282. (org-agenda-check-type t 'agenda 'timeline)
  7283. (beginning-of-line 1)
  7284. (if (not (re-search-backward "^\\S-" nil t arg))
  7285. (error "No previous date before this line in this buffer")))
  7286. ;; Initialize the highlight
  7287. (defvar org-hl (org-make-overlay 1 1))
  7288. (org-overlay-put org-hl 'face 'highlight)
  7289. (defun org-highlight (begin end &optional buffer)
  7290. "Highlight a region with overlay."
  7291. (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
  7292. org-hl begin end (or buffer (current-buffer))))
  7293. (defun org-unhighlight ()
  7294. "Detach overlay INDEX."
  7295. (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
  7296. (defun org-agenda-follow-mode ()
  7297. "Toggle follow mode in an agenda buffer."
  7298. (interactive)
  7299. (setq org-agenda-follow-mode (not org-agenda-follow-mode))
  7300. (org-agenda-set-mode-name)
  7301. (message "Follow mode is %s"
  7302. (if org-agenda-follow-mode "on" "off")))
  7303. (defun org-agenda-log-mode ()
  7304. "Toggle log mode in an agenda buffer."
  7305. (interactive)
  7306. (org-agenda-check-type t 'agenda 'timeline)
  7307. (setq org-agenda-show-log (not org-agenda-show-log))
  7308. (org-agenda-set-mode-name)
  7309. (org-agenda-redo)
  7310. (message "Log mode is %s"
  7311. (if org-agenda-show-log "on" "off")))
  7312. (defun org-agenda-toggle-diary ()
  7313. "Toggle diary inclusion in an agenda buffer."
  7314. (interactive)
  7315. (org-agenda-check-type t 'agenda)
  7316. (setq org-agenda-include-diary (not org-agenda-include-diary))
  7317. (org-agenda-redo)
  7318. (org-agenda-set-mode-name)
  7319. (message "Diary inclusion turned %s"
  7320. (if org-agenda-include-diary "on" "off")))
  7321. (defun org-agenda-toggle-time-grid ()
  7322. "Toggle time grid in an agenda buffer."
  7323. (interactive)
  7324. (org-agenda-check-type t 'agenda)
  7325. (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
  7326. (org-agenda-redo)
  7327. (org-agenda-set-mode-name)
  7328. (message "Time-grid turned %s"
  7329. (if org-agenda-use-time-grid "on" "off")))
  7330. (defun org-agenda-set-mode-name ()
  7331. "Set the mode name to indicate all the small mode settings."
  7332. (setq mode-name
  7333. (concat "Org-Agenda"
  7334. (if (equal org-agenda-ndays 1) " Day" "")
  7335. (if (equal org-agenda-ndays 7) " Week" "")
  7336. (if org-agenda-follow-mode " Follow" "")
  7337. (if org-agenda-include-diary " Diary" "")
  7338. (if org-agenda-use-time-grid " Grid" "")
  7339. (if org-agenda-show-log " Log" "")))
  7340. (force-mode-line-update))
  7341. (defun org-agenda-post-command-hook ()
  7342. (and (eolp) (not (bolp)) (backward-char 1))
  7343. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  7344. (if (and org-agenda-follow-mode
  7345. (get-text-property (point) 'org-marker))
  7346. (org-agenda-show)))
  7347. (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
  7348. (defun org-get-entries-from-diary (date)
  7349. "Get the (Emacs Calendar) diary entries for DATE."
  7350. (let* ((fancy-diary-buffer "*temporary-fancy-diary-buffer*")
  7351. (diary-display-hook '(fancy-diary-display))
  7352. (list-diary-entries-hook
  7353. (cons 'org-diary-default-entry list-diary-entries-hook))
  7354. (diary-file-name-prefix-function nil) ; turn this feature off
  7355. (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
  7356. entries
  7357. (org-disable-agenda-to-diary t))
  7358. (save-excursion
  7359. (save-window-excursion
  7360. (list-diary-entries date 1))) ;; Keep this name for now, compatibility
  7361. (if (not (get-buffer fancy-diary-buffer))
  7362. (setq entries nil)
  7363. (with-current-buffer fancy-diary-buffer
  7364. (setq buffer-read-only nil)
  7365. (if (= (point-max) 1)
  7366. ;; No entries
  7367. (setq entries nil)
  7368. ;; Omit the date and other unnecessary stuff
  7369. (org-agenda-cleanup-fancy-diary)
  7370. ;; Add prefix to each line and extend the text properties
  7371. (if (= (point-max) 1)
  7372. (setq entries nil)
  7373. (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
  7374. (set-buffer-modified-p nil)
  7375. (kill-buffer fancy-diary-buffer)))
  7376. (when entries
  7377. (setq entries (org-split-string entries "\n"))
  7378. (setq entries
  7379. (mapcar
  7380. (lambda (x)
  7381. (setq x (org-format-agenda-item "" x "Diary" nil 'time))
  7382. ;; Extend the text properties to the beginning of the line
  7383. (org-add-props x (text-properties-at (1- (length x)) x)))
  7384. entries)))))
  7385. (defun org-agenda-cleanup-fancy-diary ()
  7386. "Remove unwanted stuff in buffer created by `fancy-diary-display'.
  7387. This gets rid of the date, the underline under the date, and
  7388. the dummy entry installed by `org-mode' to ensure non-empty diary for each
  7389. date. It also removes lines that contain only whitespace."
  7390. (goto-char (point-min))
  7391. (if (looking-at ".*?:[ \t]*")
  7392. (progn
  7393. (replace-match "")
  7394. (re-search-forward "\n=+$" nil t)
  7395. (replace-match "")
  7396. (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
  7397. (re-search-forward "\n=+$" nil t)
  7398. (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
  7399. (goto-char (point-min))
  7400. (while (re-search-forward "^ +\n" nil t)
  7401. (replace-match ""))
  7402. (goto-char (point-min))
  7403. (if (re-search-forward "^Org-mode dummy\n?" nil t)
  7404. (replace-match "")))
  7405. ;; Make sure entries from the diary have the right text properties.
  7406. (eval-after-load "diary-lib"
  7407. '(if (boundp 'diary-modify-entry-list-string-function)
  7408. ;; We can rely on the hook, nothing to do
  7409. nil
  7410. ;; Hook not avaiable, must use advice to make this work
  7411. (defadvice add-to-diary-list (before org-mark-diary-entry activate)
  7412. "Make the position visible."
  7413. (if (and org-disable-agenda-to-diary ;; called from org-agenda
  7414. (stringp string)
  7415. buffer-file-name)
  7416. (setq string (org-modify-diary-entry-string string))))))
  7417. (defun org-modify-diary-entry-string (string)
  7418. "Add text properties to string, allowing org-mode to act on it."
  7419. (org-add-props string nil
  7420. 'mouse-face 'highlight
  7421. 'keymap org-agenda-keymap
  7422. 'help-echo (format "mouse-2 or RET jump to diary file %s"
  7423. (abbreviate-file-name buffer-file-name))
  7424. 'org-agenda-diary-link t
  7425. 'org-marker (org-agenda-new-marker (point-at-bol))))
  7426. (defun org-diary-default-entry ()
  7427. "Add a dummy entry to the diary.
  7428. Needed to avoid empty dates which mess up holiday display."
  7429. ;; Catch the error if dealing with the new add-to-diary-alist
  7430. (when org-disable-agenda-to-diary
  7431. (condition-case nil
  7432. (add-to-diary-list original-date "Org-mode dummy" "")
  7433. (error
  7434. (add-to-diary-list original-date "Org-mode dummy" "" nil)))))
  7435. ;;;###autoload
  7436. (defun org-cycle-agenda-files ()
  7437. "Cycle through the files in `org-agenda-files'.
  7438. If the current buffer visits an agenda file, find the next one in the list.
  7439. If the current buffer does not, find the first agenda file."
  7440. (interactive)
  7441. (let* ((fs (org-agenda-files t))
  7442. (files (append fs (list (car fs))))
  7443. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  7444. file)
  7445. (unless files (error "No agenda files"))
  7446. (catch 'exit
  7447. (while (setq file (pop files))
  7448. (if (equal (file-truename file) tcf)
  7449. (when (car files)
  7450. (find-file (car files))
  7451. (throw 'exit t))))
  7452. (find-file (car fs)))))
  7453. (defun org-agenda-file-to-end ()
  7454. "Move/add the current file to the end of the agenda file list.
  7455. If the file is not present in the list, it is appended to the list. If it is
  7456. present, it is moved there."
  7457. (interactive)
  7458. (org-agenda-file-to-front 'to-end))
  7459. (defun org-agenda-file-to-front (&optional to-end)
  7460. "Move/add the current file to the top of the agenda file list.
  7461. If the file is not present in the list, it is added to the front. If it is
  7462. present, it is moved there. With optional argument TO-END, add/move to the
  7463. end of the list."
  7464. (interactive "P")
  7465. (let ((file-alist (mapcar (lambda (x)
  7466. (cons (file-truename x) x))
  7467. (org-agenda-files t)))
  7468. (ctf (file-truename buffer-file-name))
  7469. x had)
  7470. (setq x (assoc ctf file-alist) had x)
  7471. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  7472. (if to-end
  7473. (setq file-alist (append (delq x file-alist) (list x)))
  7474. (setq file-alist (cons x (delq x file-alist))))
  7475. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  7476. (org-install-agenda-files-menu)
  7477. (message "File %s to %s of agenda file list"
  7478. (if had "moved" "added") (if to-end "end" "front"))))
  7479. (defun org-remove-file (&optional file)
  7480. "Remove current file from the list of files in variable `org-agenda-files'.
  7481. These are the files which are being checked for agenda entries.
  7482. Optional argument FILE means, use this file instead of the current."
  7483. (interactive)
  7484. (let* ((file (or file buffer-file-name))
  7485. (true-file (file-truename file))
  7486. (afile (abbreviate-file-name file))
  7487. (files (delq nil (mapcar
  7488. (lambda (x)
  7489. (if (equal true-file
  7490. (file-truename x))
  7491. nil x))
  7492. (org-agenda-files t)))))
  7493. (if (not (= (length files) (length (org-agenda-files t))))
  7494. (progn
  7495. (org-store-new-agenda-file-list files)
  7496. (org-install-agenda-files-menu)
  7497. (message "Removed file: %s" afile))
  7498. (message "File was not in list: %s" afile))))
  7499. (defun org-file-menu-entry (file)
  7500. (vector file (list 'find-file file) t))
  7501. (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty)
  7502. "Return a list of all relevant day numbers from BEG to END buffer positions.
  7503. If NO-RANGES is non-nil, include only the start and end dates of a range,
  7504. not every single day in the range. If FORCE-TODAY is non-nil, make
  7505. sure that TODAY is included in the list. If INACTIVE is non-nil, also
  7506. inactive time stamps (those in square brackets) are included.
  7507. When EMPTY is non-nil, also include days without any entries."
  7508. (let ((re (if inactive org-ts-regexp-both org-ts-regexp))
  7509. dates dates1 date day day1 day2 ts1 ts2)
  7510. (if force-today
  7511. (setq dates (list (time-to-days (current-time)))))
  7512. (save-excursion
  7513. (goto-char beg)
  7514. (while (re-search-forward re end t)
  7515. (setq day (time-to-days (org-time-string-to-time
  7516. (substring (match-string 1) 0 10))))
  7517. (or (memq day dates) (push day dates)))
  7518. (unless no-ranges
  7519. (goto-char beg)
  7520. (while (re-search-forward org-tr-regexp end t)
  7521. (setq ts1 (substring (match-string 1) 0 10)
  7522. ts2 (substring (match-string 2) 0 10)
  7523. day1 (time-to-days (org-time-string-to-time ts1))
  7524. day2 (time-to-days (org-time-string-to-time ts2)))
  7525. (while (< (setq day1 (1+ day1)) day2)
  7526. (or (memq day1 dates) (push day1 dates)))))
  7527. (setq dates (sort dates '<))
  7528. (when empty
  7529. (while (setq day (pop dates))
  7530. (setq day2 (car dates))
  7531. (push day dates1)
  7532. (when (and day2 empty)
  7533. (if (or (eq empty t)
  7534. (and (numberp empty) (<= (- day2 day) empty)))
  7535. (while (< (setq day (1+ day)) day2)
  7536. (push (list day) dates1))
  7537. (push (cons :omitted (- day2 day)) dates1))))
  7538. (setq dates (nreverse dates1)))
  7539. dates)))
  7540. ;;;###autoload
  7541. (defun org-diary (&rest args)
  7542. "Return diary information from org-files.
  7543. This function can be used in a \"sexp\" diary entry in the Emacs calendar.
  7544. It accesses org files and extracts information from those files to be
  7545. listed in the diary. The function accepts arguments specifying what
  7546. items should be listed. The following arguments are allowed:
  7547. :timestamp List the headlines of items containing a date stamp or
  7548. date range matching the selected date. Deadlines will
  7549. also be listed, on the expiration day.
  7550. :deadline List any deadlines past due, or due within
  7551. `org-deadline-warning-days'. The listing occurs only
  7552. in the diary for *today*, not at any other date. If
  7553. an entry is marked DONE, it is no longer listed.
  7554. :scheduled List all items which are scheduled for the given date.
  7555. The diary for *today* also contains items which were
  7556. scheduled earlier and are not yet marked DONE.
  7557. :todo List all TODO items from the org-file. This may be a
  7558. long list - so this is not turned on by default.
  7559. Like deadlines, these entries only show up in the
  7560. diary for *today*, not at any other date.
  7561. The call in the diary file should look like this:
  7562. &%%(org-diary) ~/path/to/some/orgfile.org
  7563. Use a separate line for each org file to check. Or, if you omit the file name,
  7564. all files listed in `org-agenda-files' will be checked automatically:
  7565. &%%(org-diary)
  7566. If you don't give any arguments (as in the example above), the default
  7567. arguments (:deadline :scheduled :timestamp) are used. So the example above may
  7568. also be written as
  7569. &%%(org-diary :deadline :timestamp :scheduled)
  7570. The function expects the lisp variables `entry' and `date' to be provided
  7571. by the caller, because this is how the calendar works. Don't use this
  7572. function from a program - use `org-agenda-get-day-entries' instead."
  7573. (org-agenda-maybe-reset-markers)
  7574. (org-compile-prefix-format 'agenda)
  7575. (org-set-sorting-strategy 'agenda)
  7576. (setq args (or args '(:deadline :scheduled :timestamp)))
  7577. (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
  7578. (list entry)
  7579. (org-agenda-files t)))
  7580. file rtn results)
  7581. ;; If this is called during org-agenda, don't return any entries to
  7582. ;; the calendar. Org Agenda will list these entries itself.
  7583. (if org-disable-agenda-to-diary (setq files nil))
  7584. (while (setq file (pop files))
  7585. (setq rtn (apply 'org-agenda-get-day-entries file date args))
  7586. (setq results (append results rtn)))
  7587. (if results
  7588. (concat (org-finalize-agenda-entries results) "\n"))))
  7589. (defvar org-category-table nil)
  7590. (defun org-get-category-table ()
  7591. "Get the table of categories and positions in current buffer."
  7592. (let (tbl)
  7593. (save-excursion
  7594. (goto-char (point-min))
  7595. (while (re-search-forward "\\(^\\|\r\\)#\\+CATEGORY:[ \t]*\\(.*\\)" nil t)
  7596. (push (cons (point) (org-trim (match-string 2))) tbl)))
  7597. tbl))
  7598. (defun org-get-category (&optional pos)
  7599. "Get the category applying to position POS."
  7600. (if (not org-category-table)
  7601. (cond
  7602. ((null org-category)
  7603. (setq org-category
  7604. (if buffer-file-name
  7605. (file-name-sans-extension
  7606. (file-name-nondirectory buffer-file-name))
  7607. "???")))
  7608. ((symbolp org-category) (symbol-name org-category))
  7609. (t org-category))
  7610. (let ((tbl org-category-table)
  7611. (pos (or pos (point))))
  7612. (while (and tbl (> (caar tbl) pos))
  7613. (pop tbl))
  7614. (or (cdar tbl) (cdr (nth (1- (length org-category-table))
  7615. org-category-table))))))
  7616. (defun org-agenda-get-day-entries (file date &rest args)
  7617. "Does the work for `org-diary' and `org-agenda'.
  7618. FILE is the path to a file to be checked for entries. DATE is date like
  7619. the one returned by `calendar-current-date'. ARGS are symbols indicating
  7620. which kind of entries should be extracted. For details about these, see
  7621. the documentation of `org-diary'."
  7622. (setq args (or args '(:deadline :scheduled :timestamp)))
  7623. (let* ((org-startup-with-deadline-check nil)
  7624. (org-startup-folded nil)
  7625. (org-startup-align-all-tables nil)
  7626. (buffer (if (file-exists-p file)
  7627. (org-get-agenda-file-buffer file)
  7628. (error "No such file %s" file)))
  7629. arg results rtn)
  7630. (if (not buffer)
  7631. ;; If file does not exist, make sure an error message ends up in diary
  7632. (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  7633. (with-current-buffer buffer
  7634. (unless (org-mode-p)
  7635. (error "Agenda file %s is not in `org-mode'" file))
  7636. (setq org-category-table (org-get-category-table))
  7637. (let ((case-fold-search nil))
  7638. (save-excursion
  7639. (save-restriction
  7640. (if org-agenda-restrict
  7641. (narrow-to-region org-agenda-restrict-begin
  7642. org-agenda-restrict-end)
  7643. (widen))
  7644. ;; The way we repeatedly append to `results' makes it O(n^2) :-(
  7645. (while (setq arg (pop args))
  7646. (cond
  7647. ((and (eq arg :todo)
  7648. (equal date (calendar-current-date)))
  7649. (setq rtn (org-agenda-get-todos))
  7650. (setq results (append results rtn)))
  7651. ((eq arg :timestamp)
  7652. (setq rtn (org-agenda-get-blocks))
  7653. (setq results (append results rtn))
  7654. (setq rtn (org-agenda-get-timestamps))
  7655. (setq results (append results rtn)))
  7656. ((eq arg :scheduled)
  7657. (setq rtn (org-agenda-get-scheduled))
  7658. (setq results (append results rtn)))
  7659. ((eq arg :closed)
  7660. (setq rtn (org-agenda-get-closed))
  7661. (setq results (append results rtn)))
  7662. ((and (eq arg :deadline)
  7663. (equal date (calendar-current-date)))
  7664. (setq rtn (org-agenda-get-deadlines))
  7665. (setq results (append results rtn))))))))
  7666. results))))
  7667. (defun org-entry-is-done-p ()
  7668. "Is the current entry marked DONE?"
  7669. (save-excursion
  7670. (and (re-search-backward "[\r\n]\\*" nil t)
  7671. (looking-at org-nl-done-regexp))))
  7672. (defun org-at-date-range-p (&optional inactive-ok)
  7673. "Is the cursor inside a date range?"
  7674. (interactive)
  7675. (save-excursion
  7676. (catch 'exit
  7677. (let ((pos (point)))
  7678. (skip-chars-backward "^[<\r\n")
  7679. (skip-chars-backward "<[")
  7680. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7681. (>= (match-end 0) pos)
  7682. (throw 'exit t))
  7683. (skip-chars-backward "^<[\r\n")
  7684. (skip-chars-backward "<[")
  7685. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7686. (>= (match-end 0) pos)
  7687. (throw 'exit t)))
  7688. nil)))
  7689. (defun org-agenda-get-todos ()
  7690. "Return the TODO information for agenda display."
  7691. (let* ((props (list 'face nil
  7692. 'done-face 'org-done
  7693. 'org-not-done-regexp org-not-done-regexp
  7694. 'mouse-face 'highlight
  7695. 'keymap org-agenda-keymap
  7696. 'help-echo
  7697. (format "mouse-2 or RET jump to org file %s"
  7698. (abbreviate-file-name buffer-file-name))))
  7699. (regexp (concat "[\n\r]\\*+ *\\("
  7700. (if org-select-this-todo-keyword
  7701. (concat "\\<\\(" org-select-this-todo-keyword
  7702. "\\)\\>")
  7703. org-not-done-regexp)
  7704. "[^\n\r]*\\)"))
  7705. (deadline-re (concat ".*\\(\n[^*].*\\)?" org-deadline-time-regexp))
  7706. (sched-re (concat ".*\\(\n[^*].*\\)?" org-scheduled-time-regexp))
  7707. ; FIXME why was this wrong? (sched-re (concat ".*\n?.*?" org-scheduled-time-regexp))
  7708. marker priority category tags
  7709. ee txt)
  7710. (goto-char (point-min))
  7711. (while (re-search-forward regexp nil t)
  7712. (catch :skip
  7713. (save-match-data
  7714. (beginning-of-line)
  7715. (when (or (and org-agenda-todo-ignore-scheduled
  7716. (looking-at sched-re))
  7717. (and org-agenda-todo-ignore-deadlines
  7718. (looking-at deadline-re)
  7719. (org-deadline-close (match-string 2))))
  7720. ;; FIXME: the following test also happens below, but we need it here
  7721. (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
  7722. (throw :skip nil)))
  7723. (org-agenda-skip)
  7724. (goto-char (match-beginning 1))
  7725. (setq marker (org-agenda-new-marker (1+ (match-beginning 0)))
  7726. category (org-get-category)
  7727. tags (org-get-tags-at (point))
  7728. txt (org-format-agenda-item "" (match-string 1) category tags)
  7729. priority
  7730. (+ (org-get-priority txt)
  7731. (if org-todo-kwd-priority-p
  7732. (- org-todo-kwd-max-priority -2
  7733. (length
  7734. (member (match-string 2) org-todo-keywords)))
  7735. 1)))
  7736. (org-add-props txt props
  7737. 'org-marker marker 'org-hd-marker marker
  7738. 'priority priority 'category category)
  7739. (push txt ee)
  7740. (if org-agenda-todo-list-sublevels
  7741. (goto-char (match-end 1))
  7742. (org-end-of-subtree 'invisible))))
  7743. (nreverse ee)))
  7744. (defconst org-agenda-no-heading-message
  7745. "No heading for this item in buffer or region.")
  7746. (defun org-agenda-get-timestamps ()
  7747. "Return the date stamp information for agenda display."
  7748. (let* ((props (list 'face nil
  7749. 'org-not-done-regexp org-not-done-regexp
  7750. 'mouse-face 'highlight
  7751. 'keymap org-agenda-keymap
  7752. 'help-echo
  7753. (format "mouse-2 or RET jump to org file %s"
  7754. (abbreviate-file-name buffer-file-name))))
  7755. (regexp (regexp-quote
  7756. (substring
  7757. (format-time-string
  7758. (car org-time-stamp-formats)
  7759. (apply 'encode-time ; DATE bound by calendar
  7760. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  7761. 0 11)))
  7762. marker hdmarker deadlinep scheduledp donep tmp priority category
  7763. ee txt timestr tags)
  7764. (goto-char (point-min))
  7765. (while (re-search-forward regexp nil t)
  7766. (catch :skip
  7767. (and (save-match-data (org-at-date-range-p)) (throw :skip nil))
  7768. (org-agenda-skip)
  7769. (setq marker (org-agenda-new-marker (match-beginning 0))
  7770. category (org-get-category (match-beginning 0))
  7771. tmp (buffer-substring (max (point-min)
  7772. (- (match-beginning 0)
  7773. org-ds-keyword-length))
  7774. (match-beginning 0))
  7775. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  7776. deadlinep (string-match org-deadline-regexp tmp)
  7777. scheduledp (string-match org-scheduled-regexp tmp)
  7778. donep (org-entry-is-done-p))
  7779. (and org-agenda-skip-scheduled-if-done
  7780. scheduledp donep
  7781. (throw :skip t))
  7782. (if (string-match ">" timestr)
  7783. ;; substring should only run to end of time stamp
  7784. (setq timestr (substring timestr 0 (match-end 0))))
  7785. (save-excursion
  7786. (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t)
  7787. (progn
  7788. (goto-char (match-end 1))
  7789. (setq hdmarker (org-agenda-new-marker)
  7790. tags (org-get-tags-at))
  7791. (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
  7792. (setq txt (org-format-agenda-item
  7793. (format "%s%s"
  7794. (if deadlinep "Deadline: " "")
  7795. (if scheduledp "Scheduled: " ""))
  7796. (match-string 1) category tags timestr)))
  7797. (setq txt org-agenda-no-heading-message))
  7798. (setq priority (org-get-priority txt))
  7799. (org-add-props txt props
  7800. 'org-marker marker 'org-hd-marker hdmarker)
  7801. (if deadlinep
  7802. (org-add-props txt nil
  7803. 'face (if donep 'org-done 'org-warning)
  7804. 'undone-face 'org-warning 'done-face 'org-done
  7805. 'category category 'priority (+ 100 priority))
  7806. (if scheduledp
  7807. (org-add-props txt nil
  7808. 'face 'org-scheduled-today
  7809. 'undone-face 'org-scheduled-today 'done-face 'org-done
  7810. 'category category 'priority (+ 99 priority))
  7811. (org-add-props txt nil 'priority priority 'category category)))
  7812. (push txt ee))
  7813. (outline-next-heading)))
  7814. (nreverse ee)))
  7815. (defun org-agenda-get-closed ()
  7816. "Return the logged TODO entries for agenda display."
  7817. (let* ((props (list 'mouse-face 'highlight
  7818. 'org-not-done-regexp org-not-done-regexp
  7819. 'keymap org-agenda-keymap
  7820. 'help-echo
  7821. (format "mouse-2 or RET jump to org file %s"
  7822. (abbreviate-file-name buffer-file-name))))
  7823. (regexp (concat
  7824. "\\<\\(" org-closed-string "\\|" org-clock-string "\\) *\\["
  7825. (regexp-quote
  7826. (substring
  7827. (format-time-string
  7828. (car org-time-stamp-formats)
  7829. (apply 'encode-time ; DATE bound by calendar
  7830. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  7831. 1 11))))
  7832. marker hdmarker priority category tags closedp
  7833. ee txt timestr)
  7834. (goto-char (point-min))
  7835. (while (re-search-forward regexp nil t)
  7836. (catch :skip
  7837. (org-agenda-skip)
  7838. (setq marker (org-agenda-new-marker (match-beginning 0))
  7839. closedp (equal (match-string 1) org-closed-string)
  7840. category (org-get-category (match-beginning 0))
  7841. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  7842. ;; donep (org-entry-is-done-p)
  7843. )
  7844. (if (string-match "\\]" timestr)
  7845. ;; substring should only run to end of time stamp
  7846. (setq timestr (substring timestr 0 (match-end 0))))
  7847. (save-excursion
  7848. (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t)
  7849. (progn
  7850. (goto-char (match-end 1))
  7851. (setq hdmarker (org-agenda-new-marker)
  7852. tags (org-get-tags-at))
  7853. (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
  7854. (setq txt (org-format-agenda-item
  7855. (if closedp "Closed: " "Clocked: ")
  7856. (match-string 1) category tags timestr)))
  7857. (setq txt org-agenda-no-heading-message))
  7858. (setq priority 100000)
  7859. (org-add-props txt props
  7860. 'org-marker marker 'org-hd-marker hdmarker 'face 'org-done
  7861. 'priority priority 'category category
  7862. 'undone-face 'org-warning 'done-face 'org-done)
  7863. (push txt ee))
  7864. (outline-next-heading)))
  7865. (nreverse ee)))
  7866. (defun org-agenda-get-deadlines ()
  7867. "Return the deadline information for agenda display."
  7868. (let* ((wdays org-deadline-warning-days)
  7869. (props (list 'mouse-face 'highlight
  7870. 'org-not-done-regexp org-not-done-regexp
  7871. 'keymap org-agenda-keymap
  7872. 'help-echo
  7873. (format "mouse-2 or RET jump to org file %s"
  7874. (abbreviate-file-name buffer-file-name))))
  7875. (regexp org-deadline-time-regexp)
  7876. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  7877. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  7878. d2 diff pos pos1 category tags
  7879. ee txt head face)
  7880. (goto-char (point-min))
  7881. (while (re-search-forward regexp nil t)
  7882. (catch :skip
  7883. (org-agenda-skip)
  7884. (setq pos (1- (match-beginning 1))
  7885. d2 (time-to-days
  7886. (org-time-string-to-time (match-string 1)))
  7887. diff (- d2 d1))
  7888. ;; When to show a deadline in the calendar:
  7889. ;; If the expiration is within wdays warning time.
  7890. ;; Past-due deadlines are only shown on the current date
  7891. (if (and (< diff wdays) todayp (not (= diff 0)))
  7892. (save-excursion
  7893. (setq category (org-get-category))
  7894. (if (re-search-backward "\\(^\\|\r\\)\\*+[ \t]*" nil t)
  7895. (progn
  7896. (goto-char (match-end 0))
  7897. (setq pos1 (match-end 1))
  7898. (setq tags (org-get-tags-at pos1))
  7899. (setq head (buffer-substring-no-properties
  7900. (point)
  7901. (progn (skip-chars-forward "^\r\n")
  7902. (point))))
  7903. (if (string-match org-looking-at-done-regexp head)
  7904. (setq txt nil)
  7905. (setq txt (org-format-agenda-item
  7906. (format "In %3d d.: " diff) head category tags))))
  7907. (setq txt org-agenda-no-heading-message))
  7908. (when txt
  7909. (setq face (cond ((<= diff 0) 'org-warning)
  7910. ((<= diff 5) 'org-upcoming-deadline)
  7911. (t nil)))
  7912. (org-add-props txt props
  7913. 'org-marker (org-agenda-new-marker pos)
  7914. 'org-hd-marker (org-agenda-new-marker pos1)
  7915. 'priority (+ (- 10 diff) (org-get-priority txt))
  7916. 'category category
  7917. 'face face 'undone-face face 'done-face 'org-done)
  7918. (push txt ee))))))
  7919. ee))
  7920. (defun org-agenda-get-scheduled ()
  7921. "Return the scheduled information for agenda display."
  7922. (let* ((props (list 'face 'org-scheduled-previously
  7923. 'org-not-done-regexp org-not-done-regexp
  7924. 'undone-face 'org-scheduled-previously
  7925. 'done-face 'org-done
  7926. 'mouse-face 'highlight
  7927. 'keymap org-agenda-keymap
  7928. 'help-echo
  7929. (format "mouse-2 or RET jump to org file %s"
  7930. (abbreviate-file-name buffer-file-name))))
  7931. (regexp org-scheduled-time-regexp)
  7932. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  7933. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  7934. d2 diff pos pos1 category tags donep
  7935. ee txt head)
  7936. (goto-char (point-min))
  7937. (while (re-search-forward regexp nil t)
  7938. (catch :skip
  7939. (org-agenda-skip)
  7940. (setq pos (1- (match-beginning 1))
  7941. d2 (time-to-days
  7942. (org-time-string-to-time (match-string 1)))
  7943. diff (- d2 d1))
  7944. ;; When to show a scheduled item in the calendar:
  7945. ;; If it is on or past the date.
  7946. (if (and (< diff 0) todayp)
  7947. (save-excursion
  7948. (setq category (org-get-category))
  7949. (if (re-search-backward "\\(^\\|\r\\)\\*+[ \t]*" nil t)
  7950. (progn
  7951. (goto-char (match-end 0))
  7952. (setq pos1 (match-end 1))
  7953. (setq tags (org-get-tags-at))
  7954. (setq head (buffer-substring-no-properties
  7955. (point)
  7956. (progn (skip-chars-forward "^\r\n") (point))))
  7957. (if (string-match org-looking-at-done-regexp head)
  7958. (setq txt nil)
  7959. (setq txt (org-format-agenda-item
  7960. (format "Sched.%2dx: " (- 1 diff)) head
  7961. category tags))))
  7962. (setq txt org-agenda-no-heading-message))
  7963. (when txt
  7964. (org-add-props txt props
  7965. 'org-marker (org-agenda-new-marker pos)
  7966. 'org-hd-marker (org-agenda-new-marker pos1)
  7967. 'priority (+ (- 5 diff) (org-get-priority txt))
  7968. 'category category)
  7969. (push txt ee))))))
  7970. ee))
  7971. (defun org-agenda-get-blocks ()
  7972. "Return the date-range information for agenda display."
  7973. (let* ((props (list 'face nil
  7974. 'org-not-done-regexp org-not-done-regexp
  7975. 'mouse-face 'highlight
  7976. 'keymap org-agenda-keymap
  7977. 'help-echo
  7978. (format "mouse-2 or RET jump to org file %s"
  7979. (abbreviate-file-name buffer-file-name))))
  7980. (regexp org-tr-regexp)
  7981. (d0 (calendar-absolute-from-gregorian date))
  7982. marker hdmarker ee txt d1 d2 s1 s2 timestr category tags pos)
  7983. (goto-char (point-min))
  7984. (while (re-search-forward regexp nil t)
  7985. (catch :skip
  7986. (org-agenda-skip)
  7987. (setq pos (point))
  7988. (setq timestr (match-string 0)
  7989. s1 (match-string 1)
  7990. s2 (match-string 2)
  7991. d1 (time-to-days (org-time-string-to-time s1))
  7992. d2 (time-to-days (org-time-string-to-time s2)))
  7993. (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
  7994. ;; Only allow days between the limits, because the normal
  7995. ;; date stamps will catch the limits.
  7996. (save-excursion
  7997. (setq marker (org-agenda-new-marker (point)))
  7998. (setq category (org-get-category))
  7999. (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t)
  8000. (progn
  8001. (setq hdmarker (org-agenda-new-marker (match-end 1)))
  8002. (goto-char (match-end 1))
  8003. (setq tags (org-get-tags-at))
  8004. (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
  8005. (setq txt (org-format-agenda-item
  8006. (format (if (= d1 d2) "" "(%d/%d): ")
  8007. (1+ (- d0 d1)) (1+ (- d2 d1)))
  8008. (match-string 1) category tags
  8009. (if (= d0 d1) timestr))))
  8010. (setq txt org-agenda-no-heading-message))
  8011. (org-add-props txt props
  8012. 'org-marker marker 'org-hd-marker hdmarker
  8013. 'priority (org-get-priority txt) 'category category)
  8014. (push txt ee)))
  8015. (goto-char pos)))
  8016. ;; Sort the entries by expiration date.
  8017. (nreverse ee)))
  8018. (defconst org-plain-time-of-day-regexp
  8019. (concat
  8020. "\\(\\<[012]?[0-9]"
  8021. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  8022. "\\(--?"
  8023. "\\(\\<[012]?[0-9]"
  8024. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  8025. "\\)?")
  8026. "Regular expression to match a plain time or time range.
  8027. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  8028. groups carry important information:
  8029. 0 the full match
  8030. 1 the first time, range or not
  8031. 8 the second time, if it is a range.")
  8032. (defconst org-stamp-time-of-day-regexp
  8033. (concat
  8034. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  8035. "\\([012][0-9]:[0-5][0-9]\\)>"
  8036. "\\(--?"
  8037. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  8038. "Regular expression to match a timestamp time or time range.
  8039. After a match, the following groups carry important information:
  8040. 0 the full match
  8041. 1 date plus weekday, for backreferencing to make sure both times on same day
  8042. 2 the first time, range or not
  8043. 4 the second time, if it is a range.")
  8044. (defvar org-prefix-has-time nil
  8045. "A flag, set by `org-compile-prefix-format'.
  8046. The flag is set if the currently compiled format contains a `%t'.")
  8047. (defvar org-prefix-has-tag nil
  8048. "A flag, set by `org-compile-prefix-format'.
  8049. The flag is set if the currently compiled format contains a `%T'.")
  8050. (defun org-format-agenda-item (extra txt &optional category tags dotime
  8051. noprefix)
  8052. "Format TXT to be inserted into the agenda buffer.
  8053. In particular, it adds the prefix and corresponding text properties. EXTRA
  8054. must be a string and replaces the `%s' specifier in the prefix format.
  8055. CATEGORY (string, symbol or nil) may be used to overrule the default
  8056. category taken from local variable or file name. It will replace the `%c'
  8057. specifier in the format. DOTIME, when non-nil, indicates that a
  8058. time-of-day should be extracted from TXT for sorting of this entry, and for
  8059. the `%t' specifier in the format. When DOTIME is a string, this string is
  8060. searched for a time before TXT is. NOPREFIX is a flag and indicates that
  8061. only the correctly processes TXT should be returned - this is used by
  8062. `org-agenda-change-all-lines'. TAGS can be the tags of the headline."
  8063. (save-match-data
  8064. ;; Diary entries sometimes have extra whitespace at the beginning
  8065. (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
  8066. (let* ((category (or category
  8067. org-category
  8068. (if buffer-file-name
  8069. (file-name-sans-extension
  8070. (file-name-nondirectory buffer-file-name))
  8071. "")))
  8072. (tag (if tags (nth (1- (length tags)) tags) ""))
  8073. time ;; needed for the eval of the prefix format
  8074. (ts (if dotime (concat (if (stringp dotime) dotime "") txt)))
  8075. (time-of-day (and dotime (org-get-time-of-day ts)))
  8076. stamp plain s0 s1 s2 rtn)
  8077. (when (and dotime time-of-day org-prefix-has-time)
  8078. ;; Extract starting and ending time and move them to prefix
  8079. (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
  8080. (setq plain (string-match org-plain-time-of-day-regexp ts)))
  8081. (setq s0 (match-string 0 ts)
  8082. s1 (match-string (if plain 1 2) ts)
  8083. s2 (match-string (if plain 8 4) ts))
  8084. ;; If the times are in TXT (not in DOTIMES), and the prefix will list
  8085. ;; them, we might want to remove them there to avoid duplication.
  8086. ;; The user can turn this off with a variable.
  8087. (if (and org-agenda-remove-times-when-in-prefix (or stamp plain)
  8088. (string-match (concat (regexp-quote s0) " *") txt)
  8089. (if (eq org-agenda-remove-times-when-in-prefix 'beg)
  8090. (= (match-beginning 0) 0)
  8091. t))
  8092. (setq txt (replace-match "" nil nil txt))))
  8093. ;; Normalize the time(s) to 24 hour
  8094. (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
  8095. (if s2 (setq s2 (org-get-time-of-day s2 'string t))))
  8096. (when (string-match "\\([ \t]+\\)\\(:[a-zA-Z_@0-9:]+:\\)[ \t]*$" txt)
  8097. ;; Tags are in the string
  8098. (if (or (eq org-agenda-remove-tags-when-in-prefix t)
  8099. (and org-agenda-remove-tags-when-in-prefix
  8100. org-prefix-has-tag))
  8101. (setq txt (replace-match "" t t txt))
  8102. (setq txt (replace-match
  8103. (concat (make-string (max (- 50 (length txt)) 1) ?\ )
  8104. (match-string 2 txt))
  8105. t t txt))))
  8106. ;; Create the final string
  8107. (if noprefix
  8108. (setq rtn txt)
  8109. ;; Prepare the variables needed in the eval of the compiled format
  8110. (setq time (cond (s2 (concat s1 "-" s2))
  8111. (s1 (concat s1 "......"))
  8112. (t ""))
  8113. extra (or extra "")
  8114. category (if (symbolp category) (symbol-name category) category))
  8115. ;; Evaluate the compiled format
  8116. (setq rtn (concat (eval org-prefix-format-compiled) txt)))
  8117. ;; And finally add the text properties
  8118. (org-add-props rtn nil
  8119. 'category (downcase category) 'tags tags
  8120. 'prefix-length (- (length rtn) (length txt))
  8121. 'time-of-day time-of-day
  8122. 'dotime dotime))))
  8123. (defvar org-agenda-sorting-strategy)
  8124. (defvar org-agenda-sorting-strategy-selected nil)
  8125. (defun org-agenda-add-time-grid-maybe (list ndays todayp)
  8126. (catch 'exit
  8127. (cond ((not org-agenda-use-time-grid) (throw 'exit list))
  8128. ((and todayp (member 'today (car org-agenda-time-grid))))
  8129. ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
  8130. ((member 'weekly (car org-agenda-time-grid)))
  8131. (t (throw 'exit list)))
  8132. (let* ((have (delq nil (mapcar
  8133. (lambda (x) (get-text-property 1 'time-of-day x))
  8134. list)))
  8135. (string (nth 1 org-agenda-time-grid))
  8136. (gridtimes (nth 2 org-agenda-time-grid))
  8137. (req (car org-agenda-time-grid))
  8138. (remove (member 'remove-match req))
  8139. new time)
  8140. (if (and (member 'require-timed req) (not have))
  8141. ;; don't show empty grid
  8142. (throw 'exit list))
  8143. (while (setq time (pop gridtimes))
  8144. (unless (and remove (member time have))
  8145. (setq time (int-to-string time))
  8146. (push (org-format-agenda-item
  8147. nil string "" nil
  8148. (concat (substring time 0 -2) ":" (substring time -2)))
  8149. new)
  8150. (put-text-property
  8151. 1 (length (car new)) 'face 'org-time-grid (car new))))
  8152. (if (member 'time-up org-agenda-sorting-strategy-selected)
  8153. (append new list)
  8154. (append list new)))))
  8155. (defun org-compile-prefix-format (key)
  8156. "Compile the prefix format into a Lisp form that can be evaluated.
  8157. The resulting form is returned and stored in the variable
  8158. `org-prefix-format-compiled'."
  8159. (setq org-prefix-has-time nil org-prefix-has-tag nil)
  8160. (let ((s (cond
  8161. ((stringp org-agenda-prefix-format)
  8162. org-agenda-prefix-format)
  8163. ((assq key org-agenda-prefix-format)
  8164. (cdr (assq key org-agenda-prefix-format)))
  8165. (t " %-12:c%?-12t% s")))
  8166. (start 0)
  8167. varform vars var e c f opt)
  8168. (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cts]\\)"
  8169. s start)
  8170. (setq var (cdr (assoc (match-string 4 s)
  8171. '(("c" . category) ("t" . time) ("s" . extra)
  8172. ("T" . tag))))
  8173. c (or (match-string 3 s) "")
  8174. opt (match-beginning 1)
  8175. start (1+ (match-beginning 0)))
  8176. (if (equal var 'time) (setq org-prefix-has-time t))
  8177. (if (equal var 'tag) (setq org-prefix-has-tag t))
  8178. (setq f (concat "%" (match-string 2 s) "s"))
  8179. (if opt
  8180. (setq varform
  8181. `(if (equal "" ,var)
  8182. ""
  8183. (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
  8184. (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
  8185. (setq s (replace-match "%s" t nil s))
  8186. (push varform vars))
  8187. (setq vars (nreverse vars))
  8188. (setq org-prefix-format-compiled `(format ,s ,@vars))))
  8189. (defun org-set-sorting-strategy (key)
  8190. (if (symbolp (car org-agenda-sorting-strategy))
  8191. ;; the old format
  8192. (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
  8193. (setq org-agenda-sorting-strategy-selected
  8194. (or (cdr (assq key org-agenda-sorting-strategy))
  8195. (cdr (assq 'agenda org-agenda-sorting-strategy))
  8196. '(time-up category-keep priority-down)))))
  8197. (defun org-get-time-of-day (s &optional string mod24)
  8198. "Check string S for a time of day.
  8199. If found, return it as a military time number between 0 and 2400.
  8200. If not found, return nil.
  8201. The optional STRING argument forces conversion into a 5 character wide string
  8202. HH:MM."
  8203. (save-match-data
  8204. (when
  8205. (or
  8206. (string-match
  8207. "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
  8208. (string-match
  8209. "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
  8210. (let* ((h (string-to-number (match-string 1 s)))
  8211. (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
  8212. (ampm (if (match-end 4) (downcase (match-string 4 s))))
  8213. (am-p (equal ampm "am"))
  8214. (h1 (cond ((not ampm) h)
  8215. ((= h 12) (if am-p 0 12))
  8216. (t (+ h (if am-p 0 12)))))
  8217. (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
  8218. (mod h1 24) h1))
  8219. (t0 (+ (* 100 h2) m))
  8220. (t1 (concat (if (>= h1 24) "+" " ")
  8221. (if (< t0 100) "0" "")
  8222. (if (< t0 10) "0" "")
  8223. (int-to-string t0))))
  8224. (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
  8225. (defun org-finalize-agenda-entries (list &optional nosort)
  8226. "Sort and concatenate the agenda items."
  8227. (setq list (mapcar 'org-agenda-highlight-todo list))
  8228. (if nosort
  8229. list
  8230. (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
  8231. (defun org-agenda-highlight-todo (x)
  8232. (let (re pl)
  8233. (if (eq x 'line)
  8234. (save-excursion
  8235. (beginning-of-line 1)
  8236. (setq re (get-text-property (point) 'org-not-done-regexp))
  8237. (goto-char (+ (point) (or (get-text-property (point) 'prefix-length) 0)))
  8238. (and (looking-at (concat "[ \t]*\\.*" re))
  8239. (add-text-properties (match-beginning 0) (match-end 0)
  8240. '(face org-todo))))
  8241. (setq re (concat (get-text-property 0 'org-not-done-regexp x))
  8242. pl (get-text-property 0 'prefix-length x))
  8243. (and re (equal (string-match (concat "\\(\\.*\\)" re) x (or pl 0)) pl)
  8244. (add-text-properties (or (match-end 1) (match-end 0)) (match-end 0)
  8245. '(face org-todo) x))
  8246. x)))
  8247. (defsubst org-cmp-priority (a b)
  8248. "Compare the priorities of string A and B."
  8249. (let ((pa (or (get-text-property 1 'priority a) 0))
  8250. (pb (or (get-text-property 1 'priority b) 0)))
  8251. (cond ((> pa pb) +1)
  8252. ((< pa pb) -1)
  8253. (t nil))))
  8254. (defsubst org-cmp-category (a b)
  8255. "Compare the string values of categories of strings A and B."
  8256. (let ((ca (or (get-text-property 1 'category a) ""))
  8257. (cb (or (get-text-property 1 'category b) "")))
  8258. (cond ((string-lessp ca cb) -1)
  8259. ((string-lessp cb ca) +1)
  8260. (t nil))))
  8261. (defsubst org-cmp-tag (a b)
  8262. "Compare the string values of categories of strings A and B."
  8263. (let ((ta (car (last (get-text-property 1 'tags a))))
  8264. (tb (car (last (get-text-property 1 'tags b)))))
  8265. (cond ((not ta) +1)
  8266. ((not tb) -1)
  8267. ((string-lessp ta tb) -1)
  8268. ((string-lessp tb ta) +1)
  8269. (t nil))))
  8270. (defsubst org-cmp-time (a b)
  8271. "Compare the time-of-day values of strings A and B."
  8272. (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
  8273. (ta (or (get-text-property 1 'time-of-day a) def))
  8274. (tb (or (get-text-property 1 'time-of-day b) def)))
  8275. (cond ((< ta tb) -1)
  8276. ((< tb ta) +1)
  8277. (t nil))))
  8278. (defun org-entries-lessp (a b)
  8279. "Predicate for sorting agenda entries."
  8280. ;; The following variables will be used when the form is evaluated.
  8281. (let* ((time-up (org-cmp-time a b))
  8282. (time-down (if time-up (- time-up) nil))
  8283. (priority-up (org-cmp-priority a b))
  8284. (priority-down (if priority-up (- priority-up) nil))
  8285. (category-up (org-cmp-category a b))
  8286. (category-down (if category-up (- category-up) nil))
  8287. (category-keep (if category-up +1 nil))
  8288. (tag-up (org-cmp-tag a b))
  8289. (tag-down (if tag-up (- tag-up) nil)))
  8290. (cdr (assoc
  8291. (eval (cons 'or org-agenda-sorting-strategy-selected))
  8292. '((-1 . t) (1 . nil) (nil . nil))))))
  8293. (defun org-agenda-show-priority ()
  8294. "Show the priority of the current item.
  8295. This priority is composed of the main priority given with the [#A] cookies,
  8296. and by additional input from the age of a schedules or deadline entry."
  8297. (interactive)
  8298. (let* ((pri (get-text-property (point-at-bol) 'priority)))
  8299. (message "Priority is %d" (if pri pri -1000))))
  8300. (defun org-agenda-show-tags ()
  8301. "Show the tags applicable to the current item."
  8302. (interactive)
  8303. (let* ((tags (get-text-property (point-at-bol) 'tags)))
  8304. (if tags
  8305. (message "Tags are :%s:"
  8306. (org-no-properties (mapconcat 'identity tags ":")))
  8307. (message "No tags associated with this line"))))
  8308. (defun org-agenda-goto (&optional highlight)
  8309. "Go to the Org-mode file which contains the item at point."
  8310. (interactive)
  8311. (let* ((marker (or (get-text-property (point) 'org-marker)
  8312. (org-agenda-error)))
  8313. (buffer (marker-buffer marker))
  8314. (pos (marker-position marker)))
  8315. (switch-to-buffer-other-window buffer)
  8316. (widen)
  8317. (goto-char pos)
  8318. (when (org-mode-p)
  8319. (org-show-context 'agenda)
  8320. (save-excursion
  8321. (and (outline-next-heading)
  8322. (org-flag-heading nil)))) ; show the next heading
  8323. (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
  8324. (defun org-agenda-kill ()
  8325. "Kill the entry or subtree belonging to the current agenda entry."
  8326. (interactive)
  8327. (let* ((marker (or (get-text-property (point) 'org-marker)
  8328. (org-agenda-error)))
  8329. (hdmarker (get-text-property (point) 'org-hd-marker))
  8330. (buffer (marker-buffer marker))
  8331. (pos (marker-position marker))
  8332. dbeg dend (n 0) conf)
  8333. (with-current-buffer buffer
  8334. (save-excursion
  8335. (goto-char pos)
  8336. (if (org-mode-p)
  8337. (setq dbeg (progn (org-back-to-heading t) (point))
  8338. dend (org-end-of-subtree t))
  8339. (setq dbeg (point-at-bol)
  8340. dend (min (point-max) (1+ (point-at-eol)))))
  8341. (goto-char dbeg)
  8342. (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
  8343. (setq conf (or (eq t org-agenda-confirm-kill)
  8344. (and (numberp org-agenda-confirm-kill)
  8345. (> n org-agenda-confirm-kill))))
  8346. (and conf
  8347. (not (y-or-n-p
  8348. (format "Delete entry with %d lines in buffer \"%s\"? "
  8349. n (buffer-name buffer))))
  8350. (error "Abort"))
  8351. (org-remove-subtree-entries-from-agenda buffer dbeg dend)
  8352. (with-current-buffer buffer (delete-region dbeg dend))
  8353. (message "Agenda item and source killed")))
  8354. (defun org-agenda-archive ()
  8355. "Kill the entry or subtree belonging to the current agenda entry."
  8356. (interactive)
  8357. (let* ((marker (or (get-text-property (point) 'org-marker)
  8358. (org-agenda-error)))
  8359. (hdmarker (get-text-property (point) 'org-hd-marker))
  8360. (buffer (marker-buffer marker))
  8361. (pos (marker-position marker))
  8362. dbeg dend txt n conf)
  8363. (with-current-buffer buffer
  8364. (if (org-mode-p)
  8365. (save-excursion
  8366. (goto-char pos)
  8367. (org-remove-subtree-entries-from-agenda)
  8368. (org-back-to-heading t)
  8369. (org-archive-subtree))
  8370. (error "Archiving works only in Org-mode files")))))
  8371. (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
  8372. "Remove all lines in the agenda that correspond to a given subtree.
  8373. The subtree is the one in buffer BUF, starting at BEG and ending at END.
  8374. If this information is not given, the function uses the tree at point."
  8375. (let ((buf (or buf (current-buffer))) m p)
  8376. (save-excursion
  8377. (unless (and beg end)
  8378. (org-back-to-heading t)
  8379. (setq beg (point))
  8380. (org-end-of-subtree t)
  8381. (setq end (point)))
  8382. (set-buffer (get-buffer org-agenda-buffer-name))
  8383. (save-excursion
  8384. (goto-char (point-max))
  8385. (beginning-of-line 1)
  8386. (while (not (bobp))
  8387. (when (and (setq m (get-text-property (point) 'org-marker))
  8388. (equal buf (marker-buffer m))
  8389. (setq p (marker-position m))
  8390. (>= p beg)
  8391. (<= p end))
  8392. (let (buffer-read-only)
  8393. (delete-region (point-at-bol) (1+ (point-at-eol)))))
  8394. (beginning-of-line 0))))))
  8395. (defun org-agenda-open-link ()
  8396. "Follow the link in the current line, if any."
  8397. (interactive)
  8398. (let ((eol (point-at-eol)))
  8399. (save-excursion
  8400. (if (or (re-search-forward org-bracket-link-regexp eol t)
  8401. (re-search-forward org-angle-link-re eol t)
  8402. (re-search-forward org-plain-link-re eol t))
  8403. (call-interactively 'org-open-at-point)
  8404. (error "No link in current line")))))
  8405. (defun org-agenda-switch-to (&optional delete-other-windows)
  8406. "Go to the Org-mode file which contains the item at point."
  8407. (interactive)
  8408. (let* ((marker (or (get-text-property (point) 'org-marker)
  8409. (org-agenda-error)))
  8410. (buffer (marker-buffer marker))
  8411. (pos (marker-position marker)))
  8412. (switch-to-buffer buffer)
  8413. (and delete-other-windows (delete-other-windows))
  8414. (widen)
  8415. (goto-char pos)
  8416. (when (org-mode-p)
  8417. (org-show-context 'agenda)
  8418. (save-excursion
  8419. (and (outline-next-heading)
  8420. (org-flag-heading nil)))))) ; show the next heading
  8421. (defun org-agenda-goto-mouse (ev)
  8422. "Go to the Org-mode file which contains the item at the mouse click."
  8423. (interactive "e")
  8424. (mouse-set-point ev)
  8425. (org-agenda-goto))
  8426. (defun org-agenda-show ()
  8427. "Display the Org-mode file which contains the item at point."
  8428. (interactive)
  8429. (let ((win (selected-window)))
  8430. (org-agenda-goto t)
  8431. (select-window win)))
  8432. (defun org-agenda-recenter (arg)
  8433. "Display the Org-mode file which contains the item at point and recenter."
  8434. (interactive "P")
  8435. (let ((win (selected-window)))
  8436. (org-agenda-goto t)
  8437. (recenter arg)
  8438. (select-window win)))
  8439. (defun org-agenda-show-mouse (ev)
  8440. "Display the Org-mode file which contains the item at the mouse click."
  8441. (interactive "e")
  8442. (mouse-set-point ev)
  8443. (org-agenda-show))
  8444. (defun org-agenda-check-no-diary ()
  8445. "Check if the entry is a diary link and abort if yes."
  8446. (if (get-text-property (point) 'org-agenda-diary-link)
  8447. (org-agenda-error)))
  8448. (defun org-agenda-error ()
  8449. (error "Command not allowed in this line"))
  8450. (defvar org-last-heading-marker (make-marker)
  8451. "Marker pointing to the headline that last changed its TODO state
  8452. by a remote command from the agenda.")
  8453. (defun org-agenda-todo (&optional arg)
  8454. "Cycle TODO state of line at point, also in Org-mode file.
  8455. This changes the line at point, all other lines in the agenda referring to
  8456. the same tree node, and the headline of the tree node in the Org-mode file."
  8457. (interactive "P")
  8458. (org-agenda-check-no-diary)
  8459. (let* ((col (current-column))
  8460. (marker (or (get-text-property (point) 'org-marker)
  8461. (org-agenda-error)))
  8462. (buffer (marker-buffer marker))
  8463. (pos (marker-position marker))
  8464. (hdmarker (get-text-property (point) 'org-hd-marker))
  8465. (buffer-read-only nil)
  8466. newhead)
  8467. (with-current-buffer buffer
  8468. (widen)
  8469. (goto-char pos)
  8470. (org-show-context 'agenda)
  8471. (save-excursion
  8472. (and (outline-next-heading)
  8473. (org-flag-heading nil))) ; show the next heading
  8474. (org-todo arg)
  8475. (and (bolp) (forward-char 1))
  8476. (setq newhead (org-get-heading))
  8477. (save-excursion
  8478. (org-back-to-heading)
  8479. (move-marker org-last-heading-marker (point))))
  8480. (beginning-of-line 1)
  8481. (save-excursion
  8482. (org-agenda-change-all-lines newhead hdmarker 'fixface))
  8483. (move-to-column col)))
  8484. (defun org-agenda-change-all-lines (newhead hdmarker &optional fixface)
  8485. "Change all lines in the agenda buffer which match HDMARKER.
  8486. The new content of the line will be NEWHEAD (as modified by
  8487. `org-format-agenda-item'). HDMARKER is checked with
  8488. `equal' against all `org-hd-marker' text properties in the file.
  8489. If FIXFACE is non-nil, the face of each item is modified acording to
  8490. the new TODO state."
  8491. (let* ((buffer-read-only nil)
  8492. props m pl undone-face done-face finish new dotime cat tags)
  8493. (save-excursion
  8494. (goto-char (point-max))
  8495. (beginning-of-line 1)
  8496. (while (not finish)
  8497. (setq finish (bobp))
  8498. (when (and (setq m (get-text-property (point) 'org-hd-marker))
  8499. (equal m hdmarker))
  8500. (setq props (text-properties-at (point))
  8501. dotime (get-text-property (point) 'dotime)
  8502. cat (get-text-property (point) 'category)
  8503. tags (get-text-property (point) 'tags)
  8504. new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
  8505. pl (get-text-property (point) 'prefix-length)
  8506. undone-face (get-text-property (point) 'undone-face)
  8507. done-face (get-text-property (point) 'done-face))
  8508. (move-to-column pl)
  8509. (cond
  8510. ((equal new "")
  8511. (beginning-of-line 1)
  8512. (and (looking-at ".*\n?") (replace-match "")))
  8513. ((looking-at ".*")
  8514. (replace-match new t t)
  8515. (beginning-of-line 1)
  8516. (add-text-properties (point-at-bol) (point-at-eol) props)
  8517. (when fixface
  8518. (add-text-properties
  8519. (point-at-bol) (point-at-eol)
  8520. (list 'face
  8521. (if org-last-todo-state-is-todo
  8522. undone-face done-face))))
  8523. (org-agenda-highlight-todo 'line)
  8524. (beginning-of-line 1))
  8525. (t (error "Line update did not work"))))
  8526. (beginning-of-line 0)))
  8527. (org-finalize-agenda)))
  8528. (defun org-agenda-align-tags (&optional line)
  8529. "Align all tags in agenda items to `org-agenda-align-tags-to-column'."
  8530. (let ((buffer-read-only))
  8531. (save-excursion
  8532. (goto-char (if line (point-at-bol) (point-min)))
  8533. (while (re-search-forward "\\([ \t]+\\):[a-zA-Z0-9_@:]+:[ \t]*$"
  8534. (if line (point-at-eol) nil) t)
  8535. (delete-region (match-beginning 1) (match-end 1))
  8536. (goto-char (match-beginning 1))
  8537. (insert (org-add-props
  8538. (make-string (max 1 (- org-agenda-align-tags-to-column
  8539. (current-column))) ?\ )
  8540. (text-properties-at (point))))))))
  8541. (defun org-agenda-priority-up ()
  8542. "Increase the priority of line at point, also in Org-mode file."
  8543. (interactive)
  8544. (org-agenda-priority 'up))
  8545. (defun org-agenda-priority-down ()
  8546. "Decrease the priority of line at point, also in Org-mode file."
  8547. (interactive)
  8548. (org-agenda-priority 'down))
  8549. (defun org-agenda-priority (&optional force-direction)
  8550. "Set the priority of line at point, also in Org-mode file.
  8551. This changes the line at point, all other lines in the agenda referring to
  8552. the same tree node, and the headline of the tree node in the Org-mode file."
  8553. (interactive)
  8554. (org-agenda-check-no-diary)
  8555. (let* ((marker (or (get-text-property (point) 'org-marker)
  8556. (org-agenda-error)))
  8557. (buffer (marker-buffer marker))
  8558. (pos (marker-position marker))
  8559. (hdmarker (get-text-property (point) 'org-hd-marker))
  8560. (buffer-read-only nil)
  8561. newhead)
  8562. (with-current-buffer buffer
  8563. (widen)
  8564. (goto-char pos)
  8565. (org-show-context 'agenda)
  8566. (save-excursion
  8567. (and (outline-next-heading)
  8568. (org-flag-heading nil))) ; show the next heading
  8569. (funcall 'org-priority force-direction)
  8570. (end-of-line 1)
  8571. (setq newhead (org-get-heading)))
  8572. (org-agenda-change-all-lines newhead hdmarker)
  8573. (beginning-of-line 1)))
  8574. (defun org-get-tags-at (&optional pos)
  8575. "Get a list of all headline tags applicable at POS.
  8576. POS defaults to point. If tags are inherited, the list contains
  8577. the targets in the same sequence as the headlines appear, i.e.
  8578. the tags of the current headline come last."
  8579. (interactive)
  8580. (let (tags)
  8581. (save-excursion
  8582. (goto-char (or pos (point)))
  8583. (save-match-data
  8584. (org-back-to-heading t)
  8585. (condition-case nil
  8586. (while t
  8587. (if (looking-at "[^\r\n]+?:\\([a-zA-Z_@0-9:]+\\):[ \t]*\\([\n\r]\\|\\'\\)")
  8588. (setq tags (append (org-split-string
  8589. (org-match-string-no-properties 1) ":")
  8590. tags)))
  8591. (or org-use-tag-inheritance (error ""))
  8592. (org-up-heading-all 1))
  8593. (error nil))))
  8594. tags))
  8595. ;; FIXME: should fix the tags property of the agenda line.
  8596. (defun org-agenda-set-tags ()
  8597. "Set tags for the current headline."
  8598. (interactive)
  8599. (org-agenda-check-no-diary)
  8600. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  8601. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  8602. (org-agenda-error)))
  8603. (buffer (marker-buffer hdmarker))
  8604. (pos (marker-position hdmarker))
  8605. (buffer-read-only nil)
  8606. newhead)
  8607. (with-current-buffer buffer
  8608. (widen)
  8609. (goto-char pos)
  8610. (org-show-context 'agenda)
  8611. (save-excursion
  8612. (and (outline-next-heading)
  8613. (org-flag-heading nil))) ; show the next heading
  8614. (call-interactively 'org-set-tags)
  8615. (end-of-line 1)
  8616. (setq newhead (org-get-heading)))
  8617. (org-agenda-change-all-lines newhead hdmarker)
  8618. (beginning-of-line 1)))
  8619. (defun org-agenda-date-later (arg &optional what)
  8620. "Change the date of this item to one day later."
  8621. (interactive "p")
  8622. (org-agenda-check-type t 'agenda 'timeline)
  8623. (org-agenda-check-no-diary)
  8624. (let* ((marker (or (get-text-property (point) 'org-marker)
  8625. (org-agenda-error)))
  8626. (buffer (marker-buffer marker))
  8627. (pos (marker-position marker)))
  8628. (with-current-buffer buffer
  8629. (widen)
  8630. (goto-char pos)
  8631. (if (not (org-at-timestamp-p))
  8632. (error "Cannot find time stamp"))
  8633. (org-timestamp-change arg (or what 'day)))
  8634. (org-agenda-overlay-new-time marker org-last-changed-timestamp)
  8635. (message "Time stamp changed to %s" org-last-changed-timestamp)))
  8636. (defun org-agenda-date-earlier (arg &optional what)
  8637. "Change the date of this item to one day earlier."
  8638. (interactive "p")
  8639. (org-agenda-date-later (- arg) what))
  8640. (defun org-agenda-overlay-new-time (marker stamp)
  8641. (let ((buffer-read-only nil)
  8642. ovs ov)
  8643. (setq stamp (concat " => " stamp))
  8644. (save-excursion
  8645. (goto-char (point-max))
  8646. (while (not (bobp))
  8647. (when (equal marker (get-text-property (point) 'org-marker))
  8648. ;; remove any old overlays
  8649. (org-find-overlays 'org-new-date (1- (point-at-eol)) 'delete)
  8650. ;; put a new overlay
  8651. (move-to-column (- (window-width) (length stamp)) t)
  8652. (setq ov (org-make-overlay (1- (point)) (point-at-eol)))
  8653. (org-overlay-put ov 'org-new-date t)
  8654. (org-overlay-display ov stamp 'secondary-selection)
  8655. (beginning-of-line 1))
  8656. (beginning-of-line 0)))))
  8657. (defun org-agenda-date-prompt (arg)
  8658. "Change the date of this item. Date is prompted for, with default today.
  8659. The prefix ARG is passed to the `org-time-stamp' command and can therefore
  8660. be used to request time specification in the time stamp."
  8661. (interactive "P")
  8662. (org-agenda-check-type t 'agenda 'timeline)
  8663. (org-agenda-check-no-diary)
  8664. (let* ((marker (or (get-text-property (point) 'org-marker)
  8665. (org-agenda-error)))
  8666. (buffer (marker-buffer marker))
  8667. (pos (marker-position marker)))
  8668. (with-current-buffer buffer
  8669. (widen)
  8670. (goto-char pos)
  8671. (if (not (org-at-timestamp-p))
  8672. (error "Cannot find time stamp"))
  8673. (org-time-stamp arg)
  8674. (message "Time stamp changed to %s" org-last-changed-timestamp))))
  8675. (defun org-agenda-schedule (arg)
  8676. "Schedule the item at point."
  8677. (interactive "P")
  8678. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  8679. (org-agenda-check-no-diary)
  8680. (let* ((marker (or (get-text-property (point) 'org-marker)
  8681. (org-agenda-error)))
  8682. (buffer (marker-buffer marker))
  8683. (pos (marker-position marker))
  8684. (org-insert-labeled-timestamps-at-point nil)
  8685. ts)
  8686. (with-current-buffer buffer
  8687. (widen)
  8688. (goto-char pos)
  8689. (setq ts (org-schedule))
  8690. (message "Item scheduled for %s" ts))))
  8691. (defun org-agenda-deadline (arg)
  8692. "Schedule the item at point."
  8693. (interactive "P")
  8694. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  8695. (org-agenda-check-no-diary)
  8696. (let* ((marker (or (get-text-property (point) 'org-marker)
  8697. (org-agenda-error)))
  8698. (buffer (marker-buffer marker))
  8699. (pos (marker-position marker))
  8700. (org-insert-labeled-timestamps-at-point nil)
  8701. ts)
  8702. (with-current-buffer buffer
  8703. (widen)
  8704. (goto-char pos)
  8705. (setq ts (org-deadline))
  8706. (message "Deadline for this item set to %s" ts))))
  8707. (defun org-get-heading ()
  8708. "Return the heading of the current entry, without the stars."
  8709. (save-excursion
  8710. (and (memq (char-before) '(?\n ?\r)) (skip-chars-forward "^\n\r"))
  8711. (if (and (re-search-backward "[\r\n]\\*" nil t)
  8712. (looking-at "[\r\n]\\*+[ \t]+\\([^\r\n]*\\)"))
  8713. (match-string 1)
  8714. "")))
  8715. (defun org-agenda-clock-in (&optional arg)
  8716. "Start the clock on the currently selected item."
  8717. (interactive "P")
  8718. (org-agenda-check-no-diary)
  8719. (let* ((marker (or (get-text-property (point) 'org-marker)
  8720. (org-agenda-error)))
  8721. (pos (marker-position marker)))
  8722. (with-current-buffer (marker-buffer marker)
  8723. (widen)
  8724. (goto-char pos)
  8725. (org-clock-in))))
  8726. (defun org-agenda-diary-entry ()
  8727. "Make a diary entry, like the `i' command from the calendar.
  8728. All the standard commands work: block, weekly etc."
  8729. (interactive)
  8730. (org-agenda-check-type t 'agenda 'timeline)
  8731. (require 'diary-lib)
  8732. (let* ((char (progn
  8733. (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
  8734. (read-char-exclusive)))
  8735. (cmd (cdr (assoc char
  8736. '((?d . insert-diary-entry)
  8737. (?w . insert-weekly-diary-entry)
  8738. (?m . insert-monthly-diary-entry)
  8739. (?y . insert-yearly-diary-entry)
  8740. (?a . insert-anniversary-diary-entry)
  8741. (?b . insert-block-diary-entry)
  8742. (?c . insert-cyclic-diary-entry)))))
  8743. (oldf (symbol-function 'calendar-cursor-to-date))
  8744. (point (point))
  8745. (mark (or (mark t) (point))))
  8746. (unless cmd
  8747. (error "No command associated with <%c>" char))
  8748. (unless (and (get-text-property point 'day)
  8749. (or (not (equal ?b char))
  8750. (get-text-property mark 'day)))
  8751. (error "Don't know which date to use for diary entry"))
  8752. ;; We implement this by hacking the `calendar-cursor-to-date' function
  8753. ;; and the `calendar-mark-ring' variable. Saves a lot of code.
  8754. (let ((calendar-mark-ring
  8755. (list (calendar-gregorian-from-absolute
  8756. (or (get-text-property mark 'day)
  8757. (get-text-property point 'day))))))
  8758. (unwind-protect
  8759. (progn
  8760. (fset 'calendar-cursor-to-date
  8761. (lambda (&optional error)
  8762. (calendar-gregorian-from-absolute
  8763. (get-text-property point 'day))))
  8764. (call-interactively cmd))
  8765. (fset 'calendar-cursor-to-date oldf)))))
  8766. (defun org-agenda-execute-calendar-command (cmd)
  8767. "Execute a calendar command from the agenda, with the date associated to
  8768. the cursor position."
  8769. (org-agenda-check-type t 'agenda 'timeline)
  8770. (require 'diary-lib)
  8771. (unless (get-text-property (point) 'day)
  8772. (error "Don't know which date to use for calendar command"))
  8773. (let* ((oldf (symbol-function 'calendar-cursor-to-date))
  8774. (point (point))
  8775. (date (calendar-gregorian-from-absolute
  8776. (get-text-property point 'day)))
  8777. (displayed-day (extract-calendar-day date))
  8778. (displayed-month (extract-calendar-month date))
  8779. (displayed-year (extract-calendar-year date)))
  8780. (unwind-protect
  8781. (progn
  8782. (fset 'calendar-cursor-to-date
  8783. (lambda (&optional error)
  8784. (calendar-gregorian-from-absolute
  8785. (get-text-property point 'day))))
  8786. (call-interactively cmd))
  8787. (fset 'calendar-cursor-to-date oldf))))
  8788. (defun org-agenda-phases-of-moon ()
  8789. "Display the phases of the moon for the 3 months around the cursor date."
  8790. (interactive)
  8791. (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
  8792. (defun org-agenda-holidays ()
  8793. "Display the holidays for the 3 months around the cursor date."
  8794. (interactive)
  8795. (org-agenda-execute-calendar-command 'list-calendar-holidays))
  8796. (defun org-agenda-sunrise-sunset (arg)
  8797. "Display sunrise and sunset for the cursor date.
  8798. Latitude and longitude can be specified with the variables
  8799. `calendar-latitude' and `calendar-longitude'. When called with prefix
  8800. argument, latitude and longitude will be prompted for."
  8801. (interactive "P")
  8802. (let ((calendar-longitude (if arg nil calendar-longitude))
  8803. (calendar-latitude (if arg nil calendar-latitude))
  8804. (calendar-location-name
  8805. (if arg "the given coordinates" calendar-location-name)))
  8806. (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
  8807. (defun org-agenda-goto-calendar ()
  8808. "Open the Emacs calendar with the date at the cursor."
  8809. (interactive)
  8810. (org-agenda-check-type t 'agenda 'timeline)
  8811. (let* ((day (or (get-text-property (point) 'day)
  8812. (error "Don't know which date to open in calendar")))
  8813. (date (calendar-gregorian-from-absolute day))
  8814. (calendar-move-hook nil)
  8815. (view-calendar-holidays-initially nil)
  8816. (view-diary-entries-initially nil))
  8817. (calendar)
  8818. (calendar-goto-date date)))
  8819. (defun org-calendar-goto-agenda ()
  8820. "Compute the Org-mode agenda for the calendar date displayed at the cursor.
  8821. This is a command that has to be installed in `calendar-mode-map'."
  8822. (interactive)
  8823. (org-agenda-list nil (calendar-absolute-from-gregorian
  8824. (calendar-cursor-to-date))
  8825. nil))
  8826. (defun org-agenda-convert-date ()
  8827. (interactive)
  8828. (org-agenda-check-type t 'agenda 'timeline)
  8829. (let ((day (get-text-property (point) 'day))
  8830. date s)
  8831. (unless day
  8832. (error "Don't know which date to convert"))
  8833. (setq date (calendar-gregorian-from-absolute day))
  8834. (setq s (concat
  8835. "Gregorian: " (calendar-date-string date) "\n"
  8836. "ISO: " (calendar-iso-date-string date) "\n"
  8837. "Day of Yr: " (calendar-day-of-year-string date) "\n"
  8838. "Julian: " (calendar-julian-date-string date) "\n"
  8839. "Astron. JD: " (calendar-astro-date-string date)
  8840. " (Julian date number at noon UTC)\n"
  8841. "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
  8842. "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
  8843. "French: " (calendar-french-date-string date) "\n"
  8844. "Mayan: " (calendar-mayan-date-string date) "\n"
  8845. "Coptic: " (calendar-coptic-date-string date) "\n"
  8846. "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
  8847. "Persian: " (calendar-persian-date-string date) "\n"
  8848. "Chinese: " (calendar-chinese-date-string date) "\n"))
  8849. (with-output-to-temp-buffer "*Dates*"
  8850. (princ s))
  8851. (if (fboundp 'fit-window-to-buffer)
  8852. (fit-window-to-buffer (get-buffer-window "*Dates*")))))
  8853. ;;; Tags
  8854. (defun org-scan-tags (action matcher &optional todo-only)
  8855. "Scan headline tags with inheritance and produce output ACTION.
  8856. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  8857. evaluated, testing if a given set of tags qualifies a headline for
  8858. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  8859. are included in the output."
  8860. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8861. (mapconcat 'regexp-quote
  8862. (nreverse (cdr (reverse org-todo-keywords)))
  8863. "\\|")
  8864. "\\>\\)\\)? *\\(.*?\\)\\(:[A-Za-z_@0-9:]+:\\)?[ \t]*$"))
  8865. (props (list 'face nil
  8866. 'done-face 'org-done
  8867. 'undone-face nil
  8868. 'mouse-face 'highlight
  8869. 'org-not-done-regexp org-not-done-regexp
  8870. 'keymap org-agenda-keymap
  8871. 'help-echo
  8872. (format "mouse-2 or RET jump to org file %s"
  8873. (abbreviate-file-name buffer-file-name))))
  8874. (case-fold-search nil)
  8875. lspos
  8876. tags tags-list tags-alist (llast 0) rtn level category i txt
  8877. todo marker)
  8878. (save-excursion
  8879. (goto-char (point-min))
  8880. (when (eq action 'sparse-tree) (org-overview))
  8881. (while (re-search-forward re nil t)
  8882. (catch :skip
  8883. (and (eq action 'agenda) (org-agenda-skip))
  8884. (setq todo (if (match-end 1) (match-string 2))
  8885. tags (if (match-end 4) (match-string 4)))
  8886. (goto-char (setq lspos (1+ (match-beginning 0))))
  8887. (setq level (funcall outline-level)
  8888. category (org-get-category))
  8889. (setq i llast llast level)
  8890. ;; remove tag lists from same and sublevels
  8891. (while (>= i level)
  8892. (when (setq entry (assoc i tags-alist))
  8893. (setq tags-alist (delete entry tags-alist)))
  8894. (setq i (1- i)))
  8895. ;; add the nex tags
  8896. (when tags
  8897. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8898. tags-alist
  8899. (cons (cons level tags) tags-alist)))
  8900. ;; compile tags for current headline
  8901. (setq tags-list
  8902. (if org-use-tag-inheritance
  8903. (apply 'append (mapcar 'cdr tags-alist))
  8904. tags))
  8905. (when (and (or (not todo-only) todo)
  8906. (eval matcher)
  8907. (or (not org-agenda-skip-archived-trees)
  8908. (not (member org-archive-tag tags-list))))
  8909. ;; list this headline
  8910. (if (eq action 'sparse-tree)
  8911. (progn
  8912. (org-show-context 'tags-tree))
  8913. (setq txt (org-format-agenda-item
  8914. ""
  8915. (concat
  8916. (if org-tags-match-list-sublevels
  8917. (make-string (1- level) ?.) "")
  8918. (org-get-heading))
  8919. category tags-list))
  8920. (goto-char lspos)
  8921. (setq marker (org-agenda-new-marker))
  8922. (org-add-props txt props
  8923. 'org-marker marker 'org-hd-marker marker 'category category)
  8924. (push txt rtn))
  8925. ;; if we are to skip sublevels, jump to end of subtree
  8926. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8927. (when (and (eq action 'sparse-tree)
  8928. (not org-sparse-tree-open-archived-trees))
  8929. (org-hide-archived-subtrees (point-min) (point-max)))
  8930. (nreverse rtn)))
  8931. (defun org-tags-sparse-tree (&optional arg match)
  8932. "Create a sparse tree according to tags string MATCH.
  8933. MATCH can contain positive and negative selection of tags, like
  8934. \"+WORK+URGENT-WITHBOSS\"."
  8935. (interactive "P")
  8936. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match))))
  8937. (defun org-make-tags-matcher (match)
  8938. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8939. (unless match
  8940. ;; Get a new match request, with completion
  8941. (setq org-last-tags-completion-table
  8942. (or org-tag-alist
  8943. org-last-tags-completion-table))
  8944. (setq match (completing-read
  8945. "Match: " 'org-tags-completion-function nil nil nil
  8946. 'org-tags-history))) ; FIXME: Separate history for this?
  8947. ;; Parse the string and create a lisp form
  8948. (let ((match0 match)
  8949. (re "^&?\\([-+:]\\)?\\({[^}]+}\\|[A-Za-z_@0-9]+\\)")
  8950. minus tag mm
  8951. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  8952. orterms term orlist re-p)
  8953. (if (string-match "/+" match)
  8954. ;; match contains also a todo-matching request
  8955. (setq tagsmatch (substring match 0 (match-beginning 0))
  8956. todomatch (substring match (match-end 0)))
  8957. ;; only matching tags
  8958. (setq tagsmatch match todomatch nil))
  8959. ;; Make the tags matcher
  8960. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8961. (setq tagsmatcher t)
  8962. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8963. (while (setq term (pop orterms))
  8964. (while (and (equal (substring term -1) "\\") orterms)
  8965. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8966. (while (string-match re term)
  8967. (setq minus (and (match-end 1)
  8968. (equal (match-string 1 term) "-"))
  8969. tag (match-string 2 term)
  8970. term (substring term (match-end 0))
  8971. re-p (equal (string-to-char tag) ?{)
  8972. mm (if re-p
  8973. `(org-match-any-p ,(substring tag 1 -1) tags-list)
  8974. `(member ,(downcase tag) tags-list))
  8975. mm (if minus (list 'not mm) mm))
  8976. (push mm tagsmatcher))
  8977. (push (if (> (length tagsmatcher) 1)
  8978. (cons 'and tagsmatcher)
  8979. (car tagsmatcher))
  8980. orlist)
  8981. (setq tagsmatcher nil))
  8982. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist))))
  8983. ;; Make the todo matcher
  8984. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8985. (setq todomatcher t)
  8986. (setq orterms (org-split-string todomatch "|") orlist nil)
  8987. (while (setq term (pop orterms))
  8988. (while (string-match re term)
  8989. (setq minus (and (match-end 1)
  8990. (equal (match-string 1 term) "-"))
  8991. kwd (match-string 2 term)
  8992. re-p (equal (string-to-char kwd) ?{)
  8993. term (substring term (match-end 0))
  8994. mm (if re-p
  8995. `(string-match ,(substring kwd 1 -1) todo)
  8996. (list 'equal 'todo kwd))
  8997. mm (if minus (list 'not mm) mm))
  8998. (push mm todomatcher))
  8999. (push (if (> (length todomatcher) 1)
  9000. (cons 'and todomatcher)
  9001. (car todomatcher))
  9002. orlist)
  9003. (setq todomatcher nil))
  9004. (setq todomatcher (if (> (length orlist) 1)
  9005. (cons 'or orlist) (car orlist))))
  9006. ;; Return the string and lisp forms of the matcher
  9007. (setq matcher (if todomatcher
  9008. (list 'and tagsmatcher todomatcher)
  9009. tagsmatcher))
  9010. (cons match0 matcher)))
  9011. (defun org-match-any-p (re list)
  9012. "Does re match any element of list?"
  9013. (setq list (mapcar (lambda (x) (string-match re x)) list))
  9014. (delq nil list))
  9015. ;;;###autoload
  9016. (defun org-tags-view (&optional todo-only match)
  9017. "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
  9018. The prefix arg TODO-ONLY limits the search to TODO entries."
  9019. (interactive "P")
  9020. (org-compile-prefix-format 'tags)
  9021. (org-set-sorting-strategy 'tags)
  9022. (let* ((org-tags-match-list-sublevels
  9023. (if todo-only t org-tags-match-list-sublevels))
  9024. (win (selected-window))
  9025. (completion-ignore-case t)
  9026. rtn rtnall files file pos matcher
  9027. buffer)
  9028. (setq matcher (org-make-tags-matcher match)
  9029. match (car matcher) matcher (cdr matcher))
  9030. (org-prepare-agenda)
  9031. (setq org-agenda-redo-command
  9032. (list 'org-tags-view (list 'quote todo-only)
  9033. (list 'if 'current-prefix-arg nil match)))
  9034. (setq files (org-agenda-files)
  9035. rtnall nil)
  9036. (while (setq file (pop files))
  9037. (catch 'nextfile
  9038. (org-check-agenda-file file)
  9039. (setq buffer (if (file-exists-p file)
  9040. (org-get-agenda-file-buffer file)
  9041. (error "No such file %s" file)))
  9042. (if (not buffer)
  9043. ;; If file does not exist, merror message to agenda
  9044. (setq rtn (list
  9045. (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  9046. rtnall (append rtnall rtn))
  9047. (with-current-buffer buffer
  9048. (unless (org-mode-p)
  9049. (error "Agenda file %s is not in `org-mode'" file))
  9050. (setq org-category-table (org-get-category-table))
  9051. (save-excursion
  9052. (save-restriction
  9053. (if org-agenda-restrict
  9054. (narrow-to-region org-agenda-restrict-begin
  9055. org-agenda-restrict-end)
  9056. (widen))
  9057. (setq rtn (org-scan-tags 'agenda matcher todo-only))
  9058. (setq rtnall (append rtnall rtn))))))))
  9059. (insert "Headlines with TAGS match: ")
  9060. (add-text-properties (point-min) (1- (point))
  9061. (list 'face 'org-level-3))
  9062. (setq pos (point))
  9063. (insert match "\n")
  9064. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  9065. (setq pos (point))
  9066. (unless org-agenda-multi
  9067. (insert "Press `C-u r' to search again with new search string\n"))
  9068. (add-text-properties pos (1- (point)) (list 'face 'org-level-3))
  9069. (when rtnall
  9070. (insert (org-finalize-agenda-entries rtnall) "\n"))
  9071. (goto-char (point-min))
  9072. (org-fit-agenda-window)
  9073. (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
  9074. (org-finalize-agenda)
  9075. (setq buffer-read-only t)
  9076. (if (not org-select-agenda-window) (select-window win))))
  9077. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  9078. (defvar org-tags-overlay (org-make-overlay 1 1))
  9079. (org-detach-overlay org-tags-overlay)
  9080. (defun org-set-tags (&optional arg just-align)
  9081. "Set the tags for the current headline.
  9082. With prefix ARG, realign all tags in headings in the current buffer."
  9083. (interactive "P")
  9084. (let* ((re (concat "^" outline-regexp))
  9085. (current (org-get-tags))
  9086. table current-tags inherited-tags ; computed below when needed
  9087. tags p0 c0 c1 rpl)
  9088. (if arg
  9089. (save-excursion
  9090. (goto-char (point-min))
  9091. (let (buffer-invisibility-spec) ; Emacs 21 compatibility
  9092. (while (re-search-forward re nil t)
  9093. (org-set-tags nil t)
  9094. (end-of-line 1)))
  9095. (message "All tags realigned to column %d" org-tags-column))
  9096. (if just-align
  9097. (setq tags current)
  9098. ;; Get a new set of tags from the user
  9099. (setq table (or org-tag-alist (org-get-buffer-tags))
  9100. org-last-tags-completion-table table
  9101. current-tags (org-split-string current ":")
  9102. inherited-tags (nreverse
  9103. (nthcdr (length current-tags)
  9104. (nreverse (org-get-tags-at))))
  9105. tags
  9106. (if (or (eq t org-use-fast-tag-selection)
  9107. (and org-use-fast-tag-selection
  9108. (delq nil (mapcar 'cdr table))))
  9109. (org-fast-tag-selection current-tags inherited-tags table)
  9110. (let ((org-add-colon-after-tag-completion t))
  9111. (org-trim
  9112. (completing-read "Tags: " 'org-tags-completion-function
  9113. nil nil current 'org-tags-history)))))
  9114. (while (string-match "[-+&]+" tags)
  9115. ;; No boolean logic, just a list
  9116. (setq tags (replace-match ":" t t tags))))
  9117. (if (string-match "\\`[\t ]*\\'" tags)
  9118. (setq tags "")
  9119. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  9120. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  9121. ;; Insert new tags at the correct column
  9122. (beginning-of-line 1)
  9123. (if (re-search-forward
  9124. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  9125. (point-at-eol) t)
  9126. (progn
  9127. (if (equal tags "")
  9128. (setq rpl "")
  9129. (goto-char (match-beginning 0))
  9130. (setq c0 (current-column) p0 (point)
  9131. c1 (max (1+ c0) (if (> org-tags-column 0)
  9132. org-tags-column
  9133. (- (- org-tags-column) (length tags))))
  9134. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  9135. (replace-match rpl)
  9136. (and (not (featurep 'xemacs)) c0 (tabify p0 (point)))
  9137. tags)
  9138. (error "Tags alignment failed")))))
  9139. (defun org-tags-completion-function (string predicate &optional flag)
  9140. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  9141. (confirm (lambda (x) (stringp (car x)))))
  9142. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  9143. (setq s1 (match-string 1 string)
  9144. s2 (match-string 2 string))
  9145. (setq s1 "" s2 string))
  9146. (cond
  9147. ((eq flag nil)
  9148. ;; try completion
  9149. (setq rtn (try-completion s2 ctable confirm))
  9150. (if (stringp rtn)
  9151. (concat s1 s2 (substring rtn (length s2))
  9152. (if (and org-add-colon-after-tag-completion
  9153. (assoc rtn ctable))
  9154. ":" "")))
  9155. )
  9156. ((eq flag t)
  9157. ;; all-completions
  9158. (all-completions s2 ctable confirm)
  9159. )
  9160. ((eq flag 'lambda)
  9161. ;; exact match?
  9162. (assoc s2 ctable)))
  9163. ))
  9164. (defun org-fast-tag-insert (kwd tags face &optional end)
  9165. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  9166. (insert (format "%-12s" (concat kwd ":"))
  9167. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  9168. (or end "")))
  9169. (defun org-fast-tag-show-exit (flag)
  9170. (save-excursion
  9171. (goto-line 3)
  9172. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  9173. (replace-match ""))
  9174. (when flag
  9175. (end-of-line 1)
  9176. (move-to-column (- (window-width) 19) t)
  9177. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  9178. (defun org-set-current-tags-overlay (current prefix)
  9179. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  9180. (if (featurep 'xemacs)
  9181. (org-overlay-display org-tags-overlay (concat prefix s)
  9182. 'secondary-selection)
  9183. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  9184. (org-overlay-display org-tags-overlay (concat prefix s)))))
  9185. (defun org-fast-tag-selection (current inherited table)
  9186. "Fast tag selection with single keys.
  9187. CURRENT is the current list of tags in the headline, INHERITED is the
  9188. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  9189. possibly with grouping information.
  9190. If the keys are nil, a-z are automatically assigned.
  9191. Returns the new tags string, or nil to not change the current settings."
  9192. (let* ((maxlen (apply 'max (mapcar
  9193. (lambda (x)
  9194. (if (stringp (car x)) (string-width (car x)) 0))
  9195. table)))
  9196. (buf (current-buffer))
  9197. (buffer-tags nil)
  9198. (fwidth (+ maxlen 3 1 3))
  9199. (ncol (/ (- (window-width) 4) fwidth))
  9200. (i-face 'org-done)
  9201. (c-face 'org-tag)
  9202. tg cnt e c char c1 c2 ntable tbl rtn
  9203. ov-start ov-end ov-prefix
  9204. (exit-after-next org-fast-tag-selection-single-key)
  9205. groups ingroup)
  9206. (save-excursion
  9207. (beginning-of-line 1)
  9208. (if (looking-at ".*[ \t]\\(:[A-Za-z_@0-9:]+:\\)[ \t]*\\(\r\\|$\\)")
  9209. (setq ov-start (match-beginning 1)
  9210. ov-end (match-end 1)
  9211. ov-prefix "")
  9212. (setq ov-start (1- (point-at-eol))
  9213. ov-end (1+ ov-start))
  9214. (skip-chars-forward "^\n\r")
  9215. (setq ov-prefix
  9216. (concat
  9217. (buffer-substring (1- (point)) (point))
  9218. (if (> (current-column) org-tags-column)
  9219. " "
  9220. (make-string (- org-tags-column (current-column)) ?\ ))))))
  9221. (org-move-overlay org-tags-overlay ov-start ov-end)
  9222. (save-window-excursion
  9223. ;; FIXME: would it be better to keep the other windows?
  9224. (delete-other-windows)
  9225. (split-window-vertically)
  9226. (switch-to-buffer-other-window (get-buffer-create " *Org tags*"))
  9227. (erase-buffer)
  9228. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  9229. (org-fast-tag-insert "Current" current c-face "\n\n")
  9230. (org-fast-tag-show-exit exit-after-next)
  9231. (org-set-current-tags-overlay current ov-prefix)
  9232. (setq tbl table char ?a cnt 0)
  9233. (while (setq e (pop tbl))
  9234. (cond
  9235. ((equal e '(:startgroup))
  9236. (push '() groups) (setq ingroup t)
  9237. (when (not (= cnt 0))
  9238. (setq cnt 0)
  9239. (insert "\n"))
  9240. (insert "{ "))
  9241. ((equal e '(:endgroup))
  9242. (setq ingroup nil cnt 0)
  9243. (insert "}\n"))
  9244. (t
  9245. (setq tg (car e) c2 nil)
  9246. (if (cdr e)
  9247. (setq c (cdr e))
  9248. ;; automatically assign a character.
  9249. (setq c1 (string-to-char
  9250. (downcase (substring
  9251. tg (if (= (string-to-char tg) ?@) 1 0)))))
  9252. (if (or (rassoc c1 ntable) (rassoc c1 table))
  9253. (while (or (rassoc char ntable) (rassoc char table))
  9254. (setq char (1+ char)))
  9255. (setq c2 c1))
  9256. (setq c (or c2 char)))
  9257. (if ingroup (push tg (car groups)))
  9258. (setq tg (org-add-props tg nil 'face
  9259. (cond
  9260. ((member tg current) c-face)
  9261. ((member tg inherited) i-face)
  9262. (t nil))))
  9263. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9264. (insert "[" c "] " tg (make-string
  9265. (- fwidth 4 (length tg)) ?\ ))
  9266. (push (cons tg c) ntable)
  9267. (when (= (setq cnt (1+ cnt)) ncol)
  9268. (insert "\n")
  9269. (if ingroup (insert " "))
  9270. (setq cnt 0)))))
  9271. (setq ntable (nreverse ntable))
  9272. (insert "\n")
  9273. (goto-char (point-min))
  9274. (if (fboundp 'fit-window-to-buffer) (fit-window-to-buffer))
  9275. (setq rtn
  9276. (catch 'exit
  9277. (while t
  9278. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [C-c]: multi%s"
  9279. (if groups " [!] no groups" ""))
  9280. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9281. (cond
  9282. ((= c ?\r) (throw 'exit t))
  9283. ((= c ?!)
  9284. (setq groups nil)
  9285. (goto-char (point-min))
  9286. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  9287. ((= c ?\C-c)
  9288. (org-fast-tag-show-exit
  9289. (setq exit-after-next (not exit-after-next))))
  9290. ((or (= c ?\C-g)
  9291. (and (= c ?q) (not (rassoc c ntable))))
  9292. (org-detach-overlay org-tags-overlay)
  9293. (setq quit-flag t))
  9294. ((= c ?\ )
  9295. (setq current nil)
  9296. (if exit-after-next (setq exit-after-next 'now)))
  9297. ((= c ?\t)
  9298. (condition-case nil
  9299. (setq tg (completing-read
  9300. "Tag: "
  9301. (or buffer-tags
  9302. (with-current-buffer buf
  9303. (org-get-buffer-tags)))))
  9304. (quit (setq tg "")))
  9305. (when (string-match "\\S-" tg)
  9306. (add-to-list 'buffer-tags (list tg))
  9307. (if (member tg current)
  9308. (setq current (delete tg current))
  9309. (push tg current)))
  9310. (if exit-after-next (setq exit-after-next 'now)))
  9311. ((setq e (rassoc c ntable) tg (car e))
  9312. (if (member tg current)
  9313. (setq current (delete tg current))
  9314. (loop for g in groups do
  9315. (if (member tg g)
  9316. (mapcar (lambda (x)
  9317. (setq current (delete x current)))
  9318. g)))
  9319. (push tg current))
  9320. (if exit-after-next (setq exit-after-next 'now))))
  9321. ;; Create a sorted list
  9322. (setq current
  9323. (sort current
  9324. (lambda (a b)
  9325. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  9326. (if (eq exit-after-next 'now) (throw 'exit t))
  9327. (goto-char (point-min))
  9328. (beginning-of-line 2)
  9329. (delete-region (point) (point-at-eol))
  9330. (org-fast-tag-insert "Current" current c-face)
  9331. (org-set-current-tags-overlay current ov-prefix)
  9332. (while (re-search-forward "\\[.\\] \\([a-zA-Z0-9_@]+\\)" nil t)
  9333. (setq tg (match-string 1))
  9334. (add-text-properties (match-beginning 1) (match-end 1)
  9335. (list 'face
  9336. (cond
  9337. ((member tg current) c-face)
  9338. ((member tg inherited) i-face)
  9339. (t nil)))))
  9340. (goto-char (point-min)))))
  9341. (org-detach-overlay org-tags-overlay)
  9342. (if rtn
  9343. (mapconcat 'identity current ":")
  9344. nil))))
  9345. (defun org-get-tags ()
  9346. "Get the TAGS string in the current headline."
  9347. (unless (org-on-heading-p t)
  9348. (error "Not on a heading"))
  9349. (save-excursion
  9350. (beginning-of-line 1)
  9351. (if (looking-at ".*[ \t]\\(:[A-Za-z_@0-9:]+:\\)[ \t]*\\(\r\\|$\\)")
  9352. (org-match-string-no-properties 1)
  9353. "")))
  9354. (defun org-get-buffer-tags ()
  9355. "Get a table of all tags used in the buffer, for completion."
  9356. (let (tags)
  9357. (save-excursion
  9358. (goto-char (point-min))
  9359. (while (re-search-forward "[ \t]:\\([A-Za-z_@0-9:]+\\):[ \t\r\n]" nil t)
  9360. (mapc (lambda (x) (add-to-list 'tags x))
  9361. (org-split-string (org-match-string-no-properties 1) ":"))))
  9362. (mapcar 'list tags)))
  9363. ;;; Link Stuff
  9364. (defvar org-create-file-search-functions nil
  9365. "List of functions to construct the right search string for a file link.
  9366. These functions are called in turn with point at the location to
  9367. which the link should point.
  9368. A function in the hook should first test if it would like to
  9369. handle this file type, for example by checking the major-mode or
  9370. the file extension. If it decides not to handle this file, it
  9371. should just return nil to give other functions a chance. If it
  9372. does handle the file, it must return the search string to be used
  9373. when following the link. The search string will be part of the
  9374. file link, given after a double colon, and `org-open-at-point'
  9375. will automatically search for it. If special measures must be
  9376. taken to make the search successful, another function should be
  9377. added to the companion hook `org-execute-file-search-functions',
  9378. which see.
  9379. A function in this hook may also use `setq' to set the variable
  9380. `description' to provide a suggestion for the descriptive text to
  9381. be used for this link when it gets inserted into an Org-mode
  9382. buffer with \\[org-insert-link].")
  9383. (defvar org-execute-file-search-functions nil
  9384. "List of functions to execute a file search triggered by a link.
  9385. Functions added to this hook must accept a single argument, the
  9386. search string that was part of the file link, the part after the
  9387. double colon. The function must first check if it would like to
  9388. handle this search, for example by checking the major-mode or the
  9389. file extension. If it decides not to handle this search, it
  9390. should just return nil to give other functions a chance. If it
  9391. does handle the search, it must return a non-nil value to keep
  9392. other functions from trying.
  9393. Each function can access the current prefix argument through the
  9394. variable `current-prefix-argument'. Note that a single prefix is
  9395. used to force opening a link in Emacs, so it may be good to only
  9396. use a numeric or double prefix to guide the search function.
  9397. In case this is needed, a function in this hook can also restore
  9398. the window configuration before `org-open-at-point' was called using:
  9399. (set-window-configuration org-window-config-before-follow-link)")
  9400. (defun org-find-file-at-mouse (ev)
  9401. "Open file link or URL at mouse."
  9402. (interactive "e")
  9403. (mouse-set-point ev)
  9404. (org-open-at-point 'in-emacs))
  9405. (defun org-open-at-mouse (ev)
  9406. "Open file link or URL at mouse."
  9407. (interactive "e")
  9408. (mouse-set-point ev)
  9409. (org-open-at-point))
  9410. (defvar org-window-config-before-follow-link nil
  9411. "The window configuration before following a link.
  9412. This is saved in case the need arises to restore it.")
  9413. ;; FIXME: IN-EMACS is used for many purposes, maybe rename this argument???
  9414. (defun org-open-at-point (&optional in-emacs)
  9415. "Open link at or after point.
  9416. If there is no link at point, this function will search forward up to
  9417. the end of the current subtree.
  9418. Normally, files will be opened by an appropriate application. If the
  9419. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  9420. (interactive "P")
  9421. (setq org-window-config-before-follow-link (current-window-configuration))
  9422. (org-remove-occur-highlights nil nil t)
  9423. (if (org-at-timestamp-p t)
  9424. (org-follow-timestamp-link)
  9425. (let (type path link line search (pos (point)))
  9426. (catch 'match
  9427. (save-excursion
  9428. (skip-chars-forward "^]\n\r")
  9429. (when (and (re-search-backward "\\[\\[" nil t)
  9430. (looking-at org-bracket-link-regexp)
  9431. (<= (match-beginning 0) pos)
  9432. (>= (match-end 0) pos))
  9433. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  9434. (while (string-match " *\n *" link)
  9435. (setq link (replace-match " " t t link)))
  9436. (setq link (org-link-expand-abbrev link))
  9437. (if (string-match org-link-re-with-space2 link)
  9438. (setq type (match-string 1 link)
  9439. path (match-string 2 link))
  9440. (setq type "thisfile"
  9441. path link))
  9442. (throw 'match t)))
  9443. (when (get-text-property (point) 'org-linked-text)
  9444. (setq type "thisfile"
  9445. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  9446. (1+ (point)) (point))
  9447. path (buffer-substring
  9448. (previous-single-property-change pos 'org-linked-text)
  9449. (next-single-property-change pos 'org-linked-text)))
  9450. (throw 'match t))
  9451. (save-excursion
  9452. (skip-chars-backward (concat "^[]" org-non-link-chars " "))
  9453. (if (equal (char-before) ?<) (backward-char 1))
  9454. (when (or (looking-at org-angle-link-re)
  9455. (looking-at org-plain-link-re)
  9456. (and (or (re-search-forward org-angle-link-re (point-at-eol) t)
  9457. (re-search-forward org-plain-link-re (point-at-eol) t))
  9458. (<= (match-beginning 0) pos)
  9459. (>= (match-end 0) pos)))
  9460. (setq type (match-string 1)
  9461. path (match-string 2))
  9462. (throw 'match t)))
  9463. (save-excursion
  9464. (skip-chars-backward "^ \t\n\r")
  9465. (when (looking-at "\\(:[A-Za-z_@0-9:]+\\):[ \t\r\n]")
  9466. (setq type "tags"
  9467. path (match-string 1))
  9468. (while (string-match ":" path)
  9469. (setq path (replace-match "+" t t path)))
  9470. (throw 'match t)))
  9471. (save-excursion
  9472. (skip-chars-backward "a-zA-Z_")
  9473. (when (and (memq 'camel org-activate-links)
  9474. (looking-at org-camel-regexp))
  9475. (setq type "camel" path (match-string 0))
  9476. (if (equal (char-before) ?*)
  9477. (setq path (concat "*" path))))
  9478. (throw 'match t)))
  9479. (unless path
  9480. (error "No link found"))
  9481. ;; Remove any trailing spaces in path
  9482. (if (string-match " +\\'" path)
  9483. (setq path (replace-match "" t t path)))
  9484. (cond
  9485. ((equal type "mailto")
  9486. (let ((cmd (car org-link-mailto-program))
  9487. (args (cdr org-link-mailto-program)) args1
  9488. (address path) (subject "") a)
  9489. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  9490. (setq address (match-string 1 path)
  9491. subject (org-link-escape (match-string 2 path))))
  9492. (while args
  9493. (cond
  9494. ((not (stringp (car args))) (push (pop args) args1))
  9495. (t (setq a (pop args))
  9496. (if (string-match "%a" a)
  9497. (setq a (replace-match address t t a)))
  9498. (if (string-match "%s" a)
  9499. (setq a (replace-match subject t t a)))
  9500. (push a args1))))
  9501. (apply cmd (nreverse args1))))
  9502. ((member type '("http" "https" "ftp" "news"))
  9503. (browse-url (concat type ":" path)))
  9504. ((string= type "tags")
  9505. (org-tags-view in-emacs path))
  9506. ((or (string= type "camel")
  9507. (string= type "thisfile"))
  9508. (if in-emacs
  9509. (switch-to-buffer-other-window
  9510. (org-get-buffer-for-internal-link (current-buffer)))
  9511. (org-mark-ring-push))
  9512. (org-link-search
  9513. path
  9514. (cond ((equal in-emacs '(4)) 'occur)
  9515. ((equal in-emacs '(16)) 'org-occur)
  9516. (t nil))))
  9517. ((string= type "file")
  9518. (if (string-match "::\\([0-9]+\\)\\'" path)
  9519. (setq line (string-to-number (match-string 1 path))
  9520. path (substring path 0 (match-beginning 0)))
  9521. (if (string-match "::\\(.+\\)\\'" path)
  9522. (setq search (match-string 1 path)
  9523. path (substring path 0 (match-beginning 0)))))
  9524. (org-open-file path in-emacs line search))
  9525. ((string= type "news")
  9526. (org-follow-gnus-link path))
  9527. ((string= type "bbdb")
  9528. (org-follow-bbdb-link path))
  9529. ((string= type "info")
  9530. (org-follow-info-link path))
  9531. ((string= type "gnus")
  9532. (let (group article)
  9533. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  9534. (error "Error in Gnus link"))
  9535. (setq group (match-string 1 path)
  9536. article (match-string 3 path))
  9537. (org-follow-gnus-link group article)))
  9538. ((string= type "vm")
  9539. (let (folder article)
  9540. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  9541. (error "Error in VM link"))
  9542. (setq folder (match-string 1 path)
  9543. article (match-string 3 path))
  9544. ;; in-emacs is the prefix arg, will be interpreted as read-only
  9545. (org-follow-vm-link folder article in-emacs)))
  9546. ((string= type "wl")
  9547. (let (folder article)
  9548. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  9549. (error "Error in Wanderlust link"))
  9550. (setq folder (match-string 1 path)
  9551. article (match-string 3 path))
  9552. (org-follow-wl-link folder article)))
  9553. ((string= type "mhe")
  9554. (let (folder article)
  9555. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  9556. (error "Error in MHE link"))
  9557. (setq folder (match-string 1 path)
  9558. article (match-string 3 path))
  9559. (org-follow-mhe-link folder article)))
  9560. ((string= type "rmail")
  9561. (let (folder article)
  9562. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  9563. (error "Error in RMAIL link"))
  9564. (setq folder (match-string 1 path)
  9565. article (match-string 3 path))
  9566. (org-follow-rmail-link folder article)))
  9567. ((string= type "shell")
  9568. (let ((cmd path))
  9569. (while (string-match "@{" cmd) ; FIXME: not needed for [[]] links
  9570. (setq cmd (replace-match "<" t t cmd)))
  9571. (while (string-match "@}" cmd) ; FIXME: not needed for [[]] links
  9572. (setq cmd (replace-match ">" t t cmd)))
  9573. (if (or (not org-confirm-shell-link-function)
  9574. (funcall org-confirm-shell-link-function
  9575. (format "Execute \"%s\" in shell? "
  9576. (org-add-props cmd nil
  9577. 'face 'org-warning))))
  9578. (progn
  9579. (message "Executing %s" cmd)
  9580. (shell-command cmd))
  9581. (error "Abort"))))
  9582. ((string= type "elisp")
  9583. (let ((cmd path))
  9584. (if (or (not org-confirm-elisp-link-function)
  9585. (funcall org-confirm-elisp-link-function
  9586. (format "Execute \"%s\" as elisp? "
  9587. (org-add-props cmd nil
  9588. 'face 'org-warning))))
  9589. (message "%s => %s" cmd (eval (read cmd)))
  9590. (error "Abort"))))
  9591. (t
  9592. (browse-url-at-point))))))
  9593. (defun org-link-expand-abbrev (link)
  9594. "Apply replacements as defined in `org-link-abbrev-alist."
  9595. (if (string-match "^\\([a-zA-Z]+\\)\\(::\\(.*\\)\\)?$" link)
  9596. (let* ((key (match-string 1 link))
  9597. (as (or (assoc key org-link-abbrev-alist-local)
  9598. (assoc key org-link-abbrev-alist)))
  9599. (tag (and (match-end 2) (match-string 3 link)))
  9600. rpl)
  9601. (if (not as)
  9602. link
  9603. (setq rpl (cdr as))
  9604. (cond
  9605. ((symbolp rpl) (funcall rpl tag))
  9606. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  9607. (t (concat rpl tag)))))
  9608. link))
  9609. (defun org-link-search (s &optional type)
  9610. "Search for a link search option.
  9611. When S is a CamelCaseWord, search for a target, or for a sentence containing
  9612. the words. If S is surrounded by forward slashes, it is interpreted as a
  9613. regular expression. In org-mode files, this will create an `org-occur'
  9614. sparse tree. In ordinary files, `occur' will be used to list matches.
  9615. If the current buffer is in `dired-mode', grep will be used to search
  9616. in all files."
  9617. (let ((case-fold-search t)
  9618. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9619. (pos (point))
  9620. (pre "") (post "")
  9621. words re0 re1 re2 re3 re4 re5 re2a reall camel)
  9622. (cond
  9623. ;; First check if there are any special
  9624. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9625. ;; Now try the builtin stuff
  9626. ((save-excursion
  9627. (goto-char (point-min))
  9628. (and
  9629. (re-search-forward
  9630. (concat "<<" (regexp-quote s0) ">>") nil t)
  9631. (setq pos (match-beginning 0))))
  9632. ;; There is an exact target for this
  9633. (goto-char pos))
  9634. ((string-match "^/\\(.*\\)/$" s)
  9635. ;; A regular expression
  9636. (cond
  9637. ((org-mode-p)
  9638. (org-occur (match-string 1 s)))
  9639. ;;((eq major-mode 'dired-mode)
  9640. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  9641. (t (org-do-occur (match-string 1 s)))))
  9642. ((or (setq camel (string-match (concat "^" org-camel-regexp "$") s))
  9643. t)
  9644. ;; A camel or a normal search string
  9645. (when (equal (string-to-char s) ?*)
  9646. ;; Anchor on headlines, post may include tags.
  9647. (setq pre "^\\*+[ \t]*\\(?:\\sw+\\)?[ \t]*"
  9648. post "[ \t]*\\(?:[ \t]+:[a-zA-Z_@0-9:+]:[ \t]*\\)?$"
  9649. s (substring s 1)))
  9650. (remove-text-properties
  9651. 0 (length s)
  9652. '(face nil mouse-face nil keymap nil fontified nil) s)
  9653. ;; Make a series of regular expressions to find a match
  9654. (setq words
  9655. (if camel
  9656. (org-camel-to-words s)
  9657. (org-split-string s "[ \n\r\t]+"))
  9658. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9659. re2 (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t]+") "\\)[ \t\r\n]")
  9660. re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  9661. re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9662. re1 (concat pre re2 post)
  9663. re3 (concat pre re4 post)
  9664. re5 (concat pre ".*" re4)
  9665. re2 (concat pre re2)
  9666. re2a (concat pre re2a)
  9667. re4 (concat pre re4)
  9668. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9669. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9670. re5 "\\)"
  9671. ))
  9672. (cond
  9673. ((eq type 'org-occur) (org-occur reall))
  9674. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9675. (t (goto-char (point-min))
  9676. (if (or (org-search-not-link re0 nil t)
  9677. (org-search-not-link re1 nil t)
  9678. (org-search-not-link re2 nil t)
  9679. (org-search-not-link re2a nil t)
  9680. (org-search-not-link re3 nil t)
  9681. (org-search-not-link re4 nil t)
  9682. (org-search-not-link re5 nil t)
  9683. )
  9684. (goto-char (match-beginning 1))
  9685. (goto-char pos)
  9686. (error "No match")))))
  9687. (t
  9688. ;; Normal string-search
  9689. (goto-char (point-min))
  9690. (if (search-forward s nil t)
  9691. (goto-char (match-beginning 0))
  9692. (error "No match"))))
  9693. (and (org-mode-p) (org-show-context 'link-search))))
  9694. (defun org-search-not-link (&rest args)
  9695. "Execute `re-search-forward', but only accept matches that are not a link."
  9696. (catch 'exit
  9697. (let (p1)
  9698. (while (apply 're-search-forward args)
  9699. (setq p1 (point))
  9700. (if (not (save-match-data
  9701. (and (re-search-backward "\\[\\[" nil t)
  9702. (looking-at org-bracket-link-regexp)
  9703. (<= (match-beginning 0) p1)
  9704. (>= (match-end 0) p1))))
  9705. (progn (goto-char (match-end 0))
  9706. (throw 'exit (point)))
  9707. (goto-char (match-end 0)))))))
  9708. (defun org-get-buffer-for-internal-link (buffer)
  9709. "Return a buffer to be used for displaying the link target of internal links."
  9710. (cond
  9711. ((not org-display-internal-link-with-indirect-buffer)
  9712. buffer)
  9713. ((string-match "(Clone)$" (buffer-name buffer))
  9714. (message "Buffer is already a clone, not making another one")
  9715. ;; we also do not modify visibility in this case
  9716. buffer)
  9717. (t ; make a new indirect buffer for displaying the link
  9718. (let* ((bn (buffer-name buffer))
  9719. (ibn (concat bn "(Clone)"))
  9720. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9721. (with-current-buffer ib (org-overview))
  9722. ib))))
  9723. (defun org-do-occur (regexp &optional cleanup)
  9724. "Call the Emacs command `occur'.
  9725. If CLEANUP is non-nil, remove the printout of the regular expression
  9726. in the *Occur* buffer. This is useful if the regex is long and not useful
  9727. to read."
  9728. (occur regexp)
  9729. (when cleanup
  9730. (let ((cwin (selected-window)) win beg end)
  9731. (when (setq win (get-buffer-window "*Occur*"))
  9732. (select-window win))
  9733. (goto-char (point-min))
  9734. (when (re-search-forward "match[a-z]+" nil t)
  9735. (setq beg (match-end 0))
  9736. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9737. (setq end (1- (match-beginning 0)))))
  9738. (and beg end (let ((buffer-read-only)) (delete-region beg end)))
  9739. (goto-char (point-min))
  9740. (select-window cwin))))
  9741. (defvar org-mark-ring nil
  9742. "Mark ring for positions before jumps in Org-mode.")
  9743. (defvar org-mark-ring-last-goto nil
  9744. "Last position in the mark ring used to go back.")
  9745. ;; Fill and close the ring
  9746. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9747. (loop for i from 1 to org-mark-ring-length do
  9748. (push (make-marker) org-mark-ring))
  9749. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9750. org-mark-ring)
  9751. (defun org-mark-ring-push (&optional pos buffer)
  9752. "Put the current position or POS into the mark ring and rotate it."
  9753. (interactive)
  9754. (setq pos (or pos (point)))
  9755. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9756. (move-marker (car org-mark-ring)
  9757. (or pos (point))
  9758. (or buffer (current-buffer)))
  9759. (message
  9760. (substitute-command-keys
  9761. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9762. (defun org-mark-ring-goto (&optional n)
  9763. "Jump to the previous position in the mark ring.
  9764. With prefix arg N, jump back that many stored positions. When
  9765. called several times in succession, walk through the entire ring.
  9766. Org-mode commands jumping to a different position in the current file,
  9767. or to another Org-mode file, automatically push the old position
  9768. onto the ring."
  9769. (interactive "p")
  9770. (let (p m)
  9771. (if (eq last-command this-command)
  9772. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9773. (setq p org-mark-ring))
  9774. (setq org-mark-ring-last-goto p)
  9775. (setq m (car p))
  9776. (switch-to-buffer (marker-buffer m))
  9777. (goto-char m)
  9778. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9779. (defun org-camel-to-words (s)
  9780. "Split \"CamelCaseWords\" to (\"Camel\" \"Case\" \"Words\")."
  9781. (let ((case-fold-search nil)
  9782. words)
  9783. (while (string-match "[a-z][A-Z]" s)
  9784. (push (substring s 0 (1+ (match-beginning 0))) words)
  9785. (setq s (substring s (1+ (match-beginning 0)))))
  9786. (nreverse (cons s words))))
  9787. (defun org-remove-angle-brackets (s)
  9788. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9789. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9790. s)
  9791. (defun org-add-angle-brackets (s)
  9792. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9793. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9794. s)
  9795. (defun org-follow-timestamp-link ()
  9796. (cond
  9797. ((org-at-date-range-p t)
  9798. (let ((org-agenda-start-on-weekday)
  9799. (t1 (match-string 1))
  9800. (t2 (match-string 2)))
  9801. (setq t1 (time-to-days (org-time-string-to-time t1))
  9802. t2 (time-to-days (org-time-string-to-time t2)))
  9803. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  9804. ((org-at-timestamp-p t)
  9805. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9806. (substring (match-string 1) 0 10)))
  9807. 1))
  9808. (t (error "This should not happen"))))
  9809. (defun org-follow-bbdb-link (name)
  9810. "Follow a BBDB link to NAME."
  9811. (require 'bbdb)
  9812. (let ((inhibit-redisplay t)
  9813. (bbdb-electric-p nil))
  9814. (catch 'exit
  9815. ;; Exact match on name
  9816. (bbdb-name (concat "\\`" name "\\'") nil)
  9817. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  9818. ;; Exact match on name
  9819. (bbdb-company (concat "\\`" name "\\'") nil)
  9820. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  9821. ;; Partial match on name
  9822. (bbdb-name name nil)
  9823. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  9824. ;; Partial match on company
  9825. (bbdb-company name nil)
  9826. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  9827. ;; General match including network address and notes
  9828. (bbdb name nil)
  9829. (when (= 0 (buffer-size (get-buffer "*BBDB*")))
  9830. (delete-window (get-buffer-window "*BBDB*"))
  9831. (error "No matching BBDB record")))))
  9832. (defun org-follow-info-link (name)
  9833. "Follow an info file & node link to NAME."
  9834. (if (or (string-match "\\(.*\\)::?\\(.*\\)" name)
  9835. (string-match "\\(.*\\)" name))
  9836. (progn
  9837. (require 'info)
  9838. (if (match-string 2 name) ; If there isn't a node, choose "Top"
  9839. (Info-find-node (match-string 1 name) (match-string 2 name))
  9840. (Info-find-node (match-string 1 name) "Top")))
  9841. (message (concat "Could not open: " name))))
  9842. (defun org-follow-gnus-link (&optional group article)
  9843. "Follow a Gnus link to GROUP and ARTICLE."
  9844. (require 'gnus)
  9845. (funcall (cdr (assq 'gnus org-link-frame-setup)))
  9846. (if gnus-other-frame-object (select-frame gnus-other-frame-object))
  9847. (if group (gnus-fetch-group group))
  9848. (if article
  9849. (or (gnus-summary-goto-article article nil 'force)
  9850. (if (fboundp 'gnus-summary-insert-cached-articles)
  9851. (progn
  9852. (gnus-summary-insert-cached-articles)
  9853. (gnus-summary-goto-article article nil 'force))
  9854. (message "Message could not be found.")))))
  9855. (defun org-follow-vm-link (&optional folder article readonly)
  9856. "Follow a VM link to FOLDER and ARTICLE."
  9857. (require 'vm)
  9858. (setq article (org-add-angle-brackets article))
  9859. (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
  9860. ;; ange-ftp or efs or tramp access
  9861. (let ((user (or (match-string 1 folder) (user-login-name)))
  9862. (host (match-string 2 folder))
  9863. (file (match-string 3 folder)))
  9864. (cond
  9865. ((featurep 'tramp)
  9866. ;; use tramp to access the file
  9867. (if (featurep 'xemacs)
  9868. (setq folder (format "[%s@%s]%s" user host file))
  9869. (setq folder (format "/%s@%s:%s" user host file))))
  9870. (t
  9871. ;; use ange-ftp or efs
  9872. (require (if (featurep 'xemacs) 'efs 'ange-ftp))
  9873. (setq folder (format "/%s@%s:%s" user host file))))))
  9874. (when folder
  9875. (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
  9876. (sit-for 0.1)
  9877. (when article
  9878. (vm-select-folder-buffer)
  9879. (widen)
  9880. (let ((case-fold-search t))
  9881. (goto-char (point-min))
  9882. (if (not (re-search-forward
  9883. (concat "^" "message-id: *" (regexp-quote article))))
  9884. (error "Could not find the specified message in this folder"))
  9885. (vm-isearch-update)
  9886. (vm-isearch-narrow)
  9887. (vm-beginning-of-message)
  9888. (vm-summarize)))))
  9889. (defun org-follow-wl-link (folder article)
  9890. "Follow a Wanderlust link to FOLDER and ARTICLE."
  9891. (setq article (org-add-angle-brackets article))
  9892. (wl-summary-goto-folder-subr folder 'no-sync t nil t nil nil)
  9893. (if article (wl-summary-jump-to-msg-by-message-id article))
  9894. (wl-summary-redisplay))
  9895. (defun org-follow-rmail-link (folder article)
  9896. "Follow an RMAIL link to FOLDER and ARTICLE."
  9897. (setq article (org-add-angle-brackets article))
  9898. (let (message-number)
  9899. (save-excursion
  9900. (save-window-excursion
  9901. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  9902. (setq message-number
  9903. (save-restriction
  9904. (widen)
  9905. (goto-char (point-max))
  9906. (if (re-search-backward
  9907. (concat "^Message-ID:\\s-+" (regexp-quote
  9908. (or article "")))
  9909. nil t)
  9910. (rmail-what-message))))))
  9911. (if message-number
  9912. (progn
  9913. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  9914. (rmail-show-message message-number)
  9915. message-number)
  9916. (error "Message not found"))))
  9917. ;; mh-e integration based on planner-mode
  9918. (defun org-mhe-get-message-real-folder ()
  9919. "Return the name of the current message real folder, so if you use
  9920. sequences, it will now work."
  9921. (save-excursion
  9922. (let* ((folder
  9923. (if (equal major-mode 'mh-folder-mode)
  9924. mh-current-folder
  9925. ;; Refer to the show buffer
  9926. mh-show-folder-buffer))
  9927. (end-index
  9928. (if (boundp 'mh-index-folder)
  9929. (min (length mh-index-folder) (length folder))))
  9930. )
  9931. ;; a simple test on mh-index-data does not work, because
  9932. ;; mh-index-data is always nil in a show buffer.
  9933. (if (and (boundp 'mh-index-folder)
  9934. (string= mh-index-folder (substring folder 0 end-index)))
  9935. (if (equal major-mode 'mh-show-mode)
  9936. (save-window-excursion
  9937. (when (buffer-live-p (get-buffer folder))
  9938. (progn
  9939. (pop-to-buffer folder)
  9940. (org-mhe-get-message-folder-from-index)
  9941. )
  9942. ))
  9943. (org-mhe-get-message-folder-from-index)
  9944. )
  9945. folder
  9946. )
  9947. )))
  9948. (defun org-mhe-get-message-folder-from-index ()
  9949. "Returns the name of the message folder in a index folder buffer."
  9950. (save-excursion
  9951. (mh-index-previous-folder)
  9952. (re-search-forward "^\\(+.*\\)$" nil t)
  9953. (message (match-string 1))))
  9954. (defun org-mhe-get-message-folder ()
  9955. "Return the name of the current message folder. Be careful if you
  9956. use sequences."
  9957. (save-excursion
  9958. (if (equal major-mode 'mh-folder-mode)
  9959. mh-current-folder
  9960. ;; Refer to the show buffer
  9961. mh-show-folder-buffer)))
  9962. (defun org-mhe-get-message-num ()
  9963. "Return the number of the current message. Be careful if you
  9964. use sequences."
  9965. (save-excursion
  9966. (if (equal major-mode 'mh-folder-mode)
  9967. (mh-get-msg-num nil)
  9968. ;; Refer to the show buffer
  9969. (mh-show-buffer-message-number))))
  9970. (defun org-mhe-get-header (header)
  9971. "Return a header of the message in folder mode. This will create a
  9972. show buffer for the corresponding message. If you have a more clever
  9973. idea..."
  9974. (let* ((folder (org-mhe-get-message-folder))
  9975. (num (org-mhe-get-message-num))
  9976. (buffer (get-buffer-create (concat "show-" folder)))
  9977. (header-field))
  9978. (with-current-buffer buffer
  9979. (mh-display-msg num folder)
  9980. (if (equal major-mode 'mh-folder-mode)
  9981. (mh-header-display)
  9982. (mh-show-header-display))
  9983. (set-buffer buffer)
  9984. (setq header-field (mh-get-header-field header))
  9985. (if (equal major-mode 'mh-folder-mode)
  9986. (mh-show)
  9987. (mh-show-show))
  9988. header-field)))
  9989. (defun org-follow-mhe-link (folder article)
  9990. "Follow an MHE link to FOLDER and ARTICLE.
  9991. If ARTICLE is nil FOLDER is shown. If the configuration variable
  9992. `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
  9993. ARTICLE is searched in all folders. Indexed searches (swish++,
  9994. namazu, and others supported by MH-E) will always search in all
  9995. folders."
  9996. (require 'mh-e)
  9997. (require 'mh-search)
  9998. (require 'mh-utils)
  9999. (mh-find-path)
  10000. (if (not article)
  10001. (mh-visit-folder (mh-normalize-folder-name folder))
  10002. (setq article (org-add-angle-brackets article))
  10003. (mh-search-choose)
  10004. (if (equal mh-searcher 'pick)
  10005. (progn
  10006. (mh-search folder (list "--message-id" article))
  10007. (when (and org-mhe-search-all-folders
  10008. (not (org-mhe-get-message-real-folder)))
  10009. (kill-this-buffer)
  10010. (mh-search "+" (list "--message-id" article))))
  10011. (mh-search "+" article))
  10012. (if (org-mhe-get-message-real-folder)
  10013. (mh-show-msg 1)
  10014. (kill-this-buffer)
  10015. (error "Message not found"))))
  10016. ;; BibTeX links
  10017. ;; Use the custom search meachnism to construct and use search strings for
  10018. ;; file links to BibTeX database entries.
  10019. (defun org-create-file-search-in-bibtex ()
  10020. "Create the search string and description for a BibTeX database entry."
  10021. (when (eq major-mode 'bibtex-mode)
  10022. ;; yes, we want to construct this search string.
  10023. ;; Make a good description for this entry, using names, year and the title
  10024. ;; Put it into the `description' variable which is dynamically scoped.
  10025. (let ((bibtex-autokey-names 1)
  10026. (bibtex-autokey-names-stretch 1)
  10027. (bibtex-autokey-name-case-convert-function 'identity)
  10028. (bibtex-autokey-name-separator " & ")
  10029. (bibtex-autokey-additional-names " et al.")
  10030. (bibtex-autokey-year-length 4)
  10031. (bibtex-autokey-name-year-separator " ")
  10032. (bibtex-autokey-titlewords 3)
  10033. (bibtex-autokey-titleword-separator " ")
  10034. (bibtex-autokey-titleword-case-convert-function 'identity)
  10035. (bibtex-autokey-titleword-length 'infty)
  10036. (bibtex-autokey-year-title-separator ": "))
  10037. (setq description (bibtex-generate-autokey)))
  10038. ;; Now parse the entry, get the key and return it.
  10039. (save-excursion
  10040. (bibtex-beginning-of-entry)
  10041. (cdr (assoc "=key=" (bibtex-parse-entry))))))
  10042. (defun org-execute-file-search-in-bibtex (s)
  10043. "Find the link search string S as a key for a database entry."
  10044. (when (eq major-mode 'bibtex-mode)
  10045. ;; Yes, we want to do the search in this file.
  10046. ;; We construct a regexp that searches for "@entrytype{" followed by the key
  10047. (goto-char (point-min))
  10048. (and (re-search-forward (concat "@[a-zA-Z]+[ \t\n]*{[ \t\n]*"
  10049. (regexp-quote s) "[ \t\n]*,") nil t)
  10050. (goto-char (match-beginning 0)))
  10051. (if (and (match-beginning 0) (equal current-prefix-arg '(16)))
  10052. ;; Use double prefix to indicate that any web link should be browsed
  10053. (let ((b (current-buffer)) (p (point)))
  10054. ;; Restore the window configuration because we just use the web link
  10055. (set-window-configuration org-window-config-before-follow-link)
  10056. (save-excursion (set-buffer b) (goto-char p)
  10057. (bibtex-url)))
  10058. (recenter 0)) ; Move entry start to beginning of window
  10059. ;; return t to indicate that the search is done.
  10060. t))
  10061. ;; Finally add the functions to the right hooks.
  10062. (add-hook 'org-create-file-search-functions 'org-create-file-search-in-bibtex)
  10063. (add-hook 'org-execute-file-search-functions 'org-execute-file-search-in-bibtex)
  10064. ;; end of Bibtex link setup
  10065. (defun org-upgrade-old-links (&optional query-description)
  10066. "Transfer old <...> style links to new [[...]] style links.
  10067. With arg query-description, ask at each match for a description text to use
  10068. for this link."
  10069. (interactive (list (y-or-n-p "Would you like to be queried for a description at each link?")))
  10070. (save-excursion
  10071. (goto-char (point-min))
  10072. (let ((re (concat "\\([^[]\\)<\\("
  10073. "\\(" (mapconcat 'identity org-link-types "\\|")
  10074. "\\):"
  10075. "[^" org-non-link-chars "]+\\)>"))
  10076. l1 l2 (cnt 0))
  10077. (while (re-search-forward re nil t)
  10078. (setq cnt (1+ cnt)
  10079. l1 (org-match-string-no-properties 2)
  10080. l2 (save-match-data (org-link-escape l1)))
  10081. (when query-description (setq l1 (read-string "Desc: " l1)))
  10082. (if (equal l1 l2)
  10083. (replace-match (concat (match-string 1) "[[" l1 "]]") t t)
  10084. (replace-match (concat (match-string 1) "[[" l2 "][" l1 "]]") t t)))
  10085. (message "%d matches have beed treated" cnt))))
  10086. (defun org-open-file (path &optional in-emacs line search)
  10087. "Open the file at PATH.
  10088. First, this expands any special file name abbreviations. Then the
  10089. configuration variable `org-file-apps' is checked if it contains an
  10090. entry for this file type, and if yes, the corresponding command is launched.
  10091. If no application is found, Emacs simply visits the file.
  10092. With optional argument IN-EMACS, Emacs will visit the file.
  10093. Optional LINE specifies a line to go to, optional SEARCH a string to
  10094. search for. If LINE or SEARCH is given, the file will always be
  10095. opened in Emacs.
  10096. If the file does not exist, an error is thrown."
  10097. (setq in-emacs (or in-emacs line search))
  10098. (let* ((file (if (equal path "")
  10099. buffer-file-name
  10100. (substitute-in-file-name (expand-file-name path))))
  10101. (apps (append org-file-apps (org-default-apps)))
  10102. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  10103. (dirp (if remp nil (file-directory-p file)))
  10104. (dfile (downcase file))
  10105. (old-buffer (current-buffer))
  10106. (old-pos (point))
  10107. (old-mode major-mode)
  10108. ext cmd)
  10109. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  10110. (setq ext (match-string 1 dfile))
  10111. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  10112. (setq ext (match-string 1 dfile))))
  10113. (if in-emacs
  10114. (setq cmd 'emacs)
  10115. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  10116. (and dirp (cdr (assoc 'directory apps)))
  10117. (cdr (assoc ext apps))
  10118. (cdr (assoc t apps)))))
  10119. (when (eq cmd 'mailcap)
  10120. (require 'mailcap)
  10121. (mailcap-parse-mailcaps)
  10122. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  10123. (command (mailcap-mime-info mime-type)))
  10124. (if (stringp command)
  10125. (setq cmd command)
  10126. (setq cmd 'emacs))))
  10127. (if (and (not (eq cmd 'emacs)) ; Emacs has not problems with non-ex files
  10128. (not (file-exists-p file))
  10129. (not org-open-non-existing-files))
  10130. (error "No such file: %s" file))
  10131. (cond
  10132. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  10133. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  10134. (if (string-match "['\"]%s['\"]" cmd)
  10135. (setq cmd (replace-match "%s" t t cmd)))
  10136. (setq cmd (format cmd (shell-quote-argument file)))
  10137. (save-window-excursion
  10138. (shell-command (concat cmd " &"))))
  10139. ((or (stringp cmd)
  10140. (eq cmd 'emacs))
  10141. ; (unless (equal (file-truename file) (file-truename (or buffer-file-name "")))
  10142. ; (funcall (cdr (assq 'file org-link-frame-setup)) file))
  10143. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  10144. (if line (goto-line line)
  10145. (if search (org-link-search search))))
  10146. ((consp cmd)
  10147. (eval cmd))
  10148. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10149. (and (org-mode-p) (eq old-mode 'org-mode)
  10150. (or (not (equal old-buffer (current-buffer)))
  10151. (not (equal old-pos (point))))
  10152. (org-mark-ring-push old-pos old-buffer))))
  10153. (defun org-default-apps ()
  10154. "Return the default applications for this operating system."
  10155. (cond
  10156. ((eq system-type 'darwin)
  10157. org-file-apps-defaults-macosx)
  10158. ((eq system-type 'windows-nt)
  10159. org-file-apps-defaults-windowsnt)
  10160. (t org-file-apps-defaults-gnu)))
  10161. (defun org-expand-file-name (path)
  10162. "Replace special path abbreviations and expand the file name."
  10163. (expand-file-name path))
  10164. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10165. (defun org-file-remote-p (file)
  10166. "Test whether FILE specifies a location on a remote system.
  10167. Return non-nil if the location is indeed remote.
  10168. For example, the filename \"/user@host:/foo\" specifies a location
  10169. on the system \"/user@host:\"."
  10170. (cond ((fboundp 'file-remote-p)
  10171. (file-remote-p file))
  10172. ((fboundp 'tramp-handle-file-remote-p)
  10173. (tramp-handle-file-remote-p file))
  10174. ((and (boundp 'ange-ftp-name-format)
  10175. (string-match (car ange-ftp-name-format) file))
  10176. t)
  10177. (t nil)))
  10178. (defvar org-insert-link-history nil
  10179. "Minibuffer history for links inserted with `org-insert-link'.")
  10180. (defvar org-stored-links nil
  10181. "Contains the links stored with `org-store-link'.")
  10182. ;;;###autoload
  10183. (defun org-store-link (arg)
  10184. "\\<org-mode-map>Store an org-link to the current location.
  10185. This link can later be inserted into an org-buffer with
  10186. \\[org-insert-link].
  10187. For some link types, a prefix arg is interpreted:
  10188. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  10189. For file links, arg negates `org-context-in-file-links'."
  10190. (interactive "P")
  10191. (let (link cpltxt desc description search txt (pos (point)))
  10192. (cond
  10193. ((eq major-mode 'bbdb-mode)
  10194. (setq cpltxt (concat
  10195. "bbdb:"
  10196. (or (bbdb-record-name (bbdb-current-record))
  10197. (bbdb-record-company (bbdb-current-record))))
  10198. link (org-make-link cpltxt)))
  10199. ((eq major-mode 'Info-mode)
  10200. (setq link (org-make-link "info:"
  10201. (file-name-nondirectory Info-current-file)
  10202. ":" Info-current-node))
  10203. (setq cpltxt (concat (file-name-nondirectory Info-current-file)
  10204. ":" Info-current-node)))
  10205. ((eq major-mode 'calendar-mode)
  10206. (let ((cd (calendar-cursor-to-date)))
  10207. (setq link
  10208. (format-time-string
  10209. (car org-time-stamp-formats)
  10210. (apply 'encode-time
  10211. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  10212. nil nil nil))))))
  10213. ((or (eq major-mode 'vm-summary-mode)
  10214. (eq major-mode 'vm-presentation-mode))
  10215. (and (eq major-mode 'vm-presentation-mode) (vm-summarize))
  10216. (vm-follow-summary-cursor)
  10217. (save-excursion
  10218. (vm-select-folder-buffer)
  10219. (let* ((message (car vm-message-pointer))
  10220. (folder buffer-file-name)
  10221. (subject (vm-su-subject message))
  10222. (author (vm-su-full-name message))
  10223. (message-id (vm-su-message-id message)))
  10224. (setq message-id (org-remove-angle-brackets message-id))
  10225. (setq folder (abbreviate-file-name folder))
  10226. (if (string-match (concat "^" (regexp-quote vm-folder-directory))
  10227. folder)
  10228. (setq folder (replace-match "" t t folder)))
  10229. (setq cpltxt (concat author " on: " subject))
  10230. (setq link (org-make-link "vm:" folder "#" message-id)))))
  10231. ((eq major-mode 'wl-summary-mode)
  10232. (let* ((msgnum (wl-summary-message-number))
  10233. (message-id (elmo-message-field wl-summary-buffer-elmo-folder
  10234. msgnum 'message-id))
  10235. (wl-message-entity
  10236. (if (fboundp 'elmo-message-entity)
  10237. (elmo-message-entity
  10238. wl-summary-buffer-elmo-folder msgnum)
  10239. (elmo-msgdb-overview-get-entity
  10240. msgnum (wl-summary-buffer-msgdb))))
  10241. (author (wl-summary-line-from)) ; FIXME: correct?
  10242. (subject "???")) ; FIXME:
  10243. (setq message-id (org-remove-angle-brackets message-id))
  10244. (setq cpltxt (concat author " on: " subject))
  10245. (setq link (org-make-link "wl:" wl-summary-buffer-folder-name
  10246. "#" message-id))))
  10247. ((or (equal major-mode 'mh-folder-mode)
  10248. (equal major-mode 'mh-show-mode))
  10249. (let ((from-header (org-mhe-get-header "From:"))
  10250. (to-header (org-mhe-get-header "To:"))
  10251. (subject (org-mhe-get-header "Subject:")))
  10252. (setq cpltxt (concat from-header " on: " subject))
  10253. (setq link (org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
  10254. (org-remove-angle-brackets
  10255. (org-mhe-get-header "Message-Id:"))))))
  10256. ((eq major-mode 'rmail-mode)
  10257. (save-excursion
  10258. (save-restriction
  10259. (rmail-narrow-to-non-pruned-header)
  10260. (let ((folder buffer-file-name)
  10261. (message-id (mail-fetch-field "message-id"))
  10262. (author (mail-fetch-field "from"))
  10263. (subject (mail-fetch-field "subject")))
  10264. (setq message-id (org-remove-angle-brackets message-id))
  10265. (setq cpltxt (concat author " on: " subject))
  10266. (setq link (org-make-link "rmail:" folder "#" message-id))))))
  10267. ((eq major-mode 'gnus-group-mode)
  10268. (let ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
  10269. (gnus-group-group-name)) ; version
  10270. ((fboundp 'gnus-group-name)
  10271. (gnus-group-name))
  10272. (t "???"))))
  10273. (setq cpltxt (concat
  10274. (if (org-xor arg org-usenet-links-prefer-google)
  10275. "http://groups.google.com/groups?group="
  10276. "gnus:")
  10277. group)
  10278. link (org-make-link cpltxt))))
  10279. ((memq major-mode '(gnus-summary-mode gnus-article-mode))
  10280. (require 'gnus-sum) ; FIXME: I don't think this is needed, actually
  10281. (and (eq major-mode 'gnus-article-mode) (gnus-article-show-summary))
  10282. (gnus-summary-beginning-of-article)
  10283. (let* ((group (car gnus-article-current))
  10284. (article (cdr gnus-article-current))
  10285. (header (gnus-summary-article-header article))
  10286. (author (mail-header-from header))
  10287. (message-id (mail-header-id header))
  10288. (date (mail-header-date header))
  10289. (subject (gnus-summary-subject-string)))
  10290. (setq cpltxt (concat author " on: " subject))
  10291. (if (org-xor arg org-usenet-links-prefer-google)
  10292. (setq link
  10293. (concat
  10294. cpltxt "\n "
  10295. (format "http://groups.google.com/groups?as_umsgid=%s"
  10296. (org-fixup-message-id-for-http message-id))))
  10297. (setq link (org-make-link "gnus:" group
  10298. "#" (number-to-string article))))))
  10299. ((eq major-mode 'w3-mode)
  10300. (setq cpltxt (url-view-url t)
  10301. link (org-make-link cpltxt)))
  10302. ((eq major-mode 'w3m-mode)
  10303. (setq cpltxt (or w3m-current-title w3m-current-url)
  10304. link (org-make-link w3m-current-url)))
  10305. ((setq search (run-hook-with-args-until-success
  10306. 'org-create-file-search-functions))
  10307. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  10308. "::" search))
  10309. (setq cpltxt (or description link)))
  10310. ((eq major-mode 'image-mode)
  10311. (setq cpltxt (concat "file:"
  10312. (abbreviate-file-name buffer-file-name))
  10313. link (org-make-link cpltxt)))
  10314. ((eq major-mode 'dired-mode)
  10315. ;; link to the file in the current line
  10316. (setq cpltxt (concat "file:"
  10317. (abbreviate-file-name
  10318. (expand-file-name
  10319. (dired-get-filename nil t))))
  10320. link (org-make-link cpltxt)))
  10321. ((and buffer-file-name (org-mode-p))
  10322. ;; Just link to current headline
  10323. (setq cpltxt (concat "file:"
  10324. (abbreviate-file-name buffer-file-name)))
  10325. ;; Add a context search string
  10326. (when (org-xor org-context-in-file-links arg)
  10327. ;; Check if we are on a target
  10328. (if (save-excursion
  10329. (skip-chars-forward "^>\n\r")
  10330. (and (re-search-backward "<<" nil t)
  10331. (looking-at "<<\\(.*?\\)>>")
  10332. (<= (match-beginning 0) pos)
  10333. (>= (match-end 0) pos)))
  10334. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  10335. (setq txt (cond
  10336. ((org-on-heading-p) nil)
  10337. ((org-region-active-p)
  10338. (buffer-substring (region-beginning) (region-end)))
  10339. (t (buffer-substring (point-at-bol) (point-at-eol)))))
  10340. (when (or (null txt) (string-match "\\S-" txt))
  10341. (setq cpltxt
  10342. (concat cpltxt "::"
  10343. (if org-file-link-context-use-camel-case
  10344. (org-make-org-heading-camel txt)
  10345. (org-make-org-heading-search-string txt)))
  10346. desc "NONE"))))
  10347. (if (string-match "::\\'" cpltxt)
  10348. (setq cpltxt (substring cpltxt 0 -2)))
  10349. (setq link (org-make-link cpltxt)))
  10350. (buffer-file-name
  10351. ;; Just link to this file here.
  10352. (setq cpltxt (concat "file:"
  10353. (abbreviate-file-name buffer-file-name)))
  10354. ;; Add a context string
  10355. (when (org-xor org-context-in-file-links arg)
  10356. (setq txt (if (org-region-active-p)
  10357. (buffer-substring (region-beginning) (region-end))
  10358. (buffer-substring (point-at-bol) (point-at-eol))))
  10359. ;; Only use search option if there is some text.
  10360. (when (string-match "\\S-" txt)
  10361. (setq cpltxt
  10362. (concat cpltxt "::"
  10363. (if org-file-link-context-use-camel-case
  10364. (org-make-org-heading-camel txt)
  10365. (org-make-org-heading-search-string txt)))
  10366. desc "NONE")))
  10367. (setq link (org-make-link cpltxt)))
  10368. ((interactive-p)
  10369. (error "Cannot link to a buffer which is not visiting a file"))
  10370. (t (setq link nil)))
  10371. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  10372. (setq link (or link cpltxt)
  10373. desc (or desc cpltxt))
  10374. (if (equal desc "NONE") (setq desc nil))
  10375. (if (and (interactive-p) link)
  10376. (progn
  10377. (setq org-stored-links
  10378. (cons (list cpltxt link desc) org-stored-links))
  10379. (message "Stored: %s" (or cpltxt link)))
  10380. (org-make-link-string link desc))))
  10381. (defun org-make-org-heading-search-string (&optional string heading)
  10382. "Make search string for STRING or current headline."
  10383. (interactive)
  10384. (let ((s (or string (org-get-heading))))
  10385. (unless (and string (not heading))
  10386. ;; We are using a headline, clean up garbage in there.
  10387. (if (string-match org-todo-regexp s)
  10388. (setq s (replace-match "" t t s)))
  10389. (if (string-match ":[a-zA-Z_@0-9:]+:[ \t]*$" s)
  10390. (setq s (replace-match "" t t s)))
  10391. (setq s (org-trim s))
  10392. (if (string-match (concat "^\\(" org-quote-string "\\|"
  10393. org-comment-string "\\)") s)
  10394. (setq s (replace-match "" t t s)))
  10395. (while (string-match org-ts-regexp s)
  10396. (setq s (replace-match "" t t s))))
  10397. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  10398. (setq s (replace-match " " t t s)))
  10399. (or string (setq s (concat "*" s))) ; Add * for headlines
  10400. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  10401. (defun org-make-org-heading-camel (&optional string heading)
  10402. "Make a CamelCase string for STRING or the current headline."
  10403. (interactive)
  10404. (let ((s (or string (org-get-heading))))
  10405. (unless (and string (not heading))
  10406. ;; We are using a headline, clean up garbage in there.
  10407. (if (string-match org-todo-regexp s)
  10408. (setq s (replace-match "" t t s)))
  10409. (if (string-match ":[a-zA-Z_@0-9:]+:[ \t]*$" s)
  10410. (setq s (replace-match "" t t s)))
  10411. (setq s (org-trim s))
  10412. (if (string-match (concat "^\\(" org-quote-string "\\|"
  10413. org-comment-string "\\)") s)
  10414. (setq s (replace-match "" t t s)))
  10415. (while (string-match org-ts-regexp s)
  10416. (setq s (replace-match "" t t s))))
  10417. (while (string-match "[^a-zA-Z_ \t]+" s)
  10418. (setq s (replace-match " " t t s)))
  10419. (or string (setq s (concat "*" s))) ; Add * for headlines
  10420. (mapconcat 'capitalize (org-split-string s "[ \t]+") "")))
  10421. (defun org-make-link (&rest strings)
  10422. "Concatenate STRINGS, format resulting string with `org-link-format'."
  10423. (format org-link-format (apply 'concat strings)))
  10424. (defun org-make-link-string (link &optional description)
  10425. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  10426. (if (eq org-link-style 'plain)
  10427. (if (equal description link)
  10428. link
  10429. (concat description "\n" link))
  10430. (when (stringp description)
  10431. ;; Remove brackets from the description, they are fatal.
  10432. (while (string-match "\\[\\|\\]" description)
  10433. (setq description (replace-match "" t t description))))
  10434. (when (equal (org-link-escape link) description)
  10435. ;; No description needed, it is identical
  10436. (setq description nil))
  10437. (when (and (not description)
  10438. (not (equal link (org-link-escape link))))
  10439. (setq description link))
  10440. (concat "[[" (org-link-escape link) "]"
  10441. (if description (concat "[" description "]") "")
  10442. "]")))
  10443. (defconst org-link-escape-chars '(("[" . "%5B") ("]" . "%5D") (" " . "%20"))
  10444. "Association list of escapes for some characters problematic in links.")
  10445. (defun org-link-escape (text)
  10446. "Escape charaters in TEXT that are problematic for links."
  10447. (when text
  10448. (let ((re (mapconcat (lambda (x) (regexp-quote (car x)))
  10449. org-link-escape-chars "\\|")))
  10450. (while (string-match re text)
  10451. (setq text
  10452. (replace-match
  10453. (cdr (assoc (match-string 0 text) org-link-escape-chars))
  10454. t t text)))
  10455. text)))
  10456. (defun org-link-unescape (text)
  10457. "Reverse the action of `org-link-escape'."
  10458. (when text
  10459. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  10460. org-link-escape-chars "\\|")))
  10461. (while (string-match re text)
  10462. (setq text
  10463. (replace-match
  10464. (car (rassoc (match-string 0 text) org-link-escape-chars))
  10465. t t text)))
  10466. text)))
  10467. (defun org-xor (a b)
  10468. "Exclusive or."
  10469. (if a (not b) b))
  10470. (defun org-get-header (header)
  10471. "Find a header field in the current buffer."
  10472. (save-excursion
  10473. (goto-char (point-min))
  10474. (let ((case-fold-search t) s)
  10475. (cond
  10476. ((eq header 'from)
  10477. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  10478. (setq s (match-string 1)))
  10479. (while (string-match "\"" s)
  10480. (setq s (replace-match "" t t s)))
  10481. (if (string-match "[<(].*" s)
  10482. (setq s (replace-match "" t t s))))
  10483. ((eq header 'message-id)
  10484. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  10485. (setq s (match-string 1))))
  10486. ((eq header 'subject)
  10487. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  10488. (setq s (match-string 1)))))
  10489. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  10490. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  10491. s)))
  10492. (defun org-fixup-message-id-for-http (s)
  10493. "Replace special characters in a message id, so it can be used in an http query."
  10494. (while (string-match "<" s)
  10495. (setq s (replace-match "%3C" t t s)))
  10496. (while (string-match ">" s)
  10497. (setq s (replace-match "%3E" t t s)))
  10498. (while (string-match "@" s)
  10499. (setq s (replace-match "%40" t t s)))
  10500. s)
  10501. (defun org-insert-link (&optional complete-file)
  10502. "Insert a link. At the prompt, enter the link.
  10503. Completion can be used to select a link previously stored with
  10504. `org-store-link'. When the empty string is entered (i.e. if you just
  10505. press RET at the prompt), the link defaults to the most recently
  10506. stored link. As SPC triggers completion in the minibuffer, you need to
  10507. use M-SPC or C-q SPC to force the insertion of a space character.
  10508. You will also be prompted for a description, and if one is given, it will
  10509. be displayed in the buffer instead of the link.
  10510. If there is already a link at point, this command will allow you to edit link
  10511. and description parts.
  10512. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
  10513. selected using completion. The path to the file will be relative to
  10514. the current directory if the file is in the current directory or a
  10515. subdirectory. Otherwise, the link will be the absolute path as
  10516. completed in the minibuffer (i.e. normally ~/path/to/file).
  10517. With two \\[universal-argument] prefixes, enforce an absolute path even if the file
  10518. is in the current directory or below.
  10519. With three \\[universal-argument] prefixes, negate the meaning of
  10520. `org-keep-stored-link-after-insertion'."
  10521. (interactive "P")
  10522. (let (link desc entry remove file (pos (point)))
  10523. (cond
  10524. ((save-excursion
  10525. (skip-chars-forward "^]\n\r")
  10526. (and (re-search-backward "\\[\\[" nil t)
  10527. (looking-at org-bracket-link-regexp)
  10528. (<= (match-beginning 0) pos)
  10529. (>= (match-end 0) pos)))
  10530. ;; We do have a link at point, and we are going to edit it.
  10531. (setq remove (list (match-beginning 0) (match-end 0)))
  10532. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  10533. (setq link (read-string "Link: "
  10534. (org-link-unescape
  10535. (org-match-string-no-properties 1)))))
  10536. ((equal complete-file '(4))
  10537. ;; Completing read for file names.
  10538. (setq file (read-file-name "File: "))
  10539. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  10540. (pwd1 (file-name-as-directory (abbreviate-file-name
  10541. (expand-file-name ".")))))
  10542. (cond
  10543. ((equal complete-file '(16))
  10544. (setq link (org-make-link
  10545. "file:"
  10546. (abbreviate-file-name (expand-file-name file)))))
  10547. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  10548. (setq link (org-make-link "file:" (match-string 1 file))))
  10549. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  10550. (expand-file-name file))
  10551. (setq link (org-make-link
  10552. "file:" (match-string 1 (expand-file-name file)))))
  10553. (t (setq link (org-make-link "file:" file))))))
  10554. (t
  10555. ;; Read link, with completion for stored links.
  10556. (setq link (org-completing-read
  10557. "Link: " org-stored-links nil nil nil
  10558. org-insert-link-history
  10559. (or (car (car org-stored-links)))))
  10560. (setq entry (assoc link org-stored-links))
  10561. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  10562. (not org-keep-stored-link-after-insertion))
  10563. (setq org-stored-links (delq (assoc link org-stored-links)
  10564. org-stored-links)))
  10565. (setq link (if entry (nth 1 entry) link)
  10566. desc (or desc (nth 2 entry)))))
  10567. (if (string-match org-plain-link-re link)
  10568. ;; URL-like link, normalize the use of angular brackets.
  10569. (setq link (org-make-link (org-remove-angle-brackets link))))
  10570. ;; Check if we are linking to the current file with a search option
  10571. ;; If yes, simplify the link by using only the search option.
  10572. (when (and buffer-file-name
  10573. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  10574. (let* ((path (match-string 1 link))
  10575. (case-fold-search nil)
  10576. (search (match-string 2 link)))
  10577. (save-match-data
  10578. (if (equal (file-truename buffer-file-name) (file-truename path))
  10579. ;; We are linking to this same file, with a search option
  10580. (setq link search)))))
  10581. ;; Check if we can/should use a relative path. If yes, simplify the link
  10582. (when (string-match "\\<file:\\(.*\\)" link)
  10583. (let* ((path (match-string 1 link))
  10584. (case-fold-search nil))
  10585. (cond
  10586. ((eq org-link-file-path-type 'absolute)
  10587. (setq path (abbreviate-file-name (expand-file-name path))))
  10588. ((eq org-link-file-path-type 'noabbrev)
  10589. (setq path (expand-file-name path)))
  10590. ((eq org-link-file-path-type 'relative)
  10591. (setq path (file-relative-name path)))
  10592. (t
  10593. (save-match-data
  10594. (if (string-match (concat "^" (regexp-quote
  10595. (file-name-as-directory
  10596. (expand-file-name "."))))
  10597. (expand-file-name path))
  10598. ;; We are linking a file with relative path name.
  10599. (setq path (substring (expand-file-name path)
  10600. (match-end 0)))))))
  10601. (setq link (concat "file:" path))))
  10602. (setq desc (read-string "Description: " desc))
  10603. (unless (string-match "\\S-" desc) (setq desc nil))
  10604. (if remove (apply 'delete-region remove))
  10605. (insert (org-make-link-string link desc))))
  10606. (defun org-completing-read (&rest args)
  10607. (let ((minibuffer-local-completion-map
  10608. (copy-keymap minibuffer-local-completion-map)))
  10609. (define-key minibuffer-local-completion-map " " 'self-insert-command)
  10610. (apply 'completing-read args)))
  10611. ;;; Hooks for remember.el
  10612. (defvar org-finish-function nil)
  10613. ;;;###autoload
  10614. (defun org-remember-annotation ()
  10615. "Return a link to the current location as an annotation for remember.el.
  10616. If you are using Org-mode files as target for data storage with
  10617. remember.el, then the annotations should include a link compatible with the
  10618. conventions in Org-mode. This function returns such a link."
  10619. (org-store-link nil))
  10620. (defconst org-remember-help
  10621. "Select a destination location for the note.
  10622. UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
  10623. RET at beg-of-buf -> Append to file as level 2 headline
  10624. RET on headline -> Store as sublevel entry to current headline
  10625. <left>/<right> -> before/after current headline, same headings level")
  10626. ;;;###autoload
  10627. (defun org-remember-apply-template ()
  10628. "Initialize *remember* buffer with template, invoke `org-mode'.
  10629. This function should be placed into `remember-mode-hook' and in fact requires
  10630. to be run from that hook to fucntion properly."
  10631. (if org-remember-templates
  10632. (let* ((entry (if (= (length org-remember-templates) 1)
  10633. (cdar org-remember-templates)
  10634. (message "Select template: %s"
  10635. (mapconcat
  10636. (lambda (x) (char-to-string (car x)))
  10637. org-remember-templates " "))
  10638. (cdr (assoc (read-char-exclusive) org-remember-templates))))
  10639. (tpl (car entry))
  10640. (file (if (consp (cdr entry)) (nth 1 entry)))
  10641. (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
  10642. (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
  10643. (v-u (concat "[" (substring v-t 1 -1) "]"))
  10644. (v-U (concat "[" (substring v-T 1 -1) "]"))
  10645. (v-a annotation) ; defined in `remember-mode'
  10646. (v-i initial) ; defined in `remember-mode'
  10647. (v-n user-full-name)
  10648. )
  10649. (unless tpl (setq tpl "") (message "No template") (ding))
  10650. (insert tpl) (goto-char (point-min))
  10651. (while (re-search-forward "%\\([tTuTai]\\)" nil t)
  10652. (when (and initial (equal (match-string 0) "%i"))
  10653. (save-match-data
  10654. (let* ((lead (buffer-substring
  10655. (point-at-bol) (match-beginning 0))))
  10656. (setq v-i (mapconcat 'identity
  10657. (org-split-string initial "\n")
  10658. (concat "\n" lead))))))
  10659. (replace-match
  10660. (or (eval (intern (concat "v-" (match-string 1)))) "")
  10661. t t))
  10662. (let ((org-startup-folded nil)
  10663. (org-startup-with-deadline-check nil))
  10664. (org-mode))
  10665. (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
  10666. (org-set-local 'org-default-notes-file file))
  10667. (goto-char (point-min))
  10668. (if (re-search-forward "%\\?" nil t) (replace-match "")))
  10669. (let ((org-startup-folded nil)
  10670. (org-startup-with-deadline-check nil))
  10671. (org-mode)))
  10672. (org-set-local 'org-finish-function 'remember-buffer))
  10673. ;;;###autoload
  10674. (defun org-remember-handler ()
  10675. "Store stuff from remember.el into an org file.
  10676. First prompts for an org file. If the user just presses return, the value
  10677. of `org-default-notes-file' is used.
  10678. Then the command offers the headings tree of the selected file in order to
  10679. file the text at a specific location.
  10680. You can either immediately press RET to get the note appended to the
  10681. file, or you can use vertical cursor motion and visibility cycling (TAB) to
  10682. find a better place. Then press RET or <left> or <right> in insert the note.
  10683. Key Cursor position Note gets inserted
  10684. -----------------------------------------------------------------------------
  10685. RET buffer-start as level 2 heading at end of file
  10686. RET on headline as sublevel of the heading at cursor
  10687. RET no heading at cursor position, level taken from context.
  10688. Or use prefix arg to specify level manually.
  10689. <left> on headline as same level, before current heading
  10690. <right> on headline as same level, after current heading
  10691. So the fastest way to store the note is to press RET RET to append it to
  10692. the default file. This way your current train of thought is not
  10693. interrupted, in accordance with the principles of remember.el. But with
  10694. little extra effort, you can push it directly to the correct location.
  10695. Before being stored away, the function ensures that the text has a
  10696. headline, i.e. a first line that starts with a \"*\". If not, a headline
  10697. is constructed from the current date and some additional data.
  10698. If the variable `org-adapt-indentation' is non-nil, the entire text is
  10699. also indented so that it starts in the same column as the headline
  10700. \(i.e. after the stars).
  10701. See also the variable `org-reverse-note-order'."
  10702. (catch 'quit
  10703. (let* ((txt (buffer-substring (point-min) (point-max)))
  10704. (fastp current-prefix-arg)
  10705. (file (if fastp org-default-notes-file (org-get-org-file)))
  10706. (visiting (find-buffer-visiting file))
  10707. (org-startup-with-deadline-check nil)
  10708. (org-startup-folded nil)
  10709. (org-startup-align-all-tables nil)
  10710. spos level indent reversed)
  10711. ;; Modify text so that it becomes a nice subtree which can be inserted
  10712. ;; into an org tree.
  10713. (let* ((lines (split-string txt "\n"))
  10714. first)
  10715. ;; remove empty lines at the beginning
  10716. (while (and lines (string-match "^[ \t]*\n" (car lines)))
  10717. (setq lines (cdr lines)))
  10718. (setq first (car lines) lines (cdr lines))
  10719. (if (string-match "^\\*+" first)
  10720. ;; Is already a headline
  10721. (setq indent nil)
  10722. ;; We need to add a headline: Use time and first buffer line
  10723. (setq lines (cons first lines)
  10724. first (concat "* " (current-time-string)
  10725. " (" (remember-buffer-desc) ")")
  10726. indent " "))
  10727. (if (and org-adapt-indentation indent)
  10728. (setq lines (mapcar (lambda (x) (concat indent x)) lines)))
  10729. (setq txt (concat first "\n"
  10730. (mapconcat 'identity lines "\n"))))
  10731. ;; Find the file
  10732. (if (not visiting)
  10733. (find-file-noselect file))
  10734. (with-current-buffer (get-file-buffer file)
  10735. (save-excursion (and (goto-char (point-min))
  10736. (not (re-search-forward "^\\* " nil t))
  10737. (insert "\n* Notes\n")))
  10738. (setq reversed (org-notes-order-reversed-p))
  10739. (save-excursion
  10740. (save-restriction
  10741. (widen)
  10742. ;; Ask the User for a location
  10743. (setq spos (if fastp 1 (org-get-location
  10744. (current-buffer)
  10745. org-remember-help)))
  10746. (if (not spos) (throw 'quit nil)) ; return nil to show we did
  10747. ; not handle this note
  10748. (goto-char spos)
  10749. (cond ((and (bobp) (not reversed))
  10750. ;; Put it at the end, as level 2
  10751. (save-restriction
  10752. (widen)
  10753. (goto-char (point-max))
  10754. (if (not (bolp)) (newline))
  10755. (org-paste-subtree 2 txt)))
  10756. ((and (bobp) reversed)
  10757. ;; Put it at the start, as level 1
  10758. (save-restriction
  10759. (widen)
  10760. (goto-char (point-min))
  10761. (re-search-forward "^\\*" nil t)
  10762. (beginning-of-line 1)
  10763. (org-paste-subtree 1 txt)))
  10764. ((and (org-on-heading-p nil) (not current-prefix-arg))
  10765. ;; Put it below this entry, at the beg/end of the subtree
  10766. (org-back-to-heading)
  10767. (setq level (funcall outline-level))
  10768. (if reversed
  10769. (outline-end-of-heading)
  10770. (outline-end-of-subtree))
  10771. (if (not (bolp)) (newline))
  10772. (beginning-of-line 1)
  10773. (org-paste-subtree (org-get-legal-level level 1) txt))
  10774. (t
  10775. ;; Put it right there, with automatic level determined by
  10776. ;; org-paste-subtree or from prefix arg
  10777. (org-paste-subtree current-prefix-arg txt)))
  10778. (when remember-save-after-remembering
  10779. (save-buffer)
  10780. (if (not visiting) (kill-buffer (current-buffer)))))))))
  10781. t) ;; return t to indicate that we took care of this note.
  10782. (defun org-get-org-file ()
  10783. "Read a filename, with default directory `org-directory'."
  10784. (let ((default (or org-default-notes-file remember-data-file)))
  10785. (read-file-name (format "File name [%s]: " default)
  10786. (file-name-as-directory org-directory)
  10787. default)))
  10788. (defun org-notes-order-reversed-p ()
  10789. "Check if the current file should receive notes in reversed order."
  10790. (cond
  10791. ((not org-reverse-note-order) nil)
  10792. ((eq t org-reverse-note-order) t)
  10793. ((not (listp org-reverse-note-order)) nil)
  10794. (t (catch 'exit
  10795. (let ((all org-reverse-note-order)
  10796. entry)
  10797. (while (setq entry (pop all))
  10798. (if (string-match (car entry) buffer-file-name)
  10799. (throw 'exit (cdr entry))))
  10800. nil)))))
  10801. ;;; Tables
  10802. ;; Watch out: Here we are talking about two different kind of tables.
  10803. ;; Most of the code is for the tables created with the Org-mode table editor.
  10804. ;; Sometimes, we talk about tables created and edited with the table.el
  10805. ;; Emacs package. We call the former org-type tables, and the latter
  10806. ;; table.el-type tables.
  10807. (defun org-before-change-function (beg end)
  10808. "Every change indicates that a table might need an update."
  10809. (setq org-table-may-need-update t))
  10810. (defconst org-table-line-regexp "^[ \t]*|"
  10811. "Detects an org-type table line.")
  10812. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  10813. "Detects an org-type table line.")
  10814. (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
  10815. "Detects a table line marked for automatic recalculation.")
  10816. (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
  10817. "Detects a table line marked for automatic recalculation.")
  10818. (defconst org-table-calculate-mark-regexp "^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
  10819. "Detects a table line marked for automatic recalculation.")
  10820. (defconst org-table-hline-regexp "^[ \t]*|-"
  10821. "Detects an org-type table hline.")
  10822. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  10823. "Detects a table-type table hline.")
  10824. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  10825. "Detects an org-type or table-type table.")
  10826. (defconst org-table-border-regexp "^[ \t]*[^| \t]"
  10827. "Searching from within a table (any type) this finds the first line
  10828. outside the table.")
  10829. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  10830. "Searching from within a table (any type) this finds the first line
  10831. outside the table.")
  10832. (defun org-table-create-with-table.el ()
  10833. "Use the table.el package to insert a new table.
  10834. If there is already a table at point, convert between Org-mode tables
  10835. and table.el tables."
  10836. (interactive)
  10837. (require 'table)
  10838. (cond
  10839. ((org-at-table.el-p)
  10840. (if (y-or-n-p "Convert table to Org-mode table? ")
  10841. (org-table-convert)))
  10842. ((org-at-table-p)
  10843. (if (y-or-n-p "Convert table to table.el table? ")
  10844. (org-table-convert)))
  10845. (t (call-interactively 'table-insert))))
  10846. (defun org-table-create-or-convert-from-region (arg)
  10847. "Convert region to table, or create an empty table.
  10848. If there is an active region, convert it to a table. If there is no such
  10849. region, create an empty table."
  10850. (interactive "P")
  10851. (if (org-region-active-p)
  10852. (org-table-convert-region (region-beginning) (region-end) arg)
  10853. (org-table-create arg)))
  10854. (defun org-table-create (&optional size)
  10855. "Query for a size and insert a table skeleton.
  10856. SIZE is a string Columns x Rows like for example \"3x2\"."
  10857. (interactive "P")
  10858. (unless size
  10859. (setq size (read-string
  10860. (concat "Table size Columns x Rows [e.g. "
  10861. org-table-default-size "]: ")
  10862. "" nil org-table-default-size)))
  10863. (let* ((pos (point))
  10864. (indent (make-string (current-column) ?\ ))
  10865. (split (org-split-string size " *x *"))
  10866. (rows (string-to-number (nth 1 split)))
  10867. (columns (string-to-number (car split)))
  10868. (line (concat (apply 'concat indent "|" (make-list columns " |"))
  10869. "\n")))
  10870. (if (string-match "^[ \t]*$" (buffer-substring-no-properties
  10871. (point-at-bol) (point)))
  10872. (beginning-of-line 1)
  10873. (newline))
  10874. ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
  10875. (dotimes (i rows) (insert line))
  10876. (goto-char pos)
  10877. (if (> rows 1)
  10878. ;; Insert a hline after the first row.
  10879. (progn
  10880. (end-of-line 1)
  10881. (insert "\n|-")
  10882. (goto-char pos)))
  10883. (org-table-align)))
  10884. (defun org-table-convert-region (beg0 end0 &optional nspace)
  10885. "Convert region to a table.
  10886. The region goes from BEG0 to END0, but these borders will be moved
  10887. slightly, to make sure a beginning of line in the first line is included.
  10888. When NSPACE is non-nil, it indicates the minimum number of spaces that
  10889. separate columns (default: just one space)."
  10890. (interactive "rP")
  10891. (let* ((beg (min beg0 end0))
  10892. (end (max beg0 end0))
  10893. (tabsep t)
  10894. re)
  10895. (goto-char beg)
  10896. (beginning-of-line 1)
  10897. (setq beg (move-marker (make-marker) (point)))
  10898. (goto-char end)
  10899. (if (bolp) (backward-char 1) (end-of-line 1))
  10900. (setq end (move-marker (make-marker) (point)))
  10901. ;; Lets see if this is tab-separated material. If every nonempty line
  10902. ;; contains a tab, we will assume that it is tab-separated material
  10903. (if nspace
  10904. (setq tabsep nil)
  10905. (goto-char beg)
  10906. (and (re-search-forward "^[^\n\t]+$" end t) (setq tabsep nil)))
  10907. (if nspace (setq tabsep nil))
  10908. (if tabsep
  10909. (setq re "^\\|\t")
  10910. (setq re (format "^ *\\| *\t *\\| \\{%d,\\}"
  10911. (max 1 (prefix-numeric-value nspace)))))
  10912. (goto-char beg)
  10913. (while (re-search-forward re end t)
  10914. (replace-match "| " t t))
  10915. (goto-char beg)
  10916. (insert " ")
  10917. (org-table-align)))
  10918. (defun org-table-import (file arg)
  10919. "Import FILE as a table.
  10920. The file is assumed to be tab-separated. Such files can be produced by most
  10921. spreadsheet and database applications. If no tabs (at least one per line)
  10922. are found, lines will be split on whitespace into fields."
  10923. (interactive "f\nP")
  10924. (or (bolp) (newline))
  10925. (let ((beg (point))
  10926. (pm (point-max)))
  10927. (insert-file-contents file)
  10928. (org-table-convert-region beg (+ (point) (- (point-max) pm)) arg)))
  10929. (defun org-table-export ()
  10930. "Export table as a tab-separated file.
  10931. Such a file can be imported into a spreadsheet program like Excel."
  10932. (interactive)
  10933. (let* ((beg (org-table-begin))
  10934. (end (org-table-end))
  10935. (table (buffer-substring beg end))
  10936. (file (read-file-name "Export table to: "))
  10937. buf)
  10938. (unless (or (not (file-exists-p file))
  10939. (y-or-n-p (format "Overwrite file %s? " file)))
  10940. (error "Abort"))
  10941. (with-current-buffer (find-file-noselect file)
  10942. (setq buf (current-buffer))
  10943. (erase-buffer)
  10944. (fundamental-mode)
  10945. (insert table)
  10946. (goto-char (point-min))
  10947. (while (re-search-forward "^[ \t]*|[ \t]*" nil t)
  10948. (replace-match "" t t)
  10949. (end-of-line 1))
  10950. (goto-char (point-min))
  10951. (while (re-search-forward "[ \t]*|[ \t]*$" nil t)
  10952. (replace-match "" t t)
  10953. (goto-char (min (1+ (point)) (point-max))))
  10954. (goto-char (point-min))
  10955. (while (re-search-forward "^-[-+]*$" nil t)
  10956. (replace-match "")
  10957. (if (looking-at "\n")
  10958. (delete-char 1)))
  10959. (goto-char (point-min))
  10960. (while (re-search-forward "[ \t]*|[ \t]*" nil t)
  10961. (replace-match "\t" t t))
  10962. (save-buffer))
  10963. (kill-buffer buf)))
  10964. (defvar org-table-aligned-begin-marker (make-marker)
  10965. "Marker at the beginning of the table last aligned.
  10966. Used to check if cursor still is in that table, to minimize realignment.")
  10967. (defvar org-table-aligned-end-marker (make-marker)
  10968. "Marker at the end of the table last aligned.
  10969. Used to check if cursor still is in that table, to minimize realignment.")
  10970. (defvar org-table-last-alignment nil
  10971. "List of flags for flushright alignment, from the last re-alignment.
  10972. This is being used to correctly align a single field after TAB or RET.")
  10973. (defvar org-table-last-column-widths nil
  10974. "List of max width of fields in each column.
  10975. This is being used to correctly align a single field after TAB or RET.")
  10976. (defvar org-last-recalc-line nil)
  10977. (defconst org-narrow-column-arrow "=>"
  10978. "Used as display property in narrowed table columns.")
  10979. (defun org-table-align ()
  10980. "Align the table at point by aligning all vertical bars."
  10981. (interactive)
  10982. (let* (
  10983. ;; Limits of table
  10984. (beg (org-table-begin))
  10985. (end (org-table-end))
  10986. ;; Current cursor position
  10987. (linepos (org-current-line))
  10988. (colpos (org-table-current-column))
  10989. (winstart (window-start))
  10990. (winstartline (org-current-line (min winstart (1- (point-max)))))
  10991. lines (new "") lengths l typenums ty fields maxfields i
  10992. column
  10993. (indent "") cnt frac
  10994. rfmt hfmt
  10995. (spaces '(1 . 1))
  10996. (sp1 (car spaces))
  10997. (sp2 (cdr spaces))
  10998. (rfmt1 (concat
  10999. (make-string sp2 ?\ ) "%%%s%ds" (make-string sp1 ?\ ) "|"))
  11000. (hfmt1 (concat
  11001. (make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
  11002. emptystrings links dates narrow fmax f1 len c e)
  11003. (untabify beg end)
  11004. (remove-text-properties beg end '(org-cwidth t org-dwidth t display t))
  11005. ;; Check if we have links or dates
  11006. (goto-char beg)
  11007. (setq links (re-search-forward org-bracket-link-regexp end t))
  11008. (goto-char beg)
  11009. (setq dates (and org-display-custom-times
  11010. (re-search-forward org-ts-regexp-both end t)))
  11011. ;; Make sure the link properties are right
  11012. (when links (goto-char beg) (while (org-activate-bracket-links end)))
  11013. ;; Make sure the date properties are right
  11014. (when dates (goto-char beg) (while (org-activate-dates end)))
  11015. ;; Check if we are narrowing any columns
  11016. (goto-char beg)
  11017. (setq narrow (and org-format-transports-properties-p
  11018. (re-search-forward "<[0-9]+>" end t)))
  11019. ;; Get the rows
  11020. (setq lines (org-split-string
  11021. (buffer-substring beg end) "\n"))
  11022. ;; Store the indentation of the first line
  11023. (if (string-match "^ *" (car lines))
  11024. (setq indent (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  11025. ;; Mark the hlines by setting the corresponding element to nil
  11026. ;; At the same time, we remove trailing space.
  11027. (setq lines (mapcar (lambda (l)
  11028. (if (string-match "^ *|-" l)
  11029. nil
  11030. (if (string-match "[ \t]+$" l)
  11031. (substring l 0 (match-beginning 0))
  11032. l)))
  11033. lines))
  11034. ;; Get the data fields by splitting the lines.
  11035. (setq fields (mapcar
  11036. (lambda (l)
  11037. (org-split-string l " *| *"))
  11038. (delq nil (copy-sequence lines))))
  11039. ;; How many fields in the longest line?
  11040. (condition-case nil
  11041. (setq maxfields (apply 'max (mapcar 'length fields)))
  11042. (error
  11043. (kill-region beg end)
  11044. (org-table-create org-table-default-size)
  11045. (error "Empty table - created default table")))
  11046. ;; A list of empty strings to fill any short rows on output
  11047. (setq emptystrings (make-list maxfields ""))
  11048. ;; Check for special formatting.
  11049. (setq i -1)
  11050. (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns
  11051. (setq column (mapcar (lambda (x) (or (nth i x) "")) fields))
  11052. ;; Check if there is an explicit width specified
  11053. (when (and org-table-limit-column-width narrow)
  11054. (setq c column fmax nil)
  11055. (while c
  11056. (setq e (pop c))
  11057. (if (and (stringp e) (string-match "^<\\([0-9]+\\)>$" e))
  11058. (setq fmax (string-to-number (match-string 1 e)) c nil)))
  11059. ;; Find fields that are wider than fmax, and shorten them
  11060. (when fmax
  11061. (loop for xx in column do
  11062. (when (and (stringp xx)
  11063. (> (org-string-width xx) fmax))
  11064. (org-add-props xx nil
  11065. 'help-echo
  11066. (concat "Clipped table field, use C-c ` to edit. Full value is:\n" (org-no-properties (copy-sequence xx))))
  11067. (setq f1 (min fmax (or (string-match org-bracket-link-regexp xx) fmax)))
  11068. (unless (> f1 1)
  11069. (error "Cannot narrow field starting with wide link \"%s\""
  11070. (match-string 0 xx)))
  11071. (add-text-properties f1 (length xx) (list 'org-cwidth t) xx)
  11072. (add-text-properties (- f1 2) f1
  11073. (list 'display org-narrow-column-arrow)
  11074. xx)))))
  11075. ;; Get the maximum width for each column
  11076. (push (apply 'max 1 (mapcar 'org-string-width column)) lengths)
  11077. ;; Get the fraction of numbers, to decide about alignment of the column
  11078. (setq cnt 0 frac 0.0)
  11079. (loop for x in column do
  11080. (if (equal x "")
  11081. nil
  11082. (setq frac ( / (+ (* frac cnt)
  11083. (if (string-match org-table-number-regexp x) 1 0))
  11084. (setq cnt (1+ cnt))))))
  11085. (push (>= frac org-table-number-fraction) typenums))
  11086. (setq lengths (nreverse lengths) typenums (nreverse typenums))
  11087. ;; Store the alignment of this table, for later editing of single fields
  11088. (setq org-table-last-alignment typenums
  11089. org-table-last-column-widths lengths)
  11090. ;; With invisible characters, `format' does not get the field width right
  11091. ;; So we need to make these fields wide by hand.
  11092. (when links
  11093. (loop for i from 0 upto (1- maxfields) do
  11094. (setq len (nth i lengths))
  11095. (loop for j from 0 upto (1- (length fields)) do
  11096. (setq c (nthcdr i (car (nthcdr j fields))))
  11097. (if (and (stringp (car c))
  11098. (string-match org-bracket-link-regexp (car c))
  11099. (< (org-string-width (car c)) len))
  11100. (setcar c (concat (car c) (make-string (- len (org-string-width (car c))) ?\ )))))))
  11101. ;; Compute the formats needed for output of the table
  11102. (setq rfmt (concat indent "|") hfmt (concat indent "|"))
  11103. (while (setq l (pop lengths))
  11104. (setq ty (if (pop typenums) "" "-")) ; number types flushright
  11105. (setq rfmt (concat rfmt (format rfmt1 ty l))
  11106. hfmt (concat hfmt (format hfmt1 (make-string l ?-)))))
  11107. (setq rfmt (concat rfmt "\n")
  11108. hfmt (concat (substring hfmt 0 -1) "|\n"))
  11109. (setq new (mapconcat
  11110. (lambda (l)
  11111. (if l (apply 'format rfmt
  11112. (append (pop fields) emptystrings))
  11113. hfmt))
  11114. lines ""))
  11115. ;; Replace the old one
  11116. (delete-region beg end)
  11117. (move-marker end nil)
  11118. (move-marker org-table-aligned-begin-marker (point))
  11119. (insert new)
  11120. (move-marker org-table-aligned-end-marker (point))
  11121. (when (and orgtbl-mode (not (org-mode-p)))
  11122. (goto-char org-table-aligned-begin-marker)
  11123. (while (org-hide-wide-columns org-table-aligned-end-marker)))
  11124. ;; Try to move to the old location
  11125. (goto-line winstartline)
  11126. (setq winstart (point-at-bol))
  11127. (goto-line linepos)
  11128. (set-window-start (selected-window) winstart 'noforce)
  11129. (org-table-goto-column colpos)
  11130. (setq org-table-may-need-update nil)
  11131. ))
  11132. (defun org-string-width (s)
  11133. "Compute width of string, ignoring invisible characters.
  11134. This ignores character with invisibility property `org-link', and also
  11135. characters with property `org-cwidth', because these will become invisible
  11136. upon the next fontification round."
  11137. (let (b l)
  11138. (when (or (eq t buffer-invisibility-spec)
  11139. (assq 'org-link buffer-invisibility-spec))
  11140. (while (setq b (text-property-any 0 (length s)
  11141. 'invisible 'org-link s))
  11142. (setq s (concat (substring s 0 b)
  11143. (substring s (or (next-single-property-change
  11144. b 'invisible s) (length s)))))))
  11145. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  11146. (setq s (concat (substring s 0 b)
  11147. (substring s (or (next-single-property-change
  11148. b 'org-cwidth s) (length s))))))
  11149. (setq l (string-width s) b -1)
  11150. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  11151. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  11152. l))
  11153. (defun org-table-begin (&optional table-type)
  11154. "Find the beginning of the table and return its position.
  11155. With argument TABLE-TYPE, go to the beginning of a table.el-type table."
  11156. (save-excursion
  11157. (if (not (re-search-backward
  11158. (if table-type org-table-any-border-regexp
  11159. org-table-border-regexp)
  11160. nil t))
  11161. (progn (goto-char (point-min)) (point))
  11162. (goto-char (match-beginning 0))
  11163. (beginning-of-line 2)
  11164. (point))))
  11165. (defun org-table-end (&optional table-type)
  11166. "Find the end of the table and return its position.
  11167. With argument TABLE-TYPE, go to the end of a table.el-type table."
  11168. (save-excursion
  11169. (if (not (re-search-forward
  11170. (if table-type org-table-any-border-regexp
  11171. org-table-border-regexp)
  11172. nil t))
  11173. (goto-char (point-max))
  11174. (goto-char (match-beginning 0)))
  11175. (point-marker)))
  11176. (defun org-table-justify-field-maybe (&optional new)
  11177. "Justify the current field, text to left, number to right.
  11178. Optional argument NEW may specify text to replace the current field content."
  11179. (cond
  11180. ((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
  11181. ((org-at-table-hline-p))
  11182. ((and (not new)
  11183. (or (not (equal (marker-buffer org-table-aligned-begin-marker)
  11184. (current-buffer)))
  11185. (< (point) org-table-aligned-begin-marker)
  11186. (>= (point) org-table-aligned-end-marker)))
  11187. ;; This is not the same table, force a full re-align
  11188. (setq org-table-may-need-update t))
  11189. (t ;; realign the current field, based on previous full realign
  11190. (let* ((pos (point)) s
  11191. (col (org-table-current-column))
  11192. (num (if (> col 0) (nth (1- col) org-table-last-alignment)))
  11193. l f n o e)
  11194. (when (> col 0)
  11195. (skip-chars-backward "^|\n")
  11196. (if (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)")
  11197. (progn
  11198. (setq s (match-string 1)
  11199. o (match-string 0)
  11200. l (max 1 (- (match-end 0) (match-beginning 0) 3))
  11201. e (not (= (match-beginning 2) (match-end 2))))
  11202. (setq f (format (if num " %%%ds %s" " %%-%ds %s")
  11203. l (if e "|" (setq org-table-may-need-update t) ""))
  11204. n (format f s))
  11205. (if new
  11206. (if (<= (length new) l) ;; FIXME: length -> str-width?
  11207. (setq n (format f new))
  11208. (setq n (concat new "|") org-table-may-need-update t)))
  11209. (or (equal n o)
  11210. (let (org-table-may-need-update)
  11211. (replace-match n))))
  11212. (setq org-table-may-need-update t))
  11213. (goto-char pos))))))
  11214. (defun org-table-next-field ()
  11215. "Go to the next field in the current table, creating new lines as needed.
  11216. Before doing so, re-align the table if necessary."
  11217. (interactive)
  11218. (org-table-maybe-eval-formula)
  11219. (org-table-maybe-recalculate-line)
  11220. (if (and org-table-automatic-realign
  11221. org-table-may-need-update)
  11222. (org-table-align))
  11223. (let ((end (org-table-end)))
  11224. (if (org-at-table-hline-p)
  11225. (end-of-line 1))
  11226. (condition-case nil
  11227. (progn
  11228. (re-search-forward "|" end)
  11229. (if (looking-at "[ \t]*$")
  11230. (re-search-forward "|" end))
  11231. (if (and (looking-at "-")
  11232. org-table-tab-jumps-over-hlines
  11233. (re-search-forward "^[ \t]*|\\([^-]\\)" end t))
  11234. (goto-char (match-beginning 1)))
  11235. (if (looking-at "-")
  11236. (progn
  11237. (beginning-of-line 0)
  11238. (org-table-insert-row 'below))
  11239. (if (looking-at " ") (forward-char 1))))
  11240. (error
  11241. (org-table-insert-row 'below)))))
  11242. (defun org-table-previous-field ()
  11243. "Go to the previous field in the table.
  11244. Before doing so, re-align the table if necessary."
  11245. (interactive)
  11246. (org-table-justify-field-maybe)
  11247. (org-table-maybe-recalculate-line)
  11248. (if (and org-table-automatic-realign
  11249. org-table-may-need-update)
  11250. (org-table-align))
  11251. (if (org-at-table-hline-p)
  11252. (end-of-line 1))
  11253. (re-search-backward "|" (org-table-begin))
  11254. (re-search-backward "|" (org-table-begin))
  11255. (while (looking-at "|\\(-\\|[ \t]*$\\)")
  11256. (re-search-backward "|" (org-table-begin)))
  11257. (if (looking-at "| ?")
  11258. (goto-char (match-end 0))))
  11259. (defun org-table-next-row ()
  11260. "Go to the next row (same column) in the current table.
  11261. Before doing so, re-align the table if necessary."
  11262. (interactive)
  11263. (org-table-maybe-eval-formula)
  11264. (org-table-maybe-recalculate-line)
  11265. (if (or (looking-at "[ \t]*$")
  11266. (save-excursion (skip-chars-backward " \t") (bolp)))
  11267. (newline)
  11268. (if (and org-table-automatic-realign
  11269. org-table-may-need-update)
  11270. (org-table-align))
  11271. (let ((col (org-table-current-column)))
  11272. (beginning-of-line 2)
  11273. (if (or (not (org-at-table-p))
  11274. (org-at-table-hline-p))
  11275. (progn
  11276. (beginning-of-line 0)
  11277. (org-table-insert-row 'below)))
  11278. (org-table-goto-column col)
  11279. (skip-chars-backward "^|\n\r")
  11280. (if (looking-at " ") (forward-char 1)))))
  11281. (defun org-table-copy-down (n)
  11282. "Copy a field down in the current column.
  11283. If the field at the cursor is empty, copy into it the content of the nearest
  11284. non-empty field above. With argument N, use the Nth non-empty field.
  11285. If the current field is not empty, it is copied down to the next row, and
  11286. the cursor is moved with it. Therefore, repeating this command causes the
  11287. column to be filled row-by-row.
  11288. If the variable `org-table-copy-increment' is non-nil and the field is an
  11289. integer, it will be incremented while copying."
  11290. (interactive "p")
  11291. (let* ((colpos (org-table-current-column))
  11292. (field (org-table-get-field))
  11293. (non-empty (string-match "[^ \t]" field))
  11294. (beg (org-table-begin))
  11295. txt)
  11296. (org-table-check-inside-data-field)
  11297. (if non-empty
  11298. (progn
  11299. (setq txt (org-trim field))
  11300. (org-table-next-row)
  11301. (org-table-blank-field))
  11302. (save-excursion
  11303. (setq txt
  11304. (catch 'exit
  11305. (while (progn (beginning-of-line 1)
  11306. (re-search-backward org-table-dataline-regexp
  11307. beg t))
  11308. (org-table-goto-column colpos t)
  11309. (if (and (looking-at
  11310. "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
  11311. (= (setq n (1- n)) 0))
  11312. (throw 'exit (match-string 1))))))))
  11313. (if txt
  11314. (progn
  11315. (if (and org-table-copy-increment
  11316. (string-match "^[0-9]+$" txt))
  11317. (setq txt (format "%d" (+ (string-to-number txt) 1))))
  11318. (insert txt)
  11319. (org-table-maybe-recalculate-line)
  11320. (org-table-align))
  11321. (error "No non-empty field found"))))
  11322. (defun org-table-check-inside-data-field ()
  11323. "Is point inside a table data field?
  11324. I.e. not on a hline or before the first or after the last column?
  11325. This actually throws an error, so it aborts the current command."
  11326. (if (or (not (org-at-table-p))
  11327. (= (org-table-current-column) 0)
  11328. (org-at-table-hline-p)
  11329. (looking-at "[ \t]*$"))
  11330. (error "Not in table data field")))
  11331. (defvar org-table-clip nil
  11332. "Clipboard for table regions.")
  11333. (defun org-table-blank-field ()
  11334. "Blank the current table field or active region."
  11335. (interactive)
  11336. (org-table-check-inside-data-field)
  11337. (if (and (interactive-p) (org-region-active-p))
  11338. (let (org-table-clip)
  11339. (org-table-cut-region (region-beginning) (region-end)))
  11340. (skip-chars-backward "^|")
  11341. (backward-char 1)
  11342. (if (looking-at "|[^|\n]+")
  11343. (let* ((pos (match-beginning 0))
  11344. (match (match-string 0))
  11345. (len (org-string-width match)))
  11346. (replace-match (concat "|" (make-string (1- len) ?\ )))
  11347. (goto-char (+ 2 pos))
  11348. (substring match 1)))))
  11349. (defun org-table-get-field (&optional n replace)
  11350. "Return the value of the field in column N of current row.
  11351. N defaults to current field.
  11352. If REPLACE is a string, replace field with this value. The return value
  11353. is always the old value."
  11354. (and n (org-table-goto-column n))
  11355. (skip-chars-backward "^|\n")
  11356. (backward-char 1)
  11357. (if (looking-at "|[^|\r\n]*")
  11358. (let* ((pos (match-beginning 0))
  11359. (val (buffer-substring (1+ pos) (match-end 0))))
  11360. (if replace
  11361. (replace-match (concat "|" replace)))
  11362. (goto-char (min (point-at-eol) (+ 2 pos)))
  11363. val)
  11364. (forward-char 1) ""))
  11365. (defun org-table-current-column ()
  11366. "Find out which column we are in.
  11367. When called interactively, column is also displayed in echo area."
  11368. (interactive)
  11369. (if (interactive-p) (org-table-check-inside-data-field))
  11370. (save-excursion
  11371. (let ((cnt 0) (pos (point)))
  11372. (beginning-of-line 1)
  11373. (while (search-forward "|" pos t)
  11374. (setq cnt (1+ cnt)))
  11375. (if (interactive-p) (message "This is table column %d" cnt))
  11376. cnt)))
  11377. (defun org-table-goto-column (n &optional on-delim force)
  11378. "Move the cursor to the Nth column in the current table line.
  11379. With optional argument ON-DELIM, stop with point before the left delimiter
  11380. of the field.
  11381. If there are less than N fields, just go to after the last delimiter.
  11382. However, when FORCE is non-nil, create new columns if necessary."
  11383. (interactive "p")
  11384. (let ((pos (point-at-eol)))
  11385. (beginning-of-line 1)
  11386. (when (> n 0)
  11387. (while (and (> (setq n (1- n)) -1)
  11388. (or (search-forward "|" pos t)
  11389. (and force
  11390. (progn (end-of-line 1)
  11391. (skip-chars-backward "^|")
  11392. (insert " | "))))))
  11393. ; (backward-char 2) t)))))
  11394. (when (and force (not (looking-at ".*|")))
  11395. (save-excursion (end-of-line 1) (insert " | ")))
  11396. (if on-delim
  11397. (backward-char 1)
  11398. (if (looking-at " ") (forward-char 1))))))
  11399. (defun org-at-table-p (&optional table-type)
  11400. "Return t if the cursor is inside an org-type table.
  11401. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  11402. (if org-enable-table-editor
  11403. (save-excursion
  11404. (beginning-of-line 1)
  11405. (looking-at (if table-type org-table-any-line-regexp
  11406. org-table-line-regexp)))
  11407. nil))
  11408. (defun org-at-table.el-p ()
  11409. "Return t if and only if we are at a table.el table."
  11410. (and (org-at-table-p 'any)
  11411. (save-excursion
  11412. (goto-char (org-table-begin 'any))
  11413. (looking-at org-table1-hline-regexp))))
  11414. (defun org-table-recognize-table.el ()
  11415. "If there is a table.el table nearby, recognize it and move into it."
  11416. (if org-table-tab-recognizes-table.el
  11417. (if (org-at-table.el-p)
  11418. (progn
  11419. (beginning-of-line 1)
  11420. (if (looking-at org-table-dataline-regexp)
  11421. nil
  11422. (if (looking-at org-table1-hline-regexp)
  11423. (progn
  11424. (beginning-of-line 2)
  11425. (if (looking-at org-table-any-border-regexp)
  11426. (beginning-of-line -1)))))
  11427. (if (re-search-forward "|" (org-table-end t) t)
  11428. (progn
  11429. (require 'table)
  11430. (if (table--at-cell-p (point))
  11431. t
  11432. (message "recognizing table.el table...")
  11433. (table-recognize-table)
  11434. (message "recognizing table.el table...done")))
  11435. (error "This should not happen..."))
  11436. t)
  11437. nil)
  11438. nil))
  11439. (defun org-at-table-hline-p ()
  11440. "Return t if the cursor is inside a hline in a table."
  11441. (if org-enable-table-editor
  11442. (save-excursion
  11443. (beginning-of-line 1)
  11444. (looking-at org-table-hline-regexp))
  11445. nil))
  11446. (defun org-table-insert-column ()
  11447. "Insert a new column into the table."
  11448. (interactive)
  11449. (if (not (org-at-table-p))
  11450. (error "Not at a table"))
  11451. (org-table-find-dataline)
  11452. (let* ((col (max 1 (org-table-current-column)))
  11453. (beg (org-table-begin))
  11454. (end (org-table-end))
  11455. ;; Current cursor position
  11456. (linepos (org-current-line))
  11457. (colpos col))
  11458. (goto-char beg)
  11459. (while (< (point) end)
  11460. (if (org-at-table-hline-p)
  11461. nil
  11462. (org-table-goto-column col t)
  11463. (insert "| "))
  11464. (beginning-of-line 2))
  11465. (move-marker end nil)
  11466. (goto-line linepos)
  11467. (org-table-goto-column colpos)
  11468. (org-table-align)
  11469. (org-table-modify-formulas 'insert col)))
  11470. (defun org-table-find-dataline ()
  11471. "Find a dataline in the current table, which is needed for column commands."
  11472. (if (and (org-at-table-p)
  11473. (not (org-at-table-hline-p)))
  11474. t
  11475. (let ((col (current-column))
  11476. (end (org-table-end)))
  11477. (move-to-column col)
  11478. (while (and (< (point) end)
  11479. (or (not (= (current-column) col))
  11480. (org-at-table-hline-p)))
  11481. (beginning-of-line 2)
  11482. (move-to-column col))
  11483. (if (and (org-at-table-p)
  11484. (not (org-at-table-hline-p)))
  11485. t
  11486. (error
  11487. "Please position cursor in a data line for column operations")))))
  11488. (defun org-table-delete-column ()
  11489. "Delete a column from the table."
  11490. (interactive)
  11491. (if (not (org-at-table-p))
  11492. (error "Not at a table"))
  11493. (org-table-find-dataline)
  11494. (org-table-check-inside-data-field)
  11495. (let* ((col (org-table-current-column))
  11496. (beg (org-table-begin))
  11497. (end (org-table-end))
  11498. ;; Current cursor position
  11499. (linepos (org-current-line))
  11500. (colpos col))
  11501. (goto-char beg)
  11502. (while (< (point) end)
  11503. (if (org-at-table-hline-p)
  11504. nil
  11505. (org-table-goto-column col t)
  11506. (and (looking-at "|[^|\n]+|")
  11507. (replace-match "|")))
  11508. (beginning-of-line 2))
  11509. (move-marker end nil)
  11510. (goto-line linepos)
  11511. (org-table-goto-column colpos)
  11512. (org-table-align)
  11513. (org-table-modify-formulas 'remove col)))
  11514. (defun org-table-move-column-right ()
  11515. "Move column to the right."
  11516. (interactive)
  11517. (org-table-move-column nil))
  11518. (defun org-table-move-column-left ()
  11519. "Move column to the left."
  11520. (interactive)
  11521. (org-table-move-column 'left))
  11522. (defun org-table-move-column (&optional left)
  11523. "Move the current column to the right. With arg LEFT, move to the left."
  11524. (interactive "P")
  11525. (if (not (org-at-table-p))
  11526. (error "Not at a table"))
  11527. (org-table-find-dataline)
  11528. (org-table-check-inside-data-field)
  11529. (let* ((col (org-table-current-column))
  11530. (col1 (if left (1- col) col))
  11531. (beg (org-table-begin))
  11532. (end (org-table-end))
  11533. ;; Current cursor position
  11534. (linepos (org-current-line))
  11535. (colpos (if left (1- col) (1+ col))))
  11536. (if (and left (= col 1))
  11537. (error "Cannot move column further left"))
  11538. (if (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
  11539. (error "Cannot move column further right"))
  11540. (goto-char beg)
  11541. (while (< (point) end)
  11542. (if (org-at-table-hline-p)
  11543. nil
  11544. (org-table-goto-column col1 t)
  11545. (and (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
  11546. (replace-match "|\\2|\\1|")))
  11547. (beginning-of-line 2))
  11548. (move-marker end nil)
  11549. (goto-line linepos)
  11550. (org-table-goto-column colpos)
  11551. (org-table-align)
  11552. (org-table-modify-formulas 'swap col (if left (1- col) (1+ col)))))
  11553. (defun org-table-move-row-down ()
  11554. "Move table row down."
  11555. (interactive)
  11556. (org-table-move-row nil))
  11557. (defun org-table-move-row-up ()
  11558. "Move table row up."
  11559. (interactive)
  11560. (org-table-move-row 'up))
  11561. (defun org-table-move-row (&optional up)
  11562. "Move the current table line down. With arg UP, move it up."
  11563. (interactive "P")
  11564. (let ((col (current-column))
  11565. (pos (point))
  11566. (tonew (if up 0 2))
  11567. txt)
  11568. (beginning-of-line tonew)
  11569. (if (not (org-at-table-p))
  11570. (progn
  11571. (goto-char pos)
  11572. (error "Cannot move row further")))
  11573. (goto-char pos)
  11574. (beginning-of-line 1)
  11575. (setq pos (point))
  11576. (setq txt (buffer-substring (point) (1+ (point-at-eol))))
  11577. (delete-region (point) (1+ (point-at-eol)))
  11578. (beginning-of-line tonew)
  11579. (insert txt)
  11580. (beginning-of-line 0)
  11581. (move-to-column col)))
  11582. (defun org-table-insert-row (&optional arg)
  11583. "Insert a new row above the current line into the table.
  11584. With prefix ARG, insert below the current line."
  11585. (interactive "P")
  11586. (if (not (org-at-table-p))
  11587. (error "Not at a table"))
  11588. (let* ((line (buffer-substring (point-at-bol) (point-at-eol)))
  11589. (new (org-table-clean-line line)))
  11590. ;; Fix the first field if necessary
  11591. (if (string-match "^[ \t]*| *[#$] *|" line)
  11592. (setq new (replace-match (match-string 0 line) t t new)))
  11593. (beginning-of-line (if arg 2 1))
  11594. (let (org-table-may-need-update) (insert-before-markers new "\n"))
  11595. (beginning-of-line 0)
  11596. (re-search-forward "| ?" (point-at-eol) t)
  11597. (and org-table-may-need-update (org-table-align))))
  11598. (defun org-table-insert-hline (&optional arg)
  11599. "Insert a horizontal-line below the current line into the table.
  11600. With prefix ARG, insert above the current line."
  11601. (interactive "P")
  11602. (if (not (org-at-table-p))
  11603. (error "Not at a table"))
  11604. (let ((line (org-table-clean-line
  11605. (buffer-substring (point-at-bol) (point-at-eol))))
  11606. (col (current-column)))
  11607. (while (string-match "|\\( +\\)|" line)
  11608. (setq line (replace-match
  11609. (concat "+" (make-string (- (match-end 1) (match-beginning 1))
  11610. ?-) "|") t t line)))
  11611. (and (string-match "\\+" line) (setq line (replace-match "|" t t line)))
  11612. (beginning-of-line (if arg 1 2))
  11613. (insert line "\n")
  11614. (beginning-of-line (if arg 1 -1))
  11615. (move-to-column col)))
  11616. (defun org-table-clean-line (s)
  11617. "Convert a table line S into a string with only \"|\" and space.
  11618. In particular, this does handle wide and invisible characters."
  11619. (if (string-match "^[ \t]*|-" s)
  11620. ;; It's a hline, just map the characters
  11621. (setq s (mapconcat (lambda (x) (if (member x '(?| ?+)) "|" " ")) s ""))
  11622. (while (string-match "|\\([ \t]*?[^ \t\r\n|][^\r\n|]*\\)|" s)
  11623. (setq s (replace-match
  11624. (concat "|" (make-string (org-string-width (match-string 1 s))
  11625. ?\ ) "|")
  11626. t t s)))
  11627. s))
  11628. (defun org-table-kill-row ()
  11629. "Delete the current row or horizontal line from the table."
  11630. (interactive)
  11631. (if (not (org-at-table-p))
  11632. (error "Not at a table"))
  11633. (let ((col (current-column)))
  11634. (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
  11635. (if (not (org-at-table-p)) (beginning-of-line 0))
  11636. (move-to-column col)))
  11637. (defun org-table-sort-lines (with-case &optional sorting-type)
  11638. "Sort table lines according to the column at point.
  11639. The position of point indicates the column to be used for
  11640. sorting, and the range of lines is the range between the nearest
  11641. horizontal separator lines, or the entire table of no such lines
  11642. exist. If point is before the first column, you will be prompted
  11643. for the sorting column. If there is an active region, the mark
  11644. specifies the first line and the sorting column, while point
  11645. should be in the last line to be included into the sorting.
  11646. The command then prompts for the sorting type which can be
  11647. alphabetically, numerically, or by time (as given in a time stamp
  11648. in the field). Sorting in reverse order is also possible.
  11649. With prefix argument WITH-CASE, alphabetic sorting will be case-sensitive.
  11650. If SORTING-TYPE is specified when this function is called from a Lisp
  11651. program, no prompting will take place. SORTING-TYPE must be a character,
  11652. any of (?a ?A ?n ?N ?t ?T) where the capital letter indicate that sorting
  11653. should be done in reverse order."
  11654. (interactive "P")
  11655. (let* ((thisline (org-current-line))
  11656. (thiscol (org-table-current-column))
  11657. beg end bcol ecol tend tbeg column lns pos)
  11658. (when (equal thiscol 0)
  11659. (if (interactive-p)
  11660. (setq thiscol
  11661. (string-to-number
  11662. (read-string "Use column N for sorting: ")))
  11663. (setq thiscol 1))
  11664. (org-table-goto-column thiscol))
  11665. (org-table-check-inside-data-field)
  11666. (if (org-region-active-p)
  11667. (progn
  11668. (setq beg (region-beginning) end (region-end))
  11669. (goto-char beg)
  11670. (setq column (org-table-current-column)
  11671. beg (point-at-bol))
  11672. (goto-char end)
  11673. (setq end (point-at-bol 2)))
  11674. (setq column (org-table-current-column)
  11675. pos (point)
  11676. tbeg (org-table-begin)
  11677. tend (org-table-end))
  11678. (if (re-search-backward org-table-hline-regexp tbeg t)
  11679. (setq beg (point-at-bol 2))
  11680. (goto-char tbeg)
  11681. (setq beg (point-at-bol 1)))
  11682. (goto-char pos)
  11683. (if (re-search-forward org-table-hline-regexp tend t)
  11684. (setq beg (point-at-bol 0))
  11685. (goto-char tend)
  11686. (setq end (point-at-bol))))
  11687. (setq beg (move-marker (make-marker) beg)
  11688. end (move-marker (make-marker) end))
  11689. (untabify beg end)
  11690. (goto-char beg)
  11691. (org-table-goto-column column)
  11692. (skip-chars-backward "^|")
  11693. (setq bcol (current-column))
  11694. (org-table-goto-column (1+ column))
  11695. (skip-chars-backward "^|")
  11696. (setq ecol (1- (current-column)))
  11697. (org-table-goto-column column)
  11698. (setq lns (mapcar (lambda(x) (cons (org-trim (substring x bcol ecol)) x))
  11699. (org-split-string (buffer-substring beg end) "\n")))
  11700. (setq lns (org-do-sort lns "Table" with-case sorting-type))
  11701. (delete-region beg end)
  11702. (move-marker beg nil)
  11703. (move-marker end nil)
  11704. (insert (mapconcat 'cdr lns "\n") "\n")
  11705. (goto-line thisline)
  11706. (org-table-goto-column thiscol)
  11707. (message "%d lines sorted, based on column %d" (length lns) column)))
  11708. (defun org-table-cut-region (beg end)
  11709. "Copy region in table to the clipboard and blank all relevant fields."
  11710. (interactive "r")
  11711. (org-table-copy-region beg end 'cut))
  11712. (defun org-table-copy-region (beg end &optional cut)
  11713. "Copy rectangular region in table to clipboard.
  11714. A special clipboard is used which can only be accessed
  11715. with `org-table-paste-rectangle'."
  11716. (interactive "rP")
  11717. (let* (l01 c01 l02 c02 l1 c1 l2 c2 ic1 ic2
  11718. region cols
  11719. (rpl (if cut " " nil)))
  11720. (goto-char beg)
  11721. (org-table-check-inside-data-field)
  11722. (setq l01 (count-lines (point-min) (point))
  11723. c01 (org-table-current-column))
  11724. (goto-char end)
  11725. (org-table-check-inside-data-field)
  11726. (setq l02 (count-lines (point-min) (point))
  11727. c02 (org-table-current-column))
  11728. (setq l1 (min l01 l02) l2 (max l01 l02)
  11729. c1 (min c01 c02) c2 (max c01 c02))
  11730. (catch 'exit
  11731. (while t
  11732. (catch 'nextline
  11733. (if (> l1 l2) (throw 'exit t))
  11734. (goto-line l1)
  11735. (if (org-at-table-hline-p) (throw 'nextline (setq l1 (1+ l1))))
  11736. (setq cols nil ic1 c1 ic2 c2)
  11737. (while (< ic1 (1+ ic2))
  11738. (push (org-table-get-field ic1 rpl) cols)
  11739. (setq ic1 (1+ ic1)))
  11740. (push (nreverse cols) region)
  11741. (setq l1 (1+ l1)))))
  11742. (setq org-table-clip (nreverse region))
  11743. (if cut (org-table-align))
  11744. org-table-clip))
  11745. (defun org-table-paste-rectangle ()
  11746. "Paste a rectangular region into a table.
  11747. The upper right corner ends up in the current field. All involved fields
  11748. will be overwritten. If the rectangle does not fit into the present table,
  11749. the table is enlarged as needed. The process ignores horizontal separator
  11750. lines."
  11751. (interactive)
  11752. (unless (and org-table-clip (listp org-table-clip))
  11753. (error "First cut/copy a region to paste!"))
  11754. (org-table-check-inside-data-field)
  11755. (let* ((clip org-table-clip)
  11756. (line (count-lines (point-min) (point)))
  11757. (col (org-table-current-column))
  11758. (org-enable-table-editor t)
  11759. (org-table-automatic-realign nil)
  11760. c cols field)
  11761. (while (setq cols (pop clip))
  11762. (while (org-at-table-hline-p) (beginning-of-line 2))
  11763. (if (not (org-at-table-p))
  11764. (progn (end-of-line 0) (org-table-next-field)))
  11765. (setq c col)
  11766. (while (setq field (pop cols))
  11767. (org-table-goto-column c nil 'force)
  11768. (org-table-get-field nil field)
  11769. (setq c (1+ c)))
  11770. (beginning-of-line 2))
  11771. (goto-line line)
  11772. (org-table-goto-column col)
  11773. (org-table-align)))
  11774. (defun org-table-convert ()
  11775. "Convert from `org-mode' table to table.el and back.
  11776. Obviously, this only works within limits. When an Org-mode table is
  11777. converted to table.el, all horizontal separator lines get lost, because
  11778. table.el uses these as cell boundaries and has no notion of horizontal lines.
  11779. A table.el table can be converted to an Org-mode table only if it does not
  11780. do row or column spanning. Multiline cells will become multiple cells.
  11781. Beware, Org-mode does not test if the table can be successfully converted - it
  11782. blindly applies a recipe that works for simple tables."
  11783. (interactive)
  11784. (require 'table)
  11785. (if (org-at-table.el-p)
  11786. ;; convert to Org-mode table
  11787. (let ((beg (move-marker (make-marker) (org-table-begin t)))
  11788. (end (move-marker (make-marker) (org-table-end t))))
  11789. (table-unrecognize-region beg end)
  11790. (goto-char beg)
  11791. (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
  11792. (replace-match ""))
  11793. (goto-char beg))
  11794. (if (org-at-table-p)
  11795. ;; convert to table.el table
  11796. (let ((beg (move-marker (make-marker) (org-table-begin)))
  11797. (end (move-marker (make-marker) (org-table-end))))
  11798. ;; first, get rid of all horizontal lines
  11799. (goto-char beg)
  11800. (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t)
  11801. (replace-match ""))
  11802. ;; insert a hline before first
  11803. (goto-char beg)
  11804. (org-table-insert-hline 'above)
  11805. (beginning-of-line -1)
  11806. ;; insert a hline after each line
  11807. (while (progn (beginning-of-line 3) (< (point) end))
  11808. (org-table-insert-hline))
  11809. (goto-char beg)
  11810. (setq end (move-marker end (org-table-end)))
  11811. ;; replace "+" at beginning and ending of hlines
  11812. (while (re-search-forward "^\\([ \t]*\\)|-" end t)
  11813. (replace-match "\\1+-"))
  11814. (goto-char beg)
  11815. (while (re-search-forward "-|[ \t]*$" end t)
  11816. (replace-match "-+"))
  11817. (goto-char beg)))))
  11818. (defun org-table-wrap-region (arg)
  11819. "Wrap several fields in a column like a paragraph.
  11820. This is useful if you'd like to spread the contents of a field over several
  11821. lines, in order to keep the table compact.
  11822. If there is an active region, and both point and mark are in the same column,
  11823. the text in the column is wrapped to minimum width for the given number of
  11824. lines. Generally, this makes the table more compact. A prefix ARG may be
  11825. used to change the number of desired lines. For example, `C-2 \\[org-table-wrap]'
  11826. formats the selected text to two lines. If the region was longer than two
  11827. lines, the remaining lines remain empty. A negative prefix argument reduces
  11828. the current number of lines by that amount. The wrapped text is pasted back
  11829. into the table. If you formatted it to more lines than it was before, fields
  11830. further down in the table get overwritten - so you might need to make space in
  11831. the table first.
  11832. If there is no region, the current field is split at the cursor position and
  11833. the text fragment to the right of the cursor is prepended to the field one
  11834. line down.
  11835. If there is no region, but you specify a prefix ARG, the current field gets
  11836. blank, and the content is appended to the field above."
  11837. (interactive "P")
  11838. (org-table-check-inside-data-field)
  11839. (if (org-region-active-p)
  11840. ;; There is a region: fill as a paragraph
  11841. (let ((beg (region-beginning))
  11842. nlines)
  11843. (org-table-cut-region (region-beginning) (region-end))
  11844. (if (> (length (car org-table-clip)) 1)
  11845. (error "Region must be limited to single column"))
  11846. (setq nlines (if arg
  11847. (if (< arg 1)
  11848. (+ (length org-table-clip) arg)
  11849. arg)
  11850. (length org-table-clip)))
  11851. (setq org-table-clip
  11852. (mapcar 'list (org-wrap (mapconcat 'car org-table-clip " ")
  11853. nil nlines)))
  11854. (goto-char beg)
  11855. (org-table-paste-rectangle))
  11856. ;; No region, split the current field at point
  11857. (if arg
  11858. ;; combine with field above
  11859. (let ((s (org-table-blank-field))
  11860. (col (org-table-current-column)))
  11861. (beginning-of-line 0)
  11862. (while (org-at-table-hline-p) (beginning-of-line 0))
  11863. (org-table-goto-column col)
  11864. (skip-chars-forward "^|")
  11865. (skip-chars-backward " ")
  11866. (insert " " (org-trim s))
  11867. (org-table-align))
  11868. ;; split field
  11869. (when (looking-at "\\([^|]+\\)+|")
  11870. (let ((s (match-string 1)))
  11871. (replace-match " |")
  11872. (goto-char (match-beginning 0))
  11873. (org-table-next-row)
  11874. (insert (org-trim s) " ")
  11875. (org-table-align))))))
  11876. (defvar org-field-marker nil)
  11877. (defun org-table-edit-field (arg)
  11878. "Edit table field in a different window.
  11879. This is mainly useful for fields that contain hidden parts.
  11880. When called with a \\[universal-argument] prefix, just make the full field visible so that
  11881. it can be edited in place."
  11882. (interactive "P")
  11883. (if arg
  11884. (let ((b (save-excursion (skip-chars-backward "^|") (point)))
  11885. (e (save-excursion (skip-chars-forward "^|\r\n") (point))))
  11886. (remove-text-properties b e '(org-cwidth t invisible t
  11887. display t intangible t))
  11888. (if (and (boundp 'font-lock-mode) font-lock-mode)
  11889. (font-lock-fontify-block)))
  11890. (let ((pos (move-marker (make-marker) (point)))
  11891. (field (org-table-get-field))
  11892. (cw (current-window-configuration))
  11893. p)
  11894. (switch-to-buffer-other-window "*Org tmp*")
  11895. (erase-buffer)
  11896. (insert "#\n# Edit field and finish with C-c C-c\n#\n")
  11897. (org-mode)
  11898. (goto-char (setq p (point-max)))
  11899. (insert (org-trim field))
  11900. (remove-text-properties p (point-max)
  11901. '(invisible t org-cwidth t display t
  11902. intangible t))
  11903. (goto-char p)
  11904. (org-set-local 'org-finish-function
  11905. 'org-table-finish-edit-field)
  11906. (org-set-local 'org-window-configuration cw)
  11907. (org-set-local 'org-field-marker pos)
  11908. (message "Edit and finish with C-c C-c"))))
  11909. (defun org-table-finish-edit-field ()
  11910. "Finish editing a table data field.
  11911. Remove all newline characters, insert the result into the table, realign
  11912. the table and kill the editing buffer."
  11913. (let ((pos org-field-marker)
  11914. (cw org-window-configuration)
  11915. (cb (current-buffer))
  11916. text)
  11917. (goto-char (point-min))
  11918. (while (re-search-forward "^#.*\n?" nil t) (replace-match ""))
  11919. (while (re-search-forward "\\([ \t]*\n[ \t]*\\)+" nil t)
  11920. (replace-match " "))
  11921. (setq text (org-trim (buffer-string)))
  11922. (set-window-configuration cw)
  11923. (kill-buffer cb)
  11924. (select-window (get-buffer-window (marker-buffer pos)))
  11925. (goto-char pos)
  11926. (move-marker pos nil)
  11927. (org-table-check-inside-data-field)
  11928. (org-table-get-field nil text)
  11929. (org-table-align)
  11930. (message "New field value inserted")))
  11931. (defun org-trim (s)
  11932. "Remove whitespace at beginning and end of string."
  11933. (if (string-match "^[ \t]+" s) (setq s (replace-match "" t t s)))
  11934. (if (string-match "[ \t]+$" s) (setq s (replace-match "" t t s)))
  11935. s)
  11936. (defun org-wrap (string &optional width lines)
  11937. "Wrap string to either a number of lines, or a width in characters.
  11938. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  11939. that costs. If there is a word longer than WIDTH, the text is actually
  11940. wrapped to the length of that word.
  11941. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  11942. many lines, whatever width that takes.
  11943. The return value is a list of lines, without newlines at the end."
  11944. (let* ((words (org-split-string string "[ \t\n]+"))
  11945. (maxword (apply 'max (mapcar 'org-string-width words)))
  11946. w ll)
  11947. (cond (width
  11948. (org-do-wrap words (max maxword width)))
  11949. (lines
  11950. (setq w maxword)
  11951. (setq ll (org-do-wrap words maxword))
  11952. (if (<= (length ll) lines)
  11953. ll
  11954. (setq ll words)
  11955. (while (> (length ll) lines)
  11956. (setq w (1+ w))
  11957. (setq ll (org-do-wrap words w)))
  11958. ll))
  11959. (t (error "Cannot wrap this")))))
  11960. (defun org-do-wrap (words width)
  11961. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  11962. (let (lines line)
  11963. (while words
  11964. (setq line (pop words))
  11965. (while (and words (< (+ (length line) (length (car words))) width))
  11966. (setq line (concat line " " (pop words))))
  11967. (setq lines (push line lines)))
  11968. (nreverse lines)))
  11969. (defun org-split-string (string &optional separators)
  11970. "Splits STRING into substrings at SEPARATORS.
  11971. No empty strings are returned if there are matches at the beginning
  11972. and end of string."
  11973. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  11974. (start 0)
  11975. notfirst
  11976. (list nil))
  11977. (while (and (string-match rexp string
  11978. (if (and notfirst
  11979. (= start (match-beginning 0))
  11980. (< start (length string)))
  11981. (1+ start) start))
  11982. (< (match-beginning 0) (length string)))
  11983. (setq notfirst t)
  11984. (or (eq (match-beginning 0) 0)
  11985. (and (eq (match-beginning 0) (match-end 0))
  11986. (eq (match-beginning 0) start))
  11987. (setq list
  11988. (cons (substring string start (match-beginning 0))
  11989. list)))
  11990. (setq start (match-end 0)))
  11991. (or (eq start (length string))
  11992. (setq list
  11993. (cons (substring string start)
  11994. list)))
  11995. (nreverse list)))
  11996. (defun org-table-map-tables (function)
  11997. "Apply FUNCTION to the start of all tables in the buffer."
  11998. (save-excursion
  11999. (save-restriction
  12000. (widen)
  12001. (goto-char (point-min))
  12002. (while (re-search-forward org-table-any-line-regexp nil t)
  12003. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  12004. (beginning-of-line 1)
  12005. (if (looking-at org-table-line-regexp)
  12006. (save-excursion (funcall function)))
  12007. (re-search-forward org-table-any-border-regexp nil 1))))
  12008. (message "Mapping tables: done"))
  12009. (defun org-table-sum (&optional beg end nlast)
  12010. "Sum numbers in region of current table column.
  12011. The result will be displayed in the echo area, and will be available
  12012. as kill to be inserted with \\[yank].
  12013. If there is an active region, it is interpreted as a rectangle and all
  12014. numbers in that rectangle will be summed. If there is no active
  12015. region and point is located in a table column, sum all numbers in that
  12016. column.
  12017. If at least one number looks like a time HH:MM or HH:MM:SS, all other
  12018. numbers are assumed to be times as well (in decimal hours) and the
  12019. numbers are added as such.
  12020. If NLAST is a number, only the NLAST fields will actually be summed."
  12021. (interactive)
  12022. (save-excursion
  12023. (let (col (timecnt 0) diff h m s org-table-clip)
  12024. (cond
  12025. ((and beg end)) ; beg and end given explicitly
  12026. ((org-region-active-p)
  12027. (setq beg (region-beginning) end (region-end)))
  12028. (t
  12029. (setq col (org-table-current-column))
  12030. (goto-char (org-table-begin))
  12031. (unless (re-search-forward "^[ \t]*|[^-]" nil t)
  12032. (error "No table data"))
  12033. (org-table-goto-column col)
  12034. ;not needed? (skip-chars-backward "^|")
  12035. (setq beg (point))
  12036. (goto-char (org-table-end))
  12037. (unless (re-search-backward "^[ \t]*|[^-]" nil t)
  12038. (error "No table data"))
  12039. (org-table-goto-column col)
  12040. ;not needed? (skip-chars-forward "^|")
  12041. (setq end (point))))
  12042. (let* ((items (apply 'append (org-table-copy-region beg end)))
  12043. (items1 (cond ((not nlast) items)
  12044. ((>= nlast (length items)) items)
  12045. (t (setq items (reverse items))
  12046. (setcdr (nthcdr (1- nlast) items) nil)
  12047. (nreverse items))))
  12048. (numbers (delq nil (mapcar 'org-table-get-number-for-summing
  12049. items1)))
  12050. (res (apply '+ numbers))
  12051. (sres (if (= timecnt 0)
  12052. (format "%g" res)
  12053. (setq diff (* 3600 res)
  12054. h (floor (/ diff 3600)) diff (mod diff 3600)
  12055. m (floor (/ diff 60)) diff (mod diff 60)
  12056. s diff)
  12057. (format "%d:%02d:%02d" h m s))))
  12058. (kill-new sres)
  12059. (if (interactive-p)
  12060. (message "%s"
  12061. (substitute-command-keys
  12062. (format "Sum of %d items: %-20s (\\[yank] will insert result into buffer)"
  12063. (length numbers) sres))))
  12064. sres))))
  12065. (defun org-table-get-number-for-summing (s)
  12066. (let (n)
  12067. (if (string-match "^ *|? *" s)
  12068. (setq s (replace-match "" nil nil s)))
  12069. (if (string-match " *|? *$" s)
  12070. (setq s (replace-match "" nil nil s)))
  12071. (setq n (string-to-number s))
  12072. (cond
  12073. ((and (string-match "0" s)
  12074. (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
  12075. ((string-match "\\`[ \t]+\\'" s) nil)
  12076. ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
  12077. (let ((h (string-to-number (or (match-string 1 s) "0")))
  12078. (m (string-to-number (or (match-string 2 s) "0")))
  12079. (s (string-to-number (or (match-string 4 s) "0"))))
  12080. (if (boundp 'timecnt) (setq timecnt (1+ timecnt)))
  12081. (* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
  12082. ((equal n 0) nil)
  12083. (t n))))
  12084. (defun org-table-get-vertical-vector (desc &optional tbeg col)
  12085. "Get a calc vector from a column, accorting to descriptor DESC.
  12086. Optional arguments TBEG and COL can give the beginning of the table and
  12087. the current column, to avoid unnecessary parsing."
  12088. (save-excursion
  12089. (or tbeg (setq tbeg (org-table-begin)))
  12090. (or col (setq col (org-table-current-column)))
  12091. (let (beg end nn n n1 n2 l (thisline (org-current-line)) hline-list)
  12092. (cond
  12093. ((string-match "\\(I+\\)\\(-\\(I+\\)\\)?" desc)
  12094. (setq n1 (- (match-end 1) (match-beginning 1)))
  12095. (if (match-beginning 3)
  12096. (setq n2 (- (match-end 2) (match-beginning 3))))
  12097. (setq n (if n2 (max n1 n2) n1))
  12098. (setq n1 (if n2 (min n1 n2)))
  12099. (setq nn n)
  12100. (while (and (> nn 0)
  12101. (re-search-backward org-table-hline-regexp tbeg t))
  12102. (push (org-current-line) hline-list)
  12103. (setq nn (1- nn)))
  12104. (setq hline-list (nreverse hline-list))
  12105. (goto-line (nth (1- n) hline-list))
  12106. (when (re-search-forward org-table-dataline-regexp)
  12107. (org-table-goto-column col)
  12108. (setq beg (point)))
  12109. (goto-line (if n1 (nth (1- n1) hline-list) thisline))
  12110. (when (re-search-backward org-table-dataline-regexp)
  12111. (org-table-goto-column col)
  12112. (setq end (point)))
  12113. (setq l (apply 'append (org-table-copy-region beg end)))
  12114. (concat "[" (mapconcat (lambda (x) (setq x (org-trim x))
  12115. (if (equal x "") "0" x))
  12116. l ",") "]"))
  12117. ((string-match "\\([0-9]+\\)-\\([0-9]+\\)" desc)
  12118. (setq n1 (string-to-number (match-string 1 desc))
  12119. n2 (string-to-number (match-string 2 desc)))
  12120. (beginning-of-line 1)
  12121. (save-excursion
  12122. (when (re-search-backward org-table-dataline-regexp tbeg t n1)
  12123. (org-table-goto-column col)
  12124. (setq beg (point))))
  12125. (when (re-search-backward org-table-dataline-regexp tbeg t n2)
  12126. (org-table-goto-column col)
  12127. (setq end (point)))
  12128. (setq l (apply 'append (org-table-copy-region beg end)))
  12129. (concat "[" (mapconcat
  12130. (lambda (x) (setq x (org-trim x))
  12131. (if (equal x "") "0" x))
  12132. l ",") "]"))
  12133. ((string-match "\\([0-9]+\\)" desc)
  12134. (beginning-of-line 1)
  12135. (when (re-search-backward org-table-dataline-regexp tbeg t
  12136. (string-to-number (match-string 0 desc)))
  12137. (org-table-goto-column col)
  12138. (org-trim (org-table-get-field))))))))
  12139. (defvar org-table-formula-history nil)
  12140. (defvar org-table-column-names nil
  12141. "Alist with column names, derived from the `!' line.")
  12142. (defvar org-table-column-name-regexp nil
  12143. "Regular expression matching the current column names.")
  12144. (defvar org-table-local-parameters nil
  12145. "Alist with parameter names, derived from the `$' line.")
  12146. (defvar org-table-named-field-locations nil
  12147. "Alist with locations of named fields.")
  12148. (defun org-table-get-formula (&optional equation named)
  12149. "Read a formula from the minibuffer, offer stored formula as default."
  12150. (let* ((name (car (rassoc (list (org-current-line)
  12151. (org-table-current-column))
  12152. org-table-named-field-locations)))
  12153. (scol (if named
  12154. (if name name
  12155. (error "Not in a named field"))
  12156. (int-to-string (org-table-current-column))))
  12157. (dummy (and name (not named)
  12158. (not (y-or-n-p "Replace named-field formula with column equation? " ))
  12159. (error "Abort")))
  12160. (org-table-may-need-update nil)
  12161. (stored-list (org-table-get-stored-formulas))
  12162. (stored (cdr (assoc scol stored-list)))
  12163. (eq (cond
  12164. ((and stored equation (string-match "^ *=? *$" equation))
  12165. stored)
  12166. ((stringp equation)
  12167. equation)
  12168. (t (read-string
  12169. (format "%s formula $%s=" (if named "Field" "Column") scol)
  12170. (or stored "") 'org-table-formula-history
  12171. ;stored
  12172. ))))
  12173. mustsave)
  12174. (when (not (string-match "\\S-" eq))
  12175. ;; remove formula
  12176. (setq stored-list (delq (assoc scol stored-list) stored-list))
  12177. (org-table-store-formulas stored-list)
  12178. (error "Formula removed"))
  12179. (if (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
  12180. (if (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
  12181. (if (and name (not named))
  12182. ;; We set the column equation, delete the named one.
  12183. (setq stored-list (delq (assoc name stored-list) stored-list)
  12184. mustsave t))
  12185. (if stored
  12186. (setcdr (assoc scol stored-list) eq)
  12187. (setq stored-list (cons (cons scol eq) stored-list)))
  12188. (if (or mustsave (not (equal stored eq)))
  12189. (org-table-store-formulas stored-list))
  12190. eq))
  12191. (defun org-table-store-formulas (alist)
  12192. "Store the list of formulas below the current table."
  12193. (setq alist (sort alist (lambda (a b) (string< (car a) (car b)))))
  12194. (save-excursion
  12195. (goto-char (org-table-end))
  12196. (if (looking-at "\\([ \t]*\n\\)*#\\+TBLFM:.*\n?")
  12197. (delete-region (point) (match-end 0)))
  12198. (insert "#+TBLFM: "
  12199. (mapconcat (lambda (x)
  12200. (concat "$" (car x) "=" (cdr x)))
  12201. alist "::")
  12202. "\n")))
  12203. (defun org-table-get-stored-formulas ()
  12204. "Return an alist with the stored formulas directly after current table."
  12205. (interactive)
  12206. (let (scol eq eq-alist strings string seen)
  12207. (save-excursion
  12208. (goto-char (org-table-end))
  12209. (when (looking-at "\\([ \t]*\n\\)*#\\+TBLFM: *\\(.*\\)")
  12210. (setq strings (org-split-string (match-string 2) " *:: *"))
  12211. (while (setq string (pop strings))
  12212. (when (string-match "\\$\\([a-zA-Z0-9]+\\) *= *\\(.*[^ \t]\\)" string)
  12213. (setq scol (match-string 1 string)
  12214. eq (match-string 2 string)
  12215. eq-alist (cons (cons scol eq) eq-alist))
  12216. (if (member scol seen)
  12217. (error "Double definition `$%s=' in TBLFM line, please fix by hand" scol)
  12218. (push scol seen))))))
  12219. (nreverse eq-alist)))
  12220. (defun org-table-modify-formulas (action &rest columns)
  12221. "Modify the formulas stored below the current table.
  12222. ACTION can be `remove', `insert', `swap'. For `swap', two column numbers are
  12223. expected, for the other actions only a single column number is needed."
  12224. (let ((list (org-table-get-stored-formulas))
  12225. (nmax (length (org-split-string
  12226. (buffer-substring (point-at-bol) (point-at-eol))
  12227. "|")))
  12228. col col1 col2 scol si sc1 sc2)
  12229. (cond
  12230. ((null list)) ; No action needed if there are no stored formulas
  12231. ((eq action 'remove)
  12232. (setq col (car columns)
  12233. scol (int-to-string col))
  12234. (org-table-replace-in-formulas list scol "INVALID")
  12235. (if (assoc scol list) (setq list (delq (assoc scol list) list)))
  12236. (loop for i from (1+ col) upto nmax by 1 do
  12237. (setq si (int-to-string i))
  12238. (org-table-replace-in-formulas list si (int-to-string (1- i)))
  12239. (if (assoc si list) (setcar (assoc si list)
  12240. (int-to-string (1- i))))))
  12241. ((eq action 'insert)
  12242. (setq col (car columns))
  12243. (loop for i from nmax downto col by 1 do
  12244. (setq si (int-to-string i))
  12245. (org-table-replace-in-formulas list si (int-to-string (1+ i)))
  12246. (if (assoc si list) (setcar (assoc si list)
  12247. (int-to-string (1+ i))))))
  12248. ((eq action 'swap)
  12249. (setq col1 (car columns) col2 (nth 1 columns)
  12250. sc1 (int-to-string col1) sc2 (int-to-string col2))
  12251. ;; Hopefully, ZqZtZ will never be a name in a table
  12252. (org-table-replace-in-formulas list sc1 "ZqZtZ")
  12253. (org-table-replace-in-formulas list sc2 sc1)
  12254. (org-table-replace-in-formulas list "ZqZtZ" sc2)
  12255. (if (assoc sc1 list) (setcar (assoc sc1 list) "ZqZtZ"))
  12256. (if (assoc sc2 list) (setcar (assoc sc2 list) sc1))
  12257. (if (assoc "ZqZtZ" list) (setcar (assoc "ZqZtZ" list) sc2)))
  12258. (t (error "Invalid action in `org-table-modify-formulas'")))
  12259. (if list (org-table-store-formulas list))))
  12260. (defun org-table-replace-in-formulas (list s1 s2)
  12261. (let (elt re s)
  12262. (setq s1 (concat "$" (if (integerp s1) (int-to-string s1) s1))
  12263. s2 (concat "$" (if (integerp s2) (int-to-string s2) s2))
  12264. re (concat (regexp-quote s1) "\\>"))
  12265. (while (setq elt (pop list))
  12266. (setq s (cdr elt))
  12267. (while (string-match re s)
  12268. (setq s (replace-match s2 t t s)))
  12269. (setcdr elt s))))
  12270. (defun org-table-get-specials ()
  12271. "Get the column names and local parameters for this table."
  12272. (save-excursion
  12273. (let ((beg (org-table-begin)) (end (org-table-end))
  12274. names name fields fields1 field cnt c v line col)
  12275. (setq org-table-column-names nil
  12276. org-table-local-parameters nil
  12277. org-table-named-field-locations nil)
  12278. (goto-char beg)
  12279. (when (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t)
  12280. (setq names (org-split-string (match-string 1) " *| *")
  12281. cnt 1)
  12282. (while (setq name (pop names))
  12283. (setq cnt (1+ cnt))
  12284. (if (string-match "^[a-zA-Z][a-zA-Z0-9]*$" name)
  12285. (push (cons name (int-to-string cnt)) org-table-column-names))))
  12286. (setq org-table-column-names (nreverse org-table-column-names))
  12287. (setq org-table-column-name-regexp
  12288. (concat "\\$\\(" (mapconcat 'car org-table-column-names "\\|") "\\)\\>"))
  12289. (goto-char beg)
  12290. (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t)
  12291. (setq fields (org-split-string (match-string 1) " *| *"))
  12292. (while (setq field (pop fields))
  12293. (if (string-match "^\\([a-zA-Z][a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field)
  12294. (push (cons (match-string 1 field) (match-string 2 field))
  12295. org-table-local-parameters))))
  12296. (goto-char beg)
  12297. (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t)
  12298. (setq c (match-string 1)
  12299. fields (org-split-string (match-string 2) " *| *"))
  12300. (save-excursion
  12301. (beginning-of-line (if (equal c "_") 2 0))
  12302. (setq line (org-current-line) col 1)
  12303. (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
  12304. (setq fields1 (org-split-string (match-string 1) " *| *"))))
  12305. (while (and fields1 (setq field (pop fields)))
  12306. (setq v (pop fields1) col (1+ col))
  12307. (when (and (stringp field) (stringp v)
  12308. (string-match "^[a-zA-Z][a-zA-Z0-9]*$" field))
  12309. (push (cons field v) org-table-local-parameters)
  12310. (push (list field line col) org-table-named-field-locations)))))))
  12311. (defun org-this-word ()
  12312. ;; Get the current word
  12313. (save-excursion
  12314. (let ((beg (progn (skip-chars-backward "^ \t\n") (point)))
  12315. (end (progn (skip-chars-forward "^ \t\n") (point))))
  12316. (buffer-substring-no-properties beg end))))
  12317. (defun org-table-maybe-eval-formula ()
  12318. "Check if the current field starts with \"=\" or \":=\".
  12319. If yes, store the formula and apply it."
  12320. ;; We already know we are in a table. Get field will only return a formula
  12321. ;; when appropriate. It might return a separator line, but no problem.
  12322. (when org-table-formula-evaluate-inline
  12323. (let* ((field (org-trim (or (org-table-get-field) "")))
  12324. named eq)
  12325. (when (string-match "^:?=\\(.*\\)" field)
  12326. (setq named (equal (string-to-char field) ?:)
  12327. eq (match-string 1 field))
  12328. (if (fboundp 'calc-eval)
  12329. (org-table-eval-formula (if named '(4) nil) eq))))))
  12330. (defvar org-recalc-commands nil
  12331. "List of commands triggering the recalculation of a line.
  12332. Will be filled automatically during use.")
  12333. (defvar org-recalc-marks
  12334. '((" " . "Unmarked: no special line, no automatic recalculation")
  12335. ("#" . "Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
  12336. ("*" . "Recalculate only when entire table is recalculated with `C-u C-c *'")
  12337. ("!" . "Column name definition line. Reference in formula as $name.")
  12338. ("$" . "Parameter definition line name=value. Reference in formula as $name.")
  12339. ("_" . "Names for values in row below this one.")
  12340. ("^" . "Names for values in row above this one.")))
  12341. (defun org-table-rotate-recalc-marks (&optional newchar)
  12342. "Rotate the recalculation mark in the first column.
  12343. If in any row, the first field is not consistent with a mark,
  12344. insert a new column for the markers.
  12345. When there is an active region, change all the lines in the region,
  12346. after prompting for the marking character.
  12347. After each change, a message will be displayed indicating the meaning
  12348. of the new mark."
  12349. (interactive)
  12350. (unless (org-at-table-p) (error "Not at a table"))
  12351. (let* ((marks (append (mapcar 'car org-recalc-marks) '(" ")))
  12352. (beg (org-table-begin))
  12353. (end (org-table-end))
  12354. (l (org-current-line))
  12355. (l1 (if (org-region-active-p) (org-current-line (region-beginning))))
  12356. (l2 (if (org-region-active-p) (org-current-line (region-end))))
  12357. (have-col
  12358. (save-excursion
  12359. (goto-char beg)
  12360. (not (re-search-forward "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" end t))))
  12361. (col (org-table-current-column))
  12362. (forcenew (car (assoc newchar org-recalc-marks)))
  12363. epos new)
  12364. (when l1
  12365. (message "Change region to what mark? Type # * ! $ or SPC: ")
  12366. (setq newchar (char-to-string (read-char-exclusive))
  12367. forcenew (car (assoc newchar org-recalc-marks))))
  12368. (if (and newchar (not forcenew))
  12369. (error "Invalid NEWCHAR `%s' in `org-table-rotate-recalc-marks'"
  12370. newchar))
  12371. (if l1 (goto-line l1))
  12372. (save-excursion
  12373. (beginning-of-line 1)
  12374. (unless (looking-at org-table-dataline-regexp)
  12375. (error "Not at a table data line")))
  12376. (unless have-col
  12377. (org-table-goto-column 1)
  12378. (org-table-insert-column)
  12379. (org-table-goto-column (1+ col)))
  12380. (setq epos (point-at-eol))
  12381. (save-excursion
  12382. (beginning-of-line 1)
  12383. (org-table-get-field
  12384. 1 (if (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")
  12385. (concat " "
  12386. (setq new (or forcenew
  12387. (cadr (member (match-string 1) marks))))
  12388. " ")
  12389. " # ")))
  12390. (if (and l1 l2)
  12391. (progn
  12392. (goto-line l1)
  12393. (while (progn (beginning-of-line 2) (not (= (org-current-line) l2)))
  12394. (and (looking-at org-table-dataline-regexp)
  12395. (org-table-get-field 1 (concat " " new " "))))
  12396. (goto-line l1)))
  12397. (if (not (= epos (point-at-eol))) (org-table-align))
  12398. (goto-line l)
  12399. (and (interactive-p) (message (cdr (assoc new org-recalc-marks))))))
  12400. (defun org-table-maybe-recalculate-line ()
  12401. "Recompute the current line if marked for it, and if we haven't just done it."
  12402. (interactive)
  12403. (and org-table-allow-automatic-line-recalculation
  12404. (not (and (memq last-command org-recalc-commands)
  12405. (equal org-last-recalc-line (org-current-line))))
  12406. (save-excursion (beginning-of-line 1)
  12407. (looking-at org-table-auto-recalculate-regexp))
  12408. (fboundp 'calc-eval)
  12409. (org-table-recalculate) t))
  12410. (defvar org-table-formula-debug nil
  12411. "Non-nil means, debug table formulas.
  12412. When nil, simply write \"#ERROR\" in corrupted fields.")
  12413. (defvar modes)
  12414. (defsubst org-set-calc-mode (var &optional value)
  12415. (if (stringp var)
  12416. (setq var (assoc var '(("D" calc-angle-mode deg)
  12417. ("R" calc-angle-mode rad)
  12418. ("F" calc-prefer-frac t)
  12419. ("S" calc-symbolic-mode t)))
  12420. value (nth 2 var) var (nth 1 var)))
  12421. (if (memq var modes)
  12422. (setcar (cdr (memq var modes)) value)
  12423. (cons var (cons value modes)))
  12424. modes)
  12425. (defun org-table-eval-formula (&optional arg equation
  12426. suppress-align suppress-const
  12427. suppress-store)
  12428. "Replace the table field value at the cursor by the result of a calculation.
  12429. This function makes use of Dave Gillespie's Calc package, in my view the
  12430. most exciting program ever written for GNU Emacs. So you need to have Calc
  12431. installed in order to use this function.
  12432. In a table, this command replaces the value in the current field with the
  12433. result of a formula. It also installs the formula as the \"current\" column
  12434. formula, by storing it in a special line below the table. When called
  12435. with a `C-u' prefix, the current field must ba a named field, and the
  12436. formula is installed as valid in only this specific field.
  12437. When called, the command first prompts for a formula, which is read in
  12438. the minibuffer. Previously entered formulas are available through the
  12439. history list, and the last used formula is offered as a default.
  12440. These stored formulas are adapted correctly when moving, inserting, or
  12441. deleting columns with the corresponding commands.
  12442. The formula can be any algebraic expression understood by the Calc package.
  12443. For details, see the Org-mode manual.
  12444. This function can also be called from Lisp programs and offers
  12445. additional arguments: EQUATION can be the formula to apply. If this
  12446. argument is given, the user will not be prompted. SUPPRESS-ALIGN is
  12447. used to speed-up recursive calls by by-passing unnecessary aligns.
  12448. SUPPRESS-CONST suppresses the interpretation of constants in the
  12449. formula, assuming that this has been done already outside the function.
  12450. SUPPRESS-STORE means the formula should not be stored, either because
  12451. it is already stored, or because it is a modified equation that should
  12452. not overwrite the stored one."
  12453. (interactive "P")
  12454. (require 'calc)
  12455. (org-table-check-inside-data-field)
  12456. (org-table-get-specials)
  12457. (let* (fields
  12458. (ndown (if (integerp arg) arg 1))
  12459. (org-table-automatic-realign nil)
  12460. (case-fold-search nil)
  12461. (down (> ndown 1))
  12462. (formula (if (and equation suppress-store)
  12463. equation
  12464. (org-table-get-formula equation (equal arg '(4)))))
  12465. (n0 (org-table-current-column))
  12466. (modes (copy-sequence org-calc-default-modes))
  12467. n form fmt x ev orig c lispp)
  12468. ;; Parse the format string. Since we have a lot of modes, this is
  12469. ;; a lot of work. However, I think calc still uses most of the time.
  12470. (if (string-match ";" formula)
  12471. (let ((tmp (org-split-string formula ";")))
  12472. (setq formula (car tmp)
  12473. fmt (concat (cdr (assoc "%" org-table-local-parameters))
  12474. (nth 1 tmp)))
  12475. (while (string-match "\\([pnfse]\\)\\(-?[0-9]+\\)" fmt)
  12476. (setq c (string-to-char (match-string 1 fmt))
  12477. n (string-to-number (match-string 2 fmt)))
  12478. (if (= c ?p)
  12479. (setq modes (org-set-calc-mode 'calc-internal-prec n))
  12480. (setq modes (org-set-calc-mode
  12481. 'calc-float-format
  12482. (list (cdr (assoc c '((?n . float) (?f . fix)
  12483. (?s . sci) (?e . eng))))
  12484. n))))
  12485. (setq fmt (replace-match "" t t fmt)))
  12486. (while (string-match "[DRFS]" fmt)
  12487. (setq modes (org-set-calc-mode (match-string 0 fmt)))
  12488. (setq fmt (replace-match "" t t fmt)))
  12489. (unless (string-match "\\S-" fmt)
  12490. (setq fmt nil))))
  12491. (if (and (not suppress-const) org-table-formula-use-constants)
  12492. (setq formula (org-table-formula-substitute-names formula)))
  12493. (setq orig (or (get-text-property 1 :orig-formula formula) "?"))
  12494. (while (> ndown 0)
  12495. (setq fields (org-split-string
  12496. (buffer-substring
  12497. (point-at-bol) (point-at-eol)) " *| *"))
  12498. (if org-table-formula-numbers-only
  12499. (setq fields (mapcar
  12500. (lambda (x) (number-to-string (string-to-number x)))
  12501. fields)))
  12502. (setq ndown (1- ndown))
  12503. (setq form (copy-sequence formula)
  12504. lispp (equal (substring form 0 2) "'("))
  12505. ;; Insert the references to fields in same row
  12506. (while (string-match "\\$\\([0-9]+\\)?" form)
  12507. (setq n (if (match-beginning 1)
  12508. (string-to-number (match-string 1 form))
  12509. n0)
  12510. x (nth (1- n) fields))
  12511. (unless x (error "Invalid field specifier \"%s\""
  12512. (match-string 0 form)))
  12513. (if (equal x "") (setq x "0"))
  12514. (setq form (replace-match
  12515. (if lispp x (concat "(" x ")"))
  12516. t t form)))
  12517. ;; Insert ranges in current column
  12518. (while (string-match "\\&[-I0-9]+" form)
  12519. (setq form (replace-match
  12520. (save-match-data
  12521. (org-table-get-vertical-vector (match-string 0 form)
  12522. nil n0))
  12523. t t form)))
  12524. (if lispp
  12525. (setq ev (eval (eval (read form)))
  12526. ev (if (numberp ev) (number-to-string ev) ev))
  12527. (setq ev (calc-eval (cons form modes)
  12528. (if org-table-formula-numbers-only 'num))))
  12529. (when org-table-formula-debug
  12530. (with-output-to-temp-buffer "*Help*"
  12531. (princ (format "Substitution history of formula
  12532. Orig: %s
  12533. $xyz-> %s
  12534. $1-> %s\n" orig formula form))
  12535. (if (listp ev)
  12536. (princ (format " %s^\nError: %s"
  12537. (make-string (car ev) ?\-) (nth 1 ev)))
  12538. (princ (format "Result: %s\nFormat: %s\nFinal: %s"
  12539. ev (or fmt "NONE")
  12540. (if fmt (format fmt (string-to-number ev)) ev)))))
  12541. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  12542. (unless (and (interactive-p) (not ndown))
  12543. (unless (let (inhibit-redisplay)
  12544. (y-or-n-p "Debugging Formula. Continue to next? "))
  12545. (org-table-align)
  12546. (error "Abort"))
  12547. (delete-window (get-buffer-window "*Help*"))
  12548. (message "")))
  12549. (if (listp ev) (setq fmt nil ev "#ERROR"))
  12550. (org-table-justify-field-maybe
  12551. (if fmt (format fmt (string-to-number ev)) ev))
  12552. (if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
  12553. (call-interactively 'org-return)
  12554. (setq ndown 0)))
  12555. (and down (org-table-maybe-recalculate-line))
  12556. (or suppress-align (and org-table-may-need-update
  12557. (org-table-align)))))
  12558. (defun org-table-recalculate (&optional all noalign)
  12559. "Recalculate the current table line by applying all stored formulas.
  12560. With prefix arg ALL, do this for all lines in the table."
  12561. (interactive "P")
  12562. (or (memq this-command org-recalc-commands)
  12563. (setq org-recalc-commands (cons this-command org-recalc-commands)))
  12564. (unless (org-at-table-p) (error "Not at a table"))
  12565. (org-table-get-specials)
  12566. (let* ((eqlist (sort (org-table-get-stored-formulas)
  12567. (lambda (a b) (string< (car a) (car b)))))
  12568. (inhibit-redisplay t)
  12569. (line-re org-table-dataline-regexp)
  12570. (thisline (org-current-line))
  12571. (thiscol (org-table-current-column))
  12572. beg end entry eqlnum eqlname eql (cnt 0) eq a name)
  12573. ;; Insert constants in all formulas
  12574. (setq eqlist
  12575. (mapcar (lambda (x)
  12576. (setcdr x (org-table-formula-substitute-names (cdr x)))
  12577. x)
  12578. eqlist))
  12579. ;; Split the equation list
  12580. (while (setq eq (pop eqlist))
  12581. (if (<= (string-to-char (car eq)) ?9)
  12582. (push eq eqlnum)
  12583. (push eq eqlname)))
  12584. (setq eqlnum (nreverse eqlnum) eqlname (nreverse eqlname))
  12585. (if all
  12586. (progn
  12587. (setq end (move-marker (make-marker) (1+ (org-table-end))))
  12588. (goto-char (setq beg (org-table-begin)))
  12589. (if (re-search-forward org-table-calculate-mark-regexp end t)
  12590. ;; This is a table with marked lines, only compute selected lines
  12591. (setq line-re org-table-recalculate-regexp)
  12592. ;; Move forward to the first non-header line
  12593. (if (and (re-search-forward org-table-dataline-regexp end t)
  12594. (re-search-forward org-table-hline-regexp end t)
  12595. (re-search-forward org-table-dataline-regexp end t))
  12596. (setq beg (match-beginning 0))
  12597. nil))) ;; just leave beg where it is
  12598. (setq beg (point-at-bol)
  12599. end (move-marker (make-marker) (1+ (point-at-eol)))))
  12600. (goto-char beg)
  12601. (and all (message "Re-applying formulas to full table..."))
  12602. (while (re-search-forward line-re end t)
  12603. (unless (string-match "^ *[_^!$] *$" (org-table-get-field 1))
  12604. ;; Unprotected line, recalculate
  12605. (and all (message "Re-applying formulas to full table...(line %d)"
  12606. (setq cnt (1+ cnt))))
  12607. (setq org-last-recalc-line (org-current-line))
  12608. (setq eql eqlnum)
  12609. (while (setq entry (pop eql))
  12610. (goto-line org-last-recalc-line)
  12611. (org-table-goto-column (string-to-number (car entry)) nil 'force)
  12612. (org-table-eval-formula nil (cdr entry) 'noalign 'nocst 'nostore))))
  12613. (goto-line thisline)
  12614. (org-table-goto-column thiscol)
  12615. (or noalign (and org-table-may-need-update (org-table-align))
  12616. (and all (message "Re-applying formulas to %d lines...done" cnt)))
  12617. ;; Now do the names fields
  12618. (while (setq eq (pop eqlname))
  12619. (setq name (car eq)
  12620. a (assoc name org-table-named-field-locations))
  12621. (when a
  12622. (message "Re-applying formula to named field: %s" name)
  12623. (goto-line (nth 1 a))
  12624. (org-table-goto-column (nth 2 a))
  12625. (org-table-eval-formula nil (cdr eq) 'noalign 'nocst 'nostore)))
  12626. ;; back to initial position
  12627. (goto-line thisline)
  12628. (org-table-goto-column thiscol)
  12629. (or noalign (and org-table-may-need-update (org-table-align))
  12630. (and all (message "Re-applying formulas...done")))))
  12631. (defun org-table-formula-substitute-names (f)
  12632. "Replace $const with values in string F."
  12633. (let ((start 0) a n1 n2 nn1 nn2 s (f1 f))
  12634. ;; First, check for column names
  12635. (while (setq start (string-match org-table-column-name-regexp f start))
  12636. (setq start (1+ start))
  12637. (setq a (assoc (match-string 1 f) org-table-column-names))
  12638. (setq f (replace-match (concat "$" (cdr a)) t t f)))
  12639. ;; Expand ranges to vectors
  12640. (while (string-match "\\$\\([0-9]+\\)\\.\\.\\.?\\$\\([0-9]+\\)" f)
  12641. (setq n1 (string-to-number (match-string 1 f))
  12642. n2 (string-to-number (match-string 2 f))
  12643. nn1 (1+ (min n1 n2)) nn2 (max n1 n2)
  12644. s (concat "[($" (number-to-string (1- nn1)) ")"))
  12645. (loop for i from nn1 upto nn2 do
  12646. (setq s (concat s ",($" (int-to-string i) ")")))
  12647. (setq s (concat s "]"))
  12648. (if (< n2 n1) (setq s (concat "rev(" s ")")))
  12649. (setq f (replace-match s t t f)))
  12650. ;; Parameters and constants
  12651. (setq start 0)
  12652. (while (setq start (string-match "\\$\\([a-zA-Z][a-zA-Z0-9]*\\)" f start))
  12653. (setq start (1+ start))
  12654. (if (setq a (save-match-data
  12655. (org-table-get-constant (match-string 1 f))))
  12656. (setq f (replace-match (concat "(" a ")") t t f))))
  12657. (if org-table-formula-debug
  12658. (put-text-property 0 (length f) :orig-formula f1 f))
  12659. f))
  12660. (defun org-table-get-constant (const)
  12661. "Find the value for a parameter or constant in a formula.
  12662. Parameters get priority."
  12663. (or (cdr (assoc const org-table-local-parameters))
  12664. (cdr (assoc const org-table-formula-constants))
  12665. (and (fboundp 'constants-get) (constants-get const))
  12666. "#UNDEFINED_NAME"))
  12667. (defvar org-edit-formulas-map (make-sparse-keymap))
  12668. (define-key org-edit-formulas-map "\C-c\C-c" 'org-finish-edit-formulas)
  12669. (define-key org-edit-formulas-map "\C-c\C-q" 'org-abort-edit-formulas)
  12670. (define-key org-edit-formulas-map "\C-c?" 'org-show-variable)
  12671. (defvar org-pos)
  12672. (defun org-table-edit-formulas ()
  12673. "Edit the formulas of the current table in a separate buffer."
  12674. (interactive)
  12675. (unless (org-at-table-p)
  12676. (error "Not at a table"))
  12677. (org-table-get-specials)
  12678. (let ((eql (org-table-get-stored-formulas))
  12679. (pos (move-marker (make-marker) (point)))
  12680. (wc (current-window-configuration))
  12681. entry loc s)
  12682. (switch-to-buffer-other-window "*Edit Formulas*")
  12683. (erase-buffer)
  12684. (fundamental-mode)
  12685. (org-set-local 'org-pos pos)
  12686. (org-set-local 'org-window-configuration wc)
  12687. (use-local-map org-edit-formulas-map)
  12688. (setq s "# Edit formulas and finish with `C-c C-c'.
  12689. # Use `C-u C-c C-c' to also appy them immediately to the entire table.
  12690. # Use `C-c ?' to get information about $name at point.
  12691. # To cancel editing, press `C-c C-q'.\n")
  12692. (put-text-property 0 (length s) 'face 'font-lock-comment-face s)
  12693. (insert s)
  12694. (while (setq entry (pop eql))
  12695. (when (setq loc (assoc (car entry) org-table-named-field-locations))
  12696. (setq s (format "# Named formula, referring to column %d in line %d\n"
  12697. (nth 2 loc) (nth 1 loc)))
  12698. (put-text-property 0 (length s) 'face 'font-lock-comment-face s)
  12699. (insert s))
  12700. (setq s (concat "$" (car entry) "=" (cdr entry) "\n"))
  12701. (remove-text-properties 0 (length s) '(face nil) s)
  12702. (insert s))
  12703. (goto-char (point-min))
  12704. (message "Edit formulas and finish with `C-c C-c'.")))
  12705. (defun org-show-variable ()
  12706. "Show the location/value of the $ expression at point."
  12707. (interactive)
  12708. (let (var (pos org-pos) (win (selected-window)) e)
  12709. (save-excursion
  12710. (or (looking-at "\\$") (skip-chars-backward "$a-zA-Z0-9"))
  12711. (if (looking-at "\\$\\([a-zA-Z0-9]+\\)")
  12712. (setq var (match-string 1))
  12713. (error "No variable at point")))
  12714. (cond
  12715. ((setq e (assoc var org-table-named-field-locations))
  12716. (switch-to-buffer-other-window (marker-buffer pos))
  12717. (goto-line (nth 1 e))
  12718. (org-table-goto-column (nth 2 e))
  12719. (select-window win)
  12720. (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
  12721. ((setq e (assoc var org-table-column-names))
  12722. (switch-to-buffer-other-window (marker-buffer pos))
  12723. (goto-char pos)
  12724. (goto-char (org-table-begin))
  12725. (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
  12726. (org-table-end) t)
  12727. (progn
  12728. (goto-char (match-beginning 1))
  12729. (message "Named column (column %s)" (cdr e)))
  12730. (error "Column name not found"))
  12731. (select-window win))
  12732. ((string-match "^[0-9]$" var)
  12733. ;; column number
  12734. (switch-to-buffer-other-window (marker-buffer pos))
  12735. (goto-char pos)
  12736. (goto-char (org-table-begin))
  12737. (recenter 1)
  12738. (if (re-search-forward org-table-dataline-regexp
  12739. (org-table-end) t)
  12740. (progn
  12741. (goto-char (match-beginning 0))
  12742. (org-table-goto-column (string-to-number var))
  12743. (message "Column %s" var))
  12744. (error "Column name not found"))
  12745. (select-window win))
  12746. ((setq e (assoc var org-table-local-parameters))
  12747. (switch-to-buffer-other-window (marker-buffer pos))
  12748. (goto-char pos)
  12749. (goto-char (org-table-begin))
  12750. (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
  12751. (progn
  12752. (goto-char (match-beginning 1))
  12753. (message "Local parameter."))
  12754. (error "Parameter not found"))
  12755. (select-window win))
  12756. (t
  12757. (cond
  12758. ((setq e (assoc var org-table-formula-constants))
  12759. (message "Constant: $%s=%s in `org-table-formula-constants'." var (cdr e)))
  12760. ((setq e (and (fboundp 'constants-get) (constants-get var)))
  12761. (message "Constant: $%s=%s, retrieved from `constants.el'." var e))
  12762. (t (error "Undefined name $%s" var)))))))
  12763. (defun org-finish-edit-formulas (&optional arg)
  12764. "Parse the buffer for formula definitions and install them.
  12765. With prefix ARG, apply the new formulas to the table."
  12766. (interactive "P")
  12767. (let ((pos org-pos) eql)
  12768. (goto-char (point-min))
  12769. (while (re-search-forward "^\\$\\([a-zA-Z0-9]+\\) *= *\\(.*?\\) *$" nil t)
  12770. (push (cons (match-string 1) (match-string 2)) eql))
  12771. (set-window-configuration org-window-configuration)
  12772. (select-window (get-buffer-window (marker-buffer pos)))
  12773. (goto-char pos)
  12774. (unless (org-at-table-p)
  12775. (error "Lost table position - cannot install formulae"))
  12776. (org-table-store-formulas eql)
  12777. (move-marker pos nil)
  12778. (kill-buffer "*Edit Formulas*")
  12779. (if arg
  12780. (org-table-recalculate 'all)
  12781. (message "New formulas installed - press C-u C-c C-c to apply."))))
  12782. (defun org-abort-edit-formulas ()
  12783. "Abort editing formulas, without installing the changes."
  12784. (interactive)
  12785. (let ((pos org-pos))
  12786. (set-window-configuration org-window-configuration)
  12787. (select-window (get-buffer-window (marker-buffer pos)))
  12788. (goto-char pos)
  12789. (message "Formula editing aborted without installing changes")))
  12790. ;;; The orgtbl minor mode
  12791. ;; Define a minor mode which can be used in other modes in order to
  12792. ;; integrate the org-mode table editor.
  12793. ;; This is really a hack, because the org-mode table editor uses several
  12794. ;; keys which normally belong to the major mode, for example the TAB and
  12795. ;; RET keys. Here is how it works: The minor mode defines all the keys
  12796. ;; necessary to operate the table editor, but wraps the commands into a
  12797. ;; function which tests if the cursor is currently inside a table. If that
  12798. ;; is the case, the table editor command is executed. However, when any of
  12799. ;; those keys is used outside a table, the function uses `key-binding' to
  12800. ;; look up if the key has an associated command in another currently active
  12801. ;; keymap (minor modes, major mode, global), and executes that command.
  12802. ;; There might be problems if any of the keys used by the table editor is
  12803. ;; otherwise used as a prefix key.
  12804. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  12805. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  12806. ;; addresses this by checking explicitly for both bindings.
  12807. ;; The optimized version (see variable `orgtbl-optimized') takes over
  12808. ;; all keys which are bound to `self-insert-command' in the *global map*.
  12809. ;; Some modes bind other commands to simple characters, for example
  12810. ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
  12811. ;; active, this binding is ignored inside tables and replaced with a
  12812. ;; modified self-insert.
  12813. (defvar orgtbl-mode nil
  12814. "Variable controlling `orgtbl-mode', a minor mode enabling the `org-mode'
  12815. table editor in arbitrary modes.")
  12816. (make-variable-buffer-local 'orgtbl-mode)
  12817. (defvar orgtbl-mode-map (make-keymap)
  12818. "Keymap for `orgtbl-mode'.")
  12819. ;;;###autoload
  12820. (defun turn-on-orgtbl ()
  12821. "Unconditionally turn on `orgtbl-mode'."
  12822. (orgtbl-mode 1))
  12823. ;;;###autoload
  12824. (defun orgtbl-mode (&optional arg)
  12825. "The `org-mode' table editor as a minor mode for use in other modes."
  12826. (interactive)
  12827. (if (org-mode-p)
  12828. ;; Exit without error, in case some hook functions calls this
  12829. ;; by accident in org-mode.
  12830. (message "Orgtbl-mode is not useful in org-mode, command ignored")
  12831. (setq orgtbl-mode
  12832. (if arg (> (prefix-numeric-value arg) 0) (not orgtbl-mode)))
  12833. (if orgtbl-mode
  12834. (progn
  12835. (and (orgtbl-setup) (defun orgtbl-setup () nil))
  12836. ;; Make sure we are first in minor-mode-map-alist
  12837. (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
  12838. (and c (setq minor-mode-map-alist
  12839. (cons c (delq c minor-mode-map-alist)))))
  12840. (org-set-local (quote org-table-may-need-update) t)
  12841. (org-add-hook 'before-change-functions 'org-before-change-function
  12842. nil 'local)
  12843. (org-set-local 'org-old-auto-fill-inhibit-regexp
  12844. auto-fill-inhibit-regexp)
  12845. (org-set-local 'auto-fill-inhibit-regexp
  12846. (if auto-fill-inhibit-regexp
  12847. (concat "\\([ \t]*|\\|" auto-fill-inhibit-regexp)
  12848. "[ \t]*|"))
  12849. (org-add-to-invisibility-spec '(org-cwidth))
  12850. (easy-menu-add orgtbl-mode-menu)
  12851. (run-hooks 'orgtbl-mode-hook))
  12852. (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
  12853. (org-cleanup-narrow-column-properties)
  12854. (org-remove-from-invisibility-spec '(org-cwidth))
  12855. (remove-hook 'before-change-functions 'org-before-change-function t)
  12856. (easy-menu-remove orgtbl-mode-menu)
  12857. (force-mode-line-update 'all))))
  12858. (defun org-cleanup-narrow-column-properties ()
  12859. "Remove all properties related to narrow-column invisibility."
  12860. (let ((s 1))
  12861. (while (setq s (text-property-any s (point-max)
  12862. 'display org-narrow-column-arrow))
  12863. (remove-text-properties s (1+ s) '(display t)))
  12864. (setq s 1)
  12865. (while (setq s (text-property-any s (point-max) 'org-cwidth 1))
  12866. (remove-text-properties s (1+ s) '(org-cwidth t)))
  12867. (setq s 1)
  12868. (while (setq s (text-property-any s (point-max) 'invisible 'org-cwidth))
  12869. (remove-text-properties s (1+ s) '(invisible t)))))
  12870. ;; Install it as a minor mode.
  12871. (put 'orgtbl-mode :included t)
  12872. (put 'orgtbl-mode :menu-tag "Org Table Mode")
  12873. (add-minor-mode 'orgtbl-mode " OrgTbl" orgtbl-mode-map)
  12874. (defun orgtbl-make-binding (fun n &rest keys)
  12875. "Create a function for binding in the table minor mode.
  12876. FUN is the command to call inside a table. N is used to create a unique
  12877. command name. KEYS are keys that should be checked in for a command
  12878. to execute outside of tables."
  12879. (eval
  12880. (list 'defun
  12881. (intern (concat "orgtbl-hijacker-command-" (int-to-string n)))
  12882. '(arg)
  12883. (concat "In tables, run `" (symbol-name fun) "'.\n"
  12884. "Outside of tables, run the binding of `"
  12885. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  12886. "'.")
  12887. '(interactive "p")
  12888. (list 'if
  12889. '(org-at-table-p)
  12890. (list 'call-interactively (list 'quote fun))
  12891. (list 'let '(orgtbl-mode)
  12892. (list 'call-interactively
  12893. (append '(or)
  12894. (mapcar (lambda (k)
  12895. (list 'key-binding k))
  12896. keys)
  12897. '('orgtbl-error))))))))
  12898. (defun orgtbl-error ()
  12899. "Error when there is no default binding for a table key."
  12900. (interactive)
  12901. (error "This key is has no function outside tables"))
  12902. (defun orgtbl-setup ()
  12903. "Setup orgtbl keymaps."
  12904. (let ((nfunc 0)
  12905. (bindings
  12906. (list
  12907. '([(meta shift left)] org-table-delete-column)
  12908. '([(meta left)] org-table-move-column-left)
  12909. '([(meta right)] org-table-move-column-right)
  12910. '([(meta shift right)] org-table-insert-column)
  12911. '([(meta shift up)] org-table-kill-row)
  12912. '([(meta shift down)] org-table-insert-row)
  12913. '([(meta up)] org-table-move-row-up)
  12914. '([(meta down)] org-table-move-row-down)
  12915. '("\C-c\C-w" org-table-cut-region)
  12916. '("\C-c\M-w" org-table-copy-region)
  12917. '("\C-c\C-y" org-table-paste-rectangle)
  12918. '("\C-c-" org-table-insert-hline)
  12919. ; '([(shift tab)] org-table-previous-field)
  12920. '("\C-m" org-table-next-row)
  12921. (list (org-key 'S-return) 'org-table-copy-down)
  12922. '([(meta return)] org-table-wrap-region)
  12923. '("\C-c\C-q" org-table-wrap-region)
  12924. '("\C-c?" org-table-current-column)
  12925. '("\C-c " org-table-blank-field)
  12926. '("\C-c+" org-table-sum)
  12927. '("\C-c=" org-table-eval-formula)
  12928. '("\C-c'" org-table-edit-formulas)
  12929. '("\C-c`" org-table-edit-field)
  12930. '("\C-c*" org-table-recalculate)
  12931. '("\C-c|" org-table-create-or-convert-from-region)
  12932. '("\C-c^" org-table-sort-lines)
  12933. '([(control ?#)] org-table-rotate-recalc-marks)))
  12934. elt key fun cmd)
  12935. (while (setq elt (pop bindings))
  12936. (setq nfunc (1+ nfunc))
  12937. (setq key (car elt)
  12938. fun (nth 1 elt)
  12939. cmd (orgtbl-make-binding fun nfunc key))
  12940. (define-key orgtbl-mode-map key cmd))
  12941. ;; Special treatment needed for TAB and RET
  12942. (define-key orgtbl-mode-map [(return)]
  12943. (orgtbl-make-binding 'orgtbl-ret 100 [(return)] "\C-m"))
  12944. (define-key orgtbl-mode-map "\C-m"
  12945. (orgtbl-make-binding 'orgtbl-ret 101 "\C-m" [(return)]))
  12946. (define-key orgtbl-mode-map [(tab)]
  12947. (orgtbl-make-binding 'orgtbl-tab 102 [(tab)] "\C-i"))
  12948. (define-key orgtbl-mode-map "\C-i"
  12949. (orgtbl-make-binding 'orgtbl-tab 103 "\C-i" [(tab)])))
  12950. (define-key orgtbl-mode-map "\C-i"
  12951. (orgtbl-make-binding 'orgtbl-tab 104 [(shift tab)]))
  12952. (define-key orgtbl-mode-map "\C-c\C-c"
  12953. (orgtbl-make-binding 'org-ctrl-c-ctrl-c 105 "\C-c\C-c"))
  12954. (when orgtbl-optimized
  12955. ;; If the user wants maximum table support, we need to hijack
  12956. ;; some standard editing functions
  12957. (org-remap orgtbl-mode-map
  12958. 'self-insert-command 'orgtbl-self-insert-command
  12959. 'delete-char 'org-delete-char
  12960. 'delete-backward-char 'org-delete-backward-char)
  12961. (define-key orgtbl-mode-map "|" 'org-force-self-insert))
  12962. (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
  12963. '("OrgTbl"
  12964. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
  12965. ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
  12966. ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
  12967. ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
  12968. "--"
  12969. ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
  12970. ["Edit Field" org-table-edit-field :active (org-at-table-p) :keys "C-c ` "]
  12971. ["Copy Field from Above"
  12972. org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
  12973. "--"
  12974. ("Column"
  12975. ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
  12976. ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
  12977. ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
  12978. ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"]
  12979. "--"
  12980. ["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])
  12981. ("Row"
  12982. ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
  12983. ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
  12984. ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
  12985. ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
  12986. ["Sort lines in region" org-table-sort-lines (org-at-table-p) :keys "C-c ^"]
  12987. "--"
  12988. ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
  12989. ("Rectangle"
  12990. ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
  12991. ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
  12992. ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
  12993. ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
  12994. "--"
  12995. ["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
  12996. ["Set Named Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  12997. ["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
  12998. ["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
  12999. ["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
  13000. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
  13001. ["Sum Column/Rectangle" org-table-sum
  13002. :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
  13003. ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
  13004. ["Debug Formulas"
  13005. (setq org-table-formula-debug (not org-table-formula-debug))
  13006. :style toggle :selected org-table-formula-debug]
  13007. ))
  13008. t)
  13009. (defun orgtbl-tab (arg)
  13010. "Justification and field motion for `orgtbl-mode'."
  13011. (interactive "P")
  13012. (if arg (org-table-edit-field t)
  13013. (org-table-justify-field-maybe)
  13014. (org-table-next-field)))
  13015. (defun orgtbl-ret ()
  13016. "Justification and field motion for `orgtbl-mode'."
  13017. (interactive)
  13018. (org-table-justify-field-maybe)
  13019. (org-table-next-row))
  13020. (defun orgtbl-self-insert-command (N)
  13021. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  13022. If the cursor is in a table looking at whitespace, the whitespace is
  13023. overwritten, and the table is not marked as requiring realignment."
  13024. (interactive "p")
  13025. (if (and (org-at-table-p)
  13026. (or
  13027. (and org-table-auto-blank-field
  13028. (member last-command
  13029. '(orgtbl-hijacker-command-100
  13030. orgtbl-hijacker-command-101
  13031. orgtbl-hijacker-command-102
  13032. orgtbl-hijacker-command-103
  13033. orgtbl-hijacker-command-104
  13034. orgtbl-hijacker-command-105))
  13035. (org-table-blank-field))
  13036. t)
  13037. (eq N 1)
  13038. (looking-at "[^|\n]* +|"))
  13039. (let (org-table-may-need-update)
  13040. (goto-char (1- (match-end 0)))
  13041. (delete-backward-char 1)
  13042. (goto-char (match-beginning 0))
  13043. (self-insert-command N))
  13044. (setq org-table-may-need-update t)
  13045. (let (orgtbl-mode)
  13046. (call-interactively (key-binding (vector last-input-event))))))
  13047. (defun org-force-self-insert (N)
  13048. "Needed to enforce self-insert under remapping."
  13049. (interactive "p")
  13050. (self-insert-command N))
  13051. ;;; Exporting
  13052. (defconst org-level-max 20)
  13053. (defvar org-export-html-preamble nil
  13054. "Preamble, to be inserted just after <body>. Set by publishing functions.")
  13055. (defvar org-export-html-postamble nil
  13056. "Preamble, to be inserted just before </body>. Set by publishing functions.")
  13057. (defvar org-export-html-auto-preamble t
  13058. "Should default preamble be inserted? Set by publishing functions.")
  13059. (defvar org-export-html-auto-postamble t
  13060. "Should default postamble be inserted? Set by publishing functions.")
  13061. (defconst org-export-plist-vars
  13062. '((:language . org-export-default-language)
  13063. (:customtime . org-display-custom-times)
  13064. (:headline-levels . org-export-headline-levels)
  13065. (:section-numbers . org-export-with-section-numbers)
  13066. (:table-of-contents . org-export-with-toc)
  13067. (:archived-trees . org-export-with-archived-trees)
  13068. (:emphasize . org-export-with-emphasize)
  13069. (:sub-superscript . org-export-with-sub-superscripts)
  13070. (:TeX-macros . org-export-with-TeX-macros)
  13071. (:LaTeX-fragments . org-export-with-LaTeX-fragments)
  13072. (:fixed-width . org-export-with-fixed-width)
  13073. (:timestamps . org-export-with-timestamps)
  13074. (:tables . org-export-with-tables)
  13075. (:table-auto-headline . org-export-highlight-first-table-line)
  13076. (:style . org-export-html-style)
  13077. (:convert-org-links . org-export-html-link-org-files-as-html)
  13078. (:inline-images . org-export-html-inline-images)
  13079. (:expand-quoted-html . org-export-html-expand)
  13080. (:timestamp . org-export-html-with-timestamp)
  13081. (:publishing-directory . org-export-publishing-directory)
  13082. (:preamble . org-export-html-preamble)
  13083. (:postamble . org-export-html-postamble)
  13084. (:auto-preamble . org-export-html-auto-preamble)
  13085. (:auto-postamble . org-export-html-auto-postamble)
  13086. (:author . user-full-name)
  13087. (:email . user-mail-address)))
  13088. (defun org-default-export-plist ()
  13089. "Return the property list with default settings for the export variables."
  13090. (let ((l org-export-plist-vars) rtn e)
  13091. (while (setq e (pop l))
  13092. (setq rtn (cons (car e) (cons (symbol-value (cdr e)) rtn))))
  13093. rtn))
  13094. (defun org-infile-export-plist ()
  13095. "Return the property list with file-local settings for export."
  13096. (save-excursion
  13097. (goto-char 0)
  13098. (let ((re (org-make-options-regexp
  13099. '("TITLE" "AUTHOR" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE")))
  13100. p key val text options)
  13101. (while (re-search-forward re nil t)
  13102. (setq key (org-match-string-no-properties 1)
  13103. val (org-match-string-no-properties 2))
  13104. (cond
  13105. ((string-equal key "TITLE") (setq p (plist-put p :title val)))
  13106. ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
  13107. ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
  13108. ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
  13109. ((string-equal key "TEXT")
  13110. (setq text (if text (concat text "\n" val) val)))
  13111. ((string-equal key "OPTIONS") (setq options val))))
  13112. (setq p (plist-put p :text text))
  13113. (when options
  13114. (let ((op '(("H" . :headline-levels)
  13115. ("num" . :section-numbers)
  13116. ("toc" . :table-of-contents)
  13117. ("\\n" . :preserve-breaks)
  13118. ("@" . :expand-quoted-html)
  13119. (":" . :fixed-width)
  13120. ("|" . :tables)
  13121. ("^" . :sub-superscript)
  13122. ("*" . :emphasize)
  13123. ("TeX" . :TeX-macros)
  13124. ("LaTeX" . :LaTeX-fragments)))
  13125. o)
  13126. (while (setq o (pop op))
  13127. (if (string-match (concat (regexp-quote (car o))
  13128. ":\\([^ \t\n\r;,.]*\\)")
  13129. options)
  13130. (setq p (plist-put p (cdr o)
  13131. (car (read-from-string
  13132. (match-string 1 options)))))))))
  13133. p)))
  13134. (defun org-combine-plists (&rest plists)
  13135. "Create a single property list from all plists in PLISTS.
  13136. The process starts by copying the last list, and then setting properties
  13137. from the other lists. Settings in the first list are the most significant
  13138. ones and overrule settings in the other lists."
  13139. (let ((rtn (copy-sequence (pop plists)))
  13140. p v ls)
  13141. (while plists
  13142. (setq ls (pop plists))
  13143. (while ls
  13144. (setq p (pop ls) v (pop ls))
  13145. (setq rtn (plist-put rtn p v))))
  13146. rtn))
  13147. (defun org-export-directory (type plist)
  13148. (let* ((val (plist-get plist :publishing-directory))
  13149. (dir (if (listp val)
  13150. (or (cdr (assoc type val)) ".")
  13151. val)))
  13152. dir))
  13153. (defun org-export-find-first-heading-line (list)
  13154. "Remove all lines from LIST which are before the first headline."
  13155. (let ((orig-list list)
  13156. (re (concat "^" outline-regexp)))
  13157. (while (and list
  13158. (not (string-match re (car list))))
  13159. (pop list))
  13160. (or list orig-list)))
  13161. (defun org-skip-comments (lines)
  13162. "Skip lines starting with \"#\" and subtrees starting with COMMENT."
  13163. (let ((re1 (concat "^\\(\\*+\\)[ \t]+" org-comment-string))
  13164. (re2 "^\\(\\*+\\)[ \t\n\r]")
  13165. rtn line level)
  13166. (while (setq line (pop lines))
  13167. (cond
  13168. ((and (string-match re1 line)
  13169. (setq level (- (match-end 1) (match-beginning 1))))
  13170. ;; Beginning of a COMMENT subtree. Skip it.
  13171. (while (and (setq line (pop lines))
  13172. (or (not (string-match re2 line))
  13173. (> (- (match-end 1) (match-beginning 1)) level))))
  13174. (setq lines (cons line lines)))
  13175. ((string-match "^#" line)
  13176. ;; an ordinary comment line
  13177. )
  13178. ((and org-export-table-remove-special-lines
  13179. (string-match "^[ \t]*|" line)
  13180. (or (string-match "^[ \t]*| *[!_^] *|" line)
  13181. (and (string-match "| *<[0-9]+> *|" line)
  13182. (not (string-match "| *[^ <|]" line)))))
  13183. ;; a special table line that should be removed
  13184. )
  13185. (t (setq rtn (cons line rtn)))))
  13186. (nreverse rtn)))
  13187. (defun org-export (&optional arg)
  13188. (interactive)
  13189. (let ((help "[t] insert the export option template
  13190. \[v] limit export to visible part of outline tree
  13191. \[a] export as ASCII
  13192. \[h] export as HTML
  13193. \[b] export as HTML and browse immediately
  13194. \[x] export as XOXO
  13195. \[i] export current file as iCalendar file
  13196. \[I] export all agenda files as iCalendar files
  13197. \[c] export agenda files into combined iCalendar file
  13198. \[F] publish current file
  13199. \[P] publish current project
  13200. \[X] publish... (project will be prompted for)
  13201. \[A] publish all projects")
  13202. (cmds
  13203. '((?t . org-insert-export-options-template)
  13204. (?v . org-export-visible)
  13205. (?a . org-export-as-ascii)
  13206. (?h . org-export-as-html)
  13207. (?b . org-export-as-html-and-open)
  13208. (?x . org-export-as-xoxo)
  13209. (?i . org-export-icalendar-this-file)
  13210. (?I . org-export-icalendar-all-agenda-files)
  13211. (?c . org-export-icalendar-combine-agenda-files)
  13212. (?F . org-publish-current-file)
  13213. (?P . org-publish-current-project)
  13214. (?X . org-publish)
  13215. (?A . org-publish-all)))
  13216. r1 r2 ass)
  13217. (save-window-excursion
  13218. (delete-other-windows)
  13219. (with-output-to-temp-buffer "*Org Export/Publishing Help*"
  13220. (princ help))
  13221. (message "Select command: ")
  13222. (setq r1 (read-char-exclusive)))
  13223. (setq r2 (if (< r1 27) (+ r1 96) r1))
  13224. (if (setq ass (assq r2 cmds))
  13225. (call-interactively (cdr ass))
  13226. (error "No command associated with key %c" r1))))
  13227. ;; ASCII
  13228. (defconst org-html-entities
  13229. '(("nbsp")
  13230. ("iexcl")
  13231. ("cent")
  13232. ("pound")
  13233. ("curren")
  13234. ("yen")
  13235. ("brvbar")
  13236. ("sect")
  13237. ("uml")
  13238. ("copy")
  13239. ("ordf")
  13240. ("laquo")
  13241. ("not")
  13242. ("shy")
  13243. ("reg")
  13244. ("macr")
  13245. ("deg")
  13246. ("plusmn")
  13247. ("sup2")
  13248. ("sup3")
  13249. ("acute")
  13250. ("micro")
  13251. ("para")
  13252. ("middot")
  13253. ("odot"."o")
  13254. ("star"."*")
  13255. ("cedil")
  13256. ("sup1")
  13257. ("ordm")
  13258. ("raquo")
  13259. ("frac14")
  13260. ("frac12")
  13261. ("frac34")
  13262. ("iquest")
  13263. ("Agrave")
  13264. ("Aacute")
  13265. ("Acirc")
  13266. ("Atilde")
  13267. ("Auml")
  13268. ("Aring") ("AA"."&Aring;")
  13269. ("AElig")
  13270. ("Ccedil")
  13271. ("Egrave")
  13272. ("Eacute")
  13273. ("Ecirc")
  13274. ("Euml")
  13275. ("Igrave")
  13276. ("Iacute")
  13277. ("Icirc")
  13278. ("Iuml")
  13279. ("ETH")
  13280. ("Ntilde")
  13281. ("Ograve")
  13282. ("Oacute")
  13283. ("Ocirc")
  13284. ("Otilde")
  13285. ("Ouml")
  13286. ("times")
  13287. ("Oslash")
  13288. ("Ugrave")
  13289. ("Uacute")
  13290. ("Ucirc")
  13291. ("Uuml")
  13292. ("Yacute")
  13293. ("THORN")
  13294. ("szlig")
  13295. ("agrave")
  13296. ("aacute")
  13297. ("acirc")
  13298. ("atilde")
  13299. ("auml")
  13300. ("aring")
  13301. ("aelig")
  13302. ("ccedil")
  13303. ("egrave")
  13304. ("eacute")
  13305. ("ecirc")
  13306. ("euml")
  13307. ("igrave")
  13308. ("iacute")
  13309. ("icirc")
  13310. ("iuml")
  13311. ("eth")
  13312. ("ntilde")
  13313. ("ograve")
  13314. ("oacute")
  13315. ("ocirc")
  13316. ("otilde")
  13317. ("ouml")
  13318. ("divide")
  13319. ("oslash")
  13320. ("ugrave")
  13321. ("uacute")
  13322. ("ucirc")
  13323. ("uuml")
  13324. ("yacute")
  13325. ("thorn")
  13326. ("yuml")
  13327. ("fnof")
  13328. ("Alpha")
  13329. ("Beta")
  13330. ("Gamma")
  13331. ("Delta")
  13332. ("Epsilon")
  13333. ("Zeta")
  13334. ("Eta")
  13335. ("Theta")
  13336. ("Iota")
  13337. ("Kappa")
  13338. ("Lambda")
  13339. ("Mu")
  13340. ("Nu")
  13341. ("Xi")
  13342. ("Omicron")
  13343. ("Pi")
  13344. ("Rho")
  13345. ("Sigma")
  13346. ("Tau")
  13347. ("Upsilon")
  13348. ("Phi")
  13349. ("Chi")
  13350. ("Psi")
  13351. ("Omega")
  13352. ("alpha")
  13353. ("beta")
  13354. ("gamma")
  13355. ("delta")
  13356. ("epsilon")
  13357. ("varepsilon"."&epsilon;")
  13358. ("zeta")
  13359. ("eta")
  13360. ("theta")
  13361. ("iota")
  13362. ("kappa")
  13363. ("lambda")
  13364. ("mu")
  13365. ("nu")
  13366. ("xi")
  13367. ("omicron")
  13368. ("pi")
  13369. ("rho")
  13370. ("sigmaf") ("varsigma"."&sigmaf;")
  13371. ("sigma")
  13372. ("tau")
  13373. ("upsilon")
  13374. ("phi")
  13375. ("chi")
  13376. ("psi")
  13377. ("omega")
  13378. ("thetasym") ("vartheta"."&thetasym;")
  13379. ("upsih")
  13380. ("piv")
  13381. ("bull") ("bullet"."&bull;")
  13382. ("hellip") ("dots"."&hellip;")
  13383. ("prime")
  13384. ("Prime")
  13385. ("oline")
  13386. ("frasl")
  13387. ("weierp")
  13388. ("image")
  13389. ("real")
  13390. ("trade")
  13391. ("alefsym")
  13392. ("larr") ("leftarrow"."&larr;") ("gets"."&larr;")
  13393. ("uarr") ("uparrow"."&uarr;")
  13394. ("rarr") ("to"."&rarr;") ("rightarrow"."&rarr;")
  13395. ("darr")("downarrow"."&darr;")
  13396. ("harr") ("leftrightarrow"."&harr;")
  13397. ("crarr") ("hookleftarrow"."&crarr;") ; has round hook, not quite CR
  13398. ("lArr") ("Leftarrow"."&lArr;")
  13399. ("uArr") ("Uparrow"."&uArr;")
  13400. ("rArr") ("Rightarrow"."&rArr;")
  13401. ("dArr") ("Downarrow"."&dArr;")
  13402. ("hArr") ("Leftrightarrow"."&hArr;")
  13403. ("forall")
  13404. ("part") ("partial"."&part;")
  13405. ("exist") ("exists"."&exist;")
  13406. ("empty") ("emptyset"."&empty;")
  13407. ("nabla")
  13408. ("isin") ("in"."&isin;")
  13409. ("notin")
  13410. ("ni")
  13411. ("prod")
  13412. ("sum")
  13413. ("minus")
  13414. ("lowast") ("ast"."&lowast;")
  13415. ("radic")
  13416. ("prop") ("proptp"."&prop;")
  13417. ("infin") ("infty"."&infin;")
  13418. ("ang") ("angle"."&ang;")
  13419. ("and") ("vee"."&and;")
  13420. ("or") ("wedge"."&or;")
  13421. ("cap")
  13422. ("cup")
  13423. ("int")
  13424. ("there4")
  13425. ("sim")
  13426. ("cong") ("simeq"."&cong;")
  13427. ("asymp")("approx"."&asymp;")
  13428. ("ne") ("neq"."&ne;")
  13429. ("equiv")
  13430. ("le")
  13431. ("ge")
  13432. ("sub") ("subset"."&sub;")
  13433. ("sup") ("supset"."&sup;")
  13434. ("nsub")
  13435. ("sube")
  13436. ("supe")
  13437. ("oplus")
  13438. ("otimes")
  13439. ("perp")
  13440. ("sdot") ("cdot"."&sdot;")
  13441. ("lceil")
  13442. ("rceil")
  13443. ("lfloor")
  13444. ("rfloor")
  13445. ("lang")
  13446. ("rang")
  13447. ("loz") ("Diamond"."&loz;")
  13448. ("spades") ("spadesuit"."&spades;")
  13449. ("clubs") ("clubsuit"."&clubs;")
  13450. ("hearts") ("diamondsuit"."&hearts;")
  13451. ("diams") ("diamondsuit"."&diams;")
  13452. ("quot")
  13453. ("amp")
  13454. ("lt")
  13455. ("gt")
  13456. ("OElig")
  13457. ("oelig")
  13458. ("Scaron")
  13459. ("scaron")
  13460. ("Yuml")
  13461. ("circ")
  13462. ("tilde")
  13463. ("ensp")
  13464. ("emsp")
  13465. ("thinsp")
  13466. ("zwnj")
  13467. ("zwj")
  13468. ("lrm")
  13469. ("rlm")
  13470. ("ndash")
  13471. ("mdash")
  13472. ("lsquo")
  13473. ("rsquo")
  13474. ("sbquo")
  13475. ("ldquo")
  13476. ("rdquo")
  13477. ("bdquo")
  13478. ("dagger")
  13479. ("Dagger")
  13480. ("permil")
  13481. ("lsaquo")
  13482. ("rsaquo")
  13483. ("euro")
  13484. ("arccos"."arccos")
  13485. ("arcsin"."arcsin")
  13486. ("arctan"."arctan")
  13487. ("arg"."arg")
  13488. ("cos"."cos")
  13489. ("cosh"."cosh")
  13490. ("cot"."cot")
  13491. ("coth"."coth")
  13492. ("csc"."csc")
  13493. ("deg"."deg")
  13494. ("det"."det")
  13495. ("dim"."dim")
  13496. ("exp"."exp")
  13497. ("gcd"."gcd")
  13498. ("hom"."hom")
  13499. ("inf"."inf")
  13500. ("ker"."ker")
  13501. ("lg"."lg")
  13502. ("lim"."lim")
  13503. ("liminf"."liminf")
  13504. ("limsup"."limsup")
  13505. ("ln"."ln")
  13506. ("log"."log")
  13507. ("max"."max")
  13508. ("min"."min")
  13509. ("Pr"."Pr")
  13510. ("sec"."sec")
  13511. ("sin"."sin")
  13512. ("sinh"."sinh")
  13513. ("sup"."sup")
  13514. ("tan"."tan")
  13515. ("tanh"."tanh")
  13516. )
  13517. "Entities for TeX->HTML translation.
  13518. Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
  13519. \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
  13520. In that case, \"\\ent\" will be translated to \"&other;\".
  13521. The list contains HTML entities for Latin-1, Greek and other symbols.
  13522. It is supplemented by a number of commonly used TeX macros with appropriate
  13523. translations. There is currently no way for users to extend this.")
  13524. (defun org-cleaned-string-for-export (string &rest parameters)
  13525. "Cleanup a buffer substring so that links can be created safely."
  13526. (interactive)
  13527. (let* ((re-radio (and org-target-link-regexp
  13528. (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)")))
  13529. (re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
  13530. (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
  13531. (re-archive (concat ":" org-archive-tag ":"))
  13532. rtn)
  13533. (save-excursion
  13534. (set-buffer (get-buffer-create " org-mode-tmp"))
  13535. (erase-buffer)
  13536. (insert string)
  13537. (let ((org-inhibit-startup t)) (org-mode))
  13538. (untabify (point-min) (point-max))
  13539. ;; Get rid of archived trees
  13540. (when (not (eq org-export-with-archived-trees t))
  13541. (goto-char (point-min))
  13542. (while (re-search-forward re-archive nil t)
  13543. (if (not (org-on-heading-p))
  13544. (org-end-of-subtree t)
  13545. (beginning-of-line 1)
  13546. (delete-region
  13547. (if org-export-with-archived-trees (1+ (point-at-eol)) (point))
  13548. (org-end-of-subtree t)))))
  13549. ;; Find targets in comments and move them out of comments,
  13550. ;; but mark them as targets that should be invisible
  13551. (goto-char (point-min))
  13552. (while (re-search-forward "^#.*?\\(<<<?[^>\r\n]+>>>?\\).*" nil t)
  13553. (replace-match "\\1(INVISIBLE)"))
  13554. ;; Remove comments
  13555. (goto-char (point-min))
  13556. (while (re-search-forward "^#.*\n?" nil t)
  13557. (replace-match ""))
  13558. ;; Find matches for radio targets and turn them into internal links
  13559. (goto-char (point-min))
  13560. (when re-radio
  13561. (while (re-search-forward re-radio nil t)
  13562. (replace-match "\\1[[\\2]]")))
  13563. ;; Find all links that contain a newline and put them into a single line
  13564. (goto-char (point-min))
  13565. (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
  13566. (replace-match "\\1 \\3")
  13567. (goto-char (match-beginning 0)))
  13568. ;; Convert LaTeX fragments to images
  13569. (when (memq :LaTeX-fragments parameters)
  13570. (org-format-latex
  13571. (concat "ltxpng/" (file-name-sans-extension
  13572. (file-name-nondirectory
  13573. org-current-export-file)))
  13574. org-current-export-dir nil "Creating LaTeX image %s"))
  13575. (message "Exporting...")
  13576. ;; Normalize links: Convert angle and plain links into bracket links
  13577. ;; Expand link abbreviations
  13578. (goto-char (point-min))
  13579. (while (re-search-forward re-plain-link nil t)
  13580. (replace-match
  13581. (concat
  13582. (match-string 1) "[[" (match-string 2) ":" (match-string 3) "]]")
  13583. t t))
  13584. (goto-char (point-min))
  13585. (while (re-search-forward re-angle-link nil t)
  13586. (replace-match
  13587. (concat
  13588. (match-string 1) "[[" (match-string 2) ":" (match-string 3) "]]")
  13589. t t))
  13590. (goto-char (point-min))
  13591. (while (re-search-forward org-bracket-link-regexp nil t)
  13592. (replace-match
  13593. (concat "[[" (save-match-data
  13594. (org-link-expand-abbrev (match-string 1)))
  13595. "]"
  13596. (if (match-end 3)
  13597. (match-string 2)
  13598. (concat "[" (match-string 1) "]"))
  13599. "]")
  13600. t t))
  13601. ;; Find multiline emphasis and put them into single line
  13602. (when (memq :emph-multiline parameters)
  13603. (goto-char (point-min))
  13604. (while (re-search-forward org-emph-re nil t)
  13605. (subst-char-in-region (match-beginning 0) (match-end 0) ?\n ?\ t)
  13606. (goto-char (1- (match-end 0)))))
  13607. (setq rtn (buffer-string)))
  13608. (kill-buffer " org-mode-tmp")
  13609. rtn))
  13610. (defun org-solidify-link-text (s &optional alist)
  13611. "Take link text and make a safe target out of it."
  13612. (save-match-data
  13613. (let* ((rtn
  13614. (mapconcat
  13615. 'identity
  13616. (org-split-string s "[ \t\r\n]+") "--"))
  13617. (a (assoc rtn alist)))
  13618. (or (cdr a) rtn))))
  13619. (defun org-convert-to-odd-levels ()
  13620. "Convert an org-mode file with all levels allowed to one with odd levels.
  13621. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  13622. level 5 etc."
  13623. (interactive)
  13624. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  13625. (let ((org-odd-levels-only nil) n)
  13626. (save-excursion
  13627. (goto-char (point-min))
  13628. (while (re-search-forward "^\\*\\*+" nil t)
  13629. (setq n (1- (length (match-string 0))))
  13630. (while (>= (setq n (1- n)) 0)
  13631. (org-demote))
  13632. (end-of-line 1))))))
  13633. (defun org-convert-to-oddeven-levels ()
  13634. "Convert an org-mode file with only odd levels to one with odd and even levels.
  13635. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  13636. section with an even level, conversion would destroy the structure of the file. An error
  13637. is signaled in this case."
  13638. (interactive)
  13639. (goto-char (point-min))
  13640. ;; First check if there are no even levels
  13641. (when (re-search-forward "^\\(\\*\\*\\)+[^*]" nil t)
  13642. (org-show-context t)
  13643. (error "Not all levels are odd in this file. Conversion not possible."))
  13644. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  13645. (let ((org-odd-levels-only nil) n)
  13646. (save-excursion
  13647. (goto-char (point-min))
  13648. (while (re-search-forward "^\\*\\*+" nil t)
  13649. (setq n (/ (length (match-string 0)) 2))
  13650. (while (>= (setq n (1- n)) 0)
  13651. (org-promote))
  13652. (end-of-line 1))))))
  13653. (defun org-tr-level (n)
  13654. "Make N odd if required."
  13655. (if org-odd-levels-only (1+ (/ n 2)) n))
  13656. (defvar org-last-level nil) ; dynamically scoped variable
  13657. (defvar org-ascii-current-indentation nil) ; For communication
  13658. (defun org-export-as-ascii (arg)
  13659. "Export the outline as a pretty ASCII file.
  13660. If there is an active region, export only the region.
  13661. The prefix ARG specifies how many levels of the outline should become
  13662. underlined headlines. The default is 3."
  13663. (interactive "P")
  13664. (setq-default org-todo-line-regexp org-todo-line-regexp)
  13665. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  13666. (org-infile-export-plist)))
  13667. (region
  13668. (buffer-substring
  13669. (if (org-region-active-p) (region-beginning) (point-min))
  13670. (if (org-region-active-p) (region-end) (point-max))))
  13671. (custom-times org-display-custom-times)
  13672. (lines (org-export-find-first-heading-line
  13673. (org-skip-comments
  13674. (org-split-string
  13675. (org-cleaned-string-for-export region)
  13676. "[\r\n]"))))
  13677. (org-ascii-current-indentation '(0 . 0))
  13678. (org-startup-with-deadline-check nil)
  13679. (level 0) line txt
  13680. (umax nil)
  13681. (case-fold-search nil)
  13682. (filename (concat (file-name-as-directory
  13683. (org-export-directory :ascii opt-plist))
  13684. (file-name-sans-extension
  13685. (file-name-nondirectory buffer-file-name))
  13686. ".txt"))
  13687. (buffer (find-file-noselect filename))
  13688. (levels-open (make-vector org-level-max nil))
  13689. (odd org-odd-levels-only)
  13690. (date (format-time-string "%Y/%m/%d" (current-time)))
  13691. (time (format-time-string "%X" (org-current-time)))
  13692. (author (plist-get opt-plist :author))
  13693. (title (or (plist-get opt-plist :title)
  13694. (file-name-sans-extension
  13695. (file-name-nondirectory buffer-file-name))))
  13696. (email (plist-get opt-plist :email))
  13697. (language (plist-get opt-plist :language))
  13698. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  13699. (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
  13700. (text nil)
  13701. (todo nil)
  13702. (lang-words nil))
  13703. (setq org-last-level 1)
  13704. (org-init-section-numbers)
  13705. (find-file-noselect filename)
  13706. (setq lang-words (or (assoc language org-export-language-setup)
  13707. (assoc "en" org-export-language-setup)))
  13708. (if org-export-ascii-show-new-buffer
  13709. (switch-to-buffer-other-window buffer)
  13710. (set-buffer buffer))
  13711. (erase-buffer)
  13712. (fundamental-mode)
  13713. ;; create local variables for all options, to make sure all called
  13714. ;; functions get the correct information
  13715. (mapcar (lambda (x)
  13716. (set (make-local-variable (cdr x))
  13717. (plist-get opt-plist (car x))))
  13718. org-export-plist-vars)
  13719. (org-set-local 'org-odd-levels-only odd)
  13720. (setq umax (if arg (prefix-numeric-value arg)
  13721. org-export-headline-levels))
  13722. ;; File header
  13723. (if title (org-insert-centered title ?=))
  13724. (insert "\n")
  13725. (if (or author email)
  13726. (insert (concat (nth 1 lang-words) ": " (or author "")
  13727. (if email (concat " <" email ">") "")
  13728. "\n")))
  13729. (if (and date time)
  13730. (insert (concat (nth 2 lang-words) ": " date " " time "\n")))
  13731. (if text (insert (concat (org-html-expand-for-ascii text) "\n\n")))
  13732. (insert "\n\n")
  13733. (if org-export-with-toc
  13734. (progn
  13735. (insert (nth 3 lang-words) "\n"
  13736. (make-string (length (nth 3 lang-words)) ?=) "\n")
  13737. (mapcar '(lambda (line)
  13738. (if (string-match org-todo-line-regexp
  13739. line)
  13740. ;; This is a headline
  13741. (progn
  13742. (setq level (- (match-end 1) (match-beginning 1))
  13743. level (org-tr-level level)
  13744. txt (match-string 3 line)
  13745. todo
  13746. (or (and org-export-mark-todo-in-toc
  13747. (match-beginning 2)
  13748. (not (equal (match-string 2 line)
  13749. org-done-string)))
  13750. ; TODO, not DONE
  13751. (and org-export-mark-todo-in-toc
  13752. (= level umax)
  13753. (org-search-todo-below
  13754. line lines level))))
  13755. (setq txt (org-html-expand-for-ascii txt))
  13756. (if (and (memq org-export-with-tags '(not-in-toc nil))
  13757. (string-match "[ \t]+:[a-zA-Z0-9_@:]+:[ \t]*$" txt))
  13758. (setq txt (replace-match "" t t txt)))
  13759. (if (string-match quote-re0 txt)
  13760. (setq txt (replace-match "" t t txt)))
  13761. (if org-export-with-section-numbers
  13762. (setq txt (concat (org-section-number level)
  13763. " " txt)))
  13764. (if (<= level umax)
  13765. (progn
  13766. (insert
  13767. (make-string (* (1- level) 4) ?\ )
  13768. (format (if todo "%s (*)\n" "%s\n") txt))
  13769. (setq org-last-level level))
  13770. ))))
  13771. lines)))
  13772. (org-init-section-numbers)
  13773. (while (setq line (pop lines))
  13774. ;; Remove the quoted HTML tags.
  13775. (setq line (org-html-expand-for-ascii line))
  13776. ;; Remove targets
  13777. (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line)
  13778. (setq line (replace-match "" t t line)))
  13779. ;; Replace internal links
  13780. (while (string-match org-bracket-link-regexp line)
  13781. (setq line (replace-match
  13782. (if (match-end 3) "[\\3]" "[\\1]")
  13783. t nil line)))
  13784. (when custom-times
  13785. (setq line (org-translate-time line)))
  13786. (cond
  13787. ((string-match "^\\(\\*+\\)[ \t]*\\(.*\\)" line)
  13788. ;; a Headline
  13789. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  13790. txt (match-string 2 line))
  13791. (org-ascii-level-start level txt umax lines))
  13792. (t
  13793. (insert (org-fix-indentation line org-ascii-current-indentation) "\n"))))
  13794. (normal-mode)
  13795. (save-buffer)
  13796. ;; remove display and invisible chars
  13797. (let (beg end)
  13798. (goto-char (point-min))
  13799. (while (setq beg (next-single-property-change (point) 'display))
  13800. (setq end (next-single-property-change beg 'display))
  13801. (delete-region beg end)
  13802. (goto-char beg)
  13803. (insert "=>"))
  13804. (goto-char (point-min))
  13805. (while (setq beg (next-single-property-change (point) 'org-cwidth))
  13806. (setq end (next-single-property-change beg 'org-cwidth))
  13807. (delete-region beg end)
  13808. (goto-char beg)))
  13809. (goto-char (point-min))))
  13810. (defun org-search-todo-below (line lines level)
  13811. "Search the subtree below LINE for any TODO entries."
  13812. (let ((rest (cdr (memq line lines)))
  13813. (re org-todo-line-regexp)
  13814. line lv todo)
  13815. (catch 'exit
  13816. (while (setq line (pop rest))
  13817. (if (string-match re line)
  13818. (progn
  13819. (setq lv (- (match-end 1) (match-beginning 1))
  13820. todo (and (match-beginning 2)
  13821. (not (equal (match-string 2 line)
  13822. org-done-string))))
  13823. ; TODO, not DONE
  13824. (if (<= lv level) (throw 'exit nil))
  13825. (if todo (throw 'exit t))))))))
  13826. (defun org-html-expand-for-ascii (line)
  13827. "Handle quoted HTML for ASCII export."
  13828. (if org-export-html-expand
  13829. (while (string-match "@<[^<>\n]*>" line)
  13830. ;; We just remove the tags for now.
  13831. (setq line (replace-match "" nil nil line))))
  13832. line)
  13833. (defun org-insert-centered (s &optional underline)
  13834. "Insert the string S centered and underline it with character UNDERLINE."
  13835. (let ((ind (max (/ (- 80 (string-width s)) 2) 0)))
  13836. (insert (make-string ind ?\ ) s "\n")
  13837. (if underline
  13838. (insert (make-string ind ?\ )
  13839. (make-string (string-width s) underline)
  13840. "\n"))))
  13841. (defun org-ascii-level-start (level title umax &optional lines)
  13842. "Insert a new level in ASCII export."
  13843. (let (char (n (- level umax 1)) (ind 0))
  13844. (if (> level umax)
  13845. (progn
  13846. (insert (make-string (* 2 n) ?\ )
  13847. (char-to-string (nth (% n (length org-export-ascii-bullets))
  13848. org-export-ascii-bullets))
  13849. " " title "\n")
  13850. ;; find the indentation of the next non-empty line
  13851. (catch 'stop
  13852. (while lines
  13853. (if (string-match "^\\*" (car lines)) (throw 'stop nil))
  13854. (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
  13855. (throw 'stop (setq ind (org-get-indentation (car lines)))))
  13856. (pop lines)))
  13857. (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
  13858. (if (or (not (equal (char-before) ?\n))
  13859. (not (equal (char-before (1- (point))) ?\n)))
  13860. (insert "\n"))
  13861. (setq char (nth (- umax level) (reverse org-export-ascii-underline)))
  13862. (unless org-export-with-tags
  13863. (if (string-match "[ \t]+\\(:[a-zA-Z0-9_@:]+:\\)[ \t]*$" title)
  13864. (setq title (replace-match "" t t title))))
  13865. (if org-export-with-section-numbers
  13866. (setq title (concat (org-section-number level) " " title)))
  13867. (insert title "\n" (make-string (string-width title) char) "\n")
  13868. (setq org-ascii-current-indentation '(0 . 0)))))
  13869. (defun org-export-visible (type arg)
  13870. "Create a copy of the visible part of the current buffer, and export it.
  13871. The copy is created in a temporary buffer and removed after use.
  13872. TYPE is the final key (as a string) that also select the export command in
  13873. the `C-c C-e' export dispatcher.
  13874. As a special case, if the you type SPC at the prompt, the temporary
  13875. org-mode file will not be removed but presented to you so that you can
  13876. continue to use it. The prefix arg ARG is passed through to the exporting
  13877. command."
  13878. (interactive
  13879. (list (progn
  13880. (message "Export visible: [a]SCII [h]tml [b]rowse HTML [x]OXO [ ]keep buffer")
  13881. (read-char-exclusive))
  13882. current-prefix-arg))
  13883. (if (not (member type '(?a ?\C-a ?b ?\C-b ?h ?x ?\ )))
  13884. (error "Invalid export key"))
  13885. (let* ((binding (cdr (assoc type
  13886. '((?a . org-export-as-ascii)
  13887. (?\C-a . org-export-as-ascii)
  13888. (?b . org-export-as-html-and-open)
  13889. (?\C-b . org-export-as-html-and-open)
  13890. (?h . org-export-as-html)
  13891. (?x . org-export-as-xoxo)))))
  13892. (keepp (equal type ?\ ))
  13893. (file buffer-file-name)
  13894. (buffer (get-buffer-create "*Org Export Visible*"))
  13895. s e)
  13896. (with-current-buffer buffer (erase-buffer))
  13897. (save-excursion
  13898. (setq s (goto-char (point-min)))
  13899. (while (not (= (point) (point-max)))
  13900. (goto-char (org-find-invisible))
  13901. (append-to-buffer buffer s (point))
  13902. (setq s (goto-char (org-find-visible))))
  13903. (goto-char (point-min))
  13904. (unless keepp
  13905. ;; Copy all comment lines to the end, to make sure #+ settings are
  13906. ;; still available for the second export step. Kind of a hack, but
  13907. ;; does do the trick.
  13908. (if (looking-at "#[^\r\n]*")
  13909. (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
  13910. (while (re-search-forward "[\n\r]#[^\n\r]*" nil t)
  13911. (append-to-buffer buffer (1+ (match-beginning 0))
  13912. (min (point-max) (1+ (match-end 0))))))
  13913. (set-buffer buffer)
  13914. (let ((buffer-file-name file)
  13915. (org-inhibit-startup t))
  13916. (org-mode)
  13917. (show-all)
  13918. (unless keepp (funcall binding arg))))
  13919. (if (not keepp)
  13920. (kill-buffer buffer)
  13921. (switch-to-buffer-other-window buffer)
  13922. (goto-char (point-min)))))
  13923. (defun org-find-visible ()
  13924. (let ((s (point)))
  13925. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  13926. (get-char-property s 'invisible)))
  13927. s))
  13928. (defun org-find-invisible ()
  13929. (let ((s (point)))
  13930. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  13931. (not (get-char-property s 'invisible))))
  13932. s))
  13933. ;; HTML
  13934. (defun org-get-current-options ()
  13935. "Return a string with current options as keyword options.
  13936. Does include HTML export options as well as TODO and CATEGORY stuff."
  13937. (format
  13938. "#+TITLE: %s
  13939. #+AUTHOR: %s
  13940. #+EMAIL: %s
  13941. #+LANGUAGE: %s
  13942. #+TEXT: Some descriptive text to be emitted. Several lines OK.
  13943. #+OPTIONS: H:%d num:%s toc:%s \\n:%s @:%s ::%s |:%s ^:%s *:%s TeX:%s LaTeX:%s
  13944. #+CATEGORY: %s
  13945. #+SEQ_TODO: %s
  13946. #+TYP_TODO: %s
  13947. #+STARTUP: %s %s %s %s %s %s
  13948. #+TAGS: %s
  13949. #+ARCHIVE: %s
  13950. #+LINK: %s
  13951. "
  13952. (buffer-name) (user-full-name) user-mail-address org-export-default-language
  13953. org-export-headline-levels
  13954. org-export-with-section-numbers
  13955. org-export-with-toc
  13956. org-export-preserve-breaks
  13957. org-export-html-expand
  13958. org-export-with-fixed-width
  13959. org-export-with-tables
  13960. org-export-with-sub-superscripts
  13961. org-export-with-emphasize
  13962. org-export-with-TeX-macros
  13963. org-export-with-LaTeX-fragments
  13964. (file-name-nondirectory buffer-file-name)
  13965. (if (equal org-todo-interpretation 'sequence)
  13966. (mapconcat 'identity org-todo-keywords " ")
  13967. "TODO FEEDBACK VERIFY DONE")
  13968. (if (equal org-todo-interpretation 'type)
  13969. (mapconcat 'identity org-todo-keywords " ")
  13970. "Me Jason Marie DONE")
  13971. (cdr (assoc org-startup-folded
  13972. '((nil . "showall") (t . "overview") (content . "content"))))
  13973. (if org-startup-with-deadline-check "dlcheck" "nodlcheck")
  13974. (if org-odd-levels-only "odd" "oddeven")
  13975. (if org-hide-leading-stars "hidestars" "showstars")
  13976. (if org-startup-align-all-tables "align" "noalign")
  13977. (if org-log-done "logging" "nologging")
  13978. (or (mapconcat (lambda (x)
  13979. (cond
  13980. ((equal '(:startgroup) x) "{")
  13981. ((equal '(:endgroup) x) "}")
  13982. ((cdr x) (format "%s(%c)" (car x) (cdr x)))
  13983. (t (car x))))
  13984. (or org-tag-alist (org-get-buffer-tags)) " ") "")
  13985. org-archive-location
  13986. "org file:~/org/%s.org"
  13987. ))
  13988. (defun org-insert-export-options-template ()
  13989. "Insert into the buffer a template with information for exporting."
  13990. (interactive)
  13991. (if (not (bolp)) (newline))
  13992. (let ((s (org-get-current-options)))
  13993. (and (string-match "#\\+CATEGORY" s)
  13994. (setq s (substring s 0 (match-beginning 0))))
  13995. (insert s)))
  13996. (defun org-toggle-fixed-width-section (arg)
  13997. "Toggle the fixed-width export.
  13998. If there is no active region, the QUOTE keyword at the current headline is
  13999. inserted or removed. When present, it causes the text between this headline
  14000. and the next to be exported as fixed-width text, and unmodified.
  14001. If there is an active region, this command adds or removes a colon as the
  14002. first character of this line. If the first character of a line is a colon,
  14003. this line is also exported in fixed-width font."
  14004. (interactive "P")
  14005. (let* ((cc 0)
  14006. (regionp (org-region-active-p))
  14007. (beg (if regionp (region-beginning) (point)))
  14008. (end (if regionp (region-end)))
  14009. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  14010. (re "[ \t]*\\(:\\)")
  14011. off)
  14012. (if regionp
  14013. (save-excursion
  14014. (goto-char beg)
  14015. (setq cc (current-column))
  14016. (beginning-of-line 1)
  14017. (setq off (looking-at re))
  14018. (while (> nlines 0)
  14019. (setq nlines (1- nlines))
  14020. (beginning-of-line 1)
  14021. (cond
  14022. (arg
  14023. (move-to-column cc t)
  14024. (insert ":\n")
  14025. (forward-line -1))
  14026. ((and off (looking-at re))
  14027. (replace-match "" t t nil 1))
  14028. ((not off) (move-to-column cc t) (insert ":")))
  14029. (forward-line 1)))
  14030. (save-excursion
  14031. (org-back-to-heading)
  14032. (if (looking-at (concat outline-regexp
  14033. "\\( +\\<" org-quote-string "\\>\\)"))
  14034. (replace-match "" t t nil 1)
  14035. (if (looking-at outline-regexp)
  14036. (progn
  14037. (goto-char (match-end 0))
  14038. (insert " " org-quote-string))))))))
  14039. (defun org-export-as-html-and-open (arg)
  14040. "Export the outline as HTML and immediately open it with a browser.
  14041. If there is an active region, export only the region.
  14042. The prefix ARG specifies how many levels of the outline should become
  14043. headlines. The default is 3. Lower levels will become bulleted lists."
  14044. (interactive "P")
  14045. (org-export-as-html arg 'hidden)
  14046. (org-open-file buffer-file-name))
  14047. (defun org-export-as-html-batch ()
  14048. "Call `org-export-as-html', may be used in batch processing as
  14049. emacs --batch
  14050. --load=$HOME/lib/emacs/org.el
  14051. --eval \"(setq org-export-headline-levels 2)\"
  14052. --visit=MyFile --funcall org-export-as-html-batch"
  14053. (org-export-as-html org-export-headline-levels 'hidden))
  14054. (defun org-export-as-html (arg &optional hidden ext-plist)
  14055. "Export the outline as a pretty HTML file.
  14056. If there is an active region, export only the region.
  14057. The prefix ARG specifies how many levels of the outline should become
  14058. headlines. The default is 3. Lower levels will become bulleted lists.
  14059. When HIDDEN is non-nil, don't display the HTML buffer.
  14060. EXT-PLIST is a property list with external parameters overriding
  14061. org-mode's default settings, but still inferior to file-local settings."
  14062. (interactive "P")
  14063. (message "Exporting...")
  14064. (setq-default org-todo-line-regexp org-todo-line-regexp)
  14065. (setq-default org-deadline-line-regexp org-deadline-line-regexp)
  14066. (setq-default org-done-string org-done-string)
  14067. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  14068. ext-plist
  14069. (org-infile-export-plist)))
  14070. (style (plist-get opt-plist :style))
  14071. (link-validate (plist-get opt-plist :link-validation-function))
  14072. valid
  14073. (odd org-odd-levels-only)
  14074. (region-p (org-region-active-p))
  14075. (region
  14076. (buffer-substring
  14077. (if region-p (region-beginning) (point-min))
  14078. (if region-p (region-end) (point-max))))
  14079. ;; The following two are dynamically scoped into other
  14080. ;; routines below.
  14081. (org-current-export-dir (org-export-directory :html opt-plist))
  14082. (org-current-export-file buffer-file-name)
  14083. (all_lines
  14084. (org-skip-comments (org-split-string
  14085. (org-cleaned-string-for-export
  14086. region :emph-multiline
  14087. (if (plist-get opt-plist :LaTeX-fragments)
  14088. :LaTeX-fragments))
  14089. "[\r\n]")))
  14090. (lines (org-export-find-first-heading-line all_lines))
  14091. (level 0) (line "") (origline "") txt todo
  14092. (umax nil)
  14093. (filename (concat (file-name-as-directory
  14094. (org-export-directory :html opt-plist))
  14095. (file-name-sans-extension
  14096. (file-name-nondirectory buffer-file-name))
  14097. ".html"))
  14098. (current-dir (file-name-directory buffer-file-name))
  14099. (buffer (find-file-noselect filename))
  14100. (levels-open (make-vector org-level-max nil))
  14101. (date (format-time-string "%Y/%m/%d" (current-time)))
  14102. (time (format-time-string "%X" (org-current-time)))
  14103. (author (plist-get opt-plist :author))
  14104. (title (or (plist-get opt-plist :title)
  14105. (file-name-sans-extension
  14106. (file-name-nondirectory buffer-file-name))))
  14107. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  14108. (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
  14109. (inquote nil)
  14110. (infixed nil)
  14111. (in-local-list nil)
  14112. (local-list-num nil)
  14113. (local-list-indent nil)
  14114. (llt org-plain-list-ordered-item-terminator)
  14115. (email (plist-get opt-plist :email))
  14116. (language (plist-get opt-plist :language))
  14117. (text (plist-get opt-plist :text))
  14118. (lang-words nil)
  14119. (target-alist nil) tg
  14120. (head-count 0) cnt
  14121. (start 0)
  14122. (coding-system (and (boundp 'buffer-file-coding-system)
  14123. buffer-file-coding-system))
  14124. (coding-system-for-write coding-system)
  14125. (save-buffer-coding-system coding-system)
  14126. (charset (and coding-system
  14127. (fboundp 'coding-system-get)
  14128. (coding-system-get coding-system 'mime-charset)))
  14129. table-open type
  14130. table-buffer table-orig-buffer
  14131. ind start-is-num starter
  14132. rpl path desc descp desc1 desc2 link
  14133. )
  14134. (message "Exporting...")
  14135. (setq org-last-level 1)
  14136. (org-init-section-numbers)
  14137. ;; Get the language-dependent settings
  14138. (setq lang-words (or (assoc language org-export-language-setup)
  14139. (assoc "en" org-export-language-setup)))
  14140. ;; Switch to the output buffer
  14141. (if (or hidden (not org-export-html-show-new-buffer))
  14142. (set-buffer buffer)
  14143. (switch-to-buffer-other-window buffer))
  14144. (erase-buffer)
  14145. (fundamental-mode)
  14146. (let ((case-fold-search nil)
  14147. (org-odd-levels-only odd))
  14148. ;; create local variables for all options, to make sure all called
  14149. ;; functions get the correct information
  14150. (mapcar (lambda (x)
  14151. (set (make-local-variable (cdr x))
  14152. (plist-get opt-plist (car x))))
  14153. org-export-plist-vars)
  14154. (setq umax (if arg (prefix-numeric-value arg)
  14155. org-export-headline-levels))
  14156. ;; File header
  14157. (insert (format
  14158. "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
  14159. \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
  14160. <html xmlns=\"http://www.w3.org/1999/xhtml\"
  14161. lang=\"%s\" xml:lang=\"%s\">
  14162. <head>
  14163. <title>%s</title>
  14164. <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
  14165. <meta name=\"generator\" content=\"Org-mode\"/>
  14166. <meta name=\"generated\" content=\"%s %s\"/>
  14167. <meta name=\"author\" content=\"%s\"/>
  14168. %s
  14169. </head><body>
  14170. "
  14171. language language (org-html-expand title) (or charset "iso-8859-1")
  14172. date time author style))
  14173. (insert (or (plist-get opt-plist :preamble) ""))
  14174. (when (plist-get opt-plist :auto-preamble)
  14175. (if title (insert (format org-export-html-title-format
  14176. (org-html-expand title))))
  14177. (if text (insert "<p>\n" (org-html-expand text) "</p>")))
  14178. (if org-export-with-toc
  14179. (progn
  14180. (insert (format "<h%d>%s</h%d>\n"
  14181. org-export-html-toplevel-hlevel
  14182. (nth 3 lang-words)
  14183. org-export-html-toplevel-hlevel))
  14184. (insert "<ul>\n<li>")
  14185. (setq lines
  14186. (mapcar '(lambda (line)
  14187. (if (string-match org-todo-line-regexp line)
  14188. ;; This is a headline
  14189. (progn
  14190. (setq level (- (match-end 1) (match-beginning 1))
  14191. level (org-tr-level level)
  14192. txt (save-match-data
  14193. (org-html-expand
  14194. (org-export-cleanup-toc-line
  14195. (match-string 3 line))))
  14196. todo
  14197. (or (and org-export-mark-todo-in-toc
  14198. (match-beginning 2)
  14199. (not (equal (match-string 2 line)
  14200. org-done-string)))
  14201. ; TODO, not DONE
  14202. (and org-export-mark-todo-in-toc
  14203. (= level umax)
  14204. (org-search-todo-below
  14205. line lines level))))
  14206. (if (and (memq org-export-with-tags '(not-in-toc nil))
  14207. (string-match "[ \t]+:[a-zA-Z0-9_@:]+:[ \t]*$" txt))
  14208. (setq txt (replace-match "" t t txt)))
  14209. (if (string-match quote-re0 txt)
  14210. (setq txt (replace-match "" t t txt)))
  14211. (if org-export-with-section-numbers
  14212. (setq txt (concat (org-section-number level)
  14213. " " txt)))
  14214. (if (<= level umax)
  14215. (progn
  14216. (setq head-count (+ head-count 1))
  14217. (if (> level org-last-level)
  14218. (progn
  14219. (setq cnt (- level org-last-level))
  14220. (while (>= (setq cnt (1- cnt)) 0)
  14221. (insert "\n<ul>\n<li>"))
  14222. (insert "\n")))
  14223. (if (< level org-last-level)
  14224. (progn
  14225. (setq cnt (- org-last-level level))
  14226. (while (>= (setq cnt (1- cnt)) 0)
  14227. (insert "</li>\n</ul>"))
  14228. (insert "\n")))
  14229. ;; Check for targets
  14230. (while (string-match org-target-regexp line)
  14231. (setq tg (match-string 1 line)
  14232. line (replace-match
  14233. (concat "@<span class=\"target\">" tg "@</span> ")
  14234. t t line))
  14235. (push (cons (org-solidify-link-text tg)
  14236. (format "sec-%d" head-count))
  14237. target-alist))
  14238. (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
  14239. (setq txt (replace-match "" t t txt)))
  14240. (insert
  14241. (format
  14242. (if todo
  14243. "</li>\n<li><a href=\"#sec-%d\"><span class=\"todo\">%s</span></a>"
  14244. "</li>\n<li><a href=\"#sec-%d\">%s</a>")
  14245. head-count txt))
  14246. (setq org-last-level level))
  14247. )))
  14248. line)
  14249. lines))
  14250. (while (> org-last-level 0)
  14251. (setq org-last-level (1- org-last-level))
  14252. (insert "</li>\n</ul>\n"))
  14253. ))
  14254. (setq head-count 0)
  14255. (org-init-section-numbers)
  14256. (while (setq line (pop lines) origline line)
  14257. (catch 'nextline
  14258. ;; end of quote section?
  14259. (when (and inquote (string-match "^\\*+" line))
  14260. (insert "</pre>\n")
  14261. (setq inquote nil))
  14262. ;; inside a quote section?
  14263. (when inquote
  14264. (insert (org-html-protect line) "\n")
  14265. (throw 'nextline nil))
  14266. ;; verbatim lines
  14267. (when (and org-export-with-fixed-width
  14268. (string-match "^[ \t]*:\\(.*\\)" line))
  14269. (when (not infixed)
  14270. (setq infixed t)
  14271. (insert "<pre>\n"))
  14272. (insert (org-html-protect (match-string 1 line)) "\n")
  14273. (when (and lines
  14274. (not (string-match "^[ \t]*\\(:.*\\)"
  14275. (car lines))))
  14276. (setq infixed nil)
  14277. (insert "</pre>\n"))
  14278. (throw 'nextline nil))
  14279. ;; make targets to anchors
  14280. (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
  14281. (cond
  14282. ((match-end 2)
  14283. (setq line (replace-match
  14284. (concat "@<a name=\""
  14285. (org-solidify-link-text (match-string 1 line))
  14286. "\">\\nbsp@</a>")
  14287. t t line)))
  14288. ((and org-export-with-toc (equal (string-to-char line) ?*))
  14289. (setq line (replace-match
  14290. (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
  14291. ; (concat "@<i>" (match-string 1 line) "@</i> ")
  14292. t t line)))
  14293. (t
  14294. (setq line (replace-match
  14295. (concat "@<a name=\""
  14296. (org-solidify-link-text (match-string 1 line))
  14297. "\" class=\"target\">" (match-string 1 line) "@</a> ")
  14298. t t line)))))
  14299. (setq line (org-html-handle-time-stamps line))
  14300. ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  14301. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  14302. ;; Also handle sub_superscripts and checkboxes
  14303. (setq line (org-html-expand line))
  14304. ;; Format the links
  14305. (setq start 0)
  14306. (while (string-match org-bracket-link-analytic-regexp line start)
  14307. (setq start (match-beginning 0))
  14308. (setq type (if (match-end 2) (match-string 2 line) "internal"))
  14309. (setq path (match-string 3 line))
  14310. (setq desc1 (if (match-end 5) (match-string 5 line))
  14311. desc2 (if (match-end 2) (concat type ":" path) path)
  14312. descp (and desc1 (not (equal desc1 desc2)))
  14313. desc (or desc1 desc2))
  14314. ;; FIXME: do we need to unescape here somewhere?
  14315. (cond
  14316. ((equal type "internal")
  14317. (setq rpl
  14318. (concat
  14319. "<a href=\"#"
  14320. (org-solidify-link-text path target-alist)
  14321. "\">" desc "</a>")))
  14322. ((member type '("http" "https" "ftp" "mailto" "news"))
  14323. ;; standard URL
  14324. (setq link (concat type ":" path))
  14325. (setq rpl (concat "<a href=\"" link "\">" desc "</a>")))
  14326. ((string= type "file")
  14327. ;; FILE link
  14328. (let* ((filename path)
  14329. (abs-p (file-name-absolute-p filename))
  14330. thefile file-is-image-p search)
  14331. (save-match-data
  14332. (if (string-match "::\\(.*\\)" filename)
  14333. (setq search (match-string 1 filename)
  14334. filename (replace-match "" t nil filename)))
  14335. (setq valid
  14336. (if (functionp link-validate)
  14337. (funcall link-validate filename current-dir)
  14338. t))
  14339. (setq file-is-image-p
  14340. (string-match (org-image-file-name-regexp) filename))
  14341. (setq thefile (if abs-p (expand-file-name filename) filename))
  14342. (when (and org-export-html-link-org-files-as-html
  14343. (string-match "\\.org$" thefile))
  14344. (setq thefile (concat (substring thefile 0
  14345. (match-beginning 0))
  14346. ".html"))
  14347. (if (and search
  14348. ;; make sure this is can be used as target search
  14349. (not (string-match "^[0-9]*$" search))
  14350. (not (string-match "^\\*" search))
  14351. (not (string-match "^/.*/$" search)))
  14352. (setq thefile (concat thefile "#"
  14353. (org-solidify-link-text
  14354. (org-link-unescape search)))))
  14355. (when (string-match "^file:" desc)
  14356. (setq desc (replace-match "" t t desc))
  14357. (if (string-match "\\.org$" desc)
  14358. (setq desc (replace-match "" t t desc))))))
  14359. (setq rpl (if (and file-is-image-p
  14360. (or (eq t org-export-html-inline-images)
  14361. (and org-export-html-inline-images
  14362. (not descp))))
  14363. (concat "<img src=\"" thefile "\"/>")
  14364. (concat "<a href=\"" thefile "\">" desc "</a>")))
  14365. (if (not valid) (setq rpl desc))))
  14366. ((member type '("bbdb" "vm" "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  14367. (setq rpl (concat "<i>&lt;" type ":"
  14368. (save-match-data (org-link-unescape path))
  14369. "&gt;</i>"))))
  14370. (setq line (replace-match rpl t t line)
  14371. start (+ start (length rpl))))
  14372. ;; TODO items
  14373. (if (and (string-match org-todo-line-regexp line)
  14374. (match-beginning 2))
  14375. (if (equal (match-string 2 line) org-done-string)
  14376. (setq line (replace-match
  14377. "<span class=\"done\">\\2</span>"
  14378. t nil line 2))
  14379. (setq line (replace-match "<span class=\"todo\">\\2</span>"
  14380. t nil line 2))))
  14381. (cond
  14382. ((string-match "^\\(\\*+\\)[ \t]*\\(.*\\)" line)
  14383. ;; This is a headline
  14384. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  14385. txt (match-string 2 line))
  14386. (if (string-match quote-re0 txt)
  14387. (setq txt (replace-match "" t t txt)))
  14388. (if (<= level umax) (setq head-count (+ head-count 1)))
  14389. (when in-local-list
  14390. ;; Close any local lists before inserting a new header line
  14391. (while local-list-num
  14392. (org-close-li)
  14393. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  14394. (pop local-list-num))
  14395. (setq local-list-indent nil
  14396. in-local-list nil))
  14397. (org-html-level-start level txt umax
  14398. (and org-export-with-toc (<= level umax))
  14399. head-count)
  14400. ;; QUOTES
  14401. (when (string-match quote-re line)
  14402. (insert "<pre>")
  14403. (setq inquote t)))
  14404. ((and org-export-with-tables
  14405. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  14406. (if (not table-open)
  14407. ;; New table starts
  14408. (setq table-open t table-buffer nil table-orig-buffer nil))
  14409. ;; Accumulate lines
  14410. (setq table-buffer (cons line table-buffer)
  14411. table-orig-buffer (cons origline table-orig-buffer))
  14412. (when (or (not lines)
  14413. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  14414. (car lines))))
  14415. (setq table-open nil
  14416. table-buffer (nreverse table-buffer)
  14417. table-orig-buffer (nreverse table-orig-buffer))
  14418. (org-close-par-maybe)
  14419. (insert (org-format-table-html table-buffer table-orig-buffer))))
  14420. (t
  14421. ;; Normal lines
  14422. (when (string-match
  14423. (cond
  14424. ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  14425. ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  14426. ((= llt ?\)) "^\\( \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  14427. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
  14428. line)
  14429. (setq ind (org-get-string-indentation line)
  14430. start-is-num (match-beginning 4)
  14431. starter (if (match-beginning 2)
  14432. (substring (match-string 2 line) 0 -1))
  14433. line (substring line (match-beginning 5)))
  14434. (unless (string-match "[^ \t]" line)
  14435. ;; empty line. Pretend indentation is large.
  14436. (setq ind (1+ (or (car local-list-indent) 1))))
  14437. (while (and in-local-list
  14438. (or (and (= ind (car local-list-indent))
  14439. (not starter))
  14440. (< ind (car local-list-indent))))
  14441. (org-close-li)
  14442. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  14443. (pop local-list-num) (pop local-list-indent)
  14444. (setq in-local-list local-list-indent))
  14445. (cond
  14446. ((and starter
  14447. (or (not in-local-list)
  14448. (> ind (car local-list-indent))))
  14449. ;; Start new (level of ) list
  14450. (org-close-par-maybe)
  14451. (insert (if start-is-num "<ol>\n<li>\n" "<ul>\n<li>\n"))
  14452. (push start-is-num local-list-num)
  14453. (push ind local-list-indent)
  14454. (setq in-local-list t))
  14455. (starter
  14456. ;; continue current list
  14457. (org-close-li)
  14458. (insert "<li>\n")))
  14459. (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line)
  14460. (setq line
  14461. (replace-match
  14462. (if (equal (match-string 1 line) "X")
  14463. "<b>[X]</b>"
  14464. "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
  14465. t t line))))
  14466. ;; Empty lines start a new paragraph. If hand-formatted lists
  14467. ;; are not fully interpreted, lines starting with "-", "+", "*"
  14468. ;; also start a new paragraph.
  14469. (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
  14470. ;; Check if the line break needs to be conserved
  14471. (cond
  14472. ((string-match "\\\\\\\\[ \t]*$" line)
  14473. (setq line (replace-match "<br/>" t t line)))
  14474. (org-export-preserve-breaks
  14475. (setq line (concat line "<br/>"))))
  14476. (insert line "\n")))))
  14477. ;; Properly close all local lists and other lists
  14478. (when inquote (insert "</pre>\n"))
  14479. (when in-local-list
  14480. ;; Close any local lists before inserting a new header line
  14481. (while local-list-num
  14482. (org-close-li)
  14483. (insert (if (car local-list-num) "</ol>\n" "</ul>\n"))
  14484. (pop local-list-num))
  14485. (setq local-list-indent nil
  14486. in-local-list nil))
  14487. (org-html-level-start 1 nil umax
  14488. (and org-export-with-toc (<= level umax))
  14489. head-count)
  14490. (when (plist-get opt-plist :auto-postamble)
  14491. (when author
  14492. (insert "<p class=\"author\"> "
  14493. (nth 1 lang-words) ": " author "\n")
  14494. (when email
  14495. (insert "<a href=\"mailto:" email "\">&lt;"
  14496. email "&gt;</a>\n"))
  14497. (insert "</p>\n"))
  14498. (when (and date time)
  14499. (insert "<p class=\"date\"> "
  14500. (nth 2 lang-words) ": "
  14501. date " " time "</p>\n")))
  14502. (if org-export-html-with-timestamp
  14503. (insert org-export-html-html-helper-timestamp))
  14504. (insert (or (plist-get opt-plist :postamble) ""))
  14505. (insert "</body>\n</html>\n")
  14506. (normal-mode)
  14507. ;; remove empty paragraphs and lists
  14508. (goto-char (point-min))
  14509. (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
  14510. (replace-match ""))
  14511. (goto-char (point-min))
  14512. (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t)
  14513. (replace-match ""))
  14514. (save-buffer)
  14515. (goto-char (point-min))
  14516. (message "Exporting... done"))))
  14517. (defun org-format-table-html (lines olines)
  14518. "Find out which HTML converter to use and return the HTML code."
  14519. (if (string-match "^[ \t]*|" (car lines))
  14520. ;; A normal org table
  14521. (org-format-org-table-html lines)
  14522. ;; Table made by table.el - test for spanning
  14523. (let* ((hlines (delq nil (mapcar
  14524. (lambda (x)
  14525. (if (string-match "^[ \t]*\\+-" x) x
  14526. nil))
  14527. lines)))
  14528. (first (car hlines))
  14529. (ll (and (string-match "\\S-+" first)
  14530. (match-string 0 first)))
  14531. (re (concat "^[ \t]*" (regexp-quote ll)))
  14532. (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
  14533. hlines))))
  14534. (if (and (not spanning)
  14535. (not org-export-prefer-native-exporter-for-tables))
  14536. ;; We can use my own converter with HTML conversions
  14537. (org-format-table-table-html lines)
  14538. ;; Need to use the code generator in table.el, with the original text.
  14539. (org-format-table-table-html-using-table-generate-source olines)))))
  14540. (defun org-format-org-table-html (lines)
  14541. "Format a table into HTML."
  14542. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  14543. (setq lines (nreverse lines))
  14544. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  14545. (setq lines (nreverse lines))
  14546. (when org-export-table-remove-special-lines
  14547. ;; Check if the table has a marking column. If yes remove the
  14548. ;; column and the special lines
  14549. (let* ((special
  14550. (not
  14551. (memq nil
  14552. (mapcar
  14553. (lambda (x)
  14554. (or (string-match "^[ \t]*|-" x)
  14555. (string-match "^[ \t]*| *\\([#!$*_^ ]\\) *|" x)))
  14556. lines)))))
  14557. (if special
  14558. (setq lines
  14559. (delq nil
  14560. (mapcar
  14561. (lambda (x)
  14562. (if (string-match "^[ \t]*| *[!_^] *|" x)
  14563. nil ; ignore this line
  14564. (and (or (string-match "^[ \t]*|-+\\+" x)
  14565. (string-match "^[ \t]*|[^|]*|" x))
  14566. (replace-match "|" t t x))))
  14567. lines))))))
  14568. (let ((head (and org-export-highlight-first-table-line
  14569. (delq nil (mapcar
  14570. (lambda (x) (string-match "^[ \t]*|-" x))
  14571. (cdr lines)))))
  14572. line fields html)
  14573. (setq html (concat org-export-html-table-tag "\n"))
  14574. (while (setq line (pop lines))
  14575. (catch 'next-line
  14576. (if (string-match "^[ \t]*|-" line)
  14577. (progn
  14578. (setq head nil) ;; head ends here, first time around
  14579. ;; ignore this line
  14580. (throw 'next-line t)))
  14581. ;; Break the line into fields
  14582. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  14583. (setq html (concat
  14584. html
  14585. "<tr>"
  14586. (mapconcat (lambda (x)
  14587. (if head
  14588. (concat "<th>" x "</th>")
  14589. (concat "<td>" x "</td>")))
  14590. fields "")
  14591. "</tr>\n"))))
  14592. (setq html (concat html "</table>\n"))
  14593. html))
  14594. (defun org-fake-empty-table-line (line)
  14595. "Replace everything except \"|\" with spaces."
  14596. (let ((i (length line))
  14597. (newstr (copy-sequence line)))
  14598. (while (> i 0)
  14599. (setq i (1- i))
  14600. (if (not (eq (aref newstr i) ?|))
  14601. (aset newstr i ?\ )))
  14602. newstr))
  14603. (defun org-format-table-table-html (lines)
  14604. "Format a table generated by table.el into HTML.
  14605. This conversion does *not* use `table-generate-source' from table.el.
  14606. This has the advantage that Org-mode's HTML conversions can be used.
  14607. But it has the disadvantage, that no cell- or row-spanning is allowed."
  14608. (let (line field-buffer
  14609. (head org-export-highlight-first-table-line)
  14610. fields html empty)
  14611. (setq html (concat org-export-html-table-tag "\n"))
  14612. (while (setq line (pop lines))
  14613. (setq empty "&nbsp;")
  14614. (catch 'next-line
  14615. (if (string-match "^[ \t]*\\+-" line)
  14616. (progn
  14617. (if field-buffer
  14618. (progn
  14619. (setq html (concat
  14620. html
  14621. "<tr>"
  14622. (mapconcat
  14623. (lambda (x)
  14624. (if (equal x "") (setq x empty))
  14625. (if head
  14626. (concat "<th>" x "</th>\n")
  14627. (concat "<td>" x "</td>\n")))
  14628. field-buffer "\n")
  14629. "</tr>\n"))
  14630. (setq head nil)
  14631. (setq field-buffer nil)))
  14632. ;; Ignore this line
  14633. (throw 'next-line t)))
  14634. ;; Break the line into fields and store the fields
  14635. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  14636. (if field-buffer
  14637. (setq field-buffer (mapcar
  14638. (lambda (x)
  14639. (concat x "<br/>" (pop fields)))
  14640. field-buffer))
  14641. (setq field-buffer fields))))
  14642. (setq html (concat html "</table>\n"))
  14643. html))
  14644. (defun org-format-table-table-html-using-table-generate-source (lines)
  14645. "Format a table into html, using `table-generate-source' from table.el.
  14646. This has the advantage that cell- or row-spanning is allowed.
  14647. But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
  14648. (require 'table)
  14649. (with-current-buffer (get-buffer-create " org-tmp1 ")
  14650. (erase-buffer)
  14651. (insert (mapconcat 'identity lines "\n"))
  14652. (goto-char (point-min))
  14653. (if (not (re-search-forward "|[^+]" nil t))
  14654. (error "Error processing table"))
  14655. (table-recognize-table)
  14656. (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
  14657. (table-generate-source 'html " org-tmp2 ")
  14658. (set-buffer " org-tmp2 ")
  14659. (buffer-substring (point-min) (point-max))))
  14660. (defun org-html-handle-time-stamps (s)
  14661. "Format time stamps in string S, or remove them."
  14662. (catch 'exit
  14663. (let (r b)
  14664. (while (string-match org-maybe-keyword-time-regexp s)
  14665. (if (and (match-end 1) (equal (match-string 1 s) org-clock-string))
  14666. ;; never export CLOCK
  14667. (throw 'exit ""))
  14668. (or b (setq b (substring s 0 (match-beginning 0))))
  14669. (if (not org-export-with-timestamps)
  14670. (setq r (concat r (substring s 0 (match-beginning 0)))
  14671. s (substring s (match-end 0)))
  14672. (setq r (concat
  14673. r (substring s 0 (match-beginning 0))
  14674. (if (match-end 1)
  14675. (format "@<span class=\"timestamp-kwd\">%s @</span>"
  14676. (match-string 1 s)))
  14677. (format " @<span class=\"timestamp\">%s@</span>"
  14678. (substring
  14679. (org-translate-time (match-string 3 s)) 1 -1)))
  14680. s (substring s (match-end 0)))))
  14681. ;; Line break if line started and ended with time stamp stuff
  14682. (if (not r)
  14683. s
  14684. (setq r (concat r s))
  14685. (unless (string-match "\\S-" (concat b s))
  14686. (setq r (concat r "@<br/>")))
  14687. r))))
  14688. (defun org-html-protect (s)
  14689. ;; convert & to &amp;, < to &lt; and > to &gt;
  14690. (let ((start 0))
  14691. (while (string-match "&" s start)
  14692. (setq s (replace-match "&amp;" t t s)
  14693. start (1+ (match-beginning 0))))
  14694. (while (string-match "<" s)
  14695. (setq s (replace-match "&lt;" t t s)))
  14696. (while (string-match ">" s)
  14697. (setq s (replace-match "&gt;" t t s))))
  14698. s)
  14699. (defun org-export-cleanup-toc-line (s)
  14700. "Remove tags and time staps from lines going into the toc."
  14701. (if (string-match " +:[a-zA-Z0-9_@:]+: *$" s)
  14702. (setq s (replace-match "" t t s)))
  14703. (when org-export-remove-timestamps-from-toc
  14704. (while (string-match org-maybe-keyword-time-regexp s)
  14705. (setq s (replace-match "" t t s))))
  14706. s)
  14707. (defun org-html-expand (string)
  14708. "Prepare STRING for HTML export. Applies all active conversions.
  14709. If there are links in the string, don't modify these."
  14710. (let* (m s l res)
  14711. (while (setq m (string-match org-bracket-link-regexp string))
  14712. (setq s (substring string 0 m)
  14713. l (match-string 0 string)
  14714. string (substring string (match-end 0)))
  14715. (push (org-html-do-expand s) res)
  14716. (push l res))
  14717. (push (org-html-do-expand string) res)
  14718. (apply 'concat (nreverse res))))
  14719. (defun org-html-do-expand (s)
  14720. "Apply all active conversions to translate special ASCII to HTML."
  14721. (setq s (org-html-protect s))
  14722. (if org-export-html-expand
  14723. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  14724. (setq s (replace-match "<\\1>" t nil s))))
  14725. (if org-export-with-emphasize
  14726. (setq s (org-export-html-convert-emphasize s)))
  14727. (if org-export-with-sub-superscripts
  14728. (setq s (org-export-html-convert-sub-super s)))
  14729. (if org-export-with-TeX-macros
  14730. (let ((start 0) wd ass)
  14731. (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)" s start))
  14732. (setq wd (match-string 1 s))
  14733. (if (setq ass (assoc wd org-html-entities))
  14734. (setq s (replace-match (or (cdr ass)
  14735. (concat "&" (car ass) ";"))
  14736. t t s))
  14737. (setq start (+ start (length wd)))))))
  14738. s)
  14739. (defun org-create-multibrace-regexp (left right n)
  14740. "Create a regular expression which will match a balanced sexp.
  14741. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  14742. as single character strings.
  14743. The regexp returned will match the entire expression including the
  14744. delimiters. It will also define a single group which contains the
  14745. match except for the outermost delimiters. The maximum depth of
  14746. stacked delimiters is N. Escaping delimiters is not possible."
  14747. (let* ((nothing (concat "[^" "\\" left "\\" right "]*?"))
  14748. (or "\\|")
  14749. (re nothing)
  14750. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  14751. (while (> n 1)
  14752. (setq n (1- n)
  14753. re (concat re or next)
  14754. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  14755. (concat left "\\(" re "\\)" right)))
  14756. (defvar org-match-substring-regexp
  14757. (concat
  14758. "\\([^\\]\\)\\([_^]\\)\\("
  14759. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  14760. "\\|"
  14761. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  14762. "\\|"
  14763. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  14764. "The regular expression matching a sub- or superscript.")
  14765. (defun org-export-html-convert-sub-super (string)
  14766. "Convert sub- and superscripts in STRING to HTML."
  14767. (let (key c)
  14768. (while (string-match org-match-substring-regexp string)
  14769. (setq key (if (string= (match-string 2 string) "_") "sub" "sup"))
  14770. (setq c (or (match-string 8 string)
  14771. (match-string 6 string)
  14772. (match-string 5 string)))
  14773. (setq string (replace-match
  14774. (concat (match-string 1 string)
  14775. "<" key ">" c "</" key ">")
  14776. t t string)))
  14777. (while (string-match "\\\\\\([_^]\\)" string)
  14778. (setq string (replace-match (match-string 1 string) t t string))))
  14779. string)
  14780. (defun org-export-html-convert-emphasize (string)
  14781. "Apply emphasis."
  14782. (while (string-match org-emph-re string)
  14783. (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)))
  14784. string)
  14785. (defvar org-par-open nil)
  14786. (defun org-open-par ()
  14787. "Insert <p>, but first close previous paragraph if any."
  14788. (org-close-par-maybe)
  14789. (insert "\n<p>")
  14790. (setq org-par-open t))
  14791. (defun org-close-par-maybe ()
  14792. "Close paragraph if there is one open."
  14793. (when org-par-open
  14794. (insert "</p>")
  14795. (setq org-par-open nil)))
  14796. (defun org-close-li ()
  14797. "Close <li> if necessary."
  14798. (org-close-par-maybe)
  14799. (insert "</li>\n"))
  14800. ; (when (save-excursion
  14801. ; (re-search-backward "</?\\(ul\\|ol\\|li\\|[hH][0-9]\\)>" nil t))
  14802. ; (if (member (match-string 0) '("</ul>" "</ol>" "<li>"))
  14803. ; (insert "</li>"))))
  14804. (defun org-html-level-start (level title umax with-toc head-count)
  14805. "Insert a new level in HTML export.
  14806. When TITLE is nil, just close all open levels."
  14807. (org-close-par-maybe)
  14808. (let ((l (1+ (max level umax))))
  14809. (while (<= l org-level-max)
  14810. (if (aref levels-open (1- l))
  14811. (progn
  14812. (org-html-level-close l)
  14813. (aset levels-open (1- l) nil)))
  14814. (setq l (1+ l)))
  14815. (when title
  14816. ;; If title is nil, this means this function is called to close
  14817. ;; all levels, so the rest is done only if title is given
  14818. (when (string-match "\\(:[a-zA-Z0-9_@:]+:\\)[ \t]*$" title)
  14819. (setq title (replace-match
  14820. (if org-export-with-tags
  14821. (save-match-data
  14822. (concat
  14823. "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
  14824. (mapconcat 'identity (org-split-string
  14825. (match-string 1 title) ":")
  14826. "&nbsp;")
  14827. "</span>"))
  14828. "")
  14829. t t title)))
  14830. (if (> level umax)
  14831. (progn
  14832. (if (aref levels-open (1- level))
  14833. (progn
  14834. (org-close-li)
  14835. (insert "<li>" title "<br/>\n"))
  14836. (aset levels-open (1- level) t)
  14837. (org-close-par-maybe)
  14838. (insert "<ul>\n<li>" title "<br/>\n")))
  14839. (if org-export-with-section-numbers
  14840. (setq title (concat (org-section-number level) " " title)))
  14841. (setq level (+ level org-export-html-toplevel-hlevel -1))
  14842. (if with-toc
  14843. (insert (format "\n<h%d><a name=\"sec-%d\">%s</a></h%d>\n"
  14844. level head-count title level))
  14845. (insert (format "\n<h%d>%s</h%d>\n" level title level)))
  14846. (org-open-par)))))
  14847. (defun org-html-level-close (&rest args)
  14848. "Terminate one level in HTML export."
  14849. (org-close-li)
  14850. (insert "</ul>"))
  14851. ;; Variable holding the vector with section numbers
  14852. (defvar org-section-numbers (make-vector org-level-max 0))
  14853. (defun org-init-section-numbers ()
  14854. "Initialize the vector for the section numbers."
  14855. (let* ((level -1)
  14856. (numbers (nreverse (org-split-string "" "\\.")))
  14857. (depth (1- (length org-section-numbers)))
  14858. (i depth) number-string)
  14859. (while (>= i 0)
  14860. (if (> i level)
  14861. (aset org-section-numbers i 0)
  14862. (setq number-string (or (car numbers) "0"))
  14863. (if (string-match "\\`[A-Z]\\'" number-string)
  14864. (aset org-section-numbers i
  14865. (- (string-to-char number-string) ?A -1))
  14866. (aset org-section-numbers i (string-to-number number-string)))
  14867. (pop numbers))
  14868. (setq i (1- i)))))
  14869. (defun org-section-number (&optional level)
  14870. "Return a string with the current section number.
  14871. When LEVEL is non-nil, increase section numbers on that level."
  14872. (let* ((depth (1- (length org-section-numbers))) idx n (string ""))
  14873. (when level
  14874. (when (> level -1)
  14875. (aset org-section-numbers
  14876. level (1+ (aref org-section-numbers level))))
  14877. (setq idx (1+ level))
  14878. (while (<= idx depth)
  14879. (if (not (= idx 1))
  14880. (aset org-section-numbers idx 0))
  14881. (setq idx (1+ idx))))
  14882. (setq idx 0)
  14883. (while (<= idx depth)
  14884. (setq n (aref org-section-numbers idx))
  14885. (setq string (concat string (if (not (string= string "")) "." "")
  14886. (int-to-string n)))
  14887. (setq idx (1+ idx)))
  14888. (save-match-data
  14889. (if (string-match "\\`\\([@0]\\.\\)+" string)
  14890. (setq string (replace-match "" t nil string)))
  14891. (if (string-match "\\(\\.0\\)+\\'" string)
  14892. (setq string (replace-match "" t nil string))))
  14893. string))
  14894. ;;;###autoload
  14895. (defun org-export-icalendar-this-file ()
  14896. "Export current file as an iCalendar file.
  14897. The iCalendar file will be located in the same directory as the Org-mode
  14898. file, but with extension `.ics'."
  14899. (interactive)
  14900. (org-export-icalendar nil buffer-file-name))
  14901. (defun org-export-as-xoxo-insert-into (buffer &rest output)
  14902. (with-current-buffer buffer
  14903. (apply 'insert output)))
  14904. (put 'org-export-as-xoxo-insert-into 'lisp-indent-function 1)
  14905. (defun org-export-as-xoxo (&optional buffer)
  14906. "Export the org buffer as XOXO.
  14907. The XOXO buffer is named *xoxo-<source buffer name>*"
  14908. (interactive (list (current-buffer)))
  14909. ;; A quickie abstraction
  14910. ;; Output everything as XOXO
  14911. (with-current-buffer (get-buffer buffer)
  14912. (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
  14913. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  14914. (org-infile-export-plist)))
  14915. (filename (concat (file-name-as-directory
  14916. (org-export-directory :xoxo opt-plist))
  14917. (file-name-sans-extension
  14918. (file-name-nondirectory buffer-file-name))
  14919. ".html"))
  14920. (out (find-file-noselect filename))
  14921. (last-level 1)
  14922. (hanging-li nil))
  14923. ;; Check the output buffer is empty.
  14924. (with-current-buffer out (erase-buffer))
  14925. ;; Kick off the output
  14926. (org-export-as-xoxo-insert-into out "<ol class='xoxo'>\n")
  14927. (while (re-search-forward "^\\(\\*+\\) \\(.+\\)" (point-max) 't)
  14928. (let* ((hd (match-string-no-properties 1))
  14929. (level (length hd))
  14930. (text (concat
  14931. (match-string-no-properties 2)
  14932. (save-excursion
  14933. (goto-char (match-end 0))
  14934. (let ((str ""))
  14935. (catch 'loop
  14936. (while 't
  14937. (forward-line)
  14938. (if (looking-at "^[ \t]\\(.*\\)")
  14939. (setq str (concat str (match-string-no-properties 1)))
  14940. (throw 'loop str)))))))))
  14941. ;; Handle level rendering
  14942. (cond
  14943. ((> level last-level)
  14944. (org-export-as-xoxo-insert-into out "\n<ol>\n"))
  14945. ((< level last-level)
  14946. (dotimes (- (- last-level level) 1)
  14947. (if hanging-li
  14948. (org-export-as-xoxo-insert-into out "</li>\n"))
  14949. (org-export-as-xoxo-insert-into out "</ol>\n"))
  14950. (when hanging-li
  14951. (org-export-as-xoxo-insert-into out "</li>\n")
  14952. (setq hanging-li nil)))
  14953. ((equal level last-level)
  14954. (if hanging-li
  14955. (org-export-as-xoxo-insert-into out "</li>\n")))
  14956. )
  14957. (setq last-level level)
  14958. ;; And output the new li
  14959. (setq hanging-li 't)
  14960. (if (equal ?+ (elt text 0))
  14961. (org-export-as-xoxo-insert-into out "<li class='" (substring text 1) "'>")
  14962. (org-export-as-xoxo-insert-into out "<li>" text))))
  14963. ;; Finally finish off the ol
  14964. (dotimes (- last-level 1)
  14965. (if hanging-li
  14966. (org-export-as-xoxo-insert-into out "</li>\n"))
  14967. (org-export-as-xoxo-insert-into out "</ol>\n"))
  14968. ;; Finish the buffer off and clean it up.
  14969. (switch-to-buffer-other-window out)
  14970. (indent-region (point-min) (point-max) nil)
  14971. (save-buffer)
  14972. (goto-char (point-min))
  14973. )))
  14974. ;;;###autoload
  14975. (defun org-export-icalendar-all-agenda-files ()
  14976. "Export all files in `org-agenda-files' to iCalendar .ics files.
  14977. Each iCalendar file will be located in the same directory as the Org-mode
  14978. file, but with extension `.ics'."
  14979. (interactive)
  14980. (apply 'org-export-icalendar nil (org-agenda-files t)))
  14981. ;;;###autoload
  14982. (defun org-export-icalendar-combine-agenda-files ()
  14983. "Export all files in `org-agenda-files' to a single combined iCalendar file.
  14984. The file is stored under the name `org-combined-agenda-icalendar-file'."
  14985. (interactive)
  14986. (apply 'org-export-icalendar t (org-agenda-files t)))
  14987. (defun org-export-icalendar (combine &rest files)
  14988. "Create iCalendar files for all elements of FILES.
  14989. If COMBINE is non-nil, combine all calendar entries into a single large
  14990. file and store it under the name `org-combined-agenda-icalendar-file'."
  14991. (save-excursion
  14992. (let* ((dir (org-export-directory
  14993. :ical (list :publishing-directory
  14994. org-export-publishing-directory)))
  14995. file ical-file ical-buffer category started org-agenda-new-buffers)
  14996. (when combine
  14997. (setq ical-file
  14998. (if (file-name-absolute-p org-combined-agenda-icalendar-file)
  14999. org-combined-agenda-icalendar-file
  15000. (expand-file-name org-combined-agenda-icalendar-file dir))
  15001. ical-buffer (org-get-agenda-file-buffer ical-file))
  15002. (set-buffer ical-buffer) (erase-buffer))
  15003. (while (setq file (pop files))
  15004. (catch 'nextfile
  15005. (org-check-agenda-file file)
  15006. (set-buffer (org-get-agenda-file-buffer file))
  15007. (unless combine
  15008. (setq ical-file (concat (file-name-as-directory dir)
  15009. (file-name-sans-extension
  15010. (file-name-nondirectory buffer-file-name))
  15011. ".ics"))
  15012. (setq ical-buffer (org-get-agenda-file-buffer ical-file))
  15013. (with-current-buffer ical-buffer (erase-buffer)))
  15014. (setq category (or org-category
  15015. (file-name-sans-extension
  15016. (file-name-nondirectory buffer-file-name))))
  15017. (if (symbolp category) (setq category (symbol-name category)))
  15018. (let ((standard-output ical-buffer))
  15019. (if combine
  15020. (and (not started) (setq started t)
  15021. (org-start-icalendar-file org-icalendar-combined-name))
  15022. (org-start-icalendar-file category))
  15023. (org-print-icalendar-entries combine category)
  15024. (when (or (and combine (not files)) (not combine))
  15025. (org-finish-icalendar-file)
  15026. (set-buffer ical-buffer)
  15027. (save-buffer)
  15028. (run-hooks 'org-after-save-iCalendar-file-hook)))))
  15029. (org-release-buffers org-agenda-new-buffers))))
  15030. (defvar org-after-save-iCalendar-file-hook nil
  15031. "Hook run after an iCalendar file has been saved.
  15032. The iCalendar buffer is still current when this hook is run.
  15033. A good way to use this is to tell a desktop calenndar application to re-read
  15034. the iCalendar file.")
  15035. (defun org-print-icalendar-entries (&optional combine category)
  15036. "Print iCalendar entries for the current Org-mode file to `standard-output'.
  15037. When COMBINE is non nil, add the category to each line."
  15038. (let ((re2 (concat "--?-?\\(" org-ts-regexp "\\)"))
  15039. (dts (org-ical-ts-to-string
  15040. (format-time-string (cdr org-time-stamp-formats) (current-time))
  15041. "DTSTART"))
  15042. hd ts ts2 state (inc t) pos scheduledp deadlinep tmp pri)
  15043. (save-excursion
  15044. (goto-char (point-min))
  15045. (while (re-search-forward org-ts-regexp nil t)
  15046. (setq pos (match-beginning 0)
  15047. ts (match-string 0)
  15048. inc t
  15049. hd (org-get-heading))
  15050. (if (looking-at re2)
  15051. (progn
  15052. (goto-char (match-end 0))
  15053. (setq ts2 (match-string 1) inc nil))
  15054. (setq ts2 ts
  15055. tmp (buffer-substring (max (point-min)
  15056. (- pos org-ds-keyword-length))
  15057. pos)
  15058. deadlinep (string-match org-deadline-regexp tmp)
  15059. scheduledp (string-match org-scheduled-regexp tmp)
  15060. ;; donep (org-entry-is-done-p)
  15061. ))
  15062. (if (or (string-match org-tr-regexp hd)
  15063. (string-match org-ts-regexp hd))
  15064. (setq hd (replace-match "" t t hd)))
  15065. (if combine
  15066. (setq hd (concat hd " (category " category ")")))
  15067. (if deadlinep (setq hd (concat "DL: " hd " This is a deadline")))
  15068. (if scheduledp (setq hd (concat "S: " hd " Scheduled for this date")))
  15069. (princ (format "BEGIN:VEVENT
  15070. %s
  15071. %s
  15072. SUMMARY:%s
  15073. END:VEVENT\n"
  15074. (org-ical-ts-to-string ts "DTSTART")
  15075. (org-ical-ts-to-string ts2 "DTEND" inc)
  15076. hd)))
  15077. (when org-icalendar-include-todo
  15078. (goto-char (point-min))
  15079. (while (re-search-forward org-todo-line-regexp nil t)
  15080. (setq state (match-string 1))
  15081. (unless (equal state org-done-string)
  15082. (setq hd (match-string 3))
  15083. (if (string-match org-priority-regexp hd)
  15084. (setq pri (string-to-char (match-string 2 hd))
  15085. hd (concat (substring hd 0 (match-beginning 1))
  15086. (substring hd (- (match-end 1)))))
  15087. (setq pri org-default-priority))
  15088. (setq pri (floor (1+ (* 8. (/ (float (- org-lowest-priority pri))
  15089. (- org-lowest-priority ?A))))))
  15090. (princ (format "BEGIN:VTODO
  15091. %s
  15092. SUMMARY:%s
  15093. SEQUENCE:1
  15094. PRIORITY:%d
  15095. END:VTODO\n"
  15096. dts hd pri))))))))
  15097. (defun org-start-icalendar-file (name)
  15098. "Start an iCalendar file by inserting the header."
  15099. (let ((user user-full-name)
  15100. (name (or name "unknown"))
  15101. (timezone (cadr (current-time-zone))))
  15102. (princ
  15103. (format "BEGIN:VCALENDAR
  15104. VERSION:2.0
  15105. X-WR-CALNAME:%s
  15106. PRODID:-//%s//Emacs with Org-mode//EN
  15107. X-WR-TIMEZONE:%s
  15108. CALSCALE:GREGORIAN\n" name user timezone))))
  15109. (defun org-finish-icalendar-file ()
  15110. "Finish an iCalendar file by inserting the END statement."
  15111. (princ "END:VCALENDAR\n"))
  15112. (defun org-ical-ts-to-string (s keyword &optional inc)
  15113. "Take a time string S and convert it to iCalendar format.
  15114. KEYWORD is added in front, to make a complete line like DTSTART....
  15115. When INC is non-nil, increase the hour by two (if time string contains
  15116. a time), or the day by one (if it does not contain a time)."
  15117. (let ((t1 (org-parse-time-string s 'nodefault))
  15118. t2 fmt have-time time)
  15119. (if (and (car t1) (nth 1 t1) (nth 2 t1))
  15120. (setq t2 t1 have-time t)
  15121. (setq t2 (org-parse-time-string s)))
  15122. (let ((s (car t2)) (mi (nth 1 t2)) (h (nth 2 t2))
  15123. (d (nth 3 t2)) (m (nth 4 t2)) (y (nth 5 t2)))
  15124. (when inc
  15125. (if have-time (setq h (+ 2 h)) (setq d (1+ d))))
  15126. (setq time (encode-time s mi h d m y)))
  15127. (setq fmt (if have-time ":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
  15128. (concat keyword (format-time-string fmt time))))
  15129. ;;; LaTeX stuff
  15130. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  15131. "Keymap for the minor `org-cdlatex-mode'.")
  15132. (define-key org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  15133. (define-key org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  15134. (define-key org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  15135. (define-key org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  15136. (define-key org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  15137. (defvar org-cdlatex-texmathp-advice-is-done nil
  15138. "Flag remembering if we have applied the advice to texmathp already.")
  15139. (define-minor-mode org-cdlatex-mode
  15140. "Toggle the minor `org-cdlatex-mode'.
  15141. This mode supports entering LaTeX environment and math in LaTeX fragments
  15142. in Org-mode.
  15143. \\{org-cdlatex-mode-map}"
  15144. nil " OCDL" nil
  15145. (when org-cdlatex-mode (require 'cdlatex))
  15146. (unless org-cdlatex-texmathp-advice-is-done
  15147. (setq org-cdlatex-texmathp-advice-is-done t)
  15148. (defadvice texmathp (around org-math-always-on activate)
  15149. "Always return t in org-mode buffers.
  15150. This is because we want to insert math symbols without dollars even outside
  15151. the LaTeX math segments. If Orgmode thinks that point is actually inside
  15152. en embedded LaTeX fragement, let texmathp do its job.
  15153. \\[org-cdlatex-mode-map]"
  15154. (interactive)
  15155. (let (p)
  15156. (cond
  15157. ((not (org-mode-p)) ad-do-it)
  15158. ((eq this-command 'cdlatex-math-symbol)
  15159. (setq ad-return-value t
  15160. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  15161. (t
  15162. (let ((p (org-inside-LaTeX-fragment-p)))
  15163. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  15164. (setq ad-return-value t
  15165. texmathp-why '("Org-mode embedded math" . 0))
  15166. (if p ad-do-it)))))))))
  15167. (defun turn-on-org-cdlatex ()
  15168. "Unconditionally turn on `org-cdlatex-mode'."
  15169. (org-cdlatex-mode 1))
  15170. (defun org-inside-LaTeX-fragment-p ()
  15171. "Test if point is inside a LaTeX fragment.
  15172. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  15173. sequence appearing also before point.
  15174. Even though the matchers for math are configurable, this function assumes
  15175. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  15176. delimiters are skipped when they have been removed by customization.
  15177. The return value is nil, or a cons cell with the delimiter and
  15178. and the position of this delimiter.
  15179. This function does a reasonably good job, but can locally be fooled by
  15180. for example currency specifications. For example it will assume being in
  15181. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  15182. fragments that are properly closed, but during editing, we have to live
  15183. with the uncertainty caused by missing closing delimiters. This function
  15184. looks only before point, not after."
  15185. (catch 'exit
  15186. (let ((pos (point))
  15187. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  15188. (lim (progn
  15189. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  15190. (point)))
  15191. dd-on str (start 0) m re)
  15192. (goto-char pos)
  15193. (when dodollar
  15194. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  15195. re (nth 1 (assoc "$" org-latex-regexps)))
  15196. (while (string-match re str start)
  15197. (cond
  15198. ((= (match-end 0) (length str))
  15199. (throw 'exit (cons "$" (+ lim (match-beginning 0)))))
  15200. ((= (match-end 0) (- (length str) 5))
  15201. (throw 'exit nil))
  15202. (t (setq start (match-end 0))))))
  15203. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  15204. (goto-char pos)
  15205. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  15206. (and (match-beginning 2) (throw 'exit nil))
  15207. ;; count $$
  15208. (while (re-search-backward "\\$\\$" lim t)
  15209. (setq dd-on (not dd-on)))
  15210. (goto-char pos)
  15211. (if dd-on (cons "$$" m))))))
  15212. (defun org-try-cdlatex-tab ()
  15213. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  15214. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  15215. - inside a LaTeX fragment, or
  15216. - after the first word in a line, where an abbreviation expansion could
  15217. insert a LaTeX environment."
  15218. (when org-cdlatex-mode
  15219. (cond
  15220. ((save-excursion
  15221. (skip-chars-backward "a-zA-Z0-9*")
  15222. (skip-chars-backward " \t")
  15223. (bolp))
  15224. (cdlatex-tab) t)
  15225. ((org-inside-LaTeX-fragment-p)
  15226. (cdlatex-tab) t)
  15227. (t nil))))
  15228. (defun org-cdlatex-underscore-caret (&optional arg)
  15229. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  15230. Revert to the normal definition outside of these fragments."
  15231. (interactive "P")
  15232. (if (org-inside-LaTeX-fragment-p)
  15233. (call-interactively 'cdlatex-sub-superscript)
  15234. (let (org-cdlatex-mode)
  15235. (call-interactively (key-binding (vector last-input-event))))))
  15236. (defun org-cdlatex-math-modify (&optional arg)
  15237. "Execute `cdlatex-math-modify' in LaTeX fragments.
  15238. Revert to the normal definition outside of these fragments."
  15239. (interactive "P")
  15240. (if (org-inside-LaTeX-fragment-p)
  15241. (call-interactively 'cdlatex-math-modify)
  15242. (let (org-cdlatex-mode)
  15243. (call-interactively (key-binding (vector last-input-event))))))
  15244. (defvar org-latex-fragment-image-overlays nil
  15245. "List of overlays carrying the images of latex fragments.")
  15246. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  15247. (defun org-remove-latex-fragment-image-overlays ()
  15248. "Remove all overlays with LaTeX fragment images in current buffer."
  15249. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  15250. (setq org-latex-fragment-image-overlays nil))
  15251. (defun org-preview-latex-fragment (&optional subtree)
  15252. "Preview the LaTeX fragment at point, or all locally or globally.
  15253. If the cursor is in a LaTeX fragment, create the image and overlay
  15254. it over the source code. If there is no fragment at point, display
  15255. all fragments in the current text, from one headline to the next. With
  15256. prefix SUBTREE, display all fragments in the current subtree. With a
  15257. double prefix `C-u C-u', or when the cursor is before the first headline,
  15258. display all fragments in the buffer.
  15259. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  15260. (interactive "P")
  15261. (org-remove-latex-fragment-image-overlays)
  15262. (save-excursion
  15263. (save-restriction
  15264. (let (beg end at msg)
  15265. (cond
  15266. ((or (equal subtree '(16))
  15267. (not (save-excursion
  15268. (re-search-backward (concat "^" outline-regexp) nil t))))
  15269. (setq beg (point-min) end (point-max)
  15270. msg "Creating images for buffer...%s"))
  15271. ((equal subtree '(4))
  15272. (org-back-to-heading)
  15273. (setq beg (point) end (org-end-of-subtree t)
  15274. msg "Creating images for subtree...%s"))
  15275. (t
  15276. (if (setq at (org-inside-LaTeX-fragment-p))
  15277. (goto-char (max (point-min) (- (cdr at) 2)))
  15278. (org-back-to-heading))
  15279. (setq beg (point) end (progn (outline-next-heading) (point))
  15280. msg (if at "Creating image...%s"
  15281. "Creating images for entry...%s"))))
  15282. (message msg "")
  15283. (narrow-to-region beg end)
  15284. (org-format-latex
  15285. (concat "ltxpng/" (file-name-sans-extension
  15286. (file-name-nondirectory
  15287. buffer-file-name)))
  15288. default-directory 'overlays msg at)
  15289. (message msg "done. Use `C-c C-c' to remove images.")))))
  15290. (defvar org-latex-regexps
  15291. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  15292. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  15293. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  15294. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  15295. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  15296. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  15297. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  15298. "Regular expressions for matching embedded LaTeX.")
  15299. (defun org-format-latex (prefix &optional dir overlays msg at)
  15300. "Replace LaTeX fragments with links to an image, and produce images."
  15301. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  15302. (let* ((prefixnodir (file-name-nondirectory prefix))
  15303. (absprefix (expand-file-name prefix dir))
  15304. (todir (file-name-directory absprefix))
  15305. (opt org-format-latex-options)
  15306. (matchers (plist-get opt :matchers))
  15307. (re-list org-latex-regexps)
  15308. (cnt 0) txt link beg end re e oldfiles
  15309. m n block linkfile movefile ov)
  15310. ;; Make sure the directory exists
  15311. (or (file-directory-p todir) (make-directory todir))
  15312. ;; Check if there are old images files with this prefix, and remove them
  15313. (setq oldfiles (directory-files
  15314. todir 'full
  15315. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$")))
  15316. (while oldfiles (delete-file (pop oldfiles)))
  15317. ;; Check the different regular expressions
  15318. (while (setq e (pop re-list))
  15319. (setq m (car e) re (nth 1 e) n (nth 2 e)
  15320. block (if (nth 3 e) "\n\n" ""))
  15321. (when (member m matchers)
  15322. (goto-char (point-min))
  15323. (while (re-search-forward re nil t)
  15324. (when (or (not at) (equal (cdr at) (match-beginning n)))
  15325. (setq txt (match-string n)
  15326. beg (match-beginning n) end (match-end n)
  15327. cnt (1+ cnt)
  15328. linkfile (format "%s_%04d.png" prefix cnt)
  15329. movefile (format "%s_%04d.png" absprefix cnt)
  15330. link (concat block "[[file:" linkfile "]]" block))
  15331. (if msg (message msg cnt))
  15332. (goto-char beg)
  15333. (org-create-formula-image
  15334. txt movefile opt)
  15335. (if overlays
  15336. (progn
  15337. (setq ov (org-make-overlay beg end))
  15338. (if (featurep 'xemacs)
  15339. (progn
  15340. (org-overlay-put ov 'invisible t)
  15341. (org-overlay-put
  15342. ov 'end-glyph
  15343. (make-glyph (vector 'png :file movefile))))
  15344. (org-overlay-put
  15345. ov 'display
  15346. (list 'image :type 'png :file movefile :ascent 'center)))
  15347. (push ov org-latex-fragment-image-overlays)
  15348. (goto-char end))
  15349. (delete-region beg end)
  15350. (insert link))))))))
  15351. ;; This function borrows from Ganesh Swami's latex2png.el
  15352. (defun org-create-formula-image (string tofile options)
  15353. (let* ((tmpdir (if (featurep 'xemacs)
  15354. (temp-directory)
  15355. temporary-file-directory))
  15356. (texfilebase (make-temp-name
  15357. (expand-file-name "orgtex" tmpdir)))
  15358. ;(texfilebase (make-temp-file "orgtex"))
  15359. ; (dummy (delete-file texfilebase))
  15360. (texfile (concat texfilebase ".tex"))
  15361. (dvifile (concat texfilebase ".dvi"))
  15362. (pngfile (concat texfilebase ".png"))
  15363. (scale (number-to-string (* 1000 (or (plist-get options :scale) 1.0))))
  15364. (fg (or (plist-get options :foreground) "Black"))
  15365. (bg (or (plist-get options :background) "Transparent")))
  15366. (with-temp-file texfile
  15367. (insert "\\documentclass{article}
  15368. \\usepackage{fullpage}
  15369. \\usepackage{amssymb}
  15370. \\usepackage[usenames]{color}
  15371. \\usepackage{amsmath}
  15372. \\usepackage{latexsym}
  15373. \\usepackage[mathscr]{eucal}
  15374. \\pagestyle{empty}
  15375. \\begin{document}\n" string "\n\\end{document}\n"))
  15376. (let ((dir default-directory))
  15377. (condition-case nil
  15378. (progn
  15379. (cd tmpdir)
  15380. (call-process "latex" nil nil nil texfile))
  15381. (error nil))
  15382. (cd dir))
  15383. (if (not (file-exists-p dvifile))
  15384. (progn (message "Failed to create dvi file from %s" texfile) nil)
  15385. (call-process "dvipng" nil nil nil
  15386. "-E" "-fg" fg "-bg" bg
  15387. "-x" scale "-y" scale "-T" "tight"
  15388. "-o" pngfile
  15389. dvifile)
  15390. (if (not (file-exists-p pngfile))
  15391. (progn (message "Failed to create png file from %s" texfile) nil)
  15392. ;; Use the requested file name and clean up
  15393. (copy-file pngfile tofile 'replace)
  15394. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  15395. (delete-file (concat texfilebase e)))
  15396. pngfile))))
  15397. ;;; Key bindings
  15398. ;; - Bindings in Org-mode map are currently
  15399. ;; 0123456789abcdefghijklmnopqrstuvwxyz!?@#$%^&-+*/=()_{}[]:;"|,.<>~`'\t the alphabet
  15400. ;; abcd fgh j lmnopqrstuvwxyz!? #$ ^ -+*/= [] ; |,.<>~ '\t necessary bindings
  15401. ;; e (?) useful from outline-mode
  15402. ;; i k @ expendable from outline-mode
  15403. ;; 0123456789 % & ()_{} " ` free
  15404. ;; Make `C-c C-x' a prefix key
  15405. (define-key org-mode-map "\C-c\C-x" (make-sparse-keymap))
  15406. ;; TAB key with modifiers
  15407. (define-key org-mode-map "\C-i" 'org-cycle)
  15408. (define-key org-mode-map [(tab)] 'org-cycle)
  15409. (define-key org-mode-map [(control tab)] 'org-force-cycle-archived)
  15410. (define-key org-mode-map [(meta tab)] 'org-complete)
  15411. (define-key org-mode-map "\M-\C-i" 'org-complete)
  15412. ;; The following line is necessary under Suse GNU/Linux
  15413. (unless (featurep 'xemacs)
  15414. (define-key org-mode-map [S-iso-lefttab] 'org-shifttab))
  15415. (define-key org-mode-map [(shift tab)] 'org-shifttab)
  15416. (define-key org-mode-map (org-key 'S-return) 'org-table-copy-down)
  15417. (define-key org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  15418. (define-key org-mode-map [(meta return)] 'org-meta-return)
  15419. ;; Cursor keys with modifiers
  15420. (define-key org-mode-map [(meta left)] 'org-metaleft)
  15421. (define-key org-mode-map [(meta right)] 'org-metaright)
  15422. (define-key org-mode-map [(meta up)] 'org-metaup)
  15423. (define-key org-mode-map [(meta down)] 'org-metadown)
  15424. (define-key org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  15425. (define-key org-mode-map [(meta shift right)] 'org-shiftmetaright)
  15426. (define-key org-mode-map [(meta shift up)] 'org-shiftmetaup)
  15427. (define-key org-mode-map [(meta shift down)] 'org-shiftmetadown)
  15428. (define-key org-mode-map (org-key 'S-up) 'org-shiftup)
  15429. (define-key org-mode-map (org-key 'S-down) 'org-shiftdown)
  15430. (define-key org-mode-map (org-key 'S-left) 'org-shiftleft)
  15431. (define-key org-mode-map (org-key 'S-right) 'org-shiftright)
  15432. ;; Extra keys for tty access. We only set them when really needed
  15433. ;; because otherwise the menus don't show the simple keys
  15434. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  15435. (not window-system))
  15436. (define-key org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  15437. (define-key org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  15438. (define-key org-mode-map "\C-c\C-xm" 'org-meta-return)
  15439. (define-key org-mode-map [?\e (return)] 'org-meta-return)
  15440. (define-key org-mode-map [?\e (left)] 'org-metaleft)
  15441. (define-key org-mode-map "\C-c\C-xl" 'org-metaleft)
  15442. (define-key org-mode-map [?\e (right)] 'org-metaright)
  15443. (define-key org-mode-map "\C-c\C-xr" 'org-metaright)
  15444. (define-key org-mode-map [?\e (up)] 'org-metaup)
  15445. (define-key org-mode-map "\C-c\C-xu" 'org-metaup)
  15446. (define-key org-mode-map [?\e (down)] 'org-metadown)
  15447. (define-key org-mode-map "\C-c\C-xd" 'org-metadown)
  15448. (define-key org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  15449. (define-key org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  15450. (define-key org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  15451. (define-key org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  15452. (define-key org-mode-map [?\C-c ?\C-x (up)] 'org-shiftup)
  15453. (define-key org-mode-map [?\C-c ?\C-x (down)] 'org-shiftdown)
  15454. (define-key org-mode-map [?\C-c ?\C-x (left)] 'org-shiftleft)
  15455. (define-key org-mode-map [?\C-c ?\C-x (right)] 'org-shiftright))
  15456. ;; All the other keys
  15457. (define-key org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  15458. (define-key org-mode-map "\C-c\C-r" 'org-reveal)
  15459. (define-key org-mode-map "\C-xns" 'org-narrow-to-subtree)
  15460. (define-key org-mode-map "\C-c$" 'org-archive-subtree)
  15461. (define-key org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  15462. (define-key org-mode-map "\C-c\C-j" 'org-goto)
  15463. (define-key org-mode-map "\C-c\C-t" 'org-todo)
  15464. (define-key org-mode-map "\C-c\C-s" 'org-schedule)
  15465. (define-key org-mode-map "\C-c\C-d" 'org-deadline)
  15466. (define-key org-mode-map "\C-c;" 'org-toggle-comment)
  15467. (define-key org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  15468. (define-key org-mode-map "\C-c\C-w" 'org-check-deadlines)
  15469. (define-key org-mode-map "\C-c/" 'org-occur) ; Minor-mode reserved
  15470. (define-key org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  15471. (define-key org-mode-map "\C-c\C-m" 'org-insert-heading)
  15472. (define-key org-mode-map "\M-\C-m" 'org-insert-heading)
  15473. (define-key org-mode-map "\C-c\C-l" 'org-insert-link)
  15474. (define-key org-mode-map "\C-c\C-o" 'org-open-at-point)
  15475. (define-key org-mode-map "\C-c%" 'org-mark-ring-push)
  15476. (define-key org-mode-map "\C-c&" 'org-mark-ring-goto)
  15477. (define-key org-mode-map "\C-c\C-z" 'org-time-stamp) ; Alternative binding
  15478. (define-key org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  15479. (define-key org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  15480. (define-key org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  15481. (define-key org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  15482. (define-key org-mode-map "\C-c>" 'org-goto-calendar)
  15483. (define-key org-mode-map "\C-c<" 'org-date-from-calendar)
  15484. (define-key org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  15485. (define-key org-mode-map "\C-c[" 'org-agenda-file-to-front)
  15486. (define-key org-mode-map "\C-c]" 'org-remove-file)
  15487. (define-key org-mode-map "\C-c-" 'org-table-insert-hline)
  15488. (define-key org-mode-map "\C-c^" 'org-sort)
  15489. (define-key org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  15490. (define-key org-mode-map "\C-c#" 'org-update-checkbox-count)
  15491. (define-key org-mode-map "\C-m" 'org-return)
  15492. (define-key org-mode-map "\C-c?" 'org-table-current-column)
  15493. (define-key org-mode-map "\C-c " 'org-table-blank-field)
  15494. (define-key org-mode-map "\C-c+" 'org-table-sum)
  15495. (define-key org-mode-map "\C-c=" 'org-table-eval-formula)
  15496. (define-key org-mode-map "\C-c'" 'org-table-edit-formulas)
  15497. (define-key org-mode-map "\C-c`" 'org-table-edit-field)
  15498. (define-key org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  15499. (define-key org-mode-map "\C-c*" 'org-table-recalculate)
  15500. (define-key org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  15501. (define-key org-mode-map "\C-c~" 'org-table-create-with-table.el)
  15502. (define-key org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  15503. (define-key org-mode-map "\C-c\C-e" 'org-export)
  15504. (define-key org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  15505. (define-key org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  15506. (define-key org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  15507. (define-key org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  15508. (define-key org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  15509. (define-key org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  15510. (define-key org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  15511. (define-key org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  15512. (define-key org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  15513. (define-key org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  15514. (define-key org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  15515. (define-key org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  15516. (define-key org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  15517. (define-key org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  15518. (when (featurep 'xemacs)
  15519. (define-key org-mode-map 'button3 'popup-mode-menu))
  15520. (defsubst org-table-p () (org-at-table-p))
  15521. (defun org-self-insert-command (N)
  15522. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  15523. If the cursor is in a table looking at whitespace, the whitespace is
  15524. overwritten, and the table is not marked as requiring realignment."
  15525. (interactive "p")
  15526. (if (and (org-table-p)
  15527. (progn
  15528. ;; check if we blank the field, and if that triggers align
  15529. (and org-table-auto-blank-field
  15530. (member last-command
  15531. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  15532. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  15533. ;; got extra space, this field does not determine column width
  15534. (let (org-table-may-need-update) (org-table-blank-field))
  15535. ;; no extra space, this field may determine column width
  15536. (org-table-blank-field)))
  15537. t)
  15538. (eq N 1)
  15539. (looking-at "[^|\n]* |"))
  15540. (let (org-table-may-need-update)
  15541. (goto-char (1- (match-end 0)))
  15542. (delete-backward-char 1)
  15543. (goto-char (match-beginning 0))
  15544. (self-insert-command N))
  15545. (setq org-table-may-need-update t)
  15546. (self-insert-command N)))
  15547. (defun org-delete-backward-char (N)
  15548. "Like `delete-backward-char', insert whitespace at field end in tables.
  15549. When deleting backwards, in tables this function will insert whitespace in
  15550. front of the next \"|\" separator, to keep the table aligned. The table will
  15551. still be marked for re-alignment if the field did fill the entire column,
  15552. because, in this case the deletion might narrow the column."
  15553. (interactive "p")
  15554. (if (and (org-table-p)
  15555. (eq N 1)
  15556. (string-match "|" (buffer-substring (point-at-bol) (point)))
  15557. (looking-at ".*?|"))
  15558. (let ((pos (point))
  15559. (noalign (looking-at "[^|\n\r]* |"))
  15560. (c org-table-may-need-update))
  15561. (backward-delete-char N)
  15562. (skip-chars-forward "^|")
  15563. (insert " ")
  15564. (goto-char (1- pos))
  15565. ;; noalign: if there were two spaces at the end, this field
  15566. ;; does not determine the width of the column.
  15567. (if noalign (setq org-table-may-need-update c)))
  15568. (backward-delete-char N)))
  15569. (defun org-delete-char (N)
  15570. "Like `delete-char', but insert whitespace at field end in tables.
  15571. When deleting characters, in tables this function will insert whitespace in
  15572. front of the next \"|\" separator, to keep the table aligned. The table will
  15573. still be marked for re-alignment if the field did fill the entire column,
  15574. because, in this case the deletion might narrow the column."
  15575. (interactive "p")
  15576. (if (and (org-table-p)
  15577. (not (bolp))
  15578. (not (= (char-after) ?|))
  15579. (eq N 1))
  15580. (if (looking-at ".*?|")
  15581. (let ((pos (point))
  15582. (noalign (looking-at "[^|\n\r]* |"))
  15583. (c org-table-may-need-update))
  15584. (replace-match (concat
  15585. (substring (match-string 0) 1 -1)
  15586. " |"))
  15587. (goto-char pos)
  15588. ;; noalign: if there were two spaces at the end, this field
  15589. ;; does not determine the width of the column.
  15590. (if noalign (setq org-table-may-need-update c)))
  15591. (delete-char N))
  15592. (delete-char N)))
  15593. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  15594. (put 'org-self-insert-command 'delete-selection t)
  15595. (put 'orgtbl-self-insert-command 'delete-selection t)
  15596. (put 'org-delete-char 'delete-selection 'supersede)
  15597. (put 'org-delete-backward-char 'delete-selection 'supersede)
  15598. ;; How to do this: Measure non-white length of current string
  15599. ;; If equal to column width, we should realign.
  15600. (defun org-remap (map &rest commands)
  15601. "In MAP, remap the functions given in COMMANDS.
  15602. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  15603. (let (new old)
  15604. (while commands
  15605. (setq old (pop commands) new (pop commands))
  15606. (if (fboundp 'command-remapping)
  15607. (define-key map (vector 'remap old) new)
  15608. (substitute-key-definition old new map global-map)))))
  15609. (when (eq org-enable-table-editor 'optimized)
  15610. ;; If the user wants maximum table support, we need to hijack
  15611. ;; some standard editing functions
  15612. (org-remap org-mode-map
  15613. 'self-insert-command 'org-self-insert-command
  15614. 'delete-char 'org-delete-char
  15615. 'delete-backward-char 'org-delete-backward-char)
  15616. (define-key org-mode-map "|" 'org-force-self-insert))
  15617. (defun org-shiftcursor-error ()
  15618. "Throw an error because Shift-Cursor command was applied in wrong context."
  15619. (error "This command is active in special context like tables, headlines or timestamps"))
  15620. (defun org-shifttab (&optional arg)
  15621. "Global visibility cycling or move to previous table field.
  15622. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  15623. on context.
  15624. See the individual commands for more information."
  15625. (interactive "P")
  15626. (cond
  15627. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  15628. (t (call-interactively 'org-global-cycle))))
  15629. (defun org-shiftmetaleft ()
  15630. "Promote subtree or delete table column.
  15631. Calls `org-promote-subtree' or `org-table-delete-column', depending on context.
  15632. See the individual commands for more information."
  15633. (interactive)
  15634. (cond
  15635. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  15636. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  15637. ((org-at-item-p) (call-interactively 'org-outdent-item))
  15638. (t (org-shiftcursor-error))))
  15639. (defun org-shiftmetaright ()
  15640. "Demote subtree or insert table column.
  15641. Calls `org-demote-subtree' or `org-table-insert-column', depending on context.
  15642. See the individual commands for more information."
  15643. (interactive)
  15644. (cond
  15645. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  15646. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  15647. ((org-at-item-p) (call-interactively 'org-indent-item))
  15648. (t (org-shiftcursor-error))))
  15649. (defun org-shiftmetaup (&optional arg)
  15650. "Move subtree up or kill table row.
  15651. Calls `org-move-subtree-up' or `org-table-kill-row' or
  15652. `org-move-item-up' depending on context. See the individual commands
  15653. for more information."
  15654. (interactive "P")
  15655. (cond
  15656. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  15657. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  15658. ((org-at-item-p) (call-interactively 'org-move-item-up))
  15659. (t (org-shiftcursor-error))))
  15660. (defun org-shiftmetadown (&optional arg)
  15661. "Move subtree down or insert table row.
  15662. Calls `org-move-subtree-down' or `org-table-insert-row' or
  15663. `org-move-item-down', depending on context. See the individual
  15664. commands for more information."
  15665. (interactive "P")
  15666. (cond
  15667. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  15668. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  15669. ((org-at-item-p) (call-interactively 'org-move-item-down))
  15670. (t (org-shiftcursor-error))))
  15671. (defun org-metaleft (&optional arg)
  15672. "Promote heading or move table column to left.
  15673. Calls `org-do-promote' or `org-table-move-column', depending on context.
  15674. With no specific context, calls the Emacs default `backward-word'.
  15675. See the individual commands for more information."
  15676. (interactive "P")
  15677. (cond
  15678. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  15679. ((or (org-on-heading-p) (org-region-active-p))
  15680. (call-interactively 'org-do-promote))
  15681. ((org-at-item-p) (call-interactively 'org-outdent-item))
  15682. (t (call-interactively 'backward-word))))
  15683. (defun org-metaright (&optional arg)
  15684. "Demote subtree or move table column to right.
  15685. Calls `org-do-demote' or `org-table-move-column', depending on context.
  15686. With no specific context, calls the Emacs default `forward-word'.
  15687. See the individual commands for more information."
  15688. (interactive "P")
  15689. (cond
  15690. ((org-at-table-p) (call-interactively 'org-table-move-column))
  15691. ((or (org-on-heading-p) (org-region-active-p))
  15692. (call-interactively 'org-do-demote))
  15693. ((org-at-item-p) (call-interactively 'org-indent-item))
  15694. (t (call-interactively 'forward-word))))
  15695. (defun org-metaup (&optional arg)
  15696. "Move subtree up or move table row up.
  15697. Calls `org-move-subtree-up' or `org-table-move-row' or
  15698. `org-move-item-up', depending on context. See the individual commands
  15699. for more information."
  15700. (interactive "P")
  15701. (cond
  15702. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  15703. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  15704. ((org-at-item-p) (call-interactively 'org-move-item-up))
  15705. (t (org-shiftcursor-error))))
  15706. (defun org-metadown (&optional arg)
  15707. "Move subtree down or move table row down.
  15708. Calls `org-move-subtree-down' or `org-table-move-row' or
  15709. `org-move-item-down', depending on context. See the individual
  15710. commands for more information."
  15711. (interactive "P")
  15712. (cond
  15713. ((org-at-table-p) (call-interactively 'org-table-move-row))
  15714. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  15715. ((org-at-item-p) (call-interactively 'org-move-item-down))
  15716. (t (org-shiftcursor-error))))
  15717. (defun org-shiftup (&optional arg)
  15718. "Increase item in timestamp or increase priority of current headline.
  15719. Calls `org-timestamp-up' or `org-priority-up', depending on context.
  15720. See the individual commands for more information."
  15721. (interactive "P")
  15722. (cond
  15723. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up))
  15724. ((org-on-heading-p) (call-interactively 'org-priority-up))
  15725. ((org-at-item-p) (call-interactively 'org-previous-item))
  15726. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  15727. (defun org-shiftdown (&optional arg)
  15728. "Decrease item in timestamp or decrease priority of current headline.
  15729. Calls `org-timestamp-down' or `org-priority-down', depending on context.
  15730. See the individual commands for more information."
  15731. (interactive "P")
  15732. (cond
  15733. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down))
  15734. ((org-on-heading-p) (call-interactively 'org-priority-down))
  15735. (t (call-interactively 'org-next-item))))
  15736. (defun org-shiftright ()
  15737. "Next TODO keyword or timestamp one day later, depending on context."
  15738. (interactive)
  15739. (cond
  15740. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  15741. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  15742. (t (org-shiftcursor-error))))
  15743. (defun org-shiftleft ()
  15744. "Previous TODO keyword or timestamp one day earlier, depending on context."
  15745. (interactive)
  15746. (cond
  15747. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  15748. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  15749. (t (org-shiftcursor-error))))
  15750. (defun org-copy-special ()
  15751. "Copy region in table or copy current subtree.
  15752. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  15753. See the individual commands for more information."
  15754. (interactive)
  15755. (call-interactively
  15756. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  15757. (defun org-cut-special ()
  15758. "Cut region in table or cut current subtree.
  15759. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  15760. See the individual commands for more information."
  15761. (interactive)
  15762. (call-interactively
  15763. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  15764. (defun org-paste-special (arg)
  15765. "Paste rectangular region into table, or past subtree relative to level.
  15766. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  15767. See the individual commands for more information."
  15768. (interactive "P")
  15769. (if (org-at-table-p)
  15770. (org-table-paste-rectangle)
  15771. (org-paste-subtree arg)))
  15772. (defun org-ctrl-c-ctrl-c (&optional arg)
  15773. "Set tags in headline, or update according to changed information at point.
  15774. This command does many different things, depending on context:
  15775. - If the cursor is in a headline, prompt for tags and insert them
  15776. into the current line, aligned to `org-tags-column'. When called
  15777. with prefix arg, realign all tags in the current buffer.
  15778. - If the cursor is in one of the special #+KEYWORD lines, this
  15779. triggers scanning the buffer for these lines and updating the
  15780. information.
  15781. - If the cursor is inside a table, realign the table. This command
  15782. works even if the automatic table editor has been turned off.
  15783. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  15784. the entire table.
  15785. - If the cursor is inside a table created by the table.el package,
  15786. activate that table.
  15787. - If the current buffer is a remember buffer, close note and file it.
  15788. with a prefix argument, file it without further interaction to the default
  15789. location.
  15790. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  15791. links in this buffer.
  15792. - If the cursor is on a numbered item in a plain list, renumber the
  15793. ordered list."
  15794. (interactive "P")
  15795. (let ((org-enable-table-editor t))
  15796. (cond
  15797. ((or org-clock-overlays
  15798. org-occur-highlights
  15799. org-latex-fragment-image-overlays)
  15800. (org-remove-clock-overlays)
  15801. (org-remove-occur-highlights)
  15802. (org-remove-latex-fragment-image-overlays)
  15803. (message "Temporary highlights/overlays removed from current buffer"))
  15804. ((and (local-variable-p 'org-finish-function (current-buffer))
  15805. (fboundp org-finish-function))
  15806. (funcall org-finish-function))
  15807. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  15808. ((org-on-heading-p) (call-interactively 'org-set-tags))
  15809. ((org-at-table.el-p)
  15810. (require 'table)
  15811. (beginning-of-line 1)
  15812. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  15813. (call-interactively 'table-recognize-table))
  15814. ((org-at-table-p)
  15815. (org-table-maybe-eval-formula)
  15816. (if arg
  15817. (call-interactively 'org-table-recalculate)
  15818. (org-table-maybe-recalculate-line))
  15819. (call-interactively 'org-table-align))
  15820. ((org-at-item-checkbox-p)
  15821. (call-interactively 'org-toggle-checkbox))
  15822. ((org-at-item-p)
  15823. (call-interactively 'org-renumber-ordered-list))
  15824. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  15825. (cond
  15826. ((equal (match-string 1) "TBLFM")
  15827. ;; Recalculate the table before this line
  15828. (save-excursion
  15829. (beginning-of-line 1)
  15830. (skip-chars-backward " \r\n\t")
  15831. (if (org-at-table-p)
  15832. (org-call-with-arg 'org-table-recalculate t))))
  15833. (t
  15834. (call-interactively 'org-mode-restart))))
  15835. (t (error "C-c C-c can do nothing useful at this location.")))))
  15836. (defun org-mode-restart ()
  15837. "Restart Org-mode, to scan again for special lines.
  15838. Also updates the keyword regular expressions."
  15839. (interactive)
  15840. (let ((org-inhibit-startup t)) (org-mode))
  15841. (message "Org-mode restarted to refresh keyword and special line setup"))
  15842. (defun org-return ()
  15843. "Goto next table row or insert a newline.
  15844. Calls `org-table-next-row' or `newline', depending on context.
  15845. See the individual commands for more information."
  15846. (interactive)
  15847. (cond
  15848. ((org-at-table-p)
  15849. (org-table-justify-field-maybe)
  15850. (call-interactively 'org-table-next-row))
  15851. (t (newline))))
  15852. (defun org-meta-return (&optional arg)
  15853. "Insert a new heading or wrap a region in a table.
  15854. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  15855. See the individual commands for more information."
  15856. (interactive "P")
  15857. (cond
  15858. ((org-at-table-p)
  15859. (call-interactively 'org-table-wrap-region))
  15860. (t (call-interactively 'org-insert-heading))))
  15861. ;;; Menu entries
  15862. ;; Define the Org-mode menus
  15863. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  15864. '("Tbl"
  15865. ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
  15866. ["Next Field" org-cycle (org-at-table-p)]
  15867. ["Previous Field" org-shifttab (org-at-table-p)]
  15868. ["Next Row" org-return (org-at-table-p)]
  15869. "--"
  15870. ["Blank Field" org-table-blank-field (org-at-table-p)]
  15871. ["Edit Field" org-table-edit-field (org-at-table-p)]
  15872. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  15873. "--"
  15874. ("Column"
  15875. ["Move Column Left" org-metaleft (org-at-table-p)]
  15876. ["Move Column Right" org-metaright (org-at-table-p)]
  15877. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  15878. ["Insert Column" org-shiftmetaright (org-at-table-p)]
  15879. "--"
  15880. ["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])
  15881. ("Row"
  15882. ["Move Row Up" org-metaup (org-at-table-p)]
  15883. ["Move Row Down" org-metadown (org-at-table-p)]
  15884. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  15885. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  15886. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  15887. "--"
  15888. ["Insert Hline" org-table-insert-hline (org-at-table-p)])
  15889. ("Rectangle"
  15890. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  15891. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  15892. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  15893. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  15894. "--"
  15895. ("Calculate"
  15896. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  15897. ["Set Named Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  15898. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  15899. "--"
  15900. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  15901. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  15902. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  15903. "--"
  15904. ["Sum Column/Rectangle" org-table-sum
  15905. (or (org-at-table-p) (org-region-active-p))]
  15906. ["Which Column?" org-table-current-column (org-at-table-p)])
  15907. ["Debug Formulas"
  15908. (setq org-table-formula-debug (not org-table-formula-debug))
  15909. :style toggle :selected org-table-formula-debug]
  15910. "--"
  15911. ["Create" org-table-create (and (not (org-at-table-p))
  15912. org-enable-table-editor)]
  15913. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  15914. ["Import from File" org-table-import (not (org-at-table-p))]
  15915. ["Export to File" org-table-export (org-at-table-p)]
  15916. "--"
  15917. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  15918. (easy-menu-define org-org-menu org-mode-map "Org menu"
  15919. '("Org"
  15920. ("Show/Hide"
  15921. ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
  15922. ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
  15923. ["Sparse Tree" org-occur t]
  15924. ["Reveal Context" org-reveal t]
  15925. ["Show All" show-all t])
  15926. "--"
  15927. ["New Heading" org-insert-heading t]
  15928. ("Navigate Headings"
  15929. ["Up" outline-up-heading t]
  15930. ["Next" outline-next-visible-heading t]
  15931. ["Previous" outline-previous-visible-heading t]
  15932. ["Next Same Level" outline-forward-same-level t]
  15933. ["Previous Same Level" outline-backward-same-level t]
  15934. "--"
  15935. ["Jump" org-goto t])
  15936. ("Edit Structure"
  15937. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  15938. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  15939. "--"
  15940. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  15941. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  15942. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  15943. "--"
  15944. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  15945. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  15946. ["Demote Heading" org-metaright (not (org-at-table-p))]
  15947. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  15948. "--"
  15949. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  15950. "--"
  15951. ["Convert to odd levels" org-convert-to-odd-levels t]
  15952. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  15953. ("Archive"
  15954. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  15955. ["Check and Tag Children" (org-toggle-archive-tag (4))
  15956. :active t :keys "C-u C-c C-x C-a"]
  15957. ["Sparse trees open ARCHIVE trees"
  15958. (setq org-sparse-tree-open-archived-trees
  15959. (not org-sparse-tree-open-archived-trees))
  15960. :style toggle :selected org-sparse-tree-open-archived-trees]
  15961. ["Cycling opens ARCHIVE trees"
  15962. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  15963. :style toggle :selected org-cycle-open-archived-trees]
  15964. ["Agenda includes ARCHIVE trees"
  15965. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  15966. :style toggle :selected (not org-agenda-skip-archived-trees)]
  15967. "--"
  15968. ["Move Subtree to Archive" org-archive-subtree t]
  15969. ["Check and Move Children" (org-archive-subtree '(4))
  15970. :active t :keys "C-u C-c $"])
  15971. "--"
  15972. ("TODO Lists"
  15973. ["TODO/DONE/-" org-todo t]
  15974. ("Select keyword"
  15975. ["Next keyword" org-shiftright (org-on-heading-p)]
  15976. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  15977. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))])
  15978. ["Show TODO Tree" org-show-todo-tree t]
  15979. ["Global TODO list" org-todo-list t]
  15980. "--"
  15981. ["Set Priority" org-priority t]
  15982. ["Priority Up" org-shiftup t]
  15983. ["Priority Down" org-shiftdown t]
  15984. "--"
  15985. ; ["Insert Checkbox" org-insert-todo-heading (org-in-item-p)]
  15986. ; ["Toggle Checkbox" org-ctrl-c-ctrl-c (org-at-item-checkbox-p)]
  15987. ; ["Insert [n/m] cookie" (progn (insert "[/]") (org-update-checkbox-count))
  15988. ; (or (org-on-heading-p) (org-at-item-p))]
  15989. ; ["Insert [%] cookie" (progn (insert "[%]") (org-update-checkbox-count))
  15990. ; (or (org-on-heading-p) (org-at-item-p))]
  15991. ; ["Update Statistics" org-update-checkbox-count t]
  15992. )
  15993. ("Dates and Scheduling"
  15994. ["Timestamp" org-time-stamp t]
  15995. ["Timestamp (inactive)" org-time-stamp-inactive t]
  15996. ("Change Date"
  15997. ["1 Day Later" org-shiftright t]
  15998. ["1 Day Earlier" org-shiftleft t]
  15999. ["1 ... Later" org-shiftup t]
  16000. ["1 ... Earlier" org-shiftdown t])
  16001. ["Compute Time Range" org-evaluate-time-range t]
  16002. ["Schedule Item" org-schedule t]
  16003. ["Deadline" org-deadline t]
  16004. "--"
  16005. ["Custom time format" org-toggle-time-stamp-overlays
  16006. :style radio :selected org-display-custom-times]
  16007. "--"
  16008. ["Goto Calendar" org-goto-calendar t]
  16009. ["Date from Calendar" org-date-from-calendar t])
  16010. ("Logging work"
  16011. ["Clock in" org-clock-in t]
  16012. ["Clock out" org-clock-out t]
  16013. ["Clock cancel" org-clock-cancel t]
  16014. ["Display times" org-clock-display t]
  16015. ["Create clock table" org-clock-report t]
  16016. "--"
  16017. ["Record DONE time"
  16018. (progn (setq org-log-done (not org-log-done))
  16019. (message "Switching to %s will %s record a timestamp"
  16020. org-done-string
  16021. (if org-log-done "automatically" "not")))
  16022. :style toggle :selected org-log-done])
  16023. "--"
  16024. ["Agenda Command..." org-agenda t]
  16025. ("File List for Agenda")
  16026. ("Special views current file"
  16027. ["TODO Tree" org-show-todo-tree t]
  16028. ["Check Deadlines" org-check-deadlines t]
  16029. ["Timeline" org-timeline t]
  16030. ["Tags Tree" org-tags-sparse-tree t])
  16031. "--"
  16032. ("Hyperlinks"
  16033. ["Store Link (Global)" org-store-link t]
  16034. ["Insert Link" org-insert-link t]
  16035. ["Follow Link" org-open-at-point t]
  16036. "--"
  16037. ["Descriptive Links"
  16038. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  16039. :style radio :selected (member '(org-link) buffer-invisibility-spec)]
  16040. ["Literal Links"
  16041. (progn
  16042. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  16043. :style radio :selected (not (member '(org-link) buffer-invisibility-spec))]
  16044. "--"
  16045. ["Upgrade all <link> to [[link][desc]]" org-upgrade-old-links
  16046. (save-excursion (goto-char (point-min))
  16047. (re-search-forward "<[a-z]+:" nil t))])
  16048. "--"
  16049. ["Export/Publish..." org-export t]
  16050. ("LaTeX"
  16051. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  16052. :selected org-cdlatex-mode]
  16053. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  16054. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  16055. ["Modify math symbol" org-cdlatex-math-modify
  16056. (org-inside-LaTeX-fragment-p)]
  16057. ["Export LaTeX fragments as images"
  16058. (setq org-export-with-LaTeX-fragments (not org-export-with-LaTeX-fragments))
  16059. :style toggle :selected org-export-with-LaTeX-fragments])
  16060. "--"
  16061. ("Documentation"
  16062. ["Show Version" org-version t]
  16063. ["Info Documentation" org-info t])
  16064. ("Customize"
  16065. ["Browse Org Group" org-customize t]
  16066. "--"
  16067. ["Expand This Menu" org-create-customize-menu
  16068. (fboundp 'customize-menu-create)])
  16069. "--"
  16070. ["Refresh setup" org-mode-restart t]
  16071. ))
  16072. (defun org-info (&optional node)
  16073. "Read documentation for Org-mode in the info system.
  16074. With optional NODE, go directly to that node."
  16075. (interactive)
  16076. (require 'info)
  16077. (Info-goto-node (format "(org)%s" (or node ""))))
  16078. (defun org-install-agenda-files-menu ()
  16079. (let ((bl (buffer-list)))
  16080. (save-excursion
  16081. (while bl
  16082. (set-buffer (pop bl))
  16083. (if (org-mode-p) (setq bl nil)))
  16084. (when (org-mode-p)
  16085. (easy-menu-change
  16086. '("Org") "File List for Agenda"
  16087. (append
  16088. (list
  16089. ["Edit File List" (org-edit-agenda-file-list) t]
  16090. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  16091. ["Remove Current File from List" org-remove-file t]
  16092. ["Cycle through agenda files" org-cycle-agenda-files t]
  16093. "--")
  16094. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  16095. ;;; Documentation
  16096. (defun org-customize ()
  16097. "Call the customize function with org as argument."
  16098. (interactive)
  16099. (customize-browse 'org))
  16100. (defun org-create-customize-menu ()
  16101. "Create a full customization menu for Org-mode, insert it into the menu."
  16102. (interactive)
  16103. (if (fboundp 'customize-menu-create)
  16104. (progn
  16105. (easy-menu-change
  16106. '("Org") "Customize"
  16107. `(["Browse Org group" org-customize t]
  16108. "--"
  16109. ,(customize-menu-create 'org)
  16110. ["Set" Custom-set t]
  16111. ["Save" Custom-save t]
  16112. ["Reset to Current" Custom-reset-current t]
  16113. ["Reset to Saved" Custom-reset-saved t]
  16114. ["Reset to Standard Settings" Custom-reset-standard t]))
  16115. (message "\"Org\"-menu now contains full customization menu"))
  16116. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  16117. ;;; Miscellaneous stuff
  16118. (defun org-context ()
  16119. "Return a list of contexts of the current cursor position.
  16120. If several contexts apply, all are returned.
  16121. Each context entry is a list with a symbol naming the context, and
  16122. two positions indicating start and end of the context. Possible
  16123. contexts are:
  16124. :headline anywhere in a headline
  16125. :headline-stars on the leading stars in a headline
  16126. :todo-keyword on a TODO keyword (including DONE) in a headline
  16127. :tags on the TAGS in a headline
  16128. :priority on the priority cookie in a headline
  16129. :item on the first line of a plain list item
  16130. :item-bullet on the bullet/number of a plain list item
  16131. :checkbox on the checkbox in a plain list item
  16132. :table in an org-mode table
  16133. :table-special on a special filed in a table
  16134. :table-table in a table.el table
  16135. :link on a hyperline
  16136. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  16137. :target on a <<target>>
  16138. :radio-target on a <<<radio-target>>>
  16139. :latex-fragment on a LaTeX fragment
  16140. :latex-preview on a LaTeX fragment with overlayed preview image
  16141. This function expects the position to be visible because it uses font-lock
  16142. faces as a help to recognize the following contexts: :table-special, :link,
  16143. and :keyword."
  16144. (let* ((f (get-text-property (point) 'face))
  16145. (faces (if (listp f) f (list f)))
  16146. (p (point)) clist o)
  16147. ;; First the large context
  16148. (cond
  16149. ((org-on-heading-p)
  16150. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  16151. (when (progn
  16152. (beginning-of-line 1)
  16153. (looking-at org-todo-line-tags-regexp))
  16154. (push (org-point-in-group p 1 :headline-stars) clist)
  16155. (push (org-point-in-group p 2 :todo-keyword) clist)
  16156. (push (org-point-in-group p 4 :tags) clist))
  16157. (goto-char p)
  16158. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  16159. (if (looking-at "\\[#[A-Z]\\]")
  16160. (push (org-point-in-group p 0 :priority) clist)))
  16161. ((org-at-item-p)
  16162. (push (org-point-in-group p 2 :item-bullet) clist)
  16163. (push (list :item (point-at-bol)
  16164. (save-excursion (org-end-of-item) (point)))
  16165. clist)
  16166. (and (org-at-item-checkbox-p)
  16167. (push (org-point-in-group p 0 :checkbox) clist)))
  16168. ((org-at-table-p)
  16169. (push (list :table (org-table-begin) (org-table-end)) clist)
  16170. (if (memq 'org-formula faces)
  16171. (push (list :table-special
  16172. (previous-single-property-change p 'face)
  16173. (next-single-property-change p 'face)) clist)))
  16174. ((org-at-table-p 'any)
  16175. (push (list :table-table) clist)))
  16176. (goto-char p)
  16177. ;; Now the small context
  16178. (cond
  16179. ((org-at-timestamp-p)
  16180. (push (org-point-in-group p 0 :timestamp) clist))
  16181. ((memq 'org-link faces)
  16182. (push (list :link
  16183. (previous-single-property-change p 'face)
  16184. (next-single-property-change p 'face)) clist))
  16185. ((memq 'org-special-keyword faces)
  16186. (push (list :keyword
  16187. (previous-single-property-change p 'face)
  16188. (next-single-property-change p 'face)) clist))
  16189. ((org-on-target-p)
  16190. (push (org-point-in-group p 0 :target) clist)
  16191. (goto-char (1- (match-beginning 0)))
  16192. (if (looking-at org-radio-target-regexp)
  16193. (push (org-point-in-group p 0 :radio-target) clist))
  16194. (goto-char p))
  16195. ((setq o (car (delq nil
  16196. (mapcar
  16197. (lambda (x)
  16198. (if (memq x org-latex-fragment-image-overlays) x))
  16199. (org-overlays-at (point))))))
  16200. (push (list :latex-fragment
  16201. (org-overlay-start o) (org-overlay-end o)) clist)
  16202. (push (list :latex-preview
  16203. (org-overlay-start o) (org-overlay-end o)) clist))
  16204. ((org-inside-LaTeX-fragment-p)
  16205. ;; FIXME: positions wrong.
  16206. (push (list :latex-fragment (point) (point)) clist)))
  16207. (setq clist (nreverse (delq nil clist)))
  16208. clist))
  16209. (defun org-point-in-group (point group &optional context)
  16210. "Check if POINT is in match-group GROUP.
  16211. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  16212. match. If the match group does ot exist or point is not inside it,
  16213. return nil."
  16214. (and (match-beginning group)
  16215. (>= point (match-beginning group))
  16216. (<= point (match-end group))
  16217. (if context
  16218. (list context (match-beginning group) (match-end group))
  16219. t)))
  16220. (defun org-move-line-down (arg)
  16221. "Move the current line down. With prefix argument, move it past ARG lines."
  16222. (interactive "p")
  16223. (let ((col (current-column))
  16224. beg end pos)
  16225. (beginning-of-line 1) (setq beg (point))
  16226. (beginning-of-line 2) (setq end (point))
  16227. (beginning-of-line (+ 1 arg))
  16228. (setq pos (move-marker (make-marker) (point)))
  16229. (insert (delete-and-extract-region beg end))
  16230. (goto-char pos)
  16231. (move-to-column col)))
  16232. (defun org-move-line-up (arg)
  16233. "Move the current line up. With prefix argument, move it past ARG lines."
  16234. (interactive "p")
  16235. (let ((col (current-column))
  16236. beg end pos)
  16237. (beginning-of-line 1) (setq beg (point))
  16238. (beginning-of-line 2) (setq end (point))
  16239. (beginning-of-line (- arg))
  16240. (setq pos (move-marker (make-marker) (point)))
  16241. (insert (delete-and-extract-region beg end))
  16242. (goto-char pos)
  16243. (move-to-column col)))
  16244. (defun org-replace-escapes (string table)
  16245. ;; FIXME: document and make safer
  16246. (let (e)
  16247. (while (setq e (pop table))
  16248. (while (string-match (car e) string)
  16249. (setq string (replace-match (cdr e) t t string))))
  16250. string))
  16251. ;; Paragraph filling stuff.
  16252. ;; We want this to be just right, so use the full arsenal.
  16253. (defun org-set-autofill-regexps ()
  16254. (interactive)
  16255. ;; In the paragraph separator we include headlines, because filling
  16256. ;; text in a line directly attached to a headline would otherwise
  16257. ;; fill the headline as well.
  16258. (org-set-local 'comment-start-skip "^#+[ \t]*")
  16259. (org-set-local 'paragraph-separate "\f\\|\\*\\|[ ]*$\\|[ \t]*[:|]")
  16260. ;; The paragraph starter includes hand-formatted lists.
  16261. (org-set-local 'paragraph-start
  16262. "\f\\|[ ]*$\\|\\([*\f]+\\)\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  16263. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  16264. ;; But only if the user has not turned off tables or fixed-width regions
  16265. (org-set-local
  16266. 'auto-fill-inhibit-regexp
  16267. (concat "\\*\\|#\\+"
  16268. "\\|[ \t]*" org-keyword-time-regexp
  16269. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  16270. (concat
  16271. "\\|[ \t]*["
  16272. (if org-enable-table-editor "|" "")
  16273. (if org-enable-fixed-width-editor ":" "")
  16274. "]"))))
  16275. ;; We use our own fill-paragraph function, to make sure that tables
  16276. ;; and fixed-width regions are not wrapped. That function will pass
  16277. ;; through to `fill-paragraph' when appropriate.
  16278. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  16279. ; Adaptive filling: To get full control, first make sure that
  16280. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  16281. (org-set-local 'adaptive-fill-regexp "\000")
  16282. (org-set-local 'adaptive-fill-function
  16283. 'org-adaptive-fill-function))
  16284. (defun org-fill-paragraph (&optional justify)
  16285. "Re-align a table, pass through to fill-paragraph if no table."
  16286. (let ((table-p (org-at-table-p))
  16287. (table.el-p (org-at-table.el-p)))
  16288. (cond ((equal (char-after (point-at-bol)) ?*) t) ; skip headlines
  16289. (table.el-p t) ; skip table.el tables
  16290. (table-p (org-table-align) t) ; align org-mode tables
  16291. (t nil)))) ; call paragraph-fill
  16292. ;; For reference, this is the default value of adaptive-fill-regexp
  16293. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  16294. (defun org-adaptive-fill-function ()
  16295. "Return a fill prefix for org-mode files.
  16296. In particular, this makes sure hanging paragraphs for hand-formatted lists
  16297. work correctly."
  16298. ;;FIXME (if (looking-at " *\\([-*+] \\|[0-9]+[.)] \\)?")
  16299. (if (looking-at " *\\([-*+] \\|[0-9]+[.)] \\)?\|#\\+[ \t]+")
  16300. (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  16301. ;; Functions needed for Emacs/XEmacs region compatibility
  16302. (defun org-add-hook (hook function &optional append local)
  16303. "Add-hook, compatible with both Emacsen."
  16304. (if (and local (featurep 'xemacs))
  16305. (add-local-hook hook function append)
  16306. (add-hook hook function append local)))
  16307. (defun org-region-active-p ()
  16308. "Is `transient-mark-mode' on and the region active?
  16309. Works on both Emacs and XEmacs."
  16310. (if org-ignore-region
  16311. nil
  16312. (if (featurep 'xemacs)
  16313. (and zmacs-regions (region-active-p))
  16314. (and transient-mark-mode mark-active))))
  16315. (defun org-add-to-invisibility-spec (arg)
  16316. "Add elements to `buffer-invisibility-spec'.
  16317. See documentation for `buffer-invisibility-spec' for the kind of elements
  16318. that can be added."
  16319. (cond
  16320. ((fboundp 'add-to-invisibility-spec)
  16321. (add-to-invisibility-spec arg))
  16322. ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
  16323. (setq buffer-invisibility-spec (list arg)))
  16324. (t
  16325. (setq buffer-invisibility-spec
  16326. (cons arg buffer-invisibility-spec)))))
  16327. (defun org-remove-from-invisibility-spec (arg)
  16328. "Remove elements from `buffer-invisibility-spec'."
  16329. (if (fboundp 'remove-from-invisibility-spec)
  16330. (remove-from-invisibility-spec arg)
  16331. (if (consp buffer-invisibility-spec)
  16332. (setq buffer-invisibility-spec
  16333. (delete arg buffer-invisibility-spec)))))
  16334. (defun org-in-invisibility-spec-p (arg)
  16335. "Is ARG a member of `buffer-invisibility-spec'?"
  16336. (if (consp buffer-invisibility-spec)
  16337. (member arg buffer-invisibility-spec)
  16338. nil))
  16339. (defun org-image-file-name-regexp ()
  16340. "Return regexp matching the file names of images."
  16341. (if (fboundp 'image-file-name-regexp)
  16342. (image-file-name-regexp)
  16343. (let ((image-file-name-extensions
  16344. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  16345. "xbm" "xpm" "pbm" "pgm" "ppm")))
  16346. (concat "\\."
  16347. (regexp-opt (nconc (mapcar 'upcase
  16348. image-file-name-extensions)
  16349. image-file-name-extensions)
  16350. t)
  16351. "\\'"))))
  16352. ;; Functions extending outline functionality
  16353. ;; C-a should go to the beginning of a *visible* line, also in the
  16354. ;; new outline.el. I guess this should be patched into Emacs?
  16355. (defun org-beginning-of-line ()
  16356. "Go to the beginning of the current line. If that is invisible, continue
  16357. to a visible line beginning. This makes the function of C-a more intuitive."
  16358. (interactive)
  16359. (beginning-of-line 1)
  16360. (if (bobp)
  16361. nil
  16362. (backward-char 1)
  16363. (if (org-invisible-p)
  16364. (while (and (not (bobp)) (org-invisible-p))
  16365. (backward-char 1)
  16366. (beginning-of-line 1))
  16367. (forward-char 1))))
  16368. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  16369. (defun org-invisible-p ()
  16370. "Check if point is at a character currently not visible."
  16371. ;; Early versions of noutline don't have `outline-invisible-p'.
  16372. (if (fboundp 'outline-invisible-p)
  16373. (outline-invisible-p)
  16374. (get-char-property (point) 'invisible)))
  16375. (defun org-invisible-p2 ()
  16376. "Check if point is at a character currently not visible."
  16377. (save-excursion
  16378. (if (and (eolp) (not (bobp))) (backward-char 1))
  16379. ;; Early versions of noutline don't have `outline-invisible-p'.
  16380. (if (fboundp 'outline-invisible-p)
  16381. (outline-invisible-p)
  16382. (get-char-property (point) 'invisible))))
  16383. (defalias 'org-back-to-heading 'outline-back-to-heading)
  16384. (defalias 'org-on-heading-p 'outline-on-heading-p)
  16385. (defun org-on-target-p ()
  16386. (let ((pos (point)))
  16387. (save-excursion
  16388. (skip-chars-forward "<")
  16389. (and (re-search-backward "<<" nil t)
  16390. (or (looking-at org-radio-target-regexp)
  16391. (looking-at org-target-regexp))
  16392. (<= (match-beginning 0) pos)
  16393. (>= (1+ (match-end 0)) pos)))))
  16394. (defun org-up-heading-all (arg)
  16395. "Move to the heading line of which the present line is a subheading.
  16396. This function considers both visible and invisible heading lines.
  16397. With argument, move up ARG levels."
  16398. (if (fboundp 'outline-up-heading-all)
  16399. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  16400. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  16401. (defun org-goto-sibling (&optional previous)
  16402. "Goto the next sibling, even if it is invisible.
  16403. When PREVIOUS is set, go to the previous sibling instead. Returns t
  16404. when a sibling was found. When none is found, return nil and don't
  16405. move point."
  16406. (let ((fun (if previous 're-search-backward 're-search-forward))
  16407. (pos (point))
  16408. (re (concat "^" outline-regexp))
  16409. level l)
  16410. (org-back-to-heading t)
  16411. (setq level (funcall outline-level))
  16412. (catch 'exit
  16413. (or previous (forward-char 1))
  16414. (while (funcall fun re nil t)
  16415. (setq l (funcall outline-level))
  16416. (when (< l level) (goto-char pos) (throw 'exit nil))
  16417. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  16418. (goto-char pos)
  16419. nil)))
  16420. (defun org-show-hidden-entry ()
  16421. "Show an entry where even the heading is hidden."
  16422. (save-excursion
  16423. (org-show-entry)))
  16424. (defun org-flag-heading (flag &optional entry)
  16425. "Flag the current heading. FLAG non-nil means make invisible.
  16426. When ENTRY is non-nil, show the entire entry."
  16427. (save-excursion
  16428. (org-back-to-heading t)
  16429. ;; Check if we should show the entire entry
  16430. (if entry
  16431. (progn
  16432. (org-show-entry)
  16433. (save-excursion
  16434. (and (outline-next-heading)
  16435. (org-flag-heading nil))))
  16436. (outline-flag-region (max 1 (1- (point)))
  16437. (save-excursion (outline-end-of-heading) (point))
  16438. flag))))
  16439. (defun org-end-of-subtree (&optional invisible-OK)
  16440. ;; This is an exact copy of the original function, but it uses
  16441. ;; `org-back-to-heading', to make it work also in invisible
  16442. ;; trees. And is uses an invisible-OK argument.
  16443. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  16444. (org-back-to-heading invisible-OK)
  16445. (let ((first t)
  16446. (level (funcall outline-level)))
  16447. (while (and (not (eobp))
  16448. (or first (> (funcall outline-level) level)))
  16449. (setq first nil)
  16450. (outline-next-heading))
  16451. (if (memq (preceding-char) '(?\n ?\^M))
  16452. (progn
  16453. ;; Go to end of line before heading
  16454. (forward-char -1)
  16455. (if (memq (preceding-char) '(?\n ?\^M))
  16456. ;; leave blank line before heading
  16457. (forward-char -1)))))
  16458. (point))
  16459. (defun org-show-subtree ()
  16460. "Show everything after this heading at deeper levels."
  16461. (outline-flag-region
  16462. (point)
  16463. (save-excursion
  16464. (outline-end-of-subtree) (outline-next-heading) (point))
  16465. nil))
  16466. (defun org-show-entry ()
  16467. "Show the body directly following this heading.
  16468. Show the heading too, if it is currently invisible."
  16469. (interactive)
  16470. (save-excursion
  16471. (org-back-to-heading t)
  16472. (outline-flag-region
  16473. (max 1 (1- (point)))
  16474. (save-excursion
  16475. (re-search-forward (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  16476. (or (match-beginning 1) (point-max)))
  16477. nil)))
  16478. (defun org-make-options-regexp (kwds)
  16479. "Make a regular expression for keyword lines."
  16480. (concat
  16481. "^"
  16482. "#?[ \t]*\\+\\("
  16483. (mapconcat 'regexp-quote kwds "\\|")
  16484. "\\):[ \t]*"
  16485. "\\(.+\\)"))
  16486. ;; Make `bookmark-jump' show the jump location if it was hidden.
  16487. (eval-after-load "bookmark"
  16488. '(if (boundp 'bookmark-after-jump-hook)
  16489. ;; We can use the hook
  16490. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  16491. ;; Hook not available, use advice
  16492. (defadvice bookmark-jump (after org-make-visible activate)
  16493. "Make the position visible."
  16494. (org-bookmark-jump-unhide))))
  16495. (defun org-bookmark-jump-unhide ()
  16496. "Unhide the current position, to show the bookmark location."
  16497. (and (org-mode-p)
  16498. (or (org-invisible-p)
  16499. (save-excursion (goto-char (max (point-min) (1- (point))))
  16500. (org-invisible-p)))
  16501. (org-show-context 'bookmark-jump)))
  16502. ;; Make session.el ignore our circular variable
  16503. (eval-after-load "session"
  16504. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  16505. ;;; Experimental code
  16506. ;;; Finish up
  16507. (provide 'org)
  16508. (run-hooks 'org-load-hook)
  16509. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  16510. ;;; org.el ends here