org.el 710 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325
  1. ;;;; org.el --- Outline-based notes management and organize
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004, 2005, 2006, 2007 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.64a
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software; you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation; either version 2, or (at your option)
  15. ;; any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  22. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  23. ;; Boston, MA 02110-1301, USA.
  24. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  25. ;;
  26. ;;; Commentary:
  27. ;;
  28. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  29. ;; project planning with a fast and effective plain-text system.
  30. ;;
  31. ;; Org-mode develops organizational tasks around NOTES files that contain
  32. ;; information about projects as plain text. Org-mode is implemented on
  33. ;; top of outline-mode, which makes it possible to keep the content of
  34. ;; large files well structured. Visibility cycling and structure editing
  35. ;; help to work with the tree. Tables are easily created with a built-in
  36. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  37. ;; and scheduling. It dynamically compiles entries into an agenda that
  38. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  39. ;; Plain text URL-like links connect to websites, emails, Usenet
  40. ;; messages, BBDB entries, and any files related to the projects. For
  41. ;; printing and sharing of notes, an Org-mode file can be exported as a
  42. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  43. ;; iCalendar file. It can also serve as a publishing tool for a set of
  44. ;; linked webpages.
  45. ;;
  46. ;; Installation and Activation
  47. ;; ---------------------------
  48. ;; See the corresponding sections in the manual at
  49. ;;
  50. ;; http://staff.science.uva.nl/~dominik/Tools/org/org.html#Installation
  51. ;;
  52. ;; Documentation
  53. ;; -------------
  54. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  55. ;; distribution also contains a PDF version of it. At the homepage of
  56. ;; Org-mode, you can read the same text online as HTML. There is also an
  57. ;; excellent reference card made by Philip Rooke. This card can be found
  58. ;; in the etc/ directory of Emacs 22.
  59. ;;
  60. ;; A list of recent changes can be found at
  61. ;; http://www.astro.uva.nl/~dominik/Tools/org/Changes
  62. ;;
  63. ;;; Code:
  64. ;;;; Require other packages
  65. (eval-when-compile
  66. (require 'cl)
  67. (require 'gnus-sum)
  68. (require 'calendar))
  69. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  70. ;; the file noutline.el being loaded.
  71. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  72. ;; We require noutline, which might be provided in outline.el
  73. (require 'outline) (require 'noutline)
  74. ;; Other stuff we need.
  75. (require 'time-date)
  76. (require 'easymenu)
  77. ;;;; Customization variables
  78. ;;; Version
  79. (defvar org-version "4.64a"
  80. "The version number of the file org.el.")
  81. (defun org-version ()
  82. (interactive)
  83. (message "Org-mode version %s" org-version))
  84. ;;; Compatibility constants
  85. (defconst org-xemacs-p (featurep 'xemacs)) ; not used by org.el itself
  86. (defconst org-format-transports-properties-p
  87. (let ((x "a"))
  88. (add-text-properties 0 1 '(test t) x)
  89. (get-text-property 0 'test (format "%s" x)))
  90. "Does format transport text properties?")
  91. ;;; The custom variables
  92. (defgroup org nil
  93. "Outline-based notes management and organizer."
  94. :tag "Org"
  95. :group 'outlines
  96. :group 'hypermedia
  97. :group 'calendar)
  98. (defgroup org-startup nil
  99. "Options concerning startup of Org-mode."
  100. :tag "Org Startup"
  101. :group 'org)
  102. (defcustom org-startup-folded t
  103. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  104. This can also be configured on a per-file basis by adding one of
  105. the following lines anywhere in the buffer:
  106. #+STARTUP: fold
  107. #+STARTUP: nofold
  108. #+STARTUP: content"
  109. :group 'org-startup
  110. :type '(choice
  111. (const :tag "nofold: show all" nil)
  112. (const :tag "fold: overview" t)
  113. (const :tag "content: all headlines" content)))
  114. (defcustom org-startup-truncated t
  115. "Non-nil means, entering Org-mode will set `truncate-lines'.
  116. This is useful since some lines containing links can be very long and
  117. uninteresting. Also tables look terrible when wrapped."
  118. :group 'org-startup
  119. :type 'boolean)
  120. (defcustom org-startup-align-all-tables nil
  121. "Non-nil means, align all tables when visiting a file.
  122. This is useful when the column width in tables is forced with <N> cookies
  123. in table fields. Such tables will look correct only after the first re-align.
  124. This can also be configured on a per-file basis by adding one of
  125. the following lines anywhere in the buffer:
  126. #+STARTUP: align
  127. #+STARTUP: noalign"
  128. :group 'org-startup
  129. :type 'boolean)
  130. (defcustom org-startup-with-deadline-check nil
  131. "Non-nil means, entering Org-mode will run the deadline check.
  132. This means, if you start editing an org file, you will get an
  133. immediate reminder of any due deadlines.
  134. This can also be configured on a per-file basis by adding one of
  135. the following lines anywhere in the buffer:
  136. #+STARTUP: dlcheck
  137. #+STARTUP: nodlcheck"
  138. :group 'org-startup
  139. :type 'boolean)
  140. (defcustom org-insert-mode-line-in-empty-file nil
  141. "Non-nil means insert the first line setting Org-mode in empty files.
  142. When the function `org-mode' is called interactively in an empty file, this
  143. normally means that the file name does not automatically trigger Org-mode.
  144. To ensure that the file will always be in Org-mode in the future, a
  145. line enforcing Org-mode will be inserted into the buffer, if this option
  146. has been set."
  147. :group 'org-startup
  148. :type 'boolean)
  149. (defcustom org-CUA-compatible nil
  150. "Non-nil means use alternative key bindings for S-<cursor movement>.
  151. Org-mode used S-<cursor movement> for changing timestamps and priorities.
  152. S-<cursor movement> is also used for example by `CUA-mode' to select text.
  153. If you want to use Org-mode together with `CUA-mode', Org-mode needs to use
  154. alternative bindings. Setting this variable to t will replace the following
  155. keys both in Org-mode and in the Org-agenda buffer.
  156. S-RET -> C-S-RET
  157. S-up -> M-p
  158. S-down -> M-n
  159. S-left -> M--
  160. S-right -> M-+
  161. If you do not like the alternative keys, take a look at the variable
  162. `org-disputed-keys'.
  163. This option is only relevant at load-time of Org-mode, and must be set
  164. *before* org.el is loaded. Changing it requires a restart of Emacs to
  165. become effective."
  166. :group 'org-startup
  167. :type 'boolean)
  168. (defvar org-disputed-keys
  169. '((S-up [(shift up)] [(meta ?p)])
  170. (S-down [(shift down)] [(meta ?n)])
  171. (S-left [(shift left)] [(meta ?-)])
  172. (S-right [(shift right)] [(meta ?+)])
  173. (S-return [(shift return)] [(control shift return)]))
  174. "Keys for which Org-mode and other modes compete.
  175. This is an alist, cars are symbols for lookup, 1st element is the default key,
  176. second element will be used when `org-CUA-compatible' is t.")
  177. (defun org-key (key)
  178. "Select a key according to `org-CUA-compatible'."
  179. (nth (if org-CUA-compatible 2 1)
  180. (or (assq key org-disputed-keys)
  181. (error "Invalid Key %s in `org-key'" key))))
  182. (defcustom org-ellipsis nil
  183. "The ellipsis to use in the Org-mode outline.
  184. When nil, just use the standard three dots. When a string, use that instead,
  185. and just in Org-mode (which will then use its own display table).
  186. Changing this requires executing `M-x org-mode' in a buffer to become
  187. effective."
  188. :group 'org-startup
  189. :type '(choice (const :tag "Default" nil)
  190. (string :tag "String" :value "...#")))
  191. (defvar org-display-table nil
  192. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  193. (defgroup org-keywords nil
  194. "Keywords in Org-mode."
  195. :tag "Org Keywords"
  196. :group 'org)
  197. (defcustom org-deadline-string "DEADLINE:"
  198. "String to mark deadline entries.
  199. A deadline is this string, followed by a time stamp. Should be a word,
  200. terminated by a colon. You can insert a schedule keyword and
  201. a timestamp with \\[org-deadline].
  202. Changes become only effective after restarting Emacs."
  203. :group 'org-keywords
  204. :type 'string)
  205. (defcustom org-scheduled-string "SCHEDULED:"
  206. "String to mark scheduled TODO entries.
  207. A schedule is this string, followed by a time stamp. Should be a word,
  208. terminated by a colon. You can insert a schedule keyword and
  209. a timestamp with \\[org-schedule].
  210. Changes become only effective after restarting Emacs."
  211. :group 'org-keywords
  212. :type 'string)
  213. (defcustom org-closed-string "CLOSED:"
  214. "String used as the prefix for timestamps logging closing a TODO entry."
  215. :group 'org-keywords
  216. :type 'string)
  217. (defcustom org-clock-string "CLOCK:"
  218. "String used as prefix for timestamps clocking work hours on an item."
  219. :group 'org-keywords
  220. :type 'string)
  221. (defcustom org-comment-string "COMMENT"
  222. "Entries starting with this keyword will never be exported.
  223. An entry can be toggled between COMMENT and normal with
  224. \\[org-toggle-comment].
  225. Changes become only effective after restarting Emacs."
  226. :group 'org-keywords
  227. :type 'string)
  228. (defcustom org-quote-string "QUOTE"
  229. "Entries starting with this keyword will be exported in fixed-width font.
  230. Quoting applies only to the text in the entry following the headline, and does
  231. not extend beyond the next headline, even if that is lower level.
  232. An entry can be toggled between QUOTE and normal with
  233. \\[org-toggle-fixed-width-section]."
  234. :group 'org-keywords
  235. :type 'string)
  236. (defvar org-repeat-re "\\<REPEAT(\\([-+ 0-9dwmy]+\\))"
  237. "Regular expression for specifying repeated events.
  238. After a match, group 1 contains the repeat expression.")
  239. (defgroup org-structure nil
  240. "Options concerning the general structure of Org-mode files."
  241. :tag "Org Structure"
  242. :group 'org)
  243. (defgroup org-reveal-location nil
  244. "Options about how to make context of a location visible."
  245. :tag "Org Reveal Location"
  246. :group 'org-structure)
  247. (defcustom org-show-hierarchy-above '((default . t))
  248. "Non-nil means, show full hierarchy when revealing a location.
  249. Org-mode often shows locations in an org-mode file which might have
  250. been invisible before. When this is set, the hierarchy of headings
  251. above the exposed location is shown.
  252. Turning this off for example for sparse trees makes them very compact.
  253. Instead of t, this can also be an alist specifying this option for different
  254. contexts. Valid contexts are
  255. agenda when exposing an entry from the agenda
  256. org-goto when using the command `org-goto' on key C-c C-j
  257. occur-tree when using the command `org-occur' on key C-c /
  258. tags-tree when constructing a sparse tree based on tags matches
  259. link-search when exposing search matches associated with a link
  260. mark-goto when exposing the jump goal of a mark
  261. bookmark-jump when exposing a bookmark location
  262. isearch when exiting from an incremental search
  263. default default for all contexts not set explicitly"
  264. :group 'org-reveal-location
  265. :type '(choice
  266. (const :tag "Always" t)
  267. (const :tag "Never" nil)
  268. (repeat :greedy t :tag "Individual contexts"
  269. (cons
  270. (choice :tag "Context"
  271. (const agenda)
  272. (const org-goto)
  273. (const occur-tree)
  274. (const tags-tree)
  275. (const link-search)
  276. (const mark-goto)
  277. (const bookmark-jump)
  278. (const isearch)
  279. (const default))
  280. (boolean)))))
  281. (defcustom org-show-following-heading '((default . t))
  282. "Non-nil means, show following heading when revealing a location.
  283. Org-mode often shows locations in an org-mode file which might have
  284. been invisible before. When this is set, the heading following the
  285. match is shown.
  286. Turning this off for example for sparse trees makes them very compact,
  287. but makes it harder to edit the location of the match. In such a case,
  288. use the command \\[org-reveal] to show more context.
  289. Instead of t, this can also be an alist specifying this option for different
  290. contexts. See `org-show-hierarchy-above' for valid contexts."
  291. :group 'org-reveal-location
  292. :type '(choice
  293. (const :tag "Always" t)
  294. (const :tag "Never" nil)
  295. (repeat :greedy t :tag "Individual contexts"
  296. (cons
  297. (choice :tag "Context"
  298. (const agenda)
  299. (const org-goto)
  300. (const occur-tree)
  301. (const tags-tree)
  302. (const link-search)
  303. (const mark-goto)
  304. (const bookmark-jump)
  305. (const isearch)
  306. (const default))
  307. (boolean)))))
  308. (defcustom org-show-siblings '((default . nil) (isearch t))
  309. "Non-nil means, show all sibling heading when revealing a location.
  310. Org-mode often shows locations in an org-mode file which might have
  311. been invisible before. When this is set, the sibling of the current entry
  312. heading are all made visible. If `org-show-hierarchy-above' is t,
  313. the same happens on each level of the hierarchy above the current entry.
  314. By default this is on for the isearch context, off for all other contexts.
  315. Turning this off for example for sparse trees makes them very compact,
  316. but makes it harder to edit the location of the match. In such a case,
  317. use the command \\[org-reveal] to show more context.
  318. Instead of t, this can also be an alist specifying this option for different
  319. contexts. See `org-show-hierarchy-above' for valid contexts."
  320. :group 'org-reveal-location
  321. :type '(choice
  322. (const :tag "Always" t)
  323. (const :tag "Never" nil)
  324. (repeat :greedy t :tag "Individual contexts"
  325. (cons
  326. (choice :tag "Context"
  327. (const agenda)
  328. (const org-goto)
  329. (const occur-tree)
  330. (const tags-tree)
  331. (const link-search)
  332. (const mark-goto)
  333. (const bookmark-jump)
  334. (const isearch)
  335. (const default))
  336. (boolean)))))
  337. (defgroup org-cycle nil
  338. "Options concerning visibility cycling in Org-mode."
  339. :tag "Org Cycle"
  340. :group 'org-structure)
  341. (defcustom org-cycle-global-at-bob t
  342. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  343. This makes it possible to do global cycling without having to use S-TAB or
  344. C-u TAB. For this special case to work, the first line of the buffer
  345. must not be a headline - it may be empty ot some other text. When used in
  346. this way, `org-cycle-hook' is disables temporarily, to make sure the
  347. cursor stays at the beginning of the buffer.
  348. When this option is nil, don't do anything special at the beginning
  349. of the buffer."
  350. :group 'org-cycle
  351. :type 'boolean)
  352. (defcustom org-cycle-emulate-tab t
  353. "Where should `org-cycle' emulate TAB.
  354. nil Never
  355. white Only in completely white lines
  356. whitestart Only at the beginning of lines, before the first non-white char.
  357. t Everywhere except in headlines
  358. If TAB is used in a place where it does not emulate TAB, the current subtree
  359. visibility is cycled."
  360. :group 'org-cycle
  361. :type '(choice (const :tag "Never" nil)
  362. (const :tag "Only in completely white lines" white)
  363. (const :tag "Before first char in a line" whitestart)
  364. (const :tag "Everywhere except in headlines" t)
  365. ))
  366. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  367. org-optimize-window-after-visibility-change)
  368. "Hook that is run after `org-cycle' has changed the buffer visibility.
  369. The function(s) in this hook must accept a single argument which indicates
  370. the new state that was set by the most recent `org-cycle' command. The
  371. argument is a symbol. After a global state change, it can have the values
  372. `overview', `content', or `all'. After a local state change, it can have
  373. the values `folded', `children', or `subtree'."
  374. :group 'org-cycle
  375. :type 'hook)
  376. (defgroup org-edit-structure nil
  377. "Options concerning structure editing in Org-mode."
  378. :tag "Org Edit Structure"
  379. :group 'org-structure)
  380. (defcustom org-odd-levels-only nil
  381. "Non-nil means, skip even levels and only use odd levels for the outline.
  382. This has the effect that two stars are being added/taken away in
  383. promotion/demotion commands. It also influences how levels are
  384. handled by the exporters.
  385. Changing it requires restart of `font-lock-mode' to become effective
  386. for fontification also in regions already fontified.
  387. You may also set this on a per-file basis by adding one of the following
  388. lines to the buffer:
  389. #+STARTUP: odd
  390. #+STARTUP: oddeven"
  391. :group 'org-edit-structure
  392. :group 'org-font-lock
  393. :type 'boolean)
  394. (defcustom org-adapt-indentation t
  395. "Non-nil means, adapt indentation when promoting and demoting.
  396. When this is set and the *entire* text in an entry is indented, the
  397. indentation is increased by one space in a demotion command, and
  398. decreased by one in a promotion command. If any line in the entry
  399. body starts at column 0, indentation is not changed at all."
  400. :group 'org-edit-structure
  401. :type 'boolean)
  402. (defcustom org-blank-before-new-entry '((heading . nil)
  403. (plain-list-item . nil))
  404. "Should `org-insert-heading' leave a blank line before new heading/item?
  405. The value is an alist, with `heading' and `plain-list-item' as car,
  406. and a boolean flag as cdr."
  407. :group 'org-edit-structure
  408. :type '(list
  409. (cons (const heading) (boolean))
  410. (cons (const plain-list-item) (boolean))))
  411. (defcustom org-insert-heading-hook nil
  412. "Hook being run after inserting a new heading."
  413. :group 'org-edit-structure
  414. :type 'boolean)
  415. (defcustom org-enable-fixed-width-editor t
  416. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  417. This currently only means, they are never auto-wrapped.
  418. When nil, such lines will be treated like ordinary lines.
  419. See also the QUOTE keyword."
  420. :group 'org-edit-structure
  421. :type 'boolean)
  422. (defgroup org-sparse-trees nil
  423. "Options concerning sparse trees in Org-mode."
  424. :tag "Org Sparse Trees"
  425. :group 'org-structure)
  426. (defcustom org-highlight-sparse-tree-matches t
  427. "Non-nil means, highlight all matches that define a sparse tree.
  428. The highlights will automatically disappear the next time the buffer is
  429. changed by an edit command."
  430. :group 'org-sparse-trees
  431. :type 'boolean)
  432. (defcustom org-remove-highlights-with-change t
  433. "Non-nil means, any change to the buffer will remove temporary highlights.
  434. Such highlights are created by `org-occur' and `org-clock-display'.
  435. When nil, `C-c C-c needs to be used to get rid of the highlights.
  436. The highlights created by `org-preview-latex-fragment' always need
  437. `C-c C-c' to be removed."
  438. :group 'org-sparse-trees
  439. :group 'org-time
  440. :type 'boolean)
  441. (defcustom org-occur-hook '(org-first-headline-recenter)
  442. "Hook that is run after `org-occur' has constructed a sparse tree.
  443. This can be used to recenter the window to show as much of the structure
  444. as possible."
  445. :group 'org-sparse-trees
  446. :type 'hook)
  447. (defgroup org-plain-lists nil
  448. "Options concerning plain lists in Org-mode."
  449. :tag "Org Plain lists"
  450. :group 'org-structure)
  451. (defcustom org-cycle-include-plain-lists nil
  452. "Non-nil means, include plain lists into visibility cycling.
  453. This means that during cycling, plain list items will *temporarily* be
  454. interpreted as outline headlines with a level given by 1000+i where i is the
  455. indentation of the bullet. In all other operations, plain list items are
  456. not seen as headlines. For example, you cannot assign a TODO keyword to
  457. such an item."
  458. :group 'org-plain-lists
  459. :type 'boolean)
  460. (defcustom org-plain-list-ordered-item-terminator t
  461. "The character that makes a line with leading number an ordered list item.
  462. Valid values are ?. and ?\). To get both terminators, use t. While
  463. ?. may look nicer, it creates the danger that a line with leading
  464. number may be incorrectly interpreted as an item. ?\) therefore is
  465. the safe choice."
  466. :group 'org-plain-lists
  467. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  468. (const :tag "paren like in \"2)\"" ?\))
  469. (const :tab "both" t)))
  470. (defcustom org-auto-renumber-ordered-lists t
  471. "Non-nil means, automatically renumber ordered plain lists.
  472. Renumbering happens when the sequence have been changed with
  473. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  474. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  475. :group 'org-plain-lists
  476. :type 'boolean)
  477. (defcustom org-provide-checkbox-statistics t
  478. "Non-nil means, update checkbox statistics after insert and toggle.
  479. When this is set, checkbox statistics is updated each time you either insert
  480. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  481. with \\[org-ctrl-c-ctrl-c\\]."
  482. :group 'org-plain-lists
  483. :type 'boolean)
  484. (defgroup org-archive nil
  485. "Options concerning archiving in Org-mode."
  486. :tag "Org Archive"
  487. :group 'org-structure)
  488. (defcustom org-archive-tag "ARCHIVE"
  489. "The tag that marks a subtree as archived.
  490. An archived subtree does not open during visibility cycling, and does
  491. not contribute to the agenda listings."
  492. :group 'org-archive
  493. :group 'org-keywords
  494. :type 'string)
  495. (defcustom org-agenda-skip-archived-trees t
  496. "Non-nil means, the agenda will skip any items located in archived trees.
  497. An archived tree is a tree marked with the tag ARCHIVE."
  498. :group 'org-archive
  499. :group 'org-agenda-skip
  500. :type 'boolean)
  501. (defcustom org-cycle-open-archived-trees nil
  502. "Non-nil means, `org-cycle' will open archived trees.
  503. An archived tree is a tree marked with the tag ARCHIVE.
  504. When nil, archived trees will stay folded. You can still open them with
  505. normal outline commands like `show-all', but not with the cycling commands."
  506. :group 'org-archive
  507. :group 'org-cycle
  508. :type 'boolean)
  509. (defcustom org-sparse-tree-open-archived-trees nil
  510. "Non-nil means sparse tree construction shows matches in archived trees.
  511. When nil, matches in these trees are highlighted, but the trees are kept in
  512. collapsed state."
  513. :group 'org-archive
  514. :group 'org-sparse-trees
  515. :type 'boolean)
  516. (defcustom org-archive-location "%s_archive::"
  517. "The location where subtrees should be archived.
  518. This string consists of two parts, separated by a double-colon.
  519. The first part is a file name - when omitted, archiving happens in the same
  520. file. %s will be replaced by the current file name (without directory part).
  521. Archiving to a different file is useful to keep archived entries from
  522. contributing to the Org-mode Agenda.
  523. The part after the double colon is a headline. The archived entries will be
  524. filed under that headline. When omitted, the subtrees are simply filed away
  525. at the end of the file, as top-level entries.
  526. Here are a few examples:
  527. \"%s_archive::\"
  528. If the current file is Projects.org, archive in file
  529. Projects.org_archive, as top-level trees. This is the default.
  530. \"::* Archived Tasks\"
  531. Archive in the current file, under the top-level headline
  532. \"* Archived Tasks\".
  533. \"~/org/archive.org::\"
  534. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  535. \"basement::** Finished Tasks\"
  536. Archive in file ./basement (relative path), as level 3 trees
  537. below the level 2 heading \"** Finished Tasks\".
  538. You may set this option on a per-file basis by adding to the buffer a
  539. line like
  540. #+ARCHIVE: basement::** Finished Tasks"
  541. :group 'org-archive
  542. :type 'string)
  543. (defcustom org-archive-mark-done t
  544. "Non-nil means, mark entries as DONE when they are moved to the archive file."
  545. :group 'org-archive
  546. :type 'boolean)
  547. (defcustom org-archive-stamp-time t
  548. "Non-nil means, add a time stamp to entries moved to an archive file.
  549. The time stamp will be added directly after the TODO state keyword in the
  550. first line, so it is probably best to use this in combinations with
  551. `org-archive-mark-done'."
  552. :group 'org-archive
  553. :type 'boolean)
  554. (defgroup org-table nil
  555. "Options concerning tables in Org-mode."
  556. :tag "Org Table"
  557. :group 'org)
  558. (defcustom org-enable-table-editor 'optimized
  559. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  560. When nil, such lines will be treated like ordinary lines.
  561. When equal to the symbol `optimized', the table editor will be optimized to
  562. do the following:
  563. - Automatic overwrite mode in front of whitespace in table fields.
  564. This makes the structure of the table stay in tact as long as the edited
  565. field does not exceed the column width.
  566. - Minimize the number of realigns. Normally, the table is aligned each time
  567. TAB or RET are pressed to move to another field. With optimization this
  568. happens only if changes to a field might have changed the column width.
  569. Optimization requires replacing the functions `self-insert-command',
  570. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  571. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  572. very good at guessing when a re-align will be necessary, but you can always
  573. force one with \\[org-ctrl-c-ctrl-c].
  574. If you would like to use the optimized version in Org-mode, but the
  575. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  576. This variable can be used to turn on and off the table editor during a session,
  577. but in order to toggle optimization, a restart is required.
  578. See also the variable `org-table-auto-blank-field'."
  579. :group 'org-table
  580. :type '(choice
  581. (const :tag "off" nil)
  582. (const :tag "on" t)
  583. (const :tag "on, optimized" optimized)))
  584. (defcustom orgtbl-optimized (eq org-enable-table-editor 'optimized)
  585. "Non-nil means, use the optimized table editor version for `orgtbl-mode'.
  586. In the optimized version, the table editor takes over all simple keys that
  587. normally just insert a character. In tables, the characters are inserted
  588. in a way to minimize disturbing the table structure (i.e. in overwrite mode
  589. for empty fields). Outside tables, the correct binding of the keys is
  590. restored.
  591. The default for this option is t if the optimized version is also used in
  592. Org-mode. See the variable `org-enable-table-editor' for details. Changing
  593. this variable requires a restart of Emacs to become effective."
  594. :group 'org-table
  595. :type 'boolean)
  596. (defgroup org-table-settings nil
  597. "Settings for tables in Org-mode."
  598. :tag "Org Table Settings"
  599. :group 'org-table)
  600. (defcustom org-table-default-size "5x2"
  601. "The default size for newly created tables, Columns x Rows."
  602. :group 'org-table-settings
  603. :type 'string)
  604. (defcustom org-table-number-regexp
  605. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%:]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\)$"
  606. "Regular expression for recognizing numbers in table columns.
  607. If a table column contains mostly numbers, it will be aligned to the
  608. right. If not, it will be aligned to the left.
  609. The default value of this option is a regular expression which allows
  610. anything which looks remotely like a number as used in scientific
  611. context. For example, all of the following will be considered a
  612. number:
  613. 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
  614. Other options offered by the customize interface are more restrictive."
  615. :group 'org-table-settings
  616. :type '(choice
  617. (const :tag "Positive Integers"
  618. "^[0-9]+$")
  619. (const :tag "Integers"
  620. "^[-+]?[0-9]+$")
  621. (const :tag "Floating Point Numbers"
  622. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
  623. (const :tag "Floating Point Number or Integer"
  624. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
  625. (const :tag "Exponential, Floating point, Integer"
  626. "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
  627. (const :tag "Very General Number-Like, including hex"
  628. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\)$")
  629. (string :tag "Regexp:")))
  630. (defcustom org-table-number-fraction 0.5
  631. "Fraction of numbers in a column required to make the column align right.
  632. In a column all non-white fields are considered. If at least this
  633. fraction of fields is matched by `org-table-number-fraction',
  634. alignment to the right border applies."
  635. :group 'org-table-settings
  636. :type 'number)
  637. (defgroup org-table-editing nil
  638. "Bahavior of tables during editing in Org-mode."
  639. :tag "Org Table Editing"
  640. :group 'org-table)
  641. (defcustom org-table-automatic-realign t
  642. "Non-nil means, automatically re-align table when pressing TAB or RETURN.
  643. When nil, aligning is only done with \\[org-table-align], or after column
  644. removal/insertion."
  645. :group 'org-table-editing
  646. :type 'boolean)
  647. (defcustom org-table-limit-column-width t ;kw
  648. "Non-nil means, allow to limit the width of table columns with <N> fields."
  649. :group 'org-table-editing
  650. :type 'boolean)
  651. (defcustom org-table-auto-blank-field t
  652. "Non-nil means, automatically blank table field when starting to type into it.
  653. This only happens when typing immediately after a field motion
  654. command (TAB, S-TAB or RET).
  655. Only relevant when `org-enable-table-editor' is equal to `optimized'."
  656. :group 'org-table-editing
  657. :type 'boolean)
  658. (defcustom org-table-tab-jumps-over-hlines t
  659. "Non-nil means, tab in the last column of a table with jump over a hline.
  660. If a horizontal separator line is following the current line,
  661. `org-table-next-field' can either create a new row before that line, or jump
  662. over the line. When this option is nil, a new line will be created before
  663. this line."
  664. :group 'org-table-editing
  665. :type 'boolean)
  666. (defcustom org-table-tab-recognizes-table.el t
  667. "Non-nil means, TAB will automatically notice a table.el table.
  668. When it sees such a table, it moves point into it and - if necessary -
  669. calls `table-recognize-table'."
  670. :group 'org-table-editing
  671. :type 'boolean)
  672. (defgroup org-table-calculation nil
  673. "Options concerning tables in Org-mode."
  674. :tag "Org Table Calculation"
  675. :group 'org-table)
  676. (defcustom org-table-copy-increment t
  677. "Non-nil means, increment when copying current field with \\[org-table-copy-down]."
  678. :group 'org-table-calculation
  679. :type 'boolean)
  680. (defcustom org-calc-default-modes
  681. '(calc-internal-prec 12
  682. calc-float-format (float 5)
  683. calc-angle-mode deg
  684. calc-prefer-frac nil
  685. calc-symbolic-mode nil
  686. calc-date-format (YYYY "-" MM "-" DD " " Www (" " HH ":" mm))
  687. calc-display-working-message t
  688. )
  689. "List with Calc mode settings for use in calc-eval for table formulas.
  690. The list must contain alternating symbols (Calc modes variables and values).
  691. Don't remove any of the default settings, just change the values. Org-mode
  692. relies on the variables to be present in the list."
  693. :group 'org-table-calculation
  694. :type 'plist)
  695. (defcustom org-table-formula-evaluate-inline t
  696. "Non-nil means, TAB and RET evaluate a formula in current table field.
  697. If the current field starts with an equal sign, it is assumed to be a formula
  698. which should be evaluated as described in the manual and in the documentation
  699. string of the command `org-table-eval-formula'. This feature requires the
  700. Emacs calc package.
  701. When this variable is nil, formula calculation is only available through
  702. the command \\[org-table-eval-formula]."
  703. :group 'org-table-calculation
  704. :type 'boolean)
  705. ;; FIXME this is also a variable that makes Org-mode files non-portable
  706. ;; Maybe I should have a #+ options for constants?
  707. ;; How about the SI/cgs issue?
  708. (defcustom org-table-formula-use-constants t
  709. "Non-nil means, interpret constants in formulas in tables.
  710. A constant looks like `$c' or `$Grav' and will be replaced before evaluation
  711. by the value given in `org-table-formula-constants', or by a value obtained
  712. from the `constants.el' package."
  713. :group 'org-table-calculation
  714. :type 'boolean)
  715. (defcustom org-table-formula-constants nil
  716. "Alist with constant names and values, for use in table formulas.
  717. The car of each element is a name of a constant, without the `$' before it.
  718. The cdr is the value as a string. For example, if you'd like to use the
  719. speed of light in a formula, you would configure
  720. (setq org-table-formula-constants '((\"c\" . \"299792458.\")))
  721. and then use it in an equation like `$1*$c'."
  722. :group 'org-table-calculation
  723. :type '(repeat
  724. (cons (string :tag "name")
  725. (string :tag "value"))))
  726. (defcustom org-table-formula-numbers-only nil
  727. "Non-nil means, calculate only with numbers in table formulas.
  728. Then all input fields will be converted to a number, and the result
  729. must also be a number. When nil, calc's full potential is available
  730. in table calculations, including symbolics etc.
  731. It is more portable to set this mode uding the \"N\" mode switch in formulas,"
  732. :group 'org-table-calculation
  733. :type 'boolean)
  734. (defcustom org-table-allow-automatic-line-recalculation t
  735. "Non-nil means, lines marked with |#| or |*| will be recomputed automatically.
  736. Automatically means, when TAB or RET or C-c C-c are pressed in the line."
  737. :group 'org-table-calculation
  738. :type 'boolean)
  739. (defgroup org-link nil
  740. "Options concerning links in Org-mode."
  741. :tag "Org Link"
  742. :group 'org)
  743. (defvar org-link-abbrev-alist-local nil
  744. "buffer-local version of `org-link-abbrev-alist', which see.
  745. The value of this is taken from the #+LINK lines.")
  746. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  747. (defcustom org-link-abbrev-alist nil
  748. "Alist of link abbreviations.
  749. The car of each element is a string, to be replaced at the start of a link.
  750. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  751. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  752. [[linkkey:tag][description]]
  753. If REPLACE is a string, the tag will simply be appended to create the link.
  754. If the string contains \"%s\", the tag will be inserted there. REPLACE may
  755. also be a function that will be called with the tag as the only argument to
  756. create the link. See the manual for examples."
  757. :group 'org-link
  758. :type 'alist)
  759. (defcustom org-descriptive-links t
  760. "Non-nil means, hide link part and only show description of bracket links.
  761. Bracket links are like [[link][descritpion]]. This variable sets the initial
  762. state in new org-mode buffers. The setting can then be toggled on a
  763. per-buffer basis from the Org->Hyperlinks menu."
  764. :group 'org-link
  765. :type 'boolean)
  766. (defcustom org-link-style 'bracket
  767. "The style of links to be inserted with \\[org-insert-link].
  768. Possible values are:
  769. bracket [[link][description]]. This is recommended
  770. plain Description \\n link. The old way, no longer recommended."
  771. :group 'org-link
  772. :type '(choice
  773. (const :tag "Bracket (recommended)" bracket)
  774. (const :tag "Plain (no longer recommended)" plain)))
  775. (defcustom org-link-format "%s"
  776. "Default format for external, URL-like linkes in the buffer.
  777. This is a format string for printf, %s will be replaced by the link text.
  778. The recommended value is just \"%s\", since links will be protected by
  779. enclosing them in double brackets. If you prefer plain links (see variable
  780. `org-link-style'), \"<%s>\" is useful. Some people also recommend an
  781. additional URL: prefix, so the format would be \"<URL:%s>\"."
  782. :group 'org-link
  783. :type '(choice
  784. (const :tag "\"%s\" (e.g. http://www.there.com)" "%s")
  785. (const :tag "\"<%s>\" (e.g. <http://www.there.com>)" "<%s>")
  786. (const :tag "\"<URL:%s>\" (e.g. <URL:http://www.there.com>)" "<URL:%s>")
  787. (string :tag "Other" :value "<%s>")))
  788. (defcustom org-link-file-path-type 'adaptive
  789. "How the path name in file links should be stored.
  790. Valid values are:
  791. relative relative to the current directory, i.e. the directory of the file
  792. into which the link is being inserted.
  793. absolute absolute path, if possible with ~ for home directory.
  794. noabbrev absolute path, no abbreviation of home directory.
  795. adaptive Use relative path for files in the current directory and sub-
  796. directories of it. For other files, use an absolute path."
  797. :group 'org-link
  798. :type '(choice
  799. (const relative)
  800. (const absolute)
  801. (const noabbrev)
  802. (const adaptive)))
  803. (defcustom org-activate-links '(bracket angle plain radio tag date)
  804. "Types of links that should be activated in Org-mode files.
  805. This is a list of symbols, each leading to the activation of a certain link
  806. type. In principle, it does not hurt to turn on most link types - there may
  807. be a small gain when turning off unused link types. The types are:
  808. bracket The recommended [[link][description]] or [[link]] links with hiding.
  809. angular Links in angular brackes that may contain whitespace like
  810. <bbdb:Carsten Dominik>.
  811. plain Plain links in normal text, no whitespace, like http://google.com.
  812. radio Text that is matched by a radio target, see manual for details.
  813. tag Tag settings in a headline (link to tag search).
  814. date Time stamps (link to calendar).
  815. camel CamelCase words defining text searches.
  816. Changing this variable requires a restart of Emacs to become effective."
  817. :group 'org-link
  818. :type '(set (const :tag "Double bracket links (new style)" bracket)
  819. (const :tag "Angular bracket links (old style)" angular)
  820. (const :tag "plain text links" plain)
  821. (const :tag "Radio target matches" radio)
  822. (const :tag "Tags" tag)
  823. (const :tag "Timestamps" date)
  824. (const :tag "CamelCase words" camel)))
  825. (defgroup org-link-store nil
  826. "Options concerning storing links in Org-mode"
  827. :tag "Org Store Link"
  828. :group 'org-link)
  829. ;; FIXME: separate format for gnus?
  830. (defcustom org-email-link-description-format "Email %c: %.30s"
  831. "Format of the description part of a link to an email or usenet message.
  832. The following %-excapes will be replaced by corresponding information:
  833. %F full \"From\" field
  834. %f name, taken from \"From\" field, address if no name
  835. %T full \"To\" field
  836. %t first name in \"To\" field, address if no name
  837. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  838. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  839. %s subject
  840. %m message-id.
  841. You may use normal field width specification between the % and the letter.
  842. This is for example useful to limit the length of the subject.
  843. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  844. :group 'org-link-store
  845. :type 'string)
  846. (defcustom org-from-is-user-regexp
  847. (let (r1 r2)
  848. (when (and user-mail-address (not (string= user-mail-address "")))
  849. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  850. (when (and user-full-name (not (string= user-full-name "")))
  851. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  852. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  853. "Regexp mached against the \"From:\" header of an email or usenet message.
  854. It should match if the message is from the user him/herself."
  855. :group 'org-link-store
  856. :type 'regexp)
  857. (defcustom org-context-in-file-links t
  858. "Non-nil means, file links from `org-store-link' contain context.
  859. A search string will be added to the file name with :: as separator and
  860. used to find the context when the link is activated by the command
  861. `org-open-at-point'.
  862. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  863. negates this setting for the duration of the command."
  864. :group 'org-link-store
  865. :type 'boolean)
  866. (defcustom org-file-link-context-use-camel-case nil
  867. "Non-nil means, use CamelCase to store a search context in a file link.
  868. When nil, the search string simply consists of the words of the string.
  869. CamelCase is deprecated, and support for it may be dropped in the future."
  870. :group 'org-link-store
  871. :type 'boolean)
  872. (defcustom org-keep-stored-link-after-insertion nil
  873. "Non-nil means, keep link in list for entire session.
  874. The command `org-store-link' adds a link pointing to the current
  875. location to an internal list. These links accumulate during a session.
  876. The command `org-insert-link' can be used to insert links into any
  877. Org-mode file (offering completion for all stored links). When this
  878. option is nil, every link which has been inserted once using \\[org-insert-link]
  879. will be removed from the list, to make completing the unused links
  880. more efficient."
  881. :group 'org-link-store
  882. :type 'boolean)
  883. (defcustom org-usenet-links-prefer-google nil
  884. "Non-nil means, `org-store-link' will create web links to Google groups.
  885. When nil, Gnus will be used for such links.
  886. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  887. negates this setting for the duration of the command."
  888. :group 'org-link-store
  889. :type 'boolean)
  890. (defgroup org-link-follow nil
  891. "Options concerning following links in Org-mode"
  892. :tag "Org Follow Link"
  893. :group 'org-link)
  894. (defcustom org-tab-follows-link nil
  895. "Non-nil means, on links TAB will follow the link.
  896. Needs to be set before org.el is loaded."
  897. :group 'org-link-follow
  898. :type 'boolean)
  899. (defcustom org-return-follows-link nil
  900. "Non-nil means, on links RET will follow the link.
  901. Needs to be set before org.el is loaded."
  902. :group 'org-link-follow
  903. :type 'boolean)
  904. (defcustom org-mouse-1-follows-link t
  905. "Non-nil means, mouse-1 on a link will follow the link.
  906. A longer mouse click will still set point. Does not wortk on XEmacs.
  907. Needs to be set before org.el is loaded."
  908. :group 'org-link-follow
  909. :type 'boolean)
  910. (defcustom org-mark-ring-length 4
  911. "Number of different positions to be recorded in the ring
  912. Changing this requires a restart of Emacs to work correctly."
  913. :group 'org-link-follow
  914. :type 'interger)
  915. (defcustom org-link-frame-setup
  916. '((vm . vm-visit-folder-other-frame)
  917. (gnus . gnus-other-frame)
  918. (file . find-file-other-window))
  919. "Setup the frame configuration for following links.
  920. When following a link with Emacs, it may often be useful to display
  921. this link in another window or frame. This variable can be used to
  922. set this up for the different types of links.
  923. For VM, use any of
  924. `vm-visit-folder'
  925. `vm-visit-folder-other-frame'
  926. For Gnus, use any of
  927. `gnus'
  928. `gnus-other-frame'
  929. For FILE, use any of
  930. `find-file'
  931. `find-file-other-window'
  932. `find-file-other-frame'
  933. For the calendar, use the variable `calendar-setup'.
  934. For BBDB, it is currently only possible to display the matches in
  935. another window."
  936. :group 'org-link-follow
  937. :type '(list
  938. (cons (const vm)
  939. (choice
  940. (const vm-visit-folder)
  941. (const vm-visit-folder-other-window)
  942. (const vm-visit-folder-other-frame)))
  943. (cons (const gnus)
  944. (choice
  945. (const gnus)
  946. (const gnus-other-frame)))
  947. (cons (const file)
  948. (choice
  949. (const find-file)
  950. (const find-file-other-window)
  951. (const find-file-other-frame)))))
  952. (defcustom org-display-internal-link-with-indirect-buffer nil
  953. "Non-nil means, use indirect buffer to display infile links.
  954. Activating internal links (from one location in a file to another location
  955. in the same file) normally just jumps to the location. When the link is
  956. activated with a C-u prefix (or with mouse-3), the link is displayed in
  957. another window. When this option is set, the other window actually displays
  958. an indirect buffer clone of the current buffer, to avoid any visibility
  959. changes to the current buffer."
  960. :group 'org-link-follow
  961. :type 'boolean)
  962. (defcustom org-open-non-existing-files nil
  963. "Non-nil means, `org-open-file' will open non-existing file.
  964. When nil, an error will be generated."
  965. :group 'org-link-follow
  966. :type 'boolean)
  967. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  968. "Function and arguments to call for following mailto links.
  969. This is a list with the first element being a lisp function, and the
  970. remaining elements being arguments to the function. In string arguments,
  971. %a will be replaced by the address, and %s will be replaced by the subject
  972. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  973. :group 'org-link-follow
  974. :type '(choice
  975. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  976. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  977. (const :tag "message-mail" (message-mail "%a" "%s"))
  978. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  979. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  980. "Non-nil means, ask for confirmation before executing shell links.
  981. Shell links can be dangerous, just thing about a link
  982. [[shell:rm -rf ~/*][Google Search]]
  983. This link would show up in your Org-mode document as \"Google Search\"
  984. but really it would remove your entire home directory.
  985. Therefore I *definitely* advise against setting this variable to nil.
  986. Just change it to `y-or-n-p' of you want to confirm with a single key press
  987. rather than having to type \"yes\"."
  988. :group 'org-link-follow
  989. :type '(choice
  990. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  991. (const :tag "with y-or-n (faster)" y-or-n-p)
  992. (const :tag "no confirmation (dangerous)" nil)))
  993. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  994. "Non-nil means, ask for confirmation before executing elisp links.
  995. Elisp links can be dangerous, just think about a link
  996. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  997. This link would show up in your Org-mode document as \"Google Search\"
  998. but really it would remove your entire home directory.
  999. Therefore I *definitely* advise against setting this variable to nil.
  1000. Just change it to `y-or-n-p' of you want to confirm with a single key press
  1001. rather than having to type \"yes\"."
  1002. :group 'org-link-follow
  1003. :type '(choice
  1004. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1005. (const :tag "with y-or-n (faster)" y-or-n-p)
  1006. (const :tag "no confirmation (dangerous)" nil)))
  1007. (defconst org-file-apps-defaults-gnu
  1008. '((remote . emacs)
  1009. (t . mailcap))
  1010. "Default file applications on a UNIX or GNU/Linux system.
  1011. See `org-file-apps'.")
  1012. (defconst org-file-apps-defaults-macosx
  1013. '((remote . emacs)
  1014. (t . "open %s")
  1015. ("ps" . "gv %s")
  1016. ("ps.gz" . "gv %s")
  1017. ("eps" . "gv %s")
  1018. ("eps.gz" . "gv %s")
  1019. ("dvi" . "xdvi %s")
  1020. ("fig" . "xfig %s"))
  1021. "Default file applications on a MacOS X system.
  1022. The system \"open\" is known as a default, but we use X11 applications
  1023. for some files for which the OS does not have a good default.
  1024. See `org-file-apps'.")
  1025. (defconst org-file-apps-defaults-windowsnt
  1026. (list
  1027. '(remote . emacs)
  1028. (cons t
  1029. (list (if (featurep 'xemacs)
  1030. 'mswindows-shell-execute
  1031. 'w32-shell-execute)
  1032. "open" 'file)))
  1033. "Default file applications on a Windows NT system.
  1034. The system \"open\" is used for most files.
  1035. See `org-file-apps'.")
  1036. (defcustom org-file-apps
  1037. '(
  1038. ("txt" . emacs)
  1039. ("tex" . emacs)
  1040. ("ltx" . emacs)
  1041. ("org" . emacs)
  1042. ("el" . emacs)
  1043. ("bib" . emacs)
  1044. )
  1045. "External applications for opening `file:path' items in a document.
  1046. Org-mode uses system defaults for different file types, but
  1047. you can use this variable to set the application for a given file
  1048. extension. The entries in this list are cons cells where the car identifies
  1049. files and the cdr the corresponding command. Possible values for the
  1050. file identifier are
  1051. \"ext\" A string identifying an extension
  1052. `directory' Matches a directory
  1053. `remote' Matches a remote file, accessible through tramp or efs.
  1054. Remote files most likely should be visited through Emacs
  1055. because external applications cannot handle such paths.
  1056. t Default for all remaining files
  1057. Possible values for the command are:
  1058. `emacs' The file will be visited by the current Emacs process.
  1059. `default' Use the default application for this file type.
  1060. string A command to be executed by a shell; %s will be replaced
  1061. by the path to the file.
  1062. sexp A Lisp form which will be evaluated. The file path will
  1063. be available in the Lisp variable `file'.
  1064. For more examples, see the system specific constants
  1065. `org-file-apps-defaults-macosx'
  1066. `org-file-apps-defaults-windowsnt'
  1067. `org-file-apps-defaults-gnu'."
  1068. :group 'org-link-follow
  1069. :type '(repeat
  1070. (cons (choice :value ""
  1071. (string :tag "Extension")
  1072. (const :tag "Default for unrecognized files" t)
  1073. (const :tag "Remote file" remote)
  1074. (const :tag "Links to a directory" directory))
  1075. (choice :value ""
  1076. (const :tag "Visit with Emacs" emacs)
  1077. (const :tag "Use system default" default)
  1078. (string :tag "Command")
  1079. (sexp :tag "Lisp form")))))
  1080. (defcustom org-mhe-search-all-folders nil
  1081. "Non-nil means, that the search for the mh-message will be extended to
  1082. all folders if the message cannot be found in the folder given in the link.
  1083. Searching all folders is very effective with one of the search engines
  1084. supported by MH-E, but will be slow with pick."
  1085. :group 'org-link-follow
  1086. :type 'boolean)
  1087. (defgroup org-remember nil
  1088. "Options concerning interaction with remember.el."
  1089. :tag "Org Remember"
  1090. :group 'org)
  1091. (defcustom org-directory "~/org"
  1092. "Directory with org files.
  1093. This directory will be used as default to prompt for org files.
  1094. Used by the hooks for remember.el."
  1095. :group 'org-remember
  1096. :type 'directory)
  1097. (defcustom org-default-notes-file "~/.notes"
  1098. "Default target for storing notes.
  1099. Used by the hooks for remember.el. This can be a string, or nil to mean
  1100. the value of `remember-data-file'.
  1101. You can set this on a per-template basis with the variable
  1102. `org-remember-templates'."
  1103. :group 'org-remember
  1104. :type '(choice
  1105. (const :tag "Default from remember-data-file" nil)
  1106. file))
  1107. (defcustom org-remember-default-headline ""
  1108. "The headline that should be the default location in the notes file.
  1109. When filing remember notes, the cursor will start at that position.
  1110. You can set this on a per-template basis with the variable
  1111. `org-remember-templates'."
  1112. :group 'org-remember
  1113. :type 'string)
  1114. (defcustom org-remember-templates nil
  1115. "Templates for the creation of remember buffers.
  1116. When nil, just let remember make the buffer.
  1117. When not nil, this is a list of 4-element lists. In each entry, the first
  1118. element is a character, a unique key to select this template.
  1119. The second element is the template. The third element is optional and can
  1120. specify a destination file for remember items created with this template.
  1121. The default file is given by `org-default-notes-file'. An optional third
  1122. element can specify the headline in that file that should be offered
  1123. first when the user is asked to file the entry. The default headline is
  1124. given in the variable `org-remember-default-headline'.
  1125. The template specifies the structure of the remember buffer. It should have
  1126. a first line starting with a star, to act as the org-mode headline.
  1127. Furthermore, the following %-escapes will be replaced with content:
  1128. %^{prompt} prompt the user for a string and replace this sequence with it.
  1129. %t time stamp, date only
  1130. %T time stamp with date and time
  1131. %u, %U like the above, but inactive time stamps
  1132. %^t like %t, but prompt for date. Similarly %^T, %^u, %^U
  1133. You may define a prompt like %^{Please specify birthday}t
  1134. %n user name (taken from `user-full-name')
  1135. %a annotation, normally the link created with org-store-link
  1136. %i initial content, the region when remember is called with C-u.
  1137. If %i is indented, the entire inserted text will be indented
  1138. as well.
  1139. %? After completing the template, position cursor here.
  1140. Apart from these general escapes, you can access information specific to the
  1141. link type that is created. For example, calling `remember' in emails or gnus
  1142. will record the author and the subject of the message, which you can access
  1143. with %:author and %:subject, respectively. Here is a complete list of what
  1144. is recorded for each link type.
  1145. Link type | Available information
  1146. -------------------+------------------------------------------------------
  1147. bbdb | %:type %:name %:company
  1148. vm, wl, mh, rmail | %:type %:subject %:message-id
  1149. | %:from %:fromname %:fromaddress
  1150. | %:to %:toname %:toaddress
  1151. | %:fromto (either \"to NAME\" or \"from NAME\")
  1152. gnus | %:group, for messages also all email fields
  1153. w3, w3m | %:type %:url
  1154. info | %:type %:file %:node
  1155. calendar | %:type %:date"
  1156. :group 'org-remember
  1157. :get (lambda (var) ; Make sure all entries have 4 elements
  1158. (mapcar (lambda (x)
  1159. (cond ((= (length x) 3) (append x '("")))
  1160. ((= (length x) 2) (append x '("" "")))
  1161. (t x)))
  1162. (default-value var)))
  1163. :type '(repeat
  1164. :tag "enabled"
  1165. (list :value (?a "\n" nil nil)
  1166. (character :tag "Selection Key")
  1167. (string :tag "Template")
  1168. (file :tag "Destination file (optional)")
  1169. (string :tag "Destination headline (optional)"))))
  1170. (defcustom org-reverse-note-order nil
  1171. "Non-nil means, store new notes at the beginning of a file or entry.
  1172. When nil, new notes will be filed to the end of a file or entry."
  1173. :group 'org-remember
  1174. :type '(choice
  1175. (const :tag "Reverse always" t)
  1176. (const :tag "Reverse never" nil)
  1177. (repeat :tag "By file name regexp"
  1178. (cons regexp boolean))))
  1179. (defgroup org-todo nil
  1180. "Options concerning TODO items in Org-mode."
  1181. :tag "Org TODO"
  1182. :group 'org)
  1183. (defgroup org-progress nil
  1184. "Options concerning Progress logging in Org-mode."
  1185. :tag "Org Progress"
  1186. :group 'org-time)
  1187. (defcustom org-todo-keywords '("TODO" "DONE")
  1188. "List of TODO entry keywords.
  1189. \\<org-mode-map>By default, this is '(\"TODO\" \"DONE\"). The last entry in the list is
  1190. considered to mean that the entry is \"done\". All the other mean that
  1191. action is required, and will make the entry show up in todo lists, diaries
  1192. etc.
  1193. The command \\[org-todo] cycles an entry through these states, and an
  1194. additional state where no keyword is present. For details about this
  1195. cycling, see also the variable `org-todo-interpretation'
  1196. Changes become only effective after restarting Emacs."
  1197. :group 'org-todo
  1198. :group 'org-keywords
  1199. :type '(repeat (string :tag "Keyword")))
  1200. (defcustom org-todo-interpretation 'sequence
  1201. "Controls how TODO keywords are interpreted.
  1202. This variable is only relevant if `org-todo-keywords' contains more than two
  1203. states. \\<org-mode-map>Possible values are `sequence' and `type'.
  1204. When `sequence', \\[org-todo] will always switch to the next state in the
  1205. `org-todo-keywords' list. When `type', \\[org-todo] only cycles from state
  1206. to state when executed several times in direct succession. Otherwise, it
  1207. switches directly to DONE from any state.
  1208. See the manual for more information."
  1209. :group 'org-todo
  1210. :group 'org-keywords
  1211. :type '(choice (const sequence)
  1212. (const type)))
  1213. (defcustom org-after-todo-state-change-hook nil
  1214. "Hook which is run after the state of a TODO item was changed.
  1215. The new state (a string with a TODO keyword, or nil) is available in the
  1216. Lisp variable `state'."
  1217. :group 'org-todo
  1218. :type 'hook)
  1219. (defcustom org-log-done nil
  1220. "When set, insert a (non-active) time stamp when TODO entry is marked DONE.
  1221. When the state of an entry is changed from nothing to TODO, remove a previous
  1222. closing date.
  1223. This can also be a list of symbols indicating under which conditions
  1224. the time stamp recording the action should be annotated with a short note.
  1225. Valid members of this list are
  1226. done Offer to record a note when marking entries done
  1227. state Offer to record a note whenever changing the TODO state
  1228. of an item. This is only relevant if TODO keywords are
  1229. interpreted as sequence, see variable `org-todo-interpretation'.
  1230. When `state' is set, this includes tracking `done'.
  1231. clock-out Offer to record a note when clocking out of an item.
  1232. A separate window will then pop up and allow you to type a note.
  1233. After finishing with C-c C-c, the note will be added directly after the
  1234. timestamp, as a plain list item. See also the variable
  1235. `org-log-note-headings'.
  1236. Logging can also be configured on a per-file basis by adding one of
  1237. the following lines anywhere in the buffer:
  1238. #+STARTUP: logdone
  1239. #+STARTUP: nologging
  1240. #+STARTUP: lognotedone
  1241. #+STARTUP: lognotestate
  1242. #+STARTUP: lognoteclock-out"
  1243. :group 'org-todo
  1244. :group 'org-progress
  1245. :type '(choice
  1246. (const :tag "off" nil)
  1247. (const :tag "on" t)
  1248. (set :tag "on, with notes, detailed control" :greedy t :value (done)
  1249. (const :tag "when item is marked DONE" done)
  1250. (const :tag "when TODO state changes" state)
  1251. (const :tag "when clocking out" clock-out))))
  1252. (defcustom org-log-note-headings
  1253. '((done . "CLOSING NOTE %t")
  1254. (state . "State %-12s %t")
  1255. (clock-out . ""))
  1256. "Headings for notes added when clocking out or closing TODO items.
  1257. The value is an alist, with the car being a sympol indicating the note
  1258. context, and the cdr is the heading to be used. The heading may also be the
  1259. empty string.
  1260. %t in the heading will be replaced by a time stamp.
  1261. %s will be replaced by the new TODO state, in double quotes.
  1262. %u will be replaced by the user name.
  1263. %U will be replaced by the full user name."
  1264. :group 'org-todo
  1265. :group 'org-progress
  1266. :type '(list :greedy t
  1267. (cons (const :tag "Heading when closing an item" done) string)
  1268. (cons (const :tag
  1269. "Heading when changing todo state (todo sequence only)"
  1270. state) string)
  1271. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1272. (defgroup org-priorities nil
  1273. "Priorities in Org-mode."
  1274. :tag "Org Priorities"
  1275. :group 'org-todo)
  1276. (defcustom org-default-priority ?B
  1277. "The default priority of TODO items.
  1278. This is the priority an item get if no explicit priority is given."
  1279. :group 'org-priorities
  1280. :type 'character)
  1281. (defcustom org-lowest-priority ?C
  1282. "The lowest priority of TODO items. A character like ?A, ?B etc."
  1283. :group 'org-priorities
  1284. :type 'character)
  1285. (defgroup org-time nil
  1286. "Options concerning time stamps and deadlines in Org-mode."
  1287. :tag "Org Time"
  1288. :group 'org)
  1289. (defcustom org-insert-labeled-timestamps-at-point nil
  1290. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1291. When nil, these labeled time stamps are forces into the second line of an
  1292. entry, just after the headline. When scheduling from the global TODO list,
  1293. the time stamp will always be forced into the second line."
  1294. :group 'org-time
  1295. :type 'boolean)
  1296. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1297. "Formats for `format-time-string' which are used for time stamps.
  1298. It is not recommended to change this constant.")
  1299. (defcustom org-time-stamp-rounding-minutes 0
  1300. "Number of minutes to round time stamps to upon insertion.
  1301. When zero, insert the time unmodified. Useful rounding numbers
  1302. should be factors of 60, so for example 5, 10, 15.
  1303. When this is not zero, you can still force an exact time-stamp by using
  1304. a double prefix argument to a time-stamp command like `C-c .' or `C-c !'."
  1305. :group 'org-time
  1306. :type 'integer)
  1307. (defcustom org-display-custom-times nil
  1308. "Non-nil means, overlay custom formats over all time stamps.
  1309. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1310. To turn this on on a per-file basis, insert anywhere in the file:
  1311. #+STARTUP: customtime"
  1312. :group 'org-time
  1313. :set 'set-default
  1314. :type 'sexp)
  1315. (make-variable-buffer-local 'org-display-custom-times)
  1316. (defcustom org-time-stamp-custom-formats
  1317. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1318. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1319. These are overlayed over the default ISO format if the variable
  1320. `org-display-custom-times' is set."
  1321. :group 'org-time
  1322. :type 'sexp)
  1323. (defun org-time-stamp-format (&optional long inactive)
  1324. "Get the right format for a time string."
  1325. (let ((f (if long (cdr org-time-stamp-formats)
  1326. (car org-time-stamp-formats))))
  1327. (if inactive
  1328. (concat "[" (substring f 1 -1) "]")
  1329. f)))
  1330. (defcustom org-deadline-warning-days 30
  1331. "No. of days before expiration during which a deadline becomes active.
  1332. This variable governs the display in sparse trees and in the agenda."
  1333. :group 'org-time
  1334. :type 'number)
  1335. (defcustom org-popup-calendar-for-date-prompt t
  1336. "Non-nil means, pop up a calendar when prompting for a date.
  1337. In the calendar, the date can be selected with mouse-1. However, the
  1338. minibuffer will also be active, and you can simply enter the date as well.
  1339. When nil, only the minibuffer will be available."
  1340. :group 'org-time
  1341. :type 'boolean)
  1342. (defcustom org-calendar-follow-timestamp-change t
  1343. "Non-nil means, make the calendar window follow timestamp changes.
  1344. When a timestamp is modified and the calendar window is visible, it will be
  1345. moved to the new date."
  1346. :group 'org-time
  1347. :type 'boolean)
  1348. (defgroup org-tags nil
  1349. "Options concerning tags in Org-mode."
  1350. :tag "Org Tags"
  1351. :group 'org)
  1352. (defcustom org-tag-alist nil
  1353. "List of tags allowed in Org-mode files.
  1354. When this list is nil, Org-mode will base TAG input on what is already in the
  1355. buffer.
  1356. The value of this variable is an alist, the car may be (and should) be a
  1357. character that is used to select that tag through the fast-tag-selection
  1358. interface. See the manual for details."
  1359. :group 'org-tags
  1360. :type '(repeat
  1361. (choice
  1362. (cons (string :tag "Tag name")
  1363. (character :tag "Access char"))
  1364. (const :tag "Start radio group" (:startgroup))
  1365. (const :tag "End radio group" (:endgroup)))))
  1366. (defcustom org-use-fast-tag-selection 'auto
  1367. "Non-nil means, use fast tag selection scheme.
  1368. This is a special interface to select and deselect tags with single keys.
  1369. When nil, fast selection is never used.
  1370. When the symbol `auto', fast selection is used if and only if selection
  1371. characters for tags have been configured, either through the variable
  1372. `org-tag-alist' or through a #+TAGS line in the buffer.
  1373. When t, fast selection is always used and selection keys are assigned
  1374. automatically if necessary."
  1375. :group 'org-tags
  1376. :type '(choice
  1377. (const :tag "Always" t)
  1378. (const :tag "Never" nil)
  1379. (const :tag "When selection characters are configured" 'auto)))
  1380. (defcustom org-fast-tag-selection-single-key nil
  1381. "Non-nil means, fast tag selection exits after first change.
  1382. When nil, you have to press RET to exit it.
  1383. During fast tag selection, you can toggle this flag with `C-c'."
  1384. :group 'org-tags
  1385. :type 'boolean)
  1386. (defcustom org-tags-column 48
  1387. "The column to which tags should be indented in a headline.
  1388. If this number is positive, it specifies the column. If it is negative,
  1389. it means that the tags should be flushright to that column. For example,
  1390. -79 works well for a normal 80 character screen."
  1391. :group 'org-tags
  1392. :type 'integer)
  1393. (defcustom org-auto-align-tags t
  1394. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1395. These operations change the length of a headline and therefore shift
  1396. the tags around. With this options turned on, after each such operation
  1397. the tags are again aligned to `org-tags-column'."
  1398. :group 'org-tags
  1399. :type 'boolean)
  1400. (defcustom org-use-tag-inheritance t
  1401. "Non-nil means, tags in levels apply also for sublevels.
  1402. When nil, only the tags directly given in a specific line apply there.
  1403. If you turn off this option, you very likely want to turn on the
  1404. companion option `org-tags-match-list-sublevels'."
  1405. :group 'org-tags
  1406. :type 'boolean)
  1407. (defcustom org-tags-match-list-sublevels nil
  1408. "Non-nil means list also sublevels of headlines matching tag search.
  1409. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1410. the sublevels of a headline matching a tag search often also match
  1411. the same search. Listing all of them can create very long lists.
  1412. Setting this variable to nil causes subtrees of a match to be skipped.
  1413. This option is off by default, because inheritance in on. If you turn
  1414. inheritance off, you very likely want to turn this option on.
  1415. As a special case, if the tag search is restricted to TODO items, the
  1416. value of this variable is ignored and sublevels are always checked, to
  1417. make sure all corresponding TODO items find their way into the list."
  1418. :group 'org-tags
  1419. :type 'boolean)
  1420. (defvar org-tags-history nil
  1421. "History of minibuffer reads for tags.")
  1422. (defvar org-last-tags-completion-table nil
  1423. "The last used completion table for tags.")
  1424. (defgroup org-agenda nil
  1425. "Options concerning agenda views in Org-mode."
  1426. :tag "Org Agenda"
  1427. :group 'org)
  1428. (defvar org-category nil
  1429. "Variable used by org files to set a category for agenda display.
  1430. Such files should use a file variable to set it, for example
  1431. -*- mode: org; org-category: \"ELisp\"
  1432. or contain a special line
  1433. #+CATEGORY: ELisp
  1434. If the file does not specify a category, then file's base name
  1435. is used instead.")
  1436. (make-variable-buffer-local 'org-category)
  1437. (defcustom org-agenda-files nil
  1438. "The files to be used for agenda display.
  1439. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1440. \\[org-remove-file]. You can also use customize to edit the list.
  1441. If the value of the variable is not a list but a single file name, then
  1442. the list of agenda files is actually stored and maintained in that file, one
  1443. agenda file per line."
  1444. :group 'org-agenda
  1445. :type '(choice
  1446. (repeat :tag "List of files" file)
  1447. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1448. (defcustom org-agenda-confirm-kill 1
  1449. "When set, remote killing from the agenda buffer needs confirmation.
  1450. When t, a confirmation is always needed. When a number N, confirmation is
  1451. only needed when the text to be killed contains more than N non-white lines."
  1452. :group 'org-agenda
  1453. :type '(choice
  1454. (const :tag "Never" nil)
  1455. (const :tag "Always" t)
  1456. (number :tag "When more than N lines")))
  1457. (defcustom org-calendar-to-agenda-key [?c]
  1458. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1459. The command `org-calendar-goto-agenda' will be bound to this key. The
  1460. default is the character `c' because then `c' can be used to switch back and
  1461. forth between agenda and calendar."
  1462. :group 'org-agenda
  1463. :type 'sexp)
  1464. (defgroup org-agenda-custom-commands nil
  1465. "Options concerning agenda views in Org-mode."
  1466. :tag "Org Agenda Custom Commands"
  1467. :group 'org-agenda)
  1468. (defcustom org-agenda-custom-commands '(("w" todo "WAITING"))
  1469. "Custom commands for the agenda.
  1470. These commands will be offered on the splash screen displayed by the
  1471. agenda dispatcher \\[org-agenda]. Each entry is a list like this:
  1472. (key type match options)
  1473. key The key (a single char as a string) to be associated with the command.
  1474. type The command type, any of the following symbols:
  1475. todo Entries with a specific TODO keyword, in all agenda files.
  1476. tags Tags match in all agenda files.
  1477. tags-todo Tags match in all agenda files, TODO entries only.
  1478. todo-tree Sparse tree of specific TODO keyword in *current* file.
  1479. tags-tree Sparse tree with all tags matches in *current* file.
  1480. occur-tree Occur sparse tree for *current* file.
  1481. match What to search for:
  1482. - a single keyword for TODO keyword searches
  1483. - a tags match expression for tags searches
  1484. - a regular expression for occur searches
  1485. options A list of option setttings, similar to that in a let form, so like
  1486. this: ((opt1 val1) (opt2 val2) ...)
  1487. You can also define a set of commands, to create a composite agenda buffer.
  1488. In this case, an entry looks like this:
  1489. (key desc (cmd1 cmd2 ...) general-options)
  1490. where
  1491. desc A description string to be displayed in the dispatcher menu.
  1492. cmd An agenda command, similar to the above. However, tree commands
  1493. are no allowed, but instead you can get agenda and global todo list.
  1494. So valid commands for a set are:
  1495. (agenda)
  1496. (alltodo)
  1497. (stuck)
  1498. (todo \"match\" options)
  1499. (tags \"match\" options )
  1500. (tags-todo \"match\" options)
  1501. Each command can carry a list of options, and another set of options can be
  1502. given for the whole set of commands. Individual command options take
  1503. precedence over the general options."
  1504. :group 'org-agenda-custom-commands
  1505. :type '(repeat
  1506. (choice
  1507. (list :tag "Single command"
  1508. (string :tag "Key")
  1509. (choice
  1510. (const :tag "Tags search (all agenda files)" tags)
  1511. (const :tag "Tags search of TODO entries (all agenda files)" tags-todo)
  1512. (const :tag "TODO keyword search (all agenda files)" todo)
  1513. (const :tag "Tags sparse tree (current buffer)" tags-tree)
  1514. (const :tag "TODO keyword tree (current buffer)" todo-tree)
  1515. (const :tag "Occur tree (current buffer)" occur-tree)
  1516. (symbol :tag "Other, user-defined function"))
  1517. (string :tag "Match")
  1518. (repeat :tag "Local options"
  1519. (list (variable :tag "Option") (sexp :tag "Value"))))
  1520. (list :tag "Command series, all agenda files"
  1521. (string :tag "Key")
  1522. (string :tag "Description")
  1523. (repeat
  1524. (choice
  1525. (const :tag "Agenda" (agenda))
  1526. (const :tag "TODO list" (alltodo))
  1527. (const :tag "Stuck projects" (stuck))
  1528. (list :tag "Tags search"
  1529. (const :format "" tags)
  1530. (string :tag "Match")
  1531. (repeat :tag "Local options"
  1532. (list (variable :tag "Option")
  1533. (sexp :tag "Value"))))
  1534. (list :tag "Tags search, TODO entries only"
  1535. (const :format "" tags-todo)
  1536. (string :tag "Match")
  1537. (repeat :tag "Local options"
  1538. (list (variable :tag "Option")
  1539. (sexp :tag "Value"))))
  1540. (list :tag "TODO keyword search"
  1541. (const :format "" todo)
  1542. (string :tag "Match")
  1543. (repeat :tag "Local options"
  1544. (list (variable :tag "Option")
  1545. (sexp :tag "Value"))))
  1546. (list :tag "Other, user-defined function"
  1547. (symbol :tag "function")
  1548. (string :tag "Match")
  1549. (repeat :tag "Local options"
  1550. (list (variable :tag "Option")
  1551. (sexp :tag "Value"))))))
  1552. (repeat :tag "General options"
  1553. (list (variable :tag "Option")
  1554. (sexp :tag "Value")))))))
  1555. (defcustom org-stuck-projects
  1556. '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil)
  1557. "How to identify stuck projects.
  1558. This is a list of three items:
  1559. 1. A tags/todo matcher string that is used to identify a project.
  1560. The entire tree below a headline matched by this is considered a project.
  1561. 2. A list of TODO keywords itentifying non-stuck projects.
  1562. If the project subtree contains any headline with one of these todo
  1563. keywords, the project is consitered to be not stuck.
  1564. 3. A list of tags identifying non-stuck projects.
  1565. If the project subtree contains any headline with one of these tags,
  1566. the project is consitered to be not stuck.
  1567. After defining this variable, you may use \\[org-agenda-list-stuck-projects]
  1568. or `C-c a #' to produce the list."
  1569. :group 'org-agenda-custom-commands
  1570. :type '(list
  1571. (string :tag "Tags/TODO match to identify a project")
  1572. (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
  1573. (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))))
  1574. (defgroup org-agenda-skip nil
  1575. "Options concerning skipping parts of agenda files."
  1576. :tag "Org Agenda Skip"
  1577. :group 'org-agenda)
  1578. (defcustom org-agenda-todo-list-sublevels t
  1579. "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
  1580. When nil, the sublevels of a TODO entry are not checked, resulting in
  1581. potentially much shorter TODO lists."
  1582. :group 'org-agenda-skip
  1583. :group 'org-todo
  1584. :type 'boolean)
  1585. (defcustom org-agenda-todo-ignore-scheduled nil
  1586. "Non-nil means, don't show scheduled entries in the global todo list.
  1587. The idea behind this is that by scheduling it, you have already taken care
  1588. of this item."
  1589. :group 'org-agenda-skip
  1590. :group 'org-todo
  1591. :type 'boolean)
  1592. (defcustom org-agenda-todo-ignore-deadlines nil
  1593. "Non-nil means, don't show near deadline entries in the global todo list.
  1594. Near means closer than `org-deadline-warning-days' days.
  1595. The idea behind this is that such items will appear in the agenda anyway."
  1596. :group 'org-agenda-skip
  1597. :group 'org-todo
  1598. :type 'boolean)
  1599. (defcustom org-agenda-skip-scheduled-if-done nil
  1600. "Non-nil means don't show scheduled items in agenda when they are done.
  1601. This is relevant for the daily/weekly agenda, not for the TODO list."
  1602. :group 'org-agenda-skip
  1603. :type 'boolean)
  1604. (defcustom org-timeline-show-empty-dates 3
  1605. "Non-nil means, `org-timeline' also shows dates without an entry.
  1606. When nil, only the days which actually have entries are shown.
  1607. When t, all days between the first and the last date are shown.
  1608. When an integer, show also empty dates, but if there is a gap of more than
  1609. N days, just insert a special line indicating the size of the gap."
  1610. :group 'org-agenda-skip ; FIXME: not quite the right group...
  1611. :type '(choice
  1612. (const :tag "None" nil)
  1613. (const :tag "All" t)
  1614. (number :tag "at most")))
  1615. (defgroup org-agenda-startup nil
  1616. "Options concerning initial settings in the Agenda in Org Mode."
  1617. :tag "Org Agenda Startup"
  1618. :group 'org-agenda)
  1619. (defcustom org-finalize-agenda-hook nil
  1620. "Hook run just before displaying an agenda buffer."
  1621. :group 'org-agenda-startup
  1622. :type 'hook)
  1623. (defcustom org-agenda-mouse-1-follows-link nil
  1624. "Non-nil means, mouse-1 on a link will follow the link in the agenda.
  1625. A longer mouse click will still set point. Does not wortk on XEmacs.
  1626. Needs to be set before org.el is loaded."
  1627. :group 'org-agenda-startup
  1628. :type 'boolean)
  1629. (defcustom org-agenda-start-with-follow-mode nil
  1630. "The initial value of follwo-mode in a newly created agenda window."
  1631. :group 'org-agenda-startup
  1632. :type 'boolean)
  1633. (defgroup org-agenda-windows nil
  1634. "Options concerning the windows used by the Agenda in Org Mode."
  1635. :tag "Org Agenda Windows"
  1636. :group 'org-agenda)
  1637. (defcustom org-agenda-window-setup 'reorganize-frame
  1638. "How the agenda buffer should be displayed.
  1639. Possible values for this option are:
  1640. current-window Show agenda in the current window, keeping all other windows.
  1641. other-frame Use `switch-to-buffer-other-frame' to display agenda.
  1642. other-window Use `switch-to-buffer-other-window' to display agenda.
  1643. reorganize-frame Show only two windows on the current frame, the current
  1644. window and the agenda. Also, if the option
  1645. `org-fit-agenda-window' is set, resize the agenda window to
  1646. try to show as much as possible of the buffer content.
  1647. See also the variable `org-agenda-restore-windows-after-quit'."
  1648. :group 'org-agenda-windows
  1649. :type '(choice
  1650. (const current-window)
  1651. (const other-frame)
  1652. (const other-window)
  1653. (const reorganize-frame)))
  1654. (defcustom org-agenda-restore-windows-after-quit nil
  1655. "Non-nil means, restore window configuration open exiting agenda.
  1656. Before the window configuration is changed for displaying the agenda,
  1657. the current status is recorded. When the agenda is exited with
  1658. `q' or `x' and this option is set, the old state is restored. If
  1659. `org-agenda-window-setup' is `other-frame', the value of this
  1660. option will be ignored.."
  1661. :group 'org-agenda-windows
  1662. :type 'boolean)
  1663. ;; FIXME: I think this variable could be removed, default set to t
  1664. (defcustom org-select-agenda-window t
  1665. "Non-nil means, after creating an agenda, move cursor into Agenda window.
  1666. When nil, cursor will remain in the current window."
  1667. :group 'org-agenda-windows
  1668. :type 'boolean)
  1669. ;; FIXME: I think this variable could be removed, default set to t
  1670. (defcustom org-fit-agenda-window t
  1671. "Non-nil means, change window size of agenda to fit content.
  1672. This is only effective if `org-agenda-window-setup' is `reorganize-frame'."
  1673. :group 'org-agenda-windows
  1674. :type 'boolean)
  1675. (defcustom org-indirect-buffer-display 'other-window
  1676. "How should indirect tree buffers be displayed?
  1677. This applies to indirect buffers created with the commands
  1678. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  1679. Valid values are:
  1680. current-window Display in the current window
  1681. other-window Just display in another window.
  1682. dedicated-frame Create one new frame, and re-use it each time.
  1683. new-frame Make a new frame each time."
  1684. :group 'org-structure
  1685. :group 'org-agenda-windows
  1686. :type '(choice
  1687. (const :tag "In current window" current-window)
  1688. (const :tag "In current frame, other window" other-window)
  1689. (const :tag "Each time a new frame" new-frame)
  1690. (const :tag "One dedicated frame" dedicated-frame)))
  1691. (defgroup org-agenda-daily/weekly nil
  1692. "Options concerning the daily/weekly agenda."
  1693. :tag "Org Agenda Daily/Weekly"
  1694. :group 'org-agenda)
  1695. (defcustom org-agenda-ndays 7
  1696. "Number of days to include in overview display.
  1697. Should be 1 or 7."
  1698. :group 'org-agenda-daily/weekly
  1699. :type 'number)
  1700. (defcustom org-agenda-start-on-weekday 1
  1701. "Non-nil means, start the overview always on the specified weekday.
  1702. 0 denotes Sunday, 1 denotes Monday etc.
  1703. When nil, always start on the current day."
  1704. :group 'org-agenda-daily/weekly
  1705. :type '(choice (const :tag "Today" nil)
  1706. (number :tag "Weekday No.")))
  1707. (defcustom org-agenda-show-all-dates t
  1708. "Non-nil means, `org-agenda' shows every day in the selected range.
  1709. When nil, only the days which actually have entries are shown."
  1710. :group 'org-agenda-daily/weekly
  1711. :type 'boolean)
  1712. (defcustom org-agenda-date-format "%A %d %B %Y"
  1713. "Format string for displaying dates in the agenda.
  1714. Used by the daily/weekly agenda and by the timeline. This should be
  1715. a format string understood by `format-time-string'.
  1716. FIXME: Not used currently, because of timezone problem."
  1717. :group 'org-agenda-daily/weekly
  1718. :type 'string)
  1719. (defcustom org-agenda-include-diary nil
  1720. "If non-nil, include in the agenda entries from the Emacs Calendar's diary."
  1721. :group 'org-agenda-daily/weekly
  1722. :type 'boolean)
  1723. ;; FIXME: This variable could be removed
  1724. (defcustom org-agenda-include-all-todo nil
  1725. "Set means weekly/daily agenda will always contain all TODO entries.
  1726. The TODO entries will be listed at the top of the agenda, before
  1727. the entries for specific days."
  1728. :group 'org-agenda-daily/weekly
  1729. :type 'boolean)
  1730. (defgroup org-agenda-time-grid nil
  1731. "Options concerning the time grid in the Org-mode Agenda."
  1732. :tag "Org Agenda Time Grid"
  1733. :group 'org-agenda)
  1734. (defcustom org-agenda-use-time-grid t
  1735. "Non-nil means, show a time grid in the agenda schedule.
  1736. A time grid is a set of lines for specific times (like every two hours between
  1737. 8:00 and 20:00). The items scheduled for a day at specific times are
  1738. sorted in between these lines.
  1739. For details about when the grid will be shown, and what it will look like, see
  1740. the variable `org-agenda-time-grid'."
  1741. :group 'org-agenda-time-grid
  1742. :type 'boolean)
  1743. (defcustom org-agenda-time-grid
  1744. '((daily today require-timed)
  1745. "----------------"
  1746. (800 1000 1200 1400 1600 1800 2000))
  1747. "The settings for time grid for agenda display.
  1748. This is a list of three items. The first item is again a list. It contains
  1749. symbols specifying conditions when the grid should be displayed:
  1750. daily if the agenda shows a single day
  1751. weekly if the agenda shows an entire week
  1752. today show grid on current date, independent of daily/weekly display
  1753. require-timed show grid only if at least one item has a time specification
  1754. The second item is a string which will be places behing the grid time.
  1755. The third item is a list of integers, indicating the times that should have
  1756. a grid line."
  1757. :group 'org-agenda-time-grid
  1758. :type
  1759. '(list
  1760. (set :greedy t :tag "Grid Display Options"
  1761. (const :tag "Show grid in single day agenda display" daily)
  1762. (const :tag "Show grid in weekly agenda display" weekly)
  1763. (const :tag "Always show grid for today" today)
  1764. (const :tag "Show grid only if any timed entries are present"
  1765. require-timed)
  1766. (const :tag "Skip grid times already present in an entry"
  1767. remove-match))
  1768. (string :tag "Grid String")
  1769. (repeat :tag "Grid Times" (integer :tag "Time"))))
  1770. (defgroup org-agenda-sorting nil
  1771. "Options concerning sorting in the Org-mode Agenda."
  1772. :tag "Org Agenda Sorting"
  1773. :group 'org-agenda)
  1774. (let ((sorting-choice
  1775. '(choice
  1776. (const time-up) (const time-down)
  1777. (const category-keep) (const category-up) (const category-down)
  1778. (const tag-down) (const tag-up)
  1779. (const priority-up) (const priority-down))))
  1780. (defcustom org-agenda-sorting-strategy
  1781. '((agenda time-up category-keep priority-down)
  1782. (todo category-keep priority-down)
  1783. (tags category-keep))
  1784. "Sorting structure for the agenda items of a single day.
  1785. This is a list of symbols which will be used in sequence to determine
  1786. if an entry should be listed before another entry. The following
  1787. symbols are recognized:
  1788. time-up Put entries with time-of-day indications first, early first
  1789. time-down Put entries with time-of-day indications first, late first
  1790. category-keep Keep the default order of categories, corresponding to the
  1791. sequence in `org-agenda-files'.
  1792. category-up Sort alphabetically by category, A-Z.
  1793. category-down Sort alphabetically by category, Z-A.
  1794. tag-up Sort alphabetically by last tag, A-Z.
  1795. tag-down Sort alphabetically by last tag, Z-A.
  1796. priority-up Sort numerically by priority, high priority last.
  1797. priority-down Sort numerically by priority, high priority first.
  1798. The different possibilities will be tried in sequence, and testing stops
  1799. if one comparison returns a \"not-equal\". For example, the default
  1800. '(time-up category-keep priority-down)
  1801. means: Pull out all entries having a specified time of day and sort them,
  1802. in order to make a time schedule for the current day the first thing in the
  1803. agenda listing for the day. Of the entries without a time indication, keep
  1804. the grouped in categories, don't sort the categories, but keep them in
  1805. the sequence given in `org-agenda-files'. Within each category sort by
  1806. priority.
  1807. Leaving out `category-keep' would mean that items will be sorted across
  1808. categories by priority."
  1809. :group 'org-agenda-sorting
  1810. :type `(choice
  1811. (repeat :tag "General" ,sorting-choice)
  1812. (list :tag "Individually"
  1813. (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
  1814. (repeat ,sorting-choice))
  1815. (cons (const :tag "Strategy for TODO lists" todo)
  1816. (repeat ,sorting-choice))
  1817. (cons (const :tag "Strategy for Tags matches" tags)
  1818. (repeat ,sorting-choice))))))
  1819. (defcustom org-sort-agenda-notime-is-late t
  1820. "Non-nil means, items without time are considered late.
  1821. This is only relevant for sorting. When t, items which have no explicit
  1822. time like 15:30 will be considered as 99:01, i.e. later than any items which
  1823. do have a time. When nil, the default time is before 0:00. You can use this
  1824. option to decide if the schedule for today should come before or after timeless
  1825. agenda entries."
  1826. :group 'org-agenda-sorting
  1827. :type 'boolean)
  1828. (defgroup org-agenda-prefix nil
  1829. "Options concerning the entry prefix in the Org-mode agenda display."
  1830. :tag "Org Agenda Prefix"
  1831. :group 'org-agenda)
  1832. (defcustom org-agenda-prefix-format
  1833. '((agenda . " %-12:c%?-12t% s")
  1834. (timeline . " % s")
  1835. (todo . " %-12:c")
  1836. (tags . " %-12:c"))
  1837. "Format specifications for the prefix of items in the agenda views.
  1838. An alist with four entries, for the different agenda types. The keys to the
  1839. sublists are `agenda', `timeline', `todo', and `tags'. The values
  1840. are format strings.
  1841. This format works similar to a printf format, with the following meaning:
  1842. %c the category of the item, \"Diary\" for entries from the diary, or
  1843. as given by the CATEGORY keyword or derived from the file name.
  1844. %T the *last* tag of the item. Last because inherited tags come
  1845. first in the list.
  1846. %t the time-of-day specification if one applies to the entry, in the
  1847. format HH:MM
  1848. %s Scheduling/Deadline information, a short string
  1849. All specifiers work basically like the standard `%s' of printf, but may
  1850. contain two additional characters: A question mark just after the `%' and
  1851. a whitespace/punctuation character just before the final letter.
  1852. If the first character after `%' is a question mark, the entire field
  1853. will only be included if the corresponding value applies to the
  1854. current entry. This is useful for fields which should have fixed
  1855. width when present, but zero width when absent. For example,
  1856. \"%?-12t\" will result in a 12 character time field if a time of the
  1857. day is specified, but will completely disappear in entries which do
  1858. not contain a time.
  1859. If there is punctuation or whitespace character just before the final
  1860. format letter, this character will be appended to the field value if
  1861. the value is not empty. For example, the format \"%-12:c\" leads to
  1862. \"Diary: \" if the category is \"Diary\". If the category were be
  1863. empty, no additional colon would be interted.
  1864. The default value of this option is \" %-12:c%?-12t% s\", meaning:
  1865. - Indent the line with two space characters
  1866. - Give the category in a 12 chars wide field, padded with whitespace on
  1867. the right (because of `-'). Append a colon if there is a category
  1868. (because of `:').
  1869. - If there is a time-of-day, put it into a 12 chars wide field. If no
  1870. time, don't put in an empty field, just skip it (because of '?').
  1871. - Finally, put the scheduling information and append a whitespace.
  1872. As another example, if you don't want the time-of-day of entries in
  1873. the prefix, you could use:
  1874. (setq org-agenda-prefix-format \" %-11:c% s\")
  1875. See also the variables `org-agenda-remove-times-when-in-prefix' and
  1876. `org-agenda-remove-tags-when-in-prefix'."
  1877. :type '(choice
  1878. (string :tag "General format")
  1879. (list :greedy t :tag "View dependent"
  1880. (cons (const agenda) (string :tag "Format"))
  1881. (cons (const timeline) (string :tag "Format"))
  1882. (cons (const todo) (string :tag "Format"))
  1883. (cons (const tags) (string :tag "Format"))))
  1884. :group 'org-agenda-prefix)
  1885. (defvar org-prefix-format-compiled nil
  1886. "The compiled version of the most recently used prefix format.
  1887. See the variable `org-agenda-prefix-format'.")
  1888. (defcustom org-agenda-remove-times-when-in-prefix t
  1889. "Non-nil means, remove duplicate time specifications in agenda items.
  1890. When the format `org-agenda-prefix-format' contains a `%t' specifier, a
  1891. time-of-day specification in a headline or diary entry is extracted and
  1892. placed into the prefix. If this option is non-nil, the original specification
  1893. \(a timestamp or -range, or just a plain time(range) specification like
  1894. 11:30-4pm) will be removed for agenda display. This makes the agenda less
  1895. cluttered.
  1896. The option can be t or nil. It may also be the symbol `beg', indicating
  1897. that the time should only be removed what it is located at the beginning of
  1898. the headline/diary entry."
  1899. :group 'org-agenda-prefix
  1900. :type '(choice
  1901. (const :tag "Always" t)
  1902. (const :tag "Never" nil)
  1903. (const :tag "When at beginning of entry" beg)))
  1904. (defcustom org-agenda-remove-tags-when-in-prefix nil
  1905. "Non-nil means, remove the tags from the headline copy in the agenda.
  1906. When this is the symbol `prefix', only remove tags when
  1907. `org-agenda-prefix-format' contains a `%T' specifier."
  1908. :group 'org-agenda-prefix
  1909. :type '(choice
  1910. (const :tag "Always" t)
  1911. (const :tag "Never" nil)
  1912. (const :tag "When prefix format contains %T" prefix)))
  1913. (defcustom org-agenda-align-tags-to-column 65
  1914. "Shift tags in agenda items to this column."
  1915. :group 'org-agenda-prefix
  1916. :type 'integer)
  1917. (defgroup org-latex nil
  1918. "Options for embedding LaTeX code into Org-mode"
  1919. :tag "Org LaTeX"
  1920. :group 'org)
  1921. (defcustom org-format-latex-options
  1922. '(:foreground "Black" :background "Transparent" :scale 1.0
  1923. :matchers ("begin" "$" "$$" "\\(" "\\["))
  1924. "Options for creating images from LaTeX fragments.
  1925. This is a property list with the following properties:
  1926. :foreground the foreground color, for example \"Black\".
  1927. :background the background color, or \"Transparent\".
  1928. :scale a scaling factor for the size of the images
  1929. :matchers a list indicating which matchers should be used to
  1930. find LaTeX fragments. Valid members of this list are:
  1931. \"begin\" find environments
  1932. \"$\" find math expressions surrounded by $...$
  1933. \"$$\" find math expressions surrounded by $$....$$
  1934. \"\\(\" find math expressions surrounded by \\(...\\)
  1935. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  1936. :group 'org-latex
  1937. :type 'plist)
  1938. (defgroup org-export nil
  1939. "Options for exporting org-listings."
  1940. :tag "Org Export"
  1941. :group 'org)
  1942. (defgroup org-export-general nil
  1943. "General options for exporting Org-mode files."
  1944. :tag "Org Export General"
  1945. :group 'org-export)
  1946. (defcustom org-export-publishing-directory "."
  1947. "Path to the location where exported files should be located.
  1948. This path may be relative to the directory where the Org-mode file lives.
  1949. The default is to put them into the same directory as the Org-mode file.
  1950. The variable may also be an alist with export types `:html', `:ascii',
  1951. `:ical', or `:xoxo' and the corresponding directories. If a direcoty path
  1952. is relative, it is interpreted relative to the directory where the exported
  1953. Org-mode files lives."
  1954. :group 'org-export-general
  1955. :type '(choice
  1956. (directory)
  1957. (repeat
  1958. (cons
  1959. (choice :tag "Type"
  1960. (const :html) (const :ascii) (const :ical) (const :xoxo))
  1961. (directory)))))
  1962. (defcustom org-export-language-setup
  1963. '(("en" "Author" "Date" "Table of Contents")
  1964. ("cs" "Autor" "Datum" "Obsah")
  1965. ("da" "Ophavsmand" "Dato" "Indhold")
  1966. ("de" "Autor" "Datum" "Inhaltsverzeichnis")
  1967. ("es" "Autor" "Fecha" "\xccndice")
  1968. ("fr" "Auteur" "Date" "Table des Mati\xe8res")
  1969. ("it" "Autore" "Data" "Indice")
  1970. ("nl" "Auteur" "Datum" "Inhoudsopgave")
  1971. ("nn" "Forfattar" "Dato" "Innhold") ;; nn = Norsk (nynorsk)
  1972. ("sv" "F\xf6rfattarens" "Datum" "Inneh\xe5ll"))
  1973. "Terms used in export text, translated to different languages.
  1974. Use the variable `org-export-default-language' to set the language,
  1975. or use the +OPTION lines for a per-file setting."
  1976. :group 'org-export-general
  1977. :type '(repeat
  1978. (list
  1979. (string :tag "HTML language tag")
  1980. (string :tag "Author")
  1981. (string :tag "Date")
  1982. (string :tag "Table of Contents"))))
  1983. (defcustom org-export-default-language "en"
  1984. "The default language of HTML export, as a string.
  1985. This should have an association in `org-export-language-setup'."
  1986. :group 'org-export-general
  1987. :type 'string)
  1988. (defcustom org-export-headline-levels 3
  1989. "The last level which is still exported as a headline.
  1990. Inferior levels will produce itemize lists when exported.
  1991. Note that a numeric prefix argument to an exporter function overrides
  1992. this setting.
  1993. This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
  1994. :group 'org-export-general
  1995. :type 'number)
  1996. (defcustom org-export-with-section-numbers t
  1997. "Non-nil means, add section numbers to headlines when exporting.
  1998. This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
  1999. :group 'org-export-general
  2000. :type 'boolean)
  2001. (defcustom org-export-with-toc t
  2002. "Non-nil means, create a table of contents in exported files.
  2003. The TOC contains headlines with levels up to`org-export-headline-levels'.
  2004. Headlines which contain any TODO items will be marked with \"(*)\" in
  2005. ASCII export, and with red color in HTML output.
  2006. In HTML output, the TOC will be clickable.
  2007. This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"."
  2008. :group 'org-export-general
  2009. :type 'boolean)
  2010. (defcustom org-export-mark-todo-in-toc nil
  2011. "Non-nil means, mark TOC lines that contain any open TODO items."
  2012. :group 'org-export-general
  2013. :type 'boolean)
  2014. (defcustom org-export-preserve-breaks nil
  2015. "Non-nil means, preserve all line breaks when exporting.
  2016. Normally, in HTML output paragraphs will be reformatted. In ASCII
  2017. export, line breaks will always be preserved, regardless of this variable.
  2018. This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
  2019. :group 'org-export-general
  2020. :type 'boolean)
  2021. (defcustom org-export-with-archived-trees 'headline
  2022. "Whether subtrees with the ARCHIVE tag should be exported.
  2023. This can have three different values
  2024. nil Do not export, pretend this tree is not present
  2025. t Do export the entire tree
  2026. headline Only export the headline, but skip the tree below it."
  2027. :group 'org-export-general
  2028. :group 'org-archive
  2029. :type '(choice
  2030. (const :tag "not at all" nil)
  2031. (const :tag "headline only" 'headline)
  2032. (const :tag "entirely" t)))
  2033. (defcustom org-export-with-timestamps t
  2034. "If nil, do not export time stamps and associated keywords."
  2035. :group 'org-export-general
  2036. :type 'boolean)
  2037. (defcustom org-export-remove-timestamps-from-toc t
  2038. "If nil, remove timestamps from the table of contents entries."
  2039. :group 'org-export-general
  2040. :type 'boolean)
  2041. (defcustom org-export-with-tags 'not-in-toc
  2042. "If nil, do not export tags, just remove them from headlines.
  2043. If this is the symbol `not-in-toc', tags will be removed from table of
  2044. contents entries, but still be shown in the headlines of the document."
  2045. :group 'org-export-general
  2046. :type '(choice
  2047. (const :tag "Off" nil)
  2048. (const :tag "Not in TOC" not-in-toc)
  2049. (const :tag "On" t)))
  2050. (defgroup org-export-translation nil
  2051. "Options for translating special ascii sequences for the export backends."
  2052. :tag "Org Export Translation"
  2053. :group 'org-export)
  2054. (defcustom org-export-with-emphasize t
  2055. "Non-nil means, interpret *word*, /word/, and _word_ as emphasized text.
  2056. If the export target supports emphasizing text, the word will be
  2057. typeset in bold, italic, or underlined, respectively. Works only for
  2058. single words, but you can say: I *really* *mean* *this*.
  2059. Not all export backends support this.
  2060. This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
  2061. :group 'org-export-translation
  2062. :type 'boolean)
  2063. (defcustom org-export-with-sub-superscripts t
  2064. "Non-nil means, interpret \"_\" and \"^\" for export.
  2065. When this option is turned on, you can use TeX-like syntax for sub- and
  2066. superscripts. Several characters after \"_\" or \"^\" will be
  2067. considered as a single item - so grouping with {} is normally not
  2068. needed. For example, the following things will be parsed as single
  2069. sub- or superscripts.
  2070. 10^24 or 10^tau several digits will be considered 1 item.
  2071. 10^-12 or 10^-tau a leading sign with digits or a word
  2072. x^2-y^3 will be read as x^2 - y^3, because items are
  2073. terminated by almost any nonword/nondigit char.
  2074. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  2075. Still, ambiguity is possible - so when in doubt use {} to enclose the
  2076. sub/superscript.
  2077. Not all export backends support this, but HTML does.
  2078. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  2079. :group 'org-export-translation
  2080. :type 'boolean)
  2081. (defcustom org-export-with-TeX-macros t
  2082. "Non-nil means, interpret simple TeX-like macros when exporting.
  2083. For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
  2084. No only real TeX macros will work here, but the standard HTML entities
  2085. for math can be used as macro names as well. For a list of supported
  2086. names in HTML export, see the constant `org-html-entities'.
  2087. Not all export backends support this.
  2088. This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
  2089. :group 'org-export-translation
  2090. :group 'org-latex
  2091. :type 'boolean)
  2092. (defcustom org-export-with-LaTeX-fragments nil
  2093. "Non-nil means, convert LaTeX fragments to images when exporting to HTML.
  2094. When set, the exporter will find LaTeX environments if the \\begin line is
  2095. the first non-white thing on a line. It will also find the math delimiters
  2096. like $a=b$ and \\( a=b \\) for inline math, $$a=b$$ and \\[ a=b \\] for
  2097. display math.
  2098. This option can also be set with the +OPTIONS line, e.g. \"LaTeX:t\"."
  2099. :group 'org-export-translation
  2100. :group 'org-latex
  2101. :type 'boolean)
  2102. (defcustom org-export-with-fixed-width t
  2103. "Non-nil means, lines starting with \":\" will be in fixed width font.
  2104. This can be used to have pre-formatted text, fragments of code etc. For
  2105. example:
  2106. : ;; Some Lisp examples
  2107. : (while (defc cnt)
  2108. : (ding))
  2109. will be looking just like this in also HTML. See also the QUOTE keyword.
  2110. Not all export backends support this.
  2111. This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
  2112. :group 'org-export-translation
  2113. :type 'boolean)
  2114. (defcustom org-match-sexp-depth 3
  2115. "Number of stacked braces for sub/superscript matching.
  2116. This has to be set before loading org.el to be effective."
  2117. :group 'org-export-translation
  2118. :type 'integer)
  2119. (defgroup org-export-tables nil
  2120. "Options for exporting tables in Org-mode."
  2121. :tag "Org Export Tables"
  2122. :group 'org-export)
  2123. (defcustom org-export-with-tables t
  2124. "If non-nil, lines starting with \"|\" define a table.
  2125. For example:
  2126. | Name | Address | Birthday |
  2127. |-------------+----------+-----------|
  2128. | Arthur Dent | England | 29.2.2100 |
  2129. Not all export backends support this.
  2130. This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
  2131. :group 'org-export-tables
  2132. :type 'boolean)
  2133. (defcustom org-export-highlight-first-table-line t
  2134. "Non-nil means, highlight the first table line.
  2135. In HTML export, this means use <th> instead of <td>.
  2136. In tables created with table.el, this applies to the first table line.
  2137. In Org-mode tables, all lines before the first horizontal separator
  2138. line will be formatted with <th> tags."
  2139. :group 'org-export-tables
  2140. :type 'boolean)
  2141. (defcustom org-export-table-remove-special-lines t
  2142. "Remove special lines and marking characters in calculating tables.
  2143. This removes the special marking character column from tables that are set
  2144. up for spreadsheet calculations. It also removes the entire lines
  2145. marked with `!', `_', or `^'. The lines with `$' are kept, because
  2146. the values of constants may be useful to have."
  2147. :group 'org-export-tables
  2148. :type 'boolean)
  2149. (defcustom org-export-prefer-native-exporter-for-tables nil
  2150. "Non-nil means, always export tables created with table.el natively.
  2151. Natively means, use the HTML code generator in table.el.
  2152. When nil, Org-mode's own HTML generator is used when possible (i.e. if
  2153. the table does not use row- or column-spanning). This has the
  2154. advantage, that the automatic HTML conversions for math symbols and
  2155. sub/superscripts can be applied. Org-mode's HTML generator is also
  2156. much faster."
  2157. :group 'org-export-tables
  2158. :type 'boolean)
  2159. (defgroup org-export-ascii nil
  2160. "Options specific for ASCII export of Org-mode files."
  2161. :tag "Org Export ASCII"
  2162. :group 'org-export)
  2163. (defcustom org-export-ascii-underline '(?\$ ?\# ?^ ?\~ ?\= ?\-)
  2164. "Characters for underlining headings in ASCII export.
  2165. In the given sequence, these characters will be used for level 1, 2, ..."
  2166. :group 'org-export-ascii
  2167. :type '(repeat character))
  2168. (defcustom org-export-ascii-bullets '(?* ?+ ?-)
  2169. "Bullet characters for headlines converted to lists in ASCII export.
  2170. The first character is is used for the first lest level generated in this
  2171. way, and so on. If there are more levels than characters given here,
  2172. the list will be repeated.
  2173. Note that plain lists will keep the same bullets as the have in the
  2174. Org-mode file."
  2175. :group 'org-export-ascii
  2176. :type '(repeat character))
  2177. (defcustom org-export-ascii-show-new-buffer t
  2178. "Non-nil means, popup buffer containing the exported ASCII text.
  2179. Otherwise the buffer will just be saved to a file and stay hidden."
  2180. :group 'org-export-ascii
  2181. :type 'boolean)
  2182. (defgroup org-export-xml nil
  2183. "Options specific for XML export of Org-mode files."
  2184. :tag "Org Export XML"
  2185. :group 'org-export)
  2186. (defgroup org-export-html nil
  2187. "Options specific for HTML export of Org-mode files."
  2188. :tag "Org Export HTML"
  2189. :group 'org-export)
  2190. (defcustom org-export-html-style
  2191. "<style type=\"text/css\">
  2192. html {
  2193. font-family: Times, serif;
  2194. font-size: 12pt;
  2195. }
  2196. .title { text-align: center; }
  2197. .todo { color: red; }
  2198. .done { color: green; }
  2199. .timestamp { color: grey }
  2200. .timestamp-kwd { color: CadetBlue }
  2201. .tag { background-color:lightblue; font-weight:normal }
  2202. .target { background-color: lavender; }
  2203. pre {
  2204. border: 1pt solid #AEBDCC;
  2205. background-color: #F3F5F7;
  2206. padding: 5pt;
  2207. font-family: courier, monospace;
  2208. }
  2209. table { border-collapse: collapse; }
  2210. td, th {
  2211. vertical-align: top;
  2212. border: 1pt solid #ADB9CC;
  2213. }
  2214. </style>"
  2215. "The default style specification for exported HTML files.
  2216. Since there are different ways of setting style information, this variable
  2217. needs to contain the full HTML structure to provide a style, including the
  2218. surrounding HTML tags. The style specifications should include definitions
  2219. for new classes todo, done, title, and deadline. For example, legal values
  2220. would be:
  2221. <style type=\"text/css\">
  2222. p { font-weight: normal; color: gray; }
  2223. h1 { color: black; }
  2224. .title { text-align: center; }
  2225. .todo, .deadline { color: red; }
  2226. .done { color: green; }
  2227. </style>
  2228. or, if you want to keep the style in a file,
  2229. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  2230. As the value of this option simply gets inserted into the HTML <head> header,
  2231. you can \"misuse\" it to add arbitrary text to the header."
  2232. :group 'org-export-html
  2233. :type 'string)
  2234. (defcustom org-export-html-title-format "<h1 class=\"title\">%s</h1>\n"
  2235. "Format for typesetting the document title in HTML export."
  2236. :group 'org-export-html
  2237. :type 'string)
  2238. (defcustom org-export-html-toplevel-hlevel 2
  2239. "The <H> level for level 1 headings in HTML export."
  2240. :group 'org-export-html
  2241. :type 'string)
  2242. (defcustom org-export-html-link-org-files-as-html t
  2243. "Non-nil means, make file links to `file.org' point to `file.html'.
  2244. When org-mode is exporting an org-mode file to HTML, links to
  2245. non-html files are directly put into a href tag in HTML.
  2246. However, links to other Org-mode files (recognized by the
  2247. extension `.org.) should become links to the corresponding html
  2248. file, assuming that the linked org-mode file will also be
  2249. converted to HTML.
  2250. When nil, the links still point to the plain `.org' file."
  2251. :group 'org-export-html
  2252. :type 'boolean)
  2253. (defcustom org-export-html-inline-images 'maybe
  2254. "Non-nil means, inline images into exported HTML pages.
  2255. This is done using an <img> tag. When nil, an anchor with href is used to
  2256. link to the image. If this option is `maybe', then images in links with
  2257. an empty description will be inlined, while images with a description will
  2258. be linked only."
  2259. :group 'org-export-html
  2260. :type '(choice (const :tag "Never" nil)
  2261. (const :tag "Always" t)
  2262. (const :tag "When there is no description" maybe)))
  2263. (defcustom org-export-html-expand t
  2264. "Non-nil means, for HTML export, treat @<...> as HTML tag.
  2265. When nil, these tags will be exported as plain text and therefore
  2266. not be interpreted by a browser.
  2267. This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
  2268. :group 'org-export-html
  2269. :type 'boolean)
  2270. (defcustom org-export-html-table-tag
  2271. "<table border=\"1\" cellspacing=\"0\" cellpadding=\"6\">"
  2272. "The HTML tag used to start a table.
  2273. This must be a <table> tag, but you may change the options like
  2274. borders and spacing."
  2275. :group 'org-export-html
  2276. :type 'string)
  2277. (defcustom org-export-html-with-timestamp nil
  2278. "If non-nil, write `org-export-html-html-helper-timestamp'
  2279. into the exported HTML text. Otherwise, the buffer will just be saved
  2280. to a file."
  2281. :group 'org-export-html
  2282. :type 'boolean)
  2283. (defcustom org-export-html-html-helper-timestamp
  2284. "<br/><br/><hr><p><!-- hhmts start --> <!-- hhmts end --></p>\n"
  2285. "The HTML tag used as timestamp delimiter for HTML-helper-mode."
  2286. :group 'org-export-html
  2287. :type 'string)
  2288. (defcustom org-export-html-show-new-buffer nil
  2289. "Non-nil means, popup buffer containing the exported html text.
  2290. Otherwise, the buffer will just be saved to a file and stay hidden."
  2291. :group 'org-export-html
  2292. :type 'boolean)
  2293. (defgroup org-export-icalendar nil
  2294. "Options specific for iCalendar export of Org-mode files."
  2295. :tag "Org Export iCalendar"
  2296. :group 'org-export)
  2297. (defcustom org-combined-agenda-icalendar-file "~/org.ics"
  2298. "The file name for the iCalendar file covering all agenda files.
  2299. This file is created with the command \\[org-export-icalendar-all-agenda-files].
  2300. The file name should be absolute."
  2301. :group 'org-export-icalendar
  2302. :type 'file)
  2303. (defcustom org-icalendar-include-todo nil
  2304. "Non-nil means, export to iCalendar files should also cover TODO items."
  2305. :group 'org-export-icalendar
  2306. :type 'boolean)
  2307. (defcustom org-icalendar-combined-name "OrgMode"
  2308. "Calendar name for the combined iCalendar representing all agenda files."
  2309. :group 'org-export-icalendar
  2310. :type 'string)
  2311. (defgroup org-font-lock nil
  2312. "Font-lock settings for highlighting in Org-mode."
  2313. :tag "Org Font Lock"
  2314. :group 'org)
  2315. (defcustom org-level-color-stars-only nil
  2316. "Non-nil means fontify only the stars in each headline.
  2317. When nil, the entire headline is fontified.
  2318. Changing it requires restart of `font-lock-mode' to become effective
  2319. also in regions already fontified."
  2320. :group 'org-font-lock
  2321. :type 'boolean)
  2322. (defcustom org-hide-leading-stars nil
  2323. "Non-nil means, hide the first N-1 stars in a headline.
  2324. This works by using the face `org-hide' for these stars. This
  2325. face is white for a light background, and black for a dark
  2326. background. You may have to customize the face `org-hide' to
  2327. make this work.
  2328. Changing it requires restart of `font-lock-mode' to become effective
  2329. also in regions already fontified.
  2330. You may also set this on a per-file basis by adding one of the following
  2331. lines to the buffer:
  2332. #+STARTUP: hidestars
  2333. #+STARTUP: showstars"
  2334. :group 'org-font-lock
  2335. :type 'boolean)
  2336. (defcustom org-fontify-done-headline nil
  2337. "Non-nil means, change the face of a headline if it is marked DONE.
  2338. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2339. When this is non-nil, the headline after the keyword is set to the
  2340. `org-headline-done' as an additional indication."
  2341. :group 'org-font-lock
  2342. :type 'boolean)
  2343. (defcustom org-fontify-emphasized-text t
  2344. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2345. Changing this variable requires a restart of Emacs to take effect."
  2346. :group 'org-font-lock
  2347. :type 'boolean)
  2348. (defvar org-emph-re nil
  2349. "Regular expression for matching emphasis.")
  2350. (defvar org-emphasis-regexp-components) ; defined just below
  2351. (defvar org-emphasis-alist) ; defined just below
  2352. (defun org-set-emph-re (var val)
  2353. "Set variable and compute the emphasis regular expression."
  2354. (set var val)
  2355. (when (and (boundp 'org-emphasis-alist)
  2356. (boundp 'org-emphasis-regexp-components)
  2357. org-emphasis-alist org-emphasis-regexp-components)
  2358. (let* ((e org-emphasis-regexp-components)
  2359. (pre (car e))
  2360. (post (nth 1 e))
  2361. (border (nth 2 e))
  2362. (body (nth 3 e))
  2363. (nl (nth 4 e))
  2364. (stacked (nth 5 e))
  2365. (body1 (concat body "*?"))
  2366. (markers (mapconcat 'car org-emphasis-alist "")))
  2367. ;; make sure special characters appear at the right position in the class
  2368. (if (string-match "\\^" markers)
  2369. (setq markers (concat (replace-match "" t t markers) "^")))
  2370. (if (string-match "-" markers)
  2371. (setq markers (concat (replace-match "" t t markers) "-")))
  2372. ; (while (>= (setq nl (1- nl)) 0) (setq body1 (concat body1 "\n?" body "*?")))
  2373. ; (while (>= (setq nl (1- nl)) 0) (setq body1 (concat body1 "\\(?:\n?" body "*?\\)?")))
  2374. (if (> nl 0)
  2375. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2376. (int-to-string nl) "\\}")))
  2377. ;; Make the regexp
  2378. (setq org-emph-re
  2379. (concat "\\([" pre (if stacked markers) "]\\|^\\)"
  2380. "\\("
  2381. "\\([" markers "]\\)"
  2382. "\\("
  2383. "[^" border markers "]"
  2384. body1
  2385. "[^" border markers "]"
  2386. "\\)"
  2387. "\\3\\)"
  2388. "\\([" post (if stacked markers) "]\\|$\\)")))))
  2389. (defcustom org-emphasis-regexp-components
  2390. '(" \t('\"" " \t.,?;'\")" " \t\r\n," "." 1 nil)
  2391. "Components used to build the reqular expression for emphasis.
  2392. This is a list with 6 entries. Terminology: In an emphasis string
  2393. like \" *strong word* \", we call the initial space PREMATCH, the final
  2394. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2395. and \"trong wor\" is the body. The different components in this variable
  2396. specify what is allowed/forbidden in each part:
  2397. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2398. post Chars allowed as postmatch. End of line will be allowed too.
  2399. border The chars *forbidden* as border characters. In addition to the
  2400. characters given here, all marker characters are forbidden too.
  2401. body-regexp A regexp like \".\" to match a body character. Don't use
  2402. non-shy groups here, and don't allow newline here.
  2403. newline The maximum number of newlines allowed in an emphasis exp.
  2404. stacked Non-nil means, allow stacked styles. This works only in HTML
  2405. export. When this is set, all marker characters (as given in
  2406. `org-emphasis-alist') will be allowed as pre/post, aiding
  2407. inside-out matching.
  2408. Use customize to modify this, or restart Emacs after changing it."
  2409. :group 'org-font-lock
  2410. :set 'org-set-emph-re
  2411. :type '(list
  2412. (sexp :tag "Allowed chars in pre ")
  2413. (sexp :tag "Allowed chars in post ")
  2414. (sexp :tag "Forbidden chars in border ")
  2415. (sexp :tag "Regexp for body ")
  2416. (integer :tag "number of newlines allowed")
  2417. (boolean :tag "Stacking allowed ")))
  2418. (defcustom org-emphasis-alist
  2419. '(("*" bold "<b>" "</b>")
  2420. ("/" italic "<i>" "</i>")
  2421. ("_" underline "<u>" "</u>")
  2422. ("=" shadow "<code>" "</code>")
  2423. ("+" (:strike-through t) "<del>" "</del>")
  2424. )
  2425. "Special syntax for emphasized text.
  2426. Text starting and ending with a special character will be emphasized, for
  2427. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2428. characters, the face to bbe used by font-lock for highlighting in Org-mode
  2429. Emacs buffers, and the HTML tags to be used for this.
  2430. Use customize to modify this, or restart Emacs after changing it."
  2431. :group 'org-font-lock
  2432. :set 'org-set-emph-re
  2433. :type '(repeat
  2434. (list
  2435. (string :tag "Marker character")
  2436. (choice
  2437. (face :tag "Font-lock-face")
  2438. (plist :tag "Face property list"))
  2439. (string :tag "HTML start tag")
  2440. (string :tag "HTML end tag"))))
  2441. ;;; The faces
  2442. (defgroup org-faces nil
  2443. "Faces in Org-mode."
  2444. :tag "Org Faces"
  2445. :group 'org-font-lock)
  2446. (defun org-compatible-face (specs)
  2447. "Make a compatible face specification.
  2448. XEmacs and Emacs 21 do not know about the `min-colors' attribute.
  2449. For them we convert a (min-colors 8) entry to a `tty' entry and move it
  2450. to the top of the list. The `min-colors' attribute will be removed from
  2451. any other entries, and any resulting duplicates will be removed entirely."
  2452. (if (or (featurep 'xemacs) (< emacs-major-version 22))
  2453. (let (r e a)
  2454. (while (setq e (pop specs))
  2455. (cond
  2456. ((memq (car e) '(t default)) (push e r))
  2457. ((setq a (member '(min-colors 8) (car e)))
  2458. (nconc r (list (cons (cons '(type tty) (delq (car a) (car e)))
  2459. (cdr e)))))
  2460. ((setq a (assq 'min-colors (car e)))
  2461. (setq e (cons (delq a (car e)) (cdr e)))
  2462. (or (assoc (car e) r) (push e r)))
  2463. (t (or (assoc (car e) r) (push e r)))))
  2464. (nreverse r))
  2465. specs))
  2466. (defface org-hide
  2467. '((((background light)) (:foreground "white"))
  2468. (((background dark)) (:foreground "black")))
  2469. "Face used to hide leading stars in headlines.
  2470. The forground color of this face should be equal to the background
  2471. color of the frame."
  2472. :group 'org-faces)
  2473. (defface org-level-1 ;; font-lock-function-name-face
  2474. (org-compatible-face
  2475. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2476. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2477. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2478. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2479. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  2480. (t (:bold t))))
  2481. "Face used for level 1 headlines."
  2482. :group 'org-faces)
  2483. (defface org-level-2 ;; font-lock-variable-name-face
  2484. (org-compatible-face
  2485. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  2486. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  2487. (((class color) (min-colors 8) (background light)) (:foreground "yellow"))
  2488. (((class color) (min-colors 8) (background dark)) (:foreground "yellow" :bold t))
  2489. (t (:bold t))))
  2490. "Face used for level 2 headlines."
  2491. :group 'org-faces)
  2492. (defface org-level-3 ;; font-lock-keyword-face
  2493. (org-compatible-face
  2494. '((((class color) (min-colors 88) (background light)) (:foreground "Purple"))
  2495. (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
  2496. (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
  2497. (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
  2498. (((class color) (min-colors 8) (background light)) (:foreground "purple" :bold t))
  2499. (((class color) (min-colors 8) (background dark)) (:foreground "cyan" :bold t))
  2500. (t (:bold t))))
  2501. "Face used for level 3 headlines."
  2502. :group 'org-faces)
  2503. (defface org-level-4 ;; font-lock-comment-face
  2504. (org-compatible-face
  2505. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2506. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2507. (((class color) (min-colors 16) (background light)) (:foreground "red"))
  2508. (((class color) (min-colors 16) (background dark)) (:foreground "red1"))
  2509. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2510. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2511. (t (:bold t))))
  2512. "Face used for level 4 headlines."
  2513. :group 'org-faces)
  2514. (defface org-level-5 ;; font-lock-type-face
  2515. (org-compatible-face
  2516. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
  2517. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
  2518. (((class color) (min-colors 8)) (:foreground "green"))))
  2519. "Face used for level 5 headlines."
  2520. :group 'org-faces)
  2521. (defface org-level-6 ;; font-lock-constant-face
  2522. (org-compatible-face
  2523. '((((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
  2524. (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
  2525. (((class color) (min-colors 8)) (:foreground "magenta"))))
  2526. "Face used for level 6 headlines."
  2527. :group 'org-faces)
  2528. (defface org-level-7 ;; font-lock-builtin-face
  2529. (org-compatible-face
  2530. '((((class color) (min-colors 16) (background light)) (:foreground "Orchid"))
  2531. (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue"))
  2532. (((class color) (min-colors 8)) (:foreground "blue"))))
  2533. "Face used for level 7 headlines."
  2534. :group 'org-faces)
  2535. (defface org-level-8 ;; font-lock-string-face
  2536. (org-compatible-face
  2537. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2538. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2539. (((class color) (min-colors 8)) (:foreground "green"))))
  2540. "Face used for level 8 headlines."
  2541. :group 'org-faces)
  2542. (defface org-special-keyword ;; font-lock-string-face
  2543. (org-compatible-face
  2544. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2545. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2546. (t (:italic t))))
  2547. "Face used for special keywords."
  2548. :group 'org-faces)
  2549. (defface org-warning ;; font-lock-warning-face
  2550. (org-compatible-face
  2551. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  2552. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  2553. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2554. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2555. (t (:bold t))))
  2556. "Face for deadlines and TODO keywords."
  2557. :group 'org-faces)
  2558. (defface org-headline-done ;; font-lock-string-face
  2559. (org-compatible-face
  2560. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2561. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2562. (((class color) (min-colors 8) (background light)) (:bold nil))))
  2563. "Face used to indicate that a headline is DONE.
  2564. This face is only used if `org-fontify-done-headline' is set."
  2565. :group 'org-faces)
  2566. (defface org-archived ; similar to shadow
  2567. (org-compatible-face
  2568. '((((class color grayscale) (min-colors 88) (background light))
  2569. (:foreground "grey50"))
  2570. (((class color grayscale) (min-colors 88) (background dark))
  2571. (:foreground "grey70"))
  2572. (((class color) (min-colors 8) (background light))
  2573. (:foreground "green"))
  2574. (((class color) (min-colors 8) (background dark))
  2575. (:foreground "yellow"))))
  2576. "Face for headline with the ARCHIVE tag."
  2577. :group 'org-faces)
  2578. (defface org-link
  2579. '((((class color) (background light)) (:foreground "Purple" :underline t))
  2580. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  2581. (t (:underline t)))
  2582. "Face for links."
  2583. :group 'org-faces)
  2584. (defface org-date
  2585. '((((class color) (background light)) (:foreground "Purple" :underline t))
  2586. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  2587. (t (:underline t)))
  2588. "Face for links."
  2589. :group 'org-faces)
  2590. (defface org-tag
  2591. '((t (:bold t)))
  2592. "Face for tags."
  2593. :group 'org-faces)
  2594. (defface org-todo ;; font-lock-warning-face
  2595. (org-compatible-face
  2596. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  2597. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  2598. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2599. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2600. (t (:inverse-video t :bold t))))
  2601. "Face for TODO keywords."
  2602. :group 'org-faces)
  2603. (defface org-done ;; font-lock-type-face
  2604. (org-compatible-face
  2605. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen" :bold t))
  2606. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen" :bold t))
  2607. (((class color) (min-colors 8)) (:foreground "green"))
  2608. (t (:bold t))))
  2609. "Face used for DONE."
  2610. :group 'org-faces)
  2611. (defface org-table ;; font-lock-function-name-face
  2612. (org-compatible-face
  2613. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2614. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2615. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2616. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2617. (((class color) (min-colors 8) (background light)) (:foreground "blue"))
  2618. (((class color) (min-colors 8) (background dark)))))
  2619. "Face used for tables."
  2620. :group 'org-faces)
  2621. (defface org-formula
  2622. (org-compatible-face
  2623. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2624. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2625. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2626. (((class color) (min-colors 8) (background dark)) (:foreground "red"))
  2627. (t (:bold t :italic t))))
  2628. "Face for formulas."
  2629. :group 'org-faces)
  2630. (defface org-scheduled-today
  2631. (org-compatible-face
  2632. '((((class color) (min-colors 88) (background light)) (:foreground "DarkGreen"))
  2633. (((class color) (min-colors 88) (background dark)) (:foreground "PaleGreen"))
  2634. (((class color) (min-colors 8)) (:foreground "green"))
  2635. (t (:bold t :italic t))))
  2636. "Face for items scheduled for a certain day."
  2637. :group 'org-faces)
  2638. (defface org-scheduled-previously
  2639. (org-compatible-face
  2640. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2641. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2642. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2643. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2644. (t (:bold t))))
  2645. "Face for items scheduled previously, and not yet done."
  2646. :group 'org-faces)
  2647. (defface org-upcoming-deadline
  2648. (org-compatible-face
  2649. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2650. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2651. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2652. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2653. (t (:bold t))))
  2654. "Face for items scheduled previously, and not yet done."
  2655. :group 'org-faces)
  2656. (defface org-time-grid ;; font-lock-variable-name-face
  2657. (org-compatible-face
  2658. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  2659. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  2660. (((class color) (min-colors 8)) (:foreground "yellow" :weight light))))
  2661. "Face used for time grids."
  2662. :group 'org-faces)
  2663. (defconst org-level-faces
  2664. '(org-level-1 org-level-2 org-level-3 org-level-4
  2665. org-level-5 org-level-6 org-level-7 org-level-8
  2666. ))
  2667. (defconst org-n-levels (length org-level-faces))
  2668. ;;; Variables for pre-computed regular expressions, all buffer local
  2669. (defvar org-done-string nil
  2670. "The last string in `org-todo-keywords', indicating an item is DONE.")
  2671. (make-variable-buffer-local 'org-done-string)
  2672. (defvar org-todo-regexp nil
  2673. "Matches any of the TODO state keywords.")
  2674. (make-variable-buffer-local 'org-todo-regexp)
  2675. (defvar org-not-done-regexp nil
  2676. "Matches any of the TODO state keywords except the last one.")
  2677. (make-variable-buffer-local 'org-not-done-regexp)
  2678. (defvar org-todo-line-regexp nil
  2679. "Matches a headline and puts TODO state into group 2 if present.")
  2680. (make-variable-buffer-local 'org-todo-line-regexp)
  2681. (defvar org-todo-line-tags-regexp nil
  2682. "Matches a headline and puts TODO state into group 2 if present.
  2683. Also put tags into group 4 if tags are present.")
  2684. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2685. (defvar org-nl-done-regexp nil
  2686. "Matches newline followed by a headline with the DONE keyword.")
  2687. (make-variable-buffer-local 'org-nl-done-regexp)
  2688. (defvar org-looking-at-done-regexp nil
  2689. "Matches the DONE keyword a point.")
  2690. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2691. (defvar org-todo-kwd-priority-p nil
  2692. "Do TODO items have priorities?")
  2693. (make-variable-buffer-local 'org-todo-kwd-priority-p)
  2694. (defvar org-todo-kwd-max-priority nil
  2695. "Maximum priority of TODO items.")
  2696. (make-variable-buffer-local 'org-todo-kwd-max-priority)
  2697. (defvar org-ds-keyword-length 12
  2698. "Maximum length of the Deadline and SCHEDULED keywords.")
  2699. (make-variable-buffer-local 'org-ds-keyword-length)
  2700. (defvar org-deadline-regexp nil
  2701. "Matches the DEADLINE keyword.")
  2702. (make-variable-buffer-local 'org-deadline-regexp)
  2703. (defvar org-deadline-time-regexp nil
  2704. "Matches the DEADLINE keyword together with a time stamp.")
  2705. (make-variable-buffer-local 'org-deadline-time-regexp)
  2706. (defvar org-deadline-line-regexp nil
  2707. "Matches the DEADLINE keyword and the rest of the line.")
  2708. (make-variable-buffer-local 'org-deadline-line-regexp)
  2709. (defvar org-scheduled-regexp nil
  2710. "Matches the SCHEDULED keyword.")
  2711. (make-variable-buffer-local 'org-scheduled-regexp)
  2712. (defvar org-scheduled-time-regexp nil
  2713. "Matches the SCHEDULED keyword together with a time stamp.")
  2714. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2715. (defvar org-closed-time-regexp nil
  2716. "Matches the CLOSED keyword together with a time stamp.")
  2717. (make-variable-buffer-local 'org-closed-time-regexp)
  2718. (defvar org-keyword-time-regexp nil
  2719. "Matches any of the 4 keywords, together with the time stamp.")
  2720. (make-variable-buffer-local 'org-keyword-time-regexp)
  2721. (defvar org-keyword-time-not-clock-regexp nil
  2722. "Matches any of the 3 keywords, together with the time stamp.")
  2723. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2724. (defvar org-maybe-keyword-time-regexp nil
  2725. "Matches a timestamp, possibly preceeded by a keyword.")
  2726. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2727. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  2728. rear-nonsticky t mouse-map t fontified t)
  2729. "Properties to remove when a string without properties is wanted.")
  2730. (defsubst org-match-string-no-properties (num &optional string)
  2731. (if (featurep 'xemacs)
  2732. (let ((s (match-string num string)))
  2733. (remove-text-properties 0 (length s) org-rm-props s)
  2734. s)
  2735. (match-string-no-properties num string)))
  2736. (defsubst org-no-properties (s)
  2737. (remove-text-properties 0 (length s) org-rm-props s)
  2738. s)
  2739. (defsubst org-get-alist-option (option key)
  2740. (cond ((eq key t) t)
  2741. ((eq option t) t)
  2742. ((assoc key option) (cdr (assoc key option)))
  2743. (t (cdr (assq 'default option)))))
  2744. (defsubst org-set-local (var value)
  2745. "Make VAR local in current buffer and set it to VALUE."
  2746. (set (make-variable-buffer-local var) value))
  2747. (defsubst org-mode-p ()
  2748. "Check if the current buffer is in Org-mode."
  2749. (eq major-mode 'org-mode))
  2750. (defsubst org-last (list)
  2751. "Return the last element of LIST."
  2752. (car (last list)))
  2753. (defun org-let (list &rest body)
  2754. (eval (cons 'let (cons list body))))
  2755. (put 'org-let 'lisp-indent-function 1)
  2756. (defun org-let2 (list1 list2 &rest body)
  2757. (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
  2758. (put 'org-let2 'lisp-indent-function 2)
  2759. ;; FIXME: document this, in particular the push argument.
  2760. (defconst org-startup-options
  2761. '(("fold" org-startup-folded t)
  2762. ("overview" org-startup-folded t)
  2763. ("nofold" org-startup-folded nil)
  2764. ("showall" org-startup-folded nil)
  2765. ("content" org-startup-folded content)
  2766. ("hidestars" org-hide-leading-stars t)
  2767. ("showstars" org-hide-leading-stars nil)
  2768. ("odd" org-odd-levels-only t)
  2769. ("oddeven" org-odd-levels-only nil)
  2770. ("align" org-startup-align-all-tables t)
  2771. ("noalign" org-startup-align-all-tables nil)
  2772. ("customtime" org-display-custom-times t)
  2773. ("logging" org-log-done t)
  2774. ("logdone" org-log-done t)
  2775. ("nologging" org-log-done nil)
  2776. ("lognotedone" org-log-done done push)
  2777. ("lognotestate" org-log-done state push)
  2778. ("lognoteclock-out" org-log-done clock-out push)
  2779. ("dlcheck" org-startup-with-deadline-check t)
  2780. ("nodlcheck" org-startup-with-deadline-check nil)))
  2781. (defun org-set-regexps-and-options ()
  2782. "Precompute regular expressions for current buffer."
  2783. (when (org-mode-p)
  2784. (let ((re (org-make-options-regexp
  2785. '("CATEGORY" "SEQ_TODO" "PRI_TODO" "TYP_TODO"
  2786. "STARTUP" "ARCHIVE" "TAGS" "LINK")))
  2787. (splitre "[ \t]+")
  2788. kwds int key value cat arch tags links)
  2789. (save-excursion
  2790. (save-restriction
  2791. (widen)
  2792. (goto-char (point-min))
  2793. (while (re-search-forward re nil t)
  2794. (setq key (match-string 1) value (org-match-string-no-properties 2))
  2795. (cond
  2796. ((equal key "CATEGORY")
  2797. (if (string-match "[ \t]+$" value)
  2798. (setq value (replace-match "" t t value)))
  2799. (setq cat (intern value)))
  2800. ((equal key "SEQ_TODO")
  2801. (setq int 'sequence
  2802. kwds (append kwds (org-split-string value splitre))))
  2803. ((equal key "PRI_TODO")
  2804. (setq int 'priority
  2805. kwds (append kwds (org-split-string value splitre))))
  2806. ((equal key "TYP_TODO")
  2807. (setq int 'type
  2808. kwds (append kwds (org-split-string value splitre))))
  2809. ((equal key "TAGS")
  2810. (setq tags (append tags (org-split-string value splitre))))
  2811. ((equal key "LINK")
  2812. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2813. (push (cons (match-string 1 value)
  2814. (org-trim (match-string 2 value)))
  2815. links)))
  2816. ((equal key "STARTUP")
  2817. (let ((opts (org-split-string value splitre))
  2818. l var val)
  2819. (while (setq l (assoc (pop opts) org-startup-options))
  2820. (setq var (nth 1 l) val (nth 2 l))
  2821. (if (not (nth 3 l))
  2822. (set (make-local-variable var) val)
  2823. (if (not (listp (symbol-value var)))
  2824. (set (make-local-variable var) nil))
  2825. (set (make-local-variable var) (symbol-value var))
  2826. (add-to-list var val)))))
  2827. ((equal key "ARCHIVE")
  2828. (string-match " *$" value)
  2829. (setq arch (replace-match "" t t value))
  2830. (remove-text-properties 0 (length arch)
  2831. '(face t fontified t) arch)))
  2832. )))
  2833. (and cat (org-set-local 'org-category cat))
  2834. (and kwds (org-set-local 'org-todo-keywords kwds))
  2835. (and arch (org-set-local 'org-archive-location arch))
  2836. (and int (org-set-local 'org-todo-interpretation int))
  2837. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2838. (when tags
  2839. (let (e tgs)
  2840. (while (setq e (pop tags))
  2841. (cond
  2842. ((equal e "{") (push '(:startgroup) tgs))
  2843. ((equal e "}") (push '(:endgroup) tgs))
  2844. ((string-match "^\\([0-9a-zA-Z_@]+\\)(\\(.\\))$" e)
  2845. (push (cons (match-string 1 e)
  2846. (string-to-char (match-string 2 e)))
  2847. tgs))
  2848. (t (push (list e) tgs))))
  2849. (org-set-local 'org-tag-alist nil)
  2850. (while (setq e (pop tgs))
  2851. (or (and (stringp (car e))
  2852. (assoc (car e) org-tag-alist))
  2853. (push e org-tag-alist))))))
  2854. ;; Compute the regular expressions and other local variables
  2855. (setq org-todo-kwd-priority-p (equal org-todo-interpretation 'priority)
  2856. org-todo-kwd-max-priority (1- (length org-todo-keywords))
  2857. org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2858. (length org-scheduled-string)))
  2859. org-done-string
  2860. (nth (1- (length org-todo-keywords)) org-todo-keywords)
  2861. org-todo-regexp
  2862. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords
  2863. "\\|") "\\)\\>")
  2864. org-not-done-regexp
  2865. (concat "\\<\\("
  2866. (mapconcat 'regexp-quote
  2867. (nreverse (cdr (reverse org-todo-keywords)))
  2868. "\\|")
  2869. "\\)\\>")
  2870. org-todo-line-regexp
  2871. (concat "^\\(\\*+\\)[ \t]*\\(?:\\("
  2872. (mapconcat 'regexp-quote org-todo-keywords "\\|")
  2873. "\\)\\>\\)? *\\(.*\\)")
  2874. org-nl-done-regexp
  2875. (concat "[\r\n]\\*+[ \t]+" org-done-string "\\>")
  2876. org-todo-line-tags-regexp
  2877. (concat "^\\(\\*+\\)[ \t]*\\(?:\\("
  2878. (mapconcat 'regexp-quote org-todo-keywords "\\|")
  2879. "\\)\\>\\)? *\\(.*?\\([ \t]:[a-zA-Z0-9:_@]+:[ \t]*\\)?$\\)")
  2880. org-looking-at-done-regexp (concat "^" org-done-string "\\>")
  2881. org-deadline-regexp (concat "\\<" org-deadline-string)
  2882. org-deadline-time-regexp
  2883. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2884. org-deadline-line-regexp
  2885. (concat "\\<\\(" org-deadline-string "\\).*")
  2886. org-scheduled-regexp
  2887. (concat "\\<" org-scheduled-string)
  2888. org-scheduled-time-regexp
  2889. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2890. org-closed-time-regexp
  2891. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2892. org-keyword-time-regexp
  2893. (concat "\\<\\(" org-scheduled-string
  2894. "\\|" org-deadline-string
  2895. "\\|" org-closed-string
  2896. "\\|" org-clock-string "\\)"
  2897. " *[[<]\\([^]>]+\\)[]>]")
  2898. org-keyword-time-not-clock-regexp
  2899. (concat "\\<\\(" org-scheduled-string
  2900. "\\|" org-deadline-string
  2901. "\\|" org-closed-string "\\)"
  2902. " *[[<]\\([^]>]+\\)[]>]")
  2903. org-maybe-keyword-time-regexp
  2904. (concat "\\(\\<\\(" org-scheduled-string
  2905. "\\|" org-deadline-string
  2906. "\\|" org-closed-string
  2907. "\\|" org-clock-string "\\)\\)?"
  2908. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}[^]\r\n>]*?[]>]\\)"))
  2909. (org-set-font-lock-defaults)))
  2910. ;;; Some variables ujsed in various places
  2911. (defvar org-window-configuration nil
  2912. "Used in various places to store a window configuration.")
  2913. (defvar org-finish-function nil
  2914. "Function to be called when `C-c C-c' is used.
  2915. This is for getting out of special buffers like remember.")
  2916. ;;; Foreign variables, to inform the compiler
  2917. ;; XEmacs only
  2918. (defvar outline-mode-menu-heading)
  2919. (defvar outline-mode-menu-show)
  2920. (defvar outline-mode-menu-hide)
  2921. (defvar zmacs-regions) ; XEmacs regions
  2922. ;; Emacs only
  2923. (defvar mark-active)
  2924. ;; Packages that org-mode interacts with
  2925. (defvar calc-embedded-close-formula)
  2926. (defvar calc-embedded-open-formula)
  2927. (defvar font-lock-unfontify-region-function)
  2928. (defvar org-goto-start-pos)
  2929. (defvar vm-message-pointer)
  2930. (defvar vm-folder-directory)
  2931. (defvar wl-summary-buffer-elmo-folder)
  2932. (defvar wl-summary-buffer-folder-name)
  2933. (defvar gnus-other-frame-object)
  2934. (defvar gnus-group-name)
  2935. (defvar gnus-article-current)
  2936. (defvar w3m-current-url)
  2937. (defvar w3m-current-title)
  2938. (defvar mh-progs)
  2939. (defvar mh-current-folder)
  2940. (defvar mh-show-folder-buffer)
  2941. (defvar mh-index-folder)
  2942. (defvar mh-searcher)
  2943. (defvar calendar-mode-map)
  2944. (defvar Info-current-file)
  2945. (defvar Info-current-node)
  2946. (defvar texmathp-why)
  2947. (defvar remember-save-after-remembering)
  2948. (defvar remember-data-file)
  2949. (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
  2950. (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
  2951. (defvar org-latex-regexps)
  2952. (defvar original-date) ; dynamically scoped in calendar.el does scope this
  2953. ;; FIXME: Occasionally check by commenting these, to make sure
  2954. ;; no other functions uses these, forgetting to let-bind them.
  2955. (defvar entry)
  2956. (defvar state)
  2957. (defvar last-state)
  2958. (defvar date)
  2959. (defvar description)
  2960. ;; Defined somewhere in this file, but used before definition.
  2961. (defvar orgtbl-mode-menu) ; defined when orgtbl mode get initialized
  2962. (defvar org-agenda-undo-list)
  2963. (defvar org-agenda-pending-undo-list)
  2964. (defvar org-agenda-overriding-header)
  2965. (defvar orgtbl-mode)
  2966. (defvar org-html-entities)
  2967. (defvar org-struct-menu)
  2968. (defvar org-org-menu)
  2969. (defvar org-tbl-menu)
  2970. (defvar org-agenda-keymap)
  2971. (defvar org-category-table)
  2972. ;;;; Emacs/XEmacs compatibility
  2973. ;; Overlay compatibility functions
  2974. (defun org-make-overlay (beg end &optional buffer)
  2975. (if (featurep 'xemacs)
  2976. (make-extent beg end buffer)
  2977. (make-overlay beg end buffer)))
  2978. (defun org-delete-overlay (ovl)
  2979. (if (featurep 'xemacs) (delete-extent ovl) (delete-overlay ovl)))
  2980. (defun org-detach-overlay (ovl)
  2981. (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
  2982. (defun org-move-overlay (ovl beg end &optional buffer)
  2983. (if (featurep 'xemacs)
  2984. (set-extent-endpoints ovl beg end (or buffer (current-buffer)))
  2985. (move-overlay ovl beg end buffer)))
  2986. (defun org-overlay-put (ovl prop value)
  2987. (if (featurep 'xemacs)
  2988. (set-extent-property ovl prop value)
  2989. (overlay-put ovl prop value)))
  2990. (defun org-overlay-display (ovl text &optional face)
  2991. "Make overlay OVL display TEXT with face FACE."
  2992. (if (featurep 'xemacs)
  2993. (let ((gl (make-glyph text)))
  2994. (and face (set-glyph-face gl face))
  2995. (set-extent-property ovl 'invisible t)
  2996. (set-extent-property ovl 'end-glyph gl))
  2997. (overlay-put ovl 'display text)
  2998. (if face (overlay-put ovl 'face face))))
  2999. (defun org-overlay-get (ovl prop)
  3000. (if (featurep 'xemacs)
  3001. (extent-property ovl prop)
  3002. (overlay-get ovl prop)))
  3003. (defun org-overlays-at (pos)
  3004. (if (featurep 'xemacs) (extents-at pos) (overlays-at pos)))
  3005. (defun org-overlays-in (&optional start end)
  3006. (if (featurep 'xemacs)
  3007. (extent-list nil start end)
  3008. (overlays-in start end)))
  3009. (defun org-overlay-start (o)
  3010. (if (featurep 'xemacs) (extent-start-position o) (overlay-start o)))
  3011. (defun org-overlay-end (o)
  3012. (if (featurep 'xemacs) (extent-end-position o) (overlay-end o)))
  3013. (defun org-find-overlays (prop &optional pos delete)
  3014. "Find all overlays specifying PROP at POS or point.
  3015. If DELETE is non-nil, delete all those overlays."
  3016. (let ((overlays (org-overlays-at (or pos (point))))
  3017. ov found)
  3018. (while (setq ov (pop overlays))
  3019. (if (org-overlay-get ov prop)
  3020. (if delete (org-delete-overlay ov) (push ov found))))
  3021. found))
  3022. ;; Region compatibility
  3023. (defun org-add-hook (hook function &optional append local)
  3024. "Add-hook, compatible with both Emacsen."
  3025. (if (and local (featurep 'xemacs))
  3026. (add-local-hook hook function append)
  3027. (add-hook hook function append local)))
  3028. (defvar org-ignore-region nil
  3029. "To temporarily disable the active region.")
  3030. (defun org-region-active-p ()
  3031. "Is `transient-mark-mode' on and the region active?
  3032. Works on both Emacs and XEmacs."
  3033. (if org-ignore-region
  3034. nil
  3035. (if (featurep 'xemacs)
  3036. (and zmacs-regions (region-active-p))
  3037. (and transient-mark-mode mark-active))))
  3038. ;; Invisibility compatibility
  3039. (defun org-add-to-invisibility-spec (arg)
  3040. "Add elements to `buffer-invisibility-spec'.
  3041. See documentation for `buffer-invisibility-spec' for the kind of elements
  3042. that can be added."
  3043. (cond
  3044. ((fboundp 'add-to-invisibility-spec)
  3045. (add-to-invisibility-spec arg))
  3046. ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
  3047. (setq buffer-invisibility-spec (list arg)))
  3048. (t
  3049. (setq buffer-invisibility-spec
  3050. (cons arg buffer-invisibility-spec)))))
  3051. (defun org-remove-from-invisibility-spec (arg)
  3052. "Remove elements from `buffer-invisibility-spec'."
  3053. (if (fboundp 'remove-from-invisibility-spec)
  3054. (remove-from-invisibility-spec arg)
  3055. (if (consp buffer-invisibility-spec)
  3056. (setq buffer-invisibility-spec
  3057. (delete arg buffer-invisibility-spec)))))
  3058. (defun org-in-invisibility-spec-p (arg)
  3059. "Is ARG a member of `buffer-invisibility-spec'?"
  3060. (if (consp buffer-invisibility-spec)
  3061. (member arg buffer-invisibility-spec)
  3062. nil))
  3063. ;;;; Define the Org-mode
  3064. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3065. (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."))
  3066. ;; We use a before-change function to check if a table might need
  3067. ;; an update.
  3068. (defvar org-table-may-need-update t
  3069. "Indicates that a table might need an update.
  3070. This variable is set by `org-before-change-function'.
  3071. `org-table-align' sets it back to nil.")
  3072. (defvar org-mode-map)
  3073. (defvar org-mode-hook nil)
  3074. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3075. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3076. ;;;###autoload
  3077. (define-derived-mode org-mode outline-mode "Org"
  3078. "Outline-based notes management and organizer, alias
  3079. \"Carsten's outline-mode for keeping track of everything.\"
  3080. Org-mode develops organizational tasks around a NOTES file which
  3081. contains information about projects as plain text. Org-mode is
  3082. implemented on top of outline-mode, which is ideal to keep the content
  3083. of large files well structured. It supports ToDo items, deadlines and
  3084. time stamps, which magically appear in the diary listing of the Emacs
  3085. calendar. Tables are easily created with a built-in table editor.
  3086. Plain text URL-like links connect to websites, emails (VM), Usenet
  3087. messages (Gnus), BBDB entries, and any files related to the project.
  3088. For printing and sharing of notes, an Org-mode file (or a part of it)
  3089. can be exported as a structured ASCII or HTML file.
  3090. The following commands are available:
  3091. \\{org-mode-map}"
  3092. ;; Get rid of Outline menus, they are not needed
  3093. ;; Need to do this here because define-derived-mode sets up
  3094. ;; the keymap so late. Still, it is a waste to call this each time
  3095. ;; we switch another buffer into org-mode.
  3096. (if (featurep 'xemacs)
  3097. (when (boundp 'outline-mode-menu-heading)
  3098. ;; Assume this is Greg's port, it used easymenu
  3099. (easy-menu-remove outline-mode-menu-heading)
  3100. (easy-menu-remove outline-mode-menu-show)
  3101. (easy-menu-remove outline-mode-menu-hide))
  3102. (define-key org-mode-map [menu-bar headings] 'undefined)
  3103. (define-key org-mode-map [menu-bar hide] 'undefined)
  3104. (define-key org-mode-map [menu-bar show] 'undefined))
  3105. (easy-menu-add org-org-menu)
  3106. (easy-menu-add org-tbl-menu)
  3107. (org-install-agenda-files-menu)
  3108. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3109. (org-add-to-invisibility-spec '(org-cwidth))
  3110. (when (featurep 'xemacs)
  3111. (org-set-local 'line-move-ignore-invisible t))
  3112. (setq outline-regexp "\\*+")
  3113. ;;(setq outline-regexp "\\(?:\\*+\\|[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\) \\)")
  3114. (setq outline-level 'org-outline-level)
  3115. (when (and org-ellipsis (stringp org-ellipsis))
  3116. (unless org-display-table
  3117. (setq org-display-table (make-display-table)))
  3118. (set-display-table-slot org-display-table
  3119. 4 (string-to-vector org-ellipsis))
  3120. (setq buffer-display-table org-display-table))
  3121. (org-set-regexps-and-options)
  3122. ;; Calc embedded
  3123. (org-set-local 'calc-embedded-open-mode "# ")
  3124. (modify-syntax-entry ?# "<")
  3125. (if org-startup-truncated (setq truncate-lines t))
  3126. (org-set-local 'font-lock-unfontify-region-function
  3127. 'org-unfontify-region)
  3128. ;; Activate before-change-function
  3129. (org-set-local 'org-table-may-need-update t)
  3130. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3131. 'local)
  3132. ;; Check for running clock before killing a buffer
  3133. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3134. ;; Paragraphs and auto-filling
  3135. (org-set-autofill-regexps)
  3136. (org-update-radio-target-regexp)
  3137. ;; Comment characters
  3138. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3139. (org-set-local 'comment-padding " ")
  3140. ;; Make isearch reveal context
  3141. (if (or (featurep 'xemacs)
  3142. (not (boundp 'outline-isearch-open-invisible-function)))
  3143. ;; Emacs 21 and XEmacs make use of the hook
  3144. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3145. ;; Emacs 22 deals with this through a special variable
  3146. (org-set-local 'outline-isearch-open-invisible-function
  3147. (lambda (&rest ignore) (org-show-context 'isearch))))
  3148. ;; If empty file that did not turn on org-mode automatically, make it to.
  3149. (if (and org-insert-mode-line-in-empty-file
  3150. (interactive-p)
  3151. (= (point-min) (point-max)))
  3152. (insert " -*- mode: org -*-\n\n"))
  3153. (unless org-inhibit-startup
  3154. (when org-startup-align-all-tables
  3155. (let ((bmp (buffer-modified-p)))
  3156. (org-table-map-tables 'org-table-align)
  3157. (set-buffer-modified-p bmp)))
  3158. (if org-startup-with-deadline-check
  3159. (call-interactively 'org-check-deadlines)
  3160. (cond
  3161. ((eq org-startup-folded t)
  3162. (org-cycle '(4)))
  3163. ((eq org-startup-folded 'content)
  3164. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  3165. (org-cycle '(4)) (org-cycle '(4))))))))
  3166. (defsubst org-call-with-arg (command arg)
  3167. "Call COMMAND interactively, but pretend prefix are was ARG."
  3168. (let ((current-prefix-arg arg)) (call-interactively command)))
  3169. (defsubst org-current-line (&optional pos)
  3170. (save-excursion
  3171. (and pos (goto-char pos))
  3172. (+ (if (bolp) 1 0) (count-lines 1 (point)))))
  3173. (defun org-current-time ()
  3174. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3175. (if (> org-time-stamp-rounding-minutes 0)
  3176. (let ((r org-time-stamp-rounding-minutes)
  3177. (time (decode-time)))
  3178. (apply 'encode-time
  3179. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3180. (nthcdr 2 time))))
  3181. (current-time)))
  3182. (defun org-add-props (string plist &rest props)
  3183. "Add text properties to entire string, from beginning to end.
  3184. PLIST may be a list of properties, PROPS are individual properties and values
  3185. that will be added to PLIST. Returns the string that was modified."
  3186. (add-text-properties
  3187. 0 (length string) (if props (append plist props) plist) string)
  3188. string)
  3189. (put 'org-add-props 'lisp-indent-function 2)
  3190. ;;;; Font-Lock stuff, including the activators
  3191. (defvar org-mouse-map (make-sparse-keymap))
  3192. (define-key org-mouse-map
  3193. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3194. (define-key org-mouse-map
  3195. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3196. (when org-mouse-1-follows-link
  3197. (define-key org-mouse-map [follow-link] 'mouse-face))
  3198. (when org-tab-follows-link
  3199. (define-key org-mouse-map [(tab)] 'org-open-at-point)
  3200. (define-key org-mouse-map "\C-i" 'org-open-at-point))
  3201. (when org-return-follows-link
  3202. (define-key org-mouse-map [(return)] 'org-open-at-point)
  3203. (define-key org-mouse-map "\C-m" 'org-open-at-point))
  3204. (require 'font-lock)
  3205. (defconst org-non-link-chars "]\t\n\r<>")
  3206. (defconst org-link-types '("https?" "ftp" "mailto" "file" "news" "bbdb" "vm"
  3207. "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  3208. (defconst org-link-re-with-space
  3209. (concat
  3210. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3211. "\\([^" org-non-link-chars " ]"
  3212. "[^" org-non-link-chars "]*"
  3213. "[^" org-non-link-chars " ]\\)>?")
  3214. "Matches a link with spaces, optional angular brackets around it.")
  3215. (defconst org-link-re-with-space2
  3216. (concat
  3217. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3218. "\\([^" org-non-link-chars " ]"
  3219. "[^]\t\n\r]*"
  3220. "[^" org-non-link-chars " ]\\)>?")
  3221. "Matches a link with spaces, optional angular brackets around it.")
  3222. (defconst org-angle-link-re
  3223. (concat
  3224. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3225. "\\([^" org-non-link-chars " ]"
  3226. "[^" org-non-link-chars "]*"
  3227. "\\)>")
  3228. "Matches link with angular brackets, spaces are allowed.")
  3229. (defconst org-plain-link-re
  3230. (concat
  3231. "\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3232. "\\([^]\t\n\r<>,;() ]+\\)")
  3233. "Matches plain link, without spaces.")
  3234. (defconst org-bracket-link-regexp
  3235. "\\[\\[\\([^]]+\\)\\]\\(\\[\\([^]]+\\)\\]\\)?\\]"
  3236. "Matches a link in double brackets.")
  3237. (defconst org-bracket-link-analytic-regexp
  3238. (concat
  3239. "\\[\\["
  3240. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3241. "\\([^]]+\\)"
  3242. "\\]"
  3243. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3244. "\\]"))
  3245. ; 1: http:
  3246. ; 2: http
  3247. ; 3: path
  3248. ; 4: [desc]
  3249. ; 5: desc
  3250. (defconst org-any-link-re
  3251. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3252. org-angle-link-re "\\)\\|\\("
  3253. org-plain-link-re "\\)")
  3254. "Regular expression matching any link.")
  3255. (defconst org-ts-lengths
  3256. (cons (length (format-time-string (car org-time-stamp-formats)))
  3257. (length (format-time-string (cdr org-time-stamp-formats))))
  3258. "This holds the lengths of the two different time formats.")
  3259. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}[^\r\n>]*?\\)>"
  3260. "Regular expression for fast time stamp matching.")
  3261. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}[^\r\n>]*?\\)[]>]"
  3262. "Regular expression for fast time stamp matching.")
  3263. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3264. "Regular expression matching time strings for analysis.")
  3265. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 ">")
  3266. "Regular expression matching time stamps, with groups.")
  3267. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[]>]")
  3268. "Regular expression matching time stamps (also [..]), with groups.")
  3269. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3270. "Regular expression matching a time stamp range.")
  3271. (defconst org-tr-regexp-both
  3272. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3273. "Regular expression matching a time stamp range.")
  3274. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3275. org-ts-regexp "\\)?")
  3276. "Regular expression matching a time stamp or time stamp range.")
  3277. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3278. org-ts-regexp-both "\\)?")
  3279. "Regular expression matching a time stamp or time stamp range.
  3280. The time stamps may be either active or inactive.")
  3281. (defvar org-emph-face nil)
  3282. (defun org-do-emphasis-faces (limit)
  3283. "Run through the buffer and add overlays to links."
  3284. (if (re-search-forward org-emph-re limit t)
  3285. (progn
  3286. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3287. 'face
  3288. (nth 1 (assoc (match-string 3)
  3289. org-emphasis-alist)))
  3290. (add-text-properties (match-beginning 2) (match-end 2)
  3291. '(font-lock-multiline t))
  3292. (backward-char 1)
  3293. t)))
  3294. (defun org-activate-plain-links (limit)
  3295. "Run through the buffer and add overlays to links."
  3296. (if (re-search-forward org-plain-link-re limit t)
  3297. (progn
  3298. (add-text-properties (match-beginning 0) (match-end 0)
  3299. (list 'mouse-face 'highlight
  3300. 'rear-nonsticky t
  3301. 'keymap org-mouse-map
  3302. ))
  3303. t)))
  3304. (defun org-activate-angle-links (limit)
  3305. "Run through the buffer and add overlays to links."
  3306. (if (re-search-forward org-angle-link-re limit t)
  3307. (progn
  3308. (add-text-properties (match-beginning 0) (match-end 0)
  3309. (list 'mouse-face 'highlight
  3310. 'rear-nonsticky t
  3311. 'keymap org-mouse-map
  3312. ))
  3313. t)))
  3314. (defmacro org-maybe-intangible (props)
  3315. "Add '(intangigble t) to PROPS if Emacs version is earlier than Emacs 22.
  3316. In emacs 21, invisible text is not avoided by the command loop, so the
  3317. intangible property is needed to make sure point skips this text.
  3318. In Emacs 22, this is not necessary. The intangible text property has
  3319. led to problems with flyspell. These problems are fixed in flyspell.el,
  3320. but we still avoid setting the property in Emacs 22 and later.
  3321. We use a macro so that the test can happen at compilation time."
  3322. (if (< emacs-major-version 22)
  3323. `(append '(intangible t) ,props)
  3324. props))
  3325. (defun org-activate-bracket-links (limit)
  3326. "Run through the buffer and add overlays to bracketed links."
  3327. (if (re-search-forward org-bracket-link-regexp limit t)
  3328. (let* ((help (concat "LINK: "
  3329. (org-match-string-no-properties 1)))
  3330. ;; FIXME: above we should remove the escapes.
  3331. ;; but that requires another match, protecting match data,
  3332. ;; a lot of overhead for font-lock.
  3333. (ip (org-maybe-intangible
  3334. (list 'invisible 'org-link 'rear-nonsticky t
  3335. 'keymap org-mouse-map 'mouse-face 'highlight
  3336. 'help-echo help)))
  3337. (vp (list 'rear-nonsticky t
  3338. 'keymap org-mouse-map 'mouse-face 'highlight
  3339. 'help-echo help)))
  3340. ;; We need to remove the invisible property here. Table narrowing
  3341. ;; may have made some of this invisible.
  3342. (remove-text-properties (match-beginning 0) (match-end 0)
  3343. '(invisible nil))
  3344. (if (match-end 3)
  3345. (progn
  3346. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3347. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3348. (add-text-properties (match-end 3) (match-end 0) ip))
  3349. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3350. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3351. (add-text-properties (match-end 1) (match-end 0) ip))
  3352. t)))
  3353. (defun org-activate-dates (limit)
  3354. "Run through the buffer and add overlays to dates."
  3355. (if (re-search-forward org-tsr-regexp-both limit t)
  3356. (progn
  3357. (add-text-properties (match-beginning 0) (match-end 0)
  3358. (list 'mouse-face 'highlight
  3359. 'rear-nonsticky t
  3360. 'keymap org-mouse-map))
  3361. (when org-display-custom-times
  3362. (if (match-end 3)
  3363. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3364. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3365. t)))
  3366. (defvar org-target-link-regexp nil
  3367. "Regular expression matching radio targets in plain text.")
  3368. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3369. "Regular expression matching a link target.")
  3370. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3371. "Regular expression matching a link target.")
  3372. (defun org-activate-target-links (limit)
  3373. "Run through the buffer and add overlays to target matches."
  3374. (when org-target-link-regexp
  3375. (let ((case-fold-search t))
  3376. (if (re-search-forward org-target-link-regexp limit t)
  3377. (progn
  3378. (add-text-properties (match-beginning 0) (match-end 0)
  3379. (list 'mouse-face 'highlight
  3380. 'rear-nonsticky t
  3381. 'keymap org-mouse-map
  3382. 'help-echo "Radio target link"
  3383. 'org-linked-text t))
  3384. t)))))
  3385. (defun org-update-radio-target-regexp ()
  3386. "Find all radio targets in this file and update the regular expression."
  3387. (interactive)
  3388. (when (memq 'radio org-activate-links)
  3389. (setq org-target-link-regexp
  3390. (org-make-target-link-regexp (org-all-targets 'radio)))
  3391. (org-restart-font-lock)))
  3392. (defun org-hide-wide-columns (limit)
  3393. (let (s e)
  3394. (setq s (text-property-any (point) (or limit (point-max))
  3395. 'org-cwidth t))
  3396. (when s
  3397. (setq e (next-single-property-change s 'org-cwidth))
  3398. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3399. (goto-char e)
  3400. t)))
  3401. (defun org-restart-font-lock ()
  3402. "Restart font-lock-mode, to force refontification."
  3403. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3404. (font-lock-mode -1)
  3405. (font-lock-mode 1)))
  3406. (defun org-all-targets (&optional radio)
  3407. "Return a list of all targets in this file.
  3408. With optional argument RADIO, only find radio targets."
  3409. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3410. rtn)
  3411. (save-excursion
  3412. (goto-char (point-min))
  3413. (while (re-search-forward re nil t)
  3414. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3415. rtn)))
  3416. (defun org-make-target-link-regexp (targets)
  3417. "Make regular expression matching all strings in TARGETS.
  3418. The regular expression finds the targets also if there is a line break
  3419. between words."
  3420. (and targets
  3421. (concat
  3422. "\\<\\("
  3423. (mapconcat
  3424. (lambda (x)
  3425. (while (string-match " +" x)
  3426. (setq x (replace-match "\\s-+" t t x)))
  3427. x)
  3428. targets
  3429. "\\|")
  3430. "\\)\\>")))
  3431. (defvar org-camel-regexp "\\*?\\<[A-Z]+[a-z]+[A-Z][a-zA-Z]*\\>"
  3432. "Matches CamelCase words, possibly with a star before it.")
  3433. (defun org-activate-camels (limit)
  3434. "Run through the buffer and add overlays to dates."
  3435. (if (re-search-forward org-camel-regexp limit t)
  3436. (progn
  3437. (add-text-properties (match-beginning 0) (match-end 0)
  3438. (list 'mouse-face 'highlight
  3439. 'rear-nonsticky t
  3440. 'keymap org-mouse-map))
  3441. t)))
  3442. (defun org-activate-tags (limit)
  3443. (if (re-search-forward "[ \t]\\(:[A-Za-z_@0-9:]+:\\)[ \r\n]" limit t)
  3444. (progn
  3445. (add-text-properties (match-beginning 1) (match-end 1)
  3446. (list 'mouse-face 'highlight
  3447. 'rear-nonsticky t
  3448. 'keymap org-mouse-map))
  3449. t)))
  3450. (defun org-font-lock-level ()
  3451. (save-excursion
  3452. (org-back-to-heading t)
  3453. (- (match-end 0) (match-beginning 0))))
  3454. (defun org-outline-level ()
  3455. (save-excursion
  3456. (looking-at outline-regexp)
  3457. (if (match-beginning 1)
  3458. (+ (org-get-string-indentation (match-string 1)) 1000)
  3459. (- (match-end 0) (match-beginning 0)))))
  3460. (defvar org-font-lock-keywords nil)
  3461. (defun org-set-font-lock-defaults ()
  3462. (let* ((em org-fontify-emphasized-text)
  3463. (lk org-activate-links)
  3464. (org-font-lock-extra-keywords
  3465. ;; Headlines
  3466. (list
  3467. '("^\\(\\**\\)\\(\\*\\)\\(.*\\)" (1 (org-get-level-face 1))
  3468. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3469. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3470. (1 'org-table))
  3471. ;; Links
  3472. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3473. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3474. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3475. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3476. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3477. (if (memq 'camel lk) '(org-activate-camels (0 'org-link t)))
  3478. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3479. (if org-table-limit-column-width
  3480. '(org-hide-wide-columns (0 nil append)))
  3481. ;; TODO lines
  3482. (list (concat "^\\*+[ \t]*" org-not-done-regexp)
  3483. '(1 'org-todo t))
  3484. ;; Priorities
  3485. (list (concat "\\[#[A-Z]\\]") '(0 'org-special-keyword t))
  3486. ;; Special keywords
  3487. (list org-repeat-re '(0 'org-special-keyword t))
  3488. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3489. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3490. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3491. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3492. ;; Emphasis
  3493. (if em
  3494. (if (featurep 'xemacs)
  3495. '(org-do-emphasis-faces (0 nil append))
  3496. '(org-do-emphasis-faces)))
  3497. ;; Checkboxes, similar to Frank Ruell's org-checklet.el
  3498. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[ X]\\]\\)"
  3499. 2 'bold prepend)
  3500. (if org-provide-checkbox-statistics
  3501. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3502. (0 (org-get-checkbox-statistics-face) t)))
  3503. ;; COMMENT
  3504. (list (concat "^\\*+[ \t]*\\<\\(" org-comment-string
  3505. "\\|" org-quote-string "\\)\\>")
  3506. '(1 'org-special-keyword t))
  3507. '("^#.*" (0 'font-lock-comment-face t))
  3508. ;; DONE
  3509. (if org-fontify-done-headline
  3510. (list (concat "^[*]+ +\\<\\(" org-done-string "\\)\\(.*\\)\\>")
  3511. '(1 'org-done t) '(2 'org-headline-done t))
  3512. (list (concat "^[*]+ +\\<\\(" org-done-string "\\)\\>")
  3513. '(1 'org-done t)))
  3514. ;; Table stuff
  3515. '("^[ \t]*\\(:.*\\)" (1 'org-table t))
  3516. '("| *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3517. '("^[ \t]*| *\\([#!$*_^]\\) *|" (1 'org-formula t))
  3518. (if org-format-transports-properties-p
  3519. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3520. '("^\\*+ \\(.*:ARCHIVE:.*\\)" (1 'org-archived prepend))
  3521. )))
  3522. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3523. ;; Now set the full font-lock-keywords
  3524. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3525. (org-set-local 'font-lock-defaults
  3526. '(org-font-lock-keywords t nil nil backward-paragraph))
  3527. (kill-local-variable 'font-lock-keywords) nil))
  3528. (defvar org-m nil)
  3529. (defvar org-l nil)
  3530. (defvar org-f nil)
  3531. (defun org-get-level-face (n)
  3532. "Get the right face for match N in font-lock matching of healdines."
  3533. (setq org-l (- (match-end 2) (match-beginning 1)))
  3534. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3535. ; (setq org-f (nth (1- (% org-l org-n-levels)) org-level-faces))
  3536. (setq org-f (nth (% (1- org-l) org-n-levels) org-level-faces))
  3537. (cond
  3538. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3539. ((eq n 2) org-f)
  3540. (t (if org-level-color-stars-only nil org-f))))
  3541. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3542. "Remove fontification and activation overlays from links."
  3543. (font-lock-default-unfontify-region beg end)
  3544. (let* ((buffer-undo-list t)
  3545. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3546. (inhibit-modification-hooks t)
  3547. deactivate-mark buffer-file-name buffer-file-truename)
  3548. (remove-text-properties beg end
  3549. '(mouse-face t keymap t org-linked-text t
  3550. rear-nonsticky t
  3551. invisible t intangible t))))
  3552. ;;;; Visibility cycling, including org-goto and indirect buffer
  3553. ;;; Cycling
  3554. (defvar org-cycle-global-status nil)
  3555. (make-variable-buffer-local 'org-cycle-global-status)
  3556. (defvar org-cycle-subtree-status nil)
  3557. (make-variable-buffer-local 'org-cycle-subtree-status)
  3558. ;;;###autoload
  3559. (defun org-cycle (&optional arg)
  3560. "Visibility cycling for Org-mode.
  3561. - When this function is called with a prefix argument, rotate the entire
  3562. buffer through 3 states (global cycling)
  3563. 1. OVERVIEW: Show only top-level headlines.
  3564. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3565. 3. SHOW ALL: Show everything.
  3566. - When point is at the beginning of a headline, rotate the subtree started
  3567. by this line through 3 different states (local cycling)
  3568. 1. FOLDED: Only the main headline is shown.
  3569. 2. CHILDREN: The main headline and the direct children are shown.
  3570. From this state, you can move to one of the children
  3571. and zoom in further.
  3572. 3. SUBTREE: Show the entire subtree, including body text.
  3573. - When there is a numeric prefix, go up to a heading with level ARG, do
  3574. a `show-subtree' and return to the previous cursor position. If ARG
  3575. is negative, go up that many levels.
  3576. - When point is not at the beginning of a headline, execute
  3577. `indent-relative', like TAB normally does. See the option
  3578. `org-cycle-emulate-tab' for details.
  3579. - Special case: if point is the the beginning of the buffer and there is
  3580. no headline in line 1, this function will act as if called with prefix arg."
  3581. (interactive "P")
  3582. (let* ((outline-regexp
  3583. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3584. "\\(?:\\*+\\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3585. outline-regexp))
  3586. (bob-special (and org-cycle-global-at-bob (bobp)
  3587. (not (looking-at outline-regexp))))
  3588. (org-cycle-hook
  3589. (if bob-special
  3590. (delq 'org-optimize-window-after-visibility-change
  3591. (copy-sequence org-cycle-hook))
  3592. org-cycle-hook))
  3593. (pos (point)))
  3594. (if (or bob-special (equal arg '(4)))
  3595. ;; special case: use global cycling
  3596. (setq arg t))
  3597. (cond
  3598. ((org-at-table-p 'any)
  3599. ;; Enter the table or move to the next field in the table
  3600. (or (org-table-recognize-table.el)
  3601. (progn
  3602. (if arg (org-table-edit-field t)
  3603. (org-table-justify-field-maybe)
  3604. (call-interactively 'org-table-next-field)))))
  3605. ((eq arg t) ;; Global cycling
  3606. (cond
  3607. ((and (eq last-command this-command)
  3608. (eq org-cycle-global-status 'overview))
  3609. ;; We just created the overview - now do table of contents
  3610. ;; This can be slow in very large buffers, so indicate action
  3611. (message "CONTENTS...")
  3612. (org-content)
  3613. (message "CONTENTS...done")
  3614. (setq org-cycle-global-status 'contents)
  3615. (run-hook-with-args 'org-cycle-hook 'contents))
  3616. ((and (eq last-command this-command)
  3617. (eq org-cycle-global-status 'contents))
  3618. ;; We just showed the table of contents - now show everything
  3619. (show-all)
  3620. (message "SHOW ALL")
  3621. (setq org-cycle-global-status 'all)
  3622. (run-hook-with-args 'org-cycle-hook 'all))
  3623. (t
  3624. ;; Default action: go to overview
  3625. (org-overview)
  3626. (message "OVERVIEW")
  3627. (setq org-cycle-global-status 'overview)
  3628. (run-hook-with-args 'org-cycle-hook 'overview))))
  3629. ((integerp arg)
  3630. ;; Show-subtree, ARG levels up from here.
  3631. (save-excursion
  3632. (org-back-to-heading)
  3633. (outline-up-heading (if (< arg 0) (- arg)
  3634. (- (funcall outline-level) arg)))
  3635. (org-show-subtree)))
  3636. ((save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3637. ;; At a heading: rotate between three different views
  3638. (org-back-to-heading)
  3639. (let ((goal-column 0) eoh eol eos)
  3640. ;; First, some boundaries
  3641. (save-excursion
  3642. (org-back-to-heading)
  3643. (save-excursion
  3644. (beginning-of-line 2)
  3645. (while (and (not (eobp)) ;; this is like `next-line'
  3646. (get-char-property (1- (point)) 'invisible))
  3647. (beginning-of-line 2)) (setq eol (point)))
  3648. (outline-end-of-heading) (setq eoh (point))
  3649. (org-end-of-subtree t)
  3650. (skip-chars-forward " \t\n")
  3651. (beginning-of-line 1) ; in case this is an item
  3652. (setq eos (1- (point))))
  3653. ;; Find out what to do next and set `this-command'
  3654. (cond
  3655. ((= eos eoh)
  3656. ;; Nothing is hidden behind this heading
  3657. (message "EMPTY ENTRY")
  3658. (setq org-cycle-subtree-status nil))
  3659. ((>= eol eos)
  3660. ;; Entire subtree is hidden in one line: open it
  3661. (org-show-entry)
  3662. (show-children)
  3663. (message "CHILDREN")
  3664. (setq org-cycle-subtree-status 'children)
  3665. (run-hook-with-args 'org-cycle-hook 'children))
  3666. ((and (eq last-command this-command)
  3667. (eq org-cycle-subtree-status 'children))
  3668. ;; We just showed the children, now show everything.
  3669. (org-show-subtree)
  3670. (message "SUBTREE")
  3671. (setq org-cycle-subtree-status 'subtree)
  3672. (run-hook-with-args 'org-cycle-hook 'subtree))
  3673. (t
  3674. ;; Default action: hide the subtree.
  3675. (hide-subtree)
  3676. (message "FOLDED")
  3677. (setq org-cycle-subtree-status 'folded)
  3678. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3679. ;; TAB emulation
  3680. (buffer-read-only (org-back-to-heading))
  3681. ((org-try-cdlatex-tab))
  3682. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3683. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3684. (or (and (eq org-cycle-emulate-tab 'white)
  3685. (= (match-end 0) (point-at-eol)))
  3686. (and (eq org-cycle-emulate-tab 'whitestart)
  3687. (>= (match-end 0) pos))))
  3688. t
  3689. (eq org-cycle-emulate-tab t))
  3690. (if (and (looking-at "[ \n\r\t]")
  3691. (string-match "^[ \t]*$" (buffer-substring
  3692. (point-at-bol) (point))))
  3693. (progn
  3694. (beginning-of-line 1)
  3695. (and (looking-at "[ \t]+") (replace-match ""))))
  3696. (indent-relative))
  3697. (t (save-excursion
  3698. (org-back-to-heading)
  3699. (org-cycle))))))
  3700. ;;;###autoload
  3701. (defun org-global-cycle (&optional arg)
  3702. "Cycle the global visibility. For details see `org-cycle'."
  3703. (interactive "P")
  3704. (let ((org-cycle-include-plain-lists
  3705. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  3706. (if (integerp arg)
  3707. (progn
  3708. (show-all)
  3709. (hide-sublevels arg)
  3710. (setq org-cycle-global-status 'contents))
  3711. (org-cycle '(4)))))
  3712. (defun org-overview ()
  3713. "Switch to overview mode, shoing only top-level headlines.
  3714. Really, this shows all headlines with level equal or greater than the level
  3715. of the first headline in the buffer. This is important, because if the
  3716. first headline is not level one, then (hide-sublevels 1) gives confusing
  3717. results."
  3718. (interactive)
  3719. (hide-sublevels (save-excursion
  3720. (goto-char (point-min))
  3721. (if (re-search-forward (concat "^" outline-regexp) nil t)
  3722. (progn
  3723. (goto-char (match-beginning 0))
  3724. (funcall outline-level))
  3725. 1))))
  3726. ;; FIXME: allow an argument to give a limiting level for this.
  3727. (defun org-content ()
  3728. "Show all headlines in the buffer, like a table of contents"
  3729. (interactive)
  3730. (save-excursion
  3731. ;; Visit all headings and show their offspring
  3732. (goto-char (point-max))
  3733. (catch 'exit
  3734. (while (and (progn (condition-case nil
  3735. (outline-previous-visible-heading 1)
  3736. (error (goto-char (point-min))))
  3737. t)
  3738. (looking-at outline-regexp))
  3739. (show-branches)
  3740. (if (bobp) (throw 'exit nil))))))
  3741. (defun org-optimize-window-after-visibility-change (state)
  3742. "Adjust the window after a change in outline visibility.
  3743. This function is the default value of the hook `org-cycle-hook'."
  3744. (when (get-buffer-window (current-buffer))
  3745. (cond
  3746. ((eq state 'overview) (org-first-headline-recenter 1))
  3747. ((eq state 'content) nil)
  3748. ((eq state 'all) nil)
  3749. ((eq state 'folded) nil)
  3750. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  3751. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  3752. (defun org-subtree-end-visible-p ()
  3753. "Is the end of the current subtree visible?"
  3754. (pos-visible-in-window-p
  3755. (save-excursion (org-end-of-subtree t) (point))))
  3756. (defun org-first-headline-recenter (&optional N)
  3757. "Move cursor to the first headline and recenter the headline.
  3758. Optional argument N means, put the headline into the Nth line of the window."
  3759. (goto-char (point-min))
  3760. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  3761. (beginning-of-line)
  3762. (recenter (prefix-numeric-value N))))
  3763. ;;; Org-goto
  3764. (defvar org-goto-window-configuration nil)
  3765. (defvar org-goto-marker nil)
  3766. (defvar org-goto-map (make-sparse-keymap))
  3767. (let ((cmds '(isearch-forward isearch-backward)) cmd)
  3768. (while (setq cmd (pop cmds))
  3769. (substitute-key-definition cmd cmd org-goto-map global-map)))
  3770. (define-key org-goto-map "\C-m" 'org-goto-ret)
  3771. (define-key org-goto-map [(left)] 'org-goto-left)
  3772. (define-key org-goto-map [(right)] 'org-goto-right)
  3773. (define-key org-goto-map [(?q)] 'org-goto-quit)
  3774. (define-key org-goto-map [(control ?g)] 'org-goto-quit)
  3775. (define-key org-goto-map "\C-i" 'org-cycle)
  3776. (define-key org-goto-map [(tab)] 'org-cycle)
  3777. (define-key org-goto-map [(down)] 'outline-next-visible-heading)
  3778. (define-key org-goto-map [(up)] 'outline-previous-visible-heading)
  3779. (define-key org-goto-map "n" 'outline-next-visible-heading)
  3780. (define-key org-goto-map "p" 'outline-previous-visible-heading)
  3781. (define-key org-goto-map "f" 'outline-forward-same-level)
  3782. (define-key org-goto-map "b" 'outline-backward-same-level)
  3783. (define-key org-goto-map "u" 'outline-up-heading)
  3784. (define-key org-goto-map "\C-c\C-n" 'outline-next-visible-heading)
  3785. (define-key org-goto-map "\C-c\C-p" 'outline-previous-visible-heading)
  3786. (define-key org-goto-map "\C-c\C-f" 'outline-forward-same-level)
  3787. (define-key org-goto-map "\C-c\C-b" 'outline-backward-same-level)
  3788. (define-key org-goto-map "\C-c\C-u" 'outline-up-heading)
  3789. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  3790. (while l (define-key org-goto-map (int-to-string (pop l)) 'digit-argument)))
  3791. (defconst org-goto-help
  3792. "Select a location to jump to, press RET
  3793. \[Up]/[Down]=next/prev headline TAB=cycle visibility RET=select [Q]uit")
  3794. (defun org-goto ()
  3795. "Go to a different location of the document, keeping current visibility.
  3796. When you want to go to a different location in a document, the fastest way
  3797. is often to fold the entire buffer and then dive into the tree. This
  3798. method has the disadvantage, that the previous location will be folded,
  3799. which may not be what you want.
  3800. This command works around this by showing a copy of the current buffer in
  3801. overview mode. You can dive into the tree in that copy, to find the
  3802. location you want to reach. When pressing RET, the command returns to the
  3803. original buffer in which the visibility is still unchanged. It then jumps
  3804. to the new location, making it and the headline hierarchy above it visible."
  3805. (interactive)
  3806. (let* ((org-goto-start-pos (point))
  3807. (selected-point
  3808. (org-get-location (current-buffer) org-goto-help)))
  3809. (if selected-point
  3810. (progn
  3811. (org-mark-ring-push org-goto-start-pos)
  3812. (goto-char selected-point)
  3813. (if (or (org-invisible-p) (org-invisible-p2))
  3814. (org-show-context 'org-goto)))
  3815. (error "Quit"))))
  3816. (defvar org-selected-point nil) ; dynamically scoped parameter
  3817. (defun org-get-location (buf help)
  3818. "Let the user select a location in the Org-mode buffer BUF.
  3819. This function uses a recursive edit. It returns the selected position
  3820. or nil."
  3821. (let (org-selected-point)
  3822. (save-excursion
  3823. (save-window-excursion
  3824. (delete-other-windows)
  3825. (switch-to-buffer (get-buffer-create "*org-goto*"))
  3826. (with-output-to-temp-buffer "*Help*"
  3827. (princ help))
  3828. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  3829. (setq buffer-read-only nil)
  3830. (erase-buffer)
  3831. (insert-buffer-substring buf)
  3832. (let ((org-startup-truncated t)
  3833. (org-startup-folded t)
  3834. (org-startup-align-all-tables nil)
  3835. (org-startup-with-deadline-check nil))
  3836. (org-mode))
  3837. (setq buffer-read-only t)
  3838. (if (and (boundp 'org-goto-start-pos)
  3839. (integer-or-marker-p org-goto-start-pos))
  3840. (let ((org-show-hierarchy-above t)
  3841. (org-show-siblings t)
  3842. (org-show-following-heading t))
  3843. (goto-char org-goto-start-pos)
  3844. (and (org-invisible-p) (org-show-context)))
  3845. (goto-char (point-min)))
  3846. (org-beginning-of-line)
  3847. (message "Select location and press RET")
  3848. ;; now we make sure that during selection, ony very few keys work
  3849. ;; and that it is impossible to switch to another window.
  3850. (let ((gm (current-global-map))
  3851. (overriding-local-map org-goto-map))
  3852. (unwind-protect
  3853. (progn
  3854. (use-global-map org-goto-map)
  3855. (recursive-edit))
  3856. (use-global-map gm)))))
  3857. (kill-buffer "*org-goto*")
  3858. org-selected-point))
  3859. (defun org-goto-ret (&optional arg)
  3860. "Finish `org-goto' by going to the new location."
  3861. (interactive "P")
  3862. (setq org-selected-point (point)
  3863. current-prefix-arg arg)
  3864. (throw 'exit nil))
  3865. (defun org-goto-left ()
  3866. "Finish `org-goto' by going to the new location."
  3867. (interactive)
  3868. (if (org-on-heading-p)
  3869. (progn
  3870. (beginning-of-line 1)
  3871. (setq org-selected-point (point)
  3872. current-prefix-arg (- (match-end 0) (match-beginning 0)))
  3873. (throw 'exit nil))
  3874. (error "Not on a heading")))
  3875. (defun org-goto-right ()
  3876. "Finish `org-goto' by going to the new location."
  3877. (interactive)
  3878. (if (org-on-heading-p)
  3879. (progn
  3880. (outline-end-of-subtree)
  3881. (or (eobp) (forward-char 1))
  3882. (setq org-selected-point (point)
  3883. current-prefix-arg (- (match-end 0) (match-beginning 0)))
  3884. (throw 'exit nil))
  3885. (error "Not on a heading")))
  3886. (defun org-goto-quit ()
  3887. "Finish `org-goto' without cursor motion."
  3888. (interactive)
  3889. (setq org-selected-point nil)
  3890. (throw 'exit nil))
  3891. ;;; Indirect buffer display of subtrees
  3892. (defvar org-indirect-dedicated-frame nil
  3893. "This is the frame being used for indirect tree display.")
  3894. (defvar org-last-indirect-buffer nil)
  3895. (defun org-tree-to-indirect-buffer (&optional arg)
  3896. "Create indirect buffer and narrow it to current subtree.
  3897. With numerical prefix ARG, go up to this level and then take that tree.
  3898. If ARG is negative, go up that many levels.
  3899. Normally this command removes the indirect buffer previously made
  3900. with this command. However, when called with a C-u prefix, the last buffer
  3901. is kept so that you can work with several indirect buffers at the same time.
  3902. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  3903. requests that a new frame be made for the new buffer, so that the dedicated
  3904. frame is not changed."
  3905. (interactive "P")
  3906. (let ((cbuf (current-buffer))
  3907. (cwin (selected-window))
  3908. (pos (point))
  3909. beg end level heading ibuf)
  3910. (save-excursion
  3911. (org-back-to-heading t)
  3912. (when (numberp arg)
  3913. (setq level (org-outline-level))
  3914. (if (< arg 0) (setq arg (+ level arg)))
  3915. (while (> (setq level (org-outline-level)) arg)
  3916. (outline-up-heading 1 t)))
  3917. (setq beg (point)
  3918. heading (org-get-heading))
  3919. (org-end-of-subtree t) (setq end (point)))
  3920. (if (and (not arg)
  3921. (buffer-live-p org-last-indirect-buffer))
  3922. (kill-buffer org-last-indirect-buffer))
  3923. (setq ibuf (org-get-indirect-buffer cbuf)
  3924. org-last-indirect-buffer ibuf)
  3925. (cond
  3926. ((or (eq org-indirect-buffer-display 'new-frame)
  3927. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  3928. (select-frame (make-frame))
  3929. (delete-other-windows)
  3930. (switch-to-buffer ibuf)
  3931. (org-set-frame-title heading))
  3932. ((eq org-indirect-buffer-display 'dedicated-frame)
  3933. (raise-frame
  3934. (select-frame (or (and org-indirect-dedicated-frame
  3935. (frame-live-p org-indirect-dedicated-frame)
  3936. org-indirect-dedicated-frame)
  3937. (setq org-indirect-dedicated-frame (make-frame)))))
  3938. (delete-other-windows)
  3939. (switch-to-buffer ibuf)
  3940. (org-set-frame-title (concat "Indirect: " heading)))
  3941. ((eq org-indirect-buffer-display 'current-window)
  3942. (switch-to-buffer ibuf))
  3943. ((eq org-indirect-buffer-display 'other-window)
  3944. (pop-to-buffer ibuf))
  3945. (t (error "Invalid value.")))
  3946. (if (featurep 'xemacs)
  3947. (save-excursion (org-mode) (turn-on-font-lock)))
  3948. (narrow-to-region beg end)
  3949. (show-all)
  3950. (goto-char pos)
  3951. (and (window-live-p cwin) (select-window cwin))))
  3952. (defun org-get-indirect-buffer (&optional buffer)
  3953. (setq buffer (or buffer (current-buffer)))
  3954. (let ((n 1) (base (buffer-name buffer)) bname)
  3955. (while (buffer-live-p
  3956. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  3957. (setq n (1+ n)))
  3958. (condition-case nil
  3959. (make-indirect-buffer buffer bname 'clone)
  3960. (error (make-indirect-buffer buffer bname)))))
  3961. (defun org-set-frame-title (title)
  3962. "Set the title of the current frame to the string TITLE."
  3963. ;; FIXME: how to name a single frame in XEmacs???
  3964. (unless (featurep 'xemacs)
  3965. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  3966. ;;;; Structure editing
  3967. ;;; Inserting headlines
  3968. (defun org-insert-heading (&optional force-heading)
  3969. "Insert a new heading or item with same depth at point.
  3970. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  3971. If point is at the beginning of a headline, insert a sibling before the
  3972. current headline. If point is in the middle of a headline, split the headline
  3973. at that position and make the rest of the headline part of the sibling below
  3974. the current headline."
  3975. (interactive "P")
  3976. (if (= (buffer-size) 0)
  3977. (insert "\n* ")
  3978. (when (or force-heading (not (org-insert-item)))
  3979. (let* ((head (save-excursion
  3980. (condition-case nil
  3981. (progn
  3982. (org-back-to-heading)
  3983. (match-string 0))
  3984. (error "*"))))
  3985. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  3986. pos)
  3987. (cond
  3988. ((and (org-on-heading-p) (bolp)
  3989. (save-excursion (backward-char 1) (not (org-invisible-p))))
  3990. (open-line (if blank 2 1)))
  3991. ((and (bolp) (save-excursion
  3992. (backward-char 1) (not (org-invisible-p))))
  3993. nil)
  3994. (t (newline (if blank 2 1))))
  3995. (insert head) (just-one-space)
  3996. (setq pos (point))
  3997. (end-of-line 1)
  3998. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  3999. (run-hooks 'org-insert-heading-hook)))))
  4000. (defun org-insert-todo-heading (arg)
  4001. "Insert a new heading with the same level and TODO state as current heading.
  4002. If the heading has no TODO state, or if the state is DONE, use the first
  4003. state (TODO by default). Also with prefix arg, force first state."
  4004. (interactive "P")
  4005. (when (not (org-insert-item 'checkbox))
  4006. (org-insert-heading)
  4007. (save-excursion
  4008. (org-back-to-heading)
  4009. (outline-previous-heading)
  4010. (looking-at org-todo-line-regexp))
  4011. (if (or arg
  4012. (not (match-beginning 2))
  4013. (equal (match-string 2) org-done-string))
  4014. (insert (car org-todo-keywords) " ")
  4015. (insert (match-string 2) " "))))
  4016. ;;; Promotion and Demotion
  4017. (defun org-promote-subtree ()
  4018. "Promote the entire subtree.
  4019. See also `org-promote'."
  4020. (interactive)
  4021. (save-excursion
  4022. (org-map-tree 'org-promote))
  4023. (org-fix-position-after-promote))
  4024. (defun org-demote-subtree ()
  4025. "Demote the entire subtree. See `org-demote'.
  4026. See also `org-promote'."
  4027. (interactive)
  4028. (save-excursion
  4029. (org-map-tree 'org-demote))
  4030. (org-fix-position-after-promote))
  4031. (defun org-do-promote ()
  4032. "Promote the current heading higher up the tree.
  4033. If the region is active in `transient-mark-mode', promote all headings
  4034. in the region."
  4035. (interactive)
  4036. (save-excursion
  4037. (if (org-region-active-p)
  4038. (org-map-region 'org-promote (region-beginning) (region-end))
  4039. (org-promote)))
  4040. (org-fix-position-after-promote))
  4041. (defun org-do-demote ()
  4042. "Demote the current heading lower down the tree.
  4043. If the region is active in `transient-mark-mode', demote all headings
  4044. in the region."
  4045. (interactive)
  4046. (save-excursion
  4047. (if (org-region-active-p)
  4048. (org-map-region 'org-demote (region-beginning) (region-end))
  4049. (org-demote)))
  4050. (org-fix-position-after-promote))
  4051. (defun org-fix-position-after-promote ()
  4052. "Make sure that after pro/demotion cursor position is right."
  4053. (let ((pos (point)))
  4054. (when (save-excursion
  4055. (beginning-of-line 1)
  4056. (looking-at org-todo-line-regexp)
  4057. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4058. (cond ((eobp) (insert " "))
  4059. ((eolp) (insert " "))
  4060. ((equal (char-after) ?\ ) (forward-char 1))))))
  4061. (defun org-get-legal-level (level &optional change)
  4062. "Rectify a level change under the influence of `org-odd-levels-only'
  4063. LEVEL is a current level, CHANGE is by how much the level should be
  4064. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4065. even level numbers will become the next higher odd number."
  4066. (if org-odd-levels-only
  4067. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4068. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4069. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4070. (max 1 (+ level change))))
  4071. (defun org-promote ()
  4072. "Promote the current heading higher up the tree.
  4073. If the region is active in `transient-mark-mode', promote all headings
  4074. in the region."
  4075. (org-back-to-heading t)
  4076. (let* ((level (save-match-data (funcall outline-level)))
  4077. (up-head (make-string (org-get-legal-level level -1) ?*))
  4078. (diff (abs (- level (length up-head)))))
  4079. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4080. (replace-match up-head nil t)
  4081. ;; Fixup tag positioning
  4082. (and org-auto-align-tags (org-set-tags nil t))
  4083. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4084. (defun org-demote ()
  4085. "Demote the current heading lower down the tree.
  4086. If the region is active in `transient-mark-mode', demote all headings
  4087. in the region."
  4088. (org-back-to-heading t)
  4089. (let* ((level (save-match-data (funcall outline-level)))
  4090. (down-head (make-string (org-get-legal-level level 1) ?*))
  4091. (diff (abs (- level (length down-head)))))
  4092. (replace-match down-head nil t)
  4093. ;; Fixup tag positioning
  4094. (and org-auto-align-tags (org-set-tags nil t))
  4095. (if org-adapt-indentation (org-fixup-indentation diff))))
  4096. (defun org-map-tree (fun)
  4097. "Call FUN for every heading underneath the current one."
  4098. (org-back-to-heading)
  4099. (let ((level (funcall outline-level)))
  4100. (save-excursion
  4101. (funcall fun)
  4102. (while (and (progn
  4103. (outline-next-heading)
  4104. (> (funcall outline-level) level))
  4105. (not (eobp)))
  4106. (funcall fun)))))
  4107. (defun org-map-region (fun beg end)
  4108. "Call FUN for every heading between BEG and END."
  4109. (let ((org-ignore-region t))
  4110. (save-excursion
  4111. (setq end (copy-marker end))
  4112. (goto-char beg)
  4113. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4114. (< (point) end))
  4115. (funcall fun))
  4116. (while (and (progn
  4117. (outline-next-heading)
  4118. (< (point) end))
  4119. (not (eobp)))
  4120. (funcall fun)))))
  4121. (defun org-fixup-indentation (diff)
  4122. "Change the indentation in the current entry by DIFF
  4123. However, if any line in the current entry has no indentation, or if it
  4124. would end up with no indentation after the change, nothing at all is done."
  4125. (save-excursion
  4126. (let ((end (save-excursion (outline-next-heading)
  4127. (point-marker)))
  4128. (prohibit (if (> diff 0)
  4129. "^\\S-"
  4130. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4131. col)
  4132. (unless (save-excursion (re-search-forward prohibit end t))
  4133. (while (re-search-forward "^[ \t]+" end t)
  4134. (goto-char (match-end 0))
  4135. (setq col (current-column))
  4136. (if (< diff 0) (replace-match ""))
  4137. (indent-to (+ diff col))))
  4138. (move-marker end nil))))
  4139. (defun org-convert-to-odd-levels ()
  4140. "Convert an org-mode file with all levels allowed to one with odd levels.
  4141. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4142. level 5 etc."
  4143. (interactive)
  4144. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4145. (let ((org-odd-levels-only nil) n)
  4146. (save-excursion
  4147. (goto-char (point-min))
  4148. (while (re-search-forward "^\\*\\*+" nil t)
  4149. (setq n (1- (length (match-string 0))))
  4150. (while (>= (setq n (1- n)) 0)
  4151. (org-demote))
  4152. (end-of-line 1))))))
  4153. (defun org-convert-to-oddeven-levels ()
  4154. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4155. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4156. section with an even level, conversion would destroy the structure of the file. An error
  4157. is signaled in this case."
  4158. (interactive)
  4159. (goto-char (point-min))
  4160. ;; First check if there are no even levels
  4161. (when (re-search-forward "^\\(\\*\\*\\)+[^*]" nil t)
  4162. (org-show-context t)
  4163. (error "Not all levels are odd in this file. Conversion not possible."))
  4164. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4165. (let ((org-odd-levels-only nil) n)
  4166. (save-excursion
  4167. (goto-char (point-min))
  4168. (while (re-search-forward "^\\*\\*+" nil t)
  4169. (setq n (/ (length (match-string 0)) 2))
  4170. (while (>= (setq n (1- n)) 0)
  4171. (org-promote))
  4172. (end-of-line 1))))))
  4173. (defun org-tr-level (n)
  4174. "Make N odd if required."
  4175. (if org-odd-levels-only (1+ (/ n 2)) n))
  4176. ;;; Vertical tree motion, cutting and pasting of subtrees
  4177. (defun org-move-subtree-up (&optional arg)
  4178. "Move the current subtree up past ARG headlines of the same level."
  4179. (interactive "p")
  4180. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4181. (defun org-move-subtree-down (&optional arg)
  4182. "Move the current subtree down past ARG headlines of the same level."
  4183. (interactive "p")
  4184. (setq arg (prefix-numeric-value arg))
  4185. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4186. 'outline-get-last-sibling))
  4187. (ins-point (make-marker))
  4188. (cnt (abs arg))
  4189. beg end txt folded)
  4190. ;; Select the tree
  4191. (org-back-to-heading)
  4192. (setq beg (point))
  4193. (save-match-data
  4194. (save-excursion (outline-end-of-heading)
  4195. (setq folded (org-invisible-p)))
  4196. (outline-end-of-subtree))
  4197. (outline-next-heading)
  4198. (setq end (point))
  4199. ;; Find insertion point, with error handling
  4200. (goto-char beg)
  4201. (while (> cnt 0)
  4202. (or (and (funcall movfunc) (looking-at outline-regexp))
  4203. (progn (goto-char beg)
  4204. (error "Cannot move past superior level or buffer limit")))
  4205. (setq cnt (1- cnt)))
  4206. (if (> arg 0)
  4207. ;; Moving forward - still need to move over subtree
  4208. (progn (outline-end-of-subtree)
  4209. (outline-next-heading)
  4210. (if (not (or (looking-at (concat "^" outline-regexp))
  4211. (bolp)))
  4212. (newline))))
  4213. (move-marker ins-point (point))
  4214. (setq txt (buffer-substring beg end))
  4215. (delete-region beg end)
  4216. (insert txt)
  4217. (goto-char ins-point)
  4218. (if folded (hide-subtree))
  4219. (move-marker ins-point nil)))
  4220. (defvar org-subtree-clip ""
  4221. "Clipboard for cut and paste of subtrees.
  4222. This is actually only a copy of the kill, because we use the normal kill
  4223. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4224. (defvar org-subtree-clip-folded nil
  4225. "Was the last copied subtree folded?
  4226. This is used to fold the tree back after pasting.")
  4227. (defun org-cut-subtree ()
  4228. "Cut the current subtree into the clipboard.
  4229. This is a short-hand for marking the subtree and then cutting it."
  4230. (interactive)
  4231. (org-copy-subtree 'cut))
  4232. (defun org-copy-subtree (&optional cut)
  4233. "Cut the current subtree into the clipboard.
  4234. This is a short-hand for marking the subtree and then copying it.
  4235. If CUT is non-nil, actually cut the subtree."
  4236. (interactive)
  4237. (let (beg end folded)
  4238. (org-back-to-heading)
  4239. (setq beg (point))
  4240. (save-match-data
  4241. (save-excursion (outline-end-of-heading)
  4242. (setq folded (org-invisible-p)))
  4243. (outline-end-of-subtree))
  4244. (if (equal (char-after) ?\n) (forward-char 1))
  4245. (setq end (point))
  4246. (goto-char beg)
  4247. (when (> end beg)
  4248. (setq org-subtree-clip-folded folded)
  4249. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4250. (setq org-subtree-clip (current-kill 0))
  4251. (message "%s: Subtree with %d characters"
  4252. (if cut "Cut" "Copied")
  4253. (length org-subtree-clip)))))
  4254. (defun org-paste-subtree (&optional level tree)
  4255. "Paste the clipboard as a subtree, with modification of headline level.
  4256. The entire subtree is promoted or demoted in order to match a new headline
  4257. level. By default, the new level is derived from the visible headings
  4258. before and after the insertion point, and taken to be the inferior headline
  4259. level of the two. So if the previous visible heading is level 3 and the
  4260. next is level 4 (or vice versa), level 4 will be used for insertion.
  4261. This makes sure that the subtree remains an independent subtree and does
  4262. not swallow low level entries.
  4263. You can also force a different level, either by using a numeric prefix
  4264. argument, or by inserting the heading marker by hand. For example, if the
  4265. cursor is after \"*****\", then the tree will be shifted to level 5.
  4266. If you want to insert the tree as is, just use \\[yank].
  4267. If optional TREE is given, use this text instead of the kill ring."
  4268. (interactive "P")
  4269. (unless (org-kill-is-subtree-p tree)
  4270. (error
  4271. (substitute-command-keys
  4272. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4273. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  4274. (^re (concat "^\\(" outline-regexp "\\)"))
  4275. (re (concat "\\(" outline-regexp "\\)"))
  4276. (^re_ (concat "\\(" outline-regexp "\\)[ \t]*"))
  4277. (old-level (if (string-match ^re txt)
  4278. (- (match-end 0) (match-beginning 0))
  4279. -1))
  4280. (force-level (cond (level (prefix-numeric-value level))
  4281. ((string-match
  4282. ^re_ (buffer-substring (point-at-bol) (point)))
  4283. (- (match-end 0) (match-beginning 0)))
  4284. (t nil)))
  4285. (previous-level (save-excursion
  4286. (condition-case nil
  4287. (progn
  4288. (outline-previous-visible-heading 1)
  4289. (if (looking-at re)
  4290. (- (match-end 0) (match-beginning 0))
  4291. 1))
  4292. (error 1))))
  4293. (next-level (save-excursion
  4294. (condition-case nil
  4295. (progn
  4296. (outline-next-visible-heading 1)
  4297. (if (looking-at re)
  4298. (- (match-end 0) (match-beginning 0))
  4299. 1))
  4300. (error 1))))
  4301. (new-level (or force-level (max previous-level next-level)))
  4302. (shift (if (or (= old-level -1)
  4303. (= new-level -1)
  4304. (= old-level new-level))
  4305. 0
  4306. (- new-level old-level)))
  4307. (shift1 shift)
  4308. (delta (if (> shift 0) -1 1))
  4309. (func (if (> shift 0) 'org-demote 'org-promote))
  4310. (org-odd-levels-only nil)
  4311. beg end)
  4312. ;; Remove the forces level indicator
  4313. (if force-level
  4314. (delete-region (point-at-bol) (point)))
  4315. ;; Make sure we start at the beginning of an empty line
  4316. (if (not (bolp)) (insert "\n"))
  4317. (if (not (looking-at "[ \t]*$"))
  4318. (progn (insert "\n") (backward-char 1)))
  4319. ;; Paste
  4320. (setq beg (point))
  4321. (if (string-match "[ \t\r\n]+\\'" txt)
  4322. (setq txt (replace-match "\n" t t txt)))
  4323. (insert txt)
  4324. (setq end (point))
  4325. (if (looking-at "[ \t\r\n]+")
  4326. (replace-match "\n"))
  4327. (goto-char beg)
  4328. ;; Shift if necessary
  4329. (if (= shift 0)
  4330. (message "Pasted at level %d, without shift" new-level)
  4331. (save-restriction
  4332. (narrow-to-region beg end)
  4333. (while (not (= shift 0))
  4334. (org-map-region func (point-min) (point-max))
  4335. (setq shift (+ delta shift)))
  4336. (goto-char (point-min))
  4337. (message "Pasted at level %d, with shift by %d levels"
  4338. new-level shift1)))
  4339. (if (and kill-ring
  4340. (eq org-subtree-clip (current-kill 0))
  4341. org-subtree-clip-folded)
  4342. ;; The tree was folded before it was killed/copied
  4343. (hide-subtree))))
  4344. (defun org-kill-is-subtree-p (&optional txt)
  4345. "Check if the current kill is an outline subtree, or a set of trees.
  4346. Returns nil if kill does not start with a headline, or if the first
  4347. headline level is not the largest headline level in the tree.
  4348. So this will actually accept several entries of equal levels as well,
  4349. which is OK for `org-paste-subtree'.
  4350. If optional TXT is given, check this string instead of the current kill."
  4351. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  4352. (start-level (and kill
  4353. (string-match (concat "\\`" outline-regexp) kill)
  4354. (- (match-end 0) (match-beginning 0))))
  4355. (re (concat "^" outline-regexp))
  4356. (start 1))
  4357. (if (not start-level)
  4358. nil ;; does not even start with a heading
  4359. (catch 'exit
  4360. (while (setq start (string-match re kill (1+ start)))
  4361. (if (< (- (match-end 0) (match-beginning 0)) start-level)
  4362. (throw 'exit nil)))
  4363. t))))
  4364. (defun org-narrow-to-subtree ()
  4365. "Narrow buffer to the current subtree."
  4366. (interactive)
  4367. (save-excursion
  4368. (narrow-to-region
  4369. (progn (org-back-to-heading) (point))
  4370. (progn (org-end-of-subtree t) (point)))))
  4371. ;;; Outline Sorting
  4372. (defun org-sort (with-case)
  4373. "Call `org-sort-entries' or `org-table-sort-lines', depending on context."
  4374. (interactive "P")
  4375. (if (org-at-table-p)
  4376. (org-call-with-arg 'org-table-sort-lines with-case)
  4377. (org-call-with-arg 'org-sort-entries with-case)))
  4378. (defun org-sort-entries (&optional with-case sorting-type)
  4379. "Sort entries on a certain level of an outline tree.
  4380. If there is an active region, the entries in the region are sorted.
  4381. If not, the children of the entry at point are sorted.
  4382. Sorting can be alphabetically, numerically, and by date/time as given by
  4383. the first time stamp in the entry. The command prompts for the sorting
  4384. type unless it has been given to the function through the SORTING-TYPE
  4385. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T).
  4386. Comparing entries ignores case by default. However, with an optional argument
  4387. WITH-CASE, the sorting considers case as well. With two prefix arguments
  4388. `C-u C-u', sorting is case-sensitive and duplicate entries will be removed."
  4389. (interactive "P")
  4390. (let ((unique (equal with-case '(16)))
  4391. start beg end entries stars re re2 p nentries (nremoved 0) last txt)
  4392. ;; Find beginning and end of region to sort
  4393. (if (org-region-active-p)
  4394. (progn
  4395. ;; we will sort the region
  4396. (setq end (region-end))
  4397. (goto-char (1- (setq start (region-beginning)))))
  4398. ;; we will sort the children of the current headline
  4399. (setq start (point) end (org-end-of-subtree))
  4400. (goto-char start)
  4401. (show-subtree))
  4402. (outline-next-heading) ; this is the first heading to be included
  4403. (setq beg (point))
  4404. (if (>= (point) end) (error "Nothing to sort"))
  4405. (looking-at "\\(\\*+\\)")
  4406. (setq stars (match-string 1)
  4407. re (concat "^" (regexp-quote stars) " +")
  4408. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  4409. txt (buffer-substring beg end))
  4410. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  4411. (if (string-match re2 txt)
  4412. (error "Region to sort contains a level above the first entry"))
  4413. ;; Make a list that can be sorted.
  4414. ;; The car is the string for comparison, the cdr is the subtree
  4415. (message "Sorting entries...")
  4416. (setq entries
  4417. (mapcar
  4418. (lambda (x)
  4419. (string-match "^.*\\(\n.*\\)?" x) ; take two lines
  4420. (cons (match-string 0 x) x))
  4421. (org-split-string txt re)))
  4422. ;; Sort the list
  4423. (setq entries (org-do-sort
  4424. entries
  4425. (if (org-region-active-p) "region" "children")
  4426. with-case sorting-type))
  4427. ;; Delete the old stuff
  4428. (goto-char beg)
  4429. (kill-region beg end)
  4430. (setq nentries (length entries))
  4431. ;; Insert the sorted entries, and remove duplicates if this is required
  4432. (while (setq p (pop entries))
  4433. (if (and unique (equal last (setq last (org-trim (cdr p)))))
  4434. (setq nremoved (1+ nremoved)) ; same entry as before, skip it
  4435. (insert stars " " (cdr p))))
  4436. (goto-char start)
  4437. (message "Sorting entries...done (%d entries%s)"
  4438. nentries
  4439. (if unique (format ", %d duplicates removed" nremoved) ""))))
  4440. (defun org-do-sort (table what &optional with-case sorting-type)
  4441. "Sort TABLE of WHAT according to SORTING-TYPE.
  4442. The user will be prompted for the SORTING-TYPE if the call to this
  4443. function does not specify it. WHAT is only for the prompt, to indicate
  4444. what is being sorted. The sorting key will be extracted from
  4445. the car of the elements of the table.
  4446. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  4447. (unless sorting-type
  4448. (message
  4449. "Sort %s:[a]lphabetically [n]umerically [t]ime. A/N/T means reversed:"
  4450. what)
  4451. (setq sorting-type (read-char-exclusive)))
  4452. (let ((dcst (downcase sorting-type))
  4453. extractfun comparefun)
  4454. ;; Define the appropriate functions
  4455. (cond
  4456. ((= dcst ?n)
  4457. (setq extractfun 'string-to-number
  4458. comparefun (if (= dcst sorting-type) '< '>)))
  4459. ((= dcst ?a)
  4460. (setq extractfun (if with-case 'identity 'downcase)
  4461. comparefun (if (= dcst sorting-type)
  4462. 'string<
  4463. (lambda (a b) (and (not (string< a b))
  4464. (not (string= a b)))))))
  4465. ((= dcst ?t)
  4466. (setq extractfun
  4467. (lambda (x)
  4468. (if (string-match org-ts-regexp x)
  4469. (time-to-seconds
  4470. (org-time-string-to-time (match-string 0 x)))
  4471. 0))
  4472. comparefun (if (= dcst sorting-type) '< '>)))
  4473. (t (error "Invalid sorting type `%c'" sorting-type)))
  4474. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  4475. table)
  4476. (lambda (a b) (funcall comparefun (car a) (car b))))))
  4477. ;;;; Plain list items, including checkboxes
  4478. ;;; Plain list items
  4479. (defun org-at-item-p ()
  4480. "Is point in a line starting a hand-formatted item?"
  4481. (let ((llt org-plain-list-ordered-item-terminator))
  4482. (save-excursion
  4483. (goto-char (point-at-bol))
  4484. (looking-at
  4485. (cond
  4486. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4487. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4488. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4489. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  4490. (defun org-in-item-p ()
  4491. "It the cursor inside a plain list item.
  4492. Does not have to be the first line."
  4493. (save-excursion
  4494. (condition-case nil
  4495. (progn
  4496. (org-beginning-of-item)
  4497. (org-at-item-p)
  4498. t)
  4499. (error nil))))
  4500. (defun org-insert-item (&optional checkbox)
  4501. "Insert a new item at the current level.
  4502. Return t when things worked, nil when we are not in an item."
  4503. (when (save-excursion
  4504. (condition-case nil
  4505. (progn
  4506. (org-beginning-of-item)
  4507. (org-at-item-p)
  4508. (if (org-invisible-p) (error "Invisible item"))
  4509. t)
  4510. (error nil)))
  4511. (let* ((bul (match-string 0))
  4512. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  4513. (match-end 0)))
  4514. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  4515. pos)
  4516. (cond
  4517. ((and (org-at-item-p) (<= (point) eow))
  4518. ;; before the bullet
  4519. (beginning-of-line 1)
  4520. (open-line (if blank 2 1)))
  4521. ((<= (point) eow)
  4522. (beginning-of-line 1))
  4523. (t (newline (if blank 2 1))))
  4524. (insert bul (if checkbox "[ ]" ""))
  4525. (just-one-space)
  4526. (setq pos (point))
  4527. (end-of-line 1)
  4528. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  4529. (org-maybe-renumber-ordered-list)
  4530. (and checkbox (org-update-checkbox-count-maybe))
  4531. t))
  4532. ;;; Checkboxes
  4533. (defun org-at-item-checkbox-p ()
  4534. "Is point at a line starting a plain-list item with a checklet?"
  4535. (and (org-at-item-p)
  4536. (save-excursion
  4537. (goto-char (match-end 0))
  4538. (skip-chars-forward " \t")
  4539. (looking-at "\\[[ X]\\]"))))
  4540. (defun org-toggle-checkbox (&optional arg)
  4541. "Toggle the checkbox in the current line."
  4542. (interactive "P")
  4543. (catch 'exit
  4544. (let (beg end status (firstnew 'unknown))
  4545. (cond
  4546. ((org-region-active-p)
  4547. (setq beg (region-beginning) end (region-end)))
  4548. ((org-on-heading-p)
  4549. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  4550. ((org-at-item-checkbox-p)
  4551. (save-excursion
  4552. (replace-match (if (equal (match-string 0) "[ ]") "[X]" "[ ]") t t))
  4553. (throw 'exit t))
  4554. (t (error "Not at a checkbox or heading, and no active region")))
  4555. (save-excursion
  4556. (goto-char beg)
  4557. (while (< (point) end)
  4558. (when (org-at-item-checkbox-p)
  4559. (setq status (equal (match-string 0) "[X]"))
  4560. (when (eq firstnew 'unknown)
  4561. (setq firstnew (not status)))
  4562. (replace-match
  4563. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  4564. (beginning-of-line 2)))))
  4565. (org-update-checkbox-count-maybe))
  4566. (defun org-update-checkbox-count-maybe ()
  4567. "Update checkbox statistics unless turned off by user."
  4568. (when org-provide-checkbox-statistics
  4569. (org-update-checkbox-count)))
  4570. (defun org-update-checkbox-count (&optional all)
  4571. "Update the checkbox statistics in the current section.
  4572. This will find all statistic cookies like [57%] and [6/12] and update them
  4573. with the current numbers. With optional prefix argument ALL, do this for
  4574. the whole buffer."
  4575. (interactive "P")
  4576. (save-excursion
  4577. (let* ((buffer-invisibility-spec nil) ; Emacs 21 compatibility
  4578. (beg (progn (outline-back-to-heading) (point)))
  4579. (end (move-marker (make-marker)
  4580. (progn (outline-next-heading) (point))))
  4581. (re "\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)")
  4582. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[ X]\\]\\)")
  4583. b1 e1 f1 c-on c-off lim (cstat 0))
  4584. (when all
  4585. (goto-char (point-min))
  4586. (outline-next-heading)
  4587. (setq beg (point) end (point-max)))
  4588. (goto-char beg)
  4589. (while (re-search-forward re end t)
  4590. (setq cstat (1+ cstat)
  4591. b1 (match-beginning 0)
  4592. e1 (match-end 0)
  4593. f1 (match-beginning 1)
  4594. lim (cond
  4595. ((org-on-heading-p) (outline-next-heading) (point))
  4596. ((org-at-item-p) (org-end-of-item) (point))
  4597. (t nil))
  4598. c-on 0 c-off 0)
  4599. (goto-char e1)
  4600. (when lim
  4601. (while (re-search-forward re-box lim t)
  4602. (if (equal (match-string 2) "[ ]")
  4603. (setq c-off (1+ c-off))
  4604. (setq c-on (1+ c-on))))
  4605. (delete-region b1 e1)
  4606. (goto-char b1)
  4607. (insert (if f1
  4608. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  4609. (format "[%d/%d]" c-on (+ c-on c-off))))))
  4610. (when (interactive-p)
  4611. (message "Checkbox satistics updated %s (%d places)"
  4612. (if all "in entire file" "in current outline entry") cstat)))))
  4613. (defun org-get-checkbox-statistics-face ()
  4614. "Select the face for checkbox statistics.
  4615. The face will be `org-done' when all relevant boxes are checked. Otherwise
  4616. it will be `org-todo'."
  4617. (if (match-end 1)
  4618. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  4619. (if (and (> (match-end 2) (match-beginning 2))
  4620. (equal (match-string 2) (match-string 3)))
  4621. 'org-done
  4622. 'org-todo)))
  4623. (defun org-get-indentation (&optional line)
  4624. "Get the indentation of the current line, interpreting tabs.
  4625. When LINE is given, assume it represents a line and compute its indentation."
  4626. (if line
  4627. (if (string-match "^ *" (org-remove-tabs line))
  4628. (match-end 0))
  4629. (save-excursion
  4630. (beginning-of-line 1)
  4631. (skip-chars-forward " \t")
  4632. (current-column))))
  4633. (defun org-remove-tabs (s &optional width)
  4634. "Replace tabulators in S with spaces.
  4635. Assumes that s is a single line, starting in column 0."
  4636. (setq width (or width tab-width))
  4637. (while (string-match "\t" s)
  4638. (setq s (replace-match
  4639. (make-string
  4640. (- (* width (/ (+ (match-beginning 0) width) width))
  4641. (match-beginning 0)) ?\ )
  4642. t t s)))
  4643. s)
  4644. (defun org-fix-indentation (line ind)
  4645. "Fix indentation in LINE.
  4646. IND is a cons cell with target and minimum indentation.
  4647. If the current indenation in LINE is smaller than the minimum,
  4648. leave it alone. If it is larger than ind, set it to the target."
  4649. (let* ((l (org-remove-tabs line))
  4650. (i (org-get-indentation l))
  4651. (i1 (car ind)) (i2 (cdr ind)))
  4652. (if (>= i i2) (setq l (substring line i2)))
  4653. (if (> i1 0)
  4654. (concat (make-string i1 ?\ ) l)
  4655. l)))
  4656. (defun org-beginning-of-item ()
  4657. "Go to the beginning of the current hand-formatted item.
  4658. If the cursor is not in an item, throw an error."
  4659. (interactive)
  4660. (let ((pos (point))
  4661. (limit (save-excursion (org-back-to-heading)
  4662. (beginning-of-line 2) (point)))
  4663. ind ind1)
  4664. (if (org-at-item-p)
  4665. (beginning-of-line 1)
  4666. (beginning-of-line 1)
  4667. (skip-chars-forward " \t")
  4668. (setq ind (current-column))
  4669. (if (catch 'exit
  4670. (while t
  4671. (beginning-of-line 0)
  4672. (if (< (point) limit) (throw 'exit nil))
  4673. (unless (looking-at "[ \t]*$")
  4674. (skip-chars-forward " \t")
  4675. (setq ind1 (current-column))
  4676. (if (< ind1 ind)
  4677. (throw 'exit (org-at-item-p))))))
  4678. nil
  4679. (goto-char pos)
  4680. (error "Not in an item")))))
  4681. (defun org-end-of-item ()
  4682. "Go to the end of the current hand-formatted item.
  4683. If the cursor is not in an item, throw an error."
  4684. (interactive)
  4685. (let ((pos (point))
  4686. (limit (save-excursion (outline-next-heading) (point)))
  4687. (ind (save-excursion
  4688. (org-beginning-of-item)
  4689. (skip-chars-forward " \t")
  4690. (current-column)))
  4691. ind1)
  4692. (if (catch 'exit
  4693. (while t
  4694. (beginning-of-line 2)
  4695. (if (>= (point) limit) (throw 'exit t))
  4696. (unless (looking-at "[ \t]*$")
  4697. (skip-chars-forward " \t")
  4698. (setq ind1 (current-column))
  4699. (if (<= ind1 ind) (throw 'exit t)))))
  4700. (beginning-of-line 1)
  4701. (goto-char pos)
  4702. (error "Not in an item"))))
  4703. (defun org-next-item ()
  4704. "Move to the beginning of the next item in the current plain list.
  4705. Error if not at a plain list, or if this is the last item in the list."
  4706. (interactive)
  4707. (let (ind ind1 (pos (point)))
  4708. (org-beginning-of-item)
  4709. (setq ind (org-get-indentation))
  4710. (org-end-of-item)
  4711. (setq ind1 (org-get-indentation))
  4712. (unless (and (org-at-item-p) (= ind ind1))
  4713. (goto-char pos)
  4714. (error "On last item"))))
  4715. (defun org-previous-item ()
  4716. "Move to the beginning of the previous item in the current plain list.
  4717. Error if not at a plain list, or if this is the last item in the list."
  4718. (interactive)
  4719. (let (beg ind (pos (point)))
  4720. (org-beginning-of-item)
  4721. (setq beg (point))
  4722. (setq ind (org-get-indentation))
  4723. (goto-char beg)
  4724. (catch 'exit
  4725. (while t
  4726. (beginning-of-line 0)
  4727. (if (looking-at "[ \t]*$")
  4728. nil
  4729. (if (<= (org-get-indentation) ind)
  4730. (throw 'exit t)))))
  4731. (condition-case nil
  4732. (org-beginning-of-item)
  4733. (error (goto-char pos)
  4734. (error "On first item")))))
  4735. (defun org-move-item-down ()
  4736. "Move the plain list item at point down, i.e. swap with following item.
  4737. Subitems (items with larger indentation) are considered part of the item,
  4738. so this really moves item trees."
  4739. (interactive)
  4740. (let (beg end ind ind1 (pos (point)) txt)
  4741. (org-beginning-of-item)
  4742. (setq beg (point))
  4743. (setq ind (org-get-indentation))
  4744. (org-end-of-item)
  4745. (setq end (point))
  4746. (setq ind1 (org-get-indentation))
  4747. (if (and (org-at-item-p) (= ind ind1))
  4748. (progn
  4749. (org-end-of-item)
  4750. (setq txt (buffer-substring beg end))
  4751. (save-excursion
  4752. (delete-region beg end))
  4753. (setq pos (point))
  4754. (insert txt)
  4755. (goto-char pos)
  4756. (org-maybe-renumber-ordered-list))
  4757. (goto-char pos)
  4758. (error "Cannot move this item further down"))))
  4759. (defun org-move-item-up (arg)
  4760. "Move the plain list item at point up, i.e. swap with previous item.
  4761. Subitems (items with larger indentation) are considered part of the item,
  4762. so this really moves item trees."
  4763. (interactive "p")
  4764. (let (beg end ind ind1 (pos (point)) txt)
  4765. (org-beginning-of-item)
  4766. (setq beg (point))
  4767. (setq ind (org-get-indentation))
  4768. (org-end-of-item)
  4769. (setq end (point))
  4770. (goto-char beg)
  4771. (catch 'exit
  4772. (while t
  4773. (beginning-of-line 0)
  4774. (if (looking-at "[ \t]*$")
  4775. nil
  4776. (if (<= (setq ind1 (org-get-indentation)) ind)
  4777. (throw 'exit t)))))
  4778. (condition-case nil
  4779. (org-beginning-of-item)
  4780. (error (goto-char beg)
  4781. (error "Cannot move this item further up")))
  4782. (setq ind1 (org-get-indentation))
  4783. (if (and (org-at-item-p) (= ind ind1))
  4784. (progn
  4785. (setq txt (buffer-substring beg end))
  4786. (save-excursion
  4787. (delete-region beg end))
  4788. (setq pos (point))
  4789. (insert txt)
  4790. (goto-char pos)
  4791. (org-maybe-renumber-ordered-list))
  4792. (goto-char pos)
  4793. (error "Cannot move this item further up"))))
  4794. (defun org-maybe-renumber-ordered-list ()
  4795. "Renumber the ordered list at point if setup allows it.
  4796. This tests the user option `org-auto-renumber-ordered-lists' before
  4797. doing the renumbering."
  4798. (and org-auto-renumber-ordered-lists
  4799. (org-at-item-p)
  4800. (match-beginning 3)
  4801. (org-renumber-ordered-list 1)))
  4802. (defun org-get-string-indentation (s)
  4803. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  4804. (let ((n -1) (i 0) (w tab-width) c)
  4805. (catch 'exit
  4806. (while (< (setq n (1+ n)) (length s))
  4807. (setq c (aref s n))
  4808. (cond ((= c ?\ ) (setq i (1+ i)))
  4809. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  4810. (t (throw 'exit t)))))
  4811. i))
  4812. (defun org-renumber-ordered-list (arg)
  4813. "Renumber an ordered plain list.
  4814. Cursor needs to be in the first line of an item, the line that starts
  4815. with something like \"1.\" or \"2)\"."
  4816. (interactive "p")
  4817. (unless (and (org-at-item-p)
  4818. (match-beginning 3))
  4819. (error "This is not an ordered list"))
  4820. (let ((line (org-current-line))
  4821. (col (current-column))
  4822. (ind (org-get-string-indentation
  4823. (buffer-substring (point-at-bol) (match-beginning 3))))
  4824. ;; (term (substring (match-string 3) -1))
  4825. ind1 (n (1- arg)))
  4826. ;; find where this list begins
  4827. (catch 'exit
  4828. (while t
  4829. (catch 'next
  4830. (beginning-of-line 0)
  4831. (if (looking-at "[ \t]*$") (throw 'next t))
  4832. (skip-chars-forward " \t") (setq ind1 (current-column))
  4833. (if (or (< ind1 ind)
  4834. (and (= ind1 ind)
  4835. (not (org-at-item-p))))
  4836. (throw 'exit t)))))
  4837. ;; Walk forward and replace these numbers
  4838. (catch 'exit
  4839. (while t
  4840. (catch 'next
  4841. (beginning-of-line 2)
  4842. (if (eobp) (throw 'exit nil))
  4843. (if (looking-at "[ \t]*$") (throw 'next nil))
  4844. (skip-chars-forward " \t") (setq ind1 (current-column))
  4845. (if (> ind1 ind) (throw 'next t))
  4846. (if (< ind1 ind) (throw 'exit t))
  4847. (if (not (org-at-item-p)) (throw 'exit nil))
  4848. (if (not (match-beginning 3))
  4849. (error "unordered bullet in ordered list. Press \\[undo] to recover"))
  4850. (delete-region (match-beginning 3) (1- (match-end 3)))
  4851. (goto-char (match-beginning 3))
  4852. (insert (format "%d" (setq n (1+ n)))))))
  4853. (goto-line line)
  4854. (move-to-column col)))
  4855. (defvar org-last-indent-begin-marker (make-marker))
  4856. (defvar org-last-indent-end-marker (make-marker))
  4857. (defun org-outdent-item (arg)
  4858. "Outdent a local list item."
  4859. (interactive "p")
  4860. (org-indent-item (- arg)))
  4861. (defun org-indent-item (arg)
  4862. "Indent a local list item."
  4863. (interactive "p")
  4864. (unless (org-at-item-p)
  4865. (error "Not on an item"))
  4866. (save-excursion
  4867. (let (beg end ind ind1)
  4868. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  4869. (setq beg org-last-indent-begin-marker
  4870. end org-last-indent-end-marker)
  4871. (org-beginning-of-item)
  4872. (setq beg (move-marker org-last-indent-begin-marker (point)))
  4873. (org-end-of-item)
  4874. (setq end (move-marker org-last-indent-end-marker (point))))
  4875. (goto-char beg)
  4876. (skip-chars-forward " \t") (setq ind (current-column))
  4877. (if (< (+ arg ind) 0) (error "Cannot outdent beyond margin"))
  4878. (while (< (point) end)
  4879. (beginning-of-line 1)
  4880. (skip-chars-forward " \t") (setq ind1 (current-column))
  4881. (delete-region (point-at-bol) (point))
  4882. (indent-to-column (+ ind1 arg))
  4883. (beginning-of-line 2)))))
  4884. ;;;; Archiving
  4885. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  4886. (defun org-archive-subtree (&optional find-done)
  4887. "Move the current subtree to the archive.
  4888. The archive can be a certain top-level heading in the current file, or in
  4889. a different file. The tree will be moved to that location, the subtree
  4890. heading be marked DONE, and the current time will be added.
  4891. When called with prefix argument FIND-DONE, find whole trees without any
  4892. open TODO items and archive them (after getting confirmation from the user).
  4893. If the cursor is not at a headline when this comand is called, try all level
  4894. 1 trees. If the cursor is on a headline, only try the direct children of
  4895. this heading. "
  4896. (interactive "P")
  4897. (if find-done
  4898. (org-archive-all-done)
  4899. ;; Save all relevant TODO keyword-relatex variables
  4900. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  4901. (tr-org-todo-keywords org-todo-keywords)
  4902. (tr-org-todo-interpretation org-todo-interpretation)
  4903. (tr-org-done-string org-done-string)
  4904. (tr-org-todo-regexp org-todo-regexp)
  4905. (tr-org-todo-line-regexp org-todo-line-regexp)
  4906. (this-buffer (current-buffer))
  4907. (org-archive-location org-archive-location)
  4908. (re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
  4909. file heading buffer level newfile-p)
  4910. ;; Try to find a local archive location
  4911. (save-excursion
  4912. (if (or (re-search-backward re nil t) (re-search-forward re nil t))
  4913. (setq org-archive-location (match-string 1))))
  4914. (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
  4915. (progn
  4916. (setq file (format (match-string 1 org-archive-location)
  4917. (file-name-nondirectory buffer-file-name))
  4918. heading (match-string 2 org-archive-location)))
  4919. (error "Invalid `org-archive-location'"))
  4920. (if (> (length file) 0)
  4921. (setq newfile-p (not (file-exists-p file))
  4922. buffer (find-file-noselect file))
  4923. (setq buffer (current-buffer)))
  4924. (unless buffer
  4925. (error "Cannot access file \"%s\"" file))
  4926. (if (and (> (length heading) 0)
  4927. (string-match "^\\*+" heading))
  4928. (setq level (match-end 0))
  4929. (setq heading nil level 0))
  4930. (save-excursion
  4931. ;; We first only copy, in case something goes wrong
  4932. ;; we need to protect this-command, to avoid kill-region sets it,
  4933. ;; which would lead to duplication of subtrees
  4934. (let (this-command) (org-copy-subtree))
  4935. (set-buffer buffer)
  4936. ;; Enforce org-mode for the archive buffer
  4937. (if (not (org-mode-p))
  4938. ;; Force the mode for future visits.
  4939. (let ((org-insert-mode-line-in-empty-file t))
  4940. (call-interactively 'org-mode)))
  4941. (when newfile-p
  4942. (goto-char (point-max))
  4943. (insert (format "\nArchived entries from file %s\n\n"
  4944. (buffer-file-name this-buffer))))
  4945. ;; Force the TODO keywords of the original buffer
  4946. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  4947. (org-todo-keywords tr-org-todo-keywords)
  4948. (org-todo-interpretation tr-org-todo-interpretation)
  4949. (org-done-string tr-org-done-string)
  4950. (org-todo-regexp tr-org-todo-regexp)
  4951. (org-todo-line-regexp tr-org-todo-line-regexp))
  4952. (goto-char (point-min))
  4953. (if heading
  4954. (progn
  4955. (if (re-search-forward
  4956. (concat "\\(^\\|\r\\)"
  4957. (regexp-quote heading) "[ \t]*\\($\\|\r\\)")
  4958. nil t)
  4959. (goto-char (match-end 0))
  4960. ;; Heading not found, just insert it at the end
  4961. (goto-char (point-max))
  4962. (or (bolp) (insert "\n"))
  4963. (insert "\n" heading "\n")
  4964. (end-of-line 0))
  4965. ;; Make the subtree visible
  4966. (show-subtree)
  4967. (org-end-of-subtree t)
  4968. (skip-chars-backward " \t\r\n")
  4969. (and (looking-at "[ \t\r\n]*")
  4970. (replace-match "\n\n")))
  4971. ;; No specific heading, just go to end of file.
  4972. (goto-char (point-max)) (insert "\n"))
  4973. ;; Paste
  4974. (org-paste-subtree (1+ level))
  4975. ;; Mark the entry as done, i.e. set to last work in org-todo-keywords
  4976. (if org-archive-mark-done
  4977. (let (org-log-done)
  4978. (org-todo (length org-todo-keywords))))
  4979. ;; Move cursor to right after the TODO keyword
  4980. (when org-archive-stamp-time
  4981. (beginning-of-line 1)
  4982. (looking-at org-todo-line-regexp)
  4983. (goto-char (or (match-end 2) (match-beginning 3)))
  4984. (org-insert-time-stamp (org-current-time) t t "(" ")"))
  4985. ;; Save the buffer, if it is not the same buffer.
  4986. (if (not (eq this-buffer buffer)) (save-buffer))))
  4987. ;; Here we are back in the original buffer. Everything seems to have
  4988. ;; worked. So now cut the tree and finish up.
  4989. (let (this-command) (org-cut-subtree))
  4990. (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
  4991. (message "Subtree archived %s"
  4992. (if (eq this-buffer buffer)
  4993. (concat "under heading: " heading)
  4994. (concat "in file: " (abbreviate-file-name file)))))))
  4995. (defun org-archive-all-done (&optional tag)
  4996. "Archive sublevels of the current tree without open TODO items.
  4997. If the cursor is not on a headline, try all level 1 trees. If
  4998. it is on a headline, try all direct children.
  4999. When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
  5000. (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
  5001. (rea (concat ".*:" org-archive-tag ":"))
  5002. (begm (make-marker))
  5003. (endm (make-marker))
  5004. (question (if tag "Set ARCHIVE tag (no open TODO items)? "
  5005. "Move subtree to archive (no open TODO items)? "))
  5006. beg end (cntarch 0))
  5007. (if (org-on-heading-p)
  5008. (progn
  5009. (setq re1 (concat "^" (regexp-quote
  5010. (make-string
  5011. (1+ (- (match-end 0) (match-beginning 0)))
  5012. ?*))
  5013. " "))
  5014. (move-marker begm (point))
  5015. (move-marker endm (org-end-of-subtree t)))
  5016. (setq re1 "^* ")
  5017. (move-marker begm (point-min))
  5018. (move-marker endm (point-max)))
  5019. (save-excursion
  5020. (goto-char begm)
  5021. (while (re-search-forward re1 endm t)
  5022. (setq beg (match-beginning 0)
  5023. end (save-excursion (org-end-of-subtree t) (point)))
  5024. (goto-char beg)
  5025. (if (re-search-forward re end t)
  5026. (goto-char end)
  5027. (goto-char beg)
  5028. (if (and (or (not tag) (not (looking-at rea)))
  5029. (y-or-n-p question))
  5030. (progn
  5031. (if tag
  5032. (org-toggle-tag org-archive-tag 'on)
  5033. (org-archive-subtree))
  5034. (setq cntarch (1+ cntarch)))
  5035. (goto-char end)))))
  5036. (message "%d trees archived" cntarch)))
  5037. (defun org-cycle-hide-archived-subtrees (state)
  5038. "Re-hide all archived subtrees after a visibility state change."
  5039. (when (and (not org-cycle-open-archived-trees)
  5040. (not (memq state '(overview folded))))
  5041. (save-excursion
  5042. (let* ((globalp (memq state '(contents all)))
  5043. (beg (if globalp (point-min) (point)))
  5044. (end (if globalp (point-max) (org-end-of-subtree t))))
  5045. (org-hide-archived-subtrees beg end)
  5046. (goto-char beg)
  5047. (if (looking-at (concat ".*:" org-archive-tag ":"))
  5048. (message (substitute-command-keys
  5049. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  5050. (defun org-force-cycle-archived ()
  5051. "Cycle subtree even if it is archived."
  5052. (interactive)
  5053. (setq this-command 'org-cycle)
  5054. (let ((org-cycle-open-archived-trees t))
  5055. (call-interactively 'org-cycle)))
  5056. (defun org-hide-archived-subtrees (beg end)
  5057. "Re-hide all archived subtrees after a visibility state change."
  5058. (save-excursion
  5059. (let* ((re (concat ":" org-archive-tag ":")))
  5060. (goto-char beg)
  5061. (while (re-search-forward re end t)
  5062. (and (org-on-heading-p) (hide-subtree))
  5063. (org-end-of-subtree t)))))
  5064. (defun org-toggle-tag (tag &optional onoff)
  5065. "Toggle the tag TAG for the current line.
  5066. If ONOFF is `on' or `off', don't toggle but set to this state."
  5067. (unless (org-on-heading-p) (error "Not on headling"))
  5068. (let (res current)
  5069. (save-excursion
  5070. (beginning-of-line)
  5071. (if (re-search-forward "[ \t]:\\([a-zA-Z0-9_@:]+\\):[ \t]*$"
  5072. (point-at-eol) t)
  5073. (progn
  5074. (setq current (match-string 1))
  5075. (replace-match ""))
  5076. (setq current ""))
  5077. (setq current (nreverse (org-split-string current ":")))
  5078. (cond
  5079. ((eq onoff 'on)
  5080. (setq res t)
  5081. (or (member tag current) (push tag current)))
  5082. ((eq onoff 'off)
  5083. (or (not (member tag current)) (setq current (delete tag current))))
  5084. (t (if (member tag current)
  5085. (setq current (delete tag current))
  5086. (setq res t)
  5087. (push tag current))))
  5088. (end-of-line 1)
  5089. (when current
  5090. (insert " :" (mapconcat 'identity (nreverse current) ":") ":"))
  5091. (org-set-tags nil t))
  5092. res))
  5093. (defun org-toggle-archive-tag (&optional arg)
  5094. "Toggle the archive tag for the current headline.
  5095. With prefix ARG, check all children of current headline and offer tagging
  5096. the children that do not contain any open TODO items."
  5097. (interactive "P")
  5098. (if arg
  5099. (org-archive-all-done 'tag)
  5100. (let (set)
  5101. (save-excursion
  5102. (org-back-to-heading t)
  5103. (setq set (org-toggle-tag org-archive-tag))
  5104. (when set (hide-subtree)))
  5105. (and set (beginning-of-line 1))
  5106. (message "Subtree %s" (if set "archived" "unarchived")))))
  5107. ;;;; Tables
  5108. ;;; The table editor
  5109. ;; Watch out: Here we are talking about two different kind of tables.
  5110. ;; Most of the code is for the tables created with the Org-mode table editor.
  5111. ;; Sometimes, we talk about tables created and edited with the table.el
  5112. ;; Emacs package. We call the former org-type tables, and the latter
  5113. ;; table.el-type tables.
  5114. (defun org-before-change-function (beg end)
  5115. "Every change indicates that a table might need an update."
  5116. (setq org-table-may-need-update t))
  5117. (defconst org-table-line-regexp "^[ \t]*|"
  5118. "Detects an org-type table line.")
  5119. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  5120. "Detects an org-type table line.")
  5121. (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
  5122. "Detects a table line marked for automatic recalculation.")
  5123. (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
  5124. "Detects a table line marked for automatic recalculation.")
  5125. (defconst org-table-calculate-mark-regexp "^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
  5126. "Detects a table line marked for automatic recalculation.")
  5127. (defconst org-table-hline-regexp "^[ \t]*|-"
  5128. "Detects an org-type table hline.")
  5129. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  5130. "Detects a table-type table hline.")
  5131. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  5132. "Detects an org-type or table-type table.")
  5133. (defconst org-table-border-regexp "^[ \t]*[^| \t]"
  5134. "Searching from within a table (any type) this finds the first line
  5135. outside the table.")
  5136. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  5137. "Searching from within a table (any type) this finds the first line
  5138. outside the table.")
  5139. (defvar org-table-last-highlighted-reference nil)
  5140. (defvar org-table-formula-history nil)
  5141. (defvar org-table-column-names nil
  5142. "Alist with column names, derived from the `!' line.")
  5143. (defvar org-table-column-name-regexp nil
  5144. "Regular expression matching the current column names.")
  5145. (defvar org-table-local-parameters nil
  5146. "Alist with parameter names, derived from the `$' line.")
  5147. (defvar org-table-named-field-locations nil
  5148. "Alist with locations of named fields.")
  5149. (defvar org-table-current-line-types nil
  5150. "Table row types, non-nil only for the duration of a comand.")
  5151. (defvar org-table-current-begin-line nil
  5152. "Table begin line, non-nil only for the duration of a comand.")
  5153. (defvar org-table-dlines nil
  5154. "Vector of data line line numbers in the current table.")
  5155. (defvar org-table-hlines nil
  5156. "Vector of hline line numbers in the current table.")
  5157. (defconst org-table-range-regexp
  5158. "@\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\(\\.\\.@?\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\)?"
  5159. ;; 1 2 3 4 5
  5160. "Regular expression for matching ranges in formulas.")
  5161. (defconst org-table-range-regexp2
  5162. "@\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[a-zA-Z0-9]+\\)?\\(\\.\\.@?\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[a-zA-Z0-9]+\\)?\\)?\\|\\$[a-zA-Z0-9]+\\.\\.\\$[a-zA-Z0-9]+"
  5163. "Regular expression to recognize ranges in formulas for highlighting.")
  5164. (defvar org-inhibit-highlight-removal nil)
  5165. (defun org-table-create-with-table.el ()
  5166. "Use the table.el package to insert a new table.
  5167. If there is already a table at point, convert between Org-mode tables
  5168. and table.el tables."
  5169. (interactive)
  5170. (require 'table)
  5171. (cond
  5172. ((org-at-table.el-p)
  5173. (if (y-or-n-p "Convert table to Org-mode table? ")
  5174. (org-table-convert)))
  5175. ((org-at-table-p)
  5176. (if (y-or-n-p "Convert table to table.el table? ")
  5177. (org-table-convert)))
  5178. (t (call-interactively 'table-insert))))
  5179. (defun org-table-create-or-convert-from-region (arg)
  5180. "Convert region to table, or create an empty table.
  5181. If there is an active region, convert it to a table. If there is no such
  5182. region, create an empty table."
  5183. (interactive "P")
  5184. (if (org-region-active-p)
  5185. (org-table-convert-region (region-beginning) (region-end) arg)
  5186. (org-table-create arg)))
  5187. (defun org-table-create (&optional size)
  5188. "Query for a size and insert a table skeleton.
  5189. SIZE is a string Columns x Rows like for example \"3x2\"."
  5190. (interactive "P")
  5191. (unless size
  5192. (setq size (read-string
  5193. (concat "Table size Columns x Rows [e.g. "
  5194. org-table-default-size "]: ")
  5195. "" nil org-table-default-size)))
  5196. (let* ((pos (point))
  5197. (indent (make-string (current-column) ?\ ))
  5198. (split (org-split-string size " *x *"))
  5199. (rows (string-to-number (nth 1 split)))
  5200. (columns (string-to-number (car split)))
  5201. (line (concat (apply 'concat indent "|" (make-list columns " |"))
  5202. "\n")))
  5203. (if (string-match "^[ \t]*$" (buffer-substring-no-properties
  5204. (point-at-bol) (point)))
  5205. (beginning-of-line 1)
  5206. (newline))
  5207. ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
  5208. (dotimes (i rows) (insert line))
  5209. (goto-char pos)
  5210. (if (> rows 1)
  5211. ;; Insert a hline after the first row.
  5212. (progn
  5213. (end-of-line 1)
  5214. (insert "\n|-")
  5215. (goto-char pos)))
  5216. (org-table-align)))
  5217. (defun org-table-convert-region (beg0 end0 &optional nspace)
  5218. "Convert region to a table.
  5219. The region goes from BEG0 to END0, but these borders will be moved
  5220. slightly, to make sure a beginning of line in the first line is included.
  5221. When NSPACE is non-nil, it indicates the minimum number of spaces that
  5222. separate columns (default: just one space)."
  5223. (interactive "rP")
  5224. (let* ((beg (min beg0 end0))
  5225. (end (max beg0 end0))
  5226. (tabsep t)
  5227. re)
  5228. (goto-char beg)
  5229. (beginning-of-line 1)
  5230. (setq beg (move-marker (make-marker) (point)))
  5231. (goto-char end)
  5232. (if (bolp) (backward-char 1) (end-of-line 1))
  5233. (setq end (move-marker (make-marker) (point)))
  5234. ;; Lets see if this is tab-separated material. If every nonempty line
  5235. ;; contains a tab, we will assume that it is tab-separated material
  5236. (if nspace
  5237. (setq tabsep nil)
  5238. (goto-char beg)
  5239. (and (re-search-forward "^[^\n\t]+$" end t) (setq tabsep nil)))
  5240. (if nspace (setq tabsep nil))
  5241. (if tabsep
  5242. (setq re "^\\|\t")
  5243. (setq re (format "^ *\\| *\t *\\| \\{%d,\\}"
  5244. (max 1 (prefix-numeric-value nspace)))))
  5245. (goto-char beg)
  5246. (while (re-search-forward re end t)
  5247. (replace-match "| " t t))
  5248. (goto-char beg)
  5249. (insert " ")
  5250. (org-table-align)))
  5251. (defun org-table-import (file arg)
  5252. "Import FILE as a table.
  5253. The file is assumed to be tab-separated. Such files can be produced by most
  5254. spreadsheet and database applications. If no tabs (at least one per line)
  5255. are found, lines will be split on whitespace into fields."
  5256. (interactive "f\nP")
  5257. (or (bolp) (newline))
  5258. (let ((beg (point))
  5259. (pm (point-max)))
  5260. (insert-file-contents file)
  5261. (org-table-convert-region beg (+ (point) (- (point-max) pm)) arg)))
  5262. (defun org-table-export ()
  5263. "Export table as a tab-separated file.
  5264. Such a file can be imported into a spreadsheet program like Excel."
  5265. (interactive)
  5266. (let* ((beg (org-table-begin))
  5267. (end (org-table-end))
  5268. (table (buffer-substring beg end))
  5269. (file (read-file-name "Export table to: "))
  5270. buf)
  5271. (unless (or (not (file-exists-p file))
  5272. (y-or-n-p (format "Overwrite file %s? " file)))
  5273. (error "Abort"))
  5274. (with-current-buffer (find-file-noselect file)
  5275. (setq buf (current-buffer))
  5276. (erase-buffer)
  5277. (fundamental-mode)
  5278. (insert table)
  5279. (goto-char (point-min))
  5280. (while (re-search-forward "^[ \t]*|[ \t]*" nil t)
  5281. (replace-match "" t t)
  5282. (end-of-line 1))
  5283. (goto-char (point-min))
  5284. (while (re-search-forward "[ \t]*|[ \t]*$" nil t)
  5285. (replace-match "" t t)
  5286. (goto-char (min (1+ (point)) (point-max))))
  5287. (goto-char (point-min))
  5288. (while (re-search-forward "^-[-+]*$" nil t)
  5289. (replace-match "")
  5290. (if (looking-at "\n")
  5291. (delete-char 1)))
  5292. (goto-char (point-min))
  5293. (while (re-search-forward "[ \t]*|[ \t]*" nil t)
  5294. (replace-match "\t" t t))
  5295. (save-buffer))
  5296. (kill-buffer buf)))
  5297. (defvar org-table-aligned-begin-marker (make-marker)
  5298. "Marker at the beginning of the table last aligned.
  5299. Used to check if cursor still is in that table, to minimize realignment.")
  5300. (defvar org-table-aligned-end-marker (make-marker)
  5301. "Marker at the end of the table last aligned.
  5302. Used to check if cursor still is in that table, to minimize realignment.")
  5303. (defvar org-table-last-alignment nil
  5304. "List of flags for flushright alignment, from the last re-alignment.
  5305. This is being used to correctly align a single field after TAB or RET.")
  5306. (defvar org-table-last-column-widths nil
  5307. "List of max width of fields in each column.
  5308. This is being used to correctly align a single field after TAB or RET.")
  5309. (defvar org-last-recalc-line nil)
  5310. (defconst org-narrow-column-arrow "=>"
  5311. "Used as display property in narrowed table columns.")
  5312. (defun org-table-align ()
  5313. "Align the table at point by aligning all vertical bars."
  5314. (interactive)
  5315. (let* (
  5316. ;; Limits of table
  5317. (beg (org-table-begin))
  5318. (end (org-table-end))
  5319. ;; Current cursor position
  5320. (linepos (org-current-line))
  5321. (colpos (org-table-current-column))
  5322. (winstart (window-start))
  5323. (winstartline (org-current-line (min winstart (1- (point-max)))))
  5324. lines (new "") lengths l typenums ty fields maxfields i
  5325. column
  5326. (indent "") cnt frac
  5327. rfmt hfmt
  5328. (spaces '(1 . 1))
  5329. (sp1 (car spaces))
  5330. (sp2 (cdr spaces))
  5331. (rfmt1 (concat
  5332. (make-string sp2 ?\ ) "%%%s%ds" (make-string sp1 ?\ ) "|"))
  5333. (hfmt1 (concat
  5334. (make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
  5335. emptystrings links dates narrow fmax f1 len c e)
  5336. (untabify beg end)
  5337. (remove-text-properties beg end '(org-cwidth t org-dwidth t display t))
  5338. ;; Check if we have links or dates
  5339. (goto-char beg)
  5340. (setq links (re-search-forward org-bracket-link-regexp end t))
  5341. (goto-char beg)
  5342. (setq dates (and org-display-custom-times
  5343. (re-search-forward org-ts-regexp-both end t)))
  5344. ;; Make sure the link properties are right
  5345. (when links (goto-char beg) (while (org-activate-bracket-links end)))
  5346. ;; Make sure the date properties are right
  5347. (when dates (goto-char beg) (while (org-activate-dates end)))
  5348. ;; Check if we are narrowing any columns
  5349. (goto-char beg)
  5350. (setq narrow (and org-format-transports-properties-p
  5351. (re-search-forward "<[0-9]+>" end t)))
  5352. ;; Get the rows
  5353. (setq lines (org-split-string
  5354. (buffer-substring beg end) "\n"))
  5355. ;; Store the indentation of the first line
  5356. (if (string-match "^ *" (car lines))
  5357. (setq indent (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  5358. ;; Mark the hlines by setting the corresponding element to nil
  5359. ;; At the same time, we remove trailing space.
  5360. (setq lines (mapcar (lambda (l)
  5361. (if (string-match "^ *|-" l)
  5362. nil
  5363. (if (string-match "[ \t]+$" l)
  5364. (substring l 0 (match-beginning 0))
  5365. l)))
  5366. lines))
  5367. ;; Get the data fields by splitting the lines.
  5368. (setq fields (mapcar
  5369. (lambda (l)
  5370. (org-split-string l " *| *"))
  5371. (delq nil (copy-sequence lines))))
  5372. ;; How many fields in the longest line?
  5373. (condition-case nil
  5374. (setq maxfields (apply 'max (mapcar 'length fields)))
  5375. (error
  5376. (kill-region beg end)
  5377. (org-table-create org-table-default-size)
  5378. (error "Empty table - created default table")))
  5379. ;; A list of empty strings to fill any short rows on output
  5380. (setq emptystrings (make-list maxfields ""))
  5381. ;; Check for special formatting.
  5382. (setq i -1)
  5383. (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns
  5384. (setq column (mapcar (lambda (x) (or (nth i x) "")) fields))
  5385. ;; Check if there is an explicit width specified
  5386. (when (and org-table-limit-column-width narrow)
  5387. (setq c column fmax nil)
  5388. (while c
  5389. (setq e (pop c))
  5390. (if (and (stringp e) (string-match "^<\\([0-9]+\\)>$" e))
  5391. (setq fmax (string-to-number (match-string 1 e)) c nil)))
  5392. ;; Find fields that are wider than fmax, and shorten them
  5393. (when fmax
  5394. (loop for xx in column do
  5395. (when (and (stringp xx)
  5396. (> (org-string-width xx) fmax))
  5397. (org-add-props xx nil
  5398. 'help-echo
  5399. (concat "Clipped table field, use C-c ` to edit. Full value is:\n" (org-no-properties (copy-sequence xx))))
  5400. (setq f1 (min fmax (or (string-match org-bracket-link-regexp xx) fmax)))
  5401. (unless (> f1 1)
  5402. (error "Cannot narrow field starting with wide link \"%s\""
  5403. (match-string 0 xx)))
  5404. (add-text-properties f1 (length xx) (list 'org-cwidth t) xx)
  5405. (add-text-properties (- f1 2) f1
  5406. (list 'display org-narrow-column-arrow)
  5407. xx)))))
  5408. ;; Get the maximum width for each column
  5409. (push (apply 'max 1 (mapcar 'org-string-width column)) lengths)
  5410. ;; Get the fraction of numbers, to decide about alignment of the column
  5411. (setq cnt 0 frac 0.0)
  5412. (loop for x in column do
  5413. (if (equal x "")
  5414. nil
  5415. (setq frac ( / (+ (* frac cnt)
  5416. (if (string-match org-table-number-regexp x) 1 0))
  5417. (setq cnt (1+ cnt))))))
  5418. (push (>= frac org-table-number-fraction) typenums))
  5419. (setq lengths (nreverse lengths) typenums (nreverse typenums))
  5420. ;; Store the alignment of this table, for later editing of single fields
  5421. (setq org-table-last-alignment typenums
  5422. org-table-last-column-widths lengths)
  5423. ;; With invisible characters, `format' does not get the field width right
  5424. ;; So we need to make these fields wide by hand.
  5425. (when links
  5426. (loop for i from 0 upto (1- maxfields) do
  5427. (setq len (nth i lengths))
  5428. (loop for j from 0 upto (1- (length fields)) do
  5429. (setq c (nthcdr i (car (nthcdr j fields))))
  5430. (if (and (stringp (car c))
  5431. (string-match org-bracket-link-regexp (car c))
  5432. (< (org-string-width (car c)) len))
  5433. (setcar c (concat (car c) (make-string (- len (org-string-width (car c))) ?\ )))))))
  5434. ;; Compute the formats needed for output of the table
  5435. (setq rfmt (concat indent "|") hfmt (concat indent "|"))
  5436. (while (setq l (pop lengths))
  5437. (setq ty (if (pop typenums) "" "-")) ; number types flushright
  5438. (setq rfmt (concat rfmt (format rfmt1 ty l))
  5439. hfmt (concat hfmt (format hfmt1 (make-string l ?-)))))
  5440. (setq rfmt (concat rfmt "\n")
  5441. hfmt (concat (substring hfmt 0 -1) "|\n"))
  5442. (setq new (mapconcat
  5443. (lambda (l)
  5444. (if l (apply 'format rfmt
  5445. (append (pop fields) emptystrings))
  5446. hfmt))
  5447. lines ""))
  5448. ;; Replace the old one
  5449. (delete-region beg end)
  5450. (move-marker end nil)
  5451. (move-marker org-table-aligned-begin-marker (point))
  5452. (insert new)
  5453. (move-marker org-table-aligned-end-marker (point))
  5454. (when (and orgtbl-mode (not (org-mode-p)))
  5455. (goto-char org-table-aligned-begin-marker)
  5456. (while (org-hide-wide-columns org-table-aligned-end-marker)))
  5457. ;; Try to move to the old location
  5458. (goto-line winstartline)
  5459. (setq winstart (point-at-bol))
  5460. (goto-line linepos)
  5461. (set-window-start (selected-window) winstart 'noforce)
  5462. (org-table-goto-column colpos)
  5463. (setq org-table-may-need-update nil)
  5464. ))
  5465. (defun org-string-width (s)
  5466. "Compute width of string, ignoring invisible characters.
  5467. This ignores character with invisibility property `org-link', and also
  5468. characters with property `org-cwidth', because these will become invisible
  5469. upon the next fontification round."
  5470. (let (b l)
  5471. (when (or (eq t buffer-invisibility-spec)
  5472. (assq 'org-link buffer-invisibility-spec))
  5473. (while (setq b (text-property-any 0 (length s)
  5474. 'invisible 'org-link s))
  5475. (setq s (concat (substring s 0 b)
  5476. (substring s (or (next-single-property-change
  5477. b 'invisible s) (length s)))))))
  5478. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  5479. (setq s (concat (substring s 0 b)
  5480. (substring s (or (next-single-property-change
  5481. b 'org-cwidth s) (length s))))))
  5482. (setq l (string-width s) b -1)
  5483. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  5484. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  5485. l))
  5486. (defun org-table-begin (&optional table-type)
  5487. "Find the beginning of the table and return its position.
  5488. With argument TABLE-TYPE, go to the beginning of a table.el-type table."
  5489. (save-excursion
  5490. (if (not (re-search-backward
  5491. (if table-type org-table-any-border-regexp
  5492. org-table-border-regexp)
  5493. nil t))
  5494. (progn (goto-char (point-min)) (point))
  5495. (goto-char (match-beginning 0))
  5496. (beginning-of-line 2)
  5497. (point))))
  5498. (defun org-table-end (&optional table-type)
  5499. "Find the end of the table and return its position.
  5500. With argument TABLE-TYPE, go to the end of a table.el-type table."
  5501. (save-excursion
  5502. (if (not (re-search-forward
  5503. (if table-type org-table-any-border-regexp
  5504. org-table-border-regexp)
  5505. nil t))
  5506. (goto-char (point-max))
  5507. (goto-char (match-beginning 0)))
  5508. (point-marker)))
  5509. (defun org-table-justify-field-maybe (&optional new)
  5510. "Justify the current field, text to left, number to right.
  5511. Optional argument NEW may specify text to replace the current field content."
  5512. (cond
  5513. ((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
  5514. ((org-at-table-hline-p))
  5515. ((and (not new)
  5516. (or (not (equal (marker-buffer org-table-aligned-begin-marker)
  5517. (current-buffer)))
  5518. (< (point) org-table-aligned-begin-marker)
  5519. (>= (point) org-table-aligned-end-marker)))
  5520. ;; This is not the same table, force a full re-align
  5521. (setq org-table-may-need-update t))
  5522. (t ;; realign the current field, based on previous full realign
  5523. (let* ((pos (point)) s
  5524. (col (org-table-current-column))
  5525. (num (if (> col 0) (nth (1- col) org-table-last-alignment)))
  5526. l f n o e)
  5527. (when (> col 0)
  5528. (skip-chars-backward "^|\n")
  5529. (if (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)")
  5530. (progn
  5531. (setq s (match-string 1)
  5532. o (match-string 0)
  5533. l (max 1 (- (match-end 0) (match-beginning 0) 3))
  5534. e (not (= (match-beginning 2) (match-end 2))))
  5535. (setq f (format (if num " %%%ds %s" " %%-%ds %s")
  5536. l (if e "|" (setq org-table-may-need-update t) ""))
  5537. n (format f s))
  5538. (if new
  5539. (if (<= (length new) l) ;; FIXME: length -> str-width?
  5540. (setq n (format f new))
  5541. (setq n (concat new "|") org-table-may-need-update t)))
  5542. (or (equal n o)
  5543. (let (org-table-may-need-update)
  5544. (replace-match n))))
  5545. (setq org-table-may-need-update t))
  5546. (goto-char pos))))))
  5547. (defun org-table-next-field ()
  5548. "Go to the next field in the current table, creating new lines as needed.
  5549. Before doing so, re-align the table if necessary."
  5550. (interactive)
  5551. (org-table-maybe-eval-formula)
  5552. (org-table-maybe-recalculate-line)
  5553. (if (and org-table-automatic-realign
  5554. org-table-may-need-update)
  5555. (org-table-align))
  5556. (let ((end (org-table-end)))
  5557. (if (org-at-table-hline-p)
  5558. (end-of-line 1))
  5559. (condition-case nil
  5560. (progn
  5561. (re-search-forward "|" end)
  5562. (if (looking-at "[ \t]*$")
  5563. (re-search-forward "|" end))
  5564. (if (and (looking-at "-")
  5565. org-table-tab-jumps-over-hlines
  5566. (re-search-forward "^[ \t]*|\\([^-]\\)" end t))
  5567. (goto-char (match-beginning 1)))
  5568. (if (looking-at "-")
  5569. (progn
  5570. (beginning-of-line 0)
  5571. (org-table-insert-row 'below))
  5572. (if (looking-at " ") (forward-char 1))))
  5573. (error
  5574. (org-table-insert-row 'below)))))
  5575. (defun org-table-previous-field ()
  5576. "Go to the previous field in the table.
  5577. Before doing so, re-align the table if necessary."
  5578. (interactive)
  5579. (org-table-justify-field-maybe)
  5580. (org-table-maybe-recalculate-line)
  5581. (if (and org-table-automatic-realign
  5582. org-table-may-need-update)
  5583. (org-table-align))
  5584. (if (org-at-table-hline-p)
  5585. (end-of-line 1))
  5586. (re-search-backward "|" (org-table-begin))
  5587. (re-search-backward "|" (org-table-begin))
  5588. (while (looking-at "|\\(-\\|[ \t]*$\\)")
  5589. (re-search-backward "|" (org-table-begin)))
  5590. (if (looking-at "| ?")
  5591. (goto-char (match-end 0))))
  5592. (defun org-table-next-row ()
  5593. "Go to the next row (same column) in the current table.
  5594. Before doing so, re-align the table if necessary."
  5595. (interactive)
  5596. (org-table-maybe-eval-formula)
  5597. (org-table-maybe-recalculate-line)
  5598. (if (or (looking-at "[ \t]*$")
  5599. (save-excursion (skip-chars-backward " \t") (bolp)))
  5600. (newline)
  5601. (if (and org-table-automatic-realign
  5602. org-table-may-need-update)
  5603. (org-table-align))
  5604. (let ((col (org-table-current-column)))
  5605. (beginning-of-line 2)
  5606. (if (or (not (org-at-table-p))
  5607. (org-at-table-hline-p))
  5608. (progn
  5609. (beginning-of-line 0)
  5610. (org-table-insert-row 'below)))
  5611. (org-table-goto-column col)
  5612. (skip-chars-backward "^|\n\r")
  5613. (if (looking-at " ") (forward-char 1)))))
  5614. (defun org-table-copy-down (n)
  5615. "Copy a field down in the current column.
  5616. If the field at the cursor is empty, copy into it the content of the nearest
  5617. non-empty field above. With argument N, use the Nth non-empty field.
  5618. If the current field is not empty, it is copied down to the next row, and
  5619. the cursor is moved with it. Therefore, repeating this command causes the
  5620. column to be filled row-by-row.
  5621. If the variable `org-table-copy-increment' is non-nil and the field is an
  5622. integer, it will be incremented while copying."
  5623. (interactive "p")
  5624. (let* ((colpos (org-table-current-column))
  5625. (field (org-table-get-field))
  5626. (non-empty (string-match "[^ \t]" field))
  5627. (beg (org-table-begin))
  5628. txt)
  5629. (org-table-check-inside-data-field)
  5630. (if non-empty
  5631. (progn
  5632. (setq txt (org-trim field))
  5633. (org-table-next-row)
  5634. (org-table-blank-field))
  5635. (save-excursion
  5636. (setq txt
  5637. (catch 'exit
  5638. (while (progn (beginning-of-line 1)
  5639. (re-search-backward org-table-dataline-regexp
  5640. beg t))
  5641. (org-table-goto-column colpos t)
  5642. (if (and (looking-at
  5643. "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
  5644. (= (setq n (1- n)) 0))
  5645. (throw 'exit (match-string 1))))))))
  5646. (if txt
  5647. (progn
  5648. (if (and org-table-copy-increment
  5649. (string-match "^[0-9]+$" txt))
  5650. (setq txt (format "%d" (+ (string-to-number txt) 1))))
  5651. (insert txt)
  5652. (org-table-maybe-recalculate-line)
  5653. (org-table-align))
  5654. (error "No non-empty field found"))))
  5655. (defun org-table-check-inside-data-field ()
  5656. "Is point inside a table data field?
  5657. I.e. not on a hline or before the first or after the last column?
  5658. This actually throws an error, so it aborts the current command."
  5659. (if (or (not (org-at-table-p))
  5660. (= (org-table-current-column) 0)
  5661. (org-at-table-hline-p)
  5662. (looking-at "[ \t]*$"))
  5663. (error "Not in table data field")))
  5664. (defvar org-table-clip nil
  5665. "Clipboard for table regions.")
  5666. (defun org-table-blank-field ()
  5667. "Blank the current table field or active region."
  5668. (interactive)
  5669. (org-table-check-inside-data-field)
  5670. (if (and (interactive-p) (org-region-active-p))
  5671. (let (org-table-clip)
  5672. (org-table-cut-region (region-beginning) (region-end)))
  5673. (skip-chars-backward "^|")
  5674. (backward-char 1)
  5675. (if (looking-at "|[^|\n]+")
  5676. (let* ((pos (match-beginning 0))
  5677. (match (match-string 0))
  5678. (len (org-string-width match)))
  5679. (replace-match (concat "|" (make-string (1- len) ?\ )))
  5680. (goto-char (+ 2 pos))
  5681. (substring match 1)))))
  5682. (defun org-table-get-field (&optional n replace)
  5683. "Return the value of the field in column N of current row.
  5684. N defaults to current field.
  5685. If REPLACE is a string, replace field with this value. The return value
  5686. is always the old value."
  5687. (and n (org-table-goto-column n))
  5688. (skip-chars-backward "^|\n")
  5689. (backward-char 1)
  5690. (if (looking-at "|[^|\r\n]*")
  5691. (let* ((pos (match-beginning 0))
  5692. (val (buffer-substring (1+ pos) (match-end 0))))
  5693. (if replace
  5694. (replace-match (concat "|" replace)))
  5695. (goto-char (min (point-at-eol) (+ 2 pos)))
  5696. val)
  5697. (forward-char 1) ""))
  5698. (defun org-table-field-info (arg)
  5699. "Show info about the current field, and highlight any reference at point."
  5700. (interactive "P")
  5701. (org-table-get-specials)
  5702. (save-excursion
  5703. (let* ((pos (point))
  5704. (col (org-table-current-column))
  5705. (cname (car (rassoc (int-to-string col) org-table-column-names)))
  5706. (name (car (rassoc (list (org-current-line) col)
  5707. org-table-named-field-locations)))
  5708. (eql (org-table-get-stored-formulas))
  5709. (dline (org-table-current-dline))
  5710. (ref (format "@%d$%d" dline col))
  5711. (fequation (or (assoc name eql) (assoc ref eql)))
  5712. (cequation (assoc (int-to-string col) eql)))
  5713. (goto-char pos)
  5714. (condition-case nil
  5715. (org-show-reference 'local)
  5716. (error nil))
  5717. (message "line @%d, col $%s%s, ref @%d$%d%s%s"
  5718. dline col
  5719. (if cname (concat " or $" cname) "")
  5720. dline col
  5721. (if name (concat " or $" name) "")
  5722. ;; FIXME: formula info not correct if special table line
  5723. (if (or fequation cequation)
  5724. (concat ", " (if fequation "field" "column")
  5725. " formula applies" "")
  5726. "")))))
  5727. (defun org-table-current-column ()
  5728. "Find out which column we are in.
  5729. When called interactively, column is also displayed in echo area."
  5730. (interactive)
  5731. (if (interactive-p) (org-table-check-inside-data-field))
  5732. (save-excursion
  5733. (let ((cnt 0) (pos (point)))
  5734. (beginning-of-line 1)
  5735. (while (search-forward "|" pos t)
  5736. (setq cnt (1+ cnt)))
  5737. (if (interactive-p) (message "This is table column %d" cnt))
  5738. cnt)))
  5739. (defun org-table-current-dline ()
  5740. "Find out what table data line we are in.
  5741. Only datalins count for this."
  5742. (interactive)
  5743. (if (interactive-p) (org-table-check-inside-data-field))
  5744. (save-excursion
  5745. (let ((cnt 0) (pos (point)))
  5746. (goto-char (org-table-begin))
  5747. (while (<= (point) pos)
  5748. (if (looking-at org-table-dataline-regexp) (setq cnt (1+ cnt)))
  5749. (beginning-of-line 2))
  5750. (if (interactive-p) (message "This is table line %d" cnt))
  5751. cnt)))
  5752. (defun org-table-goto-column (n &optional on-delim force)
  5753. "Move the cursor to the Nth column in the current table line.
  5754. With optional argument ON-DELIM, stop with point before the left delimiter
  5755. of the field.
  5756. If there are less than N fields, just go to after the last delimiter.
  5757. However, when FORCE is non-nil, create new columns if necessary."
  5758. (interactive "p")
  5759. (let ((pos (point-at-eol)))
  5760. (beginning-of-line 1)
  5761. (when (> n 0)
  5762. (while (and (> (setq n (1- n)) -1)
  5763. (or (search-forward "|" pos t)
  5764. (and force
  5765. (progn (end-of-line 1)
  5766. (skip-chars-backward "^|")
  5767. (insert " | "))))))
  5768. ; (backward-char 2) t)))))
  5769. (when (and force (not (looking-at ".*|")))
  5770. (save-excursion (end-of-line 1) (insert " | ")))
  5771. (if on-delim
  5772. (backward-char 1)
  5773. (if (looking-at " ") (forward-char 1))))))
  5774. (defun org-at-table-p (&optional table-type)
  5775. "Return t if the cursor is inside an org-type table.
  5776. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  5777. (if org-enable-table-editor
  5778. (save-excursion
  5779. (beginning-of-line 1)
  5780. (looking-at (if table-type org-table-any-line-regexp
  5781. org-table-line-regexp)))
  5782. nil))
  5783. (defun org-at-table.el-p ()
  5784. "Return t if and only if we are at a table.el table."
  5785. (and (org-at-table-p 'any)
  5786. (save-excursion
  5787. (goto-char (org-table-begin 'any))
  5788. (looking-at org-table1-hline-regexp))))
  5789. (defun org-table-recognize-table.el ()
  5790. "If there is a table.el table nearby, recognize it and move into it."
  5791. (if org-table-tab-recognizes-table.el
  5792. (if (org-at-table.el-p)
  5793. (progn
  5794. (beginning-of-line 1)
  5795. (if (looking-at org-table-dataline-regexp)
  5796. nil
  5797. (if (looking-at org-table1-hline-regexp)
  5798. (progn
  5799. (beginning-of-line 2)
  5800. (if (looking-at org-table-any-border-regexp)
  5801. (beginning-of-line -1)))))
  5802. (if (re-search-forward "|" (org-table-end t) t)
  5803. (progn
  5804. (require 'table)
  5805. (if (table--at-cell-p (point))
  5806. t
  5807. (message "recognizing table.el table...")
  5808. (table-recognize-table)
  5809. (message "recognizing table.el table...done")))
  5810. (error "This should not happen..."))
  5811. t)
  5812. nil)
  5813. nil))
  5814. (defun org-at-table-hline-p ()
  5815. "Return t if the cursor is inside a hline in a table."
  5816. (if org-enable-table-editor
  5817. (save-excursion
  5818. (beginning-of-line 1)
  5819. (looking-at org-table-hline-regexp))
  5820. nil))
  5821. (defun org-table-insert-column ()
  5822. "Insert a new column into the table."
  5823. (interactive)
  5824. (if (not (org-at-table-p))
  5825. (error "Not at a table"))
  5826. (org-table-find-dataline)
  5827. (let* ((col (max 1 (org-table-current-column)))
  5828. (beg (org-table-begin))
  5829. (end (org-table-end))
  5830. ;; Current cursor position
  5831. (linepos (org-current-line))
  5832. (colpos col))
  5833. (goto-char beg)
  5834. (while (< (point) end)
  5835. (if (org-at-table-hline-p)
  5836. nil
  5837. (org-table-goto-column col t)
  5838. (insert "| "))
  5839. (beginning-of-line 2))
  5840. (move-marker end nil)
  5841. (goto-line linepos)
  5842. (org-table-goto-column colpos)
  5843. (org-table-align)
  5844. (org-table-fix-formulas "$" nil (1- col) 1)))
  5845. (defun org-table-find-dataline ()
  5846. "Find a dataline in the current table, which is needed for column commands."
  5847. (if (and (org-at-table-p)
  5848. (not (org-at-table-hline-p)))
  5849. t
  5850. (let ((col (current-column))
  5851. (end (org-table-end)))
  5852. (move-to-column col)
  5853. (while (and (< (point) end)
  5854. (or (not (= (current-column) col))
  5855. (org-at-table-hline-p)))
  5856. (beginning-of-line 2)
  5857. (move-to-column col))
  5858. (if (and (org-at-table-p)
  5859. (not (org-at-table-hline-p)))
  5860. t
  5861. (error
  5862. "Please position cursor in a data line for column operations")))))
  5863. (defun org-table-delete-column ()
  5864. "Delete a column from the table."
  5865. (interactive)
  5866. (if (not (org-at-table-p))
  5867. (error "Not at a table"))
  5868. (org-table-find-dataline)
  5869. (org-table-check-inside-data-field)
  5870. (let* ((col (org-table-current-column))
  5871. (beg (org-table-begin))
  5872. (end (org-table-end))
  5873. ;; Current cursor position
  5874. (linepos (org-current-line))
  5875. (colpos col))
  5876. (goto-char beg)
  5877. (while (< (point) end)
  5878. (if (org-at-table-hline-p)
  5879. nil
  5880. (org-table-goto-column col t)
  5881. (and (looking-at "|[^|\n]+|")
  5882. (replace-match "|")))
  5883. (beginning-of-line 2))
  5884. (move-marker end nil)
  5885. (goto-line linepos)
  5886. (org-table-goto-column colpos)
  5887. (org-table-align)
  5888. (org-table-fix-formulas "$" (list (cons (number-to-string col) "INVALID"))
  5889. col -1 col)))
  5890. (defun org-table-move-column-right ()
  5891. "Move column to the right."
  5892. (interactive)
  5893. (org-table-move-column nil))
  5894. (defun org-table-move-column-left ()
  5895. "Move column to the left."
  5896. (interactive)
  5897. (org-table-move-column 'left))
  5898. (defun org-table-move-column (&optional left)
  5899. "Move the current column to the right. With arg LEFT, move to the left."
  5900. (interactive "P")
  5901. (if (not (org-at-table-p))
  5902. (error "Not at a table"))
  5903. (org-table-find-dataline)
  5904. (org-table-check-inside-data-field)
  5905. (let* ((col (org-table-current-column))
  5906. (col1 (if left (1- col) col))
  5907. (beg (org-table-begin))
  5908. (end (org-table-end))
  5909. ;; Current cursor position
  5910. (linepos (org-current-line))
  5911. (colpos (if left (1- col) (1+ col))))
  5912. (if (and left (= col 1))
  5913. (error "Cannot move column further left"))
  5914. (if (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
  5915. (error "Cannot move column further right"))
  5916. (goto-char beg)
  5917. (while (< (point) end)
  5918. (if (org-at-table-hline-p)
  5919. nil
  5920. (org-table-goto-column col1 t)
  5921. (and (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
  5922. (replace-match "|\\2|\\1|")))
  5923. (beginning-of-line 2))
  5924. (move-marker end nil)
  5925. (goto-line linepos)
  5926. (org-table-goto-column colpos)
  5927. (org-table-align)
  5928. (org-table-fix-formulas
  5929. "$" (list (cons (number-to-string col) (number-to-string colpos))
  5930. (cons (number-to-string colpos) (number-to-string col))))))
  5931. (defun org-table-move-row-down ()
  5932. "Move table row down."
  5933. (interactive)
  5934. (org-table-move-row nil))
  5935. (defun org-table-move-row-up ()
  5936. "Move table row up."
  5937. (interactive)
  5938. (org-table-move-row 'up))
  5939. (defun org-table-move-row (&optional up)
  5940. "Move the current table line down. With arg UP, move it up."
  5941. (interactive "P")
  5942. (let* ((col (current-column))
  5943. (pos (point))
  5944. (hline1p (save-excursion (beginning-of-line 1)
  5945. (looking-at org-table-hline-regexp)))
  5946. (dline1 (org-table-current-dline))
  5947. (dline2 (+ dline1 (if up -1 1)))
  5948. (tonew (if up 0 2))
  5949. txt hline2p)
  5950. (beginning-of-line tonew)
  5951. (unless (org-at-table-p)
  5952. (goto-char pos)
  5953. (error "Cannot move row further"))
  5954. (setq hline2p (looking-at org-table-hline-regexp))
  5955. (goto-char pos)
  5956. (beginning-of-line 1)
  5957. (setq pos (point))
  5958. (setq txt (buffer-substring (point) (1+ (point-at-eol))))
  5959. (delete-region (point) (1+ (point-at-eol)))
  5960. (beginning-of-line tonew)
  5961. (insert txt)
  5962. (beginning-of-line 0)
  5963. (move-to-column col)
  5964. (unless (or hline1p hline2p)
  5965. (org-table-fix-formulas
  5966. "@" (list (cons (number-to-string dline1) (number-to-string dline2))
  5967. (cons (number-to-string dline2) (number-to-string dline1)))))))
  5968. (defun org-table-insert-row (&optional arg)
  5969. "Insert a new row above the current line into the table.
  5970. With prefix ARG, insert below the current line."
  5971. (interactive "P")
  5972. (if (not (org-at-table-p))
  5973. (error "Not at a table"))
  5974. (let* ((line (buffer-substring (point-at-bol) (point-at-eol)))
  5975. (new (org-table-clean-line line)))
  5976. ;; Fix the first field if necessary
  5977. (if (string-match "^[ \t]*| *[#$] *|" line)
  5978. (setq new (replace-match (match-string 0 line) t t new)))
  5979. (beginning-of-line (if arg 2 1))
  5980. (let (org-table-may-need-update) (insert-before-markers new "\n"))
  5981. (beginning-of-line 0)
  5982. (re-search-forward "| ?" (point-at-eol) t)
  5983. (and org-table-may-need-update (org-table-align))
  5984. (org-table-fix-formulas "@" nil (1- (org-table-current-dline)) 1)))
  5985. (defun org-table-insert-hline (&optional arg)
  5986. "Insert a horizontal-line below the current line into the table.
  5987. With prefix ARG, insert above the current line."
  5988. (interactive "P")
  5989. (if (not (org-at-table-p))
  5990. (error "Not at a table"))
  5991. (let ((line (org-table-clean-line
  5992. (buffer-substring (point-at-bol) (point-at-eol))))
  5993. (col (current-column)))
  5994. (while (string-match "|\\( +\\)|" line)
  5995. (setq line (replace-match
  5996. (concat "+" (make-string (- (match-end 1) (match-beginning 1))
  5997. ?-) "|") t t line)))
  5998. (and (string-match "\\+" line) (setq line (replace-match "|" t t line)))
  5999. (beginning-of-line (if arg 1 2))
  6000. (insert line "\n")
  6001. (beginning-of-line (if arg 1 -1))
  6002. (move-to-column col)))
  6003. (defun org-table-clean-line (s)
  6004. "Convert a table line S into a string with only \"|\" and space.
  6005. In particular, this does handle wide and invisible characters."
  6006. (if (string-match "^[ \t]*|-" s)
  6007. ;; It's a hline, just map the characters
  6008. (setq s (mapconcat (lambda (x) (if (member x '(?| ?+)) "|" " ")) s ""))
  6009. (while (string-match "|\\([ \t]*?[^ \t\r\n|][^\r\n|]*\\)|" s)
  6010. (setq s (replace-match
  6011. (concat "|" (make-string (org-string-width (match-string 1 s))
  6012. ?\ ) "|")
  6013. t t s)))
  6014. s))
  6015. (defun org-table-kill-row ()
  6016. "Delete the current row or horizontal line from the table."
  6017. (interactive)
  6018. (if (not (org-at-table-p))
  6019. (error "Not at a table"))
  6020. (let ((col (current-column))
  6021. (dline (org-table-current-dline)))
  6022. (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
  6023. (if (not (org-at-table-p)) (beginning-of-line 0))
  6024. (move-to-column col)
  6025. (org-table-fix-formulas "@" (list (cons (number-to-string dline) "INVALID"))
  6026. dline -1 dline)))
  6027. (defun org-table-sort-lines (with-case &optional sorting-type)
  6028. "Sort table lines according to the column at point.
  6029. The position of point indicates the column to be used for
  6030. sorting, and the range of lines is the range between the nearest
  6031. horizontal separator lines, or the entire table of no such lines
  6032. exist. If point is before the first column, you will be prompted
  6033. for the sorting column. If there is an active region, the mark
  6034. specifies the first line and the sorting column, while point
  6035. should be in the last line to be included into the sorting.
  6036. The command then prompts for the sorting type which can be
  6037. alphabetically, numerically, or by time (as given in a time stamp
  6038. in the field). Sorting in reverse order is also possible.
  6039. With prefix argument WITH-CASE, alphabetic sorting will be case-sensitive.
  6040. If SORTING-TYPE is specified when this function is called from a Lisp
  6041. program, no prompting will take place. SORTING-TYPE must be a character,
  6042. any of (?a ?A ?n ?N ?t ?T) where the capital letter indicate that sorting
  6043. should be done in reverse order."
  6044. (interactive "P")
  6045. (let* ((thisline (org-current-line))
  6046. (thiscol (org-table-current-column))
  6047. beg end bcol ecol tend tbeg column lns pos)
  6048. (when (equal thiscol 0)
  6049. (if (interactive-p)
  6050. (setq thiscol
  6051. (string-to-number
  6052. (read-string "Use column N for sorting: ")))
  6053. (setq thiscol 1))
  6054. (org-table-goto-column thiscol))
  6055. (org-table-check-inside-data-field)
  6056. (if (org-region-active-p)
  6057. (progn
  6058. (setq beg (region-beginning) end (region-end))
  6059. (goto-char beg)
  6060. (setq column (org-table-current-column)
  6061. beg (point-at-bol))
  6062. (goto-char end)
  6063. (setq end (point-at-bol 2)))
  6064. (setq column (org-table-current-column)
  6065. pos (point)
  6066. tbeg (org-table-begin)
  6067. tend (org-table-end))
  6068. (if (re-search-backward org-table-hline-regexp tbeg t)
  6069. (setq beg (point-at-bol 2))
  6070. (goto-char tbeg)
  6071. (setq beg (point-at-bol 1)))
  6072. (goto-char pos)
  6073. (if (re-search-forward org-table-hline-regexp tend t)
  6074. (setq beg (point-at-bol 0))
  6075. (goto-char tend)
  6076. (setq end (point-at-bol))))
  6077. (setq beg (move-marker (make-marker) beg)
  6078. end (move-marker (make-marker) end))
  6079. (untabify beg end)
  6080. (goto-char beg)
  6081. (org-table-goto-column column)
  6082. (skip-chars-backward "^|")
  6083. (setq bcol (current-column))
  6084. (org-table-goto-column (1+ column))
  6085. (skip-chars-backward "^|")
  6086. (setq ecol (1- (current-column)))
  6087. (org-table-goto-column column)
  6088. (setq lns (mapcar (lambda(x) (cons (org-trim (substring x bcol ecol)) x))
  6089. (org-split-string (buffer-substring beg end) "\n")))
  6090. (setq lns (org-do-sort lns "Table" with-case sorting-type))
  6091. (delete-region beg end)
  6092. (move-marker beg nil)
  6093. (move-marker end nil)
  6094. (insert (mapconcat 'cdr lns "\n") "\n")
  6095. (goto-line thisline)
  6096. (org-table-goto-column thiscol)
  6097. (message "%d lines sorted, based on column %d" (length lns) column)))
  6098. (defun org-table-cut-region (beg end)
  6099. "Copy region in table to the clipboard and blank all relevant fields."
  6100. (interactive "r")
  6101. (org-table-copy-region beg end 'cut))
  6102. (defun org-table-copy-region (beg end &optional cut)
  6103. "Copy rectangular region in table to clipboard.
  6104. A special clipboard is used which can only be accessed
  6105. with `org-table-paste-rectangle'."
  6106. (interactive "rP")
  6107. (let* (l01 c01 l02 c02 l1 c1 l2 c2 ic1 ic2
  6108. region cols
  6109. (rpl (if cut " " nil)))
  6110. (goto-char beg)
  6111. (org-table-check-inside-data-field)
  6112. (setq l01 (org-current-line)
  6113. c01 (org-table-current-column))
  6114. (goto-char end)
  6115. (org-table-check-inside-data-field)
  6116. (setq l02 (org-current-line)
  6117. c02 (org-table-current-column))
  6118. (setq l1 (min l01 l02) l2 (max l01 l02)
  6119. c1 (min c01 c02) c2 (max c01 c02))
  6120. (catch 'exit
  6121. (while t
  6122. (catch 'nextline
  6123. (if (> l1 l2) (throw 'exit t))
  6124. (goto-line l1)
  6125. (if (org-at-table-hline-p) (throw 'nextline (setq l1 (1+ l1))))
  6126. (setq cols nil ic1 c1 ic2 c2)
  6127. (while (< ic1 (1+ ic2))
  6128. (push (org-table-get-field ic1 rpl) cols)
  6129. (setq ic1 (1+ ic1)))
  6130. (push (nreverse cols) region)
  6131. (setq l1 (1+ l1)))))
  6132. (setq org-table-clip (nreverse region))
  6133. (if cut (org-table-align))
  6134. org-table-clip))
  6135. (defun org-table-paste-rectangle ()
  6136. "Paste a rectangular region into a table.
  6137. The upper right corner ends up in the current field. All involved fields
  6138. will be overwritten. If the rectangle does not fit into the present table,
  6139. the table is enlarged as needed. The process ignores horizontal separator
  6140. lines."
  6141. (interactive)
  6142. (unless (and org-table-clip (listp org-table-clip))
  6143. (error "First cut/copy a region to paste!"))
  6144. (org-table-check-inside-data-field)
  6145. (let* ((clip org-table-clip)
  6146. (line (org-current-line))
  6147. (col (org-table-current-column))
  6148. (org-enable-table-editor t)
  6149. (org-table-automatic-realign nil)
  6150. c cols field)
  6151. (while (setq cols (pop clip))
  6152. (while (org-at-table-hline-p) (beginning-of-line 2))
  6153. (if (not (org-at-table-p))
  6154. (progn (end-of-line 0) (org-table-next-field)))
  6155. (setq c col)
  6156. (while (setq field (pop cols))
  6157. (org-table-goto-column c nil 'force)
  6158. (org-table-get-field nil field)
  6159. (setq c (1+ c)))
  6160. (beginning-of-line 2))
  6161. (goto-line line)
  6162. (org-table-goto-column col)
  6163. (org-table-align)))
  6164. (defun org-table-convert ()
  6165. "Convert from `org-mode' table to table.el and back.
  6166. Obviously, this only works within limits. When an Org-mode table is
  6167. converted to table.el, all horizontal separator lines get lost, because
  6168. table.el uses these as cell boundaries and has no notion of horizontal lines.
  6169. A table.el table can be converted to an Org-mode table only if it does not
  6170. do row or column spanning. Multiline cells will become multiple cells.
  6171. Beware, Org-mode does not test if the table can be successfully converted - it
  6172. blindly applies a recipe that works for simple tables."
  6173. (interactive)
  6174. (require 'table)
  6175. (if (org-at-table.el-p)
  6176. ;; convert to Org-mode table
  6177. (let ((beg (move-marker (make-marker) (org-table-begin t)))
  6178. (end (move-marker (make-marker) (org-table-end t))))
  6179. (table-unrecognize-region beg end)
  6180. (goto-char beg)
  6181. (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
  6182. (replace-match ""))
  6183. (goto-char beg))
  6184. (if (org-at-table-p)
  6185. ;; convert to table.el table
  6186. (let ((beg (move-marker (make-marker) (org-table-begin)))
  6187. (end (move-marker (make-marker) (org-table-end))))
  6188. ;; first, get rid of all horizontal lines
  6189. (goto-char beg)
  6190. (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t)
  6191. (replace-match ""))
  6192. ;; insert a hline before first
  6193. (goto-char beg)
  6194. (org-table-insert-hline 'above)
  6195. (beginning-of-line -1)
  6196. ;; insert a hline after each line
  6197. (while (progn (beginning-of-line 3) (< (point) end))
  6198. (org-table-insert-hline))
  6199. (goto-char beg)
  6200. (setq end (move-marker end (org-table-end)))
  6201. ;; replace "+" at beginning and ending of hlines
  6202. (while (re-search-forward "^\\([ \t]*\\)|-" end t)
  6203. (replace-match "\\1+-"))
  6204. (goto-char beg)
  6205. (while (re-search-forward "-|[ \t]*$" end t)
  6206. (replace-match "-+"))
  6207. (goto-char beg)))))
  6208. (defun org-table-wrap-region (arg)
  6209. "Wrap several fields in a column like a paragraph.
  6210. This is useful if you'd like to spread the contents of a field over several
  6211. lines, in order to keep the table compact.
  6212. If there is an active region, and both point and mark are in the same column,
  6213. the text in the column is wrapped to minimum width for the given number of
  6214. lines. Generally, this makes the table more compact. A prefix ARG may be
  6215. used to change the number of desired lines. For example, `C-2 \\[org-table-wrap]'
  6216. formats the selected text to two lines. If the region was longer than two
  6217. lines, the remaining lines remain empty. A negative prefix argument reduces
  6218. the current number of lines by that amount. The wrapped text is pasted back
  6219. into the table. If you formatted it to more lines than it was before, fields
  6220. further down in the table get overwritten - so you might need to make space in
  6221. the table first.
  6222. If there is no region, the current field is split at the cursor position and
  6223. the text fragment to the right of the cursor is prepended to the field one
  6224. line down.
  6225. If there is no region, but you specify a prefix ARG, the current field gets
  6226. blank, and the content is appended to the field above."
  6227. (interactive "P")
  6228. (org-table-check-inside-data-field)
  6229. (if (org-region-active-p)
  6230. ;; There is a region: fill as a paragraph
  6231. (let ((beg (region-beginning))
  6232. nlines)
  6233. (org-table-cut-region (region-beginning) (region-end))
  6234. (if (> (length (car org-table-clip)) 1)
  6235. (error "Region must be limited to single column"))
  6236. (setq nlines (if arg
  6237. (if (< arg 1)
  6238. (+ (length org-table-clip) arg)
  6239. arg)
  6240. (length org-table-clip)))
  6241. (setq org-table-clip
  6242. (mapcar 'list (org-wrap (mapconcat 'car org-table-clip " ")
  6243. nil nlines)))
  6244. (goto-char beg)
  6245. (org-table-paste-rectangle))
  6246. ;; No region, split the current field at point
  6247. (if arg
  6248. ;; combine with field above
  6249. (let ((s (org-table-blank-field))
  6250. (col (org-table-current-column)))
  6251. (beginning-of-line 0)
  6252. (while (org-at-table-hline-p) (beginning-of-line 0))
  6253. (org-table-goto-column col)
  6254. (skip-chars-forward "^|")
  6255. (skip-chars-backward " ")
  6256. (insert " " (org-trim s))
  6257. (org-table-align))
  6258. ;; split field
  6259. (when (looking-at "\\([^|]+\\)+|")
  6260. (let ((s (match-string 1)))
  6261. (replace-match " |")
  6262. (goto-char (match-beginning 0))
  6263. (org-table-next-row)
  6264. (insert (org-trim s) " ")
  6265. (org-table-align))))))
  6266. (defvar org-field-marker nil)
  6267. (defun org-table-edit-field (arg)
  6268. "Edit table field in a different window.
  6269. This is mainly useful for fields that contain hidden parts.
  6270. When called with a \\[universal-argument] prefix, just make the full field visible so that
  6271. it can be edited in place."
  6272. (interactive "P")
  6273. (if arg
  6274. (let ((b (save-excursion (skip-chars-backward "^|") (point)))
  6275. (e (save-excursion (skip-chars-forward "^|\r\n") (point))))
  6276. (remove-text-properties b e '(org-cwidth t invisible t
  6277. display t intangible t))
  6278. (if (and (boundp 'font-lock-mode) font-lock-mode)
  6279. (font-lock-fontify-block)))
  6280. (let ((pos (move-marker (make-marker) (point)))
  6281. (field (org-table-get-field))
  6282. (cw (current-window-configuration))
  6283. p)
  6284. (switch-to-buffer-other-window "*Org tmp*")
  6285. (erase-buffer)
  6286. (insert "#\n# Edit field and finish with C-c C-c\n#\n")
  6287. (org-mode)
  6288. (goto-char (setq p (point-max)))
  6289. (insert (org-trim field))
  6290. (remove-text-properties p (point-max)
  6291. '(invisible t org-cwidth t display t
  6292. intangible t))
  6293. (goto-char p)
  6294. (org-set-local 'org-finish-function
  6295. 'org-table-finish-edit-field)
  6296. (org-set-local 'org-window-configuration cw)
  6297. (org-set-local 'org-field-marker pos)
  6298. (message "Edit and finish with C-c C-c"))))
  6299. (defun org-table-finish-edit-field ()
  6300. "Finish editing a table data field.
  6301. Remove all newline characters, insert the result into the table, realign
  6302. the table and kill the editing buffer."
  6303. (let ((pos org-field-marker)
  6304. (cw org-window-configuration)
  6305. (cb (current-buffer))
  6306. text)
  6307. (goto-char (point-min))
  6308. (while (re-search-forward "^#.*\n?" nil t) (replace-match ""))
  6309. (while (re-search-forward "\\([ \t]*\n[ \t]*\\)+" nil t)
  6310. (replace-match " "))
  6311. (setq text (org-trim (buffer-string)))
  6312. (set-window-configuration cw)
  6313. (kill-buffer cb)
  6314. (select-window (get-buffer-window (marker-buffer pos)))
  6315. (goto-char pos)
  6316. (move-marker pos nil)
  6317. (org-table-check-inside-data-field)
  6318. (org-table-get-field nil text)
  6319. (org-table-align)
  6320. (message "New field value inserted")))
  6321. (defun org-trim (s)
  6322. "Remove whitespace at beginning and end of string."
  6323. (if (string-match "^[ \t]+" s) (setq s (replace-match "" t t s)))
  6324. (if (string-match "[ \t]+$" s) (setq s (replace-match "" t t s)))
  6325. s)
  6326. (defun org-wrap (string &optional width lines)
  6327. "Wrap string to either a number of lines, or a width in characters.
  6328. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  6329. that costs. If there is a word longer than WIDTH, the text is actually
  6330. wrapped to the length of that word.
  6331. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  6332. many lines, whatever width that takes.
  6333. The return value is a list of lines, without newlines at the end."
  6334. (let* ((words (org-split-string string "[ \t\n]+"))
  6335. (maxword (apply 'max (mapcar 'org-string-width words)))
  6336. w ll)
  6337. (cond (width
  6338. (org-do-wrap words (max maxword width)))
  6339. (lines
  6340. (setq w maxword)
  6341. (setq ll (org-do-wrap words maxword))
  6342. (if (<= (length ll) lines)
  6343. ll
  6344. (setq ll words)
  6345. (while (> (length ll) lines)
  6346. (setq w (1+ w))
  6347. (setq ll (org-do-wrap words w)))
  6348. ll))
  6349. (t (error "Cannot wrap this")))))
  6350. (defun org-do-wrap (words width)
  6351. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  6352. (let (lines line)
  6353. (while words
  6354. (setq line (pop words))
  6355. (while (and words (< (+ (length line) (length (car words))) width))
  6356. (setq line (concat line " " (pop words))))
  6357. (setq lines (push line lines)))
  6358. (nreverse lines)))
  6359. (defun org-split-string (string &optional separators)
  6360. "Splits STRING into substrings at SEPARATORS.
  6361. No empty strings are returned if there are matches at the beginning
  6362. and end of string."
  6363. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  6364. (start 0)
  6365. notfirst
  6366. (list nil))
  6367. (while (and (string-match rexp string
  6368. (if (and notfirst
  6369. (= start (match-beginning 0))
  6370. (< start (length string)))
  6371. (1+ start) start))
  6372. (< (match-beginning 0) (length string)))
  6373. (setq notfirst t)
  6374. (or (eq (match-beginning 0) 0)
  6375. (and (eq (match-beginning 0) (match-end 0))
  6376. (eq (match-beginning 0) start))
  6377. (setq list
  6378. (cons (substring string start (match-beginning 0))
  6379. list)))
  6380. (setq start (match-end 0)))
  6381. (or (eq start (length string))
  6382. (setq list
  6383. (cons (substring string start)
  6384. list)))
  6385. (nreverse list)))
  6386. (defun org-table-map-tables (function)
  6387. "Apply FUNCTION to the start of all tables in the buffer."
  6388. (save-excursion
  6389. (save-restriction
  6390. (widen)
  6391. (goto-char (point-min))
  6392. (while (re-search-forward org-table-any-line-regexp nil t)
  6393. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  6394. (beginning-of-line 1)
  6395. (if (looking-at org-table-line-regexp)
  6396. (save-excursion (funcall function)))
  6397. (re-search-forward org-table-any-border-regexp nil 1))))
  6398. (message "Mapping tables: done"))
  6399. (defvar org-timecnt) ; dynamically scoped parameter
  6400. (defun org-table-sum (&optional beg end nlast)
  6401. "Sum numbers in region of current table column.
  6402. The result will be displayed in the echo area, and will be available
  6403. as kill to be inserted with \\[yank].
  6404. If there is an active region, it is interpreted as a rectangle and all
  6405. numbers in that rectangle will be summed. If there is no active
  6406. region and point is located in a table column, sum all numbers in that
  6407. column.
  6408. If at least one number looks like a time HH:MM or HH:MM:SS, all other
  6409. numbers are assumed to be times as well (in decimal hours) and the
  6410. numbers are added as such.
  6411. If NLAST is a number, only the NLAST fields will actually be summed."
  6412. (interactive)
  6413. (save-excursion
  6414. (let (col (org-timecnt 0) diff h m s org-table-clip)
  6415. (cond
  6416. ((and beg end)) ; beg and end given explicitly
  6417. ((org-region-active-p)
  6418. (setq beg (region-beginning) end (region-end)))
  6419. (t
  6420. (setq col (org-table-current-column))
  6421. (goto-char (org-table-begin))
  6422. (unless (re-search-forward "^[ \t]*|[^-]" nil t)
  6423. (error "No table data"))
  6424. (org-table-goto-column col)
  6425. ;not needed? (skip-chars-backward "^|")
  6426. (setq beg (point))
  6427. (goto-char (org-table-end))
  6428. (unless (re-search-backward "^[ \t]*|[^-]" nil t)
  6429. (error "No table data"))
  6430. (org-table-goto-column col)
  6431. ;not needed? (skip-chars-forward "^|")
  6432. (setq end (point))))
  6433. (let* ((items (apply 'append (org-table-copy-region beg end)))
  6434. (items1 (cond ((not nlast) items)
  6435. ((>= nlast (length items)) items)
  6436. (t (setq items (reverse items))
  6437. (setcdr (nthcdr (1- nlast) items) nil)
  6438. (nreverse items))))
  6439. (numbers (delq nil (mapcar 'org-table-get-number-for-summing
  6440. items1)))
  6441. (res (apply '+ numbers))
  6442. (sres (if (= org-timecnt 0)
  6443. (format "%g" res)
  6444. (setq diff (* 3600 res)
  6445. h (floor (/ diff 3600)) diff (mod diff 3600)
  6446. m (floor (/ diff 60)) diff (mod diff 60)
  6447. s diff)
  6448. (format "%d:%02d:%02d" h m s))))
  6449. (kill-new sres)
  6450. (if (interactive-p)
  6451. (message "%s"
  6452. (substitute-command-keys
  6453. (format "Sum of %d items: %-20s (\\[yank] will insert result into buffer)"
  6454. (length numbers) sres))))
  6455. sres))))
  6456. (defun org-table-get-number-for-summing (s)
  6457. (let (n)
  6458. (if (string-match "^ *|? *" s)
  6459. (setq s (replace-match "" nil nil s)))
  6460. (if (string-match " *|? *$" s)
  6461. (setq s (replace-match "" nil nil s)))
  6462. (setq n (string-to-number s))
  6463. (cond
  6464. ((and (string-match "0" s)
  6465. (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
  6466. ((string-match "\\`[ \t]+\\'" s) nil)
  6467. ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
  6468. (let ((h (string-to-number (or (match-string 1 s) "0")))
  6469. (m (string-to-number (or (match-string 2 s) "0")))
  6470. (s (string-to-number (or (match-string 4 s) "0"))))
  6471. (if (boundp 'org-timecnt) (setq org-timecnt (1+ org-timecnt)))
  6472. (* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
  6473. ((equal n 0) nil)
  6474. (t n))))
  6475. (defun org-table-current-field-formula ()
  6476. "Return the formula active for the current field.
  6477. Assumes that specials are in place."
  6478. (let* ((name (car (rassoc (list (org-current-line)
  6479. (org-table-current-column))
  6480. org-table-named-field-locations)))
  6481. (col (org-table-current-column))
  6482. (scol (int-to-string col))
  6483. (ref (format "@%d$%d" (org-table-current-dline) col))
  6484. (stored-list (org-table-get-stored-formulas))
  6485. (ass (or (assoc name stored-list)
  6486. (assoc ref stored-list)
  6487. (assoc scol stored-list))))
  6488. (if ass (concat (if (string-match "^[0-9]+$" (car ass)) "=" ":=")
  6489. (cdr ass)))))
  6490. (defun org-table-get-formula (&optional equation named)
  6491. "Read a formula from the minibuffer, offer stored formula as default.
  6492. When NAMED is non-nil, look for a named equation."
  6493. (let* ((stored-list (org-table-get-stored-formulas))
  6494. (name (car (rassoc (list (org-current-line)
  6495. (org-table-current-column))
  6496. org-table-named-field-locations)))
  6497. (ref (format "@%d$%d" (org-table-current-dline)
  6498. (org-table-current-column)))
  6499. (refass (assoc ref stored-list))
  6500. (scol (if named
  6501. (if name name ref)
  6502. (int-to-string (org-table-current-column))))
  6503. (dummy (and (or name refass) (not named)
  6504. (not (y-or-n-p "Replace field formula with column formula? " ))
  6505. (error "Abort")))
  6506. (name (or name ref))
  6507. (org-table-may-need-update nil)
  6508. (stored (cdr (assoc scol stored-list)))
  6509. (eq (cond
  6510. ((and stored equation (string-match "^ *=? *$" equation))
  6511. stored)
  6512. ((stringp equation)
  6513. equation)
  6514. (t (read-string
  6515. (format "%s formula $%s=" (if named "Field" "Column") scol)
  6516. (or stored "") 'org-table-formula-history
  6517. ;stored
  6518. ))))
  6519. mustsave)
  6520. (when (not (string-match "\\S-" eq))
  6521. ;; remove formula
  6522. (setq stored-list (delq (assoc scol stored-list) stored-list))
  6523. (org-table-store-formulas stored-list)
  6524. (error "Formula removed"))
  6525. (if (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
  6526. (if (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
  6527. (if (and name (not named))
  6528. ;; We set the column equation, delete the named one.
  6529. (setq stored-list (delq (assoc name stored-list) stored-list)
  6530. mustsave t))
  6531. (if stored
  6532. (setcdr (assoc scol stored-list) eq)
  6533. (setq stored-list (cons (cons scol eq) stored-list)))
  6534. (if (or mustsave (not (equal stored eq)))
  6535. (org-table-store-formulas stored-list))
  6536. eq))
  6537. (defun org-table-store-formulas (alist)
  6538. "Store the list of formulas below the current table."
  6539. (setq alist (sort alist (lambda (a b) (string< (car a) (car b)))))
  6540. (save-excursion
  6541. (goto-char (org-table-end))
  6542. (if (looking-at "\\([ \t]*\n\\)*#\\+TBLFM:\\(.*\n?\\)")
  6543. (progn
  6544. ;; don't overwrite TBLFM, we might use text properties to store stuff
  6545. (goto-char (match-beginning 2))
  6546. (delete-region (match-beginning 2) (match-end 0)))
  6547. (insert "#+TBLFM:"))
  6548. (insert " "
  6549. (mapconcat (lambda (x)
  6550. (concat
  6551. (if (equal (string-to-char (car x)) ?@) "" "$")
  6552. (car x) "=" (cdr x)))
  6553. alist "::")
  6554. "\n")))
  6555. (defun org-table-get-stored-formulas ()
  6556. "Return an alist with the stored formulas directly after current table."
  6557. (interactive)
  6558. (let (scol eq eq-alist strings string seen)
  6559. (save-excursion
  6560. (goto-char (org-table-end))
  6561. (when (looking-at "\\([ \t]*\n\\)*#\\+TBLFM: *\\(.*\\)")
  6562. (setq strings (org-split-string (match-string 2) " *:: *"))
  6563. (while (setq string (pop strings))
  6564. (when (string-match "\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*[^ \t]\\)" string)
  6565. (setq scol (if (match-end 2)
  6566. (match-string 2 string)
  6567. (match-string 1 string))
  6568. eq (match-string 3 string)
  6569. eq-alist (cons (cons scol eq) eq-alist))
  6570. (if (member scol seen)
  6571. (error "Double definition `$%s=' in TBLFM line, please fix by hand" scol)
  6572. (push scol seen))))))
  6573. (nreverse eq-alist)))
  6574. (defun org-table-fix-formulas (key replace &optional limit delta remove)
  6575. "Modify the equations after the table structure has been edited.
  6576. KEY is \"@\" or \"$\". REPLACE is an alist of numbers to replace.
  6577. For all numbers larger than LIMIT, shift them by DELTA."
  6578. (save-excursion
  6579. (goto-char (org-table-end))
  6580. (when (looking-at "#\\+TBLFM:")
  6581. (let ((re (concat key "\\([0-9]+\\)"))
  6582. (re2
  6583. (when remove
  6584. (if (equal key "$")
  6585. (format "\\(@[0-9]+\\)?\\$%d=.*?\\(::\\|$\\)" remove)
  6586. (format "@%d\\$[0-9]+=.*?\\(::\\|$\\)" remove))))
  6587. s n a)
  6588. (when remove
  6589. (while (re-search-forward re2 (point-at-eol) t)
  6590. (replace-match "")))
  6591. (while (re-search-forward re (point-at-eol) t)
  6592. (setq s (match-string 1) n (string-to-number s))
  6593. (cond
  6594. ((setq a (assoc s replace))
  6595. (replace-match (concat key (cdr a)) t t))
  6596. ((and limit (> n limit))
  6597. (replace-match (concat key (int-to-string (+ n delta))) t t))))))))
  6598. (defun org-table-get-specials ()
  6599. "Get the column names and local parameters for this table."
  6600. (save-excursion
  6601. (let ((beg (org-table-begin)) (end (org-table-end))
  6602. names name fields fields1 field cnt
  6603. c v l line col types dlines hlines)
  6604. (setq org-table-column-names nil
  6605. org-table-local-parameters nil
  6606. org-table-named-field-locations nil
  6607. org-table-current-begin-line nil
  6608. org-table-current-line-types nil)
  6609. (goto-char beg)
  6610. (when (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t)
  6611. (setq names (org-split-string (match-string 1) " *| *")
  6612. cnt 1)
  6613. (while (setq name (pop names))
  6614. (setq cnt (1+ cnt))
  6615. (if (string-match "^[a-zA-Z][a-zA-Z0-9]*$" name)
  6616. (push (cons name (int-to-string cnt)) org-table-column-names))))
  6617. (setq org-table-column-names (nreverse org-table-column-names))
  6618. (setq org-table-column-name-regexp
  6619. (concat "\\$\\(" (mapconcat 'car org-table-column-names "\\|") "\\)\\>"))
  6620. (goto-char beg)
  6621. (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t)
  6622. (setq fields (org-split-string (match-string 1) " *| *"))
  6623. (while (setq field (pop fields))
  6624. (if (string-match "^\\([a-zA-Z][a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field)
  6625. (push (cons (match-string 1 field) (match-string 2 field))
  6626. org-table-local-parameters))))
  6627. (goto-char beg)
  6628. (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t)
  6629. (setq c (match-string 1)
  6630. fields (org-split-string (match-string 2) " *| *"))
  6631. (save-excursion
  6632. (beginning-of-line (if (equal c "_") 2 0))
  6633. (setq line (org-current-line) col 1)
  6634. (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
  6635. (setq fields1 (org-split-string (match-string 1) " *| *"))))
  6636. (while (and fields1 (setq field (pop fields)))
  6637. (setq v (pop fields1) col (1+ col))
  6638. (when (and (stringp field) (stringp v)
  6639. (string-match "^[a-zA-Z][a-zA-Z0-9]*$" field))
  6640. (push (cons field v) org-table-local-parameters)
  6641. (push (list field line col) org-table-named-field-locations))))
  6642. ;; Analyse the line types
  6643. (goto-char beg)
  6644. (setq org-table-current-begin-line (org-current-line)
  6645. l org-table-current-begin-line)
  6646. (while (looking-at "[ \t]*|\\(-\\)?")
  6647. (push (if (match-end 1) 'hline 'dline) types)
  6648. (if (match-end 1) (push l hlines) (push l dlines))
  6649. (beginning-of-line 2)
  6650. (setq l (1+ l)))
  6651. (setq org-table-current-line-types (apply 'vector (nreverse types))
  6652. org-table-dlines (apply 'vector (cons nil (nreverse dlines)))
  6653. org-table-hlines (apply 'vector (cons nil (nreverse hlines)))))))
  6654. (defun org-this-word ()
  6655. ;; Get the current word
  6656. (save-excursion
  6657. (let ((beg (progn (skip-chars-backward "^ \t\n") (point)))
  6658. (end (progn (skip-chars-forward "^ \t\n") (point))))
  6659. (buffer-substring-no-properties beg end))))
  6660. (defun org-table-maybe-eval-formula ()
  6661. "Check if the current field starts with \"=\" or \":=\".
  6662. If yes, store the formula and apply it."
  6663. ;; We already know we are in a table. Get field will only return a formula
  6664. ;; when appropriate. It might return a separator line, but no problem.
  6665. (when org-table-formula-evaluate-inline
  6666. (let* ((field (org-trim (or (org-table-get-field) "")))
  6667. named eq)
  6668. (when (string-match "^:?=\\(.*\\)" field)
  6669. (setq named (equal (string-to-char field) ?:)
  6670. eq (match-string 1 field))
  6671. (if (or (fboundp 'calc-eval)
  6672. (equal (substring eq 0 (min 2 (length eq))) "'("))
  6673. (org-table-eval-formula (if named '(4) nil) eq)
  6674. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))))))
  6675. (defvar org-recalc-commands nil
  6676. "List of commands triggering the recalculation of a line.
  6677. Will be filled automatically during use.")
  6678. (defvar org-recalc-marks
  6679. '((" " . "Unmarked: no special line, no automatic recalculation")
  6680. ("#" . "Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
  6681. ("*" . "Recalculate only when entire table is recalculated with `C-u C-c *'")
  6682. ("!" . "Column name definition line. Reference in formula as $name.")
  6683. ("$" . "Parameter definition line name=value. Reference in formula as $name.")
  6684. ("_" . "Names for values in row below this one.")
  6685. ("^" . "Names for values in row above this one.")))
  6686. (defun org-table-rotate-recalc-marks (&optional newchar)
  6687. "Rotate the recalculation mark in the first column.
  6688. If in any row, the first field is not consistent with a mark,
  6689. insert a new column for the markers.
  6690. When there is an active region, change all the lines in the region,
  6691. after prompting for the marking character.
  6692. After each change, a message will be displayed indicating the meaning
  6693. of the new mark."
  6694. (interactive)
  6695. (unless (org-at-table-p) (error "Not at a table"))
  6696. (let* ((marks (append (mapcar 'car org-recalc-marks) '(" ")))
  6697. (beg (org-table-begin))
  6698. (end (org-table-end))
  6699. (l (org-current-line))
  6700. (l1 (if (org-region-active-p) (org-current-line (region-beginning))))
  6701. (l2 (if (org-region-active-p) (org-current-line (region-end))))
  6702. (have-col
  6703. (save-excursion
  6704. (goto-char beg)
  6705. (not (re-search-forward "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" end t))))
  6706. (col (org-table-current-column))
  6707. (forcenew (car (assoc newchar org-recalc-marks)))
  6708. epos new)
  6709. (when l1
  6710. (message "Change region to what mark? Type # * ! $ or SPC: ")
  6711. (setq newchar (char-to-string (read-char-exclusive))
  6712. forcenew (car (assoc newchar org-recalc-marks))))
  6713. (if (and newchar (not forcenew))
  6714. (error "Invalid NEWCHAR `%s' in `org-table-rotate-recalc-marks'"
  6715. newchar))
  6716. (if l1 (goto-line l1))
  6717. (save-excursion
  6718. (beginning-of-line 1)
  6719. (unless (looking-at org-table-dataline-regexp)
  6720. (error "Not at a table data line")))
  6721. (unless have-col
  6722. (org-table-goto-column 1)
  6723. (org-table-insert-column)
  6724. (org-table-goto-column (1+ col)))
  6725. (setq epos (point-at-eol))
  6726. (save-excursion
  6727. (beginning-of-line 1)
  6728. (org-table-get-field
  6729. 1 (if (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")
  6730. (concat " "
  6731. (setq new (or forcenew
  6732. (cadr (member (match-string 1) marks))))
  6733. " ")
  6734. " # ")))
  6735. (if (and l1 l2)
  6736. (progn
  6737. (goto-line l1)
  6738. (while (progn (beginning-of-line 2) (not (= (org-current-line) l2)))
  6739. (and (looking-at org-table-dataline-regexp)
  6740. (org-table-get-field 1 (concat " " new " "))))
  6741. (goto-line l1)))
  6742. (if (not (= epos (point-at-eol))) (org-table-align))
  6743. (goto-line l)
  6744. (and (interactive-p) (message (cdr (assoc new org-recalc-marks))))))
  6745. (defun org-table-maybe-recalculate-line ()
  6746. "Recompute the current line if marked for it, and if we haven't just done it."
  6747. (interactive)
  6748. (and org-table-allow-automatic-line-recalculation
  6749. (not (and (memq last-command org-recalc-commands)
  6750. (equal org-last-recalc-line (org-current-line))))
  6751. (save-excursion (beginning-of-line 1)
  6752. (looking-at org-table-auto-recalculate-regexp))
  6753. (org-table-recalculate) t))
  6754. (defvar org-table-formula-debug nil
  6755. "Non-nil means, debug table formulas.
  6756. When nil, simply write \"#ERROR\" in corrupted fields.")
  6757. (defvar modes)
  6758. (defsubst org-set-calc-mode (var &optional value)
  6759. (if (stringp var)
  6760. (setq var (assoc var '(("D" calc-angle-mode deg)
  6761. ("R" calc-angle-mode rad)
  6762. ("F" calc-prefer-frac t)
  6763. ("S" calc-symbolic-mode t)))
  6764. value (nth 2 var) var (nth 1 var)))
  6765. (if (memq var modes)
  6766. (setcar (cdr (memq var modes)) value)
  6767. (cons var (cons value modes)))
  6768. modes)
  6769. (defun org-table-eval-formula (&optional arg equation
  6770. suppress-align suppress-const
  6771. suppress-store suppress-analysis)
  6772. "Replace the table field value at the cursor by the result of a calculation.
  6773. This function makes use of Dave Gillespie's Calc package, in my view the
  6774. most exciting program ever written for GNU Emacs. So you need to have Calc
  6775. installed in order to use this function.
  6776. In a table, this command replaces the value in the current field with the
  6777. result of a formula. It also installs the formula as the \"current\" column
  6778. formula, by storing it in a special line below the table. When called
  6779. with a `C-u' prefix, the current field must ba a named field, and the
  6780. formula is installed as valid in only this specific field.
  6781. When called with two `C-u' prefixes, insert the active equation
  6782. for the field back into the current field, so that it can be
  6783. edited there. This is useful in order to use \\[org-show-reference]
  6784. to check the referenced fields.
  6785. When called, the command first prompts for a formula, which is read in
  6786. the minibuffer. Previously entered formulas are available through the
  6787. history list, and the last used formula is offered as a default.
  6788. These stored formulas are adapted correctly when moving, inserting, or
  6789. deleting columns with the corresponding commands.
  6790. The formula can be any algebraic expression understood by the Calc package.
  6791. For details, see the Org-mode manual.
  6792. This function can also be called from Lisp programs and offers
  6793. additional arguments: EQUATION can be the formula to apply. If this
  6794. argument is given, the user will not be prompted. SUPPRESS-ALIGN is
  6795. used to speed-up recursive calls by by-passing unnecessary aligns.
  6796. SUPPRESS-CONST suppresses the interpretation of constants in the
  6797. formula, assuming that this has been done already outside the function.
  6798. SUPPRESS-STORE means the formula should not be stored, either because
  6799. it is already stored, or because it is a modified equation that should
  6800. not overwrite the stored one."
  6801. (interactive "P")
  6802. (org-table-check-inside-data-field)
  6803. (or suppress-analysis (org-table-get-specials))
  6804. (if (equal arg '(16))
  6805. (let ((eq (org-table-current-field-formula)))
  6806. (or eq (error "No equation active for current field"))
  6807. (org-table-get-field nil eq)
  6808. (org-table-align)
  6809. (setq org-table-may-need-update t))
  6810. (let* (fields
  6811. (ndown (if (integerp arg) arg 1))
  6812. (org-table-automatic-realign nil)
  6813. (case-fold-search nil)
  6814. (down (> ndown 1))
  6815. (formula (if (and equation suppress-store)
  6816. equation
  6817. (org-table-get-formula equation (equal arg '(4)))))
  6818. (n0 (org-table-current-column))
  6819. (modes (copy-sequence org-calc-default-modes))
  6820. (numbers org-table-formula-numbers-only)
  6821. (keep-empty nil)
  6822. n form form0 bw fmt x ev orig c lispp)
  6823. ;; Parse the format string. Since we have a lot of modes, this is
  6824. ;; a lot of work. However, I think calc still uses most of the time.
  6825. (if (string-match ";" formula)
  6826. (let ((tmp (org-split-string formula ";")))
  6827. (setq formula (car tmp)
  6828. fmt (concat (cdr (assoc "%" org-table-local-parameters))
  6829. (nth 1 tmp)))
  6830. (while (string-match "\\([pnfse]\\)\\(-?[0-9]+\\)" fmt)
  6831. (setq c (string-to-char (match-string 1 fmt))
  6832. n (string-to-number (match-string 2 fmt)))
  6833. (if (= c ?p)
  6834. (setq modes (org-set-calc-mode 'calc-internal-prec n))
  6835. (setq modes (org-set-calc-mode
  6836. 'calc-float-format
  6837. (list (cdr (assoc c '((?n . float) (?f . fix)
  6838. (?s . sci) (?e . eng))))
  6839. n))))
  6840. (setq fmt (replace-match "" t t fmt)))
  6841. (if (string-match "[NT]" fmt)
  6842. (setq numbers (equal (match-string 0 fmt) "N")
  6843. fmt (replace-match "" t t fmt)))
  6844. (if (string-match "E" fmt)
  6845. (setq keep-empty t
  6846. fmt (replace-match "" t t fmt)))
  6847. (while (string-match "[DRFS]" fmt)
  6848. (setq modes (org-set-calc-mode (match-string 0 fmt)))
  6849. (setq fmt (replace-match "" t t fmt)))
  6850. (unless (string-match "\\S-" fmt)
  6851. (setq fmt nil))))
  6852. (if (and (not suppress-const) org-table-formula-use-constants)
  6853. (setq formula (org-table-formula-substitute-names formula)))
  6854. (setq orig (or (get-text-property 1 :orig-formula formula) "?"))
  6855. (while (> ndown 0)
  6856. (setq fields (org-split-string
  6857. (org-no-properties
  6858. (buffer-substring (point-at-bol) (point-at-eol)))
  6859. " *| *"))
  6860. (if numbers
  6861. (setq fields (mapcar
  6862. (lambda (x) (number-to-string (string-to-number x)))
  6863. fields)))
  6864. (setq ndown (1- ndown))
  6865. (setq form (copy-sequence formula)
  6866. lispp (and (> (length form) 2)(equal (substring form 0 2) "'(")))
  6867. ;; Check for old vertical references
  6868. (setq form (org-rewrite-old-row-references form))
  6869. ;; Insert complex ranges
  6870. (while (string-match org-table-range-regexp form)
  6871. (setq form
  6872. (replace-match
  6873. (save-match-data
  6874. (org-table-make-reference
  6875. (org-table-get-range (match-string 0 form) nil n0)
  6876. keep-empty numbers lispp))
  6877. t t form)))
  6878. ;; Insert simple ranges
  6879. (while (string-match "\\$\\([0-9]+\\)\\.\\.\\$\\([0-9]+\\)" form)
  6880. (setq form
  6881. (replace-match
  6882. (save-match-data
  6883. (org-table-make-reference
  6884. (org-sublist
  6885. fields (string-to-number (match-string 1 form))
  6886. (string-to-number (match-string 2 form)))
  6887. keep-empty numbers lispp))
  6888. t t form)))
  6889. (setq form0 form)
  6890. ;; Insert the references to fields in same row
  6891. (while (string-match "\\$\\([0-9]+\\)?" form)
  6892. (setq n (if (match-beginning 1)
  6893. (string-to-number (match-string 1 form))
  6894. n0)
  6895. x (nth (1- n) fields))
  6896. (unless x (error "Invalid field specifier \"%s\""
  6897. (match-string 0 form)))
  6898. (setq form (replace-match
  6899. (save-match-data
  6900. (org-table-make-reference x nil numbers lispp))
  6901. t t form)))
  6902. (if lispp
  6903. (setq ev (condition-case nil
  6904. (eval (eval (read form)))
  6905. (error "#ERROR"))
  6906. ev (if (numberp ev) (number-to-string ev) ev))
  6907. (or (fboundp 'calc-eval)
  6908. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))
  6909. (setq ev (calc-eval (cons form modes)
  6910. (if numbers 'num))))
  6911. (when org-table-formula-debug
  6912. (with-output-to-temp-buffer "*Substitution History*"
  6913. (princ (format "Substitution history of formula
  6914. Orig: %s
  6915. $xyz-> %s
  6916. @r$c-> %s
  6917. $1-> %s\n" orig formula form0 form))
  6918. (if (listp ev)
  6919. (princ (format " %s^\nError: %s"
  6920. (make-string (car ev) ?\-) (nth 1 ev)))
  6921. (princ (format "Result: %s\nFormat: %s\nFinal: %s"
  6922. ev (or fmt "NONE")
  6923. (if fmt (format fmt (string-to-number ev)) ev)))))
  6924. (setq bw (get-buffer-window "*Substitution History*"))
  6925. (shrink-window-if-larger-than-buffer bw)
  6926. (unless (and (interactive-p) (not ndown))
  6927. (unless (let (inhibit-redisplay)
  6928. (y-or-n-p "Debugging Formula. Continue to next? "))
  6929. (org-table-align)
  6930. (error "Abort"))
  6931. (delete-window bw)
  6932. (message "")))
  6933. (if (listp ev) (setq fmt nil ev "#ERROR"))
  6934. (org-table-justify-field-maybe
  6935. (if fmt (format fmt (string-to-number ev)) ev))
  6936. (if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
  6937. (call-interactively 'org-return)
  6938. (setq ndown 0)))
  6939. (and down (org-table-maybe-recalculate-line))
  6940. (or suppress-align (and org-table-may-need-update
  6941. (org-table-align))))))
  6942. (defun org-table-get-range (desc &optional tbeg col highlight)
  6943. "Get a calc vector from a column, accorting to descriptor DESC.
  6944. Optional arguments TBEG and COL can give the beginning of the table and
  6945. the current column, to avoid unnecessary parsing.
  6946. HIGHLIGHT means, just highlight the range."
  6947. (if (not (equal (string-to-char desc) ?@))
  6948. (setq desc (concat "@" desc)))
  6949. (save-excursion
  6950. (or tbeg (setq tbeg (org-table-begin)))
  6951. (or col (setq col (org-table-current-column)))
  6952. (let ((thisline (org-current-line))
  6953. beg end c1 c2 r1 r2 rangep tmp)
  6954. (unless (string-match org-table-range-regexp desc)
  6955. (error "Invalid table range specifier `%s'" desc))
  6956. (setq rangep (match-end 3)
  6957. r1 (and (match-end 1) (match-string 1 desc))
  6958. r2 (and (match-end 4) (match-string 4 desc))
  6959. c1 (and (match-end 2) (substring (match-string 2 desc) 1))
  6960. c2 (and (match-end 5) (substring (match-string 5 desc) 1)))
  6961. (and c1 (setq c1 (+ (string-to-number c1)
  6962. (if (memq (string-to-char c1) '(?- ?+)) col 0))))
  6963. (and c2 (setq c2 (+ (string-to-number c2)
  6964. (if (memq (string-to-char c2) '(?- ?+)) col 0))))
  6965. (if (equal r1 "") (setq r1 nil))
  6966. (if (equal r2 "") (setq r2 nil))
  6967. (if r1 (setq r1 (org-table-get-descriptor-line r1)))
  6968. (if r2 (setq r2 (org-table-get-descriptor-line r2)))
  6969. ; (setq r2 (or r2 r1) c2 (or c2 c1))
  6970. (if (not r1) (setq r1 thisline))
  6971. (if (not r2) (setq r2 thisline))
  6972. (if (not c1) (setq c1 col))
  6973. (if (not c2) (setq c2 col))
  6974. (if (or (not rangep) (and (= r1 r2) (= c1 c2)))
  6975. ;; just one field
  6976. (progn
  6977. (goto-line r1)
  6978. (while (not (looking-at org-table-dataline-regexp))
  6979. (beginning-of-line 2))
  6980. (prog1 (org-table-get-field c1)
  6981. (if highlight (org-table-highlight-rectangle (point) (point)))))
  6982. ;; A range, return a vector
  6983. ;; First sort the numbers to get a regular ractangle
  6984. (if (< r2 r1) (setq tmp r1 r1 r2 r2 tmp))
  6985. (if (< c2 c1) (setq tmp c1 c1 c2 c2 tmp))
  6986. (goto-line r1)
  6987. (while (not (looking-at org-table-dataline-regexp))
  6988. (beginning-of-line 2))
  6989. (org-table-goto-column c1)
  6990. (setq beg (point))
  6991. (goto-line r2)
  6992. (while (not (looking-at org-table-dataline-regexp))
  6993. (beginning-of-line 0))
  6994. (org-table-goto-column c2)
  6995. (setq end (point))
  6996. (if highlight
  6997. (org-table-highlight-rectangle
  6998. beg (progn (skip-chars-forward "^|\n") (point))))
  6999. ;; return string representation of calc vector
  7000. (apply 'append (org-table-copy-region beg end))))))
  7001. (defun org-table-get-descriptor-line (desc &optional cline bline table)
  7002. "Analyze descriptor DESC and retrieve the corresponding line number.
  7003. The cursor is currently in line CLINE, the table begins in line BLINE,
  7004. and TABLE is a vector with line types."
  7005. (if (string-match "^[0-9]+$" desc)
  7006. (aref org-table-dlines (string-to-number desc))
  7007. (setq cline (or cline (org-current-line))
  7008. bline (or bline org-table-current-begin-line)
  7009. table (or table org-table-current-line-types))
  7010. (if (or
  7011. (not (string-match "^\\(\\([-+]\\)?\\(I+\\)\\)?\\(\\([-+]\\)?\\([0-9]+\\)\\)?" desc))
  7012. ;; 1 2 3 4 5 6
  7013. (and (not (match-end 3)) (not (match-end 6)))
  7014. (and (match-end 3) (match-end 6) (not (match-end 5))))
  7015. (error "invalid row descriptor `%s'" desc))
  7016. (let* ((hdir (and (match-end 2) (match-string 2 desc)))
  7017. (hn (if (match-end 3) (- (match-end 3) (match-beginning 3)) nil))
  7018. (odir (and (match-end 5) (match-string 5 desc)))
  7019. (on (if (match-end 6) (string-to-number (match-string 6 desc))))
  7020. (i (- cline bline))
  7021. (rel (and (match-end 6)
  7022. (or (and (match-end 1) (not (match-end 3)))
  7023. (match-end 5)))))
  7024. (if (and hn (not hdir))
  7025. (progn
  7026. (setq i 0 hdir "+")
  7027. (if (eq (aref table 0) 'hline) (setq hn (1- hn)))))
  7028. (if (and (not hn) on (not odir))
  7029. (error "should never happen");;(aref org-table-dlines on) FIXME
  7030. (if (and hn (> hn 0))
  7031. (setq i (org-find-row-type table i 'hline (equal hdir "-") nil hn)))
  7032. (if on
  7033. (setq i (org-find-row-type table i 'dline (equal odir "-") rel on)))
  7034. (+ bline i)))))
  7035. (defun org-find-row-type (table i type backwards relative n)
  7036. (let ((l (length table)))
  7037. (while (> n 0)
  7038. (while (and (setq i (+ i (if backwards -1 1)))
  7039. (>= i 0) (< i l)
  7040. (not (eq (aref table i) type))
  7041. (if (and relative (eq (aref table i) 'hline))
  7042. (progn (setq i (- i (if backwards -1 1)) n 1) nil)
  7043. t)))
  7044. (setq n (1- n)))
  7045. (if (or (< i 0) (>= i l))
  7046. (error "Row descriptior leads outside table")
  7047. i)))
  7048. (defun org-rewrite-old-row-references (s)
  7049. (if (string-match "&[-+0-9I]" s)
  7050. (error "Formula contains old &row reference, please rewrite using @-syntax")
  7051. s))
  7052. (defun org-table-make-reference (elements keep-empty numbers lispp)
  7053. "Convert list ELEMENTS to something appropriate to insert into formula.
  7054. KEEP-EMPTY indicated to keep empty fields, default is to skip them.
  7055. NUMBERS indicates that everything should be converted to numbers.
  7056. LISPP means to return something appropriate for a Lisp list."
  7057. (if (stringp elements) ; just a single val
  7058. (if lispp
  7059. (prin1-to-string (if numbers (string-to-number elements) elements))
  7060. (if (equal elements "") (setq elements "0"))
  7061. (if numbers (number-to-string (string-to-number elements)) elements))
  7062. (unless keep-empty
  7063. (setq elements
  7064. (delq nil
  7065. (mapcar (lambda (x) (if (string-match "\\S-" x) x nil))
  7066. elements))))
  7067. (setq elements (or elements '("0")))
  7068. (if lispp
  7069. (mapconcat 'prin1-to-string
  7070. (if numbers (mapcar 'string-to-number elements) elements)
  7071. " ")
  7072. (concat "[" (mapconcat
  7073. (lambda (x)
  7074. (if numbers (number-to-string (string-to-number x)) x))
  7075. elements
  7076. ",") "]"))))
  7077. (defun org-table-recalculate (&optional all noalign)
  7078. "Recalculate the current table line by applying all stored formulas.
  7079. With prefix arg ALL, do this for all lines in the table."
  7080. (interactive "P")
  7081. (or (memq this-command org-recalc-commands)
  7082. (setq org-recalc-commands (cons this-command org-recalc-commands)))
  7083. (unless (org-at-table-p) (error "Not at a table"))
  7084. (if (equal all '(16))
  7085. (org-table-iterate)
  7086. (org-table-get-specials)
  7087. (let* ((eqlist (sort (org-table-get-stored-formulas)
  7088. (lambda (a b) (string< (car a) (car b)))))
  7089. (inhibit-redisplay (not debug-on-error))
  7090. (line-re org-table-dataline-regexp)
  7091. (thisline (org-current-line))
  7092. (thiscol (org-table-current-column))
  7093. beg end entry eqlnum eqlname eql (cnt 0) eq a name)
  7094. ;; Insert constants in all formulas
  7095. (setq eqlist
  7096. (mapcar (lambda (x)
  7097. (setcdr x (org-table-formula-substitute-names (cdr x)))
  7098. x)
  7099. eqlist))
  7100. ;; Split the equation list
  7101. (while (setq eq (pop eqlist))
  7102. (if (<= (string-to-char (car eq)) ?9)
  7103. (push eq eqlnum)
  7104. (push eq eqlname)))
  7105. (setq eqlnum (nreverse eqlnum) eqlname (nreverse eqlname))
  7106. (if all
  7107. (progn
  7108. (setq end (move-marker (make-marker) (1+ (org-table-end))))
  7109. (goto-char (setq beg (org-table-begin)))
  7110. (if (re-search-forward org-table-calculate-mark-regexp end t)
  7111. ;; This is a table with marked lines, compute selected lines
  7112. (setq line-re org-table-recalculate-regexp)
  7113. ;; Move forward to the first non-header line
  7114. (if (and (re-search-forward org-table-dataline-regexp end t)
  7115. (re-search-forward org-table-hline-regexp end t)
  7116. (re-search-forward org-table-dataline-regexp end t))
  7117. (setq beg (match-beginning 0))
  7118. nil))) ;; just leave beg where it is
  7119. (setq beg (point-at-bol)
  7120. end (move-marker (make-marker) (1+ (point-at-eol)))))
  7121. (goto-char beg)
  7122. (and all (message "Re-applying formulas to full table..."))
  7123. (while (re-search-forward line-re end t)
  7124. (unless (string-match "^ *[_^!$] *$" (org-table-get-field 1))
  7125. ;; Unprotected line, recalculate
  7126. (and all (message "Re-applying formulas to full table...(line %d)"
  7127. (setq cnt (1+ cnt))))
  7128. (setq org-last-recalc-line (org-current-line))
  7129. (setq eql eqlnum)
  7130. (while (setq entry (pop eql))
  7131. (goto-line org-last-recalc-line)
  7132. (org-table-goto-column (string-to-number (car entry)) nil 'force)
  7133. (org-table-eval-formula nil (cdr entry)
  7134. 'noalign 'nocst 'nostore 'noanalysis))))
  7135. (goto-line thisline)
  7136. (org-table-goto-column thiscol)
  7137. (or noalign (and org-table-may-need-update (org-table-align))
  7138. (and all (message "Re-applying formulas to %d lines...done" cnt)))
  7139. ;; Now do the named fields
  7140. (while (setq eq (pop eqlname))
  7141. (setq name (car eq)
  7142. a (assoc name org-table-named-field-locations))
  7143. (and (not a)
  7144. (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" name)
  7145. (setq a
  7146. (list
  7147. name
  7148. (aref org-table-dlines
  7149. (string-to-number (match-string 1 name)))
  7150. (string-to-number (match-string 2 name)))))
  7151. (when (and a (or all (equal (nth 1 a) thisline)))
  7152. (message "Re-applying formula to field: %s" name)
  7153. (goto-line (nth 1 a))
  7154. (org-table-goto-column (nth 2 a))
  7155. (org-table-eval-formula nil (cdr eq) 'noalign 'nocst
  7156. 'nostore 'noanalysis)))
  7157. ;; back to initial position
  7158. (message "Re-applying formulas...done")
  7159. (goto-line thisline)
  7160. (org-table-goto-column thiscol)
  7161. (or noalign (and org-table-may-need-update (org-table-align))
  7162. (and all (message "Re-applying formulas...done"))))))
  7163. (defun org-table-iterate (&optional arg)
  7164. "Recalculate the table until it does not change anymore."
  7165. (interactive "P")
  7166. (let ((imax (if arg (prefix-numeric-value arg) 10))
  7167. (i 0)
  7168. (lasttbl (buffer-substring (org-table-begin) (org-table-end)))
  7169. thistbl)
  7170. (catch 'exit
  7171. (while (< i imax)
  7172. (setq i (1+ i))
  7173. (org-table-recalculate 'all)
  7174. (setq thistbl (buffer-substring (org-table-begin) (org-table-end)))
  7175. (if (not (string= lasttbl thistbl))
  7176. (setq lasttbl thistbl)
  7177. (if (> i 1)
  7178. (message "Convergence after %d iterations" i)
  7179. (message "Table was already stable"))
  7180. (throw 'exit t)))
  7181. (error "No convergence after %d iterations" i))))
  7182. (defun org-table-formula-substitute-names (f)
  7183. "Replace $const with values in string F."
  7184. (let ((start 0) a (f1 f))
  7185. ;; First, check for column names
  7186. (while (setq start (string-match org-table-column-name-regexp f start))
  7187. (setq start (1+ start))
  7188. (setq a (assoc (match-string 1 f) org-table-column-names))
  7189. (setq f (replace-match (concat "$" (cdr a)) t t f)))
  7190. ;; Parameters and constants
  7191. (setq start 0)
  7192. (while (setq start (string-match "\\$\\([a-zA-Z][a-zA-Z0-9]*\\)" f start))
  7193. (setq start (1+ start))
  7194. (if (setq a (save-match-data
  7195. (org-table-get-constant (match-string 1 f))))
  7196. (setq f (replace-match (concat "(" a ")") t t f))))
  7197. (if org-table-formula-debug
  7198. (put-text-property 0 (length f) :orig-formula f1 f))
  7199. f))
  7200. (defun org-table-get-constant (const)
  7201. "Find the value for a parameter or constant in a formula.
  7202. Parameters get priority."
  7203. (or (cdr (assoc const org-table-local-parameters))
  7204. (cdr (assoc const org-table-formula-constants))
  7205. (and (fboundp 'constants-get) (constants-get const))
  7206. "#UNDEFINED_NAME"))
  7207. (defvar org-edit-formulas-map (make-sparse-keymap))
  7208. (define-key org-edit-formulas-map "\C-c\C-c" 'org-finish-edit-formulas)
  7209. (define-key org-edit-formulas-map "\C-c\C-q" 'org-abort-edit-formulas)
  7210. (define-key org-edit-formulas-map "\C-c?" 'org-show-reference)
  7211. (define-key org-edit-formulas-map [(shift up)] 'org-table-edit-line-up)
  7212. (define-key org-edit-formulas-map [(shift down)] 'org-table-edit-line-down)
  7213. (define-key org-edit-formulas-map [(shift left)] 'org-table-edit-backward-field)
  7214. (define-key org-edit-formulas-map [(shift right)] 'org-table-edit-next-field)
  7215. (define-key org-edit-formulas-map [(meta up)] 'org-table-edit-scroll-down)
  7216. (define-key org-edit-formulas-map [(meta down)] 'org-table-edit-scroll)
  7217. (defvar org-pos)
  7218. (defun org-table-edit-formulas ()
  7219. "Edit the formulas of the current table in a separate buffer."
  7220. (interactive)
  7221. (unless (org-at-table-p) (error "Not at a table"))
  7222. (org-table-get-specials)
  7223. (let ((eql (org-table-get-stored-formulas))
  7224. (pos (move-marker (make-marker) (point)))
  7225. (wc (current-window-configuration))
  7226. entry s)
  7227. (switch-to-buffer-other-window "*Edit Formulas*")
  7228. (erase-buffer)
  7229. (fundamental-mode)
  7230. (org-set-local 'org-pos pos)
  7231. (org-set-local 'org-window-configuration wc)
  7232. (use-local-map org-edit-formulas-map)
  7233. (org-add-hook 'post-command-hook 'org-table-edit-formulas-post-command t t)
  7234. (setq s "# `C-c C-c' to finish, `C-u C-c C-c' to also apply, `C-c C-q' to abort.
  7235. # `M-up/down' to scroll table, `S-up/down' to change line for column formulas\n")
  7236. (put-text-property 0 (length s) 'face 'font-lock-comment-face s)
  7237. (insert s)
  7238. (while (setq entry (pop eql))
  7239. (setq s (concat (if (equal (string-to-char (car entry)) ?@) "" "$")
  7240. (car entry) "=" (cdr entry) "\n"))
  7241. (remove-text-properties 0 (length s) '(face nil) s)
  7242. (insert s))
  7243. (goto-char (point-min))
  7244. (message "Edit formulas and finish with `C-c C-c'.")))
  7245. (defun org-table-edit-formulas-post-command ()
  7246. (let ((win (selected-window)))
  7247. (save-excursion
  7248. (condition-case nil
  7249. (org-show-reference)
  7250. (error nil))
  7251. (select-window win))))
  7252. (defun org-finish-edit-formulas (&optional arg)
  7253. "Parse the buffer for formula definitions and install them.
  7254. With prefix ARG, apply the new formulas to the table."
  7255. (interactive "P")
  7256. (org-table-remove-rectangle-highlight)
  7257. (let ((pos org-pos) eql)
  7258. (setq org-pos nil)
  7259. (goto-char (point-min))
  7260. (while (re-search-forward
  7261. "^\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*?\\) *$"
  7262. nil t)
  7263. (push (cons (if (match-end 2) (match-string 2) (match-string 1))
  7264. (match-string 3)) eql))
  7265. (set-window-configuration org-window-configuration)
  7266. (select-window (get-buffer-window (marker-buffer pos)))
  7267. (goto-char pos)
  7268. (unless (org-at-table-p)
  7269. (error "Lost table position - cannot install formulae"))
  7270. (org-table-store-formulas eql)
  7271. (move-marker pos nil)
  7272. (kill-buffer "*Edit Formulas*")
  7273. (if arg
  7274. (org-table-recalculate 'all)
  7275. (message "New formulas installed - press C-u C-c C-c to apply."))))
  7276. (defun org-abort-edit-formulas ()
  7277. "Abort editing formulas, without installing the changes."
  7278. (interactive)
  7279. (org-table-remove-rectangle-highlight)
  7280. (let ((pos org-pos))
  7281. (set-window-configuration org-window-configuration)
  7282. (select-window (get-buffer-window (marker-buffer pos)))
  7283. (goto-char pos)
  7284. (move-marker pos nil)
  7285. (message "Formula editing aborted without installing changes")))
  7286. (defvar org-show-positions nil)
  7287. (defun org-show-reference (&optional local)
  7288. "Show the location/value of the $ expression at point."
  7289. (interactive)
  7290. (org-table-remove-rectangle-highlight)
  7291. (catch 'exit
  7292. (let ((pos (if local (point) org-pos))
  7293. (face2 'highlight)
  7294. (org-inhibit-highlight-removal t)
  7295. (win (selected-window))
  7296. (org-show-positions nil)
  7297. var name e what match dest)
  7298. (if local (org-table-get-specials))
  7299. (setq what (cond
  7300. ((org-at-regexp-p org-table-range-regexp2) 'range)
  7301. ((org-at-regexp-p "\\$[a-zA-Z][a-zA-Z0-9]*") 'name)
  7302. ((org-at-regexp-p "\\$[0-9]+") 'column)
  7303. ((not local) nil)
  7304. (t (error "No reference at point")))
  7305. match (and what (match-string 0)))
  7306. (when (and match (not (equal (match-beginning 0) (point-at-bol))))
  7307. (org-table-add-rectangle-overlay (match-beginning 0) (match-end 0)
  7308. 'secondary-selection))
  7309. (org-add-hook 'before-change-functions
  7310. 'org-table-remove-rectangle-highlight)
  7311. (if (eq what 'name) (setq var (substring match 1)))
  7312. (when (eq what 'range)
  7313. (or (equal (string-to-char match) ?@) (setq match (concat "@" match)))
  7314. (setq match (org-table-formula-substitute-names match)))
  7315. (unless local
  7316. (save-excursion
  7317. (beginning-of-line 1)
  7318. (when (looking-at "\\(\\$[0-9a-zA-Z]+\\|@[0-9]+\\$[0-9]+\\)=")
  7319. (setq dest (match-string 1))
  7320. (org-table-add-rectangle-overlay
  7321. (match-beginning 1) (match-end 1) face2))))
  7322. (if (and (markerp pos) (marker-buffer pos))
  7323. (if (get-buffer-window (marker-buffer pos))
  7324. (select-window (get-buffer-window (marker-buffer pos)))
  7325. (switch-to-buffer-other-window (get-buffer-window
  7326. (marker-buffer pos)))))
  7327. (goto-char pos)
  7328. (org-table-force-dataline)
  7329. (when dest
  7330. (setq name (substring dest 1))
  7331. (cond
  7332. ((string-match "^\\$[a-zA-Z][a-zA-Z0-9]*" dest)
  7333. (setq e (assoc name org-table-named-field-locations))
  7334. (goto-line (nth 1 e))
  7335. (org-table-goto-column (nth 2 e)))
  7336. ((string-match "^@\\([0-9]+\\)\\$\\([0-9]+\\)" dest)
  7337. (let ((l (string-to-number (match-string 1 dest)))
  7338. (c (string-to-number (match-string 2 dest))))
  7339. (goto-line (aref org-table-dlines l))
  7340. (org-table-goto-column c)))
  7341. (t (org-table-goto-column (string-to-number name))))
  7342. (move-marker pos (point))
  7343. (org-table-highlight-rectangle nil nil face2))
  7344. (cond
  7345. ((equal dest match))
  7346. ((not match))
  7347. ((eq what 'range)
  7348. (condition-case nil
  7349. (save-excursion
  7350. (org-table-get-range match nil nil 'highlight))
  7351. (error nil)))
  7352. ((setq e (assoc var org-table-named-field-locations))
  7353. (goto-line (nth 1 e))
  7354. (org-table-goto-column (nth 2 e))
  7355. (org-table-highlight-rectangle (point) (point))
  7356. (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
  7357. ((setq e (assoc var org-table-column-names))
  7358. (org-table-goto-column (string-to-number (cdr e)))
  7359. (org-table-highlight-rectangle (point) (point))
  7360. (goto-char (org-table-begin))
  7361. (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
  7362. (org-table-end) t)
  7363. (progn
  7364. (goto-char (match-beginning 1))
  7365. (org-table-highlight-rectangle)
  7366. (message "Named column (column %s)" (cdr e)))
  7367. (error "Column name not found")))
  7368. ((eq what 'column)
  7369. ;; column number
  7370. (org-table-goto-column (string-to-number (substring match 1)))
  7371. (org-table-highlight-rectangle (point) (point))
  7372. (message "Column %s" (substring match 1)))
  7373. ((setq e (assoc var org-table-local-parameters))
  7374. (goto-char (org-table-begin))
  7375. (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
  7376. (progn
  7377. (goto-char (match-beginning 1))
  7378. (org-table-highlight-rectangle)
  7379. (message "Local parameter."))
  7380. (error "Parameter not found")))
  7381. (t
  7382. (cond
  7383. ((not var) (error "No reference at point"))
  7384. ((setq e (assoc var org-table-formula-constants))
  7385. (message "Constant: $%s=%s in `org-table-formula-constants'."
  7386. var (cdr e)))
  7387. ((setq e (and (fboundp 'constants-get) (constants-get var)))
  7388. (message "Constant: $%s=%s, retrieved from `constants.el'." var e))
  7389. (t (error "Undefined name $%s" var)))))
  7390. (goto-char pos)
  7391. (when org-show-positions
  7392. (push pos org-show-positions)
  7393. (let ((min (apply 'min org-show-positions))
  7394. (max (apply 'max org-show-positions)))
  7395. (when (or (not (pos-visible-in-window-p min))
  7396. (no t(pos-visible-in-window-p max)))
  7397. (goto-char min)
  7398. (set-window-start (selected-window) (point-at-bol))
  7399. (goto-char pos))))
  7400. (select-window win))))
  7401. (defun org-table-force-dataline ()
  7402. "Make sure the cursor is in a dataline in a table."
  7403. (unless (save-excursion
  7404. (beginning-of-line 1)
  7405. (looking-at org-table-dataline-regexp))
  7406. (let* ((re org-table-dataline-regexp)
  7407. (p1 (save-excursion (re-search-forward re nil 'move)))
  7408. (p2 (save-excursion (re-search-backward re nil 'move))))
  7409. (cond ((and p1 p2)
  7410. (goto-char (if (< (abs (- p1 (point))) (abs (- p2 (point))))
  7411. p1 p2)))
  7412. ((or p1 p2) (goto-char (or p1 p2)))
  7413. (t (error "No table dataline around here"))))))
  7414. (defun org-table-edit-line-up ()
  7415. "Move cursor one line up in the window showing the table."
  7416. (interactive)
  7417. (org-table-edit-move 'previous-line))
  7418. (defun org-table-edit-line-down ()
  7419. "Move cursor one line down in the window showing the table."
  7420. (interactive)
  7421. (org-table-edit-move 'next-line))
  7422. (defun org-table-edit-backward-field ()
  7423. "Move cursor one field backward in the window showing the table."
  7424. (interactive)
  7425. (org-table-edit-move 'org-table-previous-field))
  7426. (defun org-table-edit-next-field ()
  7427. "Move cursor one field forward in the window showing the table."
  7428. (interactive)
  7429. (org-table-edit-move 'org-table-next-field))
  7430. (defun org-table-edit-move (command)
  7431. "Move the cursor in the window shoinw the table.
  7432. Use COMMAND to do the motion, repeat if necessary to end up in a data line."
  7433. (let ((org-table-allow-automatic-line-recalculation nil)
  7434. (pos org-pos) (win (selected-window)) p)
  7435. (select-window (get-buffer-window (marker-buffer org-pos)))
  7436. (setq p (point))
  7437. (call-interactively command)
  7438. (while (and (org-at-table-p)
  7439. (org-at-table-hline-p))
  7440. (call-interactively command))
  7441. (or (org-at-table-p) (goto-char p))
  7442. (move-marker pos (point))
  7443. (select-window win)))
  7444. (defun org-table-edit-scroll (N)
  7445. (interactive "p")
  7446. (let ((other-window-scroll-buffer (marker-buffer org-pos)))
  7447. (scroll-other-window N)))
  7448. (defun org-table-edit-scroll-down (N)
  7449. (interactive "p")
  7450. (org-table-edit-scroll (- N)))
  7451. (defvar org-table-rectangle-overlays nil)
  7452. (defun org-table-add-rectangle-overlay (beg end &optional face)
  7453. "Add a new overlay."
  7454. (let ((ov (org-make-overlay beg end)))
  7455. (org-overlay-put ov 'face (or face 'secondary-selection))
  7456. (push ov org-table-rectangle-overlays)))
  7457. (defun org-table-highlight-rectangle (&optional beg end face)
  7458. "Highlight rectangular region in a table."
  7459. (setq beg (or beg (point)) end (or end (point)))
  7460. (let ((b (min beg end))
  7461. (e (max beg end))
  7462. l1 c1 l2 c2 tmp)
  7463. (and (boundp 'org-show-positions)
  7464. (setq org-show-positions (cons b (cons e org-show-positions))))
  7465. (goto-char (min beg end))
  7466. (setq l1 (org-current-line)
  7467. c1 (org-table-current-column))
  7468. (goto-char (max beg end))
  7469. (setq l2 (org-current-line)
  7470. c2 (org-table-current-column))
  7471. (if (> c1 c2) (setq tmp c1 c1 c2 c2 tmp))
  7472. (goto-line l1)
  7473. (beginning-of-line 1)
  7474. (loop for line from l1 to l2 do
  7475. (when (looking-at org-table-dataline-regexp)
  7476. (org-table-goto-column c1)
  7477. (skip-chars-backward "^|\n") (setq beg (point))
  7478. (org-table-goto-column c2)
  7479. (skip-chars-forward "^|\n") (setq end (point))
  7480. (org-table-add-rectangle-overlay beg end face))
  7481. (beginning-of-line 2))
  7482. (goto-char b))
  7483. (add-hook 'before-change-functions 'org-table-remove-rectangle-highlight))
  7484. (defun org-table-remove-rectangle-highlight (&rest ignore)
  7485. "Remove the rectangle overlays."
  7486. (unless org-inhibit-highlight-removal
  7487. (remove-hook 'before-change-functions 'org-table-remove-rectangle-highlight)
  7488. (mapc 'org-delete-overlay org-table-rectangle-overlays)
  7489. (setq org-table-rectangle-overlays nil)))
  7490. ;;; The orgtbl minor mode
  7491. ;; Define a minor mode which can be used in other modes in order to
  7492. ;; integrate the org-mode table editor.
  7493. ;; This is really a hack, because the org-mode table editor uses several
  7494. ;; keys which normally belong to the major mode, for example the TAB and
  7495. ;; RET keys. Here is how it works: The minor mode defines all the keys
  7496. ;; necessary to operate the table editor, but wraps the commands into a
  7497. ;; function which tests if the cursor is currently inside a table. If that
  7498. ;; is the case, the table editor command is executed. However, when any of
  7499. ;; those keys is used outside a table, the function uses `key-binding' to
  7500. ;; look up if the key has an associated command in another currently active
  7501. ;; keymap (minor modes, major mode, global), and executes that command.
  7502. ;; There might be problems if any of the keys used by the table editor is
  7503. ;; otherwise used as a prefix key.
  7504. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  7505. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  7506. ;; addresses this by checking explicitly for both bindings.
  7507. ;; The optimized version (see variable `orgtbl-optimized') takes over
  7508. ;; all keys which are bound to `self-insert-command' in the *global map*.
  7509. ;; Some modes bind other commands to simple characters, for example
  7510. ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
  7511. ;; active, this binding is ignored inside tables and replaced with a
  7512. ;; modified self-insert.
  7513. (defvar orgtbl-mode nil
  7514. "Variable controlling `orgtbl-mode', a minor mode enabling the `org-mode'
  7515. table editor in arbitrary modes.")
  7516. (make-variable-buffer-local 'orgtbl-mode)
  7517. (defvar orgtbl-mode-map (make-keymap)
  7518. "Keymap for `orgtbl-mode'.")
  7519. ;;;###autoload
  7520. (defun turn-on-orgtbl ()
  7521. "Unconditionally turn on `orgtbl-mode'."
  7522. (orgtbl-mode 1))
  7523. (defvar org-old-auto-fill-inhibit-regexp nil
  7524. "Local variable used by `orgtbl-mode'")
  7525. ;;;###autoload
  7526. (defun orgtbl-mode (&optional arg)
  7527. "The `org-mode' table editor as a minor mode for use in other modes."
  7528. (interactive)
  7529. (if (org-mode-p)
  7530. ;; Exit without error, in case some hook functions calls this
  7531. ;; by accident in org-mode.
  7532. (message "Orgtbl-mode is not useful in org-mode, command ignored")
  7533. (setq orgtbl-mode
  7534. (if arg (> (prefix-numeric-value arg) 0) (not orgtbl-mode)))
  7535. (if orgtbl-mode
  7536. (progn
  7537. (and (orgtbl-setup) (defun orgtbl-setup () nil))
  7538. ;; Make sure we are first in minor-mode-map-alist
  7539. (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
  7540. (and c (setq minor-mode-map-alist
  7541. (cons c (delq c minor-mode-map-alist)))))
  7542. (org-set-local (quote org-table-may-need-update) t)
  7543. (org-add-hook 'before-change-functions 'org-before-change-function
  7544. nil 'local)
  7545. (org-set-local 'org-old-auto-fill-inhibit-regexp
  7546. auto-fill-inhibit-regexp)
  7547. (org-set-local 'auto-fill-inhibit-regexp
  7548. (if auto-fill-inhibit-regexp
  7549. (concat "\\([ \t]*|\\|" auto-fill-inhibit-regexp)
  7550. "[ \t]*|"))
  7551. (org-add-to-invisibility-spec '(org-cwidth))
  7552. (easy-menu-add orgtbl-mode-menu)
  7553. (run-hooks 'orgtbl-mode-hook))
  7554. (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
  7555. (org-cleanup-narrow-column-properties)
  7556. (org-remove-from-invisibility-spec '(org-cwidth))
  7557. (remove-hook 'before-change-functions 'org-before-change-function t)
  7558. (easy-menu-remove orgtbl-mode-menu)
  7559. (force-mode-line-update 'all))))
  7560. (defun org-cleanup-narrow-column-properties ()
  7561. "Remove all properties related to narrow-column invisibility."
  7562. (let ((s 1))
  7563. (while (setq s (text-property-any s (point-max)
  7564. 'display org-narrow-column-arrow))
  7565. (remove-text-properties s (1+ s) '(display t)))
  7566. (setq s 1)
  7567. (while (setq s (text-property-any s (point-max) 'org-cwidth 1))
  7568. (remove-text-properties s (1+ s) '(org-cwidth t)))
  7569. (setq s 1)
  7570. (while (setq s (text-property-any s (point-max) 'invisible 'org-cwidth))
  7571. (remove-text-properties s (1+ s) '(invisible t)))))
  7572. ;; Install it as a minor mode.
  7573. (put 'orgtbl-mode :included t)
  7574. (put 'orgtbl-mode :menu-tag "Org Table Mode")
  7575. (add-minor-mode 'orgtbl-mode " OrgTbl" orgtbl-mode-map)
  7576. (defun orgtbl-make-binding (fun n &rest keys)
  7577. "Create a function for binding in the table minor mode.
  7578. FUN is the command to call inside a table. N is used to create a unique
  7579. command name. KEYS are keys that should be checked in for a command
  7580. to execute outside of tables."
  7581. (eval
  7582. (list 'defun
  7583. (intern (concat "orgtbl-hijacker-command-" (int-to-string n)))
  7584. '(arg)
  7585. (concat "In tables, run `" (symbol-name fun) "'.\n"
  7586. "Outside of tables, run the binding of `"
  7587. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7588. "'.")
  7589. '(interactive "p")
  7590. (list 'if
  7591. '(org-at-table-p)
  7592. (list 'call-interactively (list 'quote fun))
  7593. (list 'let '(orgtbl-mode)
  7594. (list 'call-interactively
  7595. (append '(or)
  7596. (mapcar (lambda (k)
  7597. (list 'key-binding k))
  7598. keys)
  7599. '('orgtbl-error))))))))
  7600. (defun orgtbl-error ()
  7601. "Error when there is no default binding for a table key."
  7602. (interactive)
  7603. (error "This key is has no function outside tables"))
  7604. (defun orgtbl-setup ()
  7605. "Setup orgtbl keymaps."
  7606. (let ((nfunc 0)
  7607. (bindings
  7608. (list
  7609. '([(meta shift left)] org-table-delete-column)
  7610. '([(meta left)] org-table-move-column-left)
  7611. '([(meta right)] org-table-move-column-right)
  7612. '([(meta shift right)] org-table-insert-column)
  7613. '([(meta shift up)] org-table-kill-row)
  7614. '([(meta shift down)] org-table-insert-row)
  7615. '([(meta up)] org-table-move-row-up)
  7616. '([(meta down)] org-table-move-row-down)
  7617. '("\C-c\C-w" org-table-cut-region)
  7618. '("\C-c\M-w" org-table-copy-region)
  7619. '("\C-c\C-y" org-table-paste-rectangle)
  7620. '("\C-c-" org-table-insert-hline)
  7621. ; '([(shift tab)] org-table-previous-field)
  7622. '("\C-m" org-table-next-row)
  7623. (list (org-key 'S-return) 'org-table-copy-down)
  7624. '([(meta return)] org-table-wrap-region)
  7625. '("\C-c\C-q" org-table-wrap-region)
  7626. '("\C-c?" org-table-field-info)
  7627. '("\C-c " org-table-blank-field)
  7628. '("\C-c+" org-table-sum)
  7629. '("\C-c=" org-table-eval-formula)
  7630. '("\C-c'" org-table-edit-formulas)
  7631. '("\C-c`" org-table-edit-field)
  7632. '("\C-c*" org-table-recalculate)
  7633. '("\C-c|" org-table-create-or-convert-from-region)
  7634. '("\C-c^" org-table-sort-lines)
  7635. '([(control ?#)] org-table-rotate-recalc-marks)))
  7636. elt key fun cmd)
  7637. (while (setq elt (pop bindings))
  7638. (setq nfunc (1+ nfunc))
  7639. (setq key (car elt)
  7640. fun (nth 1 elt)
  7641. cmd (orgtbl-make-binding fun nfunc key))
  7642. (define-key orgtbl-mode-map key cmd))
  7643. ;; Special treatment needed for TAB and RET
  7644. (define-key orgtbl-mode-map [(return)]
  7645. (orgtbl-make-binding 'orgtbl-ret 100 [(return)] "\C-m"))
  7646. (define-key orgtbl-mode-map "\C-m"
  7647. (orgtbl-make-binding 'orgtbl-ret 101 "\C-m" [(return)]))
  7648. (define-key orgtbl-mode-map [(tab)]
  7649. (orgtbl-make-binding 'orgtbl-tab 102 [(tab)] "\C-i"))
  7650. (define-key orgtbl-mode-map "\C-i"
  7651. (orgtbl-make-binding 'orgtbl-tab 103 "\C-i" [(tab)])))
  7652. (define-key orgtbl-mode-map "\C-i"
  7653. (orgtbl-make-binding 'orgtbl-tab 104 [(shift tab)]))
  7654. (define-key orgtbl-mode-map "\C-c\C-c"
  7655. (orgtbl-make-binding 'org-ctrl-c-ctrl-c 105 "\C-c\C-c"))
  7656. (when orgtbl-optimized
  7657. ;; If the user wants maximum table support, we need to hijack
  7658. ;; some standard editing functions
  7659. (org-remap orgtbl-mode-map
  7660. 'self-insert-command 'orgtbl-self-insert-command
  7661. 'delete-char 'org-delete-char
  7662. 'delete-backward-char 'org-delete-backward-char)
  7663. (define-key orgtbl-mode-map "|" 'org-force-self-insert))
  7664. (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
  7665. '("OrgTbl"
  7666. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
  7667. ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
  7668. ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
  7669. ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
  7670. "--"
  7671. ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
  7672. ["Edit Field" org-table-edit-field :active (org-at-table-p) :keys "C-c ` "]
  7673. ["Copy Field from Above"
  7674. org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
  7675. "--"
  7676. ("Column"
  7677. ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
  7678. ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
  7679. ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
  7680. ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"]
  7681. "--"
  7682. ["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])
  7683. ("Row"
  7684. ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
  7685. ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
  7686. ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
  7687. ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
  7688. ["Sort lines in region" org-table-sort-lines (org-at-table-p) :keys "C-c ^"]
  7689. "--"
  7690. ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
  7691. ("Rectangle"
  7692. ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
  7693. ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
  7694. ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
  7695. ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
  7696. "--"
  7697. ["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
  7698. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  7699. ["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
  7700. ["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
  7701. ["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
  7702. ["Iterate all" (org-table-recalculate '(16)) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  7703. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
  7704. ["Sum Column/Rectangle" org-table-sum
  7705. :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
  7706. ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
  7707. ["Debug Formulas"
  7708. (setq org-table-formula-debug (not org-table-formula-debug))
  7709. :style toggle :selected org-table-formula-debug]
  7710. ))
  7711. t)
  7712. (defun orgtbl-tab (arg)
  7713. "Justification and field motion for `orgtbl-mode'."
  7714. (interactive "P")
  7715. (if arg (org-table-edit-field t)
  7716. (org-table-justify-field-maybe)
  7717. (org-table-next-field)))
  7718. (defun orgtbl-ret ()
  7719. "Justification and field motion for `orgtbl-mode'."
  7720. (interactive)
  7721. (org-table-justify-field-maybe)
  7722. (org-table-next-row))
  7723. (defun orgtbl-self-insert-command (N)
  7724. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  7725. If the cursor is in a table looking at whitespace, the whitespace is
  7726. overwritten, and the table is not marked as requiring realignment."
  7727. (interactive "p")
  7728. (if (and (org-at-table-p)
  7729. (or
  7730. (and org-table-auto-blank-field
  7731. (member last-command
  7732. '(orgtbl-hijacker-command-100
  7733. orgtbl-hijacker-command-101
  7734. orgtbl-hijacker-command-102
  7735. orgtbl-hijacker-command-103
  7736. orgtbl-hijacker-command-104
  7737. orgtbl-hijacker-command-105))
  7738. (org-table-blank-field))
  7739. t)
  7740. (eq N 1)
  7741. (looking-at "[^|\n]* +|"))
  7742. (let (org-table-may-need-update)
  7743. (goto-char (1- (match-end 0)))
  7744. (delete-backward-char 1)
  7745. (goto-char (match-beginning 0))
  7746. (self-insert-command N))
  7747. (setq org-table-may-need-update t)
  7748. (let (orgtbl-mode)
  7749. (call-interactively (key-binding (vector last-input-event))))))
  7750. (defun org-force-self-insert (N)
  7751. "Needed to enforce self-insert under remapping."
  7752. (interactive "p")
  7753. (self-insert-command N))
  7754. ;;;; Link Stuff
  7755. ;;; Link abbreviations
  7756. (defun org-link-expand-abbrev (link)
  7757. "Apply replacements as defined in `org-link-abbrev-alist."
  7758. (if (string-match "^\\([a-zA-Z]+\\)\\(::?\\(.*\\)\\)?$" link)
  7759. (let* ((key (match-string 1 link))
  7760. (as (or (assoc key org-link-abbrev-alist-local)
  7761. (assoc key org-link-abbrev-alist)))
  7762. (tag (and (match-end 2) (match-string 3 link)))
  7763. rpl)
  7764. (if (not as)
  7765. link
  7766. (setq rpl (cdr as))
  7767. (cond
  7768. ((symbolp rpl) (funcall rpl tag))
  7769. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  7770. (t (concat rpl tag)))))
  7771. link))
  7772. ;;; Storing and inserting links
  7773. (defvar org-insert-link-history nil
  7774. "Minibuffer history for links inserted with `org-insert-link'.")
  7775. (defvar org-stored-links nil
  7776. "Contains the links stored with `org-store-link'.")
  7777. (defvar org-store-link-plist nil
  7778. "Plist with info about the most recently link created with `org-store-link'.")
  7779. ;;;###autoload
  7780. (defun org-store-link (arg)
  7781. "\\<org-mode-map>Store an org-link to the current location.
  7782. This link can later be inserted into an org-buffer with
  7783. \\[org-insert-link].
  7784. For some link types, a prefix arg is interpreted:
  7785. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  7786. For file links, arg negates `org-context-in-file-links'."
  7787. (interactive "P")
  7788. (setq org-store-link-plist nil) ; reset
  7789. (let (link cpltxt desc description search txt (pos (point)))
  7790. (cond
  7791. ((eq major-mode 'bbdb-mode)
  7792. (let ((name (bbdb-record-name (bbdb-current-record)))
  7793. (company (bbdb-record-company (bbdb-current-record))))
  7794. (setq cpltxt (concat "bbdb:" (or name company))
  7795. link (org-make-link cpltxt))
  7796. (org-store-link-props :type "bbdb" :name name :company company)))
  7797. ((eq major-mode 'Info-mode)
  7798. (setq link (org-make-link "info:"
  7799. (file-name-nondirectory Info-current-file)
  7800. ":" Info-current-node))
  7801. (setq cpltxt (concat (file-name-nondirectory Info-current-file)
  7802. ":" Info-current-node))
  7803. (org-store-link-props :type "info" :file Info-current-file
  7804. :node Info-current-node))
  7805. ((eq major-mode 'calendar-mode)
  7806. (let ((cd (calendar-cursor-to-date)))
  7807. (setq link
  7808. (format-time-string
  7809. (car org-time-stamp-formats)
  7810. (apply 'encode-time
  7811. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7812. nil nil nil))))
  7813. (org-store-link-props :type "calendar" :date cd)))
  7814. ((or (eq major-mode 'vm-summary-mode)
  7815. (eq major-mode 'vm-presentation-mode))
  7816. (and (eq major-mode 'vm-presentation-mode) (vm-summarize))
  7817. (vm-follow-summary-cursor)
  7818. (save-excursion
  7819. (vm-select-folder-buffer)
  7820. (let* ((message (car vm-message-pointer))
  7821. (folder buffer-file-name)
  7822. (subject (vm-su-subject message))
  7823. (to (vm-get-header-contents message "To")) ; FIXME: untested
  7824. (from (vm-get-header-contents message "From")) ; FIXME: untested
  7825. (message-id (vm-su-message-id message)))
  7826. (org-store-link-props :type "vm" :from from :to to :subject subject
  7827. :message-id message-id)
  7828. (setq message-id (org-remove-angle-brackets message-id))
  7829. (setq folder (abbreviate-file-name folder))
  7830. (if (string-match (concat "^" (regexp-quote vm-folder-directory))
  7831. folder)
  7832. (setq folder (replace-match "" t t folder)))
  7833. (setq cpltxt (org-email-link-description))
  7834. (setq link (org-make-link "vm:" folder "#" message-id)))))
  7835. ((eq major-mode 'wl-summary-mode)
  7836. (let* ((msgnum (wl-summary-message-number))
  7837. (message-id (elmo-message-field wl-summary-buffer-elmo-folder
  7838. msgnum 'message-id))
  7839. (wl-message-entity
  7840. (if (fboundp 'elmo-message-entity)
  7841. (elmo-message-entity
  7842. wl-summary-buffer-elmo-folder msgnum)
  7843. (elmo-msgdb-overview-get-entity
  7844. msgnum (wl-summary-buffer-msgdb))))
  7845. (from (wl-summary-line-from))
  7846. (subject (wl-summary-line-subject))) ; FIXME: untested
  7847. (org-store-link-props :type "wl" :from from :to "???"
  7848. :subject subject :message-id message-id)
  7849. (setq message-id (org-remove-angle-brackets message-id))
  7850. (setq cpltxt (org-email-link-description))
  7851. (setq link (org-make-link "wl:" wl-summary-buffer-folder-name
  7852. "#" message-id))))
  7853. ((or (equal major-mode 'mh-folder-mode)
  7854. (equal major-mode 'mh-show-mode))
  7855. (let ((from (org-mhe-get-header "From:"))
  7856. (to (org-mhe-get-header "To:"))
  7857. (message-id (org-mhe-get-header "Message-Id:"))
  7858. (subject (org-mhe-get-header "Subject:")))
  7859. (org-store-link-props :type "mh" :from from :to to
  7860. :subject subject :message-id message-id)
  7861. (setq cpltxt (org-email-link-description))
  7862. (setq link (org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
  7863. (org-remove-angle-brackets message-id)))))
  7864. ((eq major-mode 'rmail-mode)
  7865. (save-excursion
  7866. (save-restriction
  7867. (rmail-narrow-to-non-pruned-header)
  7868. (let ((folder buffer-file-name)
  7869. (message-id (mail-fetch-field "message-id"))
  7870. (from (mail-fetch-field "from"))
  7871. (to (mail-fetch-field "to"))
  7872. (subject (mail-fetch-field "subject")))
  7873. (org-store-link-props
  7874. :type "rmail" :from from :to to
  7875. :subject subject :message-id message-id)
  7876. (setq message-id (org-remove-angle-brackets message-id))
  7877. (setq cpltxt (org-email-link-description))
  7878. (setq link (org-make-link "rmail:" folder "#" message-id))))))
  7879. ((eq major-mode 'gnus-group-mode)
  7880. (let ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
  7881. (gnus-group-group-name)) ; version
  7882. ((fboundp 'gnus-group-name)
  7883. (gnus-group-name))
  7884. (t "???"))))
  7885. (unless group (error "Not on a group"))
  7886. (org-store-link-props :type "gnus" :group group)
  7887. (setq cpltxt (concat
  7888. (if (org-xor arg org-usenet-links-prefer-google)
  7889. "http://groups.google.com/groups?group="
  7890. "gnus:")
  7891. group)
  7892. link (org-make-link cpltxt))))
  7893. ((memq major-mode '(gnus-summary-mode gnus-article-mode))
  7894. (and (eq major-mode 'gnus-article-mode) (gnus-article-show-summary))
  7895. (let* ((group gnus-newsgroup-name)
  7896. (article (gnus-summary-article-number))
  7897. (header (gnus-summary-article-header article))
  7898. (from (mail-header-from header))
  7899. (message-id (mail-header-id header))
  7900. (date (mail-header-date header))
  7901. (subject (gnus-summary-subject-string)))
  7902. (org-store-link-props :type "gnus" :from from :subject subject
  7903. :message-id message-id :group group)
  7904. (setq cpltxt (org-email-link-description))
  7905. (if (org-xor arg org-usenet-links-prefer-google)
  7906. (setq link
  7907. (concat
  7908. cpltxt "\n "
  7909. (format "http://groups.google.com/groups?as_umsgid=%s"
  7910. (org-fixup-message-id-for-http message-id))))
  7911. (setq link (org-make-link "gnus:" group
  7912. "#" (number-to-string article))))))
  7913. ((eq major-mode 'w3-mode)
  7914. (setq cpltxt (url-view-url t)
  7915. link (org-make-link cpltxt))
  7916. (org-store-link-props :type "w3" :url (url-view-url t)))
  7917. ((eq major-mode 'w3m-mode)
  7918. (setq cpltxt (or w3m-current-title w3m-current-url)
  7919. link (org-make-link w3m-current-url))
  7920. (org-store-link-props :type "w3m" :url (url-view-url t)))
  7921. ((setq search (run-hook-with-args-until-success
  7922. 'org-create-file-search-functions))
  7923. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  7924. "::" search))
  7925. (setq cpltxt (or description link)))
  7926. ((eq major-mode 'image-mode)
  7927. (setq cpltxt (concat "file:"
  7928. (abbreviate-file-name buffer-file-name))
  7929. link (org-make-link cpltxt))
  7930. (org-store-link-props :type "image" :file buffer-file-name))
  7931. ((eq major-mode 'dired-mode)
  7932. ;; link to the file in the current line
  7933. (setq cpltxt (concat "file:"
  7934. (abbreviate-file-name
  7935. (expand-file-name
  7936. (dired-get-filename nil t))))
  7937. link (org-make-link cpltxt)))
  7938. ((and buffer-file-name (org-mode-p))
  7939. ;; Just link to current headline
  7940. (setq cpltxt (concat "file:"
  7941. (abbreviate-file-name buffer-file-name)))
  7942. ;; Add a context search string
  7943. (when (org-xor org-context-in-file-links arg)
  7944. ;; Check if we are on a target
  7945. (if (save-excursion
  7946. (skip-chars-forward "^>\n\r")
  7947. (and (re-search-backward "<<" nil t)
  7948. (looking-at "<<\\(.*?\\)>>")
  7949. (<= (match-beginning 0) pos)
  7950. (>= (match-end 0) pos)))
  7951. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  7952. (setq txt (cond
  7953. ((org-on-heading-p) nil)
  7954. ((org-region-active-p)
  7955. (buffer-substring (region-beginning) (region-end)))
  7956. (t (buffer-substring (point-at-bol) (point-at-eol)))))
  7957. (when (or (null txt) (string-match "\\S-" txt))
  7958. (setq cpltxt
  7959. (concat cpltxt "::"
  7960. (if org-file-link-context-use-camel-case
  7961. (org-make-org-heading-camel txt)
  7962. (org-make-org-heading-search-string txt)))
  7963. desc "NONE"))))
  7964. (if (string-match "::\\'" cpltxt)
  7965. (setq cpltxt (substring cpltxt 0 -2)))
  7966. (setq link (org-make-link cpltxt)))
  7967. (buffer-file-name
  7968. ;; Just link to this file here.
  7969. (setq cpltxt (concat "file:"
  7970. (abbreviate-file-name buffer-file-name)))
  7971. ;; Add a context string
  7972. (when (org-xor org-context-in-file-links arg)
  7973. (setq txt (if (org-region-active-p)
  7974. (buffer-substring (region-beginning) (region-end))
  7975. (buffer-substring (point-at-bol) (point-at-eol))))
  7976. ;; Only use search option if there is some text.
  7977. (when (string-match "\\S-" txt)
  7978. (setq cpltxt
  7979. (concat cpltxt "::"
  7980. (if org-file-link-context-use-camel-case
  7981. (org-make-org-heading-camel txt)
  7982. (org-make-org-heading-search-string txt)))
  7983. desc "NONE")))
  7984. (setq link (org-make-link cpltxt)))
  7985. ((interactive-p)
  7986. (error "Cannot link to a buffer which is not visiting a file"))
  7987. (t (setq link nil)))
  7988. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  7989. (setq link (or link cpltxt)
  7990. desc (or desc cpltxt))
  7991. (if (equal desc "NONE") (setq desc nil))
  7992. (if (and (interactive-p) link)
  7993. (progn
  7994. (setq org-stored-links
  7995. (cons (list cpltxt link desc) org-stored-links))
  7996. (message "Stored: %s" (or cpltxt link)))
  7997. (org-make-link-string link desc))))
  7998. (defun org-store-link-props (&rest plist)
  7999. "Store link properties, extract names and addresses."
  8000. (let (x adr)
  8001. (when (setq x (plist-get plist :from))
  8002. (setq adr (mail-extract-address-components x))
  8003. (plist-put plist :fromname (car adr))
  8004. (plist-put plist :fromaddress (nth 1 adr)))
  8005. (when (setq x (plist-get plist :to))
  8006. (setq adr (mail-extract-address-components x))
  8007. (plist-put plist :toname (car adr))
  8008. (plist-put plist :toaddress (nth 1 adr))))
  8009. (let ((from (plist-get plist :from))
  8010. (to (plist-get plist :to)))
  8011. (when (and from to org-from-is-user-regexp)
  8012. (plist-put plist :fromto
  8013. (if (string-match org-from-is-user-regexp from)
  8014. (concat "to %t")
  8015. (concat "from %f")))))
  8016. (setq org-store-link-plist plist))
  8017. (defun org-email-link-description (&optional fmt)
  8018. "Return the description part of an email link.
  8019. This takes information from `org-store-link-plist' and formats it
  8020. according to FMT (default from `org-email-link-description-format')."
  8021. (setq fmt (or fmt org-email-link-description-format))
  8022. (let* ((p org-store-link-plist)
  8023. (to (plist-get p :toaddress))
  8024. (from (plist-get p :fromaddress))
  8025. (table
  8026. (list
  8027. (cons "%c" (plist-get p :fromto))
  8028. (cons "%F" (plist-get p :from))
  8029. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8030. (cons "%T" (plist-get p :to))
  8031. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8032. (cons "%s" (plist-get p :subject))
  8033. (cons "%m" (plist-get p :message-id)))))
  8034. (when (string-match "%c" fmt)
  8035. ;; Check if the user wrote this message
  8036. (if (and org-from-is-user-regexp from to
  8037. (save-match-data (string-match org-from-is-user-regexp from)))
  8038. (setq fmt (replace-match "to %t" t t fmt))
  8039. (setq fmt (replace-match "from %f" t t fmt))))
  8040. (org-replace-escapes fmt table)))
  8041. (defun org-make-org-heading-search-string (&optional string heading)
  8042. "Make search string for STRING or current headline."
  8043. (interactive)
  8044. (let ((s (or string (org-get-heading))))
  8045. (unless (and string (not heading))
  8046. ;; We are using a headline, clean up garbage in there.
  8047. (if (string-match org-todo-regexp s)
  8048. (setq s (replace-match "" t t s)))
  8049. (if (string-match ":[a-zA-Z_@0-9:]+:[ \t]*$" s)
  8050. (setq s (replace-match "" t t s)))
  8051. (setq s (org-trim s))
  8052. (if (string-match (concat "^\\(" org-quote-string "\\|"
  8053. org-comment-string "\\)") s)
  8054. (setq s (replace-match "" t t s)))
  8055. (while (string-match org-ts-regexp s)
  8056. (setq s (replace-match "" t t s))))
  8057. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  8058. (setq s (replace-match " " t t s)))
  8059. (or string (setq s (concat "*" s))) ; Add * for headlines
  8060. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8061. (defun org-make-org-heading-camel (&optional string heading)
  8062. "Make a CamelCase string for STRING or the current headline."
  8063. (interactive)
  8064. (let ((s (or string (org-get-heading))))
  8065. (unless (and string (not heading))
  8066. ;; We are using a headline, clean up garbage in there.
  8067. (if (string-match org-todo-regexp s)
  8068. (setq s (replace-match "" t t s)))
  8069. (if (string-match ":[a-zA-Z_@0-9:]+:[ \t]*$" s)
  8070. (setq s (replace-match "" t t s)))
  8071. (setq s (org-trim s))
  8072. (if (string-match (concat "^\\(" org-quote-string "\\|"
  8073. org-comment-string "\\)") s)
  8074. (setq s (replace-match "" t t s)))
  8075. (while (string-match org-ts-regexp s)
  8076. (setq s (replace-match "" t t s))))
  8077. (while (string-match "[^a-zA-Z_ \t]+" s)
  8078. (setq s (replace-match " " t t s)))
  8079. (or string (setq s (concat "*" s))) ; Add * for headlines
  8080. (mapconcat 'capitalize (org-split-string s "[ \t]+") "")))
  8081. (defun org-make-link (&rest strings)
  8082. "Concatenate STRINGS, format resulting string with `org-link-format'."
  8083. (format org-link-format (apply 'concat strings)))
  8084. (defun org-make-link-string (link &optional description)
  8085. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8086. (if (eq org-link-style 'plain)
  8087. (if (equal description link)
  8088. link
  8089. (concat description "\n" link))
  8090. (when (stringp description)
  8091. ;; Remove brackets from the description, they are fatal.
  8092. (while (string-match "\\[\\|\\]" description)
  8093. (setq description (replace-match "" t t description))))
  8094. (when (equal (org-link-escape link) description)
  8095. ;; No description needed, it is identical
  8096. (setq description nil))
  8097. (when (and (not description)
  8098. (not (equal link (org-link-escape link))))
  8099. (setq description link))
  8100. (concat "[[" (org-link-escape link) "]"
  8101. (if description (concat "[" description "]") "")
  8102. "]")))
  8103. (defconst org-link-escape-chars '(("[" . "%5B") ("]" . "%5D") (" " . "%20"))
  8104. "Association list of escapes for some characters problematic in links.")
  8105. (defun org-link-escape (text)
  8106. "Escape charaters in TEXT that are problematic for links."
  8107. (when text
  8108. (let ((re (mapconcat (lambda (x) (regexp-quote (car x)))
  8109. org-link-escape-chars "\\|")))
  8110. (while (string-match re text)
  8111. (setq text
  8112. (replace-match
  8113. (cdr (assoc (match-string 0 text) org-link-escape-chars))
  8114. t t text)))
  8115. text)))
  8116. (defun org-link-unescape (text)
  8117. "Reverse the action of `org-link-escape'."
  8118. (when text
  8119. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  8120. org-link-escape-chars "\\|")))
  8121. (while (string-match re text)
  8122. (setq text
  8123. (replace-match
  8124. (car (rassoc (match-string 0 text) org-link-escape-chars))
  8125. t t text)))
  8126. text)))
  8127. (defun org-xor (a b)
  8128. "Exclusive or."
  8129. (if a (not b) b))
  8130. (defun org-get-header (header)
  8131. "Find a header field in the current buffer."
  8132. (save-excursion
  8133. (goto-char (point-min))
  8134. (let ((case-fold-search t) s)
  8135. (cond
  8136. ((eq header 'from)
  8137. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  8138. (setq s (match-string 1)))
  8139. (while (string-match "\"" s)
  8140. (setq s (replace-match "" t t s)))
  8141. (if (string-match "[<(].*" s)
  8142. (setq s (replace-match "" t t s))))
  8143. ((eq header 'message-id)
  8144. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  8145. (setq s (match-string 1))))
  8146. ((eq header 'subject)
  8147. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  8148. (setq s (match-string 1)))))
  8149. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  8150. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  8151. s)))
  8152. (defun org-fixup-message-id-for-http (s)
  8153. "Replace special characters in a message id, so it can be used in an http query."
  8154. (while (string-match "<" s)
  8155. (setq s (replace-match "%3C" t t s)))
  8156. (while (string-match ">" s)
  8157. (setq s (replace-match "%3E" t t s)))
  8158. (while (string-match "@" s)
  8159. (setq s (replace-match "%40" t t s)))
  8160. s)
  8161. (defun org-insert-link (&optional complete-file)
  8162. "Insert a link. At the prompt, enter the link.
  8163. Completion can be used to select a link previously stored with
  8164. `org-store-link'. When the empty string is entered (i.e. if you just
  8165. press RET at the prompt), the link defaults to the most recently
  8166. stored link. As SPC triggers completion in the minibuffer, you need to
  8167. use M-SPC or C-q SPC to force the insertion of a space character.
  8168. You will also be prompted for a description, and if one is given, it will
  8169. be displayed in the buffer instead of the link.
  8170. If there is already a link at point, this command will allow you to edit link
  8171. and description parts.
  8172. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
  8173. selected using completion. The path to the file will be relative to
  8174. the current directory if the file is in the current directory or a
  8175. subdirectory. Otherwise, the link will be the absolute path as
  8176. completed in the minibuffer (i.e. normally ~/path/to/file).
  8177. With two \\[universal-argument] prefixes, enforce an absolute path even if the file
  8178. is in the current directory or below.
  8179. With three \\[universal-argument] prefixes, negate the meaning of
  8180. `org-keep-stored-link-after-insertion'."
  8181. (interactive "P")
  8182. (let ((region (if (org-region-active-p)
  8183. (prog1 (buffer-substring (region-beginning) (region-end))
  8184. (delete-region (region-beginning) (region-end)))))
  8185. link desc entry remove file (pos (point)) tmphist)
  8186. (cond
  8187. ((save-excursion
  8188. (skip-chars-forward "^]\n\r")
  8189. (and (re-search-backward "\\[\\[" nil t)
  8190. (looking-at org-bracket-link-regexp)
  8191. (<= (match-beginning 0) pos)
  8192. (>= (match-end 0) pos)))
  8193. ;; We do have a link at point, and we are going to edit it.
  8194. (setq remove (list (match-beginning 0) (match-end 0)))
  8195. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8196. (setq link (read-string "Link: "
  8197. (org-link-unescape
  8198. (org-match-string-no-properties 1)))))
  8199. ((equal complete-file '(4))
  8200. ;; Completing read for file names.
  8201. (setq file (read-file-name "File: "))
  8202. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8203. (pwd1 (file-name-as-directory (abbreviate-file-name
  8204. (expand-file-name ".")))))
  8205. (cond
  8206. ((equal complete-file '(16))
  8207. (setq link (org-make-link
  8208. "file:"
  8209. (abbreviate-file-name (expand-file-name file)))))
  8210. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8211. (setq link (org-make-link "file:" (match-string 1 file))))
  8212. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8213. (expand-file-name file))
  8214. (setq link (org-make-link
  8215. "file:" (match-string 1 (expand-file-name file)))))
  8216. (t (setq link (org-make-link "file:" file))))))
  8217. (t
  8218. ;; Read link, with completion for stored links.
  8219. ;; Fake a link history
  8220. (setq tmphist (append (mapcar 'car org-stored-links)
  8221. org-insert-link-history))
  8222. (setq link (org-completing-read
  8223. "Link: " org-stored-links nil nil nil
  8224. 'tmphist
  8225. (or (car (car org-stored-links)))))
  8226. (setq entry (assoc link org-stored-links))
  8227. (or entry (push link org-insert-link-history))
  8228. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8229. (not org-keep-stored-link-after-insertion))
  8230. (setq org-stored-links (delq (assoc link org-stored-links)
  8231. org-stored-links)))
  8232. (setq link (if entry (nth 1 entry) link)
  8233. desc (or region desc (nth 2 entry)))))
  8234. (if (string-match org-plain-link-re link)
  8235. ;; URL-like link, normalize the use of angular brackets.
  8236. (setq link (org-make-link (org-remove-angle-brackets link))))
  8237. ;; Check if we are linking to the current file with a search option
  8238. ;; If yes, simplify the link by using only the search option.
  8239. (when (and buffer-file-name
  8240. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  8241. (let* ((path (match-string 1 link))
  8242. (case-fold-search nil)
  8243. (search (match-string 2 link)))
  8244. (save-match-data
  8245. (if (equal (file-truename buffer-file-name) (file-truename path))
  8246. ;; We are linking to this same file, with a search option
  8247. (setq link search)))))
  8248. ;; Check if we can/should use a relative path. If yes, simplify the link
  8249. (when (string-match "\\<file:\\(.*\\)" link)
  8250. (let* ((path (match-string 1 link))
  8251. (case-fold-search nil))
  8252. (cond
  8253. ((eq org-link-file-path-type 'absolute)
  8254. (setq path (abbreviate-file-name (expand-file-name path))))
  8255. ((eq org-link-file-path-type 'noabbrev)
  8256. (setq path (expand-file-name path)))
  8257. ((eq org-link-file-path-type 'relative)
  8258. (setq path (file-relative-name path)))
  8259. (t
  8260. (save-match-data
  8261. (if (string-match (concat "^" (regexp-quote
  8262. (file-name-as-directory
  8263. (expand-file-name "."))))
  8264. (expand-file-name path))
  8265. ;; We are linking a file with relative path name.
  8266. (setq path (substring (expand-file-name path)
  8267. (match-end 0)))))))
  8268. (setq link (concat "file:" path))))
  8269. (setq desc (read-string "Description: " desc))
  8270. (unless (string-match "\\S-" desc) (setq desc nil))
  8271. (if remove (apply 'delete-region remove))
  8272. (insert (org-make-link-string link desc))))
  8273. (defun org-completing-read (&rest args)
  8274. (let ((minibuffer-local-completion-map
  8275. (copy-keymap minibuffer-local-completion-map)))
  8276. (define-key minibuffer-local-completion-map " " 'self-insert-command)
  8277. (apply 'completing-read args)))
  8278. ;;; Opening/following a link
  8279. (defvar org-link-search-failed nil)
  8280. (defun org-next-link ()
  8281. "Move forward to the next link.
  8282. If the link is in hidden text, expose it."
  8283. (interactive)
  8284. (when (and org-link-search-failed (eq this-command last-command))
  8285. (goto-char (point-min))
  8286. (message "Link search wrapped back to beginning of buffer"))
  8287. (setq org-link-search-failed nil)
  8288. (let* ((pos (point))
  8289. (ct (org-context))
  8290. (a (assoc :link ct)))
  8291. (if a (goto-char (nth 2 a)))
  8292. (if (re-search-forward org-any-link-re nil t)
  8293. (progn
  8294. (goto-char (match-beginning 0))
  8295. (if (org-invisible-p) (org-show-context)))
  8296. (goto-char pos)
  8297. (setq org-link-search-failed t)
  8298. (error "No further link found"))))
  8299. (defun org-previous-link ()
  8300. "Move backward to the previous link.
  8301. If the link is in hidden text, expose it."
  8302. (interactive)
  8303. (when (and org-link-search-failed (eq this-command last-command))
  8304. (goto-char (point-max))
  8305. (message "Link search wrapped back to end of buffer"))
  8306. (setq org-link-search-failed nil)
  8307. (let* ((pos (point))
  8308. (ct (org-context))
  8309. (a (assoc :link ct)))
  8310. (if a (goto-char (nth 1 a)))
  8311. (if (re-search-backward org-any-link-re nil t)
  8312. (progn
  8313. (goto-char (match-beginning 0))
  8314. (if (org-invisible-p) (org-show-context)))
  8315. (goto-char pos)
  8316. (setq org-link-search-failed t)
  8317. (error "No further link found"))))
  8318. (defun org-find-file-at-mouse (ev)
  8319. "Open file link or URL at mouse."
  8320. (interactive "e")
  8321. (mouse-set-point ev)
  8322. (org-open-at-point 'in-emacs))
  8323. (defun org-open-at-mouse (ev)
  8324. "Open file link or URL at mouse."
  8325. (interactive "e")
  8326. (mouse-set-point ev)
  8327. (org-open-at-point))
  8328. (defvar org-window-config-before-follow-link nil
  8329. "The window configuration before following a link.
  8330. This is saved in case the need arises to restore it.")
  8331. (defun org-open-at-point (&optional in-emacs)
  8332. "Open link at or after point.
  8333. If there is no link at point, this function will search forward up to
  8334. the end of the current subtree.
  8335. Normally, files will be opened by an appropriate application. If the
  8336. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  8337. (interactive "P")
  8338. (setq org-window-config-before-follow-link (current-window-configuration))
  8339. (org-remove-occur-highlights nil nil t)
  8340. (if (org-at-timestamp-p t)
  8341. (org-follow-timestamp-link)
  8342. (let (type path link line search (pos (point)))
  8343. (catch 'match
  8344. (save-excursion
  8345. (skip-chars-forward "^]\n\r")
  8346. (when (and (re-search-backward "\\[\\[" nil t)
  8347. (looking-at org-bracket-link-regexp)
  8348. (<= (match-beginning 0) pos)
  8349. (>= (match-end 0) pos))
  8350. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  8351. (while (string-match " *\n *" link)
  8352. (setq link (replace-match " " t t link)))
  8353. (setq link (org-link-expand-abbrev link))
  8354. (if (string-match org-link-re-with-space2 link)
  8355. (setq type (match-string 1 link)
  8356. path (match-string 2 link))
  8357. (setq type "thisfile"
  8358. path link))
  8359. (throw 'match t)))
  8360. (when (get-text-property (point) 'org-linked-text)
  8361. (setq type "thisfile"
  8362. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8363. (1+ (point)) (point))
  8364. path (buffer-substring
  8365. (previous-single-property-change pos 'org-linked-text)
  8366. (next-single-property-change pos 'org-linked-text)))
  8367. (throw 'match t))
  8368. (save-excursion
  8369. (skip-chars-backward (concat "^[]" org-non-link-chars " "))
  8370. (if (equal (char-before) ?<) (backward-char 1))
  8371. (when (or (looking-at org-angle-link-re)
  8372. (looking-at org-plain-link-re)
  8373. (and (or (re-search-forward org-angle-link-re (point-at-eol) t)
  8374. (re-search-forward org-plain-link-re (point-at-eol) t))
  8375. (<= (match-beginning 0) pos)
  8376. (>= (match-end 0) pos)))
  8377. (setq type (match-string 1)
  8378. path (match-string 2))
  8379. (throw 'match t)))
  8380. (save-excursion
  8381. (skip-chars-backward "^ \t\n\r")
  8382. (when (looking-at "\\(:[A-Za-z_@0-9:]+\\):[ \t\r\n]")
  8383. (setq type "tags"
  8384. path (match-string 1))
  8385. (while (string-match ":" path)
  8386. (setq path (replace-match "+" t t path)))
  8387. (throw 'match t)))
  8388. (save-excursion
  8389. (skip-chars-backward "a-zA-Z_")
  8390. (when (and (memq 'camel org-activate-links)
  8391. (looking-at org-camel-regexp))
  8392. (setq type "camel" path (match-string 0))
  8393. (if (equal (char-before) ?*)
  8394. (setq path (concat "*" path))))
  8395. (throw 'match t)))
  8396. (unless path
  8397. (error "No link found"))
  8398. ;; Remove any trailing spaces in path
  8399. (if (string-match " +\\'" path)
  8400. (setq path (replace-match "" t t path)))
  8401. (cond
  8402. ((equal type "mailto")
  8403. (let ((cmd (car org-link-mailto-program))
  8404. (args (cdr org-link-mailto-program)) args1
  8405. (address path) (subject "") a)
  8406. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  8407. (setq address (match-string 1 path)
  8408. subject (org-link-escape (match-string 2 path))))
  8409. (while args
  8410. (cond
  8411. ((not (stringp (car args))) (push (pop args) args1))
  8412. (t (setq a (pop args))
  8413. (if (string-match "%a" a)
  8414. (setq a (replace-match address t t a)))
  8415. (if (string-match "%s" a)
  8416. (setq a (replace-match subject t t a)))
  8417. (push a args1))))
  8418. (apply cmd (nreverse args1))))
  8419. ((member type '("http" "https" "ftp" "news"))
  8420. (browse-url (concat type ":" path)))
  8421. ((string= type "tags")
  8422. (org-tags-view in-emacs path))
  8423. ((or (string= type "camel")
  8424. (string= type "thisfile"))
  8425. (if in-emacs
  8426. (switch-to-buffer-other-window
  8427. (org-get-buffer-for-internal-link (current-buffer)))
  8428. (org-mark-ring-push))
  8429. (org-link-search
  8430. path
  8431. (cond ((equal in-emacs '(4)) 'occur)
  8432. ((equal in-emacs '(16)) 'org-occur)
  8433. (t nil))
  8434. pos))
  8435. ((string= type "file")
  8436. (if (string-match "::\\([0-9]+\\)\\'" path)
  8437. (setq line (string-to-number (match-string 1 path))
  8438. path (substring path 0 (match-beginning 0)))
  8439. (if (string-match "::\\(.+\\)\\'" path)
  8440. (setq search (match-string 1 path)
  8441. path (substring path 0 (match-beginning 0)))))
  8442. (org-open-file path in-emacs line search))
  8443. ((string= type "news")
  8444. (org-follow-gnus-link path))
  8445. ((string= type "bbdb")
  8446. (org-follow-bbdb-link path))
  8447. ((string= type "info")
  8448. (org-follow-info-link path))
  8449. ((string= type "gnus")
  8450. (let (group article)
  8451. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  8452. (error "Error in Gnus link"))
  8453. (setq group (match-string 1 path)
  8454. article (match-string 3 path))
  8455. (org-follow-gnus-link group article)))
  8456. ((string= type "vm")
  8457. (let (folder article)
  8458. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  8459. (error "Error in VM link"))
  8460. (setq folder (match-string 1 path)
  8461. article (match-string 3 path))
  8462. ;; in-emacs is the prefix arg, will be interpreted as read-only
  8463. (org-follow-vm-link folder article in-emacs)))
  8464. ((string= type "wl")
  8465. (let (folder article)
  8466. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  8467. (error "Error in Wanderlust link"))
  8468. (setq folder (match-string 1 path)
  8469. article (match-string 3 path))
  8470. (org-follow-wl-link folder article)))
  8471. ((string= type "mhe")
  8472. (let (folder article)
  8473. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  8474. (error "Error in MHE link"))
  8475. (setq folder (match-string 1 path)
  8476. article (match-string 3 path))
  8477. (org-follow-mhe-link folder article)))
  8478. ((string= type "rmail")
  8479. (let (folder article)
  8480. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  8481. (error "Error in RMAIL link"))
  8482. (setq folder (match-string 1 path)
  8483. article (match-string 3 path))
  8484. (org-follow-rmail-link folder article)))
  8485. ((string= type "shell")
  8486. (let ((cmd path))
  8487. ;; FIXME: the following is only for backward compatibility
  8488. (while (string-match "@{" cmd) (setq cmd (replace-match "<" t t cmd)))
  8489. (while (string-match "@}" cmd) (setq cmd (replace-match ">" t t cmd)))
  8490. (if (or (not org-confirm-shell-link-function)
  8491. (funcall org-confirm-shell-link-function
  8492. (format "Execute \"%s\" in shell? "
  8493. (org-add-props cmd nil
  8494. 'face 'org-warning))))
  8495. (progn
  8496. (message "Executing %s" cmd)
  8497. (shell-command cmd))
  8498. (error "Abort"))))
  8499. ((string= type "elisp")
  8500. (let ((cmd path))
  8501. (if (or (not org-confirm-elisp-link-function)
  8502. (funcall org-confirm-elisp-link-function
  8503. (format "Execute \"%s\" as elisp? "
  8504. (org-add-props cmd nil
  8505. 'face 'org-warning))))
  8506. (message "%s => %s" cmd (eval (read cmd)))
  8507. (error "Abort"))))
  8508. (t
  8509. (browse-url-at-point))))))
  8510. ;;; File search
  8511. (defvar org-create-file-search-functions nil
  8512. "List of functions to construct the right search string for a file link.
  8513. These functions are called in turn with point at the location to
  8514. which the link should point.
  8515. A function in the hook should first test if it would like to
  8516. handle this file type, for example by checking the major-mode or
  8517. the file extension. If it decides not to handle this file, it
  8518. should just return nil to give other functions a chance. If it
  8519. does handle the file, it must return the search string to be used
  8520. when following the link. The search string will be part of the
  8521. file link, given after a double colon, and `org-open-at-point'
  8522. will automatically search for it. If special measures must be
  8523. taken to make the search successful, another function should be
  8524. added to the companion hook `org-execute-file-search-functions',
  8525. which see.
  8526. A function in this hook may also use `setq' to set the variable
  8527. `description' to provide a suggestion for the descriptive text to
  8528. be used for this link when it gets inserted into an Org-mode
  8529. buffer with \\[org-insert-link].")
  8530. (defvar org-execute-file-search-functions nil
  8531. "List of functions to execute a file search triggered by a link.
  8532. Functions added to this hook must accept a single argument, the
  8533. search string that was part of the file link, the part after the
  8534. double colon. The function must first check if it would like to
  8535. handle this search, for example by checking the major-mode or the
  8536. file extension. If it decides not to handle this search, it
  8537. should just return nil to give other functions a chance. If it
  8538. does handle the search, it must return a non-nil value to keep
  8539. other functions from trying.
  8540. Each function can access the current prefix argument through the
  8541. variable `current-prefix-argument'. Note that a single prefix is
  8542. used to force opening a link in Emacs, so it may be good to only
  8543. use a numeric or double prefix to guide the search function.
  8544. In case this is needed, a function in this hook can also restore
  8545. the window configuration before `org-open-at-point' was called using:
  8546. (set-window-configuration org-window-config-before-follow-link)")
  8547. (defun org-link-search (s &optional type avoid-pos)
  8548. "Search for a link search option.
  8549. When S is a CamelCaseWord, search for a target, or for a sentence containing
  8550. the words. If S is surrounded by forward slashes, it is interpreted as a
  8551. regular expression. In org-mode files, this will create an `org-occur'
  8552. sparse tree. In ordinary files, `occur' will be used to list matches.
  8553. If the current buffer is in `dired-mode', grep will be used to search
  8554. in all files. If AVOID-POS is given, ignore matches near that position."
  8555. (let ((case-fold-search t)
  8556. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  8557. (pos (point))
  8558. (pre "") (post "")
  8559. words re0 re1 re2 re3 re4 re5 re2a reall camel)
  8560. (cond
  8561. ;; First check if there are any special
  8562. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  8563. ;; Now try the builtin stuff
  8564. ((save-excursion
  8565. (goto-char (point-min))
  8566. (and
  8567. (re-search-forward
  8568. (concat "<<" (regexp-quote s0) ">>") nil t)
  8569. (setq pos (match-beginning 0))))
  8570. ;; There is an exact target for this
  8571. (goto-char pos))
  8572. ((string-match "^/\\(.*\\)/$" s)
  8573. ;; A regular expression
  8574. (cond
  8575. ((org-mode-p)
  8576. (org-occur (match-string 1 s)))
  8577. ;;((eq major-mode 'dired-mode)
  8578. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  8579. (t (org-do-occur (match-string 1 s)))))
  8580. ((or (setq camel (string-match (concat "^" org-camel-regexp "$") s))
  8581. t)
  8582. ;; A camel or a normal search string
  8583. (when (equal (string-to-char s) ?*)
  8584. ;; Anchor on headlines, post may include tags.
  8585. (setq pre "^\\*+[ \t]*\\(?:\\sw+\\)?[ \t]*"
  8586. post "[ \t]*\\(?:[ \t]+:[a-zA-Z_@0-9:+]:[ \t]*\\)?$"
  8587. s (substring s 1)))
  8588. (remove-text-properties
  8589. 0 (length s)
  8590. '(face nil mouse-face nil keymap nil fontified nil) s)
  8591. ;; Make a series of regular expressions to find a match
  8592. (setq words
  8593. (if camel
  8594. (org-camel-to-words s)
  8595. (org-split-string s "[ \n\r\t]+"))
  8596. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  8597. re2 (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t]+") "\\)[ \t\r\n]")
  8598. re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  8599. re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  8600. re1 (concat pre re2 post)
  8601. re3 (concat pre re4 post)
  8602. re5 (concat pre ".*" re4)
  8603. re2 (concat pre re2)
  8604. re2a (concat pre re2a)
  8605. re4 (concat pre re4)
  8606. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  8607. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  8608. re5 "\\)"
  8609. ))
  8610. (cond
  8611. ((eq type 'org-occur) (org-occur reall))
  8612. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  8613. (t (goto-char (point-min))
  8614. (if (or (org-search-not-link re0 nil t)
  8615. (org-search-not-link re1 nil t)
  8616. (org-search-not-link re2 nil t)
  8617. (org-search-not-link re2a nil t)
  8618. (org-search-not-link re3 nil t)
  8619. (org-search-not-link re4 nil t)
  8620. (org-search-not-link re5 nil t)
  8621. )
  8622. (goto-char (match-beginning 1))
  8623. (goto-char pos)
  8624. (error "No match")))))
  8625. (t
  8626. ;; Normal string-search
  8627. (goto-char (point-min))
  8628. (if (search-forward s nil t)
  8629. (goto-char (match-beginning 0))
  8630. (error "No match"))))
  8631. (and (org-mode-p) (org-show-context 'link-search))))
  8632. (defun org-search-not-link (&rest args)
  8633. "Execute `re-search-forward', but only accept matches that are not a link."
  8634. (catch 'exit
  8635. (let (p1)
  8636. (while (apply 're-search-forward args)
  8637. (setq p1 (point))
  8638. (if (not (save-match-data
  8639. (and (re-search-backward "\\[\\[" nil t)
  8640. (looking-at org-bracket-link-regexp)
  8641. (<= (match-beginning 0) p1)
  8642. (>= (match-end 0) p1))))
  8643. (progn (goto-char (match-end 0))
  8644. (throw 'exit (point)))
  8645. (goto-char (match-end 0)))))))
  8646. (defun org-get-buffer-for-internal-link (buffer)
  8647. "Return a buffer to be used for displaying the link target of internal links."
  8648. (cond
  8649. ((not org-display-internal-link-with-indirect-buffer)
  8650. buffer)
  8651. ((string-match "(Clone)$" (buffer-name buffer))
  8652. (message "Buffer is already a clone, not making another one")
  8653. ;; we also do not modify visibility in this case
  8654. buffer)
  8655. (t ; make a new indirect buffer for displaying the link
  8656. (let* ((bn (buffer-name buffer))
  8657. (ibn (concat bn "(Clone)"))
  8658. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  8659. (with-current-buffer ib (org-overview))
  8660. ib))))
  8661. (defun org-do-occur (regexp &optional cleanup)
  8662. "Call the Emacs command `occur'.
  8663. If CLEANUP is non-nil, remove the printout of the regular expression
  8664. in the *Occur* buffer. This is useful if the regex is long and not useful
  8665. to read."
  8666. (occur regexp)
  8667. (when cleanup
  8668. (let ((cwin (selected-window)) win beg end)
  8669. (when (setq win (get-buffer-window "*Occur*"))
  8670. (select-window win))
  8671. (goto-char (point-min))
  8672. (when (re-search-forward "match[a-z]+" nil t)
  8673. (setq beg (match-end 0))
  8674. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  8675. (setq end (1- (match-beginning 0)))))
  8676. (and beg end (let ((buffer-read-only)) (delete-region beg end)))
  8677. (goto-char (point-min))
  8678. (select-window cwin))))
  8679. ;;; The mark ring for links jumps
  8680. (defvar org-mark-ring nil
  8681. "Mark ring for positions before jumps in Org-mode.")
  8682. (defvar org-mark-ring-last-goto nil
  8683. "Last position in the mark ring used to go back.")
  8684. ;; Fill and close the ring
  8685. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  8686. (loop for i from 1 to org-mark-ring-length do
  8687. (push (make-marker) org-mark-ring))
  8688. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  8689. org-mark-ring)
  8690. (defun org-mark-ring-push (&optional pos buffer)
  8691. "Put the current position or POS into the mark ring and rotate it."
  8692. (interactive)
  8693. (setq pos (or pos (point)))
  8694. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  8695. (move-marker (car org-mark-ring)
  8696. (or pos (point))
  8697. (or buffer (current-buffer)))
  8698. (message
  8699. (substitute-command-keys
  8700. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  8701. (defun org-mark-ring-goto (&optional n)
  8702. "Jump to the previous position in the mark ring.
  8703. With prefix arg N, jump back that many stored positions. When
  8704. called several times in succession, walk through the entire ring.
  8705. Org-mode commands jumping to a different position in the current file,
  8706. or to another Org-mode file, automatically push the old position
  8707. onto the ring."
  8708. (interactive "p")
  8709. (let (p m)
  8710. (if (eq last-command this-command)
  8711. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  8712. (setq p org-mark-ring))
  8713. (setq org-mark-ring-last-goto p)
  8714. (setq m (car p))
  8715. (switch-to-buffer (marker-buffer m))
  8716. (goto-char m)
  8717. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  8718. (defun org-camel-to-words (s)
  8719. "Split \"CamelCaseWords\" to (\"Camel\" \"Case\" \"Words\")."
  8720. (let ((case-fold-search nil)
  8721. words)
  8722. (while (string-match "[a-z][A-Z]" s)
  8723. (push (substring s 0 (1+ (match-beginning 0))) words)
  8724. (setq s (substring s (1+ (match-beginning 0)))))
  8725. (nreverse (cons s words))))
  8726. (defun org-remove-angle-brackets (s)
  8727. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  8728. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  8729. s)
  8730. (defun org-add-angle-brackets (s)
  8731. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  8732. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  8733. s)
  8734. ;;; Following specific links
  8735. (defun org-follow-timestamp-link ()
  8736. (cond
  8737. ((org-at-date-range-p t)
  8738. (let ((org-agenda-start-on-weekday)
  8739. (t1 (match-string 1))
  8740. (t2 (match-string 2)))
  8741. (setq t1 (time-to-days (org-time-string-to-time t1))
  8742. t2 (time-to-days (org-time-string-to-time t2)))
  8743. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  8744. ((org-at-timestamp-p t)
  8745. (org-agenda-list nil (time-to-days (org-time-string-to-time
  8746. (substring (match-string 1) 0 10)))
  8747. 1))
  8748. (t (error "This should not happen"))))
  8749. (defun org-follow-bbdb-link (name)
  8750. "Follow a BBDB link to NAME."
  8751. (require 'bbdb)
  8752. (let ((inhibit-redisplay (not debug-on-error))
  8753. (bbdb-electric-p nil))
  8754. (catch 'exit
  8755. ;; Exact match on name
  8756. (bbdb-name (concat "\\`" name "\\'") nil)
  8757. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  8758. ;; Exact match on name
  8759. (bbdb-company (concat "\\`" name "\\'") nil)
  8760. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  8761. ;; Partial match on name
  8762. (bbdb-name name nil)
  8763. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  8764. ;; Partial match on company
  8765. (bbdb-company name nil)
  8766. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  8767. ;; General match including network address and notes
  8768. (bbdb name nil)
  8769. (when (= 0 (buffer-size (get-buffer "*BBDB*")))
  8770. (delete-window (get-buffer-window "*BBDB*"))
  8771. (error "No matching BBDB record")))))
  8772. (defun org-follow-info-link (name)
  8773. "Follow an info file & node link to NAME."
  8774. (if (or (string-match "\\(.*\\)::?\\(.*\\)" name)
  8775. (string-match "\\(.*\\)" name))
  8776. (progn
  8777. (require 'info)
  8778. (if (match-string 2 name) ; If there isn't a node, choose "Top"
  8779. (Info-find-node (match-string 1 name) (match-string 2 name))
  8780. (Info-find-node (match-string 1 name) "Top")))
  8781. (message (concat "Could not open: " name))))
  8782. (defun org-follow-gnus-link (&optional group article)
  8783. "Follow a Gnus link to GROUP and ARTICLE."
  8784. (require 'gnus)
  8785. (funcall (cdr (assq 'gnus org-link-frame-setup)))
  8786. (if gnus-other-frame-object (select-frame gnus-other-frame-object))
  8787. (cond ((and group article)
  8788. (gnus-group-read-group t nil group (list (string-to-number article)))
  8789. (gnus-summary-select-article))
  8790. (group (gnus-group-jump-to-group group))))
  8791. (defun org-follow-vm-link (&optional folder article readonly)
  8792. "Follow a VM link to FOLDER and ARTICLE."
  8793. (require 'vm)
  8794. (setq article (org-add-angle-brackets article))
  8795. (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
  8796. ;; ange-ftp or efs or tramp access
  8797. (let ((user (or (match-string 1 folder) (user-login-name)))
  8798. (host (match-string 2 folder))
  8799. (file (match-string 3 folder)))
  8800. (cond
  8801. ((featurep 'tramp)
  8802. ;; use tramp to access the file
  8803. (if (featurep 'xemacs)
  8804. (setq folder (format "[%s@%s]%s" user host file))
  8805. (setq folder (format "/%s@%s:%s" user host file))))
  8806. (t
  8807. ;; use ange-ftp or efs
  8808. (require (if (featurep 'xemacs) 'efs 'ange-ftp))
  8809. (setq folder (format "/%s@%s:%s" user host file))))))
  8810. (when folder
  8811. (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
  8812. (sit-for 0.1)
  8813. (when article
  8814. (vm-select-folder-buffer)
  8815. (widen)
  8816. (let ((case-fold-search t))
  8817. (goto-char (point-min))
  8818. (if (not (re-search-forward
  8819. (concat "^" "message-id: *" (regexp-quote article))))
  8820. (error "Could not find the specified message in this folder"))
  8821. (vm-isearch-update)
  8822. (vm-isearch-narrow)
  8823. (vm-beginning-of-message)
  8824. (vm-summarize)))))
  8825. (defun org-follow-wl-link (folder article)
  8826. "Follow a Wanderlust link to FOLDER and ARTICLE."
  8827. (setq article (org-add-angle-brackets article))
  8828. (wl-summary-goto-folder-subr folder 'no-sync t nil t nil nil)
  8829. (if article (wl-summary-jump-to-msg-by-message-id article))
  8830. (wl-summary-redisplay))
  8831. (defun org-follow-rmail-link (folder article)
  8832. "Follow an RMAIL link to FOLDER and ARTICLE."
  8833. (setq article (org-add-angle-brackets article))
  8834. (let (message-number)
  8835. (save-excursion
  8836. (save-window-excursion
  8837. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  8838. (setq message-number
  8839. (save-restriction
  8840. (widen)
  8841. (goto-char (point-max))
  8842. (if (re-search-backward
  8843. (concat "^Message-ID:\\s-+" (regexp-quote
  8844. (or article "")))
  8845. nil t)
  8846. (rmail-what-message))))))
  8847. (if message-number
  8848. (progn
  8849. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  8850. (rmail-show-message message-number)
  8851. message-number)
  8852. (error "Message not found"))))
  8853. ;;; mh-e integration based on planner-mode
  8854. (defun org-mhe-get-message-real-folder ()
  8855. "Return the name of the current message real folder, so if you use
  8856. sequences, it will now work."
  8857. (save-excursion
  8858. (let* ((folder
  8859. (if (equal major-mode 'mh-folder-mode)
  8860. mh-current-folder
  8861. ;; Refer to the show buffer
  8862. mh-show-folder-buffer))
  8863. (end-index
  8864. (if (boundp 'mh-index-folder)
  8865. (min (length mh-index-folder) (length folder))))
  8866. )
  8867. ;; a simple test on mh-index-data does not work, because
  8868. ;; mh-index-data is always nil in a show buffer.
  8869. (if (and (boundp 'mh-index-folder)
  8870. (string= mh-index-folder (substring folder 0 end-index)))
  8871. (if (equal major-mode 'mh-show-mode)
  8872. (save-window-excursion
  8873. (when (buffer-live-p (get-buffer folder))
  8874. (progn
  8875. (pop-to-buffer folder)
  8876. (org-mhe-get-message-folder-from-index)
  8877. )
  8878. ))
  8879. (org-mhe-get-message-folder-from-index)
  8880. )
  8881. folder
  8882. )
  8883. )))
  8884. (defun org-mhe-get-message-folder-from-index ()
  8885. "Returns the name of the message folder in a index folder buffer."
  8886. (save-excursion
  8887. (mh-index-previous-folder)
  8888. (re-search-forward "^\\(+.*\\)$" nil t)
  8889. (message (match-string 1))))
  8890. (defun org-mhe-get-message-folder ()
  8891. "Return the name of the current message folder. Be careful if you
  8892. use sequences."
  8893. (save-excursion
  8894. (if (equal major-mode 'mh-folder-mode)
  8895. mh-current-folder
  8896. ;; Refer to the show buffer
  8897. mh-show-folder-buffer)))
  8898. (defun org-mhe-get-message-num ()
  8899. "Return the number of the current message. Be careful if you
  8900. use sequences."
  8901. (save-excursion
  8902. (if (equal major-mode 'mh-folder-mode)
  8903. (mh-get-msg-num nil)
  8904. ;; Refer to the show buffer
  8905. (mh-show-buffer-message-number))))
  8906. (defun org-mhe-get-header (header)
  8907. "Return a header of the message in folder mode. This will create a
  8908. show buffer for the corresponding message. If you have a more clever
  8909. idea..."
  8910. (let* ((folder (org-mhe-get-message-folder))
  8911. (num (org-mhe-get-message-num))
  8912. (buffer (get-buffer-create (concat "show-" folder)))
  8913. (header-field))
  8914. (with-current-buffer buffer
  8915. (mh-display-msg num folder)
  8916. (if (equal major-mode 'mh-folder-mode)
  8917. (mh-header-display)
  8918. (mh-show-header-display))
  8919. (set-buffer buffer)
  8920. (setq header-field (mh-get-header-field header))
  8921. (if (equal major-mode 'mh-folder-mode)
  8922. (mh-show)
  8923. (mh-show-show))
  8924. header-field)))
  8925. (defun org-follow-mhe-link (folder article)
  8926. "Follow an MHE link to FOLDER and ARTICLE.
  8927. If ARTICLE is nil FOLDER is shown. If the configuration variable
  8928. `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
  8929. ARTICLE is searched in all folders. Indexed searches (swish++,
  8930. namazu, and others supported by MH-E) will always search in all
  8931. folders."
  8932. (require 'mh-e)
  8933. (require 'mh-search)
  8934. (require 'mh-utils)
  8935. (mh-find-path)
  8936. (if (not article)
  8937. (mh-visit-folder (mh-normalize-folder-name folder))
  8938. (setq article (org-add-angle-brackets article))
  8939. (mh-search-choose)
  8940. (if (equal mh-searcher 'pick)
  8941. (progn
  8942. (mh-search folder (list "--message-id" article))
  8943. (when (and org-mhe-search-all-folders
  8944. (not (org-mhe-get-message-real-folder)))
  8945. (kill-this-buffer)
  8946. (mh-search "+" (list "--message-id" article))))
  8947. (mh-search "+" article))
  8948. (if (org-mhe-get-message-real-folder)
  8949. (mh-show-msg 1)
  8950. (kill-this-buffer)
  8951. (error "Message not found"))))
  8952. ;;; BibTeX links
  8953. ;; Use the custom search meachnism to construct and use search strings for
  8954. ;; file links to BibTeX database entries.
  8955. (defun org-create-file-search-in-bibtex ()
  8956. "Create the search string and description for a BibTeX database entry."
  8957. (when (eq major-mode 'bibtex-mode)
  8958. ;; yes, we want to construct this search string.
  8959. ;; Make a good description for this entry, using names, year and the title
  8960. ;; Put it into the `description' variable which is dynamically scoped.
  8961. (let ((bibtex-autokey-names 1)
  8962. (bibtex-autokey-names-stretch 1)
  8963. (bibtex-autokey-name-case-convert-function 'identity)
  8964. (bibtex-autokey-name-separator " & ")
  8965. (bibtex-autokey-additional-names " et al.")
  8966. (bibtex-autokey-year-length 4)
  8967. (bibtex-autokey-name-year-separator " ")
  8968. (bibtex-autokey-titlewords 3)
  8969. (bibtex-autokey-titleword-separator " ")
  8970. (bibtex-autokey-titleword-case-convert-function 'identity)
  8971. (bibtex-autokey-titleword-length 'infty)
  8972. (bibtex-autokey-year-title-separator ": "))
  8973. (setq description (bibtex-generate-autokey)))
  8974. ;; Now parse the entry, get the key and return it.
  8975. (save-excursion
  8976. (bibtex-beginning-of-entry)
  8977. (cdr (assoc "=key=" (bibtex-parse-entry))))))
  8978. (defun org-execute-file-search-in-bibtex (s)
  8979. "Find the link search string S as a key for a database entry."
  8980. (when (eq major-mode 'bibtex-mode)
  8981. ;; Yes, we want to do the search in this file.
  8982. ;; We construct a regexp that searches for "@entrytype{" followed by the key
  8983. (goto-char (point-min))
  8984. (and (re-search-forward (concat "@[a-zA-Z]+[ \t\n]*{[ \t\n]*"
  8985. (regexp-quote s) "[ \t\n]*,") nil t)
  8986. (goto-char (match-beginning 0)))
  8987. (if (and (match-beginning 0) (equal current-prefix-arg '(16)))
  8988. ;; Use double prefix to indicate that any web link should be browsed
  8989. (let ((b (current-buffer)) (p (point)))
  8990. ;; Restore the window configuration because we just use the web link
  8991. (set-window-configuration org-window-config-before-follow-link)
  8992. (save-excursion (set-buffer b) (goto-char p)
  8993. (bibtex-url)))
  8994. (recenter 0)) ; Move entry start to beginning of window
  8995. ;; return t to indicate that the search is done.
  8996. t))
  8997. ;; Finally add the functions to the right hooks.
  8998. (add-hook 'org-create-file-search-functions 'org-create-file-search-in-bibtex)
  8999. (add-hook 'org-execute-file-search-functions 'org-execute-file-search-in-bibtex)
  9000. ;; end of Bibtex link setup
  9001. ;;; Following file links
  9002. (defun org-open-file (path &optional in-emacs line search)
  9003. "Open the file at PATH.
  9004. First, this expands any special file name abbreviations. Then the
  9005. configuration variable `org-file-apps' is checked if it contains an
  9006. entry for this file type, and if yes, the corresponding command is launched.
  9007. If no application is found, Emacs simply visits the file.
  9008. With optional argument IN-EMACS, Emacs will visit the file.
  9009. Optional LINE specifies a line to go to, optional SEARCH a string to
  9010. search for. If LINE or SEARCH is given, the file will always be
  9011. opened in Emacs.
  9012. If the file does not exist, an error is thrown."
  9013. (setq in-emacs (or in-emacs line search))
  9014. (let* ((file (if (equal path "")
  9015. buffer-file-name
  9016. (substitute-in-file-name (expand-file-name path))))
  9017. (apps (append org-file-apps (org-default-apps)))
  9018. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9019. (dirp (if remp nil (file-directory-p file)))
  9020. (dfile (downcase file))
  9021. (old-buffer (current-buffer))
  9022. (old-pos (point))
  9023. (old-mode major-mode)
  9024. ext cmd)
  9025. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9026. (setq ext (match-string 1 dfile))
  9027. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9028. (setq ext (match-string 1 dfile))))
  9029. (if in-emacs
  9030. (setq cmd 'emacs)
  9031. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9032. (and dirp (cdr (assoc 'directory apps)))
  9033. (cdr (assoc ext apps))
  9034. (cdr (assoc t apps)))))
  9035. (when (eq cmd 'mailcap)
  9036. (require 'mailcap)
  9037. (mailcap-parse-mailcaps)
  9038. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9039. (command (mailcap-mime-info mime-type)))
  9040. (if (stringp command)
  9041. (setq cmd command)
  9042. (setq cmd 'emacs))))
  9043. (if (and (not (eq cmd 'emacs)) ; Emacs has not problems with non-ex files
  9044. (not (file-exists-p file))
  9045. (not org-open-non-existing-files))
  9046. (error "No such file: %s" file))
  9047. (cond
  9048. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9049. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9050. (if (string-match "['\"]%s['\"]" cmd)
  9051. (setq cmd (replace-match "%s" t t cmd)))
  9052. (setq cmd (format cmd (shell-quote-argument file)))
  9053. (save-window-excursion
  9054. (shell-command (concat cmd " &"))))
  9055. ((or (stringp cmd)
  9056. (eq cmd 'emacs))
  9057. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9058. (if line (goto-line line)
  9059. (if search (org-link-search search))))
  9060. ((consp cmd)
  9061. (eval cmd))
  9062. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9063. (and (org-mode-p) (eq old-mode 'org-mode)
  9064. (or (not (equal old-buffer (current-buffer)))
  9065. (not (equal old-pos (point))))
  9066. (org-mark-ring-push old-pos old-buffer))))
  9067. (defun org-default-apps ()
  9068. "Return the default applications for this operating system."
  9069. (cond
  9070. ((eq system-type 'darwin)
  9071. org-file-apps-defaults-macosx)
  9072. ((eq system-type 'windows-nt)
  9073. org-file-apps-defaults-windowsnt)
  9074. (t org-file-apps-defaults-gnu)))
  9075. (defun org-expand-file-name (path)
  9076. "Replace special path abbreviations and expand the file name."
  9077. (expand-file-name path))
  9078. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9079. (defun org-file-remote-p (file)
  9080. "Test whether FILE specifies a location on a remote system.
  9081. Return non-nil if the location is indeed remote.
  9082. For example, the filename \"/user@host:/foo\" specifies a location
  9083. on the system \"/user@host:\"."
  9084. (cond ((fboundp 'file-remote-p)
  9085. (file-remote-p file))
  9086. ((fboundp 'tramp-handle-file-remote-p)
  9087. (tramp-handle-file-remote-p file))
  9088. ((and (boundp 'ange-ftp-name-format)
  9089. (string-match (car ange-ftp-name-format) file))
  9090. t)
  9091. (t nil)))
  9092. ;;;; Hooks for remember.el
  9093. ;;;###autoload
  9094. (defun org-remember-annotation ()
  9095. "Return a link to the current location as an annotation for remember.el.
  9096. If you are using Org-mode files as target for data storage with
  9097. remember.el, then the annotations should include a link compatible with the
  9098. conventions in Org-mode. This function returns such a link."
  9099. (org-store-link nil))
  9100. (defconst org-remember-help
  9101. "Select a destination location for the note.
  9102. UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
  9103. RET at beg-of-buf -> Append to file as level 2 headline
  9104. RET on headline -> Store as sublevel entry to current headline
  9105. <left>/<right> -> before/after current headline, same headings level")
  9106. ;;;###autoload
  9107. (defun org-remember-apply-template (&optional use-char skip-interactive)
  9108. "Initialize *remember* buffer with template, invoke `org-mode'.
  9109. This function should be placed into `remember-mode-hook' and in fact requires
  9110. to be run from that hook to fucntion properly."
  9111. (if org-remember-templates
  9112. (let* ((char (or use-char
  9113. (if (= (length org-remember-templates) 1)
  9114. (caar org-remember-templates)
  9115. (message "Select template: %s"
  9116. (mapconcat
  9117. (lambda (x) (char-to-string (car x)))
  9118. org-remember-templates " "))
  9119. (read-char-exclusive))))
  9120. (entry (cdr (assoc char org-remember-templates)))
  9121. (tpl (car entry))
  9122. (plist-p (if org-store-link-plist t nil))
  9123. (file (if (and (nth 1 entry) (stringp (nth 1 entry))
  9124. (string-match "\\S-" (nth 1 entry)))
  9125. (nth 1 entry)
  9126. org-default-notes-file))
  9127. (headline (nth 2 entry))
  9128. (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
  9129. (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
  9130. (v-u (concat "[" (substring v-t 1 -1) "]"))
  9131. (v-U (concat "[" (substring v-T 1 -1) "]"))
  9132. (v-i initial) ; defined in `remember-mode'
  9133. (v-a (if (equal annotation "[[]]") "" annotation)) ; likewise
  9134. (v-n user-full-name)
  9135. (org-startup-folded nil)
  9136. (org-startup-with-deadline-check nil)
  9137. org-time-was-given x prompt char time)
  9138. (setq org-store-link-plist
  9139. (append (list :annotation v-a :initial v-i)))
  9140. (unless tpl (setq tpl "") (message "No template") (ding))
  9141. (erase-buffer)
  9142. (insert (substitute-command-keys
  9143. (format
  9144. "## `C-c C-c' to file interactively, `C-u C-c C-c' to file directly.
  9145. ## Target file \"%s\", headline \"%s\"
  9146. ## To switch templates, use `\\[org-remember]'.\n\n"
  9147. (abbreviate-file-name (or file org-default-notes-file))
  9148. (or headline ""))))
  9149. (insert tpl) (goto-char (point-min))
  9150. ;; Simple %-escapes
  9151. (while (re-search-forward "%\\([tTuTai]\\)" nil t)
  9152. (when (and initial (equal (match-string 0) "%i"))
  9153. (save-match-data
  9154. (let* ((lead (buffer-substring
  9155. (point-at-bol) (match-beginning 0))))
  9156. (setq v-i (mapconcat 'identity
  9157. (org-split-string initial "\n")
  9158. (concat "\n" lead))))))
  9159. (replace-match
  9160. (or (eval (intern (concat "v-" (match-string 1)))) "")
  9161. t t))
  9162. ;; From the property list
  9163. (when plist-p
  9164. (goto-char (point-min))
  9165. (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
  9166. (and (setq x (plist-get org-store-link-plist
  9167. (intern (match-string 1))))
  9168. (replace-match x t t))))
  9169. ;; Turn on org-mode in the remember buffer, set local variables
  9170. (org-mode)
  9171. (org-set-local 'org-finish-function 'remember-buffer)
  9172. (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
  9173. (org-set-local 'org-default-notes-file file))
  9174. (if (and headline (stringp headline) (string-match "\\S-" headline))
  9175. (org-set-local 'org-remember-default-headline headline))
  9176. ;; Interactive template entries
  9177. (goto-char (point-min))
  9178. (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([uUtT]\\)?" nil t)
  9179. (setq char (if (match-end 3) (match-string 3))
  9180. prompt (if (match-end 2) (match-string 2)))
  9181. (goto-char (match-beginning 0))
  9182. (replace-match "")
  9183. (if char
  9184. (progn
  9185. (setq org-time-was-given (equal (upcase char) char))
  9186. (setq time (org-read-date (equal (upcase char) "U") t nil
  9187. prompt))
  9188. (org-insert-time-stamp time org-time-was-given
  9189. (member char '("u" "U"))))
  9190. (insert (read-string
  9191. (if prompt (concat prompt ": ") "Enter string")))))
  9192. (goto-char (point-min))
  9193. (if (re-search-forward "%\\?" nil t)
  9194. (replace-match "")
  9195. (and (re-search-forward "^[^#\n]" nil t) (backward-char 1))))
  9196. (org-mode)
  9197. (org-set-local 'org-finish-function 'remember-buffer)))
  9198. ;;;###autoload
  9199. (defun org-remember ()
  9200. "Call `remember'. If this is already a remember buffer, re-apply template.
  9201. If there is an active region, amke sure remember uses it as initial content
  9202. of the remember buffer."
  9203. (interactive)
  9204. (if (eq org-finish-function 'remember-buffer)
  9205. (progn
  9206. (when (< (length org-remember-templates) 2)
  9207. (error "No other template available"))
  9208. (erase-buffer)
  9209. (let ((annotation (plist-get org-store-link-plist :annotation))
  9210. (initial (plist-get org-store-link-plist :initial)))
  9211. (org-remember-apply-template))
  9212. (message "Press C-c C-c to remember data"))
  9213. (if (org-region-active-p)
  9214. (remember (buffer-substring (point) (mark)))
  9215. (call-interactively 'remember))))
  9216. ;;;###autoload
  9217. (defun org-remember-handler ()
  9218. "Store stuff from remember.el into an org file.
  9219. First prompts for an org file. If the user just presses return, the value
  9220. of `org-default-notes-file' is used.
  9221. Then the command offers the headings tree of the selected file in order to
  9222. file the text at a specific location.
  9223. You can either immediately press RET to get the note appended to the
  9224. file, or you can use vertical cursor motion and visibility cycling (TAB) to
  9225. find a better place. Then press RET or <left> or <right> in insert the note.
  9226. Key Cursor position Note gets inserted
  9227. -----------------------------------------------------------------------------
  9228. RET buffer-start as level 2 heading at end of file
  9229. RET on headline as sublevel of the heading at cursor
  9230. RET no heading at cursor position, level taken from context.
  9231. Or use prefix arg to specify level manually.
  9232. <left> on headline as same level, before current heading
  9233. <right> on headline as same level, after current heading
  9234. So the fastest way to store the note is to press RET RET to append it to
  9235. the default file. This way your current train of thought is not
  9236. interrupted, in accordance with the principles of remember.el. But with
  9237. little extra effort, you can push it directly to the correct location.
  9238. Before being stored away, the function ensures that the text has a
  9239. headline, i.e. a first line that starts with a \"*\". If not, a headline
  9240. is constructed from the current date and some additional data.
  9241. If the variable `org-adapt-indentation' is non-nil, the entire text is
  9242. also indented so that it starts in the same column as the headline
  9243. \(i.e. after the stars).
  9244. See also the variable `org-reverse-note-order'."
  9245. (goto-char (point-min))
  9246. (while (looking-at "^[ \t]*\n\\|^##.*\n")
  9247. (replace-match ""))
  9248. (catch 'quit
  9249. (let* ((txt (buffer-substring (point-min) (point-max)))
  9250. (fastp current-prefix-arg)
  9251. (file (if fastp org-default-notes-file (org-get-org-file)))
  9252. (heading org-remember-default-headline)
  9253. (visiting (find-buffer-visiting file))
  9254. (org-startup-with-deadline-check nil)
  9255. (org-startup-folded nil)
  9256. (org-startup-align-all-tables nil)
  9257. (org-goto-start-pos 1)
  9258. spos level indent reversed)
  9259. ;; Modify text so that it becomes a nice subtree which can be inserted
  9260. ;; into an org tree.
  9261. (let* ((lines (split-string txt "\n"))
  9262. first)
  9263. (setq first (car lines) lines (cdr lines))
  9264. (if (string-match "^\\*+" first)
  9265. ;; Is already a headline
  9266. (setq indent nil)
  9267. ;; We need to add a headline: Use time and first buffer line
  9268. (setq lines (cons first lines)
  9269. first (concat "* " (current-time-string)
  9270. " (" (remember-buffer-desc) ")")
  9271. indent " "))
  9272. (if (and org-adapt-indentation indent)
  9273. (setq lines (mapcar (lambda (x) (concat indent x)) lines)))
  9274. (setq txt (concat first "\n"
  9275. (mapconcat 'identity lines "\n"))))
  9276. ;; Find the file
  9277. (if (not visiting)
  9278. (find-file-noselect file))
  9279. (with-current-buffer (get-file-buffer file)
  9280. (save-excursion (and (goto-char (point-min))
  9281. (not (re-search-forward "^\\* " nil t))
  9282. (insert "\n* Notes\n")))
  9283. (setq reversed (org-notes-order-reversed-p))
  9284. (save-excursion
  9285. (save-restriction
  9286. (widen)
  9287. ;; Find the default location
  9288. (when (and heading (stringp heading) (string-match "\\S-" heading))
  9289. (goto-char (point-min))
  9290. (if (re-search-forward
  9291. (concat "^\\*+[ \t]+" (regexp-quote heading)
  9292. "\\([ \t]+:[@a-zA-Z0-9_:]*\\)?[ \t]*$")
  9293. nil t)
  9294. (setq org-goto-start-pos (match-beginning 0))))
  9295. ;; Ask the User for a location
  9296. (setq spos (if fastp
  9297. org-goto-start-pos
  9298. (org-get-location (current-buffer) org-remember-help)))
  9299. (if (not spos) (throw 'quit nil)) ; return nil to show we did
  9300. ; not handle this note
  9301. (goto-char spos)
  9302. (cond ((and (bobp) (not reversed))
  9303. ;; Put it at the end, as level 2
  9304. (save-restriction
  9305. (widen)
  9306. (goto-char (point-max))
  9307. (if (not (bolp)) (newline))
  9308. (org-paste-subtree 2 txt)))
  9309. ((and (bobp) reversed)
  9310. ;; Put it at the start, as level 1
  9311. (save-restriction
  9312. (widen)
  9313. (goto-char (point-min))
  9314. (re-search-forward "^\\*" nil t)
  9315. (beginning-of-line 1)
  9316. (org-paste-subtree 1 txt)))
  9317. ((and (org-on-heading-p nil) (not current-prefix-arg))
  9318. ;; Put it below this entry, at the beg/end of the subtree
  9319. (org-back-to-heading t)
  9320. (setq level (funcall outline-level))
  9321. (if reversed
  9322. (outline-end-of-heading)
  9323. (org-end-of-subtree t))
  9324. (if (not (bolp)) (newline))
  9325. (beginning-of-line 1)
  9326. (org-paste-subtree (org-get-legal-level level 1) txt))
  9327. (t
  9328. ;; Put it right there, with automatic level determined by
  9329. ;; org-paste-subtree or from prefix arg
  9330. (org-paste-subtree current-prefix-arg txt)))
  9331. (when remember-save-after-remembering
  9332. (save-buffer)
  9333. (if (not visiting) (kill-buffer (current-buffer)))))))))
  9334. t) ;; return t to indicate that we took care of this note.
  9335. (defun org-get-org-file ()
  9336. "Read a filename, with default directory `org-directory'."
  9337. (let ((default (or org-default-notes-file remember-data-file)))
  9338. (read-file-name (format "File name [%s]: " default)
  9339. (file-name-as-directory org-directory)
  9340. default)))
  9341. (defun org-notes-order-reversed-p ()
  9342. "Check if the current file should receive notes in reversed order."
  9343. (cond
  9344. ((not org-reverse-note-order) nil)
  9345. ((eq t org-reverse-note-order) t)
  9346. ((not (listp org-reverse-note-order)) nil)
  9347. (t (catch 'exit
  9348. (let ((all org-reverse-note-order)
  9349. entry)
  9350. (while (setq entry (pop all))
  9351. (if (string-match (car entry) buffer-file-name)
  9352. (throw 'exit (cdr entry))))
  9353. nil)))))
  9354. ;;;; Dynamic blocks
  9355. (defun org-find-dblock (name)
  9356. "Find the first dynamic block with name NAME in the buffer.
  9357. If not found, stay at current position and return nil."
  9358. (let (pos)
  9359. (save-excursion
  9360. (goto-char (point-min))
  9361. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  9362. nil t)
  9363. (match-beginning 0))))
  9364. (if pos (goto-char pos))
  9365. pos))
  9366. (defconst org-dblock-start-re
  9367. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  9368. "Matches the startline of a dynamic block, with parameters.")
  9369. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  9370. "Matches the end of a dyhamic block.")
  9371. (defun org-create-dblock (plist)
  9372. "Create a dynamic block section, with parameters taken from PLIST.
  9373. PLIST must containe a :name entry which is used as name of the block."
  9374. (unless (bolp) (newline))
  9375. (let ((name (plist-get plist :name)))
  9376. (insert "#+BEGIN: " name)
  9377. (while plist
  9378. (if (eq (car plist) :name)
  9379. (setq plist (cddr plist))
  9380. (insert " " (prin1-to-string (pop plist)))))
  9381. (insert "\n\n#+END:\n")
  9382. (beginning-of-line -2)))
  9383. (defun org-prepare-dblock ()
  9384. "Prepare dynamic block for refresh.
  9385. This empties the block, puts the cursor at the insert position and returns
  9386. the property list including an extra property :name with the block name."
  9387. (unless (looking-at org-dblock-start-re)
  9388. (error "Not at a dynamic block"))
  9389. (let* ((begdel (1+ (match-end 0)))
  9390. (name (match-string 1))
  9391. (params (append (list :name name)
  9392. (read (concat "(" (match-string 3) ")")))))
  9393. (unless (re-search-forward org-dblock-end-re nil t)
  9394. (error "Dynamic block not terminated"))
  9395. (delete-region begdel (match-beginning 0))
  9396. (goto-char begdel)
  9397. (open-line 1)
  9398. params))
  9399. (defun org-map-dblocks (&optional command)
  9400. "Apply COMMAND to all dynamic blocks in the current buffer.
  9401. If COMMAND is not given, use `org-update-dblock'."
  9402. (let ((cmd (or command 'org-update-dblock))
  9403. pos)
  9404. (save-excursion
  9405. (goto-char (point-min))
  9406. (while (re-search-forward org-dblock-start-re nil t)
  9407. (goto-char (setq pos (match-beginning 0)))
  9408. (condition-case nil
  9409. (funcall cmd)
  9410. (error (message "Error during update of dynamic block")))
  9411. (goto-char pos)
  9412. (unless (re-search-forward org-dblock-end-re nil t)
  9413. (error "Dynamic block not terminated"))))))
  9414. (defun org-dblock-update (&optional arg)
  9415. "User command for updating dynamic blocks.
  9416. Update the dynamic block at point. With prefix ARG, update all dynamic
  9417. blocks in the buffer."
  9418. (interactive "P")
  9419. (if arg
  9420. (org-update-all-dblocks)
  9421. (or (looking-at org-dblock-start-re)
  9422. (org-beginning-of-dblock))
  9423. (org-update-dblock)))
  9424. (defun org-update-dblock ()
  9425. "Update the dynamic block at point
  9426. This means to empty the block, parse for parameters and then call
  9427. the correct writing function."
  9428. (let* ((pos (point))
  9429. (params (org-prepare-dblock))
  9430. (name (plist-get params :name))
  9431. (cmd (intern (concat "org-dblock-write:" name))))
  9432. (funcall cmd params)
  9433. (goto-char pos)))
  9434. (defun org-beginning-of-dblock ()
  9435. "Find the beginning of the dynamic block at point.
  9436. Error if there is no scuh block at point."
  9437. (let ((pos (point))
  9438. beg)
  9439. (end-of-line 1)
  9440. (if (and (re-search-backward org-dblock-start-re nil t)
  9441. (setq beg (match-beginning 0))
  9442. (re-search-forward org-dblock-end-re nil t)
  9443. (> (match-end 0) pos))
  9444. (goto-char beg)
  9445. (goto-char pos)
  9446. (error "Not in a dynamic block"))))
  9447. (defun org-update-all-dblocks ()
  9448. "Update all dynamic blocks in the buffer.
  9449. This function can be used in a hook."
  9450. (when (org-mode-p)
  9451. (org-map-dblocks 'org-update-dblock)))
  9452. ;;;; Completion
  9453. (defun org-complete (&optional arg)
  9454. "Perform completion on word at point.
  9455. At the beginning of a headline, this completes TODO keywords as given in
  9456. `org-todo-keywords'.
  9457. If the current word is preceded by a backslash, completes the TeX symbols
  9458. that are supported for HTML support.
  9459. If the current word is preceded by \"#+\", completes special words for
  9460. setting file options.
  9461. In the line after \"#+STARTUP:, complete valid keywords.\"
  9462. At all other locations, this simply calls `ispell-complete-word'."
  9463. (interactive "P")
  9464. (catch 'exit
  9465. (let* ((end (point))
  9466. (beg1 (save-excursion
  9467. (skip-chars-backward "a-zA-Z_@0-9")
  9468. (point)))
  9469. (beg (save-excursion
  9470. (skip-chars-backward "a-zA-Z0-9_:$")
  9471. (point)))
  9472. (confirm (lambda (x) (stringp (car x))))
  9473. (camel (equal (char-before beg) ?*))
  9474. (tag (equal (char-before beg1) ?:))
  9475. (texp (equal (char-before beg) ?\\))
  9476. (link (equal (char-before beg) ?\[))
  9477. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  9478. beg)
  9479. "#+"))
  9480. (startup (string-match "^#\\+STARTUP:.*"
  9481. (buffer-substring (point-at-bol) (point))))
  9482. (completion-ignore-case opt)
  9483. (type nil)
  9484. (tbl nil)
  9485. (table (cond
  9486. (opt
  9487. (setq type :opt)
  9488. (mapcar (lambda (x)
  9489. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  9490. (cons (match-string 2 x) (match-string 1 x)))
  9491. (org-split-string (org-get-current-options) "\n")))
  9492. (startup
  9493. (setq type :startup)
  9494. org-startup-options)
  9495. (link (append org-link-abbrev-alist-local
  9496. org-link-abbrev-alist))
  9497. (texp
  9498. (setq type :tex)
  9499. org-html-entities)
  9500. ((string-match "\\`\\*+[ \t]*\\'"
  9501. (buffer-substring (point-at-bol) beg))
  9502. (setq type :todo)
  9503. (mapcar 'list org-todo-keywords))
  9504. (camel
  9505. (setq type :camel)
  9506. (save-excursion
  9507. (goto-char (point-min))
  9508. (while (re-search-forward org-todo-line-regexp nil t)
  9509. (push (list
  9510. (if org-file-link-context-use-camel-case
  9511. (org-make-org-heading-camel (match-string 3) t)
  9512. (org-make-org-heading-search-string
  9513. (match-string 3) t)))
  9514. tbl)))
  9515. tbl)
  9516. (tag (setq type :tag beg beg1)
  9517. (or org-tag-alist (org-get-buffer-tags)))
  9518. (t (progn (ispell-complete-word arg) (throw 'exit nil)))))
  9519. (pattern (buffer-substring-no-properties beg end))
  9520. (completion (try-completion pattern table confirm)))
  9521. (cond ((eq completion t)
  9522. (if (equal type :opt)
  9523. (insert (substring (cdr (assoc (upcase pattern) table))
  9524. (length pattern)))
  9525. (if (equal type :tag) (insert ":"))))
  9526. ((null completion)
  9527. (message "Can't find completion for \"%s\"" pattern)
  9528. (ding))
  9529. ((not (string= pattern completion))
  9530. (delete-region beg end)
  9531. (if (string-match " +$" completion)
  9532. (setq completion (replace-match "" t t completion)))
  9533. (insert completion)
  9534. (if (get-buffer-window "*Completions*")
  9535. (delete-window (get-buffer-window "*Completions*")))
  9536. (if (assoc completion table)
  9537. (if (eq type :todo) (insert " ")
  9538. (if (eq type :tag) (insert ":"))))
  9539. (if (and (equal type :opt) (assoc completion table))
  9540. (message "%s" (substitute-command-keys
  9541. "Press \\[org-complete] again to insert example settings"))))
  9542. (t
  9543. (message "Making completion list...")
  9544. (let ((list (sort (all-completions pattern table confirm)
  9545. 'string<)))
  9546. (with-output-to-temp-buffer "*Completions*"
  9547. (condition-case nil
  9548. ;; Protection needed for XEmacs and emacs 21
  9549. (display-completion-list list pattern)
  9550. (error (display-completion-list list)))))
  9551. (message "Making completion list...%s" "done"))))))
  9552. ;;;; TODO, DEADLINE, Comments
  9553. (defun org-toggle-comment ()
  9554. "Change the COMMENT state of an entry."
  9555. (interactive)
  9556. (save-excursion
  9557. (org-back-to-heading)
  9558. (if (looking-at (concat outline-regexp
  9559. "\\( +\\<" org-comment-string "\\>\\)"))
  9560. (replace-match "" t t nil 1)
  9561. (if (looking-at outline-regexp)
  9562. (progn
  9563. (goto-char (match-end 0))
  9564. (insert " " org-comment-string))))))
  9565. (defvar org-last-todo-state-is-todo nil
  9566. "This is non-nil when the last TODO state change led to a TODO state.
  9567. If the last change removed the TODO tag or switched to DONE, then
  9568. this is nil.")
  9569. (defun org-todo (&optional arg)
  9570. "Change the TODO state of an item.
  9571. The state of an item is given by a keyword at the start of the heading,
  9572. like
  9573. *** TODO Write paper
  9574. *** DONE Call mom
  9575. The different keywords are specified in the variable `org-todo-keywords'.
  9576. By default the available states are \"TODO\" and \"DONE\".
  9577. So for this example: when the item starts with TODO, it is changed to DONE.
  9578. When it starts with DONE, the DONE is removed. And when neither TODO nor
  9579. DONE are present, add TODO at the beginning of the heading.
  9580. With C-u prefix arg, use completion to determine the new state.
  9581. With numeric prefix arg, switch to that state.
  9582. For calling through lisp, arg is also interpreted in the following way:
  9583. 'none -> empty state
  9584. \"\"(empty string) -> switch to empty state
  9585. 'done -> switch to DONE
  9586. \"WAITING\" -> switch to the specified keyword, but only if it
  9587. really is a member of `org-todo-keywords'."
  9588. (interactive "P")
  9589. (save-excursion
  9590. (org-back-to-heading)
  9591. (if (looking-at outline-regexp) (goto-char (match-end 0)))
  9592. (or (looking-at (concat " +" org-todo-regexp " *"))
  9593. (looking-at " *"))
  9594. (let* ((this (match-string 1))
  9595. (last-state (or this ""))
  9596. (completion-ignore-case t)
  9597. (member (member this org-todo-keywords))
  9598. (tail (cdr member))
  9599. (state (cond
  9600. ((equal arg '(4))
  9601. ;; Read a state with completion
  9602. (completing-read "State: " (mapcar (lambda(x) (list x))
  9603. org-todo-keywords)
  9604. nil t))
  9605. ((eq arg 'right)
  9606. (if this
  9607. (if tail (car tail) nil)
  9608. (car org-todo-keywords)))
  9609. ((eq arg 'left)
  9610. (if (equal member org-todo-keywords)
  9611. nil
  9612. (if this
  9613. (nth (- (length org-todo-keywords) (length tail) 2)
  9614. org-todo-keywords)
  9615. org-done-string)))
  9616. (arg
  9617. ;; user requests a specific state
  9618. (cond
  9619. ((equal arg "") nil)
  9620. ((eq arg 'none) nil)
  9621. ((eq arg 'done) (org-last org-todo-keywords))
  9622. ((car (member arg org-todo-keywords)))
  9623. ((nth (1- (prefix-numeric-value arg))
  9624. org-todo-keywords))))
  9625. ((null member) (car org-todo-keywords))
  9626. ((null tail) nil) ;; -> first entry
  9627. ((eq org-todo-interpretation 'sequence)
  9628. (car tail))
  9629. ((memq org-todo-interpretation '(type priority))
  9630. (if (eq this-command last-command)
  9631. (car tail)
  9632. (if (> (length tail) 0) org-done-string nil)))
  9633. (t nil)))
  9634. (next (if state (concat " " state " ") " "))
  9635. dostates)
  9636. (replace-match next t t)
  9637. (setq org-last-todo-state-is-todo
  9638. (not (equal state org-done-string)))
  9639. (when org-log-done
  9640. (setq dostates (and (eq org-todo-interpretation 'sequence)
  9641. (listp org-log-done) (memq 'state org-log-done)))
  9642. (cond
  9643. ((and state (not this))
  9644. (org-add-planning-info nil nil 'closed)
  9645. (and dostates (org-add-log-maybe 'state state 'findpos)))
  9646. ((and state dostates)
  9647. (org-add-log-maybe 'state state 'findpos))
  9648. ((equal state org-done-string)
  9649. ;; Planning info calls the note-setting command.
  9650. (org-add-planning-info 'closed (org-current-time)
  9651. (if (org-get-repeat) nil 'scheduled))
  9652. (org-add-log-maybe 'done state 'findpos))))
  9653. ;; Fixup tag positioning
  9654. (and org-auto-align-tags (org-set-tags nil t))
  9655. (run-hooks 'org-after-todo-state-change-hook)
  9656. (and (equal state org-done-string) (org-auto-repeat-maybe))
  9657. ))
  9658. ;; Fixup cursor location if close to the keyword
  9659. (if (and (outline-on-heading-p)
  9660. (not (bolp))
  9661. (save-excursion (beginning-of-line 1)
  9662. (looking-at org-todo-line-regexp))
  9663. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  9664. (progn
  9665. (goto-char (or (match-end 2) (match-end 1)))
  9666. (just-one-space))))
  9667. (defun org-get-repeat ()
  9668. "Return the REPEAT statement of this entry."
  9669. (save-match-data
  9670. (save-excursion
  9671. (org-back-to-heading t)
  9672. (if (re-search-forward
  9673. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  9674. (match-string 1)))))
  9675. (defun org-show-todo-tree (arg)
  9676. "Make a compact tree which shows all headlines marked with TODO.
  9677. The tree will show the lines where the regexp matches, and all higher
  9678. headlines above the match.
  9679. With \\[universal-argument] prefix, also show the DONE entries.
  9680. With a numeric prefix N, construct a sparse tree for the Nth element
  9681. of `org-todo-keywords'."
  9682. (interactive "P")
  9683. (let ((case-fold-search nil)
  9684. (kwd-re
  9685. (cond ((null arg) org-not-done-regexp)
  9686. ((equal arg '(4)) org-todo-regexp)
  9687. ((<= (prefix-numeric-value arg) (length org-todo-keywords))
  9688. (regexp-quote (nth (1- (prefix-numeric-value arg))
  9689. org-todo-keywords)))
  9690. (t (error "Invalid prefix argument: %s" arg)))))
  9691. (message "%d TODO entries found"
  9692. (org-occur (concat "^" outline-regexp " +" kwd-re )))))
  9693. (defun org-deadline ()
  9694. "Insert the DEADLINE: string to make a deadline.
  9695. A timestamp is also inserted - use \\[org-timestamp-up] and \\[org-timestamp-down]
  9696. to modify it to the correct date."
  9697. (interactive)
  9698. (org-add-planning-info 'deadline nil 'closed))
  9699. (defun org-schedule ()
  9700. "Insert the SCHEDULED: string to schedule a TODO item.
  9701. A timestamp is also inserted - use \\[org-timestamp-up] and \\[org-timestamp-down]
  9702. to modify it to the correct date."
  9703. (interactive)
  9704. (org-add-planning-info 'scheduled nil 'closed))
  9705. (defun org-add-planning-info (what &optional time &rest remove)
  9706. "Insert new timestamp with keyword in the line directly after the headline.
  9707. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  9708. If non is given, the user is prompted for a date.
  9709. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  9710. be removed."
  9711. (interactive)
  9712. (when what (setq time (or time (org-read-date nil 'to-time))))
  9713. (when (and org-insert-labeled-timestamps-at-point
  9714. (member what '(scheduled deadline)))
  9715. (insert
  9716. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  9717. (org-insert-time-stamp time)
  9718. (setq what nil))
  9719. (save-excursion
  9720. (save-restriction
  9721. (let (col list elt ts buffer-invisibility-spec)
  9722. (org-back-to-heading t)
  9723. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  9724. (goto-char (match-end 1))
  9725. (setq col (current-column))
  9726. (goto-char (1+ (match-end 0)))
  9727. (if (and (not (looking-at outline-regexp))
  9728. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  9729. "[^\r\n]*"))
  9730. (not (equal (match-string 1) org-clock-string)))
  9731. (narrow-to-region (match-beginning 0) (match-end 0))
  9732. (insert "\n")
  9733. (backward-char 1)
  9734. (narrow-to-region (point) (point))
  9735. (indent-to-column col))
  9736. ;; Check if we have to remove something.
  9737. (setq list (cons what remove))
  9738. (while list
  9739. (setq elt (pop list))
  9740. (goto-char (point-min))
  9741. (when (or (and (eq elt 'scheduled)
  9742. (re-search-forward org-scheduled-time-regexp nil t))
  9743. (and (eq elt 'deadline)
  9744. (re-search-forward org-deadline-time-regexp nil t))
  9745. (and (eq elt 'closed)
  9746. (re-search-forward org-closed-time-regexp nil t)))
  9747. (replace-match "")
  9748. (if (looking-at "--+<[^>]+>") (replace-match ""))
  9749. (if (looking-at " +") (replace-match ""))))
  9750. (goto-char (point-max))
  9751. (when what
  9752. (insert
  9753. (if (not (equal (char-before) ?\ )) " " "")
  9754. (cond ((eq what 'scheduled) org-scheduled-string)
  9755. ((eq what 'deadline) org-deadline-string)
  9756. ((eq what 'closed) org-closed-string))
  9757. " ")
  9758. (org-insert-time-stamp time (eq what 'closed) (eq what 'closed))
  9759. (end-of-line 1))
  9760. (goto-char (point-min))
  9761. (widen)
  9762. (if (looking-at "[ \t]+\r?\n")
  9763. (replace-match ""))
  9764. ts))))
  9765. (defvar org-log-note-marker (make-marker))
  9766. (defvar org-log-note-purpose nil)
  9767. (defvar org-log-note-state nil)
  9768. (defvar org-log-note-window-configuration nil)
  9769. (defvar org-log-note-return-to (make-marker))
  9770. (defvar org-log-post-message nil
  9771. "Message to be displayed after a log note has been stored.
  9772. The auto-repeater uses this.")
  9773. (defun org-add-log-maybe (&optional purpose state findpos)
  9774. (save-excursion
  9775. (when (and (listp org-log-done)
  9776. (memq purpose org-log-done))
  9777. (when findpos
  9778. (org-back-to-heading t)
  9779. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  9780. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  9781. "[^\r\n]*\\)?"))
  9782. (goto-char (match-end 0)))
  9783. (move-marker org-log-note-marker (point))
  9784. (setq org-log-note-purpose purpose)
  9785. (setq org-log-note-state state)
  9786. (add-hook 'post-command-hook 'org-add-log-note 'append))))
  9787. (defun org-add-log-note (&optional purpose)
  9788. "Pop up a window for taking a note, and add this note later at point."
  9789. (remove-hook 'post-command-hook 'org-add-log-note)
  9790. (setq org-log-note-window-configuration (current-window-configuration))
  9791. (delete-other-windows)
  9792. (move-marker org-log-note-return-to (point))
  9793. (switch-to-buffer (marker-buffer org-log-note-marker))
  9794. (goto-char org-log-note-marker)
  9795. (switch-to-buffer-other-window "*Org Note*")
  9796. (erase-buffer)
  9797. (let ((org-inhibit-startup t)) (org-mode))
  9798. (insert (format "# Insert note for %s, finish with C-c C-c.\n\n"
  9799. (cond
  9800. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  9801. ((eq org-log-note-purpose 'done) "closed todo item")
  9802. ((eq org-log-note-purpose 'state) "state change")
  9803. (t (error "This should not happen")))))
  9804. (org-set-local 'org-finish-function 'org-store-log-note))
  9805. (defun org-store-log-note ()
  9806. "Finish taking a log note, and insert it to where it belongs."
  9807. (let ((txt (buffer-string))
  9808. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  9809. lines ind)
  9810. (kill-buffer (current-buffer))
  9811. (if (string-match "^#.*\n[ \t\n]*" txt)
  9812. (setq txt (replace-match "" t t txt)))
  9813. (if (string-match "\\s-+\\'" txt)
  9814. (setq txt (replace-match "" t t txt)))
  9815. (setq lines (org-split-string txt "\n"))
  9816. (when (and note (string-match "\\S-" note))
  9817. (setq note
  9818. (org-replace-escapes
  9819. note
  9820. (list (cons "%u" (user-login-name))
  9821. (cons "%U" user-full-name)
  9822. (cons "%t" (format-time-string
  9823. (org-time-stamp-format 'long 'inactive)
  9824. (current-time)))
  9825. (cons "%s" (if org-log-note-state
  9826. (concat "\"" org-log-note-state "\"")
  9827. "")))))
  9828. (if lines (setq note (concat note " \\\\")))
  9829. (push note lines))
  9830. (save-excursion
  9831. (set-buffer (marker-buffer org-log-note-marker))
  9832. (save-excursion
  9833. (goto-char org-log-note-marker)
  9834. (move-marker org-log-note-marker nil)
  9835. (end-of-line 1)
  9836. (if (not (bolp)) (insert "\n")) (indent-relative nil)
  9837. (setq ind (concat (buffer-substring (point-at-bol) (point)) " "))
  9838. (insert " - " (pop lines))
  9839. (while lines
  9840. (insert "\n" ind (pop lines))))))
  9841. (set-window-configuration org-log-note-window-configuration)
  9842. (with-current-buffer (marker-buffer org-log-note-return-to)
  9843. (goto-char org-log-note-return-to))
  9844. (move-marker org-log-note-return-to nil)
  9845. (and org-log-post-message (message org-log-post-message)))
  9846. (defvar org-occur-highlights nil)
  9847. (make-variable-buffer-local 'org-occur-highlights)
  9848. (defun org-occur (regexp &optional keep-previous callback)
  9849. "Make a compact tree which shows all matches of REGEXP.
  9850. The tree will show the lines where the regexp matches, and all higher
  9851. headlines above the match. It will also show the heading after the match,
  9852. to make sure editing the matching entry is easy.
  9853. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  9854. call to `org-occur' will be kept, to allow stacking of calls to this
  9855. command.
  9856. If CALLBACK is non-nil, it is a function which is called to confirm
  9857. that the match should indeed be shown."
  9858. (interactive "sRegexp: \nP")
  9859. (or keep-previous (org-remove-occur-highlights nil nil t))
  9860. (let ((cnt 0))
  9861. (save-excursion
  9862. (goto-char (point-min))
  9863. (if (or (not keep-previous) ; do not want to keep
  9864. (not org-occur-highlights)) ; no previous matches
  9865. ;; hide everything
  9866. (org-overview))
  9867. (while (re-search-forward regexp nil t)
  9868. (when (or (not callback)
  9869. (save-match-data (funcall callback)))
  9870. (setq cnt (1+ cnt))
  9871. (org-highlight-new-match (match-beginning 0) (match-end 0))
  9872. (org-show-context 'occur-tree))))
  9873. (when org-remove-highlights-with-change
  9874. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  9875. nil 'local))
  9876. (unless org-sparse-tree-open-archived-trees
  9877. (org-hide-archived-subtrees (point-min) (point-max)))
  9878. (run-hooks 'org-occur-hook)
  9879. (if (interactive-p)
  9880. (message "%d match(es) for regexp %s" cnt regexp))
  9881. cnt))
  9882. ;; FIXME: Remove the siblings argument, or add args for the others too?
  9883. (defun org-show-context (&optional key)
  9884. "Make sure point and context and visible.
  9885. How much context is shown depends upon the variables
  9886. `org-show-hierarchy-above' and `org-show-following-heading'.
  9887. When SIBLINGS is non-nil, show all siblings on each hierarchy level."
  9888. (let ((heading-p (org-on-heading-p t))
  9889. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  9890. (following-p (org-get-alist-option org-show-following-heading key))
  9891. (siblings-p (org-get-alist-option org-show-siblings key)))
  9892. (catch 'exit
  9893. ;; Show heading or entry text
  9894. (if heading-p
  9895. (org-flag-heading nil) ; only show the heading
  9896. (and (or (org-invisible-p) (org-invisible-p2))
  9897. (org-show-hidden-entry))) ; show entire entry
  9898. (when following-p
  9899. ;; Show next sibling, or heading below text
  9900. (save-excursion
  9901. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  9902. (org-flag-heading nil))))
  9903. (when siblings-p (org-show-siblings))
  9904. (when hierarchy-p
  9905. ;; show all higher headings, possibly with siblings
  9906. (save-excursion
  9907. (while (and (condition-case nil
  9908. (progn (org-up-heading-all 1) t)
  9909. (error nil))
  9910. (not (bobp)))
  9911. (org-flag-heading nil)
  9912. (when siblings-p (org-show-siblings))))))))
  9913. (defun org-reveal (&optional siblings)
  9914. "Show current entry, hierarchy above it, and the following headline.
  9915. This can be used to show a consistent set of context around locations
  9916. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  9917. not t for the search context.
  9918. With optional argument SIBLINGS, on each level of the hierarchy all
  9919. siblings are shown. This repairs the tree structure to what it would
  9920. look like when opened with hierarchical calls to `org-cycle'."
  9921. (interactive "P")
  9922. (let ((org-show-hierarchy-above t)
  9923. (org-show-following-heading t)
  9924. (org-show-siblings (if siblings t org-show-siblings)))
  9925. (org-show-context nil)))
  9926. (defun org-highlight-new-match (beg end)
  9927. "Highlight from BEG to END and mark the highlight is an occur headline."
  9928. (let ((ov (org-make-overlay beg end)))
  9929. (org-overlay-put ov 'face 'secondary-selection)
  9930. (push ov org-occur-highlights)))
  9931. (defun org-remove-occur-highlights (&optional beg end noremove)
  9932. "Remove the occur highlights from the buffer.
  9933. BEG and END are ignored. If NOREMOVE is nil, remove this function
  9934. from the `before-change-functions' in the current buffer."
  9935. (interactive)
  9936. (unless org-inhibit-highlight-removal
  9937. (mapc 'org-delete-overlay org-occur-highlights)
  9938. (setq org-occur-highlights nil)
  9939. (unless noremove
  9940. (remove-hook 'before-change-functions
  9941. 'org-remove-occur-highlights 'local))))
  9942. ;;;; Priorities
  9943. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z]\\)\\] ?\\)"
  9944. "Regular expression matching the priority indicator.")
  9945. (defvar org-remove-priority-next-time nil)
  9946. (defun org-priority-up ()
  9947. "Increase the priority of the current item."
  9948. (interactive)
  9949. (org-priority 'up))
  9950. (defun org-priority-down ()
  9951. "Decrease the priority of the current item."
  9952. (interactive)
  9953. (org-priority 'down))
  9954. (defun org-priority (&optional action)
  9955. "Change the priority of an item by ARG.
  9956. ACTION can be set, up, or down."
  9957. (interactive)
  9958. (setq action (or action 'set))
  9959. (let (current new news have remove)
  9960. (save-excursion
  9961. (org-back-to-heading)
  9962. (if (looking-at org-priority-regexp)
  9963. (setq current (string-to-char (match-string 2))
  9964. have t)
  9965. (setq current org-default-priority))
  9966. (cond
  9967. ((eq action 'set)
  9968. (message "Priority A-%c, SPC to remove: " org-lowest-priority)
  9969. (setq new (read-char-exclusive))
  9970. (cond ((equal new ?\ ) (setq remove t))
  9971. ((or (< (upcase new) ?A) (> (upcase new) org-lowest-priority))
  9972. (error "Priority must be between `%c' and `%c'"
  9973. ?A org-lowest-priority))))
  9974. ((eq action 'up)
  9975. (setq new (1- current)))
  9976. ((eq action 'down)
  9977. (setq new (1+ current)))
  9978. (t (error "Invalid action")))
  9979. (setq new (min (max ?A (upcase new)) org-lowest-priority))
  9980. (setq news (format "%c" new))
  9981. (if have
  9982. (if remove
  9983. (replace-match "" t t nil 1)
  9984. (replace-match news t t nil 2))
  9985. (if remove
  9986. (error "No priority cookie found in line")
  9987. (looking-at org-todo-line-regexp)
  9988. (if (match-end 2)
  9989. (progn
  9990. (goto-char (match-end 2))
  9991. (insert " [#" news "]"))
  9992. (goto-char (match-beginning 3))
  9993. (insert "[#" news "] ")))))
  9994. (if remove
  9995. (message "Priority removed")
  9996. (message "Priority of current item set to %s" news))))
  9997. (defun org-get-priority (s)
  9998. "Find priority cookie and return priority."
  9999. (save-match-data
  10000. (if (not (string-match org-priority-regexp s))
  10001. (* 1000 (- org-lowest-priority org-default-priority))
  10002. (* 1000 (- org-lowest-priority
  10003. (string-to-char (match-string 2 s)))))))
  10004. ;;;; Tags
  10005. (defun org-scan-tags (action matcher &optional todo-only)
  10006. "Scan headline tags with inheritance and produce output ACTION.
  10007. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  10008. evaluated, testing if a given set of tags qualifies a headline for
  10009. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  10010. are included in the output."
  10011. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  10012. (mapconcat 'regexp-quote
  10013. (nreverse (cdr (reverse org-todo-keywords)))
  10014. "\\|")
  10015. "\\>\\)\\)? *\\(.*?\\)\\(:[A-Za-z_@0-9:]+:\\)?[ \t]*$"))
  10016. (props (list 'face nil
  10017. 'done-face 'org-done
  10018. 'undone-face nil
  10019. 'mouse-face 'highlight
  10020. 'org-not-done-regexp org-not-done-regexp
  10021. 'keymap org-agenda-keymap
  10022. 'help-echo
  10023. (format "mouse-2 or RET jump to org file %s"
  10024. (abbreviate-file-name buffer-file-name))))
  10025. (case-fold-search nil)
  10026. lspos
  10027. tags tags-list tags-alist (llast 0) rtn level category i txt
  10028. todo marker entry)
  10029. (save-excursion
  10030. (goto-char (point-min))
  10031. (when (eq action 'sparse-tree) (org-overview))
  10032. (while (re-search-forward re nil t)
  10033. (catch :skip
  10034. (setq todo (if (match-end 1) (match-string 2))
  10035. tags (if (match-end 4) (match-string 4)))
  10036. (goto-char (setq lspos (1+ (match-beginning 0))))
  10037. (setq level (funcall outline-level)
  10038. category (org-get-category))
  10039. (setq i llast llast level)
  10040. ;; remove tag lists from same and sublevels
  10041. (while (>= i level)
  10042. (when (setq entry (assoc i tags-alist))
  10043. (setq tags-alist (delete entry tags-alist)))
  10044. (setq i (1- i)))
  10045. ;; add the nex tags
  10046. (when tags
  10047. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  10048. tags-alist
  10049. (cons (cons level tags) tags-alist)))
  10050. ;; compile tags for current headline
  10051. (setq tags-list
  10052. (if org-use-tag-inheritance
  10053. (apply 'append (mapcar 'cdr tags-alist))
  10054. tags))
  10055. (when (and (or (not todo-only) todo)
  10056. (eval matcher)
  10057. (or (not org-agenda-skip-archived-trees)
  10058. (not (member org-archive-tag tags-list))))
  10059. (and (eq action 'agenda) (org-agenda-skip))
  10060. ;; list this headline
  10061. (if (eq action 'sparse-tree)
  10062. (progn
  10063. (org-show-context 'tags-tree))
  10064. (setq txt (org-format-agenda-item
  10065. ""
  10066. (concat
  10067. (if org-tags-match-list-sublevels
  10068. (make-string (1- level) ?.) "")
  10069. (org-get-heading))
  10070. category tags-list))
  10071. (goto-char lspos)
  10072. (setq marker (org-agenda-new-marker))
  10073. (org-add-props txt props
  10074. 'org-marker marker 'org-hd-marker marker 'category category)
  10075. (push txt rtn))
  10076. ;; if we are to skip sublevels, jump to end of subtree
  10077. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  10078. (when (and (eq action 'sparse-tree)
  10079. (not org-sparse-tree-open-archived-trees))
  10080. (org-hide-archived-subtrees (point-min) (point-max)))
  10081. (nreverse rtn)))
  10082. (defvar todo-only) ;; dynamically scoped
  10083. (defun org-tags-sparse-tree (&optional todo-only match)
  10084. "Create a sparse tree according to tags string MATCH.
  10085. MATCH can contain positive and negative selection of tags, like
  10086. \"+WORK+URGENT-WITHBOSS\".
  10087. If optional argument TODO_ONLY is non-nil, only select lines that are
  10088. also TODO lines."
  10089. (interactive "P")
  10090. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  10091. (defun org-make-tags-matcher (match)
  10092. "Create the TAGS//TODO matcher form for the selection string MATCH."
  10093. ;; todo-only is scoped dynamically into this function, and the function
  10094. ;; may change it it the matcher asksk for it.
  10095. (unless match
  10096. ;; Get a new match request, with completion
  10097. (setq org-last-tags-completion-table
  10098. (or org-tag-alist
  10099. org-last-tags-completion-table))
  10100. (setq match (completing-read
  10101. "Match: " 'org-tags-completion-function nil nil nil
  10102. 'org-tags-history))) ; FIXME: Separate history for this?
  10103. ;; Parse the string and create a lisp form
  10104. (let ((match0 match)
  10105. (re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL=\\([0-9]+\\)\\|[A-Za-z_@0-9]+\\)")
  10106. minus tag mm
  10107. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  10108. orterms term orlist re-p level-p)
  10109. (if (string-match "/+" match)
  10110. ;; match contains also a todo-matching request
  10111. (progn
  10112. (setq tagsmatch (substring match 0 (match-beginning 0))
  10113. todomatch (substring match (match-end 0)))
  10114. (if (string-match "^!" todomatch)
  10115. (setq todo-only t todomatch (substring todomatch 1)))
  10116. (if (string-match "^\\s-*$" todomatch)
  10117. (setq todomatch nil)))
  10118. ;; only matching tags
  10119. (setq tagsmatch match todomatch nil))
  10120. ;; Make the tags matcher
  10121. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  10122. (setq tagsmatcher t)
  10123. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  10124. (while (setq term (pop orterms))
  10125. (while (and (equal (substring term -1) "\\") orterms)
  10126. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  10127. (while (string-match re term)
  10128. (setq minus (and (match-end 1)
  10129. (equal (match-string 1 term) "-"))
  10130. tag (match-string 2 term)
  10131. re-p (equal (string-to-char tag) ?{)
  10132. level-p (match-end 3)
  10133. mm (cond
  10134. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  10135. (level-p `(= level ,(string-to-number
  10136. (match-string 3 term))))
  10137. (t `(member ,(downcase tag) tags-list)))
  10138. mm (if minus (list 'not mm) mm)
  10139. term (substring term (match-end 0)))
  10140. (push mm tagsmatcher))
  10141. (push (if (> (length tagsmatcher) 1)
  10142. (cons 'and tagsmatcher)
  10143. (car tagsmatcher))
  10144. orlist)
  10145. (setq tagsmatcher nil))
  10146. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist))))
  10147. ;; Make the todo matcher
  10148. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  10149. (setq todomatcher t)
  10150. (setq orterms (org-split-string todomatch "|") orlist nil)
  10151. (while (setq term (pop orterms))
  10152. (while (string-match re term)
  10153. (setq minus (and (match-end 1)
  10154. (equal (match-string 1 term) "-"))
  10155. kwd (match-string 2 term)
  10156. re-p (equal (string-to-char kwd) ?{)
  10157. term (substring term (match-end 0))
  10158. mm (if re-p
  10159. `(string-match ,(substring kwd 1 -1) todo)
  10160. (list 'equal 'todo kwd))
  10161. mm (if minus (list 'not mm) mm))
  10162. (push mm todomatcher))
  10163. (push (if (> (length todomatcher) 1)
  10164. (cons 'and todomatcher)
  10165. (car todomatcher))
  10166. orlist)
  10167. (setq todomatcher nil))
  10168. (setq todomatcher (if (> (length orlist) 1)
  10169. (cons 'or orlist) (car orlist))))
  10170. ;; Return the string and lisp forms of the matcher
  10171. (setq matcher (if todomatcher
  10172. (list 'and tagsmatcher todomatcher)
  10173. tagsmatcher))
  10174. (cons match0 matcher)))
  10175. (defun org-match-any-p (re list)
  10176. "Does re match any element of list?"
  10177. (setq list (mapcar (lambda (x) (string-match re x)) list))
  10178. (delq nil list))
  10179. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  10180. (defvar org-tags-overlay (org-make-overlay 1 1))
  10181. (org-detach-overlay org-tags-overlay)
  10182. (defun org-set-tags (&optional arg just-align)
  10183. "Set the tags for the current headline.
  10184. With prefix ARG, realign all tags in headings in the current buffer."
  10185. (interactive "P")
  10186. (let* ((re (concat "^" outline-regexp))
  10187. (current (org-get-tags))
  10188. table current-tags inherited-tags ; computed below when needed
  10189. tags p0 c0 c1 rpl)
  10190. (if arg
  10191. (save-excursion
  10192. (goto-char (point-min))
  10193. (let (buffer-invisibility-spec) ; Emacs 21 compatibility
  10194. (while (re-search-forward re nil t)
  10195. (org-set-tags nil t)
  10196. (end-of-line 1)))
  10197. (message "All tags realigned to column %d" org-tags-column))
  10198. (if just-align
  10199. (setq tags current)
  10200. ;; Get a new set of tags from the user
  10201. (setq table (or org-tag-alist (org-get-buffer-tags))
  10202. org-last-tags-completion-table table
  10203. current-tags (org-split-string current ":")
  10204. inherited-tags (nreverse
  10205. (nthcdr (length current-tags)
  10206. (nreverse (org-get-tags-at))))
  10207. tags
  10208. (if (or (eq t org-use-fast-tag-selection)
  10209. (and org-use-fast-tag-selection
  10210. (delq nil (mapcar 'cdr table))))
  10211. (org-fast-tag-selection current-tags inherited-tags table)
  10212. (let ((org-add-colon-after-tag-completion t))
  10213. (org-trim
  10214. (completing-read "Tags: " 'org-tags-completion-function
  10215. nil nil current 'org-tags-history)))))
  10216. (while (string-match "[-+&]+" tags)
  10217. ;; No boolean logic, just a list
  10218. (setq tags (replace-match ":" t t tags))))
  10219. (if (string-match "\\`[\t ]*\\'" tags)
  10220. (setq tags "")
  10221. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  10222. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  10223. ;; Insert new tags at the correct column
  10224. (beginning-of-line 1)
  10225. (if (re-search-forward
  10226. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  10227. (point-at-eol) t)
  10228. (progn
  10229. (if (equal tags "")
  10230. (setq rpl "")
  10231. (goto-char (match-beginning 0))
  10232. (setq c0 (current-column) p0 (point)
  10233. c1 (max (1+ c0) (if (> org-tags-column 0)
  10234. org-tags-column
  10235. (- (- org-tags-column) (length tags))))
  10236. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  10237. (replace-match rpl)
  10238. (and (not (featurep 'xemacs)) c0 (tabify p0 (point)))
  10239. tags)
  10240. (error "Tags alignment failed")))))
  10241. (defun org-tags-completion-function (string predicate &optional flag)
  10242. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  10243. (confirm (lambda (x) (stringp (car x)))))
  10244. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  10245. (setq s1 (match-string 1 string)
  10246. s2 (match-string 2 string))
  10247. (setq s1 "" s2 string))
  10248. (cond
  10249. ((eq flag nil)
  10250. ;; try completion
  10251. (setq rtn (try-completion s2 ctable confirm))
  10252. (if (stringp rtn)
  10253. (concat s1 s2 (substring rtn (length s2))
  10254. (if (and org-add-colon-after-tag-completion
  10255. (assoc rtn ctable))
  10256. ":" "")))
  10257. )
  10258. ((eq flag t)
  10259. ;; all-completions
  10260. (all-completions s2 ctable confirm)
  10261. )
  10262. ((eq flag 'lambda)
  10263. ;; exact match?
  10264. (assoc s2 ctable)))
  10265. ))
  10266. (defun org-fast-tag-insert (kwd tags face &optional end)
  10267. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  10268. (insert (format "%-12s" (concat kwd ":"))
  10269. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  10270. (or end "")))
  10271. (defun org-fast-tag-show-exit (flag)
  10272. (save-excursion
  10273. (goto-line 3)
  10274. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  10275. (replace-match ""))
  10276. (when flag
  10277. (end-of-line 1)
  10278. (move-to-column (- (window-width) 19) t)
  10279. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  10280. (defun org-set-current-tags-overlay (current prefix)
  10281. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  10282. (if (featurep 'xemacs)
  10283. (org-overlay-display org-tags-overlay (concat prefix s)
  10284. 'secondary-selection)
  10285. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  10286. (org-overlay-display org-tags-overlay (concat prefix s)))))
  10287. (defun org-fast-tag-selection (current inherited table)
  10288. "Fast tag selection with single keys.
  10289. CURRENT is the current list of tags in the headline, INHERITED is the
  10290. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  10291. possibly with grouping information.
  10292. If the keys are nil, a-z are automatically assigned.
  10293. Returns the new tags string, or nil to not change the current settings."
  10294. (let* ((maxlen (apply 'max (mapcar
  10295. (lambda (x)
  10296. (if (stringp (car x)) (string-width (car x)) 0))
  10297. table)))
  10298. (buf (current-buffer))
  10299. (buffer-tags nil)
  10300. (fwidth (+ maxlen 3 1 3))
  10301. (ncol (/ (- (window-width) 4) fwidth))
  10302. (i-face 'org-done)
  10303. (c-face 'org-tag)
  10304. tg cnt e c char c1 c2 ntable tbl rtn
  10305. ov-start ov-end ov-prefix
  10306. (exit-after-next org-fast-tag-selection-single-key)
  10307. groups ingroup)
  10308. (save-excursion
  10309. (beginning-of-line 1)
  10310. (if (looking-at ".*[ \t]\\(:[A-Za-z_@0-9:]+:\\)[ \t]*\\(\r\\|$\\)")
  10311. (setq ov-start (match-beginning 1)
  10312. ov-end (match-end 1)
  10313. ov-prefix "")
  10314. (setq ov-start (1- (point-at-eol))
  10315. ov-end (1+ ov-start))
  10316. (skip-chars-forward "^\n\r")
  10317. (setq ov-prefix
  10318. (concat
  10319. (buffer-substring (1- (point)) (point))
  10320. (if (> (current-column) org-tags-column)
  10321. " "
  10322. (make-string (- org-tags-column (current-column)) ?\ ))))))
  10323. (org-move-overlay org-tags-overlay ov-start ov-end)
  10324. (save-window-excursion
  10325. ;; FIXME: would it be better to keep the other windows?
  10326. ;; How about fast tag selection keeping all windows? Expert mode?
  10327. (delete-other-windows)
  10328. (split-window-vertically)
  10329. (switch-to-buffer-other-window (get-buffer-create " *Org tags*"))
  10330. (erase-buffer)
  10331. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  10332. (org-fast-tag-insert "Current" current c-face "\n\n")
  10333. (org-fast-tag-show-exit exit-after-next)
  10334. (org-set-current-tags-overlay current ov-prefix)
  10335. (setq tbl table char ?a cnt 0)
  10336. (while (setq e (pop tbl))
  10337. (cond
  10338. ((equal e '(:startgroup))
  10339. (push '() groups) (setq ingroup t)
  10340. (when (not (= cnt 0))
  10341. (setq cnt 0)
  10342. (insert "\n"))
  10343. (insert "{ "))
  10344. ((equal e '(:endgroup))
  10345. (setq ingroup nil cnt 0)
  10346. (insert "}\n"))
  10347. (t
  10348. (setq tg (car e) c2 nil)
  10349. (if (cdr e)
  10350. (setq c (cdr e))
  10351. ;; automatically assign a character.
  10352. (setq c1 (string-to-char
  10353. (downcase (substring
  10354. tg (if (= (string-to-char tg) ?@) 1 0)))))
  10355. (if (or (rassoc c1 ntable) (rassoc c1 table))
  10356. (while (or (rassoc char ntable) (rassoc char table))
  10357. (setq char (1+ char)))
  10358. (setq c2 c1))
  10359. (setq c (or c2 char)))
  10360. (if ingroup (push tg (car groups)))
  10361. (setq tg (org-add-props tg nil 'face
  10362. (cond
  10363. ((member tg current) c-face)
  10364. ((member tg inherited) i-face)
  10365. (t nil))))
  10366. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10367. (insert "[" c "] " tg (make-string
  10368. (- fwidth 4 (length tg)) ?\ ))
  10369. (push (cons tg c) ntable)
  10370. (when (= (setq cnt (1+ cnt)) ncol)
  10371. (insert "\n")
  10372. (if ingroup (insert " "))
  10373. (setq cnt 0)))))
  10374. (setq ntable (nreverse ntable))
  10375. (insert "\n")
  10376. (goto-char (point-min))
  10377. (if (fboundp 'fit-window-to-buffer) (fit-window-to-buffer))
  10378. (setq rtn
  10379. (catch 'exit
  10380. (while t
  10381. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [C-c]: multi%s"
  10382. (if groups " [!] no groups" ""))
  10383. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10384. (cond
  10385. ((= c ?\r) (throw 'exit t))
  10386. ((= c ?!)
  10387. (setq groups nil)
  10388. (goto-char (point-min))
  10389. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  10390. ((= c ?\C-c)
  10391. (org-fast-tag-show-exit
  10392. (setq exit-after-next (not exit-after-next))))
  10393. ((or (= c ?\C-g)
  10394. (and (= c ?q) (not (rassoc c ntable))))
  10395. (org-detach-overlay org-tags-overlay)
  10396. (setq quit-flag t))
  10397. ((= c ?\ )
  10398. (setq current nil)
  10399. (if exit-after-next (setq exit-after-next 'now)))
  10400. ((= c ?\t)
  10401. (condition-case nil
  10402. (setq tg (completing-read
  10403. "Tag: "
  10404. (or buffer-tags
  10405. (with-current-buffer buf
  10406. (org-get-buffer-tags)))))
  10407. (quit (setq tg "")))
  10408. (when (string-match "\\S-" tg)
  10409. (add-to-list 'buffer-tags (list tg))
  10410. (if (member tg current)
  10411. (setq current (delete tg current))
  10412. (push tg current)))
  10413. (if exit-after-next (setq exit-after-next 'now)))
  10414. ((setq e (rassoc c ntable) tg (car e))
  10415. (if (member tg current)
  10416. (setq current (delete tg current))
  10417. (loop for g in groups do
  10418. (if (member tg g)
  10419. (mapcar (lambda (x)
  10420. (setq current (delete x current)))
  10421. g)))
  10422. (push tg current))
  10423. (if exit-after-next (setq exit-after-next 'now))))
  10424. ;; Create a sorted list
  10425. (setq current
  10426. (sort current
  10427. (lambda (a b)
  10428. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  10429. (if (eq exit-after-next 'now) (throw 'exit t))
  10430. (goto-char (point-min))
  10431. (beginning-of-line 2)
  10432. (delete-region (point) (point-at-eol))
  10433. (org-fast-tag-insert "Current" current c-face)
  10434. (org-set-current-tags-overlay current ov-prefix)
  10435. (while (re-search-forward "\\[.\\] \\([a-zA-Z0-9_@]+\\)" nil t)
  10436. (setq tg (match-string 1))
  10437. (add-text-properties (match-beginning 1) (match-end 1)
  10438. (list 'face
  10439. (cond
  10440. ((member tg current) c-face)
  10441. ((member tg inherited) i-face)
  10442. (t nil)))))
  10443. (goto-char (point-min)))))
  10444. (org-detach-overlay org-tags-overlay)
  10445. (if rtn
  10446. (mapconcat 'identity current ":")
  10447. nil))))
  10448. (defun org-get-tags ()
  10449. "Get the TAGS string in the current headline."
  10450. (unless (org-on-heading-p t)
  10451. (error "Not on a heading"))
  10452. (save-excursion
  10453. (beginning-of-line 1)
  10454. (if (looking-at ".*[ \t]\\(:[A-Za-z_@0-9:]+:\\)[ \t]*\\(\r\\|$\\)")
  10455. (org-match-string-no-properties 1)
  10456. "")))
  10457. (defun org-get-buffer-tags ()
  10458. "Get a table of all tags used in the buffer, for completion."
  10459. (let (tags)
  10460. (save-excursion
  10461. (goto-char (point-min))
  10462. (while (re-search-forward "[ \t]:\\([A-Za-z_@0-9:]+\\):[ \t\r\n]" nil t)
  10463. (mapc (lambda (x) (add-to-list 'tags x))
  10464. (org-split-string (org-match-string-no-properties 1) ":"))))
  10465. (mapcar 'list tags)))
  10466. ;;;; Timestamps
  10467. (defvar org-last-changed-timestamp nil)
  10468. (defvar org-time-was-given) ; dynamically scoped parameter
  10469. (defvar org-ts-what) ; dynamically scoped parameter
  10470. (defun org-time-stamp (arg)
  10471. "Prompt for a date/time and insert a time stamp.
  10472. If the user specifies a time like HH:MM, or if this command is called
  10473. with a prefix argument, the time stamp will contain date and time.
  10474. Otherwise, only the date will be included. All parts of a date not
  10475. specified by the user will be filled in from the current date/time.
  10476. So if you press just return without typing anything, the time stamp
  10477. will represent the current date/time. If there is already a timestamp
  10478. at the cursor, it will be modified."
  10479. (interactive "P")
  10480. (let (org-time-was-given time)
  10481. (cond
  10482. ((and (org-at-timestamp-p)
  10483. (eq last-command 'org-time-stamp)
  10484. (eq this-command 'org-time-stamp))
  10485. (insert "--")
  10486. (setq time (let ((this-command this-command))
  10487. (org-read-date arg 'totime)))
  10488. (org-insert-time-stamp time (or org-time-was-given arg)))
  10489. ((org-at-timestamp-p)
  10490. (setq time (let ((this-command this-command))
  10491. (org-read-date arg 'totime)))
  10492. (when (org-at-timestamp-p) ; just to get the match data
  10493. (replace-match "")
  10494. (setq org-last-changed-timestamp
  10495. (org-insert-time-stamp time (or org-time-was-given arg))))
  10496. (message "Timestamp updated"))
  10497. (t
  10498. (setq time (let ((this-command this-command))
  10499. (org-read-date arg 'totime)))
  10500. (org-insert-time-stamp time (or org-time-was-given arg))))))
  10501. (defun org-time-stamp-inactive (&optional arg)
  10502. "Insert an inactive time stamp.
  10503. An inactive time stamp is enclosed in square brackets instead of angle
  10504. brackets. It is inactive in the sense that it does not trigger agenda entries,
  10505. does not link to the calendar and cannot be changed with the S-cursor keys.
  10506. So these are more for recording a certain time/date."
  10507. (interactive "P")
  10508. (let (org-time-was-given time)
  10509. (setq time (org-read-date arg 'totime))
  10510. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive)))
  10511. (defvar org-date-ovl (org-make-overlay 1 1))
  10512. (org-overlay-put org-date-ovl 'face 'org-warning)
  10513. (org-detach-overlay org-date-ovl)
  10514. (defvar org-ans1) ; dynamically scoped parameter
  10515. (defvar org-ans2) ; dynamically scoped parameter
  10516. (defun org-read-date (&optional with-time to-time from-string prompt)
  10517. "Read a date and make things smooth for the user.
  10518. The prompt will suggest to enter an ISO date, but you can also enter anything
  10519. which will at least partially be understood by `parse-time-string'.
  10520. Unrecognized parts of the date will default to the current day, month, year,
  10521. hour and minute. For example,
  10522. 3-2-5 --> 2003-02-05
  10523. feb 15 --> currentyear-02-15
  10524. sep 12 9 --> 2009-09-12
  10525. 12:45 --> today 12:45
  10526. 22 sept 0:34 --> currentyear-09-22 0:34
  10527. 12 --> currentyear-currentmonth-12
  10528. Fri --> nearest Friday (today or later)
  10529. +4 --> four days from today (only if +N is the only thing given)
  10530. etc.
  10531. The function understands only English month and weekday abbreviations,
  10532. but this can be configured with the variables `parse-time-months' and
  10533. `parse-time-weekdays'.
  10534. While prompting, a calendar is popped up - you can also select the
  10535. date with the mouse (button 1). The calendar shows a period of three
  10536. months. To scroll it to other months, use the keys `>' and `<'.
  10537. If you don't like the calendar, turn it off with
  10538. \(setq org-popup-calendar-for-date-prompt nil)
  10539. With optional argument TO-TIME, the date will immediately be converted
  10540. to an internal time.
  10541. With an optional argument WITH-TIME, the prompt will suggest to also
  10542. insert a time. Note that when WITH-TIME is not set, you can still
  10543. enter a time, and this function will inform the calling routine about
  10544. this change. The calling routine may then choose to change the format
  10545. used to insert the time stamp into the buffer to include the time."
  10546. (require 'parse-time)
  10547. (let* ((org-time-stamp-rounding-minutes
  10548. (if (equal with-time '(16)) 0 org-time-stamp-rounding-minutes))
  10549. (ct (org-current-time))
  10550. (default-time
  10551. ;; Default time is either today, or, when entering a range,
  10552. ;; the range start.
  10553. (if (save-excursion
  10554. (re-search-backward
  10555. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  10556. (- (point) 20) t))
  10557. (apply
  10558. 'encode-time
  10559. (mapcar (lambda(x) (or x 0))
  10560. (parse-time-string (match-string 1))))
  10561. ct))
  10562. (calendar-move-hook nil)
  10563. (view-diary-entries-initially nil)
  10564. (view-calendar-holidays-initially nil)
  10565. (timestr (format-time-string
  10566. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") default-time))
  10567. (prompt (concat (if prompt (concat prompt " ") "")
  10568. (format "YYYY-MM-DD [%s]: " timestr)))
  10569. ans org-ans1 org-ans2 (deltadays 0)
  10570. second minute hour day month year tl wday wday1)
  10571. (cond
  10572. (from-string (setq ans from-string))
  10573. (org-popup-calendar-for-date-prompt
  10574. (save-excursion
  10575. (save-window-excursion
  10576. (calendar)
  10577. (calendar-forward-day (- (time-to-days default-time)
  10578. (calendar-absolute-from-gregorian
  10579. (calendar-current-date))))
  10580. (org-eval-in-calendar nil)
  10581. (let* ((old-map (current-local-map))
  10582. (map (copy-keymap calendar-mode-map))
  10583. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  10584. (define-key map (kbd "RET") 'org-calendar-select)
  10585. (define-key map (if (featurep 'xemacs) [button1] [mouse-1])
  10586. 'org-calendar-select-mouse)
  10587. (define-key map (if (featurep 'xemacs) [button2] [mouse-2])
  10588. 'org-calendar-select-mouse)
  10589. (define-key minibuffer-local-map [(meta shift left)]
  10590. (lambda () (interactive)
  10591. (org-eval-in-calendar '(calendar-backward-month 1))))
  10592. (define-key minibuffer-local-map [(meta shift right)]
  10593. (lambda () (interactive)
  10594. (org-eval-in-calendar '(calendar-forward-month 1))))
  10595. (define-key minibuffer-local-map [(shift up)]
  10596. (lambda () (interactive)
  10597. (org-eval-in-calendar '(calendar-backward-week 1))))
  10598. (define-key minibuffer-local-map [(shift down)]
  10599. (lambda () (interactive)
  10600. (org-eval-in-calendar '(calendar-forward-week 1))))
  10601. (define-key minibuffer-local-map [(shift left)]
  10602. (lambda () (interactive)
  10603. (org-eval-in-calendar '(calendar-backward-day 1))))
  10604. (define-key minibuffer-local-map [(shift right)]
  10605. (lambda () (interactive)
  10606. (org-eval-in-calendar '(calendar-forward-day 1))))
  10607. (define-key minibuffer-local-map ">"
  10608. (lambda () (interactive)
  10609. (org-eval-in-calendar '(scroll-calendar-left 1))))
  10610. (define-key minibuffer-local-map "<"
  10611. (lambda () (interactive)
  10612. (org-eval-in-calendar '(scroll-calendar-right 1))))
  10613. (unwind-protect
  10614. (progn
  10615. (use-local-map map)
  10616. (setq ans (read-string prompt "" nil nil))
  10617. (if (not (string-match "\\S-" ans)) (setq ans nil))
  10618. (setq ans (or org-ans1 ans org-ans2)))
  10619. (use-local-map old-map))))))
  10620. (t ; Naked prompt only
  10621. (setq ans (read-string prompt "" nil timestr))))
  10622. (org-detach-overlay org-date-ovl)
  10623. (if (string-match "^[ \t]*[-+][0-9]+[ \t]*$" ans)
  10624. (setq deltadays (string-to-number ans) ans ""))
  10625. (if (string-match
  10626. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  10627. (progn
  10628. (setq year (if (match-end 2)
  10629. (string-to-number (match-string 2 ans))
  10630. (string-to-number (format-time-string "%Y")))
  10631. month (string-to-number (match-string 3 ans))
  10632. day (string-to-number (match-string 4 ans)))
  10633. (if (< year 100) (setq year (+ 2000 year)))
  10634. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  10635. t nil ans))))
  10636. (setq tl (parse-time-string ans)
  10637. year (or (nth 5 tl) (string-to-number (format-time-string "%Y" ct)))
  10638. month (or (nth 4 tl) (string-to-number (format-time-string "%m" ct)))
  10639. day (or (nth 3 tl) (string-to-number (format-time-string "%d" ct)))
  10640. hour (or (nth 2 tl) (string-to-number (format-time-string "%H" ct)))
  10641. minute (or (nth 1 tl) (string-to-number (format-time-string "%M" ct)))
  10642. second (or (nth 0 tl) 0)
  10643. wday (nth 6 tl))
  10644. (setq day (+ day deltadays))
  10645. (when (and wday (not (nth 3 tl)))
  10646. ;; Weekday was given, but no day, so pick that day in the week
  10647. ;; on or after the derived date.
  10648. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  10649. (unless (equal wday wday1)
  10650. (setq day (+ day (% (- wday wday1 -7) 7)))))
  10651. (if (and (boundp 'org-time-was-given)
  10652. (nth 2 tl))
  10653. (setq org-time-was-given t))
  10654. (if (< year 100) (setq year (+ 2000 year)))
  10655. (if to-time
  10656. (encode-time second minute hour day month year)
  10657. (if (or (nth 1 tl) (nth 2 tl))
  10658. (format "%04d-%02d-%02d %02d:%02d" year month day hour minute)
  10659. (format "%04d-%02d-%02d" year month day)))))
  10660. (defun org-eval-in-calendar (form)
  10661. "Eval FORM in the calendar window and return to current window.
  10662. Also, store the cursor date in variable org-ans2."
  10663. (let ((sw (selected-window)))
  10664. (select-window (get-buffer-window "*Calendar*"))
  10665. (eval form)
  10666. (when (calendar-cursor-to-date)
  10667. (let* ((date (calendar-cursor-to-date))
  10668. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10669. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  10670. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  10671. (select-window sw)))
  10672. (defun org-calendar-select ()
  10673. "Return to `org-read-date' with the date currently selected.
  10674. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10675. (interactive)
  10676. (when (calendar-cursor-to-date)
  10677. (let* ((date (calendar-cursor-to-date))
  10678. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10679. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10680. (if (active-minibuffer-window) (exit-minibuffer))))
  10681. (defun org-insert-time-stamp (time &optional with-hm inactive pre post)
  10682. "Insert a date stamp for the date given by the internal TIME.
  10683. WITH-HM means, use the stamp format that includes the time of the day.
  10684. INACTIVE means use square brackets instead of angular ones, so that the
  10685. stamp will not contribute to the agenda.
  10686. PRE and POST are optional strings to be inserted before and after the
  10687. stamp.
  10688. The command returns the inserted time stamp."
  10689. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  10690. stamp)
  10691. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  10692. (insert (or pre ""))
  10693. (insert (setq stamp (format-time-string fmt time)))
  10694. (insert (or post ""))
  10695. stamp))
  10696. (defun org-toggle-time-stamp-overlays ()
  10697. "Toggle the use of custom time stamp formats."
  10698. (interactive)
  10699. (setq org-display-custom-times (not org-display-custom-times))
  10700. (unless org-display-custom-times
  10701. (let ((p (point-min)) (bmp (buffer-modified-p)))
  10702. (while (setq p (next-single-property-change p 'display))
  10703. (if (and (get-text-property p 'display)
  10704. (eq (get-text-property p 'face) 'org-date))
  10705. (remove-text-properties
  10706. p (setq p (next-single-property-change p 'display))
  10707. '(display t))))
  10708. (set-buffer-modified-p bmp)))
  10709. (if (featurep 'xemacs)
  10710. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  10711. (org-restart-font-lock)
  10712. (setq org-table-may-need-update t)
  10713. (if org-display-custom-times
  10714. (message "Time stamps are overlayed with custom format")
  10715. (message "Time stamp overlays removed")))
  10716. (defun org-display-custom-time (beg end)
  10717. "Overlay modified time stamp format over timestamp between BED and END."
  10718. (let* ((t1 (save-match-data
  10719. (org-parse-time-string (buffer-substring beg end) t)))
  10720. (w1 (- end beg))
  10721. (with-hm (and (nth 1 t1) (nth 2 t1)))
  10722. (tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats))
  10723. (time (org-fix-decoded-time t1))
  10724. (str (org-add-props
  10725. (format-time-string
  10726. (substring tf 1 -1) (apply 'encode-time time))
  10727. nil 'mouse-face 'highlight))
  10728. (w2 (length str)))
  10729. (if (not (= w2 w1))
  10730. (add-text-properties (1+ beg) (+ 2 beg)
  10731. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  10732. (if (featurep 'xemacs)
  10733. (progn
  10734. (put-text-property beg end 'invisible t)
  10735. (put-text-property beg end 'end-glyph (make-glyph str)))
  10736. (put-text-property beg end 'display str))))
  10737. (defun org-translate-time (string)
  10738. "Translate all timestamps in STRING to custom format.
  10739. But do this only if the variable `org-display-custom-times' is set."
  10740. (when org-display-custom-times
  10741. (save-match-data
  10742. (let* ((start 0)
  10743. (re org-ts-regexp-both)
  10744. t1 with-hm inactive tf time str beg end)
  10745. (while (setq start (string-match re string start))
  10746. (setq beg (match-beginning 0)
  10747. end (match-end 0)
  10748. t1 (save-match-data
  10749. (org-parse-time-string (substring string beg end) t))
  10750. with-hm (and (nth 1 t1) (nth 2 t1))
  10751. inactive (equal (substring string beg (1+ beg)) "[")
  10752. tf (funcall (if with-hm 'cdr 'car)
  10753. org-time-stamp-custom-formats)
  10754. time (org-fix-decoded-time t1)
  10755. str (format-time-string
  10756. (concat
  10757. (if inactive "[" "<") (substring tf 1 -1)
  10758. (if inactive "]" ">"))
  10759. (apply 'encode-time time))
  10760. string (replace-match str t t string)
  10761. start (+ start (length str)))))))
  10762. string)
  10763. (defun org-fix-decoded-time (time)
  10764. "Set 0 instead of nil for the first 6 elements of time.
  10765. Don't touch the rest."
  10766. (let ((n 0))
  10767. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  10768. (defun org-days-to-time (timestamp-string)
  10769. "Difference between TIMESTAMP-STRING and now in days."
  10770. (- (time-to-days (org-time-string-to-time timestamp-string))
  10771. (time-to-days (current-time))))
  10772. (defun org-deadline-close (timestamp-string &optional ndays)
  10773. "Is the time in TIMESTAMP-STRING close to the current date?"
  10774. (and (< (org-days-to-time timestamp-string)
  10775. (or ndays org-deadline-warning-days))
  10776. (not (org-entry-is-done-p))))
  10777. (defun org-calendar-select-mouse (ev)
  10778. "Return to `org-read-date' with the date currently selected.
  10779. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10780. (interactive "e")
  10781. (mouse-set-point ev)
  10782. (when (calendar-cursor-to-date)
  10783. (let* ((date (calendar-cursor-to-date))
  10784. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10785. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10786. (if (active-minibuffer-window) (exit-minibuffer))))
  10787. (defun org-check-deadlines (ndays)
  10788. "Check if there are any deadlines due or past due.
  10789. A deadline is considered due if it happens within `org-deadline-warning-days'
  10790. days from today's date. If the deadline appears in an entry marked DONE,
  10791. it is not shown. The prefix arg NDAYS can be used to test that many
  10792. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  10793. (interactive "P")
  10794. (let* ((org-warn-days
  10795. (cond
  10796. ((equal ndays '(4)) 100000)
  10797. (ndays (prefix-numeric-value ndays))
  10798. (t org-deadline-warning-days)))
  10799. (case-fold-search nil)
  10800. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  10801. (callback
  10802. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  10803. (message "%d deadlines past-due or due within %d days"
  10804. (org-occur regexp nil callback)
  10805. org-warn-days)))
  10806. (defun org-evaluate-time-range (&optional to-buffer)
  10807. "Evaluate a time range by computing the difference between start and end.
  10808. Normally the result is just printed in the echo area, but with prefix arg
  10809. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  10810. If the time range is actually in a table, the result is inserted into the
  10811. next column.
  10812. For time difference computation, a year is assumed to be exactly 365
  10813. days in order to avoid rounding problems."
  10814. (interactive "P")
  10815. (or
  10816. (org-clock-update-time-maybe)
  10817. (save-excursion
  10818. (unless (org-at-date-range-p)
  10819. (goto-char (point-at-bol))
  10820. (re-search-forward org-tr-regexp (point-at-eol) t))
  10821. (if (not (org-at-date-range-p))
  10822. (error "Not at a time-stamp range, and none found in current line")))
  10823. (let* ((ts1 (match-string 1))
  10824. (ts2 (match-string 2))
  10825. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  10826. (match-end (match-end 0))
  10827. (time1 (org-time-string-to-time ts1))
  10828. (time2 (org-time-string-to-time ts2))
  10829. (t1 (time-to-seconds time1))
  10830. (t2 (time-to-seconds time2))
  10831. (diff (abs (- t2 t1)))
  10832. (negative (< (- t2 t1) 0))
  10833. ;; (ys (floor (* 365 24 60 60)))
  10834. (ds (* 24 60 60))
  10835. (hs (* 60 60))
  10836. (fy "%dy %dd %02d:%02d")
  10837. (fy1 "%dy %dd")
  10838. (fd "%dd %02d:%02d")
  10839. (fd1 "%dd")
  10840. (fh "%02d:%02d")
  10841. y d h m align)
  10842. (if havetime
  10843. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10844. y 0
  10845. d (floor (/ diff ds)) diff (mod diff ds)
  10846. h (floor (/ diff hs)) diff (mod diff hs)
  10847. m (floor (/ diff 60)))
  10848. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10849. y 0
  10850. d (floor (+ (/ diff ds) 0.5))
  10851. h 0 m 0))
  10852. (if (not to-buffer)
  10853. (message (org-make-tdiff-string y d h m))
  10854. (when (org-at-table-p)
  10855. (goto-char match-end)
  10856. (setq align t)
  10857. (and (looking-at " *|") (goto-char (match-end 0))))
  10858. (if (looking-at
  10859. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  10860. (replace-match ""))
  10861. (if negative (insert " -"))
  10862. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  10863. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  10864. (insert " " (format fh h m))))
  10865. (if align (org-table-align))
  10866. (message "Time difference inserted")))))
  10867. (defun org-make-tdiff-string (y d h m)
  10868. (let ((fmt "")
  10869. (l nil))
  10870. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  10871. l (push y l)))
  10872. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  10873. l (push d l)))
  10874. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  10875. l (push h l)))
  10876. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  10877. l (push m l)))
  10878. (apply 'format fmt (nreverse l))))
  10879. (defun org-time-string-to-time (s)
  10880. (apply 'encode-time (org-parse-time-string s)))
  10881. (defun org-parse-time-string (s &optional nodefault)
  10882. "Parse the standard Org-mode time string.
  10883. This should be a lot faster than the normal `parse-time-string'.
  10884. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  10885. hour and minute fields will be nil if not given."
  10886. (if (string-match org-ts-regexp1 s)
  10887. (list 0
  10888. (if (or (match-beginning 8) (not nodefault))
  10889. (string-to-number (or (match-string 8 s) "0")))
  10890. (if (or (match-beginning 7) (not nodefault))
  10891. (string-to-number (or (match-string 7 s) "0")))
  10892. (string-to-number (match-string 4 s))
  10893. (string-to-number (match-string 3 s))
  10894. (string-to-number (match-string 2 s))
  10895. nil nil nil)
  10896. (make-list 9 0)))
  10897. (defun org-timestamp-up (&optional arg)
  10898. "Increase the date item at the cursor by one.
  10899. If the cursor is on the year, change the year. If it is on the month or
  10900. the day, change that.
  10901. With prefix ARG, change by that many units."
  10902. (interactive "p")
  10903. (org-timestamp-change (prefix-numeric-value arg)))
  10904. (defun org-timestamp-down (&optional arg)
  10905. "Decrease the date item at the cursor by one.
  10906. If the cursor is on the year, change the year. If it is on the month or
  10907. the day, change that.
  10908. With prefix ARG, change by that many units."
  10909. (interactive "p")
  10910. (org-timestamp-change (- (prefix-numeric-value arg))))
  10911. (defun org-timestamp-up-day (&optional arg)
  10912. "Increase the date in the time stamp by one day.
  10913. With prefix ARG, change that many days."
  10914. (interactive "p")
  10915. (if (and (not (org-at-timestamp-p t))
  10916. (org-on-heading-p))
  10917. (org-todo 'up)
  10918. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  10919. (defun org-timestamp-down-day (&optional arg)
  10920. "Decrease the date in the time stamp by one day.
  10921. With prefix ARG, change that many days."
  10922. (interactive "p")
  10923. (if (and (not (org-at-timestamp-p t))
  10924. (org-on-heading-p))
  10925. (org-todo 'down)
  10926. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  10927. (defsubst org-pos-in-match-range (pos n)
  10928. (and (match-beginning n)
  10929. (<= (match-beginning n) pos)
  10930. (>= (match-end n) pos)))
  10931. (defun org-at-timestamp-p (&optional inactive-ok)
  10932. "Determine if the cursor is in or at a timestamp."
  10933. (interactive)
  10934. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  10935. (pos (point))
  10936. (ans (or (looking-at tsr)
  10937. (save-excursion
  10938. (skip-chars-backward "^[<\n\r\t")
  10939. (if (> (point) 1) (backward-char 1))
  10940. (and (looking-at tsr)
  10941. (> (- (match-end 0) pos) -1))))))
  10942. (and (boundp 'org-ts-what)
  10943. (setq org-ts-what
  10944. (cond
  10945. ((org-pos-in-match-range pos 2) 'year)
  10946. ((org-pos-in-match-range pos 3) 'month)
  10947. ((org-pos-in-match-range pos 7) 'hour)
  10948. ((org-pos-in-match-range pos 8) 'minute)
  10949. ((or (org-pos-in-match-range pos 4)
  10950. (org-pos-in-match-range pos 5)) 'day)
  10951. (t 'day))))
  10952. ans))
  10953. (defun org-timestamp-change (n &optional what)
  10954. "Change the date in the time stamp at point.
  10955. The date will be changed by N times WHAT. WHAT can be `day', `month',
  10956. `year', `minute', `second'. If WHAT is not given, the cursor position
  10957. in the timestamp determines what will be changed."
  10958. (let ((pos (point))
  10959. with-hm inactive
  10960. org-ts-what
  10961. ts time time0)
  10962. (if (not (org-at-timestamp-p t))
  10963. (error "Not at a timestamp"))
  10964. (if (and (not what) (not (eq org-ts-what 'day))
  10965. org-display-custom-times
  10966. (get-text-property (point) 'display)
  10967. (not (get-text-property (1- (point)) 'display)))
  10968. (setq org-ts-what 'day))
  10969. (setq org-ts-what (or what org-ts-what)
  10970. with-hm (<= (abs (- (cdr org-ts-lengths)
  10971. (- (match-end 0) (match-beginning 0))))
  10972. 1)
  10973. inactive (= (char-after (match-beginning 0)) ?\[)
  10974. ts (match-string 0))
  10975. (replace-match "")
  10976. (setq time0 (org-parse-time-string ts))
  10977. (setq time
  10978. (apply 'encode-time
  10979. (append
  10980. (list (or (car time0) 0))
  10981. (list (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0)))
  10982. (list (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0)))
  10983. (list (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0)))
  10984. (list (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0)))
  10985. (list (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0)))
  10986. (nthcdr 6 time0))))
  10987. (if (eq what 'calendar)
  10988. (let ((cal-date
  10989. (save-excursion
  10990. (save-match-data
  10991. (set-buffer "*Calendar*")
  10992. (calendar-cursor-to-date)))))
  10993. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  10994. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  10995. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  10996. (setcar time0 (or (car time0) 0))
  10997. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  10998. (setcar (nthcdr 2 time0) (or (nth 1 time0) 0))
  10999. (setq time (apply 'encode-time time0))))
  11000. (setq org-last-changed-timestamp
  11001. (org-insert-time-stamp time with-hm inactive))
  11002. (org-clock-update-time-maybe)
  11003. (goto-char pos)
  11004. ;; Try to recenter the calendar window, if any
  11005. (if (and org-calendar-follow-timestamp-change
  11006. (get-buffer-window "*Calendar*" t)
  11007. (memq org-ts-what '(day month year)))
  11008. (org-recenter-calendar (time-to-days time)))))
  11009. (defun org-recenter-calendar (date)
  11010. "If the calendar is visible, recenter it to DATE."
  11011. (let* ((win (selected-window))
  11012. (cwin (get-buffer-window "*Calendar*" t))
  11013. (calendar-move-hook nil))
  11014. (when cwin
  11015. (select-window cwin)
  11016. (calendar-goto-date (if (listp date) date
  11017. (calendar-gregorian-from-absolute date)))
  11018. (select-window win))))
  11019. (defun org-goto-calendar (&optional arg)
  11020. "Go to the Emacs calendar at the current date.
  11021. If there is a time stamp in the current line, go to that date.
  11022. A prefix ARG can be used to force the current date."
  11023. (interactive "P")
  11024. (let ((tsr org-ts-regexp) diff
  11025. (calendar-move-hook nil)
  11026. (view-calendar-holidays-initially nil)
  11027. (view-diary-entries-initially nil))
  11028. (if (or (org-at-timestamp-p)
  11029. (save-excursion
  11030. (beginning-of-line 1)
  11031. (looking-at (concat ".*" tsr))))
  11032. (let ((d1 (time-to-days (current-time)))
  11033. (d2 (time-to-days
  11034. (org-time-string-to-time (match-string 1)))))
  11035. (setq diff (- d2 d1))))
  11036. (calendar)
  11037. (calendar-goto-today)
  11038. (if (and diff (not arg)) (calendar-forward-day diff))))
  11039. (defun org-date-from-calendar ()
  11040. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  11041. If there is already a time stamp at the cursor position, update it."
  11042. (interactive)
  11043. (org-timestamp-change 0 'calendar))
  11044. ;;; The clock for measuring work time.
  11045. (defvar org-clock-marker (make-marker)
  11046. "Marker recording the last clock-in.")
  11047. (defun org-clock-in ()
  11048. "Start the clock on the current item.
  11049. If necessary, clock-out of the currently active clock."
  11050. (interactive)
  11051. (org-clock-out t)
  11052. (let (ts)
  11053. (save-excursion
  11054. (org-back-to-heading t)
  11055. (beginning-of-line 2)
  11056. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  11057. (not (equal (match-string 1) org-clock-string)))
  11058. (beginning-of-line 1))
  11059. (insert "\n") (backward-char 1)
  11060. (indent-relative)
  11061. (insert org-clock-string " ")
  11062. (setq ts (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  11063. (move-marker org-clock-marker (point) (buffer-base-buffer))
  11064. (message "Clock started at %s" ts))))
  11065. (defun org-clock-out (&optional fail-quietly)
  11066. "Stop the currently running clock.
  11067. If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
  11068. (interactive)
  11069. (catch 'exit
  11070. (if (not (marker-buffer org-clock-marker))
  11071. (if fail-quietly (throw 'exit t) (error "No active clock")))
  11072. (let (ts te s h m)
  11073. (save-excursion
  11074. (set-buffer (marker-buffer org-clock-marker))
  11075. (goto-char org-clock-marker)
  11076. (beginning-of-line 1)
  11077. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  11078. (equal (match-string 1) org-clock-string))
  11079. (setq ts (match-string 2))
  11080. (if fail-quietly (throw 'exit nil) (error "Clock start time is gone")))
  11081. (goto-char org-clock-marker)
  11082. (insert "--")
  11083. (setq te (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  11084. (setq s (- (time-to-seconds (apply 'encode-time (org-parse-time-string te)))
  11085. (time-to-seconds (apply 'encode-time (org-parse-time-string ts))))
  11086. h (floor (/ s 3600))
  11087. s (- s (* 3600 h))
  11088. m (floor (/ s 60))
  11089. s (- s (* 60 s)))
  11090. (insert " => " (format "%2d:%02d" h m))
  11091. (move-marker org-clock-marker nil)
  11092. (org-add-log-maybe 'clock-out)
  11093. (message "Clock stopped at %s after HH:MM = %d:%02d" te h m)))))
  11094. (defun org-clock-cancel ()
  11095. "Cancel the running clock be removing the start timestamp."
  11096. (interactive)
  11097. (if (not (marker-buffer org-clock-marker))
  11098. (error "No active clock"))
  11099. (save-excursion
  11100. (set-buffer (marker-buffer org-clock-marker))
  11101. (goto-char org-clock-marker)
  11102. (delete-region (1- (point-at-bol)) (point-at-eol)))
  11103. (message "Clock canceled"))
  11104. (defvar org-clock-file-total-minutes nil
  11105. "Holds the file total time in minutes, after a call to `org-clock-sum'.")
  11106. (make-variable-buffer-local 'org-clock-file-total-minutes)
  11107. (defun org-clock-sum (&optional tstart tend)
  11108. "Sum the times for each subtree.
  11109. Puts the resulting times in minutes as a text property on each headline."
  11110. (interactive)
  11111. (let* ((bmp (buffer-modified-p))
  11112. (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
  11113. org-clock-string
  11114. "[ \t]*\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)"))
  11115. (lmax 30)
  11116. (ltimes (make-vector lmax 0))
  11117. (t1 0)
  11118. (level 0)
  11119. ts te dt
  11120. time)
  11121. (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t))
  11122. (save-excursion
  11123. (goto-char (point-max))
  11124. (while (re-search-backward re nil t)
  11125. (if (match-end 2)
  11126. ;; A time
  11127. (setq ts (match-string 2)
  11128. te (match-string 3)
  11129. ts (time-to-seconds
  11130. (apply 'encode-time (org-parse-time-string ts)))
  11131. te (time-to-seconds
  11132. (apply 'encode-time (org-parse-time-string te)))
  11133. ts (if tstart (max ts tstart) ts)
  11134. te (if tend (min te tend) te)
  11135. dt (- te ts)
  11136. t1 (if (> dt 0) (+ t1 (floor (/ dt 60))) t1))
  11137. ;; A headline
  11138. (setq level (- (match-end 1) (match-beginning 1)))
  11139. (when (or (> t1 0) (> (aref ltimes level) 0))
  11140. (loop for l from 0 to level do
  11141. (aset ltimes l (+ (aref ltimes l) t1)))
  11142. (setq t1 0 time (aref ltimes level))
  11143. (loop for l from level to (1- lmax) do
  11144. (aset ltimes l 0))
  11145. (goto-char (match-beginning 0))
  11146. (put-text-property (point) (point-at-eol) :org-clock-minutes time))))
  11147. (setq org-clock-file-total-minutes (aref ltimes 0)))
  11148. (set-buffer-modified-p bmp)))
  11149. (defun org-clock-display (&optional total-only)
  11150. "Show subtree times in the entire buffer.
  11151. If TOTAL-ONLY is non-nil, only show the total time for the entire file
  11152. in the echo area."
  11153. (interactive)
  11154. (org-remove-clock-overlays)
  11155. (let (time h m p)
  11156. (org-clock-sum)
  11157. (unless total-only
  11158. (save-excursion
  11159. (goto-char (point-min))
  11160. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  11161. (goto-char p)
  11162. (when (setq time (get-text-property p :org-clock-minutes))
  11163. (org-put-clock-overlay time (funcall outline-level))))
  11164. (setq h (/ org-clock-file-total-minutes 60)
  11165. m (- org-clock-file-total-minutes (* 60 h)))
  11166. ;; Arrange to remove the overlays upon next change.
  11167. (when org-remove-highlights-with-change
  11168. (org-add-hook 'before-change-functions 'org-remove-clock-overlays
  11169. nil 'local))))
  11170. (message "Total file time: %d:%02d (%d hours and %d minutes)" h m h m)))
  11171. (defvar org-clock-overlays nil)
  11172. (make-variable-buffer-local 'org-clock-overlays)
  11173. (defun org-put-clock-overlay (time &optional level)
  11174. "Put an overlays on the current line, displaying TIME.
  11175. If LEVEL is given, prefix time with a corresponding number of stars.
  11176. This creates a new overlay and stores it in `org-clock-overlays', so that it
  11177. will be easy to remove."
  11178. (let* ((c 60) (h (floor (/ time 60))) (m (- time (* 60 h)))
  11179. (l (if level (org-get-legal-level level 0) 0))
  11180. (off 0)
  11181. ov tx)
  11182. (move-to-column c)
  11183. (unless (eolp) (skip-chars-backward "^ \t"))
  11184. (skip-chars-backward " \t")
  11185. (setq ov (org-make-overlay (1- (point)) (point-at-eol))
  11186. tx (concat (buffer-substring (1- (point)) (point))
  11187. (make-string (+ off (max 0 (- c (current-column)))) ?.)
  11188. (org-add-props (format "%s %2d:%02d%s"
  11189. (make-string l ?*) h m
  11190. (make-string (- 10 l) ?\ ))
  11191. '(face secondary-selection))
  11192. ""))
  11193. (if (not (featurep 'xemacs))
  11194. (org-overlay-put ov 'display tx)
  11195. (org-overlay-put ov 'invisible t)
  11196. (org-overlay-put ov 'end-glyph (make-glyph tx)))
  11197. (push ov org-clock-overlays)))
  11198. (defun org-remove-clock-overlays (&optional beg end noremove)
  11199. "Remove the occur highlights from the buffer.
  11200. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11201. from the `before-change-functions' in the current buffer."
  11202. (interactive)
  11203. (unless org-inhibit-highlight-removal
  11204. (mapc 'org-delete-overlay org-clock-overlays)
  11205. (setq org-clock-overlays nil)
  11206. (unless noremove
  11207. (remove-hook 'before-change-functions
  11208. 'org-remove-clock-overlays 'local))))
  11209. (defun org-clock-out-if-current ()
  11210. "Clock out if the current entry contains the running clock.
  11211. This is used to stop the clock after a TODO entry is marked DONE."
  11212. (when (and (equal state org-done-string)
  11213. (equal (marker-buffer org-clock-marker) (current-buffer))
  11214. (< (point) org-clock-marker)
  11215. (> (save-excursion (outline-next-heading) (point))
  11216. org-clock-marker))
  11217. ;; Clock out, but don't accept a logging message for this.
  11218. (let ((org-log-done (if (and (listp org-log-done)
  11219. (member 'clock-out org-log-done))
  11220. '(done)
  11221. org-log-done)))
  11222. (org-clock-out))))
  11223. (add-hook 'org-after-todo-state-change-hook
  11224. 'org-clock-out-if-current)
  11225. (defun org-check-running-clock ()
  11226. "Check if the current buffer contains the running clock.
  11227. If yes, offer to stop it and to save the buffer with the changes."
  11228. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  11229. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  11230. (buffer-name))))
  11231. (org-clock-out)
  11232. (when (y-or-n-p "Save changed buffer?")
  11233. (save-buffer))))
  11234. (defun org-clock-report ()
  11235. "Create a table containing a report about clocked time.
  11236. If the buffer contains lines
  11237. #+BEGIN: clocktable :maxlevel 3 :emphasize nil
  11238. #+END: clocktable
  11239. then the table will be inserted between these lines, replacing whatever
  11240. is was there before. If these lines are not in the buffer, the table
  11241. is inserted at point, surrounded by the special lines.
  11242. The BEGIN line can contain parameters. Allowed are:
  11243. :maxlevel The maximum level to be included in the table. Default is 3.
  11244. :emphasize t/nil, if levell 1 and level 2 should be bold/italic in the table."
  11245. (interactive)
  11246. (org-remove-clock-overlays)
  11247. (unless (org-find-dblock "clocktable")
  11248. (org-create-dblock (list :name "clocktable"
  11249. :maxlevel 2 :emphasize nil)))
  11250. (org-update-dblock))
  11251. (defun org-clock-update-time-maybe ()
  11252. "If this is a CLOCK line, update it and return t.
  11253. Otherwise, return nil."
  11254. (interactive)
  11255. (save-excursion
  11256. (beginning-of-line 1)
  11257. (skip-chars-forward " \t")
  11258. (when (looking-at org-clock-string)
  11259. (let ((re (concat "[ \t]*" org-clock-string
  11260. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  11261. "\\([ \t]*=>.*\\)?"))
  11262. ts te h m s)
  11263. (if (not (looking-at re))
  11264. nil
  11265. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  11266. (end-of-line 1)
  11267. (setq ts (match-string 1)
  11268. te (match-string 2))
  11269. (setq s (- (time-to-seconds
  11270. (apply 'encode-time (org-parse-time-string te)))
  11271. (time-to-seconds
  11272. (apply 'encode-time (org-parse-time-string ts))))
  11273. h (floor (/ s 3600))
  11274. s (- s (* 3600 h))
  11275. m (floor (/ s 60))
  11276. s (- s (* 60 s)))
  11277. (insert " => " (format "%2d:%02d" h m))
  11278. t)))))
  11279. (defun org-clock-special-range (key &optional time as-strings)
  11280. "Return two times bordering a special time range.
  11281. Key is a symbol specifying the range and can be one of `today', `yesterday',
  11282. `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
  11283. A week starts Monday 0:00 and ends Sunday 24:00.
  11284. The range is determined relative to TIME. TIME defaults to the current time.
  11285. The return value is a cons cell with two internal times like the ones
  11286. returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
  11287. the returned times will be formatted strings."
  11288. (let* ((tm (decode-time (or time (current-time))))
  11289. (s 0) (m (nth 1 tm)) (h (nth 2 tm))
  11290. (d (nth 3 tm)) (month (nth 4 tm)) (y (nth 5 tm))
  11291. (dow (nth 6 tm))
  11292. s1 m1 h1 d1 month1 y1 diff ts te fm)
  11293. (cond
  11294. ((eq key 'today)
  11295. (setq h 0 m 0 h1 24 m1 0))
  11296. ((eq key 'yesterday)
  11297. (setq d (1- d) h 0 m 0 h1 24 m1 0))
  11298. ((eq key 'thisweek)
  11299. (setq diff (if (= dow 0) 6 (1- dow))
  11300. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  11301. ((eq key 'lastweek)
  11302. (setq diff (+ 7 (if (= dow 0) 6 (1- dow)))
  11303. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  11304. ((eq key 'thismonth)
  11305. (setq d 1 h 0 m 0 d1 1 month1 (1+ month) h1 0 m1 0))
  11306. ((eq key 'lastmonth)
  11307. (setq d 1 h 0 m 0 d1 1 month (1- month) month1 (1+ month) h1 0 m1 0))
  11308. ((eq key 'thisyear)
  11309. (setq m 0 h 0 d 1 month 1 y1 (1+ y)))
  11310. ((eq key 'lastyear)
  11311. (setq m 0 h 0 d 1 month 1 y (1- y) y1 (1+ y)))
  11312. (t (error "No such time block %s" key)))
  11313. (setq ts (encode-time s m h d month y)
  11314. te (encode-time (or s1 s) (or m1 m) (or h1 h)
  11315. (or d1 d) (or month1 month) (or y1 y)))
  11316. (setq fm (cdr org-time-stamp-formats))
  11317. (if as-strings
  11318. (cons (format-time-string fm ts) (format-time-string fm te))
  11319. (cons ts te))))
  11320. (defun org-dblock-write:clocktable (params)
  11321. "Write the standard clocktable."
  11322. (let ((hlchars '((1 . "*") (2 . ?/)))
  11323. (emph nil)
  11324. (ins (make-marker))
  11325. ipos time h m p level hlc hdl maxlevel
  11326. ts te cc block)
  11327. (setq maxlevel (or (plist-get params :maxlevel) 3)
  11328. emph (plist-get params :emphasize)
  11329. ts (plist-get params :tstart)
  11330. te (plist-get params :tend)
  11331. block (plist-get params :block))
  11332. (when block
  11333. (setq cc (org-clock-special-range block nil t)
  11334. ts (car cc) te (cdr cc)))
  11335. (if ts (setq ts (time-to-seconds
  11336. (apply 'encode-time (org-parse-time-string ts)))))
  11337. (if te (setq te (time-to-seconds
  11338. (apply 'encode-time (org-parse-time-string te)))))
  11339. (move-marker ins (point))
  11340. (setq ipos (point))
  11341. (insert-before-markers "Clock summary at ["
  11342. (substring
  11343. (format-time-string (cdr org-time-stamp-formats))
  11344. 1 -1)
  11345. "]."
  11346. (if block
  11347. (format " Considered range is /%s/." block)
  11348. "")
  11349. "\n\n|L|Headline|Time|\n")
  11350. (org-clock-sum ts te)
  11351. (setq h (/ org-clock-file-total-minutes 60)
  11352. m (- org-clock-file-total-minutes (* 60 h)))
  11353. (insert-before-markers "|-\n|0|" "*Total file time*| "
  11354. (format "*%d:%02d*" h m)
  11355. "|\n")
  11356. (goto-char (point-min))
  11357. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  11358. (goto-char p)
  11359. (when (setq time (get-text-property p :org-clock-minutes))
  11360. (save-excursion
  11361. (beginning-of-line 1)
  11362. (when (and (looking-at "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[0-9a-zA-Z_@:]+:\\)?[ \t]*$")
  11363. (setq level (- (match-end 1) (match-beginning 1)))
  11364. (<= level maxlevel))
  11365. (setq hlc (if emph (or (cdr (assoc level hlchars)) "") "")
  11366. hdl (match-string 2)
  11367. h (/ time 60)
  11368. m (- time (* 60 h)))
  11369. (goto-char ins)
  11370. (if (= level 1) (insert-before-markers "|-\n"))
  11371. (insert-before-markers
  11372. "| " (int-to-string level) "|" hlc hdl hlc " |"
  11373. (make-string (1- level) ?|)
  11374. hlc
  11375. (format "%d:%02d" h m)
  11376. hlc
  11377. " |\n")))))
  11378. (goto-char ins)
  11379. (backward-delete-char 1)
  11380. (goto-char ipos)
  11381. (skip-chars-forward "^|")
  11382. (org-table-align)))
  11383. (defun org-collect-clock-time-entries ()
  11384. "Return an internal list with clocking information.
  11385. This list has one entry for each CLOCK interval.
  11386. FIXME: describe the elements."
  11387. (interactive)
  11388. (let ((re (concat "^[ \t]*" org-clock-string
  11389. " *\\[\\(.*?\\)\\]--\\[\\(.*?\\)\\]"))
  11390. rtn beg end next cont level title total closedp leafp
  11391. clockpos titlepos h m donep)
  11392. (save-excursion
  11393. (org-clock-sum)
  11394. (goto-char (point-min))
  11395. (while (re-search-forward re nil t)
  11396. (setq clockpos (match-beginning 0)
  11397. beg (match-string 1) end (match-string 2)
  11398. cont (match-end 0))
  11399. (setq beg (apply 'encode-time (org-parse-time-string beg))
  11400. end (apply 'encode-time (org-parse-time-string end)))
  11401. (org-back-to-heading t)
  11402. (setq donep (org-entry-is-done-p))
  11403. (setq titlepos (point)
  11404. total (or (get-text-property (1+ (point)) :org-clock-minutes) 0)
  11405. h (/ total 60) m (- total (* 60 h))
  11406. total (cons h m))
  11407. (looking-at "\\(\\*+\\) +\\(.*\\)")
  11408. (setq level (- (match-end 1) (match-beginning 1))
  11409. title (org-match-string-no-properties 2))
  11410. (save-excursion (outline-next-heading) (setq next (point)))
  11411. (setq closedp (re-search-forward org-closed-time-regexp next t))
  11412. (goto-char next)
  11413. (setq leafp (and (looking-at "^\\*+ ")
  11414. (<= (- (match-end 0) (point)) level)))
  11415. (push (list beg end clockpos closedp donep
  11416. total title titlepos level leafp)
  11417. rtn)
  11418. (goto-char cont)))
  11419. (nreverse rtn)))
  11420. ;;;; Agenda, and Diary Integration
  11421. ;;; Define the Org-agenda-mode
  11422. (defvar org-agenda-mode-map (make-sparse-keymap)
  11423. "Keymap for `org-agenda-mode'.")
  11424. (defvar org-agenda-menu) ; defined later in this file.
  11425. (defvar org-agenda-follow-mode nil)
  11426. (defvar org-agenda-show-log nil)
  11427. (defvar org-agenda-redo-command nil)
  11428. (defvar org-agenda-mode-hook nil)
  11429. (defvar org-agenda-type nil)
  11430. (defvar org-agenda-force-single-file nil)
  11431. (defun org-agenda-mode ()
  11432. "Mode for time-sorted view on action items in Org-mode files.
  11433. The following commands are available:
  11434. \\{org-agenda-mode-map}"
  11435. (interactive)
  11436. (kill-all-local-variables)
  11437. (setq org-agenda-undo-list nil
  11438. org-agenda-pending-undo-list nil)
  11439. (setq major-mode 'org-agenda-mode)
  11440. (setq mode-name "Org-Agenda")
  11441. (use-local-map org-agenda-mode-map)
  11442. (easy-menu-add org-agenda-menu)
  11443. (if org-startup-truncated (setq truncate-lines t))
  11444. (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
  11445. (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
  11446. ;; Make sure properties are removed when copying text
  11447. (when (boundp 'buffer-substring-filters)
  11448. (org-set-local 'buffer-substring-filters
  11449. (cons (lambda (x) (set-text-properties 0 (length x) nil x) x)
  11450. buffer-substring-filters)))
  11451. (unless org-agenda-keep-modes
  11452. (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
  11453. org-agenda-show-log nil))
  11454. (easy-menu-change
  11455. '("Agenda") "Agenda Files"
  11456. (append
  11457. (list
  11458. (vector
  11459. (if (get 'org-agenda-files 'org-restrict)
  11460. "Restricted to single file"
  11461. "Edit File List")
  11462. '(org-edit-agenda-file-list)
  11463. (not (get 'org-agenda-files 'org-restrict)))
  11464. "--")
  11465. (mapcar 'org-file-menu-entry (org-agenda-files))))
  11466. (org-agenda-set-mode-name)
  11467. (apply
  11468. (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
  11469. (list 'org-agenda-mode-hook)))
  11470. (substitute-key-definition 'undo 'org-agenda-undo
  11471. org-agenda-mode-map global-map)
  11472. (define-key org-agenda-mode-map "\C-i" 'org-agenda-goto)
  11473. (define-key org-agenda-mode-map [(tab)] 'org-agenda-goto)
  11474. (define-key org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
  11475. (define-key org-agenda-mode-map "\C-k" 'org-agenda-kill)
  11476. (define-key org-agenda-mode-map "\C-c$" 'org-agenda-archive)
  11477. (define-key org-agenda-mode-map "$" 'org-agenda-archive)
  11478. (define-key org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
  11479. (define-key org-agenda-mode-map " " 'org-agenda-show)
  11480. (define-key org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
  11481. (define-key org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
  11482. (define-key org-agenda-mode-map "b" 'org-agenda-tree-to-indirect-buffer)
  11483. (define-key org-agenda-mode-map "o" 'delete-other-windows)
  11484. (define-key org-agenda-mode-map "L" 'org-agenda-recenter)
  11485. (define-key org-agenda-mode-map "t" 'org-agenda-todo)
  11486. (define-key org-agenda-mode-map "a" 'org-agenda-toggle-archive-tag)
  11487. (define-key org-agenda-mode-map ":" 'org-agenda-set-tags)
  11488. (define-key org-agenda-mode-map "." 'org-agenda-goto-today)
  11489. (define-key org-agenda-mode-map "d" 'org-agenda-day-view)
  11490. (define-key org-agenda-mode-map "w" 'org-agenda-week-view)
  11491. (define-key org-agenda-mode-map (org-key 'S-right) 'org-agenda-date-later)
  11492. (define-key org-agenda-mode-map (org-key 'S-left) 'org-agenda-date-earlier)
  11493. (define-key org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-date-later)
  11494. (define-key org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-date-earlier)
  11495. (define-key org-agenda-mode-map ">" 'org-agenda-date-prompt)
  11496. (define-key org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
  11497. (define-key org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
  11498. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  11499. (while l (define-key org-agenda-mode-map
  11500. (int-to-string (pop l)) 'digit-argument)))
  11501. (define-key org-agenda-mode-map "f" 'org-agenda-follow-mode)
  11502. (define-key org-agenda-mode-map "l" 'org-agenda-log-mode)
  11503. (define-key org-agenda-mode-map "D" 'org-agenda-toggle-diary)
  11504. (define-key org-agenda-mode-map "g" 'org-agenda-toggle-time-grid)
  11505. (define-key org-agenda-mode-map "r" 'org-agenda-redo)
  11506. (define-key org-agenda-mode-map "q" 'org-agenda-quit)
  11507. (define-key org-agenda-mode-map "x" 'org-agenda-exit)
  11508. (define-key org-agenda-mode-map "s" 'org-save-all-org-buffers)
  11509. (define-key org-agenda-mode-map "P" 'org-agenda-show-priority)
  11510. (define-key org-agenda-mode-map "T" 'org-agenda-show-tags)
  11511. (define-key org-agenda-mode-map "n" 'next-line)
  11512. (define-key org-agenda-mode-map "p" 'previous-line)
  11513. (define-key org-agenda-mode-map "\C-n" 'org-agenda-next-date-line)
  11514. (define-key org-agenda-mode-map "\C-p" 'org-agenda-previous-date-line)
  11515. (define-key org-agenda-mode-map "," 'org-agenda-priority)
  11516. (define-key org-agenda-mode-map "\C-c," 'org-agenda-priority)
  11517. (define-key org-agenda-mode-map "i" 'org-agenda-diary-entry)
  11518. (define-key org-agenda-mode-map "c" 'org-agenda-goto-calendar)
  11519. (eval-after-load "calendar"
  11520. '(define-key calendar-mode-map org-calendar-to-agenda-key
  11521. 'org-calendar-goto-agenda))
  11522. (define-key org-agenda-mode-map "C" 'org-agenda-convert-date)
  11523. (define-key org-agenda-mode-map "m" 'org-agenda-phases-of-moon)
  11524. (define-key org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
  11525. (define-key org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
  11526. (define-key org-agenda-mode-map "h" 'org-agenda-holidays)
  11527. (define-key org-agenda-mode-map "H" 'org-agenda-holidays)
  11528. (define-key org-agenda-mode-map "+" 'org-agenda-priority-up)
  11529. (define-key org-agenda-mode-map "I" 'org-agenda-clock-in)
  11530. (define-key org-agenda-mode-map "O" 'org-agenda-clock-out)
  11531. (define-key org-agenda-mode-map "X" 'org-agenda-clock-cancel)
  11532. (define-key org-agenda-mode-map "-" 'org-agenda-priority-down)
  11533. (define-key org-agenda-mode-map (org-key 'S-up) 'org-agenda-priority-up)
  11534. (define-key org-agenda-mode-map (org-key 'S-down) 'org-agenda-priority-down)
  11535. (define-key org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
  11536. (define-key org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
  11537. (define-key org-agenda-mode-map [(right)] 'org-agenda-later)
  11538. (define-key org-agenda-mode-map [(left)] 'org-agenda-earlier)
  11539. (define-key org-agenda-mode-map "\C-c\C-x\C-c" 'org-export-icalendar-combine-agenda-files)
  11540. (defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
  11541. "Local keymap for agenda entries from Org-mode.")
  11542. (define-key org-agenda-keymap
  11543. (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
  11544. (define-key org-agenda-keymap
  11545. (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
  11546. (when org-agenda-mouse-1-follows-link
  11547. (define-key org-agenda-keymap [follow-link] 'mouse-face))
  11548. (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
  11549. '("Agenda"
  11550. ("Agenda Files")
  11551. "--"
  11552. ["Show" org-agenda-show t]
  11553. ["Go To (other window)" org-agenda-goto t]
  11554. ["Go To (this window)" org-agenda-switch-to t]
  11555. ["Follow Mode" org-agenda-follow-mode
  11556. :style toggle :selected org-agenda-follow-mode :active t]
  11557. ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
  11558. "--"
  11559. ["Cycle TODO" org-agenda-todo t]
  11560. ["Archive subtree" org-agenda-archive t]
  11561. ["Delete subtree" org-agenda-kill t]
  11562. "--"
  11563. ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
  11564. ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
  11565. ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
  11566. "--"
  11567. ("Tags"
  11568. ["Show all Tags" org-agenda-show-tags t]
  11569. ["Set Tags" org-agenda-set-tags t])
  11570. ("Date/Schedule"
  11571. ["Schedule" org-agenda-schedule t]
  11572. ["Set Deadline" org-agenda-deadline t]
  11573. "--"
  11574. ["Change date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
  11575. ["Change date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
  11576. ["Change date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
  11577. ("Priority"
  11578. ["Set Priority" org-agenda-priority t]
  11579. ["Increase Priority" org-agenda-priority-up t]
  11580. ["Decrease Priority" org-agenda-priority-down t]
  11581. ["Show Priority" org-agenda-show-priority t])
  11582. ("Calendar/Diary"
  11583. ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
  11584. ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
  11585. ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
  11586. ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
  11587. ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
  11588. ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
  11589. "--"
  11590. ["Create iCalendar file" org-export-icalendar-combine-agenda-files t])
  11591. "--"
  11592. ("View"
  11593. ["Day View" org-agenda-day-view :active (org-agenda-check-type nil 'agenda)
  11594. :style radio :selected (equal org-agenda-ndays 1)]
  11595. ["Week View" org-agenda-week-view :active (org-agenda-check-type nil 'agenda)
  11596. :style radio :selected (equal org-agenda-ndays 7)]
  11597. "--"
  11598. ["Show Logbook entries" org-agenda-log-mode
  11599. :style toggle :selected org-agenda-show-log :active (org-agenda-check-type nil 'agenda 'timeline)]
  11600. ["Include Diary" org-agenda-toggle-diary
  11601. :style toggle :selected org-agenda-include-diary :active (org-agenda-check-type nil 'agenda)]
  11602. ["Use Time Grid" org-agenda-toggle-time-grid
  11603. :style toggle :selected org-agenda-use-time-grid :active (org-agenda-check-type nil 'agenda)])
  11604. ["Rebuild buffer" org-agenda-redo t]
  11605. ["Save all Org-mode Buffers" org-save-all-org-buffers t]
  11606. "--"
  11607. ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
  11608. "--"
  11609. ["Quit" org-agenda-quit t]
  11610. ["Exit and Release Buffers" org-agenda-exit t]
  11611. ))
  11612. ;;; Agenda undo
  11613. (defvar org-agenda-allow-remote-undo t
  11614. "Non-nil means, allow remote undo from the agenda buffer.")
  11615. (defvar org-agenda-undo-list nil
  11616. "List of undoable operations in the agenda since last refresh.")
  11617. (defvar org-agenda-undo-has-started-in nil
  11618. "Buffers that have already seen `undo-start' in the current undo sequence.")
  11619. (defvar org-agenda-pending-undo-list nil
  11620. "In a series of undo commands, this is the list of remaning undo items.")
  11621. (defmacro org-with-remote-undo (_buffer &rest _body)
  11622. "Execute BODY while recording undo information in two buffers."
  11623. (declare (indent 1) (debug t))
  11624. `(let ((_cline (org-current-line))
  11625. (_cmd this-command)
  11626. (_buf1 (current-buffer))
  11627. (_buf2 ,_buffer)
  11628. (_undo1 buffer-undo-list)
  11629. (_undo2 (with-current-buffer ,_buffer buffer-undo-list))
  11630. _c1 _c2)
  11631. ,@_body
  11632. (when org-agenda-allow-remote-undo
  11633. (setq _c1 (org-verify-change-for-undo
  11634. _undo1 (with-current-buffer _buf1 buffer-undo-list))
  11635. _c2 (org-verify-change-for-undo
  11636. _undo2 (with-current-buffer _buf2 buffer-undo-list)))
  11637. (when (or _c1 _c2)
  11638. ;; make sure there are undo boundaries
  11639. (and _c1 (with-current-buffer _buf1 (undo-boundary)))
  11640. (and _c2 (with-current-buffer _buf2 (undo-boundary)))
  11641. ;; remember which buffer to undo
  11642. (push (list _cmd _cline _buf1 _c1 _buf2 _c2)
  11643. org-agenda-undo-list)))))
  11644. (defun org-agenda-undo ()
  11645. "Undo a remote editing step in the agenda.
  11646. This undoes changes both in the agenda buffer and in the remote buffer
  11647. that have been changed along."
  11648. (interactive)
  11649. (or org-agenda-allow-remote-undo
  11650. (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo."))
  11651. (if (not (eq this-command last-command))
  11652. (setq org-agenda-undo-has-started-in nil
  11653. org-agenda-pending-undo-list org-agenda-undo-list))
  11654. (if (not org-agenda-pending-undo-list)
  11655. (error "No further undo information"))
  11656. (let* ((entry (pop org-agenda-pending-undo-list))
  11657. buf line cmd rembuf)
  11658. (setq cmd (pop entry) line (pop entry))
  11659. (setq rembuf (nth 2 entry))
  11660. (org-with-remote-undo rembuf
  11661. (while (bufferp (setq buf (pop entry)))
  11662. (if (pop entry)
  11663. (with-current-buffer buf
  11664. (let ((last-undo-buffer buf)
  11665. buffer-read-only)
  11666. (unless (memq buf org-agenda-undo-has-started-in)
  11667. (push buf org-agenda-undo-has-started-in)
  11668. (make-local-variable 'pending-undo-list)
  11669. (undo-start))
  11670. (while (and pending-undo-list
  11671. (listp pending-undo-list)
  11672. (not (car pending-undo-list)))
  11673. (pop pending-undo-list))
  11674. (undo-more 1))))))
  11675. (goto-line line)
  11676. (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
  11677. (defun org-verify-change-for-undo (l1 l2)
  11678. "Verify that a real change occurred between the undo lists L1 and L2."
  11679. (while (and l1 (listp l1) (null (car l1))) (pop l1))
  11680. (while (and l2 (listp l2) (null (car l2))) (pop l2))
  11681. (not (eq l1 l2)))
  11682. ;;; Agenda dispatch
  11683. (defvar org-agenda-restrict nil)
  11684. (defvar org-agenda-restrict-begin (make-marker))
  11685. (defvar org-agenda-restrict-end (make-marker))
  11686. (defvar org-agenda-last-dispatch-buffer nil)
  11687. ;;;###autoload
  11688. (defun org-agenda (arg)
  11689. "Dispatch agenda commands to collect entries to the agenda buffer.
  11690. Prompts for a character to select a command. Any prefix arg will be passed
  11691. on to the selected command. The default selections are:
  11692. g
  11693. a Call `org-agenda-list' to display the agenda for current day or week.
  11694. t Call `org-todo-list' to display the global todo list.
  11695. T Call `org-todo-list' to display the global todo list, select only
  11696. entries with a specific TODO keyword (the user gets a prompt).
  11697. m Call `org-tags-view' to display headlines with tags matching
  11698. a condition (the user is prompted for the condition).
  11699. M Like `m', but select only TODO entries, no ordinary headlines.
  11700. l Create a timeeline for the current buffer.
  11701. More commands can be added by configuring the variable
  11702. `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
  11703. searches can be pre-defined in this way.
  11704. If the current buffer is in Org-mode and visiting a file, you can also
  11705. first press `1' to indicate that the agenda should be temporarily (until the
  11706. next use of \\[org-agenda]) restricted to the current file."
  11707. (interactive "P")
  11708. (catch 'exit
  11709. (let* ((buf (current-buffer))
  11710. (bfn (buffer-file-name (buffer-base-buffer)))
  11711. (restrict-ok (and bfn (org-mode-p)))
  11712. (custom org-agenda-custom-commands)
  11713. c entry key type match lprops)
  11714. ;; Turn off restriction
  11715. (put 'org-agenda-files 'org-restrict nil)
  11716. (setq org-agenda-restrict nil)
  11717. (move-marker org-agenda-restrict-begin nil)
  11718. (move-marker org-agenda-restrict-end nil)
  11719. ;; Remember where this call originated
  11720. (setq org-agenda-last-dispatch-buffer (current-buffer))
  11721. (save-window-excursion
  11722. (delete-other-windows)
  11723. (switch-to-buffer-other-window " *Agenda Commands*")
  11724. (erase-buffer)
  11725. (insert (eval-when-compile
  11726. (let ((header
  11727. "Press key for an agenda command:
  11728. -------------------------------- C Configure custom agenda commands
  11729. a Agenda for current week or day
  11730. t List of all TODO entries T Entries with special TODO kwd
  11731. m Match a TAGS query M Like m, but only TODO entries
  11732. L Timeline for current buffer # List stuck projects (!=configure)
  11733. ")
  11734. (start 0))
  11735. (while (string-match "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)" header start)
  11736. (setq start (match-end 0))
  11737. (add-text-properties (match-beginning 2) (match-end 2)
  11738. '(face bold) header))
  11739. header)))
  11740. (while (setq entry (pop custom))
  11741. (setq key (car entry) type (nth 1 entry) match (nth 2 entry))
  11742. (insert (format "\n%-4s%-14s: %s"
  11743. (org-add-props (copy-sequence key)
  11744. '(face bold))
  11745. (cond
  11746. ((stringp type) type)
  11747. ((eq type 'todo) "TODO keyword")
  11748. ((eq type 'tags) "Tags query")
  11749. ((eq type 'tags-todo) "Tags (TODO)")
  11750. ((eq type 'tags-tree) "Tags tree")
  11751. ((eq type 'todo-tree) "TODO kwd tree")
  11752. ((eq type 'occur-tree) "Occur tree")
  11753. ((functionp type) (symbol-name type))
  11754. (t "???"))
  11755. (if (stringp match)
  11756. (org-add-props match nil 'face 'org-warning)
  11757. (format "set of %d commands" (length match))))))
  11758. (if restrict-ok
  11759. (insert "\n"
  11760. (org-add-props "1 Restrict call to current buffer 0 Restrict call to region or subtree" nil 'face 'org-table)))
  11761. (goto-char (point-min))
  11762. (if (fboundp 'fit-window-to-buffer) (fit-window-to-buffer))
  11763. (message "Press key for agenda command%s"
  11764. (if restrict-ok ", or [1] or [0] to restrict" ""))
  11765. (setq c (read-char-exclusive))
  11766. (message "")
  11767. (when (memq c '(?L ?1 ?0))
  11768. (if restrict-ok
  11769. (put 'org-agenda-files 'org-restrict (list bfn))
  11770. (error "Cannot restrict agenda to current buffer"))
  11771. (with-current-buffer " *Agenda Commands*"
  11772. (goto-char (point-max))
  11773. (delete-region (point-at-bol) (point))
  11774. (goto-char (point-min)))
  11775. (when (eq c ?0)
  11776. (setq org-agenda-restrict t)
  11777. (with-current-buffer buf
  11778. (if (org-region-active-p)
  11779. (progn
  11780. (move-marker org-agenda-restrict-begin (region-beginning))
  11781. (move-marker org-agenda-restrict-end (region-end)))
  11782. (save-excursion
  11783. (org-back-to-heading t)
  11784. (move-marker org-agenda-restrict-begin (point))
  11785. (move-marker org-agenda-restrict-end
  11786. (progn (org-end-of-subtree t)))))))
  11787. (unless (eq c ?L)
  11788. (message "Press key for agenda command%s"
  11789. (if restrict-ok " (restricted to current file)" ""))
  11790. (setq c (read-char-exclusive)))
  11791. (message "")))
  11792. (require 'calendar) ; FIXME: can we avoid this for some commands?
  11793. ;; For example the todo list should not need it (but does...)
  11794. (cond
  11795. ((setq entry (assoc (char-to-string c) org-agenda-custom-commands))
  11796. (if (symbolp (nth 1 entry))
  11797. (progn
  11798. (setq type (nth 1 entry) match (nth 2 entry) lprops (nth 3 entry)
  11799. lprops (nth 3 entry))
  11800. (cond
  11801. ((eq type 'tags)
  11802. (org-let lprops '(org-tags-view current-prefix-arg match)))
  11803. ((eq type 'tags-todo)
  11804. (org-let lprops '(org-tags-view '(4) match)))
  11805. ((eq type 'todo)
  11806. (org-let lprops '(org-todo-list match)))
  11807. ((eq type 'tags-tree)
  11808. (org-check-for-org-mode)
  11809. (org-let lprops '(org-tags-sparse-tree current-prefix-arg match)))
  11810. ((eq type 'todo-tree)
  11811. (org-check-for-org-mode)
  11812. (org-let lprops
  11813. '(org-occur (concat "^" outline-regexp "[ \t]*"
  11814. (regexp-quote match) "\\>"))))
  11815. ((eq type 'occur-tree)
  11816. (org-check-for-org-mode)
  11817. (org-let lprops '(org-occur match)))
  11818. ((fboundp type)
  11819. (org-let lprops '(funcall type match)))
  11820. (t (error "Invalid custom agenda command type %s" type))))
  11821. (org-run-agenda-series (cddr entry))))
  11822. ((equal c ?C) (customize-variable 'org-agenda-custom-commands))
  11823. ((equal c ?a) (call-interactively 'org-agenda-list))
  11824. ((equal c ?t) (call-interactively 'org-todo-list))
  11825. ((equal c ?T) (org-call-with-arg 'org-todo-list (or arg '(4))))
  11826. ((equal c ?m) (call-interactively 'org-tags-view))
  11827. ((equal c ?M) (org-call-with-arg 'org-tags-view (or arg '(4))))
  11828. ((equal c ?L)
  11829. (unless restrict-ok
  11830. (error "This is not an Org-mode file"))
  11831. (org-call-with-arg 'org-timeline arg))
  11832. ((equal c ?#) (call-interactively 'org-agenda-list-stuck-projects))
  11833. ((equal c ?!) (customize-variable 'org-stuck-projects))
  11834. (t (error "Invalid key"))))))
  11835. (defun org-run-agenda-series (series)
  11836. (org-prepare-agenda)
  11837. (let* ((org-agenda-multi t)
  11838. (redo (list 'org-run-agenda-series (list 'quote series)))
  11839. (org-select-agenda-window t)
  11840. (cmds (car series))
  11841. (gprops (nth 1 series))
  11842. match ;; The byte compiler incorrectly complains about this. Keep it!
  11843. cmd type lprops)
  11844. (while (setq cmd (pop cmds))
  11845. (setq type (car cmd) match (nth 1 cmd) lprops (nth 2 cmd))
  11846. (cond
  11847. ((eq type 'agenda)
  11848. (call-interactively 'org-agenda-list))
  11849. ((eq type 'alltodo)
  11850. (call-interactively 'org-todo-list))
  11851. ((eq type 'stuck)
  11852. (call-interactively 'org-agenda-list-stuck-projects))
  11853. ((eq type 'tags)
  11854. (org-let2 gprops lprops
  11855. '(org-tags-view current-prefix-arg match)))
  11856. ((eq type 'tags-todo)
  11857. (org-let2 gprops lprops
  11858. '(org-tags-view '(4) match)))
  11859. ((eq type 'todo)
  11860. (org-let2 gprops lprops
  11861. '(org-todo-list match)))
  11862. ((fboundp type)
  11863. (org-let2 gprops lprops
  11864. '(funcall type match)))
  11865. (t (error "Invalid type in command series"))))
  11866. (widen)
  11867. (setq org-agenda-redo-command redo)
  11868. (goto-char (point-min)))
  11869. (org-finalize-agenda))
  11870. ;;;###autoload
  11871. (defmacro org-batch-agenda (cmd-key &rest parameters)
  11872. "Run an agenda command in batch mode, send result to STDOUT.
  11873. CMD-KEY is a string that is also a key in `org-agenda-custom-commands'.
  11874. Paramters are alternating variable names and values that will be bound
  11875. before running the agenda command."
  11876. (let (pars)
  11877. (while parameters
  11878. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  11879. (flet ((read-char-exclusive () (string-to-char cmd-key)))
  11880. (eval (list 'let (nreverse pars) '(org-agenda nil))))
  11881. (set-buffer "*Org Agenda*")
  11882. (princ (buffer-string))))
  11883. (defmacro org-no-read-only (&rest body)
  11884. "Inhibit read-only for BODY."
  11885. `(let ((inhibit-read-only t)) ,@body))
  11886. (defun org-check-for-org-mode ()
  11887. "Make sure current buffer is in org-mode. Error if not."
  11888. (or (org-mode-p)
  11889. (error "Cannot execute org-mode agenda command on buffer in %s."
  11890. major-mode)))
  11891. (defun org-fit-agenda-window ()
  11892. "Fit the window to the buffer size."
  11893. (and org-fit-agenda-window
  11894. (memq org-agenda-window-setup '(reorganize-frame))
  11895. (fboundp 'fit-window-to-buffer)
  11896. (fit-window-to-buffer nil (/ (* (frame-height) 3) 4)
  11897. (/ (frame-height) 2))))
  11898. ;;; Agenda file list
  11899. (defun org-agenda-files (&optional unrestricted)
  11900. "Get the list of agenda files.
  11901. Optional UNRESTRICTED means return the full list even if a restriction
  11902. is currently in place."
  11903. (cond
  11904. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  11905. ((stringp org-agenda-files) (org-read-agenda-file-list))
  11906. ((listp org-agenda-files) org-agenda-files)
  11907. (t (error "Invalid value of `org-agenda-files'"))))
  11908. (defun org-edit-agenda-file-list ()
  11909. "Edit the list of agenda files.
  11910. Depending on setup, this either uses customize to edit the variable
  11911. `org-agenda-files', or it visits the file that is holding the list. In the
  11912. latter case, the buffer is set up in a way that saving it automatically kills
  11913. the buffer and restores the previous window configuration."
  11914. (interactive)
  11915. (if (stringp org-agenda-files)
  11916. (let ((cw (current-window-configuration)))
  11917. (find-file org-agenda-files)
  11918. (org-set-local 'org-window-configuration cw)
  11919. (org-add-hook 'after-save-hook
  11920. (lambda ()
  11921. (set-window-configuration
  11922. (prog1 org-window-configuration
  11923. (kill-buffer (current-buffer))))
  11924. (org-install-agenda-files-menu)
  11925. (message "New agenda file list installed"))
  11926. nil 'local)
  11927. (message (substitute-command-keys
  11928. "Edit list and finish with \\[save-buffer]")))
  11929. (customize-variable 'org-agenda-files)))
  11930. (defun org-store-new-agenda-file-list (list)
  11931. "Set new value for the agenda file list and save it correcly."
  11932. (if (stringp org-agenda-files)
  11933. (let ((f org-agenda-files) b)
  11934. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  11935. (with-temp-file f
  11936. (insert (mapconcat 'identity list "\n") "\n")))
  11937. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  11938. (setq org-agenda-files list)
  11939. (customize-save-variable 'org-agenda-files org-agenda-files))))
  11940. (defun org-read-agenda-file-list ()
  11941. "Read the list of agenda files from a file."
  11942. (when (stringp org-agenda-files)
  11943. (with-temp-buffer
  11944. (insert-file-contents org-agenda-files)
  11945. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  11946. ;;;###autoload
  11947. (defun org-cycle-agenda-files ()
  11948. "Cycle through the files in `org-agenda-files'.
  11949. If the current buffer visits an agenda file, find the next one in the list.
  11950. If the current buffer does not, find the first agenda file."
  11951. (interactive)
  11952. (let* ((fs (org-agenda-files t))
  11953. (files (append fs (list (car fs))))
  11954. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  11955. file)
  11956. (unless files (error "No agenda files"))
  11957. (catch 'exit
  11958. (while (setq file (pop files))
  11959. (if (equal (file-truename file) tcf)
  11960. (when (car files)
  11961. (find-file (car files))
  11962. (throw 'exit t))))
  11963. (find-file (car fs)))))
  11964. (defun org-agenda-file-to-end ()
  11965. "Move/add the current file to the end of the agenda file list.
  11966. If the file is not present in the list, it is appended to the list. If it is
  11967. present, it is moved there."
  11968. (interactive)
  11969. (org-agenda-file-to-front 'to-end))
  11970. (defun org-agenda-file-to-front (&optional to-end)
  11971. "Move/add the current file to the top of the agenda file list.
  11972. If the file is not present in the list, it is added to the front. If it is
  11973. present, it is moved there. With optional argument TO-END, add/move to the
  11974. end of the list."
  11975. (interactive "P")
  11976. (let ((file-alist (mapcar (lambda (x)
  11977. (cons (file-truename x) x))
  11978. (org-agenda-files t)))
  11979. (ctf (file-truename buffer-file-name))
  11980. x had)
  11981. (setq x (assoc ctf file-alist) had x)
  11982. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  11983. (if to-end
  11984. (setq file-alist (append (delq x file-alist) (list x)))
  11985. (setq file-alist (cons x (delq x file-alist))))
  11986. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  11987. (org-install-agenda-files-menu)
  11988. (message "File %s to %s of agenda file list"
  11989. (if had "moved" "added") (if to-end "end" "front"))))
  11990. (defun org-remove-file (&optional file)
  11991. "Remove current file from the list of files in variable `org-agenda-files'.
  11992. These are the files which are being checked for agenda entries.
  11993. Optional argument FILE means, use this file instead of the current."
  11994. (interactive)
  11995. (let* ((file (or file buffer-file-name))
  11996. (true-file (file-truename file))
  11997. (afile (abbreviate-file-name file))
  11998. (files (delq nil (mapcar
  11999. (lambda (x)
  12000. (if (equal true-file
  12001. (file-truename x))
  12002. nil x))
  12003. (org-agenda-files t)))))
  12004. (if (not (= (length files) (length (org-agenda-files t))))
  12005. (progn
  12006. (org-store-new-agenda-file-list files)
  12007. (org-install-agenda-files-menu)
  12008. (message "Removed file: %s" afile))
  12009. (message "File was not in list: %s" afile))))
  12010. (defun org-file-menu-entry (file)
  12011. (vector file (list 'find-file file) t))
  12012. (defun org-check-agenda-file (file)
  12013. "Make sure FILE exists. If not, ask user what to do."
  12014. (when (not (file-exists-p file))
  12015. (message "non-existent file %s. [R]emove from list or [A]bort?"
  12016. (abbreviate-file-name file))
  12017. (let ((r (downcase (read-char-exclusive))))
  12018. (cond
  12019. ((equal r ?r)
  12020. (org-remove-file file)
  12021. (throw 'nextfile t))
  12022. (t (error "Abort"))))))
  12023. ;;; Agenda prepare and finalize
  12024. (defvar org-agenda-multi nil) ; dynammically scoped
  12025. (defvar org-agenda-buffer-name "*Org Agenda*")
  12026. (defvar org-pre-agenda-window-conf nil)
  12027. (defun org-prepare-agenda ()
  12028. (if org-agenda-multi
  12029. (progn
  12030. (setq buffer-read-only nil)
  12031. (goto-char (point-max))
  12032. (unless (= (point) 1)
  12033. (insert "\n" (make-string (window-width) ?=) "\n"))
  12034. (narrow-to-region (point) (point-max)))
  12035. (org-agenda-maybe-reset-markers 'force)
  12036. (org-prepare-agenda-buffers (org-agenda-files))
  12037. (let* ((abuf (get-buffer-create org-agenda-buffer-name))
  12038. (awin (get-buffer-window abuf)))
  12039. (cond
  12040. ((equal (current-buffer) abuf) nil)
  12041. (awin (select-window awin))
  12042. ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
  12043. ((equal org-agenda-window-setup 'current-window)
  12044. (switch-to-buffer abuf))
  12045. ((equal org-agenda-window-setup 'other-window)
  12046. (switch-to-buffer-other-window abuf))
  12047. ((equal org-agenda-window-setup 'other-frame)
  12048. (switch-to-buffer-other-frame abuf))
  12049. ((equal org-agenda-window-setup 'reorganize-frame)
  12050. (delete-other-windows)
  12051. (switch-to-buffer-other-window abuf))))
  12052. (setq buffer-read-only nil)
  12053. (erase-buffer)
  12054. (org-agenda-mode))
  12055. (setq buffer-read-only nil))
  12056. (defun org-finalize-agenda ()
  12057. "Finishing touch for the agenda buffer, called just before displaying it."
  12058. (unless org-agenda-multi
  12059. (org-agenda-align-tags)
  12060. (save-excursion
  12061. (let ((buffer-read-only))
  12062. (goto-char (point-min))
  12063. (while (org-activate-bracket-links (point-max))
  12064. (add-text-properties (match-beginning 0) (match-end 0)
  12065. '(face org-link))))
  12066. (run-hooks 'org-finalize-agenda-hook))))
  12067. (defun org-prepare-agenda-buffers (files)
  12068. "Create buffers for all agenda files, protect archived trees and comments."
  12069. (interactive)
  12070. (let ((pa '(:org-archived t))
  12071. (pc '(:org-comment t))
  12072. (pall '(:org-archived t :org-comment t))
  12073. (rea (concat ":" org-archive-tag ":"))
  12074. bmp file re)
  12075. (save-excursion
  12076. (save-restriction
  12077. (while (setq file (pop files))
  12078. (org-check-agenda-file file)
  12079. (set-buffer (org-get-agenda-file-buffer file))
  12080. (widen)
  12081. (setq bmp (buffer-modified-p))
  12082. (save-excursion
  12083. (remove-text-properties (point-min) (point-max) pall)
  12084. (when org-agenda-skip-archived-trees
  12085. (goto-char (point-min))
  12086. (while (re-search-forward rea nil t)
  12087. (if (org-on-heading-p)
  12088. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  12089. (goto-char (point-min))
  12090. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  12091. (while (re-search-forward re nil t)
  12092. (add-text-properties
  12093. (match-beginning 0) (org-end-of-subtree t) pc)))
  12094. (set-buffer-modified-p bmp))))))
  12095. (defvar org-agenda-skip-function nil
  12096. "Function to be called at each match during agenda construction.
  12097. If this function return nil, the current match should not be skipped.
  12098. Otherwise, the function must return a position from where the search
  12099. should be continued.
  12100. Never set this variable using `setq' or so, because then it will apply
  12101. to all future agenda commands. Instead, bind it with `let' to scope
  12102. it dynamically into the agenda-constructing command.")
  12103. (defun org-agenda-skip ()
  12104. "Throw to `:skip' in places that should be skipped.
  12105. Also moves point to the end of the skipped region, so that search can
  12106. continue from there."
  12107. (let ((p (point-at-bol)) to)
  12108. (and org-agenda-skip-archived-trees
  12109. (get-text-property p :org-archived)
  12110. (org-end-of-subtree t)
  12111. (throw :skip t))
  12112. (and (get-text-property p :org-comment)
  12113. (org-end-of-subtree t)
  12114. (throw :skip t))
  12115. (if (equal (char-after p) ?#) (throw :skip t))
  12116. (when (and (functionp org-agenda-skip-function)
  12117. (setq to (save-excursion
  12118. (save-match-data
  12119. (funcall org-agenda-skip-function)))))
  12120. (goto-char to)
  12121. (throw :skip t))))
  12122. (defvar org-agenda-markers nil
  12123. "List of all currently active markers created by `org-agenda'.")
  12124. (defvar org-agenda-last-marker-time (time-to-seconds (current-time))
  12125. "Creation time of the last agenda marker.")
  12126. (defun org-agenda-new-marker (&optional pos)
  12127. "Return a new agenda marker.
  12128. Org-mode keeps a list of these markers and resets them when they are
  12129. no longer in use."
  12130. (let ((m (copy-marker (or pos (point)))))
  12131. (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
  12132. (push m org-agenda-markers)
  12133. m))
  12134. (defun org-agenda-maybe-reset-markers (&optional force)
  12135. "Reset markers created by `org-agenda'. But only if they are old enough."
  12136. (if (or (and force (not org-agenda-multi))
  12137. (> (- (time-to-seconds (current-time))
  12138. org-agenda-last-marker-time)
  12139. 5))
  12140. (while org-agenda-markers
  12141. (move-marker (pop org-agenda-markers) nil))))
  12142. (defvar org-agenda-new-buffers nil
  12143. "Buffers created to visit agenda files.")
  12144. (defun org-get-agenda-file-buffer (file)
  12145. "Get a buffer visiting FILE. If the buffer needs to be created, add
  12146. it to the list of buffers which might be released later."
  12147. (let ((buf (find-buffer-visiting file)))
  12148. (if buf
  12149. buf ; just return it
  12150. ;; Make a new buffer and remember it
  12151. (setq buf (find-file-noselect file))
  12152. (if buf (push buf org-agenda-new-buffers))
  12153. buf)))
  12154. (defun org-release-buffers (blist)
  12155. "Release all buffers in list, asking the user for confirmation when needed.
  12156. When a buffer is unmodified, it is just killed. When modified, it is saved
  12157. \(if the user agrees) and then killed."
  12158. (let (buf file)
  12159. (while (setq buf (pop blist))
  12160. (setq file (buffer-file-name buf))
  12161. (when (and (buffer-modified-p buf)
  12162. file
  12163. (y-or-n-p (format "Save file %s? " file)))
  12164. (with-current-buffer buf (save-buffer)))
  12165. (kill-buffer buf))))
  12166. (defvar org-category-table nil)
  12167. (defun org-get-category-table ()
  12168. "Get the table of categories and positions in current buffer."
  12169. (let (tbl)
  12170. (save-excursion
  12171. (goto-char (point-min))
  12172. (while (re-search-forward "\\(^\\|\r\\)#\\+CATEGORY:[ \t]*\\(.*\\)" nil t)
  12173. (push (cons (point) (org-trim (match-string 2))) tbl)))
  12174. tbl))
  12175. (defun org-get-category (&optional pos)
  12176. "Get the category applying to position POS."
  12177. (if (not org-category-table)
  12178. (cond
  12179. ((null org-category)
  12180. (setq org-category
  12181. (if buffer-file-name
  12182. (file-name-sans-extension
  12183. (file-name-nondirectory buffer-file-name))
  12184. "???")))
  12185. ((symbolp org-category) (symbol-name org-category))
  12186. (t org-category))
  12187. (let ((tbl org-category-table)
  12188. (pos (or pos (point))))
  12189. (while (and tbl (> (caar tbl) pos))
  12190. (pop tbl))
  12191. (or (cdar tbl) (cdr (nth (1- (length org-category-table))
  12192. org-category-table))))))
  12193. ;;; Agenda timeline
  12194. (defun org-timeline (&optional include-all)
  12195. "Show a time-sorted view of the entries in the current org file.
  12196. Only entries with a time stamp of today or later will be listed. With
  12197. \\[universal-argument] prefix, all unfinished TODO items will also be shown,
  12198. under the current date.
  12199. If the buffer contains an active region, only check the region for
  12200. dates."
  12201. (interactive "P")
  12202. (require 'calendar)
  12203. (org-compile-prefix-format 'timeline)
  12204. (org-set-sorting-strategy 'timeline)
  12205. (let* ((dopast t)
  12206. (dotodo include-all)
  12207. (doclosed org-agenda-show-log)
  12208. (entry buffer-file-name)
  12209. (date (calendar-current-date))
  12210. (win (selected-window))
  12211. (pos1 (point))
  12212. (beg (if (org-region-active-p) (region-beginning) (point-min)))
  12213. (end (if (org-region-active-p) (region-end) (point-max)))
  12214. (day-numbers (org-get-all-dates beg end 'no-ranges
  12215. t doclosed ; always include today
  12216. org-timeline-show-empty-dates))
  12217. (today (time-to-days (current-time)))
  12218. (past t)
  12219. args
  12220. s e rtn d emptyp)
  12221. (setq org-agenda-redo-command
  12222. (list 'progn
  12223. (list 'switch-to-buffer-other-window (current-buffer))
  12224. (list 'org-timeline (list 'quote include-all))))
  12225. (if (not dopast)
  12226. ;; Remove past dates from the list of dates.
  12227. (setq day-numbers (delq nil (mapcar (lambda(x)
  12228. (if (>= x today) x nil))
  12229. day-numbers))))
  12230. (org-prepare-agenda)
  12231. (if doclosed (push :closed args))
  12232. (push :timestamp args)
  12233. (if dotodo (push :todo args))
  12234. (while (setq d (pop day-numbers))
  12235. (if (and (listp d) (eq (car d) :omitted))
  12236. (progn
  12237. (setq s (point))
  12238. (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
  12239. (put-text-property s (1- (point)) 'face 'org-level-3))
  12240. (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
  12241. (if (and (>= d today)
  12242. dopast
  12243. past)
  12244. (progn
  12245. (setq past nil)
  12246. (insert (make-string 79 ?-) "\n")))
  12247. (setq date (calendar-gregorian-from-absolute d))
  12248. (setq s (point))
  12249. (setq rtn (and (not emptyp)
  12250. (apply 'org-agenda-get-day-entries
  12251. entry date args)))
  12252. (if (or rtn (equal d today) org-timeline-show-empty-dates)
  12253. (progn
  12254. (insert (calendar-day-name date) " "
  12255. (number-to-string (extract-calendar-day date)) " "
  12256. (calendar-month-name (extract-calendar-month date)) " "
  12257. (number-to-string (extract-calendar-year date)) "\n")
  12258. ; FIXME: this gives a timezone problem
  12259. ; (insert (format-time-string org-agenda-date-format
  12260. ; (calendar-time-from-absolute d 0))
  12261. ; "\n")
  12262. (put-text-property s (1- (point)) 'face 'org-level-3)
  12263. (put-text-property s (1- (point)) 'org-date-line t)
  12264. (if (equal d today)
  12265. (put-text-property s (1- (point)) 'org-today t))
  12266. (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
  12267. (put-text-property s (1- (point)) 'day d)))))
  12268. (goto-char (point-min))
  12269. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  12270. (point-min)))
  12271. (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
  12272. (org-finalize-agenda)
  12273. (setq buffer-read-only t)
  12274. (when (not org-select-agenda-window)
  12275. (select-window win)
  12276. (goto-char pos1))))
  12277. (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty)
  12278. "Return a list of all relevant day numbers from BEG to END buffer positions.
  12279. If NO-RANGES is non-nil, include only the start and end dates of a range,
  12280. not every single day in the range. If FORCE-TODAY is non-nil, make
  12281. sure that TODAY is included in the list. If INACTIVE is non-nil, also
  12282. inactive time stamps (those in square brackets) are included.
  12283. When EMPTY is non-nil, also include days without any entries."
  12284. (let ((re (if inactive org-ts-regexp-both org-ts-regexp))
  12285. dates dates1 date day day1 day2 ts1 ts2)
  12286. (if force-today
  12287. (setq dates (list (time-to-days (current-time)))))
  12288. (save-excursion
  12289. (goto-char beg)
  12290. (while (re-search-forward re end t)
  12291. (setq day (time-to-days (org-time-string-to-time
  12292. (substring (match-string 1) 0 10))))
  12293. (or (memq day dates) (push day dates)))
  12294. (unless no-ranges
  12295. (goto-char beg)
  12296. (while (re-search-forward org-tr-regexp end t)
  12297. (setq ts1 (substring (match-string 1) 0 10)
  12298. ts2 (substring (match-string 2) 0 10)
  12299. day1 (time-to-days (org-time-string-to-time ts1))
  12300. day2 (time-to-days (org-time-string-to-time ts2)))
  12301. (while (< (setq day1 (1+ day1)) day2)
  12302. (or (memq day1 dates) (push day1 dates)))))
  12303. (setq dates (sort dates '<))
  12304. (when empty
  12305. (while (setq day (pop dates))
  12306. (setq day2 (car dates))
  12307. (push day dates1)
  12308. (when (and day2 empty)
  12309. (if (or (eq empty t)
  12310. (and (numberp empty) (<= (- day2 day) empty)))
  12311. (while (< (setq day (1+ day)) day2)
  12312. (push (list day) dates1))
  12313. (push (cons :omitted (- day2 day)) dates1))))
  12314. (setq dates (nreverse dates1)))
  12315. dates)))
  12316. ;;; Agenda Daily/Weekly
  12317. (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
  12318. (defvar org-agenda-last-arguments nil
  12319. "The arguments of the previous call to org-agenda")
  12320. (defvar org-starting-day nil) ; local variable in the agenda buffer
  12321. (defvar org-include-all-loc nil) ; local variable
  12322. ;;;###autoload
  12323. (defun org-agenda-list (&optional include-all start-day ndays)
  12324. "Produce a weekly view from all files in variable `org-agenda-files'.
  12325. The view will be for the current week, but from the overview buffer you
  12326. will be able to go to other weeks.
  12327. With one \\[universal-argument] prefix argument INCLUDE-ALL, all unfinished TODO items will
  12328. also be shown, under the current date.
  12329. With two \\[universal-argument] prefix argument INCLUDE-ALL, all TODO entries marked DONE
  12330. on the days are also shown. See the variable `org-log-done' for how
  12331. to turn on logging.
  12332. START-DAY defaults to TODAY, or to the most recent match for the weekday
  12333. given in `org-agenda-start-on-weekday'.
  12334. NDAYS defaults to `org-agenda-ndays'."
  12335. (interactive "P")
  12336. (if org-agenda-overriding-arguments
  12337. (setq include-all (car org-agenda-overriding-arguments)
  12338. start-day (nth 1 org-agenda-overriding-arguments)
  12339. ndays (nth 2 org-agenda-overriding-arguments)))
  12340. (setq org-agenda-last-arguments (list include-all start-day ndays))
  12341. (org-compile-prefix-format 'agenda)
  12342. (org-set-sorting-strategy 'agenda)
  12343. (require 'calendar)
  12344. (let* ((org-agenda-start-on-weekday
  12345. (if (or (equal ndays 1)
  12346. (and (null ndays) (equal 1 org-agenda-ndays)))
  12347. nil org-agenda-start-on-weekday))
  12348. (thefiles (org-agenda-files))
  12349. (files thefiles)
  12350. (win (selected-window))
  12351. (today (time-to-days (current-time)))
  12352. (sd (or start-day today))
  12353. (start (if (or (null org-agenda-start-on-weekday)
  12354. (< org-agenda-ndays 7))
  12355. sd
  12356. (let* ((nt (calendar-day-of-week
  12357. (calendar-gregorian-from-absolute sd)))
  12358. (n1 org-agenda-start-on-weekday)
  12359. (d (- nt n1)))
  12360. (- sd (+ (if (< d 0) 7 0) d)))))
  12361. (day-numbers (list start))
  12362. (inhibit-redisplay (not debug-on-error))
  12363. s e rtn rtnall file date d start-pos end-pos todayp nd)
  12364. (setq org-agenda-redo-command
  12365. (list 'org-agenda-list (list 'quote include-all) start-day ndays))
  12366. ;; Make the list of days
  12367. (setq ndays (or ndays org-agenda-ndays)
  12368. nd ndays)
  12369. (while (> ndays 1)
  12370. (push (1+ (car day-numbers)) day-numbers)
  12371. (setq ndays (1- ndays)))
  12372. (setq day-numbers (nreverse day-numbers))
  12373. (org-prepare-agenda)
  12374. (org-set-local 'org-starting-day (car day-numbers))
  12375. (org-set-local 'org-include-all-loc include-all)
  12376. (when (and (or include-all org-agenda-include-all-todo)
  12377. (member today day-numbers))
  12378. (setq files thefiles
  12379. rtnall nil)
  12380. (while (setq file (pop files))
  12381. (catch 'nextfile
  12382. (org-check-agenda-file file)
  12383. (setq date (calendar-gregorian-from-absolute today)
  12384. rtn (org-agenda-get-day-entries
  12385. file date :todo))
  12386. (setq rtnall (append rtnall rtn))))
  12387. (when rtnall
  12388. (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
  12389. (add-text-properties (point-min) (1- (point))
  12390. (list 'face 'org-level-3))
  12391. (insert (org-finalize-agenda-entries rtnall) "\n")))
  12392. (setq s (point))
  12393. (insert (if (= nd 7) "Week-" "Day-") "agenda:\n")
  12394. (add-text-properties s (1- (point)) (list 'face 'org-level-3))
  12395. (while (setq d (pop day-numbers))
  12396. (setq date (calendar-gregorian-from-absolute d)
  12397. s (point))
  12398. (if (or (setq todayp (= d today))
  12399. (and (not start-pos) (= d sd)))
  12400. (setq start-pos (point))
  12401. (if (and start-pos (not end-pos))
  12402. (setq end-pos (point))))
  12403. (setq files thefiles
  12404. rtnall nil)
  12405. (while (setq file (pop files))
  12406. (catch 'nextfile
  12407. (org-check-agenda-file file)
  12408. (if org-agenda-show-log
  12409. (setq rtn (org-agenda-get-day-entries
  12410. file date
  12411. :deadline :scheduled :timestamp :closed))
  12412. (setq rtn (org-agenda-get-day-entries
  12413. file date
  12414. :deadline :scheduled :timestamp)))
  12415. (setq rtnall (append rtnall rtn))))
  12416. (if org-agenda-include-diary
  12417. (progn
  12418. (require 'diary-lib)
  12419. (setq rtn (org-get-entries-from-diary date))
  12420. (setq rtnall (append rtnall rtn))))
  12421. (if (or rtnall org-agenda-show-all-dates)
  12422. (progn
  12423. (insert (format "%-9s %2d %s %4d\n"
  12424. (calendar-day-name date)
  12425. (extract-calendar-day date)
  12426. (calendar-month-name (extract-calendar-month date))
  12427. (extract-calendar-year date)))
  12428. ; FIXME: this gives a timezone problem
  12429. ; (insert (format-time-string org-agenda-date-format
  12430. ; (calendar-time-from-absolute d 0)) "\n")
  12431. (put-text-property s (1- (point)) 'face 'org-level-3)
  12432. (put-text-property s (1- (point)) 'org-date-line t)
  12433. (if todayp (put-text-property s (1- (point)) 'org-today t))
  12434. (if rtnall (insert
  12435. (org-finalize-agenda-entries
  12436. (org-agenda-add-time-grid-maybe
  12437. rtnall nd todayp))
  12438. "\n"))
  12439. (put-text-property s (1- (point)) 'day d))))
  12440. (goto-char (point-min))
  12441. (org-fit-agenda-window)
  12442. (unless (and (pos-visible-in-window-p (point-min))
  12443. (pos-visible-in-window-p (point-max)))
  12444. (goto-char (1- (point-max)))
  12445. (recenter -1)
  12446. (if (not (pos-visible-in-window-p (or start-pos 1)))
  12447. (progn
  12448. (goto-char (or start-pos 1))
  12449. (recenter 1))))
  12450. (goto-char (or start-pos 1))
  12451. (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
  12452. (org-finalize-agenda)
  12453. (setq buffer-read-only t)
  12454. (if (not org-select-agenda-window) (select-window win))
  12455. (message "")))
  12456. ;;; Agenda TODO list
  12457. (defvar org-select-this-todo-keyword nil)
  12458. (defvar org-last-arg nil)
  12459. ;;;###autoload
  12460. (defun org-todo-list (arg)
  12461. "Show all TODO entries from all agenda file in a single list.
  12462. The prefix arg can be used to select a specific TODO keyword and limit
  12463. the list to these. When using \\[universal-argument], you will be prompted
  12464. for a keyword. A numeric prefix directly selects the Nth keyword in
  12465. `org-todo-keywords'."
  12466. (interactive "P")
  12467. (require 'calendar)
  12468. (org-compile-prefix-format 'todo)
  12469. (org-set-sorting-strategy 'todo)
  12470. (let* ((today (time-to-days (current-time)))
  12471. (date (calendar-gregorian-from-absolute today))
  12472. (win (selected-window))
  12473. (kwds org-todo-keywords)
  12474. (completion-ignore-case t)
  12475. (org-select-this-todo-keyword
  12476. (if (stringp arg) arg
  12477. (and arg (integerp arg) (> arg 0)
  12478. (nth (1- arg) org-todo-keywords))))
  12479. rtn rtnall files file pos)
  12480. (when (equal arg '(4))
  12481. (setq org-select-this-todo-keyword
  12482. (completing-read "Keyword: " (mapcar 'list org-todo-keywords)
  12483. nil t)))
  12484. (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
  12485. (org-prepare-agenda)
  12486. (org-set-local 'org-last-arg arg)
  12487. (org-set-local 'org-todo-keywords kwds)
  12488. (setq org-agenda-redo-command
  12489. '(org-todo-list (or current-prefix-arg org-last-arg)))
  12490. (setq files (org-agenda-files)
  12491. rtnall nil)
  12492. (while (setq file (pop files))
  12493. (catch 'nextfile
  12494. (org-check-agenda-file file)
  12495. (setq rtn (org-agenda-get-day-entries file date :todo))
  12496. (setq rtnall (append rtnall rtn))))
  12497. (if org-agenda-overriding-header
  12498. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  12499. nil 'face 'org-level-3) "\n")
  12500. (insert "Global list of TODO items of type: ")
  12501. (add-text-properties (point-min) (1- (point))
  12502. (list 'face 'org-level-3))
  12503. (setq pos (point))
  12504. (insert (or org-select-this-todo-keyword "ALL") "\n")
  12505. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  12506. (setq pos (point))
  12507. (unless org-agenda-multi
  12508. (insert
  12509. "Available with `N r': (0)ALL "
  12510. (let ((n 0))
  12511. (mapconcat (lambda (x)
  12512. (format "(%d)%s" (setq n (1+ n)) x))
  12513. org-todo-keywords " "))
  12514. "\n"))
  12515. (add-text-properties pos (1- (point)) (list 'face 'org-level-3)))
  12516. (when rtnall
  12517. (insert (org-finalize-agenda-entries rtnall) "\n"))
  12518. (goto-char (point-min))
  12519. (org-fit-agenda-window)
  12520. (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
  12521. (org-finalize-agenda)
  12522. (setq buffer-read-only t)
  12523. (if (not org-select-agenda-window) (select-window win))))
  12524. ;;; Agenda tags match
  12525. ;;;###autoload
  12526. (defun org-tags-view (&optional todo-only match)
  12527. "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
  12528. The prefix arg TODO-ONLY limits the search to TODO entries."
  12529. (interactive "P")
  12530. (org-compile-prefix-format 'tags)
  12531. (org-set-sorting-strategy 'tags)
  12532. (let* ((org-tags-match-list-sublevels
  12533. (if todo-only t org-tags-match-list-sublevels))
  12534. (win (selected-window))
  12535. (completion-ignore-case t)
  12536. rtn rtnall files file pos matcher
  12537. buffer)
  12538. (setq matcher (org-make-tags-matcher match)
  12539. match (car matcher) matcher (cdr matcher))
  12540. (org-prepare-agenda)
  12541. (setq org-agenda-redo-command
  12542. (list 'org-tags-view (list 'quote todo-only)
  12543. (list 'if 'current-prefix-arg nil match)))
  12544. (setq files (org-agenda-files)
  12545. rtnall nil)
  12546. (while (setq file (pop files))
  12547. (catch 'nextfile
  12548. (org-check-agenda-file file)
  12549. (setq buffer (if (file-exists-p file)
  12550. (org-get-agenda-file-buffer file)
  12551. (error "No such file %s" file)))
  12552. (if (not buffer)
  12553. ;; If file does not exist, merror message to agenda
  12554. (setq rtn (list
  12555. (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  12556. rtnall (append rtnall rtn))
  12557. (with-current-buffer buffer
  12558. (unless (org-mode-p)
  12559. (error "Agenda file %s is not in `org-mode'" file))
  12560. (setq org-category-table (org-get-category-table))
  12561. (save-excursion
  12562. (save-restriction
  12563. (if org-agenda-restrict
  12564. (narrow-to-region org-agenda-restrict-begin
  12565. org-agenda-restrict-end)
  12566. (widen))
  12567. (setq rtn (org-scan-tags 'agenda matcher todo-only))
  12568. (setq rtnall (append rtnall rtn))))))))
  12569. (if org-agenda-overriding-header
  12570. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  12571. nil 'face 'org-level-3) "\n")
  12572. (insert "Headlines with TAGS match: ")
  12573. (add-text-properties (point-min) (1- (point))
  12574. (list 'face 'org-level-3))
  12575. (setq pos (point))
  12576. (insert match "\n")
  12577. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  12578. (setq pos (point))
  12579. (unless org-agenda-multi
  12580. (insert "Press `C-u r' to search again with new search string\n"))
  12581. (add-text-properties pos (1- (point)) (list 'face 'org-level-3)))
  12582. (when rtnall
  12583. (insert (org-finalize-agenda-entries rtnall) "\n"))
  12584. (goto-char (point-min))
  12585. (org-fit-agenda-window)
  12586. (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
  12587. (org-finalize-agenda)
  12588. (setq buffer-read-only t)
  12589. (if (not org-select-agenda-window) (select-window win))))
  12590. ;;; Agenda Finding stuck projects
  12591. (defvar org-agenda-skip-regexp nil
  12592. "Regular expression used in skipping subtrees for the agenda.
  12593. This is basically a temporary global variable that can be set and then
  12594. used by user-defined selections using `org-agenda-skip-function'.")
  12595. (defvar org-agenda-overriding-header nil
  12596. "When this is set during todo and tags searches, will replace header.")
  12597. (defun org-agenda-skip-subtree-when-regexp-matches ()
  12598. "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
  12599. If yes, it returns the end position of this tree, causing agenda commands
  12600. to skip this subtree. This is a function that can be put into
  12601. `org-agenda-skip-function' for the duration of a command."
  12602. (save-match-data
  12603. (let ((end (save-excursion (org-end-of-subtree t)))
  12604. skip)
  12605. (save-excursion
  12606. (setq skip (re-search-forward org-agenda-skip-regexp end t)))
  12607. (and skip end))))
  12608. (defun org-agenda-list-stuck-projects (&rest ignore)
  12609. "Create agenda view for projects that are stuck.
  12610. Stuck projects are project that have no next actions. For the definitions
  12611. of what a project is and how to check if it stuck, customize the variable
  12612. `org-stuck-projects'.
  12613. MATCH is being ignored."
  12614. (interactive)
  12615. (let* ((org-agenda-skip-function 'org-agenda-skip-subtree-when-regexp-matches)
  12616. (org-agenda-overriding-header "List of stuck projects: ")
  12617. (matcher (nth 0 org-stuck-projects))
  12618. (todo (nth 1 org-stuck-projects))
  12619. (tags (nth 2 org-stuck-projects))
  12620. (todo-re (concat "^\\*+[ \t]+\\("
  12621. (mapconcat 'identity todo "\\|")
  12622. "\\)\\>"))
  12623. (tags-re (concat "^\\*+.*:\\("
  12624. (mapconcat 'identity tags "\\|")
  12625. "\\):[a-zA-Z0-9_@:]*[ \t]*$")))
  12626. (setq org-agenda-skip-regexp
  12627. (cond
  12628. ((and todo tags)
  12629. (concat todo-re "\\|" tags-re))
  12630. (todo todo-re)
  12631. (tags tags-re)
  12632. (t (error "No information how to identify unstuck projects"))))
  12633. (org-tags-view nil matcher)
  12634. (with-current-buffer org-agenda-buffer-name
  12635. (setq org-agenda-redo-command
  12636. '(org-agenda-list-stuck-projects
  12637. (or current-prefix-arg org-last-arg))))))
  12638. ;;; Diary integration
  12639. (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
  12640. (defun org-get-entries-from-diary (date)
  12641. "Get the (Emacs Calendar) diary entries for DATE."
  12642. (let* ((fancy-diary-buffer "*temporary-fancy-diary-buffer*")
  12643. (diary-display-hook '(fancy-diary-display))
  12644. (list-diary-entries-hook
  12645. (cons 'org-diary-default-entry list-diary-entries-hook))
  12646. (diary-file-name-prefix-function nil) ; turn this feature off
  12647. (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
  12648. entries
  12649. (org-disable-agenda-to-diary t))
  12650. (save-excursion
  12651. (save-window-excursion
  12652. (list-diary-entries date 1))) ;; Keep this name for now, compatibility
  12653. (if (not (get-buffer fancy-diary-buffer))
  12654. (setq entries nil)
  12655. (with-current-buffer fancy-diary-buffer
  12656. (setq buffer-read-only nil)
  12657. (if (= (point-max) 1)
  12658. ;; No entries
  12659. (setq entries nil)
  12660. ;; Omit the date and other unnecessary stuff
  12661. (org-agenda-cleanup-fancy-diary)
  12662. ;; Add prefix to each line and extend the text properties
  12663. (if (= (point-max) 1)
  12664. (setq entries nil)
  12665. (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
  12666. (set-buffer-modified-p nil)
  12667. (kill-buffer fancy-diary-buffer)))
  12668. (when entries
  12669. (setq entries (org-split-string entries "\n"))
  12670. (setq entries
  12671. (mapcar
  12672. (lambda (x)
  12673. (setq x (org-format-agenda-item "" x "Diary" nil 'time))
  12674. ;; Extend the text properties to the beginning of the line
  12675. (org-add-props x (text-properties-at (1- (length x)) x)))
  12676. entries)))))
  12677. (defun org-agenda-cleanup-fancy-diary ()
  12678. "Remove unwanted stuff in buffer created by `fancy-diary-display'.
  12679. This gets rid of the date, the underline under the date, and
  12680. the dummy entry installed by `org-mode' to ensure non-empty diary for each
  12681. date. It also removes lines that contain only whitespace."
  12682. (goto-char (point-min))
  12683. (if (looking-at ".*?:[ \t]*")
  12684. (progn
  12685. (replace-match "")
  12686. (re-search-forward "\n=+$" nil t)
  12687. (replace-match "")
  12688. (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
  12689. (re-search-forward "\n=+$" nil t)
  12690. (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
  12691. (goto-char (point-min))
  12692. (while (re-search-forward "^ +\n" nil t)
  12693. (replace-match ""))
  12694. (goto-char (point-min))
  12695. (if (re-search-forward "^Org-mode dummy\n?" nil t)
  12696. (replace-match "")))
  12697. ;; Make sure entries from the diary have the right text properties.
  12698. (eval-after-load "diary-lib"
  12699. '(if (boundp 'diary-modify-entry-list-string-function)
  12700. ;; We can rely on the hook, nothing to do
  12701. nil
  12702. ;; Hook not avaiable, must use advice to make this work
  12703. (defadvice add-to-diary-list (before org-mark-diary-entry activate)
  12704. "Make the position visible."
  12705. (if (and org-disable-agenda-to-diary ;; called from org-agenda
  12706. (stringp string)
  12707. buffer-file-name)
  12708. (setq string (org-modify-diary-entry-string string))))))
  12709. (defun org-modify-diary-entry-string (string)
  12710. "Add text properties to string, allowing org-mode to act on it."
  12711. (org-add-props string nil
  12712. 'mouse-face 'highlight
  12713. 'keymap org-agenda-keymap
  12714. 'help-echo (format "mouse-2 or RET jump to diary file %s"
  12715. (abbreviate-file-name buffer-file-name))
  12716. 'org-agenda-diary-link t
  12717. 'org-marker (org-agenda-new-marker (point-at-bol))))
  12718. (defun org-diary-default-entry ()
  12719. "Add a dummy entry to the diary.
  12720. Needed to avoid empty dates which mess up holiday display."
  12721. ;; Catch the error if dealing with the new add-to-diary-alist
  12722. (when org-disable-agenda-to-diary
  12723. (condition-case nil
  12724. (add-to-diary-list original-date "Org-mode dummy" "")
  12725. (error
  12726. (add-to-diary-list original-date "Org-mode dummy" "" nil)))))
  12727. ;;;###autoload
  12728. (defun org-diary (&rest args)
  12729. "Return diary information from org-files.
  12730. This function can be used in a \"sexp\" diary entry in the Emacs calendar.
  12731. It accesses org files and extracts information from those files to be
  12732. listed in the diary. The function accepts arguments specifying what
  12733. items should be listed. The following arguments are allowed:
  12734. :timestamp List the headlines of items containing a date stamp or
  12735. date range matching the selected date. Deadlines will
  12736. also be listed, on the expiration day.
  12737. :deadline List any deadlines past due, or due within
  12738. `org-deadline-warning-days'. The listing occurs only
  12739. in the diary for *today*, not at any other date. If
  12740. an entry is marked DONE, it is no longer listed.
  12741. :scheduled List all items which are scheduled for the given date.
  12742. The diary for *today* also contains items which were
  12743. scheduled earlier and are not yet marked DONE.
  12744. :todo List all TODO items from the org-file. This may be a
  12745. long list - so this is not turned on by default.
  12746. Like deadlines, these entries only show up in the
  12747. diary for *today*, not at any other date.
  12748. The call in the diary file should look like this:
  12749. &%%(org-diary) ~/path/to/some/orgfile.org
  12750. Use a separate line for each org file to check. Or, if you omit the file name,
  12751. all files listed in `org-agenda-files' will be checked automatically:
  12752. &%%(org-diary)
  12753. If you don't give any arguments (as in the example above), the default
  12754. arguments (:deadline :scheduled :timestamp) are used. So the example above may
  12755. also be written as
  12756. &%%(org-diary :deadline :timestamp :scheduled)
  12757. The function expects the lisp variables `entry' and `date' to be provided
  12758. by the caller, because this is how the calendar works. Don't use this
  12759. function from a program - use `org-agenda-get-day-entries' instead."
  12760. (org-agenda-maybe-reset-markers)
  12761. (org-compile-prefix-format 'agenda)
  12762. (org-set-sorting-strategy 'agenda)
  12763. (setq args (or args '(:deadline :scheduled :timestamp)))
  12764. (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
  12765. (list entry)
  12766. (org-agenda-files t)))
  12767. file rtn results)
  12768. ;; If this is called during org-agenda, don't return any entries to
  12769. ;; the calendar. Org Agenda will list these entries itself.
  12770. (if org-disable-agenda-to-diary (setq files nil))
  12771. (while (setq file (pop files))
  12772. (setq rtn (apply 'org-agenda-get-day-entries file date args))
  12773. (setq results (append results rtn)))
  12774. (if results
  12775. (concat (org-finalize-agenda-entries results) "\n"))))
  12776. ;;; Agenda entry finders
  12777. (defun org-agenda-get-day-entries (file date &rest args)
  12778. "Does the work for `org-diary' and `org-agenda'.
  12779. FILE is the path to a file to be checked for entries. DATE is date like
  12780. the one returned by `calendar-current-date'. ARGS are symbols indicating
  12781. which kind of entries should be extracted. For details about these, see
  12782. the documentation of `org-diary'."
  12783. (setq args (or args '(:deadline :scheduled :timestamp)))
  12784. (let* ((org-startup-with-deadline-check nil)
  12785. (org-startup-folded nil)
  12786. (org-startup-align-all-tables nil)
  12787. (buffer (if (file-exists-p file)
  12788. (org-get-agenda-file-buffer file)
  12789. (error "No such file %s" file)))
  12790. arg results rtn)
  12791. (if (not buffer)
  12792. ;; If file does not exist, make sure an error message ends up in diary
  12793. (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  12794. (with-current-buffer buffer
  12795. (unless (org-mode-p)
  12796. (error "Agenda file %s is not in `org-mode'" file))
  12797. (setq org-category-table (org-get-category-table))
  12798. (let ((case-fold-search nil))
  12799. (save-excursion
  12800. (save-restriction
  12801. (if org-agenda-restrict
  12802. (narrow-to-region org-agenda-restrict-begin
  12803. org-agenda-restrict-end)
  12804. (widen))
  12805. ;; The way we repeatedly append to `results' makes it O(n^2) :-(
  12806. (while (setq arg (pop args))
  12807. (cond
  12808. ((and (eq arg :todo)
  12809. (equal date (calendar-current-date)))
  12810. (setq rtn (org-agenda-get-todos))
  12811. (setq results (append results rtn)))
  12812. ((eq arg :timestamp)
  12813. (setq rtn (org-agenda-get-blocks))
  12814. (setq results (append results rtn))
  12815. (setq rtn (org-agenda-get-timestamps))
  12816. (setq results (append results rtn)))
  12817. ((eq arg :scheduled)
  12818. (setq rtn (org-agenda-get-scheduled))
  12819. (setq results (append results rtn)))
  12820. ((eq arg :closed)
  12821. (setq rtn (org-agenda-get-closed))
  12822. (setq results (append results rtn)))
  12823. ((and (eq arg :deadline)
  12824. (equal date (calendar-current-date)))
  12825. (setq rtn (org-agenda-get-deadlines))
  12826. (setq results (append results rtn))))))))
  12827. results))))
  12828. (defun org-entry-is-done-p ()
  12829. "Is the current entry marked DONE?"
  12830. (save-excursion
  12831. (and (re-search-backward "[\r\n]\\*" nil t)
  12832. (looking-at org-nl-done-regexp))))
  12833. (defun org-at-date-range-p (&optional inactive-ok)
  12834. "Is the cursor inside a date range?"
  12835. (interactive)
  12836. (save-excursion
  12837. (catch 'exit
  12838. (let ((pos (point)))
  12839. (skip-chars-backward "^[<\r\n")
  12840. (skip-chars-backward "<[")
  12841. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  12842. (>= (match-end 0) pos)
  12843. (throw 'exit t))
  12844. (skip-chars-backward "^<[\r\n")
  12845. (skip-chars-backward "<[")
  12846. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  12847. (>= (match-end 0) pos)
  12848. (throw 'exit t)))
  12849. nil)))
  12850. (defun org-agenda-get-todos ()
  12851. "Return the TODO information for agenda display."
  12852. (let* ((props (list 'face nil
  12853. 'done-face 'org-done
  12854. 'org-not-done-regexp org-not-done-regexp
  12855. 'mouse-face 'highlight
  12856. 'keymap org-agenda-keymap
  12857. 'help-echo
  12858. (format "mouse-2 or RET jump to org file %s"
  12859. (abbreviate-file-name buffer-file-name))))
  12860. (regexp (concat "[\n\r]\\*+ *\\("
  12861. (if org-select-this-todo-keyword
  12862. (concat "\\<\\(" org-select-this-todo-keyword
  12863. "\\)\\>")
  12864. org-not-done-regexp)
  12865. "[^\n\r]*\\)"))
  12866. (deadline-re (concat ".*\\(\n[^*].*\\)?" org-deadline-time-regexp))
  12867. (sched-re (concat ".*\\(\n[^*].*\\)?" org-scheduled-time-regexp))
  12868. marker priority category tags
  12869. ee txt)
  12870. (goto-char (point-min))
  12871. (while (re-search-forward regexp nil t)
  12872. (catch :skip
  12873. (save-match-data
  12874. (beginning-of-line)
  12875. (when (or (and org-agenda-todo-ignore-scheduled
  12876. (looking-at sched-re))
  12877. (and org-agenda-todo-ignore-deadlines
  12878. (looking-at deadline-re)
  12879. (org-deadline-close (match-string 2))))
  12880. (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
  12881. (throw :skip nil)))
  12882. (org-agenda-skip)
  12883. (goto-char (match-beginning 1))
  12884. (setq marker (org-agenda-new-marker (1+ (match-beginning 0)))
  12885. category (org-get-category)
  12886. tags (org-get-tags-at (point))
  12887. txt (org-format-agenda-item "" (match-string 1) category tags)
  12888. priority
  12889. (+ (org-get-priority txt)
  12890. (if org-todo-kwd-priority-p
  12891. (- org-todo-kwd-max-priority -2
  12892. (length
  12893. (member (match-string 2) org-todo-keywords)))
  12894. 1)))
  12895. (org-add-props txt props
  12896. 'org-marker marker 'org-hd-marker marker
  12897. 'priority priority 'category category)
  12898. (push txt ee)
  12899. (if org-agenda-todo-list-sublevels
  12900. (goto-char (match-end 1))
  12901. (org-end-of-subtree 'invisible))))
  12902. (nreverse ee)))
  12903. (defconst org-agenda-no-heading-message
  12904. "No heading for this item in buffer or region.")
  12905. (defun org-agenda-get-timestamps ()
  12906. "Return the date stamp information for agenda display."
  12907. (let* ((props (list 'face nil
  12908. 'org-not-done-regexp org-not-done-regexp
  12909. 'mouse-face 'highlight
  12910. 'keymap org-agenda-keymap
  12911. 'help-echo
  12912. (format "mouse-2 or RET jump to org file %s"
  12913. (abbreviate-file-name buffer-file-name))))
  12914. (regexp (regexp-quote
  12915. (substring
  12916. (format-time-string
  12917. (car org-time-stamp-formats)
  12918. (apply 'encode-time ; DATE bound by calendar
  12919. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  12920. 0 11)))
  12921. marker hdmarker deadlinep scheduledp donep tmp priority category
  12922. ee txt timestr tags)
  12923. (goto-char (point-min))
  12924. (while (re-search-forward regexp nil t)
  12925. (catch :skip
  12926. (and (save-match-data (org-at-date-range-p)) (throw :skip nil))
  12927. (org-agenda-skip)
  12928. (setq marker (org-agenda-new-marker (match-beginning 0))
  12929. category (org-get-category (match-beginning 0))
  12930. tmp (buffer-substring (max (point-min)
  12931. (- (match-beginning 0)
  12932. org-ds-keyword-length))
  12933. (match-beginning 0))
  12934. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  12935. deadlinep (string-match org-deadline-regexp tmp)
  12936. scheduledp (string-match org-scheduled-regexp tmp)
  12937. donep (org-entry-is-done-p))
  12938. (and org-agenda-skip-scheduled-if-done
  12939. scheduledp donep
  12940. (throw :skip t))
  12941. (if (string-match ">" timestr)
  12942. ;; substring should only run to end of time stamp
  12943. (setq timestr (substring timestr 0 (match-end 0))))
  12944. (save-excursion
  12945. (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t)
  12946. (progn
  12947. (goto-char (match-end 1))
  12948. (setq hdmarker (org-agenda-new-marker)
  12949. tags (org-get-tags-at))
  12950. (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
  12951. (setq txt (org-format-agenda-item
  12952. (format "%s%s"
  12953. (if deadlinep "Deadline: " "")
  12954. (if scheduledp "Scheduled: " ""))
  12955. (match-string 1) category tags timestr)))
  12956. (setq txt org-agenda-no-heading-message))
  12957. (setq priority (org-get-priority txt))
  12958. (org-add-props txt props
  12959. 'org-marker marker 'org-hd-marker hdmarker)
  12960. (if deadlinep
  12961. (org-add-props txt nil
  12962. 'face (if donep 'org-done 'org-warning)
  12963. 'undone-face 'org-warning 'done-face 'org-done
  12964. 'category category 'priority (+ 100 priority))
  12965. (if scheduledp
  12966. (org-add-props txt nil
  12967. 'face 'org-scheduled-today
  12968. 'undone-face 'org-scheduled-today 'done-face 'org-done
  12969. 'category category 'priority (+ 99 priority))
  12970. (org-add-props txt nil 'priority priority 'category category)))
  12971. (push txt ee))
  12972. (outline-next-heading)))
  12973. (nreverse ee)))
  12974. (defun org-agenda-get-closed ()
  12975. "Return the logged TODO entries for agenda display."
  12976. (let* ((props (list 'mouse-face 'highlight
  12977. 'org-not-done-regexp org-not-done-regexp
  12978. 'keymap org-agenda-keymap
  12979. 'help-echo
  12980. (format "mouse-2 or RET jump to org file %s"
  12981. (abbreviate-file-name buffer-file-name))))
  12982. (regexp (concat
  12983. "\\<\\(" org-closed-string "\\|" org-clock-string "\\) *\\["
  12984. (regexp-quote
  12985. (substring
  12986. (format-time-string
  12987. (car org-time-stamp-formats)
  12988. (apply 'encode-time ; DATE bound by calendar
  12989. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  12990. 1 11))))
  12991. marker hdmarker priority category tags closedp
  12992. ee txt timestr)
  12993. (goto-char (point-min))
  12994. (while (re-search-forward regexp nil t)
  12995. (catch :skip
  12996. (org-agenda-skip)
  12997. (setq marker (org-agenda-new-marker (match-beginning 0))
  12998. closedp (equal (match-string 1) org-closed-string)
  12999. category (org-get-category (match-beginning 0))
  13000. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  13001. ;; donep (org-entry-is-done-p)
  13002. )
  13003. (if (string-match "\\]" timestr)
  13004. ;; substring should only run to end of time stamp
  13005. (setq timestr (substring timestr 0 (match-end 0))))
  13006. (save-excursion
  13007. (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t)
  13008. (progn
  13009. (goto-char (match-end 1))
  13010. (setq hdmarker (org-agenda-new-marker)
  13011. tags (org-get-tags-at))
  13012. (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
  13013. (setq txt (org-format-agenda-item
  13014. (if closedp "Closed: " "Clocked: ")
  13015. (match-string 1) category tags timestr)))
  13016. (setq txt org-agenda-no-heading-message))
  13017. (setq priority 100000)
  13018. (org-add-props txt props
  13019. 'org-marker marker 'org-hd-marker hdmarker 'face 'org-done
  13020. 'priority priority 'category category
  13021. 'undone-face 'org-warning 'done-face 'org-done)
  13022. (push txt ee))
  13023. (outline-next-heading)))
  13024. (nreverse ee)))
  13025. (defun org-agenda-get-deadlines ()
  13026. "Return the deadline information for agenda display."
  13027. (let* ((wdays org-deadline-warning-days)
  13028. (props (list 'mouse-face 'highlight
  13029. 'org-not-done-regexp org-not-done-regexp
  13030. 'keymap org-agenda-keymap
  13031. 'help-echo
  13032. (format "mouse-2 or RET jump to org file %s"
  13033. (abbreviate-file-name buffer-file-name))))
  13034. (regexp org-deadline-time-regexp)
  13035. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  13036. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  13037. d2 diff pos pos1 category tags
  13038. ee txt head face)
  13039. (goto-char (point-min))
  13040. (while (re-search-forward regexp nil t)
  13041. (catch :skip
  13042. (org-agenda-skip)
  13043. (setq pos (1- (match-beginning 1))
  13044. d2 (time-to-days
  13045. (org-time-string-to-time (match-string 1)))
  13046. diff (- d2 d1))
  13047. ;; When to show a deadline in the calendar:
  13048. ;; If the expiration is within wdays warning time.
  13049. ;; Past-due deadlines are only shown on the current date
  13050. (if (and (< diff wdays) todayp (not (= diff 0)))
  13051. (save-excursion
  13052. (setq category (org-get-category))
  13053. (if (re-search-backward "\\(^\\|\r\\)\\*+[ \t]*" nil t)
  13054. (progn
  13055. (goto-char (match-end 0))
  13056. (setq pos1 (match-end 1))
  13057. (setq tags (org-get-tags-at pos1))
  13058. (setq head (buffer-substring-no-properties
  13059. (point)
  13060. (progn (skip-chars-forward "^\r\n")
  13061. (point))))
  13062. (if (string-match org-looking-at-done-regexp head)
  13063. (setq txt nil)
  13064. (setq txt (org-format-agenda-item
  13065. (format "In %3d d.: " diff) head category tags))))
  13066. (setq txt org-agenda-no-heading-message))
  13067. (when txt
  13068. (setq face (cond ((<= diff 0) 'org-warning)
  13069. ((<= diff 5) 'org-upcoming-deadline)
  13070. (t nil)))
  13071. (org-add-props txt props
  13072. 'org-marker (org-agenda-new-marker pos)
  13073. 'org-hd-marker (org-agenda-new-marker pos1)
  13074. 'priority (+ (- 10 diff) (org-get-priority txt))
  13075. 'category category
  13076. 'face face 'undone-face face 'done-face 'org-done)
  13077. (push txt ee))))))
  13078. ee))
  13079. (defun org-agenda-get-scheduled ()
  13080. "Return the scheduled information for agenda display."
  13081. (let* ((props (list 'face 'org-scheduled-previously
  13082. 'org-not-done-regexp org-not-done-regexp
  13083. 'undone-face 'org-scheduled-previously
  13084. 'done-face 'org-done
  13085. 'mouse-face 'highlight
  13086. 'keymap org-agenda-keymap
  13087. 'help-echo
  13088. (format "mouse-2 or RET jump to org file %s"
  13089. (abbreviate-file-name buffer-file-name))))
  13090. (regexp org-scheduled-time-regexp)
  13091. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  13092. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  13093. d2 diff pos pos1 category tags
  13094. ee txt head)
  13095. (goto-char (point-min))
  13096. (while (re-search-forward regexp nil t)
  13097. (catch :skip
  13098. (org-agenda-skip)
  13099. (setq pos (1- (match-beginning 1))
  13100. d2 (time-to-days
  13101. (org-time-string-to-time (match-string 1)))
  13102. diff (- d2 d1))
  13103. ;; When to show a scheduled item in the calendar:
  13104. ;; If it is on or past the date.
  13105. (if (and (< diff 0) todayp)
  13106. (save-excursion
  13107. (setq category (org-get-category))
  13108. (if (re-search-backward "\\(^\\|\r\\)\\*+[ \t]*" nil t)
  13109. (progn
  13110. (goto-char (match-end 0))
  13111. (setq pos1 (match-end 1))
  13112. (setq tags (org-get-tags-at))
  13113. (setq head (buffer-substring-no-properties
  13114. (point)
  13115. (progn (skip-chars-forward "^\r\n") (point))))
  13116. (if (string-match org-looking-at-done-regexp head)
  13117. (setq txt nil)
  13118. (setq txt (org-format-agenda-item
  13119. (format "Sched.%2dx: " (- 1 diff)) head
  13120. category tags))))
  13121. (setq txt org-agenda-no-heading-message))
  13122. (when txt
  13123. (org-add-props txt props
  13124. 'org-marker (org-agenda-new-marker pos)
  13125. 'org-hd-marker (org-agenda-new-marker pos1)
  13126. 'priority (+ (- 5 diff) (org-get-priority txt))
  13127. 'category category)
  13128. (push txt ee))))))
  13129. ee))
  13130. (defun org-agenda-get-blocks ()
  13131. "Return the date-range information for agenda display."
  13132. (let* ((props (list 'face nil
  13133. 'org-not-done-regexp org-not-done-regexp
  13134. 'mouse-face 'highlight
  13135. 'keymap org-agenda-keymap
  13136. 'help-echo
  13137. (format "mouse-2 or RET jump to org file %s"
  13138. (abbreviate-file-name buffer-file-name))))
  13139. (regexp org-tr-regexp)
  13140. (d0 (calendar-absolute-from-gregorian date))
  13141. marker hdmarker ee txt d1 d2 s1 s2 timestr category tags pos)
  13142. (goto-char (point-min))
  13143. (while (re-search-forward regexp nil t)
  13144. (catch :skip
  13145. (org-agenda-skip)
  13146. (setq pos (point))
  13147. (setq timestr (match-string 0)
  13148. s1 (match-string 1)
  13149. s2 (match-string 2)
  13150. d1 (time-to-days (org-time-string-to-time s1))
  13151. d2 (time-to-days (org-time-string-to-time s2)))
  13152. (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
  13153. ;; Only allow days between the limits, because the normal
  13154. ;; date stamps will catch the limits.
  13155. (save-excursion
  13156. (setq marker (org-agenda-new-marker (point)))
  13157. (setq category (org-get-category))
  13158. (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t)
  13159. (progn
  13160. (setq hdmarker (org-agenda-new-marker (match-end 1)))
  13161. (goto-char (match-end 1))
  13162. (setq tags (org-get-tags-at))
  13163. (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
  13164. (setq txt (org-format-agenda-item
  13165. (format (if (= d1 d2) "" "(%d/%d): ")
  13166. (1+ (- d0 d1)) (1+ (- d2 d1)))
  13167. (match-string 1) category tags
  13168. (if (= d0 d1) timestr))))
  13169. (setq txt org-agenda-no-heading-message))
  13170. (org-add-props txt props
  13171. 'org-marker marker 'org-hd-marker hdmarker
  13172. 'priority (org-get-priority txt) 'category category)
  13173. (push txt ee)))
  13174. (goto-char pos)))
  13175. ;; Sort the entries by expiration date.
  13176. (nreverse ee)))
  13177. ;;; Agenda presentation and sorting
  13178. ;; FIXME: should I allow spaces around the dash?
  13179. (defconst org-plain-time-of-day-regexp
  13180. (concat
  13181. "\\(\\<[012]?[0-9]"
  13182. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  13183. "\\(--?"
  13184. "\\(\\<[012]?[0-9]"
  13185. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  13186. "\\)?")
  13187. "Regular expression to match a plain time or time range.
  13188. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  13189. groups carry important information:
  13190. 0 the full match
  13191. 1 the first time, range or not
  13192. 8 the second time, if it is a range.")
  13193. (defconst org-stamp-time-of-day-regexp
  13194. (concat
  13195. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  13196. "\\([012][0-9]:[0-5][0-9]\\)>"
  13197. "\\(--?"
  13198. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  13199. "Regular expression to match a timestamp time or time range.
  13200. After a match, the following groups carry important information:
  13201. 0 the full match
  13202. 1 date plus weekday, for backreferencing to make sure both times on same day
  13203. 2 the first time, range or not
  13204. 4 the second time, if it is a range.")
  13205. (defvar org-prefix-has-time nil
  13206. "A flag, set by `org-compile-prefix-format'.
  13207. The flag is set if the currently compiled format contains a `%t'.")
  13208. (defvar org-prefix-has-tag nil
  13209. "A flag, set by `org-compile-prefix-format'.
  13210. The flag is set if the currently compiled format contains a `%T'.")
  13211. (defun org-format-agenda-item (extra txt &optional category tags dotime
  13212. noprefix)
  13213. "Format TXT to be inserted into the agenda buffer.
  13214. In particular, it adds the prefix and corresponding text properties. EXTRA
  13215. must be a string and replaces the `%s' specifier in the prefix format.
  13216. CATEGORY (string, symbol or nil) may be used to overrule the default
  13217. category taken from local variable or file name. It will replace the `%c'
  13218. specifier in the format. DOTIME, when non-nil, indicates that a
  13219. time-of-day should be extracted from TXT for sorting of this entry, and for
  13220. the `%t' specifier in the format. When DOTIME is a string, this string is
  13221. searched for a time before TXT is. NOPREFIX is a flag and indicates that
  13222. only the correctly processes TXT should be returned - this is used by
  13223. `org-agenda-change-all-lines'. TAGS can be the tags of the headline."
  13224. (save-match-data
  13225. ;; Diary entries sometimes have extra whitespace at the beginning
  13226. (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
  13227. (let* ((category (or category
  13228. org-category
  13229. (if buffer-file-name
  13230. (file-name-sans-extension
  13231. (file-name-nondirectory buffer-file-name))
  13232. "")))
  13233. (tag (if tags (nth (1- (length tags)) tags) ""))
  13234. time ;; needed for the eval of the prefix format
  13235. (ts (if dotime (concat (if (stringp dotime) dotime "") txt)))
  13236. (time-of-day (and dotime (org-get-time-of-day ts)))
  13237. stamp plain s0 s1 s2 rtn)
  13238. (when (and dotime time-of-day org-prefix-has-time)
  13239. ;; Extract starting and ending time and move them to prefix
  13240. (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
  13241. (setq plain (string-match org-plain-time-of-day-regexp ts)))
  13242. (setq s0 (match-string 0 ts)
  13243. s1 (match-string (if plain 1 2) ts)
  13244. s2 (match-string (if plain 8 4) ts))
  13245. ;; If the times are in TXT (not in DOTIMES), and the prefix will list
  13246. ;; them, we might want to remove them there to avoid duplication.
  13247. ;; The user can turn this off with a variable.
  13248. (if (and org-agenda-remove-times-when-in-prefix (or stamp plain)
  13249. (string-match (concat (regexp-quote s0) " *") txt)
  13250. (if (eq org-agenda-remove-times-when-in-prefix 'beg)
  13251. (= (match-beginning 0) 0)
  13252. t))
  13253. (setq txt (replace-match "" nil nil txt))))
  13254. ;; Normalize the time(s) to 24 hour
  13255. (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
  13256. (if s2 (setq s2 (org-get-time-of-day s2 'string t))))
  13257. (when (string-match "\\([ \t]+\\)\\(:[a-zA-Z_@0-9:]+:\\)[ \t]*$" txt)
  13258. ;; Tags are in the string
  13259. (if (or (eq org-agenda-remove-tags-when-in-prefix t)
  13260. (and org-agenda-remove-tags-when-in-prefix
  13261. org-prefix-has-tag))
  13262. (setq txt (replace-match "" t t txt))
  13263. (setq txt (replace-match
  13264. (concat (make-string (max (- 50 (length txt)) 1) ?\ )
  13265. (match-string 2 txt))
  13266. t t txt))))
  13267. ;; Create the final string
  13268. (if noprefix
  13269. (setq rtn txt)
  13270. ;; Prepare the variables needed in the eval of the compiled format
  13271. (setq time (cond (s2 (concat s1 "-" s2))
  13272. (s1 (concat s1 "......"))
  13273. (t ""))
  13274. extra (or extra "")
  13275. category (if (symbolp category) (symbol-name category) category))
  13276. ;; Evaluate the compiled format
  13277. (setq rtn (concat (eval org-prefix-format-compiled) txt)))
  13278. ;; And finally add the text properties
  13279. (org-add-props rtn nil
  13280. 'category (downcase category) 'tags tags
  13281. 'prefix-length (- (length rtn) (length txt))
  13282. 'time-of-day time-of-day
  13283. 'dotime dotime))))
  13284. (defvar org-agenda-sorting-strategy)
  13285. (defvar org-agenda-sorting-strategy-selected nil)
  13286. (defun org-agenda-add-time-grid-maybe (list ndays todayp)
  13287. (catch 'exit
  13288. (cond ((not org-agenda-use-time-grid) (throw 'exit list))
  13289. ((and todayp (member 'today (car org-agenda-time-grid))))
  13290. ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
  13291. ((member 'weekly (car org-agenda-time-grid)))
  13292. (t (throw 'exit list)))
  13293. (let* ((have (delq nil (mapcar
  13294. (lambda (x) (get-text-property 1 'time-of-day x))
  13295. list)))
  13296. (string (nth 1 org-agenda-time-grid))
  13297. (gridtimes (nth 2 org-agenda-time-grid))
  13298. (req (car org-agenda-time-grid))
  13299. (remove (member 'remove-match req))
  13300. new time)
  13301. (if (and (member 'require-timed req) (not have))
  13302. ;; don't show empty grid
  13303. (throw 'exit list))
  13304. (while (setq time (pop gridtimes))
  13305. (unless (and remove (member time have))
  13306. (setq time (int-to-string time))
  13307. (push (org-format-agenda-item
  13308. nil string "" nil
  13309. (concat (substring time 0 -2) ":" (substring time -2)))
  13310. new)
  13311. (put-text-property
  13312. 1 (length (car new)) 'face 'org-time-grid (car new))))
  13313. (if (member 'time-up org-agenda-sorting-strategy-selected)
  13314. (append new list)
  13315. (append list new)))))
  13316. (defun org-compile-prefix-format (key)
  13317. "Compile the prefix format into a Lisp form that can be evaluated.
  13318. The resulting form is returned and stored in the variable
  13319. `org-prefix-format-compiled'."
  13320. (setq org-prefix-has-time nil org-prefix-has-tag nil)
  13321. (let ((s (cond
  13322. ((stringp org-agenda-prefix-format)
  13323. org-agenda-prefix-format)
  13324. ((assq key org-agenda-prefix-format)
  13325. (cdr (assq key org-agenda-prefix-format)))
  13326. (t " %-12:c%?-12t% s")))
  13327. (start 0)
  13328. varform vars var e c f opt)
  13329. (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cts]\\)"
  13330. s start)
  13331. (setq var (cdr (assoc (match-string 4 s)
  13332. '(("c" . category) ("t" . time) ("s" . extra)
  13333. ("T" . tag))))
  13334. c (or (match-string 3 s) "")
  13335. opt (match-beginning 1)
  13336. start (1+ (match-beginning 0)))
  13337. (if (equal var 'time) (setq org-prefix-has-time t))
  13338. (if (equal var 'tag) (setq org-prefix-has-tag t))
  13339. (setq f (concat "%" (match-string 2 s) "s"))
  13340. (if opt
  13341. (setq varform
  13342. `(if (equal "" ,var)
  13343. ""
  13344. (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
  13345. (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
  13346. (setq s (replace-match "%s" t nil s))
  13347. (push varform vars))
  13348. (setq vars (nreverse vars))
  13349. (setq org-prefix-format-compiled `(format ,s ,@vars))))
  13350. (defun org-set-sorting-strategy (key)
  13351. (if (symbolp (car org-agenda-sorting-strategy))
  13352. ;; the old format
  13353. (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
  13354. (setq org-agenda-sorting-strategy-selected
  13355. (or (cdr (assq key org-agenda-sorting-strategy))
  13356. (cdr (assq 'agenda org-agenda-sorting-strategy))
  13357. '(time-up category-keep priority-down)))))
  13358. (defun org-get-time-of-day (s &optional string mod24)
  13359. "Check string S for a time of day.
  13360. If found, return it as a military time number between 0 and 2400.
  13361. If not found, return nil.
  13362. The optional STRING argument forces conversion into a 5 character wide string
  13363. HH:MM."
  13364. (save-match-data
  13365. (when
  13366. (or
  13367. (string-match
  13368. "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
  13369. (string-match
  13370. "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
  13371. (let* ((h (string-to-number (match-string 1 s)))
  13372. (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
  13373. (ampm (if (match-end 4) (downcase (match-string 4 s))))
  13374. (am-p (equal ampm "am"))
  13375. (h1 (cond ((not ampm) h)
  13376. ((= h 12) (if am-p 0 12))
  13377. (t (+ h (if am-p 0 12)))))
  13378. (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
  13379. (mod h1 24) h1))
  13380. (t0 (+ (* 100 h2) m))
  13381. (t1 (concat (if (>= h1 24) "+" " ")
  13382. (if (< t0 100) "0" "")
  13383. (if (< t0 10) "0" "")
  13384. (int-to-string t0))))
  13385. (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
  13386. (defun org-finalize-agenda-entries (list &optional nosort)
  13387. "Sort and concatenate the agenda items."
  13388. (setq list (mapcar 'org-agenda-highlight-todo list))
  13389. (if nosort
  13390. list
  13391. (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
  13392. (defun org-agenda-highlight-todo (x)
  13393. (let (re pl)
  13394. (if (eq x 'line)
  13395. (save-excursion
  13396. (beginning-of-line 1)
  13397. (setq re (get-text-property (point) 'org-not-done-regexp))
  13398. (goto-char (+ (point) (or (get-text-property (point) 'prefix-length) 0)))
  13399. (and (looking-at (concat "[ \t]*\\.*" re))
  13400. (add-text-properties (match-beginning 0) (match-end 0)
  13401. '(face org-todo))))
  13402. (setq re (concat (get-text-property 0 'org-not-done-regexp x))
  13403. pl (get-text-property 0 'prefix-length x))
  13404. (and re (equal (string-match (concat "\\(\\.*\\)" re) x (or pl 0)) pl)
  13405. (add-text-properties (or (match-end 1) (match-end 0)) (match-end 0)
  13406. '(face org-todo) x))
  13407. x)))
  13408. (defsubst org-cmp-priority (a b)
  13409. "Compare the priorities of string A and B."
  13410. (let ((pa (or (get-text-property 1 'priority a) 0))
  13411. (pb (or (get-text-property 1 'priority b) 0)))
  13412. (cond ((> pa pb) +1)
  13413. ((< pa pb) -1)
  13414. (t nil))))
  13415. (defsubst org-cmp-category (a b)
  13416. "Compare the string values of categories of strings A and B."
  13417. (let ((ca (or (get-text-property 1 'category a) ""))
  13418. (cb (or (get-text-property 1 'category b) "")))
  13419. (cond ((string-lessp ca cb) -1)
  13420. ((string-lessp cb ca) +1)
  13421. (t nil))))
  13422. (defsubst org-cmp-tag (a b)
  13423. "Compare the string values of categories of strings A and B."
  13424. (let ((ta (car (last (get-text-property 1 'tags a))))
  13425. (tb (car (last (get-text-property 1 'tags b)))))
  13426. (cond ((not ta) +1)
  13427. ((not tb) -1)
  13428. ((string-lessp ta tb) -1)
  13429. ((string-lessp tb ta) +1)
  13430. (t nil))))
  13431. (defsubst org-cmp-time (a b)
  13432. "Compare the time-of-day values of strings A and B."
  13433. (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
  13434. (ta (or (get-text-property 1 'time-of-day a) def))
  13435. (tb (or (get-text-property 1 'time-of-day b) def)))
  13436. (cond ((< ta tb) -1)
  13437. ((< tb ta) +1)
  13438. (t nil))))
  13439. (defun org-entries-lessp (a b)
  13440. "Predicate for sorting agenda entries."
  13441. ;; The following variables will be used when the form is evaluated.
  13442. (let* ((time-up (org-cmp-time a b))
  13443. (time-down (if time-up (- time-up) nil))
  13444. (priority-up (org-cmp-priority a b))
  13445. (priority-down (if priority-up (- priority-up) nil))
  13446. (category-up (org-cmp-category a b))
  13447. (category-down (if category-up (- category-up) nil))
  13448. (category-keep (if category-up +1 nil))
  13449. (tag-up (org-cmp-tag a b))
  13450. (tag-down (if tag-up (- tag-up) nil)))
  13451. (cdr (assoc
  13452. (eval (cons 'or org-agenda-sorting-strategy-selected))
  13453. '((-1 . t) (1 . nil) (nil . nil))))))
  13454. ;;; Agenda commands
  13455. (defun org-agenda-check-type (error &rest types)
  13456. "Check if agenda buffer is of allowed type.
  13457. If ERROR is non-nil, throw an error, otherwise just return nil."
  13458. (if (memq org-agenda-type types)
  13459. t
  13460. (if error
  13461. (error "Not allowed in %s-type agenda buffers" org-agenda-type)
  13462. nil)))
  13463. (defun org-agenda-quit ()
  13464. "Exit agenda by removing the window or the buffer."
  13465. (interactive)
  13466. (let ((buf (current-buffer)))
  13467. (if (not (one-window-p)) (delete-window))
  13468. (kill-buffer buf)
  13469. (org-agenda-maybe-reset-markers 'force))
  13470. ;; Maybe restore the pre-agenda window configuration.
  13471. (and org-agenda-restore-windows-after-quit
  13472. (not (eq org-agenda-window-setup 'other-frame))
  13473. org-pre-agenda-window-conf
  13474. (set-window-configuration org-pre-agenda-window-conf)))
  13475. (defun org-agenda-exit ()
  13476. "Exit agenda by removing the window or the buffer.
  13477. Also kill all Org-mode buffers which have been loaded by `org-agenda'.
  13478. Org-mode buffers visited directly by the user will not be touched."
  13479. (interactive)
  13480. (org-release-buffers org-agenda-new-buffers)
  13481. (setq org-agenda-new-buffers nil)
  13482. (org-agenda-quit))
  13483. (defun org-save-all-org-buffers ()
  13484. "Save all Org-mode buffers without user confirmation."
  13485. (interactive)
  13486. (message "Saving all Org-mode buffers...")
  13487. (save-some-buffers t 'org-mode-p)
  13488. (message "Saving all Org-mode buffers... done"))
  13489. (defun org-agenda-redo ()
  13490. "Rebuild Agenda.
  13491. When this is the global TODO list, a prefix argument will be interpreted."
  13492. (interactive)
  13493. (let* ((org-agenda-keep-modes t)
  13494. (line (org-current-line))
  13495. (window-line (- line (org-current-line (window-start)))))
  13496. (message "Rebuilding agenda buffer...")
  13497. (eval org-agenda-redo-command)
  13498. (setq org-agenda-undo-list nil
  13499. org-agenda-pending-undo-list nil)
  13500. (message "Rebuilding agenda buffer...done")
  13501. (goto-line line)
  13502. (recenter window-line)))
  13503. (defun org-agenda-goto-today ()
  13504. "Go to today."
  13505. (interactive)
  13506. (org-agenda-check-type t 'timeline 'agenda)
  13507. (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
  13508. (cond
  13509. (tdpos (goto-char tdpos))
  13510. ((eq org-agenda-type 'agenda)
  13511. (let ((org-agenda-overriding-arguments org-agenda-last-arguments))
  13512. (setf (nth 1 org-agenda-overriding-arguments) nil)
  13513. (org-agenda-redo)
  13514. (org-agenda-find-today-or-agenda)))
  13515. (t (error "Cannot find today")))))
  13516. (defun org-agenda-find-today-or-agenda ()
  13517. (goto-char
  13518. (or (text-property-any (point-min) (point-max) 'org-today t)
  13519. (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
  13520. (point-min))))
  13521. (defun org-agenda-later (arg)
  13522. "Go forward in time by `org-agenda-ndays' days.
  13523. With prefix ARG, go forward that many times `org-agenda-ndays'."
  13524. (interactive "p")
  13525. (org-agenda-check-type t 'agenda)
  13526. (let ((org-agenda-overriding-arguments
  13527. (list (car org-agenda-last-arguments)
  13528. (+ org-starting-day (* arg org-agenda-ndays))
  13529. nil t)))
  13530. (org-agenda-redo)
  13531. (org-agenda-find-today-or-agenda)))
  13532. (defun org-agenda-earlier (arg)
  13533. "Go back in time by `org-agenda-ndays' days.
  13534. With prefix ARG, go back that many times `org-agenda-ndays'."
  13535. (interactive "p")
  13536. (org-agenda-check-type t 'agenda)
  13537. (let ((org-agenda-overriding-arguments
  13538. (list (car org-agenda-last-arguments)
  13539. (- org-starting-day (* arg org-agenda-ndays))
  13540. nil t)))
  13541. (org-agenda-redo)
  13542. (org-agenda-find-today-or-agenda)))
  13543. (defun org-agenda-week-view ()
  13544. "Switch to weekly view for agenda."
  13545. (interactive)
  13546. (org-agenda-check-type t 'agenda)
  13547. (if (= org-agenda-ndays 7)
  13548. (error "This is already the week view"))
  13549. (setq org-agenda-ndays 7)
  13550. (let ((org-agenda-overriding-arguments
  13551. (list (car org-agenda-last-arguments)
  13552. (or (get-text-property (point) 'day)
  13553. org-starting-day)
  13554. nil t)))
  13555. (org-agenda-redo)
  13556. (org-agenda-find-today-or-agenda))
  13557. (org-agenda-set-mode-name)
  13558. (message "Switched to week view"))
  13559. (defun org-agenda-day-view ()
  13560. "Switch to daily view for agenda."
  13561. (interactive)
  13562. (org-agenda-check-type t 'agenda)
  13563. (if (= org-agenda-ndays 1)
  13564. (error "This is already the day view"))
  13565. (setq org-agenda-ndays 1)
  13566. (let ((org-agenda-overriding-arguments
  13567. (list (car org-agenda-last-arguments)
  13568. (or (get-text-property (point) 'day)
  13569. org-starting-day)
  13570. nil t)))
  13571. (org-agenda-redo)
  13572. (org-agenda-find-today-or-agenda))
  13573. (org-agenda-set-mode-name)
  13574. (message "Switched to day view"))
  13575. ;; FIXME: this no longer works if user make date format that starts with a blank
  13576. (defun org-agenda-next-date-line (&optional arg)
  13577. "Jump to the next line indicating a date in agenda buffer."
  13578. (interactive "p")
  13579. (org-agenda-check-type t 'agenda 'timeline)
  13580. (beginning-of-line 1)
  13581. (if (looking-at "^\\S-") (forward-char 1))
  13582. (if (not (re-search-forward "^\\S-" nil t arg))
  13583. (progn
  13584. (backward-char 1)
  13585. (error "No next date after this line in this buffer")))
  13586. (goto-char (match-beginning 0)))
  13587. (defun org-agenda-previous-date-line (&optional arg)
  13588. "Jump to the previous line indicating a date in agenda buffer."
  13589. (interactive "p")
  13590. (org-agenda-check-type t 'agenda 'timeline)
  13591. (beginning-of-line 1)
  13592. (if (not (re-search-backward "^\\S-" nil t arg))
  13593. (error "No previous date before this line in this buffer")))
  13594. ;; Initialize the highlight
  13595. (defvar org-hl (org-make-overlay 1 1))
  13596. (org-overlay-put org-hl 'face 'highlight)
  13597. (defun org-highlight (begin end &optional buffer)
  13598. "Highlight a region with overlay."
  13599. (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
  13600. org-hl begin end (or buffer (current-buffer))))
  13601. (defun org-unhighlight ()
  13602. "Detach overlay INDEX."
  13603. (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
  13604. (defun org-highlight-until-next-command (beg end &optional buffer)
  13605. (org-highlight beg end buffer)
  13606. (add-hook 'pre-command-hook 'org-unhighlight-once))
  13607. (defun org-unhighlight-once ()
  13608. (remove-hook 'pre-command-hook 'org-unhighlight-once)
  13609. (org-unhighlight))
  13610. (defun org-agenda-follow-mode ()
  13611. "Toggle follow mode in an agenda buffer."
  13612. (interactive)
  13613. (setq org-agenda-follow-mode (not org-agenda-follow-mode))
  13614. (org-agenda-set-mode-name)
  13615. (message "Follow mode is %s"
  13616. (if org-agenda-follow-mode "on" "off")))
  13617. (defun org-agenda-log-mode ()
  13618. "Toggle log mode in an agenda buffer."
  13619. (interactive)
  13620. (org-agenda-check-type t 'agenda 'timeline)
  13621. (setq org-agenda-show-log (not org-agenda-show-log))
  13622. (org-agenda-set-mode-name)
  13623. (org-agenda-redo)
  13624. (message "Log mode is %s"
  13625. (if org-agenda-show-log "on" "off")))
  13626. (defun org-agenda-toggle-diary ()
  13627. "Toggle diary inclusion in an agenda buffer."
  13628. (interactive)
  13629. (org-agenda-check-type t 'agenda)
  13630. (setq org-agenda-include-diary (not org-agenda-include-diary))
  13631. (org-agenda-redo)
  13632. (org-agenda-set-mode-name)
  13633. (message "Diary inclusion turned %s"
  13634. (if org-agenda-include-diary "on" "off")))
  13635. (defun org-agenda-toggle-time-grid ()
  13636. "Toggle time grid in an agenda buffer."
  13637. (interactive)
  13638. (org-agenda-check-type t 'agenda)
  13639. (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
  13640. (org-agenda-redo)
  13641. (org-agenda-set-mode-name)
  13642. (message "Time-grid turned %s"
  13643. (if org-agenda-use-time-grid "on" "off")))
  13644. (defun org-agenda-set-mode-name ()
  13645. "Set the mode name to indicate all the small mode settings."
  13646. (setq mode-name
  13647. (concat "Org-Agenda"
  13648. (if (equal org-agenda-ndays 1) " Day" "")
  13649. (if (equal org-agenda-ndays 7) " Week" "")
  13650. (if org-agenda-follow-mode " Follow" "")
  13651. (if org-agenda-include-diary " Diary" "")
  13652. (if org-agenda-use-time-grid " Grid" "")
  13653. (if org-agenda-show-log " Log" "")))
  13654. (force-mode-line-update))
  13655. (defun org-agenda-post-command-hook ()
  13656. (and (eolp) (not (bolp)) (backward-char 1))
  13657. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  13658. (if (and org-agenda-follow-mode
  13659. (get-text-property (point) 'org-marker))
  13660. (org-agenda-show)))
  13661. (defun org-agenda-show-priority ()
  13662. "Show the priority of the current item.
  13663. This priority is composed of the main priority given with the [#A] cookies,
  13664. and by additional input from the age of a schedules or deadline entry."
  13665. (interactive)
  13666. (let* ((pri (get-text-property (point-at-bol) 'priority)))
  13667. (message "Priority is %d" (if pri pri -1000))))
  13668. (defun org-agenda-show-tags ()
  13669. "Show the tags applicable to the current item."
  13670. (interactive)
  13671. (let* ((tags (get-text-property (point-at-bol) 'tags)))
  13672. (if tags
  13673. (message "Tags are :%s:"
  13674. (org-no-properties (mapconcat 'identity tags ":")))
  13675. (message "No tags associated with this line"))))
  13676. (defun org-agenda-goto (&optional highlight)
  13677. "Go to the Org-mode file which contains the item at point."
  13678. (interactive)
  13679. (let* ((marker (or (get-text-property (point) 'org-marker)
  13680. (org-agenda-error)))
  13681. (buffer (marker-buffer marker))
  13682. (pos (marker-position marker)))
  13683. (switch-to-buffer-other-window buffer)
  13684. (widen)
  13685. (goto-char pos)
  13686. (when (org-mode-p)
  13687. (org-show-context 'agenda)
  13688. (save-excursion
  13689. (and (outline-next-heading)
  13690. (org-flag-heading nil)))) ; show the next heading
  13691. (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
  13692. (defun org-agenda-kill ()
  13693. "Kill the entry or subtree belonging to the current agenda entry."
  13694. (interactive)
  13695. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  13696. (let* ((marker (or (get-text-property (point) 'org-marker)
  13697. (org-agenda-error)))
  13698. (buffer (marker-buffer marker))
  13699. (pos (marker-position marker))
  13700. dbeg dend (n 0) conf)
  13701. (org-with-remote-undo buffer
  13702. (with-current-buffer buffer
  13703. (save-excursion
  13704. (goto-char pos)
  13705. (if (org-mode-p)
  13706. (setq dbeg (progn (org-back-to-heading t) (point))
  13707. dend (org-end-of-subtree t))
  13708. (setq dbeg (point-at-bol)
  13709. dend (min (point-max) (1+ (point-at-eol)))))
  13710. (goto-char dbeg)
  13711. (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
  13712. (setq conf (or (eq t org-agenda-confirm-kill)
  13713. (and (numberp org-agenda-confirm-kill)
  13714. (> n org-agenda-confirm-kill))))
  13715. (and conf
  13716. (not (y-or-n-p
  13717. (format "Delete entry with %d lines in buffer \"%s\"? "
  13718. n (buffer-name buffer))))
  13719. (error "Abort"))
  13720. (org-remove-subtree-entries-from-agenda buffer dbeg dend)
  13721. (with-current-buffer buffer (delete-region dbeg dend))
  13722. (message "Agenda item and source killed"))))
  13723. (defun org-agenda-archive ()
  13724. "Kill the entry or subtree belonging to the current agenda entry."
  13725. (interactive)
  13726. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  13727. (let* ((marker (or (get-text-property (point) 'org-marker)
  13728. (org-agenda-error)))
  13729. (buffer (marker-buffer marker))
  13730. (pos (marker-position marker)))
  13731. (org-with-remote-undo buffer
  13732. (with-current-buffer buffer
  13733. (if (org-mode-p)
  13734. (save-excursion
  13735. (goto-char pos)
  13736. (org-remove-subtree-entries-from-agenda)
  13737. (org-back-to-heading t)
  13738. (org-archive-subtree))
  13739. (error "Archiving works only in Org-mode files"))))))
  13740. (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
  13741. "Remove all lines in the agenda that correspond to a given subtree.
  13742. The subtree is the one in buffer BUF, starting at BEG and ending at END.
  13743. If this information is not given, the function uses the tree at point."
  13744. (let ((buf (or buf (current-buffer))) m p)
  13745. (save-excursion
  13746. (unless (and beg end)
  13747. (org-back-to-heading t)
  13748. (setq beg (point))
  13749. (org-end-of-subtree t)
  13750. (setq end (point)))
  13751. (set-buffer (get-buffer org-agenda-buffer-name))
  13752. (save-excursion
  13753. (goto-char (point-max))
  13754. (beginning-of-line 1)
  13755. (while (not (bobp))
  13756. (when (and (setq m (get-text-property (point) 'org-marker))
  13757. (equal buf (marker-buffer m))
  13758. (setq p (marker-position m))
  13759. (>= p beg)
  13760. (<= p end))
  13761. (let (buffer-read-only)
  13762. (delete-region (point-at-bol) (1+ (point-at-eol)))))
  13763. (beginning-of-line 0))))))
  13764. (defun org-agenda-open-link ()
  13765. "Follow the link in the current line, if any."
  13766. (interactive)
  13767. (let ((eol (point-at-eol)))
  13768. (save-excursion
  13769. (if (or (re-search-forward org-bracket-link-regexp eol t)
  13770. (re-search-forward org-angle-link-re eol t)
  13771. (re-search-forward org-plain-link-re eol t))
  13772. (call-interactively 'org-open-at-point)
  13773. (error "No link in current line")))))
  13774. (defun org-agenda-switch-to (&optional delete-other-windows)
  13775. "Go to the Org-mode file which contains the item at point."
  13776. (interactive)
  13777. (let* ((marker (or (get-text-property (point) 'org-marker)
  13778. (org-agenda-error)))
  13779. (buffer (marker-buffer marker))
  13780. (pos (marker-position marker)))
  13781. (switch-to-buffer buffer)
  13782. (and delete-other-windows (delete-other-windows))
  13783. (widen)
  13784. (goto-char pos)
  13785. (when (org-mode-p)
  13786. (org-show-context 'agenda)
  13787. (save-excursion
  13788. (and (outline-next-heading)
  13789. (org-flag-heading nil)))))) ; show the next heading
  13790. (defun org-agenda-goto-mouse (ev)
  13791. "Go to the Org-mode file which contains the item at the mouse click."
  13792. (interactive "e")
  13793. (mouse-set-point ev)
  13794. (org-agenda-goto))
  13795. (defun org-agenda-show ()
  13796. "Display the Org-mode file which contains the item at point."
  13797. (interactive)
  13798. (let ((win (selected-window)))
  13799. (org-agenda-goto t)
  13800. (select-window win)))
  13801. (defun org-agenda-recenter (arg)
  13802. "Display the Org-mode file which contains the item at point and recenter."
  13803. (interactive "P")
  13804. (let ((win (selected-window)))
  13805. (org-agenda-goto t)
  13806. (recenter arg)
  13807. (select-window win)))
  13808. (defun org-agenda-show-mouse (ev)
  13809. "Display the Org-mode file which contains the item at the mouse click."
  13810. (interactive "e")
  13811. (mouse-set-point ev)
  13812. (org-agenda-show))
  13813. (defun org-agenda-check-no-diary ()
  13814. "Check if the entry is a diary link and abort if yes."
  13815. (if (get-text-property (point) 'org-agenda-diary-link)
  13816. (org-agenda-error)))
  13817. (defun org-agenda-error ()
  13818. (error "Command not allowed in this line"))
  13819. (defun org-agenda-tree-to-indirect-buffer ()
  13820. "Show the subtree corresponding to the current entry in an indirect buffer.
  13821. This calls the command `org-tree-to-indirect-buffer' from the original
  13822. Org-mode buffer.
  13823. With numerical prefix arg ARG, go up to this level and then take that tree.
  13824. With a C-u prefix, make a separate frame for this tree (i.e. don't use the
  13825. dedicated frame)."
  13826. (interactive)
  13827. (org-agenda-check-no-diary)
  13828. (let* ((marker (or (get-text-property (point) 'org-marker)
  13829. (org-agenda-error)))
  13830. (buffer (marker-buffer marker))
  13831. (pos (marker-position marker)))
  13832. (with-current-buffer buffer
  13833. (save-excursion
  13834. (goto-char pos)
  13835. (call-interactively 'org-tree-to-indirect-buffer)))))
  13836. (defvar org-last-heading-marker (make-marker)
  13837. "Marker pointing to the headline that last changed its TODO state
  13838. by a remote command from the agenda.")
  13839. (defun org-agenda-todo (&optional arg)
  13840. "Cycle TODO state of line at point, also in Org-mode file.
  13841. This changes the line at point, all other lines in the agenda referring to
  13842. the same tree node, and the headline of the tree node in the Org-mode file."
  13843. (interactive "P")
  13844. (org-agenda-check-no-diary)
  13845. (let* ((col (current-column))
  13846. (marker (or (get-text-property (point) 'org-marker)
  13847. (org-agenda-error)))
  13848. (buffer (marker-buffer marker))
  13849. (pos (marker-position marker))
  13850. (hdmarker (get-text-property (point) 'org-hd-marker))
  13851. (buffer-read-only nil)
  13852. newhead)
  13853. (org-with-remote-undo buffer
  13854. (with-current-buffer buffer
  13855. (widen)
  13856. (goto-char pos)
  13857. (org-show-context 'agenda)
  13858. (save-excursion
  13859. (and (outline-next-heading)
  13860. (org-flag-heading nil))) ; show the next heading
  13861. (org-todo arg)
  13862. (and (bolp) (forward-char 1))
  13863. (setq newhead (org-get-heading))
  13864. (save-excursion
  13865. (org-back-to-heading)
  13866. (move-marker org-last-heading-marker (point))))
  13867. (beginning-of-line 1)
  13868. (save-excursion
  13869. (org-agenda-change-all-lines newhead hdmarker 'fixface))
  13870. (move-to-column col))))
  13871. (defun org-agenda-change-all-lines (newhead hdmarker &optional fixface)
  13872. "Change all lines in the agenda buffer which match HDMARKER.
  13873. The new content of the line will be NEWHEAD (as modified by
  13874. `org-format-agenda-item'). HDMARKER is checked with
  13875. `equal' against all `org-hd-marker' text properties in the file.
  13876. If FIXFACE is non-nil, the face of each item is modified acording to
  13877. the new TODO state."
  13878. (let* ((buffer-read-only nil)
  13879. props m pl undone-face done-face finish new dotime cat tags)
  13880. (save-excursion
  13881. (goto-char (point-max))
  13882. (beginning-of-line 1)
  13883. (while (not finish)
  13884. (setq finish (bobp))
  13885. (when (and (setq m (get-text-property (point) 'org-hd-marker))
  13886. (equal m hdmarker))
  13887. (setq props (text-properties-at (point))
  13888. dotime (get-text-property (point) 'dotime)
  13889. cat (get-text-property (point) 'category)
  13890. tags (get-text-property (point) 'tags)
  13891. new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
  13892. pl (get-text-property (point) 'prefix-length)
  13893. undone-face (get-text-property (point) 'undone-face)
  13894. done-face (get-text-property (point) 'done-face))
  13895. (move-to-column pl)
  13896. (cond
  13897. ((equal new "")
  13898. (beginning-of-line 1)
  13899. (and (looking-at ".*\n?") (replace-match "")))
  13900. ((looking-at ".*")
  13901. (replace-match new t t)
  13902. (beginning-of-line 1)
  13903. (add-text-properties (point-at-bol) (point-at-eol) props)
  13904. (when fixface
  13905. (add-text-properties
  13906. (point-at-bol) (point-at-eol)
  13907. (list 'face
  13908. (if org-last-todo-state-is-todo
  13909. undone-face done-face))))
  13910. (org-agenda-highlight-todo 'line)
  13911. (beginning-of-line 1))
  13912. (t (error "Line update did not work"))))
  13913. (beginning-of-line 0)))
  13914. (org-finalize-agenda)))
  13915. (defun org-agenda-align-tags (&optional line)
  13916. "Align all tags in agenda items to `org-agenda-align-tags-to-column'."
  13917. (let ((buffer-read-only))
  13918. (save-excursion
  13919. (goto-char (if line (point-at-bol) (point-min)))
  13920. (while (re-search-forward "\\([ \t]+\\):[a-zA-Z0-9_@:]+:[ \t]*$"
  13921. (if line (point-at-eol) nil) t)
  13922. (delete-region (match-beginning 1) (match-end 1))
  13923. (goto-char (match-beginning 1))
  13924. (insert (org-add-props
  13925. (make-string (max 1 (- org-agenda-align-tags-to-column
  13926. (current-column))) ?\ )
  13927. (text-properties-at (point))))))))
  13928. (defun org-agenda-priority-up ()
  13929. "Increase the priority of line at point, also in Org-mode file."
  13930. (interactive)
  13931. (org-agenda-priority 'up))
  13932. (defun org-agenda-priority-down ()
  13933. "Decrease the priority of line at point, also in Org-mode file."
  13934. (interactive)
  13935. (org-agenda-priority 'down))
  13936. (defun org-agenda-priority (&optional force-direction)
  13937. "Set the priority of line at point, also in Org-mode file.
  13938. This changes the line at point, all other lines in the agenda referring to
  13939. the same tree node, and the headline of the tree node in the Org-mode file."
  13940. (interactive)
  13941. (org-agenda-check-no-diary)
  13942. (let* ((marker (or (get-text-property (point) 'org-marker)
  13943. (org-agenda-error)))
  13944. (buffer (marker-buffer marker))
  13945. (pos (marker-position marker))
  13946. (hdmarker (get-text-property (point) 'org-hd-marker))
  13947. (buffer-read-only nil)
  13948. newhead)
  13949. (org-with-remote-undo buffer
  13950. (with-current-buffer buffer
  13951. (widen)
  13952. (goto-char pos)
  13953. (org-show-context 'agenda)
  13954. (save-excursion
  13955. (and (outline-next-heading)
  13956. (org-flag-heading nil))) ; show the next heading
  13957. (funcall 'org-priority force-direction)
  13958. (end-of-line 1)
  13959. (setq newhead (org-get-heading)))
  13960. (org-agenda-change-all-lines newhead hdmarker)
  13961. (beginning-of-line 1))))
  13962. (defun org-get-tags-at (&optional pos)
  13963. "Get a list of all headline tags applicable at POS.
  13964. POS defaults to point. If tags are inherited, the list contains
  13965. the targets in the same sequence as the headlines appear, i.e.
  13966. the tags of the current headline come last."
  13967. (interactive)
  13968. (let (tags)
  13969. (save-excursion
  13970. (save-restriction
  13971. (widen)
  13972. (goto-char (or pos (point)))
  13973. (save-match-data
  13974. (org-back-to-heading t)
  13975. (condition-case nil
  13976. (while t
  13977. (if (looking-at "[^\r\n]+?:\\([a-zA-Z_@0-9:]+\\):[ \t]*\\([\n\r]\\|\\'\\)")
  13978. (setq tags (append (org-split-string
  13979. (org-match-string-no-properties 1) ":")
  13980. tags)))
  13981. (or org-use-tag-inheritance (error ""))
  13982. (org-up-heading-all 1))
  13983. (error nil))))
  13984. tags)))
  13985. ;; FIXME: should fix the tags property of the agenda line.
  13986. (defun org-agenda-set-tags ()
  13987. "Set tags for the current headline."
  13988. (interactive)
  13989. (org-agenda-check-no-diary)
  13990. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  13991. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  13992. (org-agenda-error)))
  13993. (buffer (marker-buffer hdmarker))
  13994. (pos (marker-position hdmarker))
  13995. (buffer-read-only nil)
  13996. newhead)
  13997. (org-with-remote-undo buffer
  13998. (with-current-buffer buffer
  13999. (widen)
  14000. (goto-char pos)
  14001. (org-show-context 'agenda)
  14002. (save-excursion
  14003. (and (outline-next-heading)
  14004. (org-flag-heading nil))) ; show the next heading
  14005. (call-interactively 'org-set-tags)
  14006. (end-of-line 1)
  14007. (setq newhead (org-get-heading)))
  14008. (org-agenda-change-all-lines newhead hdmarker)
  14009. (beginning-of-line 1))))
  14010. (defun org-agenda-toggle-archive-tag ()
  14011. "Toggle the archive tag for the current entry."
  14012. (interactive)
  14013. (org-agenda-check-no-diary)
  14014. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  14015. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  14016. (org-agenda-error)))
  14017. (buffer (marker-buffer hdmarker))
  14018. (pos (marker-position hdmarker))
  14019. (buffer-read-only nil)
  14020. newhead)
  14021. (org-with-remote-undo buffer
  14022. (with-current-buffer buffer
  14023. (widen)
  14024. (goto-char pos)
  14025. (org-show-context 'agenda)
  14026. (save-excursion
  14027. (and (outline-next-heading)
  14028. (org-flag-heading nil))) ; show the next heading
  14029. (call-interactively 'org-toggle-archive-tag)
  14030. (end-of-line 1)
  14031. (setq newhead (org-get-heading)))
  14032. (org-agenda-change-all-lines newhead hdmarker)
  14033. (beginning-of-line 1))))
  14034. (defun org-agenda-date-later (arg &optional what)
  14035. "Change the date of this item to one day later."
  14036. (interactive "p")
  14037. (org-agenda-check-type t 'agenda 'timeline)
  14038. (org-agenda-check-no-diary)
  14039. (let* ((marker (or (get-text-property (point) 'org-marker)
  14040. (org-agenda-error)))
  14041. (buffer (marker-buffer marker))
  14042. (pos (marker-position marker)))
  14043. (org-with-remote-undo buffer
  14044. (with-current-buffer buffer
  14045. (widen)
  14046. (goto-char pos)
  14047. (if (not (org-at-timestamp-p))
  14048. (error "Cannot find time stamp"))
  14049. (org-timestamp-change arg (or what 'day)))
  14050. (org-agenda-show-new-time marker org-last-changed-timestamp))
  14051. (message "Time stamp changed to %s" org-last-changed-timestamp)))
  14052. (defun org-agenda-date-earlier (arg &optional what)
  14053. "Change the date of this item to one day earlier."
  14054. (interactive "p")
  14055. (org-agenda-date-later (- arg) what))
  14056. (defun org-agenda-show-new-time (marker stamp)
  14057. "Show new date stamp via text properties."
  14058. ;; We use text properties to make this undoable
  14059. (let ((buffer-read-only nil))
  14060. (setq stamp (concat " => " stamp))
  14061. (save-excursion
  14062. (goto-char (point-max))
  14063. (while (not (bobp))
  14064. (when (equal marker (get-text-property (point) 'org-marker))
  14065. (move-to-column (- (window-width) (length stamp)) t)
  14066. (if (featurep 'xemacs)
  14067. ;; Use `duplicable' property to trigger undo recording
  14068. (let ((ex (make-extent nil nil))
  14069. (gl (make-glyph stamp)))
  14070. (set-glyph-face gl 'secondary-selection)
  14071. (set-extent-properties
  14072. ex (list 'invisible t 'end-glyph gl 'duplicable t))
  14073. (insert-extent ex (1- (point)) (point-at-eol)))
  14074. (add-text-properties
  14075. (1- (point)) (point-at-eol)
  14076. (list 'display (org-add-props stamp nil
  14077. 'face 'secondary-selection))))
  14078. (beginning-of-line 1))
  14079. (beginning-of-line 0)))))
  14080. (defun org-agenda-date-prompt (arg)
  14081. "Change the date of this item. Date is prompted for, with default today.
  14082. The prefix ARG is passed to the `org-time-stamp' command and can therefore
  14083. be used to request time specification in the time stamp."
  14084. (interactive "P")
  14085. (org-agenda-check-type t 'agenda 'timeline)
  14086. (org-agenda-check-no-diary)
  14087. (let* ((marker (or (get-text-property (point) 'org-marker)
  14088. (org-agenda-error)))
  14089. (buffer (marker-buffer marker))
  14090. (pos (marker-position marker)))
  14091. (org-with-remote-undo buffer
  14092. (with-current-buffer buffer
  14093. (widen)
  14094. (goto-char pos)
  14095. (if (not (org-at-timestamp-p))
  14096. (error "Cannot find time stamp"))
  14097. (org-time-stamp arg)
  14098. (message "Time stamp changed to %s" org-last-changed-timestamp)))))
  14099. (defun org-agenda-schedule (arg)
  14100. "Schedule the item at point."
  14101. (interactive "P")
  14102. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  14103. (org-agenda-check-no-diary)
  14104. (let* ((marker (or (get-text-property (point) 'org-marker)
  14105. (org-agenda-error)))
  14106. (buffer (marker-buffer marker))
  14107. (pos (marker-position marker))
  14108. (org-insert-labeled-timestamps-at-point nil)
  14109. ts)
  14110. (org-with-remote-undo buffer
  14111. (with-current-buffer buffer
  14112. (widen)
  14113. (goto-char pos)
  14114. (setq ts (org-schedule))
  14115. (message "Item scheduled for %s" ts)))))
  14116. (defun org-agenda-deadline (arg)
  14117. "Schedule the item at point."
  14118. (interactive "P")
  14119. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  14120. (org-agenda-check-no-diary)
  14121. (let* ((marker (or (get-text-property (point) 'org-marker)
  14122. (org-agenda-error)))
  14123. (buffer (marker-buffer marker))
  14124. (pos (marker-position marker))
  14125. (org-insert-labeled-timestamps-at-point nil)
  14126. ts)
  14127. (org-with-remote-undo buffer
  14128. (with-current-buffer buffer
  14129. (widen)
  14130. (goto-char pos)
  14131. (setq ts (org-deadline))
  14132. (message "Deadline for this item set to %s" ts)))))
  14133. (defun org-get-heading ()
  14134. "Return the heading of the current entry, without the stars."
  14135. (save-excursion
  14136. (and (memq (char-before) '(?\n ?\r)) (skip-chars-forward "^\n\r"))
  14137. (if (and (re-search-backward "[\r\n]\\*" nil t)
  14138. (looking-at "[\r\n]\\*+[ \t]+\\([^\r\n]*\\)"))
  14139. (match-string 1)
  14140. "")))
  14141. (defun org-agenda-clock-in (&optional arg)
  14142. "Start the clock on the currently selected item."
  14143. (interactive "P")
  14144. (org-agenda-check-no-diary)
  14145. (let* ((marker (or (get-text-property (point) 'org-marker)
  14146. (org-agenda-error)))
  14147. (pos (marker-position marker)))
  14148. (org-with-remote-undo (marker-buffer marker)
  14149. (with-current-buffer (marker-buffer marker)
  14150. (widen)
  14151. (goto-char pos)
  14152. (org-clock-in)))))
  14153. (defun org-agenda-clock-out (&optional arg)
  14154. "Stop the currently running clock."
  14155. (interactive "P")
  14156. (unless (marker-buffer org-clock-marker)
  14157. (error "No running clock"))
  14158. (org-with-remote-undo (marker-buffer org-clock-marker)
  14159. (org-clock-out)))
  14160. (defun org-agenda-clock-cancel (&optional arg)
  14161. "Cancel the currently running clock."
  14162. (interactive "P")
  14163. (unless (marker-buffer org-clock-marker)
  14164. (error "No running clock"))
  14165. (org-with-remote-undo (marker-buffer org-clock-marker)
  14166. (org-clock-cancel)))
  14167. (defun org-agenda-diary-entry ()
  14168. "Make a diary entry, like the `i' command from the calendar.
  14169. All the standard commands work: block, weekly etc."
  14170. (interactive)
  14171. (org-agenda-check-type t 'agenda 'timeline)
  14172. (require 'diary-lib)
  14173. (let* ((char (progn
  14174. (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
  14175. (read-char-exclusive)))
  14176. (cmd (cdr (assoc char
  14177. '((?d . insert-diary-entry)
  14178. (?w . insert-weekly-diary-entry)
  14179. (?m . insert-monthly-diary-entry)
  14180. (?y . insert-yearly-diary-entry)
  14181. (?a . insert-anniversary-diary-entry)
  14182. (?b . insert-block-diary-entry)
  14183. (?c . insert-cyclic-diary-entry)))))
  14184. (oldf (symbol-function 'calendar-cursor-to-date))
  14185. ; (buf (get-file-buffer (substitute-in-file-name diary-file)))
  14186. (point (point))
  14187. (mark (or (mark t) (point))))
  14188. (unless cmd
  14189. (error "No command associated with <%c>" char))
  14190. (unless (and (get-text-property point 'day)
  14191. (or (not (equal ?b char))
  14192. (get-text-property mark 'day)))
  14193. (error "Don't know which date to use for diary entry"))
  14194. ;; We implement this by hacking the `calendar-cursor-to-date' function
  14195. ;; and the `calendar-mark-ring' variable. Saves a lot of code.
  14196. (let ((calendar-mark-ring
  14197. (list (calendar-gregorian-from-absolute
  14198. (or (get-text-property mark 'day)
  14199. (get-text-property point 'day))))))
  14200. (unwind-protect
  14201. (progn
  14202. (fset 'calendar-cursor-to-date
  14203. (lambda (&optional error)
  14204. (calendar-gregorian-from-absolute
  14205. (get-text-property point 'day))))
  14206. (call-interactively cmd))
  14207. (fset 'calendar-cursor-to-date oldf)))))
  14208. (defun org-agenda-execute-calendar-command (cmd)
  14209. "Execute a calendar command from the agenda, with the date associated to
  14210. the cursor position."
  14211. (org-agenda-check-type t 'agenda 'timeline)
  14212. (require 'diary-lib)
  14213. (unless (get-text-property (point) 'day)
  14214. (error "Don't know which date to use for calendar command"))
  14215. (let* ((oldf (symbol-function 'calendar-cursor-to-date))
  14216. (point (point))
  14217. (date (calendar-gregorian-from-absolute
  14218. (get-text-property point 'day)))
  14219. (displayed-day (extract-calendar-day date))
  14220. (displayed-month (extract-calendar-month date))
  14221. (displayed-year (extract-calendar-year date)))
  14222. (unwind-protect
  14223. (progn
  14224. (fset 'calendar-cursor-to-date
  14225. (lambda (&optional error)
  14226. (calendar-gregorian-from-absolute
  14227. (get-text-property point 'day))))
  14228. (call-interactively cmd))
  14229. (fset 'calendar-cursor-to-date oldf))))
  14230. (defun org-agenda-phases-of-moon ()
  14231. "Display the phases of the moon for the 3 months around the cursor date."
  14232. (interactive)
  14233. (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
  14234. (defun org-agenda-holidays ()
  14235. "Display the holidays for the 3 months around the cursor date."
  14236. (interactive)
  14237. (org-agenda-execute-calendar-command 'list-calendar-holidays))
  14238. (defun org-agenda-sunrise-sunset (arg)
  14239. "Display sunrise and sunset for the cursor date.
  14240. Latitude and longitude can be specified with the variables
  14241. `calendar-latitude' and `calendar-longitude'. When called with prefix
  14242. argument, latitude and longitude will be prompted for."
  14243. (interactive "P")
  14244. (let ((calendar-longitude (if arg nil calendar-longitude))
  14245. (calendar-latitude (if arg nil calendar-latitude))
  14246. (calendar-location-name
  14247. (if arg "the given coordinates" calendar-location-name)))
  14248. (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
  14249. (defun org-agenda-goto-calendar ()
  14250. "Open the Emacs calendar with the date at the cursor."
  14251. (interactive)
  14252. (org-agenda-check-type t 'agenda 'timeline)
  14253. (let* ((day (or (get-text-property (point) 'day)
  14254. (error "Don't know which date to open in calendar")))
  14255. (date (calendar-gregorian-from-absolute day))
  14256. (calendar-move-hook nil)
  14257. (view-calendar-holidays-initially nil)
  14258. (view-diary-entries-initially nil))
  14259. (calendar)
  14260. (calendar-goto-date date)))
  14261. (defun org-calendar-goto-agenda ()
  14262. "Compute the Org-mode agenda for the calendar date displayed at the cursor.
  14263. This is a command that has to be installed in `calendar-mode-map'."
  14264. (interactive)
  14265. (org-agenda-list nil (calendar-absolute-from-gregorian
  14266. (calendar-cursor-to-date))
  14267. nil))
  14268. (defun org-agenda-convert-date ()
  14269. (interactive)
  14270. (org-agenda-check-type t 'agenda 'timeline)
  14271. (let ((day (get-text-property (point) 'day))
  14272. date s)
  14273. (unless day
  14274. (error "Don't know which date to convert"))
  14275. (setq date (calendar-gregorian-from-absolute day))
  14276. (setq s (concat
  14277. "Gregorian: " (calendar-date-string date) "\n"
  14278. "ISO: " (calendar-iso-date-string date) "\n"
  14279. "Day of Yr: " (calendar-day-of-year-string date) "\n"
  14280. "Julian: " (calendar-julian-date-string date) "\n"
  14281. "Astron. JD: " (calendar-astro-date-string date)
  14282. " (Julian date number at noon UTC)\n"
  14283. "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
  14284. "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
  14285. "French: " (calendar-french-date-string date) "\n"
  14286. "Mayan: " (calendar-mayan-date-string date) "\n"
  14287. "Coptic: " (calendar-coptic-date-string date) "\n"
  14288. "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
  14289. "Persian: " (calendar-persian-date-string date) "\n"
  14290. "Chinese: " (calendar-chinese-date-string date) "\n"))
  14291. (with-output-to-temp-buffer "*Dates*"
  14292. (princ s))
  14293. (if (fboundp 'fit-window-to-buffer)
  14294. (fit-window-to-buffer (get-buffer-window "*Dates*")))))
  14295. ;;;; Embedded LaTeX
  14296. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  14297. "Keymap for the minor `org-cdlatex-mode'.")
  14298. (define-key org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  14299. (define-key org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  14300. (define-key org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  14301. (define-key org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  14302. (define-key org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  14303. (defvar org-cdlatex-texmathp-advice-is-done nil
  14304. "Flag remembering if we have applied the advice to texmathp already.")
  14305. (define-minor-mode org-cdlatex-mode
  14306. "Toggle the minor `org-cdlatex-mode'.
  14307. This mode supports entering LaTeX environment and math in LaTeX fragments
  14308. in Org-mode.
  14309. \\{org-cdlatex-mode-map}"
  14310. nil " OCDL" nil
  14311. (when org-cdlatex-mode (require 'cdlatex))
  14312. (unless org-cdlatex-texmathp-advice-is-done
  14313. (setq org-cdlatex-texmathp-advice-is-done t)
  14314. (defadvice texmathp (around org-math-always-on activate)
  14315. "Always return t in org-mode buffers.
  14316. This is because we want to insert math symbols without dollars even outside
  14317. the LaTeX math segments. If Orgmode thinks that point is actually inside
  14318. en embedded LaTeX fragement, let texmathp do its job.
  14319. \\[org-cdlatex-mode-map]"
  14320. (interactive)
  14321. (let (p)
  14322. (cond
  14323. ((not (org-mode-p)) ad-do-it)
  14324. ((eq this-command 'cdlatex-math-symbol)
  14325. (setq ad-return-value t
  14326. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  14327. (t
  14328. (let ((p (org-inside-LaTeX-fragment-p)))
  14329. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  14330. (setq ad-return-value t
  14331. texmathp-why '("Org-mode embedded math" . 0))
  14332. (if p ad-do-it)))))))))
  14333. (defun turn-on-org-cdlatex ()
  14334. "Unconditionally turn on `org-cdlatex-mode'."
  14335. (org-cdlatex-mode 1))
  14336. (defun org-inside-LaTeX-fragment-p ()
  14337. "Test if point is inside a LaTeX fragment.
  14338. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  14339. sequence appearing also before point.
  14340. Even though the matchers for math are configurable, this function assumes
  14341. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  14342. delimiters are skipped when they have been removed by customization.
  14343. The return value is nil, or a cons cell with the delimiter and
  14344. and the position of this delimiter.
  14345. This function does a reasonably good job, but can locally be fooled by
  14346. for example currency specifications. For example it will assume being in
  14347. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  14348. fragments that are properly closed, but during editing, we have to live
  14349. with the uncertainty caused by missing closing delimiters. This function
  14350. looks only before point, not after."
  14351. (catch 'exit
  14352. (let ((pos (point))
  14353. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  14354. (lim (progn
  14355. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  14356. (point)))
  14357. dd-on str (start 0) m re)
  14358. (goto-char pos)
  14359. (when dodollar
  14360. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  14361. re (nth 1 (assoc "$" org-latex-regexps)))
  14362. (while (string-match re str start)
  14363. (cond
  14364. ((= (match-end 0) (length str))
  14365. (throw 'exit (cons "$" (+ lim (match-beginning 0)))))
  14366. ((= (match-end 0) (- (length str) 5))
  14367. (throw 'exit nil))
  14368. (t (setq start (match-end 0))))))
  14369. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  14370. (goto-char pos)
  14371. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  14372. (and (match-beginning 2) (throw 'exit nil))
  14373. ;; count $$
  14374. (while (re-search-backward "\\$\\$" lim t)
  14375. (setq dd-on (not dd-on)))
  14376. (goto-char pos)
  14377. (if dd-on (cons "$$" m))))))
  14378. (defun org-try-cdlatex-tab ()
  14379. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  14380. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  14381. - inside a LaTeX fragment, or
  14382. - after the first word in a line, where an abbreviation expansion could
  14383. insert a LaTeX environment."
  14384. (when org-cdlatex-mode
  14385. (cond
  14386. ((save-excursion
  14387. (skip-chars-backward "a-zA-Z0-9*")
  14388. (skip-chars-backward " \t")
  14389. (bolp))
  14390. (cdlatex-tab) t)
  14391. ((org-inside-LaTeX-fragment-p)
  14392. (cdlatex-tab) t)
  14393. (t nil))))
  14394. (defun org-cdlatex-underscore-caret (&optional arg)
  14395. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  14396. Revert to the normal definition outside of these fragments."
  14397. (interactive "P")
  14398. (if (org-inside-LaTeX-fragment-p)
  14399. (call-interactively 'cdlatex-sub-superscript)
  14400. (let (org-cdlatex-mode)
  14401. (call-interactively (key-binding (vector last-input-event))))))
  14402. (defun org-cdlatex-math-modify (&optional arg)
  14403. "Execute `cdlatex-math-modify' in LaTeX fragments.
  14404. Revert to the normal definition outside of these fragments."
  14405. (interactive "P")
  14406. (if (org-inside-LaTeX-fragment-p)
  14407. (call-interactively 'cdlatex-math-modify)
  14408. (let (org-cdlatex-mode)
  14409. (call-interactively (key-binding (vector last-input-event))))))
  14410. (defvar org-latex-fragment-image-overlays nil
  14411. "List of overlays carrying the images of latex fragments.")
  14412. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  14413. (defun org-remove-latex-fragment-image-overlays ()
  14414. "Remove all overlays with LaTeX fragment images in current buffer."
  14415. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  14416. (setq org-latex-fragment-image-overlays nil))
  14417. (defun org-preview-latex-fragment (&optional subtree)
  14418. "Preview the LaTeX fragment at point, or all locally or globally.
  14419. If the cursor is in a LaTeX fragment, create the image and overlay
  14420. it over the source code. If there is no fragment at point, display
  14421. all fragments in the current text, from one headline to the next. With
  14422. prefix SUBTREE, display all fragments in the current subtree. With a
  14423. double prefix `C-u C-u', or when the cursor is before the first headline,
  14424. display all fragments in the buffer.
  14425. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  14426. (interactive "P")
  14427. (org-remove-latex-fragment-image-overlays)
  14428. (save-excursion
  14429. (save-restriction
  14430. (let (beg end at msg)
  14431. (cond
  14432. ((or (equal subtree '(16))
  14433. (not (save-excursion
  14434. (re-search-backward (concat "^" outline-regexp) nil t))))
  14435. (setq beg (point-min) end (point-max)
  14436. msg "Creating images for buffer...%s"))
  14437. ((equal subtree '(4))
  14438. (org-back-to-heading)
  14439. (setq beg (point) end (org-end-of-subtree t)
  14440. msg "Creating images for subtree...%s"))
  14441. (t
  14442. (if (setq at (org-inside-LaTeX-fragment-p))
  14443. (goto-char (max (point-min) (- (cdr at) 2)))
  14444. (org-back-to-heading))
  14445. (setq beg (point) end (progn (outline-next-heading) (point))
  14446. msg (if at "Creating image...%s"
  14447. "Creating images for entry...%s"))))
  14448. (message msg "")
  14449. (narrow-to-region beg end)
  14450. (org-format-latex
  14451. (concat "ltxpng/" (file-name-sans-extension
  14452. (file-name-nondirectory
  14453. buffer-file-name)))
  14454. default-directory 'overlays msg at)
  14455. (message msg "done. Use `C-c C-c' to remove images.")))))
  14456. (defvar org-latex-regexps
  14457. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  14458. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  14459. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  14460. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  14461. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  14462. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  14463. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  14464. "Regular expressions for matching embedded LaTeX.")
  14465. (defun org-format-latex (prefix &optional dir overlays msg at)
  14466. "Replace LaTeX fragments with links to an image, and produce images."
  14467. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  14468. (let* ((prefixnodir (file-name-nondirectory prefix))
  14469. (absprefix (expand-file-name prefix dir))
  14470. (todir (file-name-directory absprefix))
  14471. (opt org-format-latex-options)
  14472. (matchers (plist-get opt :matchers))
  14473. (re-list org-latex-regexps)
  14474. (cnt 0) txt link beg end re e checkdir
  14475. m n block linkfile movefile ov)
  14476. ;; Check if there are old images files with this prefix, and remove them
  14477. (when (file-directory-p todir)
  14478. (mapc 'delete-file
  14479. (directory-files
  14480. todir 'full
  14481. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  14482. ;; Check the different regular expressions
  14483. (while (setq e (pop re-list))
  14484. (setq m (car e) re (nth 1 e) n (nth 2 e)
  14485. block (if (nth 3 e) "\n\n" ""))
  14486. (when (member m matchers)
  14487. (goto-char (point-min))
  14488. (while (re-search-forward re nil t)
  14489. (when (or (not at) (equal (cdr at) (match-beginning n)))
  14490. (setq txt (match-string n)
  14491. beg (match-beginning n) end (match-end n)
  14492. cnt (1+ cnt)
  14493. linkfile (format "%s_%04d.png" prefix cnt)
  14494. movefile (format "%s_%04d.png" absprefix cnt)
  14495. link (concat block "[[file:" linkfile "]]" block))
  14496. (if msg (message msg cnt))
  14497. (goto-char beg)
  14498. (unless checkdir ; make sure the directory exists
  14499. (setq checkdir t)
  14500. (or (file-directory-p todir) (make-directory todir)))
  14501. (org-create-formula-image
  14502. txt movefile opt)
  14503. (if overlays
  14504. (progn
  14505. (setq ov (org-make-overlay beg end))
  14506. (if (featurep 'xemacs)
  14507. (progn
  14508. (org-overlay-put ov 'invisible t)
  14509. (org-overlay-put
  14510. ov 'end-glyph
  14511. (make-glyph (vector 'png :file movefile))))
  14512. (org-overlay-put
  14513. ov 'display
  14514. (list 'image :type 'png :file movefile :ascent 'center)))
  14515. (push ov org-latex-fragment-image-overlays)
  14516. (goto-char end))
  14517. (delete-region beg end)
  14518. (insert link))))))))
  14519. ;; This function borrows from Ganesh Swami's latex2png.el
  14520. (defun org-create-formula-image (string tofile options)
  14521. (let* ((tmpdir (if (featurep 'xemacs)
  14522. (temp-directory)
  14523. temporary-file-directory))
  14524. (texfilebase (make-temp-name
  14525. (expand-file-name "orgtex" tmpdir)))
  14526. ;(texfilebase (make-temp-file "orgtex"))
  14527. ; (dummy (delete-file texfilebase))
  14528. (texfile (concat texfilebase ".tex"))
  14529. (dvifile (concat texfilebase ".dvi"))
  14530. (pngfile (concat texfilebase ".png"))
  14531. (scale (number-to-string (* 1000 (or (plist-get options :scale) 1.0))))
  14532. (fg (or (plist-get options :foreground) "Black"))
  14533. (bg (or (plist-get options :background) "Transparent")))
  14534. (with-temp-file texfile
  14535. (insert "\\documentclass{article}
  14536. \\usepackage{fullpage}
  14537. \\usepackage{amssymb}
  14538. \\usepackage[usenames]{color}
  14539. \\usepackage{amsmath}
  14540. \\usepackage{latexsym}
  14541. \\usepackage[mathscr]{eucal}
  14542. \\pagestyle{empty}
  14543. \\begin{document}\n" string "\n\\end{document}\n"))
  14544. (let ((dir default-directory))
  14545. (condition-case nil
  14546. (progn
  14547. (cd tmpdir)
  14548. (call-process "latex" nil nil nil texfile))
  14549. (error nil))
  14550. (cd dir))
  14551. (if (not (file-exists-p dvifile))
  14552. (progn (message "Failed to create dvi file from %s" texfile) nil)
  14553. (call-process "dvipng" nil nil nil
  14554. "-E" "-fg" fg "-bg" bg
  14555. "-x" scale "-y" scale "-T" "tight"
  14556. "-o" pngfile
  14557. dvifile)
  14558. (if (not (file-exists-p pngfile))
  14559. (progn (message "Failed to create png file from %s" texfile) nil)
  14560. ;; Use the requested file name and clean up
  14561. (copy-file pngfile tofile 'replace)
  14562. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  14563. (delete-file (concat texfilebase e)))
  14564. pngfile))))
  14565. ;;;; Exporting
  14566. ;;; Variables, constants, and parameter plists
  14567. (defconst org-level-max 20)
  14568. (defvar org-export-html-preamble nil
  14569. "Preamble, to be inserted just after <body>. Set by publishing functions.")
  14570. (defvar org-export-html-postamble nil
  14571. "Preamble, to be inserted just before </body>. Set by publishing functions.")
  14572. (defvar org-export-html-auto-preamble t
  14573. "Should default preamble be inserted? Set by publishing functions.")
  14574. (defvar org-export-html-auto-postamble t
  14575. "Should default postamble be inserted? Set by publishing functions.")
  14576. (defvar org-current-export-file nil) ; dynamically scoped parameter
  14577. (defvar org-current-export-dir nil) ; dynamically scoped parameter
  14578. (defconst org-export-plist-vars
  14579. '((:language . org-export-default-language)
  14580. (:customtime . org-display-custom-times)
  14581. (:headline-levels . org-export-headline-levels)
  14582. (:section-numbers . org-export-with-section-numbers)
  14583. (:table-of-contents . org-export-with-toc)
  14584. (:archived-trees . org-export-with-archived-trees)
  14585. (:emphasize . org-export-with-emphasize)
  14586. (:sub-superscript . org-export-with-sub-superscripts)
  14587. (:TeX-macros . org-export-with-TeX-macros)
  14588. (:LaTeX-fragments . org-export-with-LaTeX-fragments)
  14589. (:fixed-width . org-export-with-fixed-width)
  14590. (:timestamps . org-export-with-timestamps)
  14591. (:tables . org-export-with-tables)
  14592. (:table-auto-headline . org-export-highlight-first-table-line)
  14593. (:style . org-export-html-style)
  14594. (:convert-org-links . org-export-html-link-org-files-as-html)
  14595. (:inline-images . org-export-html-inline-images)
  14596. (:expand-quoted-html . org-export-html-expand)
  14597. (:timestamp . org-export-html-with-timestamp)
  14598. (:publishing-directory . org-export-publishing-directory)
  14599. (:preamble . org-export-html-preamble)
  14600. (:postamble . org-export-html-postamble)
  14601. (:auto-preamble . org-export-html-auto-preamble)
  14602. (:auto-postamble . org-export-html-auto-postamble)
  14603. (:author . user-full-name)
  14604. (:email . user-mail-address)))
  14605. (defun org-default-export-plist ()
  14606. "Return the property list with default settings for the export variables."
  14607. (let ((l org-export-plist-vars) rtn e)
  14608. (while (setq e (pop l))
  14609. (setq rtn (cons (car e) (cons (symbol-value (cdr e)) rtn))))
  14610. rtn))
  14611. (defun org-infile-export-plist ()
  14612. "Return the property list with file-local settings for export."
  14613. (save-excursion
  14614. (goto-char 0)
  14615. (let ((re (org-make-options-regexp
  14616. '("TITLE" "AUTHOR" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE")))
  14617. p key val text options)
  14618. (while (re-search-forward re nil t)
  14619. (setq key (org-match-string-no-properties 1)
  14620. val (org-match-string-no-properties 2))
  14621. (cond
  14622. ((string-equal key "TITLE") (setq p (plist-put p :title val)))
  14623. ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
  14624. ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
  14625. ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
  14626. ((string-equal key "TEXT")
  14627. (setq text (if text (concat text "\n" val) val)))
  14628. ((string-equal key "OPTIONS") (setq options val))))
  14629. (setq p (plist-put p :text text))
  14630. (when options
  14631. (let ((op '(("H" . :headline-levels)
  14632. ("num" . :section-numbers)
  14633. ("toc" . :table-of-contents)
  14634. ("\\n" . :preserve-breaks)
  14635. ("@" . :expand-quoted-html)
  14636. (":" . :fixed-width)
  14637. ("|" . :tables)
  14638. ("^" . :sub-superscript)
  14639. ("*" . :emphasize)
  14640. ("TeX" . :TeX-macros)
  14641. ("LaTeX" . :LaTeX-fragments)))
  14642. o)
  14643. (while (setq o (pop op))
  14644. (if (string-match (concat (regexp-quote (car o))
  14645. ":\\([^ \t\n\r;,.]*\\)")
  14646. options)
  14647. (setq p (plist-put p (cdr o)
  14648. (car (read-from-string
  14649. (match-string 1 options)))))))))
  14650. p)))
  14651. (defun org-combine-plists (&rest plists)
  14652. "Create a single property list from all plists in PLISTS.
  14653. The process starts by copying the last list, and then setting properties
  14654. from the other lists. Settings in the first list are the most significant
  14655. ones and overrule settings in the other lists."
  14656. (let ((rtn (copy-sequence (pop plists)))
  14657. p v ls)
  14658. (while plists
  14659. (setq ls (pop plists))
  14660. (while ls
  14661. (setq p (pop ls) v (pop ls))
  14662. (setq rtn (plist-put rtn p v))))
  14663. rtn))
  14664. (defun org-export-directory (type plist)
  14665. (let* ((val (plist-get plist :publishing-directory))
  14666. (dir (if (listp val)
  14667. (or (cdr (assoc type val)) ".")
  14668. val)))
  14669. dir))
  14670. (defun org-export-find-first-heading-line (list)
  14671. "Remove all lines from LIST which are before the first headline."
  14672. (let ((orig-list list)
  14673. (re (concat "^" outline-regexp)))
  14674. (while (and list
  14675. (not (string-match re (car list))))
  14676. (pop list))
  14677. (or list orig-list)))
  14678. (defun org-skip-comments (lines)
  14679. "Skip lines starting with \"#\" and subtrees starting with COMMENT."
  14680. (let ((re1 (concat "^\\(\\*+\\)[ \t]+" org-comment-string))
  14681. (re2 "^\\(\\*+\\)[ \t\n\r]")
  14682. rtn line level)
  14683. (while (setq line (pop lines))
  14684. (cond
  14685. ((and (string-match re1 line)
  14686. (setq level (- (match-end 1) (match-beginning 1))))
  14687. ;; Beginning of a COMMENT subtree. Skip it.
  14688. (while (and (setq line (pop lines))
  14689. (or (not (string-match re2 line))
  14690. (> (- (match-end 1) (match-beginning 1)) level))))
  14691. (setq lines (cons line lines)))
  14692. ((string-match "^#" line)
  14693. ;; an ordinary comment line
  14694. )
  14695. ((and org-export-table-remove-special-lines
  14696. (string-match "^[ \t]*|" line)
  14697. (or (string-match "^[ \t]*| *[!_^] *|" line)
  14698. (and (string-match "| *<[0-9]+> *|" line)
  14699. (not (string-match "| *[^ <|]" line)))))
  14700. ;; a special table line that should be removed
  14701. )
  14702. (t (setq rtn (cons line rtn)))))
  14703. (nreverse rtn)))
  14704. (defun org-export (&optional arg)
  14705. (interactive)
  14706. (let ((help "[t] insert the export option template
  14707. \[v] limit export to visible part of outline tree
  14708. \[a] export as ASCII
  14709. \[h] export as HTML
  14710. \[b] export as HTML and browse immediately
  14711. \[x] export as XOXO
  14712. \[i] export current file as iCalendar file
  14713. \[I] export all agenda files as iCalendar files
  14714. \[c] export agenda files into combined iCalendar file
  14715. \[F] publish current file
  14716. \[P] publish current project
  14717. \[X] publish... (project will be prompted for)
  14718. \[A] publish all projects")
  14719. (cmds
  14720. '((?t . org-insert-export-options-template)
  14721. (?v . org-export-visible)
  14722. (?a . org-export-as-ascii)
  14723. (?h . org-export-as-html)
  14724. (?b . org-export-as-html-and-open)
  14725. (?x . org-export-as-xoxo)
  14726. (?i . org-export-icalendar-this-file)
  14727. (?I . org-export-icalendar-all-agenda-files)
  14728. (?c . org-export-icalendar-combine-agenda-files)
  14729. (?F . org-publish-current-file)
  14730. (?P . org-publish-current-project)
  14731. (?X . org-publish)
  14732. (?A . org-publish-all)))
  14733. r1 r2 ass)
  14734. (save-window-excursion
  14735. (delete-other-windows)
  14736. (with-output-to-temp-buffer "*Org Export/Publishing Help*"
  14737. (princ help))
  14738. (message "Select command: ")
  14739. (setq r1 (read-char-exclusive)))
  14740. (setq r2 (if (< r1 27) (+ r1 96) r1))
  14741. (if (setq ass (assq r2 cmds))
  14742. (call-interactively (cdr ass))
  14743. (error "No command associated with key %c" r1))))
  14744. (defconst org-html-entities
  14745. '(("nbsp")
  14746. ("iexcl")
  14747. ("cent")
  14748. ("pound")
  14749. ("curren")
  14750. ("yen")
  14751. ("brvbar")
  14752. ("sect")
  14753. ("uml")
  14754. ("copy")
  14755. ("ordf")
  14756. ("laquo")
  14757. ("not")
  14758. ("shy")
  14759. ("reg")
  14760. ("macr")
  14761. ("deg")
  14762. ("plusmn")
  14763. ("sup2")
  14764. ("sup3")
  14765. ("acute")
  14766. ("micro")
  14767. ("para")
  14768. ("middot")
  14769. ("odot"."o")
  14770. ("star"."*")
  14771. ("cedil")
  14772. ("sup1")
  14773. ("ordm")
  14774. ("raquo")
  14775. ("frac14")
  14776. ("frac12")
  14777. ("frac34")
  14778. ("iquest")
  14779. ("Agrave")
  14780. ("Aacute")
  14781. ("Acirc")
  14782. ("Atilde")
  14783. ("Auml")
  14784. ("Aring") ("AA"."&Aring;")
  14785. ("AElig")
  14786. ("Ccedil")
  14787. ("Egrave")
  14788. ("Eacute")
  14789. ("Ecirc")
  14790. ("Euml")
  14791. ("Igrave")
  14792. ("Iacute")
  14793. ("Icirc")
  14794. ("Iuml")
  14795. ("ETH")
  14796. ("Ntilde")
  14797. ("Ograve")
  14798. ("Oacute")
  14799. ("Ocirc")
  14800. ("Otilde")
  14801. ("Ouml")
  14802. ("times")
  14803. ("Oslash")
  14804. ("Ugrave")
  14805. ("Uacute")
  14806. ("Ucirc")
  14807. ("Uuml")
  14808. ("Yacute")
  14809. ("THORN")
  14810. ("szlig")
  14811. ("agrave")
  14812. ("aacute")
  14813. ("acirc")
  14814. ("atilde")
  14815. ("auml")
  14816. ("aring")
  14817. ("aelig")
  14818. ("ccedil")
  14819. ("egrave")
  14820. ("eacute")
  14821. ("ecirc")
  14822. ("euml")
  14823. ("igrave")
  14824. ("iacute")
  14825. ("icirc")
  14826. ("iuml")
  14827. ("eth")
  14828. ("ntilde")
  14829. ("ograve")
  14830. ("oacute")
  14831. ("ocirc")
  14832. ("otilde")
  14833. ("ouml")
  14834. ("divide")
  14835. ("oslash")
  14836. ("ugrave")
  14837. ("uacute")
  14838. ("ucirc")
  14839. ("uuml")
  14840. ("yacute")
  14841. ("thorn")
  14842. ("yuml")
  14843. ("fnof")
  14844. ("Alpha")
  14845. ("Beta")
  14846. ("Gamma")
  14847. ("Delta")
  14848. ("Epsilon")
  14849. ("Zeta")
  14850. ("Eta")
  14851. ("Theta")
  14852. ("Iota")
  14853. ("Kappa")
  14854. ("Lambda")
  14855. ("Mu")
  14856. ("Nu")
  14857. ("Xi")
  14858. ("Omicron")
  14859. ("Pi")
  14860. ("Rho")
  14861. ("Sigma")
  14862. ("Tau")
  14863. ("Upsilon")
  14864. ("Phi")
  14865. ("Chi")
  14866. ("Psi")
  14867. ("Omega")
  14868. ("alpha")
  14869. ("beta")
  14870. ("gamma")
  14871. ("delta")
  14872. ("epsilon")
  14873. ("varepsilon"."&epsilon;")
  14874. ("zeta")
  14875. ("eta")
  14876. ("theta")
  14877. ("iota")
  14878. ("kappa")
  14879. ("lambda")
  14880. ("mu")
  14881. ("nu")
  14882. ("xi")
  14883. ("omicron")
  14884. ("pi")
  14885. ("rho")
  14886. ("sigmaf") ("varsigma"."&sigmaf;")
  14887. ("sigma")
  14888. ("tau")
  14889. ("upsilon")
  14890. ("phi")
  14891. ("chi")
  14892. ("psi")
  14893. ("omega")
  14894. ("thetasym") ("vartheta"."&thetasym;")
  14895. ("upsih")
  14896. ("piv")
  14897. ("bull") ("bullet"."&bull;")
  14898. ("hellip") ("dots"."&hellip;")
  14899. ("prime")
  14900. ("Prime")
  14901. ("oline")
  14902. ("frasl")
  14903. ("weierp")
  14904. ("image")
  14905. ("real")
  14906. ("trade")
  14907. ("alefsym")
  14908. ("larr") ("leftarrow"."&larr;") ("gets"."&larr;")
  14909. ("uarr") ("uparrow"."&uarr;")
  14910. ("rarr") ("to"."&rarr;") ("rightarrow"."&rarr;")
  14911. ("darr")("downarrow"."&darr;")
  14912. ("harr") ("leftrightarrow"."&harr;")
  14913. ("crarr") ("hookleftarrow"."&crarr;") ; has round hook, not quite CR
  14914. ("lArr") ("Leftarrow"."&lArr;")
  14915. ("uArr") ("Uparrow"."&uArr;")
  14916. ("rArr") ("Rightarrow"."&rArr;")
  14917. ("dArr") ("Downarrow"."&dArr;")
  14918. ("hArr") ("Leftrightarrow"."&hArr;")
  14919. ("forall")
  14920. ("part") ("partial"."&part;")
  14921. ("exist") ("exists"."&exist;")
  14922. ("empty") ("emptyset"."&empty;")
  14923. ("nabla")
  14924. ("isin") ("in"."&isin;")
  14925. ("notin")
  14926. ("ni")
  14927. ("prod")
  14928. ("sum")
  14929. ("minus")
  14930. ("lowast") ("ast"."&lowast;")
  14931. ("radic")
  14932. ("prop") ("proptp"."&prop;")
  14933. ("infin") ("infty"."&infin;")
  14934. ("ang") ("angle"."&ang;")
  14935. ("and") ("vee"."&and;")
  14936. ("or") ("wedge"."&or;")
  14937. ("cap")
  14938. ("cup")
  14939. ("int")
  14940. ("there4")
  14941. ("sim")
  14942. ("cong") ("simeq"."&cong;")
  14943. ("asymp")("approx"."&asymp;")
  14944. ("ne") ("neq"."&ne;")
  14945. ("equiv")
  14946. ("le")
  14947. ("ge")
  14948. ("sub") ("subset"."&sub;")
  14949. ("sup") ("supset"."&sup;")
  14950. ("nsub")
  14951. ("sube")
  14952. ("supe")
  14953. ("oplus")
  14954. ("otimes")
  14955. ("perp")
  14956. ("sdot") ("cdot"."&sdot;")
  14957. ("lceil")
  14958. ("rceil")
  14959. ("lfloor")
  14960. ("rfloor")
  14961. ("lang")
  14962. ("rang")
  14963. ("loz") ("Diamond"."&loz;")
  14964. ("spades") ("spadesuit"."&spades;")
  14965. ("clubs") ("clubsuit"."&clubs;")
  14966. ("hearts") ("diamondsuit"."&hearts;")
  14967. ("diams") ("diamondsuit"."&diams;")
  14968. ("quot")
  14969. ("amp")
  14970. ("lt")
  14971. ("gt")
  14972. ("OElig")
  14973. ("oelig")
  14974. ("Scaron")
  14975. ("scaron")
  14976. ("Yuml")
  14977. ("circ")
  14978. ("tilde")
  14979. ("ensp")
  14980. ("emsp")
  14981. ("thinsp")
  14982. ("zwnj")
  14983. ("zwj")
  14984. ("lrm")
  14985. ("rlm")
  14986. ("ndash")
  14987. ("mdash")
  14988. ("lsquo")
  14989. ("rsquo")
  14990. ("sbquo")
  14991. ("ldquo")
  14992. ("rdquo")
  14993. ("bdquo")
  14994. ("dagger")
  14995. ("Dagger")
  14996. ("permil")
  14997. ("lsaquo")
  14998. ("rsaquo")
  14999. ("euro")
  15000. ("arccos"."arccos")
  15001. ("arcsin"."arcsin")
  15002. ("arctan"."arctan")
  15003. ("arg"."arg")
  15004. ("cos"."cos")
  15005. ("cosh"."cosh")
  15006. ("cot"."cot")
  15007. ("coth"."coth")
  15008. ("csc"."csc")
  15009. ("deg"."deg")
  15010. ("det"."det")
  15011. ("dim"."dim")
  15012. ("exp"."exp")
  15013. ("gcd"."gcd")
  15014. ("hom"."hom")
  15015. ("inf"."inf")
  15016. ("ker"."ker")
  15017. ("lg"."lg")
  15018. ("lim"."lim")
  15019. ("liminf"."liminf")
  15020. ("limsup"."limsup")
  15021. ("ln"."ln")
  15022. ("log"."log")
  15023. ("max"."max")
  15024. ("min"."min")
  15025. ("Pr"."Pr")
  15026. ("sec"."sec")
  15027. ("sin"."sin")
  15028. ("sinh"."sinh")
  15029. ("sup"."sup")
  15030. ("tan"."tan")
  15031. ("tanh"."tanh")
  15032. )
  15033. "Entities for TeX->HTML translation.
  15034. Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
  15035. \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
  15036. In that case, \"\\ent\" will be translated to \"&other;\".
  15037. The list contains HTML entities for Latin-1, Greek and other symbols.
  15038. It is supplemented by a number of commonly used TeX macros with appropriate
  15039. translations. There is currently no way for users to extend this.")
  15040. ;;; General functions for all backends
  15041. (defun org-cleaned-string-for-export (string &rest parameters)
  15042. "Cleanup a buffer substring so that links can be created safely."
  15043. (interactive)
  15044. (let* ((re-radio (and org-target-link-regexp
  15045. (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)")))
  15046. (re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
  15047. (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
  15048. (re-archive (concat ":" org-archive-tag ":"))
  15049. rtn)
  15050. (save-excursion
  15051. (set-buffer (get-buffer-create " org-mode-tmp"))
  15052. (erase-buffer)
  15053. (insert string)
  15054. (let ((org-inhibit-startup t)) (org-mode))
  15055. (untabify (point-min) (point-max))
  15056. ;; Get rid of archived trees
  15057. (when (not (eq org-export-with-archived-trees t))
  15058. (goto-char (point-min))
  15059. (while (re-search-forward re-archive nil t)
  15060. (if (not (org-on-heading-p))
  15061. (org-end-of-subtree t)
  15062. (beginning-of-line 1)
  15063. (delete-region
  15064. (if org-export-with-archived-trees (1+ (point-at-eol)) (point))
  15065. (org-end-of-subtree t)))))
  15066. ;; Find targets in comments and move them out of comments,
  15067. ;; but mark them as targets that should be invisible
  15068. (goto-char (point-min))
  15069. (while (re-search-forward "^#.*?\\(<<<?[^>\r\n]+>>>?\\).*" nil t)
  15070. (replace-match "\\1(INVISIBLE)"))
  15071. ;; Remove comments
  15072. (goto-char (point-min))
  15073. (while (re-search-forward "^#.*\n?" nil t)
  15074. (replace-match ""))
  15075. ;; Find matches for radio targets and turn them into internal links
  15076. (goto-char (point-min))
  15077. (when re-radio
  15078. (while (re-search-forward re-radio nil t)
  15079. (replace-match "\\1[[\\2]]")))
  15080. ;; Find all links that contain a newline and put them into a single line
  15081. (goto-char (point-min))
  15082. (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
  15083. (replace-match "\\1 \\3")
  15084. (goto-char (match-beginning 0)))
  15085. ;; Convert LaTeX fragments to images
  15086. (when (memq :LaTeX-fragments parameters)
  15087. (org-format-latex
  15088. (concat "ltxpng/" (file-name-sans-extension
  15089. (file-name-nondirectory
  15090. org-current-export-file)))
  15091. org-current-export-dir nil "Creating LaTeX image %s"))
  15092. (message "Exporting...")
  15093. ;; Normalize links: Convert angle and plain links into bracket links
  15094. ;; Expand link abbreviations
  15095. (goto-char (point-min))
  15096. (while (re-search-forward re-plain-link nil t)
  15097. (replace-match
  15098. (concat
  15099. (match-string 1) "[[" (match-string 2) ":" (match-string 3) "]]")
  15100. t t))
  15101. (goto-char (point-min))
  15102. (while (re-search-forward re-angle-link nil t)
  15103. (replace-match
  15104. (concat
  15105. (match-string 1) "[[" (match-string 2) ":" (match-string 3) "]]")
  15106. t t))
  15107. (goto-char (point-min))
  15108. (while (re-search-forward org-bracket-link-regexp nil t)
  15109. (replace-match
  15110. (concat "[[" (save-match-data
  15111. (org-link-expand-abbrev (match-string 1)))
  15112. "]"
  15113. (if (match-end 3)
  15114. (match-string 2)
  15115. (concat "[" (match-string 1) "]"))
  15116. "]")
  15117. t t))
  15118. ;; Find multiline emphasis and put them into single line
  15119. (when (memq :emph-multiline parameters)
  15120. (goto-char (point-min))
  15121. (while (re-search-forward org-emph-re nil t)
  15122. (subst-char-in-region (match-beginning 0) (match-end 0) ?\n ?\ t)
  15123. (goto-char (1- (match-end 0)))))
  15124. (setq rtn (buffer-string)))
  15125. (kill-buffer " org-mode-tmp")
  15126. rtn))
  15127. (defun org-solidify-link-text (s &optional alist)
  15128. "Take link text and make a safe target out of it."
  15129. (save-match-data
  15130. (let* ((rtn
  15131. (mapconcat
  15132. 'identity
  15133. (org-split-string s "[ \t\r\n]+") "--"))
  15134. (a (assoc rtn alist)))
  15135. (or (cdr a) rtn))))
  15136. ;; Variable holding the vector with section numbers
  15137. (defvar org-section-numbers (make-vector org-level-max 0))
  15138. (defun org-init-section-numbers ()
  15139. "Initialize the vector for the section numbers."
  15140. (let* ((level -1)
  15141. (numbers (nreverse (org-split-string "" "\\.")))
  15142. (depth (1- (length org-section-numbers)))
  15143. (i depth) number-string)
  15144. (while (>= i 0)
  15145. (if (> i level)
  15146. (aset org-section-numbers i 0)
  15147. (setq number-string (or (car numbers) "0"))
  15148. (if (string-match "\\`[A-Z]\\'" number-string)
  15149. (aset org-section-numbers i
  15150. (- (string-to-char number-string) ?A -1))
  15151. (aset org-section-numbers i (string-to-number number-string)))
  15152. (pop numbers))
  15153. (setq i (1- i)))))
  15154. (defun org-section-number (&optional level)
  15155. "Return a string with the current section number.
  15156. When LEVEL is non-nil, increase section numbers on that level."
  15157. (let* ((depth (1- (length org-section-numbers))) idx n (string ""))
  15158. (when level
  15159. (when (> level -1)
  15160. (aset org-section-numbers
  15161. level (1+ (aref org-section-numbers level))))
  15162. (setq idx (1+ level))
  15163. (while (<= idx depth)
  15164. (if (not (= idx 1))
  15165. (aset org-section-numbers idx 0))
  15166. (setq idx (1+ idx))))
  15167. (setq idx 0)
  15168. (while (<= idx depth)
  15169. (setq n (aref org-section-numbers idx))
  15170. (setq string (concat string (if (not (string= string "")) "." "")
  15171. (int-to-string n)))
  15172. (setq idx (1+ idx)))
  15173. (save-match-data
  15174. (if (string-match "\\`\\([@0]\\.\\)+" string)
  15175. (setq string (replace-match "" t nil string)))
  15176. (if (string-match "\\(\\.0\\)+\\'" string)
  15177. (setq string (replace-match "" t nil string))))
  15178. string))
  15179. ;;; ASCII export
  15180. (defvar org-last-level nil) ; dynamically scoped variable
  15181. (defvar org-levels-open nil) ; dynamically scoped parameter
  15182. (defvar org-ascii-current-indentation nil) ; For communication
  15183. (defun org-export-as-ascii (arg)
  15184. "Export the outline as a pretty ASCII file.
  15185. If there is an active region, export only the region.
  15186. The prefix ARG specifies how many levels of the outline should become
  15187. underlined headlines. The default is 3."
  15188. (interactive "P")
  15189. (setq-default org-todo-line-regexp org-todo-line-regexp)
  15190. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  15191. (org-infile-export-plist)))
  15192. (region
  15193. (buffer-substring
  15194. (if (org-region-active-p) (region-beginning) (point-min))
  15195. (if (org-region-active-p) (region-end) (point-max))))
  15196. (custom-times org-display-custom-times)
  15197. (lines (org-export-find-first-heading-line
  15198. (org-skip-comments
  15199. (org-split-string
  15200. (org-cleaned-string-for-export region)
  15201. "[\r\n]"))))
  15202. (org-ascii-current-indentation '(0 . 0))
  15203. (org-startup-with-deadline-check nil)
  15204. (level 0) line txt
  15205. (umax nil)
  15206. (case-fold-search nil)
  15207. (filename (concat (file-name-as-directory
  15208. (org-export-directory :ascii opt-plist))
  15209. (file-name-sans-extension
  15210. (file-name-nondirectory buffer-file-name))
  15211. ".txt"))
  15212. (buffer (find-file-noselect filename))
  15213. (org-levels-open (make-vector org-level-max nil))
  15214. (odd org-odd-levels-only)
  15215. (date (format-time-string "%Y/%m/%d" (current-time)))
  15216. (time (format-time-string "%X" (org-current-time)))
  15217. (author (plist-get opt-plist :author))
  15218. (title (or (plist-get opt-plist :title)
  15219. (file-name-sans-extension
  15220. (file-name-nondirectory buffer-file-name))))
  15221. (email (plist-get opt-plist :email))
  15222. (language (plist-get opt-plist :language))
  15223. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  15224. ; (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
  15225. (text nil)
  15226. (todo nil)
  15227. (lang-words nil))
  15228. (setq org-last-level 1)
  15229. (org-init-section-numbers)
  15230. (find-file-noselect filename)
  15231. (setq lang-words (or (assoc language org-export-language-setup)
  15232. (assoc "en" org-export-language-setup)))
  15233. (if org-export-ascii-show-new-buffer
  15234. (switch-to-buffer-other-window buffer)
  15235. (set-buffer buffer))
  15236. (erase-buffer)
  15237. (fundamental-mode)
  15238. ;; create local variables for all options, to make sure all called
  15239. ;; functions get the correct information
  15240. (mapcar (lambda (x)
  15241. (set (make-local-variable (cdr x))
  15242. (plist-get opt-plist (car x))))
  15243. org-export-plist-vars)
  15244. (org-set-local 'org-odd-levels-only odd)
  15245. (setq umax (if arg (prefix-numeric-value arg)
  15246. org-export-headline-levels))
  15247. ;; File header
  15248. (if title (org-insert-centered title ?=))
  15249. (insert "\n")
  15250. (if (or author email)
  15251. (insert (concat (nth 1 lang-words) ": " (or author "")
  15252. (if email (concat " <" email ">") "")
  15253. "\n")))
  15254. (if (and date time)
  15255. (insert (concat (nth 2 lang-words) ": " date " " time "\n")))
  15256. (if text (insert (concat (org-html-expand-for-ascii text) "\n\n")))
  15257. (insert "\n\n")
  15258. (if org-export-with-toc
  15259. (progn
  15260. (insert (nth 3 lang-words) "\n"
  15261. (make-string (length (nth 3 lang-words)) ?=) "\n")
  15262. (mapcar '(lambda (line)
  15263. (if (string-match org-todo-line-regexp
  15264. line)
  15265. ;; This is a headline
  15266. (progn
  15267. (setq level (- (match-end 1) (match-beginning 1))
  15268. level (org-tr-level level)
  15269. txt (match-string 3 line)
  15270. todo
  15271. (or (and org-export-mark-todo-in-toc
  15272. (match-beginning 2)
  15273. (not (equal (match-string 2 line)
  15274. org-done-string)))
  15275. ; TODO, not DONE
  15276. (and org-export-mark-todo-in-toc
  15277. (= level umax)
  15278. (org-search-todo-below
  15279. line lines level))))
  15280. (setq txt (org-html-expand-for-ascii txt))
  15281. (if (and (memq org-export-with-tags '(not-in-toc nil))
  15282. (string-match "[ \t]+:[a-zA-Z0-9_@:]+:[ \t]*$" txt))
  15283. (setq txt (replace-match "" t t txt)))
  15284. (if (string-match quote-re0 txt)
  15285. (setq txt (replace-match "" t t txt)))
  15286. (if org-export-with-section-numbers
  15287. (setq txt (concat (org-section-number level)
  15288. " " txt)))
  15289. (if (<= level umax)
  15290. (progn
  15291. (insert
  15292. (make-string (* (1- level) 4) ?\ )
  15293. (format (if todo "%s (*)\n" "%s\n") txt))
  15294. (setq org-last-level level))
  15295. ))))
  15296. lines)))
  15297. (org-init-section-numbers)
  15298. (while (setq line (pop lines))
  15299. ;; Remove the quoted HTML tags.
  15300. (setq line (org-html-expand-for-ascii line))
  15301. ;; Remove targets
  15302. (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line)
  15303. (setq line (replace-match "" t t line)))
  15304. ;; Replace internal links
  15305. (while (string-match org-bracket-link-regexp line)
  15306. (setq line (replace-match
  15307. (if (match-end 3) "[\\3]" "[\\1]")
  15308. t nil line)))
  15309. (when custom-times
  15310. (setq line (org-translate-time line)))
  15311. (cond
  15312. ((string-match "^\\(\\*+\\)[ \t]*\\(.*\\)" line)
  15313. ;; a Headline
  15314. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  15315. txt (match-string 2 line))
  15316. (org-ascii-level-start level txt umax lines))
  15317. (t
  15318. (insert (org-fix-indentation line org-ascii-current-indentation) "\n"))))
  15319. (normal-mode)
  15320. (save-buffer)
  15321. ;; remove display and invisible chars
  15322. (let (beg end)
  15323. (goto-char (point-min))
  15324. (while (setq beg (next-single-property-change (point) 'display))
  15325. (setq end (next-single-property-change beg 'display))
  15326. (delete-region beg end)
  15327. (goto-char beg)
  15328. (insert "=>"))
  15329. (goto-char (point-min))
  15330. (while (setq beg (next-single-property-change (point) 'org-cwidth))
  15331. (setq end (next-single-property-change beg 'org-cwidth))
  15332. (delete-region beg end)
  15333. (goto-char beg)))
  15334. (goto-char (point-min))))
  15335. (defun org-search-todo-below (line lines level)
  15336. "Search the subtree below LINE for any TODO entries."
  15337. (let ((rest (cdr (memq line lines)))
  15338. (re org-todo-line-regexp)
  15339. line lv todo)
  15340. (catch 'exit
  15341. (while (setq line (pop rest))
  15342. (if (string-match re line)
  15343. (progn
  15344. (setq lv (- (match-end 1) (match-beginning 1))
  15345. todo (and (match-beginning 2)
  15346. (not (equal (match-string 2 line)
  15347. org-done-string))))
  15348. ; TODO, not DONE
  15349. (if (<= lv level) (throw 'exit nil))
  15350. (if todo (throw 'exit t))))))))
  15351. (defun org-html-expand-for-ascii (line)
  15352. "Handle quoted HTML for ASCII export."
  15353. (if org-export-html-expand
  15354. (while (string-match "@<[^<>\n]*>" line)
  15355. ;; We just remove the tags for now.
  15356. (setq line (replace-match "" nil nil line))))
  15357. line)
  15358. (defun org-insert-centered (s &optional underline)
  15359. "Insert the string S centered and underline it with character UNDERLINE."
  15360. (let ((ind (max (/ (- 80 (string-width s)) 2) 0)))
  15361. (insert (make-string ind ?\ ) s "\n")
  15362. (if underline
  15363. (insert (make-string ind ?\ )
  15364. (make-string (string-width s) underline)
  15365. "\n"))))
  15366. (defun org-ascii-level-start (level title umax &optional lines)
  15367. "Insert a new level in ASCII export."
  15368. (let (char (n (- level umax 1)) (ind 0))
  15369. (if (> level umax)
  15370. (progn
  15371. (insert (make-string (* 2 n) ?\ )
  15372. (char-to-string (nth (% n (length org-export-ascii-bullets))
  15373. org-export-ascii-bullets))
  15374. " " title "\n")
  15375. ;; find the indentation of the next non-empty line
  15376. (catch 'stop
  15377. (while lines
  15378. (if (string-match "^\\*" (car lines)) (throw 'stop nil))
  15379. (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
  15380. (throw 'stop (setq ind (org-get-indentation (car lines)))))
  15381. (pop lines)))
  15382. (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
  15383. (if (or (not (equal (char-before) ?\n))
  15384. (not (equal (char-before (1- (point))) ?\n)))
  15385. (insert "\n"))
  15386. (setq char (nth (- umax level) (reverse org-export-ascii-underline)))
  15387. (unless org-export-with-tags
  15388. (if (string-match "[ \t]+\\(:[a-zA-Z0-9_@:]+:\\)[ \t]*$" title)
  15389. (setq title (replace-match "" t t title))))
  15390. (if org-export-with-section-numbers
  15391. (setq title (concat (org-section-number level) " " title)))
  15392. (insert title "\n" (make-string (string-width title) char) "\n")
  15393. (setq org-ascii-current-indentation '(0 . 0)))))
  15394. (defun org-export-visible (type arg)
  15395. "Create a copy of the visible part of the current buffer, and export it.
  15396. The copy is created in a temporary buffer and removed after use.
  15397. TYPE is the final key (as a string) that also select the export command in
  15398. the `C-c C-e' export dispatcher.
  15399. As a special case, if the you type SPC at the prompt, the temporary
  15400. org-mode file will not be removed but presented to you so that you can
  15401. continue to use it. The prefix arg ARG is passed through to the exporting
  15402. command."
  15403. (interactive
  15404. (list (progn
  15405. (message "Export visible: [a]SCII [h]tml [b]rowse HTML [x]OXO [ ]keep buffer")
  15406. (read-char-exclusive))
  15407. current-prefix-arg))
  15408. (if (not (member type '(?a ?\C-a ?b ?\C-b ?h ?x ?\ )))
  15409. (error "Invalid export key"))
  15410. (let* ((binding (cdr (assoc type
  15411. '((?a . org-export-as-ascii)
  15412. (?\C-a . org-export-as-ascii)
  15413. (?b . org-export-as-html-and-open)
  15414. (?\C-b . org-export-as-html-and-open)
  15415. (?h . org-export-as-html)
  15416. (?x . org-export-as-xoxo)))))
  15417. (keepp (equal type ?\ ))
  15418. (file buffer-file-name)
  15419. (buffer (get-buffer-create "*Org Export Visible*"))
  15420. s e)
  15421. (with-current-buffer buffer (erase-buffer))
  15422. (save-excursion
  15423. (setq s (goto-char (point-min)))
  15424. (while (not (= (point) (point-max)))
  15425. (goto-char (org-find-invisible))
  15426. (append-to-buffer buffer s (point))
  15427. (setq s (goto-char (org-find-visible))))
  15428. (goto-char (point-min))
  15429. (unless keepp
  15430. ;; Copy all comment lines to the end, to make sure #+ settings are
  15431. ;; still available for the second export step. Kind of a hack, but
  15432. ;; does do the trick.
  15433. (if (looking-at "#[^\r\n]*")
  15434. (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
  15435. (while (re-search-forward "[\n\r]#[^\n\r]*" nil t)
  15436. (append-to-buffer buffer (1+ (match-beginning 0))
  15437. (min (point-max) (1+ (match-end 0))))))
  15438. (set-buffer buffer)
  15439. (let ((buffer-file-name file)
  15440. (org-inhibit-startup t))
  15441. (org-mode)
  15442. (show-all)
  15443. (unless keepp (funcall binding arg))))
  15444. (if (not keepp)
  15445. (kill-buffer buffer)
  15446. (switch-to-buffer-other-window buffer)
  15447. (goto-char (point-min)))))
  15448. (defun org-find-visible ()
  15449. (let ((s (point)))
  15450. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  15451. (get-char-property s 'invisible)))
  15452. s))
  15453. (defun org-find-invisible ()
  15454. (let ((s (point)))
  15455. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  15456. (not (get-char-property s 'invisible))))
  15457. s))
  15458. ;;; HTML export
  15459. (defun org-get-current-options ()
  15460. "Return a string with current options as keyword options.
  15461. Does include HTML export options as well as TODO and CATEGORY stuff."
  15462. (format
  15463. "#+TITLE: %s
  15464. #+AUTHOR: %s
  15465. #+EMAIL: %s
  15466. #+LANGUAGE: %s
  15467. #+TEXT: Some descriptive text to be emitted. Several lines OK.
  15468. #+OPTIONS: H:%d num:%s toc:%s \\n:%s @:%s ::%s |:%s ^:%s *:%s TeX:%s LaTeX:%s
  15469. #+CATEGORY: %s
  15470. #+SEQ_TODO: %s
  15471. #+TYP_TODO: %s
  15472. #+STARTUP: %s %s %s %s %s %s
  15473. #+TAGS: %s
  15474. #+ARCHIVE: %s
  15475. #+LINK: %s
  15476. "
  15477. (buffer-name) (user-full-name) user-mail-address org-export-default-language
  15478. org-export-headline-levels
  15479. org-export-with-section-numbers
  15480. org-export-with-toc
  15481. org-export-preserve-breaks
  15482. org-export-html-expand
  15483. org-export-with-fixed-width
  15484. org-export-with-tables
  15485. org-export-with-sub-superscripts
  15486. org-export-with-emphasize
  15487. org-export-with-TeX-macros
  15488. org-export-with-LaTeX-fragments
  15489. (file-name-nondirectory buffer-file-name)
  15490. (if (equal org-todo-interpretation 'sequence)
  15491. (mapconcat 'identity org-todo-keywords " ")
  15492. "TODO FEEDBACK VERIFY DONE")
  15493. (if (equal org-todo-interpretation 'type)
  15494. (mapconcat 'identity org-todo-keywords " ")
  15495. "Me Jason Marie DONE")
  15496. (cdr (assoc org-startup-folded
  15497. '((nil . "showall") (t . "overview") (content . "content"))))
  15498. (if org-startup-with-deadline-check "dlcheck" "nodlcheck")
  15499. (if org-odd-levels-only "odd" "oddeven")
  15500. (if org-hide-leading-stars "hidestars" "showstars")
  15501. (if org-startup-align-all-tables "align" "noalign")
  15502. (cond ((eq t org-log-done) "logdone")
  15503. ((not org-log-done) "nologging")
  15504. ((listp org-log-done)
  15505. (mapconcat (lambda (x) (concat "lognote" (symbol-name x)))
  15506. org-log-done " ")))
  15507. (or (mapconcat (lambda (x)
  15508. (cond
  15509. ((equal '(:startgroup) x) "{")
  15510. ((equal '(:endgroup) x) "}")
  15511. ((cdr x) (format "%s(%c)" (car x) (cdr x)))
  15512. (t (car x))))
  15513. (or org-tag-alist (org-get-buffer-tags)) " ") "")
  15514. org-archive-location
  15515. "org file:~/org/%s.org"
  15516. ))
  15517. (defun org-insert-export-options-template ()
  15518. "Insert into the buffer a template with information for exporting."
  15519. (interactive)
  15520. (if (not (bolp)) (newline))
  15521. (let ((s (org-get-current-options)))
  15522. (and (string-match "#\\+CATEGORY" s)
  15523. (setq s (substring s 0 (match-beginning 0))))
  15524. (insert s)))
  15525. (defun org-toggle-fixed-width-section (arg)
  15526. "Toggle the fixed-width export.
  15527. If there is no active region, the QUOTE keyword at the current headline is
  15528. inserted or removed. When present, it causes the text between this headline
  15529. and the next to be exported as fixed-width text, and unmodified.
  15530. If there is an active region, this command adds or removes a colon as the
  15531. first character of this line. If the first character of a line is a colon,
  15532. this line is also exported in fixed-width font."
  15533. (interactive "P")
  15534. (let* ((cc 0)
  15535. (regionp (org-region-active-p))
  15536. (beg (if regionp (region-beginning) (point)))
  15537. (end (if regionp (region-end)))
  15538. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  15539. (re "[ \t]*\\(:\\)")
  15540. off)
  15541. (if regionp
  15542. (save-excursion
  15543. (goto-char beg)
  15544. (setq cc (current-column))
  15545. (beginning-of-line 1)
  15546. (setq off (looking-at re))
  15547. (while (> nlines 0)
  15548. (setq nlines (1- nlines))
  15549. (beginning-of-line 1)
  15550. (cond
  15551. (arg
  15552. (move-to-column cc t)
  15553. (insert ":\n")
  15554. (forward-line -1))
  15555. ((and off (looking-at re))
  15556. (replace-match "" t t nil 1))
  15557. ((not off) (move-to-column cc t) (insert ":")))
  15558. (forward-line 1)))
  15559. (save-excursion
  15560. (org-back-to-heading)
  15561. (if (looking-at (concat outline-regexp
  15562. "\\( +\\<" org-quote-string "\\>\\)"))
  15563. (replace-match "" t t nil 1)
  15564. (if (looking-at outline-regexp)
  15565. (progn
  15566. (goto-char (match-end 0))
  15567. (insert " " org-quote-string))))))))
  15568. (defun org-export-as-html-and-open (arg)
  15569. "Export the outline as HTML and immediately open it with a browser.
  15570. If there is an active region, export only the region.
  15571. The prefix ARG specifies how many levels of the outline should become
  15572. headlines. The default is 3. Lower levels will become bulleted lists."
  15573. (interactive "P")
  15574. (org-export-as-html arg 'hidden)
  15575. (org-open-file buffer-file-name))
  15576. (defun org-export-as-html-batch ()
  15577. "Call `org-export-as-html', may be used in batch processing as
  15578. emacs --batch
  15579. --load=$HOME/lib/emacs/org.el
  15580. --eval \"(setq org-export-headline-levels 2)\"
  15581. --visit=MyFile --funcall org-export-as-html-batch"
  15582. (org-export-as-html org-export-headline-levels 'hidden))
  15583. (defun org-export-as-html (arg &optional hidden ext-plist)
  15584. "Export the outline as a pretty HTML file.
  15585. If there is an active region, export only the region.
  15586. The prefix ARG specifies how many levels of the outline should become
  15587. headlines. The default is 3. Lower levels will become bulleted lists.
  15588. When HIDDEN is non-nil, don't display the HTML buffer.
  15589. EXT-PLIST is a property list with external parameters overriding
  15590. org-mode's default settings, but still inferior to file-local settings."
  15591. (interactive "P")
  15592. (message "Exporting...")
  15593. (setq-default org-todo-line-regexp org-todo-line-regexp)
  15594. (setq-default org-deadline-line-regexp org-deadline-line-regexp)
  15595. (setq-default org-done-string org-done-string)
  15596. (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
  15597. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  15598. ext-plist
  15599. (org-infile-export-plist)))
  15600. (style (plist-get opt-plist :style))
  15601. (link-validate (plist-get opt-plist :link-validation-function))
  15602. valid
  15603. (odd org-odd-levels-only)
  15604. (region-p (org-region-active-p))
  15605. (region
  15606. (buffer-substring
  15607. (if region-p (region-beginning) (point-min))
  15608. (if region-p (region-end) (point-max))))
  15609. ;; The following two are dynamically scoped into other
  15610. ;; routines below.
  15611. (org-current-export-dir (org-export-directory :html opt-plist))
  15612. (org-current-export-file buffer-file-name)
  15613. (all_lines
  15614. (org-skip-comments (org-split-string
  15615. (org-cleaned-string-for-export
  15616. region :emph-multiline
  15617. (if (plist-get opt-plist :LaTeX-fragments)
  15618. :LaTeX-fragments))
  15619. "[\r\n]")))
  15620. (lines (org-export-find-first-heading-line all_lines))
  15621. (level 0) (line "") (origline "") txt todo
  15622. (umax nil)
  15623. (filename (concat (file-name-as-directory
  15624. (org-export-directory :html opt-plist))
  15625. (file-name-sans-extension
  15626. (file-name-nondirectory buffer-file-name))
  15627. ".html"))
  15628. (current-dir (file-name-directory buffer-file-name))
  15629. (buffer (find-file-noselect filename))
  15630. (org-levels-open (make-vector org-level-max nil))
  15631. (date (format-time-string "%Y/%m/%d" (current-time)))
  15632. (time (format-time-string "%X" (org-current-time)))
  15633. (author (plist-get opt-plist :author))
  15634. (title (or (plist-get opt-plist :title)
  15635. (file-name-sans-extension
  15636. (file-name-nondirectory buffer-file-name))))
  15637. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  15638. (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
  15639. (inquote nil)
  15640. (infixed nil)
  15641. (in-local-list nil)
  15642. (local-list-num nil)
  15643. (local-list-indent nil)
  15644. (llt org-plain-list-ordered-item-terminator)
  15645. (email (plist-get opt-plist :email))
  15646. (language (plist-get opt-plist :language))
  15647. (text (plist-get opt-plist :text))
  15648. (lang-words nil)
  15649. (target-alist nil) tg
  15650. (head-count 0) cnt
  15651. (start 0)
  15652. (coding-system (and (boundp 'buffer-file-coding-system)
  15653. buffer-file-coding-system))
  15654. (coding-system-for-write coding-system)
  15655. (save-buffer-coding-system coding-system)
  15656. (charset (and coding-system
  15657. (fboundp 'coding-system-get)
  15658. (coding-system-get coding-system 'mime-charset)))
  15659. table-open type
  15660. table-buffer table-orig-buffer
  15661. ind start-is-num starter
  15662. rpl path desc descp desc1 desc2 link
  15663. )
  15664. (message "Exporting...")
  15665. (setq org-last-level 1)
  15666. (org-init-section-numbers)
  15667. ;; Get the language-dependent settings
  15668. (setq lang-words (or (assoc language org-export-language-setup)
  15669. (assoc "en" org-export-language-setup)))
  15670. ;; Switch to the output buffer
  15671. (if (or hidden (not org-export-html-show-new-buffer))
  15672. (set-buffer buffer)
  15673. (switch-to-buffer-other-window buffer))
  15674. (erase-buffer)
  15675. (fundamental-mode)
  15676. (let ((case-fold-search nil)
  15677. (org-odd-levels-only odd))
  15678. ;; create local variables for all options, to make sure all called
  15679. ;; functions get the correct information
  15680. (mapcar (lambda (x)
  15681. (set (make-local-variable (cdr x))
  15682. (plist-get opt-plist (car x))))
  15683. org-export-plist-vars)
  15684. (setq umax (if arg (prefix-numeric-value arg)
  15685. org-export-headline-levels))
  15686. ;; File header
  15687. (insert (format
  15688. "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
  15689. \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
  15690. <html xmlns=\"http://www.w3.org/1999/xhtml\"
  15691. lang=\"%s\" xml:lang=\"%s\">
  15692. <head>
  15693. <title>%s</title>
  15694. <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
  15695. <meta name=\"generator\" content=\"Org-mode\"/>
  15696. <meta name=\"generated\" content=\"%s %s\"/>
  15697. <meta name=\"author\" content=\"%s\"/>
  15698. %s
  15699. </head><body>
  15700. "
  15701. language language (org-html-expand title) (or charset "iso-8859-1")
  15702. date time author style))
  15703. (insert (or (plist-get opt-plist :preamble) ""))
  15704. (when (plist-get opt-plist :auto-preamble)
  15705. (if title (insert (format org-export-html-title-format
  15706. (org-html-expand title))))
  15707. (if text (insert "<p>\n" (org-html-expand text) "</p>")))
  15708. (if org-export-with-toc
  15709. (progn
  15710. (insert (format "<h%d>%s</h%d>\n"
  15711. org-export-html-toplevel-hlevel
  15712. (nth 3 lang-words)
  15713. org-export-html-toplevel-hlevel))
  15714. (insert "<ul>\n<li>")
  15715. (setq lines
  15716. (mapcar '(lambda (line)
  15717. (if (string-match org-todo-line-regexp line)
  15718. ;; This is a headline
  15719. (progn
  15720. (setq level (- (match-end 1) (match-beginning 1))
  15721. level (org-tr-level level)
  15722. txt (save-match-data
  15723. (org-html-expand
  15724. (org-export-cleanup-toc-line
  15725. (match-string 3 line))))
  15726. todo
  15727. (or (and org-export-mark-todo-in-toc
  15728. (match-beginning 2)
  15729. (not (equal (match-string 2 line)
  15730. org-done-string)))
  15731. ; TODO, not DONE
  15732. (and org-export-mark-todo-in-toc
  15733. (= level umax)
  15734. (org-search-todo-below
  15735. line lines level))))
  15736. (if (and (memq org-export-with-tags '(not-in-toc nil))
  15737. (string-match "[ \t]+:[a-zA-Z0-9_@:]+:[ \t]*$" txt))
  15738. (setq txt (replace-match "" t t txt)))
  15739. (if (string-match quote-re0 txt)
  15740. (setq txt (replace-match "" t t txt)))
  15741. (if org-export-with-section-numbers
  15742. (setq txt (concat (org-section-number level)
  15743. " " txt)))
  15744. (if (<= level umax)
  15745. (progn
  15746. (setq head-count (+ head-count 1))
  15747. (if (> level org-last-level)
  15748. (progn
  15749. (setq cnt (- level org-last-level))
  15750. (while (>= (setq cnt (1- cnt)) 0)
  15751. (insert "\n<ul>\n<li>"))
  15752. (insert "\n")))
  15753. (if (< level org-last-level)
  15754. (progn
  15755. (setq cnt (- org-last-level level))
  15756. (while (>= (setq cnt (1- cnt)) 0)
  15757. (insert "</li>\n</ul>"))
  15758. (insert "\n")))
  15759. ;; Check for targets
  15760. (while (string-match org-target-regexp line)
  15761. (setq tg (match-string 1 line)
  15762. line (replace-match
  15763. (concat "@<span class=\"target\">" tg "@</span> ")
  15764. t t line))
  15765. (push (cons (org-solidify-link-text tg)
  15766. (format "sec-%d" head-count))
  15767. target-alist))
  15768. (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
  15769. (setq txt (replace-match "" t t txt)))
  15770. (insert
  15771. (format
  15772. (if todo
  15773. "</li>\n<li><a href=\"#sec-%d\"><span class=\"todo\">%s</span></a>"
  15774. "</li>\n<li><a href=\"#sec-%d\">%s</a>")
  15775. head-count txt))
  15776. (setq org-last-level level))
  15777. )))
  15778. line)
  15779. lines))
  15780. (while (> org-last-level 0)
  15781. (setq org-last-level (1- org-last-level))
  15782. (insert "</li>\n</ul>\n"))
  15783. ))
  15784. (setq head-count 0)
  15785. (org-init-section-numbers)
  15786. (while (setq line (pop lines) origline line)
  15787. (catch 'nextline
  15788. ;; end of quote section?
  15789. (when (and inquote (string-match "^\\*+" line))
  15790. (insert "</pre>\n")
  15791. (setq inquote nil))
  15792. ;; inside a quote section?
  15793. (when inquote
  15794. (insert (org-html-protect line) "\n")
  15795. (throw 'nextline nil))
  15796. ;; verbatim lines
  15797. (when (and org-export-with-fixed-width
  15798. (string-match "^[ \t]*:\\(.*\\)" line))
  15799. (when (not infixed)
  15800. (setq infixed t)
  15801. (insert "<pre>\n"))
  15802. (insert (org-html-protect (match-string 1 line)) "\n")
  15803. (when (and lines
  15804. (not (string-match "^[ \t]*\\(:.*\\)"
  15805. (car lines))))
  15806. (setq infixed nil)
  15807. (insert "</pre>\n"))
  15808. (throw 'nextline nil))
  15809. ;; Horizontal line
  15810. (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line)
  15811. (insert "\n<hr/>\n")
  15812. (throw 'nextline nil))
  15813. ;; make targets to anchors
  15814. (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
  15815. (cond
  15816. ((match-end 2)
  15817. (setq line (replace-match
  15818. (concat "@<a name=\""
  15819. (org-solidify-link-text (match-string 1 line))
  15820. "\">\\nbsp@</a>")
  15821. t t line)))
  15822. ((and org-export-with-toc (equal (string-to-char line) ?*))
  15823. (setq line (replace-match
  15824. (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
  15825. ; (concat "@<i>" (match-string 1 line) "@</i> ")
  15826. t t line)))
  15827. (t
  15828. (setq line (replace-match
  15829. (concat "@<a name=\""
  15830. (org-solidify-link-text (match-string 1 line))
  15831. "\" class=\"target\">" (match-string 1 line) "@</a> ")
  15832. t t line)))))
  15833. (setq line (org-html-handle-time-stamps line))
  15834. ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  15835. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  15836. ;; Also handle sub_superscripts and checkboxes
  15837. (setq line (org-html-expand line))
  15838. ;; Format the links
  15839. (setq start 0)
  15840. (while (string-match org-bracket-link-analytic-regexp line start)
  15841. (setq start (match-beginning 0))
  15842. (setq type (if (match-end 2) (match-string 2 line) "internal"))
  15843. (setq path (match-string 3 line))
  15844. (setq desc1 (if (match-end 5) (match-string 5 line))
  15845. desc2 (if (match-end 2) (concat type ":" path) path)
  15846. descp (and desc1 (not (equal desc1 desc2)))
  15847. desc (or desc1 desc2))
  15848. ;; Make an image out of the description if that is so wanted
  15849. (when (and descp (org-file-image-p desc))
  15850. (save-match-data
  15851. (if (string-match "^file:" desc)
  15852. (setq desc (substring desc (match-end 0)))))
  15853. (setq desc (concat "<img src=\"" desc "\"/>")))
  15854. ;; FIXME: do we need to unescape here somewhere?
  15855. (cond
  15856. ((equal type "internal")
  15857. (setq rpl
  15858. (concat
  15859. "<a href=\"#"
  15860. (org-solidify-link-text path target-alist)
  15861. "\">" desc "</a>")))
  15862. ((member type '("http" "https")) ; FIXME: need to test this.
  15863. ;; standard URL, just check if we need to inline an image
  15864. (if (and (or (eq t org-export-html-inline-images)
  15865. (and org-export-html-inline-images (not descp)))
  15866. (org-file-image-p path))
  15867. (setq rpl (concat "<img src=\"" type ":" path "\"/>"))
  15868. (setq link (concat type ":" path))
  15869. (setq rpl (concat "<a href=\"" link "\">" desc "</a>"))))
  15870. ((member type '("ftp" "mailto" "news"))
  15871. ;; standard URL
  15872. (setq link (concat type ":" path))
  15873. (setq rpl (concat "<a href=\"" link "\">" desc "</a>")))
  15874. ((string= type "file")
  15875. ;; FILE link
  15876. (let* ((filename path)
  15877. (abs-p (file-name-absolute-p filename))
  15878. thefile file-is-image-p search)
  15879. (save-match-data
  15880. (if (string-match "::\\(.*\\)" filename)
  15881. (setq search (match-string 1 filename)
  15882. filename (replace-match "" t nil filename)))
  15883. (setq valid
  15884. (if (functionp link-validate)
  15885. (funcall link-validate filename current-dir)
  15886. t))
  15887. (setq file-is-image-p (org-file-image-p filename))
  15888. (setq thefile (if abs-p (expand-file-name filename) filename))
  15889. (when (and org-export-html-link-org-files-as-html
  15890. (string-match "\\.org$" thefile))
  15891. (setq thefile (concat (substring thefile 0
  15892. (match-beginning 0))
  15893. ".html"))
  15894. (if (and search
  15895. ;; make sure this is can be used as target search
  15896. (not (string-match "^[0-9]*$" search))
  15897. (not (string-match "^\\*" search))
  15898. (not (string-match "^/.*/$" search)))
  15899. (setq thefile (concat thefile "#"
  15900. (org-solidify-link-text
  15901. (org-link-unescape search)))))
  15902. (when (string-match "^file:" desc)
  15903. (setq desc (replace-match "" t t desc))
  15904. (if (string-match "\\.org$" desc)
  15905. (setq desc (replace-match "" t t desc))))))
  15906. (setq rpl (if (and file-is-image-p
  15907. (or (eq t org-export-html-inline-images)
  15908. (and org-export-html-inline-images
  15909. (not descp))))
  15910. (concat "<img src=\"" thefile "\"/>")
  15911. (concat "<a href=\"" thefile "\">" desc "</a>")))
  15912. (if (not valid) (setq rpl desc))))
  15913. ((member type '("bbdb" "vm" "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  15914. (setq rpl (concat "<i>&lt;" type ":"
  15915. (save-match-data (org-link-unescape path))
  15916. "&gt;</i>"))))
  15917. (setq line (replace-match rpl t t line)
  15918. start (+ start (length rpl))))
  15919. ;; TODO items
  15920. (if (and (string-match org-todo-line-regexp line)
  15921. (match-beginning 2))
  15922. (if (equal (match-string 2 line) org-done-string)
  15923. (setq line (replace-match
  15924. "<span class=\"done\">\\2</span>"
  15925. t nil line 2))
  15926. (setq line (replace-match "<span class=\"todo\">\\2</span>"
  15927. t nil line 2))))
  15928. (cond
  15929. ((string-match "^\\(\\*+\\)[ \t]*\\(.*\\)" line)
  15930. ;; This is a headline
  15931. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  15932. txt (match-string 2 line))
  15933. (if (string-match quote-re0 txt)
  15934. (setq txt (replace-match "" t t txt)))
  15935. (if (<= level umax) (setq head-count (+ head-count 1)))
  15936. (when in-local-list
  15937. ;; Close any local lists before inserting a new header line
  15938. (while local-list-num
  15939. (org-close-li)
  15940. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  15941. (pop local-list-num))
  15942. (setq local-list-indent nil
  15943. in-local-list nil))
  15944. (org-html-level-start level txt umax
  15945. (and org-export-with-toc (<= level umax))
  15946. head-count)
  15947. ;; QUOTES
  15948. (when (string-match quote-re line)
  15949. (insert "<pre>")
  15950. (setq inquote t)))
  15951. ((and org-export-with-tables
  15952. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  15953. (if (not table-open)
  15954. ;; New table starts
  15955. (setq table-open t table-buffer nil table-orig-buffer nil))
  15956. ;; Accumulate lines
  15957. (setq table-buffer (cons line table-buffer)
  15958. table-orig-buffer (cons origline table-orig-buffer))
  15959. (when (or (not lines)
  15960. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  15961. (car lines))))
  15962. (setq table-open nil
  15963. table-buffer (nreverse table-buffer)
  15964. table-orig-buffer (nreverse table-orig-buffer))
  15965. (org-close-par-maybe)
  15966. (insert (org-format-table-html table-buffer table-orig-buffer))))
  15967. (t
  15968. ;; Normal lines
  15969. (when (string-match
  15970. (cond
  15971. ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  15972. ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  15973. ((= llt ?\)) "^\\( \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  15974. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
  15975. line)
  15976. (setq ind (org-get-string-indentation line)
  15977. start-is-num (match-beginning 4)
  15978. starter (if (match-beginning 2)
  15979. (substring (match-string 2 line) 0 -1))
  15980. line (substring line (match-beginning 5)))
  15981. (unless (string-match "[^ \t]" line)
  15982. ;; empty line. Pretend indentation is large.
  15983. (setq ind (1+ (or (car local-list-indent) 1))))
  15984. (while (and in-local-list
  15985. (or (and (= ind (car local-list-indent))
  15986. (not starter))
  15987. (< ind (car local-list-indent))))
  15988. (org-close-li)
  15989. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  15990. (pop local-list-num) (pop local-list-indent)
  15991. (setq in-local-list local-list-indent))
  15992. (cond
  15993. ((and starter
  15994. (or (not in-local-list)
  15995. (> ind (car local-list-indent))))
  15996. ;; Start new (level of ) list
  15997. (org-close-par-maybe)
  15998. (insert (if start-is-num "<ol>\n<li>\n" "<ul>\n<li>\n"))
  15999. (push start-is-num local-list-num)
  16000. (push ind local-list-indent)
  16001. (setq in-local-list t))
  16002. (starter
  16003. ;; continue current list
  16004. (org-close-li)
  16005. (insert "<li>\n")))
  16006. (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line)
  16007. (setq line
  16008. (replace-match
  16009. (if (equal (match-string 1 line) "X")
  16010. "<b>[X]</b>"
  16011. "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
  16012. t t line))))
  16013. ;; Empty lines start a new paragraph. If hand-formatted lists
  16014. ;; are not fully interpreted, lines starting with "-", "+", "*"
  16015. ;; also start a new paragraph.
  16016. (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
  16017. ;; Check if the line break needs to be conserved
  16018. (cond
  16019. ((string-match "\\\\\\\\[ \t]*$" line)
  16020. (setq line (replace-match "<br/>" t t line)))
  16021. (org-export-preserve-breaks
  16022. (setq line (concat line "<br/>"))))
  16023. (insert line "\n")))))
  16024. ;; Properly close all local lists and other lists
  16025. (when inquote (insert "</pre>\n"))
  16026. (when in-local-list
  16027. ;; Close any local lists before inserting a new header line
  16028. (while local-list-num
  16029. (org-close-li)
  16030. (insert (if (car local-list-num) "</ol>\n" "</ul>\n"))
  16031. (pop local-list-num))
  16032. (setq local-list-indent nil
  16033. in-local-list nil))
  16034. (org-html-level-start 1 nil umax
  16035. (and org-export-with-toc (<= level umax))
  16036. head-count)
  16037. (when (plist-get opt-plist :auto-postamble)
  16038. (when author
  16039. (insert "<p class=\"author\"> "
  16040. (nth 1 lang-words) ": " author "\n")
  16041. (when email
  16042. (insert "<a href=\"mailto:" email "\">&lt;"
  16043. email "&gt;</a>\n"))
  16044. (insert "</p>\n"))
  16045. (when (and date time)
  16046. (insert "<p class=\"date\"> "
  16047. (nth 2 lang-words) ": "
  16048. date " " time "</p>\n")))
  16049. (if org-export-html-with-timestamp
  16050. (insert org-export-html-html-helper-timestamp))
  16051. (insert (or (plist-get opt-plist :postamble) ""))
  16052. (insert "</body>\n</html>\n")
  16053. (normal-mode)
  16054. ;; remove empty paragraphs and lists
  16055. (goto-char (point-min))
  16056. (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
  16057. (replace-match ""))
  16058. (goto-char (point-min))
  16059. (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t)
  16060. (replace-match ""))
  16061. (save-buffer)
  16062. (goto-char (point-min))
  16063. (message "Exporting... done"))))
  16064. (defun org-format-table-html (lines olines)
  16065. "Find out which HTML converter to use and return the HTML code."
  16066. (if (string-match "^[ \t]*|" (car lines))
  16067. ;; A normal org table
  16068. (org-format-org-table-html lines)
  16069. ;; Table made by table.el - test for spanning
  16070. (let* ((hlines (delq nil (mapcar
  16071. (lambda (x)
  16072. (if (string-match "^[ \t]*\\+-" x) x
  16073. nil))
  16074. lines)))
  16075. (first (car hlines))
  16076. (ll (and (string-match "\\S-+" first)
  16077. (match-string 0 first)))
  16078. (re (concat "^[ \t]*" (regexp-quote ll)))
  16079. (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
  16080. hlines))))
  16081. (if (and (not spanning)
  16082. (not org-export-prefer-native-exporter-for-tables))
  16083. ;; We can use my own converter with HTML conversions
  16084. (org-format-table-table-html lines)
  16085. ;; Need to use the code generator in table.el, with the original text.
  16086. (org-format-table-table-html-using-table-generate-source olines)))))
  16087. (defun org-format-org-table-html (lines)
  16088. "Format a table into HTML."
  16089. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  16090. (setq lines (nreverse lines))
  16091. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  16092. (setq lines (nreverse lines))
  16093. (when org-export-table-remove-special-lines
  16094. ;; Check if the table has a marking column. If yes remove the
  16095. ;; column and the special lines
  16096. (let* ((special
  16097. (not
  16098. (memq nil
  16099. (mapcar
  16100. (lambda (x)
  16101. (or (string-match "^[ \t]*|-" x)
  16102. (string-match "^[ \t]*| *\\([#!$*_^ ]\\) *|" x)))
  16103. lines)))))
  16104. (if special
  16105. (setq lines
  16106. (delq nil
  16107. (mapcar
  16108. (lambda (x)
  16109. (if (string-match "^[ \t]*| *[!_^] *|" x)
  16110. nil ; ignore this line
  16111. (and (or (string-match "^[ \t]*|-+\\+" x)
  16112. (string-match "^[ \t]*|[^|]*|" x))
  16113. (replace-match "|" t t x))))
  16114. lines))))))
  16115. (let ((head (and org-export-highlight-first-table-line
  16116. (delq nil (mapcar
  16117. (lambda (x) (string-match "^[ \t]*|-" x))
  16118. (cdr lines)))))
  16119. line fields html)
  16120. (setq html (concat org-export-html-table-tag "\n"))
  16121. (while (setq line (pop lines))
  16122. (catch 'next-line
  16123. (if (string-match "^[ \t]*|-" line)
  16124. (progn
  16125. (setq head nil) ;; head ends here, first time around
  16126. ;; ignore this line
  16127. (throw 'next-line t)))
  16128. ;; Break the line into fields
  16129. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  16130. (setq html (concat
  16131. html
  16132. "<tr>"
  16133. (mapconcat (lambda (x)
  16134. (if head
  16135. (concat "<th>" x "</th>")
  16136. (concat "<td>" x "</td>")))
  16137. fields "")
  16138. "</tr>\n"))))
  16139. (setq html (concat html "</table>\n"))
  16140. html))
  16141. (defun org-fake-empty-table-line (line)
  16142. "Replace everything except \"|\" with spaces."
  16143. (let ((i (length line))
  16144. (newstr (copy-sequence line)))
  16145. (while (> i 0)
  16146. (setq i (1- i))
  16147. (if (not (eq (aref newstr i) ?|))
  16148. (aset newstr i ?\ )))
  16149. newstr))
  16150. (defun org-format-table-table-html (lines)
  16151. "Format a table generated by table.el into HTML.
  16152. This conversion does *not* use `table-generate-source' from table.el.
  16153. This has the advantage that Org-mode's HTML conversions can be used.
  16154. But it has the disadvantage, that no cell- or row-spanning is allowed."
  16155. (let (line field-buffer
  16156. (head org-export-highlight-first-table-line)
  16157. fields html empty)
  16158. (setq html (concat org-export-html-table-tag "\n"))
  16159. (while (setq line (pop lines))
  16160. (setq empty "&nbsp;")
  16161. (catch 'next-line
  16162. (if (string-match "^[ \t]*\\+-" line)
  16163. (progn
  16164. (if field-buffer
  16165. (progn
  16166. (setq html (concat
  16167. html
  16168. "<tr>"
  16169. (mapconcat
  16170. (lambda (x)
  16171. (if (equal x "") (setq x empty))
  16172. (if head
  16173. (concat "<th>" x "</th>\n")
  16174. (concat "<td>" x "</td>\n")))
  16175. field-buffer "\n")
  16176. "</tr>\n"))
  16177. (setq head nil)
  16178. (setq field-buffer nil)))
  16179. ;; Ignore this line
  16180. (throw 'next-line t)))
  16181. ;; Break the line into fields and store the fields
  16182. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  16183. (if field-buffer
  16184. (setq field-buffer (mapcar
  16185. (lambda (x)
  16186. (concat x "<br/>" (pop fields)))
  16187. field-buffer))
  16188. (setq field-buffer fields))))
  16189. (setq html (concat html "</table>\n"))
  16190. html))
  16191. (defun org-format-table-table-html-using-table-generate-source (lines)
  16192. "Format a table into html, using `table-generate-source' from table.el.
  16193. This has the advantage that cell- or row-spanning is allowed.
  16194. But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
  16195. (require 'table)
  16196. (with-current-buffer (get-buffer-create " org-tmp1 ")
  16197. (erase-buffer)
  16198. (insert (mapconcat 'identity lines "\n"))
  16199. (goto-char (point-min))
  16200. (if (not (re-search-forward "|[^+]" nil t))
  16201. (error "Error processing table"))
  16202. (table-recognize-table)
  16203. (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
  16204. (table-generate-source 'html " org-tmp2 ")
  16205. (set-buffer " org-tmp2 ")
  16206. (buffer-substring (point-min) (point-max))))
  16207. (defun org-html-handle-time-stamps (s)
  16208. "Format time stamps in string S, or remove them."
  16209. (catch 'exit
  16210. (let (r b)
  16211. (while (string-match org-maybe-keyword-time-regexp s)
  16212. (if (and (match-end 1) (equal (match-string 1 s) org-clock-string))
  16213. ;; never export CLOCK
  16214. (throw 'exit ""))
  16215. (or b (setq b (substring s 0 (match-beginning 0))))
  16216. (if (not org-export-with-timestamps)
  16217. (setq r (concat r (substring s 0 (match-beginning 0)))
  16218. s (substring s (match-end 0)))
  16219. (setq r (concat
  16220. r (substring s 0 (match-beginning 0))
  16221. (if (match-end 1)
  16222. (format "@<span class=\"timestamp-kwd\">%s @</span>"
  16223. (match-string 1 s)))
  16224. (format " @<span class=\"timestamp\">%s@</span>"
  16225. (substring
  16226. (org-translate-time (match-string 3 s)) 1 -1)))
  16227. s (substring s (match-end 0)))))
  16228. ;; Line break if line started and ended with time stamp stuff
  16229. (if (not r)
  16230. s
  16231. (setq r (concat r s))
  16232. (unless (string-match "\\S-" (concat b s))
  16233. (setq r (concat r "@<br/>")))
  16234. r))))
  16235. (defun org-html-protect (s)
  16236. ;; convert & to &amp;, < to &lt; and > to &gt;
  16237. (let ((start 0))
  16238. (while (string-match "&" s start)
  16239. (setq s (replace-match "&amp;" t t s)
  16240. start (1+ (match-beginning 0))))
  16241. (while (string-match "<" s)
  16242. (setq s (replace-match "&lt;" t t s)))
  16243. (while (string-match ">" s)
  16244. (setq s (replace-match "&gt;" t t s))))
  16245. s)
  16246. (defun org-export-cleanup-toc-line (s)
  16247. "Remove tags and time staps from lines going into the toc."
  16248. (if (string-match " +:[a-zA-Z0-9_@:]+: *$" s)
  16249. (setq s (replace-match "" t t s)))
  16250. (when org-export-remove-timestamps-from-toc
  16251. (while (string-match org-maybe-keyword-time-regexp s)
  16252. (setq s (replace-match "" t t s))))
  16253. s)
  16254. (defun org-html-expand (string)
  16255. "Prepare STRING for HTML export. Applies all active conversions.
  16256. If there are links in the string, don't modify these."
  16257. (let* (m s l res)
  16258. (while (setq m (string-match org-bracket-link-regexp string))
  16259. (setq s (substring string 0 m)
  16260. l (match-string 0 string)
  16261. string (substring string (match-end 0)))
  16262. (push (org-html-do-expand s) res)
  16263. (push l res))
  16264. (push (org-html-do-expand string) res)
  16265. (apply 'concat (nreverse res))))
  16266. (defun org-html-do-expand (s)
  16267. "Apply all active conversions to translate special ASCII to HTML."
  16268. (setq s (org-html-protect s))
  16269. (if org-export-html-expand
  16270. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  16271. (setq s (replace-match "<\\1>" t nil s))))
  16272. (if org-export-with-emphasize
  16273. (setq s (org-export-html-convert-emphasize s)))
  16274. (if org-export-with-sub-superscripts
  16275. (setq s (org-export-html-convert-sub-super s)))
  16276. (if org-export-with-TeX-macros
  16277. (let ((start 0) wd ass)
  16278. (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)" s start))
  16279. (setq wd (match-string 1 s))
  16280. (if (setq ass (assoc wd org-html-entities))
  16281. (setq s (replace-match (or (cdr ass)
  16282. (concat "&" (car ass) ";"))
  16283. t t s))
  16284. (setq start (+ start (length wd)))))))
  16285. s)
  16286. (defun org-create-multibrace-regexp (left right n)
  16287. "Create a regular expression which will match a balanced sexp.
  16288. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  16289. as single character strings.
  16290. The regexp returned will match the entire expression including the
  16291. delimiters. It will also define a single group which contains the
  16292. match except for the outermost delimiters. The maximum depth of
  16293. stacked delimiters is N. Escaping delimiters is not possible."
  16294. (let* ((nothing (concat "[^" "\\" left "\\" right "]*?"))
  16295. (or "\\|")
  16296. (re nothing)
  16297. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  16298. (while (> n 1)
  16299. (setq n (1- n)
  16300. re (concat re or next)
  16301. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  16302. (concat left "\\(" re "\\)" right)))
  16303. (defvar org-match-substring-regexp
  16304. (concat
  16305. "\\([^\\]\\)\\([_^]\\)\\("
  16306. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  16307. "\\|"
  16308. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  16309. "\\|"
  16310. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  16311. "The regular expression matching a sub- or superscript.")
  16312. (defun org-export-html-convert-sub-super (string)
  16313. "Convert sub- and superscripts in STRING to HTML."
  16314. (let (key c)
  16315. (while (string-match org-match-substring-regexp string)
  16316. (setq key (if (string= (match-string 2 string) "_") "sub" "sup"))
  16317. (setq c (or (match-string 8 string)
  16318. (match-string 6 string)
  16319. (match-string 5 string)))
  16320. (setq string (replace-match
  16321. (concat (match-string 1 string)
  16322. "<" key ">" c "</" key ">")
  16323. t t string)))
  16324. (while (string-match "\\\\\\([_^]\\)" string)
  16325. (setq string (replace-match (match-string 1 string) t t string))))
  16326. string)
  16327. (defun org-export-html-convert-emphasize (string)
  16328. "Apply emphasis."
  16329. (while (string-match org-emph-re string)
  16330. (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)))
  16331. string)
  16332. (defvar org-par-open nil)
  16333. (defun org-open-par ()
  16334. "Insert <p>, but first close previous paragraph if any."
  16335. (org-close-par-maybe)
  16336. (insert "\n<p>")
  16337. (setq org-par-open t))
  16338. (defun org-close-par-maybe ()
  16339. "Close paragraph if there is one open."
  16340. (when org-par-open
  16341. (insert "</p>")
  16342. (setq org-par-open nil)))
  16343. (defun org-close-li ()
  16344. "Close <li> if necessary."
  16345. (org-close-par-maybe)
  16346. (insert "</li>\n"))
  16347. ; (when (save-excursion
  16348. ; (re-search-backward "</?\\(ul\\|ol\\|li\\|[hH][0-9]\\)>" nil t))
  16349. ; (if (member (match-string 0) '("</ul>" "</ol>" "<li>"))
  16350. ; (insert "</li>"))))
  16351. (defun org-html-level-start (level title umax with-toc head-count)
  16352. "Insert a new level in HTML export.
  16353. When TITLE is nil, just close all open levels."
  16354. (org-close-par-maybe)
  16355. (let ((l (1+ (max level umax))))
  16356. (while (<= l org-level-max)
  16357. (if (aref org-levels-open (1- l))
  16358. (progn
  16359. (org-html-level-close l)
  16360. (aset org-levels-open (1- l) nil)))
  16361. (setq l (1+ l)))
  16362. (when title
  16363. ;; If title is nil, this means this function is called to close
  16364. ;; all levels, so the rest is done only if title is given
  16365. (when (string-match "\\(:[a-zA-Z0-9_@:]+:\\)[ \t]*$" title)
  16366. (setq title (replace-match
  16367. (if org-export-with-tags
  16368. (save-match-data
  16369. (concat
  16370. "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
  16371. (mapconcat 'identity (org-split-string
  16372. (match-string 1 title) ":")
  16373. "&nbsp;")
  16374. "</span>"))
  16375. "")
  16376. t t title)))
  16377. (if (> level umax)
  16378. (progn
  16379. (if (aref org-levels-open (1- level))
  16380. (progn
  16381. (org-close-li)
  16382. (insert "<li>" title "<br/>\n"))
  16383. (aset org-levels-open (1- level) t)
  16384. (org-close-par-maybe)
  16385. (insert "<ul>\n<li>" title "<br/>\n")))
  16386. (if org-export-with-section-numbers
  16387. (setq title (concat (org-section-number level) " " title)))
  16388. (setq level (+ level org-export-html-toplevel-hlevel -1))
  16389. (if with-toc
  16390. (insert (format "\n<h%d><a name=\"sec-%d\">%s</a></h%d>\n"
  16391. level head-count title level))
  16392. (insert (format "\n<h%d>%s</h%d>\n" level title level)))
  16393. (org-open-par)))))
  16394. (defun org-html-level-close (&rest args)
  16395. "Terminate one level in HTML export."
  16396. (org-close-li)
  16397. (insert "</ul>"))
  16398. ;;; iCalendar export
  16399. ;;;###autoload
  16400. (defun org-export-icalendar-this-file ()
  16401. "Export current file as an iCalendar file.
  16402. The iCalendar file will be located in the same directory as the Org-mode
  16403. file, but with extension `.ics'."
  16404. (interactive)
  16405. (org-export-icalendar nil buffer-file-name))
  16406. ;;;###autoload
  16407. (defun org-export-icalendar-all-agenda-files ()
  16408. "Export all files in `org-agenda-files' to iCalendar .ics files.
  16409. Each iCalendar file will be located in the same directory as the Org-mode
  16410. file, but with extension `.ics'."
  16411. (interactive)
  16412. (apply 'org-export-icalendar nil (org-agenda-files t)))
  16413. ;;;###autoload
  16414. (defun org-export-icalendar-combine-agenda-files ()
  16415. "Export all files in `org-agenda-files' to a single combined iCalendar file.
  16416. The file is stored under the name `org-combined-agenda-icalendar-file'."
  16417. (interactive)
  16418. (apply 'org-export-icalendar t (org-agenda-files t)))
  16419. (defun org-export-icalendar (combine &rest files)
  16420. "Create iCalendar files for all elements of FILES.
  16421. If COMBINE is non-nil, combine all calendar entries into a single large
  16422. file and store it under the name `org-combined-agenda-icalendar-file'."
  16423. (save-excursion
  16424. (let* ((dir (org-export-directory
  16425. :ical (list :publishing-directory
  16426. org-export-publishing-directory)))
  16427. file ical-file ical-buffer category started org-agenda-new-buffers)
  16428. (when combine
  16429. (setq ical-file
  16430. (if (file-name-absolute-p org-combined-agenda-icalendar-file)
  16431. org-combined-agenda-icalendar-file
  16432. (expand-file-name org-combined-agenda-icalendar-file dir))
  16433. ical-buffer (org-get-agenda-file-buffer ical-file))
  16434. (set-buffer ical-buffer) (erase-buffer))
  16435. (while (setq file (pop files))
  16436. (catch 'nextfile
  16437. (org-check-agenda-file file)
  16438. (set-buffer (org-get-agenda-file-buffer file))
  16439. (unless combine
  16440. (setq ical-file (concat (file-name-as-directory dir)
  16441. (file-name-sans-extension
  16442. (file-name-nondirectory buffer-file-name))
  16443. ".ics"))
  16444. (setq ical-buffer (org-get-agenda-file-buffer ical-file))
  16445. (with-current-buffer ical-buffer (erase-buffer)))
  16446. (setq category (or org-category
  16447. (file-name-sans-extension
  16448. (file-name-nondirectory buffer-file-name))))
  16449. (if (symbolp category) (setq category (symbol-name category)))
  16450. (let ((standard-output ical-buffer))
  16451. (if combine
  16452. (and (not started) (setq started t)
  16453. (org-start-icalendar-file org-icalendar-combined-name))
  16454. (org-start-icalendar-file category))
  16455. (org-print-icalendar-entries combine)
  16456. (when (or (and combine (not files)) (not combine))
  16457. (org-finish-icalendar-file)
  16458. (set-buffer ical-buffer)
  16459. (save-buffer)
  16460. (run-hooks 'org-after-save-iCalendar-file-hook)))))
  16461. (org-release-buffers org-agenda-new-buffers))))
  16462. (defvar org-after-save-iCalendar-file-hook nil
  16463. "Hook run after an iCalendar file has been saved.
  16464. The iCalendar buffer is still current when this hook is run.
  16465. A good way to use this is to tell a desktop calenndar application to re-read
  16466. the iCalendar file.")
  16467. (defun org-print-icalendar-entries (&optional combine)
  16468. "Print iCalendar entries for the current Org-mode file to `standard-output'.
  16469. When COMBINE is non nil, add the category to each line."
  16470. (let ((re2 (concat "--?-?\\(" org-ts-regexp "\\)"))
  16471. (org-category-table (org-get-category-table))
  16472. (dts (org-ical-ts-to-string
  16473. (format-time-string (cdr org-time-stamp-formats) (current-time))
  16474. "DTSTART"))
  16475. hd ts ts2 state (inc t) pos scheduledp deadlinep tmp pri category)
  16476. (save-excursion
  16477. (goto-char (point-min))
  16478. (while (re-search-forward org-ts-regexp nil t)
  16479. (setq pos (match-beginning 0)
  16480. ts (match-string 0)
  16481. inc t
  16482. hd (org-get-heading)
  16483. category (org-get-category))
  16484. (if (looking-at re2)
  16485. (progn
  16486. (goto-char (match-end 0))
  16487. (setq ts2 (match-string 1) inc nil))
  16488. (setq ts2 ts
  16489. tmp (buffer-substring (max (point-min)
  16490. (- pos org-ds-keyword-length))
  16491. pos)
  16492. deadlinep (string-match org-deadline-regexp tmp)
  16493. scheduledp (string-match org-scheduled-regexp tmp)
  16494. ;; donep (org-entry-is-done-p)
  16495. ))
  16496. (if (or (string-match org-tr-regexp hd)
  16497. (string-match org-ts-regexp hd))
  16498. (setq hd (replace-match "" t t hd)))
  16499. (if (string-match org-bracket-link-regexp hd)
  16500. (setq hd (replace-match (if (match-end 3) (match-string 3 hd)
  16501. (match-string 1 hd))
  16502. t t hd)))
  16503. (if deadlinep (setq hd (concat "DL: " hd)))
  16504. (if scheduledp (setq hd (concat "S: " hd)))
  16505. (princ (format "BEGIN:VEVENT
  16506. %s
  16507. %s
  16508. SUMMARY:%s
  16509. CATEGORIES:%s
  16510. END:VEVENT\n"
  16511. (org-ical-ts-to-string ts "DTSTART")
  16512. (org-ical-ts-to-string ts2 "DTEND" inc)
  16513. hd category)))
  16514. (when org-icalendar-include-todo
  16515. (goto-char (point-min))
  16516. (while (re-search-forward org-todo-line-regexp nil t)
  16517. (setq state (match-string 1))
  16518. (unless (equal state org-done-string)
  16519. (setq hd (match-string 3))
  16520. (if (string-match org-priority-regexp hd)
  16521. (setq pri (string-to-char (match-string 2 hd))
  16522. hd (concat (substring hd 0 (match-beginning 1))
  16523. (substring hd (- (match-end 1)))))
  16524. (setq pri org-default-priority))
  16525. (setq pri (floor (1+ (* 8. (/ (float (- org-lowest-priority pri))
  16526. (- org-lowest-priority ?A))))))
  16527. (princ (format "BEGIN:VTODO
  16528. %s
  16529. SUMMARY:%s
  16530. CATEGORIES:%s
  16531. SEQUENCE:1
  16532. PRIORITY:%d
  16533. END:VTODO\n"
  16534. dts hd category pri))))))))
  16535. (defun org-start-icalendar-file (name)
  16536. "Start an iCalendar file by inserting the header."
  16537. (let ((user user-full-name)
  16538. (name (or name "unknown"))
  16539. (timezone (cadr (current-time-zone))))
  16540. (princ
  16541. (format "BEGIN:VCALENDAR
  16542. VERSION:2.0
  16543. X-WR-CALNAME:%s
  16544. PRODID:-//%s//Emacs with Org-mode//EN
  16545. X-WR-TIMEZONE:%s
  16546. CALSCALE:GREGORIAN\n" name user timezone))))
  16547. (defun org-finish-icalendar-file ()
  16548. "Finish an iCalendar file by inserting the END statement."
  16549. (princ "END:VCALENDAR\n"))
  16550. (defun org-ical-ts-to-string (s keyword &optional inc)
  16551. "Take a time string S and convert it to iCalendar format.
  16552. KEYWORD is added in front, to make a complete line like DTSTART....
  16553. When INC is non-nil, increase the hour by two (if time string contains
  16554. a time), or the day by one (if it does not contain a time)."
  16555. (let ((t1 (org-parse-time-string s 'nodefault))
  16556. t2 fmt have-time time)
  16557. (if (and (car t1) (nth 1 t1) (nth 2 t1))
  16558. (setq t2 t1 have-time t)
  16559. (setq t2 (org-parse-time-string s)))
  16560. (let ((s (car t2)) (mi (nth 1 t2)) (h (nth 2 t2))
  16561. (d (nth 3 t2)) (m (nth 4 t2)) (y (nth 5 t2)))
  16562. (when inc
  16563. (if have-time (setq h (+ 2 h)) (setq d (1+ d))))
  16564. (setq time (encode-time s mi h d m y)))
  16565. (setq fmt (if have-time ":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
  16566. (concat keyword (format-time-string fmt time))))
  16567. ;;; XOXO export
  16568. (defun org-export-as-xoxo-insert-into (buffer &rest output)
  16569. (with-current-buffer buffer
  16570. (apply 'insert output)))
  16571. (put 'org-export-as-xoxo-insert-into 'lisp-indent-function 1)
  16572. (defun org-export-as-xoxo (&optional buffer)
  16573. "Export the org buffer as XOXO.
  16574. The XOXO buffer is named *xoxo-<source buffer name>*"
  16575. (interactive (list (current-buffer)))
  16576. ;; A quickie abstraction
  16577. ;; Output everything as XOXO
  16578. (with-current-buffer (get-buffer buffer)
  16579. (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
  16580. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  16581. (org-infile-export-plist)))
  16582. (filename (concat (file-name-as-directory
  16583. (org-export-directory :xoxo opt-plist))
  16584. (file-name-sans-extension
  16585. (file-name-nondirectory buffer-file-name))
  16586. ".html"))
  16587. (out (find-file-noselect filename))
  16588. (last-level 1)
  16589. (hanging-li nil))
  16590. ;; Check the output buffer is empty.
  16591. (with-current-buffer out (erase-buffer))
  16592. ;; Kick off the output
  16593. (org-export-as-xoxo-insert-into out "<ol class='xoxo'>\n")
  16594. (while (re-search-forward "^\\(\\*+\\) \\(.+\\)" (point-max) 't)
  16595. (let* ((hd (match-string-no-properties 1))
  16596. (level (length hd))
  16597. (text (concat
  16598. (match-string-no-properties 2)
  16599. (save-excursion
  16600. (goto-char (match-end 0))
  16601. (let ((str ""))
  16602. (catch 'loop
  16603. (while 't
  16604. (forward-line)
  16605. (if (looking-at "^[ \t]\\(.*\\)")
  16606. (setq str (concat str (match-string-no-properties 1)))
  16607. (throw 'loop str)))))))))
  16608. ;; Handle level rendering
  16609. (cond
  16610. ((> level last-level)
  16611. (org-export-as-xoxo-insert-into out "\n<ol>\n"))
  16612. ((< level last-level)
  16613. (dotimes (- (- last-level level) 1)
  16614. (if hanging-li
  16615. (org-export-as-xoxo-insert-into out "</li>\n"))
  16616. (org-export-as-xoxo-insert-into out "</ol>\n"))
  16617. (when hanging-li
  16618. (org-export-as-xoxo-insert-into out "</li>\n")
  16619. (setq hanging-li nil)))
  16620. ((equal level last-level)
  16621. (if hanging-li
  16622. (org-export-as-xoxo-insert-into out "</li>\n")))
  16623. )
  16624. (setq last-level level)
  16625. ;; And output the new li
  16626. (setq hanging-li 't)
  16627. (if (equal ?+ (elt text 0))
  16628. (org-export-as-xoxo-insert-into out "<li class='" (substring text 1) "'>")
  16629. (org-export-as-xoxo-insert-into out "<li>" text))))
  16630. ;; Finally finish off the ol
  16631. (dotimes (- last-level 1)
  16632. (if hanging-li
  16633. (org-export-as-xoxo-insert-into out "</li>\n"))
  16634. (org-export-as-xoxo-insert-into out "</ol>\n"))
  16635. ;; Finish the buffer off and clean it up.
  16636. (switch-to-buffer-other-window out)
  16637. (indent-region (point-min) (point-max) nil)
  16638. (save-buffer)
  16639. (goto-char (point-min))
  16640. )))
  16641. ;;;; Key bindings
  16642. ;; - Bindings in Org-mode map are currently
  16643. ;; 0123456789abcdefghijklmnopqrstuvwxyz!?@#$%^&-+*/=()_{}[]:;"|,.<>~`'\t the alphabet
  16644. ;; abcd fgh j lmnopqrstuvwxyz!? #$ ^ -+*/= [] ; |,.<>~ '\t necessary bindings
  16645. ;; e (?) useful from outline-mode
  16646. ;; i k @ expendable from outline-mode
  16647. ;; 0123456789 % & ()_{} " ` free
  16648. ;; Make `C-c C-x' a prefix key
  16649. (define-key org-mode-map "\C-c\C-x" (make-sparse-keymap))
  16650. ;; TAB key with modifiers
  16651. (define-key org-mode-map "\C-i" 'org-cycle)
  16652. (define-key org-mode-map [(tab)] 'org-cycle)
  16653. (define-key org-mode-map [(control tab)] 'org-force-cycle-archived)
  16654. (define-key org-mode-map [(meta tab)] 'org-complete)
  16655. (define-key org-mode-map "\M-\t" 'org-complete)
  16656. (define-key org-mode-map "\M-\C-i" 'org-complete)
  16657. ;; The following line is necessary under Suse GNU/Linux
  16658. (unless (featurep 'xemacs)
  16659. (define-key org-mode-map [S-iso-lefttab] 'org-shifttab))
  16660. (define-key org-mode-map [(shift tab)] 'org-shifttab)
  16661. (define-key org-mode-map (org-key 'S-return) 'org-table-copy-down)
  16662. (define-key org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  16663. (define-key org-mode-map [(meta return)] 'org-meta-return)
  16664. ;; Cursor keys with modifiers
  16665. (define-key org-mode-map [(meta left)] 'org-metaleft)
  16666. (define-key org-mode-map [(meta right)] 'org-metaright)
  16667. (define-key org-mode-map [(meta up)] 'org-metaup)
  16668. (define-key org-mode-map [(meta down)] 'org-metadown)
  16669. (define-key org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  16670. (define-key org-mode-map [(meta shift right)] 'org-shiftmetaright)
  16671. (define-key org-mode-map [(meta shift up)] 'org-shiftmetaup)
  16672. (define-key org-mode-map [(meta shift down)] 'org-shiftmetadown)
  16673. (define-key org-mode-map (org-key 'S-up) 'org-shiftup)
  16674. (define-key org-mode-map (org-key 'S-down) 'org-shiftdown)
  16675. (define-key org-mode-map (org-key 'S-left) 'org-shiftleft)
  16676. (define-key org-mode-map (org-key 'S-right) 'org-shiftright)
  16677. ;;; Extra keys for tty access.
  16678. ;; We only set them when really needed because otherwise the
  16679. ;; menus don't show the simple keys
  16680. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  16681. (not window-system))
  16682. (define-key org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  16683. (define-key org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  16684. (define-key org-mode-map "\C-c\C-xm" 'org-meta-return)
  16685. (define-key org-mode-map [?\e (return)] 'org-meta-return)
  16686. (define-key org-mode-map [?\e (left)] 'org-metaleft)
  16687. (define-key org-mode-map "\C-c\C-xl" 'org-metaleft)
  16688. (define-key org-mode-map [?\e (right)] 'org-metaright)
  16689. (define-key org-mode-map "\C-c\C-xr" 'org-metaright)
  16690. (define-key org-mode-map [?\e (up)] 'org-metaup)
  16691. (define-key org-mode-map "\C-c\C-xu" 'org-metaup)
  16692. (define-key org-mode-map [?\e (down)] 'org-metadown)
  16693. (define-key org-mode-map "\C-c\C-xd" 'org-metadown)
  16694. (define-key org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  16695. (define-key org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  16696. (define-key org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  16697. (define-key org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  16698. (define-key org-mode-map [?\C-c ?\C-x (up)] 'org-shiftup)
  16699. (define-key org-mode-map [?\C-c ?\C-x (down)] 'org-shiftdown)
  16700. (define-key org-mode-map [?\C-c ?\C-x (left)] 'org-shiftleft)
  16701. (define-key org-mode-map [?\C-c ?\C-x (right)] 'org-shiftright))
  16702. ;; All the other keys
  16703. (define-key org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  16704. (define-key org-mode-map "\C-c\C-r" 'org-reveal)
  16705. (define-key org-mode-map "\C-xns" 'org-narrow-to-subtree)
  16706. (define-key org-mode-map "\C-c$" 'org-archive-subtree)
  16707. (define-key org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  16708. (define-key org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  16709. (define-key org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  16710. (define-key org-mode-map "\C-c\C-j" 'org-goto)
  16711. (define-key org-mode-map "\C-c\C-t" 'org-todo)
  16712. (define-key org-mode-map "\C-c\C-s" 'org-schedule)
  16713. (define-key org-mode-map "\C-c\C-d" 'org-deadline)
  16714. (define-key org-mode-map "\C-c;" 'org-toggle-comment)
  16715. (define-key org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  16716. (define-key org-mode-map "\C-c\C-w" 'org-check-deadlines)
  16717. (define-key org-mode-map "\C-c/" 'org-occur) ; Minor-mode reserved
  16718. (define-key org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  16719. (define-key org-mode-map "\C-c\C-m" 'org-insert-heading)
  16720. (define-key org-mode-map "\M-\C-m" 'org-insert-heading)
  16721. (define-key org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  16722. (define-key org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  16723. (define-key org-mode-map "\C-c\C-l" 'org-insert-link)
  16724. (define-key org-mode-map "\C-c\C-o" 'org-open-at-point)
  16725. (define-key org-mode-map "\C-c%" 'org-mark-ring-push)
  16726. (define-key org-mode-map "\C-c&" 'org-mark-ring-goto)
  16727. (define-key org-mode-map "\C-c\C-z" 'org-time-stamp) ; Alternative binding
  16728. (define-key org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  16729. (define-key org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  16730. (define-key org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  16731. (define-key org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  16732. (define-key org-mode-map "\C-c>" 'org-goto-calendar)
  16733. (define-key org-mode-map "\C-c<" 'org-date-from-calendar)
  16734. (define-key org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  16735. (define-key org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  16736. (define-key org-mode-map "\C-c[" 'org-agenda-file-to-front)
  16737. (define-key org-mode-map "\C-c]" 'org-remove-file)
  16738. (define-key org-mode-map "\C-c-" 'org-table-insert-hline)
  16739. (define-key org-mode-map "\C-c^" 'org-sort)
  16740. (define-key org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  16741. (define-key org-mode-map "\C-c#" 'org-update-checkbox-count)
  16742. (define-key org-mode-map "\C-m" 'org-return)
  16743. (define-key org-mode-map "\C-c?" 'org-table-field-info)
  16744. (define-key org-mode-map "\C-c " 'org-table-blank-field)
  16745. (define-key org-mode-map "\C-c+" 'org-table-sum)
  16746. (define-key org-mode-map "\C-c=" 'org-table-eval-formula)
  16747. (define-key org-mode-map "\C-c'" 'org-table-edit-formulas)
  16748. (define-key org-mode-map "\C-c`" 'org-table-edit-field)
  16749. (define-key org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  16750. (define-key org-mode-map "\C-c*" 'org-table-recalculate)
  16751. (define-key org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  16752. (define-key org-mode-map "\C-c~" 'org-table-create-with-table.el)
  16753. (define-key org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  16754. (define-key org-mode-map "\C-c\C-e" 'org-export)
  16755. (define-key org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  16756. (define-key org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  16757. (define-key org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  16758. (define-key org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  16759. (define-key org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  16760. (define-key org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  16761. (define-key org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  16762. (define-key org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  16763. (define-key org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  16764. (define-key org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  16765. (define-key org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  16766. (define-key org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  16767. (define-key org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  16768. (define-key org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  16769. (when (featurep 'xemacs)
  16770. (define-key org-mode-map 'button3 'popup-mode-menu))
  16771. (defsubst org-table-p () (org-at-table-p))
  16772. (defun org-self-insert-command (N)
  16773. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  16774. If the cursor is in a table looking at whitespace, the whitespace is
  16775. overwritten, and the table is not marked as requiring realignment."
  16776. (interactive "p")
  16777. (if (and (org-table-p)
  16778. (progn
  16779. ;; check if we blank the field, and if that triggers align
  16780. (and org-table-auto-blank-field
  16781. (member last-command
  16782. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  16783. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  16784. ;; got extra space, this field does not determine column width
  16785. (let (org-table-may-need-update) (org-table-blank-field))
  16786. ;; no extra space, this field may determine column width
  16787. (org-table-blank-field)))
  16788. t)
  16789. (eq N 1)
  16790. (looking-at "[^|\n]* |"))
  16791. (let (org-table-may-need-update)
  16792. (goto-char (1- (match-end 0)))
  16793. (delete-backward-char 1)
  16794. (goto-char (match-beginning 0))
  16795. (self-insert-command N))
  16796. (setq org-table-may-need-update t)
  16797. (self-insert-command N)))
  16798. (defun org-delete-backward-char (N)
  16799. "Like `delete-backward-char', insert whitespace at field end in tables.
  16800. When deleting backwards, in tables this function will insert whitespace in
  16801. front of the next \"|\" separator, to keep the table aligned. The table will
  16802. still be marked for re-alignment if the field did fill the entire column,
  16803. because, in this case the deletion might narrow the column."
  16804. (interactive "p")
  16805. (if (and (org-table-p)
  16806. (eq N 1)
  16807. (string-match "|" (buffer-substring (point-at-bol) (point)))
  16808. (looking-at ".*?|"))
  16809. (let ((pos (point))
  16810. (noalign (looking-at "[^|\n\r]* |"))
  16811. (c org-table-may-need-update))
  16812. (backward-delete-char N)
  16813. (skip-chars-forward "^|")
  16814. (insert " ")
  16815. (goto-char (1- pos))
  16816. ;; noalign: if there were two spaces at the end, this field
  16817. ;; does not determine the width of the column.
  16818. (if noalign (setq org-table-may-need-update c)))
  16819. (backward-delete-char N)))
  16820. (defun org-delete-char (N)
  16821. "Like `delete-char', but insert whitespace at field end in tables.
  16822. When deleting characters, in tables this function will insert whitespace in
  16823. front of the next \"|\" separator, to keep the table aligned. The table will
  16824. still be marked for re-alignment if the field did fill the entire column,
  16825. because, in this case the deletion might narrow the column."
  16826. (interactive "p")
  16827. (if (and (org-table-p)
  16828. (not (bolp))
  16829. (not (= (char-after) ?|))
  16830. (eq N 1))
  16831. (if (looking-at ".*?|")
  16832. (let ((pos (point))
  16833. (noalign (looking-at "[^|\n\r]* |"))
  16834. (c org-table-may-need-update))
  16835. (replace-match (concat
  16836. (substring (match-string 0) 1 -1)
  16837. " |"))
  16838. (goto-char pos)
  16839. ;; noalign: if there were two spaces at the end, this field
  16840. ;; does not determine the width of the column.
  16841. (if noalign (setq org-table-may-need-update c)))
  16842. (delete-char N))
  16843. (delete-char N)))
  16844. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  16845. (put 'org-self-insert-command 'delete-selection t)
  16846. (put 'orgtbl-self-insert-command 'delete-selection t)
  16847. (put 'org-delete-char 'delete-selection 'supersede)
  16848. (put 'org-delete-backward-char 'delete-selection 'supersede)
  16849. ;; How to do this: Measure non-white length of current string
  16850. ;; If equal to column width, we should realign.
  16851. (defun org-remap (map &rest commands)
  16852. "In MAP, remap the functions given in COMMANDS.
  16853. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  16854. (let (new old)
  16855. (while commands
  16856. (setq old (pop commands) new (pop commands))
  16857. (if (fboundp 'command-remapping)
  16858. (define-key map (vector 'remap old) new)
  16859. (substitute-key-definition old new map global-map)))))
  16860. (when (eq org-enable-table-editor 'optimized)
  16861. ;; If the user wants maximum table support, we need to hijack
  16862. ;; some standard editing functions
  16863. (org-remap org-mode-map
  16864. 'self-insert-command 'org-self-insert-command
  16865. 'delete-char 'org-delete-char
  16866. 'delete-backward-char 'org-delete-backward-char)
  16867. (define-key org-mode-map "|" 'org-force-self-insert))
  16868. (defun org-shiftcursor-error ()
  16869. "Throw an error because Shift-Cursor command was applied in wrong context."
  16870. (error "This command is active in special context like tables, headlines or timestamps"))
  16871. (defun org-shifttab (&optional arg)
  16872. "Global visibility cycling or move to previous table field.
  16873. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  16874. on context.
  16875. See the individual commands for more information."
  16876. (interactive "P")
  16877. (cond
  16878. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  16879. (t (call-interactively 'org-global-cycle))))
  16880. (defun org-shiftmetaleft ()
  16881. "Promote subtree or delete table column.
  16882. Calls `org-promote-subtree' or `org-table-delete-column', depending on context.
  16883. See the individual commands for more information."
  16884. (interactive)
  16885. (cond
  16886. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  16887. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  16888. ((org-at-item-p) (call-interactively 'org-outdent-item))
  16889. (t (org-shiftcursor-error))))
  16890. (defun org-shiftmetaright ()
  16891. "Demote subtree or insert table column.
  16892. Calls `org-demote-subtree' or `org-table-insert-column', depending on context.
  16893. See the individual commands for more information."
  16894. (interactive)
  16895. (cond
  16896. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  16897. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  16898. ((org-at-item-p) (call-interactively 'org-indent-item))
  16899. (t (org-shiftcursor-error))))
  16900. (defun org-shiftmetaup (&optional arg)
  16901. "Move subtree up or kill table row.
  16902. Calls `org-move-subtree-up' or `org-table-kill-row' or
  16903. `org-move-item-up' depending on context. See the individual commands
  16904. for more information."
  16905. (interactive "P")
  16906. (cond
  16907. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  16908. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  16909. ((org-at-item-p) (call-interactively 'org-move-item-up))
  16910. (t (org-shiftcursor-error))))
  16911. (defun org-shiftmetadown (&optional arg)
  16912. "Move subtree down or insert table row.
  16913. Calls `org-move-subtree-down' or `org-table-insert-row' or
  16914. `org-move-item-down', depending on context. See the individual
  16915. commands for more information."
  16916. (interactive "P")
  16917. (cond
  16918. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  16919. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  16920. ((org-at-item-p) (call-interactively 'org-move-item-down))
  16921. (t (org-shiftcursor-error))))
  16922. (defun org-metaleft (&optional arg)
  16923. "Promote heading or move table column to left.
  16924. Calls `org-do-promote' or `org-table-move-column', depending on context.
  16925. With no specific context, calls the Emacs default `backward-word'.
  16926. See the individual commands for more information."
  16927. (interactive "P")
  16928. (cond
  16929. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  16930. ((or (org-on-heading-p) (org-region-active-p))
  16931. (call-interactively 'org-do-promote))
  16932. ((org-at-item-p) (call-interactively 'org-outdent-item))
  16933. (t (call-interactively 'backward-word))))
  16934. (defun org-metaright (&optional arg)
  16935. "Demote subtree or move table column to right.
  16936. Calls `org-do-demote' or `org-table-move-column', depending on context.
  16937. With no specific context, calls the Emacs default `forward-word'.
  16938. See the individual commands for more information."
  16939. (interactive "P")
  16940. (cond
  16941. ((org-at-table-p) (call-interactively 'org-table-move-column))
  16942. ((or (org-on-heading-p) (org-region-active-p))
  16943. (call-interactively 'org-do-demote))
  16944. ((org-at-item-p) (call-interactively 'org-indent-item))
  16945. (t (call-interactively 'forward-word))))
  16946. (defun org-metaup (&optional arg)
  16947. "Move subtree up or move table row up.
  16948. Calls `org-move-subtree-up' or `org-table-move-row' or
  16949. `org-move-item-up', depending on context. See the individual commands
  16950. for more information."
  16951. (interactive "P")
  16952. (cond
  16953. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  16954. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  16955. ((org-at-item-p) (call-interactively 'org-move-item-up))
  16956. (t (org-shiftcursor-error))))
  16957. (defun org-metadown (&optional arg)
  16958. "Move subtree down or move table row down.
  16959. Calls `org-move-subtree-down' or `org-table-move-row' or
  16960. `org-move-item-down', depending on context. See the individual
  16961. commands for more information."
  16962. (interactive "P")
  16963. (cond
  16964. ((org-at-table-p) (call-interactively 'org-table-move-row))
  16965. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  16966. ((org-at-item-p) (call-interactively 'org-move-item-down))
  16967. (t (org-shiftcursor-error))))
  16968. (defun org-shiftup (&optional arg)
  16969. "Increase item in timestamp or increase priority of current headline.
  16970. Calls `org-timestamp-up' or `org-priority-up', depending on context.
  16971. See the individual commands for more information."
  16972. (interactive "P")
  16973. (cond
  16974. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up))
  16975. ((org-on-heading-p) (call-interactively 'org-priority-up))
  16976. ((org-at-item-p) (call-interactively 'org-previous-item))
  16977. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  16978. (defun org-shiftdown (&optional arg)
  16979. "Decrease item in timestamp or decrease priority of current headline.
  16980. Calls `org-timestamp-down' or `org-priority-down', depending on context.
  16981. See the individual commands for more information."
  16982. (interactive "P")
  16983. (cond
  16984. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down))
  16985. ((org-on-heading-p) (call-interactively 'org-priority-down))
  16986. (t (call-interactively 'org-next-item))))
  16987. (defun org-shiftright ()
  16988. "Next TODO keyword or timestamp one day later, depending on context."
  16989. (interactive)
  16990. (cond
  16991. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  16992. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  16993. (t (org-shiftcursor-error))))
  16994. (defun org-shiftleft ()
  16995. "Previous TODO keyword or timestamp one day earlier, depending on context."
  16996. (interactive)
  16997. (cond
  16998. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  16999. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  17000. (t (org-shiftcursor-error))))
  17001. (defun org-copy-special ()
  17002. "Copy region in table or copy current subtree.
  17003. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  17004. See the individual commands for more information."
  17005. (interactive)
  17006. (call-interactively
  17007. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  17008. (defun org-cut-special ()
  17009. "Cut region in table or cut current subtree.
  17010. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  17011. See the individual commands for more information."
  17012. (interactive)
  17013. (call-interactively
  17014. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  17015. (defun org-paste-special (arg)
  17016. "Paste rectangular region into table, or past subtree relative to level.
  17017. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  17018. See the individual commands for more information."
  17019. (interactive "P")
  17020. (if (org-at-table-p)
  17021. (org-table-paste-rectangle)
  17022. (org-paste-subtree arg)))
  17023. (defun org-ctrl-c-ctrl-c (&optional arg)
  17024. "Set tags in headline, or update according to changed information at point.
  17025. This command does many different things, depending on context:
  17026. - If the cursor is in a headline, prompt for tags and insert them
  17027. into the current line, aligned to `org-tags-column'. When called
  17028. with prefix arg, realign all tags in the current buffer.
  17029. - If the cursor is in one of the special #+KEYWORD lines, this
  17030. triggers scanning the buffer for these lines and updating the
  17031. information.
  17032. - If the cursor is inside a table, realign the table. This command
  17033. works even if the automatic table editor has been turned off.
  17034. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  17035. the entire table.
  17036. - If the cursor is inside a table created by the table.el package,
  17037. activate that table.
  17038. - If the current buffer is a remember buffer, close note and file it.
  17039. with a prefix argument, file it without further interaction to the default
  17040. location.
  17041. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  17042. links in this buffer.
  17043. - If the cursor is on a numbered item in a plain list, renumber the
  17044. ordered list."
  17045. (interactive "P")
  17046. (let ((org-enable-table-editor t))
  17047. (cond
  17048. ((or org-clock-overlays
  17049. org-occur-highlights
  17050. org-latex-fragment-image-overlays)
  17051. (org-remove-clock-overlays)
  17052. (org-remove-occur-highlights)
  17053. (org-remove-latex-fragment-image-overlays)
  17054. (message "Temporary highlights/overlays removed from current buffer"))
  17055. ((and (local-variable-p 'org-finish-function (current-buffer))
  17056. (fboundp org-finish-function))
  17057. (funcall org-finish-function))
  17058. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  17059. ((org-on-heading-p) (call-interactively 'org-set-tags))
  17060. ((org-at-table.el-p)
  17061. (require 'table)
  17062. (beginning-of-line 1)
  17063. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  17064. (call-interactively 'table-recognize-table))
  17065. ((org-at-table-p)
  17066. (org-table-maybe-eval-formula)
  17067. (if arg
  17068. (call-interactively 'org-table-recalculate)
  17069. (org-table-maybe-recalculate-line))
  17070. (call-interactively 'org-table-align))
  17071. ((org-at-item-checkbox-p)
  17072. (call-interactively 'org-toggle-checkbox))
  17073. ((org-at-item-p)
  17074. (call-interactively 'org-renumber-ordered-list))
  17075. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  17076. (cond
  17077. ((equal (match-string 1) "TBLFM")
  17078. ;; Recalculate the table before this line
  17079. (save-excursion
  17080. (beginning-of-line 1)
  17081. (skip-chars-backward " \r\n\t")
  17082. (if (org-at-table-p)
  17083. (org-call-with-arg 'org-table-recalculate t))))
  17084. (t
  17085. (call-interactively 'org-mode-restart))))
  17086. (t (error "C-c C-c can do nothing useful at this location.")))))
  17087. (defun org-mode-restart ()
  17088. "Restart Org-mode, to scan again for special lines.
  17089. Also updates the keyword regular expressions."
  17090. (interactive)
  17091. (let ((org-inhibit-startup t)) (org-mode))
  17092. (message "Org-mode restarted to refresh keyword and special line setup"))
  17093. (defun org-return ()
  17094. "Goto next table row or insert a newline.
  17095. Calls `org-table-next-row' or `newline', depending on context.
  17096. See the individual commands for more information."
  17097. (interactive)
  17098. (cond
  17099. ((org-at-table-p)
  17100. (org-table-justify-field-maybe)
  17101. (call-interactively 'org-table-next-row))
  17102. (t (newline))))
  17103. (defun org-meta-return (&optional arg)
  17104. "Insert a new heading or wrap a region in a table.
  17105. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  17106. See the individual commands for more information."
  17107. (interactive "P")
  17108. (cond
  17109. ((org-at-table-p)
  17110. (call-interactively 'org-table-wrap-region))
  17111. (t (call-interactively 'org-insert-heading))))
  17112. ;;; Menu entries
  17113. ;; Define the Org-mode menus
  17114. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  17115. '("Tbl"
  17116. ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
  17117. ["Next Field" org-cycle (org-at-table-p)]
  17118. ["Previous Field" org-shifttab (org-at-table-p)]
  17119. ["Next Row" org-return (org-at-table-p)]
  17120. "--"
  17121. ["Blank Field" org-table-blank-field (org-at-table-p)]
  17122. ["Edit Field" org-table-edit-field (org-at-table-p)]
  17123. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  17124. "--"
  17125. ("Column"
  17126. ["Move Column Left" org-metaleft (org-at-table-p)]
  17127. ["Move Column Right" org-metaright (org-at-table-p)]
  17128. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  17129. ["Insert Column" org-shiftmetaright (org-at-table-p)]
  17130. "--"
  17131. ["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])
  17132. ("Row"
  17133. ["Move Row Up" org-metaup (org-at-table-p)]
  17134. ["Move Row Down" org-metadown (org-at-table-p)]
  17135. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  17136. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  17137. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  17138. "--"
  17139. ["Insert Hline" org-table-insert-hline (org-at-table-p)])
  17140. ("Rectangle"
  17141. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  17142. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  17143. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  17144. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  17145. "--"
  17146. ("Calculate"
  17147. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  17148. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  17149. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  17150. "--"
  17151. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  17152. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  17153. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  17154. "--"
  17155. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  17156. "--"
  17157. ["Sum Column/Rectangle" org-table-sum
  17158. (or (org-at-table-p) (org-region-active-p))]
  17159. ["Which Column?" org-table-current-column (org-at-table-p)])
  17160. ["Debug Formulas"
  17161. (setq org-table-formula-debug (not org-table-formula-debug))
  17162. :style toggle :selected org-table-formula-debug]
  17163. "--"
  17164. ["Create" org-table-create (and (not (org-at-table-p))
  17165. org-enable-table-editor)]
  17166. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  17167. ["Import from File" org-table-import (not (org-at-table-p))]
  17168. ["Export to File" org-table-export (org-at-table-p)]
  17169. "--"
  17170. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  17171. (easy-menu-define org-org-menu org-mode-map "Org menu"
  17172. '("Org"
  17173. ("Show/Hide"
  17174. ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
  17175. ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
  17176. ["Sparse Tree" org-occur t]
  17177. ["Reveal Context" org-reveal t]
  17178. ["Show All" show-all t]
  17179. "--"
  17180. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  17181. "--"
  17182. ["New Heading" org-insert-heading t]
  17183. ("Navigate Headings"
  17184. ["Up" outline-up-heading t]
  17185. ["Next" outline-next-visible-heading t]
  17186. ["Previous" outline-previous-visible-heading t]
  17187. ["Next Same Level" outline-forward-same-level t]
  17188. ["Previous Same Level" outline-backward-same-level t]
  17189. "--"
  17190. ["Jump" org-goto t])
  17191. ("Edit Structure"
  17192. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  17193. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  17194. "--"
  17195. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  17196. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  17197. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  17198. "--"
  17199. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  17200. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  17201. ["Demote Heading" org-metaright (not (org-at-table-p))]
  17202. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  17203. "--"
  17204. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  17205. "--"
  17206. ["Convert to odd levels" org-convert-to-odd-levels t]
  17207. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  17208. ("Archive"
  17209. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  17210. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  17211. ; :active t :keys "C-u C-c C-x C-a"]
  17212. ["Sparse trees open ARCHIVE trees"
  17213. (setq org-sparse-tree-open-archived-trees
  17214. (not org-sparse-tree-open-archived-trees))
  17215. :style toggle :selected org-sparse-tree-open-archived-trees]
  17216. ["Cycling opens ARCHIVE trees"
  17217. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  17218. :style toggle :selected org-cycle-open-archived-trees]
  17219. ["Agenda includes ARCHIVE trees"
  17220. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  17221. :style toggle :selected (not org-agenda-skip-archived-trees)]
  17222. "--"
  17223. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  17224. ; ["Check and Move Children" (org-archive-subtree '(4))
  17225. ; :active t :keys "C-u C-c C-x C-s"]
  17226. )
  17227. "--"
  17228. ("TODO Lists"
  17229. ["TODO/DONE/-" org-todo t]
  17230. ("Select keyword"
  17231. ["Next keyword" org-shiftright (org-on-heading-p)]
  17232. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  17233. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))])
  17234. ["Show TODO Tree" org-show-todo-tree t]
  17235. ["Global TODO list" org-todo-list t]
  17236. "--"
  17237. ["Set Priority" org-priority t]
  17238. ["Priority Up" org-shiftup t]
  17239. ["Priority Down" org-shiftdown t]
  17240. "--"
  17241. ; ["Insert Checkbox" org-insert-todo-heading (org-in-item-p)]
  17242. ; ["Toggle Checkbox" org-ctrl-c-ctrl-c (org-at-item-checkbox-p)]
  17243. ; ["Insert [n/m] cookie" (progn (insert "[/]") (org-update-checkbox-count))
  17244. ; (or (org-on-heading-p) (org-at-item-p))]
  17245. ; ["Insert [%] cookie" (progn (insert "[%]") (org-update-checkbox-count))
  17246. ; (or (org-on-heading-p) (org-at-item-p))]
  17247. ; ["Update Statistics" org-update-checkbox-count t]
  17248. )
  17249. ("Dates and Scheduling"
  17250. ["Timestamp" org-time-stamp t]
  17251. ["Timestamp (inactive)" org-time-stamp-inactive t]
  17252. ("Change Date"
  17253. ["1 Day Later" org-shiftright t]
  17254. ["1 Day Earlier" org-shiftleft t]
  17255. ["1 ... Later" org-shiftup t]
  17256. ["1 ... Earlier" org-shiftdown t])
  17257. ["Compute Time Range" org-evaluate-time-range t]
  17258. ["Schedule Item" org-schedule t]
  17259. ["Deadline" org-deadline t]
  17260. "--"
  17261. ["Custom time format" org-toggle-time-stamp-overlays
  17262. :style radio :selected org-display-custom-times]
  17263. "--"
  17264. ["Goto Calendar" org-goto-calendar t]
  17265. ["Date from Calendar" org-date-from-calendar t])
  17266. ("Logging work"
  17267. ["Clock in" org-clock-in t]
  17268. ["Clock out" org-clock-out t]
  17269. ["Clock cancel" org-clock-cancel t]
  17270. ["Display times" org-clock-display t]
  17271. ["Create clock table" org-clock-report t]
  17272. "--"
  17273. ["Record DONE time"
  17274. (progn (setq org-log-done (not org-log-done))
  17275. (message "Switching to %s will %s record a timestamp"
  17276. org-done-string
  17277. (if org-log-done "automatically" "not")))
  17278. :style toggle :selected org-log-done])
  17279. "--"
  17280. ["Agenda Command..." org-agenda t]
  17281. ("File List for Agenda")
  17282. ("Special views current file"
  17283. ["TODO Tree" org-show-todo-tree t]
  17284. ["Check Deadlines" org-check-deadlines t]
  17285. ["Timeline" org-timeline t]
  17286. ["Tags Tree" org-tags-sparse-tree t])
  17287. "--"
  17288. ("Hyperlinks"
  17289. ["Store Link (Global)" org-store-link t]
  17290. ["Insert Link" org-insert-link t]
  17291. ["Follow Link" org-open-at-point t]
  17292. "--"
  17293. ["Next link" org-next-link t]
  17294. ["Previous link" org-previous-link t]
  17295. "--"
  17296. ["Descriptive Links"
  17297. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  17298. :style radio :selected (member '(org-link) buffer-invisibility-spec)]
  17299. ["Literal Links"
  17300. (progn
  17301. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  17302. :style radio :selected (not (member '(org-link) buffer-invisibility-spec))])
  17303. "--"
  17304. ["Export/Publish..." org-export t]
  17305. ("LaTeX"
  17306. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  17307. :selected org-cdlatex-mode]
  17308. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  17309. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  17310. ["Modify math symbol" org-cdlatex-math-modify
  17311. (org-inside-LaTeX-fragment-p)]
  17312. ["Export LaTeX fragments as images"
  17313. (setq org-export-with-LaTeX-fragments (not org-export-with-LaTeX-fragments))
  17314. :style toggle :selected org-export-with-LaTeX-fragments])
  17315. "--"
  17316. ("Documentation"
  17317. ["Show Version" org-version t]
  17318. ["Info Documentation" org-info t])
  17319. ("Customize"
  17320. ["Browse Org Group" org-customize t]
  17321. "--"
  17322. ["Expand This Menu" org-create-customize-menu
  17323. (fboundp 'customize-menu-create)])
  17324. "--"
  17325. ["Refresh setup" org-mode-restart t]
  17326. ))
  17327. (defun org-toggle-log-option (type)
  17328. (if (not (listp org-log-done)) (setq org-log-done nil))
  17329. (if (memq type org-log-done)
  17330. (setq org-log-done (delq type org-log-done))
  17331. (add-to-list 'org-log-done type)))
  17332. (defun org-check-log-option (type)
  17333. (and (listp org-log-done) (memq type org-log-done)))
  17334. (defun org-info (&optional node)
  17335. "Read documentation for Org-mode in the info system.
  17336. With optional NODE, go directly to that node."
  17337. (interactive)
  17338. (require 'info)
  17339. (Info-goto-node (format "(org)%s" (or node ""))))
  17340. (defun org-install-agenda-files-menu ()
  17341. (let ((bl (buffer-list)))
  17342. (save-excursion
  17343. (while bl
  17344. (set-buffer (pop bl))
  17345. (if (org-mode-p) (setq bl nil)))
  17346. (when (org-mode-p)
  17347. (easy-menu-change
  17348. '("Org") "File List for Agenda"
  17349. (append
  17350. (list
  17351. ["Edit File List" (org-edit-agenda-file-list) t]
  17352. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  17353. ["Remove Current File from List" org-remove-file t]
  17354. ["Cycle through agenda files" org-cycle-agenda-files t]
  17355. "--")
  17356. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  17357. ;;;; Documentation
  17358. (defun org-customize ()
  17359. "Call the customize function with org as argument."
  17360. (interactive)
  17361. (customize-browse 'org))
  17362. (defun org-create-customize-menu ()
  17363. "Create a full customization menu for Org-mode, insert it into the menu."
  17364. (interactive)
  17365. (if (fboundp 'customize-menu-create)
  17366. (progn
  17367. (easy-menu-change
  17368. '("Org") "Customize"
  17369. `(["Browse Org group" org-customize t]
  17370. "--"
  17371. ,(customize-menu-create 'org)
  17372. ["Set" Custom-set t]
  17373. ["Save" Custom-save t]
  17374. ["Reset to Current" Custom-reset-current t]
  17375. ["Reset to Saved" Custom-reset-saved t]
  17376. ["Reset to Standard Settings" Custom-reset-standard t]))
  17377. (message "\"Org\"-menu now contains full customization menu"))
  17378. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  17379. ;;;; Miscellaneous stuff
  17380. ;;; Generally useful functions
  17381. (defun org-context ()
  17382. "Return a list of contexts of the current cursor position.
  17383. If several contexts apply, all are returned.
  17384. Each context entry is a list with a symbol naming the context, and
  17385. two positions indicating start and end of the context. Possible
  17386. contexts are:
  17387. :headline anywhere in a headline
  17388. :headline-stars on the leading stars in a headline
  17389. :todo-keyword on a TODO keyword (including DONE) in a headline
  17390. :tags on the TAGS in a headline
  17391. :priority on the priority cookie in a headline
  17392. :item on the first line of a plain list item
  17393. :item-bullet on the bullet/number of a plain list item
  17394. :checkbox on the checkbox in a plain list item
  17395. :table in an org-mode table
  17396. :table-special on a special filed in a table
  17397. :table-table in a table.el table
  17398. :link on a hyperlink
  17399. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  17400. :target on a <<target>>
  17401. :radio-target on a <<<radio-target>>>
  17402. :latex-fragment on a LaTeX fragment
  17403. :latex-preview on a LaTeX fragment with overlayed preview image
  17404. This function expects the position to be visible because it uses font-lock
  17405. faces as a help to recognize the following contexts: :table-special, :link,
  17406. and :keyword."
  17407. (let* ((f (get-text-property (point) 'face))
  17408. (faces (if (listp f) f (list f)))
  17409. (p (point)) clist o)
  17410. ;; First the large context
  17411. (cond
  17412. ((org-on-heading-p)
  17413. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  17414. (when (progn
  17415. (beginning-of-line 1)
  17416. (looking-at org-todo-line-tags-regexp))
  17417. (push (org-point-in-group p 1 :headline-stars) clist)
  17418. (push (org-point-in-group p 2 :todo-keyword) clist)
  17419. (push (org-point-in-group p 4 :tags) clist))
  17420. (goto-char p)
  17421. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  17422. (if (looking-at "\\[#[A-Z]\\]")
  17423. (push (org-point-in-group p 0 :priority) clist)))
  17424. ((org-at-item-p)
  17425. (push (org-point-in-group p 2 :item-bullet) clist)
  17426. (push (list :item (point-at-bol)
  17427. (save-excursion (org-end-of-item) (point)))
  17428. clist)
  17429. (and (org-at-item-checkbox-p)
  17430. (push (org-point-in-group p 0 :checkbox) clist)))
  17431. ((org-at-table-p)
  17432. (push (list :table (org-table-begin) (org-table-end)) clist)
  17433. (if (memq 'org-formula faces)
  17434. (push (list :table-special
  17435. (previous-single-property-change p 'face)
  17436. (next-single-property-change p 'face)) clist)))
  17437. ((org-at-table-p 'any)
  17438. (push (list :table-table) clist)))
  17439. (goto-char p)
  17440. ;; Now the small context
  17441. (cond
  17442. ((org-at-timestamp-p)
  17443. (push (org-point-in-group p 0 :timestamp) clist))
  17444. ((memq 'org-link faces)
  17445. (push (list :link
  17446. (previous-single-property-change p 'face)
  17447. (next-single-property-change p 'face)) clist))
  17448. ((memq 'org-special-keyword faces)
  17449. (push (list :keyword
  17450. (previous-single-property-change p 'face)
  17451. (next-single-property-change p 'face)) clist))
  17452. ((org-on-target-p)
  17453. (push (org-point-in-group p 0 :target) clist)
  17454. (goto-char (1- (match-beginning 0)))
  17455. (if (looking-at org-radio-target-regexp)
  17456. (push (org-point-in-group p 0 :radio-target) clist))
  17457. (goto-char p))
  17458. ((setq o (car (delq nil
  17459. (mapcar
  17460. (lambda (x)
  17461. (if (memq x org-latex-fragment-image-overlays) x))
  17462. (org-overlays-at (point))))))
  17463. (push (list :latex-fragment
  17464. (org-overlay-start o) (org-overlay-end o)) clist)
  17465. (push (list :latex-preview
  17466. (org-overlay-start o) (org-overlay-end o)) clist))
  17467. ((org-inside-LaTeX-fragment-p)
  17468. ;; FIXME: positions wrong.
  17469. (push (list :latex-fragment (point) (point)) clist)))
  17470. (setq clist (nreverse (delq nil clist)))
  17471. clist))
  17472. (defun org-point-in-group (point group &optional context)
  17473. "Check if POINT is in match-group GROUP.
  17474. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  17475. match. If the match group does ot exist or point is not inside it,
  17476. return nil."
  17477. (and (match-beginning group)
  17478. (>= point (match-beginning group))
  17479. (<= point (match-end group))
  17480. (if context
  17481. (list context (match-beginning group) (match-end group))
  17482. t)))
  17483. (defun org-move-line-down (arg)
  17484. "Move the current line down. With prefix argument, move it past ARG lines."
  17485. (interactive "p")
  17486. (let ((col (current-column))
  17487. beg end pos)
  17488. (beginning-of-line 1) (setq beg (point))
  17489. (beginning-of-line 2) (setq end (point))
  17490. (beginning-of-line (+ 1 arg))
  17491. (setq pos (move-marker (make-marker) (point)))
  17492. (insert (delete-and-extract-region beg end))
  17493. (goto-char pos)
  17494. (move-to-column col)))
  17495. (defun org-move-line-up (arg)
  17496. "Move the current line up. With prefix argument, move it past ARG lines."
  17497. (interactive "p")
  17498. (let ((col (current-column))
  17499. beg end pos)
  17500. (beginning-of-line 1) (setq beg (point))
  17501. (beginning-of-line 2) (setq end (point))
  17502. (beginning-of-line (- arg))
  17503. (setq pos (move-marker (make-marker) (point)))
  17504. (insert (delete-and-extract-region beg end))
  17505. (goto-char pos)
  17506. (move-to-column col)))
  17507. (defun org-replace-escapes (string table)
  17508. ;; FIXME: document and make safer
  17509. (let ((case-fold-search nil)
  17510. e re rpl)
  17511. (while (setq e (pop table))
  17512. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  17513. (while (string-match re string)
  17514. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  17515. (cdr e)))
  17516. (setq string (replace-match rpl t t string))))
  17517. string))
  17518. (defun org-sublist (list start end)
  17519. "Return a section of LIST, from START to END.
  17520. Counting starts at 1."
  17521. (let (rtn (c start))
  17522. (setq list (nthcdr (1- start) list))
  17523. (while (and list (<= c end))
  17524. (push (pop list) rtn)
  17525. (setq c (1+ c)))
  17526. (nreverse rtn)))
  17527. (defun org-at-regexp-p (regexp)
  17528. "Is point inside a match of REGEXP in the current line?"
  17529. (catch 'exit
  17530. (save-excursion
  17531. (let ((pos (point)) (end (point-at-eol)))
  17532. (beginning-of-line 1)
  17533. (while (re-search-forward regexp end t)
  17534. (if (and (<= (match-beginning 0) pos)
  17535. (>= (match-end 0) pos))
  17536. (throw 'exit t)))
  17537. nil))))
  17538. ;;; Paragraph filling stuff.
  17539. ;; We want this to be just right, so use the full arsenal.
  17540. ;; FIXME: configure filladapt for XEmacs
  17541. (defun org-set-autofill-regexps ()
  17542. (interactive)
  17543. ;; In the paragraph separator we include headlines, because filling
  17544. ;; text in a line directly attached to a headline would otherwise
  17545. ;; fill the headline as well.
  17546. (org-set-local 'comment-start-skip "^#+[ \t]*")
  17547. (org-set-local 'paragraph-separate "\f\\|\\*\\|[ ]*$\\|[ \t]*[:|]")
  17548. ;; The paragraph starter includes hand-formatted lists.
  17549. (org-set-local 'paragraph-start
  17550. "\f\\|[ ]*$\\|\\([*\f]+\\)\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  17551. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  17552. ;; But only if the user has not turned off tables or fixed-width regions
  17553. (org-set-local
  17554. 'auto-fill-inhibit-regexp
  17555. (concat "\\*\\|#\\+"
  17556. "\\|[ \t]*" org-keyword-time-regexp
  17557. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  17558. (concat
  17559. "\\|[ \t]*["
  17560. (if org-enable-table-editor "|" "")
  17561. (if org-enable-fixed-width-editor ":" "")
  17562. "]"))))
  17563. ;; We use our own fill-paragraph function, to make sure that tables
  17564. ;; and fixed-width regions are not wrapped. That function will pass
  17565. ;; through to `fill-paragraph' when appropriate.
  17566. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  17567. ; Adaptive filling: To get full control, first make sure that
  17568. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  17569. (org-set-local 'adaptive-fill-regexp "\000")
  17570. (org-set-local 'adaptive-fill-function
  17571. 'org-adaptive-fill-function))
  17572. (defun org-fill-paragraph (&optional justify)
  17573. "Re-align a table, pass through to fill-paragraph if no table."
  17574. (let ((table-p (org-at-table-p))
  17575. (table.el-p (org-at-table.el-p)))
  17576. (cond ((equal (char-after (point-at-bol)) ?*) t) ; skip headlines
  17577. (table.el-p t) ; skip table.el tables
  17578. (table-p (org-table-align) t) ; align org-mode tables
  17579. (t nil)))) ; call paragraph-fill
  17580. ;; For reference, this is the default value of adaptive-fill-regexp
  17581. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  17582. (defun org-adaptive-fill-function ()
  17583. "Return a fill prefix for org-mode files.
  17584. In particular, this makes sure hanging paragraphs for hand-formatted lists
  17585. work correctly."
  17586. (cond ((looking-at "#[ \t]+")
  17587. (match-string 0))
  17588. ((looking-at " *\\([-*+] \\|[0-9]+[.)] \\)?")
  17589. (make-string (- (match-end 0) (match-beginning 0)) ?\ ))
  17590. (t nil)))
  17591. (defun org-image-file-name-regexp ()
  17592. "Return regexp matching the file names of images."
  17593. (if (fboundp 'image-file-name-regexp)
  17594. (image-file-name-regexp)
  17595. (let ((image-file-name-extensions
  17596. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  17597. "xbm" "xpm" "pbm" "pgm" "ppm")))
  17598. (concat "\\."
  17599. (regexp-opt (nconc (mapcar 'upcase
  17600. image-file-name-extensions)
  17601. image-file-name-extensions)
  17602. t)
  17603. "\\'"))))
  17604. (defun org-file-image-p (file)
  17605. "Return non-nil if FILE is an image."
  17606. (save-match-data
  17607. (string-match (org-image-file-name-regexp) file)))
  17608. ;;;; Functions extending outline functionality
  17609. ;; C-a should go to the beginning of a *visible* line, also in the
  17610. ;; new outline.el. I guess this should be patched into Emacs?
  17611. (defun org-beginning-of-line ()
  17612. "Go to the beginning of the current line. If that is invisible, continue
  17613. to a visible line beginning. This makes the function of C-a more intuitive."
  17614. (interactive)
  17615. (beginning-of-line 1)
  17616. (if (bobp)
  17617. nil
  17618. (backward-char 1)
  17619. (if (org-invisible-p)
  17620. (while (and (not (bobp)) (org-invisible-p))
  17621. (backward-char 1)
  17622. (beginning-of-line 1))
  17623. (forward-char 1))))
  17624. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  17625. (defun org-invisible-p ()
  17626. "Check if point is at a character currently not visible."
  17627. ;; Early versions of noutline don't have `outline-invisible-p'.
  17628. (if (fboundp 'outline-invisible-p)
  17629. (outline-invisible-p)
  17630. (get-char-property (point) 'invisible)))
  17631. (defun org-invisible-p2 ()
  17632. "Check if point is at a character currently not visible."
  17633. (save-excursion
  17634. (if (and (eolp) (not (bobp))) (backward-char 1))
  17635. ;; Early versions of noutline don't have `outline-invisible-p'.
  17636. (if (fboundp 'outline-invisible-p)
  17637. (outline-invisible-p)
  17638. (get-char-property (point) 'invisible))))
  17639. (defalias 'org-back-to-heading 'outline-back-to-heading)
  17640. (defalias 'org-on-heading-p 'outline-on-heading-p)
  17641. (defun org-on-target-p ()
  17642. (let ((pos (point)))
  17643. (save-excursion
  17644. (skip-chars-forward "<")
  17645. (and (re-search-backward "<<" nil t)
  17646. (or (looking-at org-radio-target-regexp)
  17647. (looking-at org-target-regexp))
  17648. (<= (match-beginning 0) pos)
  17649. (>= (1+ (match-end 0)) pos)))))
  17650. (defun org-up-heading-all (arg)
  17651. "Move to the heading line of which the present line is a subheading.
  17652. This function considers both visible and invisible heading lines.
  17653. With argument, move up ARG levels."
  17654. (if (fboundp 'outline-up-heading-all)
  17655. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  17656. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  17657. (defun org-goto-sibling (&optional previous)
  17658. "Goto the next sibling, even if it is invisible.
  17659. When PREVIOUS is set, go to the previous sibling instead. Returns t
  17660. when a sibling was found. When none is found, return nil and don't
  17661. move point."
  17662. (let ((fun (if previous 're-search-backward 're-search-forward))
  17663. (pos (point))
  17664. (re (concat "^" outline-regexp))
  17665. level l)
  17666. (org-back-to-heading t)
  17667. (setq level (funcall outline-level))
  17668. (catch 'exit
  17669. (or previous (forward-char 1))
  17670. (while (funcall fun re nil t)
  17671. (setq l (funcall outline-level))
  17672. (when (< l level) (goto-char pos) (throw 'exit nil))
  17673. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  17674. (goto-char pos)
  17675. nil)))
  17676. (defun org-show-siblings ()
  17677. "Show all siblings of the current headline."
  17678. (save-excursion
  17679. (while (org-goto-sibling) (org-flag-heading nil)))
  17680. (save-excursion
  17681. (while (org-goto-sibling 'previous)
  17682. (org-flag-heading nil))))
  17683. (defun org-show-hidden-entry ()
  17684. "Show an entry where even the heading is hidden."
  17685. (save-excursion
  17686. (org-show-entry)))
  17687. (defun org-flag-heading (flag &optional entry)
  17688. "Flag the current heading. FLAG non-nil means make invisible.
  17689. When ENTRY is non-nil, show the entire entry."
  17690. (save-excursion
  17691. (org-back-to-heading t)
  17692. ;; Check if we should show the entire entry
  17693. (if entry
  17694. (progn
  17695. (org-show-entry)
  17696. (save-excursion
  17697. (and (outline-next-heading)
  17698. (org-flag-heading nil))))
  17699. (outline-flag-region (max 1 (1- (point)))
  17700. (save-excursion (outline-end-of-heading) (point))
  17701. flag))))
  17702. (defun org-end-of-subtree (&optional invisible-OK)
  17703. ;; This is an exact copy of the original function, but it uses
  17704. ;; `org-back-to-heading', to make it work also in invisible
  17705. ;; trees. And is uses an invisible-OK argument.
  17706. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  17707. (org-back-to-heading invisible-OK)
  17708. (let ((first t)
  17709. (level (funcall outline-level)))
  17710. (while (and (not (eobp))
  17711. (or first (> (funcall outline-level) level)))
  17712. (setq first nil)
  17713. (outline-next-heading))
  17714. (if (memq (preceding-char) '(?\n ?\^M))
  17715. (progn
  17716. ;; Go to end of line before heading
  17717. (forward-char -1)
  17718. (if (memq (preceding-char) '(?\n ?\^M))
  17719. ;; leave blank line before heading
  17720. (forward-char -1)))))
  17721. (point))
  17722. (defun org-show-subtree ()
  17723. "Show everything after this heading at deeper levels."
  17724. (outline-flag-region
  17725. (point)
  17726. (save-excursion
  17727. (outline-end-of-subtree) (outline-next-heading) (point))
  17728. nil))
  17729. (defun org-show-entry ()
  17730. "Show the body directly following this heading.
  17731. Show the heading too, if it is currently invisible."
  17732. (interactive)
  17733. (save-excursion
  17734. (org-back-to-heading t)
  17735. (outline-flag-region
  17736. (max 1 (1- (point)))
  17737. (save-excursion
  17738. (re-search-forward (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  17739. (or (match-beginning 1) (point-max)))
  17740. nil)))
  17741. (defun org-make-options-regexp (kwds)
  17742. "Make a regular expression for keyword lines."
  17743. (concat
  17744. "^"
  17745. "#?[ \t]*\\+\\("
  17746. (mapconcat 'regexp-quote kwds "\\|")
  17747. "\\):[ \t]*"
  17748. "\\(.+\\)"))
  17749. ;; Make isearch reveal the necessary context
  17750. (defun org-isearch-end ()
  17751. "Reveal context after isearch exits."
  17752. (when isearch-success ; only if search was successful
  17753. (if (featurep 'xemacs)
  17754. ;; Under XEmacs, the hook is run in the correct place,
  17755. ;; we directly show the context.
  17756. (org-show-context 'isearch)
  17757. ;; In Emacs the hook runs *before* restoring the overlays.
  17758. ;; So we have to use a one-time post-command-hook to do this.
  17759. ;; (Emacs 22 has a special variable, see function `org-mode')
  17760. (unless (and (boundp 'isearch-mode-end-hook-quit)
  17761. isearch-mode-end-hook-quit)
  17762. ;; Only when the isearch was not quitted.
  17763. (org-add-hook 'post-command-hook 'org-isearch-post-command
  17764. 'append 'local)))))
  17765. (defun org-isearch-post-command ()
  17766. "Remove self from hook, and show context."
  17767. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  17768. (org-show-context 'isearch))
  17769. ;;;; Repair problems with some other packages
  17770. ;; Make `bookmark-jump' show the jump location if it was hidden.
  17771. (eval-after-load "bookmark"
  17772. '(if (boundp 'bookmark-after-jump-hook)
  17773. ;; We can use the hook
  17774. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  17775. ;; Hook not available, use advice
  17776. (defadvice bookmark-jump (after org-make-visible activate)
  17777. "Make the position visible."
  17778. (org-bookmark-jump-unhide))))
  17779. (defun org-bookmark-jump-unhide ()
  17780. "Unhide the current position, to show the bookmark location."
  17781. (and (org-mode-p)
  17782. (or (org-invisible-p)
  17783. (save-excursion (goto-char (max (point-min) (1- (point))))
  17784. (org-invisible-p)))
  17785. (org-show-context 'bookmark-jump)))
  17786. ;; Make session.el ignore our circular variable
  17787. (eval-after-load "session"
  17788. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  17789. ;;;; Experimental code
  17790. (defcustom org-allow-auto-repeat t
  17791. "Non-nil means, find REPEAT cookies in entries and apply them.
  17792. A repeat cookie looks like REPEAT(+1m) and causes deadlines and schedules
  17793. to repeat themselves shifted by a certain amount of time, each time an
  17794. entry is marked DONE."
  17795. :group 'org; FIXME ???
  17796. :type 'boolean)
  17797. (defun org-auto-repeat-maybe ()
  17798. "Check if the current headline contains a REPEAT key.
  17799. If yes, set TODO state back to what it was and change any SCHEDULED
  17800. or DEADLINE times the new date.
  17801. This function should be run in the `org-after-todo-state-change-hook'."
  17802. ;; last-state is dynamically scoped into this function
  17803. (let ((repeat (org-get-repeat))
  17804. (whata '(("d" . day) ("m" . month) ("y" . year)))
  17805. (msg "Entry repeats: ")
  17806. (org-log-done)
  17807. re type n what start)
  17808. (when repeat
  17809. (org-todo (if (eq 'org-todo-interpretation 'type)
  17810. last-state
  17811. (car org-todo-keywords)))
  17812. (unless (memq 'org-add-log-note (default-value 'post-command-hook))
  17813. ;; Make sure a note is taken
  17814. (let ((org-log-done '(done)))
  17815. (org-add-log-maybe 'done org-done-string 'findpos)))
  17816. (org-back-to-heading t)
  17817. (org-add-planning-info nil nil 'closed)
  17818. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  17819. org-deadline-time-regexp "\\)"))
  17820. (while (re-search-forward
  17821. re (save-excursion (outline-next-heading) (point)) t)
  17822. (setq type (if (match-end 1) org-scheduled-string org-deadline-string)
  17823. start 0)
  17824. (while (string-match "\\([-+]?[0-9]+\\)\\([dwmy]\\)" repeat start)
  17825. (setq start (match-end 0)
  17826. n (string-to-number (match-string 1 repeat))
  17827. what (match-string 2 repeat))
  17828. (if (equal what "w") (setq n (* n 7) what "d"))
  17829. (org-timestamp-change n (cdr (assoc what whata))))
  17830. (setq msg (concat msg type org-last-changed-timestamp " ")))
  17831. (setq org-log-post-message msg)
  17832. (message msg))))
  17833. ;;;; Finish up
  17834. (provide 'org)
  17835. (run-hooks 'org-load-hook)
  17836. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  17837. ;;; org.el ends here