org.el 565 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347
  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.36b
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software; you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation; either version 2, or (at your option)
  15. ;; any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  22. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  23. ;; Boston, MA 02110-1301, USA.
  24. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  25. ;;
  26. ;;; Commentary:
  27. ;;
  28. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  29. ;; project planning with a fast and effective plain-text system.
  30. ;;
  31. ;; Org-mode develops organizational tasks around NOTES files that contain
  32. ;; information about projects as plain text. Org-mode is implemented on
  33. ;; top of outline-mode, which makes it possible to keep the content of
  34. ;; large files well structured. Visibility cycling and structure editing
  35. ;; help to work with the tree. Tables are easily created with a built-in
  36. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  37. ;; and scheduling. It dynamically compiles entries into an agenda that
  38. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  39. ;; Plain text URL-like links connect to websites, emails, Usenet
  40. ;; messages, BBDB entries, and any files related to the projects. For
  41. ;; printing and sharing of notes, an Org-mode file can be exported as a
  42. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  43. ;; iCalendar file. It can also serve as a publishing tool for a set of
  44. ;; linked webpages.
  45. ;;
  46. ;;
  47. ;; Installation
  48. ;; ------------
  49. ;; If Org-mode is part of the Emacs distribution or an XEmacs package, you
  50. ;; only need to copy the following lines to your .emacs file. The last two
  51. ;; lines define *global* keys for the commands `org-store-link' and
  52. ;; `org-agenda' - please choose suitable keys yourself.
  53. ;;
  54. ;; (add-to-list 'auto-mode-alist '("\\.org$" . org-mode))
  55. ;; (define-key global-map "\C-cl" 'org-store-link)
  56. ;; (define-key global-map "\C-ca" 'org-agenda)
  57. ;;
  58. ;; Furthermore you need to activate font-lock-mode in org-mode buffers.
  59. ;; either of the following two lins will do the trick:
  60. ;;
  61. ;; (global-font-lock-mode 1) ; for all buffers
  62. ;; (add-hook 'org-mode-hook 'turn-on-font-lock) ; org-mode buffers only
  63. ;;
  64. ;; If you have downloaded Org-mode from the Web, you have to take additional
  65. ;; action: Byte-compile org.el and org-publish.el and put them together with
  66. ;; org-install.el on your load path. Then also add to your .emacs file:
  67. ;;
  68. ;; (require 'org-install)
  69. ;;
  70. ;;
  71. ;; Activation
  72. ;; ----------
  73. ;; The setup above will put all files with extension ".org" into Org-mode.
  74. ;; As an alternative, make the first line of a file look like this:
  75. ;;
  76. ;; MY PROJECTS -*- mode: org; -*-
  77. ;;
  78. ;; which will select Org-mode for this buffer no matter what the file's
  79. ;; name is.
  80. ;;
  81. ;; Documentation
  82. ;; -------------
  83. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  84. ;; distribution also contains a PDF version of it. At the homepage of
  85. ;; Org-mode, you can read the same text online as HTML. There is also an
  86. ;; excellent reference card made by Philip Rooke. This card can be found
  87. ;; in the etc/ directory of Emacs 22.
  88. ;;
  89. ;; Recent changes
  90. ;; --------------
  91. ;; Version 4.37
  92. ;; - Clock-feature for measuring time spent on specific items.
  93. ;; - Improved emphasizing allows configuration and stacking.
  94. ;;
  95. ;; Version 4.36
  96. ;; - Improved indentation of ASCII export, when headlines become items.
  97. ;; - Handling of 12am and 12pm fixed. Times beyond 24:00 can be used
  98. ;; and will not lead to conflicts.
  99. ;; - Support for mutually exclusive TAGS with the fast tags interface.
  100. ;; - Bug fixes.
  101. ;;
  102. ;; Version 4.35
  103. ;; - HTML export is now valid XHTML.
  104. ;; - Timeline can also show dates without entries. See new option
  105. ;; `org-timeline-show-empty-dates'.
  106. ;; - The bullets created by the ASCII exporter can now be configured.
  107. ;; See the new option `org-export-ascii-bullets'.
  108. ;; - New face `org-upcoming-deadline' (was `org-scheduled-previously').
  109. ;; - New function `org-context' to allow testing for local context.
  110. ;;
  111. ;; Version 4.34
  112. ;; - Bug fixes.
  113. ;;
  114. ;; Version 4.33
  115. ;; - New commands to move through plain lists: S-up and S-down.
  116. ;; - Bug fixes and documentation update.
  117. ;;
  118. ;; Version 4.32
  119. ;; - Fast (single-key-per-tag) interface for setting TAGS.
  120. ;; - The list of legal tags can be configured globally and locally.
  121. ;; - Elisp and Info links (thanks to Todd Neal).
  122. ;; - `org-export-publishing-directory' can be an alist, with different
  123. ;; directories for different export types.
  124. ;; - All context-sensitive commands use `call-interactively' to dispatch.
  125. ;; - `org-confirm-shell-links' renamed to `org-confirm-shell-link-function'.
  126. ;; - Bug fixes.
  127. ;;
  128. ;; Version 4.31
  129. ;; - Bug fixes.
  130. ;;
  131. ;; Version 4.30
  132. ;; - Modified installation: Autoloads have been collected in org-install.el.
  133. ;; - Logging (org-log-done) is now a #+STARTUP option.
  134. ;; - Checkboxes in plain list items, following up on Frank Ruell's idea.
  135. ;; - File links inserted with C-c C-l will use relative paths if the linked
  136. ;; file is in the current directory or a subdirectory of it.
  137. ;; - New variable `org-link-file-path-type' to specify preference for
  138. ;; relative and absolute paths.
  139. ;; - New CSS classes for tags, timestamps, timestamp keywords.
  140. ;; - Bug and typo fixes.
  141. ;;
  142. ;; Version 4.29
  143. ;; - Inlining images in HTML export now depends on wheather the link
  144. ;; contains a description or not.
  145. ;; - TODO items can be scheduled from the global TODO list using C-c C-s.
  146. ;; - TODO items already scheduled can be made to disappear from the global
  147. ;; todo list, see `org-agenda-todo-ignore-scheduled'.
  148. ;; - In Tables, formulas may also be Lisp forms.
  149. ;; - Exporting the visible part of an outline with `C-c C-x v' works now
  150. ;; for all available exporters.
  151. ;; - Bug fixes, lots of them :-(
  152. ;;
  153. ;; Version 4.28
  154. ;; - Bug fixes.
  155. ;;
  156. ;; Version 4.27
  157. ;; - HTML exporter generalized to receive external options.
  158. ;; As part of the process, author, email and date have been moved to the
  159. ;; end of the HTML file.
  160. ;; - Support for customizable file search in file links.
  161. ;; - BibTeX database links as first application of the above.
  162. ;; - New option `org-agenda-todo-list-sublevels' to turn off listing TODO
  163. ;; entries that are sublevels of another TODO entry.
  164. ;;
  165. ;;
  166. ;;; Code:
  167. (eval-when-compile
  168. (require 'cl)
  169. (require 'calendar))
  170. (require 'outline)
  171. (require 'time-date)
  172. (require 'easymenu)
  173. ;;; Customization variables
  174. (defvar org-version "4.36b"
  175. "The version number of the file org.el.")
  176. (defun org-version ()
  177. (interactive)
  178. (message "Org-mode version %s" org-version))
  179. ;; The following constant is for compatibility with different versions
  180. ;; of outline.el.
  181. (defconst org-noutline-p (featurep 'noutline)
  182. "Are we using the new outline mode?")
  183. (defconst org-xemacs-p (featurep 'xemacs)) ; not used by org.el itself
  184. (defconst org-format-transports-properties-p
  185. (let ((x "a"))
  186. (add-text-properties 0 1 '(test t) x)
  187. (get-text-property 0 'test (format "%s" x)))
  188. "Does format transport text properties?")
  189. (defgroup org nil
  190. "Outline-based notes management and organizer."
  191. :tag "Org"
  192. :group 'outlines
  193. :group 'hypermedia
  194. :group 'calendar)
  195. (defgroup org-startup nil
  196. "Options concerning startup of Org-mode."
  197. :tag "Org Startup"
  198. :group 'org)
  199. (defcustom org-startup-folded t
  200. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  201. This can also be configured on a per-file basis by adding one of
  202. the following lines anywhere in the buffer:
  203. #+STARTUP: fold
  204. #+STARTUP: nofold
  205. #+STARTUP: content"
  206. :group 'org-startup
  207. :type '(choice
  208. (const :tag "nofold: show all" nil)
  209. (const :tag "fold: overview" t)
  210. (const :tag "content: all headlines" content)))
  211. (defcustom org-startup-truncated t
  212. "Non-nil means, entering Org-mode will set `truncate-lines'.
  213. This is useful since some lines containing links can be very long and
  214. uninteresting. Also tables look terrible when wrapped."
  215. :group 'org-startup
  216. :type 'boolean)
  217. (defcustom org-startup-align-all-tables nil
  218. "Non-nil means, align all tables when visiting a file.
  219. This is useful when the column width in tables is forced with <N> cookies
  220. in table fields. Such tables will look correct only after the first re-align.
  221. This can also be configured on a per-file basis by adding one of
  222. the following lines anywhere in the buffer:
  223. #+STARTUP: align
  224. #+STARTUP: noalign"
  225. :group 'org-startup
  226. :type 'boolean)
  227. (defcustom org-startup-with-deadline-check nil
  228. "Non-nil means, entering Org-mode will run the deadline check.
  229. This means, if you start editing an org file, you will get an
  230. immediate reminder of any due deadlines.
  231. This can also be configured on a per-file basis by adding one of
  232. the following lines anywhere in the buffer:
  233. #+STARTUP: dlcheck
  234. #+STARTUP: nodlcheck"
  235. :group 'org-startup
  236. :type 'boolean)
  237. (defcustom org-insert-mode-line-in-empty-file nil
  238. "Non-nil means insert the first line setting Org-mode in empty files.
  239. When the function `org-mode' is called interactively in an empty file, this
  240. normally means that the file name does not automatically trigger Org-mode.
  241. To ensure that the file will always be in Org-mode in the future, a
  242. line enforcing Org-mode will be inserted into the buffer, if this option
  243. has been set."
  244. :group 'org-startup
  245. :type 'boolean)
  246. (defcustom org-CUA-compatible nil
  247. "Non-nil means use alternative key bindings for S-<cursor movement>.
  248. Org-mode used S-<cursor movement> for changing timestamps and priorities.
  249. S-<cursor movement> is also used for example by `CUA-mode' to select text.
  250. If you want to use Org-mode together with `CUA-mode', Org-mode needs to use
  251. alternative bindings. Setting this variable to t will replace the following
  252. keys both in Org-mode and in the Org-agenda buffer.
  253. S-RET -> C-S-RET
  254. S-up -> M-p
  255. S-down -> M-n
  256. S-left -> M--
  257. S-right -> M-+
  258. If you do not like the alternative keys, take a look at the variable
  259. `org-disputed-keys'.
  260. This option is only relevant at load-time of Org-mode. Changing it requires
  261. a restart of Emacs to become effective."
  262. :group 'org-startup
  263. :type 'boolean)
  264. (defvar org-disputed-keys
  265. '((S-up [(shift up)] [(meta ?p)])
  266. (S-down [(shift down)] [(meta ?n)])
  267. (S-left [(shift left)] [(meta ?-)])
  268. (S-right [(shift right)] [(meta ?+)])
  269. (S-return [(shift return)] [(control shift return)]))
  270. "Keys for which Org-mode and other modes compete.
  271. This is an alist, cars are symbols for lookup, 1st element is the default key,
  272. second element will be used when `org-CUA-compatible' is t.")
  273. (defun org-key (key)
  274. "Select a key according to `org-CUA-compatible'."
  275. (nth (if org-CUA-compatible 2 1)
  276. (or (assq key org-disputed-keys)
  277. (error "Invalid Key %s in `org-key'" key))))
  278. (defcustom org-ellipsis nil
  279. "The ellipsis to use in the Org-mode outline.
  280. When nil, just use the standard three dots. When a string, use that instead,
  281. and just in Org-mode (which will then use its own display table).
  282. Changing this requires executing `M-x org-mode' in a buffer to become
  283. effective."
  284. :group 'org-startup
  285. :type '(choice (const :tag "Default" nil)
  286. (string :tag "String" :value "...#")))
  287. (defvar org-display-table nil
  288. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  289. (defgroup org-keywords nil
  290. "Keywords in Org-mode."
  291. :tag "Org Keywords"
  292. :group 'org)
  293. (defcustom org-deadline-string "DEADLINE:"
  294. "String to mark deadline entries.
  295. A deadline is this string, followed by a time stamp. Should be a word,
  296. terminated by a colon. You can insert a schedule keyword and
  297. a timestamp with \\[org-deadline].
  298. Changes become only effective after restarting Emacs."
  299. :group 'org-keywords
  300. :type 'string)
  301. (defcustom org-scheduled-string "SCHEDULED:"
  302. "String to mark scheduled TODO entries.
  303. A schedule is this string, followed by a time stamp. Should be a word,
  304. terminated by a colon. You can insert a schedule keyword and
  305. a timestamp with \\[org-schedule].
  306. Changes become only effective after restarting Emacs."
  307. :group 'org-keywords
  308. :type 'string)
  309. (defcustom org-closed-string "CLOSED:"
  310. "String used as the prefix for timestamps logging closing a TODO entry."
  311. :group 'org-keywords
  312. :type 'string)
  313. (defcustom org-clock-string "CLOCK:"
  314. "String used as prefix for timestamps clocking work hours on an item."
  315. :group 'org-keywords
  316. :type 'string)
  317. (defcustom org-comment-string "COMMENT"
  318. "Entries starting with this keyword will never be exported.
  319. An entry can be toggled between COMMENT and normal with
  320. \\[org-toggle-comment].
  321. Changes become only effective after restarting Emacs."
  322. :group 'org-keywords
  323. :type 'string)
  324. (defcustom org-quote-string "QUOTE"
  325. "Entries starting with this keyword will be exported in fixed-width font.
  326. Quoting applies only to the text in the entry following the headline, and does
  327. not extend beyond the next headline, even if that is lower level.
  328. An entry can be toggled between QUOTE and normal with
  329. \\[org-toggle-fixed-width-section]."
  330. :group 'org-keywords
  331. :type 'string)
  332. (defgroup org-structure nil
  333. "Options concerning the general structure of Org-mode files."
  334. :tag "Org Structure"
  335. :group 'org)
  336. (defgroup org-cycle nil
  337. "Options concerning visibility cycling in Org-mode."
  338. :tag "Org Cycle"
  339. :group 'org-structure)
  340. (defcustom org-cycle-global-at-bob t
  341. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  342. This makes it possible to do global cycling without having to use S-TAB or
  343. C-u TAB. For this special case to work, the first line of the buffer
  344. must not be a headline - it may be empty ot some other text. When used in
  345. this way, `org-cycle-hook' is disables temporarily, to make sure the
  346. cursor stays at the beginning of the buffer.
  347. When this option is nil, don't do anything special at the beginning
  348. of the buffer."
  349. :group 'org-cycle
  350. :type 'boolean)
  351. (defcustom org-cycle-emulate-tab t
  352. "Where should `org-cycle' emulate TAB.
  353. nil Never
  354. white Only in completely white lines
  355. whitestart Only at the beginning of lines, before the first non-white char.
  356. t Everywhere except in headlines
  357. If TAB is used in a place where it does not emulate TAB, the current subtree
  358. visibility is cycled."
  359. :group 'org-cycle
  360. :type '(choice (const :tag "Never" nil)
  361. (const :tag "Only in completely white lines" white)
  362. (const :tag "Before first char in a line" whitestart)
  363. (const :tag "Everywhere except in headlines" t)
  364. ))
  365. (defcustom org-cycle-hook '(org-optimize-window-after-visibility-change)
  366. "Hook that is run after `org-cycle' has changed the buffer visibility.
  367. The function(s) in this hook must accept a single argument which indicates
  368. the new state that was set by the most recent `org-cycle' command. The
  369. argument is a symbol. After a global state change, it can have the values
  370. `overview', `content', or `all'. After a local state change, it can have
  371. the values `folded', `children', or `subtree'."
  372. :group 'org-cycle
  373. :type 'hook)
  374. (defgroup org-edit-structure nil
  375. "Options concerning structure editing in Org-mode."
  376. :tag "Org Edit Structure"
  377. :group 'org-structure)
  378. (defcustom org-odd-levels-only nil
  379. "Non-nil means, skip even levels and only use odd levels for the outline.
  380. This has the effect that two stars are being added/taken away in
  381. promotion/demotion commands. It also influences how levels are
  382. handled by the exporters.
  383. Changing it requires restart of `font-lock-mode' to become effective
  384. for fontification also in regions already fontified.
  385. You may also set this on a per-file basis by adding one of the following
  386. lines to the buffer:
  387. #+STARTUP: odd
  388. #+STARTUP: oddeven"
  389. :group 'org-edit-structure
  390. :group 'org-font-lock
  391. :type 'boolean)
  392. (defcustom org-adapt-indentation t
  393. "Non-nil means, adapt indentation when promoting and demoting.
  394. When this is set and the *entire* text in an entry is indented, the
  395. indentation is increased by one space in a demotion command, and
  396. decreased by one in a promotion command. If any line in the entry
  397. body starts at column 0, indentation is not changed at all."
  398. :group 'org-edit-structure
  399. :type 'boolean)
  400. (defcustom org-insert-heading-hook nil
  401. "Hook being run after inserting a new heading."
  402. :group 'org-edit-structure
  403. :type 'boolean)
  404. (defcustom org-enable-fixed-width-editor t
  405. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  406. This currently only means, they are never auto-wrapped.
  407. When nil, such lines will be treated like ordinary lines.
  408. See also the QUOTE keyword."
  409. :group 'org-edit-structure
  410. :type 'boolean)
  411. (defgroup org-sparse-trees nil
  412. "Options concerning sparse trees in Org-mode."
  413. :tag "Org Sparse Trees"
  414. :group 'org-structure)
  415. (defcustom org-highlight-sparse-tree-matches t
  416. "Non-nil means, highlight all matches that define a sparse tree.
  417. The highlights will automatically disappear the next time the buffer is
  418. changed by an edit command."
  419. :group 'org-sparse-trees
  420. :type 'boolean)
  421. (defcustom org-show-hierarchy-above t
  422. "Non-nil means, show full hierarchy when showing a spot in the tree.
  423. Turning this off makes sparse trees more compact, but also less clear."
  424. :group 'org-sparse-trees
  425. :type 'boolean)
  426. (defcustom org-show-following-heading t
  427. "Non-nil means, show heading following match in `org-occur'.
  428. When doing an `org-occur' it is useful to show the headline which
  429. follows the match, even if they do not match the regexp. This makes it
  430. easier to edit directly inside the sparse tree. However, if you use
  431. `org-occur' mainly as an overview, the following headlines are
  432. unnecessary clutter."
  433. :group 'org-sparse-trees
  434. :type 'boolean)
  435. (defcustom org-occur-hook '(org-first-headline-recenter)
  436. "Hook that is run after `org-occur' has constructed a sparse tree.
  437. This can be used to recenter the window to show as much of the structure
  438. as possible."
  439. :group 'org-sparse-trees
  440. :type 'hook)
  441. (defgroup org-plain-lists nil
  442. "Options concerning plain lists in Org-mode."
  443. :tag "Org Plain lists"
  444. :group 'org-structure)
  445. (defcustom org-cycle-include-plain-lists nil
  446. "Non-nil means, include plain lists into visibility cycling.
  447. This means that during cycling, plain list items will *temporarily* be
  448. interpreted as outline headlines with a level given by 1000+i where i is the
  449. indentation of the bullet. In all other operations, plain list items are
  450. not seen as headlines. For example, you cannot assign a TODO keyword to
  451. such an item."
  452. :group 'org-plain-lists
  453. :type 'boolean)
  454. (defcustom org-plain-list-ordered-item-terminator t
  455. "The character that makes a line with leading number an ordered list item.
  456. Valid values are ?. and ?\). To get both terminators, use t. While
  457. ?. may look nicer, it creates the danger that a line with leading
  458. number may be incorrectly interpreted as an item. ?\) therefore is
  459. the safe choice."
  460. :group 'org-plain-lists
  461. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  462. (const :tag "paren like in \"2)\"" ?\))
  463. (const :tab "both" t)))
  464. (defcustom org-auto-renumber-ordered-lists t
  465. "Non-nil means, automatically renumber ordered plain lists.
  466. Renumbering happens when the sequence have been changed with
  467. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  468. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  469. :group 'org-plain-lists
  470. :type 'boolean)
  471. (defgroup org-archive nil
  472. "Options concerning archiving in Org-mode."
  473. :tag "Org Archive"
  474. :group 'org-structure)
  475. (defcustom org-archive-location "%s_archive::"
  476. "The location where subtrees should be archived.
  477. This string consists of two parts, separated by a double-colon.
  478. The first part is a file name - when omitted, archiving happens in the same
  479. file. %s will be replaced by the current file name (without directory part).
  480. Archiving to a different file is useful to keep archived entries from
  481. contributing to the Org-mode Agenda.
  482. The part after the double colon is a headline. The archived entries will be
  483. filed under that headline. When omitted, the subtrees are simply filed away
  484. at the end of the file, as top-level entries.
  485. Here are a few examples:
  486. \"%s_archive::\"
  487. If the current file is Projects.org, archive in file
  488. Projects.org_archive, as top-level trees. This is the default.
  489. \"::* Archived Tasks\"
  490. Archive in the current file, under the top-level headline
  491. \"* Archived Tasks\".
  492. \"~/org/archive.org::\"
  493. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  494. \"basement::** Finished Tasks\"
  495. Archive in file ./basement (relative path), as level 3 trees
  496. below the level 2 heading \"** Finished Tasks\".
  497. You may set this option on a per-file basis by adding to the buffer a
  498. line like
  499. #+ARCHIVE: basement::** Finished Tasks"
  500. :group 'org-archive
  501. :type 'string)
  502. (defcustom org-archive-mark-done t
  503. "Non-nil means, mark archived entries as DONE."
  504. :group 'org-archive
  505. :type 'boolean)
  506. (defcustom org-archive-stamp-time t
  507. "Non-nil means, add a time stamp to archived entries.
  508. The time stamp will be added directly after the TODO state keyword in the
  509. first line, so it is probably best to use this in combinations with
  510. `org-archive-mark-done'."
  511. :group 'org-archive
  512. :type 'boolean)
  513. (defgroup org-table nil
  514. "Options concerning tables in Org-mode."
  515. :tag "Org Table"
  516. :group 'org)
  517. (defcustom org-enable-table-editor 'optimized
  518. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  519. When nil, such lines will be treated like ordinary lines.
  520. When equal to the symbol `optimized', the table editor will be optimized to
  521. do the following:
  522. - Use automatic overwrite mode in front of whitespace in table fields.
  523. This make the structure of the table stay in tact as long as the edited
  524. field does not exceed the column width.
  525. - Minimize the number of realigns. Normally, the table is aligned each time
  526. TAB or RET are pressed to move to another field. With optimization this
  527. happens only if changes to a field might have changed the column width.
  528. Optimization requires replacing the functions `self-insert-command',
  529. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  530. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  531. very good at guessing when a re-align will be necessary, but you can always
  532. force one with \\[org-ctrl-c-ctrl-c].
  533. If you would like to use the optimized version in Org-mode, but the
  534. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  535. This variable can be used to turn on and off the table editor during a session,
  536. but in order to toggle optimization, a restart is required.
  537. See also the variable `org-table-auto-blank-field'."
  538. :group 'org-table
  539. :type '(choice
  540. (const :tag "off" nil)
  541. (const :tag "on" t)
  542. (const :tag "on, optimized" optimized)))
  543. (defcustom orgtbl-optimized (eq org-enable-table-editor 'optimized)
  544. "Non-nil means, use the optimized table editor version for `orgtbl-mode'.
  545. In the optimized version, the table editor takes over all simple keys that
  546. normally just insert a character. In tables, the characters are inserted
  547. in a way to minimize disturbing the table structure (i.e. in overwrite mode
  548. for empty fields). Outside tables, the correct binding of the keys is
  549. restored.
  550. The default for this option is t if the optimized version is also used in
  551. Org-mode. See the variable `org-enable-table-editor' for details. Changing
  552. this variable requires a restart of Emacs to become effective."
  553. :group 'org-table
  554. :type 'boolean)
  555. (defgroup org-table-settings nil
  556. "Settings for tables in Org-mode."
  557. :tag "Org Table Settings"
  558. :group 'org-table)
  559. (defcustom org-table-default-size "5x2"
  560. "The default size for newly created tables, Columns x Rows."
  561. :group 'org-table-settings
  562. :type 'string)
  563. (defcustom org-table-number-regexp "^[<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*$"
  564. "Regular expression for recognizing numbers in table columns.
  565. If a table column contains mostly numbers, it will be aligned to the
  566. right. If not, it will be aligned to the left.
  567. The default value of this option is a regular expression which allows
  568. anything which looks remotely like a number as used in scientific
  569. context. For example, all of the following will be considered a
  570. number:
  571. 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
  572. Other options offered by the customize interface are more restrictive."
  573. :group 'org-table-settings
  574. :type '(choice
  575. (const :tag "Positive Integers"
  576. "^[0-9]+$")
  577. (const :tag "Integers"
  578. "^[-+]?[0-9]+$")
  579. (const :tag "Floating Point Numbers"
  580. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
  581. (const :tag "Floating Point Number or Integer"
  582. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
  583. (const :tag "Exponential, Floating point, Integer"
  584. "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
  585. (const :tag "Very General Number-Like"
  586. "^[<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*$")
  587. (string :tag "Regexp:")))
  588. (defcustom org-table-number-fraction 0.5
  589. "Fraction of numbers in a column required to make the column align right.
  590. In a column all non-white fields are considered. If at least this
  591. fraction of fields is matched by `org-table-number-fraction',
  592. alignment to the right border applies."
  593. :group 'org-table-settings
  594. :type 'number)
  595. (defgroup org-table-editing nil
  596. "Bahavior of tables during editing in Org-mode."
  597. :tag "Org Table Editing"
  598. :group 'org-table)
  599. (defcustom org-table-automatic-realign t
  600. "Non-nil means, automatically re-align table when pressing TAB or RETURN.
  601. When nil, aligning is only done with \\[org-table-align], or after column
  602. removal/insertion."
  603. :group 'org-table-editing
  604. :type 'boolean)
  605. (defcustom org-table-limit-column-width t ;kw
  606. "Non-nil means, allow to limit the width of table columns with <N> fields."
  607. :group 'org-table-editing
  608. :type 'boolean)
  609. (defcustom org-table-auto-blank-field t
  610. "Non-nil means, automatically blank table field when starting to type into it.
  611. This only happens when typing immediately after a field motion
  612. command (TAB, S-TAB or RET).
  613. Only relevant when `org-enable-table-editor' is equal to `optimized'."
  614. :group 'org-table-editing
  615. :type 'boolean)
  616. (defcustom org-table-tab-jumps-over-hlines t
  617. "Non-nil means, tab in the last column of a table with jump over a hline.
  618. If a horizontal separator line is following the current line,
  619. `org-table-next-field' can either create a new row before that line, or jump
  620. over the line. When this option is nil, a new line will be created before
  621. this line."
  622. :group 'org-table-editing
  623. :type 'boolean)
  624. (defcustom org-table-tab-recognizes-table.el t
  625. "Non-nil means, TAB will automatically notice a table.el table.
  626. When it sees such a table, it moves point into it and - if necessary -
  627. calls `table-recognize-table'."
  628. :group 'org-table-editing
  629. :type 'boolean)
  630. (defgroup org-table-calculation nil
  631. "Options concerning tables in Org-mode."
  632. :tag "Org Table Calculation"
  633. :group 'org-table)
  634. (defcustom org-table-copy-increment t
  635. "Non-nil means, increment when copying current field with \\[org-table-copy-down]."
  636. :group 'org-table-calculation
  637. :type 'boolean)
  638. (defcustom org-calc-default-modes
  639. '(calc-internal-prec 12
  640. calc-float-format (float 5)
  641. calc-angle-mode deg
  642. calc-prefer-frac nil
  643. calc-symbolic-mode nil
  644. calc-date-format (YYYY "-" MM "-" DD " " Www (" " HH ":" mm))
  645. calc-display-working-message t
  646. )
  647. "List with Calc mode settings for use in calc-eval for table formulas.
  648. The list must contain alternating symbols (Calc modes variables and values).
  649. Don't remove any of the default settings, just change the values. Org-mode
  650. relies on the variables to be present in the list."
  651. :group 'org-table-calculation
  652. :type 'plist)
  653. (defcustom org-table-formula-evaluate-inline t
  654. "Non-nil means, TAB and RET evaluate a formula in current table field.
  655. If the current field starts with an equal sign, it is assumed to be a formula
  656. which should be evaluated as described in the manual and in the documentation
  657. string of the command `org-table-eval-formula'. This feature requires the
  658. Emacs calc package.
  659. When this variable is nil, formula calculation is only available through
  660. the command \\[org-table-eval-formula]."
  661. :group 'org-table-calculation
  662. :type 'boolean)
  663. (defcustom org-table-formula-use-constants t
  664. "Non-nil means, interpret constants in formulas in tables.
  665. A constant looks like `$c' or `$Grav' and will be replaced before evaluation
  666. by the value given in `org-table-formula-constants', or by a value obtained
  667. from the `constants.el' package."
  668. :group 'org-table-calculation
  669. :type 'boolean)
  670. (defcustom org-table-formula-constants nil
  671. "Alist with constant names and values, for use in table formulas.
  672. The car of each element is a name of a constant, without the `$' before it.
  673. The cdr is the value as a string. For example, if you'd like to use the
  674. speed of light in a formula, you would configure
  675. (setq org-table-formula-constants '((\"c\" . \"299792458.\")))
  676. and then use it in an equation like `$1*$c'."
  677. :group 'org-table-calculation
  678. :type '(repeat
  679. (cons (string :tag "name")
  680. (string :tag "value"))))
  681. (defcustom org-table-formula-numbers-only nil
  682. "Non-nil means, calculate only with numbers in table formulas.
  683. Then all input fields will be converted to a number, and the result
  684. must also be a number. When nil, calc's full potential is available
  685. in table calculations, including symbolics etc."
  686. :group 'org-table-calculation
  687. :type 'boolean)
  688. (defcustom org-table-allow-automatic-line-recalculation t
  689. "Non-nil means, lines marked with |#| or |*| will be recomputed automatically.
  690. Automatically means, when TAB or RET or C-c C-c are pressed in the line."
  691. :group 'org-table-calculation
  692. :type 'boolean)
  693. (defgroup org-link nil
  694. "Options concerning links in Org-mode."
  695. :tag "Org Link"
  696. :group 'org)
  697. (defcustom org-descriptive-links t
  698. "Non-nil means, hide link part and only show description of bracket links.
  699. Bracket links are like [[link][descritpion]]. This variable sets the initial
  700. state in new org-mode buffers. The setting can then be toggled on a
  701. per-buffer basis from the Org->Hyperlinks menu."
  702. :group 'org-link
  703. :type 'boolean)
  704. (defcustom org-link-style 'bracket
  705. "The style of links to be inserted with \\[org-insert-link].
  706. Possible values are:
  707. bracket [[link][description]]. This is recommended
  708. plain Description \\n link. The old way, no longer recommended."
  709. :group 'org-link
  710. :type '(choice
  711. (const :tag "Bracket (recommended)" bracket)
  712. (const :tag "Plain (no longer recommended)" plain)))
  713. (defcustom org-link-format "%s"
  714. "Default format for external, URL-like linkes in the buffer.
  715. This is a format string for printf, %s will be replaced by the link text.
  716. The recommended value is just \"%s\", since links will be protected by
  717. enclosing them in double brackets. If you prefer plain links (see variable
  718. `org-link-style'), \"<%s>\" is useful. Some people also recommend an
  719. additional URL: prefix, so the format would be \"<URL:%s>\"."
  720. :group 'org-link
  721. :type '(choice
  722. (const :tag "\"%s\" (e.g. http://www.there.com)" "%s")
  723. (const :tag "\"<%s>\" (e.g. <http://www.there.com>)" "<%s>")
  724. (const :tag "\"<URL:%s>\" (e.g. <URL:http://www.there.com>)" "<URL:%s>")
  725. (string :tag "Other" :value "<%s>")))
  726. (defcustom org-link-file-path-type 'adaptive
  727. "How the path name in file links should be stored.
  728. Valid values are:
  729. relative relative to the current directory, i.e. the directory of the file
  730. into which the link is being inserted.
  731. absolute absolute path, if possible with ~ for home directory.
  732. noabbrev absolute path, no abbreviation of home directory.
  733. adaptive Use relative path for files in the current directory and sub-
  734. directories of it. For other files, use an absolute path."
  735. :group 'org-link
  736. :type '(choice
  737. (const relative)
  738. (const absolute)
  739. (const noabbrev)
  740. (const adaptive)))
  741. (defcustom org-activate-links '(bracket angle plain radio tag date)
  742. "Types of links that should be activated in Org-mode files.
  743. This is a list of symbols, each leading to the activation of a certain link
  744. type. In principle, it does not hurt to turn on most link types - there may
  745. be a small gain when turning off unused link types. The types are:
  746. bracket The recommended [[link][description]] or [[link]] links with hiding.
  747. angular Links in angular brackes that may contain whitespace like
  748. <bbdb:Carsten Dominik>.
  749. plain Plain links in normal text, no whitespace, like http://google.com.
  750. radio Text that is matched by a radio target, see manual for details.
  751. tag Tag settings in a headline (link to tag search).
  752. date Time stamps (link to calendar).
  753. camel CamelCase words defining text searches.
  754. Changing this variable requires a restart of Emacs to become effective."
  755. :group 'org-link
  756. :type '(set (const :tag "Double bracket links (new style)" bracket)
  757. (const :tag "Angular bracket links (old style)" angular)
  758. (const :tag "plain text links" plain)
  759. (const :tag "Radio target matches" radio)
  760. (const :tag "Tags" tag)
  761. (const :tag "Timestamps" date)
  762. (const :tag "CamelCase words" camel)))
  763. (defgroup org-link-store nil
  764. "Options concerning storing links in Org-mode"
  765. :tag "Org Store Link"
  766. :group 'org-link)
  767. (defcustom org-context-in-file-links t
  768. "Non-nil means, file links from `org-store-link' contain context.
  769. A search string will be added to the file name with :: as separator and
  770. used to find the context when the link is activated by the command
  771. `org-open-at-point'.
  772. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  773. negates this setting for the duration of the command."
  774. :group 'org-link-store
  775. :type 'boolean)
  776. (defcustom org-file-link-context-use-camel-case nil
  777. "Non-nil means, use CamelCase to store a search context in a file link.
  778. When nil, the search string simply consists of the words of the string.
  779. CamelCase is deprecated, and support for it may be dropped in the future."
  780. :group 'org-link-store
  781. :type 'boolean)
  782. (defcustom org-keep-stored-link-after-insertion nil
  783. "Non-nil means, keep link in list for entire session.
  784. The command `org-store-link' adds a link pointing to the current
  785. location to an internal list. These links accumulate during a session.
  786. The command `org-insert-link' can be used to insert links into any
  787. Org-mode file (offering completion for all stored links). When this
  788. option is nil, every link which has been inserted once using \\[org-insert-link]
  789. will be removed from the list, to make completing the unused links
  790. more efficient."
  791. :group 'org-link-store
  792. :type 'boolean)
  793. (defcustom org-usenet-links-prefer-google nil
  794. "Non-nil means, `org-store-link' will create web links to Google groups.
  795. When nil, Gnus will be used for such links.
  796. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  797. negates this setting for the duration of the command."
  798. :group 'org-link-store
  799. :type 'boolean)
  800. (defgroup org-link-follow nil
  801. "Options concerning following links in Org-mode"
  802. :tag "Org Follow Link"
  803. :group 'org-link)
  804. (defcustom org-tab-follows-link nil
  805. "Non-nil means, on links TAB will follow the link.
  806. Needs to be set before org.el is loaded."
  807. :group 'org-link-follow
  808. :type 'boolean)
  809. (defcustom org-return-follows-link nil
  810. "Non-nil means, on links RET will follow the link.
  811. Needs to be set before org.el is loaded."
  812. :group 'org-link-follow
  813. :type 'boolean)
  814. (defcustom org-mouse-1-follows-link t
  815. "Non-nil means, mouse-1 on a link will follow the link.
  816. A longer mouse click will still set point. Does not wortk on XEmacs.
  817. Needs to be set before org.el is loaded."
  818. :group 'org-link-follow
  819. :type 'boolean)
  820. (defcustom org-mark-ring-length 4
  821. "Number of different positions to be recorded in the ring
  822. Changing this requires a restart of Emacs to work correctly."
  823. :group 'org-link-follow
  824. :type 'interger)
  825. (defcustom org-link-frame-setup
  826. '((vm . vm-visit-folder-other-frame)
  827. (gnus . gnus-other-frame)
  828. (file . find-file-other-window))
  829. "Setup the frame configuration for following links.
  830. When following a link with Emacs, it may often be useful to display
  831. this link in another window or frame. This variable can be used to
  832. set this up for the different types of links.
  833. For VM, use any of
  834. `vm-visit-folder'
  835. `vm-visit-folder-other-frame'
  836. For Gnus, use any of
  837. `gnus'
  838. `gnus-other-frame'
  839. For FILE, use any of
  840. `find-file'
  841. `find-file-other-window'
  842. `find-file-other-frame'
  843. For the calendar, use the variable `calendar-setup'.
  844. For BBDB, it is currently only possible to display the matches in
  845. another window."
  846. :group 'org-link-follow
  847. :type '(list
  848. (cons (const vm)
  849. (choice
  850. (const vm-visit-folder)
  851. (const vm-visit-folder-other-window)
  852. (const vm-visit-folder-other-frame)))
  853. (cons (const gnus)
  854. (choice
  855. (const gnus)
  856. (const gnus-other-frame)))
  857. (cons (const file)
  858. (choice
  859. (const find-file)
  860. (const find-file-other-window)
  861. (const find-file-other-frame)))))
  862. (defcustom org-open-non-existing-files nil
  863. "Non-nil means, `org-open-file' will open non-existing file.
  864. When nil, an error will be generated."
  865. :group 'org-link-follow
  866. :type 'boolean)
  867. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  868. "Non-nil means, ask for confirmation before executing shell links.
  869. Shell links can be dangerous, just thing about a link
  870. [[shell:rm -rf ~/*][Google Search]]
  871. This link would show up in your Org-mode document as \"Google Search\"
  872. but really it would remove your entire home directory.
  873. Therefore I *definitely* advise against setting this variable to nil.
  874. Just change it to `y-or-n-p' of you want to confirm with a single key press
  875. rather than having to type \"yes\"."
  876. :group 'org-link-follow
  877. :type '(choice
  878. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  879. (const :tag "with y-or-n (faster)" y-or-n-p)
  880. (const :tag "no confirmation (dangerous)" nil)))
  881. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  882. "Non-nil means, ask for confirmation before executing elisp links.
  883. Elisp links can be dangerous, just thing about a link
  884. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  885. This link would show up in your Org-mode document as \"Google Search\"
  886. but really it would remove your entire home directory.
  887. Therefore I *definitely* advise against setting this variable to nil.
  888. Just change it to `y-or-n-p' of you want to confirm with a single key press
  889. rather than having to type \"yes\"."
  890. :group 'org-link-follow
  891. :type '(choice
  892. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  893. (const :tag "with y-or-n (faster)" y-or-n-p)
  894. (const :tag "no confirmation (dangerous)" nil)))
  895. (defconst org-file-apps-defaults-gnu
  896. '((t . mailcap))
  897. "Default file applications on a UNIX or GNU/Linux system.
  898. See `org-file-apps'.")
  899. (defconst org-file-apps-defaults-macosx
  900. '((t . "open %s")
  901. ("ps" . "gv %s")
  902. ("ps.gz" . "gv %s")
  903. ("eps" . "gv %s")
  904. ("eps.gz" . "gv %s")
  905. ("dvi" . "xdvi %s")
  906. ("fig" . "xfig %s"))
  907. "Default file applications on a MacOS X system.
  908. The system \"open\" is known as a default, but we use X11 applications
  909. for some files for which the OS does not have a good default.
  910. See `org-file-apps'.")
  911. (defconst org-file-apps-defaults-windowsnt
  912. (list (cons t
  913. (list (if (featurep 'xemacs)
  914. 'mswindows-shell-execute
  915. 'w32-shell-execute)
  916. "open" 'file)))
  917. "Default file applications on a Windows NT system.
  918. The system \"open\" is used for most files.
  919. See `org-file-apps'.")
  920. (defcustom org-file-apps
  921. '(
  922. ("txt" . emacs)
  923. ("tex" . emacs)
  924. ("ltx" . emacs)
  925. ("org" . emacs)
  926. ("el" . emacs)
  927. ("bib" . emacs)
  928. )
  929. "External applications for opening `file:path' items in a document.
  930. Org-mode uses system defaults for different file types, but
  931. you can use this variable to set the application for a given file
  932. extension. The entries in this list are cons cells where the car identifies
  933. files and the cdr the corresponding command. Possible values for the
  934. file identifier are
  935. \"ext\" A string identifying an extension
  936. `directory' Matches a directory
  937. t Default for all remaining files
  938. Possible values for the command are:
  939. `emacs' The file will be visited by the current Emacs process.
  940. `default' Use the default application for this file type.
  941. string A command to be executed by a shell; %s will be replaced
  942. by the path to the file.
  943. sexp A Lisp form which will be evaluated. The file path will
  944. be available in the Lisp variable `file'.
  945. For more examples, see the system specific constants
  946. `org-file-apps-defaults-macosx'
  947. `org-file-apps-defaults-windowsnt'
  948. `org-file-apps-defaults-gnu'."
  949. :group 'org-link-follow
  950. :type '(repeat
  951. (cons (choice :value ""
  952. (string :tag "Extension")
  953. (const :tag "Default for unrecognized files" t)
  954. (const :tag "Links to a directory" directory))
  955. (choice :value ""
  956. (const :tag "Visit with Emacs" emacs)
  957. (const :tag "Use system default" default)
  958. (string :tag "Command")
  959. (sexp :tag "Lisp form")))))
  960. (defcustom org-mhe-search-all-folders nil
  961. "Non-nil means, that the search for the mh-message will be extended to
  962. all folders if the message cannot be found in the folder given in the link.
  963. Searching all folders is very effective with one of the search engines
  964. supported by MH-E, but will be slow with pick."
  965. :group 'org-link-follow
  966. :type 'boolean)
  967. (defgroup org-remember nil
  968. "Options concerning interaction with remember.el."
  969. :tag "Org Remember"
  970. :group 'org)
  971. (defcustom org-directory "~/org"
  972. "Directory with org files.
  973. This directory will be used as default to prompt for org files.
  974. Used by the hooks for remember.el."
  975. :group 'org-remember
  976. :type 'directory)
  977. (defcustom org-default-notes-file "~/.notes"
  978. "Default target for storing notes.
  979. Used by the hooks for remember.el. This can be a string, or nil to mean
  980. the value of `remember-data-file'."
  981. :group 'org-remember
  982. :type '(choice
  983. (const :tag "Default from remember-data-file" nil)
  984. file))
  985. (defcustom org-remember-templates nil
  986. "Templates for the creation of remember buffers.
  987. When nil, just let remember make the buffer.
  988. When not nil, this is a list of 3-element lists. In each entry, the first
  989. element is a character, a unique key to select this template.
  990. The second element is the template. The third element is optional and can
  991. specify a destination file for remember items created with this template.
  992. The default file is given by `org-default-notes-file'.
  993. The template specifies the structure of the remember buffer. It should have
  994. a first line starting with a star, to act as the org-mode headline.
  995. Furthermore, the following %-escapes will be replaced with content:
  996. %t time stamp, date only
  997. %T time stamp with date and time
  998. %u inactive time stamp, date only
  999. %U inactive time stamp with date and time
  1000. %n user name
  1001. %a annotation, normally the link created with org-store-link
  1002. %i initial content, the region when remember is called with C-u.
  1003. If %i is indented, the entire inserted text will be indented as well.
  1004. %? This will be removed, and the cursor placed at this position."
  1005. :group 'org-remember
  1006. :type '(repeat :tag "enabled"
  1007. (list :value (?a "\n" nil)
  1008. (character :tag "Selection Key")
  1009. (string :tag "Template")
  1010. (file :tag "Destination file (optional)"))))
  1011. (defcustom org-reverse-note-order nil
  1012. "Non-nil means, store new notes at the beginning of a file or entry.
  1013. When nil, new notes will be filed to the end of a file or entry."
  1014. :group 'org-remember
  1015. :type '(choice
  1016. (const :tag "Reverse always" t)
  1017. (const :tag "Reverse never" nil)
  1018. (repeat :tag "By file name regexp"
  1019. (cons regexp boolean))))
  1020. (defgroup org-todo nil
  1021. "Options concerning TODO items in Org-mode."
  1022. :tag "Org TODO"
  1023. :group 'org)
  1024. (defcustom org-todo-keywords '("TODO" "DONE")
  1025. "List of TODO entry keywords.
  1026. \\<org-mode-map>By default, this is '(\"TODO\" \"DONE\"). The last entry in the list is
  1027. considered to mean that the entry is \"done\". All the other mean that
  1028. action is required, and will make the entry show up in todo lists, diaries
  1029. etc.
  1030. The command \\[org-todo] cycles an entry through these states, and an
  1031. additional state where no keyword is present. For details about this
  1032. cycling, see also the variable `org-todo-interpretation'
  1033. Changes become only effective after restarting Emacs."
  1034. :group 'org-todo
  1035. :group 'org-keywords
  1036. :type '(repeat (string :tag "Keyword")))
  1037. (defcustom org-todo-interpretation 'sequence
  1038. "Controls how TODO keywords are interpreted.
  1039. This variable is only relevant if `org-todo-keywords' contains more than two
  1040. states. \\<org-mode-map>Possible values are `sequence' and `type'.
  1041. When `sequence', \\[org-todo] will always switch to the next state in the
  1042. `org-todo-keywords' list. When `type', \\[org-todo] only cycles from state
  1043. to state when executed several times in direct succession. Otherwise, it
  1044. switches directly to DONE from any state.
  1045. See the manual for more information."
  1046. :group 'org-todo
  1047. :group 'org-keywords
  1048. :type '(choice (const sequence)
  1049. (const type)))
  1050. (defcustom org-after-todo-state-change-hook nil
  1051. "Hook which is run after the state of a TODO item was changed.
  1052. The new state (a string with a TODO keyword, or nil) is available in the
  1053. Lisp variable `state'."
  1054. :group 'org-todo
  1055. :type 'hook)
  1056. (defcustom org-log-done nil
  1057. "When set, insert a (non-active) time stamp when TODO entry is marked DONE.
  1058. When the state of an entry is changed from nothing to TODO, remove a previous
  1059. closing date.
  1060. This can also be configured on a per-file basis by adding one of
  1061. the following lines anywhere in the buffer:
  1062. #+STARTUP: logging
  1063. #+STARTUP: nologging"
  1064. :group 'org-todo
  1065. :type 'boolean)
  1066. (defgroup org-priorities nil
  1067. "Priorities in Org-mode."
  1068. :tag "Org Priorities"
  1069. :group 'org-todo)
  1070. (defcustom org-default-priority ?B
  1071. "The default priority of TODO items.
  1072. This is the priority an item get if no explicit priority is given."
  1073. :group 'org-priorities
  1074. :type 'character)
  1075. (defcustom org-lowest-priority ?C
  1076. "The lowest priority of TODO items. A character like ?A, ?B etc."
  1077. :group 'org-priorities
  1078. :type 'character)
  1079. (defgroup org-time nil
  1080. "Options concerning time stamps and deadlines in Org-mode."
  1081. :tag "Org Time"
  1082. :group 'org)
  1083. (defcustom org-insert-labeled-timestamps-at-point nil
  1084. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1085. When nil, these labeled time stamps are forces into the second line of an
  1086. entry, just after the headline. When scheduling from the global TODO list,
  1087. the time stamp will always be forced into the second line."
  1088. :group 'org-time
  1089. :type 'boolean)
  1090. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1091. "Formats for `format-time-string' which are used for time stamps.
  1092. It is not recommended to change this constant.")
  1093. (defcustom org-time-stamp-rounding-minutes 0
  1094. "Number of minutes to round time stamps to upon insertion.
  1095. When zero, insert the time unmodified. Useful rounding numbers
  1096. should be factors of 60, so for example 5, 10, 15.
  1097. When this is not zero, you can still force an exact time-stamp by using
  1098. a double prefix argument to a time-stamp command like `C-c .' or `C-c !'."
  1099. :group 'org-time
  1100. :type 'integer)
  1101. (defcustom org-deadline-warning-days 30
  1102. "No. of days before expiration during which a deadline becomes active.
  1103. This variable governs the display in the org file."
  1104. :group 'org-time
  1105. :type 'number)
  1106. (defcustom org-popup-calendar-for-date-prompt t
  1107. "Non-nil means, pop up a calendar when prompting for a date.
  1108. In the calendar, the date can be selected with mouse-1. However, the
  1109. minibuffer will also be active, and you can simply enter the date as well.
  1110. When nil, only the minibuffer will be available."
  1111. :group 'org-time
  1112. :type 'boolean)
  1113. (defcustom org-calendar-follow-timestamp-change t
  1114. "Non-nil means, make the calendar window follow timestamp changes.
  1115. When a timestamp is modified and the calendar window is visible, it will be
  1116. moved to the new date."
  1117. :group 'org-time
  1118. :type 'boolean)
  1119. (defgroup org-tags nil
  1120. "Options concerning tags in Org-mode."
  1121. :tag "Org Tags"
  1122. :group 'org)
  1123. (defcustom org-tag-alist nil
  1124. "List of tags allowed in Org-mode files.
  1125. When this list is nil, Org-mode will base TAG input on what is already in the
  1126. buffer.
  1127. The value of this variable is an alist, the car may be (and should) be a
  1128. character that is used to select that tag through the fast-tag-selection
  1129. interface. See the manual for details."
  1130. :group 'org-tags
  1131. :type '(repeat
  1132. (choice
  1133. (cons (string :tag "Tag name")
  1134. (character :tag "Access char"))
  1135. (const :tag "Start radio group" (:startgroup))
  1136. (const :tag "End radio group" (:endgroup)))))
  1137. (defcustom org-use-fast-tag-selection 'auto
  1138. "Non-nil means, use fast tag selection scheme.
  1139. This is a special interface to select and deselect tags with single keys.
  1140. When nil, fast selection is never used.
  1141. When the symbol `auto', fast selection is used if and only if selection
  1142. characters for tags have been configured, either through the variable
  1143. `org-tag-alist' or through a #+TAGS line in the buffer.
  1144. When t, fast selection is always used and selection keys are assigned
  1145. automatically if necessary."
  1146. :group 'org-tags
  1147. :type '(choice
  1148. (const :tag "Always" t)
  1149. (const :tag "Never" nil)
  1150. (const :tag "When selection characters are configured" 'auto)))
  1151. (defcustom org-tags-column 48
  1152. "The column to which tags should be indented in a headline.
  1153. If this number is positive, it specifies the column. If it is negative,
  1154. it means that the tags should be flushright to that column. For example,
  1155. -79 works well for a normal 80 character screen."
  1156. :group 'org-tags
  1157. :type 'integer)
  1158. (defcustom org-auto-align-tags t
  1159. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1160. These operations change the length of a headline and therefore shift
  1161. the tags around. With this options turned on, after each such operation
  1162. the tags are again aligned to `org-tags-column'."
  1163. :group 'org-tags
  1164. :type 'boolean)
  1165. (defcustom org-use-tag-inheritance t
  1166. "Non-nil means, tags in levels apply also for sublevels.
  1167. When nil, only the tags directly given in a specific line apply there.
  1168. If you turn off this option, you very likely want to turn on the
  1169. companion option `org-tags-match-list-sublevels'."
  1170. :group 'org-tags
  1171. :type 'boolean)
  1172. (defcustom org-tags-match-list-sublevels nil
  1173. "Non-nil means list also sublevels of headlines matching tag search.
  1174. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1175. the sublevels of a headline matching a tag search often also match
  1176. the same search. Listing all of them can create very long lists.
  1177. Setting this variable to nil causes subtrees of a match to be skipped.
  1178. This option is off by default, because inheritance in on. If you turn
  1179. inheritance off, you very likely want to turn this option on.
  1180. As a special case, if the tag search is restricted to TODO items, the
  1181. value of this variable is ignored and sublevels are always checked, to
  1182. make sure all corresponding TODO items find their way into the list."
  1183. :group 'org-tags
  1184. :type 'boolean)
  1185. (defvar org-tags-history nil
  1186. "History of minibuffer reads for tags.")
  1187. (defvar org-last-tags-completion-table nil
  1188. "The last used completion table for tags.")
  1189. (defgroup org-agenda nil
  1190. "Options concerning agenda display Org-mode."
  1191. :tag "Org Agenda"
  1192. :group 'org)
  1193. (defvar org-category nil
  1194. "Variable used by org files to set a category for agenda display.
  1195. Such files should use a file variable to set it, for example
  1196. -*- mode: org; org-category: \"ELisp\"
  1197. or contain a special line
  1198. #+CATEGORY: ELisp
  1199. If the file does not specify a category, then file's base name
  1200. is used instead.")
  1201. (make-variable-buffer-local 'org-category)
  1202. (defcustom org-agenda-files nil
  1203. "The files to be used for agenda display.
  1204. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1205. \\[org-remove-file]. You can also use customize to edit the list.
  1206. If the value of the variable is not a list but a single file name, then
  1207. the list of agenda files is actually stored and maintained in that file, one
  1208. agenda file per line."
  1209. :group 'org-agenda
  1210. :type '(choice
  1211. (repeat :tag "List of files" file)
  1212. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1213. (defcustom org-agenda-custom-commands '(("w" todo "WAITING"))
  1214. "Custom commands for the agenda.
  1215. These commands will be offered on the splash screen displayed by the
  1216. agenda dispatcher \\[org-agenda]. Each entry is a list of 3 items:
  1217. key The key (a single char as a string) to be associated with the command.
  1218. type The command type, any of the following symbols:
  1219. todo Entries with a specific TODO keyword, in all agenda files.
  1220. tags Tags match in all agenda files.
  1221. tags-todo Tags match in all agenda files, TODO entries only.
  1222. todo-tree Sparse tree of specific TODO keyword in *current* file.
  1223. tags-tree Sparse tree with all tags matches in *current* file.
  1224. occur-tree Occur sparse tree for current file.
  1225. match What to search for:
  1226. - a single keyword for TODO keyword searches
  1227. - a tags match expression for tags searches
  1228. - a regular expression for occur searches"
  1229. :group 'org-agenda
  1230. :type '(repeat
  1231. (list (string :tag "Key")
  1232. (choice :tag "Type"
  1233. (const :tag "Tags search in all agenda files" tags)
  1234. (const :tag "Tags search of TODO entries, all agenda files" tags-todo)
  1235. (const :tag "TODO keyword search in all agenda files" todo)
  1236. (const :tag "Tags sparse tree in current buffer" tags-tree)
  1237. (const :tag "TODO keyword tree in current buffer" todo-tree)
  1238. (const :tag "Occur tree in current buffer" occur-tree))
  1239. (string :tag "Match"))))
  1240. (defcustom org-agenda-todo-list-sublevels t
  1241. "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
  1242. When nil, the sublevels of a TODO entry are not checked, resulting in
  1243. potentially much shorter TODO lists."
  1244. :group 'org-agenda
  1245. :group 'org-todo
  1246. :type 'boolean)
  1247. (defcustom org-agenda-todo-ignore-scheduled nil
  1248. "Non-nil means, don't show scheduled entries in the global todo list.
  1249. The idea behind this is that by scheduling it, you have already taken care
  1250. of this item."
  1251. :group 'org-agenda
  1252. :group 'org-todo
  1253. :type 'boolean)
  1254. (defcustom org-agenda-include-all-todo nil
  1255. "Non-nil means, the agenda will always contain all TODO entries.
  1256. When nil, date-less entries will only be shown if `org-agenda' is called
  1257. with a prefix argument.
  1258. When non-nil, the TODO entries will be listed at the top of the agenda, before
  1259. the entries for specific days."
  1260. :group 'org-agenda
  1261. :type 'boolean)
  1262. (defcustom org-agenda-include-diary nil
  1263. "If non-nil, include in the agenda entries from the Emacs Calendar's diary."
  1264. :group 'org-agenda
  1265. :type 'boolean)
  1266. (defcustom org-calendar-to-agenda-key [?c]
  1267. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1268. The command `org-calendar-goto-agenda' will be bound to this key. The
  1269. default is the character `c' because then `c' can be used to switch back and
  1270. forth between agenda and calendar."
  1271. :group 'org-agenda
  1272. :type 'sexp)
  1273. (defgroup org-agenda-setup nil
  1274. "Options concerning setting up the Agenda window in Org Mode."
  1275. :tag "Org Agenda Window Setup"
  1276. :group 'org-agenda)
  1277. (defcustom org-agenda-mouse-1-follows-link nil
  1278. "Non-nil means, mouse-1 on a link will follow the link in the agenda.
  1279. A longer mouse click will still set point. Does not wortk on XEmacs.
  1280. Needs to be set before org.el is loaded."
  1281. :group 'org-agenda-setup
  1282. :type 'boolean)
  1283. (defcustom org-agenda-start-with-follow-mode nil
  1284. "The initial value of follwo-mode in a newly created agenda window."
  1285. :group 'org-agenda-setup
  1286. :type 'boolean)
  1287. (defcustom org-select-agenda-window t
  1288. "Non-nil means, after creating an agenda, move cursor into Agenda window.
  1289. When nil, cursor will remain in the current window."
  1290. :group 'org-agenda-setup
  1291. :type 'boolean)
  1292. (defcustom org-fit-agenda-window t
  1293. "Non-nil means, change window size of agenda to fit content."
  1294. :group 'org-agenda-setup
  1295. :type 'boolean)
  1296. (defgroup org-agenda-display nil
  1297. "Options concerning what to display initially in Agenda."
  1298. :tag "Org Agenda Display"
  1299. :group 'org-agenda)
  1300. (defcustom org-agenda-show-all-dates t
  1301. "Non-nil means, `org-agenda' shows every day in the selected range.
  1302. When nil, only the days which actually have entries are shown."
  1303. :group 'org-agenda-display
  1304. :type 'boolean)
  1305. (defcustom org-agenda-start-on-weekday 1
  1306. "Non-nil means, start the overview always on the specified weekday.
  1307. 0 denotes Sunday, 1 denotes Monday etc.
  1308. When nil, always start on the current day."
  1309. :group 'org-agenda-display
  1310. :type '(choice (const :tag "Today" nil)
  1311. (number :tag "Weekday No.")))
  1312. (defcustom org-agenda-ndays 7
  1313. "Number of days to include in overview display.
  1314. Should be 1 or 7."
  1315. :group 'org-agenda-display
  1316. :type 'number)
  1317. (defcustom org-agenda-use-time-grid t
  1318. "Non-nil means, show a time grid in the agenda schedule.
  1319. A time grid is a set of lines for specific times (like every two hours between
  1320. 8:00 and 20:00). The items scheduled for a day at specific times are
  1321. sorted in between these lines.
  1322. For details about when the grid will be shown, and what it will look like, see
  1323. the variable `org-agenda-time-grid'."
  1324. :group 'org-agenda-display
  1325. :type 'boolean)
  1326. (defcustom org-agenda-time-grid
  1327. '((daily today require-timed)
  1328. "----------------"
  1329. (800 1000 1200 1400 1600 1800 2000))
  1330. "The settings for time grid for agenda display.
  1331. This is a list of three items. The first item is again a list. It contains
  1332. symbols specifying conditions when the grid should be displayed:
  1333. daily if the agenda shows a single day
  1334. weekly if the agenda shows an entire week
  1335. today show grid on current date, independent of daily/weekly display
  1336. require-timed show grid only if at least on item has a time specification
  1337. The second item is a string which will be places behing the grid time.
  1338. The third item is a list of integers, indicating the times that should have
  1339. a grid line."
  1340. :group 'org-agenda-display
  1341. :type
  1342. '(list
  1343. (set :greedy t :tag "Grid Display Options"
  1344. (const :tag "Show grid in single day agenda display" daily)
  1345. (const :tag "Show grid in weekly agenda display" weekly)
  1346. (const :tag "Always show grid for today" today)
  1347. (const :tag "Show grid only if any timed entries are present"
  1348. require-timed)
  1349. (const :tag "Skip grid times already present in an entry"
  1350. remove-match))
  1351. (string :tag "Grid String")
  1352. (repeat :tag "Grid Times" (integer :tag "Time"))))
  1353. (defcustom org-agenda-sorting-strategy '(time-up category-keep priority-down)
  1354. "Sorting structure for the agenda items of a single day.
  1355. This is a list of symbols which will be used in sequence to determine
  1356. if an entry should be listed before another entry. The following
  1357. symbols are recognized:
  1358. time-up Put entries with time-of-day indications first, early first
  1359. time-down Put entries with time-of-day indications first, late first
  1360. category-keep Keep the default order of categories, corresponding to the
  1361. sequence in `org-agenda-files'.
  1362. category-up Sort alphabetically by category, A-Z.
  1363. category-down Sort alphabetically by category, Z-A.
  1364. priority-up Sort numerically by priority, high priority last.
  1365. priority-down Sort numerically by priority, high priority first.
  1366. The different possibilities will be tried in sequence, and testing stops
  1367. if one comparison returns a \"not-equal\". For example, the default
  1368. '(time-up category-keep priority-down)
  1369. means: Pull out all entries having a specified time of day and sort them,
  1370. in order to make a time schedule for the current day the first thing in the
  1371. agenda listing for the day. Of the entries without a time indication, keep
  1372. the grouped in categories, don't sort the categories, but keep them in
  1373. the sequence given in `org-agenda-files'. Within each category sort by
  1374. priority.
  1375. Leaving out `category-keep' would mean that items will be sorted across
  1376. categories by priority."
  1377. :group 'org-agenda-display
  1378. :type '(repeat
  1379. (choice
  1380. (const time-up)
  1381. (const time-down)
  1382. (const category-keep)
  1383. (const category-up)
  1384. (const category-down)
  1385. (const priority-up)
  1386. (const priority-down))))
  1387. (defcustom org-sort-agenda-notime-is-late t
  1388. "Non-nil means, items without time are considered late.
  1389. This is only relevant for sorting. When t, items which have no explicit
  1390. time like 15:30 will be considered as 99:01, i.e. later than any items which
  1391. do have a time. When nil, the default time is before 0:00. You can use this
  1392. option to decide if the schedule for today should come before or after timeless
  1393. agenda entries."
  1394. :group 'org-agenda-display
  1395. :type 'boolean)
  1396. (defgroup org-agenda-prefix nil
  1397. "Options concerning the entry prefix in the Org-mode agenda display."
  1398. :tag "Org Agenda Prefix"
  1399. :group 'org-agenda)
  1400. (defcustom org-agenda-prefix-format " %-12:c%?-12t% s"
  1401. "Format specification for the prefix of items in the agenda buffer.
  1402. This format works similar to a printf format, with the following meaning:
  1403. %c the category of the item, \"Diary\" for entries from the diary, or
  1404. as given by the CATEGORY keyword or derived from the file name.
  1405. %T the first tag of the item.
  1406. %t the time-of-day specification if one applies to the entry, in the
  1407. format HH:MM
  1408. %s Scheduling/Deadline information, a short string
  1409. All specifiers work basically like the standard `%s' of printf, but may
  1410. contain two additional characters: A question mark just after the `%' and
  1411. a whitespace/punctuation character just before the final letter.
  1412. If the first character after `%' is a question mark, the entire field
  1413. will only be included if the corresponding value applies to the
  1414. current entry. This is useful for fields which should have fixed
  1415. width when present, but zero width when absent. For example,
  1416. \"%?-12t\" will result in a 12 character time field if a time of the
  1417. day is specified, but will completely disappear in entries which do
  1418. not contain a time.
  1419. If there is punctuation or whitespace character just before the final
  1420. format letter, this character will be appended to the field value if
  1421. the value is not empty. For example, the format \"%-12:c\" leads to
  1422. \"Diary: \" if the category is \"Diary\". If the category were be
  1423. empty, no additional colon would be interted.
  1424. The default value of this option is \" %-12:c%?-12t% s\", meaning:
  1425. - Indent the line with two space characters
  1426. - Give the category in a 12 chars wide field, padded with whitespace on
  1427. the right (because of `-'). Append a colon if there is a category
  1428. (because of `:').
  1429. - If there is a time-of-day, put it into a 12 chars wide field. If no
  1430. time, don't put in an empty field, just skip it (because of '?').
  1431. - Finally, put the scheduling information and append a whitespace.
  1432. As another example, if you don't want the time-of-day of entries in
  1433. the prefix, you could use:
  1434. (setq org-agenda-prefix-format \" %-11:c% s\")
  1435. See also the variables `org-agenda-remove-times-when-in-prefix' and
  1436. `org-agenda-remove-tags-when-in-prefix'."
  1437. :type 'string
  1438. :group 'org-agenda-prefix)
  1439. (defvar org-prefix-format-compiled nil
  1440. "The compiled version of the most recently used prefix format.
  1441. Depending on which command was used last, this may be the compiled version
  1442. of `org-agenda-prefix-format' or `org-timeline-prefix-format'.")
  1443. (defcustom org-agenda-remove-times-when-in-prefix t
  1444. "Non-nil means, remove duplicate time specifications in agenda items.
  1445. When the format `org-agenda-prefix-format' contains a `%t' specifier, a
  1446. time-of-day specification in a headline or diary entry is extracted and
  1447. placed into the prefix. If this option is non-nil, the original specification
  1448. \(a timestamp or -range, or just a plain time(range) specification like
  1449. 11:30-4pm) will be removed for agenda display. This makes the agenda less
  1450. cluttered.
  1451. The option can be t or nil. It may also be the symbol `beg', indicating
  1452. that the time should only be removed what it is located at the beginning of
  1453. the headline/diary entry."
  1454. :group 'org-agenda-prefix
  1455. :type '(choice
  1456. (const :tag "Always" t)
  1457. (const :tag "Never" nil)
  1458. (const :tag "When at beginning of entry" beg)))
  1459. (defcustom org-agenda-remove-tags-when-in-prefix nil
  1460. "Non-nil means, remove the tags from the headline copy in the agenda.
  1461. When this is the symbol `prefix', only remove tags when
  1462. `org-agenda-prefix-format' contains a `%T' specifier."
  1463. :group 'org-agenda-prefix
  1464. :type '(choice
  1465. (const :tag "Always" t)
  1466. (const :tag "Never" nil)
  1467. (const :tag "When prefix format contains %T" prefix)))
  1468. (defgroup org-agenda-timeline nil
  1469. "Options concerning the timeline buffer in Org Mode."
  1470. :tag "Org Agenda Timeline"
  1471. :group 'org-agenda)
  1472. (defcustom org-timeline-prefix-format " % s"
  1473. "Like `org-agenda-prefix-format', but for the timeline of a single file."
  1474. :type 'string
  1475. :group 'org-agenda-timeline)
  1476. (defcustom org-select-timeline-window t
  1477. "Non-nil means, after creating a timeline, move cursor into Timeline window.
  1478. When nil, cursor will remain in the current window."
  1479. :group 'org-agenda-timeline
  1480. :type 'boolean)
  1481. (defcustom org-timeline-show-empty-dates 3
  1482. "Non-nil means, `org-timeline' also shows dates without an entry.
  1483. When nil, only the days which actually have entries are shown.
  1484. When t, all days between the first and the last date are shown.
  1485. When an integer, show also empty dates, but if there is a gap of more than
  1486. N days, just insert a special line indicating the size of the gap."
  1487. :group 'org-agenda-timeline
  1488. :type '(choice
  1489. (const :tag "None" nil)
  1490. (const :tag "All" t)
  1491. (number :tag "at most")))
  1492. (defgroup org-export nil
  1493. "Options for exporting org-listings."
  1494. :tag "Org Export"
  1495. :group 'org)
  1496. (defgroup org-export-general nil
  1497. "General options for exporting Org-mode files."
  1498. :tag "Org Export General"
  1499. :group 'org-export)
  1500. (defcustom org-export-publishing-directory "."
  1501. "Path to the location where exported files should be located.
  1502. This path may be relative to the directory where the Org-mode file lives.
  1503. The default is to put them into the same directory as the Org-mode file.
  1504. The variable may also be an alist with export types `:html', `:ascii',
  1505. `:ical', or `:xoxo' and the corresponding directories. If a direcoty path
  1506. is relative, it is interpreted relative to the directory where the exported
  1507. Org-mode files lives."
  1508. :group 'org-export-general
  1509. :type '(choice
  1510. (directory)
  1511. (repeat
  1512. (cons
  1513. (choice :tag "Type"
  1514. (const :html) (const :ascii) (const :ical) (const :xoxo))
  1515. (directory)))))
  1516. (defcustom org-export-language-setup
  1517. '(("en" "Author" "Date" "Table of Contents")
  1518. ("da" "Ophavsmand" "Dato" "Indhold")
  1519. ("de" "Autor" "Datum" "Inhaltsverzeichnis")
  1520. ("es" "Autor" "Fecha" "\xccndice")
  1521. ("fr" "Auteur" "Date" "Table des Mati\xe8res")
  1522. ("it" "Autore" "Data" "Indice")
  1523. ("nl" "Auteur" "Datum" "Inhoudsopgave")
  1524. ("nn" "Forfattar" "Dato" "Innhold") ;; nn = Norsk (nynorsk)
  1525. ("sv" "F\xf6rfattarens" "Datum" "Inneh\xe5ll"))
  1526. "Terms used in export text, translated to different languages.
  1527. Use the variable `org-export-default-language' to set the language,
  1528. or use the +OPTION lines for a per-file setting."
  1529. :group 'org-export-general
  1530. :type '(repeat
  1531. (list
  1532. (string :tag "HTML language tag")
  1533. (string :tag "Author")
  1534. (string :tag "Date")
  1535. (string :tag "Table of Contents"))))
  1536. (defcustom org-export-default-language "en"
  1537. "The default language of HTML export, as a string.
  1538. This should have an association in `org-export-language-setup'."
  1539. :group 'org-export-general
  1540. :type 'string)
  1541. (defcustom org-export-headline-levels 3
  1542. "The last level which is still exported as a headline.
  1543. Inferior levels will produce itemize lists when exported.
  1544. Note that a numeric prefix argument to an exporter function overrides
  1545. this setting.
  1546. This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
  1547. :group 'org-export-general
  1548. :type 'number)
  1549. (defcustom org-export-with-section-numbers t
  1550. "Non-nil means, add section numbers to headlines when exporting.
  1551. This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
  1552. :group 'org-export-general
  1553. :type 'boolean)
  1554. (defcustom org-export-with-toc t
  1555. "Non-nil means, create a table of contents in exported files.
  1556. The TOC contains headlines with levels up to`org-export-headline-levels'.
  1557. Headlines which contain any TODO items will be marked with \"(*)\" in
  1558. ASCII export, and with red color in HTML output.
  1559. In HTML output, the TOC will be clickable.
  1560. This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"."
  1561. :group 'org-export-general
  1562. :type 'boolean)
  1563. (defcustom org-export-mark-todo-in-toc nil
  1564. "Non-nil means, mark TOC lines that contain any open TODO items."
  1565. :group 'org-export-general
  1566. :type 'boolean)
  1567. (defcustom org-export-preserve-breaks nil
  1568. "Non-nil means, preserve all line breaks when exporting.
  1569. Normally, in HTML output paragraphs will be reformatted. In ASCII
  1570. export, line breaks will always be preserved, regardless of this variable.
  1571. This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
  1572. :group 'org-export-general
  1573. :type 'boolean)
  1574. (defcustom org-export-with-timestamps t
  1575. "Nil means, do not export time stamps and associated keywords."
  1576. :group 'org-export
  1577. :type 'boolean)
  1578. (defcustom org-export-with-tags t
  1579. "Nil means, do not export tags, just remove them from headlines."
  1580. :group 'org-export-general
  1581. :type 'boolean)
  1582. (defcustom org-export-with-timestamps t
  1583. "Nil means, do not export timestamps and associated keywords."
  1584. :group 'org-export-general
  1585. :type 'boolean)
  1586. (defgroup org-export-translation nil
  1587. "Options for translating special ascii sequences for the export backends."
  1588. :tag "Org Export Translation"
  1589. :group 'org-export)
  1590. (defcustom org-export-with-emphasize t
  1591. "Non-nil means, interpret *word*, /word/, and _word_ as emphasized text.
  1592. If the export target supports emphasizing text, the word will be
  1593. typeset in bold, italic, or underlined, respectively. Works only for
  1594. single words, but you can say: I *really* *mean* *this*.
  1595. Not all export backends support this.
  1596. This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
  1597. :group 'org-export-translation
  1598. :type 'boolean)
  1599. (defcustom org-export-with-sub-superscripts t
  1600. "Non-nil means, interpret \"_\" and \"^\" for export.
  1601. When this option is turned on, you can use TeX-like syntax for sub- and
  1602. superscripts. Several characters after \"_\" or \"^\" will be
  1603. considered as a single item - so grouping with {} is normally not
  1604. needed. For example, the following things will be parsed as single
  1605. sub- or superscripts.
  1606. 10^24 or 10^tau several digits will be considered 1 item.
  1607. 10^-12 or 10^-tau a leading sign with digits or a word
  1608. x^2-y^3 will be read as x^2 - y^3, because items are
  1609. terminated by almost any nonword/nondigit char.
  1610. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  1611. Still, ambiguity is possible - so when in doubt use {} to enclose the
  1612. sub/superscript.
  1613. Not all export backends support this, but HTML does.
  1614. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  1615. :group 'org-export-translation
  1616. :type 'boolean)
  1617. (defcustom org-export-with-TeX-macros t
  1618. "Non-nil means, interpret simple TeX-like macros when exporting.
  1619. For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
  1620. No only real TeX macros will work here, but the standard HTML entities
  1621. for math can be used as macro names as well. For a list of supported
  1622. names in HTML export, see the constant `org-html-entities'.
  1623. Not all export backends support this.
  1624. This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
  1625. :group 'org-export-translation
  1626. :type 'boolean)
  1627. (defcustom org-export-with-fixed-width t
  1628. "Non-nil means, lines starting with \":\" will be in fixed width font.
  1629. This can be used to have pre-formatted text, fragments of code etc. For
  1630. example:
  1631. : ;; Some Lisp examples
  1632. : (while (defc cnt)
  1633. : (ding))
  1634. will be looking just like this in also HTML. See also the QUOTE keyword.
  1635. Not all export backends support this.
  1636. This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
  1637. :group 'org-export-translation
  1638. :type 'boolean)
  1639. (defcustom org-match-sexp-depth 3
  1640. "Number of stacked braces for sub/superscript matching.
  1641. This has to be set before loading org.el to be effective."
  1642. :group 'org-export-translation
  1643. :type 'integer)
  1644. (defgroup org-export-tables nil
  1645. "Options for exporting tables in Org-mode."
  1646. :tag "Org Export Tables"
  1647. :group 'org-export)
  1648. (defcustom org-export-with-tables t
  1649. "If non-nil, lines starting with \"|\" define a table.
  1650. For example:
  1651. | Name | Address | Birthday |
  1652. |-------------+----------+-----------|
  1653. | Arthur Dent | England | 29.2.2100 |
  1654. Not all export backends support this.
  1655. This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
  1656. :group 'org-export-tables
  1657. :type 'boolean)
  1658. (defcustom org-export-highlight-first-table-line t
  1659. "Non-nil means, highlight the first table line.
  1660. In HTML export, this means use <th> instead of <td>.
  1661. In tables created with table.el, this applies to the first table line.
  1662. In Org-mode tables, all lines before the first horizontal separator
  1663. line will be formatted with <th> tags."
  1664. :group 'org-export-tables
  1665. :type 'boolean)
  1666. (defcustom org-export-table-remove-special-lines t
  1667. "Remove special lines and marking characters in calculating tables.
  1668. This removes the special marking character column from tables that are set
  1669. up for spreadsheet calculations. It also removes the entire lines
  1670. marked with `!', `_', or `^'. The lines with `$' are kept, because
  1671. the values of constants may be useful to have."
  1672. :group 'org-export-tables
  1673. :type 'boolean)
  1674. (defcustom org-export-prefer-native-exporter-for-tables nil
  1675. "Non-nil means, always export tables created with table.el natively.
  1676. Natively means, use the HTML code generator in table.el.
  1677. When nil, Org-mode's own HTML generator is used when possible (i.e. if
  1678. the table does not use row- or column-spanning). This has the
  1679. advantage, that the automatic HTML conversions for math symbols and
  1680. sub/superscripts can be applied. Org-mode's HTML generator is also
  1681. much faster."
  1682. :group 'org-export-tables
  1683. :type 'boolean)
  1684. (defgroup org-export-ascii nil
  1685. "Options specific for ASCII export of Org-mode files."
  1686. :tag "Org Export ASCII"
  1687. :group 'org-export)
  1688. (defcustom org-export-ascii-underline '(?\$ ?\# ?^ ?\~ ?\= ?\-)
  1689. "Characters for underlining headings in ASCII export.
  1690. In the given sequence, these characters will be used for level 1, 2, ..."
  1691. :group 'org-export-ascii
  1692. :type '(repeat character))
  1693. (defcustom org-export-ascii-bullets '(?* ?+ ?-)
  1694. "Bullet characters for headlines converted to lists in ASCII export.
  1695. The first character is is used for the first lest level generated in this
  1696. way, and so on. If there are more levels than characters given here,
  1697. the list will be repeated.
  1698. Note that plain lists will keep the same bullets as the have in the
  1699. Org-mode file."
  1700. :group 'org-export-ascii
  1701. :type '(repeat character))
  1702. (defcustom org-export-ascii-show-new-buffer t
  1703. "Non-nil means, popup buffer containing the exported ASCII text.
  1704. Otherwise the buffer will just be saved to a file and stay hidden."
  1705. :group 'org-export-ascii
  1706. :type 'boolean)
  1707. (defgroup org-export-xml nil
  1708. "Options specific for XML export of Org-mode files."
  1709. :tag "Org Export XML"
  1710. :group 'org-export)
  1711. (defgroup org-export-html nil
  1712. "Options specific for HTML export of Org-mode files."
  1713. :tag "Org Export HTML"
  1714. :group 'org-export)
  1715. (defcustom org-export-html-style
  1716. "<style type=\"text/css\">
  1717. html {
  1718. font-family: Times, serif;
  1719. font-size: 12pt;
  1720. }
  1721. .title { text-align: center; }
  1722. .todo { color: red; }
  1723. .done { color: green; }
  1724. .timestamp { color: grey }
  1725. .timestamp-kwd { color: CadetBlue }
  1726. .tag { background-color:lightblue; font-weight:normal }
  1727. .target { background-color: lavender; }
  1728. pre {
  1729. border: 1pt solid #AEBDCC;
  1730. background-color: #F3F5F7;
  1731. padding: 5pt;
  1732. font-family: courier, monospace;
  1733. }
  1734. table { border-collapse: collapse; }
  1735. td, th {
  1736. vertical-align: top;
  1737. border: 1pt solid #ADB9CC;
  1738. }
  1739. </style>"
  1740. "The default style specification for exported HTML files.
  1741. Since there are different ways of setting style information, this variable
  1742. needs to contain the full HTML structure to provide a style, including the
  1743. surrounding HTML tags. The style specifications should include definitions
  1744. for new classes todo, done, title, and deadline. For example, legal values
  1745. would be:
  1746. <style type=\"text/css\">
  1747. p { font-weight: normal; color: gray; }
  1748. h1 { color: black; }
  1749. .title { text-align: center; }
  1750. .todo, .deadline { color: red; }
  1751. .done { color: green; }
  1752. </style>
  1753. or, if you want to keep the style in a file,
  1754. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  1755. As the value of this option simply gets inserted into the HTML <head> header,
  1756. you can \"misuse\" it to add arbitrary text to the header."
  1757. :group 'org-export-html
  1758. :type 'string)
  1759. (defcustom org-export-html-link-org-files-as-html t
  1760. "Non-nil means, make file links to `file.org' point to `file.html'.
  1761. When org-mode is exporting an org-mode file to HTML, links to
  1762. non-html files are directly put into a href tag in HTML.
  1763. However, links to other Org-mode files (recognized by the
  1764. extension `.org.) should become links to the corresponding html
  1765. file, assuming that the linked org-mode file will also be
  1766. converted to HTML.
  1767. When nil, the links still point to the plain `.org' file."
  1768. :group 'org-export-html
  1769. :type 'boolean)
  1770. (defcustom org-export-html-inline-images 'maybe
  1771. "Non-nil means, inline images into exported HTML pages.
  1772. This is done using an <img> tag. When nil, an anchor with href is used to
  1773. link to the image. If this option is `maybe', then images in links with
  1774. an empty description will be inlined, while images with a description will
  1775. be linked only."
  1776. :group 'org-export-html
  1777. :type '(choice (const :tag "Never" nil)
  1778. (const :tag "Always" t)
  1779. (const :tag "When there is no description" maybe)))
  1780. (defcustom org-export-html-expand t
  1781. "Non-nil means, for HTML export, treat @<...> as HTML tag.
  1782. When nil, these tags will be exported as plain text and therefore
  1783. not be interpreted by a browser.
  1784. This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
  1785. :group 'org-export-html
  1786. :type 'boolean)
  1787. (defcustom org-export-html-table-tag
  1788. "<table border=\"1\" cellspacing=\"0\" cellpadding=\"6\">"
  1789. "The HTML tag used to start a table.
  1790. This must be a <table> tag, but you may change the options like
  1791. borders and spacing."
  1792. :group 'org-export-html
  1793. :type 'string)
  1794. (defcustom org-export-html-with-timestamp nil
  1795. "If non-nil, write `org-export-html-html-helper-timestamp'
  1796. into the exported HTML text. Otherwise, the buffer will just be saved
  1797. to a file."
  1798. :group 'org-export-html
  1799. :type 'boolean)
  1800. (defcustom org-export-html-html-helper-timestamp
  1801. "<br/><br/><hr><p><!-- hhmts start --> <!-- hhmts end --></p>\n"
  1802. "The HTML tag used as timestamp delimiter for HTML-helper-mode."
  1803. :group 'org-export-html
  1804. :type 'string)
  1805. (defcustom org-export-html-show-new-buffer nil
  1806. "Non-nil means, popup buffer containing the exported html text.
  1807. Otherwise, the buffer will just be saved to a file and stay hidden."
  1808. :group 'org-export-html
  1809. :type 'boolean)
  1810. (defgroup org-export-icalendar nil
  1811. "Options specific for iCalendar export of Org-mode files."
  1812. :tag "Org Export iCalendar"
  1813. :group 'org-export)
  1814. (defcustom org-combined-agenda-icalendar-file "~/org.ics"
  1815. "The file name for the iCalendar file covering all agenda files.
  1816. This file is created with the command \\[org-export-icalendar-all-agenda-files].
  1817. The file name should be absolute."
  1818. :group 'org-export-icalendar
  1819. :type 'file)
  1820. (defcustom org-icalendar-include-todo nil
  1821. "Non-nil means, export to iCalendar files should also cover TODO items."
  1822. :group 'org-export-icalendar
  1823. :type 'boolean)
  1824. (defcustom org-icalendar-combined-name "OrgMode"
  1825. "Calendar name for the combined iCalendar representing all agenda files."
  1826. :group 'org-export-icalendar
  1827. :type 'string)
  1828. (defgroup org-font-lock nil
  1829. "Font-lock settings for highlighting in Org-mode."
  1830. :tag "Org Font Lock"
  1831. :group 'org)
  1832. (defcustom org-level-color-stars-only nil
  1833. "Non-nil means fontify only the stars in each headline.
  1834. When nil, the entire headline is fontified.
  1835. Changing it requires restart of `font-lock-mode' to become effective
  1836. also in regions already fontified."
  1837. :group 'org-font-lock
  1838. :type 'boolean)
  1839. (defcustom org-hide-leading-stars nil
  1840. "Non-nil means, hide the first N-1 stars in a headline.
  1841. This works by using the face `org-hide' for these stars. This
  1842. face is white for a light background, and black for a dark
  1843. background. You may have to customize the face `org-hide' to
  1844. make this work.
  1845. Changing it requires restart of `font-lock-mode' to become effective
  1846. also in regions already fontified.
  1847. You may also set this on a per-file basis by adding one of the following
  1848. lines to the buffer:
  1849. #+STARTUP: hidestars
  1850. #+STARTUP: showstars"
  1851. :group 'org-font-lock
  1852. :type 'boolean)
  1853. (defcustom org-fontify-done-headline nil
  1854. "Non-nil means, change the face of a headline if it is marked DONE.
  1855. Normally, only the TODO/DONE keyword indicates the state of a headline.
  1856. When this is non-nil, the headline after the keyword is set to the
  1857. `org-headline-done' as an additional indication."
  1858. :group 'org-font-lock
  1859. :type 'boolean)
  1860. (defcustom org-fontify-emphasized-text t
  1861. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  1862. Changing this variable requires a restart of Emacs to take effect."
  1863. :group 'org-font-lock
  1864. :type 'boolean)
  1865. (defvar org-emph-re nil
  1866. "Regular expression for matching emphasis.")
  1867. (defvar org-emphasis-regexp-components) ; defined just below
  1868. (defvar org-emphasis-alist) ; defined just below
  1869. (defun org-set-emph-re (var val)
  1870. "Set variable and compute the emphasis regular expression."
  1871. (set var val)
  1872. (when (and (boundp 'org-emphasis-alist)
  1873. (boundp 'org-emphasis-regexp-components)
  1874. org-emphasis-alist org-emphasis-regexp-components)
  1875. (let* ((e org-emphasis-regexp-components)
  1876. (pre (car e))
  1877. (post (nth 1 e))
  1878. (border (nth 2 e))
  1879. (body (nth 3 e))
  1880. (nl (nth 4 e))
  1881. (stacked (nth 5 e))
  1882. (body1 (concat body "*?"))
  1883. (markers (mapconcat 'car org-emphasis-alist "")))
  1884. ;; make sure special characters appear at the right position in the class
  1885. (if (string-match "\\^" markers)
  1886. (setq markers (concat (replace-match "" t t markers) "^")))
  1887. (if (string-match "-" markers)
  1888. (setq markers (concat (replace-match "" t t markers) "-")))
  1889. (while (>= (setq nl (1- nl)) 0) (setq body1 (concat body1 "\n?" body "*?")))
  1890. ;; Make the regexp
  1891. (setq org-emph-re
  1892. (concat "\\([" pre (if stacked markers) "]\\|^\\)"
  1893. "\\("
  1894. "\\([" markers "]\\)"
  1895. "\\("
  1896. "[^" border markers "]"
  1897. body1
  1898. "[^" border markers "]"
  1899. "\\)"
  1900. "\\3\\)"
  1901. "\\([" post (if stacked markers) "]\\|$\\)")))))
  1902. (defcustom org-emphasis-regexp-components
  1903. '(" \t(" " \t.,?;:'\")" " \t\r\n,." "." 1 nil)
  1904. "Components used to build the reqular expression for emphasis.
  1905. This is a list with 6 entries. Terminology: In an emphasis string
  1906. like \" *strong word* \", we call the initial space PREMATCH, the final
  1907. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  1908. and \"trong wor\" is the body. The different components in this variable
  1909. specify what is allowed/forbidden in each part:
  1910. pre Chars allowed as prematch. Beginning of line will be allowed too.
  1911. post Chars allowed as postmatch. End of line will be allowed too.
  1912. border The chars *forbidden* as border characters. In addition to the
  1913. characters given here, all marker characters are forbidden too.
  1914. body-regexp A regexp like \".\" to match a body character. Don't use
  1915. non-shy groups here, and don't allow newline here.
  1916. newline The maximum number of newlines allowed in an emphasis exp.
  1917. stacked Non-nil means, allow stacked styles. This works only in HTML
  1918. export. When this is set, all marker characters (as given in
  1919. `org-emphasis-alist') will be allowed as pre/post, aiding
  1920. inside-out matching.
  1921. Use customize to modify this, or restart emacs after changing it."
  1922. :group 'org-fixme
  1923. :set 'org-set-emph-re
  1924. :type '(list
  1925. (sexp :tag "Allowed chars in pre ")
  1926. (sexp :tag "Allowed chars in post ")
  1927. (sexp :tag "Forbidden chars in border ")
  1928. (sexp :tag "Regexp for body ")
  1929. (integer :tag "number of newlines allowed")
  1930. (boolean :tag "Stacking allowed ")))
  1931. (defcustom org-emphasis-alist
  1932. '(("*" bold "<b>" "</b>")
  1933. ("/" italic "<i>" "</i>")
  1934. ("_" underline "<u>" "</u>")
  1935. ("=" shadow "<code>" "</code>"))
  1936. "Special syntax for emphasised text.
  1937. Text starting and ending with a special character will be emphasized, for
  1938. example *bold*, _underlined_ and /italic/. This variable sets the marker
  1939. characters, the face to bbe used by font-lock for highlighting in Org-mode
  1940. emacs buffers, and the HTML tags to be used for this.
  1941. Use customize to modify this, or restart emacs after changing it."
  1942. :group 'org-fixme
  1943. :set 'org-set-emph-re
  1944. :type '(repeat
  1945. (list
  1946. (string :tag "Marker character")
  1947. (face :tag "Font-lock-face")
  1948. (string :tag "HTML start tag")
  1949. (string :tag "HTML end tag"))))
  1950. (defgroup org-faces nil
  1951. "Faces in Org-mode."
  1952. :tag "Org Faces"
  1953. :group 'org-font-lock)
  1954. (defun org-compatible-face (specs)
  1955. "Make a compatible face specification.
  1956. XEmacs and Emacs 21 do not know about the `min-colors' attribute.
  1957. For them we convert a (min-colors 8) entry to a `tty' entry and move it
  1958. to the top of the list. The `min-colors' attribute will be removed from
  1959. any other entries, and any resulting duplicates will be removed entirely."
  1960. (if (or (featurep 'xemacs) (< emacs-major-version 22))
  1961. (let (r e a)
  1962. (while (setq e (pop specs))
  1963. (cond
  1964. ((memq (car e) '(t default)) (push e r))
  1965. ((setq a (member '(min-colors 8) (car e)))
  1966. (nconc r (list (cons (cons '(type tty) (delq (car a) (car e)))
  1967. (cdr e)))))
  1968. ((setq a (assq 'min-colors (car e)))
  1969. (setq e (cons (delq a (car e)) (cdr e)))
  1970. (or (assoc (car e) r) (push e r)))
  1971. (t (or (assoc (car e) r) (push e r)))))
  1972. (nreverse r))
  1973. specs))
  1974. (defface org-hide
  1975. '((((background light)) (:foreground "white"))
  1976. (((background dark)) (:foreground "black")))
  1977. "Face used to hide leading stars in headlines.
  1978. The forground color of this face should be equal to the background
  1979. color of the frame."
  1980. :group 'org-faces)
  1981. (defface org-level-1 ;; font-lock-function-name-face
  1982. (org-compatible-face
  1983. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  1984. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  1985. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  1986. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  1987. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  1988. (t (:bold t))))
  1989. "Face used for level 1 headlines."
  1990. :group 'org-faces)
  1991. (defface org-level-2 ;; font-lock-variable-name-face
  1992. (org-compatible-face
  1993. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  1994. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  1995. (((class color) (min-colors 8) (background light)) (:foreground "yellow"))
  1996. (((class color) (min-colors 8) (background dark)) (:foreground "yellow" :bold t))
  1997. (t (:bold t))))
  1998. "Face used for level 2 headlines."
  1999. :group 'org-faces)
  2000. (defface org-level-3 ;; font-lock-keyword-face
  2001. (org-compatible-face
  2002. '((((class color) (min-colors 88) (background light)) (:foreground "Purple"))
  2003. (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
  2004. (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
  2005. (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
  2006. (((class color) (min-colors 8) (background light)) (:foreground "purple" :bold t))
  2007. (((class color) (min-colors 8) (background dark)) (:foreground "cyan" :bold t))
  2008. (t (:bold t))))
  2009. "Face used for level 3 headlines."
  2010. :group 'org-faces)
  2011. (defface org-level-4 ;; font-lock-comment-face
  2012. (org-compatible-face
  2013. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2014. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2015. (((class color) (min-colors 16) (background light)) (:foreground "red"))
  2016. (((class color) (min-colors 16) (background dark)) (:foreground "red1"))
  2017. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2018. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2019. (t (:bold t))))
  2020. "Face used for level 4 headlines."
  2021. :group 'org-faces)
  2022. (defface org-level-5 ;; font-lock-type-face
  2023. (org-compatible-face
  2024. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
  2025. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
  2026. (((class color) (min-colors 8)) (:foreground "green"))))
  2027. "Face used for level 5 headlines."
  2028. :group 'org-faces)
  2029. (defface org-level-6 ;; font-lock-constant-face
  2030. (org-compatible-face
  2031. '((((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
  2032. (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
  2033. (((class color) (min-colors 8)) (:foreground "magenta"))))
  2034. "Face used for level 6 headlines."
  2035. :group 'org-faces)
  2036. (defface org-level-7 ;; font-lock-builtin-face
  2037. (org-compatible-face
  2038. '((((class color) (min-colors 16) (background light)) (:foreground "Orchid"))
  2039. (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue"))
  2040. (((class color) (min-colors 8)) (:foreground "blue"))))
  2041. "Face used for level 7 headlines."
  2042. :group 'org-faces)
  2043. (defface org-level-8 ;; font-lock-string-face
  2044. (org-compatible-face
  2045. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2046. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2047. (((class color) (min-colors 8)) (:foreground "green"))))
  2048. "Face used for level 8 headlines."
  2049. :group 'org-faces)
  2050. (defface org-special-keyword ;; font-lock-string-face
  2051. (org-compatible-face
  2052. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2053. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2054. (t (:italic t))))
  2055. "Face used for special keywords."
  2056. :group 'org-faces)
  2057. (defface org-warning ;; font-lock-warning-face
  2058. (org-compatible-face
  2059. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  2060. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  2061. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2062. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2063. (t (:bold t))))
  2064. "Face for deadlines and TODO keywords."
  2065. :group 'org-faces)
  2066. (defface org-headline-done ;; font-lock-string-face
  2067. (org-compatible-face
  2068. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2069. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2070. (((class color) (min-colors 8) (background light)) (:bold nil))))
  2071. "Face used to indicate that a headline is DONE.
  2072. This face is only used if `org-fontify-done-headline' is set."
  2073. :group 'org-faces)
  2074. (defface org-link
  2075. '((((class color) (background light)) (:foreground "Purple" :underline t))
  2076. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  2077. (t (:underline t)))
  2078. "Face for links."
  2079. :group 'org-faces)
  2080. (defface org-date
  2081. '((((class color) (background light)) (:foreground "Purple" :underline t))
  2082. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  2083. (t (:underline t)))
  2084. "Face for links."
  2085. :group 'org-faces)
  2086. (defface org-tag
  2087. '((t (:bold t)))
  2088. "Face for tags."
  2089. :group 'org-faces)
  2090. (defface org-todo ;; font-lock-warning-face
  2091. (org-compatible-face
  2092. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  2093. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  2094. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2095. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2096. (t (:inverse-video t :bold t))))
  2097. "Face for TODO keywords."
  2098. :group 'org-faces)
  2099. (defface org-done ;; font-lock-type-face
  2100. (org-compatible-face
  2101. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
  2102. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
  2103. (((class color) (min-colors 8)) (:foreground "green"))
  2104. (t (:bold t))))
  2105. "Face used for DONE."
  2106. :group 'org-faces)
  2107. (defface org-table ;; font-lock-function-name-face
  2108. (org-compatible-face
  2109. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2110. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2111. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2112. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2113. (((class color) (min-colors 8) (background light)) (:foreground "blue"))
  2114. (((class color) (min-colors 8) (background dark)))))
  2115. "Face used for tables."
  2116. :group 'org-faces)
  2117. (defface org-formula
  2118. (org-compatible-face
  2119. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2120. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2121. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2122. (((class color) (min-colors 8) (background dark)) (:foreground "red"))
  2123. (t (:bold t :italic t))))
  2124. "Face for formulas."
  2125. :group 'org-faces)
  2126. (defface org-scheduled-today
  2127. (org-compatible-face
  2128. '((((class color) (min-colors 88) (background light)) (:foreground "DarkGreen"))
  2129. (((class color) (min-colors 88) (background dark)) (:foreground "PaleGreen"))
  2130. (((class color) (min-colors 8)) (:foreground "green"))
  2131. (t (:bold t :italic t))))
  2132. "Face for items scheduled for a certain day."
  2133. :group 'org-faces)
  2134. (defface org-scheduled-previously
  2135. (org-compatible-face
  2136. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2137. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2138. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2139. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2140. (t (:bold t))))
  2141. "Face for items scheduled previously, and not yet done."
  2142. :group 'org-faces)
  2143. (defface org-upcoming-deadline
  2144. (org-compatible-face
  2145. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2146. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2147. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2148. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2149. (t (:bold t))))
  2150. "Face for items scheduled previously, and not yet done."
  2151. :group 'org-faces)
  2152. (defface org-time-grid ;; font-lock-variable-name-face
  2153. (org-compatible-face
  2154. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  2155. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  2156. (((class color) (min-colors 8)) (:foreground "yellow" :weight light))))
  2157. "Face used for time grids."
  2158. :group 'org-faces)
  2159. (defconst org-level-faces
  2160. '(org-level-1 org-level-2 org-level-3 org-level-4
  2161. org-level-5 org-level-6 org-level-7 org-level-8
  2162. ))
  2163. (defconst org-n-levels (length org-level-faces))
  2164. ;; Variables for pre-computed regular expressions, all buffer local
  2165. (defvar org-done-string nil
  2166. "The last string in `org-todo-keywords', indicating an item is DONE.")
  2167. (make-variable-buffer-local 'org-done-string)
  2168. (defvar org-todo-regexp nil
  2169. "Matches any of the TODO state keywords.")
  2170. (make-variable-buffer-local 'org-todo-regexp)
  2171. (defvar org-not-done-regexp nil
  2172. "Matches any of the TODO state keywords except the last one.")
  2173. (make-variable-buffer-local 'org-not-done-regexp)
  2174. (defvar org-todo-line-regexp nil
  2175. "Matches a headline and puts TODO state into group 2 if present.")
  2176. (make-variable-buffer-local 'org-todo-line-regexp)
  2177. (defvar org-todo-line-tags-regexp nil
  2178. "Matches a headline and puts TODO state into group 2 if present.
  2179. Also put tags into group 4 if tags are present.")
  2180. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2181. (defvar org-nl-done-regexp nil
  2182. "Matches newline followed by a headline with the DONE keyword.")
  2183. (make-variable-buffer-local 'org-nl-done-regexp)
  2184. (defvar org-looking-at-done-regexp nil
  2185. "Matches the DONE keyword a point.")
  2186. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2187. (defvar org-todo-kwd-priority-p nil
  2188. "Do TODO items have priorities?")
  2189. (make-variable-buffer-local 'org-todo-kwd-priority-p)
  2190. (defvar org-todo-kwd-max-priority nil
  2191. "Maximum priority of TODO items.")
  2192. (make-variable-buffer-local 'org-todo-kwd-max-priority)
  2193. (defvar org-ds-keyword-length 12
  2194. "Maximum length of the Deadline and SCHEDULED keywords.")
  2195. (make-variable-buffer-local 'org-ds-keyword-length)
  2196. (defvar org-deadline-regexp nil
  2197. "Matches the DEADLINE keyword.")
  2198. (make-variable-buffer-local 'org-deadline-regexp)
  2199. (defvar org-deadline-time-regexp nil
  2200. "Matches the DEADLINE keyword together with a time stamp.")
  2201. (make-variable-buffer-local 'org-deadline-time-regexp)
  2202. (defvar org-deadline-line-regexp nil
  2203. "Matches the DEADLINE keyword and the rest of the line.")
  2204. (make-variable-buffer-local 'org-deadline-line-regexp)
  2205. (defvar org-scheduled-regexp nil
  2206. "Matches the SCHEDULED keyword.")
  2207. (make-variable-buffer-local 'org-scheduled-regexp)
  2208. (defvar org-scheduled-time-regexp nil
  2209. "Matches the SCHEDULED keyword together with a time stamp.")
  2210. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2211. (defvar org-closed-time-regexp nil
  2212. "Matches the CLOSED keyword together with a time stamp.")
  2213. (make-variable-buffer-local 'org-closed-time-regexp)
  2214. (defvar org-keyword-time-regexp nil
  2215. "Matches any of the 3 keywords, together with the time stamp.")
  2216. (make-variable-buffer-local 'org-keyword-time-regexp)
  2217. (defvar org-maybe-keyword-time-regexp nil
  2218. "Matches a timestamp, possibly preceeded by a keyword.")
  2219. (make-variable-buffer-local 'org-keyword-time-regexp)
  2220. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  2221. mouse-map t)
  2222. "Properties to remove when a string without properties is wanted.")
  2223. (defsubst org-match-string-no-properties (num &optional string)
  2224. (if (featurep 'xemacs)
  2225. (let ((s (match-string num string)))
  2226. (remove-text-properties 0 (length s) org-rm-props s)
  2227. s)
  2228. (match-string-no-properties num string)))
  2229. (defsubst org-no-properties (s)
  2230. (remove-text-properties 0 (length s) org-rm-props s)
  2231. s)
  2232. (defun org-set-regexps-and-options ()
  2233. "Precompute regular expressions for current buffer."
  2234. (when (eq major-mode 'org-mode)
  2235. (let ((re (org-make-options-regexp
  2236. '("CATEGORY" "SEQ_TODO" "PRI_TODO" "TYP_TODO"
  2237. "STARTUP" "ARCHIVE" "TAGS")))
  2238. (splitre "[ \t]+")
  2239. kwds int key value cat arch tags)
  2240. (save-excursion
  2241. (save-restriction
  2242. (widen)
  2243. (goto-char (point-min))
  2244. (while (re-search-forward re nil t)
  2245. (setq key (match-string 1) value (org-match-string-no-properties 2))
  2246. (cond
  2247. ((equal key "CATEGORY")
  2248. (if (string-match "[ \t]+$" value)
  2249. (setq value (replace-match "" t t value)))
  2250. (setq cat (intern value)))
  2251. ((equal key "SEQ_TODO")
  2252. (setq int 'sequence
  2253. kwds (append kwds (org-split-string value splitre))))
  2254. ((equal key "PRI_TODO")
  2255. (setq int 'priority
  2256. kwds (append kwds (org-split-string value splitre))))
  2257. ((equal key "TYP_TODO")
  2258. (setq int 'type
  2259. kwds (append kwds (org-split-string value splitre))))
  2260. ((equal key "TAGS")
  2261. (setq tags (append tags (org-split-string value splitre))))
  2262. ((equal key "STARTUP")
  2263. (let ((opts (org-split-string value splitre))
  2264. (set '(("fold" org-startup-folded t)
  2265. ("overview" org-startup-folded t)
  2266. ("nofold" org-startup-folded nil)
  2267. ("showall" org-startup-folded nil)
  2268. ("content" org-startup-folded content)
  2269. ("hidestars" org-hide-leading-stars t)
  2270. ("showstars" org-hide-leading-stars nil)
  2271. ("odd" org-odd-levels-only t)
  2272. ("oddeven" org-odd-levels-only nil)
  2273. ("align" org-startup-align-all-tables t)
  2274. ("noalign" org-startup-align-all-tables nil)
  2275. ("logging" org-log-done t)
  2276. ("nologging" org-log-done nil)
  2277. ("dlcheck" org-startup-with-deadline-check t)
  2278. ("nodlcheck" org-startup-with-deadline-check nil)))
  2279. l var val)
  2280. (while (setq l (assoc (pop opts) set))
  2281. (setq var (nth 1 l) val (nth 2 l))
  2282. (set (make-local-variable var) val))))
  2283. ((equal key "ARCHIVE")
  2284. (string-match " *$" value)
  2285. (setq arch (replace-match "" t t value))
  2286. (remove-text-properties 0 (length arch)
  2287. '(face t fontified t) arch)))
  2288. )))
  2289. (and cat (set (make-local-variable 'org-category) cat))
  2290. (and kwds (set (make-local-variable 'org-todo-keywords) kwds))
  2291. (and arch (set (make-local-variable 'org-archive-location) arch))
  2292. (and int (set (make-local-variable 'org-todo-interpretation) int))
  2293. (when tags
  2294. (let (e tg c tgs)
  2295. (while (setq e (pop tags))
  2296. (cond
  2297. ((equal e "{") (push '(:startgroup) tgs))
  2298. ((equal e "}") (push '(:endgroup) tgs))
  2299. ((string-match "^\\([0-9a-zA-Z_@]+\\)(\\(.\\))$" e)
  2300. (push (cons (match-string 1 e)
  2301. (string-to-char (match-string 2 e)))
  2302. tgs))
  2303. (t (push (list e) tgs))))
  2304. (set (make-local-variable 'org-tag-alist) nil)
  2305. (while (setq e (pop tgs))
  2306. (or (and (stringp (car e))
  2307. (assoc (car e) org-tag-alist))
  2308. (push e org-tag-alist))))))
  2309. ;; Compute the regular expressions and other local variables
  2310. (setq org-todo-kwd-priority-p (equal org-todo-interpretation 'priority)
  2311. org-todo-kwd-max-priority (1- (length org-todo-keywords))
  2312. org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2313. (length org-scheduled-string)))
  2314. org-done-string
  2315. (nth (1- (length org-todo-keywords)) org-todo-keywords)
  2316. org-todo-regexp
  2317. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords
  2318. "\\|") "\\)\\>")
  2319. org-not-done-regexp
  2320. (concat "\\<\\("
  2321. (mapconcat 'regexp-quote
  2322. (nreverse (cdr (reverse org-todo-keywords)))
  2323. "\\|")
  2324. "\\)\\>")
  2325. org-todo-line-regexp
  2326. (concat "^\\(\\*+\\)[ \t]*\\("
  2327. (mapconcat 'regexp-quote org-todo-keywords "\\|")
  2328. "\\)? *\\(.*\\)")
  2329. org-nl-done-regexp
  2330. (concat "[\r\n]\\*+[ \t]+" org-done-string "\\>")
  2331. org-todo-line-tags-regexp
  2332. (concat "^\\(\\*+\\)[ \t]*\\("
  2333. (mapconcat 'regexp-quote org-todo-keywords "\\|")
  2334. "\\)? *\\(.*?\\([ \t]:[a-zA-Z0-9:_@]+:[ \t]*\\)?$\\)")
  2335. org-looking-at-done-regexp (concat "^" org-done-string "\\>")
  2336. org-deadline-regexp (concat "\\<" org-deadline-string)
  2337. org-deadline-time-regexp
  2338. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2339. org-deadline-line-regexp
  2340. (concat "\\<\\(" org-deadline-string "\\).*")
  2341. org-scheduled-regexp
  2342. (concat "\\<" org-scheduled-string)
  2343. org-scheduled-time-regexp
  2344. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2345. org-closed-time-regexp
  2346. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2347. org-keyword-time-regexp
  2348. (concat "\\<\\(" org-scheduled-string
  2349. "\\|" org-deadline-string
  2350. "\\|" org-closed-string
  2351. "\\|" org-clock-string "\\)"
  2352. " *[[<]\\([^]>]+\\)[]>]")
  2353. org-maybe-keyword-time-regexp
  2354. (concat "\\(\\<\\(" org-scheduled-string
  2355. "\\|" org-deadline-string
  2356. "\\|" org-closed-string
  2357. "\\|" org-clock-string "\\)\\)?"
  2358. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}[^]\r\n>]*?[]>]\\)"))
  2359. (org-set-font-lock-defaults)))
  2360. ;; Tell the compiler about dynamically scoped variables,
  2361. ;; and variables from other packages
  2362. (defvar calc-embedded-close-formula) ; defined by the calc package
  2363. (defvar calc-embedded-open-formula) ; defined by the calc package
  2364. (defvar font-lock-unfontify-region-function) ; defined by font-lock.el
  2365. (defvar zmacs-regions) ; XEmacs regions
  2366. (defvar original-date) ; dynamically scoped in calendar
  2367. (defvar org-old-auto-fill-inhibit-regexp) ; local variable used by `orgtbl-mode'
  2368. (defvar orgtbl-mode-menu) ; defined when orgtbl mode get initialized
  2369. (defvar org-html-entities) ; defined later in this file
  2370. (defvar org-goto-start-pos) ; dynamically scoped parameter
  2371. (defvar org-time-was-given) ; dynamically scoped parameter
  2372. (defvar org-ts-what) ; dynamically scoped parameter
  2373. (defvar mark-active) ; Emacs only, not available in XEmacs.
  2374. (defvar timecnt) ; dynamically scoped parameter
  2375. (defvar levels-open) ; dynamically scoped parameter
  2376. (defvar entry) ; dynamically scoped parameter
  2377. (defvar state) ; dynamically scoped into `org-after-todo-state-change-hook'
  2378. (defvar date) ; dynamically scoped parameter
  2379. (defvar description) ; dynamically scoped parameter
  2380. (defvar ans1) ; dynamically scoped parameter
  2381. (defvar ans2) ; dynamically scoped parameter
  2382. (defvar starting-day) ; local variable
  2383. (defvar include-all-loc) ; local variable
  2384. (defvar vm-message-pointer) ; from vm
  2385. (defvar vm-folder-directory) ; from vm
  2386. (defvar wl-summary-buffer-elmo-folder) ; from wanderlust
  2387. (defvar wl-summary-buffer-folder-name) ; from wanderlust
  2388. (defvar gnus-group-name) ; from gnus
  2389. (defvar gnus-article-current) ; from gnus
  2390. (defvar w3m-current-url) ; from w3m
  2391. (defvar mh-progs) ; from MH-E
  2392. (defvar mh-current-folder) ; from MH-E
  2393. (defvar mh-show-folder-buffer) ; from MH-E
  2394. (defvar mh-index-folder) ; from MH-E
  2395. (defvar mh-searcher) ; from MH-E
  2396. (defvar org-selected-point) ; dynamically scoped parameter
  2397. (defvar calendar-mode-map) ; from calendar.el
  2398. (defvar last-arg) ; local variable
  2399. (defvar remember-save-after-remembering) ; from remember.el
  2400. (defvar remember-data-file) ; from remember.el
  2401. (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
  2402. (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
  2403. (defvar orgtbl-mode) ; defined later in this file
  2404. (defvar Info-current-file) ; from info.el
  2405. (defvar Info-current-node) ; from info.el
  2406. ;;; Define the mode
  2407. (defvar org-mode-map
  2408. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  2409. (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.")
  2410. (copy-keymap outline-mode-map))
  2411. "Keymap for Org-mode.")
  2412. (defvar org-struct-menu) ; defined later in this file
  2413. (defvar org-org-menu) ; defined later in this file
  2414. (defvar org-tbl-menu) ; defined later in this file
  2415. ;; We use a before-change function to check if a table might need
  2416. ;; an update.
  2417. (defvar org-table-may-need-update t
  2418. "Indicates that a table might need an update.
  2419. This variable is set by `org-before-change-function'.
  2420. `org-table-align' sets it back to nil.")
  2421. (defvar org-mode-hook nil)
  2422. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  2423. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  2424. ;;;###autoload
  2425. (define-derived-mode org-mode outline-mode "Org"
  2426. "Outline-based notes management and organizer, alias
  2427. \"Carsten's outline-mode for keeping track of everything.\"
  2428. Org-mode develops organizational tasks around a NOTES file which
  2429. contains information about projects as plain text. Org-mode is
  2430. implemented on top of outline-mode, which is ideal to keep the content
  2431. of large files well structured. It supports ToDo items, deadlines and
  2432. time stamps, which magically appear in the diary listing of the Emacs
  2433. calendar. Tables are easily created with a built-in table editor.
  2434. Plain text URL-like links connect to websites, emails (VM), Usenet
  2435. messages (Gnus), BBDB entries, and any files related to the project.
  2436. For printing and sharing of notes, an Org-mode file (or a part of it)
  2437. can be exported as a structured ASCII or HTML file.
  2438. The following commands are available:
  2439. \\{org-mode-map}"
  2440. ;; Get rid of Outline menus, they are not needed
  2441. ;; Need to do this here because define-derived-mode sets up
  2442. ;; the keymap so late.
  2443. (if (featurep 'xemacs)
  2444. (if org-noutline-p
  2445. (progn
  2446. (easy-menu-remove outline-mode-menu-heading)
  2447. (easy-menu-remove outline-mode-menu-show)
  2448. (easy-menu-remove outline-mode-menu-hide))
  2449. (delete-menu-item '("Headings"))
  2450. (delete-menu-item '("Show"))
  2451. (delete-menu-item '("Hide"))
  2452. (set-menubar-dirty-flag))
  2453. (define-key org-mode-map [menu-bar headings] 'undefined)
  2454. (define-key org-mode-map [menu-bar hide] 'undefined)
  2455. (define-key org-mode-map [menu-bar show] 'undefined))
  2456. (easy-menu-add org-org-menu)
  2457. (easy-menu-add org-tbl-menu)
  2458. (org-install-agenda-files-menu)
  2459. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  2460. (org-add-to-invisibility-spec '(org-cwidth))
  2461. (when (featurep 'xemacs)
  2462. (set (make-local-variable 'line-move-ignore-invisible) t))
  2463. (setq outline-regexp "\\*+")
  2464. ;;(setq outline-regexp "\\(?:\\*+\\|[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\) \\)")
  2465. (setq outline-level 'org-outline-level)
  2466. (when (and org-ellipsis (stringp org-ellipsis))
  2467. (unless org-display-table
  2468. (setq org-display-table (make-display-table)))
  2469. (set-display-table-slot org-display-table
  2470. 4 (string-to-vector org-ellipsis))
  2471. (setq buffer-display-table org-display-table))
  2472. (org-set-regexps-and-options)
  2473. (modify-syntax-entry ?# "<")
  2474. (if org-startup-truncated (setq truncate-lines t))
  2475. (set (make-local-variable 'font-lock-unfontify-region-function)
  2476. 'org-unfontify-region)
  2477. ;; Activate before-change-function
  2478. (set (make-local-variable 'org-table-may-need-update) t)
  2479. (org-add-hook 'before-change-functions 'org-before-change-function nil
  2480. 'local)
  2481. ;; Check for running clock before killing a buffer
  2482. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  2483. ;; Paragraphs and auto-filling
  2484. (org-set-autofill-regexps)
  2485. (org-update-radio-target-regexp)
  2486. ;; Settings for Calc embedded mode
  2487. (set (make-local-variable 'calc-embedded-open-formula) "|\\|\n")
  2488. (set (make-local-variable 'calc-embedded-close-formula) "|\\|\n")
  2489. (if (and org-insert-mode-line-in-empty-file
  2490. (interactive-p)
  2491. (= (point-min) (point-max)))
  2492. (insert " -*- mode: org -*-\n\n"))
  2493. (unless org-inhibit-startup
  2494. (if org-startup-align-all-tables
  2495. (org-table-map-tables 'org-table-align))
  2496. (if org-startup-with-deadline-check
  2497. (call-interactively 'org-check-deadlines)
  2498. (cond
  2499. ((eq org-startup-folded t)
  2500. (org-cycle '(4)))
  2501. ((eq org-startup-folded 'content)
  2502. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  2503. (org-cycle '(4)) (org-cycle '(4))))))))
  2504. (defsubst org-call-with-arg (command arg)
  2505. "Call COMMAND interactively, but pretend prefix are was ARG."
  2506. (let ((current-prefix-arg arg)) (call-interactively command)))
  2507. (defsubst org-current-line (&optional pos)
  2508. (+ (if (bolp) 1 0) (count-lines (point-min) (or pos (point)))))
  2509. (defun org-current-time ()
  2510. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  2511. (if (> org-time-stamp-rounding-minutes 0)
  2512. (let ((r org-time-stamp-rounding-minutes)
  2513. (time (decode-time)))
  2514. (apply 'encode-time
  2515. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  2516. (nthcdr 2 time))))
  2517. (current-time)))
  2518. (defun org-add-props (string plist &rest props)
  2519. "Add text properties to entire string, from beginning to end.
  2520. PLIST may be a list of properties, PROPS are individual properties and values
  2521. that will be added to PLIST. Returns the string that was modified."
  2522. (add-text-properties
  2523. 0 (length string) (if props (append plist props) plist) string)
  2524. string)
  2525. (put 'org-add-props 'lisp-indent-function 2)
  2526. ;;; Font-Lock stuff
  2527. (defvar org-mouse-map (make-sparse-keymap))
  2528. (define-key org-mouse-map
  2529. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  2530. (define-key org-mouse-map
  2531. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  2532. (when org-mouse-1-follows-link
  2533. (define-key org-mouse-map [follow-link] 'mouse-face))
  2534. (when org-tab-follows-link
  2535. (define-key org-mouse-map [(tab)] 'org-open-at-point)
  2536. (define-key org-mouse-map "\C-i" 'org-open-at-point))
  2537. (when org-return-follows-link
  2538. (define-key org-mouse-map [(return)] 'org-open-at-point)
  2539. (define-key org-mouse-map "\C-m" 'org-open-at-point))
  2540. (require 'font-lock)
  2541. (defconst org-non-link-chars "]\t\n\r<>")
  2542. (defconst org-link-types '("https?" "ftp" "mailto" "file" "news" "bbdb" "vm"
  2543. "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  2544. (defconst org-link-re-with-space
  2545. (concat
  2546. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2547. "\\([^" org-non-link-chars " ]"
  2548. "[^" org-non-link-chars "]*"
  2549. "[^" org-non-link-chars " ]\\)>?")
  2550. "Matches a link with spaces, optional angular brackets around it.")
  2551. (defconst org-link-re-with-space2
  2552. (concat
  2553. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2554. "\\([^" org-non-link-chars " ]"
  2555. "[^]\t\n\r]*"
  2556. "[^" org-non-link-chars " ]\\)>?")
  2557. "Matches a link with spaces, optional angular brackets around it.")
  2558. (defconst org-angle-link-re
  2559. (concat
  2560. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2561. "\\([^" org-non-link-chars " ]"
  2562. "[^" org-non-link-chars "]*"
  2563. "\\)>")
  2564. "Matches link with angular brackets, spaces are allowed.")
  2565. (defconst org-plain-link-re
  2566. (concat
  2567. "\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2568. "\\([^]\t\n\r<>,;() ]+\\)")
  2569. "Matches plain link, without spaces.")
  2570. (defconst org-bracket-link-regexp
  2571. "\\[\\[\\([^]]+\\)\\]\\(\\[\\([^]]+\\)\\]\\)?\\]"
  2572. "Matches a link in double brackets.")
  2573. (defconst org-bracket-link-analytic-regexp
  2574. (concat
  2575. "\\[\\["
  2576. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  2577. "\\([^]]+\\)"
  2578. "\\]"
  2579. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  2580. "\\]"))
  2581. ; 1: http:
  2582. ; 2: http
  2583. ; 3: path
  2584. ; 4: [desc]
  2585. ; 5: desc
  2586. (defconst org-ts-lengths
  2587. (cons (length (format-time-string (car org-time-stamp-formats)))
  2588. (length (format-time-string (cdr org-time-stamp-formats))))
  2589. "This holds the lengths of the two different time formats.")
  2590. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}[^\r\n>]*?\\)>"
  2591. "Regular expression for fast time stamp matching.")
  2592. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}[^\r\n>]*?\\)[]>]"
  2593. "Regular expression for fast time stamp matching.")
  2594. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\([^0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  2595. "Regular expression matching time strings for analysis.")
  2596. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 ">")
  2597. "Regular expression matching time stamps, with groups.")
  2598. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  2599. "Regular expression matching a time stamp range.")
  2600. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  2601. org-ts-regexp "\\)?")
  2602. "Regular expression matching a time stamp or time stamp range.")
  2603. (defvar org-§emph-face nil)
  2604. (defun org-do-emphasis-faces (limit)
  2605. "Run through the buffer and add overlays to links."
  2606. (if (re-search-forward org-emph-re limit t)
  2607. (progn
  2608. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  2609. 'face
  2610. (nth 1 (assoc (match-string 3)
  2611. org-emphasis-alist)))
  2612. (add-text-properties (match-beginning 2) (match-end 2)
  2613. '(font-lock-multiline t))
  2614. (backward-char 1)
  2615. t)))
  2616. (defun org-activate-plain-links (limit)
  2617. "Run through the buffer and add overlays to links."
  2618. (if (re-search-forward org-plain-link-re limit t)
  2619. (progn
  2620. (add-text-properties (match-beginning 0) (match-end 0)
  2621. (list 'mouse-face 'highlight
  2622. 'keymap org-mouse-map
  2623. ))
  2624. t)))
  2625. (defun org-activate-angle-links (limit)
  2626. "Run through the buffer and add overlays to links."
  2627. (if (re-search-forward org-angle-link-re limit t)
  2628. (progn
  2629. (add-text-properties (match-beginning 0) (match-end 0)
  2630. (list 'mouse-face 'highlight
  2631. 'keymap org-mouse-map
  2632. ))
  2633. t)))
  2634. (defun org-activate-bracket-links (limit)
  2635. "Run through the buffer and add overlays to bracketed links."
  2636. (if (re-search-forward org-bracket-link-regexp limit t)
  2637. (let* ((help (concat "LINK: "
  2638. (org-match-string-no-properties 1)))
  2639. ;; FIXME: above we should remove the escapes.
  2640. ;; but that requires another match, protecting match data,
  2641. ;; a lot of overhead for font-lock.
  2642. (ip (list 'invisible 'org-link 'intangible t 'rear-nonsticky t
  2643. 'keymap org-mouse-map 'mouse-face 'highlight
  2644. 'help-echo help))
  2645. (vp (list 'rear-nonsticky t
  2646. 'keymap org-mouse-map 'mouse-face 'highlight
  2647. 'help-echo help)))
  2648. ;; We need to remove the invisible property here. Table narrowing
  2649. ;; may have made some of this invisible.
  2650. (remove-text-properties (match-beginning 0) (match-end 0)
  2651. '(invisible nil))
  2652. (if (match-end 3)
  2653. (progn
  2654. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  2655. (add-text-properties (match-beginning 3) (match-end 3) vp)
  2656. (add-text-properties (match-end 3) (match-end 0) ip))
  2657. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  2658. (add-text-properties (match-beginning 1) (match-end 1) vp)
  2659. (add-text-properties (match-end 1) (match-end 0) ip))
  2660. t)))
  2661. (defun org-activate-dates (limit)
  2662. "Run through the buffer and add overlays to dates."
  2663. (if (re-search-forward org-tsr-regexp limit t)
  2664. (progn
  2665. (add-text-properties (match-beginning 0) (match-end 0)
  2666. (list 'mouse-face 'highlight
  2667. 'keymap org-mouse-map))
  2668. t)))
  2669. (defvar org-target-link-regexp nil
  2670. "Regular expression matching radio targets in plain text.")
  2671. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  2672. "Regular expression matching a link target.")
  2673. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  2674. "Regular expression matching a link target.")
  2675. (defun org-activate-target-links (limit)
  2676. "Run through the buffer and add overlays to target matches."
  2677. (when org-target-link-regexp
  2678. (let ((case-fold-search t))
  2679. (if (re-search-forward org-target-link-regexp limit t)
  2680. (progn
  2681. (add-text-properties (match-beginning 0) (match-end 0)
  2682. (list 'mouse-face 'highlight
  2683. 'keymap org-mouse-map
  2684. 'help-echo "Radio target link"
  2685. 'org-linked-text t))
  2686. t)))))
  2687. (defun org-update-radio-target-regexp ()
  2688. "Find all radio targets in this file and update the regular expression."
  2689. (interactive)
  2690. (when (memq 'radio org-activate-links)
  2691. (setq org-target-link-regexp
  2692. (org-make-target-link-regexp (org-all-targets 'radio)))
  2693. (org-restart-font-lock)))
  2694. (defun org-hide-wide-columns (limit)
  2695. (let (s e)
  2696. (setq s (text-property-any (point) (or limit (point-max))
  2697. 'org-cwidth t))
  2698. (when s
  2699. (setq e (next-single-property-change s 'org-cwidth))
  2700. (add-text-properties s e '(invisible org-cwidth intangible t))
  2701. (goto-char e)
  2702. t)))
  2703. (defun org-restart-font-lock ()
  2704. "Restart font-lock-mode, to force refontification."
  2705. (when (and (boundp 'font-lock-mode) font-lock-mode)
  2706. (font-lock-mode -1)
  2707. (font-lock-mode 1)))
  2708. (defun org-all-targets (&optional radio)
  2709. "Return a list of all targets in this file.
  2710. With optional argument RADIO, only find radio targets."
  2711. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  2712. rtn)
  2713. (save-excursion
  2714. (goto-char (point-min))
  2715. (while (re-search-forward re nil t)
  2716. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  2717. rtn)))
  2718. (defun org-make-target-link-regexp (targets)
  2719. "Make regular expression matching all strings in TARGETS.
  2720. The regular expression finds the targets also if there is a line break
  2721. between words."
  2722. (and targets
  2723. (concat
  2724. "\\<\\("
  2725. (mapconcat
  2726. (lambda (x)
  2727. (while (string-match " +" x)
  2728. (setq x (replace-match "\\s-+" t t x)))
  2729. x)
  2730. targets
  2731. "\\|")
  2732. "\\)\\>")))
  2733. (defvar org-camel-regexp "\\*?\\<[A-Z]+[a-z]+[A-Z][a-zA-Z]*\\>"
  2734. "Matches CamelCase words, possibly with a star before it.")
  2735. (defun org-activate-camels (limit)
  2736. "Run through the buffer and add overlays to dates."
  2737. (if (re-search-forward org-camel-regexp limit t)
  2738. (progn
  2739. (add-text-properties (match-beginning 0) (match-end 0)
  2740. (list 'mouse-face 'highlight
  2741. 'keymap org-mouse-map))
  2742. t)))
  2743. (defun org-activate-tags (limit)
  2744. (if (re-search-forward "[ \t]\\(:[A-Za-z_@0-9:]+:\\)[ \r\n]" limit t)
  2745. (progn
  2746. (add-text-properties (match-beginning 1) (match-end 1)
  2747. (list 'mouse-face 'highlight
  2748. 'keymap org-mouse-map))
  2749. t)))
  2750. (defun org-font-lock-level ()
  2751. (save-excursion
  2752. (org-back-to-heading t)
  2753. (- (match-end 0) (match-beginning 0))))
  2754. (defun org-outline-level ()
  2755. (save-excursion
  2756. (looking-at outline-regexp)
  2757. (if (match-beginning 1)
  2758. (+ (org-get-string-indentation (match-string 1)) 1000)
  2759. (- (match-end 0) (match-beginning 0)))))
  2760. (defvar org-font-lock-keywords nil)
  2761. (defun org-set-font-lock-defaults ()
  2762. (let* ((em org-fontify-emphasized-text)
  2763. (lk org-activate-links)
  2764. (org-font-lock-extra-keywords
  2765. ;; Headlines
  2766. (list
  2767. '("^\\(\\**\\)\\(\\*\\)\\(.*\\)" (1 (org-get-level-face 1))
  2768. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  2769. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  2770. (1 'org-table))
  2771. ;; Links
  2772. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  2773. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  2774. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  2775. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  2776. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  2777. (if (memq 'camel lk) '(org-activate-camels (0 'org-link t)))
  2778. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  2779. (if org-table-limit-column-width
  2780. '(org-hide-wide-columns (0 nil append)))
  2781. ;; TODO lines
  2782. (list (concat "^\\*+[ \t]*" org-not-done-regexp)
  2783. '(1 'org-todo t))
  2784. ;; Priorities
  2785. (list (concat "\\[#[A-Z]\\]") '(0 'org-special-keyword t))
  2786. ;; Special keywords
  2787. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  2788. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  2789. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  2790. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  2791. ;; Emphasis
  2792. (if em '(org-do-emphasis-faces))
  2793. ;; Checkboxes, similar to Frank Ruell's org-checklet.el
  2794. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[ X]\\]\\)"
  2795. 2 'bold prepend)
  2796. ;; COMMENT
  2797. (list (concat "^\\*+[ \t]*\\<\\(" org-comment-string
  2798. "\\|" org-quote-string "\\)\\>")
  2799. '(1 'org-special-keyword t))
  2800. '("^#.*" (0 'font-lock-comment-face t))
  2801. ;; DONE
  2802. (if org-fontify-done-headline
  2803. (list (concat "^[*]+ +\\<\\(" org-done-string "\\)\\(.*\\)\\>")
  2804. '(1 'org-done t) '(2 'org-headline-done t))
  2805. (list (concat "^[*]+ +\\<\\(" org-done-string "\\)\\>")
  2806. '(1 'org-done t)))
  2807. ;; Table stuff
  2808. '("^[ \t]*\\(:.*\\)" (1 'org-table t))
  2809. '("| *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  2810. '("^[ \t]*| *\\([#!$*_^]\\) *|" (1 'org-formula t))
  2811. (if org-format-transports-properties-p
  2812. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  2813. )))
  2814. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  2815. ;; Now set the full font-lock-keywords
  2816. (set (make-local-variable 'org-font-lock-keywords)
  2817. org-font-lock-extra-keywords)
  2818. (set (make-local-variable 'font-lock-defaults)
  2819. '(org-font-lock-keywords t nil nil backward-paragraph))
  2820. (kill-local-variable 'font-lock-keywords) nil))
  2821. (defvar org-m nil)
  2822. (defvar org-l nil)
  2823. (defvar org-f nil)
  2824. (defun org-get-level-face (n)
  2825. "Get the right face for match N in font-lock matching of healdines."
  2826. (setq org-l (- (match-end 2) (match-beginning 1)))
  2827. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  2828. ; (setq org-f (nth (1- (% org-l org-n-levels)) org-level-faces))
  2829. (setq org-f (nth (% (1- org-l) org-n-levels) org-level-faces))
  2830. (cond
  2831. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  2832. ((eq n 2) org-f)
  2833. (t (if org-level-color-stars-only nil org-f))))
  2834. (defun org-unfontify-region (beg end &optional maybe_loudly)
  2835. "Remove fontification and activation overlays from links."
  2836. (font-lock-default-unfontify-region beg end)
  2837. (let* ((buffer-undo-list t)
  2838. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  2839. (inhibit-modification-hooks t)
  2840. deactivate-mark buffer-file-name buffer-file-truename)
  2841. (remove-text-properties beg end
  2842. '(mouse-face nil keymap nil org-linked-text nil
  2843. invisible nil intangible nil))))
  2844. ;;; Visibility cycling
  2845. (defvar org-cycle-global-status nil)
  2846. (make-variable-buffer-local 'org-cycle-global-status)
  2847. (defvar org-cycle-subtree-status nil)
  2848. (make-variable-buffer-local 'org-cycle-subtree-status)
  2849. ;;;###autoload
  2850. (defun org-cycle (&optional arg)
  2851. "Visibility cycling for Org-mode.
  2852. - When this function is called with a prefix argument, rotate the entire
  2853. buffer through 3 states (global cycling)
  2854. 1. OVERVIEW: Show only top-level headlines.
  2855. 2. CONTENTS: Show all headlines of all levels, but no body text.
  2856. 3. SHOW ALL: Show everything.
  2857. - When point is at the beginning of a headline, rotate the subtree started
  2858. by this line through 3 different states (local cycling)
  2859. 1. FOLDED: Only the main headline is shown.
  2860. 2. CHILDREN: The main headline and the direct children are shown.
  2861. From this state, you can move to one of the children
  2862. and zoom in further.
  2863. 3. SUBTREE: Show the entire subtree, including body text.
  2864. - When there is a numeric prefix, go up to a heading with level ARG, do
  2865. a `show-subtree' and return to the previous cursor position. If ARG
  2866. is negative, go up that many levels.
  2867. - When point is not at the beginning of a headline, execute
  2868. `indent-relative', like TAB normally does. See the option
  2869. `org-cycle-emulate-tab' for details.
  2870. - Special case: if point is the the beginning of the buffer and there is
  2871. no headline in line 1, this function will act as if called with prefix arg."
  2872. (interactive "P")
  2873. (let* ((outline-regexp
  2874. (if org-cycle-include-plain-lists
  2875. "\\*+\\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) "
  2876. outline-regexp))
  2877. (bob-special (and org-cycle-global-at-bob (bobp)
  2878. (not (looking-at outline-regexp))))
  2879. (org-cycle-hook (if bob-special nil org-cycle-hook))
  2880. (pos (point)))
  2881. (if (or bob-special (equal arg '(4)))
  2882. ;; special case: use global cycling
  2883. (setq arg t))
  2884. (cond
  2885. ((org-at-table-p 'any)
  2886. ;; Enter the table or move to the next field in the table
  2887. (or (org-table-recognize-table.el)
  2888. (progn
  2889. (if arg (org-table-edit-field t)
  2890. (org-table-justify-field-maybe)
  2891. (call-interactively 'org-table-next-field)))))
  2892. ((eq arg t) ;; Global cycling
  2893. (cond
  2894. ((and (eq last-command this-command)
  2895. (eq org-cycle-global-status 'overview))
  2896. ;; We just created the overview - now do table of contents
  2897. ;; This can be slow in very large buffers, so indicate action
  2898. (message "CONTENTS...")
  2899. (org-content)
  2900. (message "CONTENTS...done")
  2901. (setq org-cycle-global-status 'contents)
  2902. (run-hook-with-args 'org-cycle-hook 'contents))
  2903. ((and (eq last-command this-command)
  2904. (eq org-cycle-global-status 'contents))
  2905. ;; We just showed the table of contents - now show everything
  2906. (show-all)
  2907. (message "SHOW ALL")
  2908. (setq org-cycle-global-status 'all)
  2909. (run-hook-with-args 'org-cycle-hook 'all))
  2910. (t
  2911. ;; Default action: go to overview
  2912. (org-overview)
  2913. (message "OVERVIEW")
  2914. (setq org-cycle-global-status 'overview)
  2915. (run-hook-with-args 'org-cycle-hook 'overview))))
  2916. ((integerp arg)
  2917. ;; Show-subtree, ARG levels up from here.
  2918. (save-excursion
  2919. (org-back-to-heading)
  2920. (outline-up-heading (if (< arg 0) (- arg)
  2921. (- (funcall outline-level) arg)))
  2922. (org-show-subtree)))
  2923. ((save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  2924. ;; At a heading: rotate between three different views
  2925. (org-back-to-heading)
  2926. (let ((goal-column 0) eoh eol eos)
  2927. ;; First, some boundaries
  2928. (save-excursion
  2929. (org-back-to-heading)
  2930. (save-excursion
  2931. (beginning-of-line 2)
  2932. (while (and (not (eobp)) ;; this is like `next-line'
  2933. (get-char-property (1- (point)) 'invisible))
  2934. (beginning-of-line 2)) (setq eol (point)))
  2935. (outline-end-of-heading) (setq eoh (point))
  2936. (org-end-of-subtree t) (setq eos (point))
  2937. (outline-next-heading))
  2938. ;; Find out what to do next and set `this-command'
  2939. (cond
  2940. ((and (= eos eoh)
  2941. ;; Nothing is hidden behind this heading
  2942. (message "EMPTY ENTRY")
  2943. (setq org-cycle-subtree-status nil)))
  2944. ((>= eol eos)
  2945. ;; Entire subtree is hidden in one line: open it
  2946. (org-show-entry)
  2947. (show-children)
  2948. (message "CHILDREN")
  2949. (setq org-cycle-subtree-status 'children)
  2950. (run-hook-with-args 'org-cycle-hook 'children))
  2951. ((and (eq last-command this-command)
  2952. (eq org-cycle-subtree-status 'children))
  2953. ;; We just showed the children, now show everything.
  2954. (org-show-subtree)
  2955. (message "SUBTREE")
  2956. (setq org-cycle-subtree-status 'subtree)
  2957. (run-hook-with-args 'org-cycle-hook 'subtree))
  2958. (t
  2959. ;; Default action: hide the subtree.
  2960. (hide-subtree)
  2961. (message "FOLDED")
  2962. (setq org-cycle-subtree-status 'folded)
  2963. (run-hook-with-args 'org-cycle-hook 'folded)))))
  2964. ;; TAB emulation
  2965. (buffer-read-only (org-back-to-heading))
  2966. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  2967. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  2968. (or (and (eq org-cycle-emulate-tab 'white)
  2969. (= (match-end 0) (point-at-eol)))
  2970. (and (eq org-cycle-emulate-tab 'whitestart)
  2971. (>= (match-end 0) pos))))
  2972. t
  2973. (eq org-cycle-emulate-tab t))
  2974. (if (and (looking-at "[ \n\r\t]")
  2975. (string-match "^[ \t]*$" (buffer-substring
  2976. (point-at-bol) (point))))
  2977. (progn
  2978. (beginning-of-line 1)
  2979. (and (looking-at "[ \t]+") (replace-match ""))))
  2980. (indent-relative))
  2981. (t (save-excursion
  2982. (org-back-to-heading)
  2983. (org-cycle))))))
  2984. ;;;###autoload
  2985. (defun org-global-cycle (&optional arg)
  2986. "Cycle the global visibility. For details see `org-cycle'."
  2987. (interactive "P")
  2988. (if (integerp arg)
  2989. (progn
  2990. (show-all)
  2991. (hide-sublevels arg)
  2992. (setq org-cycle-global-status 'contents))
  2993. (org-cycle '(4))))
  2994. (defun org-overview ()
  2995. "Switch to overview mode, shoing only top-level headlines.
  2996. Really, this shows all headlines with level equal or greater than the level
  2997. of the first headline in the buffer. This is important, because if the
  2998. first headline is not level one, then (hide-sublevels 1) gives confusing
  2999. results."
  3000. (interactive)
  3001. (hide-sublevels (save-excursion
  3002. (goto-char (point-min))
  3003. (if (re-search-forward (concat "^" outline-regexp) nil t)
  3004. (progn
  3005. (goto-char (match-beginning 0))
  3006. (funcall outline-level))
  3007. 1))))
  3008. ;; FIXME: allow an argument to give a limiting level for this.
  3009. (defun org-content ()
  3010. "Show all headlines in the buffer, like a table of contents"
  3011. (interactive)
  3012. (save-excursion
  3013. ;; Visit all headings and show their offspring
  3014. (goto-char (point-max))
  3015. (catch 'exit
  3016. (while (and (progn (condition-case nil
  3017. (outline-previous-visible-heading 1)
  3018. (error (goto-char (point-min))))
  3019. t)
  3020. (looking-at outline-regexp))
  3021. (show-branches)
  3022. (if (bobp) (throw 'exit nil))))))
  3023. (defun org-optimize-window-after-visibility-change (state)
  3024. "Adjust the window after a change in outline visibility.
  3025. This function is the default value of the hook `org-cycle-hook'."
  3026. (when (get-buffer-window (current-buffer))
  3027. (cond
  3028. ((eq state 'overview) (org-first-headline-recenter 1))
  3029. ((eq state 'content) nil)
  3030. ((eq state 'all) nil)
  3031. ((eq state 'folded) nil)
  3032. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  3033. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  3034. (defun org-subtree-end-visible-p ()
  3035. "Is the end of the current subtree visible?"
  3036. (pos-visible-in-window-p
  3037. (save-excursion (org-end-of-subtree t) (point))))
  3038. (defun org-first-headline-recenter (&optional N)
  3039. "Move cursor to the first headline and recenter the headline.
  3040. Optional argument N means, put the headline into the Nth line of the window."
  3041. (goto-char (point-min))
  3042. (when (re-search-forward (concat "^" outline-regexp) nil t)
  3043. (beginning-of-line)
  3044. (recenter (prefix-numeric-value N))))
  3045. (defvar org-goto-window-configuration nil)
  3046. (defvar org-goto-marker nil)
  3047. (defvar org-goto-map (make-sparse-keymap))
  3048. (let ((cmds '(isearch-forward isearch-backward)) cmd)
  3049. (while (setq cmd (pop cmds))
  3050. (substitute-key-definition cmd cmd org-goto-map global-map)))
  3051. (define-key org-goto-map "\C-m" 'org-goto-ret)
  3052. (define-key org-goto-map [(left)] 'org-goto-left)
  3053. (define-key org-goto-map [(right)] 'org-goto-right)
  3054. (define-key org-goto-map [(?q)] 'org-goto-quit)
  3055. (define-key org-goto-map [(control ?g)] 'org-goto-quit)
  3056. (define-key org-goto-map "\C-i" 'org-cycle)
  3057. (define-key org-goto-map [(tab)] 'org-cycle)
  3058. (define-key org-goto-map [(down)] 'outline-next-visible-heading)
  3059. (define-key org-goto-map [(up)] 'outline-previous-visible-heading)
  3060. (define-key org-goto-map "n" 'outline-next-visible-heading)
  3061. (define-key org-goto-map "p" 'outline-previous-visible-heading)
  3062. (define-key org-goto-map "f" 'outline-forward-same-level)
  3063. (define-key org-goto-map "b" 'outline-backward-same-level)
  3064. (define-key org-goto-map "u" 'outline-up-heading)
  3065. (define-key org-goto-map "\C-c\C-n" 'outline-next-visible-heading)
  3066. (define-key org-goto-map "\C-c\C-p" 'outline-previous-visible-heading)
  3067. (define-key org-goto-map "\C-c\C-f" 'outline-forward-same-level)
  3068. (define-key org-goto-map "\C-c\C-b" 'outline-backward-same-level)
  3069. (define-key org-goto-map "\C-c\C-u" 'outline-up-heading)
  3070. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  3071. (while l (define-key org-goto-map (int-to-string (pop l)) 'digit-argument)))
  3072. (defconst org-goto-help
  3073. "Select a location to jump to, press RET
  3074. \[Up]/[Down]=next/prev headline TAB=cycle visibility RET=select [Q]uit")
  3075. (defun org-goto ()
  3076. "Go to a different location of the document, keeping current visibility.
  3077. When you want to go to a different location in a document, the fastest way
  3078. is often to fold the entire buffer and then dive into the tree. This
  3079. method has the disadvantage, that the previous location will be folded,
  3080. which may not be what you want.
  3081. This command works around this by showing a copy of the current buffer in
  3082. overview mode. You can dive into the tree in that copy, to find the
  3083. location you want to reach. When pressing RET, the command returns to the
  3084. original buffer in which the visibility is still unchanged. It then jumps
  3085. to the new location, making it and the headline hierarchy above it visible."
  3086. (interactive)
  3087. (let* ((org-goto-start-pos (point))
  3088. (selected-point
  3089. (org-get-location (current-buffer) org-goto-help)))
  3090. (if selected-point
  3091. (progn
  3092. (org-mark-ring-push org-goto-start-pos)
  3093. (goto-char selected-point)
  3094. (if (or (org-invisible-p) (org-invisible-p2))
  3095. (org-show-hierarchy-above)))
  3096. (error "Quit"))))
  3097. (defun org-get-location (buf help)
  3098. "Let the user select a location in the Org-mode buffer BUF.
  3099. This function uses a recursive edit. It returns the selected position
  3100. or nil."
  3101. (let (org-selected-point)
  3102. (save-excursion
  3103. (save-window-excursion
  3104. (delete-other-windows)
  3105. (switch-to-buffer (get-buffer-create "*org-goto*"))
  3106. (with-output-to-temp-buffer "*Help*"
  3107. (princ help))
  3108. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  3109. (setq buffer-read-only nil)
  3110. (erase-buffer)
  3111. (insert-buffer-substring buf)
  3112. (let ((org-startup-truncated t)
  3113. (org-startup-folded t)
  3114. (org-startup-align-all-tables nil)
  3115. (org-startup-with-deadline-check nil))
  3116. (org-mode))
  3117. (setq buffer-read-only t)
  3118. (if (boundp 'org-goto-start-pos)
  3119. (goto-char org-goto-start-pos)
  3120. (goto-char (point-min)))
  3121. (org-beginning-of-line)
  3122. (message "Select location and press RET")
  3123. ;; now we make sure that during selection, ony very few keys work
  3124. ;; and that it is impossible to switch to another window.
  3125. (let ((gm (current-global-map))
  3126. (overriding-local-map org-goto-map))
  3127. (unwind-protect
  3128. (progn
  3129. (use-global-map org-goto-map)
  3130. (recursive-edit))
  3131. (use-global-map gm)))))
  3132. (kill-buffer "*org-goto*")
  3133. org-selected-point))
  3134. (defun org-goto-ret (&optional arg)
  3135. "Finish `org-goto' by going to the new location."
  3136. (interactive "P")
  3137. (setq org-selected-point (point)
  3138. current-prefix-arg arg)
  3139. (throw 'exit nil))
  3140. (defun org-goto-left ()
  3141. "Finish `org-goto' by going to the new location."
  3142. (interactive)
  3143. (if (org-on-heading-p)
  3144. (progn
  3145. (beginning-of-line 1)
  3146. (setq org-selected-point (point)
  3147. current-prefix-arg (- (match-end 0) (match-beginning 0)))
  3148. (throw 'exit nil))
  3149. (error "Not on a heading")))
  3150. (defun org-goto-right ()
  3151. "Finish `org-goto' by going to the new location."
  3152. (interactive)
  3153. (if (org-on-heading-p)
  3154. (progn
  3155. (outline-end-of-subtree)
  3156. (or (eobp) (forward-char 1))
  3157. (setq org-selected-point (point)
  3158. current-prefix-arg (- (match-end 0) (match-beginning 0)))
  3159. (throw 'exit nil))
  3160. (error "Not on a heading")))
  3161. (defun org-goto-quit ()
  3162. "Finish `org-goto' without cursor motion."
  3163. (interactive)
  3164. (setq org-selected-point nil)
  3165. (throw 'exit nil))
  3166. ;;; Promotion, Demotion, Inserting new headlines
  3167. (defvar org-ignore-region nil
  3168. "To temporarily disable the active region.")
  3169. (defun org-insert-heading (&optional force-heading)
  3170. "Insert a new heading or item with same depth at point.
  3171. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  3172. If point is at the beginning of a headline, insert a sibling before the
  3173. current headline. If point is in the middle of a headline, split the headline
  3174. at that position and make the rest of the headline part of the sibling below
  3175. the current headline."
  3176. (interactive "P")
  3177. (if (= (buffer-size) 0)
  3178. (insert "\n* ")
  3179. (when (or force-heading (not (org-insert-item)))
  3180. (let* ((head (save-excursion
  3181. (condition-case nil
  3182. (progn
  3183. (org-back-to-heading)
  3184. (match-string 0))
  3185. (error "*"))))
  3186. pos)
  3187. (cond
  3188. ((and (org-on-heading-p) (bolp)
  3189. (save-excursion (backward-char 1) (not (org-invisible-p))))
  3190. (open-line 1))
  3191. ((and (bolp) (save-excursion
  3192. (backward-char 1) (not (org-invisible-p))))
  3193. nil)
  3194. (t (newline)))
  3195. (insert head) (just-one-space)
  3196. (setq pos (point))
  3197. (end-of-line 1)
  3198. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  3199. (run-hooks 'org-insert-heading-hook)))))
  3200. (defun org-insert-item (&optional checkbox)
  3201. "Insert a new item at the current level.
  3202. Return t when things worked, nil when we are not in an item."
  3203. (when (save-excursion
  3204. (condition-case nil
  3205. (progn
  3206. (org-beginning-of-item)
  3207. (org-at-item-p)
  3208. t)
  3209. (error nil)))
  3210. (let* ((bul (match-string 0))
  3211. (end (match-end 0))
  3212. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  3213. (match-end 0)))
  3214. (eowcol (save-excursion (goto-char eow) (current-column)))
  3215. pos)
  3216. (cond
  3217. ((and (org-at-item-p) (<= (point) eow))
  3218. ;; before the bullet
  3219. (beginning-of-line 1)
  3220. (open-line 1))
  3221. ((<= (point) eow)
  3222. (beginning-of-line 1))
  3223. (t (newline)))
  3224. (insert bul (if checkbox "[ ]" ""))
  3225. (just-one-space)
  3226. (setq pos (point))
  3227. (end-of-line 1)
  3228. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  3229. (org-maybe-renumber-ordered-list)
  3230. t))
  3231. (defun org-insert-todo-heading (arg)
  3232. "Insert a new heading with the same level and TODO state as current heading.
  3233. If the heading has no TODO state, or if the state is DONE, use the first
  3234. state (TODO by default). Also with prefix arg, force first state."
  3235. (interactive "P")
  3236. (when (not (org-insert-item 'checkbox))
  3237. (org-insert-heading)
  3238. (save-excursion
  3239. (org-back-to-heading)
  3240. (if org-noutline-p
  3241. (outline-previous-heading)
  3242. (outline-previous-visible-heading t))
  3243. (looking-at org-todo-line-regexp))
  3244. (if (or arg
  3245. (not (match-beginning 2))
  3246. (equal (match-string 2) org-done-string))
  3247. (insert (car org-todo-keywords) " ")
  3248. (insert (match-string 2) " "))))
  3249. (defun org-promote-subtree ()
  3250. "Promote the entire subtree.
  3251. See also `org-promote'."
  3252. (interactive)
  3253. (save-excursion
  3254. (org-map-tree 'org-promote)))
  3255. (defun org-demote-subtree ()
  3256. "Demote the entire subtree. See `org-demote'.
  3257. See also `org-promote'."
  3258. (interactive)
  3259. (save-excursion
  3260. (org-map-tree 'org-demote)))
  3261. (defun org-do-promote ()
  3262. "Promote the current heading higher up the tree.
  3263. If the region is active in `transient-mark-mode', promote all headings
  3264. in the region."
  3265. (interactive)
  3266. (save-excursion
  3267. (if (org-region-active-p)
  3268. (org-map-region 'org-promote (region-beginning) (region-end))
  3269. (org-promote)))
  3270. (org-fix-position-after-promote))
  3271. (defun org-do-demote ()
  3272. "Demote the current heading lower down the tree.
  3273. If the region is active in `transient-mark-mode', demote all headings
  3274. in the region."
  3275. (interactive)
  3276. (save-excursion
  3277. (if (org-region-active-p)
  3278. (org-map-region 'org-demote (region-beginning) (region-end))
  3279. (org-demote)))
  3280. (org-fix-position-after-promote))
  3281. (defun org-fix-position-after-promote ()
  3282. "Make sure that after pro/demotion cursor position is right."
  3283. (and (equal (char-after) ?\ )
  3284. (equal (char-before) ?*)
  3285. (forward-char 1)))
  3286. (defun org-get-legal-level (level change)
  3287. "Rectify a level change under the influence of `org-odd-levels-only'
  3288. LEVEL is a current level, CHANGE is by how much the level should be
  3289. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  3290. even level numbers will become the next higher odd number."
  3291. (if org-odd-levels-only
  3292. (cond ((not change) (1+ (* 2 (/ level 2))))
  3293. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  3294. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  3295. (max 1 (+ level change))))
  3296. (defun org-promote ()
  3297. "Promote the current heading higher up the tree.
  3298. If the region is active in `transient-mark-mode', promote all headings
  3299. in the region."
  3300. (org-back-to-heading t)
  3301. (let* ((level (save-match-data (funcall outline-level)))
  3302. (up-head (make-string (org-get-legal-level level -1) ?*))
  3303. (diff (abs (- level (length up-head)))))
  3304. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover"))
  3305. (replace-match up-head nil t)
  3306. ;; Fixup tag positioning
  3307. (and org-auto-align-tags (org-set-tags nil t))
  3308. (if org-adapt-indentation
  3309. (org-fixup-indentation (if (> diff 1) "^ " "^ ") ""
  3310. (if (> diff 1) "^ ? ?\\S-" "^ ?\\S-")))))
  3311. (defun org-demote ()
  3312. "Demote the current heading lower down the tree.
  3313. If the region is active in `transient-mark-mode', demote all headings
  3314. in the region."
  3315. (org-back-to-heading t)
  3316. (let* ((level (save-match-data (funcall outline-level)))
  3317. (down-head (make-string (org-get-legal-level level 1) ?*))
  3318. (diff (abs (- level (length down-head)))))
  3319. (replace-match down-head nil t)
  3320. ;; Fixup tag positioning
  3321. (and org-auto-align-tags (org-set-tags nil t))
  3322. (if org-adapt-indentation
  3323. (org-fixup-indentation "^ " (if (> diff 1) " " " ") "^\\S-"))))
  3324. (defun org-map-tree (fun)
  3325. "Call FUN for every heading underneath the current one."
  3326. (org-back-to-heading)
  3327. (let ((level (funcall outline-level)))
  3328. (save-excursion
  3329. (funcall fun)
  3330. (while (and (progn
  3331. (outline-next-heading)
  3332. (> (funcall outline-level) level))
  3333. (not (eobp)))
  3334. (funcall fun)))))
  3335. (defun org-map-region (fun beg end)
  3336. "Call FUN for every heading between BEG and END."
  3337. (let ((org-ignore-region t))
  3338. (save-excursion
  3339. (setq end (copy-marker end))
  3340. (goto-char beg)
  3341. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  3342. (< (point) end))
  3343. (funcall fun))
  3344. (while (and (progn
  3345. (outline-next-heading)
  3346. (< (point) end))
  3347. (not (eobp)))
  3348. (funcall fun)))))
  3349. ;; FIXME: this does not work well with Tabulators. This has to be re-written entirely.
  3350. (defun org-fixup-indentation (from to prohibit)
  3351. "Change the indentation in the current entry by re-replacing FROM with TO.
  3352. However, if the regexp PROHIBIT matches at all, don't do anything.
  3353. This is being used to change indentation along with the length of the
  3354. heading marker. But if there are any lines which are not indented, nothing
  3355. is changed at all."
  3356. (save-excursion
  3357. (let ((end (save-excursion (outline-next-heading)
  3358. (point-marker))))
  3359. (unless (save-excursion (re-search-forward prohibit end t))
  3360. (while (re-search-forward from end t)
  3361. (replace-match to)
  3362. (beginning-of-line 2)))
  3363. (move-marker end nil))))
  3364. ;;; Vertical tree motion, cutting and pasting of subtrees
  3365. (defun org-move-subtree-up (&optional arg)
  3366. "Move the current subtree up past ARG headlines of the same level."
  3367. (interactive "p")
  3368. (org-move-subtree-down (- (prefix-numeric-value arg))))
  3369. (defun org-move-subtree-down (&optional arg)
  3370. "Move the current subtree down past ARG headlines of the same level."
  3371. (interactive "p")
  3372. (setq arg (prefix-numeric-value arg))
  3373. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  3374. 'outline-get-last-sibling))
  3375. (ins-point (make-marker))
  3376. (cnt (abs arg))
  3377. beg end txt folded)
  3378. ;; Select the tree
  3379. (org-back-to-heading)
  3380. (setq beg (point))
  3381. (save-match-data
  3382. (save-excursion (outline-end-of-heading)
  3383. (setq folded (org-invisible-p)))
  3384. (outline-end-of-subtree))
  3385. (outline-next-heading)
  3386. (setq end (point))
  3387. ;; Find insertion point, with error handling
  3388. (goto-char beg)
  3389. (while (> cnt 0)
  3390. (or (and (funcall movfunc) (looking-at outline-regexp))
  3391. (progn (goto-char beg)
  3392. (error "Cannot move past superior level or buffer limit")))
  3393. (setq cnt (1- cnt)))
  3394. (if (> arg 0)
  3395. ;; Moving forward - still need to move over subtree
  3396. (progn (outline-end-of-subtree)
  3397. (outline-next-heading)
  3398. (if (not (or (looking-at (concat "^" outline-regexp))
  3399. (bolp)))
  3400. (newline))))
  3401. (move-marker ins-point (point))
  3402. (setq txt (buffer-substring beg end))
  3403. (delete-region beg end)
  3404. (insert txt)
  3405. (goto-char ins-point)
  3406. (if folded (hide-subtree))
  3407. (move-marker ins-point nil)))
  3408. (defvar org-subtree-clip ""
  3409. "Clipboard for cut and paste of subtrees.
  3410. This is actually only a copy of the kill, because we use the normal kill
  3411. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  3412. (defvar org-subtree-clip-folded nil
  3413. "Was the last copied subtree folded?
  3414. This is used to fold the tree back after pasting.")
  3415. (defun org-cut-subtree ()
  3416. "Cut the current subtree into the clipboard.
  3417. This is a short-hand for marking the subtree and then cutting it."
  3418. (interactive)
  3419. (org-copy-subtree 'cut))
  3420. (defun org-copy-subtree (&optional cut)
  3421. "Cut the current subtree into the clipboard.
  3422. This is a short-hand for marking the subtree and then copying it.
  3423. If CUT is non nil, actually cut the subtree."
  3424. (interactive)
  3425. (let (beg end folded)
  3426. (org-back-to-heading)
  3427. (setq beg (point))
  3428. (save-match-data
  3429. (save-excursion (outline-end-of-heading)
  3430. (setq folded (org-invisible-p)))
  3431. (outline-end-of-subtree))
  3432. (if (equal (char-after) ?\n) (forward-char 1))
  3433. (setq end (point))
  3434. (goto-char beg)
  3435. (when (> end beg)
  3436. (setq org-subtree-clip-folded folded)
  3437. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  3438. (setq org-subtree-clip (current-kill 0))
  3439. (message "%s: Subtree with %d characters"
  3440. (if cut "Cut" "Copied")
  3441. (length org-subtree-clip)))))
  3442. (defun org-paste-subtree (&optional level tree)
  3443. "Paste the clipboard as a subtree, with modification of headline level.
  3444. The entire subtree is promoted or demoted in order to match a new headline
  3445. level. By default, the new level is derived from the visible headings
  3446. before and after the insertion point, and taken to be the inferior headline
  3447. level of the two. So if the previous visible heading is level 3 and the
  3448. next is level 4 (or vice versa), level 4 will be used for insertion.
  3449. This makes sure that the subtree remains an independent subtree and does
  3450. not swallow low level entries.
  3451. You can also force a different level, either by using a numeric prefix
  3452. argument, or by inserting the heading marker by hand. For example, if the
  3453. cursor is after \"*****\", then the tree will be shifted to level 5.
  3454. If you want to insert the tree as is, just use \\[yank].
  3455. If optional TREE is given, use this text instead of the kill ring."
  3456. (interactive "P")
  3457. (unless (org-kill-is-subtree-p tree)
  3458. (error
  3459. (substitute-command-keys
  3460. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  3461. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  3462. (^re (concat "^\\(" outline-regexp "\\)"))
  3463. (re (concat "\\(" outline-regexp "\\)"))
  3464. (^re_ (concat "\\(" outline-regexp "\\)[ \t]*"))
  3465. (old-level (if (string-match ^re txt)
  3466. (- (match-end 0) (match-beginning 0))
  3467. -1))
  3468. (force-level (cond (level (prefix-numeric-value level))
  3469. ((string-match
  3470. ^re_ (buffer-substring (point-at-bol) (point)))
  3471. (- (match-end 0) (match-beginning 0)))
  3472. (t nil)))
  3473. (previous-level (save-excursion
  3474. (condition-case nil
  3475. (progn
  3476. (outline-previous-visible-heading 1)
  3477. (if (looking-at re)
  3478. (- (match-end 0) (match-beginning 0))
  3479. 1))
  3480. (error 1))))
  3481. (next-level (save-excursion
  3482. (condition-case nil
  3483. (progn
  3484. (outline-next-visible-heading 1)
  3485. (if (looking-at re)
  3486. (- (match-end 0) (match-beginning 0))
  3487. 1))
  3488. (error 1))))
  3489. (new-level (or force-level (max previous-level next-level)))
  3490. (shift (if (or (= old-level -1)
  3491. (= new-level -1)
  3492. (= old-level new-level))
  3493. 0
  3494. (- new-level old-level)))
  3495. (shift1 shift)
  3496. (delta (if (> shift 0) -1 1))
  3497. (func (if (> shift 0) 'org-demote 'org-promote))
  3498. (org-odd-levels-only nil)
  3499. beg end)
  3500. ;; Remove the forces level indicator
  3501. (if force-level
  3502. (delete-region (point-at-bol) (point)))
  3503. ;; Make sure we start at the beginning of an empty line
  3504. (if (not (bolp)) (insert "\n"))
  3505. (if (not (looking-at "[ \t]*$"))
  3506. (progn (insert "\n") (backward-char 1)))
  3507. ;; Paste
  3508. (setq beg (point))
  3509. (if (string-match "[ \t\r\n]+\\'" txt)
  3510. (setq txt (replace-match "\n" t t txt)))
  3511. (insert txt)
  3512. (setq end (point))
  3513. (if (looking-at "[ \t\r\n]+")
  3514. (replace-match "\n"))
  3515. (goto-char beg)
  3516. ;; Shift if necessary
  3517. (if (= shift 0)
  3518. (message "Pasted at level %d, without shift" new-level)
  3519. (save-restriction
  3520. (narrow-to-region beg end)
  3521. (while (not (= shift 0))
  3522. (org-map-region func (point-min) (point-max))
  3523. (setq shift (+ delta shift)))
  3524. (goto-char (point-min))
  3525. (message "Pasted at level %d, with shift by %d levels"
  3526. new-level shift1)))
  3527. (if (and kill-ring
  3528. (eq org-subtree-clip (current-kill 0))
  3529. org-subtree-clip-folded)
  3530. ;; The tree was folded before it was killed/copied
  3531. (hide-subtree))))
  3532. (defun org-kill-is-subtree-p (&optional txt)
  3533. "Check if the current kill is an outline subtree, or a set of trees.
  3534. Returns nil if kill does not start with a headline, or if the first
  3535. headline level is not the largest headline level in the tree.
  3536. So this will actually accept several entries of equal levels as well,
  3537. which is OK for `org-paste-subtree'.
  3538. If optional TXT is given, check this string instead of the current kill."
  3539. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  3540. (start-level (and kill
  3541. (string-match (concat "\\`" outline-regexp) kill)
  3542. (- (match-end 0) (match-beginning 0))))
  3543. (re (concat "^" outline-regexp))
  3544. (start 1))
  3545. (if (not start-level)
  3546. nil ;; does not even start with a heading
  3547. (catch 'exit
  3548. (while (setq start (string-match re kill (1+ start)))
  3549. (if (< (- (match-end 0) (match-beginning 0)) start-level)
  3550. (throw 'exit nil)))
  3551. t))))
  3552. ;;; Plain list items
  3553. (defun org-at-item-p ()
  3554. "Is point in a line starting a hand-formatted item?"
  3555. (let ((llt org-plain-list-ordered-item-terminator))
  3556. (save-excursion
  3557. (goto-char (point-at-bol))
  3558. (looking-at
  3559. (cond
  3560. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  3561. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  3562. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  3563. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  3564. (defun org-at-item-checkbox-p ()
  3565. "Is point at a line starting a plain-list item with a checklet?"
  3566. (and (org-at-item-p)
  3567. (save-excursion
  3568. (goto-char (match-end 0))
  3569. (skip-chars-forward " \t")
  3570. (looking-at "\\[[ X]\\]"))))
  3571. (defun org-toggle-checkbox ()
  3572. "Toggle the checkbox in the current line."
  3573. (interactive)
  3574. (save-excursion
  3575. (if (org-at-item-checkbox-p)
  3576. (replace-match (if (equal (match-string 0) "[ ]") "[X]" "[ ]") t t))))
  3577. (defun org-get-indentation (&optional line)
  3578. "Get the indentation of the current line, interpreting tabs.
  3579. When LINE is given, assume it represents a line and compute its indentation."
  3580. (if line
  3581. (if (string-match "^ *" (org-remove-tabs line))
  3582. (match-end 0))
  3583. (save-excursion
  3584. (beginning-of-line 1)
  3585. (skip-chars-forward " \t")
  3586. (current-column))))
  3587. (defun org-remove-tabs (s &optional width)
  3588. "Replace tabulators in S with spaces.
  3589. Assumes that s is a single line, starting in column 0."
  3590. (setq width (or width tab-width))
  3591. (while (string-match "\t" s)
  3592. (setq s (replace-match
  3593. (make-string
  3594. (- (* width (/ (+ (match-beginning 0) width) width))
  3595. (match-beginning 0)) ?\ )
  3596. t t s)))
  3597. s)
  3598. ;; FIXME: document properly.
  3599. (defun org-fix-indentation (line ind)
  3600. "If the current indenation is smaller than ind1, leave it alone.
  3601. If it is larger than ind, reduce it by ind."
  3602. (let* ((l (org-remove-tabs line))
  3603. (i (org-get-indentation l))
  3604. (i1 (car ind)) (i2 (cdr ind)))
  3605. (if (>= i i2) (setq l (substring line i2)))
  3606. (if (> i1 0)
  3607. (concat (make-string i1 ?\ ) l)
  3608. l)))
  3609. (defun org-beginning-of-item ()
  3610. "Go to the beginning of the current hand-formatted item.
  3611. If the cursor is not in an item, throw an error."
  3612. (interactive)
  3613. (let ((pos (point))
  3614. (limit (save-excursion (org-back-to-heading)
  3615. (beginning-of-line 2) (point)))
  3616. ind ind1)
  3617. (if (org-at-item-p)
  3618. (beginning-of-line 1)
  3619. (beginning-of-line 1)
  3620. (skip-chars-forward " \t")
  3621. (setq ind (current-column))
  3622. (if (catch 'exit
  3623. (while t
  3624. (beginning-of-line 0)
  3625. (if (< (point) limit) (throw 'exit nil))
  3626. (unless (looking-at " \t]*$")
  3627. (skip-chars-forward " \t")
  3628. (setq ind1 (current-column))
  3629. (if (< ind1 ind)
  3630. (throw 'exit (org-at-item-p))))))
  3631. nil
  3632. (goto-char pos)
  3633. (error "Not in an item")))))
  3634. (defun org-end-of-item ()
  3635. "Go to the end of the current hand-formatted item.
  3636. If the cursor is not in an item, throw an error."
  3637. (interactive)
  3638. (let ((pos (point))
  3639. (limit (save-excursion (outline-next-heading) (point)))
  3640. (ind (save-excursion
  3641. (org-beginning-of-item)
  3642. (skip-chars-forward " \t")
  3643. (current-column)))
  3644. ind1)
  3645. (if (catch 'exit
  3646. (while t
  3647. (beginning-of-line 2)
  3648. (if (>= (point) limit) (throw 'exit t))
  3649. (unless (looking-at "[ \t]*$")
  3650. (skip-chars-forward " \t")
  3651. (setq ind1 (current-column))
  3652. (if (<= ind1 ind) (throw 'exit t)))))
  3653. (beginning-of-line 1)
  3654. (goto-char pos)
  3655. (error "Not in an item"))))
  3656. (defun org-next-item ()
  3657. "Move to the beginning of the next item in the current plain list.
  3658. Error if not at a plain list, or if this is the last item in the list."
  3659. (interactive)
  3660. (let (beg end ind ind1 (pos (point)) txt)
  3661. (org-beginning-of-item)
  3662. (setq beg (point))
  3663. (setq ind (org-get-indentation))
  3664. (org-end-of-item)
  3665. (setq end (point))
  3666. (setq ind1 (org-get-indentation))
  3667. (unless (and (org-at-item-p) (= ind ind1))
  3668. (goto-char pos)
  3669. (error "On last item"))))
  3670. (defun org-previous-item ()
  3671. "Move to the beginning of the previous item in the current plain list.
  3672. Error if not at a plain list, or if this is the last item in the list."
  3673. (interactive)
  3674. (let (beg end ind ind1 (pos (point)) txt)
  3675. (org-beginning-of-item)
  3676. (setq beg (point))
  3677. (setq ind (org-get-indentation))
  3678. (goto-char beg)
  3679. (catch 'exit
  3680. (while t
  3681. (beginning-of-line 0)
  3682. (if (looking-at "[ \t]*$")
  3683. nil
  3684. (if (<= (setq ind1 (org-get-indentation)) ind)
  3685. (throw 'exit t)))))
  3686. (condition-case nil
  3687. (org-beginning-of-item)
  3688. (error (goto-char pos)
  3689. (error "On first item")))))
  3690. (defun org-move-item-down ()
  3691. "Move the plain list item at point down, i.e. swap with following item.
  3692. Subitems (items with larger indentation) are considered part of the item,
  3693. so this really moves item trees."
  3694. (interactive)
  3695. (let (beg end ind ind1 (pos (point)) txt)
  3696. (org-beginning-of-item)
  3697. (setq beg (point))
  3698. (setq ind (org-get-indentation))
  3699. (org-end-of-item)
  3700. (setq end (point))
  3701. (setq ind1 (org-get-indentation))
  3702. (if (and (org-at-item-p) (= ind ind1))
  3703. (progn
  3704. (org-end-of-item)
  3705. (setq txt (buffer-substring beg end))
  3706. (save-excursion
  3707. (delete-region beg end))
  3708. (setq pos (point))
  3709. (insert txt)
  3710. (goto-char pos)
  3711. (org-maybe-renumber-ordered-list))
  3712. (goto-char pos)
  3713. (error "Cannot move this item further down"))))
  3714. (defun org-move-item-up (arg)
  3715. "Move the plain list item at point up, i.e. swap with previous item.
  3716. Subitems (items with larger indentation) are considered part of the item,
  3717. so this really moves item trees."
  3718. (interactive "p")
  3719. (let (beg end ind ind1 (pos (point)) txt)
  3720. (org-beginning-of-item)
  3721. (setq beg (point))
  3722. (setq ind (org-get-indentation))
  3723. (org-end-of-item)
  3724. (setq end (point))
  3725. (goto-char beg)
  3726. (catch 'exit
  3727. (while t
  3728. (beginning-of-line 0)
  3729. (if (looking-at "[ \t]*$")
  3730. nil
  3731. (if (<= (setq ind1 (org-get-indentation)) ind)
  3732. (throw 'exit t)))))
  3733. (condition-case nil
  3734. (org-beginning-of-item)
  3735. (error (goto-char beg)
  3736. (error "Cannot move this item further up")))
  3737. (setq ind1 (org-get-indentation))
  3738. (if (and (org-at-item-p) (= ind ind1))
  3739. (progn
  3740. (setq txt (buffer-substring beg end))
  3741. (save-excursion
  3742. (delete-region beg end))
  3743. (setq pos (point))
  3744. (insert txt)
  3745. (goto-char pos)
  3746. (org-maybe-renumber-ordered-list))
  3747. (goto-char pos)
  3748. (error "Cannot move this item further up"))))
  3749. (defun org-maybe-renumber-ordered-list ()
  3750. "Renumber the ordered list at point if setup allows it.
  3751. This tests the user option `org-auto-renumber-ordered-lists' before
  3752. doing the renumbering."
  3753. (and org-auto-renumber-ordered-lists
  3754. (org-at-item-p)
  3755. (match-beginning 3)
  3756. (org-renumber-ordered-list 1)))
  3757. (defun org-get-string-indentation (s)
  3758. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  3759. (let ((n -1) (i 0) (w tab-width) c)
  3760. (catch 'exit
  3761. (while (< (setq n (1+ n)) (length s))
  3762. (setq c (aref s n))
  3763. (cond ((= c ?\ ) (setq i (1+ i)))
  3764. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  3765. (t (throw 'exit t)))))
  3766. i))
  3767. (defun org-renumber-ordered-list (arg)
  3768. "Renumber an ordered plain list.
  3769. Cursor needs to be in the first line of an item, the line that starts
  3770. with something like \"1.\" or \"2)\"."
  3771. (interactive "p")
  3772. (unless (and (org-at-item-p)
  3773. (match-beginning 3))
  3774. (error "This is not an ordered list"))
  3775. (let ((line (org-current-line))
  3776. (col (current-column))
  3777. (ind (org-get-string-indentation
  3778. (buffer-substring (point-at-bol) (match-beginning 3))))
  3779. ;; (term (substring (match-string 3) -1))
  3780. ind1 (n (1- arg)))
  3781. ;; find where this list begins
  3782. (catch 'exit
  3783. (while t
  3784. (catch 'next
  3785. (beginning-of-line 0)
  3786. (if (looking-at "[ \t]*$") (throw 'next t))
  3787. (skip-chars-forward " \t") (setq ind1 (current-column))
  3788. (if (or (< ind1 ind)
  3789. (and (= ind1 ind)
  3790. (not (org-at-item-p))))
  3791. (throw 'exit t)))))
  3792. ;; Walk forward and replace these numbers
  3793. (catch 'exit
  3794. (while t
  3795. (catch 'next
  3796. (beginning-of-line 2)
  3797. (if (eobp) (throw 'exit nil))
  3798. (if (looking-at "[ \t]*$") (throw 'next nil))
  3799. (skip-chars-forward " \t") (setq ind1 (current-column))
  3800. (if (> ind1 ind) (throw 'next t))
  3801. (if (< ind1 ind) (throw 'exit t))
  3802. (if (not (org-at-item-p)) (throw 'exit nil))
  3803. (if (not (match-beginning 3))
  3804. (error "unordered bullet in ordered list. Press \\[undo] to recover"))
  3805. (delete-region (match-beginning 3) (1- (match-end 3)))
  3806. (goto-char (match-beginning 3))
  3807. (insert (format "%d" (setq n (1+ n)))))))
  3808. (goto-line line)
  3809. (move-to-column col)))
  3810. (defvar org-last-indent-begin-marker (make-marker))
  3811. (defvar org-last-indent-end-marker (make-marker))
  3812. (defun org-outdent-item (arg)
  3813. "Outdent a local list item."
  3814. (interactive "p")
  3815. (org-indent-item (- arg)))
  3816. (defun org-indent-item (arg)
  3817. "Indent a local list item."
  3818. (interactive "p")
  3819. (unless (org-at-item-p)
  3820. (error "Not on an item"))
  3821. (save-excursion
  3822. (let (beg end ind ind1)
  3823. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  3824. (setq beg org-last-indent-begin-marker
  3825. end org-last-indent-end-marker)
  3826. (org-beginning-of-item)
  3827. (setq beg (move-marker org-last-indent-begin-marker (point)))
  3828. (org-end-of-item)
  3829. (setq end (move-marker org-last-indent-end-marker (point))))
  3830. (goto-char beg)
  3831. (skip-chars-forward " \t") (setq ind (current-column))
  3832. (if (< (+ arg ind) 0) (error "Cannot outdent beyond margin"))
  3833. (while (< (point) end)
  3834. (beginning-of-line 1)
  3835. (skip-chars-forward " \t") (setq ind1 (current-column))
  3836. (delete-region (point-at-bol) (point))
  3837. (indent-to-column (+ ind1 arg))
  3838. (beginning-of-line 2)))))
  3839. ;;; Archiving
  3840. (defun org-archive-subtree ()
  3841. "Move the current subtree to the archive.
  3842. The archive can be a certain top-level heading in the current file, or in
  3843. a different file. The tree will be moved to that location, the subtree
  3844. heading be marked DONE, and the current time will be added."
  3845. (interactive)
  3846. ;; Save all relevant TODO keyword-relatex variables
  3847. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  3848. (tr-org-todo-keywords org-todo-keywords)
  3849. (tr-org-todo-interpretation org-todo-interpretation)
  3850. (tr-org-done-string org-done-string)
  3851. (tr-org-todo-regexp org-todo-regexp)
  3852. (tr-org-todo-line-regexp org-todo-line-regexp)
  3853. (this-buffer (current-buffer))
  3854. file heading buffer level newfile-p)
  3855. (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
  3856. (progn
  3857. (setq file (format (match-string 1 org-archive-location)
  3858. (file-name-nondirectory buffer-file-name))
  3859. heading (match-string 2 org-archive-location)))
  3860. (error "Invalid `org-archive-location'"))
  3861. (if (> (length file) 0)
  3862. (setq newfile-p (not (file-exists-p file))
  3863. buffer (find-file-noselect file))
  3864. (setq buffer (current-buffer)))
  3865. (unless buffer
  3866. (error "Cannot access file \"%s\"" file))
  3867. (if (and (> (length heading) 0)
  3868. (string-match "^\\*+" heading))
  3869. (setq level (match-end 0))
  3870. (setq heading nil level 0))
  3871. (save-excursion
  3872. ;; We first only copy, in case something goes wrong
  3873. ;; we need to protect this-command, to avoid kill-region sets it,
  3874. ;; which would lead to duplication of subtrees
  3875. (let (this-command) (org-copy-subtree))
  3876. (set-buffer buffer)
  3877. ;; Enforce org-mode for the archive buffer
  3878. (if (not (eq major-mode 'org-mode))
  3879. ;; Force the mode for future visits.
  3880. (let ((org-insert-mode-line-in-empty-file t))
  3881. (call-interactively 'org-mode)))
  3882. (when newfile-p
  3883. (goto-char (point-max))
  3884. (insert (format "\nArchived entries from file %s\n\n"
  3885. (buffer-file-name this-buffer))))
  3886. ;; Force the TODO keywords of the original buffer
  3887. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  3888. (org-todo-keywords tr-org-todo-keywords)
  3889. (org-todo-interpretation tr-org-todo-interpretation)
  3890. (org-done-string tr-org-done-string)
  3891. (org-todo-regexp tr-org-todo-regexp)
  3892. (org-todo-line-regexp tr-org-todo-line-regexp))
  3893. (goto-char (point-min))
  3894. (if heading
  3895. (progn
  3896. (if (re-search-forward
  3897. (concat "\\(^\\|\r\\)"
  3898. (regexp-quote heading) "[ \t]*\\($\\|\r\\)")
  3899. nil t)
  3900. (goto-char (match-end 0))
  3901. ;; Heading not found, just insert it at the end
  3902. (goto-char (point-max))
  3903. (or (bolp) (insert "\n"))
  3904. (insert "\n" heading "\n")
  3905. (end-of-line 0))
  3906. ;; Make the subtree visible
  3907. (show-subtree)
  3908. (org-end-of-subtree t)
  3909. (skip-chars-backward " \t\r\n]")
  3910. (and (looking-at "[ \t\r\n]*")
  3911. (replace-match "\n\n")))
  3912. ;; No specific heading, just go to end of file.
  3913. (goto-char (point-max)) (insert "\n"))
  3914. ;; Paste
  3915. (org-paste-subtree (1+ level))
  3916. ;; Mark the entry as done, i.e. set to last work in org-todo-keywords
  3917. (if org-archive-mark-done
  3918. (org-todo (length org-todo-keywords)))
  3919. ;; Move cursor to right after the TODO keyword
  3920. (when org-archive-stamp-time
  3921. (beginning-of-line 1)
  3922. (looking-at org-todo-line-regexp)
  3923. (goto-char (or (match-end 2) (match-beginning 3)))
  3924. (insert "(" (format-time-string (cdr org-time-stamp-formats)
  3925. (org-current-time))
  3926. ")"))
  3927. ;; Save the buffer, if it is not the same buffer.
  3928. (if (not (eq this-buffer buffer)) (save-buffer))))
  3929. ;; Here we are back in the original buffer. Everything seems to have
  3930. ;; worked. So now cut the tree and finish up.
  3931. (let (this-command) (org-cut-subtree))
  3932. (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
  3933. (message "Subtree archived %s"
  3934. (if (eq this-buffer buffer)
  3935. (concat "under heading: " heading)
  3936. (concat "in file: " (abbreviate-file-name file))))))
  3937. ;;; Completion
  3938. (defun org-complete (&optional arg)
  3939. "Perform completion on word at point.
  3940. At the beginning of a headline, this completes TODO keywords as given in
  3941. `org-todo-keywords'.
  3942. If the current word is preceded by a backslash, completes the TeX symbols
  3943. that are supported for HTML support.
  3944. If the current word is preceded by \"#+\", completes special words for
  3945. setting file options.
  3946. At all other locations, this simply calls `ispell-complete-word'."
  3947. (interactive "P")
  3948. (catch 'exit
  3949. (let* ((end (point))
  3950. (beg1 (save-excursion
  3951. (if (equal (char-before (point)) ?\ ) (backward-char 1))
  3952. (skip-chars-backward "a-zA-Z_@0-9")
  3953. (point)))
  3954. (beg (save-excursion
  3955. (if (equal (char-before (point)) ?\ ) (backward-char 1))
  3956. (skip-chars-backward "a-zA-Z0-9_:$")
  3957. (point)))
  3958. (confirm (lambda (x) (stringp (car x))))
  3959. (camel (equal (char-before beg) ?*))
  3960. (tag (equal (char-before beg1) ?:))
  3961. (texp (equal (char-before beg) ?\\))
  3962. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  3963. beg)
  3964. "#+"))
  3965. (completion-ignore-case opt)
  3966. (type nil)
  3967. (tbl nil)
  3968. (table (cond
  3969. (opt
  3970. (setq type :opt)
  3971. (mapcar (lambda (x)
  3972. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  3973. (cons (match-string 2 x) (match-string 1 x)))
  3974. (org-split-string (org-get-current-options) "\n")))
  3975. (texp
  3976. (setq type :tex)
  3977. org-html-entities)
  3978. ((string-match "\\`\\*+[ \t]*\\'"
  3979. (buffer-substring (point-at-bol) beg))
  3980. (setq type :todo)
  3981. (mapcar 'list org-todo-keywords))
  3982. (camel
  3983. (setq type :camel)
  3984. (save-excursion
  3985. (goto-char (point-min))
  3986. (while (re-search-forward org-todo-line-regexp nil t)
  3987. (push (list
  3988. (if org-file-link-context-use-camel-case
  3989. (org-make-org-heading-camel (match-string 3) t)
  3990. (org-make-org-heading-search-string
  3991. (match-string 3) t)))
  3992. tbl)))
  3993. tbl)
  3994. (tag (setq type :tag beg beg1)
  3995. (or org-tag-alist (org-get-buffer-tags)))
  3996. (t (progn (ispell-complete-word arg) (throw 'exit nil)))))
  3997. (pattern (buffer-substring-no-properties beg end))
  3998. (completion (try-completion pattern table confirm)))
  3999. (cond ((eq completion t)
  4000. (if (equal type :opt)
  4001. (insert (substring (cdr (assoc (upcase pattern) table))
  4002. (length pattern)))))
  4003. ((null completion)
  4004. (message "Can't find completion for \"%s\"" pattern)
  4005. (ding))
  4006. ((not (string= pattern completion))
  4007. (delete-region beg end)
  4008. (if (string-match " +$" completion)
  4009. (setq completion (replace-match "" t t completion)))
  4010. (insert completion)
  4011. (if (get-buffer-window "*Completions*")
  4012. (delete-window (get-buffer-window "*Completions*")))
  4013. (if (assoc completion table)
  4014. (if (eq type :todo) (insert " ")
  4015. (if (eq type :tag) (insert ":"))))
  4016. (if (and (equal type :opt) (assoc completion table))
  4017. (message "%s" (substitute-command-keys
  4018. "Press \\[org-complete] again to insert example settings"))))
  4019. (t
  4020. (message "Making completion list...")
  4021. (let ((list (sort (all-completions pattern table confirm)
  4022. 'string<)))
  4023. (with-output-to-temp-buffer "*Completions*"
  4024. (condition-case nil
  4025. ;; Protection needed for XEmacs and emacs 21
  4026. (display-completion-list list pattern)
  4027. (error (display-completion-list list)))))
  4028. (message "Making completion list...%s" "done"))))))
  4029. ;;; Comments, TODO and DEADLINE
  4030. (defun org-toggle-comment ()
  4031. "Change the COMMENT state of an entry."
  4032. (interactive)
  4033. (save-excursion
  4034. (org-back-to-heading)
  4035. (if (looking-at (concat outline-regexp
  4036. "\\( +\\<" org-comment-string "\\>\\)"))
  4037. (replace-match "" t t nil 1)
  4038. (if (looking-at outline-regexp)
  4039. (progn
  4040. (goto-char (match-end 0))
  4041. (insert " " org-comment-string))))))
  4042. (defvar org-last-todo-state-is-todo nil
  4043. "This is non-nil when the last TODO state change led to a TODO state.
  4044. If the last change removed the TODO tag or switched to DONE, then
  4045. this is nil.")
  4046. (defun org-todo (&optional arg)
  4047. "Change the TODO state of an item.
  4048. The state of an item is given by a keyword at the start of the heading,
  4049. like
  4050. *** TODO Write paper
  4051. *** DONE Call mom
  4052. The different keywords are specified in the variable `org-todo-keywords'.
  4053. By default the available states are \"TODO\" and \"DONE\".
  4054. So for this example: when the item starts with TODO, it is changed to DONE.
  4055. When it starts with DONE, the DONE is removed. And when neither TODO nor
  4056. DONE are present, add TODO at the beginning of the heading.
  4057. With prefix arg, use completion to determine the new state. With numeric
  4058. prefix arg, switch to that state."
  4059. (interactive "P")
  4060. (save-excursion
  4061. (org-back-to-heading)
  4062. (if (looking-at outline-regexp) (goto-char (match-end 0)))
  4063. (or (looking-at (concat " +" org-todo-regexp " *"))
  4064. (looking-at " *"))
  4065. (let* ((this (match-string 1))
  4066. (completion-ignore-case t)
  4067. (member (member this org-todo-keywords))
  4068. (tail (cdr member))
  4069. (state (cond
  4070. ((equal arg '(4))
  4071. ;; Read a state with completion
  4072. (completing-read "State: " (mapcar (lambda(x) (list x))
  4073. org-todo-keywords)
  4074. nil t))
  4075. ((eq arg 'right)
  4076. (if this
  4077. (if tail (car tail) nil)
  4078. (car org-todo-keywords)))
  4079. ((eq arg 'left)
  4080. (if (equal member org-todo-keywords)
  4081. nil
  4082. (if this
  4083. (nth (- (length org-todo-keywords) (length tail) 2)
  4084. org-todo-keywords)
  4085. org-done-string)))
  4086. (arg
  4087. ;; user requests a specific state
  4088. (nth (1- (prefix-numeric-value arg))
  4089. org-todo-keywords))
  4090. ((null member) (car org-todo-keywords))
  4091. ((null tail) nil) ;; -> first entry
  4092. ((eq org-todo-interpretation 'sequence)
  4093. (car tail))
  4094. ((memq org-todo-interpretation '(type priority))
  4095. (if (eq this-command last-command)
  4096. (car tail)
  4097. (if (> (length tail) 0) org-done-string nil)))
  4098. (t nil)))
  4099. (next (if state (concat " " state " ") " ")))
  4100. (replace-match next t t)
  4101. (setq org-last-todo-state-is-todo
  4102. (not (equal state org-done-string)))
  4103. (when org-log-done
  4104. (if (equal state org-done-string)
  4105. (org-add-planning-info 'closed (current-time) 'scheduled)
  4106. (if (not this)
  4107. (org-add-planning-info nil nil 'closed))))
  4108. ;; Fixup tag positioning
  4109. (and org-auto-align-tags (org-set-tags nil t))
  4110. (run-hooks 'org-after-todo-state-change-hook)))
  4111. ;; Fixup cursor location if close to the keyword
  4112. (if (and (outline-on-heading-p)
  4113. (not (bolp))
  4114. (save-excursion (beginning-of-line 1)
  4115. (looking-at org-todo-line-regexp))
  4116. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  4117. (progn
  4118. (goto-char (or (match-end 2) (match-end 1)))
  4119. (just-one-space))))
  4120. (defun org-log-done (&optional undone)
  4121. "Add a time stamp logging that a TODO entry has been closed.
  4122. When UNDONE is non-nil, remove such a time stamp again."
  4123. (interactive)
  4124. (let (beg end col)
  4125. (save-excursion
  4126. (org-back-to-heading t)
  4127. (setq beg (point))
  4128. (looking-at (concat outline-regexp " *"))
  4129. (goto-char (match-end 0))
  4130. (setq col (current-column))
  4131. (outline-next-heading)
  4132. (setq end (point))
  4133. (goto-char beg)
  4134. (when (re-search-forward (concat
  4135. "[\r\n]\\([ \t]*"
  4136. (regexp-quote org-closed-string)
  4137. " *\\[.*?\\][^\n\r]*[\n\r]?\\)") end t)
  4138. (delete-region (match-beginning 1) (match-end 1)))
  4139. (unless undone
  4140. (org-back-to-heading t)
  4141. (skip-chars-forward "^\n\r")
  4142. (goto-char (min (1+ (point)) (point-max)))
  4143. (when (not (member (char-before) '(?\r ?\n)))
  4144. (insert "\n"))
  4145. (indent-to col)
  4146. (insert org-closed-string " "
  4147. (format-time-string
  4148. (concat "[" (substring (cdr org-time-stamp-formats) 1 -1) "]")
  4149. (org-current-time))
  4150. "\n")))))
  4151. (defun org-show-todo-tree (arg)
  4152. "Make a compact tree which shows all headlines marked with TODO.
  4153. The tree will show the lines where the regexp matches, and all higher
  4154. headlines above the match.
  4155. With \\[universal-argument] prefix, also show the DONE entries.
  4156. With a numeric prefix N, construct a sparse tree for the Nth element
  4157. of `org-todo-keywords'."
  4158. (interactive "P")
  4159. (let ((case-fold-search nil)
  4160. (kwd-re
  4161. (cond ((null arg) org-not-done-regexp)
  4162. ((equal arg '(4)) org-todo-regexp)
  4163. ((<= (prefix-numeric-value arg) (length org-todo-keywords))
  4164. (regexp-quote (nth (1- (prefix-numeric-value arg))
  4165. org-todo-keywords)))
  4166. (t (error "Invalid prefix argument: %s" arg)))))
  4167. (message "%d TODO entries found"
  4168. (org-occur (concat "^" outline-regexp " +" kwd-re )))))
  4169. (defun org-deadline ()
  4170. "Insert the DEADLINE: string to make a deadline.
  4171. A timestamp is also inserted - use \\[org-timestamp-up] and \\[org-timestamp-down]
  4172. to modify it to the correct date."
  4173. (interactive)
  4174. (org-add-planning-info 'deadline nil 'closed))
  4175. (defun org-schedule ()
  4176. "Insert the SCHEDULED: string to schedule a TODO item.
  4177. A timestamp is also inserted - use \\[org-timestamp-up] and \\[org-timestamp-down]
  4178. to modify it to the correct date."
  4179. (interactive)
  4180. (org-add-planning-info 'scheduled nil 'closed))
  4181. (defun org-add-planning-info (what &optional time &rest remove)
  4182. "Insert new timestamp with keyword in the line directly after the headline.
  4183. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  4184. If non is given, the user is prompted for a date.
  4185. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  4186. be removed."
  4187. (interactive)
  4188. (when what (setq time (or time (org-read-date nil 'to-time))))
  4189. (when (and org-insert-labeled-timestamps-at-point
  4190. (member what '(scheduled deadline)))
  4191. (insert
  4192. (if (eq what 'scheduled) org-scheduled-string org-deadline-string)
  4193. " "
  4194. (format-time-string (car org-time-stamp-formats) time))
  4195. (setq what nil))
  4196. (save-excursion
  4197. (let (beg end col list elt (buffer-invisibility-spec nil) ts)
  4198. (org-back-to-heading t)
  4199. (setq beg (point))
  4200. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  4201. (goto-char (match-end 1))
  4202. (setq col (current-column))
  4203. (goto-char (1+ (match-end 0)))
  4204. (if (and (not (looking-at outline-regexp))
  4205. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  4206. "[^\r\n]*"))
  4207. (not (equal (match-string 1) org-clock-string)))
  4208. (narrow-to-region (match-beginning 0) (match-end 0))
  4209. (insert "\n")
  4210. (backward-char 1)
  4211. (narrow-to-region (point) (point))
  4212. (indent-to-column col))
  4213. ;; Check if we have to remove something.
  4214. (setq list (cons what remove))
  4215. (while list
  4216. (setq elt (pop list))
  4217. (goto-char (point-min))
  4218. (when (or (and (eq elt 'scheduled)
  4219. (re-search-forward org-scheduled-time-regexp nil t))
  4220. (and (eq elt 'deadline)
  4221. (re-search-forward org-deadline-time-regexp nil t))
  4222. (and (eq elt 'closed)
  4223. (re-search-forward org-closed-time-regexp nil t)))
  4224. (replace-match "")
  4225. (if (looking-at " +") (replace-match ""))))
  4226. (goto-char (point-max))
  4227. (when what
  4228. (insert
  4229. (if (not (equal (char-before) ?\ )) " " "")
  4230. (cond ((eq what 'scheduled) org-scheduled-string)
  4231. ((eq what 'deadline) org-deadline-string)
  4232. ((eq what 'closed) org-closed-string))
  4233. " ")
  4234. (insert
  4235. (setq ts
  4236. (format-time-string
  4237. (if (eq what 'closed)
  4238. (concat "[" (substring (cdr org-time-stamp-formats) 1 -1) "]")
  4239. (car org-time-stamp-formats))
  4240. time))))
  4241. (goto-char (point-min))
  4242. (widen)
  4243. (if (looking-at "[ \t]+\r?\n")
  4244. (replace-match ""))
  4245. ts)))
  4246. (defun org-occur (regexp &optional callback)
  4247. "Make a compact tree which shows all matches of REGEXP.
  4248. The tree will show the lines where the regexp matches, and all higher
  4249. headlines above the match. It will also show the heading after the match,
  4250. to make sure editing the matching entry is easy.
  4251. If CALLBACK is non-nil, it is a function which is called to confirm
  4252. that the match should indeed be shown."
  4253. (interactive "sRegexp: ")
  4254. (org-remove-occur-highlights nil nil t)
  4255. (setq regexp (org-check-occur-regexp regexp))
  4256. (let ((cnt 0))
  4257. (save-excursion
  4258. (goto-char (point-min))
  4259. (org-overview)
  4260. (while (re-search-forward regexp nil t)
  4261. (when (or (not callback)
  4262. (save-match-data (funcall callback)))
  4263. (setq cnt (1+ cnt))
  4264. (org-highlight-new-match (match-beginning 0) (match-end 0))
  4265. (org-show-hierarchy-above))))
  4266. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  4267. nil 'local)
  4268. (run-hooks 'org-occur-hook)
  4269. (if (interactive-p)
  4270. (message "%d match(es) for regexp %s" cnt regexp))
  4271. cnt))
  4272. (defun org-show-hierarchy-above ()
  4273. "Make sure point and the headings hierarchy above is visible."
  4274. (catch 'exit
  4275. (if (org-on-heading-p t)
  4276. (org-flag-heading nil) ; only show the heading
  4277. (and (or (org-invisible-p) (org-invisible-p2))
  4278. (org-show-hidden-entry))) ; show entire entry
  4279. (save-excursion
  4280. (and org-show-following-heading
  4281. (outline-next-heading)
  4282. (org-flag-heading nil))) ; show the next heading
  4283. (when org-show-hierarchy-above
  4284. (save-excursion ; show all higher headings
  4285. (while (and (condition-case nil
  4286. (progn (org-up-heading-all 1) t)
  4287. (error nil))
  4288. (not (bobp)))
  4289. (org-flag-heading nil))))))
  4290. ;; Overlay compatibility functions
  4291. (defun org-make-overlay (beg end &optional buffer)
  4292. (if (featurep 'xemacs)
  4293. (make-extent beg end buffer)
  4294. (make-overlay beg end buffer)))
  4295. (defun org-delete-overlay (ovl)
  4296. (if (featurep 'xemacs) (delete-extent ovl) (delete-overlay ovl)))
  4297. (defun org-detatch-overlay (ovl)
  4298. (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
  4299. (defun org-move-overlay (ovl beg end &optional buffer)
  4300. (if (featurep 'xemacs)
  4301. (set-extent-endpoints ovl beg end buffer)
  4302. (move-overlay ovl beg end buffer)))
  4303. (defun org-overlay-put (ovl prop value)
  4304. (if (featurep 'xemacs)
  4305. (set-extent-property ovl prop value)
  4306. (overlay-put ovl prop value)))
  4307. (defvar org-occur-highlights nil)
  4308. (defun org-highlight-new-match (beg end)
  4309. "Highlight from BEG to END and mark the highlight is an occur headline."
  4310. (let ((ov (org-make-overlay beg end)))
  4311. (org-overlay-put ov 'face 'secondary-selection)
  4312. (push ov org-occur-highlights)))
  4313. (defun org-remove-occur-highlights (&optional beg end noremove)
  4314. "Remove the occur highlights from the buffer.
  4315. BEG and END are ignored. If NOREMOVE is nil, remove this function
  4316. from the `before-change-functions' in the current buffer."
  4317. (interactive)
  4318. (mapc 'org-delete-overlay org-occur-highlights)
  4319. (setq org-occur-highlights nil)
  4320. (unless noremove
  4321. (remove-hook 'before-change-functions
  4322. 'org-remove-occur-highlights 'local)))
  4323. ;;; Priorities
  4324. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z]\\)\\] ?\\)"
  4325. "Regular expression matching the priority indicator.")
  4326. (defvar org-remove-priority-next-time nil)
  4327. (defun org-priority-up ()
  4328. "Increase the priority of the current item."
  4329. (interactive)
  4330. (org-priority 'up))
  4331. (defun org-priority-down ()
  4332. "Decrease the priority of the current item."
  4333. (interactive)
  4334. (org-priority 'down))
  4335. (defun org-priority (&optional action)
  4336. "Change the priority of an item by ARG.
  4337. ACTION can be set, up, or down."
  4338. (interactive)
  4339. (setq action (or action 'set))
  4340. (let (current new news have remove)
  4341. (save-excursion
  4342. (org-back-to-heading)
  4343. (if (looking-at org-priority-regexp)
  4344. (setq current (string-to-char (match-string 2))
  4345. have t)
  4346. (setq current org-default-priority))
  4347. (cond
  4348. ((eq action 'set)
  4349. (message "Priority A-%c, SPC to remove: " org-lowest-priority)
  4350. (setq new (read-char-exclusive))
  4351. (cond ((equal new ?\ ) (setq remove t))
  4352. ((or (< (upcase new) ?A) (> (upcase new) org-lowest-priority))
  4353. (error "Priority must be between `%c' and `%c'"
  4354. ?A org-lowest-priority))))
  4355. ((eq action 'up)
  4356. (setq new (1- current)))
  4357. ((eq action 'down)
  4358. (setq new (1+ current)))
  4359. (t (error "Invalid action")))
  4360. (setq new (min (max ?A (upcase new)) org-lowest-priority))
  4361. (setq news (format "%c" new))
  4362. (if have
  4363. (if remove
  4364. (replace-match "" t t nil 1)
  4365. (replace-match news t t nil 2))
  4366. (if remove
  4367. (error "No priority cookie found in line")
  4368. (looking-at org-todo-line-regexp)
  4369. (if (match-end 2)
  4370. (progn
  4371. (goto-char (match-end 2))
  4372. (insert " [#" news "]"))
  4373. (goto-char (match-beginning 3))
  4374. (insert "[#" news "] ")))))
  4375. (if remove
  4376. (message "Priority removed")
  4377. (message "Priority of current item set to %s" news))))
  4378. (defun org-get-priority (s)
  4379. "Find priority cookie and return priority."
  4380. (save-match-data
  4381. (if (not (string-match org-priority-regexp s))
  4382. (* 1000 (- org-lowest-priority org-default-priority))
  4383. (* 1000 (- org-lowest-priority
  4384. (string-to-char (match-string 2 s)))))))
  4385. ;;; Timestamps
  4386. (defvar org-last-changed-timestamp nil)
  4387. (defun org-time-stamp (arg)
  4388. "Prompt for a date/time and insert a time stamp.
  4389. If the user specifies a time like HH:MM, or if this command is called
  4390. with a prefix argument, the time stamp will contain date and time.
  4391. Otherwise, only the date will be included. All parts of a date not
  4392. specified by the user will be filled in from the current date/time.
  4393. So if you press just return without typing anything, the time stamp
  4394. will represent the current date/time. If there is already a timestamp
  4395. at the cursor, it will be modified."
  4396. (interactive "P")
  4397. (let ((fmt (if arg (cdr org-time-stamp-formats)
  4398. (car org-time-stamp-formats)))
  4399. (org-time-was-given nil)
  4400. time)
  4401. (cond
  4402. ((and (org-at-timestamp-p)
  4403. (eq last-command 'org-time-stamp)
  4404. (eq this-command 'org-time-stamp))
  4405. (insert "--")
  4406. (setq time (let ((this-command this-command))
  4407. (org-read-date arg 'totime)))
  4408. (if org-time-was-given (setq fmt (cdr org-time-stamp-formats)))
  4409. (insert (format-time-string fmt time)))
  4410. ((org-at-timestamp-p)
  4411. (setq time (let ((this-command this-command))
  4412. (org-read-date arg 'totime)))
  4413. (and (org-at-timestamp-p) (replace-match
  4414. (setq org-last-changed-timestamp
  4415. (format-time-string fmt time))
  4416. t t))
  4417. (message "Timestamp updated"))
  4418. (t
  4419. (setq time (let ((this-command this-command))
  4420. (org-read-date arg 'totime)))
  4421. (if org-time-was-given (setq fmt (cdr org-time-stamp-formats)))
  4422. (insert (format-time-string fmt time))))))
  4423. (defun org-time-stamp-inactive (&optional arg)
  4424. "Insert an inactive time stamp.
  4425. An inactive time stamp is enclosed in square brackets instead of angle
  4426. brackets. It is inactive in the sense that it does not trigger agenda entries,
  4427. does not link to the calendar and cannot be changed with the S-cursor keys.
  4428. So these are more for recording a certain time/date."
  4429. (interactive "P")
  4430. (let ((fmt (if arg (cdr org-time-stamp-formats)
  4431. (car org-time-stamp-formats)))
  4432. (org-time-was-given nil)
  4433. time)
  4434. (setq time (org-read-date arg 'totime))
  4435. (if org-time-was-given (setq fmt (cdr org-time-stamp-formats)))
  4436. (setq fmt (concat "[" (substring fmt 1 -1) "]"))
  4437. (insert (format-time-string fmt time))))
  4438. (defvar org-date-ovl (org-make-overlay 1 1))
  4439. (org-overlay-put org-date-ovl 'face 'org-warning)
  4440. (org-detatch-overlay org-date-ovl)
  4441. (defun org-read-date (&optional with-time to-time)
  4442. "Read a date and make things smooth for the user.
  4443. The prompt will suggest to enter an ISO date, but you can also enter anything
  4444. which will at least partially be understood by `parse-time-string'.
  4445. Unrecognized parts of the date will default to the current day, month, year,
  4446. hour and minute. For example,
  4447. 3-2-5 --> 2003-02-05
  4448. feb 15 --> currentyear-02-15
  4449. sep 12 9 --> 2009-09-12
  4450. 12:45 --> today 12:45
  4451. 22 sept 0:34 --> currentyear-09-22 0:34
  4452. 12 --> currentyear-currentmonth-12
  4453. Fri --> nearest Friday (today or later)
  4454. etc.
  4455. The function understands only English month and weekday abbreviations,
  4456. but this can be configured with the variables `parse-time-months' and
  4457. `parse-time-weekdays'.
  4458. While prompting, a calendar is popped up - you can also select the
  4459. date with the mouse (button 1). The calendar shows a period of three
  4460. months. To scroll it to other months, use the keys `>' and `<'.
  4461. If you don't like the calendar, turn it off with
  4462. \(setq org-popup-calendar-for-date-prompt nil)
  4463. With optional argument TO-TIME, the date will immediately be converted
  4464. to an internal time.
  4465. With an optional argument WITH-TIME, the prompt will suggest to also
  4466. insert a time. Note that when WITH-TIME is not set, you can still
  4467. enter a time, and this function will inform the calling routine about
  4468. this change. The calling routine may then choose to change the format
  4469. used to insert the time stamp into the buffer to include the time."
  4470. (require 'parse-time)
  4471. (let* ((org-time-stamp-rounding-minutes
  4472. (if (equal with-time '(16)) 0 org-time-stamp-rounding-minutes))
  4473. (ct (org-current-time))
  4474. (default-time
  4475. ;; Default time is either today, or, when entering a range,
  4476. ;; the range start.
  4477. (if (save-excursion
  4478. (re-search-backward
  4479. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  4480. (- (point) 20) t))
  4481. (apply
  4482. 'encode-time
  4483. (mapcar (lambda(x) (or x 0))
  4484. (parse-time-string (match-string 1))))
  4485. ct))
  4486. (calendar-move-hook nil)
  4487. (view-diary-entries-initially nil)
  4488. (view-calendar-holidays-initially nil)
  4489. (timestr (format-time-string
  4490. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") default-time))
  4491. (prompt (format "YYYY-MM-DD [%s]: " timestr))
  4492. ans ans1 ans2
  4493. second minute hour day month year tl wday wday1)
  4494. (if org-popup-calendar-for-date-prompt
  4495. (save-excursion
  4496. (save-window-excursion
  4497. (calendar)
  4498. (calendar-forward-day (- (time-to-days default-time)
  4499. (calendar-absolute-from-gregorian
  4500. (calendar-current-date))))
  4501. (org-eval-in-calendar nil)
  4502. (let* ((old-map (current-local-map))
  4503. (map (copy-keymap calendar-mode-map))
  4504. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  4505. (define-key map (kbd "RET") 'org-calendar-select)
  4506. (define-key map (if (featurep 'xemacs) [button1] [mouse-1])
  4507. 'org-calendar-select-mouse)
  4508. (define-key map (if (featurep 'xemacs) [button2] [mouse-2])
  4509. 'org-calendar-select-mouse)
  4510. (define-key minibuffer-local-map [(meta shift left)]
  4511. (lambda () (interactive)
  4512. (org-eval-in-calendar '(calendar-backward-month 1))))
  4513. (define-key minibuffer-local-map [(meta shift right)]
  4514. (lambda () (interactive)
  4515. (org-eval-in-calendar '(calendar-forward-month 1))))
  4516. (define-key minibuffer-local-map [(shift up)]
  4517. (lambda () (interactive)
  4518. (org-eval-in-calendar '(calendar-backward-week 1))))
  4519. (define-key minibuffer-local-map [(shift down)]
  4520. (lambda () (interactive)
  4521. (org-eval-in-calendar '(calendar-forward-week 1))))
  4522. (define-key minibuffer-local-map [(shift left)]
  4523. (lambda () (interactive)
  4524. (org-eval-in-calendar '(calendar-backward-day 1))))
  4525. (define-key minibuffer-local-map [(shift right)]
  4526. (lambda () (interactive)
  4527. (org-eval-in-calendar '(calendar-forward-day 1))))
  4528. (define-key minibuffer-local-map ">"
  4529. (lambda () (interactive)
  4530. (org-eval-in-calendar '(scroll-calendar-left 1))))
  4531. (define-key minibuffer-local-map "<"
  4532. (lambda () (interactive)
  4533. (org-eval-in-calendar '(scroll-calendar-right 1))))
  4534. (unwind-protect
  4535. (progn
  4536. (use-local-map map)
  4537. (setq ans (read-string prompt "" nil nil))
  4538. (if (not (string-match "\\S-" ans)) (setq ans nil))
  4539. (setq ans (or ans1 ans ans2)))
  4540. (use-local-map old-map)))))
  4541. ;; Naked prompt only
  4542. (setq ans (read-string prompt "" nil timestr)))
  4543. (org-detatch-overlay org-date-ovl)
  4544. (if (string-match
  4545. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  4546. (progn
  4547. (setq year (if (match-end 2)
  4548. (string-to-number (match-string 2 ans))
  4549. (string-to-number (format-time-string "%Y")))
  4550. month (string-to-number (match-string 3 ans))
  4551. day (string-to-number (match-string 4 ans)))
  4552. (if (< year 100) (setq year (+ 2000 year)))
  4553. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  4554. t nil ans))))
  4555. (setq tl (parse-time-string ans)
  4556. year (or (nth 5 tl) (string-to-number (format-time-string "%Y" ct)))
  4557. month (or (nth 4 tl) (string-to-number (format-time-string "%m" ct)))
  4558. day (or (nth 3 tl) (string-to-number (format-time-string "%d" ct)))
  4559. hour (or (nth 2 tl) (string-to-number (format-time-string "%H" ct)))
  4560. minute (or (nth 1 tl) (string-to-number (format-time-string "%M" ct)))
  4561. second (or (nth 0 tl) 0)
  4562. wday (nth 6 tl))
  4563. (when (and wday (not (nth 3 tl)))
  4564. ;; Weekday was given, but no day, so pick that day in the week
  4565. ;; on or after the derived date.
  4566. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  4567. (unless (equal wday wday1)
  4568. (setq day (+ day (% (- wday wday1 -7) 7)))))
  4569. (if (and (boundp 'org-time-was-given)
  4570. (nth 2 tl))
  4571. (setq org-time-was-given t))
  4572. (if (< year 100) (setq year (+ 2000 year)))
  4573. (if to-time
  4574. (encode-time second minute hour day month year)
  4575. (if (or (nth 1 tl) (nth 2 tl))
  4576. (format "%04d-%02d-%02d %02d:%02d" year month day hour minute)
  4577. (format "%04d-%02d-%02d" year month day)))))
  4578. (defun org-eval-in-calendar (form)
  4579. "Eval FORM in the calendar window and return to current window.
  4580. Also, store the cursor date in variable ans2."
  4581. (let ((sw (selected-window)))
  4582. (select-window (get-buffer-window "*Calendar*"))
  4583. (eval form)
  4584. (when (calendar-cursor-to-date)
  4585. (let* ((date (calendar-cursor-to-date))
  4586. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  4587. (setq ans2 (format-time-string "%Y-%m-%d" time))))
  4588. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  4589. (select-window sw)))
  4590. (defun org-calendar-select ()
  4591. "Return to `org-read-date' with the date currently selected.
  4592. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  4593. (interactive)
  4594. (when (calendar-cursor-to-date)
  4595. (let* ((date (calendar-cursor-to-date))
  4596. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  4597. (setq ans1 (format-time-string "%Y-%m-%d" time)))
  4598. (if (active-minibuffer-window) (exit-minibuffer))))
  4599. (defun org-calendar-select-mouse (ev)
  4600. "Return to `org-read-date' with the date currently selected.
  4601. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  4602. (interactive "e")
  4603. (mouse-set-point ev)
  4604. (when (calendar-cursor-to-date)
  4605. (let* ((date (calendar-cursor-to-date))
  4606. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  4607. (setq ans1 (format-time-string "%Y-%m-%d" time)))
  4608. (if (active-minibuffer-window) (exit-minibuffer))))
  4609. (defun org-check-deadlines (ndays)
  4610. "Check if there are any deadlines due or past due.
  4611. A deadline is considered due if it happens within `org-deadline-warning-days'
  4612. days from today's date. If the deadline appears in an entry marked DONE,
  4613. it is not shown. The prefix arg NDAYS can be used to test that many
  4614. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  4615. (interactive "P")
  4616. (let* ((org-warn-days
  4617. (cond
  4618. ((equal ndays '(4)) 100000)
  4619. (ndays (prefix-numeric-value ndays))
  4620. (t org-deadline-warning-days)))
  4621. (case-fold-search nil)
  4622. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  4623. (callback
  4624. (lambda ()
  4625. (and (let ((d1 (time-to-days (current-time)))
  4626. (d2 (time-to-days
  4627. (org-time-string-to-time (match-string 1)))))
  4628. (< (- d2 d1) org-warn-days))
  4629. (not (org-entry-is-done-p))))))
  4630. (message "%d deadlines past-due or due within %d days"
  4631. (org-occur regexp callback)
  4632. org-warn-days)))
  4633. (defun org-evaluate-time-range (&optional to-buffer)
  4634. "Evaluate a time range by computing the difference between start and end.
  4635. Normally the result is just printed in the echo area, but with prefix arg
  4636. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  4637. If the time range is actually in a table, the result is inserted into the
  4638. next column.
  4639. For time difference computation, a year is assumed to be exactly 365
  4640. days in order to avoid rounding problems."
  4641. (interactive "P")
  4642. (save-excursion
  4643. (unless (org-at-date-range-p)
  4644. (goto-char (point-at-bol))
  4645. (re-search-forward org-tr-regexp (point-at-eol) t))
  4646. (if (not (org-at-date-range-p))
  4647. (error "Not at a time-stamp range, and none found in current line")))
  4648. (let* ((ts1 (match-string 1))
  4649. (ts2 (match-string 2))
  4650. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  4651. (match-end (match-end 0))
  4652. (time1 (org-time-string-to-time ts1))
  4653. (time2 (org-time-string-to-time ts2))
  4654. (t1 (time-to-seconds time1))
  4655. (t2 (time-to-seconds time2))
  4656. (diff (abs (- t2 t1)))
  4657. (negative (< (- t2 t1) 0))
  4658. ;; (ys (floor (* 365 24 60 60)))
  4659. (ds (* 24 60 60))
  4660. (hs (* 60 60))
  4661. (fy "%dy %dd %02d:%02d")
  4662. (fy1 "%dy %dd")
  4663. (fd "%dd %02d:%02d")
  4664. (fd1 "%dd")
  4665. (fh "%02d:%02d")
  4666. y d h m align)
  4667. (if havetime
  4668. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  4669. y 0
  4670. d (floor (/ diff ds)) diff (mod diff ds)
  4671. h (floor (/ diff hs)) diff (mod diff hs)
  4672. m (floor (/ diff 60)))
  4673. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  4674. y 0
  4675. d (floor (+ (/ diff ds) 0.5))
  4676. h 0 m 0))
  4677. (if (not to-buffer)
  4678. (message (org-make-tdiff-string y d h m))
  4679. (when (org-at-table-p)
  4680. (goto-char match-end)
  4681. (setq align t)
  4682. (and (looking-at " *|") (goto-char (match-end 0))))
  4683. (if (looking-at
  4684. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  4685. (replace-match ""))
  4686. (if negative (insert " -"))
  4687. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  4688. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  4689. (insert " " (format fh h m))))
  4690. (if align (org-table-align))
  4691. (message "Time difference inserted"))))
  4692. (defun org-make-tdiff-string (y d h m)
  4693. (let ((fmt "")
  4694. (l nil))
  4695. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  4696. l (push y l)))
  4697. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  4698. l (push d l)))
  4699. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  4700. l (push h l)))
  4701. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  4702. l (push m l)))
  4703. (apply 'format fmt (nreverse l))))
  4704. (defun org-time-string-to-time (s)
  4705. (apply 'encode-time (org-parse-time-string s)))
  4706. (defun org-parse-time-string (s &optional nodefault)
  4707. "Parse the standard Org-mode time string.
  4708. This should be a lot faster than the normal `parse-time-string'.
  4709. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  4710. hour and minute fields will be nil if not given."
  4711. (if (string-match org-ts-regexp1 s)
  4712. (list 0
  4713. (if (or (match-beginning 8) (not nodefault))
  4714. (string-to-number (or (match-string 8 s) "0")))
  4715. (if (or (match-beginning 7) (not nodefault))
  4716. (string-to-number (or (match-string 7 s) "0")))
  4717. (string-to-number (match-string 4 s))
  4718. (string-to-number (match-string 3 s))
  4719. (string-to-number (match-string 2 s))
  4720. nil nil nil)
  4721. (make-list 9 0)))
  4722. (defun org-timestamp-up (&optional arg)
  4723. "Increase the date item at the cursor by one.
  4724. If the cursor is on the year, change the year. If it is on the month or
  4725. the day, change that.
  4726. With prefix ARG, change by that many units."
  4727. (interactive "p")
  4728. (org-timestamp-change (prefix-numeric-value arg)))
  4729. (defun org-timestamp-down (&optional arg)
  4730. "Decrease the date item at the cursor by one.
  4731. If the cursor is on the year, change the year. If it is on the month or
  4732. the day, change that.
  4733. With prefix ARG, change by that many units."
  4734. (interactive "p")
  4735. (org-timestamp-change (- (prefix-numeric-value arg))))
  4736. (defun org-timestamp-up-day (&optional arg)
  4737. "Increase the date in the time stamp by one day.
  4738. With prefix ARG, change that many days."
  4739. (interactive "p")
  4740. (if (and (not (org-at-timestamp-p))
  4741. (org-on-heading-p))
  4742. (org-todo 'up)
  4743. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  4744. (defun org-timestamp-down-day (&optional arg)
  4745. "Decrease the date in the time stamp by one day.
  4746. With prefix ARG, change that many days."
  4747. (interactive "p")
  4748. (if (and (not (org-at-timestamp-p))
  4749. (org-on-heading-p))
  4750. (org-todo 'down)
  4751. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  4752. (defsubst org-pos-in-match-range (pos n)
  4753. (and (match-beginning n)
  4754. (<= (match-beginning n) pos)
  4755. (>= (match-end n) pos)))
  4756. (defun org-at-timestamp-p ()
  4757. "Determine if the cursor is in or at a timestamp."
  4758. (interactive)
  4759. (let* ((tsr org-ts-regexp2)
  4760. (pos (point))
  4761. (ans (or (looking-at tsr)
  4762. (save-excursion
  4763. (skip-chars-backward "^<\n\r\t")
  4764. (if (> (point) 1) (backward-char 1))
  4765. (and (looking-at tsr)
  4766. (> (- (match-end 0) pos) -1))))))
  4767. (and (boundp 'org-ts-what)
  4768. (setq org-ts-what
  4769. (cond
  4770. ((org-pos-in-match-range pos 2) 'year)
  4771. ((org-pos-in-match-range pos 3) 'month)
  4772. ((org-pos-in-match-range pos 7) 'hour)
  4773. ((org-pos-in-match-range pos 8) 'minute)
  4774. ((or (org-pos-in-match-range pos 4)
  4775. (org-pos-in-match-range pos 5)) 'day)
  4776. (t 'day))))
  4777. ans))
  4778. (defun org-timestamp-change (n &optional what)
  4779. "Change the date in the time stamp at point.
  4780. The date will be changed by N times WHAT. WHAT can be `day', `month',
  4781. `year', `minute', `second'. If WHAT is not given, the cursor position
  4782. in the timestamp determines what will be changed."
  4783. (let ((fmt (car org-time-stamp-formats))
  4784. org-ts-what
  4785. (pos (point))
  4786. ts time time0)
  4787. (if (not (org-at-timestamp-p))
  4788. (error "Not at a timestamp"))
  4789. (setq org-ts-what (or what org-ts-what))
  4790. (setq fmt (if (<= (abs (- (cdr org-ts-lengths)
  4791. (- (match-end 0) (match-beginning 0))))
  4792. 1)
  4793. (cdr org-time-stamp-formats)
  4794. (car org-time-stamp-formats)))
  4795. (setq ts (match-string 0))
  4796. (replace-match "")
  4797. (setq time0 (org-parse-time-string ts))
  4798. (setq time
  4799. (apply 'encode-time
  4800. (append
  4801. (list (or (car time0) 0))
  4802. (list (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0)))
  4803. (list (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0)))
  4804. (list (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0)))
  4805. (list (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0)))
  4806. (list (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0)))
  4807. (nthcdr 6 time0))))
  4808. (if (eq what 'calendar)
  4809. (let ((cal-date
  4810. (save-excursion
  4811. (save-match-data
  4812. (set-buffer "*Calendar*")
  4813. (calendar-cursor-to-date)))))
  4814. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  4815. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  4816. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  4817. (setcar time0 (or (car time0) 0))
  4818. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  4819. (setcar (nthcdr 2 time0) (or (nth 1 time0) 0))
  4820. (setq time (apply 'encode-time time0))))
  4821. (insert (setq org-last-changed-timestamp (format-time-string fmt time)))
  4822. (goto-char pos)
  4823. ;; Try to recenter the calendar window, if any
  4824. (if (and org-calendar-follow-timestamp-change
  4825. (get-buffer-window "*Calendar*" t)
  4826. (memq org-ts-what '(day month year)))
  4827. (org-recenter-calendar (time-to-days time)))))
  4828. (defun org-recenter-calendar (date)
  4829. "If the calendar is visible, recenter it to DATE."
  4830. (let* ((win (selected-window))
  4831. (cwin (get-buffer-window "*Calendar*" t))
  4832. (calendar-move-hook nil))
  4833. (when cwin
  4834. (select-window cwin)
  4835. (calendar-goto-date (if (listp date) date
  4836. (calendar-gregorian-from-absolute date)))
  4837. (select-window win))))
  4838. (defun org-goto-calendar (&optional arg)
  4839. "Go to the Emacs calendar at the current date.
  4840. If there is a time stamp in the current line, go to that date.
  4841. A prefix ARG can be used to force the current date."
  4842. (interactive "P")
  4843. (let ((tsr org-ts-regexp) diff
  4844. (calendar-move-hook nil)
  4845. (view-calendar-holidays-initially nil)
  4846. (view-diary-entries-initially nil))
  4847. (if (or (org-at-timestamp-p)
  4848. (save-excursion
  4849. (beginning-of-line 1)
  4850. (looking-at (concat ".*" tsr))))
  4851. (let ((d1 (time-to-days (current-time)))
  4852. (d2 (time-to-days
  4853. (org-time-string-to-time (match-string 1)))))
  4854. (setq diff (- d2 d1))))
  4855. (calendar)
  4856. (calendar-goto-today)
  4857. (if (and diff (not arg)) (calendar-forward-day diff))))
  4858. (defun org-date-from-calendar ()
  4859. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  4860. If there is already a time stamp at the cursor position, update it."
  4861. (interactive)
  4862. (org-timestamp-change 0 'calendar))
  4863. ;;; The clock for measuring work time.
  4864. (defvar org-clock-marker (make-marker)
  4865. "Marker recording the last clock-in.")
  4866. (defun org-clock-in ()
  4867. "Start the clock on the current item.
  4868. If necessary, clock-out of the currently active clock."
  4869. (interactive)
  4870. (org-clock-out t)
  4871. (let (ts)
  4872. (save-excursion
  4873. (org-back-to-heading t)
  4874. (beginning-of-line 2)
  4875. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  4876. (not (equal (match-string 1) org-clock-string)))
  4877. (beginning-of-line 1))
  4878. (insert "\n") (backward-char 1)
  4879. (indent-relative)
  4880. (insert org-clock-string " "
  4881. (setq ts (concat "[" (format-time-string
  4882. (substring
  4883. (cdr org-time-stamp-formats) 1 -1)
  4884. (current-time))
  4885. "]")))
  4886. (move-marker org-clock-marker (point))
  4887. (message "Clock started at %s" ts))))
  4888. (defun org-clock-out (&optional fail-quietly)
  4889. "Stop the currently running clock.
  4890. If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
  4891. (interactive)
  4892. (catch 'exit
  4893. (if (not (marker-buffer org-clock-marker))
  4894. (if fail-quietly (throw 'exit t) (error "No active clock")))
  4895. (let (ts te s h m)
  4896. (save-excursion
  4897. (set-buffer (marker-buffer org-clock-marker))
  4898. (goto-char org-clock-marker)
  4899. (beginning-of-line 1)
  4900. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  4901. (equal (match-string 1) org-clock-string))
  4902. (setq ts (match-string 2))
  4903. (if fail-quietly (throw 'exit nil) (error "Clock start time is gone")))
  4904. (goto-char org-clock-marker)
  4905. (setq te (concat "[" (format-time-string
  4906. (substring
  4907. (cdr org-time-stamp-formats) 1 -1)
  4908. (current-time))
  4909. "]"))
  4910. (setq s (- (time-to-seconds (apply 'encode-time (org-parse-time-string te)))
  4911. (time-to-seconds (apply 'encode-time (org-parse-time-string ts))))
  4912. h (floor (/ s 3600))
  4913. s (- s (* 3600 h))
  4914. m (floor (/ s 60))
  4915. s (- s (* 60 s)))
  4916. (insert "--" te " => " (format "%2d:%02d" h m))
  4917. (move-marker org-clock-marker nil)
  4918. (message "Clock stopped at %s after HH:MM = %d:%02d" te h m)))))
  4919. (defun org-clock-cancel ()
  4920. "Cancel the running clock be removing the start timestamp."
  4921. (interactive)
  4922. (if (not (marker-buffer org-clock-marker))
  4923. (error "No active clock"))
  4924. (save-excursion
  4925. (set-buffer (marker-buffer org-clock-marker))
  4926. (goto-char org-clock-marker)
  4927. (delete-region (1- (point-at-bol)) (point-at-eol)))
  4928. (message "Clock canceled"))
  4929. (defvar org-clock-file-total-minutes nil
  4930. "Holds the file total time in minutes, after a call to `org-clock-sum'.")
  4931. (make-variable-buffer-local 'org-clock-file-total-minutes)
  4932. (defun org-clock-sum ()
  4933. "Sum the times for each subtree.
  4934. Puts the resulting times in minutes as a text property on each headline."
  4935. (interactive)
  4936. (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t))
  4937. (let* ((re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
  4938. org-clock-string
  4939. ".*=>[ \t]*\\([0-9]+\\):\\([0-9]+\\)[ \t]*$"))
  4940. (lmax 30)
  4941. (ltimes (make-vector lmax 0))
  4942. (t1 0)
  4943. (level 0)
  4944. (lastlevel 0) time)
  4945. (save-excursion
  4946. (goto-char (point-max))
  4947. (while (re-search-backward re nil t)
  4948. (if (match-end 2)
  4949. ;; A time
  4950. (setq t1 (+ t1 (* 60 (string-to-number (match-string 2)))
  4951. (string-to-number (match-string 3))))
  4952. ;; A headline
  4953. (setq level (- (match-end 1) (match-beginning 1)))
  4954. (when (or (> t1 0) (> (aref ltimes level) 0))
  4955. (loop for l from 0 to level do
  4956. (aset ltimes l (+ (aref ltimes l) t1)))
  4957. (setq t1 0 time (aref ltimes level))
  4958. (loop for l from level to (1- lmax) do
  4959. (aset ltimes l 0))
  4960. (goto-char (match-beginning 0))
  4961. (put-text-property (point) (point-at-eol) :org-clock-minutes time))))
  4962. (setq org-clock-file-total-minutes (aref ltimes 0)))))
  4963. (defun org-clock-display (&optional total-only)
  4964. "Show subtree times in the entire buffer.
  4965. If TOTAL-ONLY is non-nil, only show the total time for the entire file
  4966. in the echo area."
  4967. (interactive)
  4968. (org-remove-clock-overlays)
  4969. (let (time h m p)
  4970. (org-clock-sum)
  4971. (unless total-only
  4972. (save-excursion
  4973. (goto-char (point-min))
  4974. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  4975. (goto-char p)
  4976. (when (setq time (get-text-property p :org-clock-minutes))
  4977. (org-put-clock-overlay time (funcall outline-level))))
  4978. (setq h (/ org-clock-file-total-minutes 60)
  4979. m (- org-clock-file-total-minutes (* 60 h)))
  4980. ;; Arrange to remove the overlays upon next change.
  4981. (org-add-hook 'before-change-functions 'org-remove-clock-overlays
  4982. nil 'local)))
  4983. (message "Total file time: %d:%02d (%d hours and %d minutes)" h m h m)))
  4984. (defvar org-clock-overlays nil)
  4985. (defun org-put-clock-overlay (time &optional level)
  4986. "Put an overlays on the current line, displaying TIME.
  4987. If LEVEL is given, prefix time with a corresponding number of stars.
  4988. This creates a new overlay and stores it in `org-clock-overlays', so that it
  4989. will be easy to remove."
  4990. (let* ((c 60) (h (floor (/ time 60))) (m (- time (* 60 h)))
  4991. (l (if level (org-get-legal-level level 0) 0))
  4992. (off 0)
  4993. ov tx)
  4994. (move-to-column c)
  4995. (if (eolp) (setq off 1))
  4996. (unless (eolp) (skip-chars-backward "^ \t"))
  4997. (skip-chars-backward " \t")
  4998. (setq ov (org-make-overlay (- (point) off) (point-at-eol))
  4999. tx (concat (make-string (+ off (max 0 (- c (current-column)))) ?.)
  5000. (org-add-props (format "%s %2d:%02d%s"
  5001. (make-string l ?*) h m
  5002. (make-string (- 10 l) ?\ ))
  5003. '(face secondary-selection))
  5004. ""))
  5005. (org-overlay-put ov 'display tx)
  5006. (push ov org-clock-overlays)))
  5007. (defun org-remove-clock-overlays (&optional beg end noremove)
  5008. "Remove the occur highlights from the buffer.
  5009. BEG and END are ignored. If NOREMOVE is nil, remove this function
  5010. from the `before-change-functions' in the current buffer."
  5011. (interactive)
  5012. (mapc 'org-delete-overlay org-clock-overlays)
  5013. (setq org-clock-overlays nil)
  5014. (unless noremove
  5015. (remove-hook 'before-change-functions
  5016. 'org-remove-clock-overlays 'local)))
  5017. (defun org-clock-out-if-current ()
  5018. "Clock out if the current entry contains the running clock.
  5019. This is used to stop the clock after a TODO entry is marked DONE."
  5020. (when (and (equal state org-done-string)
  5021. (equal (marker-buffer org-clock-marker) (current-buffer))
  5022. (< (point) org-clock-marker)
  5023. (> (save-excursion (outline-next-heading) (point))
  5024. org-clock-marker))
  5025. (org-clock-out)))
  5026. (add-hook 'org-after-todo-state-change-hook
  5027. 'org-clock-out-if-current)
  5028. (defun org-check-running-clock ()
  5029. "Check if the current buffer contains the running clock.
  5030. If yes, offer to stop it and to save the buffer with the changes."
  5031. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  5032. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  5033. (buffer-name))))
  5034. (org-clock-out)
  5035. (when (y-or-n-p "Save changed buffer?")
  5036. (save-buffer))))
  5037. ;;; Agenda, and Diary Integration
  5038. ;;; Define the mode
  5039. (defvar org-agenda-mode-map (make-sparse-keymap)
  5040. "Keymap for `org-agenda-mode'.")
  5041. (defvar org-agenda-menu) ; defined later in this file.
  5042. (defvar org-agenda-follow-mode nil)
  5043. (defvar org-agenda-show-log nil)
  5044. (defvar org-agenda-buffer-name "*Org Agenda*")
  5045. (defvar org-agenda-redo-command nil)
  5046. (defvar org-agenda-mode-hook nil)
  5047. (defvar org-agenda-type nil)
  5048. (defvar org-agenda-force-single-file nil)
  5049. (defun org-agenda-mode ()
  5050. "Mode for time-sorted view on action items in Org-mode files.
  5051. The following commands are available:
  5052. \\{org-agenda-mode-map}"
  5053. (interactive)
  5054. (kill-all-local-variables)
  5055. (setq major-mode 'org-agenda-mode)
  5056. (setq mode-name "Org-Agenda")
  5057. (use-local-map org-agenda-mode-map)
  5058. (easy-menu-add org-agenda-menu)
  5059. (if org-startup-truncated (setq truncate-lines t))
  5060. (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
  5061. (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
  5062. (unless org-agenda-keep-modes
  5063. (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
  5064. org-agenda-show-log nil))
  5065. (easy-menu-change
  5066. '("Agenda") "Agenda Files"
  5067. (append
  5068. (list
  5069. (vector
  5070. (if (get 'org-agenda-files 'org-restrict)
  5071. "Restricted to single file"
  5072. "Edit File List")
  5073. '(org-edit-agenda-file-list)
  5074. (not (get 'org-agenda-files 'org-restrict)))
  5075. "--")
  5076. (mapcar 'org-file-menu-entry (org-agenda-files))))
  5077. (org-agenda-set-mode-name)
  5078. (apply
  5079. (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
  5080. (list 'org-agenda-mode-hook)))
  5081. (define-key org-agenda-mode-map "\C-i" 'org-agenda-goto)
  5082. (define-key org-agenda-mode-map [(tab)] 'org-agenda-goto)
  5083. (define-key org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
  5084. (define-key org-agenda-mode-map " " 'org-agenda-show)
  5085. (define-key org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
  5086. (define-key org-agenda-mode-map "o" 'delete-other-windows)
  5087. (define-key org-agenda-mode-map "L" 'org-agenda-recenter)
  5088. (define-key org-agenda-mode-map "t" 'org-agenda-todo)
  5089. (define-key org-agenda-mode-map ":" 'org-agenda-set-tags)
  5090. (define-key org-agenda-mode-map "." 'org-agenda-goto-today)
  5091. (define-key org-agenda-mode-map "d" 'org-agenda-day-view)
  5092. (define-key org-agenda-mode-map "w" 'org-agenda-week-view)
  5093. (define-key org-agenda-mode-map (org-key 'S-right) 'org-agenda-date-later)
  5094. (define-key org-agenda-mode-map (org-key 'S-left) 'org-agenda-date-earlier)
  5095. (define-key org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-date-later)
  5096. (define-key org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-date-earlier)
  5097. (define-key org-agenda-mode-map ">" 'org-agenda-date-prompt)
  5098. (define-key org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
  5099. (define-key org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
  5100. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  5101. (while l (define-key org-agenda-mode-map
  5102. (int-to-string (pop l)) 'digit-argument)))
  5103. (define-key org-agenda-mode-map "f" 'org-agenda-follow-mode)
  5104. (define-key org-agenda-mode-map "l" 'org-agenda-log-mode)
  5105. (define-key org-agenda-mode-map "D" 'org-agenda-toggle-diary)
  5106. (define-key org-agenda-mode-map "g" 'org-agenda-toggle-time-grid)
  5107. (define-key org-agenda-mode-map "r" 'org-agenda-redo)
  5108. (define-key org-agenda-mode-map "q" 'org-agenda-quit)
  5109. (define-key org-agenda-mode-map "x" 'org-agenda-exit)
  5110. (define-key org-agenda-mode-map "P" 'org-agenda-show-priority)
  5111. (define-key org-agenda-mode-map "T" 'org-agenda-show-tags)
  5112. (define-key org-agenda-mode-map "n" 'next-line)
  5113. (define-key org-agenda-mode-map "p" 'previous-line)
  5114. (define-key org-agenda-mode-map "\C-n" 'org-agenda-next-date-line)
  5115. (define-key org-agenda-mode-map "\C-p" 'org-agenda-previous-date-line)
  5116. (define-key org-agenda-mode-map "," 'org-agenda-priority)
  5117. (define-key org-agenda-mode-map "\C-c," 'org-agenda-priority)
  5118. (define-key org-agenda-mode-map "i" 'org-agenda-diary-entry)
  5119. (define-key org-agenda-mode-map "c" 'org-agenda-goto-calendar)
  5120. (eval-after-load "calendar"
  5121. '(define-key calendar-mode-map org-calendar-to-agenda-key
  5122. 'org-calendar-goto-agenda))
  5123. (define-key org-agenda-mode-map "C" 'org-agenda-convert-date)
  5124. (define-key org-agenda-mode-map "m" 'org-agenda-phases-of-moon)
  5125. (define-key org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
  5126. (define-key org-agenda-mode-map "s" 'org-agenda-sunrise-sunset)
  5127. (define-key org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
  5128. (define-key org-agenda-mode-map "h" 'org-agenda-holidays)
  5129. (define-key org-agenda-mode-map "H" 'org-agenda-holidays)
  5130. (define-key org-agenda-mode-map "+" 'org-agenda-priority-up)
  5131. (define-key org-agenda-mode-map "I" 'org-agenda-clock-in)
  5132. (define-key org-agenda-mode-map "O" 'org-clock-out)
  5133. (define-key org-agenda-mode-map "X" 'org-clock-cancel)
  5134. (define-key org-agenda-mode-map "-" 'org-agenda-priority-down)
  5135. (define-key org-agenda-mode-map (org-key 'S-up) 'org-agenda-priority-up)
  5136. (define-key org-agenda-mode-map (org-key 'S-down) 'org-agenda-priority-down)
  5137. (define-key org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
  5138. (define-key org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
  5139. (define-key org-agenda-mode-map [(right)] 'org-agenda-later)
  5140. (define-key org-agenda-mode-map [(left)] 'org-agenda-earlier)
  5141. (define-key org-agenda-mode-map "\C-c\C-x\C-c" 'org-export-icalendar-combine-agenda-files)
  5142. (defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
  5143. "Local keymap for agenda entries from Org-mode.")
  5144. (define-key org-agenda-keymap
  5145. (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
  5146. (define-key org-agenda-keymap
  5147. (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
  5148. (when org-agenda-mouse-1-follows-link
  5149. (define-key org-agenda-keymap [follow-link] 'mouse-face))
  5150. (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
  5151. '("Agenda"
  5152. ("Agenda Files")
  5153. "--"
  5154. ["Show" org-agenda-show t]
  5155. ["Go To (other window)" org-agenda-goto t]
  5156. ["Go To (one window)" org-agenda-switch-to t]
  5157. ["Follow Mode" org-agenda-follow-mode
  5158. :style toggle :selected org-agenda-follow-mode :active t]
  5159. "--"
  5160. ["Cycle TODO" org-agenda-todo t]
  5161. ("Tags"
  5162. ["Show all Tags" org-agenda-show-tags t]
  5163. ["Set Tags" org-agenda-set-tags t])
  5164. ("Schedule"
  5165. ["Schedule" org-agenda-schedule t]
  5166. ["Set Deadline" org-agenda-deadline t]
  5167. "--"
  5168. ["Reschedule +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
  5169. ["Reschedule -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
  5170. ["Reschedule to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
  5171. ("Priority"
  5172. ["Set Priority" org-agenda-priority t]
  5173. ["Increase Priority" org-agenda-priority-up t]
  5174. ["Decrease Priority" org-agenda-priority-down t]
  5175. ["Show Priority" org-agenda-show-priority t])
  5176. "--"
  5177. ;; ["New agenda command" org-agenda t]
  5178. ["Rebuild buffer" org-agenda-redo t]
  5179. "--"
  5180. ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
  5181. ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
  5182. ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
  5183. "--"
  5184. ["Day View" org-agenda-day-view :active (org-agenda-check-type nil 'agenda)
  5185. :style radio :selected (equal org-agenda-ndays 1)]
  5186. ["Week View" org-agenda-week-view :active (org-agenda-check-type nil 'agenda)
  5187. :style radio :selected (equal org-agenda-ndays 7)]
  5188. "--"
  5189. ["Show Logbook entries" org-agenda-log-mode
  5190. :style toggle :selected org-agenda-show-log :active (org-agenda-check-type nil 'agenda 'timeline)]
  5191. ["Include Diary" org-agenda-toggle-diary
  5192. :style toggle :selected org-agenda-include-diary :active (org-agenda-check-type nil 'agenda)]
  5193. ["Use Time Grid" org-agenda-toggle-time-grid
  5194. :style toggle :selected org-agenda-use-time-grid :active (org-agenda-check-type nil 'agenda)]
  5195. "--"
  5196. ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
  5197. ("Calendar Commands"
  5198. ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
  5199. ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
  5200. ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
  5201. ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
  5202. ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)])
  5203. ["Create iCalendar file" org-export-icalendar-combine-agenda-files t]
  5204. "--"
  5205. ["Quit" org-agenda-quit t]
  5206. ["Exit and Release Buffers" org-agenda-exit t]
  5207. ))
  5208. ;;;###autoload
  5209. (defun org-agenda (arg)
  5210. "Dispatch agenda commands to collect entries to the agenda buffer.
  5211. Prompts for a character to select a command. Any prefix arg will be passed
  5212. on to the selected command. The default selections are:
  5213. a Call `org-agenda' to display the agenda for the current day or week.
  5214. t Call `org-todo-list' to display the global todo list.
  5215. T Call `org-todo-list' to display the global todo list, select only
  5216. entries with a specific TODO keyword (the user gets a prompt).
  5217. m Call `org-tags-view' to display headlines with tags matching
  5218. a condition (the user is prompted for the condition).
  5219. M Like `m', but select only TODO entries, no ordinary headlines.
  5220. More commands can be added by configuring the variable
  5221. `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
  5222. searches can be pre-defined in this way.
  5223. If the current buffer is in Org-mode and visiting a file, you can also
  5224. first press `1' to indicate that the agenda should be temporarily (until the
  5225. next use of \\[org-agenda]) restricted to the current file."
  5226. (interactive "P")
  5227. (catch 'exit
  5228. (let ((restrict-ok (and buffer-file-name (eq major-mode 'org-mode)))
  5229. (bfn buffer-file-name)
  5230. (custom org-agenda-custom-commands)
  5231. c entry key type string)
  5232. (put 'org-agenda-files 'org-restrict nil)
  5233. (save-window-excursion
  5234. (delete-other-windows)
  5235. (switch-to-buffer-other-window " *Agenda Commands*")
  5236. (erase-buffer)
  5237. (insert
  5238. "Press key for an agenda command:
  5239. --------------------------------
  5240. a Agenda for current week or day
  5241. t List of all TODO entries T Entries with special TODO kwd
  5242. m Match a TAGS query M Like m, but only TODO entries
  5243. C Configure your own agenda commands")
  5244. (while (setq entry (pop custom))
  5245. (setq key (car entry) type (nth 1 entry) string (nth 2 entry))
  5246. (insert (format "\n%-4s%-14s: %s"
  5247. key
  5248. (cond
  5249. ((eq type 'tags) "Tags query")
  5250. ((eq type 'todo) "TODO keyword")
  5251. ((eq type 'tags-tree) "Tags tree")
  5252. ((eq type 'todo-tree) "TODO kwd tree")
  5253. ((eq type 'occur-tree) "Occur tree")
  5254. (t "???"))
  5255. (org-add-props string nil 'face 'org-warning))))
  5256. (goto-char (point-min))
  5257. (if (fboundp 'fit-window-to-buffer) (fit-window-to-buffer))
  5258. (message "Press key for agenda command%s"
  5259. (if restrict-ok ", or [1] to restrict to current file" ""))
  5260. (setq c (read-char-exclusive))
  5261. (message "")
  5262. (when (equal c ?1)
  5263. (if restrict-ok
  5264. (put 'org-agenda-files 'org-restrict (list bfn))
  5265. (error "Cannot restrict agenda to current buffer"))
  5266. (message "Press key for agenda command%s"
  5267. (if restrict-ok " (restricted to current file)" ""))
  5268. (setq c (read-char-exclusive))
  5269. (message "")))
  5270. (require 'calendar) ; FIXME: can we avoid this for some commands?
  5271. ;; For example the todo list should not need it (but does...)
  5272. (cond
  5273. ((equal c ?C) (customize-variable 'org-agenda-custom-commands))
  5274. ((equal c ?a) (call-interactively 'org-agenda-list))
  5275. ((equal c ?t) (call-interactively 'org-todo-list))
  5276. ((equal c ?T) (org-call-with-arg 'org-todo-list (or arg '(4))))
  5277. ((equal c ?m) (call-interactively 'org-tags-view))
  5278. ((equal c ?M) (org-call-with-arg 'org-tags-view (or arg '(4))))
  5279. ((setq entry (assoc (char-to-string c) org-agenda-custom-commands))
  5280. (setq type (nth 1 entry) string (nth 2 entry))
  5281. (cond
  5282. ((eq type 'tags)
  5283. (org-tags-view current-prefix-arg string))
  5284. ((eq type 'tags-todo)
  5285. (org-tags-view '(4) string))
  5286. ((eq type 'todo)
  5287. (org-todo-list string))
  5288. ((eq type 'tags-tree)
  5289. (org-check-for-org-mode)
  5290. (org-tags-sparse-tree current-prefix-arg string))
  5291. ((eq type 'todo-tree)
  5292. (org-check-for-org-mode)
  5293. (org-occur (concat "^" outline-regexp "[ \t]*"
  5294. (regexp-quote string) "\\>")))
  5295. ((eq type 'occur-tree)
  5296. (org-check-for-org-mode)
  5297. (org-occur string))
  5298. (t (error "Invalid custom agenda command type %s" type))))
  5299. (t (error "Invalid key"))))))
  5300. (defun org-check-for-org-mode ()
  5301. "Make sure current buffer is in org-mode. Error if not."
  5302. (or (eq major-mode 'org-mode)
  5303. (error "Cannot execute org-mode agenda command on buffer in %s."
  5304. major-mode)))
  5305. (defun org-fit-agenda-window ()
  5306. "Fit the window to the buffer size."
  5307. (and org-fit-agenda-window
  5308. (fboundp 'fit-window-to-buffer)
  5309. (fit-window-to-buffer nil (/ (* (frame-height) 3) 4)
  5310. (/ (frame-height) 2))))
  5311. (defun org-agenda-files (&optional unrestricted)
  5312. "Get the list of agenda files.
  5313. Optional UNRESTRICTED means return the full list even if a restriction
  5314. is currently in place."
  5315. (cond
  5316. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  5317. ((stringp org-agenda-files) (org-read-agenda-file-list))
  5318. ((listp org-agenda-files) org-agenda-files)
  5319. (t (error "Invalid value of `org-agenda-files'"))))
  5320. (defvar org-window-configuration)
  5321. (defun org-edit-agenda-file-list ()
  5322. "Edit the list of agenda files.
  5323. Depending on setup, this either uses customize to edit the variable
  5324. `org-agenda-files', or it visits the file that is holding the list. In the
  5325. latter case, the buffer is set up in a way that saving it automatically kills
  5326. the buffer and restores the previous window configuration."
  5327. (interactive)
  5328. (if (stringp org-agenda-files)
  5329. (let ((cw (current-window-configuration)))
  5330. (find-file org-agenda-files)
  5331. (set (make-local-variable 'org-window-configuration) cw)
  5332. (org-add-hook 'after-save-hook
  5333. (lambda ()
  5334. (set-window-configuration
  5335. (prog1 org-window-configuration
  5336. (kill-buffer (current-buffer))))
  5337. (org-install-agenda-files-menu)
  5338. (message "New agenda file list installed"))
  5339. nil 'local)
  5340. (message (substitute-command-keys
  5341. "Edit list and finish with \\[save-buffer]")))
  5342. (customize-variable 'org-agenda-files)))
  5343. (defun org-store-new-agenda-file-list (list)
  5344. "Set new value for the agenda file list and save it correcly."
  5345. (if (stringp org-agenda-files)
  5346. (let ((f org-agenda-files) b)
  5347. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  5348. (with-temp-file f
  5349. (insert (mapconcat 'identity list "\n") "\n")))
  5350. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  5351. (setq org-agenda-files list)
  5352. (customize-save-variable 'org-agenda-files org-agenda-files))))
  5353. (defun org-read-agenda-file-list ()
  5354. "Read the list of agenda files from a file."
  5355. (when (stringp org-agenda-files)
  5356. (with-temp-buffer
  5357. (insert-file-contents org-agenda-files)
  5358. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  5359. (defvar org-agenda-markers nil
  5360. "List of all currently active markers created by `org-agenda'.")
  5361. (defvar org-agenda-last-marker-time (time-to-seconds (current-time))
  5362. "Creation time of the last agenda marker.")
  5363. (defun org-agenda-new-marker (&optional pos)
  5364. "Return a new agenda marker.
  5365. Org-mode keeps a list of these markers and resets them when they are
  5366. no longer in use."
  5367. (let ((m (copy-marker (or pos (point)))))
  5368. (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
  5369. (push m org-agenda-markers)
  5370. m))
  5371. (defun org-agenda-maybe-reset-markers (&optional force)
  5372. "Reset markers created by `org-agenda'. But only if they are old enough."
  5373. (if (or force
  5374. (> (- (time-to-seconds (current-time))
  5375. org-agenda-last-marker-time)
  5376. 5))
  5377. (while org-agenda-markers
  5378. (move-marker (pop org-agenda-markers) nil))))
  5379. (defvar org-agenda-new-buffers nil
  5380. "Buffers created to visit agenda files.")
  5381. (defun org-get-agenda-file-buffer (file)
  5382. "Get a buffer visiting FILE. If the buffer needs to be created, add
  5383. it to the list of buffers which might be released later."
  5384. (let ((buf (find-buffer-visiting file)))
  5385. (if buf
  5386. buf ; just return it
  5387. ;; Make a new buffer and remember it
  5388. (setq buf (find-file-noselect file))
  5389. (if buf (push buf org-agenda-new-buffers))
  5390. buf)))
  5391. (defun org-release-buffers (blist)
  5392. "Release all buffers in list, asking the user for confirmation when needed.
  5393. When a buffer is unmodified, it is just killed. When modified, it is saved
  5394. \(if the user agrees) and then killed."
  5395. (let (buf file)
  5396. (while (setq buf (pop blist))
  5397. (setq file (buffer-file-name buf))
  5398. (when (and (buffer-modified-p buf)
  5399. file
  5400. (y-or-n-p (format "Save file %s? " file)))
  5401. (with-current-buffer buf (save-buffer)))
  5402. (kill-buffer buf))))
  5403. (defvar org-respect-restriction nil) ; Dynamically-scoped param.
  5404. (defun org-timeline (&optional include-all keep-modes)
  5405. "Show a time-sorted view of the entries in the current org file.
  5406. Only entries with a time stamp of today or later will be listed. With
  5407. \\[universal-argument] prefix, all unfinished TODO items will also be shown,
  5408. under the current date.
  5409. If the buffer contains an active region, only check the region for
  5410. dates."
  5411. (interactive "P")
  5412. (require 'calendar)
  5413. (org-agenda-maybe-reset-markers 'force)
  5414. (org-compile-prefix-format org-timeline-prefix-format)
  5415. (let* ((dopast t)
  5416. (dotodo include-all)
  5417. (doclosed org-agenda-show-log)
  5418. (org-agenda-keep-modes keep-modes)
  5419. (entry buffer-file-name)
  5420. (org-agenda-files (list buffer-file-name))
  5421. (date (calendar-current-date))
  5422. (win (selected-window))
  5423. (pos1 (point))
  5424. (beg (if (org-region-active-p) (region-beginning) (point-min)))
  5425. (end (if (org-region-active-p) (region-end) (point-max)))
  5426. (day-numbers (org-get-all-dates beg end 'no-ranges
  5427. t doclosed ; always include today
  5428. org-timeline-show-empty-dates))
  5429. (today (time-to-days (current-time)))
  5430. (org-respect-restriction t)
  5431. (past t)
  5432. args
  5433. s e rtn d emptyp)
  5434. (setq org-agenda-redo-command
  5435. (list 'progn
  5436. (list 'switch-to-buffer-other-window (current-buffer))
  5437. (list 'org-timeline (list 'quote include-all) t)))
  5438. (if (not dopast)
  5439. ;; Remove past dates from the list of dates.
  5440. (setq day-numbers (delq nil (mapcar (lambda(x)
  5441. (if (>= x today) x nil))
  5442. day-numbers))))
  5443. (switch-to-buffer-other-window
  5444. (get-buffer-create org-agenda-buffer-name))
  5445. (setq buffer-read-only nil)
  5446. (erase-buffer)
  5447. (org-agenda-mode) (setq buffer-read-only nil)
  5448. (set (make-local-variable 'org-agenda-type) 'timeline)
  5449. (if doclosed (push :closed args))
  5450. (push :timestamp args)
  5451. (if dotodo (push :todo args))
  5452. (while (setq d (pop day-numbers))
  5453. (if (and (listp d) (eq (car d) :omitted))
  5454. (progn
  5455. (setq s (point))
  5456. (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
  5457. (put-text-property s (1- (point)) 'face 'org-level-3))
  5458. (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
  5459. (if (and (>= d today)
  5460. dopast
  5461. past)
  5462. (progn
  5463. (setq past nil)
  5464. (insert (make-string 79 ?-) "\n")))
  5465. (setq date (calendar-gregorian-from-absolute d))
  5466. (setq s (point))
  5467. (setq rtn (and (not emptyp)
  5468. (apply 'org-agenda-get-day-entries
  5469. entry date args)))
  5470. (if (or rtn (equal d today) org-timeline-show-empty-dates)
  5471. (progn
  5472. (insert (calendar-day-name date) " "
  5473. (number-to-string (extract-calendar-day date)) " "
  5474. (calendar-month-name (extract-calendar-month date)) " "
  5475. (number-to-string (extract-calendar-year date)) "\n")
  5476. (put-text-property s (1- (point)) 'face
  5477. 'org-level-3)
  5478. (if (equal d today)
  5479. (put-text-property s (1- (point)) 'org-today t))
  5480. (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
  5481. (put-text-property s (1- (point)) 'day d)))))
  5482. (goto-char (point-min))
  5483. (setq buffer-read-only t)
  5484. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  5485. (point-min)))
  5486. (when (not org-select-timeline-window)
  5487. (select-window win)
  5488. (goto-char pos1))))
  5489. ;;;###autoload
  5490. (defun org-agenda-list (&optional include-all start-day ndays keep-modes)
  5491. "Produce a weekly view from all files in variable `org-agenda-files'.
  5492. The view will be for the current week, but from the overview buffer you
  5493. will be able to go to other weeks.
  5494. With one \\[universal-argument] prefix argument INCLUDE-ALL, all unfinished TODO items will
  5495. also be shown, under the current date.
  5496. With two \\[universal-argument] prefix argument INCLUDE-ALL, all TODO entries marked DONE
  5497. on the days are also shown. See the variable `org-log-done' for how
  5498. to turn on logging.
  5499. START-DAY defaults to TODAY, or to the most recent match for the weekday
  5500. given in `org-agenda-start-on-weekday'.
  5501. NDAYS defaults to `org-agenda-ndays'."
  5502. (interactive "P")
  5503. (org-agenda-maybe-reset-markers 'force)
  5504. (org-compile-prefix-format org-agenda-prefix-format)
  5505. (require 'calendar)
  5506. (let* ((org-agenda-start-on-weekday
  5507. (if (or (equal ndays 1)
  5508. (and (null ndays) (equal 1 org-agenda-ndays)))
  5509. nil org-agenda-start-on-weekday))
  5510. (org-agenda-keep-modes keep-modes)
  5511. (thefiles (org-agenda-files))
  5512. (files thefiles)
  5513. (win (selected-window))
  5514. (today (time-to-days (current-time)))
  5515. (sd (or start-day today))
  5516. (start (if (or (null org-agenda-start-on-weekday)
  5517. (< org-agenda-ndays 7))
  5518. sd
  5519. (let* ((nt (calendar-day-of-week
  5520. (calendar-gregorian-from-absolute sd)))
  5521. (n1 org-agenda-start-on-weekday)
  5522. (d (- nt n1)))
  5523. (- sd (+ (if (< d 0) 7 0) d)))))
  5524. (day-numbers (list start))
  5525. (inhibit-redisplay t)
  5526. s e rtn rtnall file date d start-pos end-pos todayp nd)
  5527. (setq org-agenda-redo-command
  5528. (list 'org-agenda-list (list 'quote include-all) start-day ndays t))
  5529. ;; Make the list of days
  5530. (setq ndays (or ndays org-agenda-ndays)
  5531. nd ndays)
  5532. (while (> ndays 1)
  5533. (push (1+ (car day-numbers)) day-numbers)
  5534. (setq ndays (1- ndays)))
  5535. (setq day-numbers (nreverse day-numbers))
  5536. (if (not (equal (current-buffer) (get-buffer org-agenda-buffer-name)))
  5537. (progn
  5538. (delete-other-windows)
  5539. (switch-to-buffer-other-window
  5540. (get-buffer-create org-agenda-buffer-name))))
  5541. (setq buffer-read-only nil)
  5542. (erase-buffer)
  5543. (org-agenda-mode) (setq buffer-read-only nil)
  5544. (set (make-local-variable 'org-agenda-type) 'agenda)
  5545. (set (make-local-variable 'starting-day) (car day-numbers))
  5546. (set (make-local-variable 'include-all-loc) include-all)
  5547. (when (and (or include-all org-agenda-include-all-todo)
  5548. (member today day-numbers))
  5549. (setq files thefiles
  5550. rtnall nil)
  5551. (while (setq file (pop files))
  5552. (catch 'nextfile
  5553. (org-check-agenda-file file)
  5554. (setq date (calendar-gregorian-from-absolute today)
  5555. rtn (org-agenda-get-day-entries
  5556. file date :todo))
  5557. (setq rtnall (append rtnall rtn))))
  5558. (when rtnall
  5559. (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
  5560. (add-text-properties (point-min) (1- (point))
  5561. (list 'face 'org-level-3))
  5562. (insert (org-finalize-agenda-entries rtnall) "\n")))
  5563. (while (setq d (pop day-numbers))
  5564. (setq date (calendar-gregorian-from-absolute d)
  5565. s (point))
  5566. (if (or (setq todayp (= d today))
  5567. (and (not start-pos) (= d sd)))
  5568. (setq start-pos (point))
  5569. (if (and start-pos (not end-pos))
  5570. (setq end-pos (point))))
  5571. (setq files thefiles
  5572. rtnall nil)
  5573. (while (setq file (pop files))
  5574. (catch 'nextfile
  5575. (org-check-agenda-file file)
  5576. (if org-agenda-show-log
  5577. (setq rtn (org-agenda-get-day-entries
  5578. file date
  5579. :deadline :scheduled :timestamp :closed))
  5580. (setq rtn (org-agenda-get-day-entries
  5581. file date
  5582. :deadline :scheduled :timestamp)))
  5583. (setq rtnall (append rtnall rtn))))
  5584. (if org-agenda-include-diary
  5585. (progn
  5586. (require 'diary-lib)
  5587. (setq rtn (org-get-entries-from-diary date))
  5588. (setq rtnall (append rtnall rtn))))
  5589. (if (or rtnall org-agenda-show-all-dates)
  5590. (progn
  5591. (insert (format "%-9s %2d %s %4d\n"
  5592. (calendar-day-name date)
  5593. (extract-calendar-day date)
  5594. (calendar-month-name (extract-calendar-month date))
  5595. (extract-calendar-year date)))
  5596. (put-text-property s (1- (point)) 'face
  5597. 'org-level-3)
  5598. (if rtnall (insert
  5599. (org-finalize-agenda-entries
  5600. (org-agenda-add-time-grid-maybe
  5601. rtnall nd todayp))
  5602. "\n"))
  5603. (put-text-property s (1- (point)) 'day d))))
  5604. (goto-char (point-min))
  5605. (setq buffer-read-only t)
  5606. (org-fit-agenda-window)
  5607. (unless (and (pos-visible-in-window-p (point-min))
  5608. (pos-visible-in-window-p (point-max)))
  5609. (goto-char (1- (point-max)))
  5610. (recenter -1)
  5611. (if (not (pos-visible-in-window-p (or start-pos 1)))
  5612. (progn
  5613. (goto-char (or start-pos 1))
  5614. (recenter 1))))
  5615. (goto-char (or start-pos 1))
  5616. (if (not org-select-agenda-window) (select-window win))
  5617. (message "")))
  5618. (defvar org-select-this-todo-keyword nil)
  5619. ;;;###autoload
  5620. (defun org-todo-list (arg &optional keep-modes)
  5621. "Show all TODO entries from all agenda file in a single list.
  5622. The prefix arg can be used to select a specific TODO keyword and limit
  5623. the list to these. When using \\[universal-argument], you will be prompted
  5624. for a keyword. A numeric prefix directly selects the Nth keyword in
  5625. `org-todo-keywords'."
  5626. (interactive "P")
  5627. (org-agenda-maybe-reset-markers 'force)
  5628. (org-compile-prefix-format org-agenda-prefix-format)
  5629. (let* ((org-agenda-keep-modes keep-modes)
  5630. (today (time-to-days (current-time)))
  5631. (date (calendar-gregorian-from-absolute today))
  5632. (win (selected-window))
  5633. (kwds org-todo-keywords)
  5634. (completion-ignore-case t)
  5635. (org-select-this-todo-keyword
  5636. (if (stringp arg) arg
  5637. (and arg (integerp arg) (> arg 0)
  5638. (nth (1- arg) org-todo-keywords))))
  5639. rtn rtnall files file pos)
  5640. (when (equal arg '(4))
  5641. (setq org-select-this-todo-keyword
  5642. (completing-read "Keyword: " (mapcar 'list org-todo-keywords)
  5643. nil t)))
  5644. (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
  5645. (if (not (equal (current-buffer) (get-buffer org-agenda-buffer-name)))
  5646. (progn
  5647. (delete-other-windows)
  5648. (switch-to-buffer-other-window
  5649. (get-buffer-create org-agenda-buffer-name))))
  5650. (setq buffer-read-only nil)
  5651. (erase-buffer)
  5652. (org-agenda-mode) (setq buffer-read-only nil)
  5653. (set (make-local-variable 'org-agenda-type) 'todo)
  5654. (set (make-local-variable 'last-arg) arg)
  5655. (set (make-local-variable 'org-todo-keywords) kwds)
  5656. (set (make-local-variable 'org-agenda-redo-command)
  5657. '(org-todo-list (or current-prefix-arg last-arg) t))
  5658. (setq files (org-agenda-files)
  5659. rtnall nil)
  5660. (while (setq file (pop files))
  5661. (catch 'nextfile
  5662. (org-check-agenda-file file)
  5663. (setq rtn (org-agenda-get-day-entries file date :todo))
  5664. (setq rtnall (append rtnall rtn))))
  5665. (insert "Global list of TODO items of type: ")
  5666. (add-text-properties (point-min) (1- (point))
  5667. (list 'face 'org-level-3))
  5668. (setq pos (point))
  5669. (insert (or org-select-this-todo-keyword "ALL") "\n")
  5670. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  5671. (setq pos (point))
  5672. (insert
  5673. "Available with `N r': (0)ALL "
  5674. (let ((n 0))
  5675. (mapconcat (lambda (x)
  5676. (format "(%d)%s" (setq n (1+ n)) x))
  5677. org-todo-keywords " "))
  5678. "\n")
  5679. (add-text-properties pos (1- (point)) (list 'face 'org-level-3))
  5680. (when rtnall
  5681. (insert (org-finalize-agenda-entries rtnall) "\n"))
  5682. (goto-char (point-min))
  5683. (setq buffer-read-only t)
  5684. (org-fit-agenda-window)
  5685. (if (not org-select-agenda-window) (select-window win))))
  5686. (defun org-check-agenda-file (file)
  5687. "Make sure FILE exists. If not, ask user what to do."
  5688. (when (not (file-exists-p file))
  5689. (message "non-existent file %s. [R]emove from list or [A]bort?"
  5690. (abbreviate-file-name file))
  5691. (let ((r (downcase (read-char-exclusive))))
  5692. (cond
  5693. ((equal r ?r)
  5694. (org-remove-file file)
  5695. (throw 'nextfile t))
  5696. (t (error "Abort"))))))
  5697. (defun org-agenda-check-type (error &rest types)
  5698. "Check if agenda buffer is of allowed type.
  5699. If ERROR is non-nil, throw an error, otherwise just return nil."
  5700. (if (memq org-agenda-type types)
  5701. t
  5702. (if error
  5703. (error "Not allowed in %s-type agenda buffers" org-agenda-type)
  5704. nil)))
  5705. (defun org-agenda-quit ()
  5706. "Exit agenda by removing the window or the buffer."
  5707. (interactive)
  5708. (let ((buf (current-buffer)))
  5709. (if (not (one-window-p)) (delete-window))
  5710. (kill-buffer buf)
  5711. (org-agenda-maybe-reset-markers 'force)))
  5712. (defun org-agenda-exit ()
  5713. "Exit agenda by removing the window or the buffer.
  5714. Also kill all Org-mode buffers which have been loaded by `org-agenda'.
  5715. Org-mode buffers visited directly by the user will not be touched."
  5716. (interactive)
  5717. (org-release-buffers org-agenda-new-buffers)
  5718. (setq org-agenda-new-buffers nil)
  5719. (org-agenda-quit))
  5720. (defun org-agenda-redo ()
  5721. "Rebuild Agenda.
  5722. When this is the global TODO list, a prefix argument will be interpreted."
  5723. (interactive)
  5724. (message "Rebuilding agenda buffer...")
  5725. (eval org-agenda-redo-command)
  5726. (message "Rebuilding agenda buffer...done"))
  5727. (defun org-agenda-goto-today ()
  5728. "Go to today."
  5729. (interactive)
  5730. (org-agenda-check-type t 'timeline 'agenda)
  5731. (if (boundp 'starting-day)
  5732. (let ((cmd (car org-agenda-redo-command))
  5733. (iall (nth 1 org-agenda-redo-command))
  5734. (nday (nth 3 org-agenda-redo-command))
  5735. (keep (nth 4 org-agenda-redo-command)))
  5736. (eval (list cmd iall nil nday keep)))
  5737. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  5738. (point-min)))))
  5739. (defun org-agenda-later (arg)
  5740. "Go forward in time by `org-agenda-ndays' days.
  5741. With prefix ARG, go forward that many times `org-agenda-ndays'."
  5742. (interactive "p")
  5743. (org-agenda-check-type t 'agenda)
  5744. (org-agenda-list (if (boundp 'include-all-loc) include-all-loc nil)
  5745. (+ starting-day (* arg org-agenda-ndays)) nil t))
  5746. (defun org-agenda-earlier (arg)
  5747. "Go back in time by `org-agenda-ndays' days.
  5748. With prefix ARG, go back that many times `org-agenda-ndays'."
  5749. (interactive "p")
  5750. (org-agenda-check-type t 'agenda)
  5751. (org-agenda-list (if (boundp 'include-all-loc) include-all-loc nil)
  5752. (- starting-day (* arg org-agenda-ndays)) nil t))
  5753. (defun org-agenda-week-view ()
  5754. "Switch to weekly view for agenda."
  5755. (interactive)
  5756. (org-agenda-check-type t 'agenda)
  5757. (setq org-agenda-ndays 7)
  5758. (org-agenda-list include-all-loc
  5759. (or (get-text-property (point) 'day)
  5760. starting-day)
  5761. nil t)
  5762. (org-agenda-set-mode-name)
  5763. (message "Switched to week view"))
  5764. (defun org-agenda-day-view ()
  5765. "Switch to daily view for agenda."
  5766. (interactive)
  5767. (org-agenda-check-type t 'agenda)
  5768. (setq org-agenda-ndays 1)
  5769. (org-agenda-list include-all-loc
  5770. (or (get-text-property (point) 'day)
  5771. starting-day)
  5772. nil t)
  5773. (org-agenda-set-mode-name)
  5774. (message "Switched to day view"))
  5775. (defun org-agenda-next-date-line (&optional arg)
  5776. "Jump to the next line indicating a date in agenda buffer."
  5777. (interactive "p")
  5778. (org-agenda-check-type t 'agenda 'timeline)
  5779. (beginning-of-line 1)
  5780. (if (looking-at "^\\S-") (forward-char 1))
  5781. (if (not (re-search-forward "^\\S-" nil t arg))
  5782. (progn
  5783. (backward-char 1)
  5784. (error "No next date after this line in this buffer")))
  5785. (goto-char (match-beginning 0)))
  5786. (defun org-agenda-previous-date-line (&optional arg)
  5787. "Jump to the previous line indicating a date in agenda buffer."
  5788. (interactive "p")
  5789. (org-agenda-check-type t 'agenda 'timeline)
  5790. (beginning-of-line 1)
  5791. (if (not (re-search-backward "^\\S-" nil t arg))
  5792. (error "No previous date before this line in this buffer")))
  5793. ;; Initialize the highlight
  5794. (defvar org-hl (org-make-overlay 1 1))
  5795. (org-overlay-put org-hl 'face 'highlight)
  5796. (defun org-highlight (begin end &optional buffer)
  5797. "Highlight a region with overlay."
  5798. (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
  5799. org-hl begin end (or buffer (current-buffer))))
  5800. (defun org-unhighlight ()
  5801. "Detach overlay INDEX."
  5802. (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
  5803. (defun org-agenda-follow-mode ()
  5804. "Toggle follow mode in an agenda buffer."
  5805. (interactive)
  5806. (setq org-agenda-follow-mode (not org-agenda-follow-mode))
  5807. (org-agenda-set-mode-name)
  5808. (message "Follow mode is %s"
  5809. (if org-agenda-follow-mode "on" "off")))
  5810. (defun org-agenda-log-mode ()
  5811. "Toggle log mode in an agenda buffer."
  5812. (interactive)
  5813. (org-agenda-check-type t 'agenda 'timeline)
  5814. (setq org-agenda-show-log (not org-agenda-show-log))
  5815. (org-agenda-set-mode-name)
  5816. (org-agenda-redo)
  5817. (message "Log mode is %s"
  5818. (if org-agenda-show-log "on" "off")))
  5819. (defun org-agenda-toggle-diary ()
  5820. "Toggle diary inclusion in an agenda buffer."
  5821. (interactive)
  5822. (org-agenda-check-type t 'agenda)
  5823. (setq org-agenda-include-diary (not org-agenda-include-diary))
  5824. (org-agenda-redo)
  5825. (org-agenda-set-mode-name)
  5826. (message "Diary inclusion turned %s"
  5827. (if org-agenda-include-diary "on" "off")))
  5828. (defun org-agenda-toggle-time-grid ()
  5829. "Toggle time grid in an agenda buffer."
  5830. (interactive)
  5831. (org-agenda-check-type t 'agenda)
  5832. (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
  5833. (org-agenda-redo)
  5834. (org-agenda-set-mode-name)
  5835. (message "Time-grid turned %s"
  5836. (if org-agenda-use-time-grid "on" "off")))
  5837. (defun org-agenda-set-mode-name ()
  5838. "Set the mode name to indicate all the small mode settings."
  5839. (setq mode-name
  5840. (concat "Org-Agenda"
  5841. (if (equal org-agenda-ndays 1) " Day" "")
  5842. (if (equal org-agenda-ndays 7) " Week" "")
  5843. (if org-agenda-follow-mode " Follow" "")
  5844. (if org-agenda-include-diary " Diary" "")
  5845. (if org-agenda-use-time-grid " Grid" "")
  5846. (if org-agenda-show-log " Log" "")))
  5847. (force-mode-line-update))
  5848. (defun org-agenda-post-command-hook ()
  5849. (and (eolp) (not (bolp)) (backward-char 1))
  5850. (if (and org-agenda-follow-mode
  5851. (get-text-property (point) 'org-marker))
  5852. (org-agenda-show)))
  5853. (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
  5854. (defun org-get-entries-from-diary (date)
  5855. "Get the (Emacs Calendar) diary entries for DATE."
  5856. (let* ((fancy-diary-buffer "*temporary-fancy-diary-buffer*")
  5857. (diary-display-hook '(fancy-diary-display))
  5858. (list-diary-entries-hook
  5859. (cons 'org-diary-default-entry list-diary-entries-hook))
  5860. (diary-file-name-prefix-function nil) ; turn this feature off
  5861. (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
  5862. entries
  5863. (org-disable-agenda-to-diary t))
  5864. (save-excursion
  5865. (save-window-excursion
  5866. (list-diary-entries date 1))) ;; Keep this name for now, compatibility
  5867. (if (not (get-buffer fancy-diary-buffer))
  5868. (setq entries nil)
  5869. (with-current-buffer fancy-diary-buffer
  5870. (setq buffer-read-only nil)
  5871. (if (= (point-max) 1)
  5872. ;; No entries
  5873. (setq entries nil)
  5874. ;; Omit the date and other unnecessary stuff
  5875. (org-agenda-cleanup-fancy-diary)
  5876. ;; Add prefix to each line and extend the text properties
  5877. (if (= (point-max) 1)
  5878. (setq entries nil)
  5879. (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
  5880. (set-buffer-modified-p nil)
  5881. (kill-buffer fancy-diary-buffer)))
  5882. (when entries
  5883. (setq entries (org-split-string entries "\n"))
  5884. (setq entries
  5885. (mapcar
  5886. (lambda (x)
  5887. (setq x (org-format-agenda-item "" x "Diary" nil 'time))
  5888. ;; Extend the text properties to the beginning of the line
  5889. (org-add-props x (text-properties-at (1- (length x)) x)))
  5890. entries)))))
  5891. (defun org-agenda-cleanup-fancy-diary ()
  5892. "Remove unwanted stuff in buffer created by `fancy-diary-display'.
  5893. This gets rid of the date, the underline under the date, and
  5894. the dummy entry installed by `org-mode' to ensure non-empty diary for each
  5895. date. It also removes lines that contain only whitespace."
  5896. (goto-char (point-min))
  5897. (if (looking-at ".*?:[ \t]*")
  5898. (progn
  5899. (replace-match "")
  5900. (re-search-forward "\n=+$" nil t)
  5901. (replace-match "")
  5902. (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
  5903. (re-search-forward "\n=+$" nil t)
  5904. (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
  5905. (goto-char (point-min))
  5906. (while (re-search-forward "^ +\n" nil t)
  5907. (replace-match ""))
  5908. (goto-char (point-min))
  5909. (if (re-search-forward "^Org-mode dummy\n?" nil t)
  5910. (replace-match "")))
  5911. ;; Make sure entries from the diary have the right text properties.
  5912. (eval-after-load "diary-lib"
  5913. '(if (boundp 'diary-modify-entry-list-string-function)
  5914. ;; We can rely on the hook, nothing to do
  5915. nil
  5916. ;; Hook not avaiable, must use advice to make this work
  5917. (defadvice add-to-diary-list (before org-mark-diary-entry activate)
  5918. "Make the position visible."
  5919. (if (and org-disable-agenda-to-diary ;; called from org-agenda
  5920. (stringp string)
  5921. buffer-file-name)
  5922. (setq string (org-modify-diary-entry-string string))))))
  5923. (defun org-modify-diary-entry-string (string)
  5924. "Add text properties to string, allowing org-mode to act on it."
  5925. (org-add-props string nil
  5926. 'mouse-face 'highlight
  5927. 'keymap org-agenda-keymap
  5928. 'help-echo (format "mouse-2 or RET jump to diary file %s"
  5929. (abbreviate-file-name buffer-file-name))
  5930. 'org-agenda-diary-link t
  5931. 'org-marker (org-agenda-new-marker (point-at-bol))))
  5932. (defun org-diary-default-entry ()
  5933. "Add a dummy entry to the diary.
  5934. Needed to avoid empty dates which mess up holiday display."
  5935. ;; Catch the error if dealing with the new add-to-diary-alist
  5936. (when org-disable-agenda-to-diary
  5937. (condition-case nil
  5938. (add-to-diary-list original-date "Org-mode dummy" "")
  5939. (error
  5940. (add-to-diary-list original-date "Org-mode dummy" "" nil)))))
  5941. (defun org-cycle-agenda-files ()
  5942. "Cycle through the files in `org-agenda-files'.
  5943. If the current buffer visits an agenda file, find the next one in the list.
  5944. If the current buffer does not, find the first agenda file."
  5945. (interactive)
  5946. (let* ((fs (org-agenda-files t))
  5947. (files (append fs (list (car fs))))
  5948. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  5949. file)
  5950. (unless files (error "No agenda files"))
  5951. (catch 'exit
  5952. (while (setq file (pop files))
  5953. (if (equal (file-truename file) tcf)
  5954. (when (car files)
  5955. (find-file (car files))
  5956. (throw 'exit t))))
  5957. (find-file (car fs)))))
  5958. (defun org-agenda-file-to-end ()
  5959. "Move/add the current file to the end of the agenda file list.
  5960. If the file is not present in the list, it is appended to the list. If it is
  5961. present, it is moved there."
  5962. (interactive)
  5963. (org-agenda-file-to-front 'to-end))
  5964. (defun org-agenda-file-to-front (&optional to-end)
  5965. "Move/add the current file to the top of the agenda file list.
  5966. If the file is not present in the list, it is added to the front. If it is
  5967. present, it is moved there. With optional argument TO-END, add/move to the
  5968. end of the list."
  5969. (interactive "P")
  5970. (let ((file-alist (mapcar (lambda (x)
  5971. (cons (file-truename x) x))
  5972. (org-agenda-files t)))
  5973. (ctf (file-truename buffer-file-name))
  5974. x had)
  5975. (setq x (assoc ctf file-alist) had x)
  5976. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  5977. (if to-end
  5978. (setq file-alist (append (delq x file-alist) (list x)))
  5979. (setq file-alist (cons x (delq x file-alist))))
  5980. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  5981. (org-install-agenda-files-menu)
  5982. (message "File %s to %s of agenda file list"
  5983. (if had "moved" "added") (if to-end "end" "front"))))
  5984. (defun org-remove-file (&optional file)
  5985. "Remove current file from the list of files in variable `org-agenda-files'.
  5986. These are the files which are being checked for agenda entries.
  5987. Optional argument FILE means, use this file instead of the current."
  5988. (interactive)
  5989. (let* ((file (or file buffer-file-name))
  5990. (true-file (file-truename file))
  5991. (afile (abbreviate-file-name file))
  5992. (files (delq nil (mapcar
  5993. (lambda (x)
  5994. (if (equal true-file
  5995. (file-truename x))
  5996. nil x))
  5997. (org-agenda-files t)))))
  5998. (if (not (= (length files) (length (org-agenda-files t))))
  5999. (progn
  6000. (org-store-new-agenda-file-list files)
  6001. (org-install-agenda-files-menu)
  6002. (message "Removed file: %s" afile))
  6003. (message "File was not in list: %s" afile))))
  6004. (defun org-file-menu-entry (file)
  6005. (vector file (list 'find-file file) t))
  6006. (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty)
  6007. "Return a list of all relevant day numbers from BEG to END buffer positions.
  6008. If NO-RANGES is non-nil, include only the start and end dates of a range,
  6009. not every single day in the range. If FORCE-TODAY is non-nil, make
  6010. sure that TODAY is included in the list. If INACTIVE is non-nil, also
  6011. inactive time stamps (those in square brackets) are included.
  6012. When EMPTY is non-nil, also include days without any entries."
  6013. (let ((re (if inactive org-ts-regexp-both org-ts-regexp))
  6014. dates dates1 date day day1 day2 ts1 ts2)
  6015. (if force-today
  6016. (setq dates (list (time-to-days (current-time)))))
  6017. (save-excursion
  6018. (goto-char beg)
  6019. (while (re-search-forward re end t)
  6020. (setq day (time-to-days (org-time-string-to-time
  6021. (substring (match-string 1) 0 10))))
  6022. (or (memq day dates) (push day dates)))
  6023. (unless no-ranges
  6024. (goto-char beg)
  6025. (while (re-search-forward org-tr-regexp end t)
  6026. (setq ts1 (substring (match-string 1) 0 10)
  6027. ts2 (substring (match-string 2) 0 10)
  6028. day1 (time-to-days (org-time-string-to-time ts1))
  6029. day2 (time-to-days (org-time-string-to-time ts2)))
  6030. (while (< (setq day1 (1+ day1)) day2)
  6031. (or (memq day1 dates) (push day1 dates)))))
  6032. (setq dates (sort dates '<))
  6033. (when empty
  6034. (while (setq day (pop dates))
  6035. (setq day2 (car dates))
  6036. (push day dates1)
  6037. (when (and day2 empty)
  6038. (if (or (eq empty t)
  6039. (and (numberp empty) (<= (- day2 day) empty)))
  6040. (while (< (setq day (1+ day)) day2)
  6041. (push (list day) dates1))
  6042. (push (cons :omitted (- day2 day)) dates1))))
  6043. (setq dates (nreverse dates1)))
  6044. dates)))
  6045. ;;;###autoload
  6046. (defun org-diary (&rest args)
  6047. "Return diary information from org-files.
  6048. This function can be used in a \"sexp\" diary entry in the Emacs calendar.
  6049. It accesses org files and extracts information from those files to be
  6050. listed in the diary. The function accepts arguments specifying what
  6051. items should be listed. The following arguments are allowed:
  6052. :timestamp List the headlines of items containing a date stamp or
  6053. date range matching the selected date. Deadlines will
  6054. also be listed, on the expiration day.
  6055. :deadline List any deadlines past due, or due within
  6056. `org-deadline-warning-days'. The listing occurs only
  6057. in the diary for *today*, not at any other date. If
  6058. an entry is marked DONE, it is no longer listed.
  6059. :scheduled List all items which are scheduled for the given date.
  6060. The diary for *today* also contains items which were
  6061. scheduled earlier and are not yet marked DONE.
  6062. :todo List all TODO items from the org-file. This may be a
  6063. long list - so this is not turned on by default.
  6064. Like deadlines, these entries only show up in the
  6065. diary for *today*, not at any other date.
  6066. The call in the diary file should look like this:
  6067. &%%(org-diary) ~/path/to/some/orgfile.org
  6068. Use a separate line for each org file to check. Or, if you omit the file name,
  6069. all files listed in `org-agenda-files' will be checked automatically:
  6070. &%%(org-diary)
  6071. If you don't give any arguments (as in the example above), the default
  6072. arguments (:deadline :scheduled :timestamp) are used. So the example above may
  6073. also be written as
  6074. &%%(org-diary :deadline :timestamp :scheduled)
  6075. The function expects the lisp variables `entry' and `date' to be provided
  6076. by the caller, because this is how the calendar works. Don't use this
  6077. function from a program - use `org-agenda-get-day-entries' instead."
  6078. (org-agenda-maybe-reset-markers)
  6079. (org-compile-prefix-format org-agenda-prefix-format)
  6080. (setq args (or args '(:deadline :scheduled :timestamp)))
  6081. (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
  6082. (list entry)
  6083. (org-agenda-files t)))
  6084. file rtn results)
  6085. ;; If this is called during org-agenda, don't return any entries to
  6086. ;; the calendar. Org Agenda will list these entries itself.
  6087. (if org-disable-agenda-to-diary (setq files nil))
  6088. (while (setq file (pop files))
  6089. (setq rtn (apply 'org-agenda-get-day-entries file date args))
  6090. (setq results (append results rtn)))
  6091. (if results
  6092. (concat (org-finalize-agenda-entries results) "\n"))))
  6093. (defvar org-category-table nil)
  6094. (defun org-get-category-table ()
  6095. "Get the table of categories and positions in current buffer."
  6096. (let (tbl)
  6097. (save-excursion
  6098. (goto-char (point-min))
  6099. (while (re-search-forward "\\(^\\|\r\\)#\\+CATEGORY:[ \t]*\\(.*\\)" nil t)
  6100. (push (cons (point) (org-trim (match-string 2))) tbl)))
  6101. tbl))
  6102. (defun org-get-category (&optional pos)
  6103. "Get the category applying to position POS."
  6104. (if (not org-category-table)
  6105. (cond
  6106. ((null org-category)
  6107. (setq org-category
  6108. (if buffer-file-name
  6109. (file-name-sans-extension
  6110. (file-name-nondirectory buffer-file-name))
  6111. "???")))
  6112. ((symbolp org-category) (symbol-name org-category))
  6113. (t org-category))
  6114. (let ((tbl org-category-table)
  6115. (pos (or pos (point))))
  6116. (while (and tbl (> (caar tbl) pos))
  6117. (pop tbl))
  6118. (or (cdar tbl) (cdr (nth (1- (length org-category-table))
  6119. org-category-table))))))
  6120. (defun org-agenda-get-day-entries (file date &rest args)
  6121. "Does the work for `org-diary' and `org-agenda'.
  6122. FILE is the path to a file to be checked for entries. DATE is date like
  6123. the one returned by `calendar-current-date'. ARGS are symbols indicating
  6124. which kind of entries should be extracted. For details about these, see
  6125. the documentation of `org-diary'."
  6126. (setq args (or args '(:deadline :scheduled :timestamp)))
  6127. (let* ((org-startup-with-deadline-check nil)
  6128. (org-startup-folded nil)
  6129. (org-startup-align-all-tables nil)
  6130. (buffer (if (file-exists-p file)
  6131. (org-get-agenda-file-buffer file)
  6132. (error "No such file %s" file)))
  6133. arg results rtn)
  6134. (if (not buffer)
  6135. ;; If file does not exist, make sure an error message ends up in diary
  6136. (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  6137. (with-current-buffer buffer
  6138. (unless (eq major-mode 'org-mode)
  6139. (error "Agenda file %s is not in `org-mode'" file))
  6140. (setq org-category-table (org-get-category-table))
  6141. (let ((case-fold-search nil))
  6142. (save-excursion
  6143. (save-restriction
  6144. (if org-respect-restriction
  6145. (if (org-region-active-p)
  6146. ;; Respect a region to restrict search
  6147. (narrow-to-region (region-beginning) (region-end)))
  6148. ;; If we work for the calendar or many files,
  6149. ;; get rid of any restriction
  6150. (widen))
  6151. ;; The way we repeatedly append to `results' makes it O(n^2) :-(
  6152. (while (setq arg (pop args))
  6153. (cond
  6154. ((and (eq arg :todo)
  6155. (equal date (calendar-current-date)))
  6156. (setq rtn (org-agenda-get-todos))
  6157. (setq results (append results rtn)))
  6158. ((eq arg :timestamp)
  6159. (setq rtn (org-agenda-get-blocks))
  6160. (setq results (append results rtn))
  6161. (setq rtn (org-agenda-get-timestamps))
  6162. (setq results (append results rtn)))
  6163. ((eq arg :scheduled)
  6164. (setq rtn (org-agenda-get-scheduled))
  6165. (setq results (append results rtn)))
  6166. ((eq arg :closed)
  6167. (setq rtn (org-agenda-get-closed))
  6168. (setq results (append results rtn)))
  6169. ((and (eq arg :deadline)
  6170. (equal date (calendar-current-date)))
  6171. (setq rtn (org-agenda-get-deadlines))
  6172. (setq results (append results rtn))))))))
  6173. results))))
  6174. (defun org-entry-is-done-p ()
  6175. "Is the current entry marked DONE?"
  6176. (save-excursion
  6177. (and (re-search-backward "[\r\n]\\*" nil t)
  6178. (looking-at org-nl-done-regexp))))
  6179. (defun org-at-date-range-p ()
  6180. "Is the cursor inside a date range?"
  6181. (interactive)
  6182. (save-excursion
  6183. (catch 'exit
  6184. (let ((pos (point)))
  6185. (skip-chars-backward "^<\r\n")
  6186. (skip-chars-backward "<")
  6187. (and (looking-at org-tr-regexp)
  6188. (>= (match-end 0) pos)
  6189. (throw 'exit t))
  6190. (skip-chars-backward "^<\r\n")
  6191. (skip-chars-backward "<")
  6192. (and (looking-at org-tr-regexp)
  6193. (>= (match-end 0) pos)
  6194. (throw 'exit t)))
  6195. nil)))
  6196. (defun org-agenda-get-todos ()
  6197. "Return the TODO information for agenda display."
  6198. (let* ((props (list 'face nil
  6199. 'done-face 'org-done
  6200. 'org-not-done-regexp org-not-done-regexp
  6201. 'mouse-face 'highlight
  6202. 'keymap org-agenda-keymap
  6203. 'help-echo
  6204. (format "mouse-2 or RET jump to org file %s"
  6205. (abbreviate-file-name buffer-file-name))))
  6206. (regexp (concat "[\n\r]\\*+ *\\("
  6207. (if org-select-this-todo-keyword
  6208. (concat "\\<\\(" org-select-this-todo-keyword
  6209. "\\)\\>")
  6210. org-not-done-regexp)
  6211. "[^\n\r]*\\)"))
  6212. (sched-re (concat ".*\n?.*?" org-scheduled-time-regexp))
  6213. marker priority category tags
  6214. ee txt)
  6215. (goto-char (point-min))
  6216. (while (re-search-forward regexp nil t)
  6217. (when (not (and org-agenda-todo-ignore-scheduled
  6218. (save-match-data (looking-at sched-re))))
  6219. (goto-char (match-beginning 1))
  6220. (setq marker (org-agenda-new-marker (1+ (match-beginning 0)))
  6221. category (org-get-category)
  6222. tags (org-get-tags-at (point))
  6223. txt (org-format-agenda-item "" (match-string 1) category tags)
  6224. priority
  6225. (+ (org-get-priority txt)
  6226. (if org-todo-kwd-priority-p
  6227. (- org-todo-kwd-max-priority -2
  6228. (length
  6229. (member (match-string 2) org-todo-keywords)))
  6230. 1)))
  6231. (org-add-props txt props
  6232. 'org-marker marker 'org-hd-marker marker
  6233. 'priority priority 'category category)
  6234. (push txt ee))
  6235. (if org-agenda-todo-list-sublevels
  6236. (goto-char (match-end 1))
  6237. (org-end-of-subtree 'invisible)))
  6238. (nreverse ee)))
  6239. (defconst org-agenda-no-heading-message
  6240. "No heading for this item in buffer or region.")
  6241. (defun org-agenda-get-timestamps ()
  6242. "Return the date stamp information for agenda display."
  6243. (let* ((props (list 'face nil
  6244. 'org-not-done-regexp org-not-done-regexp
  6245. 'mouse-face 'highlight
  6246. 'keymap org-agenda-keymap
  6247. 'help-echo
  6248. (format "mouse-2 or RET jump to org file %s"
  6249. (abbreviate-file-name buffer-file-name))))
  6250. (regexp (regexp-quote
  6251. (substring
  6252. (format-time-string
  6253. (car org-time-stamp-formats)
  6254. (apply 'encode-time ; DATE bound by calendar
  6255. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  6256. 0 11)))
  6257. marker hdmarker deadlinep scheduledp donep tmp priority category
  6258. ee txt timestr tags)
  6259. (goto-char (point-min))
  6260. (while (re-search-forward regexp nil t)
  6261. (if (not (save-match-data (org-at-date-range-p)))
  6262. (progn
  6263. (setq marker (org-agenda-new-marker (match-beginning 0))
  6264. category (org-get-category (match-beginning 0))
  6265. tmp (buffer-substring (max (point-min)
  6266. (- (match-beginning 0)
  6267. org-ds-keyword-length))
  6268. (match-beginning 0))
  6269. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  6270. deadlinep (string-match org-deadline-regexp tmp)
  6271. scheduledp (string-match org-scheduled-regexp tmp)
  6272. donep (org-entry-is-done-p))
  6273. (if (string-match ">" timestr)
  6274. ;; substring should only run to end of time stamp
  6275. (setq timestr (substring timestr 0 (match-end 0))))
  6276. (save-excursion
  6277. (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t)
  6278. (progn
  6279. (goto-char (match-end 1))
  6280. (setq hdmarker (org-agenda-new-marker)
  6281. tags (org-get-tags-at))
  6282. (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
  6283. (setq txt (org-format-agenda-item
  6284. (format "%s%s"
  6285. (if deadlinep "Deadline: " "")
  6286. (if scheduledp "Scheduled: " ""))
  6287. (match-string 1) category tags timestr)))
  6288. (setq txt org-agenda-no-heading-message))
  6289. (setq priority (org-get-priority txt))
  6290. (org-add-props txt props
  6291. 'org-marker marker 'org-hd-marker hdmarker)
  6292. (if deadlinep
  6293. (org-add-props txt nil
  6294. 'face (if donep 'org-done 'org-warning)
  6295. 'undone-face 'org-warning 'done-face 'org-done
  6296. 'category category 'priority (+ 100 priority))
  6297. (if scheduledp
  6298. (org-add-props txt nil
  6299. 'face 'org-scheduled-today
  6300. 'undone-face 'org-scheduled-today 'done-face 'org-done
  6301. 'category category 'priority (+ 99 priority))
  6302. (org-add-props txt nil 'priority priority 'category category)))
  6303. (push txt ee))
  6304. (outline-next-heading))))
  6305. (nreverse ee)))
  6306. (defun org-agenda-get-closed ()
  6307. "Return the logged TODO entries for agenda display."
  6308. (let* ((props (list 'mouse-face 'highlight
  6309. 'org-not-done-regexp org-not-done-regexp
  6310. 'keymap org-agenda-keymap
  6311. 'help-echo
  6312. (format "mouse-2 or RET jump to org file %s"
  6313. (abbreviate-file-name buffer-file-name))))
  6314. (regexp (concat
  6315. "\\<\\(" org-closed-string "\\|" org-clock-string "\\) *\\["
  6316. (regexp-quote
  6317. (substring
  6318. (format-time-string
  6319. (car org-time-stamp-formats)
  6320. (apply 'encode-time ; DATE bound by calendar
  6321. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  6322. 1 11))))
  6323. marker hdmarker priority category tags closedp
  6324. ee txt timestr)
  6325. (goto-char (point-min))
  6326. (while (re-search-forward regexp nil t)
  6327. (if (not (save-match-data (org-at-date-range-p)))
  6328. (progn
  6329. (setq marker (org-agenda-new-marker (match-beginning 0))
  6330. closedp (equal (match-string 1) org-closed-string)
  6331. category (org-get-category (match-beginning 0))
  6332. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  6333. ;; donep (org-entry-is-done-p)
  6334. )
  6335. (if (string-match "\\]" timestr)
  6336. ;; substring should only run to end of time stamp
  6337. (setq timestr (substring timestr 0 (match-end 0))))
  6338. (save-excursion
  6339. (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t)
  6340. (progn
  6341. (goto-char (match-end 1))
  6342. (setq hdmarker (org-agenda-new-marker)
  6343. tags (org-get-tags-at))
  6344. (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
  6345. (setq txt (org-format-agenda-item
  6346. (if closedp "Closed: " "Clocked: ")
  6347. (match-string 1) category tags timestr)))
  6348. (setq txt org-agenda-no-heading-message))
  6349. (setq priority 100000)
  6350. (org-add-props txt props
  6351. 'org-marker marker 'org-hd-marker hdmarker 'face 'org-done
  6352. 'priority priority 'category category
  6353. 'undone-face 'org-warning 'done-face 'org-done)
  6354. (push txt ee))
  6355. (outline-next-heading))))
  6356. (nreverse ee)))
  6357. (defun org-agenda-get-deadlines ()
  6358. "Return the deadline information for agenda display."
  6359. (let* ((wdays org-deadline-warning-days)
  6360. (props (list 'mouse-face 'highlight
  6361. 'org-not-done-regexp org-not-done-regexp
  6362. 'keymap org-agenda-keymap
  6363. 'help-echo
  6364. (format "mouse-2 or RET jump to org file %s"
  6365. (abbreviate-file-name buffer-file-name))))
  6366. (regexp org-deadline-time-regexp)
  6367. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  6368. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  6369. d2 diff pos pos1 category tags
  6370. ee txt head face)
  6371. (goto-char (point-min))
  6372. (while (re-search-forward regexp nil t)
  6373. (setq pos (1- (match-beginning 1))
  6374. d2 (time-to-days
  6375. (org-time-string-to-time (match-string 1)))
  6376. diff (- d2 d1))
  6377. ;; When to show a deadline in the calendar:
  6378. ;; If the expiration is within wdays warning time.
  6379. ;; Past-due deadlines are only shown on the current date
  6380. (if (and (< diff wdays) todayp (not (= diff 0)))
  6381. (save-excursion
  6382. (setq category (org-get-category))
  6383. (if (re-search-backward "\\(^\\|\r\\)\\*+[ \t]*" nil t)
  6384. (progn
  6385. (goto-char (match-end 0))
  6386. (setq pos1 (match-end 1))
  6387. (setq tags (org-get-tags-at pos1))
  6388. (setq head (buffer-substring-no-properties
  6389. (point)
  6390. (progn (skip-chars-forward "^\r\n")
  6391. (point))))
  6392. (if (string-match org-looking-at-done-regexp head)
  6393. (setq txt nil)
  6394. (setq txt (org-format-agenda-item
  6395. (format "In %3d d.: " diff) head category tags))))
  6396. (setq txt org-agenda-no-heading-message))
  6397. (when txt
  6398. (setq face (cond ((<= diff 0) 'org-warning)
  6399. ((<= diff 5) 'org-upcoming-deadline)
  6400. (t nil)))
  6401. (org-add-props txt props
  6402. 'org-marker (org-agenda-new-marker pos)
  6403. 'org-hd-marker (org-agenda-new-marker pos1)
  6404. 'priority (+ (- 10 diff) (org-get-priority txt))
  6405. 'category category
  6406. 'face face 'undone-face face 'done-face 'org-done)
  6407. (push txt ee)))))
  6408. ee))
  6409. (defun org-agenda-get-scheduled ()
  6410. "Return the scheduled information for agenda display."
  6411. (let* ((props (list 'face 'org-scheduled-previously
  6412. 'org-not-done-regexp org-not-done-regexp
  6413. 'undone-face 'org-scheduled-previously
  6414. 'done-face 'org-done
  6415. 'mouse-face 'highlight
  6416. 'keymap org-agenda-keymap
  6417. 'help-echo
  6418. (format "mouse-2 or RET jump to org file %s"
  6419. (abbreviate-file-name buffer-file-name))))
  6420. (regexp org-scheduled-time-regexp)
  6421. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  6422. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  6423. d2 diff pos pos1 category tags
  6424. ee txt head)
  6425. (goto-char (point-min))
  6426. (while (re-search-forward regexp nil t)
  6427. (setq pos (1- (match-beginning 1))
  6428. d2 (time-to-days
  6429. (org-time-string-to-time (match-string 1)))
  6430. diff (- d2 d1))
  6431. ;; When to show a scheduled item in the calendar:
  6432. ;; If it is on or past the date.
  6433. (if (and (< diff 0) todayp)
  6434. (save-excursion
  6435. (setq category (org-get-category))
  6436. (if (re-search-backward "\\(^\\|\r\\)\\*+[ \t]*" nil t)
  6437. (progn
  6438. (goto-char (match-end 0))
  6439. (setq pos1 (match-end 1))
  6440. (setq tags (org-get-tags-at))
  6441. (setq head (buffer-substring-no-properties
  6442. (point)
  6443. (progn (skip-chars-forward "^\r\n") (point))))
  6444. (if (string-match org-looking-at-done-regexp head)
  6445. (setq txt nil)
  6446. (setq txt (org-format-agenda-item
  6447. (format "Sched.%2dx: " (- 1 diff)) head
  6448. category tags))))
  6449. (setq txt org-agenda-no-heading-message))
  6450. (when txt
  6451. (org-add-props txt props
  6452. 'org-marker (org-agenda-new-marker pos)
  6453. 'org-hd-marker (org-agenda-new-marker pos1)
  6454. 'priority (+ (- 5 diff) (org-get-priority txt))
  6455. 'category category)
  6456. (push txt ee)))))
  6457. ee))
  6458. (defun org-agenda-get-blocks ()
  6459. "Return the date-range information for agenda display."
  6460. (let* ((props (list 'face nil
  6461. 'org-not-done-regexp org-not-done-regexp
  6462. 'mouse-face 'highlight
  6463. 'keymap org-agenda-keymap
  6464. 'help-echo
  6465. (format "mouse-2 or RET jump to org file %s"
  6466. (abbreviate-file-name buffer-file-name))))
  6467. (regexp org-tr-regexp)
  6468. (d0 (calendar-absolute-from-gregorian date))
  6469. marker hdmarker ee txt d1 d2 s1 s2 timestr category tags)
  6470. (goto-char (point-min))
  6471. (while (re-search-forward regexp nil t)
  6472. (setq timestr (match-string 0)
  6473. s1 (match-string 1)
  6474. s2 (match-string 2)
  6475. d1 (time-to-days (org-time-string-to-time s1))
  6476. d2 (time-to-days (org-time-string-to-time s2)))
  6477. (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
  6478. ;; Only allow days between the limits, because the normal
  6479. ;; date stamps will catch the limits.
  6480. (save-excursion
  6481. (setq marker (org-agenda-new-marker (point)))
  6482. (setq category (org-get-category))
  6483. (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t)
  6484. (progn
  6485. (setq hdmarker (org-agenda-new-marker (match-end 1)))
  6486. (goto-char (match-end 1))
  6487. (setq tags (org-get-tags-at))
  6488. (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
  6489. (setq txt (org-format-agenda-item
  6490. (format (if (= d1 d2) "" "(%d/%d): ")
  6491. (1+ (- d0 d1)) (1+ (- d2 d1)))
  6492. (match-string 1) category tags
  6493. (if (= d0 d1) timestr))))
  6494. (setq txt org-agenda-no-heading-message))
  6495. (org-add-props txt props
  6496. 'org-marker marker 'org-hd-marker hdmarker
  6497. 'priority (org-get-priority txt) 'category category)
  6498. (push txt ee)))
  6499. (outline-next-heading))
  6500. ;; Sort the entries by expiration date.
  6501. (nreverse ee)))
  6502. (defconst org-plain-time-of-day-regexp
  6503. (concat
  6504. "\\(\\<[012]?[0-9]"
  6505. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  6506. "\\(--?"
  6507. "\\(\\<[012]?[0-9]"
  6508. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  6509. "\\)?")
  6510. "Regular expression to match a plain time or time range.
  6511. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  6512. groups carry important information:
  6513. 0 the full match
  6514. 1 the first time, range or not
  6515. 8 the second time, if it is a range.")
  6516. (defconst org-stamp-time-of-day-regexp
  6517. (concat
  6518. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +[a-zA-Z]+ +\\)"
  6519. "\\([012][0-9]:[0-5][0-9]\\)>"
  6520. "\\(--?"
  6521. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  6522. "Regular expression to match a timestamp time or time range.
  6523. After a match, the following groups carry important information:
  6524. 0 the full match
  6525. 1 date plus weekday, for backreferencing to make sure both times on same day
  6526. 2 the first time, range or not
  6527. 4 the second time, if it is a range.")
  6528. (defvar org-prefix-has-time nil
  6529. "A flag, set by `org-compile-prefix-format'.
  6530. The flag is set if the currently compiled format contains a `%t'.")
  6531. (defvar org-prefix-has-tag nil
  6532. "A flag, set by `org-compile-prefix-format'.
  6533. The flag is set if the currently compiled format contains a `%T'.")
  6534. (defun org-format-agenda-item (extra txt &optional category tags dotime noprefix)
  6535. "Format TXT to be inserted into the agenda buffer.
  6536. In particular, it adds the prefix and corresponding text properties. EXTRA
  6537. must be a string and replaces the `%s' specifier in the prefix format.
  6538. CATEGORY (string, symbol or nil) may be used to overrule the default
  6539. category taken from local variable or file name. It will replace the `%c'
  6540. specifier in the format. DOTIME, when non-nil, indicates that a
  6541. time-of-day should be extracted from TXT for sorting of this entry, and for
  6542. the `%t' specifier in the format. When DOTIME is a string, this string is
  6543. searched for a time before TXT is. NOPREFIX is a flag and indicates that
  6544. only the correctly processes TXT should be returned - this is used by
  6545. `org-agenda-change-all-lines'. TAG can be the tag of the headline."
  6546. (save-match-data
  6547. ;; Diary entries sometimes have extra whitespace at the beginning
  6548. (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
  6549. (let* ((category (or category
  6550. org-category
  6551. (if buffer-file-name
  6552. (file-name-sans-extension
  6553. (file-name-nondirectory buffer-file-name))
  6554. "")))
  6555. (tag (if tags (nth (1- (length tags)) tags) ""))
  6556. time ;; needed for the eval of the prefix format
  6557. (ts (if dotime (concat (if (stringp dotime) dotime "") txt)))
  6558. (time-of-day (and dotime (org-get-time-of-day ts)))
  6559. stamp plain s0 s1 s2 rtn)
  6560. (when (and dotime time-of-day org-prefix-has-time)
  6561. ;; Extract starting and ending time and move them to prefix
  6562. (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
  6563. (setq plain (string-match org-plain-time-of-day-regexp ts)))
  6564. (setq s0 (match-string 0 ts)
  6565. s1 (match-string (if plain 1 2) ts)
  6566. s2 (match-string (if plain 8 4) ts))
  6567. ;; If the times are in TXT (not in DOTIMES), and the prefix will list
  6568. ;; them, we might want to remove them there to avoid duplication.
  6569. ;; The user can turn this off with a variable.
  6570. (if (and org-agenda-remove-times-when-in-prefix (or stamp plain)
  6571. (string-match (concat (regexp-quote s0) " *") txt)
  6572. (if (eq org-agenda-remove-times-when-in-prefix 'beg)
  6573. (= (match-beginning 0) 0)
  6574. t))
  6575. (setq txt (replace-match "" nil nil txt))))
  6576. ;; Normalize the time(s) to 24 hour
  6577. (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
  6578. (if s2 (setq s2 (org-get-time-of-day s2 'string t))))
  6579. (when (string-match "\\([ \t]+\\)\\(:[a-zA-Z_@0-9:]+:\\)[ \t]*$" txt)
  6580. ;; Tags are in the string
  6581. (if (or (eq org-agenda-remove-tags-when-in-prefix t)
  6582. (and org-agenda-remove-tags-when-in-prefix
  6583. org-prefix-has-tag))
  6584. (setq txt (replace-match "" t t txt))
  6585. (setq txt (replace-match
  6586. (concat (make-string (max (- 50 (length txt)) 1) ?\ )
  6587. (match-string 2 txt))
  6588. t t txt))))
  6589. ;; Create the final string
  6590. (if noprefix
  6591. (setq rtn txt)
  6592. ;; Prepare the variables needed in the eval of the compiled format
  6593. (setq time (cond (s2 (concat s1 "-" s2))
  6594. (s1 (concat s1 "......"))
  6595. (t ""))
  6596. extra (or extra "")
  6597. category (if (symbolp category) (symbol-name category) category))
  6598. ;; Evaluate the compiled format
  6599. (setq rtn (concat (eval org-prefix-format-compiled) txt)))
  6600. ;; And finally add the text properties
  6601. (org-add-props rtn nil
  6602. 'category (downcase category) 'tags tags
  6603. 'prefix-length (- (length rtn) (length txt))
  6604. 'time-of-day time-of-day
  6605. 'dotime dotime))))
  6606. (defun org-agenda-add-time-grid-maybe (list ndays todayp)
  6607. (catch 'exit
  6608. (cond ((not org-agenda-use-time-grid) (throw 'exit list))
  6609. ((and todayp (member 'today (car org-agenda-time-grid))))
  6610. ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
  6611. ((member 'weekly (car org-agenda-time-grid)))
  6612. (t (throw 'exit list)))
  6613. (let* ((have (delq nil (mapcar
  6614. (lambda (x) (get-text-property 1 'time-of-day x))
  6615. list)))
  6616. (string (nth 1 org-agenda-time-grid))
  6617. (gridtimes (nth 2 org-agenda-time-grid))
  6618. (req (car org-agenda-time-grid))
  6619. (remove (member 'remove-match req))
  6620. new time)
  6621. (if (and (member 'require-timed req) (not have))
  6622. ;; don't show empty grid
  6623. (throw 'exit list))
  6624. (while (setq time (pop gridtimes))
  6625. (unless (and remove (member time have))
  6626. (setq time (int-to-string time))
  6627. (push (org-format-agenda-item
  6628. nil string "" nil
  6629. (concat (substring time 0 -2) ":" (substring time -2)))
  6630. new)
  6631. (put-text-property
  6632. 1 (length (car new)) 'face 'org-time-grid (car new))))
  6633. (if (member 'time-up org-agenda-sorting-strategy)
  6634. (append new list)
  6635. (append list new)))))
  6636. (defun org-compile-prefix-format (format)
  6637. "Compile the prefix format into a Lisp form that can be evaluated.
  6638. The resulting form is returned and stored in the variable
  6639. `org-prefix-format-compiled'."
  6640. (setq org-prefix-has-time nil org-prefix-has-tag nil)
  6641. (let ((start 0) varform vars var (s format)e c f opt)
  6642. (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cts]\\)"
  6643. s start)
  6644. (setq var (cdr (assoc (match-string 4 s)
  6645. '(("c" . category) ("t" . time) ("s" . extra)
  6646. ("T" . tag))))
  6647. c (or (match-string 3 s) "")
  6648. opt (match-beginning 1)
  6649. start (1+ (match-beginning 0)))
  6650. (if (equal var 'time) (setq org-prefix-has-time t))
  6651. (if (equal var 'tag) (setq org-prefix-has-tag t))
  6652. (setq f (concat "%" (match-string 2 s) "s"))
  6653. (if opt
  6654. (setq varform
  6655. `(if (equal "" ,var)
  6656. ""
  6657. (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
  6658. (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
  6659. (setq s (replace-match "%s" t nil s))
  6660. (push varform vars))
  6661. (setq vars (nreverse vars))
  6662. (setq org-prefix-format-compiled `(format ,s ,@vars))))
  6663. (defun org-get-time-of-day (s &optional string mod24)
  6664. "Check string S for a time of day.
  6665. If found, return it as a military time number between 0 and 2400.
  6666. If not found, return nil.
  6667. The optional STRING argument forces conversion into a 5 character wide string
  6668. HH:MM."
  6669. (save-match-data
  6670. (when
  6671. (or
  6672. (string-match
  6673. "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
  6674. (string-match
  6675. "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
  6676. (let* ((h (string-to-number (match-string 1 s)))
  6677. (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
  6678. (ampm (if (match-end 4) (downcase (match-string 4 s))))
  6679. (am-p (equal ampm "am"))
  6680. (h1 (cond ((not ampm) h)
  6681. ((= h 12) (if am-p 0 12))
  6682. (t (+ h (if am-p 0 12)))))
  6683. (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
  6684. (mod h1 24) h1))
  6685. (t0 (+ (* 100 h2) m))
  6686. (t1 (concat (if (>= h1 24) "+" " ")
  6687. (if (< t0 100) "0" "")
  6688. (if (< t0 10) "0" "")
  6689. (int-to-string t0))))
  6690. (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
  6691. (defun org-finalize-agenda-entries (list)
  6692. "Sort and concatenate the agenda items."
  6693. (setq list (mapcar 'org-agenda-highlight-todo list))
  6694. (mapconcat 'identity (sort list 'org-entries-lessp) "\n"))
  6695. (defun org-agenda-highlight-todo (x)
  6696. (let (re pl)
  6697. (if (eq x 'line)
  6698. (save-excursion
  6699. (beginning-of-line 1)
  6700. (setq re (get-text-property (point) 'org-not-done-regexp))
  6701. (goto-char (+ (point) (get-text-property (point) 'prefix-length)))
  6702. (and (looking-at (concat "[ \t]*" re))
  6703. (add-text-properties (match-beginning 0) (match-end 0)
  6704. '(face org-todo))))
  6705. (setq re (get-text-property 0 'org-not-done-regexp x)
  6706. pl (get-text-property 0 'prefix-length x))
  6707. (and re (equal (string-match re x pl) pl)
  6708. (add-text-properties (match-beginning 0) (match-end 0)
  6709. '(face org-todo) x))
  6710. x)))
  6711. (defsubst org-cmp-priority (a b)
  6712. "Compare the priorities of string A and B."
  6713. (let ((pa (or (get-text-property 1 'priority a) 0))
  6714. (pb (or (get-text-property 1 'priority b) 0)))
  6715. (cond ((> pa pb) +1)
  6716. ((< pa pb) -1)
  6717. (t nil))))
  6718. (defsubst org-cmp-category (a b)
  6719. "Compare the string values of categories of strings A and B."
  6720. (let ((ca (or (get-text-property 1 'category a) ""))
  6721. (cb (or (get-text-property 1 'category b) "")))
  6722. (cond ((string-lessp ca cb) -1)
  6723. ((string-lessp cb ca) +1)
  6724. (t nil))))
  6725. (defsubst org-cmp-time (a b)
  6726. "Compare the time-of-day values of strings A and B."
  6727. (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
  6728. (ta (or (get-text-property 1 'time-of-day a) def))
  6729. (tb (or (get-text-property 1 'time-of-day b) def)))
  6730. (cond ((< ta tb) -1)
  6731. ((< tb ta) +1)
  6732. (t nil))))
  6733. (defun org-entries-lessp (a b)
  6734. "Predicate for sorting agenda entries."
  6735. ;; The following variables will be used when the form is evaluated.
  6736. (let* ((time-up (org-cmp-time a b))
  6737. (time-down (if time-up (- time-up) nil))
  6738. (priority-up (org-cmp-priority a b))
  6739. (priority-down (if priority-up (- priority-up) nil))
  6740. (category-up (org-cmp-category a b))
  6741. (category-down (if category-up (- category-up) nil))
  6742. (category-keep (if category-up +1 nil)))
  6743. (cdr (assoc
  6744. (eval (cons 'or org-agenda-sorting-strategy))
  6745. '((-1 . t) (1 . nil) (nil . nil))))))
  6746. (defun org-agenda-show-priority ()
  6747. "Show the priority of the current item.
  6748. This priority is composed of the main priority given with the [#A] cookies,
  6749. and by additional input from the age of a schedules or deadline entry."
  6750. (interactive)
  6751. (let* ((pri (get-text-property (point-at-bol) 'priority)))
  6752. (message "Priority is %d" (if pri pri -1000))))
  6753. (defun org-agenda-show-tags ()
  6754. "Show the tags applicable to the current item."
  6755. (interactive)
  6756. (let* ((tags (get-text-property (point-at-bol) 'tags)))
  6757. (if tags
  6758. (message "Tags are :%s:"
  6759. (org-no-properties (mapconcat 'identity tags ":")))
  6760. (message "No tags associated with this line"))))
  6761. (defun org-agenda-goto (&optional highlight)
  6762. "Go to the Org-mode file which contains the item at point."
  6763. (interactive)
  6764. (let* ((marker (or (get-text-property (point) 'org-marker)
  6765. (org-agenda-error)))
  6766. (buffer (marker-buffer marker))
  6767. (pos (marker-position marker)))
  6768. (switch-to-buffer-other-window buffer)
  6769. (widen)
  6770. (goto-char pos)
  6771. (when (eq major-mode 'org-mode)
  6772. (org-show-hidden-entry)
  6773. (save-excursion
  6774. (and (outline-next-heading)
  6775. (org-flag-heading nil)))) ; show the next heading
  6776. (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
  6777. (defun org-agenda-switch-to ()
  6778. "Go to the Org-mode file which contains the item at point."
  6779. (interactive)
  6780. (let* ((marker (or (get-text-property (point) 'org-marker)
  6781. (org-agenda-error)))
  6782. (buffer (marker-buffer marker))
  6783. (pos (marker-position marker)))
  6784. (switch-to-buffer buffer)
  6785. (delete-other-windows)
  6786. (widen)
  6787. (goto-char pos)
  6788. (when (eq major-mode 'org-mode)
  6789. (org-show-hidden-entry)
  6790. (save-excursion
  6791. (and (outline-next-heading)
  6792. (org-flag-heading nil)))))) ; show the next heading
  6793. (defun org-agenda-goto-mouse (ev)
  6794. "Go to the Org-mode file which contains the item at the mouse click."
  6795. (interactive "e")
  6796. (mouse-set-point ev)
  6797. (org-agenda-goto))
  6798. (defun org-agenda-show ()
  6799. "Display the Org-mode file which contains the item at point."
  6800. (interactive)
  6801. (let ((win (selected-window)))
  6802. (org-agenda-goto t)
  6803. (select-window win)))
  6804. (defun org-agenda-recenter (arg)
  6805. "Display the Org-mode file which contains the item at point and recenter."
  6806. (interactive "P")
  6807. (let ((win (selected-window)))
  6808. (org-agenda-goto t)
  6809. (recenter arg)
  6810. (select-window win)))
  6811. (defun org-agenda-show-mouse (ev)
  6812. "Display the Org-mode file which contains the item at the mouse click."
  6813. (interactive "e")
  6814. (mouse-set-point ev)
  6815. (org-agenda-show))
  6816. (defun org-agenda-check-no-diary ()
  6817. "Check if the entry is a diary link and abort if yes."
  6818. (if (get-text-property (point) 'org-agenda-diary-link)
  6819. (org-agenda-error)))
  6820. (defun org-agenda-error ()
  6821. (error "Command not allowed in this line"))
  6822. (defvar org-last-heading-marker (make-marker)
  6823. "Marker pointing to the headline that last changed its TODO state
  6824. by a remote command from the agenda.")
  6825. (defun org-agenda-todo (&optional arg)
  6826. "Cycle TODO state of line at point, also in Org-mode file.
  6827. This changes the line at point, all other lines in the agenda referring to
  6828. the same tree node, and the headline of the tree node in the Org-mode file."
  6829. (interactive "P")
  6830. (org-agenda-check-no-diary)
  6831. (let* ((col (current-column))
  6832. (marker (or (get-text-property (point) 'org-marker)
  6833. (org-agenda-error)))
  6834. (buffer (marker-buffer marker))
  6835. (pos (marker-position marker))
  6836. (hdmarker (get-text-property (point) 'org-hd-marker))
  6837. (buffer-read-only nil)
  6838. newhead)
  6839. (with-current-buffer buffer
  6840. (widen)
  6841. (goto-char pos)
  6842. (org-show-hidden-entry)
  6843. (save-excursion
  6844. (and (outline-next-heading)
  6845. (org-flag-heading nil))) ; show the next heading
  6846. (org-todo arg)
  6847. (and (bolp) (forward-char 1))
  6848. (setq newhead (org-get-heading))
  6849. (save-excursion
  6850. (org-back-to-heading)
  6851. (move-marker org-last-heading-marker (point))))
  6852. (beginning-of-line 1)
  6853. (save-excursion
  6854. (org-agenda-change-all-lines newhead hdmarker 'fixface))
  6855. (move-to-column col)))
  6856. (defun org-agenda-change-all-lines (newhead hdmarker &optional fixface)
  6857. "Change all lines in the agenda buffer which match HDMARKER.
  6858. The new content of the line will be NEWHEAD (as modified by
  6859. `org-format-agenda-item'). HDMARKER is checked with
  6860. `equal' against all `org-hd-marker' text properties in the file.
  6861. If FIXFACE is non-nil, the face of each item is modified acording to
  6862. the new TODO state."
  6863. (let* (props m pl undone-face done-face finish new dotime cat tags)
  6864. ; (setq newhead (org-format-agenda-item "x" newhead "x" nil 'noprefix))
  6865. (save-excursion
  6866. (goto-char (point-max))
  6867. (beginning-of-line 1)
  6868. (while (not finish)
  6869. (setq finish (bobp))
  6870. (when (and (setq m (get-text-property (point) 'org-hd-marker))
  6871. (equal m hdmarker))
  6872. (setq props (text-properties-at (point))
  6873. dotime (get-text-property (point) 'dotime)
  6874. cat (get-text-property (point) 'category)
  6875. tags (get-text-property (point) 'tags)
  6876. new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
  6877. pl (get-text-property (point) 'prefix-length)
  6878. undone-face (get-text-property (point) 'undone-face)
  6879. done-face (get-text-property (point) 'done-face))
  6880. (move-to-column pl)
  6881. (if (looking-at ".*")
  6882. (progn
  6883. (replace-match new t t)
  6884. (beginning-of-line 1)
  6885. (add-text-properties (point-at-bol) (point-at-eol) props)
  6886. (when fixface
  6887. (add-text-properties
  6888. (point-at-bol) (point-at-eol)
  6889. (list 'face
  6890. (if org-last-todo-state-is-todo
  6891. undone-face done-face)))
  6892. (org-agenda-highlight-todo 'line))
  6893. (beginning-of-line 1))
  6894. (error "Line update did not work")))
  6895. (beginning-of-line 0)))))
  6896. (defun org-agenda-priority-up ()
  6897. "Increase the priority of line at point, also in Org-mode file."
  6898. (interactive)
  6899. (org-agenda-priority 'up))
  6900. (defun org-agenda-priority-down ()
  6901. "Decrease the priority of line at point, also in Org-mode file."
  6902. (interactive)
  6903. (org-agenda-priority 'down))
  6904. (defun org-agenda-priority (&optional force-direction)
  6905. "Set the priority of line at point, also in Org-mode file.
  6906. This changes the line at point, all other lines in the agenda referring to
  6907. the same tree node, and the headline of the tree node in the Org-mode file."
  6908. (interactive)
  6909. (org-agenda-check-no-diary)
  6910. (let* ((marker (or (get-text-property (point) 'org-marker)
  6911. (org-agenda-error)))
  6912. (buffer (marker-buffer marker))
  6913. (pos (marker-position marker))
  6914. (hdmarker (get-text-property (point) 'org-hd-marker))
  6915. (buffer-read-only nil)
  6916. newhead)
  6917. (with-current-buffer buffer
  6918. (widen)
  6919. (goto-char pos)
  6920. (org-show-hidden-entry)
  6921. (save-excursion
  6922. (and (outline-next-heading)
  6923. (org-flag-heading nil))) ; show the next heading
  6924. (funcall 'org-priority force-direction)
  6925. (end-of-line 1)
  6926. (setq newhead (org-get-heading)))
  6927. (org-agenda-change-all-lines newhead hdmarker)
  6928. (beginning-of-line 1)))
  6929. (defun org-get-tags-at (&optional pos)
  6930. "Get a list of all headline tags applicable at POS.
  6931. POS defaults to point. If tags are inherited, the list contains
  6932. the targets in the same sequence as the headlines appear, i.e.
  6933. the tags of the current headline come last."
  6934. (interactive)
  6935. (let (tags)
  6936. (save-excursion
  6937. (goto-char (or pos (point)))
  6938. (save-match-data
  6939. (org-back-to-heading t)
  6940. (condition-case nil
  6941. (while t
  6942. (if (looking-at "[^\r\n]+?:\\([a-zA-Z_@0-9:]+\\):[ \t]*\\([\n\r]\\|\\'\\)")
  6943. (setq tags (append (org-split-string
  6944. (org-match-string-no-properties 1) ":")
  6945. tags)))
  6946. (or org-use-tag-inheritance (error ""))
  6947. (org-up-heading-all 1))
  6948. (error nil))))
  6949. (message "%s" tags)
  6950. tags))
  6951. (defun org-agenda-set-tags ()
  6952. "Set tags for the current headline."
  6953. (interactive)
  6954. (org-agenda-check-no-diary)
  6955. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  6956. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  6957. (org-agenda-error)))
  6958. (buffer (marker-buffer hdmarker))
  6959. (pos (marker-position hdmarker))
  6960. (buffer-read-only nil)
  6961. newhead)
  6962. (with-current-buffer buffer
  6963. (widen)
  6964. (goto-char pos)
  6965. (org-show-hidden-entry)
  6966. (save-excursion
  6967. (and (outline-next-heading)
  6968. (org-flag-heading nil))) ; show the next heading
  6969. (call-interactively 'org-set-tags)
  6970. (end-of-line 1)
  6971. (setq newhead (org-get-heading)))
  6972. (org-agenda-change-all-lines newhead hdmarker)
  6973. (beginning-of-line 1)))
  6974. (defun org-agenda-date-later (arg &optional what)
  6975. "Change the date of this item to one day later."
  6976. (interactive "p")
  6977. (org-agenda-check-type t 'agenda 'timeline)
  6978. (org-agenda-check-no-diary)
  6979. (let* ((marker (or (get-text-property (point) 'org-marker)
  6980. (org-agenda-error)))
  6981. (buffer (marker-buffer marker))
  6982. (pos (marker-position marker)))
  6983. (with-current-buffer buffer
  6984. (widen)
  6985. (goto-char pos)
  6986. (if (not (org-at-timestamp-p))
  6987. (error "Cannot find time stamp"))
  6988. (org-timestamp-change arg (or what 'day))
  6989. (message "Time stamp changed to %s" org-last-changed-timestamp))))
  6990. (defun org-agenda-date-earlier (arg &optional what)
  6991. "Change the date of this item to one day earlier."
  6992. (interactive "p")
  6993. (org-agenda-date-later (- arg) what))
  6994. (defun org-agenda-date-prompt (arg)
  6995. "Change the date of this item. Date is prompted for, with default today.
  6996. The prefix ARG is passed to the `org-time-stamp' command and can therefore
  6997. be used to request time specification in the time stamp."
  6998. (interactive "P")
  6999. (org-agenda-check-type t 'agenda 'timeline)
  7000. (org-agenda-check-no-diary)
  7001. (let* ((marker (or (get-text-property (point) 'org-marker)
  7002. (org-agenda-error)))
  7003. (buffer (marker-buffer marker))
  7004. (pos (marker-position marker)))
  7005. (with-current-buffer buffer
  7006. (widen)
  7007. (goto-char pos)
  7008. (if (not (org-at-timestamp-p))
  7009. (error "Cannot find time stamp"))
  7010. (org-time-stamp arg)
  7011. (message "Time stamp changed to %s" org-last-changed-timestamp))))
  7012. (defun org-agenda-schedule (arg)
  7013. "Schedule the item at point."
  7014. (interactive "P")
  7015. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  7016. (org-agenda-check-no-diary)
  7017. (let* ((marker (or (get-text-property (point) 'org-marker)
  7018. (org-agenda-error)))
  7019. (buffer (marker-buffer marker))
  7020. (pos (marker-position marker))
  7021. (org-insert-labeled-timestamps-at-point nil)
  7022. ts)
  7023. (with-current-buffer buffer
  7024. (widen)
  7025. (goto-char pos)
  7026. (setq ts (org-schedule))
  7027. (message "Item scheduled for %s" ts))))
  7028. (defun org-agenda-deadline (arg)
  7029. "Schedule the item at point."
  7030. (interactive "P")
  7031. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  7032. (org-agenda-check-no-diary)
  7033. (let* ((marker (or (get-text-property (point) 'org-marker)
  7034. (org-agenda-error)))
  7035. (buffer (marker-buffer marker))
  7036. (pos (marker-position marker))
  7037. (org-insert-labeled-timestamps-at-point nil)
  7038. ts)
  7039. (with-current-buffer buffer
  7040. (widen)
  7041. (goto-char pos)
  7042. (setq ts (org-deadline))
  7043. (message "Deadline for this item set to %s" ts))))
  7044. (defun org-get-heading ()
  7045. "Return the heading of the current entry, without the stars."
  7046. (save-excursion
  7047. (and (memq (char-before) '(?\n ?\r)) (skip-chars-forward "^\n\r"))
  7048. (if (and (re-search-backward "[\r\n]\\*" nil t)
  7049. (looking-at "[\r\n]\\*+[ \t]+\\([^\r\n]*\\)"))
  7050. (match-string 1)
  7051. "")))
  7052. (defun org-agenda-clock-in (&optional arg)
  7053. "Start the clock on the currently selected item."
  7054. (interactive "P")
  7055. (org-agenda-check-no-diary)
  7056. (let* ((marker (or (get-text-property (point) 'org-marker)
  7057. (org-agenda-error)))
  7058. (buffer (marker-buffer marker))
  7059. (pos (marker-position marker))
  7060. (hdmarker (get-text-property (point) 'org-hd-marker)))
  7061. (with-current-buffer (marker-buffer marker)
  7062. (widen)
  7063. (goto-char pos)
  7064. (org-clock-in))))
  7065. (defun org-agenda-diary-entry ()
  7066. "Make a diary entry, like the `i' command from the calendar.
  7067. All the standard commands work: block, weekly etc."
  7068. (interactive)
  7069. (org-agenda-check-type t 'agenda 'timeline)
  7070. (require 'diary-lib)
  7071. (let* ((char (progn
  7072. (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
  7073. (read-char-exclusive)))
  7074. (cmd (cdr (assoc char
  7075. '((?d . insert-diary-entry)
  7076. (?w . insert-weekly-diary-entry)
  7077. (?m . insert-monthly-diary-entry)
  7078. (?y . insert-yearly-diary-entry)
  7079. (?a . insert-anniversary-diary-entry)
  7080. (?b . insert-block-diary-entry)
  7081. (?c . insert-cyclic-diary-entry)))))
  7082. (oldf (symbol-function 'calendar-cursor-to-date))
  7083. (point (point))
  7084. (mark (or (mark t) (point))))
  7085. (unless cmd
  7086. (error "No command associated with <%c>" char))
  7087. (unless (and (get-text-property point 'day)
  7088. (or (not (equal ?b char))
  7089. (get-text-property mark 'day)))
  7090. (error "Don't know which date to use for diary entry"))
  7091. ;; We implement this by hacking the `calendar-cursor-to-date' function
  7092. ;; and the `calendar-mark-ring' variable. Saves a lot of code.
  7093. (let ((calendar-mark-ring
  7094. (list (calendar-gregorian-from-absolute
  7095. (or (get-text-property mark 'day)
  7096. (get-text-property point 'day))))))
  7097. (unwind-protect
  7098. (progn
  7099. (fset 'calendar-cursor-to-date
  7100. (lambda (&optional error)
  7101. (calendar-gregorian-from-absolute
  7102. (get-text-property point 'day))))
  7103. (call-interactively cmd))
  7104. (fset 'calendar-cursor-to-date oldf)))))
  7105. (defun org-agenda-execute-calendar-command (cmd)
  7106. "Execute a calendar command from the agenda, with the date associated to
  7107. the cursor position."
  7108. (org-agenda-check-type t 'agenda 'timeline)
  7109. (require 'diary-lib)
  7110. (unless (get-text-property (point) 'day)
  7111. (error "Don't know which date to use for calendar command"))
  7112. (let* ((oldf (symbol-function 'calendar-cursor-to-date))
  7113. (point (point))
  7114. (date (calendar-gregorian-from-absolute
  7115. (get-text-property point 'day)))
  7116. (displayed-day (extract-calendar-day date))
  7117. (displayed-month (extract-calendar-month date))
  7118. (displayed-year (extract-calendar-year date)))
  7119. (unwind-protect
  7120. (progn
  7121. (fset 'calendar-cursor-to-date
  7122. (lambda (&optional error)
  7123. (calendar-gregorian-from-absolute
  7124. (get-text-property point 'day))))
  7125. (call-interactively cmd))
  7126. (fset 'calendar-cursor-to-date oldf))))
  7127. (defun org-agenda-phases-of-moon ()
  7128. "Display the phases of the moon for the 3 months around the cursor date."
  7129. (interactive)
  7130. (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
  7131. (defun org-agenda-holidays ()
  7132. "Display the holidays for the 3 months around the cursor date."
  7133. (interactive)
  7134. (org-agenda-execute-calendar-command 'list-calendar-holidays))
  7135. (defun org-agenda-sunrise-sunset (arg)
  7136. "Display sunrise and sunset for the cursor date.
  7137. Latitude and longitude can be specified with the variables
  7138. `calendar-latitude' and `calendar-longitude'. When called with prefix
  7139. argument, latitude and longitude will be prompted for."
  7140. (interactive "P")
  7141. (let ((calendar-longitude (if arg nil calendar-longitude))
  7142. (calendar-latitude (if arg nil calendar-latitude))
  7143. (calendar-location-name
  7144. (if arg "the given coordinates" calendar-location-name)))
  7145. (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
  7146. (defun org-agenda-goto-calendar ()
  7147. "Open the Emacs calendar with the date at the cursor."
  7148. (interactive)
  7149. (org-agenda-check-type t 'agenda 'timeline)
  7150. (let* ((day (or (get-text-property (point) 'day)
  7151. (error "Don't know which date to open in calendar")))
  7152. (date (calendar-gregorian-from-absolute day))
  7153. (calendar-move-hook nil)
  7154. (view-calendar-holidays-initially nil)
  7155. (view-diary-entries-initially nil))
  7156. (calendar)
  7157. (calendar-goto-date date)))
  7158. (defun org-calendar-goto-agenda ()
  7159. "Compute the Org-mode agenda for the calendar date displayed at the cursor.
  7160. This is a command that has to be installed in `calendar-mode-map'."
  7161. (interactive)
  7162. (org-agenda-list nil (calendar-absolute-from-gregorian
  7163. (calendar-cursor-to-date))
  7164. nil t))
  7165. (defun org-agenda-convert-date ()
  7166. (interactive)
  7167. (org-agenda-check-type t 'agenda 'timeline)
  7168. (let ((day (get-text-property (point) 'day))
  7169. date s)
  7170. (unless day
  7171. (error "Don't know which date to convert"))
  7172. (setq date (calendar-gregorian-from-absolute day))
  7173. (setq s (concat
  7174. "Gregorian: " (calendar-date-string date) "\n"
  7175. "ISO: " (calendar-iso-date-string date) "\n"
  7176. "Day of Yr: " (calendar-day-of-year-string date) "\n"
  7177. "Julian: " (calendar-julian-date-string date) "\n"
  7178. "Astron. JD: " (calendar-astro-date-string date)
  7179. " (Julian date number at noon UTC)\n"
  7180. "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
  7181. "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
  7182. "French: " (calendar-french-date-string date) "\n"
  7183. "Mayan: " (calendar-mayan-date-string date) "\n"
  7184. "Coptic: " (calendar-coptic-date-string date) "\n"
  7185. "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
  7186. "Persian: " (calendar-persian-date-string date) "\n"
  7187. "Chinese: " (calendar-chinese-date-string date) "\n"))
  7188. (with-output-to-temp-buffer "*Dates*"
  7189. (princ s))
  7190. (if (fboundp 'fit-window-to-buffer)
  7191. (fit-window-to-buffer (get-buffer-window "*Dates*")))))
  7192. ;;; Tags
  7193. (defun org-scan-tags (action matcher &optional todo-only)
  7194. "Scan headline tags with inheritance and produce output ACTION.
  7195. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  7196. evaluated, testing if a given set of tags qualifies a headline for
  7197. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  7198. are included in the output."
  7199. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  7200. (mapconcat 'regexp-quote
  7201. (nreverse (cdr (reverse org-todo-keywords)))
  7202. "\\|")
  7203. "\\>\\)\\)? *\\(.*?\\)\\(:[A-Za-z_@0-9:]+:\\)?[ \t]*[\n\r]"))
  7204. (props (list 'face nil
  7205. 'done-face 'org-done
  7206. 'undone-face nil
  7207. 'mouse-face 'highlight
  7208. 'keymap org-agenda-keymap
  7209. 'help-echo
  7210. (format "mouse-2 or RET jump to org file %s"
  7211. (abbreviate-file-name buffer-file-name))))
  7212. lspos
  7213. tags tags-list tags-alist (llast 0) rtn level category i txt
  7214. todo marker)
  7215. (save-excursion
  7216. (goto-char (point-min))
  7217. (when (eq action 'sparse-tree) (org-overview))
  7218. (while (re-search-forward re nil t)
  7219. (setq todo (if (match-end 1) (match-string 2))
  7220. tags (if (match-end 4) (match-string 4)))
  7221. (goto-char (setq lspos (1+ (match-beginning 0))))
  7222. (setq level (funcall outline-level)
  7223. category (org-get-category))
  7224. (setq i llast llast level)
  7225. ;; remove tag lists from same and sublevels
  7226. (while (>= i level)
  7227. (when (setq entry (assoc i tags-alist))
  7228. (setq tags-alist (delete entry tags-alist)))
  7229. (setq i (1- i)))
  7230. ;; add the nex tags
  7231. (when tags
  7232. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  7233. tags-alist
  7234. (cons (cons level tags) tags-alist)))
  7235. ;; compile tags for current headline
  7236. (setq tags-list
  7237. (if org-use-tag-inheritance
  7238. (apply 'append (mapcar 'cdr tags-alist))
  7239. tags))
  7240. (when (and (or (not todo-only) todo)
  7241. (eval matcher))
  7242. ;; list this headline
  7243. (if (eq action 'sparse-tree)
  7244. (progn
  7245. (org-show-hierarchy-above))
  7246. (setq txt (org-format-agenda-item
  7247. ""
  7248. (concat
  7249. (if org-tags-match-list-sublevels
  7250. (make-string (1- level) ?.) "")
  7251. (org-get-heading))
  7252. category tags-list))
  7253. (goto-char lspos)
  7254. (setq marker (org-agenda-new-marker))
  7255. (org-add-props txt props
  7256. 'org-marker marker 'org-hd-marker marker 'category category)
  7257. (push txt rtn))
  7258. ;; if we are to skip sublevels, jump to end of subtree
  7259. (point)
  7260. (or org-tags-match-list-sublevels (org-end-of-subtree)))))
  7261. (nreverse rtn)))
  7262. (defun org-tags-sparse-tree (&optional arg match)
  7263. "Create a sparse tree according to tags search string MATCH.
  7264. MATCH can contain positive and negative selection of tags, like
  7265. \"+WORK+URGENT-WITHBOSS\"."
  7266. (interactive "P")
  7267. (let ((org-show-following-heading nil)
  7268. (org-show-hierarchy-above nil))
  7269. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)))))
  7270. (defun org-make-tags-matcher (match)
  7271. "Create the TAGS matcher form for the tags-selecting string MATCH."
  7272. (unless match
  7273. ;; Get a new match request, with completion
  7274. (setq org-last-tags-completion-table
  7275. (or (org-get-buffer-tags)
  7276. org-last-tags-completion-table))
  7277. (setq match (completing-read
  7278. "Tags: " 'org-tags-completion-function nil nil nil
  7279. 'org-tags-history)))
  7280. ;; parse the string and create a lisp form
  7281. (let ((match0 match) minus tag mm matcher orterms term orlist)
  7282. (setq orterms (org-split-string match "|"))
  7283. (while (setq term (pop orterms))
  7284. (while (string-match "^&?\\([-+:]\\)?\\([A-Za-z_@0-9]+\\)" term)
  7285. (setq minus (and (match-end 1)
  7286. (equal (match-string 1 term) "-"))
  7287. tag (match-string 2 term)
  7288. term (substring term (match-end 0))
  7289. mm (list 'member (downcase tag) 'tags-list)
  7290. mm (if minus (list 'not mm) mm))
  7291. (push mm matcher))
  7292. (push (if (> (length matcher) 1) (cons 'and matcher) (car matcher))
  7293. orlist)
  7294. (setq matcher nil))
  7295. (setq matcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  7296. ;; Return the string and lisp forms of the matcher
  7297. (cons match0 matcher)))
  7298. ;;;###autoload
  7299. (defun org-tags-view (&optional todo-only match keep-modes)
  7300. "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
  7301. The prefix arg TODO-ONLY limits the search to TODO entries."
  7302. (interactive "P")
  7303. (org-agenda-maybe-reset-markers 'force)
  7304. (org-compile-prefix-format org-agenda-prefix-format)
  7305. (let* ((org-agenda-keep-modes keep-modes)
  7306. (org-tags-match-list-sublevels
  7307. (if todo-only t org-tags-match-list-sublevels))
  7308. (win (selected-window))
  7309. (completion-ignore-case t)
  7310. rtn rtnall files file pos matcher
  7311. buffer)
  7312. (setq matcher (org-make-tags-matcher match)
  7313. match (car matcher) matcher (cdr matcher))
  7314. (if (not (equal (current-buffer) (get-buffer org-agenda-buffer-name)))
  7315. (progn
  7316. (delete-other-windows)
  7317. (switch-to-buffer-other-window
  7318. (get-buffer-create org-agenda-buffer-name))))
  7319. (setq buffer-read-only nil)
  7320. (erase-buffer)
  7321. (org-agenda-mode) (setq buffer-read-only nil)
  7322. (set (make-local-variable 'org-agenda-type) 'tags)
  7323. (set (make-local-variable 'org-agenda-redo-command)
  7324. (list 'org-tags-view (list 'quote todo-only)
  7325. (list 'if 'current-prefix-arg nil match) t))
  7326. (setq files (org-agenda-files)
  7327. rtnall nil)
  7328. (while (setq file (pop files))
  7329. (catch 'nextfile
  7330. (org-check-agenda-file file)
  7331. (setq buffer (if (file-exists-p file)
  7332. (org-get-agenda-file-buffer file)
  7333. (error "No such file %s" file)))
  7334. (if (not buffer)
  7335. ;; If file does not exist, merror message to agenda
  7336. (setq rtn (list
  7337. (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  7338. rtnall (append rtnall rtn))
  7339. (with-current-buffer buffer
  7340. (unless (eq major-mode 'org-mode)
  7341. (error "Agenda file %s is not in `org-mode'" file))
  7342. (setq org-category-table (org-get-category-table))
  7343. (save-excursion
  7344. (save-restriction
  7345. (if org-respect-restriction
  7346. (if (org-region-active-p)
  7347. ;; Respect a region to restrict search
  7348. (narrow-to-region (region-beginning) (region-end)))
  7349. ;; If we work for the calendar or many files,
  7350. ;; get rid of any restriction
  7351. (widen))
  7352. (setq rtn (org-scan-tags 'agenda matcher todo-only))
  7353. (setq rtnall (append rtnall rtn))))))))
  7354. (insert "Headlines with TAGS match: ")
  7355. (add-text-properties (point-min) (1- (point))
  7356. (list 'face 'org-level-3))
  7357. (setq pos (point))
  7358. (insert match "\n")
  7359. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  7360. (setq pos (point))
  7361. (insert "Press `C-u r' to search again with new search string\n")
  7362. (add-text-properties pos (1- (point)) (list 'face 'org-level-3))
  7363. (when rtnall
  7364. (insert (mapconcat 'identity rtnall "\n")))
  7365. (goto-char (point-min))
  7366. (setq buffer-read-only t)
  7367. (org-fit-agenda-window)
  7368. (if (not org-select-agenda-window) (select-window win))))
  7369. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  7370. (defun org-set-tags (&optional arg just-align)
  7371. "Set the tags for the current headline.
  7372. With prefix ARG, realign all tags in headings in the current buffer."
  7373. (interactive "P")
  7374. (let* ((re (concat "^" outline-regexp))
  7375. (col (current-column))
  7376. (current (org-get-tags))
  7377. table current-tags inherited-tags ; computed below when needed
  7378. tags hd empty invis)
  7379. (if arg
  7380. (save-excursion
  7381. (goto-char (point-min))
  7382. (while (re-search-forward re nil t)
  7383. (org-set-tags nil t))
  7384. (message "All tags realigned to column %d" org-tags-column))
  7385. (if just-align
  7386. (setq tags current)
  7387. (setq table (or org-tag-alist (org-get-buffer-tags))
  7388. org-last-tags-completion-table table
  7389. current-tags (org-split-string current ":")
  7390. inherited-tags (nreverse
  7391. (nthcdr (length current-tags)
  7392. (nreverse (org-get-tags-at))))
  7393. tags
  7394. (if (or (eq t org-use-fast-tag-selection)
  7395. (and org-use-fast-tag-selection
  7396. (delq nil (mapcar 'cdr table))))
  7397. (org-fast-tag-selection current-tags inherited-tags table)
  7398. (let ((org-add-colon-after-tag-completion t))
  7399. (completing-read "Tags: " 'org-tags-completion-function
  7400. nil nil current 'org-tags-history))))
  7401. (while (string-match "[-+&]+" tags)
  7402. (setq tags (replace-match ":" t t tags))))
  7403. (unless (setq empty (string-match "\\`[\t ]*\\'" tags))
  7404. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  7405. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  7406. (if (equal current "")
  7407. (progn
  7408. (end-of-line 1)
  7409. (or empty (insert-before-markers " ")))
  7410. (beginning-of-line 1)
  7411. (setq invis (org-invisible-p))
  7412. (looking-at (concat "\\(.*\\)\\(" (regexp-quote current) "\\)[ \t]*"))
  7413. (setq hd (match-string 1))
  7414. (delete-region (match-beginning 0) (match-end 0))
  7415. (insert-before-markers (org-trim hd) (if empty "" " ")))
  7416. (unless (equal tags "")
  7417. (move-to-column (max (current-column)
  7418. (if (> org-tags-column 0)
  7419. org-tags-column
  7420. (- (- org-tags-column) (length tags))))
  7421. t)
  7422. (insert-before-markers tags)
  7423. (if (and (not invis) (org-invisible-p))
  7424. (outline-flag-region (point-at-bol) (point) nil)))
  7425. (move-to-column col))))
  7426. (defun org-tags-completion-function (string predicate &optional flag)
  7427. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  7428. (confirm (lambda (x) (stringp (car x)))))
  7429. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  7430. (setq s1 (match-string 1 string)
  7431. s2 (match-string 2 string))
  7432. (setq s1 "" s2 string))
  7433. (cond
  7434. ((eq flag nil)
  7435. ;; try completion
  7436. (setq rtn (try-completion s2 ctable confirm))
  7437. (if (stringp rtn)
  7438. (concat s1 s2 (substring rtn (length s2))
  7439. (if (and org-add-colon-after-tag-completion
  7440. (assoc rtn ctable))
  7441. ":" "")))
  7442. )
  7443. ((eq flag t)
  7444. ;; all-completions
  7445. (all-completions s2 ctable confirm)
  7446. )
  7447. ((eq flag 'lambda)
  7448. ;; exact match?
  7449. (assoc s2 ctable)))
  7450. ))
  7451. (defun org-fast-tag-insert (kwd tags face &optional end)
  7452. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  7453. (insert (format "%-12s" (concat kwd ":"))
  7454. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  7455. (or end "")))
  7456. (defun org-fast-tag-selection (current inherited table)
  7457. "Fast tag selection with single keys.
  7458. CURRENT is the current list of tags in the headline, INHERITED is the
  7459. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  7460. possibly with grouping information.
  7461. If the keys are nil, a-z are automatically assigned.
  7462. Returns the new tags string, or nil to not change the current settings."
  7463. (let* ((maxlen (apply 'max (mapcar
  7464. (lambda (x)
  7465. (if (stringp (car x)) (string-width (car x)) 0))
  7466. table)))
  7467. (fwidth (+ maxlen 3 1 3))
  7468. (ncol (/ (- (window-width) 4) fwidth))
  7469. (i-face 'org-done)
  7470. (c-face 'org-tag)
  7471. tg cnt e c char c1 c2 ntable tbl rtn
  7472. groups ingroup)
  7473. (save-window-excursion
  7474. (delete-other-windows)
  7475. (split-window-vertically)
  7476. (switch-to-buffer-other-window (get-buffer-create " *Org tags*"))
  7477. (erase-buffer)
  7478. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  7479. (org-fast-tag-insert "Current" current c-face "\n\n")
  7480. (setq tbl table char ?a cnt 0)
  7481. (while (setq e (pop tbl))
  7482. (cond
  7483. ((equal e '(:startgroup))
  7484. (push '() groups) (setq ingroup t)
  7485. (when (not (= cnt 0))
  7486. (setq cnt 0)
  7487. (insert "\n"))
  7488. (insert "{ "))
  7489. ((equal e '(:endgroup))
  7490. (setq ingroup nil cnt 0)
  7491. (insert "}\n"))
  7492. (t
  7493. (setq tg (car e) c2 nil)
  7494. (if (cdr e)
  7495. (setq c (cdr e))
  7496. ;; automatically assign a character.
  7497. (setq c1 (string-to-char
  7498. (downcase (substring
  7499. tg (if (= (string-to-char tg) ?@) 1 0)))))
  7500. (if (or (rassoc c1 ntable) (rassoc c1 table))
  7501. (while (or (rassoc char ntable) (rassoc char table))
  7502. (setq char (1+ char)))
  7503. (setq c2 c1))
  7504. (setq c (or c2 char)))
  7505. (if ingroup (push tg (car groups)))
  7506. (setq tg (org-add-props tg nil 'face
  7507. (cond
  7508. ((member tg current) c-face)
  7509. ((member tg inherited) i-face)
  7510. (t nil))))
  7511. (if (and (= cnt 0) (not ingroup)) (insert " "))
  7512. (insert "[" c "] " tg (make-string
  7513. (- fwidth 4 (length tg)) ?\ ))
  7514. (push (cons tg c) ntable)
  7515. (when (= (setq cnt (1+ cnt)) ncol)
  7516. (insert "\n")
  7517. (if ingroup (insert " "))
  7518. (setq cnt 0)))))
  7519. (setq ntable (nreverse ntable))
  7520. (insert "\n")
  7521. (goto-char (point-min))
  7522. (if (fboundp 'fit-window-to-buffer) (fit-window-to-buffer))
  7523. (setq rtn
  7524. (catch 'exit
  7525. (while t
  7526. (message "[key]:Toggle SPC: clear current RET accept%s"
  7527. (if groups " [!] ignore goups" ""))
  7528. (setq c (read-char-exclusive))
  7529. (cond
  7530. ((= c ?\r) (throw 'exit t))
  7531. ((= c ?!)
  7532. (setq groups nil)
  7533. (goto-char (point-min))
  7534. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  7535. ((or (= c ?\C-g)
  7536. (and (= c ?q) (not (rassoc c ntable))))
  7537. (setq quit-flag t))
  7538. ((= c ?\ ) (setq current nil))
  7539. ((setq e (rassoc c ntable) tg (car e))
  7540. (if (member tg current)
  7541. (setq current (delete tg current))
  7542. (loop for g in groups do
  7543. (if (member tg g)
  7544. (mapcar (lambda (x)
  7545. (setq current (delete x current)))
  7546. g)))
  7547. (setq current (cons tg current)))))
  7548. ;; Create a sorted list
  7549. (setq current
  7550. (sort current
  7551. (lambda (a b)
  7552. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  7553. (goto-char (point-min))
  7554. (beginning-of-line 2)
  7555. (delete-region (point) (point-at-eol))
  7556. (org-fast-tag-insert "Current" current c-face)
  7557. (while (re-search-forward "\\[.\\] \\([a-zA-Z0-9_@]+\\)" nil t)
  7558. (setq tg (match-string 1))
  7559. (add-text-properties (match-beginning 1) (match-end 1)
  7560. (list 'face
  7561. (cond
  7562. ((member tg current) c-face)
  7563. ((member tg inherited) i-face)
  7564. (t nil)))))
  7565. (goto-char (point-min)))))
  7566. (if rtn
  7567. (mapconcat 'identity current ":")
  7568. nil))))
  7569. (defun org-get-tags ()
  7570. "Get the TAGS string in the current headline."
  7571. (unless (org-on-heading-p)
  7572. (error "Not on a heading"))
  7573. (save-excursion
  7574. (beginning-of-line 1)
  7575. (if (looking-at ".*[ \t]\\(:[A-Za-z_@0-9:]+:\\)[ \t]*\\(\r\\|$\\)")
  7576. (org-match-string-no-properties 1)
  7577. "")))
  7578. (defun org-get-buffer-tags ()
  7579. "Get a table of all tags used in the buffer, for completion."
  7580. (let (tags)
  7581. (save-excursion
  7582. (goto-char (point-min))
  7583. (while (re-search-forward "[ \t]:\\([A-Za-z_@0-9:]+\\):[ \t\r\n]" nil t)
  7584. (mapc (lambda (x) (add-to-list 'tags x))
  7585. (org-split-string (org-match-string-no-properties 1) ":"))))
  7586. (mapcar 'list tags)))
  7587. ;;; Link Stuff
  7588. (defvar org-create-file-search-functions nil
  7589. "List of functions to construct the right search string for a file link.
  7590. These functions are called in turn with point at the location to
  7591. which the link should point.
  7592. A function in the hook should first test if it would like to
  7593. handle this file type, for example by checking the major-mode or
  7594. the file extension. If it decides not to handle this file, it
  7595. should just return nil to give other functions a chance. If it
  7596. does handle the file, it must return the search string to be used
  7597. when following the link. The search string will be part of the
  7598. file link, given after a double colon, and `org-open-at-point'
  7599. will automatically search for it. If special measures must be
  7600. taken to make the search successful, another function should be
  7601. added to the companion hook `org-execute-file-search-functions',
  7602. which see.
  7603. A function in this hook may also use `setq' to set the variable
  7604. `description' to provide a suggestion for the descriptive text to
  7605. be used for this link when it gets inserted into an Org-mode
  7606. buffer with \\[org-insert-link].")
  7607. (defvar org-execute-file-search-functions nil
  7608. "List of functions to execute a file search triggered by a link.
  7609. Functions added to this hook must accept a single argument, the
  7610. search string that was part of the file link, the part after the
  7611. double colon. The function must first check if it would like to
  7612. handle this search, for example by checking the major-mode or the
  7613. file extension. If it decides not to handle this search, it
  7614. should just return nil to give other functions a chance. If it
  7615. does handle the search, it must return a non-nil value to keep
  7616. other functions from trying.
  7617. Each function can access the current prefix argument through the
  7618. variable `current-prefix-argument'. Note that a single prefix is
  7619. used to force opening a link in Emacs, so it may be good to only
  7620. use a numeric or double prefix to guide the search function.
  7621. In case this is needed, a function in this hook can also restore
  7622. the window configuration before `org-open-at-point' was called using:
  7623. (set-window-configuration org-window-config-before-follow-link)")
  7624. (defun org-find-file-at-mouse (ev)
  7625. "Open file link or URL at mouse."
  7626. (interactive "e")
  7627. (mouse-set-point ev)
  7628. (org-open-at-point 'in-emacs))
  7629. (defun org-open-at-mouse (ev)
  7630. "Open file link or URL at mouse."
  7631. (interactive "e")
  7632. (mouse-set-point ev)
  7633. (org-open-at-point))
  7634. (defvar org-window-config-before-follow-link nil
  7635. "The window configuration before following a link.
  7636. This is saved in case the need arises to restore it.")
  7637. (defun org-open-at-point (&optional in-emacs)
  7638. "Open link at or after point.
  7639. If there is no link at point, this function will search forward up to
  7640. the end of the current subtree.
  7641. Normally, files will be opened by an appropriate application. If the
  7642. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  7643. (interactive "P")
  7644. (setq org-window-config-before-follow-link (current-window-configuration))
  7645. (org-remove-occur-highlights nil nil t)
  7646. (if (org-at-timestamp-p)
  7647. (org-agenda-list nil (time-to-days (org-time-string-to-time
  7648. (substring (match-string 1) 0 10)))
  7649. 1)
  7650. (let (type path link line search (pos (point)))
  7651. (catch 'match
  7652. (save-excursion
  7653. (skip-chars-forward "^]\n\r")
  7654. (when (and (re-search-backward "\\[\\[" nil t)
  7655. (looking-at org-bracket-link-regexp)
  7656. (<= (match-beginning 0) pos)
  7657. (>= (match-end 0) pos))
  7658. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  7659. (while (string-match " *\n *" link)
  7660. (setq link (replace-match " " t t link)))
  7661. (if (string-match org-link-re-with-space2 link)
  7662. (setq type (match-string 1 link)
  7663. path (match-string 2 link))
  7664. (setq type "thisfile"
  7665. path link))
  7666. (throw 'match t)))
  7667. (when (get-text-property (point) 'org-linked-text)
  7668. (setq type "thisfile"
  7669. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  7670. (1+ (point)) (point))
  7671. path (buffer-substring
  7672. (previous-single-property-change pos 'org-linked-text)
  7673. (next-single-property-change pos 'org-linked-text)))
  7674. (throw 'match t))
  7675. (save-excursion
  7676. (skip-chars-backward (concat "^[]" org-non-link-chars " "))
  7677. (if (equal (char-before) ?<) (backward-char 1))
  7678. (when (or (looking-at org-angle-link-re)
  7679. (looking-at org-plain-link-re)
  7680. (and (or (re-search-forward org-angle-link-re (point-at-eol) t)
  7681. (re-search-forward org-plain-link-re (point-at-eol) t))
  7682. (<= (match-beginning 0) pos)
  7683. (>= (match-end 0) pos)))
  7684. (setq type (match-string 1)
  7685. path (match-string 2))
  7686. (throw 'match t)))
  7687. (save-excursion
  7688. (skip-chars-backward "^ \t\n\r")
  7689. (when (looking-at "\\(:[A-Za-z_@0-9:]+\\):[ \t\r\n]")
  7690. (setq type "tags"
  7691. path (match-string 1))
  7692. (while (string-match ":" path)
  7693. (setq path (replace-match "+" t t path)))
  7694. (throw 'match t)))
  7695. (save-excursion
  7696. (skip-chars-backward "a-zA-Z_")
  7697. (when (and (memq 'camel org-activate-links)
  7698. (looking-at org-camel-regexp))
  7699. (setq type "camel" path (match-string 0))
  7700. (if (equal (char-before) ?*)
  7701. (setq path (concat "*" path))))
  7702. (throw 'match t)))
  7703. (unless path
  7704. (error "No link found"))
  7705. ;; Remove any trailing spaces in path
  7706. (if (string-match " +\\'" path)
  7707. (setq path (replace-match "" t t path)))
  7708. (cond
  7709. ((member type '("http" "https" "ftp" "mailto" "news"))
  7710. (browse-url (concat type ":" path)))
  7711. ((string= type "tags")
  7712. (org-tags-view in-emacs path))
  7713. ((or (string= type "camel")
  7714. (string= type "thisfile"))
  7715. (org-mark-ring-push)
  7716. (org-link-search
  7717. path
  7718. (cond ((equal in-emacs '(4)) 'occur)
  7719. ((equal in-emacs '(16)) 'org-occur)
  7720. (t nil))))
  7721. ((string= type "file")
  7722. (if (string-match "::\\([0-9]+\\)\\'" path)
  7723. (setq line (string-to-number (match-string 1 path))
  7724. path (substring path 0 (match-beginning 0)))
  7725. (if (string-match "::\\(.+\\)\\'" path)
  7726. (setq search (match-string 1 path)
  7727. path (substring path 0 (match-beginning 0)))))
  7728. (org-open-file path in-emacs line search))
  7729. ((string= type "news")
  7730. (org-follow-gnus-link path))
  7731. ((string= type "bbdb")
  7732. (org-follow-bbdb-link path))
  7733. ((string= type "info")
  7734. (org-follow-info-link path))
  7735. ((string= type "gnus")
  7736. (let (group article)
  7737. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  7738. (error "Error in Gnus link"))
  7739. (setq group (match-string 1 path)
  7740. article (match-string 3 path))
  7741. (org-follow-gnus-link group article)))
  7742. ((string= type "vm")
  7743. (let (folder article)
  7744. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  7745. (error "Error in VM link"))
  7746. (setq folder (match-string 1 path)
  7747. article (match-string 3 path))
  7748. ;; in-emacs is the prefix arg, will be interpreted as read-only
  7749. (org-follow-vm-link folder article in-emacs)))
  7750. ((string= type "wl")
  7751. (let (folder article)
  7752. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  7753. (error "Error in Wanderlust link"))
  7754. (setq folder (match-string 1 path)
  7755. article (match-string 3 path))
  7756. (org-follow-wl-link folder article)))
  7757. ((string= type "mhe")
  7758. (let (folder article)
  7759. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  7760. (error "Error in MHE link"))
  7761. (setq folder (match-string 1 path)
  7762. article (match-string 3 path))
  7763. (org-follow-mhe-link folder article)))
  7764. ((string= type "rmail")
  7765. (let (folder article)
  7766. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  7767. (error "Error in RMAIL link"))
  7768. (setq folder (match-string 1 path)
  7769. article (match-string 3 path))
  7770. (org-follow-rmail-link folder article)))
  7771. ((string= type "shell")
  7772. (let ((cmd path))
  7773. (while (string-match "@{" cmd)
  7774. (setq cmd (replace-match "<" t t cmd)))
  7775. (while (string-match "@}" cmd)
  7776. (setq cmd (replace-match ">" t t cmd)))
  7777. (if (or (not org-confirm-shell-link-function)
  7778. (funcall org-confirm-shell-link-function
  7779. (format "Execute \"%s\" in shell? "
  7780. (org-add-props cmd nil
  7781. 'face 'org-warning))))
  7782. (progn
  7783. (message "Executing %s" cmd)
  7784. (shell-command cmd))
  7785. (error "Abort"))))
  7786. ((string= type "elisp")
  7787. (let ((cmd path))
  7788. (if (or (not org-confirm-elisp-link-function)
  7789. (funcall org-confirm-elisp-link-function
  7790. (format "Execute \"%s\" as elisp? "
  7791. (org-add-props cmd nil
  7792. 'face 'org-warning))))
  7793. (message "%s => %s" cmd (eval (read cmd)))
  7794. (error "Abort"))))
  7795. (t
  7796. (browse-url-at-point))))))
  7797. (defun org-link-search (s &optional type)
  7798. "Search for a link search option.
  7799. When S is a CamelCaseWord, search for a target, or for a sentence containing
  7800. the words. If S is surrounded by forward slashes, it is interpreted as a
  7801. regular expression. In org-mode files, this will create an `org-occur'
  7802. sparse tree. In ordinary files, `occur' will be used to list matches.
  7803. If the current buffer is in `dired-mode', grep will be used to search
  7804. in all files."
  7805. (let ((case-fold-search t)
  7806. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  7807. (pos (point))
  7808. (pre "") (post "")
  7809. words re0 re1 re2 re3 re4 re5 re2a reall camel)
  7810. (cond
  7811. ;; First check if there are any special
  7812. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  7813. ;; Now try the builtin stuff
  7814. ((save-excursion
  7815. (goto-char (point-min))
  7816. (and
  7817. (re-search-forward
  7818. (concat "<<" (regexp-quote s0) ">>") nil t)
  7819. (setq pos (match-beginning 0))))
  7820. ;; There is an exact target for this
  7821. (goto-char pos))
  7822. ((string-match "^/\\(.*\\)/$" s)
  7823. ;; A regular expression
  7824. (cond
  7825. ((eq major-mode 'org-mode)
  7826. (org-occur (match-string 1 s)))
  7827. ;;((eq major-mode 'dired-mode)
  7828. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  7829. (t (org-do-occur (match-string 1 s)))))
  7830. ((or (setq camel (string-match (concat "^" org-camel-regexp "$") s))
  7831. t)
  7832. ;; A camel or a normal search string
  7833. (when (equal (string-to-char s) ?*)
  7834. ;; Anchor on headlines, post may include tags.
  7835. (setq pre "^\\*+[ \t]*\\(?:\\sw+\\)?[ \t]*"
  7836. post "[ \t]*\\(?:[ \t]+:[a-zA-Z_@0-9:+]:[ \t]*\\)?$"
  7837. s (substring s 1)))
  7838. (remove-text-properties
  7839. 0 (length s)
  7840. '(face nil mouse-face nil keymap nil fontified nil) s)
  7841. ;; Make a series of regular expressions to find a match
  7842. (setq words
  7843. (if camel
  7844. (org-camel-to-words s)
  7845. (org-split-string s "[ \n\r\t]+"))
  7846. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  7847. re2 (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t]+") "\\)[ \t\r\n]")
  7848. re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  7849. re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  7850. re1 (concat pre re2 post)
  7851. re3 (concat pre re4 post)
  7852. re5 (concat pre ".*" re4)
  7853. re2 (concat pre re2)
  7854. re2a (concat pre re2a)
  7855. re4 (concat pre re4)
  7856. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  7857. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  7858. re5 "\\)"
  7859. ))
  7860. (cond
  7861. ((eq type 'org-occur) (org-occur reall))
  7862. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  7863. (t (goto-char (point-min))
  7864. (if (or (org-search-not-link re0 nil t)
  7865. (org-search-not-link re1 nil t)
  7866. (org-search-not-link re2 nil t)
  7867. (org-search-not-link re2a nil t)
  7868. (org-search-not-link re3 nil t)
  7869. (org-search-not-link re4 nil t)
  7870. (org-search-not-link re5 nil t)
  7871. )
  7872. (goto-char (match-beginning 1))
  7873. (goto-char pos)
  7874. (error "No match")))))
  7875. (t
  7876. ;; Normal string-search
  7877. (goto-char (point-min))
  7878. (if (search-forward s nil t)
  7879. (goto-char (match-beginning 0))
  7880. (error "No match"))))
  7881. (and (eq major-mode 'org-mode) (org-show-hierarchy-above))))
  7882. (defun org-search-not-link (&rest args)
  7883. "Execute `re-search-forward', but only accept matches that are not a link."
  7884. (catch 'exit
  7885. (let ((pos (point)) p1)
  7886. (while (apply 're-search-forward args)
  7887. (setq p1 (point))
  7888. (if (not (save-match-data
  7889. (and (re-search-backward "\\[\\[" nil t)
  7890. (looking-at org-bracket-link-regexp)
  7891. (<= (match-beginning 0) p1)
  7892. (>= (match-end 0) p1))))
  7893. (progn (goto-char (match-end 0))
  7894. (throw 'exit (point)))
  7895. (goto-char (match-end 0)))))))
  7896. (defun org-do-occur (regexp &optional cleanup)
  7897. "Call the Emacs command `occur'.
  7898. If CLEANUP is non-nil, remove the printout of the regular expression
  7899. in the *Occur* buffer. This is useful if the regex is long and not useful
  7900. to read."
  7901. (occur regexp)
  7902. (when cleanup
  7903. (let ((cwin (selected-window)) win beg end)
  7904. (when (setq win (get-buffer-window "*Occur*"))
  7905. (select-window win))
  7906. (goto-char (point-min))
  7907. (when (re-search-forward "match[a-z]+" nil t)
  7908. (setq beg (match-end 0))
  7909. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  7910. (setq end (1- (match-beginning 0)))))
  7911. (and beg end (let ((buffer-read-only)) (delete-region beg end)))
  7912. (goto-char (point-min))
  7913. (select-window cwin))))
  7914. (defvar org-mark-ring nil
  7915. "Mark ring for positions before jumps in Org-mode.")
  7916. (defvar org-mark-ring-last-goto nil
  7917. "Last position in the mark ring used to go back.")
  7918. ;; Fill and close the ring
  7919. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  7920. (loop for i from 1 to org-mark-ring-length do
  7921. (push (make-marker) org-mark-ring))
  7922. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  7923. org-mark-ring)
  7924. (defun org-mark-ring-push (&optional pos buffer)
  7925. "Put the current position or POS into the mark ring and rotate it."
  7926. (interactive)
  7927. (setq pos (or pos (point)))
  7928. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  7929. (move-marker (car org-mark-ring)
  7930. (or pos (point))
  7931. (or buffer (current-buffer)))
  7932. (message
  7933. (substitute-command-keys
  7934. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  7935. (defun org-mark-ring-goto (&optional n)
  7936. "Jump to the previous position in the mark ring.
  7937. With prefix arg N, jump back that many stored positions. When
  7938. called several times in succession, walk through the entire ring.
  7939. Org-mode commands jumping to a different position in the current file,
  7940. or to another Org-mode file, automatically push the old position
  7941. onto the ring."
  7942. (interactive "p")
  7943. (let (p m)
  7944. (if (eq last-command this-command)
  7945. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  7946. (setq p org-mark-ring))
  7947. (setq org-mark-ring-last-goto p)
  7948. (setq m (car p))
  7949. (switch-to-buffer (marker-buffer m))
  7950. (goto-char m)
  7951. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-hierarchy-above))))
  7952. (defun org-camel-to-words (s)
  7953. "Split \"CamelCaseWords\" to (\"Camel\" \"Case\" \"Words\")."
  7954. (let ((case-fold-search nil)
  7955. words)
  7956. (while (string-match "[a-z][A-Z]" s)
  7957. (push (substring s 0 (1+ (match-beginning 0))) words)
  7958. (setq s (substring s (1+ (match-beginning 0)))))
  7959. (nreverse (cons s words))))
  7960. (defun org-remove-angle-brackets (s)
  7961. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  7962. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  7963. s)
  7964. (defun org-add-angle-brackets (s)
  7965. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  7966. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  7967. s)
  7968. (defun org-follow-bbdb-link (name)
  7969. "Follow a BBDB link to NAME."
  7970. (require 'bbdb)
  7971. (let ((inhibit-redisplay t)
  7972. (bbdb-electric-p nil))
  7973. (catch 'exit
  7974. ;; Exact match on name
  7975. (bbdb-name (concat "\\`" name "\\'") nil)
  7976. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  7977. ;; Exact match on name
  7978. (bbdb-company (concat "\\`" name "\\'") nil)
  7979. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  7980. ;; Partial match on name
  7981. (bbdb-name name nil)
  7982. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  7983. ;; Partial match on company
  7984. (bbdb-company name nil)
  7985. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  7986. ;; General match including network address and notes
  7987. (bbdb name nil)
  7988. (when (= 0 (buffer-size (get-buffer "*BBDB*")))
  7989. (delete-window (get-buffer-window "*BBDB*"))
  7990. (error "No matching BBDB record")))))
  7991. (defun org-follow-info-link (name)
  7992. "Follow an info file & node link to NAME."
  7993. (if (or (string-match "\\(.*\\)::?\\(.*\\)" name)
  7994. (string-match "\\(.*\\)" name))
  7995. (progn
  7996. (require 'info)
  7997. (if (match-string 2 name) ; If there isn't a node, choose "Top"
  7998. (Info-find-node (match-string 1 name) (match-string 2 name))
  7999. (Info-find-node (match-string 1 name) "Top")))
  8000. (message (concat "Could not open: " name))))
  8001. (defun org-follow-gnus-link (&optional group article)
  8002. "Follow a Gnus link to GROUP and ARTICLE."
  8003. (require 'gnus)
  8004. (funcall (cdr (assq 'gnus org-link-frame-setup)))
  8005. (if group (gnus-fetch-group group))
  8006. (if article
  8007. (or (gnus-summary-goto-article article nil 'force)
  8008. (if (fboundp 'gnus-summary-insert-cached-articles)
  8009. (progn
  8010. (gnus-summary-insert-cached-articles)
  8011. (gnus-summary-goto-article article nil 'force))
  8012. (message "Message could not be found.")))))
  8013. (defun org-follow-vm-link (&optional folder article readonly)
  8014. "Follow a VM link to FOLDER and ARTICLE."
  8015. (require 'vm)
  8016. (setq article (org-add-angle-brackets article))
  8017. (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
  8018. ;; ange-ftp or efs or tramp access
  8019. (let ((user (or (match-string 1 folder) (user-login-name)))
  8020. (host (match-string 2 folder))
  8021. (file (match-string 3 folder)))
  8022. (cond
  8023. ((featurep 'tramp)
  8024. ;; use tramp to access the file
  8025. (if (featurep 'xemacs)
  8026. (setq folder (format "[%s@%s]%s" user host file))
  8027. (setq folder (format "/%s@%s:%s" user host file))))
  8028. (t
  8029. ;; use ange-ftp or efs
  8030. (require (if (featurep 'xemacs) 'efs 'ange-ftp))
  8031. (setq folder (format "/%s@%s:%s" user host file))))))
  8032. (when folder
  8033. (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
  8034. (sit-for 0.1)
  8035. (when article
  8036. (vm-select-folder-buffer)
  8037. (widen)
  8038. (let ((case-fold-search t))
  8039. (goto-char (point-min))
  8040. (if (not (re-search-forward
  8041. (concat "^" "message-id: *" (regexp-quote article))))
  8042. (error "Could not find the specified message in this folder"))
  8043. (vm-isearch-update)
  8044. (vm-isearch-narrow)
  8045. (vm-beginning-of-message)
  8046. (vm-summarize)))))
  8047. (defun org-follow-wl-link (folder article)
  8048. "Follow a Wanderlust link to FOLDER and ARTICLE."
  8049. (setq article (org-add-angle-brackets article))
  8050. (wl-summary-goto-folder-subr folder 'no-sync t nil t)
  8051. (if article (wl-summary-jump-to-msg-by-message-id article ">"))
  8052. (wl-summary-redisplay))
  8053. (defun org-follow-rmail-link (folder article)
  8054. "Follow an RMAIL link to FOLDER and ARTICLE."
  8055. (setq article (org-add-angle-brackets article))
  8056. (let (message-number)
  8057. (save-excursion
  8058. (save-window-excursion
  8059. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  8060. (setq message-number
  8061. (save-restriction
  8062. (widen)
  8063. (goto-char (point-max))
  8064. (if (re-search-backward
  8065. (concat "^Message-ID:\\s-+" (regexp-quote
  8066. (or article "")))
  8067. nil t)
  8068. (rmail-what-message))))))
  8069. (if message-number
  8070. (progn
  8071. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  8072. (rmail-show-message message-number)
  8073. message-number)
  8074. (error "Message not found"))))
  8075. ;; mh-e integration based on planner-mode
  8076. (defun org-mhe-get-message-real-folder ()
  8077. "Return the name of the current message real folder, so if you use
  8078. sequences, it will now work."
  8079. (save-excursion
  8080. (let* ((folder
  8081. (if (equal major-mode 'mh-folder-mode)
  8082. mh-current-folder
  8083. ;; Refer to the show buffer
  8084. mh-show-folder-buffer))
  8085. (end-index
  8086. (if (boundp 'mh-index-folder)
  8087. (min (length mh-index-folder) (length folder))))
  8088. )
  8089. ;; a simple test on mh-index-data does not work, because
  8090. ;; mh-index-data is always nil in a show buffer.
  8091. (if (and (boundp 'mh-index-folder)
  8092. (string= mh-index-folder (substring folder 0 end-index)))
  8093. (if (equal major-mode 'mh-show-mode)
  8094. (save-window-excursion
  8095. (when (buffer-live-p (get-buffer folder))
  8096. (progn
  8097. (pop-to-buffer folder)
  8098. (org-mhe-get-message-folder-from-index)
  8099. )
  8100. ))
  8101. (org-mhe-get-message-folder-from-index)
  8102. )
  8103. folder
  8104. )
  8105. )))
  8106. (defun org-mhe-get-message-folder-from-index ()
  8107. "Returns the name of the message folder in a index folder buffer."
  8108. (save-excursion
  8109. (mh-index-previous-folder)
  8110. (re-search-forward "^\\(+.*\\)$" nil t)
  8111. (message (match-string 1))))
  8112. (defun org-mhe-get-message-folder ()
  8113. "Return the name of the current message folder. Be careful if you
  8114. use sequences."
  8115. (save-excursion
  8116. (if (equal major-mode 'mh-folder-mode)
  8117. mh-current-folder
  8118. ;; Refer to the show buffer
  8119. mh-show-folder-buffer)))
  8120. (defun org-mhe-get-message-num ()
  8121. "Return the number of the current message. Be careful if you
  8122. use sequences."
  8123. (save-excursion
  8124. (if (equal major-mode 'mh-folder-mode)
  8125. (mh-get-msg-num nil)
  8126. ;; Refer to the show buffer
  8127. (mh-show-buffer-message-number))))
  8128. (defun org-mhe-get-header (header)
  8129. "Return a header of the message in folder mode. This will create a
  8130. show buffer for the corresponding message. If you have a more clever
  8131. idea..."
  8132. (let* ((folder (org-mhe-get-message-folder))
  8133. (num (org-mhe-get-message-num))
  8134. (buffer (get-buffer-create (concat "show-" folder)))
  8135. (header-field))
  8136. (with-current-buffer buffer
  8137. (mh-display-msg num folder)
  8138. (if (equal major-mode 'mh-folder-mode)
  8139. (mh-header-display)
  8140. (mh-show-header-display))
  8141. (set-buffer buffer)
  8142. (setq header-field (mh-get-header-field header))
  8143. (if (equal major-mode 'mh-folder-mode)
  8144. (mh-show)
  8145. (mh-show-show))
  8146. header-field)))
  8147. (defun org-follow-mhe-link (folder article)
  8148. "Follow an MHE link to FOLDER and ARTICLE.
  8149. If ARTICLE is nil FOLDER is shown. If the configuration variable
  8150. `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
  8151. ARTICLE is searched in all folders. Indexed searches (swish++,
  8152. namazu, and others supported by MH-E) will always search in all
  8153. folders."
  8154. (require 'mh-e)
  8155. (require 'mh-search)
  8156. (require 'mh-utils)
  8157. (mh-find-path)
  8158. (if (not article)
  8159. (mh-visit-folder (mh-normalize-folder-name folder))
  8160. (setq article (org-add-angle-brackets article))
  8161. (mh-search-choose)
  8162. (if (equal mh-searcher 'pick)
  8163. (progn
  8164. (mh-search folder (list "--message-id" article))
  8165. (when (and org-mhe-search-all-folders
  8166. (not (org-mhe-get-message-real-folder)))
  8167. (kill-this-buffer)
  8168. (mh-search "+" (list "--message-id" article))))
  8169. (mh-search "+" article))
  8170. (if (org-mhe-get-message-real-folder)
  8171. (mh-show-msg 1)
  8172. (kill-this-buffer)
  8173. (error "Message not found"))))
  8174. ;; BibTeX links
  8175. ;; Use the custom search meachnism to construct and use search strings for
  8176. ;; file links to BibTeX database entries.
  8177. (defun org-create-file-search-in-bibtex ()
  8178. "Create the search string and description for a BibTeX database entry."
  8179. (when (eq major-mode 'bibtex-mode)
  8180. ;; yes, we want to construct this search string.
  8181. ;; Make a good description for this entry, using names, year and the title
  8182. ;; Put it into the `description' variable which is dynamically scoped.
  8183. (let ((bibtex-autokey-names 1)
  8184. (bibtex-autokey-names-stretch 1)
  8185. (bibtex-autokey-name-case-convert-function 'identity)
  8186. (bibtex-autokey-name-separator " & ")
  8187. (bibtex-autokey-additional-names " et al.")
  8188. (bibtex-autokey-year-length 4)
  8189. (bibtex-autokey-name-year-separator " ")
  8190. (bibtex-autokey-titlewords 3)
  8191. (bibtex-autokey-titleword-separator " ")
  8192. (bibtex-autokey-titleword-case-convert-function 'identity)
  8193. (bibtex-autokey-titleword-length 'infty)
  8194. (bibtex-autokey-year-title-separator ": "))
  8195. (setq description (bibtex-generate-autokey)))
  8196. ;; Now parse the entry, get the key and return it.
  8197. (save-excursion
  8198. (bibtex-beginning-of-entry)
  8199. (cdr (assoc "=key=" (bibtex-parse-entry))))))
  8200. (defun org-execute-file-search-in-bibtex (s)
  8201. "Find the link search string S as a key for a database entry."
  8202. (when (eq major-mode 'bibtex-mode)
  8203. ;; Yes, we want to do the search in this file.
  8204. ;; We construct a regexp that searches for "@entrytype{" followed by the key
  8205. (goto-char (point-min))
  8206. (and (re-search-forward (concat "@[a-zA-Z]+[ \t\n]*{[ \t\n]*"
  8207. (regexp-quote s) "[ \t\n]*,") nil t)
  8208. (goto-char (match-beginning 0)))
  8209. (if (and (match-beginning 0) (equal current-prefix-arg '(16)))
  8210. ;; Use double prefix to indicate that any web link should be browsed
  8211. (let ((b (current-buffer)) (p (point)))
  8212. ;; Restore the window configuration because we just use the web link
  8213. (set-window-configuration org-window-config-before-follow-link)
  8214. (save-excursion (set-buffer b) (goto-char p)
  8215. (bibtex-url)))
  8216. (recenter 0)) ; Move entry start to beginning of window
  8217. ;; return t to indicate that the search is done.
  8218. t))
  8219. ;; Finally add the functions to the right hooks.
  8220. (add-hook 'org-create-file-search-functions 'org-create-file-search-in-bibtex)
  8221. (add-hook 'org-execute-file-search-functions 'org-execute-file-search-in-bibtex)
  8222. ;; end of Bibtex link setup
  8223. (defun org-upgrade-old-links (&optional query-description)
  8224. "Transfer old <...> style links to new [[...]] style links.
  8225. With arg query-description, ask at each match for a description text to use
  8226. for this link."
  8227. (interactive (list (y-or-n-p "Would you like to be queried for a description at each link?")))
  8228. (save-excursion
  8229. (goto-char (point-min))
  8230. (let ((re (concat "\\([^[]\\)<\\("
  8231. "\\(" (mapconcat 'identity org-link-types "\\|")
  8232. "\\):"
  8233. "[^" org-non-link-chars "]+\\)>"))
  8234. l1 l2 (cnt 0))
  8235. (while (re-search-forward re nil t)
  8236. (setq cnt (1+ cnt)
  8237. l1 (org-match-string-no-properties 2)
  8238. l2 (save-match-data (org-link-escape l1)))
  8239. (when query-description (setq l1 (read-string "Desc: " l1)))
  8240. (if (equal l1 l2)
  8241. (replace-match (concat (match-string 1) "[[" l1 "]]") t t)
  8242. (replace-match (concat (match-string 1) "[[" l2 "][" l1 "]]") t t)))
  8243. (message "%d matches have beed treated" cnt))))
  8244. (defun org-open-file (path &optional in-emacs line search)
  8245. "Open the file at PATH.
  8246. First, this expands any special file name abbreviations. Then the
  8247. configuration variable `org-file-apps' is checked if it contains an
  8248. entry for this file type, and if yes, the corresponding command is launched.
  8249. If no application is found, Emacs simply visits the file.
  8250. With optional argument IN-EMACS, Emacs will visit the file.
  8251. Optional LINE specifies a line to go to, optional SEARCH a string to
  8252. search for. If LINE or SEARCH is given, the file will always be
  8253. opened in Emacs.
  8254. If the file does not exist, an error is thrown."
  8255. (setq in-emacs (or in-emacs line search))
  8256. (let* ((file (if (equal path "")
  8257. buffer-file-name
  8258. (convert-standard-filename (org-expand-file-name path))))
  8259. (dirp (file-directory-p file))
  8260. (dfile (downcase file))
  8261. (old-buffer (current-buffer))
  8262. (old-pos (point))
  8263. (old-mode major-mode)
  8264. ext cmd apps)
  8265. (if (and (not (file-exists-p file))
  8266. (not org-open-non-existing-files))
  8267. (error "No such file: %s" file))
  8268. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  8269. (setq ext (match-string 1 dfile))
  8270. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  8271. (setq ext (match-string 1 dfile))))
  8272. (setq apps (append org-file-apps (org-default-apps)))
  8273. (if in-emacs
  8274. (setq cmd 'emacs)
  8275. (setq cmd (or (and dirp (cdr (assoc 'directory apps)))
  8276. (cdr (assoc ext apps))
  8277. (cdr (assoc t apps)))))
  8278. (when (eq cmd 'mailcap)
  8279. (require 'mailcap)
  8280. (mailcap-parse-mailcaps)
  8281. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  8282. (command (mailcap-mime-info mime-type)))
  8283. (if (stringp command)
  8284. (setq cmd command)
  8285. (setq cmd 'emacs))))
  8286. (cond
  8287. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  8288. ;; Normalize use of quote, this can vary.
  8289. (if (string-match "['\"]%s['\"]" cmd)
  8290. (setq cmd (replace-match "'%s'" t t cmd)))
  8291. (setq cmd (format cmd file))
  8292. (save-window-excursion
  8293. (shell-command (concat cmd " &"))))
  8294. ((or (stringp cmd)
  8295. (eq cmd 'emacs))
  8296. (unless (equal (file-truename file) (file-truename (or buffer-file-name "")))
  8297. (funcall (cdr (assq 'file org-link-frame-setup)) file))
  8298. (if line (goto-line line)
  8299. (if search (org-link-search search))))
  8300. ((consp cmd)
  8301. (eval cmd))
  8302. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  8303. (and (eq major-mode 'org-mode) (eq old-mode 'org-mode)
  8304. (or (not (equal old-buffer (current-buffer)))
  8305. (not (equal old-pos (point))))
  8306. (org-mark-ring-push old-pos old-buffer))))
  8307. (defun org-default-apps ()
  8308. "Return the default applications for this operating system."
  8309. (cond
  8310. ((eq system-type 'darwin)
  8311. org-file-apps-defaults-macosx)
  8312. ((eq system-type 'windows-nt)
  8313. org-file-apps-defaults-windowsnt)
  8314. (t org-file-apps-defaults-gnu)))
  8315. (defun org-expand-file-name (path)
  8316. "Replace special path abbreviations and expand the file name."
  8317. (expand-file-name path))
  8318. (defvar org-insert-link-history nil
  8319. "Minibuffer history for links inserted with `org-insert-link'.")
  8320. (defvar org-stored-links nil
  8321. "Contains the links stored with `org-store-link'.")
  8322. ;;;###autoload
  8323. (defun org-store-link (arg)
  8324. "\\<org-mode-map>Store an org-link to the current location.
  8325. This link can later be inserted into an org-buffer with
  8326. \\[org-insert-link].
  8327. For some link types, a prefix arg is interpreted:
  8328. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  8329. For file links, arg negates `org-context-in-file-links'."
  8330. (interactive "P")
  8331. (let (link cpltxt desc description search txt (pos (point)))
  8332. (cond
  8333. ((eq major-mode 'bbdb-mode)
  8334. (setq cpltxt (concat
  8335. "bbdb:"
  8336. (or (bbdb-record-name (bbdb-current-record))
  8337. (bbdb-record-company (bbdb-current-record))))
  8338. link (org-make-link cpltxt)))
  8339. ((eq major-mode 'Info-mode)
  8340. (setq link (org-make-link "info:"
  8341. (file-name-nondirectory Info-current-file)
  8342. ":" Info-current-node))
  8343. (setq cpltxt (concat (file-name-nondirectory Info-current-file)
  8344. ":" Info-current-node)))
  8345. ((eq major-mode 'calendar-mode)
  8346. (let ((cd (calendar-cursor-to-date)))
  8347. (setq link
  8348. (format-time-string
  8349. (car org-time-stamp-formats)
  8350. (apply 'encode-time
  8351. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8352. nil nil nil))))))
  8353. ((or (eq major-mode 'vm-summary-mode)
  8354. (eq major-mode 'vm-presentation-mode))
  8355. (and (eq major-mode 'vm-presentation-mode) (vm-summarize))
  8356. (vm-follow-summary-cursor)
  8357. (save-excursion
  8358. (vm-select-folder-buffer)
  8359. (let* ((message (car vm-message-pointer))
  8360. (folder buffer-file-name)
  8361. (subject (vm-su-subject message))
  8362. (author (vm-su-full-name message))
  8363. (message-id (vm-su-message-id message)))
  8364. (setq message-id (org-remove-angle-brackets message-id))
  8365. (setq folder (abbreviate-file-name folder))
  8366. (if (string-match (concat "^" (regexp-quote vm-folder-directory))
  8367. folder)
  8368. (setq folder (replace-match "" t t folder)))
  8369. (setq cpltxt (concat author " on: " subject))
  8370. (setq link (org-make-link "vm:" folder "#" message-id)))))
  8371. ((eq major-mode 'wl-summary-mode)
  8372. (let* ((msgnum (wl-summary-message-number))
  8373. (message-id (elmo-message-field wl-summary-buffer-elmo-folder
  8374. msgnum 'message-id))
  8375. (wl-message-entity (elmo-msgdb-overview-get-entity
  8376. msgnum (wl-summary-buffer-msgdb)))
  8377. (author (wl-summary-line-from)) ; FIXME: correct?
  8378. (subject "???")) ; FIXME:
  8379. (setq message-id (org-remove-angle-brackets message-id))
  8380. (setq cpltxt (concat author " on: " subject))
  8381. (setq link (org-make-link "wl:" wl-summary-buffer-folder-name
  8382. "#" message-id))))
  8383. ((or (equal major-mode 'mh-folder-mode)
  8384. (equal major-mode 'mh-show-mode))
  8385. (let ((from-header (org-mhe-get-header "From:"))
  8386. (to-header (org-mhe-get-header "To:"))
  8387. (subject (org-mhe-get-header "Subject:")))
  8388. (setq cpltxt (concat from-header " on: " subject))
  8389. (setq link (org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
  8390. (org-remove-angle-brackets
  8391. (org-mhe-get-header "Message-Id:"))))))
  8392. ((eq major-mode 'rmail-mode)
  8393. (save-excursion
  8394. (save-restriction
  8395. (rmail-narrow-to-non-pruned-header)
  8396. (let ((folder buffer-file-name)
  8397. (message-id (mail-fetch-field "message-id"))
  8398. (author (mail-fetch-field "from"))
  8399. (subject (mail-fetch-field "subject")))
  8400. (setq message-id (org-remove-angle-brackets message-id))
  8401. (setq cpltxt (concat author " on: " subject))
  8402. (setq link (org-make-link "rmail:" folder "#" message-id))))))
  8403. ((eq major-mode 'gnus-group-mode)
  8404. (let ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
  8405. (gnus-group-group-name)) ; version
  8406. ((fboundp 'gnus-group-name)
  8407. (gnus-group-name))
  8408. (t "???"))))
  8409. (setq cpltxt (concat
  8410. (if (org-xor arg org-usenet-links-prefer-google)
  8411. "http://groups.google.com/groups?group="
  8412. "gnus:")
  8413. group)
  8414. link (org-make-link cpltxt))))
  8415. ((memq major-mode '(gnus-summary-mode gnus-article-mode))
  8416. (and (eq major-mode 'gnus-article-mode) (gnus-article-show-summary))
  8417. (gnus-summary-beginning-of-article)
  8418. (let* ((group (car gnus-article-current))
  8419. (article (cdr gnus-article-current))
  8420. (header (gnus-summary-article-header article))
  8421. (author (mail-header-from header))
  8422. (message-id (mail-header-id header))
  8423. (date (mail-header-date header))
  8424. (subject (gnus-summary-subject-string)))
  8425. (setq cpltxt (concat author " on: " subject))
  8426. (if (org-xor arg org-usenet-links-prefer-google)
  8427. (setq link
  8428. (concat
  8429. cpltxt "\n "
  8430. (format "http://groups.google.com/groups?as_umsgid=%s"
  8431. (org-fixup-message-id-for-http message-id))))
  8432. (setq link (org-make-link "gnus:" group
  8433. "#" (number-to-string article))))))
  8434. ((eq major-mode 'w3-mode)
  8435. (setq cpltxt (url-view-url t)
  8436. link (org-make-link cpltxt)))
  8437. ((eq major-mode 'w3m-mode)
  8438. (setq cpltxt w3m-current-url
  8439. link (org-make-link cpltxt)))
  8440. ((setq search (run-hook-with-args-until-success
  8441. 'org-create-file-search-functions))
  8442. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8443. "::" search))
  8444. (setq cpltxt (or description link)))
  8445. ((eq major-mode 'org-mode)
  8446. ;; Just link to current headline
  8447. (setq cpltxt (concat "file:"
  8448. (abbreviate-file-name buffer-file-name)))
  8449. ;; Add a context search string
  8450. (when (org-xor org-context-in-file-links arg)
  8451. ;; Check if we are on a target
  8452. (if (save-excursion
  8453. (skip-chars-forward "^>\n\r")
  8454. (and (re-search-backward "<<" nil t)
  8455. (looking-at "<<\\(.*?\\)>>")
  8456. (<= (match-beginning 0) pos)
  8457. (>= (match-end 0) pos)))
  8458. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  8459. (setq txt (cond
  8460. ((org-on-heading-p) nil)
  8461. ((org-region-active-p)
  8462. (buffer-substring (region-beginning) (region-end)))
  8463. (t (buffer-substring (point-at-bol) (point-at-eol)))))
  8464. (when (or (null txt) (string-match "\\S-" txt))
  8465. (setq cpltxt
  8466. (concat cpltxt "::"
  8467. (if org-file-link-context-use-camel-case
  8468. (org-make-org-heading-camel txt)
  8469. (org-make-org-heading-search-string txt)))
  8470. desc "NONE"))))
  8471. (if (string-match "::\\'" cpltxt)
  8472. (setq cpltxt (substring cpltxt 0 -2)))
  8473. (setq link (org-make-link cpltxt)))
  8474. (buffer-file-name
  8475. ;; Just link to this file here.
  8476. (setq cpltxt (concat "file:"
  8477. (abbreviate-file-name buffer-file-name)))
  8478. ;; Add a context string
  8479. (when (org-xor org-context-in-file-links arg)
  8480. (setq txt (if (org-region-active-p)
  8481. (buffer-substring (region-beginning) (region-end))
  8482. (buffer-substring (point-at-bol) (point-at-eol))))
  8483. ;; Only use search option if there is some text.
  8484. (when (string-match "\\S-" txt)
  8485. (setq cpltxt
  8486. (concat cpltxt "::"
  8487. (if org-file-link-context-use-camel-case
  8488. (org-make-org-heading-camel txt)
  8489. (org-make-org-heading-search-string txt)))
  8490. desc "NONE")))
  8491. (setq link (org-make-link cpltxt)))
  8492. ((interactive-p)
  8493. (error "Cannot link to a buffer which is not visiting a file"))
  8494. (t (setq link nil)))
  8495. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8496. (setq link (or link cpltxt)
  8497. desc (or desc cpltxt))
  8498. (if (equal desc "NONE") (setq desc nil))
  8499. (if (and (interactive-p) link)
  8500. (progn
  8501. (setq org-stored-links
  8502. (cons (list cpltxt link desc) org-stored-links))
  8503. (message "Stored: %s" (or cpltxt link)))
  8504. (org-make-link-string link desc))))
  8505. (defun org-make-org-heading-search-string (&optional string heading)
  8506. "Make search string for STRING or current headline."
  8507. (interactive)
  8508. (let ((s (or string (org-get-heading))))
  8509. (unless (and string (not heading))
  8510. ;; We are using a headline, clean up garbage in there.
  8511. (if (string-match org-todo-regexp s)
  8512. (setq s (replace-match "" t t s)))
  8513. (if (string-match ":[a-zA-Z_@0-9:]+:[ \t]*$" s)
  8514. (setq s (replace-match "" t t s)))
  8515. (setq s (org-trim s))
  8516. (if (string-match (concat "^\\(" org-quote-string "\\|"
  8517. org-comment-string "\\)") s)
  8518. (setq s (replace-match "" t t s)))
  8519. (while (string-match org-ts-regexp s)
  8520. (setq s (replace-match "" t t s))))
  8521. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  8522. (setq s (replace-match " " t t s)))
  8523. (or string (setq s (concat "*" s))) ; Add * for headlines
  8524. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8525. (defun org-make-org-heading-camel (&optional string heading)
  8526. "Make a CamelCase string for STRING or the current headline."
  8527. (interactive)
  8528. (let ((s (or string (org-get-heading))))
  8529. (unless (and string (not heading))
  8530. ;; We are using a headline, clean up garbage in there.
  8531. (if (string-match org-todo-regexp s)
  8532. (setq s (replace-match "" t t s)))
  8533. (if (string-match ":[a-zA-Z_@0-9:]+:[ \t]*$" s)
  8534. (setq s (replace-match "" t t s)))
  8535. (setq s (org-trim s))
  8536. (if (string-match (concat "^\\(" org-quote-string "\\|"
  8537. org-comment-string "\\)") s)
  8538. (setq s (replace-match "" t t s)))
  8539. (while (string-match org-ts-regexp s)
  8540. (setq s (replace-match "" t t s))))
  8541. (while (string-match "[^a-zA-Z_ \t]+" s)
  8542. (setq s (replace-match " " t t s)))
  8543. (or string (setq s (concat "*" s))) ; Add * for headlines
  8544. (mapconcat 'capitalize (org-split-string s "[ \t]+") "")))
  8545. (defun org-make-link (&rest strings)
  8546. "Concatenate STRINGS, format resulting string with `org-link-format'."
  8547. (format org-link-format (apply 'concat strings)))
  8548. (defun org-make-link-string (link &optional description)
  8549. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8550. (if (eq org-link-style 'plain)
  8551. (if (equal description link)
  8552. link
  8553. (concat description "\n" link))
  8554. (when (stringp description)
  8555. ;; Remove brackets from the description, they are fatal.
  8556. (while (string-match "\\[\\|\\]" description)
  8557. (setq description (replace-match "" t t description))))
  8558. (when (equal (org-link-escape link) description)
  8559. ;; No description needed, it is identical
  8560. (setq description nil))
  8561. (when (and (not description)
  8562. (not (equal link (org-link-escape link))))
  8563. (setq description link))
  8564. (concat "[[" (org-link-escape link) "]"
  8565. (if description (concat "[" description "]") "")
  8566. "]")))
  8567. (defconst org-link-escape-chars '(("[" . "%5B") ("]" . "%5D") (" " . "%20"))
  8568. "Association list of escapes for some characters problematic in links.")
  8569. (defun org-link-escape (text)
  8570. "Escape charaters in TEXT that are problematic for links."
  8571. (when text
  8572. (let ((re (mapconcat (lambda (x) (regexp-quote (car x)))
  8573. org-link-escape-chars "\\|")))
  8574. (while (string-match re text)
  8575. (setq text
  8576. (replace-match
  8577. (cdr (assoc (match-string 0 text) org-link-escape-chars))
  8578. t t text)))
  8579. text)))
  8580. (defun org-link-unescape (text)
  8581. "Reverse the action of `org-link-escape'."
  8582. (when text
  8583. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  8584. org-link-escape-chars "\\|")))
  8585. (while (string-match re text)
  8586. (setq text
  8587. (replace-match
  8588. (car (rassoc (match-string 0 text) org-link-escape-chars))
  8589. t t text)))
  8590. text)))
  8591. (defun org-xor (a b)
  8592. "Exclusive or."
  8593. (if a (not b) b))
  8594. (defun org-get-header (header)
  8595. "Find a header field in the current buffer."
  8596. (save-excursion
  8597. (goto-char (point-min))
  8598. (let ((case-fold-search t) s)
  8599. (cond
  8600. ((eq header 'from)
  8601. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  8602. (setq s (match-string 1)))
  8603. (while (string-match "\"" s)
  8604. (setq s (replace-match "" t t s)))
  8605. (if (string-match "[<(].*" s)
  8606. (setq s (replace-match "" t t s))))
  8607. ((eq header 'message-id)
  8608. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  8609. (setq s (match-string 1))))
  8610. ((eq header 'subject)
  8611. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  8612. (setq s (match-string 1)))))
  8613. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  8614. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  8615. s)))
  8616. (defun org-fixup-message-id-for-http (s)
  8617. "Replace special characters in a message id, so it can be used in an http query."
  8618. (while (string-match "<" s)
  8619. (setq s (replace-match "%3C" t t s)))
  8620. (while (string-match ">" s)
  8621. (setq s (replace-match "%3E" t t s)))
  8622. (while (string-match "@" s)
  8623. (setq s (replace-match "%40" t t s)))
  8624. s)
  8625. (defun org-insert-link (&optional complete-file)
  8626. "Insert a link. At the prompt, enter the link.
  8627. Completion can be used to select a link previously stored with
  8628. `org-store-link'. When the empty string is entered (i.e. if you just
  8629. press RET at the prompt), the link defaults to the most recently
  8630. stored link. As SPC triggers completion in the minibuffer, you need to
  8631. use M-SPC or C-q SPC to force the insertion of a space character.
  8632. You will also be prompted for a description, and if one is given, it will
  8633. be displayed in the buffer instead of the link.
  8634. If there is already a link at point, this command will allow you to edit link
  8635. and description parts.
  8636. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
  8637. selected using completion. The path to the file will be relative to
  8638. the current directory if the file is in the current directory or a
  8639. subdirectory. Otherwise, the link will be the absolute path as
  8640. completed in the minibuffer (i.e. normally ~/path/to/file).
  8641. With two \\[universal-argument] prefixes, enforce an absolute path even if the file
  8642. is in the current directory or below."
  8643. (interactive "P")
  8644. (let (link desc entry remove file (pos (point)))
  8645. (cond
  8646. ((save-excursion
  8647. (skip-chars-forward "^]\n\r")
  8648. (and (re-search-backward "\\[\\[" nil t)
  8649. (looking-at org-bracket-link-regexp)
  8650. (<= (match-beginning 0) pos)
  8651. (>= (match-end 0) pos)))
  8652. ;; We do have a link at point, and we are going to edit it.
  8653. (setq remove (list (match-beginning 0) (match-end 0)))
  8654. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8655. (setq link (read-string "Link: "
  8656. (org-link-unescape
  8657. (org-match-string-no-properties 1)))))
  8658. (complete-file
  8659. ;; Completing read for file names.
  8660. (setq file (read-file-name "File: "))
  8661. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8662. (pwd1 (file-name-as-directory (abbreviate-file-name
  8663. (expand-file-name ".")))))
  8664. (cond
  8665. ((equal complete-file '(16))
  8666. (setq link (org-make-link
  8667. "file:"
  8668. (abbreviate-file-name (expand-file-name file)))))
  8669. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8670. (setq link (org-make-link "file:" (match-string 1 file))))
  8671. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8672. (expand-file-name file))
  8673. (setq link (org-make-link
  8674. "file:" (match-string 1 (expand-file-name file)))))
  8675. (t (setq link (org-make-link "file:" file))))))
  8676. (t
  8677. ;; Read link, with completion for stored links.
  8678. (setq link (org-completing-read
  8679. "Link: " org-stored-links nil nil nil
  8680. org-insert-link-history
  8681. (or (car (car org-stored-links)))))
  8682. (setq entry (assoc link org-stored-links))
  8683. (if (not org-keep-stored-link-after-insertion)
  8684. (setq org-stored-links (delq (assoc link org-stored-links)
  8685. org-stored-links)))
  8686. (setq link (if entry (nth 1 entry) link)
  8687. desc (or desc (nth 2 entry)))))
  8688. (if (string-match org-plain-link-re link)
  8689. ;; URL-like link, normalize the use of angular brackets.
  8690. (setq link (org-make-link (org-remove-angle-brackets link))))
  8691. ;; Check if we are linking to the current file with a search option
  8692. ;; If yes, simplify the link by using only the search option.
  8693. (when (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link)
  8694. (let* ((path (match-string 1 link))
  8695. (case-fold-search nil)
  8696. (search (match-string 2 link)))
  8697. (save-match-data
  8698. (if (equal (file-truename buffer-file-name) (file-truename path))
  8699. ;; We are linking to this same file, with a search option
  8700. (setq link search)))))
  8701. ;; Check if we can/should use a relative path. If yes, simplify the link
  8702. (when (string-match "\\<file:\\(.*\\)" link)
  8703. (let* ((path (match-string 1 link))
  8704. (case-fold-search nil))
  8705. (cond
  8706. ((eq org-link-file-path-type 'absolute)
  8707. (setq path (abbreviate-file-name (expand-file-name path))))
  8708. ((eq org-link-file-path-type 'noabbrev)
  8709. (setq path (expand-file-name path)))
  8710. ((eq org-link-file-path-type 'relative)
  8711. (setq path (file-relative-name path)))
  8712. (t
  8713. (save-match-data
  8714. (if (string-match (concat "^" (regexp-quote
  8715. (file-name-as-directory
  8716. (expand-file-name "."))))
  8717. (expand-file-name path))
  8718. ;; We are linking a file with relative path name.
  8719. (setq path (substring (expand-file-name path)
  8720. (match-end 0)))))))
  8721. (setq link (concat "file:" path))))
  8722. (setq desc (read-string "Description: " desc))
  8723. (unless (string-match "\\S-" desc) (setq desc nil))
  8724. (if remove (apply 'delete-region remove))
  8725. (insert (org-make-link-string link desc))))
  8726. (defun org-completing-read (&rest args)
  8727. (let ((minibuffer-local-completion-map
  8728. (copy-keymap minibuffer-local-completion-map)))
  8729. (define-key minibuffer-local-completion-map " " 'self-insert-command)
  8730. (apply 'completing-read args)))
  8731. ;;; Hooks for remember.el
  8732. (defvar org-finish-function nil)
  8733. ;;;###autoload
  8734. (defun org-remember-annotation ()
  8735. "Return a link to the current location as an annotation for remember.el.
  8736. If you are using Org-mode files as target for data storage with
  8737. remember.el, then the annotations should include a link compatible with the
  8738. conventions in Org-mode. This function returns such a link."
  8739. (org-store-link nil))
  8740. (defconst org-remember-help
  8741. "Select a destination location for the note.
  8742. UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
  8743. RET at beg-of-buf -> Append to file as level 2 headline
  8744. RET on headline -> Store as sublevel entry to current headline
  8745. <left>/<right> -> before/after current headline, same headings level")
  8746. ;;;###autoload
  8747. (defun org-remember-apply-template ()
  8748. "Initialize *remember* buffer with template, invoke `org-mode'.
  8749. This function should be placed into `remember-mode-hook' and in fact requires
  8750. to be run from that hook to fucntion properly."
  8751. (if org-remember-templates
  8752. (let* ((entry (if (= (length org-remember-templates) 1)
  8753. (cdar org-remember-templates)
  8754. (message "Select template: %s"
  8755. (mapconcat
  8756. (lambda (x) (char-to-string (car x)))
  8757. org-remember-templates " "))
  8758. (cdr (assoc (read-char-exclusive) org-remember-templates))))
  8759. (tpl (car entry))
  8760. (file (if (consp (cdr entry)) (nth 1 entry)))
  8761. (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
  8762. (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
  8763. (v-u (concat "[" (substring v-t 1 -1) "]"))
  8764. (v-U (concat "[" (substring v-T 1 -1) "]"))
  8765. (v-a annotation) ; defined in `remember-mode'
  8766. (v-i initial) ; defined in `remember-mode'
  8767. (v-n user-full-name)
  8768. )
  8769. (unless tpl (setq tpl "") (message "No template") (ding))
  8770. (insert tpl) (goto-char (point-min))
  8771. (while (re-search-forward "%\\([tTuTai]\\)" nil t)
  8772. (when (and initial (equal (match-string 0) "%i"))
  8773. (save-match-data
  8774. (let* ((lead (buffer-substring
  8775. (point-at-bol) (match-beginning 0))))
  8776. (setq v-i (mapconcat 'identity
  8777. (org-split-string initial "\n")
  8778. (concat "\n" lead))))))
  8779. (replace-match
  8780. (or (eval (intern (concat "v-" (match-string 1)))) "")
  8781. t t))
  8782. (let ((org-startup-folded nil)
  8783. (org-startup-with-deadline-check nil))
  8784. (org-mode))
  8785. (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
  8786. (set (make-local-variable 'org-default-notes-file) file))
  8787. (goto-char (point-min))
  8788. (if (re-search-forward "%\\?" nil t) (replace-match "")))
  8789. (let ((org-startup-folded nil)
  8790. (org-startup-with-deadline-check nil))
  8791. (org-mode)))
  8792. (set (make-local-variable 'org-finish-function) 'remember-buffer))
  8793. ;;;###autoload
  8794. (defun org-remember-handler ()
  8795. "Store stuff from remember.el into an org file.
  8796. First prompts for an org file. If the user just presses return, the value
  8797. of `org-default-notes-file' is used.
  8798. Then the command offers the headings tree of the selected file in order to
  8799. file the text at a specific location.
  8800. You can either immediately press RET to get the note appended to the
  8801. file, or you can use vertical cursor motion and visibility cycling (TAB) to
  8802. find a better place. Then press RET or <left> or <right> in insert the note.
  8803. Key Cursor position Note gets inserted
  8804. -----------------------------------------------------------------------------
  8805. RET buffer-start as level 2 heading at end of file
  8806. RET on headline as sublevel of the heading at cursor
  8807. RET no heading at cursor position, level taken from context.
  8808. Or use prefix arg to specify level manually.
  8809. <left> on headline as same level, before current heading
  8810. <right> on headline as same level, after current heading
  8811. So the fastest way to store the note is to press RET RET to append it to
  8812. the default file. This way your current train of thought is not
  8813. interrupted, in accordance with the principles of remember.el. But with
  8814. little extra effort, you can push it directly to the correct location.
  8815. Before being stored away, the function ensures that the text has a
  8816. headline, i.e. a first line that starts with a \"*\". If not, a headline
  8817. is constructed from the current date and some additional data.
  8818. If the variable `org-adapt-indentation' is non-nil, the entire text is
  8819. also indented so that it starts in the same column as the headline
  8820. \(i.e. after the stars).
  8821. See also the variable `org-reverse-note-order'."
  8822. (catch 'quit
  8823. (let* ((txt (buffer-substring (point-min) (point-max)))
  8824. (fastp current-prefix-arg)
  8825. (file (if fastp org-default-notes-file (org-get-org-file)))
  8826. (visiting (find-buffer-visiting file))
  8827. (org-startup-with-deadline-check nil)
  8828. (org-startup-folded nil)
  8829. (org-startup-align-all-tables nil)
  8830. spos level indent reversed)
  8831. ;; Modify text so that it becomes a nice subtree which can be inserted
  8832. ;; into an org tree.
  8833. (let* ((lines (split-string txt "\n"))
  8834. first)
  8835. ;; remove empty lines at the beginning
  8836. (while (and lines (string-match "^[ \t]*\n" (car lines)))
  8837. (setq lines (cdr lines)))
  8838. (setq first (car lines) lines (cdr lines))
  8839. (if (string-match "^\\*+" first)
  8840. ;; Is already a headline
  8841. (setq indent nil)
  8842. ;; We need to add a headline: Use time and first buffer line
  8843. (setq lines (cons first lines)
  8844. first (concat "* " (current-time-string)
  8845. " (" (remember-buffer-desc) ")")
  8846. indent " "))
  8847. (if (and org-adapt-indentation indent)
  8848. (setq lines (mapcar (lambda (x) (concat indent x)) lines)))
  8849. (setq txt (concat first "\n"
  8850. (mapconcat 'identity lines "\n"))))
  8851. ;; Find the file
  8852. (if (not visiting)
  8853. (find-file-noselect file))
  8854. (with-current-buffer (get-file-buffer file)
  8855. (save-excursion (and (goto-char (point-min))
  8856. (not (re-search-forward "^\\* " nil t))
  8857. (insert "\n* Notes\n")))
  8858. (setq reversed (org-notes-order-reversed-p))
  8859. (save-excursion
  8860. (save-restriction
  8861. (widen)
  8862. ;; Ask the User for a location
  8863. (setq spos (if fastp 1 (org-get-location
  8864. (current-buffer)
  8865. org-remember-help)))
  8866. (if (not spos) (throw 'quit nil)) ; return nil to show we did
  8867. ; not handle this note
  8868. (goto-char spos)
  8869. (cond ((bobp)
  8870. ;; Put it at the start or end, as level 2
  8871. (save-restriction
  8872. (widen)
  8873. (goto-char (if reversed (point-min) (point-max)))
  8874. (if (not (bolp)) (newline))
  8875. (org-paste-subtree 2 txt)))
  8876. ((and (org-on-heading-p nil) (not current-prefix-arg))
  8877. ;; Put it below this entry, at the beg/end of the subtree
  8878. (org-back-to-heading)
  8879. (setq level (funcall outline-level))
  8880. (if reversed
  8881. (outline-end-of-heading)
  8882. (outline-end-of-subtree))
  8883. (if (not (bolp)) (newline))
  8884. (beginning-of-line 1)
  8885. (org-paste-subtree (1+ level) txt))
  8886. (t
  8887. ;; Put it right there, with automatic level determined by
  8888. ;; org-paste-subtree or from prefix arg
  8889. (org-paste-subtree current-prefix-arg txt)))
  8890. (when remember-save-after-remembering
  8891. (save-buffer)
  8892. (if (not visiting) (kill-buffer (current-buffer)))))))))
  8893. t) ;; return t to indicate that we took care of this note.
  8894. (defun org-get-org-file ()
  8895. "Read a filename, with default directory `org-directory'."
  8896. (let ((default (or org-default-notes-file remember-data-file)))
  8897. (read-file-name (format "File name [%s]: " default)
  8898. (file-name-as-directory org-directory)
  8899. default)))
  8900. (defun org-notes-order-reversed-p ()
  8901. "Check if the current file should receive notes in reversed order."
  8902. (cond
  8903. ((not org-reverse-note-order) nil)
  8904. ((eq t org-reverse-note-order) t)
  8905. ((not (listp org-reverse-note-order)) nil)
  8906. (t (catch 'exit
  8907. (let ((all org-reverse-note-order)
  8908. entry)
  8909. (while (setq entry (pop all))
  8910. (if (string-match (car entry) buffer-file-name)
  8911. (throw 'exit (cdr entry))))
  8912. nil)))))
  8913. ;;; Tables
  8914. ;; Watch out: Here we are talking about two different kind of tables.
  8915. ;; Most of the code is for the tables created with the Org-mode table editor.
  8916. ;; Sometimes, we talk about tables created and edited with the table.el
  8917. ;; Emacs package. We call the former org-type tables, and the latter
  8918. ;; table.el-type tables.
  8919. (defun org-before-change-function (beg end)
  8920. "Every change indicates that a table might need an update."
  8921. (setq org-table-may-need-update t))
  8922. (defconst org-table-line-regexp "^[ \t]*|"
  8923. "Detects an org-type table line.")
  8924. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  8925. "Detects an org-type table line.")
  8926. (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
  8927. "Detects a table line marked for automatic recalculation.")
  8928. (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
  8929. "Detects a table line marked for automatic recalculation.")
  8930. (defconst org-table-calculate-mark-regexp "^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
  8931. "Detects a table line marked for automatic recalculation.")
  8932. (defconst org-table-hline-regexp "^[ \t]*|-"
  8933. "Detects an org-type table hline.")
  8934. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  8935. "Detects a table-type table hline.")
  8936. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  8937. "Detects an org-type or table-type table.")
  8938. (defconst org-table-border-regexp "^[ \t]*[^| \t]"
  8939. "Searching from within a table (any type) this finds the first line
  8940. outside the table.")
  8941. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  8942. "Searching from within a table (any type) this finds the first line
  8943. outside the table.")
  8944. (defun org-table-create-with-table.el ()
  8945. "Use the table.el package to insert a new table.
  8946. If there is already a table at point, convert between Org-mode tables
  8947. and table.el tables."
  8948. (interactive)
  8949. (require 'table)
  8950. (cond
  8951. ((org-at-table.el-p)
  8952. (if (y-or-n-p "Convert table to Org-mode table? ")
  8953. (org-table-convert)))
  8954. ((org-at-table-p)
  8955. (if (y-or-n-p "Convert table to table.el table? ")
  8956. (org-table-convert)))
  8957. (t (call-interactively 'table-insert))))
  8958. (defun org-table-create-or-convert-from-region (arg)
  8959. "Convert region to table, or create an empty table.
  8960. If there is an active region, convert it to a table. If there is no such
  8961. region, create an empty table."
  8962. (interactive "P")
  8963. (if (org-region-active-p)
  8964. (org-table-convert-region (region-beginning) (region-end) arg)
  8965. (org-table-create arg)))
  8966. (defun org-table-create (&optional size)
  8967. "Query for a size and insert a table skeleton.
  8968. SIZE is a string Columns x Rows like for example \"3x2\"."
  8969. (interactive "P")
  8970. (unless size
  8971. (setq size (read-string
  8972. (concat "Table size Columns x Rows [e.g. "
  8973. org-table-default-size "]: ")
  8974. "" nil org-table-default-size)))
  8975. (let* ((pos (point))
  8976. (indent (make-string (current-column) ?\ ))
  8977. (split (org-split-string size " *x *"))
  8978. (rows (string-to-number (nth 1 split)))
  8979. (columns (string-to-number (car split)))
  8980. (line (concat (apply 'concat indent "|" (make-list columns " |"))
  8981. "\n")))
  8982. (if (string-match "^[ \t]*$" (buffer-substring-no-properties
  8983. (point-at-bol) (point)))
  8984. (beginning-of-line 1)
  8985. (newline))
  8986. ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
  8987. (dotimes (i rows) (insert line))
  8988. (goto-char pos)
  8989. (if (> rows 1)
  8990. ;; Insert a hline after the first row.
  8991. (progn
  8992. (end-of-line 1)
  8993. (insert "\n|-")
  8994. (goto-char pos)))
  8995. (org-table-align)))
  8996. (defun org-table-convert-region (beg0 end0 &optional nspace)
  8997. "Convert region to a table.
  8998. The region goes from BEG0 to END0, but these borders will be moved
  8999. slightly, to make sure a beginning of line in the first line is included.
  9000. When NSPACE is non-nil, it indicates the minimum number of spaces that
  9001. separate columns (default: just one space)."
  9002. (interactive "rP")
  9003. (let* ((beg (min beg0 end0))
  9004. (end (max beg0 end0))
  9005. (tabsep t)
  9006. re)
  9007. (goto-char beg)
  9008. (beginning-of-line 1)
  9009. (setq beg (move-marker (make-marker) (point)))
  9010. (goto-char end)
  9011. (if (bolp) (backward-char 1) (end-of-line 1))
  9012. (setq end (move-marker (make-marker) (point)))
  9013. ;; Lets see if this is tab-separated material. If every nonempty line
  9014. ;; contains a tab, we will assume that it is tab-separated material
  9015. (if nspace
  9016. (setq tabsep nil)
  9017. (goto-char beg)
  9018. (and (re-search-forward "^[^\n\t]+$" end t) (setq tabsep nil)))
  9019. (if nspace (setq tabsep nil))
  9020. (if tabsep
  9021. (setq re "^\\|\t")
  9022. (setq re (format "^ *\\| *\t *\\| \\{%d,\\}"
  9023. (max 1 (prefix-numeric-value nspace)))))
  9024. (goto-char beg)
  9025. (while (re-search-forward re end t)
  9026. (replace-match "|" t t))
  9027. (goto-char beg)
  9028. (insert " ")
  9029. (org-table-align)))
  9030. (defun org-table-import (file arg)
  9031. "Import FILE as a table.
  9032. The file is assumed to be tab-separated. Such files can be produced by most
  9033. spreadsheet and database applications. If no tabs (at least one per line)
  9034. are found, lines will be split on whitespace into fields."
  9035. (interactive "f\nP")
  9036. (or (bolp) (newline))
  9037. (let ((beg (point))
  9038. (pm (point-max)))
  9039. (insert-file-contents file)
  9040. (org-table-convert-region beg (+ (point) (- (point-max) pm)) arg)))
  9041. (defun org-table-export ()
  9042. "Export table as a tab-separated file.
  9043. Such a file can be imported into a spreadsheet program like Excel."
  9044. (interactive)
  9045. (let* ((beg (org-table-begin))
  9046. (end (org-table-end))
  9047. (table (buffer-substring beg end))
  9048. (file (read-file-name "Export table to: "))
  9049. buf)
  9050. (unless (or (not (file-exists-p file))
  9051. (y-or-n-p (format "Overwrite file %s? " file)))
  9052. (error "Abort"))
  9053. (with-current-buffer (find-file-noselect file)
  9054. (setq buf (current-buffer))
  9055. (erase-buffer)
  9056. (fundamental-mode)
  9057. (insert table)
  9058. (goto-char (point-min))
  9059. (while (re-search-forward "^[ \t]*|[ \t]*" nil t)
  9060. (replace-match "" t t)
  9061. (end-of-line 1))
  9062. (goto-char (point-min))
  9063. (while (re-search-forward "[ \t]*|[ \t]*$" nil t)
  9064. (replace-match "" t t)
  9065. (goto-char (min (1+ (point)) (point-max))))
  9066. (goto-char (point-min))
  9067. (while (re-search-forward "^-[-+]*$" nil t)
  9068. (replace-match "")
  9069. (if (looking-at "\n")
  9070. (delete-char 1)))
  9071. (goto-char (point-min))
  9072. (while (re-search-forward "[ \t]*|[ \t]*" nil t)
  9073. (replace-match "\t" t t))
  9074. (save-buffer))
  9075. (kill-buffer buf)))
  9076. (defvar org-table-aligned-begin-marker (make-marker)
  9077. "Marker at the beginning of the table last aligned.
  9078. Used to check if cursor still is in that table, to minimize realignment.")
  9079. (defvar org-table-aligned-end-marker (make-marker)
  9080. "Marker at the end of the table last aligned.
  9081. Used to check if cursor still is in that table, to minimize realignment.")
  9082. (defvar org-table-last-alignment nil
  9083. "List of flags for flushright alignment, from the last re-alignment.
  9084. This is being used to correctly align a single field after TAB or RET.")
  9085. (defvar org-table-last-column-widths nil
  9086. "List of max width of fields in each column.
  9087. This is being used to correctly align a single field after TAB or RET.")
  9088. (defvar org-last-recalc-line nil)
  9089. (defconst org-narrow-column-arrow "=>"
  9090. "Used as display property in narrowed table columns.")
  9091. (defun org-table-align ()
  9092. "Align the table at point by aligning all vertical bars."
  9093. (interactive)
  9094. (let* (
  9095. ;; Limits of table
  9096. (beg (org-table-begin))
  9097. (end (org-table-end))
  9098. ;; Current cursor position
  9099. (linepos (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
  9100. (colpos (org-table-current-column))
  9101. (winstart (window-start))
  9102. text lines (new "") lengths l typenums ty fields maxfields i
  9103. column
  9104. (indent "") cnt frac
  9105. rfmt hfmt
  9106. (spaces '(1 . 1))
  9107. (sp1 (car spaces))
  9108. (sp2 (cdr spaces))
  9109. (rfmt1 (concat
  9110. (make-string sp2 ?\ ) "%%%s%ds" (make-string sp1 ?\ ) "|"))
  9111. (hfmt1 (concat
  9112. (make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
  9113. emptystrings xx links narrow fmax fmin f1 len c e)
  9114. (untabify beg end)
  9115. (remove-text-properties beg end '(org-cwidth t display t))
  9116. ;; Check if we have links
  9117. (goto-char beg)
  9118. (setq links (re-search-forward org-bracket-link-regexp end t))
  9119. ;; Make sure the link properties are right
  9120. (when links (goto-char beg) (while (org-activate-bracket-links end)))
  9121. ;; Check if we are narrowing any columns
  9122. (goto-char beg)
  9123. (setq narrow (and org-format-transports-properties-p
  9124. (re-search-forward "<[0-9]+>" end t)))
  9125. ;; Get the rows
  9126. (setq lines (org-split-string
  9127. (buffer-substring beg end) "\n"))
  9128. ;; Store the indentation of the first line
  9129. (if (string-match "^ *" (car lines))
  9130. (setq indent (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  9131. ;; Mark the hlines by setting the corresponding element to nil
  9132. ;; At the same time, we remove trailing space.
  9133. (setq lines (mapcar (lambda (l)
  9134. (if (string-match "^ *|-" l)
  9135. nil
  9136. (if (string-match "[ \t]+$" l)
  9137. (substring l 0 (match-beginning 0))
  9138. l)))
  9139. lines))
  9140. ;; Get the data fields by splitting the lines.
  9141. (setq fields (mapcar
  9142. (lambda (l)
  9143. (org-split-string l " *| *"))
  9144. (delq nil (copy-sequence lines))))
  9145. ;; How many fields in the longest line?
  9146. (condition-case nil
  9147. (setq maxfields (apply 'max (mapcar 'length fields)))
  9148. (error
  9149. (kill-region beg end)
  9150. (org-table-create org-table-default-size)
  9151. (error "Empty table - created default table")))
  9152. ;; A list of empty string to fill any short rows on output
  9153. (setq emptystrings (make-list maxfields ""))
  9154. ;; Check for special formatting.
  9155. (setq i -1)
  9156. (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns
  9157. (setq column (mapcar (lambda (x) (or (nth i x) "")) fields))
  9158. ;; Check if there is an explicit width specified
  9159. (when (and org-table-limit-column-width narrow)
  9160. (setq c column fmax nil)
  9161. (while c
  9162. (setq e (pop c))
  9163. (if (and (stringp e) (string-match "^<\\([0-9]+\\)>$" e))
  9164. (setq fmax (string-to-number (match-string 1 e)) c nil)))
  9165. ;; Find fields that are wider than fmax, and shorten them
  9166. (when fmax
  9167. (loop for xx in column do
  9168. (when (and (stringp xx)
  9169. (> (org-string-width xx) fmax))
  9170. (org-add-props xx nil
  9171. 'help-echo
  9172. (concat "Clipped table field, use C-c ` to edit. Full value is:\n" (org-no-properties (copy-sequence xx))))
  9173. (setq f1 (min fmax (or (string-match org-bracket-link-regexp xx) fmax)))
  9174. (unless (> f1 1)
  9175. (error "Cannot narrow field starting with wide link \"%s\""
  9176. (match-string 0 xx)))
  9177. (add-text-properties f1 (length xx) (list 'org-cwidth t) xx)
  9178. (add-text-properties (- f1 2) f1
  9179. (list 'display org-narrow-column-arrow)
  9180. xx)))))
  9181. ;; Get the maximum width for each column
  9182. (push (apply 'max 1 (mapcar 'org-string-width column)) lengths)
  9183. ;; Get the fraction of numbers, to decide about alignment of the column
  9184. (setq cnt 0 frac 0.0)
  9185. (loop for x in column do
  9186. (if (equal x "")
  9187. nil
  9188. (setq frac ( / (+ (* frac cnt)
  9189. (if (string-match org-table-number-regexp x) 1 0))
  9190. (setq cnt (1+ cnt))))))
  9191. (push (>= frac org-table-number-fraction) typenums))
  9192. (setq lengths (nreverse lengths) typenums (nreverse typenums))
  9193. ;; Store the alignment of this table, for later editing of single fields
  9194. (setq org-table-last-alignment typenums
  9195. org-table-last-column-widths lengths)
  9196. ;; With invisible characters, `format' does not get the field width right
  9197. ;; So we need to make these fields wide by hand.
  9198. (when links
  9199. (loop for i from 0 upto (1- maxfields) do
  9200. (setq len (nth i lengths))
  9201. (loop for j from 0 upto (1- (length fields)) do
  9202. (setq c (nthcdr i (car (nthcdr j fields))))
  9203. (if (and (stringp (car c))
  9204. (string-match org-bracket-link-regexp (car c))
  9205. (< (org-string-width (car c)) len))
  9206. (setcar c (concat (car c) (make-string (- len (org-string-width (car c))) ?\ )))))))
  9207. ;; Compute the formats needed for output of the table
  9208. (setq rfmt (concat indent "|") hfmt (concat indent "|"))
  9209. (while (setq l (pop lengths))
  9210. (setq ty (if (pop typenums) "" "-")) ; number types flushright
  9211. (setq rfmt (concat rfmt (format rfmt1 ty l))
  9212. hfmt (concat hfmt (format hfmt1 (make-string l ?-)))))
  9213. (setq rfmt (concat rfmt "\n")
  9214. hfmt (concat (substring hfmt 0 -1) "|\n"))
  9215. (setq new (mapconcat
  9216. (lambda (l)
  9217. (if l (apply 'format rfmt
  9218. (append (pop fields) emptystrings))
  9219. hfmt))
  9220. lines ""))
  9221. ;; Replace the old one
  9222. (delete-region beg end)
  9223. (move-marker end nil)
  9224. (move-marker org-table-aligned-begin-marker (point))
  9225. (insert new)
  9226. (move-marker org-table-aligned-end-marker (point))
  9227. (when (and orgtbl-mode (not (eq major-mode 'org-mode)))
  9228. (goto-char org-table-aligned-begin-marker)
  9229. (while (org-hide-wide-columns org-table-aligned-end-marker)))
  9230. ;; Try to move to the old location (approximately)
  9231. (goto-line linepos)
  9232. (set-window-start (selected-window) winstart 'noforce)
  9233. (org-table-goto-column colpos)
  9234. (setq org-table-may-need-update nil)
  9235. ))
  9236. (defun org-string-width (s)
  9237. "Compute width of string, ignoring invisible characters.
  9238. This ignores character with invisibility property `org-link', and also
  9239. characters with property `org-cwidth', because these will become invisible
  9240. upon the next fontification round."
  9241. (let (b)
  9242. (when (or (eq t buffer-invisibility-spec)
  9243. (assq 'org-link buffer-invisibility-spec))
  9244. (while (setq b (text-property-any 0 (length s)
  9245. 'invisible 'org-link s))
  9246. (setq s (concat (substring s 0 b)
  9247. (substring s (or (next-single-property-change
  9248. b 'invisible s) (length s)))))))
  9249. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  9250. (setq s (concat (substring s 0 b)
  9251. (substring s (or (next-single-property-change
  9252. b 'org-cwidth s) (length s))))))
  9253. (string-width s)))
  9254. (defun org-table-begin (&optional table-type)
  9255. "Find the beginning of the table and return its position.
  9256. With argument TABLE-TYPE, go to the beginning of a table.el-type table."
  9257. (save-excursion
  9258. (if (not (re-search-backward
  9259. (if table-type org-table-any-border-regexp
  9260. org-table-border-regexp)
  9261. nil t))
  9262. (progn (goto-char (point-min)) (point))
  9263. (goto-char (match-beginning 0))
  9264. (beginning-of-line 2)
  9265. (point))))
  9266. (defun org-table-end (&optional table-type)
  9267. "Find the end of the table and return its position.
  9268. With argument TABLE-TYPE, go to the end of a table.el-type table."
  9269. (save-excursion
  9270. (if (not (re-search-forward
  9271. (if table-type org-table-any-border-regexp
  9272. org-table-border-regexp)
  9273. nil t))
  9274. (goto-char (point-max))
  9275. (goto-char (match-beginning 0)))
  9276. (point-marker)))
  9277. (defun org-table-justify-field-maybe (&optional new)
  9278. "Justify the current field, text to left, number to right.
  9279. Optional argument NEW may specify text to replace the current field content."
  9280. (cond
  9281. ((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
  9282. ((org-at-table-hline-p))
  9283. ((and (not new)
  9284. (or (not (equal (marker-buffer org-table-aligned-begin-marker)
  9285. (current-buffer)))
  9286. (< (point) org-table-aligned-begin-marker)
  9287. (>= (point) org-table-aligned-end-marker)))
  9288. ;; This is not the same table, force a full re-align
  9289. (setq org-table-may-need-update t))
  9290. (t ;; realign the current field, based on previous full realign
  9291. (let* ((pos (point)) s
  9292. (col (org-table-current-column))
  9293. (num (if (> col 0) (nth (1- col) org-table-last-alignment)))
  9294. l f n o e)
  9295. (when (> col 0)
  9296. (skip-chars-backward "^|\n")
  9297. (if (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)")
  9298. (progn
  9299. (setq s (match-string 1)
  9300. o (match-string 0)
  9301. l (max 1 (- (match-end 0) (match-beginning 0) 3))
  9302. e (not (= (match-beginning 2) (match-end 2))))
  9303. (setq f (format (if num " %%%ds %s" " %%-%ds %s")
  9304. l (if e "|" (setq org-table-may-need-update t) ""))
  9305. n (format f s))
  9306. (if new
  9307. (if (<= (length new) l) ;; FIXME: length -> str-width?
  9308. (setq n (format f new))
  9309. (setq n (concat new "|") org-table-may-need-update t)))
  9310. (or (equal n o)
  9311. (let (org-table-may-need-update)
  9312. (replace-match n))))
  9313. (setq org-table-may-need-update t))
  9314. (goto-char pos))))))
  9315. (defun org-table-next-field ()
  9316. "Go to the next field in the current table, creating new lines as needed.
  9317. Before doing so, re-align the table if necessary."
  9318. (interactive)
  9319. (org-table-maybe-eval-formula)
  9320. (org-table-maybe-recalculate-line)
  9321. (if (and org-table-automatic-realign
  9322. org-table-may-need-update)
  9323. (org-table-align))
  9324. (let ((end (org-table-end)))
  9325. (if (org-at-table-hline-p)
  9326. (end-of-line 1))
  9327. (condition-case nil
  9328. (progn
  9329. (re-search-forward "|" end)
  9330. (if (looking-at "[ \t]*$")
  9331. (re-search-forward "|" end))
  9332. (if (and (looking-at "-")
  9333. org-table-tab-jumps-over-hlines
  9334. (re-search-forward "^[ \t]*|\\([^-]\\)" end t))
  9335. (goto-char (match-beginning 1)))
  9336. (if (looking-at "-")
  9337. (progn
  9338. (beginning-of-line 0)
  9339. (org-table-insert-row 'below))
  9340. (if (looking-at " ") (forward-char 1))))
  9341. (error
  9342. (org-table-insert-row 'below)))))
  9343. (defun org-table-previous-field ()
  9344. "Go to the previous field in the table.
  9345. Before doing so, re-align the table if necessary."
  9346. (interactive)
  9347. (org-table-justify-field-maybe)
  9348. (org-table-maybe-recalculate-line)
  9349. (if (and org-table-automatic-realign
  9350. org-table-may-need-update)
  9351. (org-table-align))
  9352. (if (org-at-table-hline-p)
  9353. (end-of-line 1))
  9354. (re-search-backward "|" (org-table-begin))
  9355. (re-search-backward "|" (org-table-begin))
  9356. (while (looking-at "|\\(-\\|[ \t]*$\\)")
  9357. (re-search-backward "|" (org-table-begin)))
  9358. (if (looking-at "| ?")
  9359. (goto-char (match-end 0))))
  9360. (defun org-table-next-row ()
  9361. "Go to the next row (same column) in the current table.
  9362. Before doing so, re-align the table if necessary."
  9363. (interactive)
  9364. (org-table-maybe-eval-formula)
  9365. (org-table-maybe-recalculate-line)
  9366. (if (or (looking-at "[ \t]*$")
  9367. (save-excursion (skip-chars-backward " \t") (bolp)))
  9368. (newline)
  9369. (if (and org-table-automatic-realign
  9370. org-table-may-need-update)
  9371. (org-table-align))
  9372. (let ((col (org-table-current-column)))
  9373. (beginning-of-line 2)
  9374. (if (or (not (org-at-table-p))
  9375. (org-at-table-hline-p))
  9376. (progn
  9377. (beginning-of-line 0)
  9378. (org-table-insert-row 'below)))
  9379. (org-table-goto-column col)
  9380. (skip-chars-backward "^|\n\r")
  9381. (if (looking-at " ") (forward-char 1)))))
  9382. (defun org-table-copy-down (n)
  9383. "Copy a field down in the current column.
  9384. If the field at the cursor is empty, copy into it the content of the nearest
  9385. non-empty field above. With argument N, use the Nth non-empty field.
  9386. If the current field is not empty, it is copied down to the next row, and
  9387. the cursor is moved with it. Therefore, repeating this command causes the
  9388. column to be filled row-by-row.
  9389. If the variable `org-table-copy-increment' is non-nil and the field is an
  9390. integer, it will be incremented while copying."
  9391. (interactive "p")
  9392. (let* ((colpos (org-table-current-column))
  9393. (field (org-table-get-field))
  9394. (non-empty (string-match "[^ \t]" field))
  9395. (beg (org-table-begin))
  9396. txt)
  9397. (org-table-check-inside-data-field)
  9398. (if non-empty
  9399. (progn
  9400. (setq txt (org-trim field))
  9401. (org-table-next-row)
  9402. (org-table-blank-field))
  9403. (save-excursion
  9404. (setq txt
  9405. (catch 'exit
  9406. (while (progn (beginning-of-line 1)
  9407. (re-search-backward org-table-dataline-regexp
  9408. beg t))
  9409. (org-table-goto-column colpos t)
  9410. (if (and (looking-at
  9411. "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
  9412. (= (setq n (1- n)) 0))
  9413. (throw 'exit (match-string 1))))))))
  9414. (if txt
  9415. (progn
  9416. (if (and org-table-copy-increment
  9417. (string-match "^[0-9]+$" txt))
  9418. (setq txt (format "%d" (+ (string-to-number txt) 1))))
  9419. (insert txt)
  9420. (org-table-maybe-recalculate-line)
  9421. (org-table-align))
  9422. (error "No non-empty field found"))))
  9423. (defun org-table-check-inside-data-field ()
  9424. "Is point inside a table data field?
  9425. I.e. not on a hline or before the first or after the last column?
  9426. This actually throws an error, so it aborts the current command."
  9427. (if (or (not (org-at-table-p))
  9428. (= (org-table-current-column) 0)
  9429. (org-at-table-hline-p)
  9430. (looking-at "[ \t]*$"))
  9431. (error "Not in table data field")))
  9432. (defvar org-table-clip nil
  9433. "Clipboard for table regions.")
  9434. (defun org-table-blank-field ()
  9435. "Blank the current table field or active region."
  9436. (interactive)
  9437. (org-table-check-inside-data-field)
  9438. (if (and (interactive-p) (org-region-active-p))
  9439. (let (org-table-clip)
  9440. (org-table-cut-region (region-beginning) (region-end)))
  9441. (skip-chars-backward "^|")
  9442. (backward-char 1)
  9443. (if (looking-at "|[^|\n]+")
  9444. (let* ((pos (match-beginning 0))
  9445. (match (match-string 0))
  9446. (len (org-string-width match)))
  9447. (replace-match (concat "|" (make-string (1- len) ?\ )))
  9448. (goto-char (+ 2 pos))
  9449. (substring match 1)))))
  9450. (defun org-table-get-field (&optional n replace)
  9451. "Return the value of the field in column N of current row.
  9452. N defaults to current field.
  9453. If REPLACE is a string, replace field with this value. The return value
  9454. is always the old value."
  9455. (and n (org-table-goto-column n))
  9456. (skip-chars-backward "^|\n")
  9457. (backward-char 1)
  9458. (if (looking-at "|[^|\r\n]*")
  9459. (let* ((pos (match-beginning 0))
  9460. (val (buffer-substring (1+ pos) (match-end 0))))
  9461. (if replace
  9462. (replace-match (concat "|" replace)))
  9463. (goto-char (min (point-at-eol) (+ 2 pos)))
  9464. val)
  9465. (forward-char 1) ""))
  9466. (defun org-table-current-column ()
  9467. "Find out which column we are in.
  9468. When called interactively, column is also displayed in echo area."
  9469. (interactive)
  9470. (if (interactive-p) (org-table-check-inside-data-field))
  9471. (save-excursion
  9472. (let ((cnt 0) (pos (point)))
  9473. (beginning-of-line 1)
  9474. (while (search-forward "|" pos t)
  9475. (setq cnt (1+ cnt)))
  9476. (if (interactive-p) (message "This is table column %d" cnt))
  9477. cnt)))
  9478. (defun org-table-goto-column (n &optional on-delim force)
  9479. "Move the cursor to the Nth column in the current table line.
  9480. With optional argument ON-DELIM, stop with point before the left delimiter
  9481. of the field.
  9482. If there are less than N fields, just go to after the last delimiter.
  9483. However, when FORCE is non-nil, create new columns if necessary."
  9484. (interactive "p")
  9485. (let ((pos (point-at-eol)))
  9486. (beginning-of-line 1)
  9487. (when (> n 0)
  9488. (while (and (> (setq n (1- n)) -1)
  9489. (or (search-forward "|" pos t)
  9490. (and force
  9491. (progn (end-of-line 1)
  9492. (skip-chars-backward "^|")
  9493. (insert " | "))))))
  9494. ; (backward-char 2) t)))))
  9495. (when (and force (not (looking-at ".*|")))
  9496. (save-excursion (end-of-line 1) (insert " | ")))
  9497. (if on-delim
  9498. (backward-char 1)
  9499. (if (looking-at " ") (forward-char 1))))))
  9500. (defun org-at-table-p (&optional table-type)
  9501. "Return t if the cursor is inside an org-type table.
  9502. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  9503. (if org-enable-table-editor
  9504. (save-excursion
  9505. (beginning-of-line 1)
  9506. (looking-at (if table-type org-table-any-line-regexp
  9507. org-table-line-regexp)))
  9508. nil))
  9509. (defun org-at-table.el-p ()
  9510. "Return t if and only if we are at a table.el table."
  9511. (and (org-at-table-p 'any)
  9512. (save-excursion
  9513. (goto-char (org-table-begin 'any))
  9514. (looking-at org-table1-hline-regexp))))
  9515. (defun org-table-recognize-table.el ()
  9516. "If there is a table.el table nearby, recognize it and move into it."
  9517. (if org-table-tab-recognizes-table.el
  9518. (if (org-at-table.el-p)
  9519. (progn
  9520. (beginning-of-line 1)
  9521. (if (looking-at org-table-dataline-regexp)
  9522. nil
  9523. (if (looking-at org-table1-hline-regexp)
  9524. (progn
  9525. (beginning-of-line 2)
  9526. (if (looking-at org-table-any-border-regexp)
  9527. (beginning-of-line -1)))))
  9528. (if (re-search-forward "|" (org-table-end t) t)
  9529. (progn
  9530. (require 'table)
  9531. (if (table--at-cell-p (point))
  9532. t
  9533. (message "recognizing table.el table...")
  9534. (table-recognize-table)
  9535. (message "recognizing table.el table...done")))
  9536. (error "This should not happen..."))
  9537. t)
  9538. nil)
  9539. nil))
  9540. (defun org-at-table-hline-p ()
  9541. "Return t if the cursor is inside a hline in a table."
  9542. (if org-enable-table-editor
  9543. (save-excursion
  9544. (beginning-of-line 1)
  9545. (looking-at org-table-hline-regexp))
  9546. nil))
  9547. (defun org-table-insert-column ()
  9548. "Insert a new column into the table."
  9549. (interactive)
  9550. (if (not (org-at-table-p))
  9551. (error "Not at a table"))
  9552. (org-table-find-dataline)
  9553. (let* ((col (max 1 (org-table-current-column)))
  9554. (beg (org-table-begin))
  9555. (end (org-table-end))
  9556. ;; Current cursor position
  9557. (linepos (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
  9558. (colpos col))
  9559. (goto-char beg)
  9560. (while (< (point) end)
  9561. (if (org-at-table-hline-p)
  9562. nil
  9563. (org-table-goto-column col t)
  9564. (insert "| "))
  9565. (beginning-of-line 2))
  9566. (move-marker end nil)
  9567. (goto-line linepos)
  9568. (org-table-goto-column colpos)
  9569. (org-table-align)
  9570. (org-table-modify-formulas 'insert col)))
  9571. (defun org-table-find-dataline ()
  9572. "Find a dataline in the current table, which is needed for column commands."
  9573. (if (and (org-at-table-p)
  9574. (not (org-at-table-hline-p)))
  9575. t
  9576. (let ((col (current-column))
  9577. (end (org-table-end)))
  9578. (move-to-column col)
  9579. (while (and (< (point) end)
  9580. (or (not (= (current-column) col))
  9581. (org-at-table-hline-p)))
  9582. (beginning-of-line 2)
  9583. (move-to-column col))
  9584. (if (and (org-at-table-p)
  9585. (not (org-at-table-hline-p)))
  9586. t
  9587. (error
  9588. "Please position cursor in a data line for column operations")))))
  9589. (defun org-table-delete-column ()
  9590. "Delete a column from the table."
  9591. (interactive)
  9592. (if (not (org-at-table-p))
  9593. (error "Not at a table"))
  9594. (org-table-find-dataline)
  9595. (org-table-check-inside-data-field)
  9596. (let* ((col (org-table-current-column))
  9597. (beg (org-table-begin))
  9598. (end (org-table-end))
  9599. ;; Current cursor position
  9600. (linepos (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
  9601. (colpos col))
  9602. (goto-char beg)
  9603. (while (< (point) end)
  9604. (if (org-at-table-hline-p)
  9605. nil
  9606. (org-table-goto-column col t)
  9607. (and (looking-at "|[^|\n]+|")
  9608. (replace-match "|")))
  9609. (beginning-of-line 2))
  9610. (move-marker end nil)
  9611. (goto-line linepos)
  9612. (org-table-goto-column colpos)
  9613. (org-table-align)
  9614. (org-table-modify-formulas 'remove col)))
  9615. (defun org-table-move-column-right ()
  9616. "Move column to the right."
  9617. (interactive)
  9618. (org-table-move-column nil))
  9619. (defun org-table-move-column-left ()
  9620. "Move column to the left."
  9621. (interactive)
  9622. (org-table-move-column 'left))
  9623. (defun org-table-move-column (&optional left)
  9624. "Move the current column to the right. With arg LEFT, move to the left."
  9625. (interactive "P")
  9626. (if (not (org-at-table-p))
  9627. (error "Not at a table"))
  9628. (org-table-find-dataline)
  9629. (org-table-check-inside-data-field)
  9630. (let* ((col (org-table-current-column))
  9631. (col1 (if left (1- col) col))
  9632. (beg (org-table-begin))
  9633. (end (org-table-end))
  9634. ;; Current cursor position
  9635. (linepos (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
  9636. (colpos (if left (1- col) (1+ col))))
  9637. (if (and left (= col 1))
  9638. (error "Cannot move column further left"))
  9639. (if (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
  9640. (error "Cannot move column further right"))
  9641. (goto-char beg)
  9642. (while (< (point) end)
  9643. (if (org-at-table-hline-p)
  9644. nil
  9645. (org-table-goto-column col1 t)
  9646. (and (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
  9647. (replace-match "|\\2|\\1|")))
  9648. (beginning-of-line 2))
  9649. (move-marker end nil)
  9650. (goto-line linepos)
  9651. (org-table-goto-column colpos)
  9652. (org-table-align)
  9653. (org-table-modify-formulas 'swap col (if left (1- col) (1+ col)))))
  9654. (defun org-table-move-row-down ()
  9655. "Move table row down."
  9656. (interactive)
  9657. (org-table-move-row nil))
  9658. (defun org-table-move-row-up ()
  9659. "Move table row up."
  9660. (interactive)
  9661. (org-table-move-row 'up))
  9662. (defun org-table-move-row (&optional up)
  9663. "Move the current table line down. With arg UP, move it up."
  9664. (interactive "P")
  9665. (let ((col (current-column))
  9666. (pos (point))
  9667. (tonew (if up 0 2))
  9668. txt)
  9669. (beginning-of-line tonew)
  9670. (if (not (org-at-table-p))
  9671. (progn
  9672. (goto-char pos)
  9673. (error "Cannot move row further")))
  9674. (goto-char pos)
  9675. (beginning-of-line 1)
  9676. (setq pos (point))
  9677. (setq txt (buffer-substring (point) (1+ (point-at-eol))))
  9678. (delete-region (point) (1+ (point-at-eol)))
  9679. (beginning-of-line tonew)
  9680. (insert txt)
  9681. (beginning-of-line 0)
  9682. (move-to-column col)))
  9683. (defun org-table-insert-row (&optional arg)
  9684. "Insert a new row above the current line into the table.
  9685. With prefix ARG, insert below the current line."
  9686. (interactive "P")
  9687. (if (not (org-at-table-p))
  9688. (error "Not at a table"))
  9689. (let* ((line (buffer-substring (point-at-bol) (point-at-eol)))
  9690. (new (org-table-clean-line line)))
  9691. ;; Fix the first field if necessary
  9692. (if (string-match "^[ \t]*| *[#$] *|" line)
  9693. (setq new (replace-match (match-string 0 line) t t new)))
  9694. (beginning-of-line (if arg 2 1))
  9695. (let (org-table-may-need-update) (insert-before-markers new "\n"))
  9696. (beginning-of-line 0)
  9697. (re-search-forward "| ?" (point-at-eol) t)
  9698. (and org-table-may-need-update (org-table-align))))
  9699. (defun org-table-insert-hline (&optional arg)
  9700. "Insert a horizontal-line below the current line into the table.
  9701. With prefix ARG, insert above the current line."
  9702. (interactive "P")
  9703. (if (not (org-at-table-p))
  9704. (error "Not at a table"))
  9705. (let ((line (org-table-clean-line
  9706. (buffer-substring (point-at-bol) (point-at-eol))))
  9707. (col (current-column)))
  9708. (while (string-match "|\\( +\\)|" line)
  9709. (setq line (replace-match
  9710. (concat "+" (make-string (- (match-end 1) (match-beginning 1))
  9711. ?-) "|") t t line)))
  9712. (and (string-match "\\+" line) (setq line (replace-match "|" t t line)))
  9713. (beginning-of-line (if arg 1 2))
  9714. (insert line "\n")
  9715. (beginning-of-line (if arg 1 -1))
  9716. (move-to-column col)))
  9717. (defun org-table-clean-line (s)
  9718. "Convert a table line S into a string with only \"|\" and space.
  9719. In particular, this does handle wide and invisible characters."
  9720. (if (string-match "^[ \t]*|-" s)
  9721. ;; It's a hline, just map the characters
  9722. (setq s (mapconcat (lambda (x) (if (member x '(?| ?+)) "|" " ")) s ""))
  9723. (while (string-match "|\\([ \t]*?[^ \t\r\n|][^\r\n|]*\\)|" s)
  9724. (setq s (replace-match
  9725. (concat "|" (make-string (org-string-width (match-string 1 s))
  9726. ?\ ) "|")
  9727. t t s)))
  9728. s))
  9729. (defun org-table-kill-row ()
  9730. "Delete the current row or horizontal line from the table."
  9731. (interactive)
  9732. (if (not (org-at-table-p))
  9733. (error "Not at a table"))
  9734. (let ((col (current-column)))
  9735. (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
  9736. (if (not (org-at-table-p)) (beginning-of-line 0))
  9737. (move-to-column col)))
  9738. (defun org-table-sort-lines (beg end numericp)
  9739. "Sort table lines in region.
  9740. Point and mark define the first and last line to include. Both point and
  9741. mark should be in the column that is used for sorting. For example, to
  9742. sort according to column 3, put the mark in the first line to sort, in
  9743. table column 3. Put point into the last line to be included in the sorting,
  9744. also in table column 3. The command will prompt for the sorting method
  9745. \(n for numerical, a for alphanumeric)."
  9746. (interactive "r\nsSorting method: [n]=numeric [a]=alpha: ")
  9747. (setq numericp (string-match "[nN]" numericp))
  9748. (org-table-align) ;; Just to be safe
  9749. (let* (bcol ecol cmp column lns)
  9750. (goto-char beg)
  9751. (org-table-check-inside-data-field)
  9752. (setq column (org-table-current-column)
  9753. beg (move-marker (make-marker) (point-at-bol)))
  9754. (goto-char end)
  9755. (org-table-check-inside-data-field)
  9756. (setq end (move-marker (make-marker) (1+ (point-at-eol))))
  9757. (untabify beg end)
  9758. (goto-char beg)
  9759. (org-table-goto-column column)
  9760. (skip-chars-backward "^|")
  9761. (setq bcol (current-column))
  9762. (org-table-goto-column (1+ column))
  9763. (skip-chars-backward "^|")
  9764. (setq ecol (1- (current-column)))
  9765. (setq cmp (if numericp
  9766. (lambda (a b) (< (car a) (car b)))
  9767. (lambda (a b) (string< (car a) (car b)))))
  9768. (setq lns (mapcar (lambda(x) (cons (org-trim (substring x bcol ecol)) x))
  9769. (org-split-string (buffer-substring beg end) "\n")))
  9770. (if numericp
  9771. (setq lns (mapcar (lambda(x)
  9772. (cons (string-to-number (car x)) (cdr x)))
  9773. lns)))
  9774. (delete-region beg end)
  9775. (move-marker beg nil)
  9776. (move-marker end nil)
  9777. (insert (mapconcat 'cdr (setq lns (sort lns cmp)) "\n") "\n")
  9778. (message "%d lines sorted %s based on column %d"
  9779. (length lns)
  9780. (if numericp "numerically" "alphabetically") column)))
  9781. (defun org-table-cut-region (beg end)
  9782. "Copy region in table to the clipboard and blank all relevant fields."
  9783. (interactive "r")
  9784. (org-table-copy-region beg end 'cut))
  9785. (defun org-table-copy-region (beg end &optional cut)
  9786. "Copy rectangular region in table to clipboard.
  9787. A special clipboard is used which can only be accessed
  9788. with `org-table-paste-rectangle'."
  9789. (interactive "rP")
  9790. (let* (l01 c01 l02 c02 l1 c1 l2 c2 ic1 ic2
  9791. region cols
  9792. (rpl (if cut " " nil)))
  9793. (goto-char beg)
  9794. (org-table-check-inside-data-field)
  9795. (setq l01 (count-lines (point-min) (point))
  9796. c01 (org-table-current-column))
  9797. (goto-char end)
  9798. (org-table-check-inside-data-field)
  9799. (setq l02 (count-lines (point-min) (point))
  9800. c02 (org-table-current-column))
  9801. (setq l1 (min l01 l02) l2 (max l01 l02)
  9802. c1 (min c01 c02) c2 (max c01 c02))
  9803. (catch 'exit
  9804. (while t
  9805. (catch 'nextline
  9806. (if (> l1 l2) (throw 'exit t))
  9807. (goto-line l1)
  9808. (if (org-at-table-hline-p) (throw 'nextline (setq l1 (1+ l1))))
  9809. (setq cols nil ic1 c1 ic2 c2)
  9810. (while (< ic1 (1+ ic2))
  9811. (push (org-table-get-field ic1 rpl) cols)
  9812. (setq ic1 (1+ ic1)))
  9813. (push (nreverse cols) region)
  9814. (setq l1 (1+ l1)))))
  9815. (setq org-table-clip (nreverse region))
  9816. (if cut (org-table-align))
  9817. org-table-clip))
  9818. (defun org-table-paste-rectangle ()
  9819. "Paste a rectangular region into a table.
  9820. The upper right corner ends up in the current field. All involved fields
  9821. will be overwritten. If the rectangle does not fit into the present table,
  9822. the table is enlarged as needed. The process ignores horizontal separator
  9823. lines."
  9824. (interactive)
  9825. (unless (and org-table-clip (listp org-table-clip))
  9826. (error "First cut/copy a region to paste!"))
  9827. (org-table-check-inside-data-field)
  9828. (let* ((clip org-table-clip)
  9829. (line (count-lines (point-min) (point)))
  9830. (col (org-table-current-column))
  9831. (org-enable-table-editor t)
  9832. (org-table-automatic-realign nil)
  9833. c cols field)
  9834. (while (setq cols (pop clip))
  9835. (while (org-at-table-hline-p) (beginning-of-line 2))
  9836. (if (not (org-at-table-p))
  9837. (progn (end-of-line 0) (org-table-next-field)))
  9838. (setq c col)
  9839. (while (setq field (pop cols))
  9840. (org-table-goto-column c nil 'force)
  9841. (org-table-get-field nil field)
  9842. (setq c (1+ c)))
  9843. (beginning-of-line 2))
  9844. (goto-line line)
  9845. (org-table-goto-column col)
  9846. (org-table-align)))
  9847. (defun org-table-convert ()
  9848. "Convert from `org-mode' table to table.el and back.
  9849. Obviously, this only works within limits. When an Org-mode table is
  9850. converted to table.el, all horizontal separator lines get lost, because
  9851. table.el uses these as cell boundaries and has no notion of horizontal lines.
  9852. A table.el table can be converted to an Org-mode table only if it does not
  9853. do row or column spanning. Multiline cells will become multiple cells.
  9854. Beware, Org-mode does not test if the table can be successfully converted - it
  9855. blindly applies a recipe that works for simple tables."
  9856. (interactive)
  9857. (require 'table)
  9858. (if (org-at-table.el-p)
  9859. ;; convert to Org-mode table
  9860. (let ((beg (move-marker (make-marker) (org-table-begin t)))
  9861. (end (move-marker (make-marker) (org-table-end t))))
  9862. (table-unrecognize-region beg end)
  9863. (goto-char beg)
  9864. (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
  9865. (replace-match ""))
  9866. (goto-char beg))
  9867. (if (org-at-table-p)
  9868. ;; convert to table.el table
  9869. (let ((beg (move-marker (make-marker) (org-table-begin)))
  9870. (end (move-marker (make-marker) (org-table-end))))
  9871. ;; first, get rid of all horizontal lines
  9872. (goto-char beg)
  9873. (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t)
  9874. (replace-match ""))
  9875. ;; insert a hline before first
  9876. (goto-char beg)
  9877. (org-table-insert-hline 'above)
  9878. (beginning-of-line -1)
  9879. ;; insert a hline after each line
  9880. (while (progn (beginning-of-line 3) (< (point) end))
  9881. (org-table-insert-hline))
  9882. (goto-char beg)
  9883. (setq end (move-marker end (org-table-end)))
  9884. ;; replace "+" at beginning and ending of hlines
  9885. (while (re-search-forward "^\\([ \t]*\\)|-" end t)
  9886. (replace-match "\\1+-"))
  9887. (goto-char beg)
  9888. (while (re-search-forward "-|[ \t]*$" end t)
  9889. (replace-match "-+"))
  9890. (goto-char beg)))))
  9891. (defun org-table-wrap-region (arg)
  9892. "Wrap several fields in a column like a paragraph.
  9893. This is useful if you'd like to spread the contents of a field over several
  9894. lines, in order to keep the table compact.
  9895. If there is an active region, and both point and mark are in the same column,
  9896. the text in the column is wrapped to minimum width for the given number of
  9897. lines. Generally, this makes the table more compact. A prefix ARG may be
  9898. used to change the number of desired lines. For example, `C-2 \\[org-table-wrap]'
  9899. formats the selected text to two lines. If the region was longer than two
  9900. lines, the remaining lines remain empty. A negative prefix argument reduces
  9901. the current number of lines by that amount. The wrapped text is pasted back
  9902. into the table. If you formatted it to more lines than it was before, fields
  9903. further down in the table get overwritten - so you might need to make space in
  9904. the table first.
  9905. If there is no region, the current field is split at the cursor position and
  9906. the text fragment to the right of the cursor is prepended to the field one
  9907. line down.
  9908. If there is no region, but you specify a prefix ARG, the current field gets
  9909. blank, and the content is appended to the field above."
  9910. (interactive "P")
  9911. (org-table-check-inside-data-field)
  9912. (if (org-region-active-p)
  9913. ;; There is a region: fill as a paragraph
  9914. (let ((beg (region-beginning))
  9915. nlines)
  9916. (org-table-cut-region (region-beginning) (region-end))
  9917. (if (> (length (car org-table-clip)) 1)
  9918. (error "Region must be limited to single column"))
  9919. (setq nlines (if arg
  9920. (if (< arg 1)
  9921. (+ (length org-table-clip) arg)
  9922. arg)
  9923. (length org-table-clip)))
  9924. (setq org-table-clip
  9925. (mapcar 'list (org-wrap (mapconcat 'car org-table-clip " ")
  9926. nil nlines)))
  9927. (goto-char beg)
  9928. (org-table-paste-rectangle))
  9929. ;; No region, split the current field at point
  9930. (if arg
  9931. ;; combine with field above
  9932. (let ((s (org-table-blank-field))
  9933. (col (org-table-current-column)))
  9934. (beginning-of-line 0)
  9935. (while (org-at-table-hline-p) (beginning-of-line 0))
  9936. (org-table-goto-column col)
  9937. (skip-chars-forward "^|")
  9938. (skip-chars-backward " ")
  9939. (insert " " (org-trim s))
  9940. (org-table-align))
  9941. ;; split field
  9942. (when (looking-at "\\([^|]+\\)+|")
  9943. (let ((s (match-string 1)))
  9944. (replace-match " |")
  9945. (goto-char (match-beginning 0))
  9946. (org-table-next-row)
  9947. (insert (org-trim s) " ")
  9948. (org-table-align))))))
  9949. (defvar org-field-marker nil)
  9950. (defun org-table-edit-field (arg)
  9951. "Edit table field in a different window.
  9952. This is mainly useful for fields that contain hidden parts.
  9953. When called with a \\[universal-argument] prefix, just make the full field visible so that
  9954. it can be edited in place."
  9955. (interactive "P")
  9956. (if arg
  9957. (let ((b (save-excursion (skip-chars-backward "^|") (point)))
  9958. (e (save-excursion (skip-chars-forward "^|\r\n") (point))))
  9959. (remove-text-properties b e '(org-cwidth t invisible t
  9960. display t intangible t))
  9961. (if (and (boundp 'font-lock-mode) font-lock-mode)
  9962. (font-lock-fontify-block)))
  9963. (let ((pos (move-marker (make-marker) (point)))
  9964. (field (org-table-get-field))
  9965. (cw (current-window-configuration))
  9966. p)
  9967. (switch-to-buffer-other-window "*Org tmp*")
  9968. (erase-buffer)
  9969. (insert "#\n# Edit field and finish with C-c C-c\n#\n")
  9970. (org-mode)
  9971. (goto-char (setq p (point-max)))
  9972. (insert (org-trim field))
  9973. (remove-text-properties p (point-max)
  9974. '(invisible t org-cwidth t display t
  9975. intangible t))
  9976. (goto-char p)
  9977. (set (make-local-variable 'org-finish-function)
  9978. 'org-table-finish-edit-field)
  9979. (set (make-local-variable 'org-window-configuration) cw)
  9980. (set (make-local-variable 'org-field-marker) pos)
  9981. (message "Edit and finish with C-c C-c"))))
  9982. (defun org-table-finish-edit-field ()
  9983. "Finish editing a table data field.
  9984. Remove all newline characters, insert the result into the table, realign
  9985. the table and kill the editing buffer."
  9986. (let ((pos org-field-marker)
  9987. (cw org-window-configuration)
  9988. (cb (current-buffer))
  9989. text)
  9990. (goto-char (point-min))
  9991. (while (re-search-forward "^#.*\n?" nil t) (replace-match ""))
  9992. (while (re-search-forward "\\([ \t]*\n[ \t]*\\)+" nil t)
  9993. (replace-match " "))
  9994. (setq text (org-trim (buffer-string)))
  9995. (set-window-configuration cw)
  9996. (kill-buffer cb)
  9997. (select-window (get-buffer-window (marker-buffer pos)))
  9998. (goto-char pos)
  9999. (move-marker pos nil)
  10000. (org-table-check-inside-data-field)
  10001. (org-table-get-field nil text)
  10002. (org-table-align)
  10003. (message "New field value inserted")))
  10004. (defun org-trim (s)
  10005. "Remove whitespace at beginning and end of string."
  10006. (if (string-match "^[ \t]+" s) (setq s (replace-match "" t t s)))
  10007. (if (string-match "[ \t]+$" s) (setq s (replace-match "" t t s)))
  10008. s)
  10009. (defun org-wrap (string &optional width lines)
  10010. "Wrap string to either a number of lines, or a width in characters.
  10011. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  10012. that costs. If there is a word longer than WIDTH, the text is actually
  10013. wrapped to the length of that word.
  10014. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  10015. many lines, whatever width that takes.
  10016. The return value is a list of lines, without newlines at the end."
  10017. (let* ((words (org-split-string string "[ \t\n]+"))
  10018. (maxword (apply 'max (mapcar 'org-string-width words)))
  10019. w ll)
  10020. (cond (width
  10021. (org-do-wrap words (max maxword width)))
  10022. (lines
  10023. (setq w maxword)
  10024. (setq ll (org-do-wrap words maxword))
  10025. (if (<= (length ll) lines)
  10026. ll
  10027. (setq ll words)
  10028. (while (> (length ll) lines)
  10029. (setq w (1+ w))
  10030. (setq ll (org-do-wrap words w)))
  10031. ll))
  10032. (t (error "Cannot wrap this")))))
  10033. (defun org-do-wrap (words width)
  10034. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  10035. (let (lines line)
  10036. (while words
  10037. (setq line (pop words))
  10038. (while (and words (< (+ (length line) (length (car words))) width))
  10039. (setq line (concat line " " (pop words))))
  10040. (setq lines (push line lines)))
  10041. (nreverse lines)))
  10042. (defun org-split-string (string &optional separators)
  10043. "Splits STRING into substrings at SEPARATORS.
  10044. No empty strings are returned if there are matches at the beginning
  10045. and end of string."
  10046. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  10047. (start 0)
  10048. notfirst
  10049. (list nil))
  10050. (while (and (string-match rexp string
  10051. (if (and notfirst
  10052. (= start (match-beginning 0))
  10053. (< start (length string)))
  10054. (1+ start) start))
  10055. (< (match-beginning 0) (length string)))
  10056. (setq notfirst t)
  10057. (or (eq (match-beginning 0) 0)
  10058. (and (eq (match-beginning 0) (match-end 0))
  10059. (eq (match-beginning 0) start))
  10060. (setq list
  10061. (cons (substring string start (match-beginning 0))
  10062. list)))
  10063. (setq start (match-end 0)))
  10064. (or (eq start (length string))
  10065. (setq list
  10066. (cons (substring string start)
  10067. list)))
  10068. (nreverse list)))
  10069. (defun org-table-map-tables (function)
  10070. "Apply FUNCTION to the start of all tables in the buffer."
  10071. (save-excursion
  10072. (save-restriction
  10073. (widen)
  10074. (goto-char (point-min))
  10075. (while (re-search-forward org-table-any-line-regexp nil t)
  10076. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  10077. (beginning-of-line 1)
  10078. (if (looking-at org-table-line-regexp)
  10079. (save-excursion (funcall function)))
  10080. (re-search-forward org-table-any-border-regexp nil 1))))
  10081. (message "Mapping tables: done"))
  10082. (defun org-table-sum (&optional beg end nlast)
  10083. "Sum numbers in region of current table column.
  10084. The result will be displayed in the echo area, and will be available
  10085. as kill to be inserted with \\[yank].
  10086. If there is an active region, it is interpreted as a rectangle and all
  10087. numbers in that rectangle will be summed. If there is no active
  10088. region and point is located in a table column, sum all numbers in that
  10089. column.
  10090. If at least one number looks like a time HH:MM or HH:MM:SS, all other
  10091. numbers are assumed to be times as well (in decimal hours) and the
  10092. numbers are added as such.
  10093. If NLAST is a number, only the NLAST fields will actually be summed."
  10094. (interactive)
  10095. (save-excursion
  10096. (let (col (timecnt 0) diff h m s org-table-clip)
  10097. (cond
  10098. ((and beg end)) ; beg and end given explicitly
  10099. ((org-region-active-p)
  10100. (setq beg (region-beginning) end (region-end)))
  10101. (t
  10102. (setq col (org-table-current-column))
  10103. (goto-char (org-table-begin))
  10104. (unless (re-search-forward "^[ \t]*|[^-]" nil t)
  10105. (error "No table data"))
  10106. (org-table-goto-column col)
  10107. ;not needed? (skip-chars-backward "^|")
  10108. (setq beg (point))
  10109. (goto-char (org-table-end))
  10110. (unless (re-search-backward "^[ \t]*|[^-]" nil t)
  10111. (error "No table data"))
  10112. (org-table-goto-column col)
  10113. ;not needed? (skip-chars-forward "^|")
  10114. (setq end (point))))
  10115. (let* ((items (apply 'append (org-table-copy-region beg end)))
  10116. (items1 (cond ((not nlast) items)
  10117. ((>= nlast (length items)) items)
  10118. (t (setq items (reverse items))
  10119. (setcdr (nthcdr (1- nlast) items) nil)
  10120. (nreverse items))))
  10121. (numbers (delq nil (mapcar 'org-table-get-number-for-summing
  10122. items1)))
  10123. (res (apply '+ numbers))
  10124. (sres (if (= timecnt 0)
  10125. (format "%g" res)
  10126. (setq diff (* 3600 res)
  10127. h (floor (/ diff 3600)) diff (mod diff 3600)
  10128. m (floor (/ diff 60)) diff (mod diff 60)
  10129. s diff)
  10130. (format "%d:%02d:%02d" h m s))))
  10131. (kill-new sres)
  10132. (if (interactive-p)
  10133. (message "%s"
  10134. (substitute-command-keys
  10135. (format "Sum of %d items: %-20s (\\[yank] will insert result into buffer)"
  10136. (length numbers) sres))))
  10137. sres))))
  10138. (defun org-table-get-number-for-summing (s)
  10139. (let (n)
  10140. (if (string-match "^ *|? *" s)
  10141. (setq s (replace-match "" nil nil s)))
  10142. (if (string-match " *|? *$" s)
  10143. (setq s (replace-match "" nil nil s)))
  10144. (setq n (string-to-number s))
  10145. (cond
  10146. ((and (string-match "0" s)
  10147. (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
  10148. ((string-match "\\`[ \t]+\\'" s) nil)
  10149. ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
  10150. (let ((h (string-to-number (or (match-string 1 s) "0")))
  10151. (m (string-to-number (or (match-string 2 s) "0")))
  10152. (s (string-to-number (or (match-string 4 s) "0"))))
  10153. (if (boundp 'timecnt) (setq timecnt (1+ timecnt)))
  10154. (* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
  10155. ((equal n 0) nil)
  10156. (t n))))
  10157. (defun org-table-get-vertical-vector (desc &optional tbeg col)
  10158. "Get a calc vector from a column, accorting to descriptor DESC.
  10159. Optional arguments TBEG and COL can give the beginning of the table and
  10160. the current column, to avoid unnecessary parsing."
  10161. (save-excursion
  10162. (or tbeg (setq tbeg (org-table-begin)))
  10163. (or col (setq col (org-table-current-column)))
  10164. (let (beg end nn n n1 n2 l (thisline (org-current-line)) hline-list)
  10165. (cond
  10166. ((string-match "\\(I+\\)\\(-\\(I+\\)\\)?" desc)
  10167. (setq n1 (- (match-end 1) (match-beginning 1)))
  10168. (if (match-beginning 3)
  10169. (setq n2 (- (match-end 2) (match-beginning 3))))
  10170. (setq n (if n2 (max n1 n2) n1))
  10171. (setq n1 (if n2 (min n1 n2)))
  10172. (setq nn n)
  10173. (while (and (> nn 0)
  10174. (re-search-backward org-table-hline-regexp tbeg t))
  10175. (push (org-current-line) hline-list)
  10176. (setq nn (1- nn)))
  10177. (setq hline-list (nreverse hline-list))
  10178. (goto-line (nth (1- n) hline-list))
  10179. (when (re-search-forward org-table-dataline-regexp)
  10180. (org-table-goto-column col)
  10181. (setq beg (point)))
  10182. (goto-line (if n1 (nth (1- n1) hline-list) thisline))
  10183. (when (re-search-backward org-table-dataline-regexp)
  10184. (org-table-goto-column col)
  10185. (setq end (point)))
  10186. (setq l (apply 'append (org-table-copy-region beg end)))
  10187. (concat "[" (mapconcat (lambda (x) (setq x (org-trim x))
  10188. (if (equal x "") "0" x))
  10189. l ",") "]"))
  10190. ((string-match "\\([0-9]+\\)-\\([0-9]+\\)" desc)
  10191. (setq n1 (string-to-number (match-string 1 desc))
  10192. n2 (string-to-number (match-string 2 desc)))
  10193. (beginning-of-line 1)
  10194. (save-excursion
  10195. (when (re-search-backward org-table-dataline-regexp tbeg t n1)
  10196. (org-table-goto-column col)
  10197. (setq beg (point))))
  10198. (when (re-search-backward org-table-dataline-regexp tbeg t n2)
  10199. (org-table-goto-column col)
  10200. (setq end (point)))
  10201. (setq l (apply 'append (org-table-copy-region beg end)))
  10202. (concat "[" (mapconcat
  10203. (lambda (x) (setq x (org-trim x))
  10204. (if (equal x "") "0" x))
  10205. l ",") "]"))
  10206. ((string-match "\\([0-9]+\\)" desc)
  10207. (beginning-of-line 1)
  10208. (when (re-search-backward org-table-dataline-regexp tbeg t
  10209. (string-to-number (match-string 0 desc)))
  10210. (org-table-goto-column col)
  10211. (org-trim (org-table-get-field))))))))
  10212. (defvar org-table-formula-history nil)
  10213. (defvar org-table-column-names nil
  10214. "Alist with column names, derived from the `!' line.")
  10215. (defvar org-table-column-name-regexp nil
  10216. "Regular expression matching the current column names.")
  10217. (defvar org-table-local-parameters nil
  10218. "Alist with parameter names, derived from the `$' line.")
  10219. (defvar org-table-named-field-locations nil
  10220. "Alist with locations of named fields.")
  10221. (defun org-table-get-formula (&optional equation named)
  10222. "Read a formula from the minibuffer, offer stored formula as default."
  10223. (let* ((name (car (rassoc (list (org-current-line)
  10224. (org-table-current-column))
  10225. org-table-named-field-locations)))
  10226. (scol (if named
  10227. (if name name
  10228. (error "Not in a named field"))
  10229. (int-to-string (org-table-current-column))))
  10230. (dummy (and name (not named)
  10231. (not (y-or-n-p "Replace named-field formula with column equation? " ))
  10232. (error "Abort")))
  10233. (org-table-may-need-update nil)
  10234. (stored-list (org-table-get-stored-formulas))
  10235. (stored (cdr (assoc scol stored-list)))
  10236. (eq (cond
  10237. ((and stored equation (string-match "^ *=? *$" equation))
  10238. stored)
  10239. ((stringp equation)
  10240. equation)
  10241. (t (read-string
  10242. (format "%s formula $%s=" (if named "Field" "Column") scol)
  10243. (or stored "") 'org-table-formula-history
  10244. ;stored
  10245. ))))
  10246. mustsave)
  10247. (when (not (string-match "\\S-" eq))
  10248. ;; remove formula
  10249. (setq stored-list (delq (assoc scol stored-list) stored-list))
  10250. (org-table-store-formulas stored-list)
  10251. (error "Formula removed"))
  10252. (if (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
  10253. (if (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
  10254. (if (and name (not named))
  10255. ;; We set the column equation, delete the named one.
  10256. (setq stored-list (delq (assoc name stored-list) stored-list)
  10257. mustsave t))
  10258. (if stored
  10259. (setcdr (assoc scol stored-list) eq)
  10260. (setq stored-list (cons (cons scol eq) stored-list)))
  10261. (if (or mustsave (not (equal stored eq)))
  10262. (org-table-store-formulas stored-list))
  10263. eq))
  10264. (defun org-table-store-formulas (alist)
  10265. "Store the list of formulas below the current table."
  10266. (setq alist (sort alist (lambda (a b) (string< (car a) (car b)))))
  10267. (save-excursion
  10268. (goto-char (org-table-end))
  10269. (if (looking-at "\\([ \t]*\n\\)*#\\+TBLFM:.*\n?")
  10270. (delete-region (point) (match-end 0)))
  10271. (insert "#+TBLFM: "
  10272. (mapconcat (lambda (x)
  10273. (concat "$" (car x) "=" (cdr x)))
  10274. alist "::")
  10275. "\n")))
  10276. (defun org-table-get-stored-formulas ()
  10277. "Return an alist with the stored formulas directly after current table."
  10278. (interactive)
  10279. (let (scol eq eq-alist strings string seen)
  10280. (save-excursion
  10281. (goto-char (org-table-end))
  10282. (when (looking-at "\\([ \t]*\n\\)*#\\+TBLFM: *\\(.*\\)")
  10283. (setq strings (org-split-string (match-string 2) " *:: *"))
  10284. (while (setq string (pop strings))
  10285. (when (string-match "\\$\\([a-zA-Z0-9]+\\) *= *\\(.*[^ \t]\\)" string)
  10286. (setq scol (match-string 1 string)
  10287. eq (match-string 2 string)
  10288. eq-alist (cons (cons scol eq) eq-alist))
  10289. (if (member scol seen)
  10290. (error "Double definition `$%s=' in TBLFM line, please fix by hand" scol)
  10291. (push scol seen))))))
  10292. (nreverse eq-alist)))
  10293. (defun org-table-modify-formulas (action &rest columns)
  10294. "Modify the formulas stored below the current table.
  10295. ACTION can be `remove', `insert', `swap'. For `swap', two column numbers are
  10296. expected, for the other actions only a single column number is needed."
  10297. (let ((list (org-table-get-stored-formulas))
  10298. (nmax (length (org-split-string
  10299. (buffer-substring (point-at-bol) (point-at-eol))
  10300. "|")))
  10301. col col1 col2 scol si sc1 sc2)
  10302. (cond
  10303. ((null list)) ; No action needed if there are no stored formulas
  10304. ((eq action 'remove)
  10305. (setq col (car columns)
  10306. scol (int-to-string col))
  10307. (org-table-replace-in-formulas list scol "INVALID")
  10308. (if (assoc scol list) (setq list (delq (assoc scol list) list)))
  10309. (loop for i from (1+ col) upto nmax by 1 do
  10310. (setq si (int-to-string i))
  10311. (org-table-replace-in-formulas list si (int-to-string (1- i)))
  10312. (if (assoc si list) (setcar (assoc si list)
  10313. (int-to-string (1- i))))))
  10314. ((eq action 'insert)
  10315. (setq col (car columns))
  10316. (loop for i from nmax downto col by 1 do
  10317. (setq si (int-to-string i))
  10318. (org-table-replace-in-formulas list si (int-to-string (1+ i)))
  10319. (if (assoc si list) (setcar (assoc si list)
  10320. (int-to-string (1+ i))))))
  10321. ((eq action 'swap)
  10322. (setq col1 (car columns) col2 (nth 1 columns)
  10323. sc1 (int-to-string col1) sc2 (int-to-string col2))
  10324. ;; Hopefully, ZqZtZ will never be a name in a table
  10325. (org-table-replace-in-formulas list sc1 "ZqZtZ")
  10326. (org-table-replace-in-formulas list sc2 sc1)
  10327. (org-table-replace-in-formulas list "ZqZtZ" sc2)
  10328. (if (assoc sc1 list) (setcar (assoc sc1 list) "ZqZtZ"))
  10329. (if (assoc sc2 list) (setcar (assoc sc2 list) sc1))
  10330. (if (assoc "ZqZtZ" list) (setcar (assoc "ZqZtZ" list) sc2)))
  10331. (t (error "Invalid action in `org-table-modify-formulas'")))
  10332. (if list (org-table-store-formulas list))))
  10333. (defun org-table-replace-in-formulas (list s1 s2)
  10334. (let (elt re s)
  10335. (setq s1 (concat "$" (if (integerp s1) (int-to-string s1) s1))
  10336. s2 (concat "$" (if (integerp s2) (int-to-string s2) s2))
  10337. re (concat (regexp-quote s1) "\\>"))
  10338. (while (setq elt (pop list))
  10339. (setq s (cdr elt))
  10340. (while (string-match re s)
  10341. (setq s (replace-match s2 t t s)))
  10342. (setcdr elt s))))
  10343. (defun org-table-get-specials ()
  10344. "Get the column names and local parameters for this table."
  10345. (save-excursion
  10346. (let ((beg (org-table-begin)) (end (org-table-end))
  10347. names name fields fields1 field cnt c v line col)
  10348. (setq org-table-column-names nil
  10349. org-table-local-parameters nil
  10350. org-table-named-field-locations nil)
  10351. (goto-char beg)
  10352. (when (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t)
  10353. (setq names (org-split-string (match-string 1) " *| *")
  10354. cnt 1)
  10355. (while (setq name (pop names))
  10356. (setq cnt (1+ cnt))
  10357. (if (string-match "^[a-zA-Z][a-zA-Z0-9]*$" name)
  10358. (push (cons name (int-to-string cnt)) org-table-column-names))))
  10359. (setq org-table-column-names (nreverse org-table-column-names))
  10360. (setq org-table-column-name-regexp
  10361. (concat "\\$\\(" (mapconcat 'car org-table-column-names "\\|") "\\)\\>"))
  10362. (goto-char beg)
  10363. (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t)
  10364. (setq fields (org-split-string (match-string 1) " *| *"))
  10365. (while (setq field (pop fields))
  10366. (if (string-match "^\\([a-zA-Z][a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field)
  10367. (push (cons (match-string 1 field) (match-string 2 field))
  10368. org-table-local-parameters))))
  10369. (goto-char beg)
  10370. (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t)
  10371. (setq c (match-string 1)
  10372. fields (org-split-string (match-string 2) " *| *"))
  10373. (save-excursion
  10374. (beginning-of-line (if (equal c "_") 2 0))
  10375. (setq line (org-current-line) col 1)
  10376. (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
  10377. (setq fields1 (org-split-string (match-string 1) " *| *"))))
  10378. (while (and fields1 (setq field (pop fields)))
  10379. (setq v (pop fields1) col (1+ col))
  10380. (when (and (stringp field) (stringp v)
  10381. (string-match "^[a-zA-Z][a-zA-Z0-9]*$" field))
  10382. (push (cons field v) org-table-local-parameters)
  10383. (push (list field line col) org-table-named-field-locations)))))))
  10384. (defun org-this-word ()
  10385. ;; Get the current word
  10386. (save-excursion
  10387. (let ((beg (progn (skip-chars-backward "^ \t\n") (point)))
  10388. (end (progn (skip-chars-forward "^ \t\n") (point))))
  10389. (buffer-substring-no-properties beg end))))
  10390. (defun org-table-maybe-eval-formula ()
  10391. "Check if the current field starts with \"=\" or \":=\".
  10392. If yes, store the formula and apply it."
  10393. ;; We already know we are in a table. Get field will only return a formula
  10394. ;; when appropriate. It might return a separator line, but no problem.
  10395. (when org-table-formula-evaluate-inline
  10396. (let* ((field (org-trim (or (org-table-get-field) "")))
  10397. named eq)
  10398. (when (string-match "^:?=\\(.*\\)" field)
  10399. (setq named (equal (string-to-char field) ?:)
  10400. eq (match-string 1 field))
  10401. (if (fboundp 'calc-eval)
  10402. (org-table-eval-formula (if named '(4) nil) eq))))))
  10403. (defvar org-recalc-commands nil
  10404. "List of commands triggering the recalculation of a line.
  10405. Will be filled automatically during use.")
  10406. (defvar org-recalc-marks
  10407. '((" " . "Unmarked: no special line, no automatic recalculation")
  10408. ("#" . "Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
  10409. ("*" . "Recalculate only when entire table is recalculated with `C-u C-c *'")
  10410. ("!" . "Column name definition line. Reference in formula as $name.")
  10411. ("$" . "Parameter definition line name=value. Reference in formula as $name.")
  10412. ("_" . "Names for values in row below this one.")
  10413. ("^" . "Names for values in row above this one.")))
  10414. (defun org-table-rotate-recalc-marks (&optional newchar)
  10415. "Rotate the recalculation mark in the first column.
  10416. If in any row, the first field is not consistent with a mark,
  10417. insert a new column for the markers.
  10418. When there is an active region, change all the lines in the region,
  10419. after prompting for the marking character.
  10420. After each change, a message will be displayed indicating the meaning
  10421. of the new mark."
  10422. (interactive)
  10423. (unless (org-at-table-p) (error "Not at a table"))
  10424. (let* ((marks (append (mapcar 'car org-recalc-marks) '(" ")))
  10425. (beg (org-table-begin))
  10426. (end (org-table-end))
  10427. (l (org-current-line))
  10428. (l1 (if (org-region-active-p) (org-current-line (region-beginning))))
  10429. (l2 (if (org-region-active-p) (org-current-line (region-end))))
  10430. (have-col
  10431. (save-excursion
  10432. (goto-char beg)
  10433. (not (re-search-forward "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" end t))))
  10434. (col (org-table-current-column))
  10435. (forcenew (car (assoc newchar org-recalc-marks)))
  10436. epos new)
  10437. (when l1
  10438. (message "Change region to what mark? Type # * ! $ or SPC: ")
  10439. (setq newchar (char-to-string (read-char-exclusive))
  10440. forcenew (car (assoc newchar org-recalc-marks))))
  10441. (if (and newchar (not forcenew))
  10442. (error "Invalid NEWCHAR `%s' in `org-table-rotate-recalc-marks'"
  10443. newchar))
  10444. (if l1 (goto-line l1))
  10445. (save-excursion
  10446. (beginning-of-line 1)
  10447. (unless (looking-at org-table-dataline-regexp)
  10448. (error "Not at a table data line")))
  10449. (unless have-col
  10450. (org-table-goto-column 1)
  10451. (org-table-insert-column)
  10452. (org-table-goto-column (1+ col)))
  10453. (setq epos (point-at-eol))
  10454. (save-excursion
  10455. (beginning-of-line 1)
  10456. (org-table-get-field
  10457. 1 (if (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")
  10458. (concat " "
  10459. (setq new (or forcenew
  10460. (cadr (member (match-string 1) marks))))
  10461. " ")
  10462. " # ")))
  10463. (if (and l1 l2)
  10464. (progn
  10465. (goto-line l1)
  10466. (while (progn (beginning-of-line 2) (not (= (org-current-line) l2)))
  10467. (and (looking-at org-table-dataline-regexp)
  10468. (org-table-get-field 1 (concat " " new " "))))
  10469. (goto-line l1)))
  10470. (if (not (= epos (point-at-eol))) (org-table-align))
  10471. (goto-line l)
  10472. (and (interactive-p) (message (cdr (assoc new org-recalc-marks))))))
  10473. (defun org-table-maybe-recalculate-line ()
  10474. "Recompute the current line if marked for it, and if we haven't just done it."
  10475. (interactive)
  10476. (and org-table-allow-automatic-line-recalculation
  10477. (not (and (memq last-command org-recalc-commands)
  10478. (equal org-last-recalc-line (org-current-line))))
  10479. (save-excursion (beginning-of-line 1)
  10480. (looking-at org-table-auto-recalculate-regexp))
  10481. (fboundp 'calc-eval)
  10482. (org-table-recalculate) t))
  10483. (defvar org-table-formula-debug nil
  10484. "Non-nil means, debug table formulas.
  10485. When nil, simply write \"#ERROR\" in corrupted fields.")
  10486. (defvar modes)
  10487. (defsubst org-set-calc-mode (var &optional value)
  10488. (if (stringp var)
  10489. (setq var (assoc var '(("D" calc-angle-mode deg)
  10490. ("R" calc-angle-mode rad)
  10491. ("F" calc-prefer-frac t)
  10492. ("S" calc-symbolic-mode t)))
  10493. value (nth 2 var) var (nth 1 var)))
  10494. (if (memq var modes)
  10495. (setcar (cdr (memq var modes)) value)
  10496. (cons var (cons value modes)))
  10497. modes)
  10498. (defun org-table-eval-formula (&optional arg equation
  10499. suppress-align suppress-const
  10500. suppress-store)
  10501. "Replace the table field value at the cursor by the result of a calculation.
  10502. This function makes use of Dave Gillespie's Calc package, in my view the
  10503. most exciting program ever written for GNU Emacs. So you need to have Calc
  10504. installed in order to use this function.
  10505. In a table, this command replaces the value in the current field with the
  10506. result of a formula. It also installs the formula as the \"current\" column
  10507. formula, by storing it in a special line below the table. When called
  10508. with a `C-u' prefix, the current field must ba a named field, and the
  10509. formula is installed as valid in only this specific field.
  10510. When called, the command first prompts for a formula, which is read in
  10511. the minibuffer. Previously entered formulas are available through the
  10512. history list, and the last used formula is offered as a default.
  10513. These stored formulas are adapted correctly when moving, inserting, or
  10514. deleting columns with the corresponding commands.
  10515. The formula can be any algebraic expression understood by the Calc package.
  10516. For details, see the Org-mode manual.
  10517. This function can also be called from Lisp programs and offers
  10518. additional arguments: EQUATION can be the formula to apply. If this
  10519. argument is given, the user will not be prompted. SUPPRESS-ALIGN is
  10520. used to speed-up recursive calls by by-passing unnecessary aligns.
  10521. SUPPRESS-CONST suppresses the interpretation of constants in the
  10522. formula, assuming that this has been done already outside the function.
  10523. SUPPRESS-STORE means the formula should not be stored, either because
  10524. it is already stored, or because it is a modified equation that should
  10525. not overwrite the stored one."
  10526. (interactive "P")
  10527. (require 'calc)
  10528. (org-table-check-inside-data-field)
  10529. (org-table-get-specials)
  10530. (let* (fields
  10531. (ndown (if (integerp arg) arg 1))
  10532. (org-table-automatic-realign nil)
  10533. (case-fold-search nil)
  10534. (down (> ndown 1))
  10535. (formula (if (and equation suppress-store)
  10536. equation
  10537. (org-table-get-formula equation (equal arg '(4)))))
  10538. (n0 (org-table-current-column))
  10539. (modes (copy-sequence org-calc-default-modes))
  10540. n form fmt x ev orig c lispp)
  10541. ;; Parse the format string. Since we have a lot of modes, this is
  10542. ;; a lot of work. However, I think calc still uses most of the time.
  10543. (if (string-match ";" formula)
  10544. (let ((tmp (org-split-string formula ";")))
  10545. (setq formula (car tmp)
  10546. fmt (concat (cdr (assoc "%" org-table-local-parameters))
  10547. (nth 1 tmp)))
  10548. (while (string-match "[pnfse]\\(-?[0-9]+\\)" fmt)
  10549. (setq c (string-to-char (match-string 1 fmt))
  10550. n (string-to-number (or (match-string 1 fmt) "")))
  10551. (if (= c ?p) (setq modes (org-set-calc-mode 'calc-internal-prec n))
  10552. (setq modes (org-set-calc-mode
  10553. 'calc-float-format
  10554. (list (cdr (assoc c '((?n . float) (?f . fix)
  10555. (?s . sci) (?e . eng))))
  10556. n))))
  10557. (setq fmt (replace-match "" t t fmt)))
  10558. (while (string-match "[DRFS]" fmt)
  10559. (setq modes (org-set-calc-mode (match-string 0 fmt)))
  10560. (setq fmt (replace-match "" t t fmt)))
  10561. (unless (string-match "\\S-" fmt)
  10562. (setq fmt nil))))
  10563. (if (and (not suppress-const) org-table-formula-use-constants)
  10564. (setq formula (org-table-formula-substitute-names formula)))
  10565. (setq orig (or (get-text-property 1 :orig-formula formula) "?"))
  10566. (while (> ndown 0)
  10567. (setq fields (org-split-string
  10568. (buffer-substring
  10569. (point-at-bol) (point-at-eol)) " *| *"))
  10570. (if org-table-formula-numbers-only
  10571. (setq fields (mapcar
  10572. (lambda (x) (number-to-string (string-to-number x)))
  10573. fields)))
  10574. (setq ndown (1- ndown))
  10575. (setq form (copy-sequence formula)
  10576. lispp (equal (substring form 0 2) "'("))
  10577. ;; Insert the references to fields in same row
  10578. (while (string-match "\\$\\([0-9]+\\)?" form)
  10579. (setq n (if (match-beginning 1)
  10580. (string-to-number (match-string 1 form))
  10581. n0)
  10582. x (nth (1- n) fields))
  10583. (unless x (error "Invalid field specifier \"%s\""
  10584. (match-string 0 form)))
  10585. (if (equal x "") (setq x "0"))
  10586. (setq form (replace-match
  10587. (if lispp x (concat "(" x ")"))
  10588. t t form)))
  10589. ;; Insert ranges in current column
  10590. (while (string-match "\\&[-I0-9]+" form)
  10591. (setq form (replace-match
  10592. (save-match-data
  10593. (org-table-get-vertical-vector (match-string 0 form)
  10594. nil n0))
  10595. t t form)))
  10596. (if lispp
  10597. (setq ev (eval (eval (read form)))
  10598. ev (if (numberp ev) (number-to-string ev) ev))
  10599. (setq ev (calc-eval (cons form modes)
  10600. (if org-table-formula-numbers-only 'num))))
  10601. (when org-table-formula-debug
  10602. (with-output-to-temp-buffer "*Help*"
  10603. (princ (format "Substitution history of formula
  10604. Orig: %s
  10605. $xyz-> %s
  10606. $1-> %s\n" orig formula form))
  10607. (if (listp ev)
  10608. (princ (format " %s^\nError: %s"
  10609. (make-string (car ev) ?\-) (nth 1 ev)))
  10610. (princ (format "Result: %s\nFormat: %s\nFinal: %s"
  10611. ev (or fmt "NONE")
  10612. (if fmt (format fmt (string-to-number ev)) ev)))))
  10613. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  10614. (unless (and (interactive-p) (not ndown))
  10615. (unless (let (inhibit-redisplay)
  10616. (y-or-n-p "Debugging Formula. Continue to next? "))
  10617. (org-table-align)
  10618. (error "Abort"))
  10619. (delete-window (get-buffer-window "*Help*"))
  10620. (message "")))
  10621. (if (listp ev) (setq fmt nil ev "#ERROR"))
  10622. (org-table-justify-field-maybe
  10623. (if fmt (format fmt (string-to-number ev)) ev))
  10624. (if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
  10625. (call-interactively 'org-return)
  10626. (setq ndown 0)))
  10627. (and down (org-table-maybe-recalculate-line))
  10628. (or suppress-align (and org-table-may-need-update
  10629. (org-table-align)))))
  10630. (defun org-table-recalculate (&optional all noalign)
  10631. "Recalculate the current table line by applying all stored formulas.
  10632. With prefix arg ALL, do this for all lines in the table."
  10633. (interactive "P")
  10634. (or (memq this-command org-recalc-commands)
  10635. (setq org-recalc-commands (cons this-command org-recalc-commands)))
  10636. (unless (org-at-table-p) (error "Not at a table"))
  10637. (org-table-get-specials)
  10638. (let* ((eqlist (sort (org-table-get-stored-formulas)
  10639. (lambda (a b) (string< (car a) (car b)))))
  10640. (inhibit-redisplay t)
  10641. (line-re org-table-dataline-regexp)
  10642. (thisline (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
  10643. (thiscol (org-table-current-column))
  10644. beg end entry eqlnum eqlname eql (cnt 0) eq a name)
  10645. ;; Insert constants in all formulas
  10646. (setq eqlist
  10647. (mapcar (lambda (x)
  10648. (setcdr x (org-table-formula-substitute-names (cdr x)))
  10649. x)
  10650. eqlist))
  10651. ;; Split the equation list
  10652. (while (setq eq (pop eqlist))
  10653. (if (<= (string-to-char (car eq)) ?9)
  10654. (push eq eqlnum)
  10655. (push eq eqlname)))
  10656. (setq eqlnum (nreverse eqlnum) eqlname (nreverse eqlname))
  10657. (if all
  10658. (progn
  10659. (setq end (move-marker (make-marker) (1+ (org-table-end))))
  10660. (goto-char (setq beg (org-table-begin)))
  10661. (if (re-search-forward org-table-calculate-mark-regexp end t)
  10662. ;; This is a table with marked lines, only compute selected lines
  10663. (setq line-re org-table-recalculate-regexp)
  10664. ;; Move forward to the first non-header line
  10665. (if (and (re-search-forward org-table-dataline-regexp end t)
  10666. (re-search-forward org-table-hline-regexp end t)
  10667. (re-search-forward org-table-dataline-regexp end t))
  10668. (setq beg (match-beginning 0))
  10669. nil))) ;; just leave beg where it is
  10670. (setq beg (point-at-bol)
  10671. end (move-marker (make-marker) (1+ (point-at-eol)))))
  10672. (goto-char beg)
  10673. (and all (message "Re-applying formulas to full table..."))
  10674. (while (re-search-forward line-re end t)
  10675. (unless (string-match "^ *[_^!$] *$" (org-table-get-field 1))
  10676. ;; Unprotected line, recalculate
  10677. (and all (message "Re-applying formulas to full table...(line %d)"
  10678. (setq cnt (1+ cnt))))
  10679. (setq org-last-recalc-line (org-current-line))
  10680. (setq eql eqlnum)
  10681. (while (setq entry (pop eql))
  10682. (goto-line org-last-recalc-line)
  10683. (org-table-goto-column (string-to-number (car entry)) nil 'force)
  10684. (org-table-eval-formula nil (cdr entry) 'noalign 'nocst 'nostore))))
  10685. (goto-line thisline)
  10686. (org-table-goto-column thiscol)
  10687. (or noalign (and org-table-may-need-update (org-table-align))
  10688. (and all (message "Re-applying formulas to %d lines...done" cnt)))
  10689. ;; Now do the names fields
  10690. (while (setq eq (pop eqlname))
  10691. (setq name (car eq)
  10692. a (assoc name org-table-named-field-locations))
  10693. (when a
  10694. (message "Re-applying formula to named field: %s" name)
  10695. (goto-line (nth 1 a))
  10696. (org-table-goto-column (nth 2 a))
  10697. (org-table-eval-formula nil (cdr eq) 'noalign 'nocst 'nostore)))
  10698. ;; back to initial position
  10699. (goto-line thisline)
  10700. (org-table-goto-column thiscol)
  10701. (or noalign (and org-table-may-need-update (org-table-align))
  10702. (and all (message "Re-applying formulas...done")))))
  10703. (defun org-table-formula-substitute-names (f)
  10704. "Replace $const with values in string F."
  10705. (let ((start 0) a n1 n2 nn1 nn2 s (f1 f))
  10706. ;; First, check for column names
  10707. (while (setq start (string-match org-table-column-name-regexp f start))
  10708. (setq start (1+ start))
  10709. (setq a (assoc (match-string 1 f) org-table-column-names))
  10710. (setq f (replace-match (concat "$" (cdr a)) t t f)))
  10711. ;; Expand ranges to vectors
  10712. (while (string-match "\\$\\([0-9]+\\)\\.\\.\\.?\\$\\([0-9]+\\)" f)
  10713. (setq n1 (string-to-number (match-string 1 f))
  10714. n2 (string-to-number (match-string 2 f))
  10715. nn1 (1+ (min n1 n2)) nn2 (max n1 n2)
  10716. s (concat "[($" (number-to-string (1- nn1)) ")"))
  10717. (loop for i from nn1 upto nn2 do
  10718. (setq s (concat s ",($" (int-to-string i) ")")))
  10719. (setq s (concat s "]"))
  10720. (if (< n2 n1) (setq s (concat "rev(" s ")")))
  10721. (setq f (replace-match s t t f)))
  10722. ;; Parameters and constants
  10723. (setq start 0)
  10724. (while (setq start (string-match "\\$\\([a-zA-Z][a-zA-Z0-9]*\\)" f start))
  10725. (setq start (1+ start))
  10726. (if (setq a (save-match-data
  10727. (org-table-get-constant (match-string 1 f))))
  10728. (setq f (replace-match (concat "(" a ")") t t f))))
  10729. (if org-table-formula-debug
  10730. (put-text-property 0 (length f) :orig-formula f1 f))
  10731. f))
  10732. (defun org-table-get-constant (const)
  10733. "Find the value for a parameter or constant in a formula.
  10734. Parameters get priority."
  10735. (or (cdr (assoc const org-table-local-parameters))
  10736. (cdr (assoc const org-table-formula-constants))
  10737. (and (fboundp 'constants-get) (constants-get const))
  10738. "#UNDEFINED_NAME"))
  10739. (defvar org-edit-formulas-map (make-sparse-keymap))
  10740. (define-key org-edit-formulas-map "\C-c\C-c" 'org-finish-edit-formulas)
  10741. (define-key org-edit-formulas-map "\C-c\C-q" 'org-abort-edit-formulas)
  10742. (define-key org-edit-formulas-map "\C-c?" 'org-show-variable)
  10743. (defvar org-pos)
  10744. (defun org-table-edit-formulas ()
  10745. "Edit the formulas of the current table in a separate buffer."
  10746. (interactive)
  10747. (unless (org-at-table-p)
  10748. (error "Not at a table"))
  10749. (org-table-get-specials)
  10750. (let ((eql (org-table-get-stored-formulas))
  10751. (pos (move-marker (make-marker) (point)))
  10752. (wc (current-window-configuration))
  10753. entry loc s)
  10754. (switch-to-buffer-other-window "*Edit Formulas*")
  10755. (erase-buffer)
  10756. (fundamental-mode)
  10757. (set (make-local-variable 'org-pos) pos)
  10758. (set (make-local-variable 'org-window-configuration) wc)
  10759. (use-local-map org-edit-formulas-map)
  10760. (setq s "# Edit formulas and finish with `C-c C-c'.
  10761. # Use `C-u C-c C-c' to also appy them immediately to the entire table.
  10762. # Use `C-c ?' to get information about $name at point.
  10763. # To cancel editing, press `C-c C-q'.\n")
  10764. (put-text-property 0 (length s) 'face 'font-lock-comment-face s)
  10765. (insert s)
  10766. (while (setq entry (pop eql))
  10767. (when (setq loc (assoc (car entry) org-table-named-field-locations))
  10768. (setq s (format "# Named formula, referring to column %d in line %d\n"
  10769. (nth 2 loc) (nth 1 loc)))
  10770. (put-text-property 0 (length s) 'face 'font-lock-comment-face s)
  10771. (insert s))
  10772. (setq s (concat "$" (car entry) "=" (cdr entry) "\n"))
  10773. (remove-text-properties 0 (length s) '(face nil) s)
  10774. (insert s))
  10775. (goto-char (point-min))
  10776. (message "Edit formulas and finish with `C-c C-c'.")))
  10777. (defun org-show-variable ()
  10778. "Show the location/value of the $ expression at point."
  10779. (interactive)
  10780. (let (var (pos org-pos) (win (selected-window)) e)
  10781. (save-excursion
  10782. (or (looking-at "\\$") (skip-chars-backward "$a-zA-Z0-9"))
  10783. (if (looking-at "\\$\\([a-zA-Z0-9]+\\)")
  10784. (setq var (match-string 1))
  10785. (error "No variable at point")))
  10786. (cond
  10787. ((setq e (assoc var org-table-named-field-locations))
  10788. (switch-to-buffer-other-window (marker-buffer pos))
  10789. (goto-line (nth 1 e))
  10790. (org-table-goto-column (nth 2 e))
  10791. (select-window win)
  10792. (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
  10793. ((setq e (assoc var org-table-column-names))
  10794. (switch-to-buffer-other-window (marker-buffer pos))
  10795. (goto-char pos)
  10796. (goto-char (org-table-begin))
  10797. (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
  10798. (org-table-end) t)
  10799. (progn
  10800. (goto-char (match-beginning 1))
  10801. (message "Named column (column %s)" (cdr e)))
  10802. (error "Column name not found"))
  10803. (select-window win))
  10804. ((string-match "^[0-9]$" var)
  10805. ;; column number
  10806. (switch-to-buffer-other-window (marker-buffer pos))
  10807. (goto-char pos)
  10808. (goto-char (org-table-begin))
  10809. (recenter 1)
  10810. (if (re-search-forward org-table-dataline-regexp
  10811. (org-table-end) t)
  10812. (progn
  10813. (goto-char (match-beginning 0))
  10814. (org-table-goto-column (string-to-number var))
  10815. (message "Column %s" var))
  10816. (error "Column name not found"))
  10817. (select-window win))
  10818. ((setq e (assoc var org-table-local-parameters))
  10819. (switch-to-buffer-other-window (marker-buffer pos))
  10820. (goto-char pos)
  10821. (goto-char (org-table-begin))
  10822. (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
  10823. (progn
  10824. (goto-char (match-beginning 1))
  10825. (message "Local parameter."))
  10826. (error "Parameter not found"))
  10827. (select-window win))
  10828. (t
  10829. (cond
  10830. ((setq e (assoc var org-table-formula-constants))
  10831. (message "Constant: $%s=%s in `org-table-formula-constants'." var (cdr e)))
  10832. ((setq e (and (fboundp 'constants-get) (constants-get var)))
  10833. (message "Constant: $%s=%s, retrieved from `constants.el'." var e))
  10834. (t (error "Undefined name $%s" var)))))))
  10835. (defun org-finish-edit-formulas (&optional arg)
  10836. "Parse the buffer for formula definitions and install them.
  10837. With prefix ARG, apply the new formulas to the table."
  10838. (interactive "P")
  10839. (let ((pos org-pos) eql)
  10840. (goto-char (point-min))
  10841. (while (re-search-forward "^\\$\\([a-zA-Z0-9]+\\) *= *\\(.*?\\) *$" nil t)
  10842. (push (cons (match-string 1) (match-string 2)) eql))
  10843. (set-window-configuration org-window-configuration)
  10844. (select-window (get-buffer-window (marker-buffer pos)))
  10845. (goto-char pos)
  10846. (unless (org-at-table-p)
  10847. (error "Lost table position - cannot install formulae"))
  10848. (org-table-store-formulas eql)
  10849. (move-marker pos nil)
  10850. (kill-buffer "*Edit Formulas*")
  10851. (if arg
  10852. (org-table-recalculate 'all)
  10853. (message "New formulas installed - press C-u C-c C-c to apply."))))
  10854. (defun org-abort-edit-formulas ()
  10855. "Abort editing formulas, without installing the changes."
  10856. (interactive)
  10857. (let ((pos org-pos))
  10858. (set-window-configuration org-window-configuration)
  10859. (select-window (get-buffer-window (marker-buffer pos)))
  10860. (goto-char pos)
  10861. (message "Formula editing aborted without installing changes")))
  10862. ;;; The orgtbl minor mode
  10863. ;; Define a minor mode which can be used in other modes in order to
  10864. ;; integrate the org-mode table editor.
  10865. ;; This is really a hack, because the org-mode table editor uses several
  10866. ;; keys which normally belong to the major mode, for example the TAB and
  10867. ;; RET keys. Here is how it works: The minor mode defines all the keys
  10868. ;; necessary to operate the table editor, but wraps the commands into a
  10869. ;; function which tests if the cursor is currently inside a table. If that
  10870. ;; is the case, the table editor command is executed. However, when any of
  10871. ;; those keys is used outside a table, the function uses `key-binding' to
  10872. ;; look up if the key has an associated command in another currently active
  10873. ;; keymap (minor modes, major mode, global), and executes that command.
  10874. ;; There might be problems if any of the keys used by the table editor is
  10875. ;; otherwise used as a prefix key.
  10876. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  10877. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  10878. ;; addresses this by checking explicitly for both bindings.
  10879. ;; The optimized version (see variable `orgtbl-optimized') takes over
  10880. ;; all keys which are bound to `self-insert-command' in the *global map*.
  10881. ;; Some modes bind other commands to simple characters, for example
  10882. ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
  10883. ;; active, this binding is ignored inside tables and replaced with a
  10884. ;; modified self-insert.
  10885. (defvar orgtbl-mode nil
  10886. "Variable controlling `orgtbl-mode', a minor mode enabling the `org-mode'
  10887. table editor in arbitrary modes.")
  10888. (make-variable-buffer-local 'orgtbl-mode)
  10889. (defvar orgtbl-mode-map (make-keymap)
  10890. "Keymap for `orgtbl-mode'.")
  10891. ;;;###autoload
  10892. (defun turn-on-orgtbl ()
  10893. "Unconditionally turn on `orgtbl-mode'."
  10894. (orgtbl-mode 1))
  10895. ;;;###autoload
  10896. (defun orgtbl-mode (&optional arg)
  10897. "The `org-mode' table editor as a minor mode for use in other modes."
  10898. (interactive)
  10899. (if (eq major-mode 'org-mode)
  10900. ;; Exit without error, in case some hook functions calls this
  10901. ;; by accident in org-mode.
  10902. (message "Orgtbl-mode is not useful in org-mode, command ignored")
  10903. (setq orgtbl-mode
  10904. (if arg (> (prefix-numeric-value arg) 0) (not orgtbl-mode)))
  10905. (if orgtbl-mode
  10906. (progn
  10907. (and (orgtbl-setup) (defun orgtbl-setup () nil))
  10908. ;; Make sure we are first in minor-mode-map-alist
  10909. (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
  10910. (and c (setq minor-mode-map-alist
  10911. (cons c (delq c minor-mode-map-alist)))))
  10912. (set (make-local-variable (quote org-table-may-need-update)) t)
  10913. (org-add-hook 'before-change-functions 'org-before-change-function
  10914. nil 'local)
  10915. (set (make-local-variable 'org-old-auto-fill-inhibit-regexp)
  10916. auto-fill-inhibit-regexp)
  10917. (set (make-local-variable 'auto-fill-inhibit-regexp)
  10918. (if auto-fill-inhibit-regexp
  10919. (concat "\\([ \t]*|\\|" auto-fill-inhibit-regexp)
  10920. "[ \t]*|"))
  10921. (org-add-to-invisibility-spec '(org-cwidth))
  10922. (easy-menu-add orgtbl-mode-menu)
  10923. (run-hooks 'orgtbl-mode-hook))
  10924. (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
  10925. (org-cleanup-narrow-column-properties)
  10926. (org-remove-from-invisibility-spec '(org-cwidth))
  10927. (remove-hook 'before-change-functions 'org-before-change-function t)
  10928. (easy-menu-remove orgtbl-mode-menu)
  10929. (force-mode-line-update 'all))))
  10930. (defun org-cleanup-narrow-column-properties ()
  10931. "Remove all properties related to narrow-column invisibility."
  10932. (let ((s 1))
  10933. (while (setq s (text-property-any s (point-max)
  10934. 'display org-narrow-column-arrow))
  10935. (remove-text-properties s (1+ s) '(display t)))
  10936. (setq s 1)
  10937. (while (setq s (text-property-any s (point-max) 'org-cwidth 1))
  10938. (remove-text-properties s (1+ s) '(org-cwidth t)))
  10939. (setq s 1)
  10940. (while (setq s (text-property-any s (point-max) 'invisible 'org-cwidth))
  10941. (remove-text-properties s (1+ s) '(invisible t)))))
  10942. ;; Install it as a minor mode.
  10943. (put 'orgtbl-mode :included t)
  10944. (put 'orgtbl-mode :menu-tag "Org Table Mode")
  10945. (add-minor-mode 'orgtbl-mode " OrgTbl" orgtbl-mode-map)
  10946. (defun orgtbl-make-binding (fun n &rest keys)
  10947. "Create a function for binding in the table minor mode.
  10948. FUN is the command to call inside a table. N is used to create a unique
  10949. command name. KEYS are keys that should be checked in for a command
  10950. to execute outside of tables."
  10951. (eval
  10952. (list 'defun
  10953. (intern (concat "orgtbl-hijacker-command-" (int-to-string n)))
  10954. '(arg)
  10955. (concat "In tables, run `" (symbol-name fun) "'.\n"
  10956. "Outside of tables, run the binding of `"
  10957. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  10958. "'.")
  10959. '(interactive "p")
  10960. (list 'if
  10961. '(org-at-table-p)
  10962. (list 'call-interactively (list 'quote fun))
  10963. (list 'let '(orgtbl-mode)
  10964. (list 'call-interactively
  10965. (append '(or)
  10966. (mapcar (lambda (k)
  10967. (list 'key-binding k))
  10968. keys)
  10969. '('orgtbl-error))))))))
  10970. (defun orgtbl-error ()
  10971. "Error when there is no default binding for a table key."
  10972. (interactive)
  10973. (error "This key is has no function outside tables"))
  10974. (defun orgtbl-setup ()
  10975. "Setup orgtbl keymaps."
  10976. (let ((nfunc 0)
  10977. (bindings
  10978. (list
  10979. '([(meta shift left)] org-table-delete-column)
  10980. '([(meta left)] org-table-move-column-left)
  10981. '([(meta right)] org-table-move-column-right)
  10982. '([(meta shift right)] org-table-insert-column)
  10983. '([(meta shift up)] org-table-kill-row)
  10984. '([(meta shift down)] org-table-insert-row)
  10985. '([(meta up)] org-table-move-row-up)
  10986. '([(meta down)] org-table-move-row-down)
  10987. '("\C-c\C-w" org-table-cut-region)
  10988. '("\C-c\M-w" org-table-copy-region)
  10989. '("\C-c\C-y" org-table-paste-rectangle)
  10990. '("\C-c-" org-table-insert-hline)
  10991. ; '([(shift tab)] org-table-previous-field)
  10992. '("\C-m" org-table-next-row)
  10993. (list (org-key 'S-return) 'org-table-copy-down)
  10994. '([(meta return)] org-table-wrap-region)
  10995. '("\C-c\C-q" org-table-wrap-region)
  10996. '("\C-c?" org-table-current-column)
  10997. '("\C-c " org-table-blank-field)
  10998. '("\C-c+" org-table-sum)
  10999. '("\C-c=" org-table-eval-formula)
  11000. '("\C-c'" org-table-edit-formulas)
  11001. '("\C-c`" org-table-edit-field)
  11002. '("\C-c*" org-table-recalculate)
  11003. '("\C-c|" org-table-create-or-convert-from-region)
  11004. '("\C-c^" org-table-sort-lines)
  11005. '([(control ?#)] org-table-rotate-recalc-marks)))
  11006. elt key fun cmd)
  11007. (while (setq elt (pop bindings))
  11008. (setq nfunc (1+ nfunc))
  11009. (setq key (car elt)
  11010. fun (nth 1 elt)
  11011. cmd (orgtbl-make-binding fun nfunc key))
  11012. (define-key orgtbl-mode-map key cmd))
  11013. ;; Special treatment needed for TAB and RET
  11014. (define-key orgtbl-mode-map [(return)]
  11015. (orgtbl-make-binding 'orgtbl-ret 100 [(return)] "\C-m"))
  11016. (define-key orgtbl-mode-map "\C-m"
  11017. (orgtbl-make-binding 'orgtbl-ret 101 "\C-m" [(return)]))
  11018. (define-key orgtbl-mode-map [(tab)]
  11019. (orgtbl-make-binding 'orgtbl-tab 102 [(tab)] "\C-i"))
  11020. (define-key orgtbl-mode-map "\C-i"
  11021. (orgtbl-make-binding 'orgtbl-tab 103 "\C-i" [(tab)])))
  11022. (define-key orgtbl-mode-map "\C-i"
  11023. (orgtbl-make-binding 'orgtbl-tab 104 [(shift tab)]))
  11024. (define-key orgtbl-mode-map "\C-c\C-c"
  11025. (orgtbl-make-binding 'org-ctrl-c-ctrl-c 105 "\C-c\C-c"))
  11026. (when orgtbl-optimized
  11027. ;; If the user wants maximum table support, we need to hijack
  11028. ;; some standard editing functions
  11029. (org-remap orgtbl-mode-map
  11030. 'self-insert-command 'orgtbl-self-insert-command
  11031. 'delete-char 'org-delete-char
  11032. 'delete-backward-char 'org-delete-backward-char)
  11033. (define-key orgtbl-mode-map "|" 'org-force-self-insert))
  11034. (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
  11035. '("OrgTbl"
  11036. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
  11037. ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
  11038. ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
  11039. ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
  11040. "--"
  11041. ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
  11042. ["Edit Field" org-table-edit-field :active (org-at-table-p) :keys "C-c ` "]
  11043. ["Copy Field from Above"
  11044. org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
  11045. "--"
  11046. ("Column"
  11047. ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
  11048. ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
  11049. ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
  11050. ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"]
  11051. "--"
  11052. ["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])
  11053. ("Row"
  11054. ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
  11055. ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
  11056. ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
  11057. ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
  11058. ["Sort lines in region" org-table-sort-lines (org-at-table-p) :keys "C-c ^"]
  11059. "--"
  11060. ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
  11061. ("Rectangle"
  11062. ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
  11063. ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
  11064. ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
  11065. ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
  11066. "--"
  11067. ["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
  11068. ["Set Named Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  11069. ["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
  11070. ["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
  11071. ["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
  11072. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
  11073. ["Sum Column/Rectangle" org-table-sum
  11074. :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
  11075. ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
  11076. ["Debug Formulas"
  11077. (setq org-table-formula-debug (not org-table-formula-debug))
  11078. :style toggle :selected org-table-formula-debug]
  11079. ))
  11080. t)
  11081. (defun orgtbl-tab (arg)
  11082. "Justification and field motion for `orgtbl-mode'."
  11083. (interactive "P")
  11084. (if arg (org-table-edit-field t)
  11085. (org-table-justify-field-maybe)
  11086. (org-table-next-field)))
  11087. (defun orgtbl-ret ()
  11088. "Justification and field motion for `orgtbl-mode'."
  11089. (interactive)
  11090. (org-table-justify-field-maybe)
  11091. (org-table-next-row))
  11092. (defun orgtbl-self-insert-command (N)
  11093. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  11094. If the cursor is in a table looking at whitespace, the whitespace is
  11095. overwritten, and the table is not marked as requiring realignment."
  11096. (interactive "p")
  11097. (if (and (org-at-table-p)
  11098. (or
  11099. (and org-table-auto-blank-field
  11100. (member last-command
  11101. '(orgtbl-hijacker-command-100
  11102. orgtbl-hijacker-command-101
  11103. orgtbl-hijacker-command-102
  11104. orgtbl-hijacker-command-103
  11105. orgtbl-hijacker-command-104
  11106. orgtbl-hijacker-command-105))
  11107. (org-table-blank-field))
  11108. t)
  11109. (eq N 1)
  11110. (looking-at "[^|\n]* +|"))
  11111. (let (org-table-may-need-update)
  11112. (goto-char (1- (match-end 0)))
  11113. (delete-backward-char 1)
  11114. (goto-char (match-beginning 0))
  11115. (self-insert-command N))
  11116. (setq org-table-may-need-update t)
  11117. (let (orgtbl-mode)
  11118. (call-interactively (key-binding (vector last-input-event))))))
  11119. (defun org-force-self-insert (N)
  11120. "Needed to enforce self-insert under remapping."
  11121. (interactive "p")
  11122. (self-insert-command N))
  11123. ;;; Exporting
  11124. (defconst org-level-max 20)
  11125. (defvar org-export-html-preamble nil
  11126. "Preamble, to be inserted just after <body>. Set by publishing functions.")
  11127. (defvar org-export-html-postamble nil
  11128. "Preamble, to be inserted just before </body>. Set by publishing functions.")
  11129. (defvar org-export-html-auto-preamble t
  11130. "Should default preamble be inserted? Set by publishing functions.")
  11131. (defvar org-export-html-auto-postamble t
  11132. "Should default postamble be inserted? Set by publishing functions.")
  11133. (defconst org-export-plist-vars
  11134. '((:language . org-export-default-language)
  11135. (:headline-levels . org-export-headline-levels)
  11136. (:section-numbers . org-export-with-section-numbers)
  11137. (:table-of-contents . org-export-with-toc)
  11138. (:emphasize . org-export-with-emphasize)
  11139. (:sub-superscript . org-export-with-sub-superscripts)
  11140. (:TeX-macros . org-export-with-TeX-macros)
  11141. (:fixed-width . org-export-with-fixed-width)
  11142. (:timestamps . org-export-with-timestamps)
  11143. (:tables . org-export-with-tables)
  11144. (:table-auto-headline . org-export-highlight-first-table-line)
  11145. (:style . org-export-html-style)
  11146. (:convert-org-links . org-export-html-link-org-files-as-html)
  11147. (:inline-images . org-export-html-inline-images)
  11148. (:expand-quoted-html . org-export-html-expand)
  11149. (:timestamp . org-export-html-with-timestamp)
  11150. (:publishing-directory . org-export-publishing-directory)
  11151. (:preamble . org-export-html-preamble)
  11152. (:postamble . org-export-html-postamble)
  11153. (:auto-preamble . org-export-html-auto-preamble)
  11154. (:auto-postamble . org-export-html-auto-postamble)
  11155. (:author . user-full-name)
  11156. (:email . user-mail-address)))
  11157. (defun org-default-export-plist ()
  11158. "Return the property list with default settings for the export variables."
  11159. (let ((l org-export-plist-vars) rtn e)
  11160. (while (setq e (pop l))
  11161. (setq rtn (cons (car e) (cons (symbol-value (cdr e)) rtn))))
  11162. rtn))
  11163. (defun org-infile-export-plist ()
  11164. "Return the property list with file-local settings for export."
  11165. (save-excursion
  11166. (goto-char 0)
  11167. (let ((re (org-make-options-regexp
  11168. '("TITLE" "AUTHOR" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE")))
  11169. (text nil)
  11170. p key val text options)
  11171. (while (re-search-forward re nil t)
  11172. (setq key (org-match-string-no-properties 1)
  11173. val (org-match-string-no-properties 2))
  11174. (cond
  11175. ((string-equal key "TITLE") (setq p (plist-put p :title val)))
  11176. ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
  11177. ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
  11178. ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
  11179. ((string-equal key "TEXT")
  11180. (setq text (if text (concat text "\n" val) val)))
  11181. ((string-equal key "OPTIONS") (setq options val))))
  11182. (setq p (plist-put p :text text))
  11183. (when options
  11184. (let ((op '(("H" . :headline-levels)
  11185. ("num" . :section-numbers)
  11186. ("toc" . :table-of-contents)
  11187. ("\\n" . :preserve-breaks)
  11188. ("@" . :expand-quoted-html)
  11189. (":" . :fixed-width)
  11190. ("|" . :tables)
  11191. ("^" . :sub-superscript)
  11192. ("*" . :emphasize)
  11193. ("TeX" . :TeX-macros)))
  11194. o)
  11195. (while (setq o (pop op))
  11196. (if (string-match (concat (regexp-quote (car o))
  11197. ":\\([^ \t\n\r;,.]*\\)")
  11198. options)
  11199. (setq p (plist-put p (cdr o)
  11200. (car (read-from-string
  11201. (match-string 1 options)))))))))
  11202. p)))
  11203. (defun org-combine-plists (&rest plists)
  11204. "Create a single property list from all plists in PLISTS.
  11205. The process starts by copying the last list, and then setting properties
  11206. from the other lists. Settings in the first list are the most significant
  11207. ones and overrule settings in the other lists."
  11208. (let ((rtn (copy-sequence (pop plists)))
  11209. p v ls)
  11210. (while plists
  11211. (setq ls (pop plists))
  11212. (while ls
  11213. (setq p (pop ls) v (pop ls))
  11214. (setq rtn (plist-put rtn p v))))
  11215. rtn))
  11216. (defun org-export-directory (type plist)
  11217. (let* ((val (plist-get plist :publishing-directory))
  11218. (dir (if (listp val)
  11219. (or (cdr (assoc type val)) ".")
  11220. val)))
  11221. dir))
  11222. (defun org-export-find-first-heading-line (list)
  11223. "Remove all lines from LIST which are before the first headline."
  11224. (let ((orig-list list)
  11225. (re (concat "^" outline-regexp)))
  11226. (while (and list
  11227. (not (string-match re (car list))))
  11228. (pop list))
  11229. (or list orig-list)))
  11230. (defun org-skip-comments (lines)
  11231. "Skip lines starting with \"#\" and subtrees starting with COMMENT."
  11232. (let ((re1 (concat "^\\(\\*+\\)[ \t]+" org-comment-string))
  11233. (re2 "^\\(\\*+\\)[ \t\n\r]")
  11234. rtn line level)
  11235. (while (setq line (pop lines))
  11236. (cond
  11237. ((and (string-match re1 line)
  11238. (setq level (- (match-end 1) (match-beginning 1))))
  11239. ;; Beginning of a COMMENT subtree. Skip it.
  11240. (while (and (setq line (pop lines))
  11241. (or (not (string-match re2 line))
  11242. (> (- (match-end 1) (match-beginning 1)) level))))
  11243. (setq lines (cons line lines)))
  11244. ((string-match "^#" line)
  11245. ;; an ordinary comment line
  11246. )
  11247. ((and org-export-table-remove-special-lines
  11248. (string-match "^[ \t]*|" line)
  11249. (or (string-match "^[ \t]*| *[!_^] *|" line)
  11250. (and (string-match "| *<[0-9]+> *|" line)
  11251. (not (string-match "| *[^ <|]" line)))))
  11252. ;; a special table line that should be removed
  11253. )
  11254. (t (setq rtn (cons line rtn)))))
  11255. (nreverse rtn)))
  11256. (defun org-export (&optional arg)
  11257. (interactive)
  11258. (let ((help "[t] insert the export option template
  11259. \[v] limit export to visible part of outline tree
  11260. \[a] export as ASCII
  11261. \[h] export as HTML
  11262. \[b] export as HTML and browse immediately
  11263. \[x] export as XOXO
  11264. \[i] export current file as iCalendar file
  11265. \[I] export all agenda files as iCalendar files
  11266. \[c] export agenda files into combined iCalendar file
  11267. \[F] publish current file
  11268. \[P] publish current project
  11269. \[X] publish... (project will be prompted for)
  11270. \[A] publish all projects")
  11271. (cmds
  11272. '((?v . org-export-visible)
  11273. (?a . org-export-as-ascii)
  11274. (?h . org-export-as-html)
  11275. (?b . org-export-as-html-and-open)
  11276. (?x . org-export-as-xoxo)
  11277. (?i . org-export-icalendar-this-file)
  11278. (?I . org-export-icalendar-all-agenda-files)
  11279. (?c . org-export-icalendar-combine-agenda-files)
  11280. (?F . org-publish-current-file)
  11281. (?P . org-publish-current-project)
  11282. (?X . org-publish)
  11283. (?A . org-publish-all)))
  11284. r1 r2 ass)
  11285. (save-window-excursion
  11286. (delete-other-windows)
  11287. (with-output-to-temp-buffer "*Org Export/Publishing Help*"
  11288. (princ help))
  11289. (message "Select command: ")
  11290. (setq r1 (read-char-exclusive)))
  11291. (setq r2 (if (< r1 27) (+ r1 96) r1))
  11292. (if (setq ass (assq r2 cmds))
  11293. (call-interactively (cdr ass))
  11294. (error "No command associated with key %c" r1))))
  11295. ;; ASCII
  11296. (defconst org-html-entities
  11297. '(("nbsp")
  11298. ("iexcl")
  11299. ("cent")
  11300. ("pound")
  11301. ("curren")
  11302. ("yen")
  11303. ("brvbar")
  11304. ("sect")
  11305. ("uml")
  11306. ("copy")
  11307. ("ordf")
  11308. ("laquo")
  11309. ("not")
  11310. ("shy")
  11311. ("reg")
  11312. ("macr")
  11313. ("deg")
  11314. ("plusmn")
  11315. ("sup2")
  11316. ("sup3")
  11317. ("acute")
  11318. ("micro")
  11319. ("para")
  11320. ("middot")
  11321. ("odot"."o")
  11322. ("star"."*")
  11323. ("cedil")
  11324. ("sup1")
  11325. ("ordm")
  11326. ("raquo")
  11327. ("frac14")
  11328. ("frac12")
  11329. ("frac34")
  11330. ("iquest")
  11331. ("Agrave")
  11332. ("Aacute")
  11333. ("Acirc")
  11334. ("Atilde")
  11335. ("Auml")
  11336. ("Aring") ("AA"."&Aring;")
  11337. ("AElig")
  11338. ("Ccedil")
  11339. ("Egrave")
  11340. ("Eacute")
  11341. ("Ecirc")
  11342. ("Euml")
  11343. ("Igrave")
  11344. ("Iacute")
  11345. ("Icirc")
  11346. ("Iuml")
  11347. ("ETH")
  11348. ("Ntilde")
  11349. ("Ograve")
  11350. ("Oacute")
  11351. ("Ocirc")
  11352. ("Otilde")
  11353. ("Ouml")
  11354. ("times")
  11355. ("Oslash")
  11356. ("Ugrave")
  11357. ("Uacute")
  11358. ("Ucirc")
  11359. ("Uuml")
  11360. ("Yacute")
  11361. ("THORN")
  11362. ("szlig")
  11363. ("agrave")
  11364. ("aacute")
  11365. ("acirc")
  11366. ("atilde")
  11367. ("auml")
  11368. ("aring")
  11369. ("aelig")
  11370. ("ccedil")
  11371. ("egrave")
  11372. ("eacute")
  11373. ("ecirc")
  11374. ("euml")
  11375. ("igrave")
  11376. ("iacute")
  11377. ("icirc")
  11378. ("iuml")
  11379. ("eth")
  11380. ("ntilde")
  11381. ("ograve")
  11382. ("oacute")
  11383. ("ocirc")
  11384. ("otilde")
  11385. ("ouml")
  11386. ("divide")
  11387. ("oslash")
  11388. ("ugrave")
  11389. ("uacute")
  11390. ("ucirc")
  11391. ("uuml")
  11392. ("yacute")
  11393. ("thorn")
  11394. ("yuml")
  11395. ("fnof")
  11396. ("Alpha")
  11397. ("Beta")
  11398. ("Gamma")
  11399. ("Delta")
  11400. ("Epsilon")
  11401. ("Zeta")
  11402. ("Eta")
  11403. ("Theta")
  11404. ("Iota")
  11405. ("Kappa")
  11406. ("Lambda")
  11407. ("Mu")
  11408. ("Nu")
  11409. ("Xi")
  11410. ("Omicron")
  11411. ("Pi")
  11412. ("Rho")
  11413. ("Sigma")
  11414. ("Tau")
  11415. ("Upsilon")
  11416. ("Phi")
  11417. ("Chi")
  11418. ("Psi")
  11419. ("Omega")
  11420. ("alpha")
  11421. ("beta")
  11422. ("gamma")
  11423. ("delta")
  11424. ("epsilon")
  11425. ("varepsilon"."&epsilon;")
  11426. ("zeta")
  11427. ("eta")
  11428. ("theta")
  11429. ("iota")
  11430. ("kappa")
  11431. ("lambda")
  11432. ("mu")
  11433. ("nu")
  11434. ("xi")
  11435. ("omicron")
  11436. ("pi")
  11437. ("rho")
  11438. ("sigmaf") ("varsigma"."&sigmaf;")
  11439. ("sigma")
  11440. ("tau")
  11441. ("upsilon")
  11442. ("phi")
  11443. ("chi")
  11444. ("psi")
  11445. ("omega")
  11446. ("thetasym") ("vartheta"."&thetasym;")
  11447. ("upsih")
  11448. ("piv")
  11449. ("bull") ("bullet"."&bull;")
  11450. ("hellip") ("dots"."&hellip;")
  11451. ("prime")
  11452. ("Prime")
  11453. ("oline")
  11454. ("frasl")
  11455. ("weierp")
  11456. ("image")
  11457. ("real")
  11458. ("trade")
  11459. ("alefsym")
  11460. ("larr") ("leftarrow"."&larr;") ("gets"."&larr;")
  11461. ("uarr") ("uparrow"."&uarr;")
  11462. ("rarr") ("to"."&rarr;") ("rightarrow"."&rarr;")
  11463. ("darr")("downarrow"."&darr;")
  11464. ("harr") ("leftrightarrow"."&harr;")
  11465. ("crarr") ("hookleftarrow"."&crarr;") ; has round hook, not quite CR
  11466. ("lArr") ("Leftarrow"."&lArr;")
  11467. ("uArr") ("Uparrow"."&uArr;")
  11468. ("rArr") ("Rightarrow"."&rArr;")
  11469. ("dArr") ("Downarrow"."&dArr;")
  11470. ("hArr") ("Leftrightarrow"."&hArr;")
  11471. ("forall")
  11472. ("part") ("partial"."&part;")
  11473. ("exist") ("exists"."&exist;")
  11474. ("empty") ("emptyset"."&empty;")
  11475. ("nabla")
  11476. ("isin") ("in"."&isin;")
  11477. ("notin")
  11478. ("ni")
  11479. ("prod")
  11480. ("sum")
  11481. ("minus")
  11482. ("lowast") ("ast"."&lowast;")
  11483. ("radic")
  11484. ("prop") ("proptp"."&prop;")
  11485. ("infin") ("infty"."&infin;")
  11486. ("ang") ("angle"."&ang;")
  11487. ("and") ("vee"."&and;")
  11488. ("or") ("wedge"."&or;")
  11489. ("cap")
  11490. ("cup")
  11491. ("int")
  11492. ("there4")
  11493. ("sim")
  11494. ("cong") ("simeq"."&cong;")
  11495. ("asymp")("approx"."&asymp;")
  11496. ("ne") ("neq"."&ne;")
  11497. ("equiv")
  11498. ("le")
  11499. ("ge")
  11500. ("sub") ("subset"."&sub;")
  11501. ("sup") ("supset"."&sup;")
  11502. ("nsub")
  11503. ("sube")
  11504. ("supe")
  11505. ("oplus")
  11506. ("otimes")
  11507. ("perp")
  11508. ("sdot") ("cdot"."&sdot;")
  11509. ("lceil")
  11510. ("rceil")
  11511. ("lfloor")
  11512. ("rfloor")
  11513. ("lang")
  11514. ("rang")
  11515. ("loz") ("Diamond"."&loz;")
  11516. ("spades") ("spadesuit"."&spades;")
  11517. ("clubs") ("clubsuit"."&clubs;")
  11518. ("hearts") ("diamondsuit"."&hearts;")
  11519. ("diams") ("diamondsuit"."&diams;")
  11520. ("quot")
  11521. ("amp")
  11522. ("lt")
  11523. ("gt")
  11524. ("OElig")
  11525. ("oelig")
  11526. ("Scaron")
  11527. ("scaron")
  11528. ("Yuml")
  11529. ("circ")
  11530. ("tilde")
  11531. ("ensp")
  11532. ("emsp")
  11533. ("thinsp")
  11534. ("zwnj")
  11535. ("zwj")
  11536. ("lrm")
  11537. ("rlm")
  11538. ("ndash")
  11539. ("mdash")
  11540. ("lsquo")
  11541. ("rsquo")
  11542. ("sbquo")
  11543. ("ldquo")
  11544. ("rdquo")
  11545. ("bdquo")
  11546. ("dagger")
  11547. ("Dagger")
  11548. ("permil")
  11549. ("lsaquo")
  11550. ("rsaquo")
  11551. ("euro")
  11552. ("arccos"."arccos")
  11553. ("arcsin"."arcsin")
  11554. ("arctan"."arctan")
  11555. ("arg"."arg")
  11556. ("cos"."cos")
  11557. ("cosh"."cosh")
  11558. ("cot"."cot")
  11559. ("coth"."coth")
  11560. ("csc"."csc")
  11561. ("deg"."deg")
  11562. ("det"."det")
  11563. ("dim"."dim")
  11564. ("exp"."exp")
  11565. ("gcd"."gcd")
  11566. ("hom"."hom")
  11567. ("inf"."inf")
  11568. ("ker"."ker")
  11569. ("lg"."lg")
  11570. ("lim"."lim")
  11571. ("liminf"."liminf")
  11572. ("limsup"."limsup")
  11573. ("ln"."ln")
  11574. ("log"."log")
  11575. ("max"."max")
  11576. ("min"."min")
  11577. ("Pr"."Pr")
  11578. ("sec"."sec")
  11579. ("sin"."sin")
  11580. ("sinh"."sinh")
  11581. ("sup"."sup")
  11582. ("tan"."tan")
  11583. ("tanh"."tanh")
  11584. )
  11585. "Entities for TeX->HTML translation.
  11586. Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
  11587. \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
  11588. In that case, \"\\ent\" will be translated to \"&other;\".
  11589. The list contains HTML entities for Latin-1, Greek and other symbols.
  11590. It is supplemented by a number of commonly used TeX macros with appropriate
  11591. translations. There is currently no way for users to extend this.")
  11592. (defun org-cleaned-string-for-export (string &rest parameters)
  11593. "Cleanup a buffer substring so that links can be created safely."
  11594. (interactive)
  11595. (let* ((cb (current-buffer))
  11596. (re-radio (and org-target-link-regexp
  11597. (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)")))
  11598. (re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
  11599. (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
  11600. rtn)
  11601. (save-excursion
  11602. (set-buffer (get-buffer-create " org-mode-tmp"))
  11603. (erase-buffer)
  11604. (insert string)
  11605. (org-mode)
  11606. ;; Find targets in comments and move them out of comments,
  11607. ;; but mark them as targets that should be invisible
  11608. (goto-char (point-min))
  11609. (while (re-search-forward "^#.*?\\(<<<?[^>\r\n]+>>>?\\).*" nil t)
  11610. (replace-match "\\1(INVISIBLE)"))
  11611. ;; Find matches for radio targets and turn them into internal links
  11612. (goto-char (point-min))
  11613. (when re-radio
  11614. (while (re-search-forward re-radio nil t)
  11615. (replace-match "\\1[[\\2]]")))
  11616. ;; Find all links that contain a newline and put them into a single line
  11617. (goto-char (point-min))
  11618. (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
  11619. (replace-match "\\1 \\3")
  11620. (goto-char (match-beginning 0)))
  11621. ;; Normalize links: Convert angle and plain links into bracket links
  11622. (goto-char (point-min))
  11623. (while (re-search-forward re-plain-link nil t)
  11624. (replace-match
  11625. (concat
  11626. (match-string 1) "[[" (match-string 2) ":" (match-string 3) "]]")
  11627. t t))
  11628. (goto-char (point-min))
  11629. (while (re-search-forward re-angle-link nil t)
  11630. (replace-match
  11631. (concat
  11632. (match-string 1) "[[" (match-string 2) ":" (match-string 3) "]]")
  11633. t t))
  11634. ;; Find multiline emphasis and put them into single line
  11635. (when (assq :emph-multiline parameters)
  11636. (goto-char (point-min))
  11637. (while (re-search-forward org-emph-re nil t)
  11638. (subst-char-in-region (match-beginning 0) (match-end 0) ?\n ?\ t)
  11639. (goto-char (1- (match-end 0)))))
  11640. ;; Remove comments
  11641. (goto-char (point-min))
  11642. (while (re-search-forward "^#.*\n?" nil t)
  11643. (replace-match ""))
  11644. (setq rtn (buffer-string)))
  11645. (kill-buffer " org-mode-tmp")
  11646. rtn))
  11647. (defun org-solidify-link-text (s &optional alist)
  11648. "Take link text and make a safe target out of it."
  11649. (save-match-data
  11650. (let* ((rtn
  11651. (mapconcat
  11652. 'identity
  11653. (org-split-string s "[ \t\r\n]+") "--"))
  11654. (a (assoc rtn alist)))
  11655. (or (cdr a) rtn))))
  11656. (defun org-convert-to-odd-levels ()
  11657. "Convert an org-mode file with all levels allowed to one with odd levels.
  11658. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  11659. level 5 etc."
  11660. (interactive)
  11661. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  11662. (let ((org-odd-levels-only nil) n)
  11663. (save-excursion
  11664. (goto-char (point-min))
  11665. (while (re-search-forward "^\\*\\*+" nil t)
  11666. (setq n (1- (length (match-string 0))))
  11667. (while (>= (setq n (1- n)) 0)
  11668. (org-demote))
  11669. (end-of-line 1))))))
  11670. (defun org-convert-to-oddeven-levels ()
  11671. "Convert an org-mode file with only odd levels to one with odd and even levels.
  11672. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  11673. section with an even level, conversion would destroy the structure of the file. An error
  11674. is signaled in this case."
  11675. (interactive)
  11676. (goto-char (point-min))
  11677. ;; First check if there are no even levels
  11678. (when (re-search-forward "^\\(\\*\\*\\)+[^*]" nil t)
  11679. (org-show-hierarchy-above)
  11680. (error "Not all levels are odd in this file. Conversion not possible."))
  11681. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  11682. (let ((org-odd-levels-only nil) n)
  11683. (save-excursion
  11684. (goto-char (point-min))
  11685. (while (re-search-forward "^\\*\\*+" nil t)
  11686. (setq n (/ (length (match-string 0)) 2))
  11687. (while (>= (setq n (1- n)) 0)
  11688. (org-promote))
  11689. (end-of-line 1))))))
  11690. (defun org-tr-level (n)
  11691. "Make N odd if required."
  11692. (if org-odd-levels-only (1+ (/ n 2)) n))
  11693. (defvar org-last-level nil) ; dynamically scoped variable
  11694. (defvar org-ascii-current-indentation nil) ; For communication
  11695. (defun org-export-as-ascii (arg)
  11696. "Export the outline as a pretty ASCII file.
  11697. If there is an active region, export only the region.
  11698. The prefix ARG specifies how many levels of the outline should become
  11699. underlined headlines. The default is 3."
  11700. (interactive "P")
  11701. (setq-default org-todo-line-regexp org-todo-line-regexp)
  11702. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  11703. (org-infile-export-plist)))
  11704. (region
  11705. (buffer-substring
  11706. (if (org-region-active-p) (region-beginning) (point-min))
  11707. (if (org-region-active-p) (region-end) (point-max))))
  11708. (lines (org-export-find-first-heading-line
  11709. (org-skip-comments
  11710. (org-split-string
  11711. (org-cleaned-string-for-export region)
  11712. "[\r\n]"))))
  11713. (org-ascii-current-indentation '(0 . 0))
  11714. (org-startup-with-deadline-check nil)
  11715. (level 0) line txt
  11716. (umax nil)
  11717. (case-fold-search nil)
  11718. (filename (concat (file-name-as-directory
  11719. (org-export-directory :ascii opt-plist))
  11720. (file-name-sans-extension
  11721. (file-name-nondirectory buffer-file-name))
  11722. ".txt"))
  11723. (buffer (find-file-noselect filename))
  11724. (levels-open (make-vector org-level-max nil))
  11725. (odd org-odd-levels-only)
  11726. (date (format-time-string "%Y/%m/%d" (current-time)))
  11727. (time (format-time-string "%X" (org-current-time)))
  11728. (author (plist-get opt-plist :author))
  11729. (title (or (plist-get opt-plist :title)
  11730. (file-name-sans-extension
  11731. (file-name-nondirectory buffer-file-name))))
  11732. (options nil)
  11733. (email (plist-get opt-plist :email))
  11734. (language (plist-get opt-plist :language))
  11735. (text nil)
  11736. (todo nil)
  11737. (lang-words nil))
  11738. (setq org-last-level 1)
  11739. (org-init-section-numbers)
  11740. (find-file-noselect filename)
  11741. (setq lang-words (or (assoc language org-export-language-setup)
  11742. (assoc "en" org-export-language-setup)))
  11743. (if org-export-ascii-show-new-buffer
  11744. (switch-to-buffer-other-window buffer)
  11745. (set-buffer buffer))
  11746. (erase-buffer)
  11747. (fundamental-mode)
  11748. ;; create local variables for all options, to make sure all called
  11749. ;; functions get the correct information
  11750. (mapcar (lambda (x)
  11751. (set (make-local-variable (cdr x))
  11752. (plist-get opt-plist (car x))))
  11753. org-export-plist-vars)
  11754. (set (make-local-variable 'org-odd-levels-only) odd)
  11755. (setq umax (if arg (prefix-numeric-value arg)
  11756. org-export-headline-levels))
  11757. ;; File header
  11758. (if title (org-insert-centered title ?=))
  11759. (insert "\n")
  11760. (if (or author email)
  11761. (insert (concat (nth 1 lang-words) ": " (or author "")
  11762. (if email (concat " <" email ">") "")
  11763. "\n")))
  11764. (if (and date time)
  11765. (insert (concat (nth 2 lang-words) ": " date " " time "\n")))
  11766. (if text (insert (concat (org-html-expand-for-ascii text) "\n\n")))
  11767. (insert "\n\n")
  11768. (if org-export-with-toc
  11769. (progn
  11770. (insert (nth 3 lang-words) "\n"
  11771. (make-string (length (nth 3 lang-words)) ?=) "\n")
  11772. (mapcar '(lambda (line)
  11773. (if (string-match org-todo-line-regexp
  11774. line)
  11775. ;; This is a headline
  11776. (progn
  11777. (setq level (- (match-end 1) (match-beginning 1))
  11778. level (org-tr-level level)
  11779. txt (match-string 3 line)
  11780. todo
  11781. (or (and org-export-mark-todo-in-toc
  11782. (match-beginning 2)
  11783. (not (equal (match-string 2 line)
  11784. org-done-string)))
  11785. ; TODO, not DONE
  11786. (and org-export-mark-todo-in-toc
  11787. (= level umax)
  11788. (org-search-todo-below
  11789. line lines level))))
  11790. (setq txt (org-html-expand-for-ascii txt))
  11791. (if org-export-with-section-numbers
  11792. (setq txt (concat (org-section-number level)
  11793. " " txt)))
  11794. (if (<= level umax)
  11795. (progn
  11796. (insert
  11797. (make-string (* (1- level) 4) ?\ )
  11798. (format (if todo "%s (*)\n" "%s\n") txt))
  11799. (setq org-last-level level))
  11800. ))))
  11801. lines)))
  11802. (org-init-section-numbers)
  11803. (while (setq line (pop lines))
  11804. ;; Remove the quoted HTML tags.
  11805. (setq line (org-html-expand-for-ascii line))
  11806. ;; Remove targets
  11807. (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line)
  11808. (setq line (replace-match "" t t line)))
  11809. ;; Replace internal links
  11810. (while (string-match org-bracket-link-regexp line)
  11811. (setq line (replace-match
  11812. (if (match-end 3) "[\\3]" "[\\1]")
  11813. t nil line)))
  11814. (cond
  11815. ((string-match "^\\(\\*+\\)[ \t]*\\(.*\\)" line)
  11816. ;; a Headline
  11817. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  11818. txt (match-string 2 line))
  11819. (org-ascii-level-start level txt umax lines))
  11820. (t
  11821. (insert (org-fix-indentation line org-ascii-current-indentation) "\n"))))
  11822. (normal-mode)
  11823. (save-buffer)
  11824. ;; remove display and invisible chars
  11825. (let (beg end s)
  11826. (goto-char (point-min))
  11827. (while (setq beg (next-single-property-change (point) 'display))
  11828. (setq end (next-single-property-change beg 'display))
  11829. (delete-region beg end)
  11830. (goto-char beg)
  11831. (insert "=>"))
  11832. (goto-char (point-min))
  11833. (while (setq beg (next-single-property-change (point) 'org-cwidth))
  11834. (setq end (next-single-property-change beg 'org-cwidth))
  11835. (delete-region beg end)
  11836. (goto-char beg)))
  11837. (goto-char (point-min))))
  11838. (defun org-search-todo-below (line lines level)
  11839. "Search the subtree below LINE for any TODO entries."
  11840. (let ((rest (cdr (memq line lines)))
  11841. (re org-todo-line-regexp)
  11842. line lv todo)
  11843. (catch 'exit
  11844. (while (setq line (pop rest))
  11845. (if (string-match re line)
  11846. (progn
  11847. (setq lv (- (match-end 1) (match-beginning 1))
  11848. todo (and (match-beginning 2)
  11849. (not (equal (match-string 2 line)
  11850. org-done-string))))
  11851. ; TODO, not DONE
  11852. (if (<= lv level) (throw 'exit nil))
  11853. (if todo (throw 'exit t))))))))
  11854. (defun org-html-expand-for-ascii (line)
  11855. "Handle quoted HTML for ASCII export."
  11856. (if org-export-html-expand
  11857. (while (string-match "@<[^<>\n]*>" line)
  11858. ;; We just remove the tags for now.
  11859. (setq line (replace-match "" nil nil line))))
  11860. line)
  11861. (defun org-insert-centered (s &optional underline)
  11862. "Insert the string S centered and underline it with character UNDERLINE."
  11863. (let ((ind (max (/ (- 80 (string-width s)) 2) 0)))
  11864. (insert (make-string ind ?\ ) s "\n")
  11865. (if underline
  11866. (insert (make-string ind ?\ )
  11867. (make-string (string-width s) underline)
  11868. "\n"))))
  11869. (defun org-ascii-level-start (level title umax &optional lines)
  11870. "Insert a new level in ASCII export."
  11871. (let (char (n (- level umax 1)) (ind 0))
  11872. (if (> level umax)
  11873. (progn
  11874. (insert (make-string (* 2 n) ?\ )
  11875. (char-to-string (nth (% n (length org-export-ascii-bullets))
  11876. org-export-ascii-bullets))
  11877. " " title "\n")
  11878. ;; find the indentation of the next non-empty line
  11879. (catch 'stop
  11880. (while lines
  11881. (if (string-match "^\\*" (car lines)) (throw 'stop nil))
  11882. (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
  11883. (throw 'stop (setq ind (org-get-indentation (car lines)))))
  11884. (pop lines)))
  11885. (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
  11886. (if (or (not (equal (char-before) ?\n))
  11887. (not (equal (char-before (1- (point))) ?\n)))
  11888. (insert "\n"))
  11889. (setq char (nth (- umax level) (reverse org-export-ascii-underline)))
  11890. (if org-export-with-section-numbers
  11891. (setq title (concat (org-section-number level) " " title)))
  11892. (insert title "\n" (make-string (string-width title) char) "\n")
  11893. (setq org-ascii-current-indentation '(0 . 0)))))
  11894. (defun org-export-visible (type arg)
  11895. "Create a copy of the visible part of the current buffer, and export it.
  11896. The copy is created in a temporary buffer and removed after use.
  11897. TYPE is the final key (as a string) of the `C-c C-x' key sequence that will
  11898. run the export command - in interactive use, the command prompts for this
  11899. key. As a special case, if the you type SPC at the prompt, the temporary
  11900. org-mode file will not be removed but presented to you so that you can
  11901. continue to use it. The prefix arg ARG is passed through to the exporting
  11902. command."
  11903. (interactive
  11904. (list (progn
  11905. (message "Export visible: [a]SCII [h]tml [b]rowse HTML [x]OXO [ ]keep buffer")
  11906. (char-to-string (read-char-exclusive)))
  11907. current-prefix-arg))
  11908. (if (not (member type '("a" "\C-a" "b" "\C-b" "h" "x" " ")))
  11909. (error "Invalid export key"))
  11910. ;; FIXME: do this more explicit?
  11911. (let* ((binding (key-binding (concat "\C-c\C-x" type)))
  11912. (keepp (equal type " "))
  11913. (file buffer-file-name)
  11914. (buffer (get-buffer-create "*Org Export Visible*"))
  11915. s e)
  11916. (with-current-buffer buffer (erase-buffer))
  11917. (save-excursion
  11918. (setq s (goto-char (point-min)))
  11919. (while (not (= (point) (point-max)))
  11920. (goto-char (org-find-invisible))
  11921. (append-to-buffer buffer s (point))
  11922. (setq s (goto-char (org-find-visible))))
  11923. (goto-char (point-min))
  11924. (unless keepp
  11925. ;; Copy all comment lines to the end, to make sure #+ settings are
  11926. ;; still available for the second export step. Kind of a hack, but
  11927. ;; does do the trick.
  11928. (if (looking-at "#[^\r\n]*")
  11929. (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
  11930. (while (re-search-forward "[\n\r]#[^\n\r]*" nil t)
  11931. (append-to-buffer buffer (1+ (match-beginning 0))
  11932. (min (point-max) (1+ (match-end 0))))))
  11933. (set-buffer buffer)
  11934. (let ((buffer-file-name file)
  11935. (org-inhibit-startup t))
  11936. (org-mode)
  11937. (show-all)
  11938. (unless keepp (funcall binding arg))))
  11939. (if (not keepp)
  11940. (kill-buffer buffer)
  11941. (switch-to-buffer-other-window buffer)
  11942. (goto-char (point-min)))))
  11943. (defun org-find-visible ()
  11944. (if (featurep 'noutline)
  11945. (let ((s (point)))
  11946. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  11947. (get-char-property s 'invisible)))
  11948. s)
  11949. (skip-chars-forward "^\n")
  11950. (point)))
  11951. (defun org-find-invisible ()
  11952. (if (featurep 'noutline)
  11953. (let ((s (point)))
  11954. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  11955. (not (get-char-property s 'invisible))))
  11956. s)
  11957. (skip-chars-forward "^\r")
  11958. (point)))
  11959. ;; HTML
  11960. (defun org-get-current-options ()
  11961. "Return a string with current options as keyword options.
  11962. Does include HTML export options as well as TODO and CATEGORY stuff."
  11963. (format
  11964. "#+TITLE: %s
  11965. #+AUTHOR: %s
  11966. #+EMAIL: %s
  11967. #+LANGUAGE: %s
  11968. #+TEXT: Some descriptive text to be emitted. Several lines OK.
  11969. #+OPTIONS: H:%d num:%s toc:%s \\n:%s @:%s ::%s |:%s ^:%s *:%s TeX:%s
  11970. #+CATEGORY: %s
  11971. #+SEQ_TODO: %s
  11972. #+TYP_TODO: %s
  11973. #+STARTUP: %s %s %s %s %s %s
  11974. #+TAGS: %s
  11975. #+ARCHIVE: %s
  11976. "
  11977. (buffer-name) (user-full-name) user-mail-address org-export-default-language
  11978. org-export-headline-levels
  11979. org-export-with-section-numbers
  11980. org-export-with-toc
  11981. org-export-preserve-breaks
  11982. org-export-html-expand
  11983. org-export-with-fixed-width
  11984. org-export-with-tables
  11985. org-export-with-sub-superscripts
  11986. org-export-with-emphasize
  11987. org-export-with-TeX-macros
  11988. (file-name-nondirectory buffer-file-name)
  11989. (if (equal org-todo-interpretation 'sequence)
  11990. (mapconcat 'identity org-todo-keywords " ")
  11991. "TODO FEEDBACK VERIFY DONE")
  11992. (if (equal org-todo-interpretation 'type)
  11993. (mapconcat 'identity org-todo-keywords " ")
  11994. "Me Jason Marie DONE")
  11995. (cdr (assoc org-startup-folded
  11996. '((nil . "showall") (t . "overview") (content . "content"))))
  11997. (if org-startup-with-deadline-check "dlcheck" "nodlcheck")
  11998. (if org-odd-levels-only "odd" "oddeven")
  11999. (if org-hide-leading-stars "hidestars" "showstars")
  12000. (if org-startup-align-all-tables "align" "noalign")
  12001. (if org-log-done "logging" "nologging")
  12002. (if org-tag-alist (mapconcat 'car org-tag-alist " ") "")
  12003. org-archive-location
  12004. ))
  12005. (defun org-insert-export-options-template ()
  12006. "Insert into the buffer a template with information for exporting."
  12007. (interactive)
  12008. (if (not (bolp)) (newline))
  12009. (let ((s (org-get-current-options)))
  12010. (and (string-match "#\\+CATEGORY" s)
  12011. (setq s (substring s 0 (match-beginning 0))))
  12012. (insert s)))
  12013. (defun org-toggle-fixed-width-section (arg)
  12014. "Toggle the fixed-width export.
  12015. If there is no active region, the QUOTE keyword at the current headline is
  12016. inserted or removed. When present, it causes the text between this headline
  12017. and the next to be exported as fixed-width text, and unmodified.
  12018. If there is an active region, this command adds or removes a colon as the
  12019. first character of this line. If the first character of a line is a colon,
  12020. this line is also exported in fixed-width font."
  12021. (interactive "P")
  12022. (let* ((cc 0)
  12023. (regionp (org-region-active-p))
  12024. (beg (if regionp (region-beginning) (point)))
  12025. (end (if regionp (region-end)))
  12026. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  12027. (re "[ \t]*\\(:\\)")
  12028. off)
  12029. (if regionp
  12030. (save-excursion
  12031. (goto-char beg)
  12032. (setq cc (current-column))
  12033. (beginning-of-line 1)
  12034. (setq off (looking-at re))
  12035. (while (> nlines 0)
  12036. (setq nlines (1- nlines))
  12037. (beginning-of-line 1)
  12038. (cond
  12039. (arg
  12040. (move-to-column cc t)
  12041. (insert ":\n")
  12042. (forward-line -1))
  12043. ((and off (looking-at re))
  12044. (replace-match "" t t nil 1))
  12045. ((not off) (move-to-column cc t) (insert ":")))
  12046. (forward-line 1)))
  12047. (save-excursion
  12048. (org-back-to-heading)
  12049. (if (looking-at (concat outline-regexp
  12050. "\\( +\\<" org-quote-string "\\>\\)"))
  12051. (replace-match "" t t nil 1)
  12052. (if (looking-at outline-regexp)
  12053. (progn
  12054. (goto-char (match-end 0))
  12055. (insert " " org-quote-string))))))))
  12056. (defun org-export-as-html-and-open (arg)
  12057. "Export the outline as HTML and immediately open it with a browser.
  12058. If there is an active region, export only the region.
  12059. The prefix ARG specifies how many levels of the outline should become
  12060. headlines. The default is 3. Lower levels will become bulleted lists."
  12061. (interactive "P")
  12062. (org-export-as-html arg 'hidden)
  12063. (org-open-file buffer-file-name))
  12064. (defun org-export-as-html-batch ()
  12065. "Call `org-export-as-html', may be used in batch processing as
  12066. emacs --batch
  12067. --load=$HOME/lib/emacs/org.el
  12068. --eval \"(setq org-export-headline-levels 2)\"
  12069. --visit=MyFile --funcall org-export-as-html-batch"
  12070. (org-export-as-html org-export-headline-levels 'hidden))
  12071. (defun org-export-as-html (arg &optional hidden ext-plist)
  12072. "Export the outline as a pretty HTML file.
  12073. If there is an active region, export only the region.
  12074. The prefix ARG specifies how many levels of the outline should become
  12075. headlines. The default is 3. Lower levels will become bulleted lists.
  12076. When HIDDEN is non-nil, don't display the HTML buffer.
  12077. EXT-PLIST is a property list with external parameters overriding
  12078. org-mode's default settings, but still inferior to file-local settings."
  12079. (interactive "P")
  12080. (setq-default org-todo-line-regexp org-todo-line-regexp)
  12081. (setq-default org-deadline-line-regexp org-deadline-line-regexp)
  12082. (setq-default org-done-string org-done-string)
  12083. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  12084. ext-plist
  12085. (org-infile-export-plist)))
  12086. (style (plist-get opt-plist :style))
  12087. (odd org-odd-levels-only)
  12088. (region-p (org-region-active-p))
  12089. (region
  12090. (buffer-substring
  12091. (if region-p (region-beginning) (point-min))
  12092. (if region-p (region-end) (point-max))))
  12093. (all_lines
  12094. (org-skip-comments (org-split-string
  12095. (org-cleaned-string-for-export
  12096. region :emph-multiline)
  12097. "[\r\n]")))
  12098. (lines (org-export-find-first-heading-line all_lines))
  12099. (level 0) (line "") (origline "") txt todo
  12100. (umax nil)
  12101. (filename (concat (file-name-as-directory
  12102. (org-export-directory :html opt-plist))
  12103. (file-name-sans-extension
  12104. (file-name-nondirectory buffer-file-name))
  12105. ".html"))
  12106. (buffer (find-file-noselect filename))
  12107. (levels-open (make-vector org-level-max nil))
  12108. (date (format-time-string "%Y/%m/%d" (current-time)))
  12109. (time (format-time-string "%X" (org-current-time)))
  12110. (author (plist-get opt-plist :author))
  12111. (title (or (plist-get opt-plist :title)
  12112. (file-name-sans-extension
  12113. (file-name-nondirectory buffer-file-name))))
  12114. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  12115. (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
  12116. (inquote nil)
  12117. (infixed nil)
  12118. (in-local-list nil)
  12119. (local-list-num nil)
  12120. (local-list-indent nil)
  12121. (llt org-plain-list-ordered-item-terminator)
  12122. (email (plist-get opt-plist :email))
  12123. (language (plist-get opt-plist :language))
  12124. (text (plist-get opt-plist :text))
  12125. (lang-words nil)
  12126. (target-alist nil) tg
  12127. (head-count 0) cnt
  12128. (start 0)
  12129. (coding-system (and (fboundp 'coding-system-get)
  12130. (boundp 'buffer-file-coding-system)
  12131. buffer-file-coding-system))
  12132. (coding-system-for-write (or coding-system coding-system-for-write))
  12133. (save-buffer-coding-system (or coding-system save-buffer-coding-system))
  12134. (charset (and coding-system
  12135. (coding-system-get coding-system 'mime-charset)))
  12136. table-open type
  12137. table-buffer table-orig-buffer
  12138. ind start-is-num starter
  12139. rpl path desc descp desc1 desc2 link
  12140. )
  12141. (message "Exporting...")
  12142. (setq org-last-level 1)
  12143. (org-init-section-numbers)
  12144. ;; Get the language-dependent settings
  12145. (setq lang-words (or (assoc language org-export-language-setup)
  12146. (assoc "en" org-export-language-setup)))
  12147. ;; Switch to the output buffer
  12148. (if (or hidden (not org-export-html-show-new-buffer))
  12149. (set-buffer buffer)
  12150. (switch-to-buffer-other-window buffer))
  12151. (erase-buffer)
  12152. (fundamental-mode)
  12153. (let ((case-fold-search nil)
  12154. (org-odd-levels-only odd))
  12155. ;; create local variables for all options, to make sure all called
  12156. ;; functions get the correct information
  12157. (mapcar (lambda (x)
  12158. (set (make-local-variable (cdr x))
  12159. (plist-get opt-plist (car x))))
  12160. org-export-plist-vars)
  12161. (setq umax (if arg (prefix-numeric-value arg)
  12162. org-export-headline-levels))
  12163. ;; File header
  12164. (insert (format
  12165. "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
  12166. \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
  12167. <html xmlns=\"http://www.w3.org/1999/xhtml\"
  12168. lang=\"%s\" xml:lang=\"%s\">
  12169. <head>
  12170. <title>%s</title>
  12171. <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
  12172. <meta name=\"generator\" content=\"Org-mode\"/>
  12173. <meta name=\"generated\" content=\"%s %s\"/>
  12174. <meta name=\"author\" content=\"%s\"/>
  12175. %s
  12176. </head><body>
  12177. "
  12178. language language (org-html-expand title) (or charset "iso-8859-1")
  12179. date time author style))
  12180. (insert (or (plist-get opt-plist :preamble) ""))
  12181. (when (plist-get opt-plist :auto-preamble)
  12182. (if title (insert (concat "<h1 class=\"title\">"
  12183. (org-html-expand title) "</h1>\n")))
  12184. (if text (insert "<p>\n" (org-html-expand text) "</p>")))
  12185. (if org-export-with-toc
  12186. (progn
  12187. (insert (format "<h2>%s</h2>\n" (nth 3 lang-words)))
  12188. (insert "<ul>\n<li>")
  12189. (setq lines
  12190. (mapcar '(lambda (line)
  12191. (if (string-match org-todo-line-regexp line)
  12192. ;; This is a headline
  12193. (progn
  12194. (setq level (- (match-end 1) (match-beginning 1))
  12195. level (org-tr-level level)
  12196. txt (save-match-data
  12197. (org-html-expand
  12198. (org-html-cleanup-toc-line
  12199. (match-string 3 line))))
  12200. todo
  12201. (or (and org-export-mark-todo-in-toc
  12202. (match-beginning 2)
  12203. (not (equal (match-string 2 line)
  12204. org-done-string)))
  12205. ; TODO, not DONE
  12206. (and org-export-mark-todo-in-toc
  12207. (= level umax)
  12208. (org-search-todo-below
  12209. line lines level))))
  12210. (if org-export-with-section-numbers
  12211. (setq txt (concat (org-section-number level)
  12212. " " txt)))
  12213. (if (<= level umax)
  12214. (progn
  12215. (setq head-count (+ head-count 1))
  12216. (if (> level org-last-level)
  12217. (progn
  12218. (setq cnt (- level org-last-level))
  12219. (while (>= (setq cnt (1- cnt)) 0)
  12220. (insert "\n<ul>\n<li>"))
  12221. (insert "\n")))
  12222. (if (< level org-last-level)
  12223. (progn
  12224. (setq cnt (- org-last-level level))
  12225. (while (>= (setq cnt (1- cnt)) 0)
  12226. (insert "</li>\n</ul>"))
  12227. (insert "\n")))
  12228. ;; Check for targets
  12229. (while (string-match org-target-regexp line)
  12230. (setq tg (match-string 1 line)
  12231. line (replace-match
  12232. (concat "@<span class=\"target\">" tg "@</span> ")
  12233. t t line))
  12234. (push (cons (org-solidify-link-text tg)
  12235. (format "sec-%d" head-count))
  12236. target-alist))
  12237. (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
  12238. (setq txt (replace-match "" t t txt)))
  12239. (insert
  12240. (format
  12241. (if todo
  12242. "</li>\n<li><a href=\"#sec-%d\"><span class=\"todo\">%s</span></a>"
  12243. "</li>\n<li><a href=\"#sec-%d\">%s</a>")
  12244. head-count txt))
  12245. (setq org-last-level level))
  12246. )))
  12247. line)
  12248. lines))
  12249. (while (> org-last-level 0)
  12250. (setq org-last-level (1- org-last-level))
  12251. (insert "</li>\n</ul>\n"))
  12252. ))
  12253. (setq head-count 0)
  12254. (org-init-section-numbers)
  12255. (while (setq line (pop lines) origline line)
  12256. (catch 'nextline
  12257. ;; end of quote section?
  12258. (when (and inquote (string-match "^\\*+" line))
  12259. (insert "</pre>\n")
  12260. (setq inquote nil))
  12261. ;; inside a quote section?
  12262. (when inquote
  12263. (insert (org-html-protect line) "\n")
  12264. (throw 'nextline nil))
  12265. ;; verbatim lines
  12266. (when (and org-export-with-fixed-width
  12267. (string-match "^[ \t]*:\\(.*\\)" line))
  12268. (when (not infixed)
  12269. (setq infixed t)
  12270. (insert "<pre>\n"))
  12271. (insert (org-html-protect (match-string 1 line)) "\n")
  12272. (when (and lines
  12273. (not (string-match "^[ \t]*\\(:.*\\)"
  12274. (car lines))))
  12275. (setq infixed nil)
  12276. (insert "</pre>\n"))
  12277. (throw 'nextline nil))
  12278. ;; make targets to anchors
  12279. (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
  12280. (cond
  12281. ((match-end 2)
  12282. (setq line (replace-match
  12283. (concat "@<a name=\""
  12284. (org-solidify-link-text (match-string 1 line))
  12285. "\">\\nbsp@</a>")
  12286. t t line)))
  12287. ((and org-export-with-toc (equal (string-to-char line) ?*))
  12288. (setq line (replace-match
  12289. (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
  12290. ; (concat "@<i>" (match-string 1 line) "@</i> ")
  12291. t t line)))
  12292. (t
  12293. (setq line (replace-match
  12294. (concat "@<a name=\""
  12295. (org-solidify-link-text (match-string 1 line))
  12296. "\" class=\"target\">" (match-string 1 line) "@</a> ")
  12297. t t line)))))
  12298. (setq line (org-html-handle-time-stamps line))
  12299. ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  12300. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  12301. ;; Also handle sub_superscripts and checkboxes
  12302. (setq line (org-html-expand line))
  12303. ;; Format the links
  12304. (setq start 0)
  12305. (while (string-match org-bracket-link-analytic-regexp line start)
  12306. (setq start (match-beginning 0))
  12307. (setq type (if (match-end 2) (match-string 2 line) "internal"))
  12308. (setq path (match-string 3 line))
  12309. (setq desc1 (if (match-end 5) (match-string 5 line))
  12310. desc2 (if (match-end 2) (concat type ":" path) path)
  12311. descp (and desc1 (not (equal desc1 desc2)))
  12312. desc (or desc1 desc2))
  12313. ;; FIXME: do we need to unescape here somewhere?
  12314. (cond
  12315. ((equal type "internal")
  12316. (setq rpl
  12317. (concat
  12318. "<a href=\"#"
  12319. (org-solidify-link-text path target-alist)
  12320. "\">" desc "</a>")))
  12321. ((member type '("http" "https" "ftp" "mailto" "news"))
  12322. ;; standard URL
  12323. (setq link (concat type ":" path))
  12324. (setq rpl (concat "<a href=\"" link "\">" desc "</a>")))
  12325. ((string= type "file")
  12326. ;; FILE link
  12327. (let* ((filename path)
  12328. (abs-p (file-name-absolute-p filename))
  12329. thefile file-is-image-p search)
  12330. (save-match-data
  12331. (if (string-match "::\\(.*\\)" filename)
  12332. (setq search (match-string 1 filename)
  12333. filename (replace-match "" t nil filename)))
  12334. (setq file-is-image-p
  12335. (string-match (org-image-file-name-regexp) filename))
  12336. (setq thefile (if abs-p (expand-file-name filename) filename))
  12337. (when (and org-export-html-link-org-files-as-html
  12338. (string-match "\\.org$" thefile))
  12339. (setq thefile (concat (substring thefile 0
  12340. (match-beginning 0))
  12341. ".html"))
  12342. (if (and search
  12343. ;; make sure this is can be used as target search
  12344. (not (string-match "^[0-9]*$" search))
  12345. (not (string-match "^\\*" search))
  12346. (not (string-match "^/.*/$" search)))
  12347. (setq thefile (concat thefile "#"
  12348. (org-solidify-link-text
  12349. (org-link-unescape search)))))
  12350. (when (string-match "^file:" desc)
  12351. (setq desc (replace-match "" t t desc))
  12352. (if (string-match "\\.org$" desc)
  12353. (setq desc (replace-match "" t t desc))))))
  12354. (setq rpl (if (and file-is-image-p
  12355. (or (eq t org-export-html-inline-images)
  12356. (and org-export-html-inline-images
  12357. (not descp))))
  12358. (concat "<img src=\"" thefile "\"/>")
  12359. (concat "<a href=\"" thefile "\">" desc "</a>")))))
  12360. ((member type '("bbdb" "vm" "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  12361. (setq rpl (concat "<i>&lt;" type ":"
  12362. (save-match-data (org-link-unescape path))
  12363. "&gt;</i>"))))
  12364. (setq line (replace-match rpl t t line)
  12365. start (+ start (length rpl))))
  12366. ;; TODO items
  12367. (if (and (string-match org-todo-line-regexp line)
  12368. (match-beginning 2))
  12369. (if (equal (match-string 2 line) org-done-string)
  12370. (setq line (replace-match
  12371. "<span class=\"done\">\\2</span>"
  12372. t nil line 2))
  12373. (setq line (replace-match "<span class=\"todo\">\\2</span>"
  12374. t nil line 2))))
  12375. (cond
  12376. ((string-match "^\\(\\*+\\)[ \t]*\\(.*\\)" line)
  12377. ;; This is a headline
  12378. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  12379. txt (match-string 2 line))
  12380. (if (string-match quote-re0 txt)
  12381. (setq txt (replace-match "" t t txt)))
  12382. (if (<= level umax) (setq head-count (+ head-count 1)))
  12383. (when in-local-list
  12384. ;; Close any local lists before inserting a new header line
  12385. (while local-list-num
  12386. (org-close-li)
  12387. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  12388. (pop local-list-num))
  12389. (setq local-list-indent nil
  12390. in-local-list nil))
  12391. (org-html-level-start level txt umax
  12392. (and org-export-with-toc (<= level umax))
  12393. head-count)
  12394. ;; QUOTES
  12395. (when (string-match quote-re line)
  12396. (insert "<pre>")
  12397. (setq inquote t)))
  12398. ((and org-export-with-tables
  12399. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  12400. (if (not table-open)
  12401. ;; New table starts
  12402. (setq table-open t table-buffer nil table-orig-buffer nil))
  12403. ;; Accumulate lines
  12404. (setq table-buffer (cons line table-buffer)
  12405. table-orig-buffer (cons origline table-orig-buffer))
  12406. (when (or (not lines)
  12407. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  12408. (car lines))))
  12409. (setq table-open nil
  12410. table-buffer (nreverse table-buffer)
  12411. table-orig-buffer (nreverse table-orig-buffer))
  12412. (org-close-par-maybe)
  12413. (insert (org-format-table-html table-buffer table-orig-buffer))))
  12414. (t
  12415. ;; Normal lines
  12416. (when (string-match
  12417. (cond
  12418. ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*]\\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  12419. ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*]\\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  12420. ((= llt ?\)) "^\\( \t]*\\)\\(\\([-+*]\\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  12421. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
  12422. line)
  12423. (setq ind (org-get-string-indentation line)
  12424. start-is-num (match-beginning 4)
  12425. starter (if (match-beginning 2)
  12426. (substring (match-string 2 line) 0 -1))
  12427. line (substring line (match-beginning 5)))
  12428. (unless (string-match "[^ \t]" line)
  12429. ;; empty line. Pretend indentation is large.
  12430. (setq ind (1+ (or (car local-list-indent) 1))))
  12431. (while (and in-local-list
  12432. (or (and (= ind (car local-list-indent))
  12433. (not starter))
  12434. (< ind (car local-list-indent))))
  12435. (org-close-li)
  12436. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  12437. (pop local-list-num) (pop local-list-indent)
  12438. (setq in-local-list local-list-indent))
  12439. (cond
  12440. ((and starter
  12441. (or (not in-local-list)
  12442. (> ind (car local-list-indent))))
  12443. ;; Start new (level of ) list
  12444. (org-close-par-maybe)
  12445. (insert (if start-is-num "<ol>\n<li>\n" "<ul>\n<li>\n"))
  12446. (push start-is-num local-list-num)
  12447. (push ind local-list-indent)
  12448. (setq in-local-list t))
  12449. (starter
  12450. ;; continue current list
  12451. (org-close-li)
  12452. (insert "<li>\n")))
  12453. (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line)
  12454. (setq line
  12455. (replace-match
  12456. (if (equal (match-string 1 line) "X")
  12457. "<b>[X]</b>"
  12458. "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
  12459. t t line))))
  12460. ;; Empty lines start a new paragraph. If hand-formatted lists
  12461. ;; are not fully interpreted, lines starting with "-", "+", "*"
  12462. ;; also start a new paragraph.
  12463. (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
  12464. ;; Check if the line break needs to be conserved
  12465. (cond
  12466. ((string-match "\\\\\\\\[ \t]*$" line)
  12467. (setq line (replace-match "<br/>" t t line)))
  12468. (org-export-preserve-breaks
  12469. (setq line (concat line "<br/>"))))
  12470. (insert line "\n")))))
  12471. ;; Properly close all local lists and other lists
  12472. (when inquote (insert "</pre>\n"))
  12473. (when in-local-list
  12474. ;; Close any local lists before inserting a new header line
  12475. (while local-list-num
  12476. (org-close-li)
  12477. (insert (if (car local-list-num) "</ol>\n" "</ul>\n"))
  12478. (pop local-list-num))
  12479. (setq local-list-indent nil
  12480. in-local-list nil))
  12481. (org-html-level-start 1 nil umax
  12482. (and org-export-with-toc (<= level umax))
  12483. head-count)
  12484. (when (plist-get opt-plist :auto-postamble)
  12485. (when author
  12486. (insert "<p class=\"author\"> "
  12487. (nth 1 lang-words) ": " author "\n")
  12488. (when email
  12489. (insert "<a href=\"mailto:" email "\">&lt;"
  12490. email "&gt;</a>\n"))
  12491. (insert "</p>\n"))
  12492. (when (and date time)
  12493. (insert "<p class=\"date\"> "
  12494. (nth 2 lang-words) ": "
  12495. date " " time "</p>\n")))
  12496. (if org-export-html-with-timestamp
  12497. (insert org-export-html-html-helper-timestamp))
  12498. (insert (or (plist-get opt-plist :postamble) ""))
  12499. (insert "</body>\n</html>\n")
  12500. (normal-mode)
  12501. ;; remove empty paragraphs and lists
  12502. (goto-char (point-min))
  12503. (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
  12504. (replace-match ""))
  12505. (goto-char (point-min))
  12506. (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t)
  12507. (replace-match ""))
  12508. (save-buffer)
  12509. (goto-char (point-min)))))
  12510. (defun org-format-table-html (lines olines)
  12511. "Find out which HTML converter to use and return the HTML code."
  12512. (if (string-match "^[ \t]*|" (car lines))
  12513. ;; A normal org table
  12514. (org-format-org-table-html lines)
  12515. ;; Table made by table.el - test for spanning
  12516. (let* ((hlines (delq nil (mapcar
  12517. (lambda (x)
  12518. (if (string-match "^[ \t]*\\+-" x) x
  12519. nil))
  12520. lines)))
  12521. (first (car hlines))
  12522. (ll (and (string-match "\\S-+" first)
  12523. (match-string 0 first)))
  12524. (re (concat "^[ \t]*" (regexp-quote ll)))
  12525. (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
  12526. hlines))))
  12527. (if (and (not spanning)
  12528. (not org-export-prefer-native-exporter-for-tables))
  12529. ;; We can use my own converter with HTML conversions
  12530. (org-format-table-table-html lines)
  12531. ;; Need to use the code generator in table.el, with the original text.
  12532. (org-format-table-table-html-using-table-generate-source olines)))))
  12533. (defun org-format-org-table-html (lines)
  12534. "Format a table into HTML."
  12535. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  12536. (setq lines (nreverse lines))
  12537. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  12538. (setq lines (nreverse lines))
  12539. (when org-export-table-remove-special-lines
  12540. ;; Check if the table has a marking column. If yes remove the
  12541. ;; column and the special lines
  12542. (let* ((special
  12543. (not
  12544. (memq nil
  12545. (mapcar
  12546. (lambda (x)
  12547. (or (string-match "^[ \t]*|-" x)
  12548. (string-match "^[ \t]*| *\\([#!$*_^ ]\\) *|" x)))
  12549. lines)))))
  12550. (if special
  12551. (setq lines
  12552. (delq nil
  12553. (mapcar
  12554. (lambda (x)
  12555. (if (string-match "^[ \t]*| *[!_^] *|" x)
  12556. nil ; ignore this line
  12557. (and (or (string-match "^[ \t]*|-+\\+" x)
  12558. (string-match "^[ \t]*|[^|]*|" x))
  12559. (replace-match "|" t t x))))
  12560. lines))))))
  12561. (let ((head (and org-export-highlight-first-table-line
  12562. (delq nil (mapcar
  12563. (lambda (x) (string-match "^[ \t]*|-" x))
  12564. (cdr lines)))))
  12565. line fields html)
  12566. (setq html (concat org-export-html-table-tag "\n"))
  12567. (while (setq line (pop lines))
  12568. (catch 'next-line
  12569. (if (string-match "^[ \t]*|-" line)
  12570. (progn
  12571. (setq head nil) ;; head ends here, first time around
  12572. ;; ignore this line
  12573. (throw 'next-line t)))
  12574. ;; Break the line into fields
  12575. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  12576. (setq html (concat
  12577. html
  12578. "<tr>"
  12579. (mapconcat (lambda (x)
  12580. (if head
  12581. (concat "<th>" x "</th>")
  12582. (concat "<td>" x "</td>")))
  12583. fields "")
  12584. "</tr>\n"))))
  12585. (setq html (concat html "</table>\n"))
  12586. html))
  12587. (defun org-fake-empty-table-line (line)
  12588. "Replace everything except \"|\" with spaces."
  12589. (let ((i (length line))
  12590. (newstr (copy-sequence line)))
  12591. (while (> i 0)
  12592. (setq i (1- i))
  12593. (if (not (eq (aref newstr i) ?|))
  12594. (aset newstr i ?\ )))
  12595. newstr))
  12596. (defun org-format-table-table-html (lines)
  12597. "Format a table generated by table.el into HTML.
  12598. This conversion does *not* use `table-generate-source' from table.el.
  12599. This has the advantage that Org-mode's HTML conversions can be used.
  12600. But it has the disadvantage, that no cell- or row-spanning is allowed."
  12601. (let (line field-buffer
  12602. (head org-export-highlight-first-table-line)
  12603. fields html empty)
  12604. (setq html (concat org-export-html-table-tag "\n"))
  12605. (while (setq line (pop lines))
  12606. (setq empty "&nbsp;")
  12607. (catch 'next-line
  12608. (if (string-match "^[ \t]*\\+-" line)
  12609. (progn
  12610. (if field-buffer
  12611. (progn
  12612. (setq html (concat
  12613. html
  12614. "<tr>"
  12615. (mapconcat
  12616. (lambda (x)
  12617. (if (equal x "") (setq x empty))
  12618. (if head
  12619. (concat "<th>" x "</th>\n")
  12620. (concat "<td>" x "</td>\n")))
  12621. field-buffer "\n")
  12622. "</tr>\n"))
  12623. (setq head nil)
  12624. (setq field-buffer nil)))
  12625. ;; Ignore this line
  12626. (throw 'next-line t)))
  12627. ;; Break the line into fields and store the fields
  12628. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  12629. (if field-buffer
  12630. (setq field-buffer (mapcar
  12631. (lambda (x)
  12632. (concat x "<br/>" (pop fields)))
  12633. field-buffer))
  12634. (setq field-buffer fields))))
  12635. (setq html (concat html "</table>\n"))
  12636. html))
  12637. (defun org-format-table-table-html-using-table-generate-source (lines)
  12638. "Format a table into html, using `table-generate-source' from table.el.
  12639. This has the advantage that cell- or row-spanning is allowed.
  12640. But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
  12641. (require 'table)
  12642. (with-current-buffer (get-buffer-create " org-tmp1 ")
  12643. (erase-buffer)
  12644. (insert (mapconcat 'identity lines "\n"))
  12645. (goto-char (point-min))
  12646. (if (not (re-search-forward "|[^+]" nil t))
  12647. (error "Error processing table"))
  12648. (table-recognize-table)
  12649. (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
  12650. (table-generate-source 'html " org-tmp2 ")
  12651. (set-buffer " org-tmp2 ")
  12652. (buffer-substring (point-min) (point-max))))
  12653. (defun org-html-handle-time-stamps (s)
  12654. "Format time stamps in string S, or remove them."
  12655. (let (r b)
  12656. (while (string-match org-maybe-keyword-time-regexp s)
  12657. (or b (setq b (substring s 0 (match-beginning 0))))
  12658. (if (not org-export-with-timestamps)
  12659. (setq r (concat r (substring s 0 (match-beginning 0)))
  12660. s (substring s (match-end 0)))
  12661. (setq r (concat
  12662. r (substring s 0 (match-beginning 0))
  12663. (if (match-end 1)
  12664. (format "@<span class=\"timestamp-kwd\">%s @</span>"
  12665. (match-string 1 s)))
  12666. (format " @<span class=\"timestamp\">%s@</span>"
  12667. (substring (match-string 3 s) 1 -1)))
  12668. s (substring s (match-end 0)))))
  12669. ;; Line break of line started and ended with time stamp stuff
  12670. (if (not r)
  12671. s
  12672. (setq r (concat r s))
  12673. (unless (string-match "\\S-" (concat b s))
  12674. (setq r (concat r "@<br/>")))
  12675. r)))
  12676. (defun org-html-protect (s)
  12677. ;; convert & to &amp;, < to &lt; and > to &gt;
  12678. (let ((start 0))
  12679. (while (string-match "&" s start)
  12680. (setq s (replace-match "&amp;" t t s)
  12681. start (1+ (match-beginning 0))))
  12682. (while (string-match "<" s)
  12683. (setq s (replace-match "&lt;" t t s)))
  12684. (while (string-match ">" s)
  12685. (setq s (replace-match "&gt;" t t s))))
  12686. s)
  12687. (defun org-html-cleanup-toc-line (s)
  12688. "Remove tags and time staps from lines going into the toc."
  12689. (if (string-match " +:[a-zA-Z0-9_@:]+: *$" s)
  12690. (setq s (replace-match "" t t s)))
  12691. (while (string-match org-maybe-keyword-time-regexp s)
  12692. (setq s (replace-match "" t t s)))
  12693. s)
  12694. (defun org-html-expand (string)
  12695. "Prepare STRING for HTML export. Applies all active conversions.
  12696. If there are links in the string, don't modify these."
  12697. (let* (m s l res)
  12698. (while (setq m (string-match org-bracket-link-regexp string))
  12699. (setq s (substring string 0 m)
  12700. l (match-string 0 string)
  12701. string (substring string (match-end 0)))
  12702. (push (org-html-do-expand s) res)
  12703. (push l res))
  12704. (push (org-html-do-expand string) res)
  12705. (apply 'concat (nreverse res))))
  12706. (defun org-html-do-expand (s)
  12707. "Apply all active conversions to translate special ASCII to HTML."
  12708. (setq s (org-html-protect s))
  12709. (if org-export-html-expand
  12710. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  12711. (setq s (replace-match "<\\1>" t nil s))))
  12712. (if org-export-with-emphasize
  12713. (setq s (org-export-html-convert-emphasize s)))
  12714. (if org-export-with-sub-superscripts
  12715. (setq s (org-export-html-convert-sub-super s)))
  12716. (if org-export-with-TeX-macros
  12717. (let ((start 0) wd ass)
  12718. (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)" s start))
  12719. (setq wd (match-string 1 s))
  12720. (if (setq ass (assoc wd org-html-entities))
  12721. (setq s (replace-match (or (cdr ass)
  12722. (concat "&" (car ass) ";"))
  12723. t t s))
  12724. (setq start (+ start (length wd)))))))
  12725. s)
  12726. (defun org-create-multibrace-regexp (left right n)
  12727. "Create a regular expression which will match a balanced sexp.
  12728. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  12729. as single character strings.
  12730. The regexp returned will match the entire expression including the
  12731. delimiters. It will also define a single group which contains the
  12732. match except for the outermost delimiters. The maximum depth of
  12733. stacked delimiters is N. Escaping delimiters is not possible."
  12734. (let* ((nothing (concat "[^" "\\" left "\\" right "]*?"))
  12735. (or "\\|")
  12736. (re nothing)
  12737. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  12738. (while (> n 1)
  12739. (setq n (1- n)
  12740. re (concat re or next)
  12741. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  12742. (concat left "\\(" re "\\)" right)))
  12743. (defvar org-match-substring-regexp
  12744. (concat
  12745. "\\([^\\]\\)\\([_^]\\)\\("
  12746. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  12747. "\\|"
  12748. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  12749. "\\|"
  12750. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  12751. "The regular expression matching a sub- or superscript.")
  12752. (defun org-export-html-convert-sub-super (string)
  12753. "Convert sub- and superscripts in STRING to HTML."
  12754. (let (key c)
  12755. (while (string-match org-match-substring-regexp string)
  12756. (setq key (if (string= (match-string 2 string) "_") "sub" "sup"))
  12757. (setq c (or (match-string 8 string)
  12758. (match-string 6 string)
  12759. (match-string 5 string)))
  12760. (setq string (replace-match
  12761. (concat (match-string 1 string)
  12762. "<" key ">" c "</" key ">")
  12763. t t string)))
  12764. (while (string-match "\\\\\\([_^]\\)" string)
  12765. (setq string (replace-match (match-string 1 string) t t string))))
  12766. string)
  12767. (defun org-export-html-convert-emphasize (string)
  12768. "Apply emphasis."
  12769. (while (string-match org-emph-re string)
  12770. (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)))
  12771. string)
  12772. (defvar org-par-open nil)
  12773. (defun org-open-par ()
  12774. "Insert <p>, but first close previous paragraph if any."
  12775. (org-close-par-maybe)
  12776. (insert "\n<p>")
  12777. (setq org-par-open t))
  12778. (defun org-close-par-maybe ()
  12779. "Close paragraph if there is one open."
  12780. (when org-par-open
  12781. (insert "</p>")
  12782. (setq org-par-open nil)))
  12783. (defun org-close-li ()
  12784. "Close <li> if necessary."
  12785. (org-close-par-maybe)
  12786. (insert "</li>\n"))
  12787. ; (when (save-excursion
  12788. ; (re-search-backward "</?\\(ul\\|ol\\|li\\|[hH][0-9]\\)>" nil t))
  12789. ; (if (member (match-string 0) '("</ul>" "</ol>" "<li>"))
  12790. ; (insert "</li>"))))
  12791. (defun org-html-level-start (level title umax with-toc head-count)
  12792. "Insert a new level in HTML export.
  12793. When TITLE is nil, just close all open levels."
  12794. (org-close-par-maybe)
  12795. (let ((l (1+ (max level umax))))
  12796. (while (<= l org-level-max)
  12797. (if (aref levels-open (1- l))
  12798. (progn
  12799. (org-html-level-close l)
  12800. (aset levels-open (1- l) nil)))
  12801. (setq l (1+ l)))
  12802. (when title
  12803. ;; If title is nil, this means this function is called to close
  12804. ;; all levels, so the rest is done only if title is given
  12805. (when (string-match "\\(:[a-zA-Z0-9_@:]+:\\)[ \t]*$" title)
  12806. (setq title (replace-match
  12807. (if org-export-with-tags
  12808. (save-match-data
  12809. (concat
  12810. "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
  12811. (mapconcat 'identity (org-split-string
  12812. (match-string 1 title) ":")
  12813. "&nbsp;")
  12814. "</span>"))
  12815. "")
  12816. t t title)))
  12817. (if (> level umax)
  12818. (progn
  12819. (if (aref levels-open (1- level))
  12820. (progn
  12821. (org-close-li)
  12822. (insert "<li>" title "<br/>\n"))
  12823. (aset levels-open (1- level) t)
  12824. (org-close-par-maybe)
  12825. (insert "<ul>\n<li>" title "<br/>\n")))
  12826. (if org-export-with-section-numbers
  12827. (setq title (concat (org-section-number level) " " title)))
  12828. (setq level (+ level 1))
  12829. (if with-toc
  12830. (insert (format "\n<h%d><a name=\"sec-%d\">%s</a></h%d>\n"
  12831. level head-count title level))
  12832. (insert (format "\n<h%d>%s</h%d>\n" level title level)))
  12833. (org-open-par)))))
  12834. (defun org-html-level-close (&rest args)
  12835. "Terminate one level in HTML export."
  12836. (org-close-li)
  12837. (insert "</ul>"))
  12838. ;; Variable holding the vector with section numbers
  12839. (defvar org-section-numbers (make-vector org-level-max 0))
  12840. (defun org-init-section-numbers ()
  12841. "Initialize the vector for the section numbers."
  12842. (let* ((level -1)
  12843. (numbers (nreverse (org-split-string "" "\\.")))
  12844. (depth (1- (length org-section-numbers)))
  12845. (i depth) number-string)
  12846. (while (>= i 0)
  12847. (if (> i level)
  12848. (aset org-section-numbers i 0)
  12849. (setq number-string (or (car numbers) "0"))
  12850. (if (string-match "\\`[A-Z]\\'" number-string)
  12851. (aset org-section-numbers i
  12852. (- (string-to-char number-string) ?A -1))
  12853. (aset org-section-numbers i (string-to-number number-string)))
  12854. (pop numbers))
  12855. (setq i (1- i)))))
  12856. (defun org-section-number (&optional level)
  12857. "Return a string with the current section number.
  12858. When LEVEL is non-nil, increase section numbers on that level."
  12859. (let* ((depth (1- (length org-section-numbers))) idx n (string ""))
  12860. (when level
  12861. (when (> level -1)
  12862. (aset org-section-numbers
  12863. level (1+ (aref org-section-numbers level))))
  12864. (setq idx (1+ level))
  12865. (while (<= idx depth)
  12866. (if (not (= idx 1))
  12867. (aset org-section-numbers idx 0))
  12868. (setq idx (1+ idx))))
  12869. (setq idx 0)
  12870. (while (<= idx depth)
  12871. (setq n (aref org-section-numbers idx))
  12872. (setq string (concat string (if (not (string= string "")) "." "")
  12873. (int-to-string n)))
  12874. (setq idx (1+ idx)))
  12875. (save-match-data
  12876. (if (string-match "\\`\\([@0]\\.\\)+" string)
  12877. (setq string (replace-match "" t nil string)))
  12878. (if (string-match "\\(\\.0\\)+\\'" string)
  12879. (setq string (replace-match "" t nil string))))
  12880. string))
  12881. ;;;###autoload
  12882. (defun org-export-icalendar-this-file ()
  12883. "Export current file as an iCalendar file.
  12884. The iCalendar file will be located in the same directory as the Org-mode
  12885. file, but with extension `.ics'."
  12886. (interactive)
  12887. (org-export-icalendar nil buffer-file-name))
  12888. (defun org-export-as-xoxo-insert-into (buffer &rest output)
  12889. (with-current-buffer buffer
  12890. (apply 'insert output)))
  12891. (defun org-export-as-xoxo (&optional buffer)
  12892. "Export the org buffer as XOXO.
  12893. The XOXO buffer is named *xoxo-<source buffer name>*"
  12894. (interactive (list (current-buffer)))
  12895. ;; A quickie abstraction
  12896. ;; Output everything as XOXO
  12897. (with-current-buffer (get-buffer buffer)
  12898. (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
  12899. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  12900. (org-infile-export-plist)))
  12901. (filename (concat (file-name-as-directory
  12902. (org-export-directory :xoxo opt-plist))
  12903. (file-name-sans-extension
  12904. (file-name-nondirectory buffer-file-name))
  12905. ".html"))
  12906. (out (find-file-noselect filename))
  12907. (last-level 1)
  12908. (hanging-li nil))
  12909. ;; Check the output buffer is empty.
  12910. (with-current-buffer out (erase-buffer))
  12911. ;; Kick off the output
  12912. (org-export-as-xoxo-insert-into out "<ol class='xoxo'>\n")
  12913. (while (re-search-forward "^\\(\\*+\\) \\(.+\\)" (point-max) 't)
  12914. (let* ((hd (match-string-no-properties 1))
  12915. (level (length hd))
  12916. (text (concat
  12917. (match-string-no-properties 2)
  12918. (save-excursion
  12919. (goto-char (match-end 0))
  12920. (let ((str ""))
  12921. (catch 'loop
  12922. (while 't
  12923. (forward-line)
  12924. (if (looking-at "^[ \t]\\(.*\\)")
  12925. (setq str (concat str (match-string-no-properties 1)))
  12926. (throw 'loop str)))))))))
  12927. ;; Handle level rendering
  12928. (cond
  12929. ((> level last-level)
  12930. (org-export-as-xoxo-insert-into out "\n<ol>\n"))
  12931. ((< level last-level)
  12932. (dotimes (- (- last-level level) 1)
  12933. (if hanging-li
  12934. (org-export-as-xoxo-insert-into out "</li>\n"))
  12935. (org-export-as-xoxo-insert-into out "</ol>\n"))
  12936. (when hanging-li
  12937. (org-export-as-xoxo-insert-into out "</li>\n")
  12938. (setq hanging-li nil)))
  12939. ((equal level last-level)
  12940. (if hanging-li
  12941. (org-export-as-xoxo-insert-into out "</li>\n")))
  12942. )
  12943. (setq last-level level)
  12944. ;; And output the new li
  12945. (setq hanging-li 't)
  12946. (if (equal ?+ (elt text 0))
  12947. (org-export-as-xoxo-insert-into out "<li class='" (substring text 1) "'>")
  12948. (org-export-as-xoxo-insert-into out "<li>" text))))
  12949. ;; Finally finish off the ol
  12950. (dotimes (- last-level 1)
  12951. (if hanging-li
  12952. (org-export-as-xoxo-insert-into out "</li>\n"))
  12953. (org-export-as-xoxo-insert-into out "</ol>\n"))
  12954. ;; Finish the buffer off and clean it up.
  12955. (switch-to-buffer-other-window out)
  12956. (indent-region (point-min) (point-max) nil)
  12957. (save-buffer)
  12958. (goto-char (point-min))
  12959. )))
  12960. ;;;###autoload
  12961. (defun org-export-icalendar-all-agenda-files ()
  12962. "Export all files in `org-agenda-files' to iCalendar .ics files.
  12963. Each iCalendar file will be located in the same directory as the Org-mode
  12964. file, but with extension `.ics'."
  12965. (interactive)
  12966. (apply 'org-export-icalendar nil (org-agenda-files t)))
  12967. ;;;###autoload
  12968. (defun org-export-icalendar-combine-agenda-files ()
  12969. "Export all files in `org-agenda-files' to a single combined iCalendar file.
  12970. The file is stored under the name `org-combined-agenda-icalendar-file'."
  12971. (interactive)
  12972. (apply 'org-export-icalendar t (org-agenda-files t)))
  12973. (defun org-export-icalendar (combine &rest files)
  12974. "Create iCalendar files for all elements of FILES.
  12975. If COMBINE is non-nil, combine all calendar entries into a single large
  12976. file and store it under the name `org-combined-agenda-icalendar-file'."
  12977. (save-excursion
  12978. (let* ((dir (org-export-directory
  12979. :ical (list :publishing-directory
  12980. org-export-publishing-directory)))
  12981. file ical-file ical-buffer category started org-agenda-new-buffers)
  12982. (when combine
  12983. (setq ical-file
  12984. (if (file-name-absolute-p org-combined-agenda-icalendar-file)
  12985. org-combined-agenda-icalendar-file
  12986. (expand-file-name org-combined-agenda-icalendar-file dir))
  12987. ical-buffer (org-get-agenda-file-buffer ical-file))
  12988. (set-buffer ical-buffer) (erase-buffer))
  12989. (while (setq file (pop files))
  12990. (catch 'nextfile
  12991. (org-check-agenda-file file)
  12992. (set-buffer (org-get-agenda-file-buffer file))
  12993. (unless combine
  12994. (setq ical-file (concat (file-name-as-directory dir)
  12995. (file-name-sans-extension
  12996. (file-name-nondirectory buffer-file-name))
  12997. ".ics"))
  12998. (setq ical-buffer (org-get-agenda-file-buffer ical-file))
  12999. (with-current-buffer ical-buffer (erase-buffer)))
  13000. (setq category (or org-category
  13001. (file-name-sans-extension
  13002. (file-name-nondirectory buffer-file-name))))
  13003. (if (symbolp category) (setq category (symbol-name category)))
  13004. (let ((standard-output ical-buffer))
  13005. (if combine
  13006. (and (not started) (setq started t)
  13007. (org-start-icalendar-file org-icalendar-combined-name))
  13008. (org-start-icalendar-file category))
  13009. (org-print-icalendar-entries combine category)
  13010. (when (or (and combine (not files)) (not combine))
  13011. (org-finish-icalendar-file)
  13012. (set-buffer ical-buffer)
  13013. (save-buffer)
  13014. (run-hooks 'org-after-save-iCalendar-file-hook)))))
  13015. (org-release-buffers org-agenda-new-buffers))))
  13016. (defvar org-after-save-iCalendar-file-hook nil
  13017. "Hook run after an iCalendar file has been saved.
  13018. The iCalendar buffer is still current when this hook is run.
  13019. A good way to use this is to tell a desktop calenndar application to re-read
  13020. the iCalendar file.")
  13021. (defun org-print-icalendar-entries (&optional combine category)
  13022. "Print iCalendar entries for the current Org-mode file to `standard-output'.
  13023. When COMBINE is non nil, add the category to each line."
  13024. (let ((re2 (concat "--?-?\\(" org-ts-regexp "\\)"))
  13025. (dts (org-ical-ts-to-string
  13026. (format-time-string (cdr org-time-stamp-formats) (current-time))
  13027. "DTSTART"))
  13028. hd ts ts2 state (inc t) pos scheduledp deadlinep tmp pri)
  13029. (save-excursion
  13030. (goto-char (point-min))
  13031. (while (re-search-forward org-ts-regexp nil t)
  13032. (setq pos (match-beginning 0)
  13033. ts (match-string 0)
  13034. inc t
  13035. hd (org-get-heading))
  13036. (if (looking-at re2)
  13037. (progn
  13038. (goto-char (match-end 0))
  13039. (setq ts2 (match-string 1) inc nil))
  13040. (setq ts2 ts
  13041. tmp (buffer-substring (max (point-min)
  13042. (- pos org-ds-keyword-length))
  13043. pos)
  13044. deadlinep (string-match org-deadline-regexp tmp)
  13045. scheduledp (string-match org-scheduled-regexp tmp)
  13046. ;; donep (org-entry-is-done-p)
  13047. ))
  13048. (if (or (string-match org-tr-regexp hd)
  13049. (string-match org-ts-regexp hd))
  13050. (setq hd (replace-match "" t t hd)))
  13051. (if combine
  13052. (setq hd (concat hd " (category " category ")")))
  13053. (if deadlinep (setq hd (concat "DL: " hd " This is a deadline")))
  13054. (if scheduledp (setq hd (concat "S: " hd " Scheduled for this date")))
  13055. (princ (format "BEGIN:VEVENT
  13056. %s
  13057. %s
  13058. SUMMARY:%s
  13059. END:VEVENT\n"
  13060. (org-ical-ts-to-string ts "DTSTART")
  13061. (org-ical-ts-to-string ts2 "DTEND" inc)
  13062. hd)))
  13063. (when org-icalendar-include-todo
  13064. (goto-char (point-min))
  13065. (while (re-search-forward org-todo-line-regexp nil t)
  13066. (setq state (match-string 1))
  13067. (unless (equal state org-done-string)
  13068. (setq hd (match-string 3))
  13069. (if (string-match org-priority-regexp hd)
  13070. (setq pri (string-to-char (match-string 2 hd))
  13071. hd (concat (substring hd 0 (match-beginning 1))
  13072. (substring hd (- (match-end 1)))))
  13073. (setq pri org-default-priority))
  13074. (setq pri (floor (1+ (* 8. (/ (float (- org-lowest-priority pri))
  13075. (- org-lowest-priority ?A))))))
  13076. (princ (format "BEGIN:VTODO
  13077. %s
  13078. SUMMARY:%s
  13079. SEQUENCE:1
  13080. PRIORITY:%d
  13081. END:VTODO\n"
  13082. dts hd pri))))))))
  13083. (defun org-start-icalendar-file (name)
  13084. "Start an iCalendar file by inserting the header."
  13085. (let ((user user-full-name)
  13086. (name (or name "unknown"))
  13087. (timezone (cadr (current-time-zone))))
  13088. (princ
  13089. (format "BEGIN:VCALENDAR
  13090. VERSION:2.0
  13091. X-WR-CALNAME:%s
  13092. PRODID:-//%s//Emacs with Org-mode//EN
  13093. X-WR-TIMEZONE:%s
  13094. CALSCALE:GREGORIAN\n" name user timezone))))
  13095. (defun org-finish-icalendar-file ()
  13096. "Finish an iCalendar file by inserting the END statement."
  13097. (princ "END:VCALENDAR\n"))
  13098. (defun org-ical-ts-to-string (s keyword &optional inc)
  13099. "Take a time string S and convert it to iCalendar format.
  13100. KEYWORD is added in front, to make a complete line like DTSTART....
  13101. When INC is non-nil, increase the hour by two (if time string contains
  13102. a time), or the day by one (if it does not contain a time)."
  13103. (let ((t1 (org-parse-time-string s 'nodefault))
  13104. t2 fmt have-time time)
  13105. (if (and (car t1) (nth 1 t1) (nth 2 t1))
  13106. (setq t2 t1 have-time t)
  13107. (setq t2 (org-parse-time-string s)))
  13108. (let ((s (car t2)) (mi (nth 1 t2)) (h (nth 2 t2))
  13109. (d (nth 3 t2)) (m (nth 4 t2)) (y (nth 5 t2)))
  13110. (when inc
  13111. (if have-time (setq h (+ 2 h)) (setq d (1+ d))))
  13112. (setq time (encode-time s mi h d m y)))
  13113. (setq fmt (if have-time ":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
  13114. (concat keyword (format-time-string fmt time))))
  13115. ;;; Key bindings
  13116. ;; - Bindings in Org-mode map are currently
  13117. ;; 0123456789abcdefghijklmnopqrstuvwxyz!?@#$%^&-+*/=()_{}[]:;"|,.<>~`'\t the alphabet
  13118. ;; abcd fgh j lmnopqrstuvwxyz!? #$ ^ -+*/= [] ; |,.<>~ '\t necessary bindings
  13119. ;; e (?) useful from outline-mode
  13120. ;; i k @ expendable from outline-mode
  13121. ;; 0123456789 % & ()_{} " ` free
  13122. ;; Make `C-c C-x' a prefix key
  13123. (define-key org-mode-map "\C-c\C-x" (make-sparse-keymap))
  13124. ;; TAB key with modifiers
  13125. (define-key org-mode-map "\C-i" 'org-cycle)
  13126. (define-key org-mode-map [(tab)] 'org-cycle)
  13127. (define-key org-mode-map [(meta tab)] 'org-complete)
  13128. (define-key org-mode-map "\M-\C-i" 'org-complete) ; for tty emacs
  13129. ;; The following line is necessary under Suse GNU/Linux
  13130. (unless (featurep 'xemacs)
  13131. (define-key org-mode-map [S-iso-lefttab] 'org-shifttab))
  13132. (define-key org-mode-map [(shift tab)] 'org-shifttab)
  13133. (define-key org-mode-map (org-key 'S-return) 'org-table-copy-down)
  13134. (define-key org-mode-map "\C-c\C-xc" 'org-table-copy-down) ; tty
  13135. (define-key org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  13136. (define-key org-mode-map "\C-c\C-xM" 'org-insert-todo-heading) ; tty
  13137. (define-key org-mode-map [(meta return)] 'org-meta-return)
  13138. (define-key org-mode-map "\C-c\C-xm" 'org-meta-return) ; tty emacs
  13139. (define-key org-mode-map [?\e (return)] 'org-meta-return) ; tty emacs
  13140. ;; Cursor keys with modifiers
  13141. (define-key org-mode-map [(meta left)] 'org-metaleft)
  13142. (define-key org-mode-map [?\e (left)] 'org-metaleft) ; for tty emacs
  13143. (define-key org-mode-map "\C-c\C-xl" 'org-metaleft) ; for tty emacs
  13144. (define-key org-mode-map [(meta right)] 'org-metaright)
  13145. (define-key org-mode-map [?\e (right)] 'org-metaright) ; for tty emacs
  13146. (define-key org-mode-map "\C-c\C-xr" 'org-metaright) ; for tty emacs
  13147. (define-key org-mode-map [(meta up)] 'org-metaup)
  13148. (define-key org-mode-map [?\e (up)] 'org-metaup) ; for tty emacs
  13149. (define-key org-mode-map "\C-c\C-xu" 'org-metaup) ; for tty emacs
  13150. (define-key org-mode-map [(meta down)] 'org-metadown)
  13151. (define-key org-mode-map [?\e (down)] 'org-metadown) ; for tty emacs
  13152. (define-key org-mode-map "\C-c\C-xd" 'org-metadown) ; for tty emacs
  13153. (define-key org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  13154. (define-key org-mode-map "\C-c\C-xL" 'org-shiftmetaleft) ; tty
  13155. (define-key org-mode-map [(meta shift right)] 'org-shiftmetaright)
  13156. (define-key org-mode-map "\C-c\C-xR" 'org-shiftmetaright) ; tty
  13157. (define-key org-mode-map [(meta shift up)] 'org-shiftmetaup)
  13158. (define-key org-mode-map "\C-c\C-xU" 'org-shiftmetaup) ; tty
  13159. (define-key org-mode-map [(meta shift down)] 'org-shiftmetadown)
  13160. (define-key org-mode-map "\C-c\C-xD" 'org-shiftmetadown) ; tty
  13161. (define-key org-mode-map (org-key 'S-up) 'org-shiftup)
  13162. (define-key org-mode-map [?\C-c ?\C-x (up)] 'org-shiftup)
  13163. (define-key org-mode-map (org-key 'S-down) 'org-shiftdown)
  13164. (define-key org-mode-map [?\C-c ?\C-x (down)] 'org-shiftdown)
  13165. (define-key org-mode-map (org-key 'S-left) 'org-shiftleft)
  13166. (define-key org-mode-map [?\C-c ?\C-x (left)] 'org-shiftleft)
  13167. (define-key org-mode-map (org-key 'S-right) 'org-shiftright)
  13168. (define-key org-mode-map [?\C-c ?\C-x (right)] 'org-shiftright)
  13169. ;; All the other keys
  13170. (define-key org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  13171. (define-key org-mode-map "\C-c$" 'org-archive-subtree)
  13172. (define-key org-mode-map "\C-c\C-j" 'org-goto)
  13173. (define-key org-mode-map "\C-c\C-t" 'org-todo)
  13174. (define-key org-mode-map "\C-c\C-s" 'org-schedule)
  13175. (define-key org-mode-map "\C-c\C-d" 'org-deadline)
  13176. (define-key org-mode-map "\C-c;" 'org-toggle-comment)
  13177. (define-key org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  13178. (define-key org-mode-map "\C-c\C-w" 'org-check-deadlines)
  13179. (define-key org-mode-map "\C-c/" 'org-occur) ; Minor-mode reserved
  13180. (define-key org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  13181. (define-key org-mode-map "\C-c\C-m" 'org-insert-heading)
  13182. (define-key org-mode-map "\M-\C-m" 'org-insert-heading)
  13183. (define-key org-mode-map "\C-c\C-l" 'org-insert-link)
  13184. (define-key org-mode-map "\C-c\C-o" 'org-open-at-point)
  13185. (define-key org-mode-map "\C-c%" 'org-mark-ring-push)
  13186. (define-key org-mode-map "\C-c&" 'org-mark-ring-goto)
  13187. (define-key org-mode-map "\C-c\C-z" 'org-time-stamp) ; Alternative binding
  13188. (define-key org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  13189. (define-key org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  13190. (define-key org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  13191. (define-key org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  13192. (define-key org-mode-map "\C-c>" 'org-goto-calendar)
  13193. (define-key org-mode-map "\C-c<" 'org-date-from-calendar)
  13194. (define-key org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  13195. (define-key org-mode-map "\C-c[" 'org-agenda-file-to-front)
  13196. (define-key org-mode-map "\C-c]" 'org-remove-file)
  13197. (define-key org-mode-map "\C-c\C-r" 'org-timeline)
  13198. (define-key org-mode-map "\C-c-" 'org-table-insert-hline)
  13199. (define-key org-mode-map "\C-c^" 'org-table-sort-lines)
  13200. (define-key org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  13201. (define-key org-mode-map "\C-m" 'org-return)
  13202. (define-key org-mode-map "\C-c?" 'org-table-current-column)
  13203. (define-key org-mode-map "\C-c " 'org-table-blank-field)
  13204. (define-key org-mode-map "\C-c+" 'org-table-sum)
  13205. (define-key org-mode-map "\C-c=" 'org-table-eval-formula)
  13206. (define-key org-mode-map "\C-c'" 'org-table-edit-formulas)
  13207. (define-key org-mode-map "\C-c`" 'org-table-edit-field)
  13208. (define-key org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  13209. (define-key org-mode-map "\C-c*" 'org-table-recalculate)
  13210. (define-key org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  13211. (define-key org-mode-map "\C-c~" 'org-table-create-with-table.el)
  13212. (define-key org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  13213. (define-key org-mode-map "\C-c\C-e" 'org-export)
  13214. ;(define-key org-mode-map "\C-c\C-xa" 'org-export-as-ascii)
  13215. ;(define-key org-mode-map "\C-c\C-x\C-a" 'org-export-as-ascii)
  13216. ;(define-key org-mode-map "\C-c\C-xv" 'org-export-visible)
  13217. ;(define-key org-mode-map "\C-c\C-x\C-v" 'org-export-visible)
  13218. ;; OPML support is only an option for the future
  13219. ;(define-key org-mode-map "\C-c\C-xo" 'org-export-as-opml)
  13220. ;(define-key org-mode-map "\C-c\C-x\C-o" 'org-export-as-opml)
  13221. ;(define-key org-mode-map "\C-c\C-xi" 'org-export-icalendar-this-file)
  13222. ;(define-key org-mode-map "\C-c\C-x\C-i" 'org-export-icalendar-all-agenda-files)
  13223. ;(define-key org-mode-map "\C-c\C-xc" 'org-export-icalendar-combine-agenda-files)
  13224. ;(define-key org-mode-map "\C-c\C-x\C-c" 'org-export-icalendar-combine-agenda-files)
  13225. ;(define-key org-mode-map "\C-c\C-xt" 'org-insert-export-options-template)
  13226. (define-key org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  13227. ;(define-key org-mode-map "\C-c\C-xh" 'org-export-as-html)
  13228. ;(define-key org-mode-map "\C-c\C-xx" 'org-export-as-xoxo)
  13229. ;(define-key org-mode-map "\C-c\C-x\C-x" 'org-export-as-xoxo)
  13230. ;(define-key org-mode-map "\C-c\C-xb" 'org-export-as-html-and-open)
  13231. ;(define-key org-mode-map "\C-c\C-x\C-b" 'org-export-as-html-and-open)
  13232. (define-key org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  13233. (define-key org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  13234. (define-key org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  13235. (define-key org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  13236. (define-key org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  13237. (define-key org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  13238. (define-key org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  13239. (define-key org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  13240. ;(define-key org-mode-map "\C-c\C-ef" 'org-publish-current-file)
  13241. ;(define-key org-mode-map "\C-c\C-ep" 'org-publish-current-project)
  13242. ;(define-key org-mode-map "\C-c\C-ec" 'org-publish)
  13243. ;(define-key org-mode-map "\C-c\C-ea" 'org-publish-all)
  13244. ;(define-key org-mode-map "\C-c\C-e\C-f" 'org-publish-current-file)
  13245. ;(define-key org-mode-map "\C-c\C-e\C-p" 'org-publish-current-project)
  13246. ;(define-key org-mode-map "\C-c\C-e\C-c" 'org-publish)
  13247. ;(define-key org-mode-map "\C-c\C-e\C-a" 'org-publish-all)
  13248. (when (featurep 'xemacs)
  13249. (define-key org-mode-map 'button3 'popup-mode-menu))
  13250. (defsubst org-table-p () (org-at-table-p))
  13251. (defun org-self-insert-command (N)
  13252. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  13253. If the cursor is in a table looking at whitespace, the whitespace is
  13254. overwritten, and the table is not marked as requiring realignment."
  13255. (interactive "p")
  13256. (if (and (org-table-p)
  13257. (progn
  13258. ;; check if we blank the field, and if that triggers align
  13259. (and org-table-auto-blank-field
  13260. (member last-command
  13261. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  13262. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  13263. ;; got extra space, this field does not determine column width
  13264. (let (org-table-may-need-update) (org-table-blank-field))
  13265. ;; no extra space, this field may determine column width
  13266. (org-table-blank-field)))
  13267. t)
  13268. (eq N 1)
  13269. (looking-at "[^|\n]* |"))
  13270. (let (org-table-may-need-update)
  13271. (goto-char (1- (match-end 0)))
  13272. (delete-backward-char 1)
  13273. (goto-char (match-beginning 0))
  13274. (self-insert-command N))
  13275. (setq org-table-may-need-update t)
  13276. (self-insert-command N)))
  13277. (defun org-delete-backward-char (N)
  13278. "Like `delete-backward-char', insert whitespace at field end in tables.
  13279. When deleting backwards, in tables this function will insert whitespace in
  13280. front of the next \"|\" separator, to keep the table aligned. The table will
  13281. still be marked for re-alignment if the field did fill the entire column,
  13282. because, in this case the deletion might narrow the column."
  13283. (interactive "p")
  13284. (if (and (org-table-p)
  13285. (eq N 1)
  13286. (string-match "|" (buffer-substring (point-at-bol) (point)))
  13287. (looking-at ".*?|"))
  13288. (let ((pos (point))
  13289. (noalign (looking-at "[^|\n\r]* |"))
  13290. (c org-table-may-need-update))
  13291. (backward-delete-char N)
  13292. (skip-chars-forward "^|")
  13293. (insert " ")
  13294. (goto-char (1- pos))
  13295. ;; noalign: if there were two spaces at the end, this field
  13296. ;; does not determine the width of the column.
  13297. (if noalign (setq org-table-may-need-update c)))
  13298. (backward-delete-char N)))
  13299. (defun org-delete-char (N)
  13300. "Like `delete-char', but insert whitespace at field end in tables.
  13301. When deleting characters, in tables this function will insert whitespace in
  13302. front of the next \"|\" separator, to keep the table aligned. The table will
  13303. still be marked for re-alignment if the field did fill the entire column,
  13304. because, in this case the deletion might narrow the column."
  13305. (interactive "p")
  13306. (if (and (org-table-p)
  13307. (not (bolp))
  13308. (not (= (char-after) ?|))
  13309. (eq N 1))
  13310. (if (looking-at ".*?|")
  13311. (let ((pos (point))
  13312. (noalign (looking-at "[^|\n\r]* |"))
  13313. (c org-table-may-need-update))
  13314. (replace-match (concat
  13315. (substring (match-string 0) 1 -1)
  13316. " |"))
  13317. (goto-char pos)
  13318. ;; noalign: if there were two spaces at the end, this field
  13319. ;; does not determine the width of the column.
  13320. (if noalign (setq org-table-may-need-update c)))
  13321. (delete-char N))
  13322. (delete-char N)))
  13323. ;; How to do this: Measure non-white length of current string
  13324. ;; If equal to column width, we should realign.
  13325. (defun org-remap (map &rest commands)
  13326. "In MAP, remap the functions given in COMMANDS.
  13327. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  13328. (let (new old)
  13329. (while commands
  13330. (setq old (pop commands) new (pop commands))
  13331. (if (fboundp 'command-remapping)
  13332. (define-key map (vector 'remap old) new)
  13333. (substitute-key-definition old new map global-map)))))
  13334. (when (eq org-enable-table-editor 'optimized)
  13335. ;; If the user wants maximum table support, we need to hijack
  13336. ;; some standard editing functions
  13337. (org-remap org-mode-map
  13338. 'self-insert-command 'org-self-insert-command
  13339. 'delete-char 'org-delete-char
  13340. 'delete-backward-char 'org-delete-backward-char)
  13341. (define-key org-mode-map "|" 'org-force-self-insert))
  13342. (defun org-shiftcursor-error ()
  13343. "Throw an error because Shift-Cursor command was applied in wrong context."
  13344. (error "This command is active in special context like tables, headlines or timestamps"))
  13345. (defun org-shifttab (&optional arg)
  13346. "Global visibility cycling or move to previous table field.
  13347. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  13348. on context.
  13349. See the individual commands for more information."
  13350. (interactive "P")
  13351. (cond
  13352. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  13353. (t (call-interactively 'org-global-cycle))))
  13354. (defun org-shiftmetaleft ()
  13355. "Promote subtree or delete table column.
  13356. Calls `org-promote-subtree' or `org-table-delete-column', depending on context.
  13357. See the individual commands for more information."
  13358. (interactive)
  13359. (cond
  13360. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  13361. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  13362. ((org-at-item-p) (call-interactively 'org-outdent-item))
  13363. (t (org-shiftcursor-error))))
  13364. (defun org-shiftmetaright ()
  13365. "Demote subtree or insert table column.
  13366. Calls `org-demote-subtree' or `org-table-insert-column', depending on context.
  13367. See the individual commands for more information."
  13368. (interactive)
  13369. (cond
  13370. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  13371. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  13372. ((org-at-item-p) (call-interactively 'org-indent-item))
  13373. (t (org-shiftcursor-error))))
  13374. (defun org-shiftmetaup (&optional arg)
  13375. "Move subtree up or kill table row.
  13376. Calls `org-move-subtree-up' or `org-table-kill-row' or
  13377. `org-move-item-up' depending on context. See the individual commands
  13378. for more information."
  13379. (interactive "P")
  13380. (cond
  13381. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  13382. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13383. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13384. (t (org-shiftcursor-error))))
  13385. (defun org-shiftmetadown (&optional arg)
  13386. "Move subtree down or insert table row.
  13387. Calls `org-move-subtree-down' or `org-table-insert-row' or
  13388. `org-move-item-down', depending on context. See the individual
  13389. commands for more information."
  13390. (interactive "P")
  13391. (cond
  13392. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  13393. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13394. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13395. (t (org-shiftcursor-error))))
  13396. (defun org-metaleft (&optional arg)
  13397. "Promote heading or move table column to left.
  13398. Calls `org-do-promote' or `org-table-move-column', depending on context.
  13399. With no specific context, calls the Emacs default `backward-word'.
  13400. See the individual commands for more information."
  13401. (interactive "P")
  13402. (cond
  13403. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  13404. ((or (org-on-heading-p) (org-region-active-p))
  13405. (call-interactively 'org-do-promote))
  13406. (t (call-interactively 'backward-word))))
  13407. (defun org-metaright (&optional arg)
  13408. "Demote subtree or move table column to right.
  13409. Calls `org-do-demote' or `org-table-move-column', depending on context.
  13410. With no specific context, calls the Emacs default `forward-word'.
  13411. See the individual commands for more information."
  13412. (interactive "P")
  13413. (cond
  13414. ((org-at-table-p) (call-interactively 'org-table-move-column))
  13415. ((or (org-on-heading-p) (org-region-active-p))
  13416. (call-interactively 'org-do-demote))
  13417. (t (call-interactively 'forward-word))))
  13418. (defun org-metaup (&optional arg)
  13419. "Move subtree up or move table row up.
  13420. Calls `org-move-subtree-up' or `org-table-move-row' or
  13421. `org-move-item-up', depending on context. See the individual commands
  13422. for more information."
  13423. (interactive "P")
  13424. (cond
  13425. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  13426. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13427. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13428. (t (org-shiftcursor-error))))
  13429. (defun org-metadown (&optional arg)
  13430. "Move subtree down or move table row down.
  13431. Calls `org-move-subtree-down' or `org-table-move-row' or
  13432. `org-move-item-down', depending on context. See the individual
  13433. commands for more information."
  13434. (interactive "P")
  13435. (cond
  13436. ((org-at-table-p) (call-interactively 'org-table-move-row))
  13437. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13438. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13439. (t (org-shiftcursor-error))))
  13440. (defun org-shiftup (&optional arg)
  13441. "Increase item in timestamp or increase priority of current headline.
  13442. Calls `org-timestamp-up' or `org-priority-up', depending on context.
  13443. See the individual commands for more information."
  13444. (interactive "P")
  13445. (cond
  13446. ((org-at-timestamp-p) (call-interactively 'org-timestamp-up))
  13447. ((org-on-heading-p) (call-interactively 'org-priority-up))
  13448. ((org-at-item-p) (call-interactively 'org-previous-item))
  13449. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  13450. (defun org-shiftdown (&optional arg)
  13451. "Decrease item in timestamp or decrease priority of current headline.
  13452. Calls `org-timestamp-down' or `org-priority-down', depending on context.
  13453. See the individual commands for more information."
  13454. (interactive "P")
  13455. (cond
  13456. ((org-at-timestamp-p) (call-interactively 'org-timestamp-down))
  13457. ((org-on-heading-p) (call-interactively 'org-priority-down))
  13458. (t (call-interactively 'org-next-item))))
  13459. (defun org-shiftright ()
  13460. "Next TODO keyword or timestamp one day later, depending on context."
  13461. (interactive)
  13462. (cond
  13463. ((org-at-timestamp-p) (call-interactively 'org-timestamp-up-day))
  13464. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  13465. (t (org-shiftcursor-error))))
  13466. (defun org-shiftleft ()
  13467. "Previous TODO keyword or timestamp one day earlier, depending on context."
  13468. (interactive)
  13469. (cond
  13470. ((org-at-timestamp-p) (call-interactively 'org-timestamp-down-day))
  13471. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  13472. (t (org-shiftcursor-error))))
  13473. (defun org-copy-special ()
  13474. "Copy region in table or copy current subtree.
  13475. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  13476. See the individual commands for more information."
  13477. (interactive)
  13478. (call-interactively
  13479. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  13480. (defun org-cut-special ()
  13481. "Cut region in table or cut current subtree.
  13482. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  13483. See the individual commands for more information."
  13484. (interactive)
  13485. (call-interactively
  13486. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  13487. (defun org-paste-special (arg)
  13488. "Paste rectangular region into table, or past subtree relative to level.
  13489. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  13490. See the individual commands for more information."
  13491. (interactive "P")
  13492. (if (org-at-table-p)
  13493. (org-table-paste-rectangle)
  13494. (org-paste-subtree arg)))
  13495. (defun org-ctrl-c-ctrl-c (&optional arg)
  13496. "Set tags in headline, or update according to changed information at point.
  13497. This command does many different things, depending on context:
  13498. - If the cursor is in a headline, prompt for tags and insert them
  13499. into the current line, aligned to `org-tags-column'. When called
  13500. with prefix arg, realign all tags in the current buffer.
  13501. - If the cursor is in one of the special #+KEYWORD lines, this
  13502. triggers scanning the buffer for these lines and updating the
  13503. information.
  13504. - If the cursor is inside a table, realign the table. This command
  13505. works even if the automatic table editor has been turned off.
  13506. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  13507. the entire table.
  13508. - If the cursor is inside a table created by the table.el package,
  13509. activate that table.
  13510. - If the current buffer is a remember buffer, close note and file it.
  13511. with a prefix argument, file it without further interaction to the default
  13512. location.
  13513. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  13514. links in this buffer.
  13515. - If the cursor is on a numbered item in a plain list, renumber the
  13516. ordered list."
  13517. (interactive "P")
  13518. (let ((org-enable-table-editor t))
  13519. (cond
  13520. (org-clock-overlays
  13521. (org-remove-clock-overlays)
  13522. (message "Clock overlays removed"))
  13523. (org-occur-highlights
  13524. (org-remove-occur-highlights)
  13525. (message "occur highlights removed"))
  13526. ((and (local-variable-p 'org-finish-function (current-buffer))
  13527. (fboundp org-finish-function))
  13528. (funcall org-finish-function))
  13529. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  13530. ((org-on-heading-p) (call-interactively 'org-set-tags))
  13531. ((org-at-table.el-p)
  13532. (require 'table)
  13533. (beginning-of-line 1)
  13534. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  13535. (call-interactively 'table-recognize-table))
  13536. ((org-at-table-p)
  13537. (org-table-maybe-eval-formula)
  13538. (if arg
  13539. (call-interactively 'org-table-recalculate)
  13540. (org-table-maybe-recalculate-line))
  13541. (call-interactively 'org-table-align))
  13542. ((org-at-item-checkbox-p)
  13543. (call-interactively 'org-toggle-checkbox))
  13544. ((org-at-item-p)
  13545. (call-interactively 'org-renumber-ordered-list))
  13546. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  13547. (cond
  13548. ((equal (match-string 1) "TBLFM")
  13549. ;; Recalculate the table before this line
  13550. (save-excursion
  13551. (beginning-of-line 1)
  13552. (skip-chars-backward " \r\n\t")
  13553. (if (org-at-table-p)
  13554. (org-call-with-arg 'org-table-recalculate t))))
  13555. (t
  13556. (call-interactively 'org-mode-restart))))
  13557. (t (error "C-c C-c can do nothing useful at this location.")))))
  13558. (defun org-mode-restart ()
  13559. "Restart Org-mode, to scan again for special lines.
  13560. Also updates the keyword regular expressions."
  13561. (interactive)
  13562. (let ((org-inhibit-startup t)) (org-mode))
  13563. (message "Org-mode restarted to refresh keyword and special line setup"))
  13564. (defun org-return ()
  13565. "Goto next table row or insert a newline.
  13566. Calls `org-table-next-row' or `newline', depending on context.
  13567. See the individual commands for more information."
  13568. (interactive)
  13569. (cond
  13570. ((org-at-table-p)
  13571. (org-table-justify-field-maybe)
  13572. (call-interactively 'org-table-next-row))
  13573. (t (newline))))
  13574. (defun org-meta-return (&optional arg)
  13575. "Insert a new heading or wrap a region in a table.
  13576. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  13577. See the individual commands for more information."
  13578. (interactive "P")
  13579. (cond
  13580. ((org-at-table-p)
  13581. (call-interactively 'org-table-wrap-region))
  13582. (t (call-interactively 'org-insert-heading))))
  13583. ;;; Menu entries
  13584. ;; Define the Org-mode menus
  13585. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  13586. '("Tbl"
  13587. ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
  13588. ["Next Field" org-cycle (org-at-table-p)]
  13589. ["Previous Field" org-shifttab (org-at-table-p)]
  13590. ["Next Row" org-return (org-at-table-p)]
  13591. "--"
  13592. ["Blank Field" org-table-blank-field (org-at-table-p)]
  13593. ["Edit Field" org-table-edit-field (org-at-table-p)]
  13594. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  13595. "--"
  13596. ("Column"
  13597. ["Move Column Left" org-metaleft (org-at-table-p)]
  13598. ["Move Column Right" org-metaright (org-at-table-p)]
  13599. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  13600. ["Insert Column" org-shiftmetaright (org-at-table-p)]
  13601. "--"
  13602. ["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])
  13603. ("Row"
  13604. ["Move Row Up" org-metaup (org-at-table-p)]
  13605. ["Move Row Down" org-metadown (org-at-table-p)]
  13606. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  13607. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  13608. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  13609. "--"
  13610. ["Insert Hline" org-table-insert-hline (org-at-table-p)])
  13611. ("Rectangle"
  13612. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  13613. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  13614. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  13615. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  13616. "--"
  13617. ("Calculate"
  13618. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  13619. ["Set Named Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  13620. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  13621. "--"
  13622. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  13623. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  13624. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  13625. "--"
  13626. ["Sum Column/Rectangle" org-table-sum
  13627. (or (org-at-table-p) (org-region-active-p))]
  13628. ["Which Column?" org-table-current-column (org-at-table-p)])
  13629. ["Debug Formulas"
  13630. (setq org-table-formula-debug (not org-table-formula-debug))
  13631. :style toggle :selected org-table-formula-debug]
  13632. "--"
  13633. ["Create" org-table-create (and (not (org-at-table-p))
  13634. org-enable-table-editor)]
  13635. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  13636. ["Import from File" org-table-import (not (org-at-table-p))]
  13637. ["Export to File" org-table-export (org-at-table-p)]
  13638. "--"
  13639. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  13640. (easy-menu-define org-org-menu org-mode-map "Org menu"
  13641. '("Org"
  13642. ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
  13643. ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
  13644. ["Sparse Tree" org-occur t]
  13645. ["Show All" show-all t]
  13646. "--"
  13647. ["New Heading" org-insert-heading t]
  13648. ("Navigate Headings"
  13649. ["Up" outline-up-heading t]
  13650. ["Next" outline-next-visible-heading t]
  13651. ["Previous" outline-previous-visible-heading t]
  13652. ["Next Same Level" outline-forward-same-level t]
  13653. ["Previous Same Level" outline-backward-same-level t]
  13654. "--"
  13655. ["Jump" org-goto t])
  13656. ("Edit Structure"
  13657. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  13658. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  13659. "--"
  13660. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  13661. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  13662. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  13663. "--"
  13664. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  13665. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  13666. ["Demote Heading" org-metaright (not (org-at-table-p))]
  13667. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  13668. "--"
  13669. ["Archive Subtree" org-archive-subtree t]
  13670. "--"
  13671. ["Convert to odd levels" org-convert-to-odd-levels t]
  13672. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  13673. "--"
  13674. ("TODO Lists"
  13675. ["TODO/DONE/-" org-todo t]
  13676. ["Show TODO Tree" org-show-todo-tree t]
  13677. ["Global TODO list" org-todo-list t]
  13678. "--"
  13679. ["Set Priority" org-priority t]
  13680. ["Priority Up" org-shiftup t]
  13681. ["Priority Down" org-shiftdown t])
  13682. ("Dates and Scheduling"
  13683. ["Timestamp" org-time-stamp t]
  13684. ["Timestamp (inactive)" org-time-stamp-inactive t]
  13685. ("Change Date"
  13686. ["1 Day Later" org-timestamp-up-day t]
  13687. ["1 Day Earlier" org-timestamp-down-day t]
  13688. ["1 ... Later" org-shiftup t]
  13689. ["1 ... Earlier" org-shiftdown t])
  13690. ["Compute Time Range" org-evaluate-time-range t]
  13691. ["Schedule Item" org-schedule t]
  13692. ["Deadline" org-deadline t]
  13693. "--"
  13694. ["Goto Calendar" org-goto-calendar t]
  13695. ["Date from Calendar" org-date-from-calendar t])
  13696. ("Logging work"
  13697. ["Clock in" org-clock-in t]
  13698. ["Clock out" org-clock-out t]
  13699. ["Clock cancel" org-clock-cancel t]
  13700. ["Display times" org-clock-display t]
  13701. "--"
  13702. ["Record DONE time"
  13703. (progn (setq org-log-done (not org-log-done))
  13704. (message "Switching to %s will %s record a timestamp"
  13705. org-done-string
  13706. (if org-log-done "automatically" "not")))
  13707. :style toggle :selected org-log-done])
  13708. "--"
  13709. ["Agenda Command" org-agenda t]
  13710. ("File List for Agenda")
  13711. ("Special views current file"
  13712. ["TODO Tree" org-show-todo-tree t]
  13713. ["Check Deadlines" org-check-deadlines t]
  13714. ["Timeline" org-timeline t]
  13715. ["Tags Tree" org-tags-sparse-tree t])
  13716. "--"
  13717. ("Hyperlinks"
  13718. ["Store Link (Global)" org-store-link t]
  13719. ["Insert Link" org-insert-link t]
  13720. ["Follow Link" org-open-at-point t]
  13721. "--"
  13722. ["Descriptive Links"
  13723. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  13724. :style radio :selected (member '(org-link) buffer-invisibility-spec)]
  13725. ["Literal Links"
  13726. (progn
  13727. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  13728. :style radio :selected (not (member '(org-link) buffer-invisibility-spec))]
  13729. "--"
  13730. ["Upgrade all <link> to [[link][desc]]" org-upgrade-old-links
  13731. (save-excursion (goto-char (point-min))
  13732. (re-search-forward "<[a-z]+:" nil t))])
  13733. "--"
  13734. ["Export/Publish" org-export t]
  13735. "--"
  13736. ("Documentation"
  13737. ["Show Version" org-version t]
  13738. ["Info Documentation" org-info t])
  13739. ("Customize"
  13740. ["Browse Org Group" org-customize t]
  13741. "--"
  13742. ["Expand This Menu" org-create-customize-menu
  13743. (fboundp 'customize-menu-create)])
  13744. "--"
  13745. ["Refresh setup" org-mode-restart t]
  13746. ))
  13747. (defun org-info (&optional node)
  13748. "Read documentation for Org-mode in the info system.
  13749. With optional NODE, go directly to that node."
  13750. (interactive)
  13751. (require 'info)
  13752. (Info-goto-node (format "(org)%s" (or node ""))))
  13753. (defun org-install-agenda-files-menu ()
  13754. (let ((bl (buffer-list)))
  13755. (save-excursion
  13756. (while bl
  13757. (set-buffer (pop bl))
  13758. (if (eq major-mode 'org-mode) (setq bl nil)))
  13759. (when (eq major-mode 'org-mode)
  13760. (easy-menu-change
  13761. '("Org") "File List for Agenda"
  13762. (append
  13763. (list
  13764. ["Edit File List" (org-edit-agenda-file-list) t]
  13765. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  13766. ["Remove Current File from List" org-remove-file t]
  13767. ["Cycle through agenda files" org-cycle-agenda-files t]
  13768. "--")
  13769. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  13770. ;;; Documentation
  13771. (defun org-customize ()
  13772. "Call the customize function with org as argument."
  13773. (interactive)
  13774. (customize-browse 'org))
  13775. (defun org-create-customize-menu ()
  13776. "Create a full customization menu for Org-mode, insert it into the menu."
  13777. (interactive)
  13778. (if (fboundp 'customize-menu-create)
  13779. (progn
  13780. (easy-menu-change
  13781. '("Org") "Customize"
  13782. `(["Browse Org group" org-customize t]
  13783. "--"
  13784. ,(customize-menu-create 'org)
  13785. ["Set" Custom-set t]
  13786. ["Save" Custom-save t]
  13787. ["Reset to Current" Custom-reset-current t]
  13788. ["Reset to Saved" Custom-reset-saved t]
  13789. ["Reset to Standard Settings" Custom-reset-standard t]))
  13790. (message "\"Org\"-menu now contains full customization menu"))
  13791. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  13792. ;;; Miscellaneous stuff
  13793. (defun org-context ()
  13794. "Return a list of contexts of the current cursor position.
  13795. If several contexts apply, all are returned.
  13796. Each context entry is a list with a symbol naming the context, and
  13797. two positions indicating start and end of the context. Possible
  13798. contexts are:
  13799. :headline anywhere in a headline
  13800. :headline-stars on the leading stars in a headline
  13801. :todo-keyword on a TODO keyword (including DONE) in a headline
  13802. :tags on the TAGS in a headline
  13803. :priority on the priority cookie in a headline
  13804. :item on the first line of a plain list item
  13805. :checkbox on the checkbox in a plain list item
  13806. :table in an org-mode table
  13807. :table-special on a special filed in a table
  13808. :table-table in a table.el table
  13809. :link on a hyperline
  13810. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  13811. :target on a <<target>>
  13812. :radio-target on a <<<radio-target>>>
  13813. This function expects the position to be visible because it uses font-lock
  13814. faces as a help to recognize the following contexts: :table-special, :link,
  13815. and :keyword."
  13816. (let* ((f (get-text-property (point) 'face))
  13817. (faces (if (listp f) f (list f)))
  13818. (p (point)) clist)
  13819. ;; First the large context
  13820. (cond
  13821. ((org-on-heading-p)
  13822. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  13823. (when (progn
  13824. (beginning-of-line 1)
  13825. (looking-at org-todo-line-tags-regexp))
  13826. (push (org-point-in-group p 1 :headline-stars) clist)
  13827. (push (org-point-in-group p 2 :todo-keyword) clist)
  13828. (push (org-point-in-group p 4 :tags) clist))
  13829. (goto-char p)
  13830. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  13831. (if (looking-at "\\[#[A-Z]\\]")
  13832. (push (org-point-in-group p 0 :priority) clist)))
  13833. ((org-at-item-p)
  13834. (push (list :item (point-at-bol)
  13835. (save-excursion (org-end-of-item) (point)))
  13836. clist)
  13837. (and (org-at-item-checkbox-p)
  13838. (push (org-point-in-group p 0 :checkbox) clist)))
  13839. ((org-at-table-p)
  13840. (push (list :table (org-table-begin) (org-table-end)) clist)
  13841. (if (memq 'org-formula faces)
  13842. (push (list :table-special
  13843. (previous-single-property-change p 'face)
  13844. (next-single-property-change p 'face)) clist)))
  13845. ((org-at-table-p 'any)
  13846. (push (list :table-table) clist)))
  13847. (goto-char p)
  13848. ;; Now the small context
  13849. (cond
  13850. ((org-at-timestamp-p)
  13851. (push (org-point-in-group p 0 :timestamp) clist))
  13852. ((memq 'org-link faces)
  13853. (push (list :link
  13854. (previous-single-property-change p 'face)
  13855. (next-single-property-change p 'face)) clist))
  13856. ((memq 'org-special-keyword faces)
  13857. (push (list :keyword
  13858. (previous-single-property-change p 'face)
  13859. (next-single-property-change p 'face)) clist))
  13860. ((org-on-target-p)
  13861. (push (org-point-in-group p 0 :target) clist)
  13862. (goto-char (1- (match-beginning 0)))
  13863. (if (looking-at org-radio-target-regexp)
  13864. (push (org-point-in-group p 0 :radio-target) clist))
  13865. (goto-char p)))
  13866. (setq clist (nreverse (delq nil clist)))
  13867. clist))
  13868. (defun org-point-in-group (point group &optional context)
  13869. "Check if POINT is in match-group GROUP.
  13870. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  13871. match. If the match group does ot exist or point is not inside it,
  13872. return nil."
  13873. (and (match-beginning group)
  13874. (>= point (match-beginning group))
  13875. (<= point (match-end group))
  13876. (if context
  13877. (list context (match-beginning group) (match-end group))
  13878. t)))
  13879. (defun org-move-line-down (arg)
  13880. "Move the current line down. With prefix argument, move it past ARG lines."
  13881. (interactive "p")
  13882. (let ((col (current-column))
  13883. beg end pos)
  13884. (beginning-of-line 1) (setq beg (point))
  13885. (beginning-of-line 2) (setq end (point))
  13886. (beginning-of-line (+ 1 arg))
  13887. (setq pos (move-marker (make-marker) (point)))
  13888. (insert (delete-and-extract-region beg end))
  13889. (goto-char pos)
  13890. (move-to-column col)))
  13891. (defun org-move-line-up (arg)
  13892. "Move the current line up. With prefix argument, move it past ARG lines."
  13893. (interactive "p")
  13894. (let ((col (current-column))
  13895. beg end pos)
  13896. (beginning-of-line 1) (setq beg (point))
  13897. (beginning-of-line 2) (setq end (point))
  13898. (beginning-of-line (- arg))
  13899. (setq pos (move-marker (make-marker) (point)))
  13900. (insert (delete-and-extract-region beg end))
  13901. (goto-char pos)
  13902. (move-to-column col)))
  13903. ;; Paragraph filling stuff.
  13904. ;; We want this to be just right, so use the full arsenal.
  13905. (defun org-set-autofill-regexps ()
  13906. (interactive)
  13907. ;; In the paragraph separator we include headlines, because filling
  13908. ;; text in a line directly attached to a headline would otherwise
  13909. ;; fill the headline as well.
  13910. (set (make-local-variable 'paragraph-separate) "\f\\|\\*\\|[ ]*$\\|[ \t]*[:|]")
  13911. ;; The paragraph starter includes hand-formatted lists.
  13912. (set (make-local-variable 'paragraph-start)
  13913. "\f\\|[ ]*$\\|\\([*\f]+\\)\\|[ \t]*\\([-+*]\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  13914. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  13915. ;; But only if the user has not turned off tables or fixed-width regions
  13916. (set (make-local-variable 'auto-fill-inhibit-regexp)
  13917. (concat "\\*\\|#"
  13918. "\\|[ \t]*" org-keyword-time-regexp
  13919. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  13920. (concat
  13921. "\\|[ \t]*["
  13922. (if org-enable-table-editor "|" "")
  13923. (if org-enable-fixed-width-editor ":" "")
  13924. "]"))))
  13925. ;; We use our own fill-paragraph function, to make sure that tables
  13926. ;; and fixed-width regions are not wrapped. That function will pass
  13927. ;; through to `fill-paragraph' when appropriate.
  13928. (set (make-local-variable 'fill-paragraph-function) 'org-fill-paragraph)
  13929. ;; Adaptive filling: To get full control, first make sure that
  13930. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  13931. (set (make-local-variable 'adaptive-fill-regexp) "\000")
  13932. (set (make-local-variable 'adaptive-fill-function)
  13933. 'org-adaptive-fill-function))
  13934. (defun org-fill-paragraph (&optional justify)
  13935. "Re-align a table, pass through to fill-paragraph if no table."
  13936. (let ((table-p (org-at-table-p))
  13937. (table.el-p (org-at-table.el-p)))
  13938. (cond ((equal (char-after (point-at-bol)) ?*) t) ; skip headlines
  13939. (table.el-p t) ; skip table.el tables
  13940. (table-p (org-table-align) t) ; align org-mode tables
  13941. (t nil)))) ; call paragraph-fill
  13942. ;; For reference, this is the default value of adaptive-fill-regexp
  13943. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  13944. (defun org-adaptive-fill-function ()
  13945. "Return a fill prefix for org-mode files.
  13946. In particular, this makes sure hanging paragraphs for hand-formatted lists
  13947. work correctly."
  13948. (if (looking-at " *\\([-*+] \\|[0-9]+[.)] \\)?")
  13949. (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  13950. ;; Functions needed for Emacs/XEmacs region compatibility
  13951. (defun org-add-hook (hook function &optional append local)
  13952. "Add-hook, compatible with both Emacsen."
  13953. (if (and local (featurep 'xemacs))
  13954. (add-local-hook hook function append)
  13955. (add-hook hook function append local)))
  13956. (defun org-region-active-p ()
  13957. "Is `transient-mark-mode' on and the region active?
  13958. Works on both Emacs and XEmacs."
  13959. (if org-ignore-region
  13960. nil
  13961. (if (featurep 'xemacs)
  13962. (and zmacs-regions (region-active-p))
  13963. (and transient-mark-mode mark-active))))
  13964. (defun org-add-to-invisibility-spec (arg)
  13965. "Add elements to `buffer-invisibility-spec'.
  13966. See documentation for `buffer-invisibility-spec' for the kind of elements
  13967. that can be added."
  13968. (cond
  13969. ((fboundp 'add-to-invisibility-spec)
  13970. (add-to-invisibility-spec arg))
  13971. ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
  13972. (setq buffer-invisibility-spec (list arg)))
  13973. (t
  13974. (setq buffer-invisibility-spec
  13975. (cons arg buffer-invisibility-spec)))))
  13976. (defun org-remove-from-invisibility-spec (arg)
  13977. "Remove elements from `buffer-invisibility-spec'."
  13978. (if (fboundp 'remove-from-invisibility-spec)
  13979. (remove-from-invisibility-spec arg)
  13980. (if (consp buffer-invisibility-spec)
  13981. (setq buffer-invisibility-spec
  13982. (delete arg buffer-invisibility-spec)))))
  13983. (defun org-in-invisibility-spec-p (arg)
  13984. "Is ARG a member of `buffer-invisibility-spec'?"
  13985. (if (consp buffer-invisibility-spec)
  13986. (member arg buffer-invisibility-spec)
  13987. nil))
  13988. (defun org-image-file-name-regexp ()
  13989. "Return regexp matching the file names of images."
  13990. (if (fboundp 'image-file-name-regexp)
  13991. (image-file-name-regexp)
  13992. (let ((image-file-name-extensions
  13993. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  13994. "xbm" "xpm" "pbm" "pgm" "ppm")))
  13995. (concat "\\."
  13996. (regexp-opt (nconc (mapcar 'upcase
  13997. image-file-name-extensions)
  13998. image-file-name-extensions)
  13999. t)
  14000. "\\'"))))
  14001. ;; Functions needed for compatibility with old outline.el.
  14002. ;; Programming for the old outline.el (that uses selective display
  14003. ;; instead of `invisible' text properties) is a nightmare, mostly
  14004. ;; because regular expressions can no longer be anchored at
  14005. ;; beginning/end of line. Therefore a number of function need special
  14006. ;; treatment when the old outline.el is being used.
  14007. ;; The following functions capture almost the entire compatibility code
  14008. ;; between the different versions of outline-mode. The only other
  14009. ;; places where this is important are the font-lock-keywords, and in
  14010. ;; `org-export-visible'. Search for `org-noutline-p' to find them.
  14011. ;; C-a should go to the beginning of a *visible* line, also in the
  14012. ;; new outline.el. I guess this should be patched into Emacs?
  14013. (defun org-beginning-of-line ()
  14014. "Go to the beginning of the current line. If that is invisible, continue
  14015. to a visible line beginning. This makes the function of C-a more intuitive."
  14016. (interactive)
  14017. (beginning-of-line 1)
  14018. (if (bobp)
  14019. nil
  14020. (backward-char 1)
  14021. (if (org-invisible-p)
  14022. (while (and (not (bobp)) (org-invisible-p))
  14023. (backward-char 1)
  14024. (beginning-of-line 1))
  14025. (forward-char 1))))
  14026. (when org-noutline-p
  14027. (define-key org-mode-map "\C-a" 'org-beginning-of-line))
  14028. (defun org-invisible-p ()
  14029. "Check if point is at a character currently not visible."
  14030. (if org-noutline-p
  14031. ;; Early versions of noutline don't have `outline-invisible-p'.
  14032. (if (fboundp 'outline-invisible-p)
  14033. (outline-invisible-p)
  14034. (get-char-property (point) 'invisible))
  14035. (save-excursion
  14036. (skip-chars-backward "^\r\n")
  14037. (equal (char-before) ?\r))))
  14038. (defun org-invisible-p2 ()
  14039. "Check if point is at a character currently not visible."
  14040. (save-excursion
  14041. (if org-noutline-p
  14042. (progn
  14043. (if (and (eolp) (not (bobp))) (backward-char 1))
  14044. ;; Early versions of noutline don't have `outline-invisible-p'.
  14045. (if (fboundp 'outline-invisible-p)
  14046. (outline-invisible-p)
  14047. (get-char-property (point) 'invisible)))
  14048. (skip-chars-backward "^\r\n")
  14049. (equal (char-before) ?\r))))
  14050. (defun org-back-to-heading (&optional invisible-ok)
  14051. "Move to previous heading line, or beg of this line if it's a heading.
  14052. Only visible heading lines are considered, unless INVISIBLE-OK is non-nil."
  14053. (if org-noutline-p
  14054. (outline-back-to-heading invisible-ok)
  14055. (if (and (or (bobp) (memq (char-before) '(?\n ?\r)))
  14056. (looking-at outline-regexp))
  14057. t
  14058. (if (re-search-backward (concat (if invisible-ok "\\([\r\n]\\|^\\)" "^")
  14059. outline-regexp)
  14060. nil t)
  14061. (if invisible-ok
  14062. (progn (goto-char (or (match-end 1) (match-beginning 0)))
  14063. (looking-at outline-regexp)))
  14064. (error "Before first heading")))))
  14065. (defun org-on-heading-p (&optional invisible-ok)
  14066. "Return t if point is on a (visible) heading line.
  14067. If INVISIBLE-OK is non-nil, an invisible heading line is ok too."
  14068. (if org-noutline-p
  14069. (outline-on-heading-p 'invisible-ok)
  14070. (save-excursion
  14071. (skip-chars-backward "^\n\r")
  14072. (and (looking-at outline-regexp)
  14073. (or invisible-ok
  14074. (bobp)
  14075. (equal (char-before) ?\n))))))
  14076. (defun org-on-target-p ()
  14077. (let ((pos (point)))
  14078. (save-excursion
  14079. (skip-chars-forward "<")
  14080. (and (re-search-backward "<<" nil t)
  14081. (or (looking-at org-radio-target-regexp)
  14082. (looking-at org-target-regexp))
  14083. (<= (match-beginning 0) pos)
  14084. (>= (1+ (match-end 0)) pos)))))
  14085. (defun org-up-heading-all (arg)
  14086. "Move to the heading line of which the present line is a subheading.
  14087. This function considers both visible and invisible heading lines.
  14088. With argument, move up ARG levels."
  14089. (if org-noutline-p
  14090. (if (fboundp 'outline-up-heading-all)
  14091. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  14092. (outline-up-heading arg t)) ; emacs 22 version of outline.el
  14093. (org-back-to-heading t)
  14094. (looking-at outline-regexp)
  14095. (if (<= (- (match-end 0) (match-beginning 0)) arg)
  14096. (error "Cannot move up %d levels" arg)
  14097. (re-search-backward
  14098. (concat "[\n\r]" (regexp-quote
  14099. (make-string (- (match-end 0) (match-beginning 0) arg)
  14100. ?*))
  14101. "[^*]"))
  14102. (forward-char 1))))
  14103. (defun org-show-hidden-entry ()
  14104. "Show an entry where even the heading is hidden."
  14105. (save-excursion
  14106. (if (not org-noutline-p)
  14107. (progn
  14108. (org-back-to-heading t)
  14109. (org-flag-heading nil)))
  14110. (org-show-entry)))
  14111. (defun org-check-occur-regexp (regexp)
  14112. "If REGEXP starts with \"^\", modify it to check for \\r as well.
  14113. Of course, only for the old outline mode."
  14114. (if org-noutline-p
  14115. regexp
  14116. (if (string-match "^\\^" regexp)
  14117. (concat "[\n\r]" (substring regexp 1))
  14118. regexp)))
  14119. (defun org-flag-heading (flag &optional entry)
  14120. "Flag the current heading. FLAG non-nil means make invisible.
  14121. When ENTRY is non-nil, show the entire entry."
  14122. (save-excursion
  14123. (org-back-to-heading t)
  14124. (if (not org-noutline-p)
  14125. ;; Make the current headline visible
  14126. (outline-flag-region (max 1 (1- (point))) (point) (if flag ?\r ?\n)))
  14127. ;; Check if we should show the entire entry
  14128. (if entry
  14129. (progn
  14130. (org-show-entry)
  14131. (save-excursion
  14132. (and (outline-next-heading)
  14133. (org-flag-heading nil))))
  14134. (outline-flag-region (max 1 (1- (point)))
  14135. (save-excursion (outline-end-of-heading) (point))
  14136. (if org-noutline-p
  14137. flag
  14138. (if flag ?\r ?\n))))))
  14139. (defun org-end-of-subtree (&optional invisible-OK)
  14140. ;; This is an exact copy of the original function, but it uses
  14141. ;; `org-back-to-heading', to make it work also in invisible
  14142. ;; trees. And is uses an invisible-OK argument.
  14143. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  14144. (org-back-to-heading invisible-OK)
  14145. (let ((first t)
  14146. (level (funcall outline-level)))
  14147. (while (and (not (eobp))
  14148. (or first (> (funcall outline-level) level)))
  14149. (setq first nil)
  14150. (outline-next-heading))
  14151. (if (memq (preceding-char) '(?\n ?\^M))
  14152. (progn
  14153. ;; Go to end of line before heading
  14154. (forward-char -1)
  14155. (if (memq (preceding-char) '(?\n ?\^M))
  14156. ;; leave blank line before heading
  14157. (forward-char -1))))))
  14158. (defun org-show-subtree ()
  14159. "Show everything after this heading at deeper levels."
  14160. (outline-flag-region
  14161. (point)
  14162. (save-excursion
  14163. (outline-end-of-subtree) (outline-next-heading) (point))
  14164. (if org-noutline-p nil ?\n)))
  14165. (defun org-show-entry ()
  14166. "Show the body directly following this heading.
  14167. Show the heading too, if it is currently invisible."
  14168. (interactive)
  14169. (save-excursion
  14170. (org-back-to-heading t)
  14171. (outline-flag-region
  14172. (max 1 (1- (point)))
  14173. (save-excursion
  14174. (re-search-forward (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  14175. (or (match-beginning 1) (point-max)))
  14176. (if org-noutline-p nil ?\n))))
  14177. (defun org-make-options-regexp (kwds)
  14178. "Make a regular expression for keyword lines."
  14179. (concat
  14180. (if org-noutline-p "^" "[\n\r]")
  14181. "#?[ \t]*\\+\\("
  14182. (mapconcat 'regexp-quote kwds "\\|")
  14183. "\\):[ \t]*"
  14184. (if org-noutline-p "\\(.+\\)" "\\([^\n\r]+\\)")))
  14185. ;; Make `bookmark-jump' show the jump location if it was hidden.
  14186. (eval-after-load "bookmark"
  14187. '(if (boundp 'bookmark-after-jump-hook)
  14188. ;; We can use the hook
  14189. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  14190. ;; Hook not available, use advice
  14191. (defadvice bookmark-jump (after org-make-visible activate)
  14192. "Make the position visible."
  14193. (org-bookmark-jump-unhide))))
  14194. (defun org-bookmark-jump-unhide ()
  14195. "Unhide the current position, to show the bookmark location."
  14196. (and (eq major-mode 'org-mode)
  14197. (or (org-invisible-p)
  14198. (save-excursion (goto-char (max (point-min) (1- (point))))
  14199. (org-invisible-p)))
  14200. (org-show-hierarchy-above)))
  14201. ;;; Experimental code
  14202. ;;; Finish up
  14203. (provide 'org)
  14204. (run-hooks 'org-load-hook)
  14205. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  14206. ;;; org.el ends here