org.el 623 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960
  1. ;;; org.el --- Outline-based notes management and organize
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <dominik at science dot uva dot nl>
  6. ;; Keywords: outlines, hypermedia, calendar, wp
  7. ;; Homepage: http://www.astro.uva.nl/~dominik/Tools/org/
  8. ;; Version: 4.50
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software; you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation; either version 2, or (at your option)
  15. ;; any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  22. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  23. ;; Boston, MA 02110-1301, USA.
  24. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  25. ;;
  26. ;;; Commentary:
  27. ;;
  28. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  29. ;; project planning with a fast and effective plain-text system.
  30. ;;
  31. ;; Org-mode develops organizational tasks around NOTES files that contain
  32. ;; information about projects as plain text. Org-mode is implemented on
  33. ;; top of outline-mode, which makes it possible to keep the content of
  34. ;; large files well structured. Visibility cycling and structure editing
  35. ;; help to work with the tree. Tables are easily created with a built-in
  36. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  37. ;; and scheduling. It dynamically compiles entries into an agenda that
  38. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  39. ;; Plain text URL-like links connect to websites, emails, Usenet
  40. ;; messages, BBDB entries, and any files related to the projects. For
  41. ;; printing and sharing of notes, an Org-mode file can be exported as a
  42. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  43. ;; iCalendar file. It can also serve as a publishing tool for a set of
  44. ;; linked webpages.
  45. ;;
  46. ;; Installation and Activation
  47. ;; ---------------------------
  48. ;; See the corresponding sections in the manual at
  49. ;;
  50. ;; http://staff.science.uva.nl/~dominik/Tools/org/org.html#Installation
  51. ;;
  52. ;; Documentation
  53. ;; -------------
  54. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  55. ;; distribution also contains a PDF version of it. At the homepage of
  56. ;; Org-mode, you can read the same text online as HTML. There is also an
  57. ;; excellent reference card made by Philip Rooke. This card can be found
  58. ;; in the etc/ directory of Emacs 22.
  59. ;;
  60. ;; Recent changes
  61. ;; --------------
  62. ;; Version 4.50
  63. ;; - Closing a TODO item can record an additional note.
  64. ;; See variables `org-log-done' and `org-log-note-headings'.
  65. ;; - Inserting headlines and bullets can leave an extra blank line.
  66. ;; See variable `org-blank-before-new-entry'. (Ed Hirgelt patch)
  67. ;; - [[bracket links]] in the agenda are active just as in org-mode buffers.
  68. ;; - C-c C-o on a date range displays the agenda for exactly this range.
  69. ;; - The default for `org-cycle-include-plain-lists' is back to nil.
  70. ;; - Calls to `org-occur' can be stacked by using a prefix argument.
  71. ;; - The options `org-show-hierarchy-above' and `org-show-following-heading'
  72. ;; now always default to `t', but can be customized differently for
  73. ;; different types of sparse trees or jump commands.
  74. ;; - Bug fixes.
  75. ;;
  76. ;; Version 4.49
  77. ;; - Agenda views can be made in batch mode from the command line.
  78. ;; - `org-store-link' does the right thing in dired-mode.
  79. ;; - File links can contain environment variables.
  80. ;; - Full Emacs 21 compatibility has been restored.
  81. ;; - Bug fixes.
  82. ;;
  83. ;; Version 4.47
  84. ;; - Custom commands may produce an agenda which contains several blocks,
  85. ;; each block created by a different agenda command.
  86. ;; - Agenda commands can be restricted to the current file, region, subtree.
  87. ;; - The timeline command must now be called through the agenda
  88. ;; dispatcher (C-c a L). `C-c C-r' no longer works.
  89. ;; - Agenda items can be sorted by tag. The *last* tag is used for this.
  90. ;; - The prefix and the sorting strategy for agenda items can depend
  91. ;; upon the agenda type.
  92. ;; - The handling of `mailto:' links can be customized, see the new
  93. ;; variable `org-link-mailto-program'.
  94. ;; - `mailto' links can specify a subject after a double colon,
  95. ;; like [[mailto:carsten@orgmode.org::Org-mode is buggy]].
  96. ;; - In the #+STARTUP line, M-TAB completes valid keywords.
  97. ;; - In the #+TAGS: line, M-TAB after ":" inserts all currently used tags.
  98. ;; - Again full Emacs 21 support: Checkboxes and publishing are fixed.
  99. ;; - More minor bug fixes.
  100. ;;
  101. ;; Version 4.45
  102. ;; - Checkbox lists can show statistics about checked items.
  103. ;; - C-TAB will cycle the visibility of archived subtrees.
  104. ;;; - Documentation about checkboxes has been moved to chapter 5.
  105. ;; - Bux fixes.
  106. ;;
  107. ;; Version 4.44
  108. ;; - Clock table can be done for a limited time interval.
  109. ;; - Obsolete support for the old outline mode has been removed.
  110. ;; - Bug fixes and code cleaning.
  111. ;;
  112. ;; Version 4.43
  113. ;; - Bug fixes
  114. ;; - `s' key in the agenda saves all org-mode buffers.
  115. ;;
  116. ;; Version 4.41
  117. ;; - Shift-curser keys can modify inactive time stamps (inactive time
  118. ;; stamps are the ones in [...] brackets.
  119. ;; - Toggle all checkboxes in a region/below a headline.
  120. ;; - Bug fixes.
  121. ;;
  122. ;;; Code:
  123. (eval-when-compile
  124. (require 'cl)
  125. (require 'calendar))
  126. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  127. ;; the file noutline.el being loaded.
  128. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  129. ;; We require noutline, which might be provided in outline.el
  130. (require 'outline) (require 'noutline)
  131. ;; Other stuff we need.
  132. (require 'time-date)
  133. (require 'easymenu)
  134. ;;; Customization variables
  135. (defvar org-version "4.50"
  136. "The version number of the file org.el.")
  137. (defun org-version ()
  138. (interactive)
  139. (message "Org-mode version %s" org-version))
  140. ;; Compatibility constants
  141. (defconst org-xemacs-p (featurep 'xemacs)) ; not used by org.el itself
  142. (defconst org-format-transports-properties-p
  143. (let ((x "a"))
  144. (add-text-properties 0 1 '(test t) x)
  145. (get-text-property 0 'test (format "%s" x)))
  146. "Does format transport text properties?")
  147. (defgroup org nil
  148. "Outline-based notes management and organizer."
  149. :tag "Org"
  150. :group 'outlines
  151. :group 'hypermedia
  152. :group 'calendar)
  153. (defgroup org-startup nil
  154. "Options concerning startup of Org-mode."
  155. :tag "Org Startup"
  156. :group 'org)
  157. (defcustom org-startup-folded t
  158. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  159. This can also be configured on a per-file basis by adding one of
  160. the following lines anywhere in the buffer:
  161. #+STARTUP: fold
  162. #+STARTUP: nofold
  163. #+STARTUP: content"
  164. :group 'org-startup
  165. :type '(choice
  166. (const :tag "nofold: show all" nil)
  167. (const :tag "fold: overview" t)
  168. (const :tag "content: all headlines" content)))
  169. (defcustom org-startup-truncated t
  170. "Non-nil means, entering Org-mode will set `truncate-lines'.
  171. This is useful since some lines containing links can be very long and
  172. uninteresting. Also tables look terrible when wrapped."
  173. :group 'org-startup
  174. :type 'boolean)
  175. (defcustom org-startup-align-all-tables nil
  176. "Non-nil means, align all tables when visiting a file.
  177. This is useful when the column width in tables is forced with <N> cookies
  178. in table fields. Such tables will look correct only after the first re-align.
  179. This can also be configured on a per-file basis by adding one of
  180. the following lines anywhere in the buffer:
  181. #+STARTUP: align
  182. #+STARTUP: noalign"
  183. :group 'org-startup
  184. :type 'boolean)
  185. (defcustom org-startup-with-deadline-check nil
  186. "Non-nil means, entering Org-mode will run the deadline check.
  187. This means, if you start editing an org file, you will get an
  188. immediate reminder of any due deadlines.
  189. This can also be configured on a per-file basis by adding one of
  190. the following lines anywhere in the buffer:
  191. #+STARTUP: dlcheck
  192. #+STARTUP: nodlcheck"
  193. :group 'org-startup
  194. :type 'boolean)
  195. (defcustom org-insert-mode-line-in-empty-file nil
  196. "Non-nil means insert the first line setting Org-mode in empty files.
  197. When the function `org-mode' is called interactively in an empty file, this
  198. normally means that the file name does not automatically trigger Org-mode.
  199. To ensure that the file will always be in Org-mode in the future, a
  200. line enforcing Org-mode will be inserted into the buffer, if this option
  201. has been set."
  202. :group 'org-startup
  203. :type 'boolean)
  204. (defcustom org-CUA-compatible nil
  205. "Non-nil means use alternative key bindings for S-<cursor movement>.
  206. Org-mode used S-<cursor movement> for changing timestamps and priorities.
  207. S-<cursor movement> is also used for example by `CUA-mode' to select text.
  208. If you want to use Org-mode together with `CUA-mode', Org-mode needs to use
  209. alternative bindings. Setting this variable to t will replace the following
  210. keys both in Org-mode and in the Org-agenda buffer.
  211. S-RET -> C-S-RET
  212. S-up -> M-p
  213. S-down -> M-n
  214. S-left -> M--
  215. S-right -> M-+
  216. If you do not like the alternative keys, take a look at the variable
  217. `org-disputed-keys'.
  218. This option is only relevant at load-time of Org-mode. Changing it requires
  219. a restart of Emacs to become effective."
  220. :group 'org-startup
  221. :type 'boolean)
  222. (defvar org-disputed-keys
  223. '((S-up [(shift up)] [(meta ?p)])
  224. (S-down [(shift down)] [(meta ?n)])
  225. (S-left [(shift left)] [(meta ?-)])
  226. (S-right [(shift right)] [(meta ?+)])
  227. (S-return [(shift return)] [(control shift return)]))
  228. "Keys for which Org-mode and other modes compete.
  229. This is an alist, cars are symbols for lookup, 1st element is the default key,
  230. second element will be used when `org-CUA-compatible' is t.")
  231. (defun org-key (key)
  232. "Select a key according to `org-CUA-compatible'."
  233. (nth (if org-CUA-compatible 2 1)
  234. (or (assq key org-disputed-keys)
  235. (error "Invalid Key %s in `org-key'" key))))
  236. (defcustom org-ellipsis nil
  237. "The ellipsis to use in the Org-mode outline.
  238. When nil, just use the standard three dots. When a string, use that instead,
  239. and just in Org-mode (which will then use its own display table).
  240. Changing this requires executing `M-x org-mode' in a buffer to become
  241. effective."
  242. :group 'org-startup
  243. :type '(choice (const :tag "Default" nil)
  244. (string :tag "String" :value "...#")))
  245. (defvar org-display-table nil
  246. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  247. (defgroup org-keywords nil
  248. "Keywords in Org-mode."
  249. :tag "Org Keywords"
  250. :group 'org)
  251. (defcustom org-deadline-string "DEADLINE:"
  252. "String to mark deadline entries.
  253. A deadline is this string, followed by a time stamp. Should be a word,
  254. terminated by a colon. You can insert a schedule keyword and
  255. a timestamp with \\[org-deadline].
  256. Changes become only effective after restarting Emacs."
  257. :group 'org-keywords
  258. :type 'string)
  259. (defcustom org-scheduled-string "SCHEDULED:"
  260. "String to mark scheduled TODO entries.
  261. A schedule is this string, followed by a time stamp. Should be a word,
  262. terminated by a colon. You can insert a schedule keyword and
  263. a timestamp with \\[org-schedule].
  264. Changes become only effective after restarting Emacs."
  265. :group 'org-keywords
  266. :type 'string)
  267. (defcustom org-closed-string "CLOSED:"
  268. "String used as the prefix for timestamps logging closing a TODO entry."
  269. :group 'org-keywords
  270. :type 'string)
  271. (defcustom org-clock-string "CLOCK:"
  272. "String used as prefix for timestamps clocking work hours on an item."
  273. :group 'org-keywords
  274. :type 'string)
  275. (defcustom org-comment-string "COMMENT"
  276. "Entries starting with this keyword will never be exported.
  277. An entry can be toggled between COMMENT and normal with
  278. \\[org-toggle-comment].
  279. Changes become only effective after restarting Emacs."
  280. :group 'org-keywords
  281. :type 'string)
  282. (defcustom org-quote-string "QUOTE"
  283. "Entries starting with this keyword will be exported in fixed-width font.
  284. Quoting applies only to the text in the entry following the headline, and does
  285. not extend beyond the next headline, even if that is lower level.
  286. An entry can be toggled between QUOTE and normal with
  287. \\[org-toggle-fixed-width-section]."
  288. :group 'org-keywords
  289. :type 'string)
  290. (defgroup org-structure nil
  291. "Options concerning the general structure of Org-mode files."
  292. :tag "Org Structure"
  293. :group 'org)
  294. (defgroup org-cycle nil
  295. "Options concerning visibility cycling in Org-mode."
  296. :tag "Org Cycle"
  297. :group 'org-structure)
  298. (defcustom org-cycle-global-at-bob t
  299. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  300. This makes it possible to do global cycling without having to use S-TAB or
  301. C-u TAB. For this special case to work, the first line of the buffer
  302. must not be a headline - it may be empty ot some other text. When used in
  303. this way, `org-cycle-hook' is disables temporarily, to make sure the
  304. cursor stays at the beginning of the buffer.
  305. When this option is nil, don't do anything special at the beginning
  306. of the buffer."
  307. :group 'org-cycle
  308. :type 'boolean)
  309. (defcustom org-cycle-emulate-tab t
  310. "Where should `org-cycle' emulate TAB.
  311. nil Never
  312. white Only in completely white lines
  313. whitestart Only at the beginning of lines, before the first non-white char.
  314. t Everywhere except in headlines
  315. If TAB is used in a place where it does not emulate TAB, the current subtree
  316. visibility is cycled."
  317. :group 'org-cycle
  318. :type '(choice (const :tag "Never" nil)
  319. (const :tag "Only in completely white lines" white)
  320. (const :tag "Before first char in a line" whitestart)
  321. (const :tag "Everywhere except in headlines" t)
  322. ))
  323. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  324. org-optimize-window-after-visibility-change)
  325. "Hook that is run after `org-cycle' has changed the buffer visibility.
  326. The function(s) in this hook must accept a single argument which indicates
  327. the new state that was set by the most recent `org-cycle' command. The
  328. argument is a symbol. After a global state change, it can have the values
  329. `overview', `content', or `all'. After a local state change, it can have
  330. the values `folded', `children', or `subtree'."
  331. :group 'org-cycle
  332. :type 'hook)
  333. (defgroup org-edit-structure nil
  334. "Options concerning structure editing in Org-mode."
  335. :tag "Org Edit Structure"
  336. :group 'org-structure)
  337. (defcustom org-odd-levels-only nil
  338. "Non-nil means, skip even levels and only use odd levels for the outline.
  339. This has the effect that two stars are being added/taken away in
  340. promotion/demotion commands. It also influences how levels are
  341. handled by the exporters.
  342. Changing it requires restart of `font-lock-mode' to become effective
  343. for fontification also in regions already fontified.
  344. You may also set this on a per-file basis by adding one of the following
  345. lines to the buffer:
  346. #+STARTUP: odd
  347. #+STARTUP: oddeven"
  348. :group 'org-edit-structure
  349. :group 'org-font-lock
  350. :type 'boolean)
  351. (defcustom org-adapt-indentation t
  352. "Non-nil means, adapt indentation when promoting and demoting.
  353. When this is set and the *entire* text in an entry is indented, the
  354. indentation is increased by one space in a demotion command, and
  355. decreased by one in a promotion command. If any line in the entry
  356. body starts at column 0, indentation is not changed at all."
  357. :group 'org-edit-structure
  358. :type 'boolean)
  359. (defcustom org-blank-before-new-entry '((heading . nil)
  360. (plain-list-item . nil))
  361. "Should `org-insert-heading' leave a blank line before new heading/item?
  362. The value is an alist, with `heading' and `plain-list-item' as car,
  363. and a boolean flag as cdr."
  364. :group 'org-edit-structure
  365. :type '(list
  366. (cons (const heading) (boolean))
  367. (cons (const plain-list-item) (boolean))))
  368. (defcustom org-insert-heading-hook nil
  369. "Hook being run after inserting a new heading."
  370. :group 'org-edit-structure
  371. :type 'boolean)
  372. (defcustom org-enable-fixed-width-editor t
  373. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  374. This currently only means, they are never auto-wrapped.
  375. When nil, such lines will be treated like ordinary lines.
  376. See also the QUOTE keyword."
  377. :group 'org-edit-structure
  378. :type 'boolean)
  379. (defgroup org-sparse-trees nil
  380. "Options concerning sparse trees in Org-mode."
  381. :tag "Org Sparse Trees"
  382. :group 'org-structure)
  383. (defcustom org-highlight-sparse-tree-matches t
  384. "Non-nil means, highlight all matches that define a sparse tree.
  385. The highlights will automatically disappear the next time the buffer is
  386. changed by an edit command."
  387. :group 'org-sparse-trees
  388. :type 'boolean)
  389. (defcustom org-remove-highlights-with-change t
  390. "Non-nil means, any change to the buffer will remove temporary highlights.
  391. Such highlights are created by `org-occur' and `org-clock-display'.
  392. When nil, `C-c C-c needs to be used to get rid of the highlights.
  393. The highlights created by `org-preview-latex-fragment' always need
  394. `C-c C-c' to be removed."
  395. :group 'org-sparse-trees
  396. :group 'org-time
  397. :type 'boolean)
  398. (defcustom org-show-hierarchy-above '((default t))
  399. "Non-nil means, show full hierarchy when showing a spot in the tree.
  400. Turning this off makes sparse trees more compact, but also less clear.
  401. Instead of t, this can also be an alist specifying this option for different
  402. contexts. Valid contexts are
  403. agenda when exposing an entry from the agenda
  404. org-goto when using the command `org-goto' on key C-c C-j
  405. occur-tree when using the command `org-occur' on key C-c /
  406. tags-tree when constructing a sparse tree based on tags matches
  407. link-search when exposing search matches associated with a link
  408. mark-goto when exposing the jump goal of a mark
  409. bookmark-jump when exposing a bookmark location
  410. default default for all contexts not set explicitly"
  411. :group 'org-sparse-trees
  412. :type '(choice
  413. (const :tag "Always" t)
  414. (const :tag "Never" nil)
  415. (repeat :greedy t :tag "Individual contexts"
  416. (cons
  417. (choice :tag "Context"
  418. (const agenda)
  419. (const org-goto)
  420. (const occur-tree)
  421. (const tags-tree)
  422. (const link-search)
  423. (const mark-goto)
  424. (const bookmark-jump)
  425. (const default))
  426. (boolean)))))
  427. (defcustom org-show-following-heading '((default t))
  428. "Non-nil means, show heading following match in `org-occur'.
  429. When doing an `org-occur' it is useful to show the headline which
  430. follows the match, even if they do not match the regexp. This makes it
  431. easier to edit directly inside the sparse tree. However, if you use
  432. `org-occur' mainly as an overview, the following headlines are
  433. unnecessary clutter.
  434. Instead of t, this can also be an alist specifying this option for different
  435. contexts. See `org-show-hierarchy-above' for valid contexts."
  436. :group 'org-sparse-trees
  437. :type '(choice
  438. (const :tag "Always" t)
  439. (const :tag "Never" nil)
  440. (repeat :greedy t :tag "Individual contexts"
  441. (cons
  442. (choice :tag "Context"
  443. (const agenda)
  444. (const org-goto)
  445. (const occur-tree)
  446. (const tags-tree)
  447. (const link-search)
  448. (const mark-goto)
  449. (const bookmark-jump)
  450. (const default))
  451. (boolean)))))
  452. (defcustom org-occur-hook '(org-first-headline-recenter)
  453. "Hook that is run after `org-occur' has constructed a sparse tree.
  454. This can be used to recenter the window to show as much of the structure
  455. as possible."
  456. :group 'org-sparse-trees
  457. :type 'hook)
  458. (defgroup org-plain-lists nil
  459. "Options concerning plain lists in Org-mode."
  460. :tag "Org Plain lists"
  461. :group 'org-structure)
  462. (defcustom org-cycle-include-plain-lists nil
  463. "Non-nil means, include plain lists into visibility cycling.
  464. This means that during cycling, plain list items will *temporarily* be
  465. interpreted as outline headlines with a level given by 1000+i where i is the
  466. indentation of the bullet. In all other operations, plain list items are
  467. not seen as headlines. For example, you cannot assign a TODO keyword to
  468. such an item."
  469. :group 'org-plain-lists
  470. :type 'boolean)
  471. (defcustom org-plain-list-ordered-item-terminator t
  472. "The character that makes a line with leading number an ordered list item.
  473. Valid values are ?. and ?\). To get both terminators, use t. While
  474. ?. may look nicer, it creates the danger that a line with leading
  475. number may be incorrectly interpreted as an item. ?\) therefore is
  476. the safe choice."
  477. :group 'org-plain-lists
  478. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  479. (const :tag "paren like in \"2)\"" ?\))
  480. (const :tab "both" t)))
  481. (defcustom org-auto-renumber-ordered-lists t
  482. "Non-nil means, automatically renumber ordered plain lists.
  483. Renumbering happens when the sequence have been changed with
  484. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  485. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  486. :group 'org-plain-lists
  487. :type 'boolean)
  488. (defcustom org-provide-checkbox-statistics t
  489. "Non-nil means, update checkbox statistics after insert and toggle.
  490. When this is set, checkbox statistics is updated each time you either insert
  491. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  492. with \\[org-ctrl-c-ctrl-c\\]."
  493. :group 'org
  494. :type 'boolean)
  495. (defgroup org-archive nil
  496. "Options concerning archiving in Org-mode."
  497. :tag "Org Archive"
  498. :group 'org-structure)
  499. (defcustom org-archive-tag "ARCHIVE"
  500. "The tag that marks a subtree as archived.
  501. An archived subtree does not open during visibility cycling, and does
  502. not contribute to the agenda listings."
  503. :group 'org-archive
  504. :group 'org-keywords
  505. :type 'string)
  506. (defcustom org-agenda-skip-archived-trees t
  507. "Non-nil means, the agenda will skip any items located in archived trees.
  508. An archived tree is a tree marked with the tag ARCHIVE."
  509. :group 'org-archive
  510. :group 'org-agenda-display
  511. :type 'boolean)
  512. (defcustom org-cycle-open-archived-trees nil
  513. "Non-nil means, `org-cycle' will open archived trees.
  514. An archived tree is a tree marked with the tag ARCHIVE.
  515. When nil, archived trees will stay folded. You can still open them with
  516. normal outline commands like `show-all', but not with the cycling commands."
  517. :group 'org-archive
  518. :group 'org-cycle
  519. :type 'boolean)
  520. (defcustom org-sparse-tree-open-archived-trees nil
  521. "Non-nil means sparse tree construction shows matches in archived trees.
  522. When nil, matches in these trees are highlighted, but the trees are kept in
  523. collapsed state."
  524. :group 'org-archive
  525. :group 'org-sparse-trees
  526. :type 'boolean)
  527. (defcustom org-archive-location "%s_archive::"
  528. "The location where subtrees should be archived.
  529. This string consists of two parts, separated by a double-colon.
  530. The first part is a file name - when omitted, archiving happens in the same
  531. file. %s will be replaced by the current file name (without directory part).
  532. Archiving to a different file is useful to keep archived entries from
  533. contributing to the Org-mode Agenda.
  534. The part after the double colon is a headline. The archived entries will be
  535. filed under that headline. When omitted, the subtrees are simply filed away
  536. at the end of the file, as top-level entries.
  537. Here are a few examples:
  538. \"%s_archive::\"
  539. If the current file is Projects.org, archive in file
  540. Projects.org_archive, as top-level trees. This is the default.
  541. \"::* Archived Tasks\"
  542. Archive in the current file, under the top-level headline
  543. \"* Archived Tasks\".
  544. \"~/org/archive.org::\"
  545. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  546. \"basement::** Finished Tasks\"
  547. Archive in file ./basement (relative path), as level 3 trees
  548. below the level 2 heading \"** Finished Tasks\".
  549. You may set this option on a per-file basis by adding to the buffer a
  550. line like
  551. #+ARCHIVE: basement::** Finished Tasks"
  552. :group 'org-archive
  553. :type 'string)
  554. (defcustom org-archive-mark-done t
  555. "Non-nil means, mark entries as DONE when they are moved to the archive file."
  556. :group 'org-archive
  557. :type 'boolean)
  558. (defcustom org-archive-stamp-time t
  559. "Non-nil means, add a time stamp to entries moved to an archive file.
  560. The time stamp will be added directly after the TODO state keyword in the
  561. first line, so it is probably best to use this in combinations with
  562. `org-archive-mark-done'."
  563. :group 'org-archive
  564. :type 'boolean)
  565. (defgroup org-table nil
  566. "Options concerning tables in Org-mode."
  567. :tag "Org Table"
  568. :group 'org)
  569. (defcustom org-enable-table-editor 'optimized
  570. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  571. When nil, such lines will be treated like ordinary lines.
  572. When equal to the symbol `optimized', the table editor will be optimized to
  573. do the following:
  574. - Automatic overwrite mode in front of whitespace in table fields.
  575. This makes the structure of the table stay in tact as long as the edited
  576. field does not exceed the column width.
  577. - Minimize the number of realigns. Normally, the table is aligned each time
  578. TAB or RET are pressed to move to another field. With optimization this
  579. happens only if changes to a field might have changed the column width.
  580. Optimization requires replacing the functions `self-insert-command',
  581. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  582. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  583. very good at guessing when a re-align will be necessary, but you can always
  584. force one with \\[org-ctrl-c-ctrl-c].
  585. If you would like to use the optimized version in Org-mode, but the
  586. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  587. This variable can be used to turn on and off the table editor during a session,
  588. but in order to toggle optimization, a restart is required.
  589. See also the variable `org-table-auto-blank-field'."
  590. :group 'org-table
  591. :type '(choice
  592. (const :tag "off" nil)
  593. (const :tag "on" t)
  594. (const :tag "on, optimized" optimized)))
  595. (defcustom orgtbl-optimized (eq org-enable-table-editor 'optimized)
  596. "Non-nil means, use the optimized table editor version for `orgtbl-mode'.
  597. In the optimized version, the table editor takes over all simple keys that
  598. normally just insert a character. In tables, the characters are inserted
  599. in a way to minimize disturbing the table structure (i.e. in overwrite mode
  600. for empty fields). Outside tables, the correct binding of the keys is
  601. restored.
  602. The default for this option is t if the optimized version is also used in
  603. Org-mode. See the variable `org-enable-table-editor' for details. Changing
  604. this variable requires a restart of Emacs to become effective."
  605. :group 'org-table
  606. :type 'boolean)
  607. (defgroup org-table-settings nil
  608. "Settings for tables in Org-mode."
  609. :tag "Org Table Settings"
  610. :group 'org-table)
  611. (defcustom org-table-default-size "5x2"
  612. "The default size for newly created tables, Columns x Rows."
  613. :group 'org-table-settings
  614. :type 'string)
  615. (defcustom org-table-number-regexp "^[<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*$"
  616. "Regular expression for recognizing numbers in table columns.
  617. If a table column contains mostly numbers, it will be aligned to the
  618. right. If not, it will be aligned to the left.
  619. The default value of this option is a regular expression which allows
  620. anything which looks remotely like a number as used in scientific
  621. context. For example, all of the following will be considered a
  622. number:
  623. 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
  624. Other options offered by the customize interface are more restrictive."
  625. :group 'org-table-settings
  626. :type '(choice
  627. (const :tag "Positive Integers"
  628. "^[0-9]+$")
  629. (const :tag "Integers"
  630. "^[-+]?[0-9]+$")
  631. (const :tag "Floating Point Numbers"
  632. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
  633. (const :tag "Floating Point Number or Integer"
  634. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
  635. (const :tag "Exponential, Floating point, Integer"
  636. "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
  637. (const :tag "Very General Number-Like"
  638. "^[<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*$")
  639. (string :tag "Regexp:")))
  640. (defcustom org-table-number-fraction 0.5
  641. "Fraction of numbers in a column required to make the column align right.
  642. In a column all non-white fields are considered. If at least this
  643. fraction of fields is matched by `org-table-number-fraction',
  644. alignment to the right border applies."
  645. :group 'org-table-settings
  646. :type 'number)
  647. (defgroup org-table-editing nil
  648. "Bahavior of tables during editing in Org-mode."
  649. :tag "Org Table Editing"
  650. :group 'org-table)
  651. (defcustom org-table-automatic-realign t
  652. "Non-nil means, automatically re-align table when pressing TAB or RETURN.
  653. When nil, aligning is only done with \\[org-table-align], or after column
  654. removal/insertion."
  655. :group 'org-table-editing
  656. :type 'boolean)
  657. (defcustom org-table-limit-column-width t ;kw
  658. "Non-nil means, allow to limit the width of table columns with <N> fields."
  659. :group 'org-table-editing
  660. :type 'boolean)
  661. (defcustom org-table-auto-blank-field t
  662. "Non-nil means, automatically blank table field when starting to type into it.
  663. This only happens when typing immediately after a field motion
  664. command (TAB, S-TAB or RET).
  665. Only relevant when `org-enable-table-editor' is equal to `optimized'."
  666. :group 'org-table-editing
  667. :type 'boolean)
  668. (defcustom org-table-tab-jumps-over-hlines t
  669. "Non-nil means, tab in the last column of a table with jump over a hline.
  670. If a horizontal separator line is following the current line,
  671. `org-table-next-field' can either create a new row before that line, or jump
  672. over the line. When this option is nil, a new line will be created before
  673. this line."
  674. :group 'org-table-editing
  675. :type 'boolean)
  676. (defcustom org-table-tab-recognizes-table.el t
  677. "Non-nil means, TAB will automatically notice a table.el table.
  678. When it sees such a table, it moves point into it and - if necessary -
  679. calls `table-recognize-table'."
  680. :group 'org-table-editing
  681. :type 'boolean)
  682. (defgroup org-table-calculation nil
  683. "Options concerning tables in Org-mode."
  684. :tag "Org Table Calculation"
  685. :group 'org-table)
  686. (defcustom org-table-copy-increment t
  687. "Non-nil means, increment when copying current field with \\[org-table-copy-down]."
  688. :group 'org-table-calculation
  689. :type 'boolean)
  690. (defcustom org-calc-default-modes
  691. '(calc-internal-prec 12
  692. calc-float-format (float 5)
  693. calc-angle-mode deg
  694. calc-prefer-frac nil
  695. calc-symbolic-mode nil
  696. calc-date-format (YYYY "-" MM "-" DD " " Www (" " HH ":" mm))
  697. calc-display-working-message t
  698. )
  699. "List with Calc mode settings for use in calc-eval for table formulas.
  700. The list must contain alternating symbols (Calc modes variables and values).
  701. Don't remove any of the default settings, just change the values. Org-mode
  702. relies on the variables to be present in the list."
  703. :group 'org-table-calculation
  704. :type 'plist)
  705. (defcustom org-table-formula-evaluate-inline t
  706. "Non-nil means, TAB and RET evaluate a formula in current table field.
  707. If the current field starts with an equal sign, it is assumed to be a formula
  708. which should be evaluated as described in the manual and in the documentation
  709. string of the command `org-table-eval-formula'. This feature requires the
  710. Emacs calc package.
  711. When this variable is nil, formula calculation is only available through
  712. the command \\[org-table-eval-formula]."
  713. :group 'org-table-calculation
  714. :type 'boolean)
  715. (defcustom org-table-formula-use-constants t
  716. "Non-nil means, interpret constants in formulas in tables.
  717. A constant looks like `$c' or `$Grav' and will be replaced before evaluation
  718. by the value given in `org-table-formula-constants', or by a value obtained
  719. from the `constants.el' package."
  720. :group 'org-table-calculation
  721. :type 'boolean)
  722. (defcustom org-table-formula-constants nil
  723. "Alist with constant names and values, for use in table formulas.
  724. The car of each element is a name of a constant, without the `$' before it.
  725. The cdr is the value as a string. For example, if you'd like to use the
  726. speed of light in a formula, you would configure
  727. (setq org-table-formula-constants '((\"c\" . \"299792458.\")))
  728. and then use it in an equation like `$1*$c'."
  729. :group 'org-table-calculation
  730. :type '(repeat
  731. (cons (string :tag "name")
  732. (string :tag "value"))))
  733. (defcustom org-table-formula-numbers-only nil
  734. "Non-nil means, calculate only with numbers in table formulas.
  735. Then all input fields will be converted to a number, and the result
  736. must also be a number. When nil, calc's full potential is available
  737. in table calculations, including symbolics etc."
  738. :group 'org-table-calculation
  739. :type 'boolean)
  740. (defcustom org-table-allow-automatic-line-recalculation t
  741. "Non-nil means, lines marked with |#| or |*| will be recomputed automatically.
  742. Automatically means, when TAB or RET or C-c C-c are pressed in the line."
  743. :group 'org-table-calculation
  744. :type 'boolean)
  745. (defgroup org-link nil
  746. "Options concerning links in Org-mode."
  747. :tag "Org Link"
  748. :group 'org)
  749. (defcustom org-descriptive-links t
  750. "Non-nil means, hide link part and only show description of bracket links.
  751. Bracket links are like [[link][descritpion]]. This variable sets the initial
  752. state in new org-mode buffers. The setting can then be toggled on a
  753. per-buffer basis from the Org->Hyperlinks menu."
  754. :group 'org-link
  755. :type 'boolean)
  756. (defcustom org-link-style 'bracket
  757. "The style of links to be inserted with \\[org-insert-link].
  758. Possible values are:
  759. bracket [[link][description]]. This is recommended
  760. plain Description \\n link. The old way, no longer recommended."
  761. :group 'org-link
  762. :type '(choice
  763. (const :tag "Bracket (recommended)" bracket)
  764. (const :tag "Plain (no longer recommended)" plain)))
  765. (defcustom org-link-format "%s"
  766. "Default format for external, URL-like linkes in the buffer.
  767. This is a format string for printf, %s will be replaced by the link text.
  768. The recommended value is just \"%s\", since links will be protected by
  769. enclosing them in double brackets. If you prefer plain links (see variable
  770. `org-link-style'), \"<%s>\" is useful. Some people also recommend an
  771. additional URL: prefix, so the format would be \"<URL:%s>\"."
  772. :group 'org-link
  773. :type '(choice
  774. (const :tag "\"%s\" (e.g. http://www.there.com)" "%s")
  775. (const :tag "\"<%s>\" (e.g. <http://www.there.com>)" "<%s>")
  776. (const :tag "\"<URL:%s>\" (e.g. <URL:http://www.there.com>)" "<URL:%s>")
  777. (string :tag "Other" :value "<%s>")))
  778. (defcustom org-link-file-path-type 'adaptive
  779. "How the path name in file links should be stored.
  780. Valid values are:
  781. relative relative to the current directory, i.e. the directory of the file
  782. into which the link is being inserted.
  783. absolute absolute path, if possible with ~ for home directory.
  784. noabbrev absolute path, no abbreviation of home directory.
  785. adaptive Use relative path for files in the current directory and sub-
  786. directories of it. For other files, use an absolute path."
  787. :group 'org-link
  788. :type '(choice
  789. (const relative)
  790. (const absolute)
  791. (const noabbrev)
  792. (const adaptive)))
  793. (defcustom org-activate-links '(bracket angle plain radio tag date)
  794. "Types of links that should be activated in Org-mode files.
  795. This is a list of symbols, each leading to the activation of a certain link
  796. type. In principle, it does not hurt to turn on most link types - there may
  797. be a small gain when turning off unused link types. The types are:
  798. bracket The recommended [[link][description]] or [[link]] links with hiding.
  799. angular Links in angular brackes that may contain whitespace like
  800. <bbdb:Carsten Dominik>.
  801. plain Plain links in normal text, no whitespace, like http://google.com.
  802. radio Text that is matched by a radio target, see manual for details.
  803. tag Tag settings in a headline (link to tag search).
  804. date Time stamps (link to calendar).
  805. camel CamelCase words defining text searches.
  806. Changing this variable requires a restart of Emacs to become effective."
  807. :group 'org-link
  808. :type '(set (const :tag "Double bracket links (new style)" bracket)
  809. (const :tag "Angular bracket links (old style)" angular)
  810. (const :tag "plain text links" plain)
  811. (const :tag "Radio target matches" radio)
  812. (const :tag "Tags" tag)
  813. (const :tag "Timestamps" date)
  814. (const :tag "CamelCase words" camel)))
  815. (defgroup org-link-store nil
  816. "Options concerning storing links in Org-mode"
  817. :tag "Org Store Link"
  818. :group 'org-link)
  819. (defcustom org-context-in-file-links t
  820. "Non-nil means, file links from `org-store-link' contain context.
  821. A search string will be added to the file name with :: as separator and
  822. used to find the context when the link is activated by the command
  823. `org-open-at-point'.
  824. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  825. negates this setting for the duration of the command."
  826. :group 'org-link-store
  827. :type 'boolean)
  828. (defcustom org-file-link-context-use-camel-case nil
  829. "Non-nil means, use CamelCase to store a search context in a file link.
  830. When nil, the search string simply consists of the words of the string.
  831. CamelCase is deprecated, and support for it may be dropped in the future."
  832. :group 'org-link-store
  833. :type 'boolean)
  834. (defcustom org-keep-stored-link-after-insertion nil
  835. "Non-nil means, keep link in list for entire session.
  836. The command `org-store-link' adds a link pointing to the current
  837. location to an internal list. These links accumulate during a session.
  838. The command `org-insert-link' can be used to insert links into any
  839. Org-mode file (offering completion for all stored links). When this
  840. option is nil, every link which has been inserted once using \\[org-insert-link]
  841. will be removed from the list, to make completing the unused links
  842. more efficient."
  843. :group 'org-link-store
  844. :type 'boolean)
  845. (defcustom org-usenet-links-prefer-google nil
  846. "Non-nil means, `org-store-link' will create web links to Google groups.
  847. When nil, Gnus will be used for such links.
  848. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  849. negates this setting for the duration of the command."
  850. :group 'org-link-store
  851. :type 'boolean)
  852. (defgroup org-link-follow nil
  853. "Options concerning following links in Org-mode"
  854. :tag "Org Follow Link"
  855. :group 'org-link)
  856. (defcustom org-tab-follows-link nil
  857. "Non-nil means, on links TAB will follow the link.
  858. Needs to be set before org.el is loaded."
  859. :group 'org-link-follow
  860. :type 'boolean)
  861. (defcustom org-return-follows-link nil
  862. "Non-nil means, on links RET will follow the link.
  863. Needs to be set before org.el is loaded."
  864. :group 'org-link-follow
  865. :type 'boolean)
  866. (defcustom org-mouse-1-follows-link t
  867. "Non-nil means, mouse-1 on a link will follow the link.
  868. A longer mouse click will still set point. Does not wortk on XEmacs.
  869. Needs to be set before org.el is loaded."
  870. :group 'org-link-follow
  871. :type 'boolean)
  872. (defcustom org-mark-ring-length 4
  873. "Number of different positions to be recorded in the ring
  874. Changing this requires a restart of Emacs to work correctly."
  875. :group 'org-link-follow
  876. :type 'interger)
  877. (defcustom org-link-frame-setup
  878. '((vm . vm-visit-folder-other-frame)
  879. (gnus . gnus-other-frame)
  880. (file . find-file-other-window))
  881. "Setup the frame configuration for following links.
  882. When following a link with Emacs, it may often be useful to display
  883. this link in another window or frame. This variable can be used to
  884. set this up for the different types of links.
  885. For VM, use any of
  886. `vm-visit-folder'
  887. `vm-visit-folder-other-frame'
  888. For Gnus, use any of
  889. `gnus'
  890. `gnus-other-frame'
  891. For FILE, use any of
  892. `find-file'
  893. `find-file-other-window'
  894. `find-file-other-frame'
  895. For the calendar, use the variable `calendar-setup'.
  896. For BBDB, it is currently only possible to display the matches in
  897. another window."
  898. :group 'org-link-follow
  899. :type '(list
  900. (cons (const vm)
  901. (choice
  902. (const vm-visit-folder)
  903. (const vm-visit-folder-other-window)
  904. (const vm-visit-folder-other-frame)))
  905. (cons (const gnus)
  906. (choice
  907. (const gnus)
  908. (const gnus-other-frame)))
  909. (cons (const file)
  910. (choice
  911. (const find-file)
  912. (const find-file-other-window)
  913. (const find-file-other-frame)))))
  914. (defcustom org-open-non-existing-files nil
  915. "Non-nil means, `org-open-file' will open non-existing file.
  916. When nil, an error will be generated."
  917. :group 'org-link-follow
  918. :type 'boolean)
  919. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  920. "Function and arguments to call for following mailto links.
  921. This is a list with the first element being a lisp function, and the
  922. remaining elements being arguments to the function. In string arguments,
  923. %a will be replaced by the address, and %s will be replaced by the subject
  924. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  925. :group 'org-link-follow
  926. :type '(choice
  927. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  928. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  929. (const :tag "message-mail" (message-mail "%a" "%s"))
  930. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  931. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  932. "Non-nil means, ask for confirmation before executing shell links.
  933. Shell links can be dangerous, just thing about a link
  934. [[shell:rm -rf ~/*][Google Search]]
  935. This link would show up in your Org-mode document as \"Google Search\"
  936. but really it would remove your entire home directory.
  937. Therefore I *definitely* advise against setting this variable to nil.
  938. Just change it to `y-or-n-p' of you want to confirm with a single key press
  939. rather than having to type \"yes\"."
  940. :group 'org-link-follow
  941. :type '(choice
  942. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  943. (const :tag "with y-or-n (faster)" y-or-n-p)
  944. (const :tag "no confirmation (dangerous)" nil)))
  945. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  946. "Non-nil means, ask for confirmation before executing elisp links.
  947. Elisp links can be dangerous, just thing about a link
  948. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  949. This link would show up in your Org-mode document as \"Google Search\"
  950. but really it would remove your entire home directory.
  951. Therefore I *definitely* advise against setting this variable to nil.
  952. Just change it to `y-or-n-p' of you want to confirm with a single key press
  953. rather than having to type \"yes\"."
  954. :group 'org-link-follow
  955. :type '(choice
  956. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  957. (const :tag "with y-or-n (faster)" y-or-n-p)
  958. (const :tag "no confirmation (dangerous)" nil)))
  959. (defconst org-file-apps-defaults-gnu
  960. '((remote . emacs)
  961. (t . mailcap))
  962. "Default file applications on a UNIX or GNU/Linux system.
  963. See `org-file-apps'.")
  964. (defconst org-file-apps-defaults-macosx
  965. '((remote . emacs)
  966. (t . "open %s")
  967. ("ps" . "gv %s")
  968. ("ps.gz" . "gv %s")
  969. ("eps" . "gv %s")
  970. ("eps.gz" . "gv %s")
  971. ("dvi" . "xdvi %s")
  972. ("fig" . "xfig %s"))
  973. "Default file applications on a MacOS X system.
  974. The system \"open\" is known as a default, but we use X11 applications
  975. for some files for which the OS does not have a good default.
  976. See `org-file-apps'.")
  977. (defconst org-file-apps-defaults-windowsnt
  978. (list
  979. '(remote . emacs)
  980. (cons t
  981. (list (if (featurep 'xemacs)
  982. 'mswindows-shell-execute
  983. 'w32-shell-execute)
  984. "open" 'file)))
  985. "Default file applications on a Windows NT system.
  986. The system \"open\" is used for most files.
  987. See `org-file-apps'.")
  988. (defcustom org-file-apps
  989. '(
  990. ("txt" . emacs)
  991. ("tex" . emacs)
  992. ("ltx" . emacs)
  993. ("org" . emacs)
  994. ("el" . emacs)
  995. ("bib" . emacs)
  996. )
  997. "External applications for opening `file:path' items in a document.
  998. Org-mode uses system defaults for different file types, but
  999. you can use this variable to set the application for a given file
  1000. extension. The entries in this list are cons cells where the car identifies
  1001. files and the cdr the corresponding command. Possible values for the
  1002. file identifier are
  1003. \"ext\" A string identifying an extension
  1004. `directory' Matches a directory
  1005. `remote' Matches a remote file, accessible through tramp or efs.
  1006. Remote files most likely should be visited through emacs
  1007. because external applications cannot handle such paths.
  1008. t Default for all remaining files
  1009. Possible values for the command are:
  1010. `emacs' The file will be visited by the current Emacs process.
  1011. `default' Use the default application for this file type.
  1012. string A command to be executed by a shell; %s will be replaced
  1013. by the path to the file.
  1014. sexp A Lisp form which will be evaluated. The file path will
  1015. be available in the Lisp variable `file'.
  1016. For more examples, see the system specific constants
  1017. `org-file-apps-defaults-macosx'
  1018. `org-file-apps-defaults-windowsnt'
  1019. `org-file-apps-defaults-gnu'."
  1020. :group 'org-link-follow
  1021. :type '(repeat
  1022. (cons (choice :value ""
  1023. (string :tag "Extension")
  1024. (const :tag "Default for unrecognized files" t)
  1025. (const :tag "Remote file" remote)
  1026. (const :tag "Links to a directory" directory))
  1027. (choice :value ""
  1028. (const :tag "Visit with Emacs" emacs)
  1029. (const :tag "Use system default" default)
  1030. (string :tag "Command")
  1031. (sexp :tag "Lisp form")))))
  1032. (defcustom org-mhe-search-all-folders nil
  1033. "Non-nil means, that the search for the mh-message will be extended to
  1034. all folders if the message cannot be found in the folder given in the link.
  1035. Searching all folders is very effective with one of the search engines
  1036. supported by MH-E, but will be slow with pick."
  1037. :group 'org-link-follow
  1038. :type 'boolean)
  1039. (defgroup org-remember nil
  1040. "Options concerning interaction with remember.el."
  1041. :tag "Org Remember"
  1042. :group 'org)
  1043. (defcustom org-directory "~/org"
  1044. "Directory with org files.
  1045. This directory will be used as default to prompt for org files.
  1046. Used by the hooks for remember.el."
  1047. :group 'org-remember
  1048. :type 'directory)
  1049. (defcustom org-default-notes-file "~/.notes"
  1050. "Default target for storing notes.
  1051. Used by the hooks for remember.el. This can be a string, or nil to mean
  1052. the value of `remember-data-file'."
  1053. :group 'org-remember
  1054. :type '(choice
  1055. (const :tag "Default from remember-data-file" nil)
  1056. file))
  1057. (defcustom org-remember-templates nil
  1058. "Templates for the creation of remember buffers.
  1059. When nil, just let remember make the buffer.
  1060. When not nil, this is a list of 3-element lists. In each entry, the first
  1061. element is a character, a unique key to select this template.
  1062. The second element is the template. The third element is optional and can
  1063. specify a destination file for remember items created with this template.
  1064. The default file is given by `org-default-notes-file'.
  1065. The template specifies the structure of the remember buffer. It should have
  1066. a first line starting with a star, to act as the org-mode headline.
  1067. Furthermore, the following %-escapes will be replaced with content:
  1068. %t time stamp, date only
  1069. %T time stamp with date and time
  1070. %u inactive time stamp, date only
  1071. %U inactive time stamp with date and time
  1072. %n user name
  1073. %a annotation, normally the link created with org-store-link
  1074. %i initial content, the region when remember is called with C-u.
  1075. If %i is indented, the entire inserted text will be indented as well.
  1076. %? This will be removed, and the cursor placed at this position."
  1077. :group 'org-remember
  1078. :type '(repeat :tag "enabled"
  1079. (list :value (?a "\n" nil)
  1080. (character :tag "Selection Key")
  1081. (string :tag "Template")
  1082. (file :tag "Destination file (optional)"))))
  1083. (defcustom org-reverse-note-order nil
  1084. "Non-nil means, store new notes at the beginning of a file or entry.
  1085. When nil, new notes will be filed to the end of a file or entry."
  1086. :group 'org-remember
  1087. :type '(choice
  1088. (const :tag "Reverse always" t)
  1089. (const :tag "Reverse never" nil)
  1090. (repeat :tag "By file name regexp"
  1091. (cons regexp boolean))))
  1092. (defgroup org-todo nil
  1093. "Options concerning TODO items in Org-mode."
  1094. :tag "Org TODO"
  1095. :group 'org)
  1096. (defcustom org-todo-keywords '("TODO" "DONE")
  1097. "List of TODO entry keywords.
  1098. \\<org-mode-map>By default, this is '(\"TODO\" \"DONE\"). The last entry in the list is
  1099. considered to mean that the entry is \"done\". All the other mean that
  1100. action is required, and will make the entry show up in todo lists, diaries
  1101. etc.
  1102. The command \\[org-todo] cycles an entry through these states, and an
  1103. additional state where no keyword is present. For details about this
  1104. cycling, see also the variable `org-todo-interpretation'
  1105. Changes become only effective after restarting Emacs."
  1106. :group 'org-todo
  1107. :group 'org-keywords
  1108. :type '(repeat (string :tag "Keyword")))
  1109. (defcustom org-todo-interpretation 'sequence
  1110. "Controls how TODO keywords are interpreted.
  1111. This variable is only relevant if `org-todo-keywords' contains more than two
  1112. states. \\<org-mode-map>Possible values are `sequence' and `type'.
  1113. When `sequence', \\[org-todo] will always switch to the next state in the
  1114. `org-todo-keywords' list. When `type', \\[org-todo] only cycles from state
  1115. to state when executed several times in direct succession. Otherwise, it
  1116. switches directly to DONE from any state.
  1117. See the manual for more information."
  1118. :group 'org-todo
  1119. :group 'org-keywords
  1120. :type '(choice (const sequence)
  1121. (const type)))
  1122. (defcustom org-after-todo-state-change-hook nil
  1123. "Hook which is run after the state of a TODO item was changed.
  1124. The new state (a string with a TODO keyword, or nil) is available in the
  1125. Lisp variable `state'."
  1126. :group 'org-todo
  1127. :type 'hook)
  1128. (defcustom org-log-done nil
  1129. "When set, insert a (non-active) time stamp when TODO entry is marked DONE.
  1130. When the state of an entry is changed from nothing to TODO, remove a previous
  1131. closing date.
  1132. This can also be a list of symbols indicating under which conditions
  1133. the time stamp recording the action should be annotated with a short note.
  1134. Valid members of this list are
  1135. done Offer to record a note when marking entries done
  1136. clock-out Offer to record a note when clocking out of an item.
  1137. A separate window will then pop up and allow you to type a note.
  1138. After finishing with C-c C-c, the note will be added directly after the
  1139. timestamp, as a plain list item. See also the variable
  1140. `org-log-note-headings'.
  1141. Logging can also be configured on a per-file basis by adding one of
  1142. the following lines anywhere in the buffer:
  1143. #+STARTUP: logging
  1144. #+STARTUP: nologging"
  1145. ;; FIXME: in-buffer words for notes???????
  1146. :group 'org-todo
  1147. :type '(choice
  1148. (const :tag "off" nil)
  1149. (const :tag "on" t)
  1150. (set :tag "on, with notes" :greedy t :value (done)
  1151. (const done) (const clock-out))))
  1152. (defcustom org-log-note-headings '((done . "CLOSING NOTE") (clock-out . ""))
  1153. "Headings for notes added when clocking out or closing TODO items.
  1154. The value is an alist, with the car being a sympol indicating the note
  1155. context, and the cdr is the heading to be used. The heading may also be the
  1156. empty string."
  1157. :group 'org-todo
  1158. :type '(list :greedy t
  1159. (cons (const :tag "Heading when closing an item" done) string)
  1160. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1161. (defgroup org-priorities nil
  1162. "Priorities in Org-mode."
  1163. :tag "Org Priorities"
  1164. :group 'org-todo)
  1165. (defcustom org-default-priority ?B
  1166. "The default priority of TODO items.
  1167. This is the priority an item get if no explicit priority is given."
  1168. :group 'org-priorities
  1169. :type 'character)
  1170. (defcustom org-lowest-priority ?C
  1171. "The lowest priority of TODO items. A character like ?A, ?B etc."
  1172. :group 'org-priorities
  1173. :type 'character)
  1174. (defgroup org-time nil
  1175. "Options concerning time stamps and deadlines in Org-mode."
  1176. :tag "Org Time"
  1177. :group 'org)
  1178. (defcustom org-insert-labeled-timestamps-at-point nil
  1179. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1180. When nil, these labeled time stamps are forces into the second line of an
  1181. entry, just after the headline. When scheduling from the global TODO list,
  1182. the time stamp will always be forced into the second line."
  1183. :group 'org-time
  1184. :type 'boolean)
  1185. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1186. "Formats for `format-time-string' which are used for time stamps.
  1187. It is not recommended to change this constant.")
  1188. (defcustom org-time-stamp-rounding-minutes 0
  1189. "Number of minutes to round time stamps to upon insertion.
  1190. When zero, insert the time unmodified. Useful rounding numbers
  1191. should be factors of 60, so for example 5, 10, 15.
  1192. When this is not zero, you can still force an exact time-stamp by using
  1193. a double prefix argument to a time-stamp command like `C-c .' or `C-c !'."
  1194. :group 'org-time
  1195. :type 'integer)
  1196. (defcustom org-deadline-warning-days 30
  1197. "No. of days before expiration during which a deadline becomes active.
  1198. This variable governs the display in the org file."
  1199. :group 'org-time
  1200. :type 'number)
  1201. (defcustom org-popup-calendar-for-date-prompt t
  1202. "Non-nil means, pop up a calendar when prompting for a date.
  1203. In the calendar, the date can be selected with mouse-1. However, the
  1204. minibuffer will also be active, and you can simply enter the date as well.
  1205. When nil, only the minibuffer will be available."
  1206. :group 'org-time
  1207. :type 'boolean)
  1208. (defcustom org-calendar-follow-timestamp-change t
  1209. "Non-nil means, make the calendar window follow timestamp changes.
  1210. When a timestamp is modified and the calendar window is visible, it will be
  1211. moved to the new date."
  1212. :group 'org-time
  1213. :type 'boolean)
  1214. (defgroup org-tags nil
  1215. "Options concerning tags in Org-mode."
  1216. :tag "Org Tags"
  1217. :group 'org)
  1218. (defcustom org-tag-alist nil
  1219. "List of tags allowed in Org-mode files.
  1220. When this list is nil, Org-mode will base TAG input on what is already in the
  1221. buffer.
  1222. The value of this variable is an alist, the car may be (and should) be a
  1223. character that is used to select that tag through the fast-tag-selection
  1224. interface. See the manual for details."
  1225. :group 'org-tags
  1226. :type '(repeat
  1227. (choice
  1228. (cons (string :tag "Tag name")
  1229. (character :tag "Access char"))
  1230. (const :tag "Start radio group" (:startgroup))
  1231. (const :tag "End radio group" (:endgroup)))))
  1232. (defcustom org-use-fast-tag-selection 'auto
  1233. "Non-nil means, use fast tag selection scheme.
  1234. This is a special interface to select and deselect tags with single keys.
  1235. When nil, fast selection is never used.
  1236. When the symbol `auto', fast selection is used if and only if selection
  1237. characters for tags have been configured, either through the variable
  1238. `org-tag-alist' or through a #+TAGS line in the buffer.
  1239. When t, fast selection is always used and selection keys are assigned
  1240. automatically if necessary."
  1241. :group 'org-tags
  1242. :type '(choice
  1243. (const :tag "Always" t)
  1244. (const :tag "Never" nil)
  1245. (const :tag "When selection characters are configured" 'auto)))
  1246. (defcustom org-tags-column 48
  1247. "The column to which tags should be indented in a headline.
  1248. If this number is positive, it specifies the column. If it is negative,
  1249. it means that the tags should be flushright to that column. For example,
  1250. -79 works well for a normal 80 character screen."
  1251. :group 'org-tags
  1252. :type 'integer)
  1253. (defcustom org-auto-align-tags t
  1254. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1255. These operations change the length of a headline and therefore shift
  1256. the tags around. With this options turned on, after each such operation
  1257. the tags are again aligned to `org-tags-column'."
  1258. :group 'org-tags
  1259. :type 'boolean)
  1260. (defcustom org-use-tag-inheritance t
  1261. "Non-nil means, tags in levels apply also for sublevels.
  1262. When nil, only the tags directly given in a specific line apply there.
  1263. If you turn off this option, you very likely want to turn on the
  1264. companion option `org-tags-match-list-sublevels'."
  1265. :group 'org-tags
  1266. :type 'boolean)
  1267. (defcustom org-tags-match-list-sublevels nil
  1268. "Non-nil means list also sublevels of headlines matching tag search.
  1269. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1270. the sublevels of a headline matching a tag search often also match
  1271. the same search. Listing all of them can create very long lists.
  1272. Setting this variable to nil causes subtrees of a match to be skipped.
  1273. This option is off by default, because inheritance in on. If you turn
  1274. inheritance off, you very likely want to turn this option on.
  1275. As a special case, if the tag search is restricted to TODO items, the
  1276. value of this variable is ignored and sublevels are always checked, to
  1277. make sure all corresponding TODO items find their way into the list."
  1278. :group 'org-tags
  1279. :type 'boolean)
  1280. (defvar org-tags-history nil
  1281. "History of minibuffer reads for tags.")
  1282. (defvar org-last-tags-completion-table nil
  1283. "The last used completion table for tags.")
  1284. (defgroup org-agenda nil
  1285. "Options concerning agenda display Org-mode."
  1286. :tag "Org Agenda"
  1287. :group 'org)
  1288. (defvar org-category nil
  1289. "Variable used by org files to set a category for agenda display.
  1290. Such files should use a file variable to set it, for example
  1291. -*- mode: org; org-category: \"ELisp\"
  1292. or contain a special line
  1293. #+CATEGORY: ELisp
  1294. If the file does not specify a category, then file's base name
  1295. is used instead.")
  1296. (make-variable-buffer-local 'org-category)
  1297. (defcustom org-agenda-files nil
  1298. "The files to be used for agenda display.
  1299. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1300. \\[org-remove-file]. You can also use customize to edit the list.
  1301. If the value of the variable is not a list but a single file name, then
  1302. the list of agenda files is actually stored and maintained in that file, one
  1303. agenda file per line."
  1304. :group 'org-agenda
  1305. :type '(choice
  1306. (repeat :tag "List of files" file)
  1307. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1308. (defcustom org-agenda-custom-commands ;'(("w" todo "WAITING"))
  1309. '(("w" todo "WAITING" ((aaa 1) (bbb 2))))
  1310. "Custom commands for the agenda.
  1311. These commands will be offered on the splash screen displayed by the
  1312. agenda dispatcher \\[org-agenda]. Each entry is a list like this:
  1313. (key type match options)
  1314. key The key (a single char as a string) to be associated with the command.
  1315. type The command type, any of the following symbols:
  1316. todo Entries with a specific TODO keyword, in all agenda files.
  1317. tags Tags match in all agenda files.
  1318. tags-todo Tags match in all agenda files, TODO entries only.
  1319. todo-tree Sparse tree of specific TODO keyword in *current* file.
  1320. tags-tree Sparse tree with all tags matches in *current* file.
  1321. occur-tree Occur sparse tree for *current* file.
  1322. match What to search for:
  1323. - a single keyword for TODO keyword searches
  1324. - a tags match expression for tags searches
  1325. - a regular expression for occur searches
  1326. options A list of option setttings, similar to that in a let form, so like
  1327. this: ((opt1 val1) (opt2 val2) ...)
  1328. You can also define a set of commands, to create a composite agenda buffer.
  1329. In this case, an entry looks like this:
  1330. (key desc (cmd1 cmd2 ...) general-options)
  1331. where
  1332. desc A description string to be displayed in the dispatcher menu.
  1333. cmd An agenda command, similar to the above. However, tree commands
  1334. are no allowed, but instead you can get agenda and global todo list.
  1335. So valid commands for a set are:
  1336. (agenda)
  1337. (alltodo)
  1338. (todo \"match\" options)
  1339. (tags \"match\" options )
  1340. (tags-todo \"match\" options)
  1341. Each command can carry a list of options, and another set of options can be
  1342. given for the whole set of commands. Individual command options take
  1343. precedence over the general options."
  1344. :group 'org-agenda
  1345. :type '(repeat
  1346. (choice
  1347. (list :tag "Single command"
  1348. (string :tag "Key")
  1349. (choice
  1350. (const :tag "Tags search (all agenda files)" tags)
  1351. (const :tag "Tags search of TODO entries (all agenda files)" tags-todo)
  1352. (const :tag "TODO keyword search (all agenda files)" todo)
  1353. (const :tag "Tags sparse tree (current buffer)" tags-tree)
  1354. (const :tag "TODO keyword tree (current buffer)" todo-tree)
  1355. (const :tag "Occur tree (current buffer)" occur-tree))
  1356. (string :tag "Match")
  1357. (repeat :tag "Local options"
  1358. (list (variable :tag "Option") (sexp :tag "Value"))))
  1359. (list :tag "Command series, all agenda files"
  1360. (string :tag "Key")
  1361. (string :tag "Description")
  1362. (repeat
  1363. (choice
  1364. (const :tag "Agenda" (agenda))
  1365. (const :tag "TODO list" (alltodo))
  1366. (list :tag "Tags search"
  1367. (const :format "" tags)
  1368. (string :tag "Match")
  1369. (repeat :tag "Local options"
  1370. (list (variable :tag "Option")
  1371. (sexp :tag "Value"))))
  1372. (list :tag "Tags search, TODO entries only"
  1373. (const :format "" tags-todo)
  1374. (string :tag "Match")
  1375. (repeat :tag "Local options"
  1376. (list (variable :tag "Option")
  1377. (sexp :tag "Value"))))
  1378. (list :tag "TODO keyword search"
  1379. (const :format "" todo)
  1380. (string :tag "Match")
  1381. (repeat :tag "Local options"
  1382. (list (variable :tag "Option")
  1383. (sexp :tag "Value"))))))
  1384. (repeat :tag "General options"
  1385. (list (variable :tag "Option")
  1386. (sexp :tag "Value")))))))
  1387. (defcustom org-agenda-todo-list-sublevels t
  1388. "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
  1389. When nil, the sublevels of a TODO entry are not checked, resulting in
  1390. potentially much shorter TODO lists."
  1391. :group 'org-agenda
  1392. :group 'org-todo
  1393. :type 'boolean)
  1394. (defcustom org-agenda-todo-ignore-scheduled nil
  1395. "Non-nil means, don't show scheduled entries in the global todo list.
  1396. The idea behind this is that by scheduling it, you have already taken care
  1397. of this item."
  1398. :group 'org-agenda
  1399. :group 'org-todo
  1400. :type 'boolean)
  1401. (defcustom org-timeline-show-empty-dates 3
  1402. "Non-nil means, `org-timeline' also shows dates without an entry.
  1403. When nil, only the days which actually have entries are shown.
  1404. When t, all days between the first and the last date are shown.
  1405. When an integer, show also empty dates, but if there is a gap of more than
  1406. N days, just insert a special line indicating the size of the gap."
  1407. :group 'org-agenda
  1408. :type '(choice
  1409. (const :tag "None" nil)
  1410. (const :tag "All" t)
  1411. (number :tag "at most")))
  1412. (defcustom org-agenda-include-all-todo nil
  1413. "Set means weekly/daily agenda will always contain all TODO entries.
  1414. The TODO entries will be listed at the top of the agenda, before
  1415. the entries for specific days."
  1416. :group 'org-agenda
  1417. :type 'boolean)
  1418. (defcustom org-agenda-include-diary nil
  1419. "If non-nil, include in the agenda entries from the Emacs Calendar's diary."
  1420. :group 'org-agenda
  1421. :type 'boolean)
  1422. (defcustom org-calendar-to-agenda-key [?c]
  1423. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1424. The command `org-calendar-goto-agenda' will be bound to this key. The
  1425. default is the character `c' because then `c' can be used to switch back and
  1426. forth between agenda and calendar."
  1427. :group 'org-agenda
  1428. :type 'sexp)
  1429. (defgroup org-agenda-setup nil
  1430. "Options concerning setting up the Agenda window in Org Mode."
  1431. :tag "Org Agenda Window Setup"
  1432. :group 'org-agenda)
  1433. (defcustom org-agenda-mouse-1-follows-link nil
  1434. "Non-nil means, mouse-1 on a link will follow the link in the agenda.
  1435. A longer mouse click will still set point. Does not wortk on XEmacs.
  1436. Needs to be set before org.el is loaded."
  1437. :group 'org-agenda-setup
  1438. :type 'boolean)
  1439. (defcustom org-agenda-start-with-follow-mode nil
  1440. "The initial value of follwo-mode in a newly created agenda window."
  1441. :group 'org-agenda-setup
  1442. :type 'boolean)
  1443. (defcustom org-select-agenda-window t
  1444. "Non-nil means, after creating an agenda, move cursor into Agenda window.
  1445. When nil, cursor will remain in the current window."
  1446. :group 'org-agenda-setup
  1447. :type 'boolean)
  1448. (defcustom org-fit-agenda-window t
  1449. "Non-nil means, change window size of agenda to fit content."
  1450. :group 'org-agenda-setup
  1451. :type 'boolean)
  1452. (defcustom org-finalize-agenda-hook nil
  1453. "Hook run just before displaying an agenda buffer."
  1454. :group 'org-agenda ;??????
  1455. :type 'hook)
  1456. (defgroup org-agenda-display nil
  1457. "Options concerning what to display initially in Agenda."
  1458. :tag "Org Agenda Display"
  1459. :group 'org-agenda)
  1460. (defcustom org-agenda-show-all-dates t
  1461. "Non-nil means, `org-agenda' shows every day in the selected range.
  1462. When nil, only the days which actually have entries are shown."
  1463. :group 'org-agenda-display
  1464. :type 'boolean)
  1465. (defcustom org-agenda-start-on-weekday 1
  1466. "Non-nil means, start the overview always on the specified weekday.
  1467. 0 denotes Sunday, 1 denotes Monday etc.
  1468. When nil, always start on the current day."
  1469. :group 'org-agenda-display
  1470. :type '(choice (const :tag "Today" nil)
  1471. (number :tag "Weekday No.")))
  1472. (defcustom org-agenda-ndays 7
  1473. "Number of days to include in overview display.
  1474. Should be 1 or 7."
  1475. :group 'org-agenda-display
  1476. :type 'number)
  1477. (defcustom org-agenda-use-time-grid t
  1478. "Non-nil means, show a time grid in the agenda schedule.
  1479. A time grid is a set of lines for specific times (like every two hours between
  1480. 8:00 and 20:00). The items scheduled for a day at specific times are
  1481. sorted in between these lines.
  1482. For details about when the grid will be shown, and what it will look like, see
  1483. the variable `org-agenda-time-grid'."
  1484. :group 'org-agenda-display
  1485. :type 'boolean)
  1486. (defcustom org-agenda-time-grid
  1487. '((daily today require-timed)
  1488. "----------------"
  1489. (800 1000 1200 1400 1600 1800 2000))
  1490. "The settings for time grid for agenda display.
  1491. This is a list of three items. The first item is again a list. It contains
  1492. symbols specifying conditions when the grid should be displayed:
  1493. daily if the agenda shows a single day
  1494. weekly if the agenda shows an entire week
  1495. today show grid on current date, independent of daily/weekly display
  1496. require-timed show grid only if at least one item has a time specification
  1497. The second item is a string which will be places behing the grid time.
  1498. The third item is a list of integers, indicating the times that should have
  1499. a grid line."
  1500. :group 'org-agenda-display
  1501. :type
  1502. '(list
  1503. (set :greedy t :tag "Grid Display Options"
  1504. (const :tag "Show grid in single day agenda display" daily)
  1505. (const :tag "Show grid in weekly agenda display" weekly)
  1506. (const :tag "Always show grid for today" today)
  1507. (const :tag "Show grid only if any timed entries are present"
  1508. require-timed)
  1509. (const :tag "Skip grid times already present in an entry"
  1510. remove-match))
  1511. (string :tag "Grid String")
  1512. (repeat :tag "Grid Times" (integer :tag "Time"))))
  1513. (let ((sorting-choice
  1514. '(choice
  1515. (const time-up) (const time-down)
  1516. (const category-keep) (const category-up) (const category-down)
  1517. (const tag-down) (const tag-up)
  1518. (const priority-up) (const priority-down))))
  1519. (defcustom org-agenda-sorting-strategy
  1520. '((agenda time-up category-keep priority-down)
  1521. (todo category-keep priority-down)
  1522. (tags category-keep))
  1523. "Sorting structure for the agenda items of a single day.
  1524. This is a list of symbols which will be used in sequence to determine
  1525. if an entry should be listed before another entry. The following
  1526. symbols are recognized:
  1527. time-up Put entries with time-of-day indications first, early first
  1528. time-down Put entries with time-of-day indications first, late first
  1529. category-keep Keep the default order of categories, corresponding to the
  1530. sequence in `org-agenda-files'.
  1531. category-up Sort alphabetically by category, A-Z.
  1532. category-down Sort alphabetically by category, Z-A.
  1533. tag-up Sort alphabetically by last tag, A-Z.
  1534. tag-down Sort alphabetically by last tag, Z-A.
  1535. priority-up Sort numerically by priority, high priority last.
  1536. priority-down Sort numerically by priority, high priority first.
  1537. The different possibilities will be tried in sequence, and testing stops
  1538. if one comparison returns a \"not-equal\". For example, the default
  1539. '(time-up category-keep priority-down)
  1540. means: Pull out all entries having a specified time of day and sort them,
  1541. in order to make a time schedule for the current day the first thing in the
  1542. agenda listing for the day. Of the entries without a time indication, keep
  1543. the grouped in categories, don't sort the categories, but keep them in
  1544. the sequence given in `org-agenda-files'. Within each category sort by
  1545. priority.
  1546. Leaving out `category-keep' would mean that items will be sorted across
  1547. categories by priority."
  1548. :group 'org-agenda-display
  1549. :type `(choice
  1550. (repeat :tag "General" ,sorting-choice)
  1551. (list :tag "Individually"
  1552. (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
  1553. (repeat ,sorting-choice))
  1554. (cons (const :tag "Strategy for TODO lists" todo)
  1555. (repeat ,sorting-choice))
  1556. (cons (const :tag "Strategy for Tags matches" tags)
  1557. (repeat ,sorting-choice))))))
  1558. (defcustom org-sort-agenda-notime-is-late t
  1559. "Non-nil means, items without time are considered late.
  1560. This is only relevant for sorting. When t, items which have no explicit
  1561. time like 15:30 will be considered as 99:01, i.e. later than any items which
  1562. do have a time. When nil, the default time is before 0:00. You can use this
  1563. option to decide if the schedule for today should come before or after timeless
  1564. agenda entries."
  1565. :group 'org-agenda-display
  1566. :type 'boolean)
  1567. (defgroup org-agenda-prefix nil
  1568. "Options concerning the entry prefix in the Org-mode agenda display."
  1569. :tag "Org Agenda Prefix"
  1570. :group 'org-agenda)
  1571. (defcustom org-agenda-prefix-format
  1572. '((agenda . " %-12:c%?-12t% s")
  1573. (timeline . " % s")
  1574. (todo . " %-12:c")
  1575. (tags . " %-12:c"))
  1576. "Format specifications for the prefix of items in the agenda views.
  1577. An alist with four entries, for the different agenda types. The keys to the
  1578. sublists are `agenda', `timeline', `todo', and `tags'. The values
  1579. are format strings.
  1580. This format works similar to a printf format, with the following meaning:
  1581. %c the category of the item, \"Diary\" for entries from the diary, or
  1582. as given by the CATEGORY keyword or derived from the file name.
  1583. %T the *last* tag of the item. Last because inherited tags come
  1584. first in the list.
  1585. %t the time-of-day specification if one applies to the entry, in the
  1586. format HH:MM
  1587. %s Scheduling/Deadline information, a short string
  1588. All specifiers work basically like the standard `%s' of printf, but may
  1589. contain two additional characters: A question mark just after the `%' and
  1590. a whitespace/punctuation character just before the final letter.
  1591. If the first character after `%' is a question mark, the entire field
  1592. will only be included if the corresponding value applies to the
  1593. current entry. This is useful for fields which should have fixed
  1594. width when present, but zero width when absent. For example,
  1595. \"%?-12t\" will result in a 12 character time field if a time of the
  1596. day is specified, but will completely disappear in entries which do
  1597. not contain a time.
  1598. If there is punctuation or whitespace character just before the final
  1599. format letter, this character will be appended to the field value if
  1600. the value is not empty. For example, the format \"%-12:c\" leads to
  1601. \"Diary: \" if the category is \"Diary\". If the category were be
  1602. empty, no additional colon would be interted.
  1603. The default value of this option is \" %-12:c%?-12t% s\", meaning:
  1604. - Indent the line with two space characters
  1605. - Give the category in a 12 chars wide field, padded with whitespace on
  1606. the right (because of `-'). Append a colon if there is a category
  1607. (because of `:').
  1608. - If there is a time-of-day, put it into a 12 chars wide field. If no
  1609. time, don't put in an empty field, just skip it (because of '?').
  1610. - Finally, put the scheduling information and append a whitespace.
  1611. As another example, if you don't want the time-of-day of entries in
  1612. the prefix, you could use:
  1613. (setq org-agenda-prefix-format \" %-11:c% s\")
  1614. See also the variables `org-agenda-remove-times-when-in-prefix' and
  1615. `org-agenda-remove-tags-when-in-prefix'."
  1616. :type '(choice
  1617. (string :tag "General format")
  1618. (list :greedy t :tag "View dependent"
  1619. (cons (const agenda) (string :tag "Format"))
  1620. (cons (const timeline) (string :tag "Format"))
  1621. (cons (const todo) (string :tag "Format"))
  1622. (cons (const tags) (string :tag "Format"))))
  1623. :group 'org-agenda-prefix)
  1624. (defvar org-prefix-format-compiled nil
  1625. "The compiled version of the most recently used prefix format.
  1626. See the variable `org-agenda-prefix-format'.")
  1627. (defcustom org-agenda-remove-times-when-in-prefix t
  1628. "Non-nil means, remove duplicate time specifications in agenda items.
  1629. When the format `org-agenda-prefix-format' contains a `%t' specifier, a
  1630. time-of-day specification in a headline or diary entry is extracted and
  1631. placed into the prefix. If this option is non-nil, the original specification
  1632. \(a timestamp or -range, or just a plain time(range) specification like
  1633. 11:30-4pm) will be removed for agenda display. This makes the agenda less
  1634. cluttered.
  1635. The option can be t or nil. It may also be the symbol `beg', indicating
  1636. that the time should only be removed what it is located at the beginning of
  1637. the headline/diary entry."
  1638. :group 'org-agenda-prefix
  1639. :type '(choice
  1640. (const :tag "Always" t)
  1641. (const :tag "Never" nil)
  1642. (const :tag "When at beginning of entry" beg)))
  1643. (defcustom org-agenda-remove-tags-when-in-prefix nil
  1644. "Non-nil means, remove the tags from the headline copy in the agenda.
  1645. When this is the symbol `prefix', only remove tags when
  1646. `org-agenda-prefix-format' contains a `%T' specifier."
  1647. :group 'org-agenda-prefix
  1648. :type '(choice
  1649. (const :tag "Always" t)
  1650. (const :tag "Never" nil)
  1651. (const :tag "When prefix format contains %T" prefix)))
  1652. (defcustom org-agenda-align-tags-to-column 65
  1653. "Shift tags in agenda items to this column."
  1654. :group 'org-agenda-prefix
  1655. :type 'integer)
  1656. (defgroup org-latex nil
  1657. "Options for embedding LaTeX code into Org-mode"
  1658. :tag "Org LaTeX"
  1659. :group 'org)
  1660. (defcustom org-format-latex-options
  1661. '(:foreground "Black" :background "Transparent" :scale 1.0
  1662. :matchers ("begin" "$" "$$" "\\(" "\\["))
  1663. "Options for creating images from LaTeX fragments.
  1664. This is a property list with the following properties:
  1665. :foreground the foreground color, for example \"Black\".
  1666. :background the background color, or \"Transparent\".
  1667. :scale a scaling factor for the size of the images
  1668. :matchers a list indicating which matchers should be used to
  1669. find LaTeX fragments. Valid members of this list are:
  1670. \"begin\" find environments
  1671. \"$\" find math expressions surrounded by $...$
  1672. \"$$\" find math expressions surrounded by $$....$$
  1673. \"\\(\" find math expressions surrounded by \\(...\\)
  1674. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  1675. :group 'org-latex
  1676. :type 'plist)
  1677. (defgroup org-export nil
  1678. "Options for exporting org-listings."
  1679. :tag "Org Export"
  1680. :group 'org)
  1681. (defgroup org-export-general nil
  1682. "General options for exporting Org-mode files."
  1683. :tag "Org Export General"
  1684. :group 'org-export)
  1685. (defcustom org-export-publishing-directory "."
  1686. "Path to the location where exported files should be located.
  1687. This path may be relative to the directory where the Org-mode file lives.
  1688. The default is to put them into the same directory as the Org-mode file.
  1689. The variable may also be an alist with export types `:html', `:ascii',
  1690. `:ical', or `:xoxo' and the corresponding directories. If a direcoty path
  1691. is relative, it is interpreted relative to the directory where the exported
  1692. Org-mode files lives."
  1693. :group 'org-export-general
  1694. :type '(choice
  1695. (directory)
  1696. (repeat
  1697. (cons
  1698. (choice :tag "Type"
  1699. (const :html) (const :ascii) (const :ical) (const :xoxo))
  1700. (directory)))))
  1701. (defcustom org-export-language-setup
  1702. '(("en" "Author" "Date" "Table of Contents")
  1703. ("cs" "Autor" "Datum" "Obsah")
  1704. ("da" "Ophavsmand" "Dato" "Indhold")
  1705. ("de" "Autor" "Datum" "Inhaltsverzeichnis")
  1706. ("es" "Autor" "Fecha" "\xccndice")
  1707. ("fr" "Auteur" "Date" "Table des Mati\xe8res")
  1708. ("it" "Autore" "Data" "Indice")
  1709. ("nl" "Auteur" "Datum" "Inhoudsopgave")
  1710. ("nn" "Forfattar" "Dato" "Innhold") ;; nn = Norsk (nynorsk)
  1711. ("sv" "F\xf6rfattarens" "Datum" "Inneh\xe5ll"))
  1712. "Terms used in export text, translated to different languages.
  1713. Use the variable `org-export-default-language' to set the language,
  1714. or use the +OPTION lines for a per-file setting."
  1715. :group 'org-export-general
  1716. :type '(repeat
  1717. (list
  1718. (string :tag "HTML language tag")
  1719. (string :tag "Author")
  1720. (string :tag "Date")
  1721. (string :tag "Table of Contents"))))
  1722. (defcustom org-export-default-language "en"
  1723. "The default language of HTML export, as a string.
  1724. This should have an association in `org-export-language-setup'."
  1725. :group 'org-export-general
  1726. :type 'string)
  1727. (defcustom org-export-headline-levels 3
  1728. "The last level which is still exported as a headline.
  1729. Inferior levels will produce itemize lists when exported.
  1730. Note that a numeric prefix argument to an exporter function overrides
  1731. this setting.
  1732. This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
  1733. :group 'org-export-general
  1734. :type 'number)
  1735. (defcustom org-export-with-section-numbers t
  1736. "Non-nil means, add section numbers to headlines when exporting.
  1737. This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
  1738. :group 'org-export-general
  1739. :type 'boolean)
  1740. (defcustom org-export-with-toc t
  1741. "Non-nil means, create a table of contents in exported files.
  1742. The TOC contains headlines with levels up to`org-export-headline-levels'.
  1743. Headlines which contain any TODO items will be marked with \"(*)\" in
  1744. ASCII export, and with red color in HTML output.
  1745. In HTML output, the TOC will be clickable.
  1746. This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"."
  1747. :group 'org-export-general
  1748. :type 'boolean)
  1749. (defcustom org-export-mark-todo-in-toc nil
  1750. "Non-nil means, mark TOC lines that contain any open TODO items."
  1751. :group 'org-export-general
  1752. :type 'boolean)
  1753. (defcustom org-export-preserve-breaks nil
  1754. "Non-nil means, preserve all line breaks when exporting.
  1755. Normally, in HTML output paragraphs will be reformatted. In ASCII
  1756. export, line breaks will always be preserved, regardless of this variable.
  1757. This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
  1758. :group 'org-export-general
  1759. :type 'boolean)
  1760. (defcustom org-export-with-archived-trees 'headline
  1761. "Whether subtrees with the ARCHIVE tag should be exported.
  1762. This can have three different values
  1763. nil Do not export, pretend this tree is not present
  1764. t Do export the entire tree
  1765. headline Only export the headline, but skip the tree below it."
  1766. :group 'org-export-general
  1767. :group 'org-archive
  1768. :type '(choice
  1769. (const :tag "not at all" nil)
  1770. (const :tag "headline only" 'headline)
  1771. (const :tag "entirely" t)))
  1772. (defcustom org-export-with-timestamps t
  1773. "Nil means, do not export time stamps and associated keywords."
  1774. :group 'org-export-general
  1775. :type 'boolean)
  1776. (defcustom org-export-remove-timestamps-from-toc t
  1777. "Nil means, remove timestamps from the table of contents entries."
  1778. :group 'org-export-general
  1779. :type 'boolean)
  1780. (defcustom org-export-with-tags 'not-in-toc
  1781. "Nil means, do not export tags, just remove them from headlines.
  1782. If this is the sysmbol `not-in-toc', tags will be removed from table of
  1783. contents entries, but still be shown in the headlines of the document."
  1784. :group 'org-export-general
  1785. :type '(choice
  1786. (const :tag "Off" nil)
  1787. (const :tag "Not in TOC" not-in-toc)
  1788. (const :tag "On" t)))
  1789. (defgroup org-export-translation nil
  1790. "Options for translating special ascii sequences for the export backends."
  1791. :tag "Org Export Translation"
  1792. :group 'org-export)
  1793. (defcustom org-export-with-emphasize t
  1794. "Non-nil means, interpret *word*, /word/, and _word_ as emphasized text.
  1795. If the export target supports emphasizing text, the word will be
  1796. typeset in bold, italic, or underlined, respectively. Works only for
  1797. single words, but you can say: I *really* *mean* *this*.
  1798. Not all export backends support this.
  1799. This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
  1800. :group 'org-export-translation
  1801. :type 'boolean)
  1802. (defcustom org-export-with-sub-superscripts t
  1803. "Non-nil means, interpret \"_\" and \"^\" for export.
  1804. When this option is turned on, you can use TeX-like syntax for sub- and
  1805. superscripts. Several characters after \"_\" or \"^\" will be
  1806. considered as a single item - so grouping with {} is normally not
  1807. needed. For example, the following things will be parsed as single
  1808. sub- or superscripts.
  1809. 10^24 or 10^tau several digits will be considered 1 item.
  1810. 10^-12 or 10^-tau a leading sign with digits or a word
  1811. x^2-y^3 will be read as x^2 - y^3, because items are
  1812. terminated by almost any nonword/nondigit char.
  1813. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  1814. Still, ambiguity is possible - so when in doubt use {} to enclose the
  1815. sub/superscript.
  1816. Not all export backends support this, but HTML does.
  1817. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  1818. :group 'org-export-translation
  1819. :type 'boolean)
  1820. (defcustom org-export-with-TeX-macros t
  1821. "Non-nil means, interpret simple TeX-like macros when exporting.
  1822. For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
  1823. No only real TeX macros will work here, but the standard HTML entities
  1824. for math can be used as macro names as well. For a list of supported
  1825. names in HTML export, see the constant `org-html-entities'.
  1826. Not all export backends support this.
  1827. This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
  1828. :group 'org-export-translation
  1829. :group 'org-latex
  1830. :type 'boolean)
  1831. (defcustom org-export-with-LaTeX-fragments nil
  1832. "Non-nil means, convert LaTeX fragments to images when exporting to HTML.
  1833. When set, the exporter will find LaTeX environments if the \\begin line is
  1834. the first non-white thing on a line. It will also find the math delimiters
  1835. like $a=b$ and \\( a=b \\) for inline math, $$a=b$$ and \\[ a=b \\] for
  1836. display math.
  1837. This option can also be set with the +OPTIONS line, e.g. \"LaTeX:t\"."
  1838. :group 'org-export-translation
  1839. :group 'org-latex
  1840. :type 'boolean)
  1841. (defcustom org-export-with-fixed-width t
  1842. "Non-nil means, lines starting with \":\" will be in fixed width font.
  1843. This can be used to have pre-formatted text, fragments of code etc. For
  1844. example:
  1845. : ;; Some Lisp examples
  1846. : (while (defc cnt)
  1847. : (ding))
  1848. will be looking just like this in also HTML. See also the QUOTE keyword.
  1849. Not all export backends support this.
  1850. This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
  1851. :group 'org-export-translation
  1852. :type 'boolean)
  1853. (defcustom org-match-sexp-depth 3
  1854. "Number of stacked braces for sub/superscript matching.
  1855. This has to be set before loading org.el to be effective."
  1856. :group 'org-export-translation
  1857. :type 'integer)
  1858. (defgroup org-export-tables nil
  1859. "Options for exporting tables in Org-mode."
  1860. :tag "Org Export Tables"
  1861. :group 'org-export)
  1862. (defcustom org-export-with-tables t
  1863. "If non-nil, lines starting with \"|\" define a table.
  1864. For example:
  1865. | Name | Address | Birthday |
  1866. |-------------+----------+-----------|
  1867. | Arthur Dent | England | 29.2.2100 |
  1868. Not all export backends support this.
  1869. This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
  1870. :group 'org-export-tables
  1871. :type 'boolean)
  1872. (defcustom org-export-highlight-first-table-line t
  1873. "Non-nil means, highlight the first table line.
  1874. In HTML export, this means use <th> instead of <td>.
  1875. In tables created with table.el, this applies to the first table line.
  1876. In Org-mode tables, all lines before the first horizontal separator
  1877. line will be formatted with <th> tags."
  1878. :group 'org-export-tables
  1879. :type 'boolean)
  1880. (defcustom org-export-table-remove-special-lines t
  1881. "Remove special lines and marking characters in calculating tables.
  1882. This removes the special marking character column from tables that are set
  1883. up for spreadsheet calculations. It also removes the entire lines
  1884. marked with `!', `_', or `^'. The lines with `$' are kept, because
  1885. the values of constants may be useful to have."
  1886. :group 'org-export-tables
  1887. :type 'boolean)
  1888. (defcustom org-export-prefer-native-exporter-for-tables nil
  1889. "Non-nil means, always export tables created with table.el natively.
  1890. Natively means, use the HTML code generator in table.el.
  1891. When nil, Org-mode's own HTML generator is used when possible (i.e. if
  1892. the table does not use row- or column-spanning). This has the
  1893. advantage, that the automatic HTML conversions for math symbols and
  1894. sub/superscripts can be applied. Org-mode's HTML generator is also
  1895. much faster."
  1896. :group 'org-export-tables
  1897. :type 'boolean)
  1898. (defgroup org-export-ascii nil
  1899. "Options specific for ASCII export of Org-mode files."
  1900. :tag "Org Export ASCII"
  1901. :group 'org-export)
  1902. (defcustom org-export-ascii-underline '(?\$ ?\# ?^ ?\~ ?\= ?\-)
  1903. "Characters for underlining headings in ASCII export.
  1904. In the given sequence, these characters will be used for level 1, 2, ..."
  1905. :group 'org-export-ascii
  1906. :type '(repeat character))
  1907. (defcustom org-export-ascii-bullets '(?* ?+ ?-)
  1908. "Bullet characters for headlines converted to lists in ASCII export.
  1909. The first character is is used for the first lest level generated in this
  1910. way, and so on. If there are more levels than characters given here,
  1911. the list will be repeated.
  1912. Note that plain lists will keep the same bullets as the have in the
  1913. Org-mode file."
  1914. :group 'org-export-ascii
  1915. :type '(repeat character))
  1916. (defcustom org-export-ascii-show-new-buffer t
  1917. "Non-nil means, popup buffer containing the exported ASCII text.
  1918. Otherwise the buffer will just be saved to a file and stay hidden."
  1919. :group 'org-export-ascii
  1920. :type 'boolean)
  1921. (defgroup org-export-xml nil
  1922. "Options specific for XML export of Org-mode files."
  1923. :tag "Org Export XML"
  1924. :group 'org-export)
  1925. (defgroup org-export-html nil
  1926. "Options specific for HTML export of Org-mode files."
  1927. :tag "Org Export HTML"
  1928. :group 'org-export)
  1929. (defcustom org-export-html-style
  1930. "<style type=\"text/css\">
  1931. html {
  1932. font-family: Times, serif;
  1933. font-size: 12pt;
  1934. }
  1935. .title { text-align: center; }
  1936. .todo { color: red; }
  1937. .done { color: green; }
  1938. .timestamp { color: grey }
  1939. .timestamp-kwd { color: CadetBlue }
  1940. .tag { background-color:lightblue; font-weight:normal }
  1941. .target { background-color: lavender; }
  1942. pre {
  1943. border: 1pt solid #AEBDCC;
  1944. background-color: #F3F5F7;
  1945. padding: 5pt;
  1946. font-family: courier, monospace;
  1947. }
  1948. table { border-collapse: collapse; }
  1949. td, th {
  1950. vertical-align: top;
  1951. border: 1pt solid #ADB9CC;
  1952. }
  1953. </style>"
  1954. "The default style specification for exported HTML files.
  1955. Since there are different ways of setting style information, this variable
  1956. needs to contain the full HTML structure to provide a style, including the
  1957. surrounding HTML tags. The style specifications should include definitions
  1958. for new classes todo, done, title, and deadline. For example, legal values
  1959. would be:
  1960. <style type=\"text/css\">
  1961. p { font-weight: normal; color: gray; }
  1962. h1 { color: black; }
  1963. .title { text-align: center; }
  1964. .todo, .deadline { color: red; }
  1965. .done { color: green; }
  1966. </style>
  1967. or, if you want to keep the style in a file,
  1968. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  1969. As the value of this option simply gets inserted into the HTML <head> header,
  1970. you can \"misuse\" it to add arbitrary text to the header."
  1971. :group 'org-export-html
  1972. :type 'string)
  1973. (defcustom org-export-html-title-format "<h1 class=\"title\">%s</h1>\n"
  1974. "Format for typesetting the document title in HTML export."
  1975. :group 'org-export-html
  1976. :type 'string)
  1977. (defcustom org-export-html-toplevel-hlevel 2
  1978. "The <H> level for level 1 headings in HTML export."
  1979. :group 'org-export-html
  1980. :type 'string)
  1981. (defcustom org-export-html-link-org-files-as-html t
  1982. "Non-nil means, make file links to `file.org' point to `file.html'.
  1983. When org-mode is exporting an org-mode file to HTML, links to
  1984. non-html files are directly put into a href tag in HTML.
  1985. However, links to other Org-mode files (recognized by the
  1986. extension `.org.) should become links to the corresponding html
  1987. file, assuming that the linked org-mode file will also be
  1988. converted to HTML.
  1989. When nil, the links still point to the plain `.org' file."
  1990. :group 'org-export-html
  1991. :type 'boolean)
  1992. (defcustom org-export-html-inline-images 'maybe
  1993. "Non-nil means, inline images into exported HTML pages.
  1994. This is done using an <img> tag. When nil, an anchor with href is used to
  1995. link to the image. If this option is `maybe', then images in links with
  1996. an empty description will be inlined, while images with a description will
  1997. be linked only."
  1998. :group 'org-export-html
  1999. :type '(choice (const :tag "Never" nil)
  2000. (const :tag "Always" t)
  2001. (const :tag "When there is no description" maybe)))
  2002. (defcustom org-export-html-expand t
  2003. "Non-nil means, for HTML export, treat @<...> as HTML tag.
  2004. When nil, these tags will be exported as plain text and therefore
  2005. not be interpreted by a browser.
  2006. This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
  2007. :group 'org-export-html
  2008. :type 'boolean)
  2009. (defcustom org-export-html-table-tag
  2010. "<table border=\"1\" cellspacing=\"0\" cellpadding=\"6\">"
  2011. "The HTML tag used to start a table.
  2012. This must be a <table> tag, but you may change the options like
  2013. borders and spacing."
  2014. :group 'org-export-html
  2015. :type 'string)
  2016. (defcustom org-export-html-with-timestamp nil
  2017. "If non-nil, write `org-export-html-html-helper-timestamp'
  2018. into the exported HTML text. Otherwise, the buffer will just be saved
  2019. to a file."
  2020. :group 'org-export-html
  2021. :type 'boolean)
  2022. (defcustom org-export-html-html-helper-timestamp
  2023. "<br/><br/><hr><p><!-- hhmts start --> <!-- hhmts end --></p>\n"
  2024. "The HTML tag used as timestamp delimiter for HTML-helper-mode."
  2025. :group 'org-export-html
  2026. :type 'string)
  2027. (defcustom org-export-html-show-new-buffer nil
  2028. "Non-nil means, popup buffer containing the exported html text.
  2029. Otherwise, the buffer will just be saved to a file and stay hidden."
  2030. :group 'org-export-html
  2031. :type 'boolean)
  2032. (defgroup org-export-icalendar nil
  2033. "Options specific for iCalendar export of Org-mode files."
  2034. :tag "Org Export iCalendar"
  2035. :group 'org-export)
  2036. (defcustom org-combined-agenda-icalendar-file "~/org.ics"
  2037. "The file name for the iCalendar file covering all agenda files.
  2038. This file is created with the command \\[org-export-icalendar-all-agenda-files].
  2039. The file name should be absolute."
  2040. :group 'org-export-icalendar
  2041. :type 'file)
  2042. (defcustom org-icalendar-include-todo nil
  2043. "Non-nil means, export to iCalendar files should also cover TODO items."
  2044. :group 'org-export-icalendar
  2045. :type 'boolean)
  2046. (defcustom org-icalendar-combined-name "OrgMode"
  2047. "Calendar name for the combined iCalendar representing all agenda files."
  2048. :group 'org-export-icalendar
  2049. :type 'string)
  2050. (defgroup org-font-lock nil
  2051. "Font-lock settings for highlighting in Org-mode."
  2052. :tag "Org Font Lock"
  2053. :group 'org)
  2054. (defcustom org-level-color-stars-only nil
  2055. "Non-nil means fontify only the stars in each headline.
  2056. When nil, the entire headline is fontified.
  2057. Changing it requires restart of `font-lock-mode' to become effective
  2058. also in regions already fontified."
  2059. :group 'org-font-lock
  2060. :type 'boolean)
  2061. (defcustom org-hide-leading-stars nil
  2062. "Non-nil means, hide the first N-1 stars in a headline.
  2063. This works by using the face `org-hide' for these stars. This
  2064. face is white for a light background, and black for a dark
  2065. background. You may have to customize the face `org-hide' to
  2066. make this work.
  2067. Changing it requires restart of `font-lock-mode' to become effective
  2068. also in regions already fontified.
  2069. You may also set this on a per-file basis by adding one of the following
  2070. lines to the buffer:
  2071. #+STARTUP: hidestars
  2072. #+STARTUP: showstars"
  2073. :group 'org-font-lock
  2074. :type 'boolean)
  2075. (defcustom org-fontify-done-headline nil
  2076. "Non-nil means, change the face of a headline if it is marked DONE.
  2077. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2078. When this is non-nil, the headline after the keyword is set to the
  2079. `org-headline-done' as an additional indication."
  2080. :group 'org-font-lock
  2081. :type 'boolean)
  2082. (defcustom org-fontify-emphasized-text t
  2083. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2084. Changing this variable requires a restart of Emacs to take effect."
  2085. :group 'org-font-lock
  2086. :type 'boolean)
  2087. (defvar org-emph-re nil
  2088. "Regular expression for matching emphasis.")
  2089. (defvar org-emphasis-regexp-components) ; defined just below
  2090. (defvar org-emphasis-alist) ; defined just below
  2091. (defun org-set-emph-re (var val)
  2092. "Set variable and compute the emphasis regular expression."
  2093. (set var val)
  2094. (when (and (boundp 'org-emphasis-alist)
  2095. (boundp 'org-emphasis-regexp-components)
  2096. org-emphasis-alist org-emphasis-regexp-components)
  2097. (let* ((e org-emphasis-regexp-components)
  2098. (pre (car e))
  2099. (post (nth 1 e))
  2100. (border (nth 2 e))
  2101. (body (nth 3 e))
  2102. (nl (nth 4 e))
  2103. (stacked (nth 5 e))
  2104. (body1 (concat body "*?"))
  2105. (markers (mapconcat 'car org-emphasis-alist "")))
  2106. ;; make sure special characters appear at the right position in the class
  2107. (if (string-match "\\^" markers)
  2108. (setq markers (concat (replace-match "" t t markers) "^")))
  2109. (if (string-match "-" markers)
  2110. (setq markers (concat (replace-match "" t t markers) "-")))
  2111. (while (>= (setq nl (1- nl)) 0) (setq body1 (concat body1 "\n?" body "*?")))
  2112. ;; Make the regexp
  2113. (setq org-emph-re
  2114. (concat "\\([" pre (if stacked markers) "]\\|^\\)"
  2115. "\\("
  2116. "\\([" markers "]\\)"
  2117. "\\("
  2118. "[^" border markers "]"
  2119. body1
  2120. "[^" border markers "]"
  2121. "\\)"
  2122. "\\3\\)"
  2123. "\\([" post (if stacked markers) "]\\|$\\)")))))
  2124. (defcustom org-emphasis-regexp-components
  2125. '(" \t(" " \t.,?;'\")" " \t\r\n," "." 1 nil)
  2126. "Components used to build the reqular expression for emphasis.
  2127. This is a list with 6 entries. Terminology: In an emphasis string
  2128. like \" *strong word* \", we call the initial space PREMATCH, the final
  2129. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2130. and \"trong wor\" is the body. The different components in this variable
  2131. specify what is allowed/forbidden in each part:
  2132. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2133. post Chars allowed as postmatch. End of line will be allowed too.
  2134. border The chars *forbidden* as border characters. In addition to the
  2135. characters given here, all marker characters are forbidden too.
  2136. body-regexp A regexp like \".\" to match a body character. Don't use
  2137. non-shy groups here, and don't allow newline here.
  2138. newline The maximum number of newlines allowed in an emphasis exp.
  2139. stacked Non-nil means, allow stacked styles. This works only in HTML
  2140. export. When this is set, all marker characters (as given in
  2141. `org-emphasis-alist') will be allowed as pre/post, aiding
  2142. inside-out matching.
  2143. Use customize to modify this, or restart emacs after changing it."
  2144. :group 'org-font-lock
  2145. :set 'org-set-emph-re
  2146. :type '(list
  2147. (sexp :tag "Allowed chars in pre ")
  2148. (sexp :tag "Allowed chars in post ")
  2149. (sexp :tag "Forbidden chars in border ")
  2150. (sexp :tag "Regexp for body ")
  2151. (integer :tag "number of newlines allowed")
  2152. (boolean :tag "Stacking allowed ")))
  2153. (defcustom org-emphasis-alist
  2154. '(("*" bold "<b>" "</b>")
  2155. ("/" italic "<i>" "</i>")
  2156. ("_" underline "<u>" "</u>")
  2157. ("=" shadow "<code>" "</code>")
  2158. ("+" (:strike-through t) "<del>" "</del>")
  2159. )
  2160. "Special syntax for emphasised text.
  2161. Text starting and ending with a special character will be emphasized, for
  2162. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2163. characters, the face to bbe used by font-lock for highlighting in Org-mode
  2164. emacs buffers, and the HTML tags to be used for this.
  2165. Use customize to modify this, or restart emacs after changing it."
  2166. :group 'org-font-lock
  2167. :set 'org-set-emph-re
  2168. :type '(repeat
  2169. (list
  2170. (string :tag "Marker character")
  2171. (choice
  2172. (face :tag "Font-lock-face")
  2173. (plist :tag "Face property list"))
  2174. (string :tag "HTML start tag")
  2175. (string :tag "HTML end tag"))))
  2176. (defgroup org-faces nil
  2177. "Faces in Org-mode."
  2178. :tag "Org Faces"
  2179. :group 'org-font-lock)
  2180. (defun org-compatible-face (specs)
  2181. "Make a compatible face specification.
  2182. XEmacs and Emacs 21 do not know about the `min-colors' attribute.
  2183. For them we convert a (min-colors 8) entry to a `tty' entry and move it
  2184. to the top of the list. The `min-colors' attribute will be removed from
  2185. any other entries, and any resulting duplicates will be removed entirely."
  2186. (if (or (featurep 'xemacs) (< emacs-major-version 22))
  2187. (let (r e a)
  2188. (while (setq e (pop specs))
  2189. (cond
  2190. ((memq (car e) '(t default)) (push e r))
  2191. ((setq a (member '(min-colors 8) (car e)))
  2192. (nconc r (list (cons (cons '(type tty) (delq (car a) (car e)))
  2193. (cdr e)))))
  2194. ((setq a (assq 'min-colors (car e)))
  2195. (setq e (cons (delq a (car e)) (cdr e)))
  2196. (or (assoc (car e) r) (push e r)))
  2197. (t (or (assoc (car e) r) (push e r)))))
  2198. (nreverse r))
  2199. specs))
  2200. (defface org-hide
  2201. '((((background light)) (:foreground "white"))
  2202. (((background dark)) (:foreground "black")))
  2203. "Face used to hide leading stars in headlines.
  2204. The forground color of this face should be equal to the background
  2205. color of the frame."
  2206. :group 'org-faces)
  2207. (defface org-level-1 ;; font-lock-function-name-face
  2208. (org-compatible-face
  2209. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2210. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2211. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2212. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2213. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  2214. (t (:bold t))))
  2215. "Face used for level 1 headlines."
  2216. :group 'org-faces)
  2217. (defface org-level-2 ;; font-lock-variable-name-face
  2218. (org-compatible-face
  2219. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  2220. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  2221. (((class color) (min-colors 8) (background light)) (:foreground "yellow"))
  2222. (((class color) (min-colors 8) (background dark)) (:foreground "yellow" :bold t))
  2223. (t (:bold t))))
  2224. "Face used for level 2 headlines."
  2225. :group 'org-faces)
  2226. (defface org-level-3 ;; font-lock-keyword-face
  2227. (org-compatible-face
  2228. '((((class color) (min-colors 88) (background light)) (:foreground "Purple"))
  2229. (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
  2230. (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
  2231. (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
  2232. (((class color) (min-colors 8) (background light)) (:foreground "purple" :bold t))
  2233. (((class color) (min-colors 8) (background dark)) (:foreground "cyan" :bold t))
  2234. (t (:bold t))))
  2235. "Face used for level 3 headlines."
  2236. :group 'org-faces)
  2237. (defface org-level-4 ;; font-lock-comment-face
  2238. (org-compatible-face
  2239. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2240. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2241. (((class color) (min-colors 16) (background light)) (:foreground "red"))
  2242. (((class color) (min-colors 16) (background dark)) (:foreground "red1"))
  2243. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2244. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2245. (t (:bold t))))
  2246. "Face used for level 4 headlines."
  2247. :group 'org-faces)
  2248. (defface org-level-5 ;; font-lock-type-face
  2249. (org-compatible-face
  2250. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
  2251. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
  2252. (((class color) (min-colors 8)) (:foreground "green"))))
  2253. "Face used for level 5 headlines."
  2254. :group 'org-faces)
  2255. (defface org-level-6 ;; font-lock-constant-face
  2256. (org-compatible-face
  2257. '((((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
  2258. (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
  2259. (((class color) (min-colors 8)) (:foreground "magenta"))))
  2260. "Face used for level 6 headlines."
  2261. :group 'org-faces)
  2262. (defface org-level-7 ;; font-lock-builtin-face
  2263. (org-compatible-face
  2264. '((((class color) (min-colors 16) (background light)) (:foreground "Orchid"))
  2265. (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue"))
  2266. (((class color) (min-colors 8)) (:foreground "blue"))))
  2267. "Face used for level 7 headlines."
  2268. :group 'org-faces)
  2269. (defface org-level-8 ;; font-lock-string-face
  2270. (org-compatible-face
  2271. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2272. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2273. (((class color) (min-colors 8)) (:foreground "green"))))
  2274. "Face used for level 8 headlines."
  2275. :group 'org-faces)
  2276. (defface org-special-keyword ;; font-lock-string-face
  2277. (org-compatible-face
  2278. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2279. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2280. (t (:italic t))))
  2281. "Face used for special keywords."
  2282. :group 'org-faces)
  2283. (defface org-warning ;; font-lock-warning-face
  2284. (org-compatible-face
  2285. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  2286. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  2287. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2288. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2289. (t (:bold t))))
  2290. "Face for deadlines and TODO keywords."
  2291. :group 'org-faces)
  2292. (defface org-headline-done ;; font-lock-string-face
  2293. (org-compatible-face
  2294. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2295. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2296. (((class color) (min-colors 8) (background light)) (:bold nil))))
  2297. "Face used to indicate that a headline is DONE.
  2298. This face is only used if `org-fontify-done-headline' is set."
  2299. :group 'org-faces)
  2300. (defface org-archived ; similar to shadow
  2301. (org-compatible-face
  2302. '((((class color grayscale) (min-colors 88) (background light))
  2303. (:foreground "grey50"))
  2304. (((class color grayscale) (min-colors 88) (background dark))
  2305. (:foreground "grey70"))
  2306. (((class color) (min-colors 8) (background light))
  2307. (:foreground "green"))
  2308. (((class color) (min-colors 8) (background dark))
  2309. (:foreground "yellow"))))
  2310. "Face for headline with the ARCHIVE tag."
  2311. :group 'org-faces)
  2312. (defface org-link
  2313. '((((class color) (background light)) (:foreground "Purple" :underline t))
  2314. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  2315. (t (:underline t)))
  2316. "Face for links."
  2317. :group 'org-faces)
  2318. (defface org-date
  2319. '((((class color) (background light)) (:foreground "Purple" :underline t))
  2320. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  2321. (t (:underline t)))
  2322. "Face for links."
  2323. :group 'org-faces)
  2324. (defface org-tag
  2325. '((t (:bold t)))
  2326. "Face for tags."
  2327. :group 'org-faces)
  2328. (defface org-todo ;; font-lock-warning-face
  2329. (org-compatible-face
  2330. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  2331. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  2332. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2333. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2334. (t (:inverse-video t :bold t))))
  2335. "Face for TODO keywords."
  2336. :group 'org-faces)
  2337. (defface org-done ;; font-lock-type-face
  2338. (org-compatible-face
  2339. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen" :bold t))
  2340. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen" :bold t))
  2341. (((class color) (min-colors 8)) (:foreground "green"))
  2342. (t (:bold t))))
  2343. "Face used for DONE."
  2344. :group 'org-faces)
  2345. (defface org-table ;; font-lock-function-name-face
  2346. (org-compatible-face
  2347. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2348. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2349. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2350. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2351. (((class color) (min-colors 8) (background light)) (:foreground "blue"))
  2352. (((class color) (min-colors 8) (background dark)))))
  2353. "Face used for tables."
  2354. :group 'org-faces)
  2355. (defface org-formula
  2356. (org-compatible-face
  2357. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2358. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2359. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2360. (((class color) (min-colors 8) (background dark)) (:foreground "red"))
  2361. (t (:bold t :italic t))))
  2362. "Face for formulas."
  2363. :group 'org-faces)
  2364. (defface org-scheduled-today
  2365. (org-compatible-face
  2366. '((((class color) (min-colors 88) (background light)) (:foreground "DarkGreen"))
  2367. (((class color) (min-colors 88) (background dark)) (:foreground "PaleGreen"))
  2368. (((class color) (min-colors 8)) (:foreground "green"))
  2369. (t (:bold t :italic t))))
  2370. "Face for items scheduled for a certain day."
  2371. :group 'org-faces)
  2372. (defface org-scheduled-previously
  2373. (org-compatible-face
  2374. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2375. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2376. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2377. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2378. (t (:bold t))))
  2379. "Face for items scheduled previously, and not yet done."
  2380. :group 'org-faces)
  2381. (defface org-upcoming-deadline
  2382. (org-compatible-face
  2383. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2384. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2385. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2386. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2387. (t (:bold t))))
  2388. "Face for items scheduled previously, and not yet done."
  2389. :group 'org-faces)
  2390. (defface org-time-grid ;; font-lock-variable-name-face
  2391. (org-compatible-face
  2392. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  2393. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  2394. (((class color) (min-colors 8)) (:foreground "yellow" :weight light))))
  2395. "Face used for time grids."
  2396. :group 'org-faces)
  2397. (defconst org-level-faces
  2398. '(org-level-1 org-level-2 org-level-3 org-level-4
  2399. org-level-5 org-level-6 org-level-7 org-level-8
  2400. ))
  2401. (defconst org-n-levels (length org-level-faces))
  2402. ;; Variables for pre-computed regular expressions, all buffer local
  2403. (defvar org-done-string nil
  2404. "The last string in `org-todo-keywords', indicating an item is DONE.")
  2405. (make-variable-buffer-local 'org-done-string)
  2406. (defvar org-todo-regexp nil
  2407. "Matches any of the TODO state keywords.")
  2408. (make-variable-buffer-local 'org-todo-regexp)
  2409. (defvar org-not-done-regexp nil
  2410. "Matches any of the TODO state keywords except the last one.")
  2411. (make-variable-buffer-local 'org-not-done-regexp)
  2412. (defvar org-todo-line-regexp nil
  2413. "Matches a headline and puts TODO state into group 2 if present.")
  2414. (make-variable-buffer-local 'org-todo-line-regexp)
  2415. (defvar org-todo-line-tags-regexp nil
  2416. "Matches a headline and puts TODO state into group 2 if present.
  2417. Also put tags into group 4 if tags are present.")
  2418. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2419. (defvar org-nl-done-regexp nil
  2420. "Matches newline followed by a headline with the DONE keyword.")
  2421. (make-variable-buffer-local 'org-nl-done-regexp)
  2422. (defvar org-looking-at-done-regexp nil
  2423. "Matches the DONE keyword a point.")
  2424. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2425. (defvar org-todo-kwd-priority-p nil
  2426. "Do TODO items have priorities?")
  2427. (make-variable-buffer-local 'org-todo-kwd-priority-p)
  2428. (defvar org-todo-kwd-max-priority nil
  2429. "Maximum priority of TODO items.")
  2430. (make-variable-buffer-local 'org-todo-kwd-max-priority)
  2431. (defvar org-ds-keyword-length 12
  2432. "Maximum length of the Deadline and SCHEDULED keywords.")
  2433. (make-variable-buffer-local 'org-ds-keyword-length)
  2434. (defvar org-deadline-regexp nil
  2435. "Matches the DEADLINE keyword.")
  2436. (make-variable-buffer-local 'org-deadline-regexp)
  2437. (defvar org-deadline-time-regexp nil
  2438. "Matches the DEADLINE keyword together with a time stamp.")
  2439. (make-variable-buffer-local 'org-deadline-time-regexp)
  2440. (defvar org-deadline-line-regexp nil
  2441. "Matches the DEADLINE keyword and the rest of the line.")
  2442. (make-variable-buffer-local 'org-deadline-line-regexp)
  2443. (defvar org-scheduled-regexp nil
  2444. "Matches the SCHEDULED keyword.")
  2445. (make-variable-buffer-local 'org-scheduled-regexp)
  2446. (defvar org-scheduled-time-regexp nil
  2447. "Matches the SCHEDULED keyword together with a time stamp.")
  2448. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2449. (defvar org-closed-time-regexp nil
  2450. "Matches the CLOSED keyword together with a time stamp.")
  2451. (make-variable-buffer-local 'org-closed-time-regexp)
  2452. (defvar org-keyword-time-regexp nil
  2453. "Matches any of the 3 keywords, together with the time stamp.")
  2454. (make-variable-buffer-local 'org-keyword-time-regexp)
  2455. (defvar org-maybe-keyword-time-regexp nil
  2456. "Matches a timestamp, possibly preceeded by a keyword.")
  2457. (make-variable-buffer-local 'org-keyword-time-regexp)
  2458. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  2459. mouse-map t)
  2460. "Properties to remove when a string without properties is wanted.")
  2461. (defsubst org-match-string-no-properties (num &optional string)
  2462. (if (featurep 'xemacs)
  2463. (let ((s (match-string num string)))
  2464. (remove-text-properties 0 (length s) org-rm-props s)
  2465. s)
  2466. (match-string-no-properties num string)))
  2467. (defsubst org-no-properties (s)
  2468. (remove-text-properties 0 (length s) org-rm-props s)
  2469. s)
  2470. (defsubst org-get-alist-option (option key)
  2471. (cond ((eq key t) t)
  2472. ((eq option t) t)
  2473. ((assoc key option) (cdr (assoc key option)))
  2474. (t (cdr (assq 'default option)))))
  2475. (defsubst org-set-local (var value)
  2476. "Make VAR local in current buffer and set it to VALUE."
  2477. (set (make-variable-buffer-local var) value))
  2478. (defsubst org-mode-p ()
  2479. "Check if the current buffer is in Org-mode."
  2480. (eq major-mode 'org-mode))
  2481. (defsubst org-last (list)
  2482. "Return the last element of LIST."
  2483. (car (last list)))
  2484. (defun org-let (list &rest body)
  2485. (eval (cons 'let (cons list body))))
  2486. (put 'org-let 'lisp-indent-function 1)
  2487. (defun org-let2 (list1 list2 &rest body)
  2488. (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
  2489. (put 'org-let2 'lisp-indent-function 2)
  2490. (defconst org-startup-options
  2491. '(("fold" org-startup-folded t)
  2492. ("overview" org-startup-folded t)
  2493. ("nofold" org-startup-folded nil)
  2494. ("showall" org-startup-folded nil)
  2495. ("content" org-startup-folded content)
  2496. ("hidestars" org-hide-leading-stars t)
  2497. ("showstars" org-hide-leading-stars nil)
  2498. ("odd" org-odd-levels-only t)
  2499. ("oddeven" org-odd-levels-only nil)
  2500. ("align" org-startup-align-all-tables t)
  2501. ("noalign" org-startup-align-all-tables nil)
  2502. ("logging" org-log-done t)
  2503. ("nologging" org-log-done nil)
  2504. ("dlcheck" org-startup-with-deadline-check t)
  2505. ("nodlcheck" org-startup-with-deadline-check nil)))
  2506. (defun org-set-regexps-and-options ()
  2507. "Precompute regular expressions for current buffer."
  2508. (when (org-mode-p)
  2509. (let ((re (org-make-options-regexp
  2510. '("CATEGORY" "SEQ_TODO" "PRI_TODO" "TYP_TODO"
  2511. "STARTUP" "ARCHIVE" "TAGS" "CALC")))
  2512. (splitre "[ \t]+")
  2513. kwds int key value cat arch tags)
  2514. (save-excursion
  2515. (save-restriction
  2516. (widen)
  2517. (goto-char (point-min))
  2518. (while (re-search-forward re nil t)
  2519. (setq key (match-string 1) value (org-match-string-no-properties 2))
  2520. (cond
  2521. ((equal key "CATEGORY")
  2522. (if (string-match "[ \t]+$" value)
  2523. (setq value (replace-match "" t t value)))
  2524. (setq cat (intern value)))
  2525. ((equal key "SEQ_TODO")
  2526. (setq int 'sequence
  2527. kwds (append kwds (org-split-string value splitre))))
  2528. ((equal key "PRI_TODO")
  2529. (setq int 'priority
  2530. kwds (append kwds (org-split-string value splitre))))
  2531. ((equal key "TYP_TODO")
  2532. (setq int 'type
  2533. kwds (append kwds (org-split-string value splitre))))
  2534. ((equal key "TAGS")
  2535. (setq tags (append tags (org-split-string value splitre))))
  2536. ((equal key "STARTUP")
  2537. (let ((opts (org-split-string value splitre))
  2538. l var val)
  2539. (while (setq l (assoc (pop opts) org-startup-options))
  2540. (setq var (nth 1 l) val (nth 2 l))
  2541. (set (make-local-variable var) val))))
  2542. ((equal key "ARCHIVE")
  2543. (string-match " *$" value)
  2544. (setq arch (replace-match "" t t value))
  2545. (remove-text-properties 0 (length arch)
  2546. '(face t fontified t) arch)))
  2547. )))
  2548. (and cat (org-set-local 'org-category cat))
  2549. (and kwds (org-set-local 'org-todo-keywords kwds))
  2550. (and arch (org-set-local 'org-archive-location arch))
  2551. (and int (org-set-local 'org-todo-interpretation int))
  2552. (when tags
  2553. (let (e tgs)
  2554. (while (setq e (pop tags))
  2555. (cond
  2556. ((equal e "{") (push '(:startgroup) tgs))
  2557. ((equal e "}") (push '(:endgroup) tgs))
  2558. ((string-match "^\\([0-9a-zA-Z_@]+\\)(\\(.\\))$" e)
  2559. (push (cons (match-string 1 e)
  2560. (string-to-char (match-string 2 e)))
  2561. tgs))
  2562. (t (push (list e) tgs))))
  2563. (org-set-local 'org-tag-alist nil)
  2564. (while (setq e (pop tgs))
  2565. (or (and (stringp (car e))
  2566. (assoc (car e) org-tag-alist))
  2567. (push e org-tag-alist))))))
  2568. ;; Compute the regular expressions and other local variables
  2569. (setq org-todo-kwd-priority-p (equal org-todo-interpretation 'priority)
  2570. org-todo-kwd-max-priority (1- (length org-todo-keywords))
  2571. org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2572. (length org-scheduled-string)))
  2573. org-done-string
  2574. (nth (1- (length org-todo-keywords)) org-todo-keywords)
  2575. org-todo-regexp
  2576. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords
  2577. "\\|") "\\)\\>")
  2578. org-not-done-regexp
  2579. (concat "\\<\\("
  2580. (mapconcat 'regexp-quote
  2581. (nreverse (cdr (reverse org-todo-keywords)))
  2582. "\\|")
  2583. "\\)\\>")
  2584. org-todo-line-regexp
  2585. (concat "^\\(\\*+\\)[ \t]*\\(?:\\("
  2586. (mapconcat 'regexp-quote org-todo-keywords "\\|")
  2587. "\\)\\>\\)? *\\(.*\\)")
  2588. org-nl-done-regexp
  2589. (concat "[\r\n]\\*+[ \t]+" org-done-string "\\>")
  2590. org-todo-line-tags-regexp
  2591. (concat "^\\(\\*+\\)[ \t]*\\(?:\\("
  2592. (mapconcat 'regexp-quote org-todo-keywords "\\|")
  2593. "\\)\\>\\)? *\\(.*?\\([ \t]:[a-zA-Z0-9:_@]+:[ \t]*\\)?$\\)")
  2594. org-looking-at-done-regexp (concat "^" org-done-string "\\>")
  2595. org-deadline-regexp (concat "\\<" org-deadline-string)
  2596. org-deadline-time-regexp
  2597. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2598. org-deadline-line-regexp
  2599. (concat "\\<\\(" org-deadline-string "\\).*")
  2600. org-scheduled-regexp
  2601. (concat "\\<" org-scheduled-string)
  2602. org-scheduled-time-regexp
  2603. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2604. org-closed-time-regexp
  2605. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2606. org-keyword-time-regexp
  2607. (concat "\\<\\(" org-scheduled-string
  2608. "\\|" org-deadline-string
  2609. "\\|" org-closed-string
  2610. "\\|" org-clock-string "\\)"
  2611. " *[[<]\\([^]>]+\\)[]>]")
  2612. org-maybe-keyword-time-regexp
  2613. (concat "\\(\\<\\(" org-scheduled-string
  2614. "\\|" org-deadline-string
  2615. "\\|" org-closed-string
  2616. "\\|" org-clock-string "\\)\\)?"
  2617. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}[^]\r\n>]*?[]>]\\)"))
  2618. (org-set-font-lock-defaults)))
  2619. ;; Tell the compiler about dynamically scoped variables,
  2620. ;; and variables from other packages
  2621. (defvar calc-embedded-close-formula) ; defined by the calc package
  2622. (defvar calc-embedded-open-formula) ; defined by the calc package
  2623. (defvar font-lock-unfontify-region-function) ; defined by font-lock.el
  2624. (defvar zmacs-regions) ; XEmacs regions
  2625. (defvar original-date) ; dynamically scoped in calendar
  2626. (defvar org-old-auto-fill-inhibit-regexp) ; local variable used by `orgtbl-mode'
  2627. (defvar orgtbl-mode-menu) ; defined when orgtbl mode get initialized
  2628. (defvar org-html-entities) ; defined later in this file
  2629. (defvar org-goto-start-pos) ; dynamically scoped parameter
  2630. (defvar org-time-was-given) ; dynamically scoped parameter
  2631. (defvar org-ts-what) ; dynamically scoped parameter
  2632. (defvar org-current-export-file) ; dynamically scoped parameter
  2633. (defvar org-current-export-dir) ; dynamically scoped parameter
  2634. (defvar mark-active) ; Emacs only, not available in XEmacs.
  2635. (defvar timecnt) ; dynamically scoped parameter
  2636. (defvar levels-open) ; dynamically scoped parameter
  2637. (defvar entry) ; dynamically scoped parameter
  2638. (defvar state) ; dynamically scoped into `org-after-todo-state-change-hook'
  2639. (defvar date) ; dynamically scoped parameter
  2640. (defvar description) ; dynamically scoped parameter
  2641. (defvar ans1) ; dynamically scoped parameter
  2642. (defvar ans2) ; dynamically scoped parameter
  2643. (defvar starting-day) ; local variable
  2644. (defvar include-all-loc) ; local variable
  2645. (defvar vm-message-pointer) ; from vm
  2646. (defvar vm-folder-directory) ; from vm
  2647. (defvar gnus-other-frame-object) ; from gnus
  2648. (defvar wl-summary-buffer-elmo-folder) ; from wanderlust
  2649. (defvar wl-summary-buffer-folder-name) ; from wanderlust
  2650. (defvar gnus-group-name) ; from gnus
  2651. (defvar gnus-article-current) ; from gnus
  2652. (defvar w3m-current-url) ; from w3m
  2653. (defvar w3m-current-title) ; from w3m
  2654. (defvar mh-progs) ; from MH-E
  2655. (defvar mh-current-folder) ; from MH-E
  2656. (defvar mh-show-folder-buffer) ; from MH-E
  2657. (defvar mh-index-folder) ; from MH-E
  2658. (defvar mh-searcher) ; from MH-E
  2659. (defvar org-selected-point) ; dynamically scoped parameter
  2660. (defvar calendar-mode-map) ; from calendar.el
  2661. (defvar last-arg) ; local variable
  2662. (defvar remember-save-after-remembering) ; from remember.el
  2663. (defvar remember-data-file) ; from remember.el
  2664. (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
  2665. (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
  2666. (defvar orgtbl-mode) ; defined later in this file
  2667. (defvar Info-current-file) ; from info.el
  2668. (defvar Info-current-node) ; from info.el
  2669. (defvar texmathp-why) ; from texmathp.el
  2670. (defvar org-latex-regexps)
  2671. ;;; Define the mode
  2672. (defvar org-mode-map
  2673. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  2674. (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.")
  2675. (copy-keymap outline-mode-map))
  2676. "Keymap for Org-mode.")
  2677. (defvar org-struct-menu) ; defined later in this file
  2678. (defvar org-org-menu) ; defined later in this file
  2679. (defvar org-tbl-menu) ; defined later in this file
  2680. ;; We use a before-change function to check if a table might need
  2681. ;; an update.
  2682. (defvar org-table-may-need-update t
  2683. "Indicates that a table might need an update.
  2684. This variable is set by `org-before-change-function'.
  2685. `org-table-align' sets it back to nil.")
  2686. (defvar org-mode-hook nil)
  2687. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  2688. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  2689. ;;;###autoload
  2690. (define-derived-mode org-mode outline-mode "Org"
  2691. "Outline-based notes management and organizer, alias
  2692. \"Carsten's outline-mode for keeping track of everything.\"
  2693. Org-mode develops organizational tasks around a NOTES file which
  2694. contains information about projects as plain text. Org-mode is
  2695. implemented on top of outline-mode, which is ideal to keep the content
  2696. of large files well structured. It supports ToDo items, deadlines and
  2697. time stamps, which magically appear in the diary listing of the Emacs
  2698. calendar. Tables are easily created with a built-in table editor.
  2699. Plain text URL-like links connect to websites, emails (VM), Usenet
  2700. messages (Gnus), BBDB entries, and any files related to the project.
  2701. For printing and sharing of notes, an Org-mode file (or a part of it)
  2702. can be exported as a structured ASCII or HTML file.
  2703. The following commands are available:
  2704. \\{org-mode-map}"
  2705. ;; Get rid of Outline menus, they are not needed
  2706. ;; Need to do this here because define-derived-mode sets up
  2707. ;; the keymap so late.
  2708. (if (featurep 'xemacs)
  2709. (progn
  2710. ;; Assume this is Greg's port, it used easymenu
  2711. (easy-menu-remove outline-mode-menu-heading)
  2712. (easy-menu-remove outline-mode-menu-show)
  2713. (easy-menu-remove outline-mode-menu-hide))
  2714. (define-key org-mode-map [menu-bar headings] 'undefined)
  2715. (define-key org-mode-map [menu-bar hide] 'undefined)
  2716. (define-key org-mode-map [menu-bar show] 'undefined))
  2717. (easy-menu-add org-org-menu)
  2718. (easy-menu-add org-tbl-menu)
  2719. (org-install-agenda-files-menu)
  2720. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  2721. (org-add-to-invisibility-spec '(org-cwidth))
  2722. (when (featurep 'xemacs)
  2723. (org-set-local 'line-move-ignore-invisible t))
  2724. (setq outline-regexp "\\*+")
  2725. ;;(setq outline-regexp "\\(?:\\*+\\|[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\) \\)")
  2726. (setq outline-level 'org-outline-level)
  2727. (when (and org-ellipsis (stringp org-ellipsis))
  2728. (unless org-display-table
  2729. (setq org-display-table (make-display-table)))
  2730. (set-display-table-slot org-display-table
  2731. 4 (string-to-vector org-ellipsis))
  2732. (setq buffer-display-table org-display-table))
  2733. (org-set-regexps-and-options)
  2734. ;; Calc embedded
  2735. (org-set-local 'calc-embedded-open-mode "# ")
  2736. (modify-syntax-entry ?# "<")
  2737. (if org-startup-truncated (setq truncate-lines t))
  2738. (org-set-local 'font-lock-unfontify-region-function
  2739. 'org-unfontify-region)
  2740. ;; Activate before-change-function
  2741. (org-set-local 'org-table-may-need-update t)
  2742. (org-add-hook 'before-change-functions 'org-before-change-function nil
  2743. 'local)
  2744. ;; Check for running clock before killing a buffer
  2745. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  2746. ;; Paragraphs and auto-filling
  2747. (org-set-autofill-regexps)
  2748. (org-update-radio-target-regexp)
  2749. (if (and org-insert-mode-line-in-empty-file
  2750. (interactive-p)
  2751. (= (point-min) (point-max)))
  2752. (insert " -*- mode: org -*-\n\n"))
  2753. (unless org-inhibit-startup
  2754. (when org-startup-align-all-tables
  2755. (let ((bmp (buffer-modified-p)))
  2756. (org-table-map-tables 'org-table-align)
  2757. (set-buffer-modified-p bmp)))
  2758. (if org-startup-with-deadline-check
  2759. (call-interactively 'org-check-deadlines)
  2760. (cond
  2761. ((eq org-startup-folded t)
  2762. (org-cycle '(4)))
  2763. ((eq org-startup-folded 'content)
  2764. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  2765. (org-cycle '(4)) (org-cycle '(4))))))))
  2766. (defsubst org-call-with-arg (command arg)
  2767. "Call COMMAND interactively, but pretend prefix are was ARG."
  2768. (let ((current-prefix-arg arg)) (call-interactively command)))
  2769. (defsubst org-current-line (&optional pos)
  2770. (+ (if (bolp) 1 0) (count-lines (point-min) (or pos (point)))))
  2771. (defun org-current-time ()
  2772. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  2773. (if (> org-time-stamp-rounding-minutes 0)
  2774. (let ((r org-time-stamp-rounding-minutes)
  2775. (time (decode-time)))
  2776. (apply 'encode-time
  2777. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  2778. (nthcdr 2 time))))
  2779. (current-time)))
  2780. (defun org-add-props (string plist &rest props)
  2781. "Add text properties to entire string, from beginning to end.
  2782. PLIST may be a list of properties, PROPS are individual properties and values
  2783. that will be added to PLIST. Returns the string that was modified."
  2784. (add-text-properties
  2785. 0 (length string) (if props (append plist props) plist) string)
  2786. string)
  2787. (put 'org-add-props 'lisp-indent-function 2)
  2788. ;;; Font-Lock stuff
  2789. (defvar org-mouse-map (make-sparse-keymap))
  2790. (define-key org-mouse-map
  2791. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  2792. (define-key org-mouse-map
  2793. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  2794. (when org-mouse-1-follows-link
  2795. (define-key org-mouse-map [follow-link] 'mouse-face))
  2796. (when org-tab-follows-link
  2797. (define-key org-mouse-map [(tab)] 'org-open-at-point)
  2798. (define-key org-mouse-map "\C-i" 'org-open-at-point))
  2799. (when org-return-follows-link
  2800. (define-key org-mouse-map [(return)] 'org-open-at-point)
  2801. (define-key org-mouse-map "\C-m" 'org-open-at-point))
  2802. (require 'font-lock)
  2803. (defconst org-non-link-chars "]\t\n\r<>")
  2804. (defconst org-link-types '("https?" "ftp" "mailto" "file" "news" "bbdb" "vm"
  2805. "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  2806. (defconst org-link-re-with-space
  2807. (concat
  2808. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2809. "\\([^" org-non-link-chars " ]"
  2810. "[^" org-non-link-chars "]*"
  2811. "[^" org-non-link-chars " ]\\)>?")
  2812. "Matches a link with spaces, optional angular brackets around it.")
  2813. (defconst org-link-re-with-space2
  2814. (concat
  2815. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2816. "\\([^" org-non-link-chars " ]"
  2817. "[^]\t\n\r]*"
  2818. "[^" org-non-link-chars " ]\\)>?")
  2819. "Matches a link with spaces, optional angular brackets around it.")
  2820. (defconst org-angle-link-re
  2821. (concat
  2822. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2823. "\\([^" org-non-link-chars " ]"
  2824. "[^" org-non-link-chars "]*"
  2825. "\\)>")
  2826. "Matches link with angular brackets, spaces are allowed.")
  2827. (defconst org-plain-link-re
  2828. (concat
  2829. "\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2830. "\\([^]\t\n\r<>,;() ]+\\)")
  2831. "Matches plain link, without spaces.")
  2832. (defconst org-bracket-link-regexp
  2833. "\\[\\[\\([^]]+\\)\\]\\(\\[\\([^]]+\\)\\]\\)?\\]"
  2834. "Matches a link in double brackets.")
  2835. (defconst org-bracket-link-analytic-regexp
  2836. (concat
  2837. "\\[\\["
  2838. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  2839. "\\([^]]+\\)"
  2840. "\\]"
  2841. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  2842. "\\]"))
  2843. ; 1: http:
  2844. ; 2: http
  2845. ; 3: path
  2846. ; 4: [desc]
  2847. ; 5: desc
  2848. (defconst org-ts-lengths
  2849. (cons (length (format-time-string (car org-time-stamp-formats)))
  2850. (length (format-time-string (cdr org-time-stamp-formats))))
  2851. "This holds the lengths of the two different time formats.")
  2852. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}[^\r\n>]*?\\)>"
  2853. "Regular expression for fast time stamp matching.")
  2854. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}[^\r\n>]*?\\)[]>]"
  2855. "Regular expression for fast time stamp matching.")
  2856. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  2857. "Regular expression matching time strings for analysis.")
  2858. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 ">")
  2859. "Regular expression matching time stamps, with groups.")
  2860. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[]>]")
  2861. "Regular expression matching time stamps (also [..]), with groups.")
  2862. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  2863. "Regular expression matching a time stamp range.")
  2864. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  2865. org-ts-regexp "\\)?")
  2866. "Regular expression matching a time stamp or time stamp range.")
  2867. (defvar org-�§emph-face nil)
  2868. (defun org-do-emphasis-faces (limit)
  2869. "Run through the buffer and add overlays to links."
  2870. (if (re-search-forward org-emph-re limit t)
  2871. (progn
  2872. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  2873. 'face
  2874. (nth 1 (assoc (match-string 3)
  2875. org-emphasis-alist)))
  2876. (add-text-properties (match-beginning 2) (match-end 2)
  2877. '(font-lock-multiline t))
  2878. (backward-char 1)
  2879. t)))
  2880. (defun org-activate-plain-links (limit)
  2881. "Run through the buffer and add overlays to links."
  2882. (if (re-search-forward org-plain-link-re limit t)
  2883. (progn
  2884. (add-text-properties (match-beginning 0) (match-end 0)
  2885. (list 'mouse-face 'highlight
  2886. 'keymap org-mouse-map
  2887. ))
  2888. t)))
  2889. (defun org-activate-angle-links (limit)
  2890. "Run through the buffer and add overlays to links."
  2891. (if (re-search-forward org-angle-link-re limit t)
  2892. (progn
  2893. (add-text-properties (match-beginning 0) (match-end 0)
  2894. (list 'mouse-face 'highlight
  2895. 'keymap org-mouse-map
  2896. ))
  2897. t)))
  2898. (defun org-activate-bracket-links (limit)
  2899. "Run through the buffer and add overlays to bracketed links."
  2900. (if (re-search-forward org-bracket-link-regexp limit t)
  2901. (let* ((help (concat "LINK: "
  2902. (org-match-string-no-properties 1)))
  2903. ;; FIXME: above we should remove the escapes.
  2904. ;; but that requires another match, protecting match data,
  2905. ;; a lot of overhead for font-lock.
  2906. (ip (list 'invisible 'org-link 'intangible t 'rear-nonsticky t
  2907. 'keymap org-mouse-map 'mouse-face 'highlight
  2908. 'help-echo help))
  2909. (vp (list 'rear-nonsticky t
  2910. 'keymap org-mouse-map 'mouse-face 'highlight
  2911. 'help-echo help)))
  2912. ;; We need to remove the invisible property here. Table narrowing
  2913. ;; may have made some of this invisible.
  2914. (remove-text-properties (match-beginning 0) (match-end 0)
  2915. '(invisible nil))
  2916. (if (match-end 3)
  2917. (progn
  2918. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  2919. (add-text-properties (match-beginning 3) (match-end 3) vp)
  2920. (add-text-properties (match-end 3) (match-end 0) ip))
  2921. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  2922. (add-text-properties (match-beginning 1) (match-end 1) vp)
  2923. (add-text-properties (match-end 1) (match-end 0) ip))
  2924. t)))
  2925. (defun org-activate-dates (limit)
  2926. "Run through the buffer and add overlays to dates."
  2927. (if (re-search-forward org-tsr-regexp limit t)
  2928. (progn
  2929. (add-text-properties (match-beginning 0) (match-end 0)
  2930. (list 'mouse-face 'highlight
  2931. 'keymap org-mouse-map))
  2932. t)))
  2933. (defvar org-target-link-regexp nil
  2934. "Regular expression matching radio targets in plain text.")
  2935. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  2936. "Regular expression matching a link target.")
  2937. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  2938. "Regular expression matching a link target.")
  2939. (defun org-activate-target-links (limit)
  2940. "Run through the buffer and add overlays to target matches."
  2941. (when org-target-link-regexp
  2942. (let ((case-fold-search t))
  2943. (if (re-search-forward org-target-link-regexp limit t)
  2944. (progn
  2945. (add-text-properties (match-beginning 0) (match-end 0)
  2946. (list 'mouse-face 'highlight
  2947. 'keymap org-mouse-map
  2948. 'help-echo "Radio target link"
  2949. 'org-linked-text t))
  2950. t)))))
  2951. (defun org-update-radio-target-regexp ()
  2952. "Find all radio targets in this file and update the regular expression."
  2953. (interactive)
  2954. (when (memq 'radio org-activate-links)
  2955. (setq org-target-link-regexp
  2956. (org-make-target-link-regexp (org-all-targets 'radio)))
  2957. (org-restart-font-lock)))
  2958. (defun org-hide-wide-columns (limit)
  2959. (let (s e)
  2960. (setq s (text-property-any (point) (or limit (point-max))
  2961. 'org-cwidth t))
  2962. (when s
  2963. (setq e (next-single-property-change s 'org-cwidth))
  2964. (add-text-properties s e '(invisible org-cwidth intangible t))
  2965. (goto-char e)
  2966. t)))
  2967. (defun org-restart-font-lock ()
  2968. "Restart font-lock-mode, to force refontification."
  2969. (when (and (boundp 'font-lock-mode) font-lock-mode)
  2970. (font-lock-mode -1)
  2971. (font-lock-mode 1)))
  2972. (defun org-all-targets (&optional radio)
  2973. "Return a list of all targets in this file.
  2974. With optional argument RADIO, only find radio targets."
  2975. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  2976. rtn)
  2977. (save-excursion
  2978. (goto-char (point-min))
  2979. (while (re-search-forward re nil t)
  2980. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  2981. rtn)))
  2982. (defun org-make-target-link-regexp (targets)
  2983. "Make regular expression matching all strings in TARGETS.
  2984. The regular expression finds the targets also if there is a line break
  2985. between words."
  2986. (and targets
  2987. (concat
  2988. "\\<\\("
  2989. (mapconcat
  2990. (lambda (x)
  2991. (while (string-match " +" x)
  2992. (setq x (replace-match "\\s-+" t t x)))
  2993. x)
  2994. targets
  2995. "\\|")
  2996. "\\)\\>")))
  2997. (defvar org-camel-regexp "\\*?\\<[A-Z]+[a-z]+[A-Z][a-zA-Z]*\\>"
  2998. "Matches CamelCase words, possibly with a star before it.")
  2999. (defun org-activate-camels (limit)
  3000. "Run through the buffer and add overlays to dates."
  3001. (if (re-search-forward org-camel-regexp limit t)
  3002. (progn
  3003. (add-text-properties (match-beginning 0) (match-end 0)
  3004. (list 'mouse-face 'highlight
  3005. 'keymap org-mouse-map))
  3006. t)))
  3007. (defun org-activate-tags (limit)
  3008. (if (re-search-forward "[ \t]\\(:[A-Za-z_@0-9:]+:\\)[ \r\n]" limit t)
  3009. (progn
  3010. (add-text-properties (match-beginning 1) (match-end 1)
  3011. (list 'mouse-face 'highlight
  3012. 'keymap org-mouse-map))
  3013. t)))
  3014. (defun org-font-lock-level ()
  3015. (save-excursion
  3016. (org-back-to-heading t)
  3017. (- (match-end 0) (match-beginning 0))))
  3018. (defun org-outline-level ()
  3019. (save-excursion
  3020. (looking-at outline-regexp)
  3021. (if (match-beginning 1)
  3022. (+ (org-get-string-indentation (match-string 1)) 1000)
  3023. (- (match-end 0) (match-beginning 0)))))
  3024. (defvar org-font-lock-keywords nil)
  3025. (defun org-set-font-lock-defaults ()
  3026. (let* ((em org-fontify-emphasized-text)
  3027. (lk org-activate-links)
  3028. (org-font-lock-extra-keywords
  3029. ;; Headlines
  3030. (list
  3031. '("^\\(\\**\\)\\(\\*\\)\\(.*\\)" (1 (org-get-level-face 1))
  3032. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3033. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3034. (1 'org-table))
  3035. ;; Links
  3036. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3037. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3038. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3039. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3040. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3041. (if (memq 'camel lk) '(org-activate-camels (0 'org-link t)))
  3042. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3043. (if org-table-limit-column-width
  3044. '(org-hide-wide-columns (0 nil append)))
  3045. ;; TODO lines
  3046. (list (concat "^\\*+[ \t]*" org-not-done-regexp)
  3047. '(1 'org-todo t))
  3048. ;; Priorities
  3049. (list (concat "\\[#[A-Z]\\]") '(0 'org-special-keyword t))
  3050. ;; Special keywords
  3051. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3052. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3053. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3054. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3055. ;; Emphasis
  3056. (if em '(org-do-emphasis-faces))
  3057. ;; Checkboxes, similar to Frank Ruell's org-checklet.el
  3058. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[ X]\\]\\)"
  3059. 2 'bold prepend)
  3060. (if org-provide-checkbox-statistics
  3061. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3062. (0 (org-get-checkbox-statistics-face) t)))
  3063. ;; COMMENT
  3064. (list (concat "^\\*+[ \t]*\\<\\(" org-comment-string
  3065. "\\|" org-quote-string "\\)\\>")
  3066. '(1 'org-special-keyword t))
  3067. '("^#.*" (0 'font-lock-comment-face t))
  3068. ;; DONE
  3069. (if org-fontify-done-headline
  3070. (list (concat "^[*]+ +\\<\\(" org-done-string "\\)\\(.*\\)\\>")
  3071. '(1 'org-done t) '(2 'org-headline-done t))
  3072. (list (concat "^[*]+ +\\<\\(" org-done-string "\\)\\>")
  3073. '(1 'org-done t)))
  3074. ;; Table stuff
  3075. '("^[ \t]*\\(:.*\\)" (1 'org-table t))
  3076. '("| *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3077. '("^[ \t]*| *\\([#!$*_^]\\) *|" (1 'org-formula t))
  3078. (if org-format-transports-properties-p
  3079. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3080. '("^\\*+ \\(.*:ARCHIVE:.*\\)" (1 'org-archived prepend))
  3081. )))
  3082. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3083. ;; Now set the full font-lock-keywords
  3084. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3085. (org-set-local 'font-lock-defaults
  3086. '(org-font-lock-keywords t nil nil backward-paragraph))
  3087. (kill-local-variable 'font-lock-keywords) nil))
  3088. (defvar org-m nil)
  3089. (defvar org-l nil)
  3090. (defvar org-f nil)
  3091. (defun org-get-level-face (n)
  3092. "Get the right face for match N in font-lock matching of healdines."
  3093. (setq org-l (- (match-end 2) (match-beginning 1)))
  3094. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3095. ; (setq org-f (nth (1- (% org-l org-n-levels)) org-level-faces))
  3096. (setq org-f (nth (% (1- org-l) org-n-levels) org-level-faces))
  3097. (cond
  3098. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3099. ((eq n 2) org-f)
  3100. (t (if org-level-color-stars-only nil org-f))))
  3101. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3102. "Remove fontification and activation overlays from links."
  3103. (font-lock-default-unfontify-region beg end)
  3104. (let* ((buffer-undo-list t)
  3105. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3106. (inhibit-modification-hooks t)
  3107. deactivate-mark buffer-file-name buffer-file-truename)
  3108. (remove-text-properties beg end
  3109. '(mouse-face nil keymap nil org-linked-text nil
  3110. invisible nil intangible nil))))
  3111. ;;; Visibility cycling
  3112. (defvar org-cycle-global-status nil)
  3113. (make-variable-buffer-local 'org-cycle-global-status)
  3114. (defvar org-cycle-subtree-status nil)
  3115. (make-variable-buffer-local 'org-cycle-subtree-status)
  3116. ;;;###autoload
  3117. (defun org-cycle (&optional arg)
  3118. "Visibility cycling for Org-mode.
  3119. - When this function is called with a prefix argument, rotate the entire
  3120. buffer through 3 states (global cycling)
  3121. 1. OVERVIEW: Show only top-level headlines.
  3122. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3123. 3. SHOW ALL: Show everything.
  3124. - When point is at the beginning of a headline, rotate the subtree started
  3125. by this line through 3 different states (local cycling)
  3126. 1. FOLDED: Only the main headline is shown.
  3127. 2. CHILDREN: The main headline and the direct children are shown.
  3128. From this state, you can move to one of the children
  3129. and zoom in further.
  3130. 3. SUBTREE: Show the entire subtree, including body text.
  3131. - When there is a numeric prefix, go up to a heading with level ARG, do
  3132. a `show-subtree' and return to the previous cursor position. If ARG
  3133. is negative, go up that many levels.
  3134. - When point is not at the beginning of a headline, execute
  3135. `indent-relative', like TAB normally does. See the option
  3136. `org-cycle-emulate-tab' for details.
  3137. - Special case: if point is the the beginning of the buffer and there is
  3138. no headline in line 1, this function will act as if called with prefix arg."
  3139. (interactive "P")
  3140. (let* ((outline-regexp
  3141. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3142. "\\(?:\\*+\\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3143. outline-regexp))
  3144. (bob-special (and org-cycle-global-at-bob (bobp)
  3145. (not (looking-at outline-regexp))))
  3146. (org-cycle-hook
  3147. (if bob-special
  3148. (delq 'org-optimize-window-after-visibility-change
  3149. (copy-sequence org-cycle-hook))
  3150. org-cycle-hook))
  3151. (pos (point)))
  3152. (if (or bob-special (equal arg '(4)))
  3153. ;; special case: use global cycling
  3154. (setq arg t))
  3155. (cond
  3156. ((org-at-table-p 'any)
  3157. ;; Enter the table or move to the next field in the table
  3158. (or (org-table-recognize-table.el)
  3159. (progn
  3160. (if arg (org-table-edit-field t)
  3161. (org-table-justify-field-maybe)
  3162. (call-interactively 'org-table-next-field)))))
  3163. ((eq arg t) ;; Global cycling
  3164. (cond
  3165. ((and (eq last-command this-command)
  3166. (eq org-cycle-global-status 'overview))
  3167. ;; We just created the overview - now do table of contents
  3168. ;; This can be slow in very large buffers, so indicate action
  3169. (message "CONTENTS...")
  3170. (org-content)
  3171. (message "CONTENTS...done")
  3172. (setq org-cycle-global-status 'contents)
  3173. (run-hook-with-args 'org-cycle-hook 'contents))
  3174. ((and (eq last-command this-command)
  3175. (eq org-cycle-global-status 'contents))
  3176. ;; We just showed the table of contents - now show everything
  3177. (show-all)
  3178. (message "SHOW ALL")
  3179. (setq org-cycle-global-status 'all)
  3180. (run-hook-with-args 'org-cycle-hook 'all))
  3181. (t
  3182. ;; Default action: go to overview
  3183. (org-overview)
  3184. (message "OVERVIEW")
  3185. (setq org-cycle-global-status 'overview)
  3186. (run-hook-with-args 'org-cycle-hook 'overview))))
  3187. ((integerp arg)
  3188. ;; Show-subtree, ARG levels up from here.
  3189. (save-excursion
  3190. (org-back-to-heading)
  3191. (outline-up-heading (if (< arg 0) (- arg)
  3192. (- (funcall outline-level) arg)))
  3193. (org-show-subtree)))
  3194. ((save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3195. ;; At a heading: rotate between three different views
  3196. (org-back-to-heading)
  3197. (let ((goal-column 0) eoh eol eos)
  3198. ;; First, some boundaries
  3199. (save-excursion
  3200. (org-back-to-heading)
  3201. (save-excursion
  3202. (beginning-of-line 2)
  3203. (while (and (not (eobp)) ;; this is like `next-line'
  3204. (get-char-property (1- (point)) 'invisible))
  3205. (beginning-of-line 2)) (setq eol (point)))
  3206. (outline-end-of-heading) (setq eoh (point))
  3207. (org-end-of-subtree t) (setq eos (point))
  3208. (outline-next-heading))
  3209. ;; Find out what to do next and set `this-command'
  3210. (cond
  3211. ((and (= eos eoh)
  3212. ;; Nothing is hidden behind this heading
  3213. (message "EMPTY ENTRY")
  3214. (setq org-cycle-subtree-status nil)))
  3215. ((>= eol eos)
  3216. ;; Entire subtree is hidden in one line: open it
  3217. (org-show-entry)
  3218. (show-children)
  3219. (message "CHILDREN")
  3220. (setq org-cycle-subtree-status 'children)
  3221. (run-hook-with-args 'org-cycle-hook 'children))
  3222. ((and (eq last-command this-command)
  3223. (eq org-cycle-subtree-status 'children))
  3224. ;; We just showed the children, now show everything.
  3225. (org-show-subtree)
  3226. (message "SUBTREE")
  3227. (setq org-cycle-subtree-status 'subtree)
  3228. (run-hook-with-args 'org-cycle-hook 'subtree))
  3229. (t
  3230. ;; Default action: hide the subtree.
  3231. (hide-subtree)
  3232. (message "FOLDED")
  3233. (setq org-cycle-subtree-status 'folded)
  3234. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3235. ;; TAB emulation
  3236. (buffer-read-only (org-back-to-heading))
  3237. ((org-try-cdlatex-tab))
  3238. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3239. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3240. (or (and (eq org-cycle-emulate-tab 'white)
  3241. (= (match-end 0) (point-at-eol)))
  3242. (and (eq org-cycle-emulate-tab 'whitestart)
  3243. (>= (match-end 0) pos))))
  3244. t
  3245. (eq org-cycle-emulate-tab t))
  3246. (if (and (looking-at "[ \n\r\t]")
  3247. (string-match "^[ \t]*$" (buffer-substring
  3248. (point-at-bol) (point))))
  3249. (progn
  3250. (beginning-of-line 1)
  3251. (and (looking-at "[ \t]+") (replace-match ""))))
  3252. (indent-relative))
  3253. (t (save-excursion
  3254. (org-back-to-heading)
  3255. (org-cycle))))))
  3256. ;;;###autoload
  3257. (defun org-global-cycle (&optional arg)
  3258. "Cycle the global visibility. For details see `org-cycle'."
  3259. (interactive "P")
  3260. (let ((org-cycle-include-plain-lists
  3261. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  3262. (if (integerp arg)
  3263. (progn
  3264. (show-all)
  3265. (hide-sublevels arg)
  3266. (setq org-cycle-global-status 'contents))
  3267. (org-cycle '(4)))))
  3268. (defun org-overview ()
  3269. "Switch to overview mode, shoing only top-level headlines.
  3270. Really, this shows all headlines with level equal or greater than the level
  3271. of the first headline in the buffer. This is important, because if the
  3272. first headline is not level one, then (hide-sublevels 1) gives confusing
  3273. results."
  3274. (interactive)
  3275. (hide-sublevels (save-excursion
  3276. (goto-char (point-min))
  3277. (if (re-search-forward (concat "^" outline-regexp) nil t)
  3278. (progn
  3279. (goto-char (match-beginning 0))
  3280. (funcall outline-level))
  3281. 1))))
  3282. ;; FIXME: allow an argument to give a limiting level for this.
  3283. (defun org-content ()
  3284. "Show all headlines in the buffer, like a table of contents"
  3285. (interactive)
  3286. (save-excursion
  3287. ;; Visit all headings and show their offspring
  3288. (goto-char (point-max))
  3289. (catch 'exit
  3290. (while (and (progn (condition-case nil
  3291. (outline-previous-visible-heading 1)
  3292. (error (goto-char (point-min))))
  3293. t)
  3294. (looking-at outline-regexp))
  3295. (show-branches)
  3296. (if (bobp) (throw 'exit nil))))))
  3297. (defun org-optimize-window-after-visibility-change (state)
  3298. "Adjust the window after a change in outline visibility.
  3299. This function is the default value of the hook `org-cycle-hook'."
  3300. (when (get-buffer-window (current-buffer))
  3301. (cond
  3302. ((eq state 'overview) (org-first-headline-recenter 1))
  3303. ((eq state 'content) nil)
  3304. ((eq state 'all) nil)
  3305. ((eq state 'folded) nil)
  3306. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  3307. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  3308. (defun org-subtree-end-visible-p ()
  3309. "Is the end of the current subtree visible?"
  3310. (pos-visible-in-window-p
  3311. (save-excursion (org-end-of-subtree t) (point))))
  3312. (defun org-first-headline-recenter (&optional N)
  3313. "Move cursor to the first headline and recenter the headline.
  3314. Optional argument N means, put the headline into the Nth line of the window."
  3315. (goto-char (point-min))
  3316. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  3317. (beginning-of-line)
  3318. (recenter (prefix-numeric-value N))))
  3319. (defvar org-goto-window-configuration nil)
  3320. (defvar org-goto-marker nil)
  3321. (defvar org-goto-map (make-sparse-keymap))
  3322. (let ((cmds '(isearch-forward isearch-backward)) cmd)
  3323. (while (setq cmd (pop cmds))
  3324. (substitute-key-definition cmd cmd org-goto-map global-map)))
  3325. (define-key org-goto-map "\C-m" 'org-goto-ret)
  3326. (define-key org-goto-map [(left)] 'org-goto-left)
  3327. (define-key org-goto-map [(right)] 'org-goto-right)
  3328. (define-key org-goto-map [(?q)] 'org-goto-quit)
  3329. (define-key org-goto-map [(control ?g)] 'org-goto-quit)
  3330. (define-key org-goto-map "\C-i" 'org-cycle)
  3331. (define-key org-goto-map [(tab)] 'org-cycle)
  3332. (define-key org-goto-map [(down)] 'outline-next-visible-heading)
  3333. (define-key org-goto-map [(up)] 'outline-previous-visible-heading)
  3334. (define-key org-goto-map "n" 'outline-next-visible-heading)
  3335. (define-key org-goto-map "p" 'outline-previous-visible-heading)
  3336. (define-key org-goto-map "f" 'outline-forward-same-level)
  3337. (define-key org-goto-map "b" 'outline-backward-same-level)
  3338. (define-key org-goto-map "u" 'outline-up-heading)
  3339. (define-key org-goto-map "\C-c\C-n" 'outline-next-visible-heading)
  3340. (define-key org-goto-map "\C-c\C-p" 'outline-previous-visible-heading)
  3341. (define-key org-goto-map "\C-c\C-f" 'outline-forward-same-level)
  3342. (define-key org-goto-map "\C-c\C-b" 'outline-backward-same-level)
  3343. (define-key org-goto-map "\C-c\C-u" 'outline-up-heading)
  3344. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  3345. (while l (define-key org-goto-map (int-to-string (pop l)) 'digit-argument)))
  3346. (defconst org-goto-help
  3347. "Select a location to jump to, press RET
  3348. \[Up]/[Down]=next/prev headline TAB=cycle visibility RET=select [Q]uit")
  3349. (defun org-goto ()
  3350. "Go to a different location of the document, keeping current visibility.
  3351. When you want to go to a different location in a document, the fastest way
  3352. is often to fold the entire buffer and then dive into the tree. This
  3353. method has the disadvantage, that the previous location will be folded,
  3354. which may not be what you want.
  3355. This command works around this by showing a copy of the current buffer in
  3356. overview mode. You can dive into the tree in that copy, to find the
  3357. location you want to reach. When pressing RET, the command returns to the
  3358. original buffer in which the visibility is still unchanged. It then jumps
  3359. to the new location, making it and the headline hierarchy above it visible."
  3360. (interactive)
  3361. (let* ((org-goto-start-pos (point))
  3362. (selected-point
  3363. (org-get-location (current-buffer) org-goto-help)))
  3364. (if selected-point
  3365. (progn
  3366. (org-mark-ring-push org-goto-start-pos)
  3367. (goto-char selected-point)
  3368. (if (or (org-invisible-p) (org-invisible-p2))
  3369. (org-show-hierarchy-above 'org-goto)))
  3370. (error "Quit"))))
  3371. (defun org-get-location (buf help)
  3372. "Let the user select a location in the Org-mode buffer BUF.
  3373. This function uses a recursive edit. It returns the selected position
  3374. or nil."
  3375. (let (org-selected-point)
  3376. (save-excursion
  3377. (save-window-excursion
  3378. (delete-other-windows)
  3379. (switch-to-buffer (get-buffer-create "*org-goto*"))
  3380. (with-output-to-temp-buffer "*Help*"
  3381. (princ help))
  3382. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  3383. (setq buffer-read-only nil)
  3384. (erase-buffer)
  3385. (insert-buffer-substring buf)
  3386. (let ((org-startup-truncated t)
  3387. (org-startup-folded t)
  3388. (org-startup-align-all-tables nil)
  3389. (org-startup-with-deadline-check nil))
  3390. (org-mode))
  3391. (setq buffer-read-only t)
  3392. (if (boundp 'org-goto-start-pos)
  3393. (goto-char org-goto-start-pos)
  3394. (goto-char (point-min)))
  3395. (org-beginning-of-line)
  3396. (message "Select location and press RET")
  3397. ;; now we make sure that during selection, ony very few keys work
  3398. ;; and that it is impossible to switch to another window.
  3399. (let ((gm (current-global-map))
  3400. (overriding-local-map org-goto-map))
  3401. (unwind-protect
  3402. (progn
  3403. (use-global-map org-goto-map)
  3404. (recursive-edit))
  3405. (use-global-map gm)))))
  3406. (kill-buffer "*org-goto*")
  3407. org-selected-point))
  3408. (defun org-goto-ret (&optional arg)
  3409. "Finish `org-goto' by going to the new location."
  3410. (interactive "P")
  3411. (setq org-selected-point (point)
  3412. current-prefix-arg arg)
  3413. (throw 'exit nil))
  3414. (defun org-goto-left ()
  3415. "Finish `org-goto' by going to the new location."
  3416. (interactive)
  3417. (if (org-on-heading-p)
  3418. (progn
  3419. (beginning-of-line 1)
  3420. (setq org-selected-point (point)
  3421. current-prefix-arg (- (match-end 0) (match-beginning 0)))
  3422. (throw 'exit nil))
  3423. (error "Not on a heading")))
  3424. (defun org-goto-right ()
  3425. "Finish `org-goto' by going to the new location."
  3426. (interactive)
  3427. (if (org-on-heading-p)
  3428. (progn
  3429. (outline-end-of-subtree)
  3430. (or (eobp) (forward-char 1))
  3431. (setq org-selected-point (point)
  3432. current-prefix-arg (- (match-end 0) (match-beginning 0)))
  3433. (throw 'exit nil))
  3434. (error "Not on a heading")))
  3435. (defun org-goto-quit ()
  3436. "Finish `org-goto' without cursor motion."
  3437. (interactive)
  3438. (setq org-selected-point nil)
  3439. (throw 'exit nil))
  3440. ;;; Promotion, Demotion, Inserting new headlines
  3441. (defvar org-ignore-region nil
  3442. "To temporarily disable the active region.")
  3443. (defun org-insert-heading (&optional force-heading)
  3444. "Insert a new heading or item with same depth at point.
  3445. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  3446. If point is at the beginning of a headline, insert a sibling before the
  3447. current headline. If point is in the middle of a headline, split the headline
  3448. at that position and make the rest of the headline part of the sibling below
  3449. the current headline."
  3450. (interactive "P")
  3451. (if (= (buffer-size) 0)
  3452. (insert "\n* ")
  3453. (when (or force-heading (not (org-insert-item)))
  3454. (let* ((head (save-excursion
  3455. (condition-case nil
  3456. (progn
  3457. (org-back-to-heading)
  3458. (match-string 0))
  3459. (error "*"))))
  3460. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  3461. pos)
  3462. (cond
  3463. ((and (org-on-heading-p) (bolp)
  3464. (save-excursion (backward-char 1) (not (org-invisible-p))))
  3465. (open-line (if blank 2 1)))
  3466. ((and (bolp) (save-excursion
  3467. (backward-char 1) (not (org-invisible-p))))
  3468. nil)
  3469. (t (newline (if blank 2 1))))
  3470. (insert head) (just-one-space)
  3471. (setq pos (point))
  3472. (end-of-line 1)
  3473. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  3474. (run-hooks 'org-insert-heading-hook)))))
  3475. (defun org-in-item-p ()
  3476. "It the cursor inside a plain list item.
  3477. Does not have to be the first line."
  3478. (save-excursion
  3479. (condition-case nil
  3480. (progn
  3481. (org-beginning-of-item)
  3482. (org-at-item-p)
  3483. t)
  3484. (error nil))))
  3485. (defun org-insert-item (&optional checkbox)
  3486. "Insert a new item at the current level.
  3487. Return t when things worked, nil when we are not in an item."
  3488. (when (save-excursion
  3489. (condition-case nil
  3490. (progn
  3491. (org-beginning-of-item)
  3492. (org-at-item-p)
  3493. t)
  3494. (error nil)))
  3495. (let* ((bul (match-string 0))
  3496. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  3497. (match-end 0)))
  3498. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  3499. pos)
  3500. (cond
  3501. ((and (org-at-item-p) (<= (point) eow))
  3502. ;; before the bullet
  3503. (beginning-of-line 1)
  3504. (open-line (if blank 2 1)))
  3505. ((<= (point) eow)
  3506. (beginning-of-line 1))
  3507. (t (newline (if blank 2 1))))
  3508. (insert bul (if checkbox "[ ]" ""))
  3509. (just-one-space)
  3510. (setq pos (point))
  3511. (end-of-line 1)
  3512. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  3513. (org-maybe-renumber-ordered-list)
  3514. (and checkbox (org-update-checkbox-count-maybe))
  3515. t))
  3516. (defun org-insert-todo-heading (arg)
  3517. "Insert a new heading with the same level and TODO state as current heading.
  3518. If the heading has no TODO state, or if the state is DONE, use the first
  3519. state (TODO by default). Also with prefix arg, force first state."
  3520. (interactive "P")
  3521. (when (not (org-insert-item 'checkbox))
  3522. (org-insert-heading)
  3523. (save-excursion
  3524. (org-back-to-heading)
  3525. (outline-previous-heading)
  3526. (looking-at org-todo-line-regexp))
  3527. (if (or arg
  3528. (not (match-beginning 2))
  3529. (equal (match-string 2) org-done-string))
  3530. (insert (car org-todo-keywords) " ")
  3531. (insert (match-string 2) " "))))
  3532. (defun org-promote-subtree ()
  3533. "Promote the entire subtree.
  3534. See also `org-promote'."
  3535. (interactive)
  3536. (save-excursion
  3537. (org-map-tree 'org-promote))
  3538. (org-fix-position-after-promote))
  3539. (defun org-demote-subtree ()
  3540. "Demote the entire subtree. See `org-demote'.
  3541. See also `org-promote'."
  3542. (interactive)
  3543. (save-excursion
  3544. (org-map-tree 'org-demote))
  3545. (org-fix-position-after-promote))
  3546. (defun org-do-promote ()
  3547. "Promote the current heading higher up the tree.
  3548. If the region is active in `transient-mark-mode', promote all headings
  3549. in the region."
  3550. (interactive)
  3551. (save-excursion
  3552. (if (org-region-active-p)
  3553. (org-map-region 'org-promote (region-beginning) (region-end))
  3554. (org-promote)))
  3555. (org-fix-position-after-promote))
  3556. (defun org-do-demote ()
  3557. "Demote the current heading lower down the tree.
  3558. If the region is active in `transient-mark-mode', demote all headings
  3559. in the region."
  3560. (interactive)
  3561. (save-excursion
  3562. (if (org-region-active-p)
  3563. (org-map-region 'org-demote (region-beginning) (region-end))
  3564. (org-demote)))
  3565. (org-fix-position-after-promote))
  3566. (defun org-fix-position-after-promote ()
  3567. "Make sure that after pro/demotion cursor position is right."
  3568. (if (and (equal (char-after) ?\n)
  3569. (save-excursion
  3570. (skip-chars-backward "a-zA-Z0-9_@")
  3571. (looking-at org-todo-regexp)))
  3572. (insert " "))
  3573. (and (equal (char-after) ?\ )
  3574. (equal (char-before) ?*)
  3575. (forward-char 1)))
  3576. (defun org-get-legal-level (level change)
  3577. "Rectify a level change under the influence of `org-odd-levels-only'
  3578. LEVEL is a current level, CHANGE is by how much the level should be
  3579. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  3580. even level numbers will become the next higher odd number."
  3581. (if org-odd-levels-only
  3582. (cond ((not change) (1+ (* 2 (/ level 2))))
  3583. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  3584. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  3585. (max 1 (+ level change))))
  3586. (defun org-promote ()
  3587. "Promote the current heading higher up the tree.
  3588. If the region is active in `transient-mark-mode', promote all headings
  3589. in the region."
  3590. (org-back-to-heading t)
  3591. (let* ((level (save-match-data (funcall outline-level)))
  3592. (up-head (make-string (org-get-legal-level level -1) ?*))
  3593. (diff (abs (- level (length up-head)))))
  3594. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover"))
  3595. (replace-match up-head nil t)
  3596. ;; Fixup tag positioning
  3597. (and org-auto-align-tags (org-set-tags nil t))
  3598. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  3599. (defun org-demote ()
  3600. "Demote the current heading lower down the tree.
  3601. If the region is active in `transient-mark-mode', demote all headings
  3602. in the region."
  3603. (org-back-to-heading t)
  3604. (let* ((level (save-match-data (funcall outline-level)))
  3605. (down-head (make-string (org-get-legal-level level 1) ?*))
  3606. (diff (abs (- level (length down-head)))))
  3607. (replace-match down-head nil t)
  3608. ;; Fixup tag positioning
  3609. (and org-auto-align-tags (org-set-tags nil t))
  3610. (if org-adapt-indentation (org-fixup-indentation diff))))
  3611. (defun org-map-tree (fun)
  3612. "Call FUN for every heading underneath the current one."
  3613. (org-back-to-heading)
  3614. (let ((level (funcall outline-level)))
  3615. (save-excursion
  3616. (funcall fun)
  3617. (while (and (progn
  3618. (outline-next-heading)
  3619. (> (funcall outline-level) level))
  3620. (not (eobp)))
  3621. (funcall fun)))))
  3622. (defun org-map-region (fun beg end)
  3623. "Call FUN for every heading between BEG and END."
  3624. (let ((org-ignore-region t))
  3625. (save-excursion
  3626. (setq end (copy-marker end))
  3627. (goto-char beg)
  3628. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  3629. (< (point) end))
  3630. (funcall fun))
  3631. (while (and (progn
  3632. (outline-next-heading)
  3633. (< (point) end))
  3634. (not (eobp)))
  3635. (funcall fun)))))
  3636. (defun org-fixup-indentation (diff)
  3637. "Change the indentation in the current entry by DIFF
  3638. However, if any line in the current entry has no indentation, or if it
  3639. would end up with no indentation after the change, nothing at all is done."
  3640. (save-excursion
  3641. (let ((end (save-excursion (outline-next-heading)
  3642. (point-marker)))
  3643. (prohibit (if (> diff 0)
  3644. "^\\S-"
  3645. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  3646. col)
  3647. (unless (save-excursion (re-search-forward prohibit end t))
  3648. (while (re-search-forward "^[ \t]+" end t)
  3649. (goto-char (match-end 0))
  3650. (setq col (current-column))
  3651. (if (< diff 0) (replace-match ""))
  3652. (indent-to (+ diff col))))
  3653. (move-marker end nil))))
  3654. ;;; Vertical tree motion, cutting and pasting of subtrees
  3655. (defun org-move-subtree-up (&optional arg)
  3656. "Move the current subtree up past ARG headlines of the same level."
  3657. (interactive "p")
  3658. (org-move-subtree-down (- (prefix-numeric-value arg))))
  3659. (defun org-move-subtree-down (&optional arg)
  3660. "Move the current subtree down past ARG headlines of the same level."
  3661. (interactive "p")
  3662. (setq arg (prefix-numeric-value arg))
  3663. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  3664. 'outline-get-last-sibling))
  3665. (ins-point (make-marker))
  3666. (cnt (abs arg))
  3667. beg end txt folded)
  3668. ;; Select the tree
  3669. (org-back-to-heading)
  3670. (setq beg (point))
  3671. (save-match-data
  3672. (save-excursion (outline-end-of-heading)
  3673. (setq folded (org-invisible-p)))
  3674. (outline-end-of-subtree))
  3675. (outline-next-heading)
  3676. (setq end (point))
  3677. ;; Find insertion point, with error handling
  3678. (goto-char beg)
  3679. (while (> cnt 0)
  3680. (or (and (funcall movfunc) (looking-at outline-regexp))
  3681. (progn (goto-char beg)
  3682. (error "Cannot move past superior level or buffer limit")))
  3683. (setq cnt (1- cnt)))
  3684. (if (> arg 0)
  3685. ;; Moving forward - still need to move over subtree
  3686. (progn (outline-end-of-subtree)
  3687. (outline-next-heading)
  3688. (if (not (or (looking-at (concat "^" outline-regexp))
  3689. (bolp)))
  3690. (newline))))
  3691. (move-marker ins-point (point))
  3692. (setq txt (buffer-substring beg end))
  3693. (delete-region beg end)
  3694. (insert txt)
  3695. (goto-char ins-point)
  3696. (if folded (hide-subtree))
  3697. (move-marker ins-point nil)))
  3698. (defvar org-subtree-clip ""
  3699. "Clipboard for cut and paste of subtrees.
  3700. This is actually only a copy of the kill, because we use the normal kill
  3701. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  3702. (defvar org-subtree-clip-folded nil
  3703. "Was the last copied subtree folded?
  3704. This is used to fold the tree back after pasting.")
  3705. (defun org-cut-subtree ()
  3706. "Cut the current subtree into the clipboard.
  3707. This is a short-hand for marking the subtree and then cutting it."
  3708. (interactive)
  3709. (org-copy-subtree 'cut))
  3710. (defun org-copy-subtree (&optional cut)
  3711. "Cut the current subtree into the clipboard.
  3712. This is a short-hand for marking the subtree and then copying it.
  3713. If CUT is non nil, actually cut the subtree."
  3714. (interactive)
  3715. (let (beg end folded)
  3716. (org-back-to-heading)
  3717. (setq beg (point))
  3718. (save-match-data
  3719. (save-excursion (outline-end-of-heading)
  3720. (setq folded (org-invisible-p)))
  3721. (outline-end-of-subtree))
  3722. (if (equal (char-after) ?\n) (forward-char 1))
  3723. (setq end (point))
  3724. (goto-char beg)
  3725. (when (> end beg)
  3726. (setq org-subtree-clip-folded folded)
  3727. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  3728. (setq org-subtree-clip (current-kill 0))
  3729. (message "%s: Subtree with %d characters"
  3730. (if cut "Cut" "Copied")
  3731. (length org-subtree-clip)))))
  3732. (defun org-paste-subtree (&optional level tree)
  3733. "Paste the clipboard as a subtree, with modification of headline level.
  3734. The entire subtree is promoted or demoted in order to match a new headline
  3735. level. By default, the new level is derived from the visible headings
  3736. before and after the insertion point, and taken to be the inferior headline
  3737. level of the two. So if the previous visible heading is level 3 and the
  3738. next is level 4 (or vice versa), level 4 will be used for insertion.
  3739. This makes sure that the subtree remains an independent subtree and does
  3740. not swallow low level entries.
  3741. You can also force a different level, either by using a numeric prefix
  3742. argument, or by inserting the heading marker by hand. For example, if the
  3743. cursor is after \"*****\", then the tree will be shifted to level 5.
  3744. If you want to insert the tree as is, just use \\[yank].
  3745. If optional TREE is given, use this text instead of the kill ring."
  3746. (interactive "P")
  3747. (unless (org-kill-is-subtree-p tree)
  3748. (error
  3749. (substitute-command-keys
  3750. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  3751. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  3752. (^re (concat "^\\(" outline-regexp "\\)"))
  3753. (re (concat "\\(" outline-regexp "\\)"))
  3754. (^re_ (concat "\\(" outline-regexp "\\)[ \t]*"))
  3755. (old-level (if (string-match ^re txt)
  3756. (- (match-end 0) (match-beginning 0))
  3757. -1))
  3758. (force-level (cond (level (prefix-numeric-value level))
  3759. ((string-match
  3760. ^re_ (buffer-substring (point-at-bol) (point)))
  3761. (- (match-end 0) (match-beginning 0)))
  3762. (t nil)))
  3763. (previous-level (save-excursion
  3764. (condition-case nil
  3765. (progn
  3766. (outline-previous-visible-heading 1)
  3767. (if (looking-at re)
  3768. (- (match-end 0) (match-beginning 0))
  3769. 1))
  3770. (error 1))))
  3771. (next-level (save-excursion
  3772. (condition-case nil
  3773. (progn
  3774. (outline-next-visible-heading 1)
  3775. (if (looking-at re)
  3776. (- (match-end 0) (match-beginning 0))
  3777. 1))
  3778. (error 1))))
  3779. (new-level (or force-level (max previous-level next-level)))
  3780. (shift (if (or (= old-level -1)
  3781. (= new-level -1)
  3782. (= old-level new-level))
  3783. 0
  3784. (- new-level old-level)))
  3785. (shift1 shift)
  3786. (delta (if (> shift 0) -1 1))
  3787. (func (if (> shift 0) 'org-demote 'org-promote))
  3788. (org-odd-levels-only nil)
  3789. beg end)
  3790. ;; Remove the forces level indicator
  3791. (if force-level
  3792. (delete-region (point-at-bol) (point)))
  3793. ;; Make sure we start at the beginning of an empty line
  3794. (if (not (bolp)) (insert "\n"))
  3795. (if (not (looking-at "[ \t]*$"))
  3796. (progn (insert "\n") (backward-char 1)))
  3797. ;; Paste
  3798. (setq beg (point))
  3799. (if (string-match "[ \t\r\n]+\\'" txt)
  3800. (setq txt (replace-match "\n" t t txt)))
  3801. (insert txt)
  3802. (setq end (point))
  3803. (if (looking-at "[ \t\r\n]+")
  3804. (replace-match "\n"))
  3805. (goto-char beg)
  3806. ;; Shift if necessary
  3807. (if (= shift 0)
  3808. (message "Pasted at level %d, without shift" new-level)
  3809. (save-restriction
  3810. (narrow-to-region beg end)
  3811. (while (not (= shift 0))
  3812. (org-map-region func (point-min) (point-max))
  3813. (setq shift (+ delta shift)))
  3814. (goto-char (point-min))
  3815. (message "Pasted at level %d, with shift by %d levels"
  3816. new-level shift1)))
  3817. (if (and kill-ring
  3818. (eq org-subtree-clip (current-kill 0))
  3819. org-subtree-clip-folded)
  3820. ;; The tree was folded before it was killed/copied
  3821. (hide-subtree))))
  3822. (defun org-kill-is-subtree-p (&optional txt)
  3823. "Check if the current kill is an outline subtree, or a set of trees.
  3824. Returns nil if kill does not start with a headline, or if the first
  3825. headline level is not the largest headline level in the tree.
  3826. So this will actually accept several entries of equal levels as well,
  3827. which is OK for `org-paste-subtree'.
  3828. If optional TXT is given, check this string instead of the current kill."
  3829. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  3830. (start-level (and kill
  3831. (string-match (concat "\\`" outline-regexp) kill)
  3832. (- (match-end 0) (match-beginning 0))))
  3833. (re (concat "^" outline-regexp))
  3834. (start 1))
  3835. (if (not start-level)
  3836. nil ;; does not even start with a heading
  3837. (catch 'exit
  3838. (while (setq start (string-match re kill (1+ start)))
  3839. (if (< (- (match-end 0) (match-beginning 0)) start-level)
  3840. (throw 'exit nil)))
  3841. t))))
  3842. (defun org-narrow-to-subtree ()
  3843. "Narrow buffer to the current subtree."
  3844. (interactive)
  3845. (save-excursion
  3846. (narrow-to-region
  3847. (progn (org-back-to-heading) (point))
  3848. (progn (org-end-of-subtree t) (point)))))
  3849. ;;; Plain list items
  3850. (defun org-at-item-p ()
  3851. "Is point in a line starting a hand-formatted item?"
  3852. (let ((llt org-plain-list-ordered-item-terminator))
  3853. (save-excursion
  3854. (goto-char (point-at-bol))
  3855. (looking-at
  3856. (cond
  3857. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  3858. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  3859. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  3860. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  3861. (defun org-at-item-checkbox-p ()
  3862. "Is point at a line starting a plain-list item with a checklet?"
  3863. (and (org-at-item-p)
  3864. (save-excursion
  3865. (goto-char (match-end 0))
  3866. (skip-chars-forward " \t")
  3867. (looking-at "\\[[ X]\\]"))))
  3868. (defun org-toggle-checkbox (&optional arg)
  3869. "Toggle the checkbox in the current line."
  3870. (interactive "P")
  3871. (catch 'exit
  3872. (let (beg end status (firstnew 'unknown))
  3873. (cond
  3874. ((org-region-active-p)
  3875. (setq beg (region-beginning) end (region-end)))
  3876. ((org-on-heading-p)
  3877. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  3878. ((org-at-item-checkbox-p)
  3879. (save-excursion
  3880. (replace-match (if (equal (match-string 0) "[ ]") "[X]" "[ ]") t t))
  3881. (throw 'exit t))
  3882. (t (error "Not at a checkbox or heading, and no active region")))
  3883. (save-excursion
  3884. (goto-char beg)
  3885. (while (< (point) end)
  3886. (when (org-at-item-checkbox-p)
  3887. (setq status (equal (match-string 0) "[X]"))
  3888. (when (eq firstnew 'unknown)
  3889. (setq firstnew (not status)))
  3890. (replace-match
  3891. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  3892. (beginning-of-line 2)))))
  3893. (org-update-checkbox-count-maybe))
  3894. (defun org-update-checkbox-count-maybe ()
  3895. "Update checkbox statistics unless turned off by user."
  3896. (when org-provide-checkbox-statistics
  3897. (org-update-checkbox-count)))
  3898. (defun org-update-checkbox-count (&optional all)
  3899. "Update the checkbox statistics in the current section.
  3900. This will find all statistic cookies like [57%] and [6/12] and update them
  3901. with the current numbers. With optional prefix argument ALL, do this for
  3902. the whole buffer."
  3903. (interactive "P")
  3904. (save-excursion
  3905. (let* ((buffer-invisibility-spec nil)
  3906. (beg (progn (outline-back-to-heading) (point)))
  3907. (end (move-marker (make-marker)
  3908. (progn (outline-next-heading) (point))))
  3909. (re "\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)")
  3910. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[ X]\\]\\)")
  3911. b1 e1 f1 c-on c-off lim (cstat 0))
  3912. (when all
  3913. (goto-char (point-min))
  3914. (outline-next-heading)
  3915. (setq beg (point) end (point-max)))
  3916. (goto-char beg)
  3917. (while (re-search-forward re end t)
  3918. (setq cstat (1+ cstat)
  3919. b1 (match-beginning 0)
  3920. e1 (match-end 0)
  3921. f1 (match-beginning 1)
  3922. lim (cond
  3923. ((org-on-heading-p) (outline-next-heading) (point))
  3924. ((org-at-item-p) (org-end-of-item) (point))
  3925. (t nil))
  3926. c-on 0 c-off 0)
  3927. (goto-char e1)
  3928. (when lim
  3929. (while (re-search-forward re-box lim t)
  3930. (if (equal (match-string 2) "[ ]")
  3931. (setq c-off (1+ c-off))
  3932. (setq c-on (1+ c-on))))
  3933. (delete-region b1 e1)
  3934. (goto-char b1)
  3935. (insert (if f1
  3936. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  3937. (format "[%d/%d]" c-on (+ c-on c-off))))))
  3938. (when (interactive-p)
  3939. (message "Checkbox satistics updated %s (%d places)"
  3940. (if all "in entire file" "in current outline entry") cstat)))))
  3941. (defun org-get-checkbox-statistics-face ()
  3942. "Select the face for checkbox statistics.
  3943. The face will be `org-done' when all relevant boxes are checked. Otherwise
  3944. it will be `org-todo'."
  3945. (if (match-end 1)
  3946. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  3947. (if (and (> (match-end 2) (match-beginning 2))
  3948. (equal (match-string 2) (match-string 3)))
  3949. 'org-done
  3950. 'org-todo)))
  3951. (defun org-get-indentation (&optional line)
  3952. "Get the indentation of the current line, interpreting tabs.
  3953. When LINE is given, assume it represents a line and compute its indentation."
  3954. (if line
  3955. (if (string-match "^ *" (org-remove-tabs line))
  3956. (match-end 0))
  3957. (save-excursion
  3958. (beginning-of-line 1)
  3959. (skip-chars-forward " \t")
  3960. (current-column))))
  3961. (defun org-remove-tabs (s &optional width)
  3962. "Replace tabulators in S with spaces.
  3963. Assumes that s is a single line, starting in column 0."
  3964. (setq width (or width tab-width))
  3965. (while (string-match "\t" s)
  3966. (setq s (replace-match
  3967. (make-string
  3968. (- (* width (/ (+ (match-beginning 0) width) width))
  3969. (match-beginning 0)) ?\ )
  3970. t t s)))
  3971. s)
  3972. ;; FIXME: document properly.
  3973. (defun org-fix-indentation (line ind)
  3974. "If the current indenation is smaller than ind1, leave it alone.
  3975. If it is larger than ind, reduce it by ind."
  3976. (let* ((l (org-remove-tabs line))
  3977. (i (org-get-indentation l))
  3978. (i1 (car ind)) (i2 (cdr ind)))
  3979. (if (>= i i2) (setq l (substring line i2)))
  3980. (if (> i1 0)
  3981. (concat (make-string i1 ?\ ) l)
  3982. l)))
  3983. (defun org-beginning-of-item ()
  3984. "Go to the beginning of the current hand-formatted item.
  3985. If the cursor is not in an item, throw an error."
  3986. (interactive)
  3987. (let ((pos (point))
  3988. (limit (save-excursion (org-back-to-heading)
  3989. (beginning-of-line 2) (point)))
  3990. ind ind1)
  3991. (if (org-at-item-p)
  3992. (beginning-of-line 1)
  3993. (beginning-of-line 1)
  3994. (skip-chars-forward " \t")
  3995. (setq ind (current-column))
  3996. (if (catch 'exit
  3997. (while t
  3998. (beginning-of-line 0)
  3999. (if (< (point) limit) (throw 'exit nil))
  4000. (unless (looking-at "[ \t]*$")
  4001. (skip-chars-forward " \t")
  4002. (setq ind1 (current-column))
  4003. (if (< ind1 ind)
  4004. (throw 'exit (org-at-item-p))))))
  4005. nil
  4006. (goto-char pos)
  4007. (error "Not in an item")))))
  4008. (defun org-end-of-item ()
  4009. "Go to the end of the current hand-formatted item.
  4010. If the cursor is not in an item, throw an error."
  4011. (interactive)
  4012. (let ((pos (point))
  4013. (limit (save-excursion (outline-next-heading) (point)))
  4014. (ind (save-excursion
  4015. (org-beginning-of-item)
  4016. (skip-chars-forward " \t")
  4017. (current-column)))
  4018. ind1)
  4019. (if (catch 'exit
  4020. (while t
  4021. (beginning-of-line 2)
  4022. (if (>= (point) limit) (throw 'exit t))
  4023. (unless (looking-at "[ \t]*$")
  4024. (skip-chars-forward " \t")
  4025. (setq ind1 (current-column))
  4026. (if (<= ind1 ind) (throw 'exit t)))))
  4027. (beginning-of-line 1)
  4028. (goto-char pos)
  4029. (error "Not in an item"))))
  4030. (defun org-next-item ()
  4031. "Move to the beginning of the next item in the current plain list.
  4032. Error if not at a plain list, or if this is the last item in the list."
  4033. (interactive)
  4034. (let (ind ind1 (pos (point)))
  4035. (org-beginning-of-item)
  4036. (setq ind (org-get-indentation))
  4037. (org-end-of-item)
  4038. (setq ind1 (org-get-indentation))
  4039. (unless (and (org-at-item-p) (= ind ind1))
  4040. (goto-char pos)
  4041. (error "On last item"))))
  4042. (defun org-previous-item ()
  4043. "Move to the beginning of the previous item in the current plain list.
  4044. Error if not at a plain list, or if this is the last item in the list."
  4045. (interactive)
  4046. (let (beg ind (pos (point)))
  4047. (org-beginning-of-item)
  4048. (setq beg (point))
  4049. (setq ind (org-get-indentation))
  4050. (goto-char beg)
  4051. (catch 'exit
  4052. (while t
  4053. (beginning-of-line 0)
  4054. (if (looking-at "[ \t]*$")
  4055. nil
  4056. (if (<= (org-get-indentation) ind)
  4057. (throw 'exit t)))))
  4058. (condition-case nil
  4059. (org-beginning-of-item)
  4060. (error (goto-char pos)
  4061. (error "On first item")))))
  4062. (defun org-move-item-down ()
  4063. "Move the plain list item at point down, i.e. swap with following item.
  4064. Subitems (items with larger indentation) are considered part of the item,
  4065. so this really moves item trees."
  4066. (interactive)
  4067. (let (beg end ind ind1 (pos (point)) txt)
  4068. (org-beginning-of-item)
  4069. (setq beg (point))
  4070. (setq ind (org-get-indentation))
  4071. (org-end-of-item)
  4072. (setq end (point))
  4073. (setq ind1 (org-get-indentation))
  4074. (if (and (org-at-item-p) (= ind ind1))
  4075. (progn
  4076. (org-end-of-item)
  4077. (setq txt (buffer-substring beg end))
  4078. (save-excursion
  4079. (delete-region beg end))
  4080. (setq pos (point))
  4081. (insert txt)
  4082. (goto-char pos)
  4083. (org-maybe-renumber-ordered-list))
  4084. (goto-char pos)
  4085. (error "Cannot move this item further down"))))
  4086. (defun org-move-item-up (arg)
  4087. "Move the plain list item at point up, i.e. swap with previous item.
  4088. Subitems (items with larger indentation) are considered part of the item,
  4089. so this really moves item trees."
  4090. (interactive "p")
  4091. (let (beg end ind ind1 (pos (point)) txt)
  4092. (org-beginning-of-item)
  4093. (setq beg (point))
  4094. (setq ind (org-get-indentation))
  4095. (org-end-of-item)
  4096. (setq end (point))
  4097. (goto-char beg)
  4098. (catch 'exit
  4099. (while t
  4100. (beginning-of-line 0)
  4101. (if (looking-at "[ \t]*$")
  4102. nil
  4103. (if (<= (setq ind1 (org-get-indentation)) ind)
  4104. (throw 'exit t)))))
  4105. (condition-case nil
  4106. (org-beginning-of-item)
  4107. (error (goto-char beg)
  4108. (error "Cannot move this item further up")))
  4109. (setq ind1 (org-get-indentation))
  4110. (if (and (org-at-item-p) (= ind ind1))
  4111. (progn
  4112. (setq txt (buffer-substring beg end))
  4113. (save-excursion
  4114. (delete-region beg end))
  4115. (setq pos (point))
  4116. (insert txt)
  4117. (goto-char pos)
  4118. (org-maybe-renumber-ordered-list))
  4119. (goto-char pos)
  4120. (error "Cannot move this item further up"))))
  4121. (defun org-maybe-renumber-ordered-list ()
  4122. "Renumber the ordered list at point if setup allows it.
  4123. This tests the user option `org-auto-renumber-ordered-lists' before
  4124. doing the renumbering."
  4125. (and org-auto-renumber-ordered-lists
  4126. (org-at-item-p)
  4127. (match-beginning 3)
  4128. (org-renumber-ordered-list 1)))
  4129. (defun org-get-string-indentation (s)
  4130. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  4131. (let ((n -1) (i 0) (w tab-width) c)
  4132. (catch 'exit
  4133. (while (< (setq n (1+ n)) (length s))
  4134. (setq c (aref s n))
  4135. (cond ((= c ?\ ) (setq i (1+ i)))
  4136. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  4137. (t (throw 'exit t)))))
  4138. i))
  4139. (defun org-renumber-ordered-list (arg)
  4140. "Renumber an ordered plain list.
  4141. Cursor needs to be in the first line of an item, the line that starts
  4142. with something like \"1.\" or \"2)\"."
  4143. (interactive "p")
  4144. (unless (and (org-at-item-p)
  4145. (match-beginning 3))
  4146. (error "This is not an ordered list"))
  4147. (let ((line (org-current-line))
  4148. (col (current-column))
  4149. (ind (org-get-string-indentation
  4150. (buffer-substring (point-at-bol) (match-beginning 3))))
  4151. ;; (term (substring (match-string 3) -1))
  4152. ind1 (n (1- arg)))
  4153. ;; find where this list begins
  4154. (catch 'exit
  4155. (while t
  4156. (catch 'next
  4157. (beginning-of-line 0)
  4158. (if (looking-at "[ \t]*$") (throw 'next t))
  4159. (skip-chars-forward " \t") (setq ind1 (current-column))
  4160. (if (or (< ind1 ind)
  4161. (and (= ind1 ind)
  4162. (not (org-at-item-p))))
  4163. (throw 'exit t)))))
  4164. ;; Walk forward and replace these numbers
  4165. (catch 'exit
  4166. (while t
  4167. (catch 'next
  4168. (beginning-of-line 2)
  4169. (if (eobp) (throw 'exit nil))
  4170. (if (looking-at "[ \t]*$") (throw 'next nil))
  4171. (skip-chars-forward " \t") (setq ind1 (current-column))
  4172. (if (> ind1 ind) (throw 'next t))
  4173. (if (< ind1 ind) (throw 'exit t))
  4174. (if (not (org-at-item-p)) (throw 'exit nil))
  4175. (if (not (match-beginning 3))
  4176. (error "unordered bullet in ordered list. Press \\[undo] to recover"))
  4177. (delete-region (match-beginning 3) (1- (match-end 3)))
  4178. (goto-char (match-beginning 3))
  4179. (insert (format "%d" (setq n (1+ n)))))))
  4180. (goto-line line)
  4181. (move-to-column col)))
  4182. (defvar org-last-indent-begin-marker (make-marker))
  4183. (defvar org-last-indent-end-marker (make-marker))
  4184. (defun org-outdent-item (arg)
  4185. "Outdent a local list item."
  4186. (interactive "p")
  4187. (org-indent-item (- arg)))
  4188. (defun org-indent-item (arg)
  4189. "Indent a local list item."
  4190. (interactive "p")
  4191. (unless (org-at-item-p)
  4192. (error "Not on an item"))
  4193. (save-excursion
  4194. (let (beg end ind ind1)
  4195. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  4196. (setq beg org-last-indent-begin-marker
  4197. end org-last-indent-end-marker)
  4198. (org-beginning-of-item)
  4199. (setq beg (move-marker org-last-indent-begin-marker (point)))
  4200. (org-end-of-item)
  4201. (setq end (move-marker org-last-indent-end-marker (point))))
  4202. (goto-char beg)
  4203. (skip-chars-forward " \t") (setq ind (current-column))
  4204. (if (< (+ arg ind) 0) (error "Cannot outdent beyond margin"))
  4205. (while (< (point) end)
  4206. (beginning-of-line 1)
  4207. (skip-chars-forward " \t") (setq ind1 (current-column))
  4208. (delete-region (point-at-bol) (point))
  4209. (indent-to-column (+ ind1 arg))
  4210. (beginning-of-line 2)))))
  4211. ;;; Archiving
  4212. (defun org-archive-subtree (&optional find-done)
  4213. "Move the current subtree to the archive.
  4214. The archive can be a certain top-level heading in the current file, or in
  4215. a different file. The tree will be moved to that location, the subtree
  4216. heading be marked DONE, and the current time will be added.
  4217. When called with prefix argument FIND-DONE, find whole trees without any
  4218. open TODO items and archive them (after getting confirmation from the user).
  4219. If the cursor is not at a headline when this comand is called, try all level
  4220. 1 trees. If the cursor is on a headline, only try the direct children of
  4221. this heading. "
  4222. (interactive "P")
  4223. (if find-done
  4224. (org-archive-all-done)
  4225. ;; Save all relevant TODO keyword-relatex variables
  4226. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  4227. (tr-org-todo-keywords org-todo-keywords)
  4228. (tr-org-todo-interpretation org-todo-interpretation)
  4229. (tr-org-done-string org-done-string)
  4230. (tr-org-todo-regexp org-todo-regexp)
  4231. (tr-org-todo-line-regexp org-todo-line-regexp)
  4232. (this-buffer (current-buffer))
  4233. file heading buffer level newfile-p)
  4234. (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
  4235. (progn
  4236. (setq file (format (match-string 1 org-archive-location)
  4237. (file-name-nondirectory buffer-file-name))
  4238. heading (match-string 2 org-archive-location)))
  4239. (error "Invalid `org-archive-location'"))
  4240. (if (> (length file) 0)
  4241. (setq newfile-p (not (file-exists-p file))
  4242. buffer (find-file-noselect file))
  4243. (setq buffer (current-buffer)))
  4244. (unless buffer
  4245. (error "Cannot access file \"%s\"" file))
  4246. (if (and (> (length heading) 0)
  4247. (string-match "^\\*+" heading))
  4248. (setq level (match-end 0))
  4249. (setq heading nil level 0))
  4250. (save-excursion
  4251. ;; We first only copy, in case something goes wrong
  4252. ;; we need to protect this-command, to avoid kill-region sets it,
  4253. ;; which would lead to duplication of subtrees
  4254. (let (this-command) (org-copy-subtree))
  4255. (set-buffer buffer)
  4256. ;; Enforce org-mode for the archive buffer
  4257. (if (not (org-mode-p))
  4258. ;; Force the mode for future visits.
  4259. (let ((org-insert-mode-line-in-empty-file t))
  4260. (call-interactively 'org-mode)))
  4261. (when newfile-p
  4262. (goto-char (point-max))
  4263. (insert (format "\nArchived entries from file %s\n\n"
  4264. (buffer-file-name this-buffer))))
  4265. ;; Force the TODO keywords of the original buffer
  4266. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  4267. (org-todo-keywords tr-org-todo-keywords)
  4268. (org-todo-interpretation tr-org-todo-interpretation)
  4269. (org-done-string tr-org-done-string)
  4270. (org-todo-regexp tr-org-todo-regexp)
  4271. (org-todo-line-regexp tr-org-todo-line-regexp))
  4272. (goto-char (point-min))
  4273. (if heading
  4274. (progn
  4275. (if (re-search-forward
  4276. (concat "\\(^\\|\r\\)"
  4277. (regexp-quote heading) "[ \t]*\\($\\|\r\\)")
  4278. nil t)
  4279. (goto-char (match-end 0))
  4280. ;; Heading not found, just insert it at the end
  4281. (goto-char (point-max))
  4282. (or (bolp) (insert "\n"))
  4283. (insert "\n" heading "\n")
  4284. (end-of-line 0))
  4285. ;; Make the subtree visible
  4286. (show-subtree)
  4287. (org-end-of-subtree t)
  4288. (skip-chars-backward " \t\r\n")
  4289. (and (looking-at "[ \t\r\n]*")
  4290. (replace-match "\n\n")))
  4291. ;; No specific heading, just go to end of file.
  4292. (goto-char (point-max)) (insert "\n"))
  4293. ;; Paste
  4294. (org-paste-subtree (1+ level))
  4295. ;; Mark the entry as done, i.e. set to last work in org-todo-keywords
  4296. (if org-archive-mark-done
  4297. (org-todo (length org-todo-keywords)))
  4298. ;; Move cursor to right after the TODO keyword
  4299. (when org-archive-stamp-time
  4300. (beginning-of-line 1)
  4301. (looking-at org-todo-line-regexp)
  4302. (goto-char (or (match-end 2) (match-beginning 3)))
  4303. (insert "(" (format-time-string (cdr org-time-stamp-formats)
  4304. (org-current-time))
  4305. ")"))
  4306. ;; Save the buffer, if it is not the same buffer.
  4307. (if (not (eq this-buffer buffer)) (save-buffer))))
  4308. ;; Here we are back in the original buffer. Everything seems to have
  4309. ;; worked. So now cut the tree and finish up.
  4310. (let (this-command) (org-cut-subtree))
  4311. (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
  4312. (message "Subtree archived %s"
  4313. (if (eq this-buffer buffer)
  4314. (concat "under heading: " heading)
  4315. (concat "in file: " (abbreviate-file-name file)))))))
  4316. (defun org-archive-all-done (&optional tag)
  4317. "Archive sublevels of the current tree without open TODO items.
  4318. If the cursor is not on a headline, try all level 1 trees. If
  4319. it is on a headline, try all direct children.
  4320. When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
  4321. (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
  4322. (rea (concat ".*:" org-archive-tag ":"))
  4323. (begm (make-marker))
  4324. (endm (make-marker))
  4325. (question (if tag "Set ARCHIVE tag (no open TODO items)? "
  4326. "Move subtree to archive (no open TODO items)? "))
  4327. beg end (cntarch 0))
  4328. (if (org-on-heading-p)
  4329. (progn
  4330. (setq re1 (concat "^" (regexp-quote
  4331. (make-string
  4332. (1+ (- (match-end 0) (match-beginning 0)))
  4333. ?*))
  4334. " "))
  4335. (move-marker begm (point))
  4336. (move-marker endm (org-end-of-subtree t)))
  4337. (setq re1 "^* ")
  4338. (move-marker begm (point-min))
  4339. (move-marker endm (point-max)))
  4340. (save-excursion
  4341. (goto-char begm)
  4342. (while (re-search-forward re1 endm t)
  4343. (setq beg (match-beginning 0)
  4344. end (save-excursion (org-end-of-subtree t) (point)))
  4345. (goto-char beg)
  4346. (if (re-search-forward re end t)
  4347. (goto-char end)
  4348. (goto-char beg)
  4349. (if (and (or (not tag) (not (looking-at rea)))
  4350. (y-or-n-p question))
  4351. (progn
  4352. (if tag
  4353. (org-toggle-tag org-archive-tag 'on)
  4354. (org-archive-subtree))
  4355. (setq cntarch (1+ cntarch)))
  4356. (goto-char end)))))
  4357. (message "%d trees archived" cntarch)))
  4358. (defun org-cycle-hide-archived-subtrees (state)
  4359. "Re-hide all archived subtrees after a visibility state change."
  4360. (when (and (not org-cycle-open-archived-trees)
  4361. (not (memq state '(overview folded))))
  4362. (save-excursion
  4363. (let* ((globalp (memq state '(contents all)))
  4364. (beg (if globalp (point-min) (point)))
  4365. (end (if globalp (point-max) (org-end-of-subtree t))))
  4366. (org-hide-archived-subtrees beg end)
  4367. (goto-char beg)
  4368. (if (looking-at (concat ".*:" org-archive-tag ":"))
  4369. (message (substitute-command-keys
  4370. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  4371. (defun org-force-cycle-archived ()
  4372. "Cycle subtree even if it is archived."
  4373. (interactive)
  4374. (setq this-command 'org-cycle)
  4375. (let ((org-cycle-open-archived-trees t))
  4376. (call-interactively 'org-cycle)))
  4377. (defun org-hide-archived-subtrees (beg end)
  4378. "Re-hide all archived subtrees after a visibility state change."
  4379. (save-excursion
  4380. (let* ((re (concat ":" org-archive-tag ":")))
  4381. (goto-char beg)
  4382. (while (re-search-forward re end t)
  4383. (and (org-on-heading-p) (hide-subtree))
  4384. (org-end-of-subtree t)))))
  4385. (defun org-toggle-tag (tag &optional onoff)
  4386. "Toggle the tag TAG for the current line.
  4387. If ONOFF is `on' or `off', don't toggle but set to this state."
  4388. (unless (org-on-heading-p) (error "Not on headling"))
  4389. (let (res current)
  4390. (save-excursion
  4391. (beginning-of-line)
  4392. (if (re-search-forward "[ \t]:\\([a-zA-Z0-9_@:]+\\):[ \t]*$"
  4393. (point-at-eol) t)
  4394. (progn
  4395. (setq current (match-string 1))
  4396. (replace-match ""))
  4397. (setq current ""))
  4398. (setq current (nreverse (org-split-string current ":")))
  4399. (cond
  4400. ((eq onoff 'on)
  4401. (setq res t)
  4402. (or (member tag current) (push tag current)))
  4403. ((eq onoff 'off)
  4404. (or (not (member tag current)) (setq current (delete tag current))))
  4405. (t (if (member tag current)
  4406. (setq current (delete tag current))
  4407. (setq res t)
  4408. (push tag current))))
  4409. (end-of-line 1)
  4410. (when current
  4411. (insert " :" (mapconcat 'identity (nreverse current) ":") ":"))
  4412. (org-set-tags nil t))
  4413. res))
  4414. (defun org-toggle-archive-tag (&optional arg)
  4415. "Toggle the archive tag for the current headline.
  4416. With prefix ARG, check all children of current headline and offer tagging
  4417. the children that do not contain any open TODO items."
  4418. (interactive "P")
  4419. (if arg
  4420. (org-archive-all-done 'tag)
  4421. (let (set)
  4422. (save-excursion
  4423. (org-back-to-heading t)
  4424. (setq set (org-toggle-tag org-archive-tag))
  4425. (when set (hide-subtree)))
  4426. (and set (beginning-of-line 1))
  4427. (message "Subtree %s" (if set "archived" "unarchived")))))
  4428. (defvar org-agenda-multi nil) ; dynammically scoped
  4429. (defvar org-agenda-buffer-name "*Org Agenda*")
  4430. (defun org-prepare-agenda ()
  4431. (if org-agenda-multi
  4432. (progn
  4433. (setq buffer-read-only nil)
  4434. (goto-char (point-max))
  4435. (unless (= (point) 1)
  4436. (insert "\n" (make-string (window-width) ?=) "\n"))
  4437. (narrow-to-region (point) (point-max)))
  4438. (org-agenda-maybe-reset-markers 'force)
  4439. (org-prepare-agenda-buffers (org-agenda-files))
  4440. (unless (equal (current-buffer) (get-buffer org-agenda-buffer-name))
  4441. (delete-other-windows)
  4442. (switch-to-buffer-other-window
  4443. (get-buffer-create org-agenda-buffer-name)))
  4444. (setq buffer-read-only nil)
  4445. (erase-buffer)
  4446. (org-agenda-mode))
  4447. (setq buffer-read-only nil))
  4448. (defun org-finalize-agenda ()
  4449. "Finishing touch for the agenda buffer, called just before displaying it."
  4450. (unless org-agenda-multi
  4451. (org-agenda-align-tags)
  4452. (save-excursion
  4453. (let ((buffer-read-only))
  4454. (goto-char (point-min))
  4455. (while (org-activate-bracket-links (point-max))
  4456. (add-text-properties (match-beginning 0) (match-end 0)
  4457. '(face org-link))))
  4458. (run-hooks 'org-finalize-agenda-hook))))
  4459. (defun org-prepare-agenda-buffers (files)
  4460. "Create buffers for all agenda files, protect archived trees and comments."
  4461. (interactive)
  4462. (let ((pa '(:org-archived t))
  4463. (pc '(:org-comment t))
  4464. (pall '(:org-archived t :org-comment t))
  4465. (rea (concat ":" org-archive-tag ":"))
  4466. bmp file re)
  4467. (save-excursion
  4468. (while (setq file (pop files))
  4469. (org-check-agenda-file file)
  4470. (set-buffer (org-get-agenda-file-buffer file))
  4471. (widen)
  4472. (setq bmp (buffer-modified-p))
  4473. (save-excursion
  4474. (remove-text-properties (point-min) (point-max) pall)
  4475. (when org-agenda-skip-archived-trees
  4476. (goto-char (point-min))
  4477. (while (re-search-forward rea nil t)
  4478. (if (org-on-heading-p)
  4479. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  4480. (goto-char (point-min))
  4481. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  4482. (while (re-search-forward re nil t)
  4483. (add-text-properties
  4484. (match-beginning 0) (org-end-of-subtree t) pc)))
  4485. (set-buffer-modified-p bmp)))))
  4486. (defun org-agenda-skip ()
  4487. "Throw to `:skip' in places that should be skipped."
  4488. (let ((p (point-at-bol)))
  4489. (and org-agenda-skip-archived-trees
  4490. (get-text-property p :org-archived)
  4491. (org-end-of-subtree t)
  4492. (throw :skip t))
  4493. (and (get-text-property p :org-comment)
  4494. (org-end-of-subtree t)
  4495. (throw :skip t))
  4496. (if (equal (char-after p) ?#) (throw :skip t))))
  4497. (defun org-agenda-toggle-archive-tag ()
  4498. "Toggle the archive tag for the current entry."
  4499. (interactive)
  4500. (org-agenda-check-no-diary)
  4501. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  4502. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  4503. (org-agenda-error)))
  4504. (buffer (marker-buffer hdmarker))
  4505. (pos (marker-position hdmarker))
  4506. (buffer-read-only nil)
  4507. newhead)
  4508. (with-current-buffer buffer
  4509. (widen)
  4510. (goto-char pos)
  4511. (org-show-hierarchy-above 'agenda)
  4512. (save-excursion
  4513. (and (outline-next-heading)
  4514. (org-flag-heading nil))) ; show the next heading
  4515. (call-interactively 'org-toggle-archive-tag)
  4516. (end-of-line 1)
  4517. (setq newhead (org-get-heading)))
  4518. (org-agenda-change-all-lines newhead hdmarker)
  4519. (beginning-of-line 1)))
  4520. ;;; Dynamic blocks
  4521. (defun org-find-dblock (name)
  4522. "Find the first dynamic block with name NAME in the buffer.
  4523. If not found, stay at current position and return nil."
  4524. (let (pos)
  4525. (save-excursion
  4526. (goto-char (point-min))
  4527. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  4528. nil t)
  4529. (match-beginning 0))))
  4530. (if pos (goto-char pos))
  4531. pos))
  4532. (defconst org-dblock-start-re
  4533. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  4534. "Matches the startline of a dynamic block, with parameters.")
  4535. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  4536. "Matches the end of a dyhamic block.")
  4537. (defun org-create-dblock (plist)
  4538. "Create a dynamic block section, with parameters taken from PLIST.
  4539. PLIST must containe a :name entry which is used as name of the block."
  4540. (unless (bolp) (newline))
  4541. (let ((name (plist-get plist :name)))
  4542. (insert "#+BEGIN: " name)
  4543. (while plist
  4544. (if (eq (car plist) :name)
  4545. (setq plist (cddr plist))
  4546. (insert " " (prin1-to-string (pop plist)))))
  4547. (insert "\n\n#+END:\n")
  4548. (beginning-of-line -2)))
  4549. (defun org-prepare-dblock ()
  4550. "Prepare dynamic block for refresh.
  4551. This empties the block, puts the cursor at the insert position and returns
  4552. the property list including an extra property :name with the block name."
  4553. (unless (looking-at org-dblock-start-re)
  4554. (error "Not at a dynamic block"))
  4555. (let* ((begdel (1+ (match-end 0)))
  4556. (name (match-string 1))
  4557. (params (append (list :name name)
  4558. (read (concat "(" (match-string 3) ")")))))
  4559. (unless (re-search-forward org-dblock-end-re nil t)
  4560. (error "Dynamic block not terminated"))
  4561. (delete-region begdel (match-beginning 0))
  4562. (goto-char begdel)
  4563. (open-line 1)
  4564. params))
  4565. (defun org-map-dblocks (&optional command)
  4566. "Apply COMMAND to all dynamic blocks in the current buffer.
  4567. If COMMAND is not given, use `org-update-dblock'."
  4568. (let ((cmd (or command 'org-update-dblock))
  4569. pos)
  4570. (save-excursion
  4571. (goto-char (point-min))
  4572. (while (re-search-forward org-dblock-start-re nil t)
  4573. (goto-char (setq pos (match-beginning 0)))
  4574. (condition-case nil
  4575. (funcall cmd)
  4576. (error (message "Error during update of dynamic block")))
  4577. (goto-char pos)
  4578. (unless (re-search-forward org-dblock-end-re nil t)
  4579. (error "Dynamic block not terminated"))))))
  4580. (defun org-dblock-update (&optional arg)
  4581. "User command for updating dynamic blocks.
  4582. Update the dynamic block at point. With prefix ARG, update all dynamic
  4583. blocks in the buffer."
  4584. (interactive "P")
  4585. (if arg
  4586. (org-update-all-dblocks)
  4587. (or (looking-at org-dblock-start-re)
  4588. (org-beginning-of-dblock))
  4589. (org-update-dblock)))
  4590. (defun org-update-dblock ()
  4591. "Update the dynamic block at point
  4592. This means to empty the block, parse for parameters and then call
  4593. the correct writing function."
  4594. (let* ((pos (point))
  4595. (params (org-prepare-dblock))
  4596. (name (plist-get params :name))
  4597. (cmd (intern (concat "org-dblock-write:" name))))
  4598. (funcall cmd params)
  4599. (goto-char pos)))
  4600. (defun org-beginning-of-dblock ()
  4601. "Find the beginning of the dynamic block at point.
  4602. Error if there is no scuh block at point."
  4603. (let ((pos (point))
  4604. beg)
  4605. (end-of-line 1)
  4606. (if (and (re-search-backward org-dblock-start-re nil t)
  4607. (setq beg (match-beginning 0))
  4608. (re-search-forward org-dblock-end-re nil t)
  4609. (> (match-end 0) pos))
  4610. (goto-char beg)
  4611. (goto-char pos)
  4612. (error "Not in a dynamic block"))))
  4613. (defun org-update-all-dblocks ()
  4614. "Update all dynamic blocks in the buffer.
  4615. This function can be used in a hook."
  4616. (when (org-mode-p)
  4617. (org-map-dblocks 'org-update-dblock)))
  4618. ;;; Completion
  4619. (defun org-complete (&optional arg)
  4620. "Perform completion on word at point.
  4621. At the beginning of a headline, this completes TODO keywords as given in
  4622. `org-todo-keywords'.
  4623. If the current word is preceded by a backslash, completes the TeX symbols
  4624. that are supported for HTML support.
  4625. If the current word is preceded by \"#+\", completes special words for
  4626. setting file options.
  4627. In the line after \"#+STARTUP:, complete valid keywords.\"
  4628. At all other locations, this simply calls `ispell-complete-word'."
  4629. (interactive "P")
  4630. (catch 'exit
  4631. (let* ((end (point))
  4632. (beg1 (save-excursion
  4633. (skip-chars-backward "a-zA-Z_@0-9")
  4634. (point)))
  4635. (beg (save-excursion
  4636. (skip-chars-backward "a-zA-Z0-9_:$")
  4637. (point)))
  4638. (confirm (lambda (x) (stringp (car x))))
  4639. (camel (equal (char-before beg) ?*))
  4640. (tag (equal (char-before beg1) ?:))
  4641. (texp (equal (char-before beg) ?\\))
  4642. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  4643. beg)
  4644. "#+"))
  4645. (startup (string-match "^#\\+STARTUP:.*"
  4646. (buffer-substring (point-at-bol) (point))))
  4647. (completion-ignore-case opt)
  4648. (type nil)
  4649. (tbl nil)
  4650. (table (cond
  4651. (opt
  4652. (setq type :opt)
  4653. (mapcar (lambda (x)
  4654. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  4655. (cons (match-string 2 x) (match-string 1 x)))
  4656. (org-split-string (org-get-current-options) "\n")))
  4657. (startup
  4658. (setq type :startup)
  4659. org-startup-options)
  4660. (texp
  4661. (setq type :tex)
  4662. org-html-entities)
  4663. ((string-match "\\`\\*+[ \t]*\\'"
  4664. (buffer-substring (point-at-bol) beg))
  4665. (setq type :todo)
  4666. (mapcar 'list org-todo-keywords))
  4667. (camel
  4668. (setq type :camel)
  4669. (save-excursion
  4670. (goto-char (point-min))
  4671. (while (re-search-forward org-todo-line-regexp nil t)
  4672. (push (list
  4673. (if org-file-link-context-use-camel-case
  4674. (org-make-org-heading-camel (match-string 3) t)
  4675. (org-make-org-heading-search-string
  4676. (match-string 3) t)))
  4677. tbl)))
  4678. tbl)
  4679. (tag (setq type :tag beg beg1)
  4680. (or org-tag-alist (org-get-buffer-tags)))
  4681. (t (progn (ispell-complete-word arg) (throw 'exit nil)))))
  4682. (pattern (buffer-substring-no-properties beg end))
  4683. (completion (try-completion pattern table confirm)))
  4684. (cond ((eq completion t)
  4685. (if (equal type :opt)
  4686. (insert (substring (cdr (assoc (upcase pattern) table))
  4687. (length pattern)))))
  4688. ((null completion)
  4689. (message "Can't find completion for \"%s\"" pattern)
  4690. (ding))
  4691. ((not (string= pattern completion))
  4692. (delete-region beg end)
  4693. (if (string-match " +$" completion)
  4694. (setq completion (replace-match "" t t completion)))
  4695. (insert completion)
  4696. (if (get-buffer-window "*Completions*")
  4697. (delete-window (get-buffer-window "*Completions*")))
  4698. (if (assoc completion table)
  4699. (if (eq type :todo) (insert " ")
  4700. (if (eq type :tag) (insert ":"))))
  4701. (if (and (equal type :opt) (assoc completion table))
  4702. (message "%s" (substitute-command-keys
  4703. "Press \\[org-complete] again to insert example settings"))))
  4704. (t
  4705. (message "Making completion list...")
  4706. (let ((list (sort (all-completions pattern table confirm)
  4707. 'string<)))
  4708. (with-output-to-temp-buffer "*Completions*"
  4709. (condition-case nil
  4710. ;; Protection needed for XEmacs and emacs 21
  4711. (display-completion-list list pattern)
  4712. (error (display-completion-list list)))))
  4713. (message "Making completion list...%s" "done"))))))
  4714. ;;; Comments, TODO and DEADLINE
  4715. (defun org-toggle-comment ()
  4716. "Change the COMMENT state of an entry."
  4717. (interactive)
  4718. (save-excursion
  4719. (org-back-to-heading)
  4720. (if (looking-at (concat outline-regexp
  4721. "\\( +\\<" org-comment-string "\\>\\)"))
  4722. (replace-match "" t t nil 1)
  4723. (if (looking-at outline-regexp)
  4724. (progn
  4725. (goto-char (match-end 0))
  4726. (insert " " org-comment-string))))))
  4727. (defvar org-last-todo-state-is-todo nil
  4728. "This is non-nil when the last TODO state change led to a TODO state.
  4729. If the last change removed the TODO tag or switched to DONE, then
  4730. this is nil.")
  4731. (defun org-todo (&optional arg)
  4732. "Change the TODO state of an item.
  4733. The state of an item is given by a keyword at the start of the heading,
  4734. like
  4735. *** TODO Write paper
  4736. *** DONE Call mom
  4737. The different keywords are specified in the variable `org-todo-keywords'.
  4738. By default the available states are \"TODO\" and \"DONE\".
  4739. So for this example: when the item starts with TODO, it is changed to DONE.
  4740. When it starts with DONE, the DONE is removed. And when neither TODO nor
  4741. DONE are present, add TODO at the beginning of the heading.
  4742. With prefix arg, use completion to determine the new state. With numeric
  4743. prefix arg, switch to that state."
  4744. (interactive "P")
  4745. (save-excursion
  4746. (org-back-to-heading)
  4747. (if (looking-at outline-regexp) (goto-char (match-end 0)))
  4748. (or (looking-at (concat " +" org-todo-regexp " *"))
  4749. (looking-at " *"))
  4750. (let* ((this (match-string 1))
  4751. (completion-ignore-case t)
  4752. (member (member this org-todo-keywords))
  4753. (tail (cdr member))
  4754. (state (cond
  4755. ((equal arg '(4))
  4756. ;; Read a state with completion
  4757. (completing-read "State: " (mapcar (lambda(x) (list x))
  4758. org-todo-keywords)
  4759. nil t))
  4760. ((eq arg 'right)
  4761. (if this
  4762. (if tail (car tail) nil)
  4763. (car org-todo-keywords)))
  4764. ((eq arg 'left)
  4765. (if (equal member org-todo-keywords)
  4766. nil
  4767. (if this
  4768. (nth (- (length org-todo-keywords) (length tail) 2)
  4769. org-todo-keywords)
  4770. org-done-string)))
  4771. (arg
  4772. ;; user requests a specific state
  4773. (nth (1- (prefix-numeric-value arg))
  4774. org-todo-keywords))
  4775. ((null member) (car org-todo-keywords))
  4776. ((null tail) nil) ;; -> first entry
  4777. ((eq org-todo-interpretation 'sequence)
  4778. (car tail))
  4779. ((memq org-todo-interpretation '(type priority))
  4780. (if (eq this-command last-command)
  4781. (car tail)
  4782. (if (> (length tail) 0) org-done-string nil)))
  4783. (t nil)))
  4784. (next (if state (concat " " state " ") " ")))
  4785. (replace-match next t t)
  4786. (setq org-last-todo-state-is-todo
  4787. (not (equal state org-done-string)))
  4788. (when org-log-done
  4789. (if (equal state org-done-string)
  4790. (org-add-planning-info 'closed (current-time) 'scheduled)
  4791. (if (not this)
  4792. (org-add-planning-info nil nil 'closed))))
  4793. ;; Fixup tag positioning
  4794. (and org-auto-align-tags (org-set-tags nil t))
  4795. (run-hooks 'org-after-todo-state-change-hook)))
  4796. ;; Fixup cursor location if close to the keyword
  4797. (if (and (outline-on-heading-p)
  4798. (not (bolp))
  4799. (save-excursion (beginning-of-line 1)
  4800. (looking-at org-todo-line-regexp))
  4801. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  4802. (progn
  4803. (goto-char (or (match-end 2) (match-end 1)))
  4804. (just-one-space))))
  4805. (defun org-show-todo-tree (arg)
  4806. "Make a compact tree which shows all headlines marked with TODO.
  4807. The tree will show the lines where the regexp matches, and all higher
  4808. headlines above the match.
  4809. With \\[universal-argument] prefix, also show the DONE entries.
  4810. With a numeric prefix N, construct a sparse tree for the Nth element
  4811. of `org-todo-keywords'."
  4812. (interactive "P")
  4813. (let ((case-fold-search nil)
  4814. (kwd-re
  4815. (cond ((null arg) org-not-done-regexp)
  4816. ((equal arg '(4)) org-todo-regexp)
  4817. ((<= (prefix-numeric-value arg) (length org-todo-keywords))
  4818. (regexp-quote (nth (1- (prefix-numeric-value arg))
  4819. org-todo-keywords)))
  4820. (t (error "Invalid prefix argument: %s" arg)))))
  4821. (message "%d TODO entries found"
  4822. (org-occur (concat "^" outline-regexp " +" kwd-re )))))
  4823. (defun org-deadline ()
  4824. "Insert the DEADLINE: string to make a deadline.
  4825. A timestamp is also inserted - use \\[org-timestamp-up] and \\[org-timestamp-down]
  4826. to modify it to the correct date."
  4827. (interactive)
  4828. (org-add-planning-info 'deadline nil 'closed))
  4829. (defun org-schedule ()
  4830. "Insert the SCHEDULED: string to schedule a TODO item.
  4831. A timestamp is also inserted - use \\[org-timestamp-up] and \\[org-timestamp-down]
  4832. to modify it to the correct date."
  4833. (interactive)
  4834. (org-add-planning-info 'scheduled nil 'closed))
  4835. (defun org-add-planning-info (what &optional time &rest remove)
  4836. "Insert new timestamp with keyword in the line directly after the headline.
  4837. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  4838. If non is given, the user is prompted for a date.
  4839. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  4840. be removed."
  4841. (interactive)
  4842. (when what (setq time (or time (org-read-date nil 'to-time))))
  4843. (when (and org-insert-labeled-timestamps-at-point
  4844. (member what '(scheduled deadline)))
  4845. (insert
  4846. (if (eq what 'scheduled) org-scheduled-string org-deadline-string)
  4847. " "
  4848. (format-time-string (car org-time-stamp-formats) time))
  4849. (setq what nil))
  4850. (save-excursion
  4851. (let (col list elt (buffer-invisibility-spec nil) ts)
  4852. (org-back-to-heading t)
  4853. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  4854. (goto-char (match-end 1))
  4855. (setq col (current-column))
  4856. (goto-char (1+ (match-end 0)))
  4857. (if (and (not (looking-at outline-regexp))
  4858. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  4859. "[^\r\n]*"))
  4860. (not (equal (match-string 1) org-clock-string)))
  4861. (narrow-to-region (match-beginning 0) (match-end 0))
  4862. (insert "\n")
  4863. (backward-char 1)
  4864. (narrow-to-region (point) (point))
  4865. (indent-to-column col))
  4866. ;; Check if we have to remove something.
  4867. (setq list (cons what remove))
  4868. (while list
  4869. (setq elt (pop list))
  4870. (goto-char (point-min))
  4871. (when (or (and (eq elt 'scheduled)
  4872. (re-search-forward org-scheduled-time-regexp nil t))
  4873. (and (eq elt 'deadline)
  4874. (re-search-forward org-deadline-time-regexp nil t))
  4875. (and (eq elt 'closed)
  4876. (re-search-forward org-closed-time-regexp nil t)))
  4877. (replace-match "")
  4878. (if (looking-at " +") (replace-match ""))))
  4879. (goto-char (point-max))
  4880. (when what
  4881. (insert
  4882. (if (not (equal (char-before) ?\ )) " " "")
  4883. (cond ((eq what 'scheduled) org-scheduled-string)
  4884. ((eq what 'deadline) org-deadline-string)
  4885. ((eq what 'closed) org-closed-string))
  4886. " ")
  4887. (insert
  4888. (setq ts
  4889. (format-time-string
  4890. (if (eq what 'closed)
  4891. (concat "[" (substring (cdr org-time-stamp-formats) 1 -1) "]")
  4892. (car org-time-stamp-formats))
  4893. time)))
  4894. (end-of-line 1)
  4895. (org-add-log-maybe 'done))
  4896. (goto-char (point-min))
  4897. (widen)
  4898. (if (looking-at "[ \t]+\r?\n")
  4899. (replace-match ""))
  4900. ts)))
  4901. (defvar org-log-note-marker (make-marker))
  4902. (defvar org-log-note-purpose nil)
  4903. (defvar org-log-note-window-configuration nil)
  4904. (defun org-add-log-maybe (&optional purpose)
  4905. (when (and (listp org-log-done)
  4906. (memq purpose org-log-done))
  4907. (move-marker org-log-note-marker (point))
  4908. (setq org-log-note-purpose purpose)
  4909. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  4910. (defun org-add-log-note (&optional purpose)
  4911. "Pop up a window for taking a note, and add this note later at point."
  4912. (remove-hook 'post-command-hook 'org-add-log-note)
  4913. (setq org-log-note-window-configuration (current-window-configuration))
  4914. (delete-other-windows)
  4915. (switch-to-buffer (marker-buffer org-log-note-marker))
  4916. (goto-char org-log-note-marker)
  4917. (switch-to-buffer-other-window "*Org Note*")
  4918. (erase-buffer)
  4919. (org-mode)
  4920. (insert (format "# Insert note for %s, finish with C-c C-c.\n\n"
  4921. (cond
  4922. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  4923. ((eq org-log-note-purpose 'done) "closed todo item")
  4924. (t (error "This should not happen")))))
  4925. (org-set-local 'org-finish-function 'org-store-log-note))
  4926. (defun org-store-log-note ()
  4927. "Finish taking a log note, and insert it to where it belongs."
  4928. (let ((txt (buffer-string))
  4929. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  4930. lines ind)
  4931. (kill-buffer (current-buffer))
  4932. (if (string-match "^#.*\n[ \t\\n]*" txt)
  4933. (setq txt (replace-match "" t t txt)))
  4934. (when (string-match "\\S-" txt)
  4935. (if (string-match "\\s-+\\'" txt)
  4936. (setq txt (replace-match "" t t txt)))
  4937. (setq lines (org-split-string txt "\n"))
  4938. (and note (string-match "\\S-" note) (push note lines))
  4939. (save-excursion
  4940. (set-buffer (marker-buffer org-log-note-marker))
  4941. (save-excursion
  4942. (goto-char org-log-note-marker)
  4943. (if (not (bolp)) (newline))
  4944. (indent-relative t)
  4945. (setq ind (concat (buffer-substring (point-at-bol) (point)) " "))
  4946. (insert "- " (pop lines))
  4947. (while lines
  4948. (insert "\n" ind (pop lines))))))
  4949. (set-window-configuration org-log-note-window-configuration)))
  4950. (defvar org-occur-highlights nil)
  4951. (make-variable-buffer-local 'org-occur-highlights)
  4952. (defun org-occur (regexp &optional keep-previous callback)
  4953. "Make a compact tree which shows all matches of REGEXP.
  4954. The tree will show the lines where the regexp matches, and all higher
  4955. headlines above the match. It will also show the heading after the match,
  4956. to make sure editing the matching entry is easy.
  4957. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  4958. call to `org-occur' will be kept, to allow stacking of calls to this
  4959. command.
  4960. If CALLBACK is non-nil, it is a function which is called to confirm
  4961. that the match should indeed be shown."
  4962. (interactive "sRegexp: \nP")
  4963. (or keep-previous (org-remove-occur-highlights nil nil t))
  4964. (let ((cnt 0))
  4965. (save-excursion
  4966. (goto-char (point-min))
  4967. (if (or (not keep-previous) ; do not want to keep
  4968. (not org-occur-highlights)) ; no previous matches
  4969. ;; hide everything
  4970. (org-overview))
  4971. (while (re-search-forward regexp nil t)
  4972. (when (or (not callback)
  4973. (save-match-data (funcall callback)))
  4974. (setq cnt (1+ cnt))
  4975. (org-highlight-new-match (match-beginning 0) (match-end 0))
  4976. (org-show-hierarchy-above 'occur-tree))))
  4977. (when org-remove-highlights-with-change
  4978. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  4979. nil 'local))
  4980. (unless org-sparse-tree-open-archived-trees
  4981. (org-hide-archived-subtrees (point-min) (point-max)))
  4982. (run-hooks 'org-occur-hook)
  4983. (if (interactive-p)
  4984. (message "%d match(es) for regexp %s" cnt regexp))
  4985. cnt))
  4986. (defun org-show-hierarchy-above (&optional context)
  4987. "Make sure point and the headings hierarchy above is visible."
  4988. (catch 'exit
  4989. (if (org-on-heading-p t)
  4990. (org-flag-heading nil) ; only show the heading
  4991. (and (or (org-invisible-p) (org-invisible-p2))
  4992. (org-show-hidden-entry))) ; show entire entry
  4993. (save-excursion
  4994. (and (org-get-alist-option org-show-following-heading context)
  4995. (outline-next-heading)
  4996. (org-flag-heading nil))) ; show the next heading
  4997. (when (org-get-alist-option org-show-hierarchy-above context)
  4998. (save-excursion ; show all higher headings
  4999. (while (and (condition-case nil
  5000. (progn (org-up-heading-all 1) t)
  5001. (error nil))
  5002. (not (bobp)))
  5003. (org-flag-heading nil))))))
  5004. ;; Overlay compatibility functions
  5005. (defun org-make-overlay (beg end &optional buffer)
  5006. (if (featurep 'xemacs)
  5007. (make-extent beg end buffer)
  5008. (make-overlay beg end buffer)))
  5009. (defun org-delete-overlay (ovl)
  5010. (if (featurep 'xemacs) (delete-extent ovl) (delete-overlay ovl)))
  5011. (defun org-detatch-overlay (ovl)
  5012. (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
  5013. (defun org-move-overlay (ovl beg end &optional buffer)
  5014. (if (featurep 'xemacs)
  5015. (set-extent-endpoints ovl beg end buffer)
  5016. (move-overlay ovl beg end buffer)))
  5017. (defun org-overlay-put (ovl prop value)
  5018. (if (featurep 'xemacs)
  5019. (set-extent-property ovl prop value)
  5020. (overlay-put ovl prop value)))
  5021. (defun org-overlays-at (pos)
  5022. (if (featurep 'xemacs) (extents-at pos) (overlays-at pos)))
  5023. (defun org-overlay-start (o)
  5024. (if (featurep 'xemacs) (extent-start-position o) (overlay-start o)))
  5025. (defun org-overlay-end (o)
  5026. (if (featurep 'xemacs) (extent-end-position o) (overlay-end o)))
  5027. (defun org-highlight-new-match (beg end)
  5028. "Highlight from BEG to END and mark the highlight is an occur headline."
  5029. (let ((ov (org-make-overlay beg end)))
  5030. (org-overlay-put ov 'face 'secondary-selection)
  5031. (push ov org-occur-highlights)))
  5032. (defvar org-inhibit-highlight-removal nil)
  5033. (defun org-remove-occur-highlights (&optional beg end noremove)
  5034. "Remove the occur highlights from the buffer.
  5035. BEG and END are ignored. If NOREMOVE is nil, remove this function
  5036. from the `before-change-functions' in the current buffer."
  5037. (interactive)
  5038. (unless org-inhibit-highlight-removal
  5039. (mapc 'org-delete-overlay org-occur-highlights)
  5040. (setq org-occur-highlights nil)
  5041. (unless noremove
  5042. (remove-hook 'before-change-functions
  5043. 'org-remove-occur-highlights 'local))))
  5044. ;;; Priorities
  5045. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z]\\)\\] ?\\)"
  5046. "Regular expression matching the priority indicator.")
  5047. (defvar org-remove-priority-next-time nil)
  5048. (defun org-priority-up ()
  5049. "Increase the priority of the current item."
  5050. (interactive)
  5051. (org-priority 'up))
  5052. (defun org-priority-down ()
  5053. "Decrease the priority of the current item."
  5054. (interactive)
  5055. (org-priority 'down))
  5056. (defun org-priority (&optional action)
  5057. "Change the priority of an item by ARG.
  5058. ACTION can be set, up, or down."
  5059. (interactive)
  5060. (setq action (or action 'set))
  5061. (let (current new news have remove)
  5062. (save-excursion
  5063. (org-back-to-heading)
  5064. (if (looking-at org-priority-regexp)
  5065. (setq current (string-to-char (match-string 2))
  5066. have t)
  5067. (setq current org-default-priority))
  5068. (cond
  5069. ((eq action 'set)
  5070. (message "Priority A-%c, SPC to remove: " org-lowest-priority)
  5071. (setq new (read-char-exclusive))
  5072. (cond ((equal new ?\ ) (setq remove t))
  5073. ((or (< (upcase new) ?A) (> (upcase new) org-lowest-priority))
  5074. (error "Priority must be between `%c' and `%c'"
  5075. ?A org-lowest-priority))))
  5076. ((eq action 'up)
  5077. (setq new (1- current)))
  5078. ((eq action 'down)
  5079. (setq new (1+ current)))
  5080. (t (error "Invalid action")))
  5081. (setq new (min (max ?A (upcase new)) org-lowest-priority))
  5082. (setq news (format "%c" new))
  5083. (if have
  5084. (if remove
  5085. (replace-match "" t t nil 1)
  5086. (replace-match news t t nil 2))
  5087. (if remove
  5088. (error "No priority cookie found in line")
  5089. (looking-at org-todo-line-regexp)
  5090. (if (match-end 2)
  5091. (progn
  5092. (goto-char (match-end 2))
  5093. (insert " [#" news "]"))
  5094. (goto-char (match-beginning 3))
  5095. (insert "[#" news "] ")))))
  5096. (if remove
  5097. (message "Priority removed")
  5098. (message "Priority of current item set to %s" news))))
  5099. (defun org-get-priority (s)
  5100. "Find priority cookie and return priority."
  5101. (save-match-data
  5102. (if (not (string-match org-priority-regexp s))
  5103. (* 1000 (- org-lowest-priority org-default-priority))
  5104. (* 1000 (- org-lowest-priority
  5105. (string-to-char (match-string 2 s)))))))
  5106. ;;; Timestamps
  5107. (defvar org-last-changed-timestamp nil)
  5108. (defun org-time-stamp (arg)
  5109. "Prompt for a date/time and insert a time stamp.
  5110. If the user specifies a time like HH:MM, or if this command is called
  5111. with a prefix argument, the time stamp will contain date and time.
  5112. Otherwise, only the date will be included. All parts of a date not
  5113. specified by the user will be filled in from the current date/time.
  5114. So if you press just return without typing anything, the time stamp
  5115. will represent the current date/time. If there is already a timestamp
  5116. at the cursor, it will be modified."
  5117. (interactive "P")
  5118. (let ((fmt (if arg (cdr org-time-stamp-formats)
  5119. (car org-time-stamp-formats)))
  5120. (org-time-was-given nil)
  5121. time)
  5122. (cond
  5123. ((and (org-at-timestamp-p)
  5124. (eq last-command 'org-time-stamp)
  5125. (eq this-command 'org-time-stamp))
  5126. (insert "--")
  5127. (setq time (let ((this-command this-command))
  5128. (org-read-date arg 'totime)))
  5129. (if org-time-was-given (setq fmt (cdr org-time-stamp-formats)))
  5130. (insert (format-time-string fmt time)))
  5131. ((org-at-timestamp-p)
  5132. (setq time (let ((this-command this-command))
  5133. (org-read-date arg 'totime)))
  5134. (and (org-at-timestamp-p) (replace-match
  5135. (setq org-last-changed-timestamp
  5136. (format-time-string fmt time))
  5137. t t))
  5138. (message "Timestamp updated"))
  5139. (t
  5140. (setq time (let ((this-command this-command))
  5141. (org-read-date arg 'totime)))
  5142. (if org-time-was-given (setq fmt (cdr org-time-stamp-formats)))
  5143. (insert (format-time-string fmt time))))))
  5144. (defun org-time-stamp-inactive (&optional arg)
  5145. "Insert an inactive time stamp.
  5146. An inactive time stamp is enclosed in square brackets instead of angle
  5147. brackets. It is inactive in the sense that it does not trigger agenda entries,
  5148. does not link to the calendar and cannot be changed with the S-cursor keys.
  5149. So these are more for recording a certain time/date."
  5150. (interactive "P")
  5151. (let ((fmt (if arg (cdr org-time-stamp-formats)
  5152. (car org-time-stamp-formats)))
  5153. (org-time-was-given nil)
  5154. time)
  5155. (setq time (org-read-date arg 'totime))
  5156. (if org-time-was-given (setq fmt (cdr org-time-stamp-formats)))
  5157. (setq fmt (concat "[" (substring fmt 1 -1) "]"))
  5158. (insert (format-time-string fmt time))))
  5159. (defvar org-date-ovl (org-make-overlay 1 1))
  5160. (org-overlay-put org-date-ovl 'face 'org-warning)
  5161. (org-detatch-overlay org-date-ovl)
  5162. (defun org-read-date (&optional with-time to-time)
  5163. "Read a date and make things smooth for the user.
  5164. The prompt will suggest to enter an ISO date, but you can also enter anything
  5165. which will at least partially be understood by `parse-time-string'.
  5166. Unrecognized parts of the date will default to the current day, month, year,
  5167. hour and minute. For example,
  5168. 3-2-5 --> 2003-02-05
  5169. feb 15 --> currentyear-02-15
  5170. sep 12 9 --> 2009-09-12
  5171. 12:45 --> today 12:45
  5172. 22 sept 0:34 --> currentyear-09-22 0:34
  5173. 12 --> currentyear-currentmonth-12
  5174. Fri --> nearest Friday (today or later)
  5175. etc.
  5176. The function understands only English month and weekday abbreviations,
  5177. but this can be configured with the variables `parse-time-months' and
  5178. `parse-time-weekdays'.
  5179. While prompting, a calendar is popped up - you can also select the
  5180. date with the mouse (button 1). The calendar shows a period of three
  5181. months. To scroll it to other months, use the keys `>' and `<'.
  5182. If you don't like the calendar, turn it off with
  5183. \(setq org-popup-calendar-for-date-prompt nil)
  5184. With optional argument TO-TIME, the date will immediately be converted
  5185. to an internal time.
  5186. With an optional argument WITH-TIME, the prompt will suggest to also
  5187. insert a time. Note that when WITH-TIME is not set, you can still
  5188. enter a time, and this function will inform the calling routine about
  5189. this change. The calling routine may then choose to change the format
  5190. used to insert the time stamp into the buffer to include the time."
  5191. (require 'parse-time)
  5192. (let* ((org-time-stamp-rounding-minutes
  5193. (if (equal with-time '(16)) 0 org-time-stamp-rounding-minutes))
  5194. (ct (org-current-time))
  5195. (default-time
  5196. ;; Default time is either today, or, when entering a range,
  5197. ;; the range start.
  5198. (if (save-excursion
  5199. (re-search-backward
  5200. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  5201. (- (point) 20) t))
  5202. (apply
  5203. 'encode-time
  5204. (mapcar (lambda(x) (or x 0))
  5205. (parse-time-string (match-string 1))))
  5206. ct))
  5207. (calendar-move-hook nil)
  5208. (view-diary-entries-initially nil)
  5209. (view-calendar-holidays-initially nil)
  5210. (timestr (format-time-string
  5211. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") default-time))
  5212. (prompt (format "YYYY-MM-DD [%s]: " timestr))
  5213. ans ans1 ans2
  5214. second minute hour day month year tl wday wday1)
  5215. (if org-popup-calendar-for-date-prompt
  5216. (save-excursion
  5217. (save-window-excursion
  5218. (calendar)
  5219. (calendar-forward-day (- (time-to-days default-time)
  5220. (calendar-absolute-from-gregorian
  5221. (calendar-current-date))))
  5222. (org-eval-in-calendar nil)
  5223. (let* ((old-map (current-local-map))
  5224. (map (copy-keymap calendar-mode-map))
  5225. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  5226. (define-key map (kbd "RET") 'org-calendar-select)
  5227. (define-key map (if (featurep 'xemacs) [button1] [mouse-1])
  5228. 'org-calendar-select-mouse)
  5229. (define-key map (if (featurep 'xemacs) [button2] [mouse-2])
  5230. 'org-calendar-select-mouse)
  5231. (define-key minibuffer-local-map [(meta shift left)]
  5232. (lambda () (interactive)
  5233. (org-eval-in-calendar '(calendar-backward-month 1))))
  5234. (define-key minibuffer-local-map [(meta shift right)]
  5235. (lambda () (interactive)
  5236. (org-eval-in-calendar '(calendar-forward-month 1))))
  5237. (define-key minibuffer-local-map [(shift up)]
  5238. (lambda () (interactive)
  5239. (org-eval-in-calendar '(calendar-backward-week 1))))
  5240. (define-key minibuffer-local-map [(shift down)]
  5241. (lambda () (interactive)
  5242. (org-eval-in-calendar '(calendar-forward-week 1))))
  5243. (define-key minibuffer-local-map [(shift left)]
  5244. (lambda () (interactive)
  5245. (org-eval-in-calendar '(calendar-backward-day 1))))
  5246. (define-key minibuffer-local-map [(shift right)]
  5247. (lambda () (interactive)
  5248. (org-eval-in-calendar '(calendar-forward-day 1))))
  5249. (define-key minibuffer-local-map ">"
  5250. (lambda () (interactive)
  5251. (org-eval-in-calendar '(scroll-calendar-left 1))))
  5252. (define-key minibuffer-local-map "<"
  5253. (lambda () (interactive)
  5254. (org-eval-in-calendar '(scroll-calendar-right 1))))
  5255. (unwind-protect
  5256. (progn
  5257. (use-local-map map)
  5258. (setq ans (read-string prompt "" nil nil))
  5259. (if (not (string-match "\\S-" ans)) (setq ans nil))
  5260. (setq ans (or ans1 ans ans2)))
  5261. (use-local-map old-map)))))
  5262. ;; Naked prompt only
  5263. (setq ans (read-string prompt "" nil timestr)))
  5264. (org-detatch-overlay org-date-ovl)
  5265. (if (string-match
  5266. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  5267. (progn
  5268. (setq year (if (match-end 2)
  5269. (string-to-number (match-string 2 ans))
  5270. (string-to-number (format-time-string "%Y")))
  5271. month (string-to-number (match-string 3 ans))
  5272. day (string-to-number (match-string 4 ans)))
  5273. (if (< year 100) (setq year (+ 2000 year)))
  5274. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  5275. t nil ans))))
  5276. (setq tl (parse-time-string ans)
  5277. year (or (nth 5 tl) (string-to-number (format-time-string "%Y" ct)))
  5278. month (or (nth 4 tl) (string-to-number (format-time-string "%m" ct)))
  5279. day (or (nth 3 tl) (string-to-number (format-time-string "%d" ct)))
  5280. hour (or (nth 2 tl) (string-to-number (format-time-string "%H" ct)))
  5281. minute (or (nth 1 tl) (string-to-number (format-time-string "%M" ct)))
  5282. second (or (nth 0 tl) 0)
  5283. wday (nth 6 tl))
  5284. (when (and wday (not (nth 3 tl)))
  5285. ;; Weekday was given, but no day, so pick that day in the week
  5286. ;; on or after the derived date.
  5287. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  5288. (unless (equal wday wday1)
  5289. (setq day (+ day (% (- wday wday1 -7) 7)))))
  5290. (if (and (boundp 'org-time-was-given)
  5291. (nth 2 tl))
  5292. (setq org-time-was-given t))
  5293. (if (< year 100) (setq year (+ 2000 year)))
  5294. (if to-time
  5295. (encode-time second minute hour day month year)
  5296. (if (or (nth 1 tl) (nth 2 tl))
  5297. (format "%04d-%02d-%02d %02d:%02d" year month day hour minute)
  5298. (format "%04d-%02d-%02d" year month day)))))
  5299. (defun org-eval-in-calendar (form)
  5300. "Eval FORM in the calendar window and return to current window.
  5301. Also, store the cursor date in variable ans2."
  5302. (let ((sw (selected-window)))
  5303. (select-window (get-buffer-window "*Calendar*"))
  5304. (eval form)
  5305. (when (calendar-cursor-to-date)
  5306. (let* ((date (calendar-cursor-to-date))
  5307. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  5308. (setq ans2 (format-time-string "%Y-%m-%d" time))))
  5309. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  5310. (select-window sw)))
  5311. (defun org-calendar-select ()
  5312. "Return to `org-read-date' with the date currently selected.
  5313. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  5314. (interactive)
  5315. (when (calendar-cursor-to-date)
  5316. (let* ((date (calendar-cursor-to-date))
  5317. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  5318. (setq ans1 (format-time-string "%Y-%m-%d" time)))
  5319. (if (active-minibuffer-window) (exit-minibuffer))))
  5320. (defun org-calendar-select-mouse (ev)
  5321. "Return to `org-read-date' with the date currently selected.
  5322. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  5323. (interactive "e")
  5324. (mouse-set-point ev)
  5325. (when (calendar-cursor-to-date)
  5326. (let* ((date (calendar-cursor-to-date))
  5327. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  5328. (setq ans1 (format-time-string "%Y-%m-%d" time)))
  5329. (if (active-minibuffer-window) (exit-minibuffer))))
  5330. (defun org-check-deadlines (ndays)
  5331. "Check if there are any deadlines due or past due.
  5332. A deadline is considered due if it happens within `org-deadline-warning-days'
  5333. days from today's date. If the deadline appears in an entry marked DONE,
  5334. it is not shown. The prefix arg NDAYS can be used to test that many
  5335. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  5336. (interactive "P")
  5337. (let* ((org-warn-days
  5338. (cond
  5339. ((equal ndays '(4)) 100000)
  5340. (ndays (prefix-numeric-value ndays))
  5341. (t org-deadline-warning-days)))
  5342. (case-fold-search nil)
  5343. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  5344. (callback
  5345. (lambda ()
  5346. (and (let ((d1 (time-to-days (current-time)))
  5347. (d2 (time-to-days
  5348. (org-time-string-to-time (match-string 1)))))
  5349. (< (- d2 d1) org-warn-days))
  5350. (not (org-entry-is-done-p))))))
  5351. (message "%d deadlines past-due or due within %d days"
  5352. (org-occur regexp nil callback)
  5353. org-warn-days)))
  5354. (defun org-evaluate-time-range (&optional to-buffer)
  5355. "Evaluate a time range by computing the difference between start and end.
  5356. Normally the result is just printed in the echo area, but with prefix arg
  5357. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  5358. If the time range is actually in a table, the result is inserted into the
  5359. next column.
  5360. For time difference computation, a year is assumed to be exactly 365
  5361. days in order to avoid rounding problems."
  5362. (interactive "P")
  5363. (or
  5364. (org-clock-update-time-maybe)
  5365. (save-excursion
  5366. (unless (org-at-date-range-p)
  5367. (goto-char (point-at-bol))
  5368. (re-search-forward org-tr-regexp (point-at-eol) t))
  5369. (if (not (org-at-date-range-p))
  5370. (error "Not at a time-stamp range, and none found in current line")))
  5371. (let* ((ts1 (match-string 1))
  5372. (ts2 (match-string 2))
  5373. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  5374. (match-end (match-end 0))
  5375. (time1 (org-time-string-to-time ts1))
  5376. (time2 (org-time-string-to-time ts2))
  5377. (t1 (time-to-seconds time1))
  5378. (t2 (time-to-seconds time2))
  5379. (diff (abs (- t2 t1)))
  5380. (negative (< (- t2 t1) 0))
  5381. ;; (ys (floor (* 365 24 60 60)))
  5382. (ds (* 24 60 60))
  5383. (hs (* 60 60))
  5384. (fy "%dy %dd %02d:%02d")
  5385. (fy1 "%dy %dd")
  5386. (fd "%dd %02d:%02d")
  5387. (fd1 "%dd")
  5388. (fh "%02d:%02d")
  5389. y d h m align)
  5390. (if havetime
  5391. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  5392. y 0
  5393. d (floor (/ diff ds)) diff (mod diff ds)
  5394. h (floor (/ diff hs)) diff (mod diff hs)
  5395. m (floor (/ diff 60)))
  5396. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  5397. y 0
  5398. d (floor (+ (/ diff ds) 0.5))
  5399. h 0 m 0))
  5400. (if (not to-buffer)
  5401. (message (org-make-tdiff-string y d h m))
  5402. (when (org-at-table-p)
  5403. (goto-char match-end)
  5404. (setq align t)
  5405. (and (looking-at " *|") (goto-char (match-end 0))))
  5406. (if (looking-at
  5407. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  5408. (replace-match ""))
  5409. (if negative (insert " -"))
  5410. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  5411. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  5412. (insert " " (format fh h m))))
  5413. (if align (org-table-align))
  5414. (message "Time difference inserted")))))
  5415. (defun org-make-tdiff-string (y d h m)
  5416. (let ((fmt "")
  5417. (l nil))
  5418. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  5419. l (push y l)))
  5420. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  5421. l (push d l)))
  5422. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  5423. l (push h l)))
  5424. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  5425. l (push m l)))
  5426. (apply 'format fmt (nreverse l))))
  5427. (defun org-time-string-to-time (s)
  5428. (apply 'encode-time (org-parse-time-string s)))
  5429. (defun org-parse-time-string (s &optional nodefault)
  5430. "Parse the standard Org-mode time string.
  5431. This should be a lot faster than the normal `parse-time-string'.
  5432. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  5433. hour and minute fields will be nil if not given."
  5434. (if (string-match org-ts-regexp1 s)
  5435. (list 0
  5436. (if (or (match-beginning 8) (not nodefault))
  5437. (string-to-number (or (match-string 8 s) "0")))
  5438. (if (or (match-beginning 7) (not nodefault))
  5439. (string-to-number (or (match-string 7 s) "0")))
  5440. (string-to-number (match-string 4 s))
  5441. (string-to-number (match-string 3 s))
  5442. (string-to-number (match-string 2 s))
  5443. nil nil nil)
  5444. (make-list 9 0)))
  5445. (defun org-timestamp-up (&optional arg)
  5446. "Increase the date item at the cursor by one.
  5447. If the cursor is on the year, change the year. If it is on the month or
  5448. the day, change that.
  5449. With prefix ARG, change by that many units."
  5450. (interactive "p")
  5451. (org-timestamp-change (prefix-numeric-value arg)))
  5452. (defun org-timestamp-down (&optional arg)
  5453. "Decrease the date item at the cursor by one.
  5454. If the cursor is on the year, change the year. If it is on the month or
  5455. the day, change that.
  5456. With prefix ARG, change by that many units."
  5457. (interactive "p")
  5458. (org-timestamp-change (- (prefix-numeric-value arg))))
  5459. (defun org-timestamp-up-day (&optional arg)
  5460. "Increase the date in the time stamp by one day.
  5461. With prefix ARG, change that many days."
  5462. (interactive "p")
  5463. (if (and (not (org-at-timestamp-p t))
  5464. (org-on-heading-p))
  5465. (org-todo 'up)
  5466. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  5467. (defun org-timestamp-down-day (&optional arg)
  5468. "Decrease the date in the time stamp by one day.
  5469. With prefix ARG, change that many days."
  5470. (interactive "p")
  5471. (if (and (not (org-at-timestamp-p t))
  5472. (org-on-heading-p))
  5473. (org-todo 'down)
  5474. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  5475. (defsubst org-pos-in-match-range (pos n)
  5476. (and (match-beginning n)
  5477. (<= (match-beginning n) pos)
  5478. (>= (match-end n) pos)))
  5479. (defun org-at-timestamp-p (&optional also-inactive)
  5480. "Determine if the cursor is in or at a timestamp."
  5481. (interactive)
  5482. (let* ((tsr (if also-inactive org-ts-regexp3 org-ts-regexp2))
  5483. (pos (point))
  5484. (ans (or (looking-at tsr)
  5485. (save-excursion
  5486. (skip-chars-backward "^[<\n\r\t")
  5487. (if (> (point) 1) (backward-char 1))
  5488. (and (looking-at tsr)
  5489. (> (- (match-end 0) pos) -1))))))
  5490. (and (boundp 'org-ts-what)
  5491. (setq org-ts-what
  5492. (cond
  5493. ((org-pos-in-match-range pos 2) 'year)
  5494. ((org-pos-in-match-range pos 3) 'month)
  5495. ((org-pos-in-match-range pos 7) 'hour)
  5496. ((org-pos-in-match-range pos 8) 'minute)
  5497. ((or (org-pos-in-match-range pos 4)
  5498. (org-pos-in-match-range pos 5)) 'day)
  5499. (t 'day))))
  5500. ans))
  5501. (defun org-timestamp-change (n &optional what)
  5502. "Change the date in the time stamp at point.
  5503. The date will be changed by N times WHAT. WHAT can be `day', `month',
  5504. `year', `minute', `second'. If WHAT is not given, the cursor position
  5505. in the timestamp determines what will be changed."
  5506. (let ((fmt (car org-time-stamp-formats))
  5507. org-ts-what
  5508. (pos (point))
  5509. ts time time0)
  5510. (if (not (org-at-timestamp-p t))
  5511. (error "Not at a timestamp"))
  5512. (setq org-ts-what (or what org-ts-what))
  5513. (setq fmt (if (<= (abs (- (cdr org-ts-lengths)
  5514. (- (match-end 0) (match-beginning 0))))
  5515. 1)
  5516. (cdr org-time-stamp-formats)
  5517. (car org-time-stamp-formats)))
  5518. (if (= (char-after (match-beginning 0)) ?\[)
  5519. (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  5520. (setq ts (match-string 0))
  5521. (replace-match "")
  5522. (setq time0 (org-parse-time-string ts))
  5523. (setq time
  5524. (apply 'encode-time
  5525. (append
  5526. (list (or (car time0) 0))
  5527. (list (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0)))
  5528. (list (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0)))
  5529. (list (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0)))
  5530. (list (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0)))
  5531. (list (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0)))
  5532. (nthcdr 6 time0))))
  5533. (if (eq what 'calendar)
  5534. (let ((cal-date
  5535. (save-excursion
  5536. (save-match-data
  5537. (set-buffer "*Calendar*")
  5538. (calendar-cursor-to-date)))))
  5539. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  5540. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  5541. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  5542. (setcar time0 (or (car time0) 0))
  5543. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  5544. (setcar (nthcdr 2 time0) (or (nth 1 time0) 0))
  5545. (setq time (apply 'encode-time time0))))
  5546. (insert (setq org-last-changed-timestamp (format-time-string fmt time)))
  5547. (org-clock-update-time-maybe)
  5548. (goto-char pos)
  5549. ;; Try to recenter the calendar window, if any
  5550. (if (and org-calendar-follow-timestamp-change
  5551. (get-buffer-window "*Calendar*" t)
  5552. (memq org-ts-what '(day month year)))
  5553. (org-recenter-calendar (time-to-days time)))))
  5554. (defun org-recenter-calendar (date)
  5555. "If the calendar is visible, recenter it to DATE."
  5556. (let* ((win (selected-window))
  5557. (cwin (get-buffer-window "*Calendar*" t))
  5558. (calendar-move-hook nil))
  5559. (when cwin
  5560. (select-window cwin)
  5561. (calendar-goto-date (if (listp date) date
  5562. (calendar-gregorian-from-absolute date)))
  5563. (select-window win))))
  5564. (defun org-goto-calendar (&optional arg)
  5565. "Go to the Emacs calendar at the current date.
  5566. If there is a time stamp in the current line, go to that date.
  5567. A prefix ARG can be used to force the current date."
  5568. (interactive "P")
  5569. (let ((tsr org-ts-regexp) diff
  5570. (calendar-move-hook nil)
  5571. (view-calendar-holidays-initially nil)
  5572. (view-diary-entries-initially nil))
  5573. (if (or (org-at-timestamp-p)
  5574. (save-excursion
  5575. (beginning-of-line 1)
  5576. (looking-at (concat ".*" tsr))))
  5577. (let ((d1 (time-to-days (current-time)))
  5578. (d2 (time-to-days
  5579. (org-time-string-to-time (match-string 1)))))
  5580. (setq diff (- d2 d1))))
  5581. (calendar)
  5582. (calendar-goto-today)
  5583. (if (and diff (not arg)) (calendar-forward-day diff))))
  5584. (defun org-date-from-calendar ()
  5585. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  5586. If there is already a time stamp at the cursor position, update it."
  5587. (interactive)
  5588. (org-timestamp-change 0 'calendar))
  5589. ;;; The clock for measuring work time.
  5590. (defvar org-clock-marker (make-marker)
  5591. "Marker recording the last clock-in.")
  5592. (defun org-clock-in ()
  5593. "Start the clock on the current item.
  5594. If necessary, clock-out of the currently active clock."
  5595. (interactive)
  5596. (org-clock-out t)
  5597. (let (ts)
  5598. (save-excursion
  5599. (org-back-to-heading t)
  5600. (beginning-of-line 2)
  5601. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  5602. (not (equal (match-string 1) org-clock-string)))
  5603. (beginning-of-line 1))
  5604. (insert "\n") (backward-char 1)
  5605. (indent-relative)
  5606. (insert org-clock-string " "
  5607. (setq ts (concat "[" (format-time-string
  5608. (substring
  5609. (cdr org-time-stamp-formats) 1 -1)
  5610. (current-time))
  5611. "]")))
  5612. (move-marker org-clock-marker (point))
  5613. (message "Clock started at %s" ts))))
  5614. (defun org-clock-out (&optional fail-quietly)
  5615. "Stop the currently running clock.
  5616. If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
  5617. (interactive)
  5618. (catch 'exit
  5619. (if (not (marker-buffer org-clock-marker))
  5620. (if fail-quietly (throw 'exit t) (error "No active clock")))
  5621. (let (ts te s h m)
  5622. (save-excursion
  5623. (set-buffer (marker-buffer org-clock-marker))
  5624. (goto-char org-clock-marker)
  5625. (beginning-of-line 1)
  5626. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  5627. (equal (match-string 1) org-clock-string))
  5628. (setq ts (match-string 2))
  5629. (if fail-quietly (throw 'exit nil) (error "Clock start time is gone")))
  5630. (goto-char org-clock-marker)
  5631. (setq te (concat "[" (format-time-string
  5632. (substring
  5633. (cdr org-time-stamp-formats) 1 -1)
  5634. (current-time))
  5635. "]"))
  5636. (setq s (- (time-to-seconds (apply 'encode-time (org-parse-time-string te)))
  5637. (time-to-seconds (apply 'encode-time (org-parse-time-string ts))))
  5638. h (floor (/ s 3600))
  5639. s (- s (* 3600 h))
  5640. m (floor (/ s 60))
  5641. s (- s (* 60 s)))
  5642. (insert "--" te " => " (format "%2d:%02d" h m))
  5643. (move-marker org-clock-marker nil)
  5644. (org-add-log-maybe 'clock-out)
  5645. (message "Clock stopped at %s after HH:MM = %d:%02d" te h m)))))
  5646. (defun org-clock-cancel ()
  5647. "Cancel the running clock be removing the start timestamp."
  5648. (interactive)
  5649. (if (not (marker-buffer org-clock-marker))
  5650. (error "No active clock"))
  5651. (save-excursion
  5652. (set-buffer (marker-buffer org-clock-marker))
  5653. (goto-char org-clock-marker)
  5654. (delete-region (1- (point-at-bol)) (point-at-eol)))
  5655. (message "Clock canceled"))
  5656. (defvar org-clock-file-total-minutes nil
  5657. "Holds the file total time in minutes, after a call to `org-clock-sum'.")
  5658. (make-variable-buffer-local 'org-clock-file-total-minutes)
  5659. (defun org-clock-sum (&optional tstart tend)
  5660. "Sum the times for each subtree.
  5661. Puts the resulting times in minutes as a text property on each headline."
  5662. (interactive)
  5663. (let* ((bmp (buffer-modified-p))
  5664. (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
  5665. org-clock-string
  5666. "[ \t]*\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)"))
  5667. (lmax 30)
  5668. (ltimes (make-vector lmax 0))
  5669. (t1 0)
  5670. (level 0)
  5671. ts te dt
  5672. time)
  5673. (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t))
  5674. (save-excursion
  5675. (goto-char (point-max))
  5676. (while (re-search-backward re nil t)
  5677. (if (match-end 2)
  5678. ;; A time
  5679. (setq ts (match-string 2)
  5680. te (match-string 3)
  5681. ts (time-to-seconds
  5682. (apply 'encode-time (org-parse-time-string ts)))
  5683. te (time-to-seconds
  5684. (apply 'encode-time (org-parse-time-string te)))
  5685. ts (if tstart (max ts tstart) ts)
  5686. te (if tend (min te tend) te)
  5687. dt (- te ts)
  5688. t1 (if (> dt 0) (+ t1 (floor (/ dt 60))) t1))
  5689. ;; A headline
  5690. (setq level (- (match-end 1) (match-beginning 1)))
  5691. (when (or (> t1 0) (> (aref ltimes level) 0))
  5692. (loop for l from 0 to level do
  5693. (aset ltimes l (+ (aref ltimes l) t1)))
  5694. (setq t1 0 time (aref ltimes level))
  5695. (loop for l from level to (1- lmax) do
  5696. (aset ltimes l 0))
  5697. (goto-char (match-beginning 0))
  5698. (put-text-property (point) (point-at-eol) :org-clock-minutes time))))
  5699. (setq org-clock-file-total-minutes (aref ltimes 0)))
  5700. (set-buffer-modified-p bmp)))
  5701. (defun org-clock-display (&optional total-only)
  5702. "Show subtree times in the entire buffer.
  5703. If TOTAL-ONLY is non-nil, only show the total time for the entire file
  5704. in the echo area."
  5705. (interactive)
  5706. (org-remove-clock-overlays)
  5707. (let (time h m p)
  5708. (org-clock-sum)
  5709. (unless total-only
  5710. (save-excursion
  5711. (goto-char (point-min))
  5712. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  5713. (goto-char p)
  5714. (when (setq time (get-text-property p :org-clock-minutes))
  5715. (org-put-clock-overlay time (funcall outline-level))))
  5716. (setq h (/ org-clock-file-total-minutes 60)
  5717. m (- org-clock-file-total-minutes (* 60 h)))
  5718. ;; Arrange to remove the overlays upon next change.
  5719. (when org-remove-highlights-with-change
  5720. (org-add-hook 'before-change-functions 'org-remove-clock-overlays
  5721. nil 'local))))
  5722. (message "Total file time: %d:%02d (%d hours and %d minutes)" h m h m)))
  5723. (defvar org-clock-overlays nil)
  5724. (make-variable-buffer-local 'org-clock-overlays)
  5725. (defun org-put-clock-overlay (time &optional level)
  5726. "Put an overlays on the current line, displaying TIME.
  5727. If LEVEL is given, prefix time with a corresponding number of stars.
  5728. This creates a new overlay and stores it in `org-clock-overlays', so that it
  5729. will be easy to remove."
  5730. (let* ((c 60) (h (floor (/ time 60))) (m (- time (* 60 h)))
  5731. (l (if level (org-get-legal-level level 0) 0))
  5732. (off 0)
  5733. ov tx)
  5734. (move-to-column c)
  5735. (unless (eolp) (skip-chars-backward "^ \t"))
  5736. (skip-chars-backward " \t")
  5737. (setq ov (org-make-overlay (1- (point)) (point-at-eol))
  5738. tx (concat (buffer-substring (1- (point)) (point))
  5739. (make-string (+ off (max 0 (- c (current-column)))) ?.)
  5740. (org-add-props (format "%s %2d:%02d%s"
  5741. (make-string l ?*) h m
  5742. (make-string (- 10 l) ?\ ))
  5743. '(face secondary-selection))
  5744. ""))
  5745. (org-overlay-put ov 'display tx)
  5746. (push ov org-clock-overlays)))
  5747. (defun org-remove-clock-overlays (&optional beg end noremove)
  5748. "Remove the occur highlights from the buffer.
  5749. BEG and END are ignored. If NOREMOVE is nil, remove this function
  5750. from the `before-change-functions' in the current buffer."
  5751. (interactive)
  5752. (unless org-inhibit-highlight-removal
  5753. (mapc 'org-delete-overlay org-clock-overlays)
  5754. (setq org-clock-overlays nil)
  5755. (unless noremove
  5756. (remove-hook 'before-change-functions
  5757. 'org-remove-clock-overlays 'local))))
  5758. (defun org-clock-out-if-current ()
  5759. "Clock out if the current entry contains the running clock.
  5760. This is used to stop the clock after a TODO entry is marked DONE."
  5761. (when (and (equal state org-done-string)
  5762. (equal (marker-buffer org-clock-marker) (current-buffer))
  5763. (< (point) org-clock-marker)
  5764. (> (save-excursion (outline-next-heading) (point))
  5765. org-clock-marker))
  5766. ;; Clock out, but don't accept a logging message for this.
  5767. (let ((org-log-done (if (and (listp org-log-done)
  5768. (member 'clock-out org-log-done))
  5769. '(done)
  5770. org-log-done)))
  5771. (org-clock-out))))
  5772. (add-hook 'org-after-todo-state-change-hook
  5773. 'org-clock-out-if-current)
  5774. (defun org-check-running-clock ()
  5775. "Check if the current buffer contains the running clock.
  5776. If yes, offer to stop it and to save the buffer with the changes."
  5777. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  5778. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  5779. (buffer-name))))
  5780. (org-clock-out)
  5781. (when (y-or-n-p "Save changed buffer?")
  5782. (save-buffer))))
  5783. (defun org-clock-report ()
  5784. "Create a table containing a report about clocked time.
  5785. If the buffer contains lines
  5786. #+BEGIN: clocktable :maxlevel 3 :emphasize nil
  5787. #+END: clocktable
  5788. then the table will be inserted between these lines, replacing whatever
  5789. is was there before. If these lines are not in the buffer, the table
  5790. is inserted at point, surrounded by the special lines.
  5791. The BEGIN line can contain parameters. Allowed are:
  5792. :maxlevel The maximum level to be included in the table. Default is 3.
  5793. :emphasize t/nil, if levell 1 and level 2 should be bold/italic in the table."
  5794. (interactive)
  5795. (org-remove-clock-overlays)
  5796. (unless (org-find-dblock "clocktable")
  5797. (org-create-dblock (list :name "clocktable"
  5798. :maxlevel 2 :emphasize nil)))
  5799. (org-update-dblock))
  5800. (defun org-clock-update-time-maybe ()
  5801. "If this is a CLOCK line, update it and return t.
  5802. Otherwise, return nil."
  5803. (interactive)
  5804. (save-excursion
  5805. (beginning-of-line 1)
  5806. (skip-chars-forward " \t")
  5807. (when (looking-at org-clock-string)
  5808. (let ((re (concat "[ \t]*" org-clock-string
  5809. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  5810. "\\([ \t]*=>.*\\)?"))
  5811. ts te h m s)
  5812. (if (not (looking-at re))
  5813. nil
  5814. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  5815. (end-of-line 1)
  5816. (setq ts (match-string 1)
  5817. te (match-string 2))
  5818. (setq s (- (time-to-seconds
  5819. (apply 'encode-time (org-parse-time-string te)))
  5820. (time-to-seconds
  5821. (apply 'encode-time (org-parse-time-string ts))))
  5822. h (floor (/ s 3600))
  5823. s (- s (* 3600 h))
  5824. m (floor (/ s 60))
  5825. s (- s (* 60 s)))
  5826. (insert " => " (format "%2d:%02d" h m))
  5827. t)))))
  5828. (defun org-clock-special-range (key &optional time as-strings)
  5829. "Return two times bordering a special time range.
  5830. Key is a symbol specifying the range and can be one of `today', `yesterday',
  5831. `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
  5832. A week starts Monday 0:00 and ends Sunday 24:00.
  5833. The range is determined relative to TIME. TIME defaults to the current time.
  5834. The return value is a cons cell with two internal times like the ones
  5835. returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
  5836. the returned times will be formatted strings."
  5837. (let* ((tm (decode-time (or time (current-time))))
  5838. (s 0) (m (nth 1 tm)) (h (nth 2 tm))
  5839. (d (nth 3 tm)) (month (nth 4 tm)) (y (nth 5 tm))
  5840. (dow (nth 6 tm))
  5841. s1 m1 h1 d1 month1 y1 diff ts te fm)
  5842. (cond
  5843. ((eq key 'today)
  5844. (setq h 0 m 0 h1 24 m1 0))
  5845. ((eq key 'yesterday)
  5846. (setq d (1- d) h 0 m 0 h1 24 m1 0))
  5847. ((eq key 'thisweek)
  5848. (setq diff (if (= dow 0) 6 (1- dow))
  5849. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  5850. ((eq key 'lastweek)
  5851. (setq diff (+ 7 (if (= dow 0) 6 (1- dow)))
  5852. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  5853. ((eq key 'thismonth)
  5854. (setq d 1 h 0 m 0 d1 1 month1 (1+ month) h1 0 m1 0))
  5855. ((eq key 'lastmonth)
  5856. (setq d 1 h 0 m 0 d1 1 month (1- month) month1 (1+ month) h1 0 m1 0))
  5857. ((eq key 'thisyear)
  5858. (setq m 0 h 0 d 1 month 1 y1 (1+ y)))
  5859. ((eq key 'lastyear)
  5860. (setq m 0 h 0 d 1 month 1 y (1- y) y1 (1+ y)))
  5861. (t (error "No such time block %s" key)))
  5862. (setq ts (encode-time s m h d month y)
  5863. te (encode-time (or s1 s) (or m1 m) (or h1 h)
  5864. (or d1 d) (or month1 month) (or y1 y)))
  5865. (setq fm (cdr org-time-stamp-formats))
  5866. (if as-strings
  5867. (cons (format-time-string fm ts) (format-time-string fm te))
  5868. (cons ts te))))
  5869. (defun org-dblock-write:clocktable (params)
  5870. "Write the standard clocktable."
  5871. (let ((hlchars '((1 . "*") (2 . ?/)))
  5872. (emph nil)
  5873. (ins (make-marker))
  5874. ipos time h m p level hlc hdl maxlevel
  5875. ts te cc block)
  5876. (setq maxlevel (or (plist-get params :maxlevel) 3)
  5877. emph (plist-get params :emphasize)
  5878. ts (plist-get params :tstart)
  5879. te (plist-get params :tend)
  5880. block (plist-get params :block))
  5881. (when block
  5882. (setq cc (org-clock-special-range block nil t)
  5883. ts (car cc) te (cdr cc)))
  5884. (if ts (setq ts (time-to-seconds
  5885. (apply 'encode-time (org-parse-time-string ts)))))
  5886. (if te (setq te (time-to-seconds
  5887. (apply 'encode-time (org-parse-time-string te)))))
  5888. (move-marker ins (point))
  5889. (setq ipos (point))
  5890. (insert-before-markers "Clock summary at ["
  5891. (substring
  5892. (format-time-string (cdr org-time-stamp-formats))
  5893. 1 -1)
  5894. "]."
  5895. (if block
  5896. (format " Considered range is /%s/." block)
  5897. "")
  5898. "\n\n|L|Headline|Time|\n")
  5899. (org-clock-sum ts te)
  5900. (setq h (/ org-clock-file-total-minutes 60)
  5901. m (- org-clock-file-total-minutes (* 60 h)))
  5902. (insert-before-markers "|-\n|0|" "*Total file time*| "
  5903. (format "*%d:%02d*" h m)
  5904. "|\n")
  5905. (goto-char (point-min))
  5906. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  5907. (goto-char p)
  5908. (when (setq time (get-text-property p :org-clock-minutes))
  5909. (save-excursion
  5910. (beginning-of-line 1)
  5911. (when (and (looking-at "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[0-9a-zA-Z_@:]+:\\)?[ \t]*$")
  5912. (setq level (- (match-end 1) (match-beginning 1)))
  5913. (<= level maxlevel))
  5914. (setq hlc (if emph (or (cdr (assoc level hlchars)) "") "")
  5915. hdl (match-string 2)
  5916. h (/ time 60)
  5917. m (- time (* 60 h)))
  5918. (goto-char ins)
  5919. (if (= level 1) (insert-before-markers "|-\n"))
  5920. (insert-before-markers
  5921. "| " (int-to-string level) "|" hlc hdl hlc " |"
  5922. (make-string (1- level) ?|)
  5923. hlc
  5924. (format "%d:%02d" h m)
  5925. hlc
  5926. " |\n")))))
  5927. (goto-char ins)
  5928. (backward-delete-char 1)
  5929. (goto-char ipos)
  5930. (skip-chars-forward "^|")
  5931. (org-table-align)))
  5932. (defun org-collect-clock-time-entries ()
  5933. "Return an internal list with clocking information.
  5934. This list has one entry for each CLOCK interval.
  5935. FIXME: describe the elements."
  5936. (interactive)
  5937. (let ((re (concat "^[ \t]*" org-clock-string
  5938. " *\\[\\(.*?\\)\\]--\\[\\(.*?\\)\\]"))
  5939. rtn beg end next cont level title total closedp leafp
  5940. clockpos titlepos h m donep)
  5941. (save-excursion
  5942. (org-clock-sum)
  5943. (goto-char (point-min))
  5944. (while (re-search-forward re nil t)
  5945. (setq clockpos (match-beginning 0)
  5946. beg (match-string 1) end (match-string 2)
  5947. cont (match-end 0))
  5948. (setq beg (apply 'encode-time (org-parse-time-string beg))
  5949. end (apply 'encode-time (org-parse-time-string end)))
  5950. (org-back-to-heading t)
  5951. (setq donep (org-entry-is-done-p))
  5952. (setq titlepos (point)
  5953. total (or (get-text-property (1+ (point)) :org-clock-minutes) 0)
  5954. h (/ total 60) m (- total (* 60 h))
  5955. total (cons h m))
  5956. (looking-at "\\(\\*+\\) +\\(.*\\)")
  5957. (setq level (- (match-end 1) (match-beginning 1))
  5958. title (org-match-string-no-properties 2))
  5959. (save-excursion (outline-next-heading) (setq next (point)))
  5960. (setq closedp (re-search-forward org-closed-time-regexp next t))
  5961. (goto-char next)
  5962. (setq leafp (and (looking-at "^\\*+ ")
  5963. (<= (- (match-end 0) (point)) level)))
  5964. (push (list beg end clockpos closedp donep
  5965. total title titlepos level leafp)
  5966. rtn)
  5967. (goto-char cont)))
  5968. (nreverse rtn)))
  5969. ;;; Agenda, and Diary Integration
  5970. ;;; Define the mode
  5971. (defvar org-agenda-mode-map (make-sparse-keymap)
  5972. "Keymap for `org-agenda-mode'.")
  5973. (defvar org-agenda-menu) ; defined later in this file.
  5974. (defvar org-agenda-follow-mode nil)
  5975. (defvar org-agenda-show-log nil)
  5976. (defvar org-agenda-redo-command nil)
  5977. (defvar org-agenda-mode-hook nil)
  5978. (defvar org-agenda-type nil)
  5979. (defvar org-agenda-force-single-file nil)
  5980. (defun org-agenda-mode ()
  5981. "Mode for time-sorted view on action items in Org-mode files.
  5982. The following commands are available:
  5983. \\{org-agenda-mode-map}"
  5984. (interactive)
  5985. (kill-all-local-variables)
  5986. (setq major-mode 'org-agenda-mode)
  5987. (setq mode-name "Org-Agenda")
  5988. (use-local-map org-agenda-mode-map)
  5989. (easy-menu-add org-agenda-menu)
  5990. (if org-startup-truncated (setq truncate-lines t))
  5991. (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
  5992. (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
  5993. (unless org-agenda-keep-modes
  5994. (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
  5995. org-agenda-show-log nil))
  5996. (easy-menu-change
  5997. '("Agenda") "Agenda Files"
  5998. (append
  5999. (list
  6000. (vector
  6001. (if (get 'org-agenda-files 'org-restrict)
  6002. "Restricted to single file"
  6003. "Edit File List")
  6004. '(org-edit-agenda-file-list)
  6005. (not (get 'org-agenda-files 'org-restrict)))
  6006. "--")
  6007. (mapcar 'org-file-menu-entry (org-agenda-files))))
  6008. (org-agenda-set-mode-name)
  6009. (apply
  6010. (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
  6011. (list 'org-agenda-mode-hook)))
  6012. (define-key org-agenda-mode-map "\C-i" 'org-agenda-goto)
  6013. (define-key org-agenda-mode-map [(tab)] 'org-agenda-goto)
  6014. (define-key org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
  6015. (define-key org-agenda-mode-map " " 'org-agenda-show)
  6016. (define-key org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
  6017. (define-key org-agenda-mode-map "o" 'delete-other-windows)
  6018. (define-key org-agenda-mode-map "L" 'org-agenda-recenter)
  6019. (define-key org-agenda-mode-map "t" 'org-agenda-todo)
  6020. (define-key org-agenda-mode-map "a" 'org-agenda-toggle-archive-tag)
  6021. (define-key org-agenda-mode-map ":" 'org-agenda-set-tags)
  6022. (define-key org-agenda-mode-map "." 'org-agenda-goto-today)
  6023. (define-key org-agenda-mode-map "d" 'org-agenda-day-view)
  6024. (define-key org-agenda-mode-map "w" 'org-agenda-week-view)
  6025. (define-key org-agenda-mode-map (org-key 'S-right) 'org-agenda-date-later)
  6026. (define-key org-agenda-mode-map (org-key 'S-left) 'org-agenda-date-earlier)
  6027. (define-key org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-date-later)
  6028. (define-key org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-date-earlier)
  6029. (define-key org-agenda-mode-map ">" 'org-agenda-date-prompt)
  6030. (define-key org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
  6031. (define-key org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
  6032. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  6033. (while l (define-key org-agenda-mode-map
  6034. (int-to-string (pop l)) 'digit-argument)))
  6035. (define-key org-agenda-mode-map "f" 'org-agenda-follow-mode)
  6036. (define-key org-agenda-mode-map "l" 'org-agenda-log-mode)
  6037. (define-key org-agenda-mode-map "D" 'org-agenda-toggle-diary)
  6038. (define-key org-agenda-mode-map "g" 'org-agenda-toggle-time-grid)
  6039. (define-key org-agenda-mode-map "r" 'org-agenda-redo)
  6040. (define-key org-agenda-mode-map "q" 'org-agenda-quit)
  6041. (define-key org-agenda-mode-map "x" 'org-agenda-exit)
  6042. (define-key org-agenda-mode-map "s" 'org-save-all-org-buffers)
  6043. (define-key org-agenda-mode-map "P" 'org-agenda-show-priority)
  6044. (define-key org-agenda-mode-map "T" 'org-agenda-show-tags)
  6045. (define-key org-agenda-mode-map "n" 'next-line)
  6046. (define-key org-agenda-mode-map "p" 'previous-line)
  6047. (define-key org-agenda-mode-map "\C-n" 'org-agenda-next-date-line)
  6048. (define-key org-agenda-mode-map "\C-p" 'org-agenda-previous-date-line)
  6049. (define-key org-agenda-mode-map "," 'org-agenda-priority)
  6050. (define-key org-agenda-mode-map "\C-c," 'org-agenda-priority)
  6051. (define-key org-agenda-mode-map "i" 'org-agenda-diary-entry)
  6052. (define-key org-agenda-mode-map "c" 'org-agenda-goto-calendar)
  6053. (eval-after-load "calendar"
  6054. '(define-key calendar-mode-map org-calendar-to-agenda-key
  6055. 'org-calendar-goto-agenda))
  6056. (define-key org-agenda-mode-map "C" 'org-agenda-convert-date)
  6057. (define-key org-agenda-mode-map "m" 'org-agenda-phases-of-moon)
  6058. (define-key org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
  6059. (define-key org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
  6060. (define-key org-agenda-mode-map "h" 'org-agenda-holidays)
  6061. (define-key org-agenda-mode-map "H" 'org-agenda-holidays)
  6062. (define-key org-agenda-mode-map "+" 'org-agenda-priority-up)
  6063. (define-key org-agenda-mode-map "I" 'org-agenda-clock-in)
  6064. (define-key org-agenda-mode-map "O" 'org-clock-out)
  6065. (define-key org-agenda-mode-map "X" 'org-clock-cancel)
  6066. (define-key org-agenda-mode-map "-" 'org-agenda-priority-down)
  6067. (define-key org-agenda-mode-map (org-key 'S-up) 'org-agenda-priority-up)
  6068. (define-key org-agenda-mode-map (org-key 'S-down) 'org-agenda-priority-down)
  6069. (define-key org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
  6070. (define-key org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
  6071. (define-key org-agenda-mode-map [(right)] 'org-agenda-later)
  6072. (define-key org-agenda-mode-map [(left)] 'org-agenda-earlier)
  6073. (define-key org-agenda-mode-map "\C-c\C-x\C-c" 'org-export-icalendar-combine-agenda-files)
  6074. (defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
  6075. "Local keymap for agenda entries from Org-mode.")
  6076. (define-key org-agenda-keymap
  6077. (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
  6078. (define-key org-agenda-keymap
  6079. (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
  6080. (when org-agenda-mouse-1-follows-link
  6081. (define-key org-agenda-keymap [follow-link] 'mouse-face))
  6082. (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
  6083. '("Agenda"
  6084. ("Agenda Files")
  6085. "--"
  6086. ["Show" org-agenda-show t]
  6087. ["Go To (other window)" org-agenda-goto t]
  6088. ["Go To (one window)" org-agenda-switch-to t]
  6089. ["Follow Mode" org-agenda-follow-mode
  6090. :style toggle :selected org-agenda-follow-mode :active t]
  6091. "--"
  6092. ["Cycle TODO" org-agenda-todo t]
  6093. ("Tags"
  6094. ["Show all Tags" org-agenda-show-tags t]
  6095. ["Set Tags" org-agenda-set-tags t])
  6096. ("Schedule"
  6097. ["Schedule" org-agenda-schedule t]
  6098. ["Set Deadline" org-agenda-deadline t]
  6099. "--"
  6100. ["Reschedule +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
  6101. ["Reschedule -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
  6102. ["Reschedule to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
  6103. ("Priority"
  6104. ["Set Priority" org-agenda-priority t]
  6105. ["Increase Priority" org-agenda-priority-up t]
  6106. ["Decrease Priority" org-agenda-priority-down t]
  6107. ["Show Priority" org-agenda-show-priority t])
  6108. "--"
  6109. ;; ["New agenda command" org-agenda t]
  6110. ["Rebuild buffer" org-agenda-redo t]
  6111. ["Save all Org-mode Buffers" org-save-all-org-buffers t]
  6112. "--"
  6113. ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
  6114. ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
  6115. ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
  6116. "--"
  6117. ["Day View" org-agenda-day-view :active (org-agenda-check-type nil 'agenda)
  6118. :style radio :selected (equal org-agenda-ndays 1)]
  6119. ["Week View" org-agenda-week-view :active (org-agenda-check-type nil 'agenda)
  6120. :style radio :selected (equal org-agenda-ndays 7)]
  6121. "--"
  6122. ["Show Logbook entries" org-agenda-log-mode
  6123. :style toggle :selected org-agenda-show-log :active (org-agenda-check-type nil 'agenda 'timeline)]
  6124. ["Include Diary" org-agenda-toggle-diary
  6125. :style toggle :selected org-agenda-include-diary :active (org-agenda-check-type nil 'agenda)]
  6126. ["Use Time Grid" org-agenda-toggle-time-grid
  6127. :style toggle :selected org-agenda-use-time-grid :active (org-agenda-check-type nil 'agenda)]
  6128. "--"
  6129. ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
  6130. ("Calendar Commands"
  6131. ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
  6132. ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
  6133. ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
  6134. ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
  6135. ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)])
  6136. ["Create iCalendar file" org-export-icalendar-combine-agenda-files t]
  6137. "--"
  6138. ["Quit" org-agenda-quit t]
  6139. ["Exit and Release Buffers" org-agenda-exit t]
  6140. ))
  6141. (defvar org-agenda-restrict nil)
  6142. (defvar org-agenda-restrict-begin (make-marker))
  6143. (defvar org-agenda-restrict-end (make-marker))
  6144. (defvar org-agenda-last-dispatch-buffer nil)
  6145. ;;;###autoload
  6146. (defun org-agenda (arg)
  6147. "Dispatch agenda commands to collect entries to the agenda buffer.
  6148. Prompts for a character to select a command. Any prefix arg will be passed
  6149. on to the selected command. The default selections are:
  6150. g
  6151. a Call `org-agenda-list' to display the agenda for current day or week.
  6152. t Call `org-todo-list' to display the global todo list.
  6153. T Call `org-todo-list' to display the global todo list, select only
  6154. entries with a specific TODO keyword (the user gets a prompt).
  6155. m Call `org-tags-view' to display headlines with tags matching
  6156. a condition (the user is prompted for the condition).
  6157. M Like `m', but select only TODO entries, no ordinary headlines.
  6158. l Create a timeeline for the current buffer.
  6159. More commands can be added by configuring the variable
  6160. `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
  6161. searches can be pre-defined in this way.
  6162. If the current buffer is in Org-mode and visiting a file, you can also
  6163. first press `1' to indicate that the agenda should be temporarily (until the
  6164. next use of \\[org-agenda]) restricted to the current file."
  6165. (interactive "P")
  6166. (catch 'exit
  6167. (let ((restrict-ok (and buffer-file-name (org-mode-p)))
  6168. (buf (current-buffer))
  6169. (bfn buffer-file-name)
  6170. (custom org-agenda-custom-commands)
  6171. c entry key type match lprops)
  6172. ;; Turn off restriction
  6173. (put 'org-agenda-files 'org-restrict nil)
  6174. (setq org-agenda-restrict nil)
  6175. (move-marker org-agenda-restrict-begin nil)
  6176. (move-marker org-agenda-restrict-end nil)
  6177. ;; Remember where this call originated
  6178. (setq org-agenda-last-dispatch-buffer (current-buffer))
  6179. (save-window-excursion
  6180. (delete-other-windows)
  6181. (switch-to-buffer-other-window " *Agenda Commands*")
  6182. (erase-buffer)
  6183. (insert
  6184. "Press key for an agenda command:
  6185. --------------------------------
  6186. a Agenda for current week or day
  6187. t List of all TODO entries T Entries with special TODO kwd
  6188. m Match a TAGS query M Like m, but only TODO entries
  6189. L Timeline for current buffer C Configure custom agenda commands")
  6190. (while (setq entry (pop custom))
  6191. (setq key (car entry) type (nth 1 entry) match (nth 2 entry))
  6192. (insert (format "\n%-4s%-14s: %s"
  6193. key
  6194. (cond
  6195. ((stringp type) type)
  6196. ((eq type 'tags) "Tags query")
  6197. ((eq type 'todo) "TODO keyword")
  6198. ((eq type 'tags-tree) "Tags tree")
  6199. ((eq type 'todo-tree) "TODO kwd tree")
  6200. ((eq type 'occur-tree) "Occur tree")
  6201. (t "???"))
  6202. (if (stringp match)
  6203. (org-add-props match nil 'face 'org-warning)
  6204. (format "set of %d commands" (+ -2 (length entry)))))))
  6205. (if restrict-ok
  6206. (insert "\n"
  6207. (org-add-props "1 Restrict call to current buffer 0 Restrict call to region or subtree" nil 'face 'org-table)))
  6208. (goto-char (point-min))
  6209. (if (fboundp 'fit-window-to-buffer) (fit-window-to-buffer))
  6210. (message "Press key for agenda command%s"
  6211. (if restrict-ok ", or [1] or [0] to restrict" ""))
  6212. (setq c (read-char-exclusive))
  6213. (message "")
  6214. (when (memq c '(?L ?1 ?0))
  6215. (if restrict-ok
  6216. (put 'org-agenda-files 'org-restrict (list bfn))
  6217. (error "Cannot restrict agenda to current buffer"))
  6218. (with-current-buffer " *Agenda Commands*"
  6219. (goto-char (point-max))
  6220. (delete-region (point-at-bol) (point))
  6221. (goto-char (point-min)))
  6222. (when (eq c ?0)
  6223. (setq org-agenda-restrict t)
  6224. (with-current-buffer buf
  6225. (if (org-region-active-p)
  6226. (progn
  6227. (move-marker org-agenda-restrict-begin (region-beginning))
  6228. (move-marker org-agenda-restrict-end (region-end)))
  6229. (save-excursion
  6230. (org-back-to-heading t)
  6231. (move-marker org-agenda-restrict-begin (point))
  6232. (move-marker org-agenda-restrict-end
  6233. (progn (org-end-of-subtree t)))))))
  6234. (unless (eq c ?L)
  6235. (message "Press key for agenda command%s"
  6236. (if restrict-ok " (restricted to current file)" ""))
  6237. (setq c (read-char-exclusive)))
  6238. (message "")))
  6239. (require 'calendar) ; FIXME: can we avoid this for some commands?
  6240. ;; For example the todo list should not need it (but does...)
  6241. (cond
  6242. ((equal c ?C) (customize-variable 'org-agenda-custom-commands))
  6243. ((equal c ?a) (call-interactively 'org-agenda-list))
  6244. ((equal c ?t) (call-interactively 'org-todo-list))
  6245. ((equal c ?T) (org-call-with-arg 'org-todo-list (or arg '(4))))
  6246. ((equal c ?m) (call-interactively 'org-tags-view))
  6247. ((equal c ?M) (org-call-with-arg 'org-tags-view (or arg '(4))))
  6248. ((equal c ?L)
  6249. (unless restrict-ok
  6250. (error "This is not an Org-mode file"))
  6251. (org-call-with-arg 'org-timeline arg))
  6252. ((setq entry (assoc (char-to-string c) org-agenda-custom-commands))
  6253. (if (symbolp (nth 1 entry))
  6254. (progn
  6255. (setq type (nth 1 entry) match (nth 2 entry) lprops (nth 3 entry)
  6256. lprops (nth 3 entry))
  6257. (cond
  6258. ((eq type 'tags)
  6259. (org-let lprops '(org-tags-view current-prefix-arg match)))
  6260. ((eq type 'tags-todo)
  6261. (org-let lprops '(org-tags-view '(4) match)))
  6262. ((eq type 'todo)
  6263. (org-let lprops '(org-todo-list match)))
  6264. ((eq type 'tags-tree)
  6265. (org-check-for-org-mode)
  6266. (org-let lprops '(org-tags-sparse-tree current-prefix-arg match)))
  6267. ((eq type 'todo-tree)
  6268. (org-check-for-org-mode)
  6269. (org-let lprops
  6270. '(org-occur (concat "^" outline-regexp "[ \t]*"
  6271. (regexp-quote match) "\\>"))))
  6272. ((eq type 'occur-tree)
  6273. (org-check-for-org-mode)
  6274. (org-let lprops '(org-occur match)))
  6275. (t (error "Invalid custom agenda command type %s" type))))
  6276. (org-run-agenda-series (cddr entry))))
  6277. (t (error "Invalid key"))))))
  6278. ;; FIXME: what is the meaning of WINDOW?????
  6279. ;; FIXME: need to force KEEP-MODES for series comands......
  6280. (defun org-run-agenda-series (series &optional window)
  6281. (org-prepare-agenda)
  6282. (let* ((org-agenda-multi t)
  6283. (redo (list 'org-run-agenda-series (list 'quote series)))
  6284. (org-select-agenda-window t)
  6285. (cmds (car series))
  6286. (gprops (nth 1 series))
  6287. cmd type match lprops)
  6288. (while (setq cmd (pop cmds))
  6289. (setq type (car cmd) match (nth 1 cmd) lprops (nth 2 cmd))
  6290. (cond
  6291. ((eq type 'agenda)
  6292. (call-interactively 'org-agenda-list))
  6293. ((eq type 'alltodo)
  6294. (call-interactively 'org-todo-list))
  6295. ((eq type 'tags)
  6296. (org-let2 gprops lprops
  6297. '(org-tags-view current-prefix-arg match)))
  6298. ((eq type 'tags-todo)
  6299. (org-let2 gprops lprops
  6300. '(org-tags-view '(4) match)))
  6301. ((eq type 'todo)
  6302. (org-let2 gprops lprops
  6303. '(org-todo-list match)))
  6304. (t (error "Invalid type in command series"))))
  6305. (widen)
  6306. (setq org-agenda-redo-command redo)
  6307. (goto-char (point-min)))
  6308. (org-finalize-agenda))
  6309. ;;;###autoload
  6310. (defmacro org-batch-agenda (cmd-key &rest parameters)
  6311. "Run an agenda command in batch mode, send result to STDOUT.
  6312. CMD-KEY is a string that is also a key in `org-agenda-custom-commands'.
  6313. Paramters are alternating variable names and values that will be bound
  6314. before running the agenda command."
  6315. (let (pars)
  6316. (while parameters
  6317. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  6318. (flet ((read-char-exclusive () (string-to-char cmd-key)))
  6319. (eval (list 'let (nreverse pars) '(org-agenda nil))))
  6320. (set-buffer "*Org Agenda*")
  6321. (princ (buffer-string))))
  6322. (defun org-check-for-org-mode ()
  6323. "Make sure current buffer is in org-mode. Error if not."
  6324. (or (org-mode-p)
  6325. (error "Cannot execute org-mode agenda command on buffer in %s."
  6326. major-mode)))
  6327. (defun org-fit-agenda-window ()
  6328. "Fit the window to the buffer size."
  6329. (and org-fit-agenda-window
  6330. (fboundp 'fit-window-to-buffer)
  6331. (fit-window-to-buffer nil (/ (* (frame-height) 3) 4)
  6332. (/ (frame-height) 2))))
  6333. (defun org-agenda-files (&optional unrestricted)
  6334. "Get the list of agenda files.
  6335. Optional UNRESTRICTED means return the full list even if a restriction
  6336. is currently in place."
  6337. (cond
  6338. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  6339. ((stringp org-agenda-files) (org-read-agenda-file-list))
  6340. ((listp org-agenda-files) org-agenda-files)
  6341. (t (error "Invalid value of `org-agenda-files'"))))
  6342. (defvar org-window-configuration)
  6343. (defun org-edit-agenda-file-list ()
  6344. "Edit the list of agenda files.
  6345. Depending on setup, this either uses customize to edit the variable
  6346. `org-agenda-files', or it visits the file that is holding the list. In the
  6347. latter case, the buffer is set up in a way that saving it automatically kills
  6348. the buffer and restores the previous window configuration."
  6349. (interactive)
  6350. (if (stringp org-agenda-files)
  6351. (let ((cw (current-window-configuration)))
  6352. (find-file org-agenda-files)
  6353. (org-set-local 'org-window-configuration cw)
  6354. (org-add-hook 'after-save-hook
  6355. (lambda ()
  6356. (set-window-configuration
  6357. (prog1 org-window-configuration
  6358. (kill-buffer (current-buffer))))
  6359. (org-install-agenda-files-menu)
  6360. (message "New agenda file list installed"))
  6361. nil 'local)
  6362. (message (substitute-command-keys
  6363. "Edit list and finish with \\[save-buffer]")))
  6364. (customize-variable 'org-agenda-files)))
  6365. (defun org-store-new-agenda-file-list (list)
  6366. "Set new value for the agenda file list and save it correcly."
  6367. (if (stringp org-agenda-files)
  6368. (let ((f org-agenda-files) b)
  6369. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  6370. (with-temp-file f
  6371. (insert (mapconcat 'identity list "\n") "\n")))
  6372. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  6373. (setq org-agenda-files list)
  6374. (customize-save-variable 'org-agenda-files org-agenda-files))))
  6375. (defun org-read-agenda-file-list ()
  6376. "Read the list of agenda files from a file."
  6377. (when (stringp org-agenda-files)
  6378. (with-temp-buffer
  6379. (insert-file-contents org-agenda-files)
  6380. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  6381. (defvar org-agenda-markers nil
  6382. "List of all currently active markers created by `org-agenda'.")
  6383. (defvar org-agenda-last-marker-time (time-to-seconds (current-time))
  6384. "Creation time of the last agenda marker.")
  6385. (defun org-agenda-new-marker (&optional pos)
  6386. "Return a new agenda marker.
  6387. Org-mode keeps a list of these markers and resets them when they are
  6388. no longer in use."
  6389. (let ((m (copy-marker (or pos (point)))))
  6390. (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
  6391. (push m org-agenda-markers)
  6392. m))
  6393. (defun org-agenda-maybe-reset-markers (&optional force)
  6394. "Reset markers created by `org-agenda'. But only if they are old enough."
  6395. (if (or (and force (not org-agenda-multi))
  6396. (> (- (time-to-seconds (current-time))
  6397. org-agenda-last-marker-time)
  6398. 5))
  6399. (while org-agenda-markers
  6400. (move-marker (pop org-agenda-markers) nil))))
  6401. (defvar org-agenda-new-buffers nil
  6402. "Buffers created to visit agenda files.")
  6403. (defun org-get-agenda-file-buffer (file)
  6404. "Get a buffer visiting FILE. If the buffer needs to be created, add
  6405. it to the list of buffers which might be released later."
  6406. (let ((buf (find-buffer-visiting file)))
  6407. (if buf
  6408. buf ; just return it
  6409. ;; Make a new buffer and remember it
  6410. (setq buf (find-file-noselect file))
  6411. (if buf (push buf org-agenda-new-buffers))
  6412. buf)))
  6413. (defun org-release-buffers (blist)
  6414. "Release all buffers in list, asking the user for confirmation when needed.
  6415. When a buffer is unmodified, it is just killed. When modified, it is saved
  6416. \(if the user agrees) and then killed."
  6417. (let (buf file)
  6418. (while (setq buf (pop blist))
  6419. (setq file (buffer-file-name buf))
  6420. (when (and (buffer-modified-p buf)
  6421. file
  6422. (y-or-n-p (format "Save file %s? " file)))
  6423. (with-current-buffer buf (save-buffer)))
  6424. (kill-buffer buf))))
  6425. (defun org-timeline (&optional include-all keep-modes)
  6426. "Show a time-sorted view of the entries in the current org file.
  6427. Only entries with a time stamp of today or later will be listed. With
  6428. \\[universal-argument] prefix, all unfinished TODO items will also be shown,
  6429. under the current date.
  6430. If the buffer contains an active region, only check the region for
  6431. dates."
  6432. (interactive "P")
  6433. (require 'calendar)
  6434. (org-compile-prefix-format 'timeline)
  6435. (org-set-sorting-strategy 'timeline)
  6436. (let* ((dopast t)
  6437. (dotodo include-all)
  6438. (doclosed org-agenda-show-log)
  6439. (org-agenda-keep-modes keep-modes)
  6440. (entry buffer-file-name)
  6441. (date (calendar-current-date))
  6442. (win (selected-window))
  6443. (pos1 (point))
  6444. (beg (if (org-region-active-p) (region-beginning) (point-min)))
  6445. (end (if (org-region-active-p) (region-end) (point-max)))
  6446. (day-numbers (org-get-all-dates beg end 'no-ranges
  6447. t doclosed ; always include today
  6448. org-timeline-show-empty-dates))
  6449. (today (time-to-days (current-time)))
  6450. (past t)
  6451. args
  6452. s e rtn d emptyp)
  6453. (setq org-agenda-redo-command
  6454. (list 'progn
  6455. (list 'switch-to-buffer-other-window (current-buffer))
  6456. (list 'org-timeline (list 'quote include-all) t)))
  6457. (if (not dopast)
  6458. ;; Remove past dates from the list of dates.
  6459. (setq day-numbers (delq nil (mapcar (lambda(x)
  6460. (if (>= x today) x nil))
  6461. day-numbers))))
  6462. (org-prepare-agenda)
  6463. (if doclosed (push :closed args))
  6464. (push :timestamp args)
  6465. (if dotodo (push :todo args))
  6466. (while (setq d (pop day-numbers))
  6467. (if (and (listp d) (eq (car d) :omitted))
  6468. (progn
  6469. (setq s (point))
  6470. (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
  6471. (put-text-property s (1- (point)) 'face 'org-level-3))
  6472. (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
  6473. (if (and (>= d today)
  6474. dopast
  6475. past)
  6476. (progn
  6477. (setq past nil)
  6478. (insert (make-string 79 ?-) "\n")))
  6479. (setq date (calendar-gregorian-from-absolute d))
  6480. (setq s (point))
  6481. (setq rtn (and (not emptyp)
  6482. (apply 'org-agenda-get-day-entries
  6483. entry date args)))
  6484. (if (or rtn (equal d today) org-timeline-show-empty-dates)
  6485. (progn
  6486. (insert (calendar-day-name date) " "
  6487. (number-to-string (extract-calendar-day date)) " "
  6488. (calendar-month-name (extract-calendar-month date)) " "
  6489. (number-to-string (extract-calendar-year date)) "\n")
  6490. (put-text-property s (1- (point)) 'face
  6491. 'org-level-3)
  6492. (if (equal d today)
  6493. (put-text-property s (1- (point)) 'org-today t))
  6494. (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
  6495. (put-text-property s (1- (point)) 'day d)))))
  6496. (goto-char (point-min))
  6497. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  6498. (point-min)))
  6499. (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
  6500. (org-finalize-agenda)
  6501. (setq buffer-read-only t)
  6502. (when (not org-select-agenda-window)
  6503. (select-window win)
  6504. (goto-char pos1))))
  6505. (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
  6506. (defvar org-agenda-last-arguments nil
  6507. "The arguments of the previous call to org-agenda")
  6508. ;;;###autoload
  6509. (defun org-agenda-list (&optional include-all start-day ndays keep-modes)
  6510. "Produce a weekly view from all files in variable `org-agenda-files'.
  6511. The view will be for the current week, but from the overview buffer you
  6512. will be able to go to other weeks.
  6513. With one \\[universal-argument] prefix argument INCLUDE-ALL, all unfinished TODO items will
  6514. also be shown, under the current date.
  6515. With two \\[universal-argument] prefix argument INCLUDE-ALL, all TODO entries marked DONE
  6516. on the days are also shown. See the variable `org-log-done' for how
  6517. to turn on logging.
  6518. START-DAY defaults to TODAY, or to the most recent match for the weekday
  6519. given in `org-agenda-start-on-weekday'.
  6520. NDAYS defaults to `org-agenda-ndays'."
  6521. (interactive "P")
  6522. (if org-agenda-overriding-arguments
  6523. (setq include-all (car org-agenda-overriding-arguments)
  6524. start-day (nth 1 org-agenda-overriding-arguments)
  6525. ndays (nth 2 org-agenda-overriding-arguments)
  6526. keep-modes (nth 3 org-agenda-overriding-arguments)))
  6527. (setq org-agenda-last-arguments (list include-all start-day ndays keep-modes))
  6528. (org-compile-prefix-format 'agenda)
  6529. (org-set-sorting-strategy 'agenda)
  6530. (require 'calendar)
  6531. (let* ((org-agenda-start-on-weekday
  6532. (if (or (equal ndays 1)
  6533. (and (null ndays) (equal 1 org-agenda-ndays)))
  6534. nil org-agenda-start-on-weekday))
  6535. (org-agenda-keep-modes keep-modes)
  6536. (thefiles (org-agenda-files))
  6537. (files thefiles)
  6538. (win (selected-window))
  6539. (today (time-to-days (current-time)))
  6540. (sd (or start-day today))
  6541. (start (if (or (null org-agenda-start-on-weekday)
  6542. (< org-agenda-ndays 7))
  6543. sd
  6544. (let* ((nt (calendar-day-of-week
  6545. (calendar-gregorian-from-absolute sd)))
  6546. (n1 org-agenda-start-on-weekday)
  6547. (d (- nt n1)))
  6548. (- sd (+ (if (< d 0) 7 0) d)))))
  6549. (day-numbers (list start))
  6550. (inhibit-redisplay t)
  6551. s e rtn rtnall file date d start-pos end-pos todayp nd)
  6552. (setq org-agenda-redo-command
  6553. (list 'org-agenda-list (list 'quote include-all) start-day ndays t))
  6554. ;; Make the list of days
  6555. (setq ndays (or ndays org-agenda-ndays)
  6556. nd ndays)
  6557. (while (> ndays 1)
  6558. (push (1+ (car day-numbers)) day-numbers)
  6559. (setq ndays (1- ndays)))
  6560. (setq day-numbers (nreverse day-numbers))
  6561. (org-prepare-agenda)
  6562. (org-set-local 'starting-day (car day-numbers))
  6563. (org-set-local 'include-all-loc include-all)
  6564. (when (and (or include-all org-agenda-include-all-todo)
  6565. (member today day-numbers))
  6566. (setq files thefiles
  6567. rtnall nil)
  6568. (while (setq file (pop files))
  6569. (catch 'nextfile
  6570. (org-check-agenda-file file)
  6571. (setq date (calendar-gregorian-from-absolute today)
  6572. rtn (org-agenda-get-day-entries
  6573. file date :todo))
  6574. (setq rtnall (append rtnall rtn))))
  6575. (when rtnall
  6576. (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
  6577. (add-text-properties (point-min) (1- (point))
  6578. (list 'face 'org-level-3))
  6579. (insert (org-finalize-agenda-entries rtnall) "\n")))
  6580. (setq s (point))
  6581. (insert (if (= nd 7) "Week-" "Day-") "agenda:\n")
  6582. (add-text-properties s (1- (point)) (list 'face 'org-level-3))
  6583. (while (setq d (pop day-numbers))
  6584. (setq date (calendar-gregorian-from-absolute d)
  6585. s (point))
  6586. (if (or (setq todayp (= d today))
  6587. (and (not start-pos) (= d sd)))
  6588. (setq start-pos (point))
  6589. (if (and start-pos (not end-pos))
  6590. (setq end-pos (point))))
  6591. (setq files thefiles
  6592. rtnall nil)
  6593. (while (setq file (pop files))
  6594. (catch 'nextfile
  6595. (org-check-agenda-file file)
  6596. (if org-agenda-show-log
  6597. (setq rtn (org-agenda-get-day-entries
  6598. file date
  6599. :deadline :scheduled :timestamp :closed))
  6600. (setq rtn (org-agenda-get-day-entries
  6601. file date
  6602. :deadline :scheduled :timestamp)))
  6603. (setq rtnall (append rtnall rtn))))
  6604. (if org-agenda-include-diary
  6605. (progn
  6606. (require 'diary-lib)
  6607. (setq rtn (org-get-entries-from-diary date))
  6608. (setq rtnall (append rtnall rtn))))
  6609. (if (or rtnall org-agenda-show-all-dates)
  6610. (progn
  6611. (insert (format "%-9s %2d %s %4d\n"
  6612. (calendar-day-name date)
  6613. (extract-calendar-day date)
  6614. (calendar-month-name (extract-calendar-month date))
  6615. (extract-calendar-year date)))
  6616. (put-text-property s (1- (point)) 'face
  6617. 'org-level-3)
  6618. (if todayp (put-text-property s (1- (point)) 'org-today t))
  6619. (if rtnall (insert
  6620. (org-finalize-agenda-entries
  6621. (org-agenda-add-time-grid-maybe
  6622. rtnall nd todayp))
  6623. "\n"))
  6624. (put-text-property s (1- (point)) 'day d))))
  6625. (goto-char (point-min))
  6626. (org-fit-agenda-window)
  6627. (unless (and (pos-visible-in-window-p (point-min))
  6628. (pos-visible-in-window-p (point-max)))
  6629. (goto-char (1- (point-max)))
  6630. (recenter -1)
  6631. (if (not (pos-visible-in-window-p (or start-pos 1)))
  6632. (progn
  6633. (goto-char (or start-pos 1))
  6634. (recenter 1))))
  6635. (goto-char (or start-pos 1))
  6636. (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
  6637. (org-finalize-agenda)
  6638. (setq buffer-read-only t)
  6639. (if (not org-select-agenda-window) (select-window win))
  6640. (message "")))
  6641. (defvar org-select-this-todo-keyword nil)
  6642. ;;;###autoload
  6643. (defun org-todo-list (arg &optional keep-modes)
  6644. "Show all TODO entries from all agenda file in a single list.
  6645. The prefix arg can be used to select a specific TODO keyword and limit
  6646. the list to these. When using \\[universal-argument], you will be prompted
  6647. for a keyword. A numeric prefix directly selects the Nth keyword in
  6648. `org-todo-keywords'."
  6649. (interactive "P")
  6650. (org-compile-prefix-format 'todo)
  6651. (org-set-sorting-strategy 'todo)
  6652. (let* ((org-agenda-keep-modes keep-modes)
  6653. (today (time-to-days (current-time)))
  6654. (date (calendar-gregorian-from-absolute today))
  6655. (win (selected-window))
  6656. (kwds org-todo-keywords)
  6657. (completion-ignore-case t)
  6658. (org-select-this-todo-keyword
  6659. (if (stringp arg) arg
  6660. (and arg (integerp arg) (> arg 0)
  6661. (nth (1- arg) org-todo-keywords))))
  6662. rtn rtnall files file pos)
  6663. (when (equal arg '(4))
  6664. (setq org-select-this-todo-keyword
  6665. (completing-read "Keyword: " (mapcar 'list org-todo-keywords)
  6666. nil t)))
  6667. (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
  6668. (org-prepare-agenda)
  6669. (org-set-local 'last-arg arg)
  6670. (org-set-local 'org-todo-keywords kwds)
  6671. (setq org-agenda-redo-command
  6672. '(org-todo-list (or current-prefix-arg last-arg) t))
  6673. (setq files (org-agenda-files)
  6674. rtnall nil)
  6675. (while (setq file (pop files))
  6676. (catch 'nextfile
  6677. (org-check-agenda-file file)
  6678. (setq rtn (org-agenda-get-day-entries file date :todo))
  6679. (setq rtnall (append rtnall rtn))))
  6680. (insert "Global list of TODO items of type: ")
  6681. (add-text-properties (point-min) (1- (point))
  6682. (list 'face 'org-level-3))
  6683. (setq pos (point))
  6684. (insert (or org-select-this-todo-keyword "ALL") "\n")
  6685. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  6686. (setq pos (point))
  6687. (unless org-agenda-multi
  6688. (insert
  6689. "Available with `N r': (0)ALL "
  6690. (let ((n 0))
  6691. (mapconcat (lambda (x)
  6692. (format "(%d)%s" (setq n (1+ n)) x))
  6693. org-todo-keywords " "))
  6694. "\n"))
  6695. (add-text-properties pos (1- (point)) (list 'face 'org-level-3))
  6696. (when rtnall
  6697. (insert (org-finalize-agenda-entries rtnall) "\n"))
  6698. (goto-char (point-min))
  6699. (org-fit-agenda-window)
  6700. (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
  6701. (org-finalize-agenda)
  6702. (setq buffer-read-only t)
  6703. (if (not org-select-agenda-window) (select-window win))))
  6704. (defun org-check-agenda-file (file)
  6705. "Make sure FILE exists. If not, ask user what to do."
  6706. (when (not (file-exists-p file))
  6707. (message "non-existent file %s. [R]emove from list or [A]bort?"
  6708. (abbreviate-file-name file))
  6709. (let ((r (downcase (read-char-exclusive))))
  6710. (cond
  6711. ((equal r ?r)
  6712. (org-remove-file file)
  6713. (throw 'nextfile t))
  6714. (t (error "Abort"))))))
  6715. (defun org-agenda-check-type (error &rest types)
  6716. "Check if agenda buffer is of allowed type.
  6717. If ERROR is non-nil, throw an error, otherwise just return nil."
  6718. (if (memq org-agenda-type types)
  6719. t
  6720. (if error
  6721. (error "Not allowed in %s-type agenda buffers" org-agenda-type)
  6722. nil)))
  6723. (defun org-agenda-quit ()
  6724. "Exit agenda by removing the window or the buffer."
  6725. (interactive)
  6726. (let ((buf (current-buffer)))
  6727. (if (not (one-window-p)) (delete-window))
  6728. (kill-buffer buf)
  6729. (org-agenda-maybe-reset-markers 'force)))
  6730. (defun org-agenda-exit ()
  6731. "Exit agenda by removing the window or the buffer.
  6732. Also kill all Org-mode buffers which have been loaded by `org-agenda'.
  6733. Org-mode buffers visited directly by the user will not be touched."
  6734. (interactive)
  6735. (org-release-buffers org-agenda-new-buffers)
  6736. (setq org-agenda-new-buffers nil)
  6737. (org-agenda-quit))
  6738. (defun org-save-all-org-buffers ()
  6739. "Save all Org-mode buffers without user confirmation."
  6740. (interactive)
  6741. (message "Saving all Org-mode buffers...")
  6742. (save-some-buffers t 'org-mode-p)
  6743. (message "Saving all Org-mode buffers... done"))
  6744. (defun org-agenda-redo ()
  6745. "Rebuild Agenda.
  6746. When this is the global TODO list, a prefix argument will be interpreted."
  6747. (interactive)
  6748. (let* ((line (org-current-line))
  6749. (window-line (- line (org-current-line (window-start)))))
  6750. (message "Rebuilding agenda buffer...")
  6751. (eval org-agenda-redo-command)
  6752. (message "Rebuilding agenda buffer...done")
  6753. (goto-line line)
  6754. (recenter window-line)))
  6755. (defun org-agenda-goto-today ()
  6756. "Go to today."
  6757. (interactive)
  6758. (org-agenda-check-type t 'timeline 'agenda)
  6759. (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
  6760. (cond
  6761. (tdpos (goto-char tdpos))
  6762. ((eq org-agenda-type 'agenda)
  6763. (let ((org-agenda-overriding-arguments org-agenda-last-arguments))
  6764. (setf (nth 1 org-agenda-overriding-arguments) nil)
  6765. (org-agenda-redo)
  6766. (org-agenda-find-today-or-agenda)))
  6767. (t (error "Cannot find today")))))
  6768. (defun org-agenda-find-today-or-agenda ()
  6769. (goto-char
  6770. (or (text-property-any (point-min) (point-max) 'org-today t)
  6771. (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
  6772. (point-min))))
  6773. (defun org-agenda-later (arg)
  6774. "Go forward in time by `org-agenda-ndays' days.
  6775. With prefix ARG, go forward that many times `org-agenda-ndays'."
  6776. (interactive "p")
  6777. (org-agenda-check-type t 'agenda)
  6778. (let ((org-agenda-overriding-arguments
  6779. (list (car org-agenda-last-arguments)
  6780. (+ starting-day (* arg org-agenda-ndays))
  6781. nil t)))
  6782. (org-agenda-redo)
  6783. (org-agenda-find-today-or-agenda)))
  6784. (defun org-agenda-earlier (arg)
  6785. "Go back in time by `org-agenda-ndays' days.
  6786. With prefix ARG, go back that many times `org-agenda-ndays'."
  6787. (interactive "p")
  6788. (org-agenda-check-type t 'agenda)
  6789. (let ((org-agenda-overriding-arguments
  6790. (list (car org-agenda-last-arguments)
  6791. (- starting-day (* arg org-agenda-ndays))
  6792. nil t)))
  6793. (org-agenda-redo)
  6794. (org-agenda-find-today-or-agenda)))
  6795. (defun org-agenda-week-view ()
  6796. "Switch to weekly view for agenda."
  6797. (interactive)
  6798. (org-agenda-check-type t 'agenda)
  6799. (if (= org-agenda-ndays 7)
  6800. (error "This is already the week view"))
  6801. (setq org-agenda-ndays 7)
  6802. (let ((org-agenda-overriding-arguments
  6803. (list (car org-agenda-last-arguments)
  6804. (or (get-text-property (point) 'day)
  6805. starting-day)
  6806. nil t)))
  6807. (org-agenda-redo)
  6808. (org-agenda-find-today-or-agenda))
  6809. (org-agenda-set-mode-name)
  6810. (message "Switched to week view"))
  6811. (defun org-agenda-day-view ()
  6812. "Switch to daily view for agenda."
  6813. (interactive)
  6814. (org-agenda-check-type t 'agenda)
  6815. (if (= org-agenda-ndays 1)
  6816. (error "This is already the day view"))
  6817. (setq org-agenda-ndays 1)
  6818. (let ((org-agenda-overriding-arguments
  6819. (list (car org-agenda-last-arguments)
  6820. (or (get-text-property (point) 'day)
  6821. starting-day)
  6822. nil t)))
  6823. (org-agenda-redo)
  6824. (org-agenda-find-today-or-agenda))
  6825. (org-agenda-set-mode-name)
  6826. (message "Switched to day view"))
  6827. (defun org-agenda-next-date-line (&optional arg)
  6828. "Jump to the next line indicating a date in agenda buffer."
  6829. (interactive "p")
  6830. (org-agenda-check-type t 'agenda 'timeline)
  6831. (beginning-of-line 1)
  6832. (if (looking-at "^\\S-") (forward-char 1))
  6833. (if (not (re-search-forward "^\\S-" nil t arg))
  6834. (progn
  6835. (backward-char 1)
  6836. (error "No next date after this line in this buffer")))
  6837. (goto-char (match-beginning 0)))
  6838. (defun org-agenda-previous-date-line (&optional arg)
  6839. "Jump to the previous line indicating a date in agenda buffer."
  6840. (interactive "p")
  6841. (org-agenda-check-type t 'agenda 'timeline)
  6842. (beginning-of-line 1)
  6843. (if (not (re-search-backward "^\\S-" nil t arg))
  6844. (error "No previous date before this line in this buffer")))
  6845. ;; Initialize the highlight
  6846. (defvar org-hl (org-make-overlay 1 1))
  6847. (org-overlay-put org-hl 'face 'highlight)
  6848. (defun org-highlight (begin end &optional buffer)
  6849. "Highlight a region with overlay."
  6850. (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
  6851. org-hl begin end (or buffer (current-buffer))))
  6852. (defun org-unhighlight ()
  6853. "Detach overlay INDEX."
  6854. (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
  6855. (defun org-agenda-follow-mode ()
  6856. "Toggle follow mode in an agenda buffer."
  6857. (interactive)
  6858. (setq org-agenda-follow-mode (not org-agenda-follow-mode))
  6859. (org-agenda-set-mode-name)
  6860. (message "Follow mode is %s"
  6861. (if org-agenda-follow-mode "on" "off")))
  6862. (defun org-agenda-log-mode ()
  6863. "Toggle log mode in an agenda buffer."
  6864. (interactive)
  6865. (org-agenda-check-type t 'agenda 'timeline)
  6866. (setq org-agenda-show-log (not org-agenda-show-log))
  6867. (org-agenda-set-mode-name)
  6868. (org-agenda-redo)
  6869. (message "Log mode is %s"
  6870. (if org-agenda-show-log "on" "off")))
  6871. (defun org-agenda-toggle-diary ()
  6872. "Toggle diary inclusion in an agenda buffer."
  6873. (interactive)
  6874. (org-agenda-check-type t 'agenda)
  6875. (setq org-agenda-include-diary (not org-agenda-include-diary))
  6876. (org-agenda-redo)
  6877. (org-agenda-set-mode-name)
  6878. (message "Diary inclusion turned %s"
  6879. (if org-agenda-include-diary "on" "off")))
  6880. (defun org-agenda-toggle-time-grid ()
  6881. "Toggle time grid in an agenda buffer."
  6882. (interactive)
  6883. (org-agenda-check-type t 'agenda)
  6884. (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
  6885. (org-agenda-redo)
  6886. (org-agenda-set-mode-name)
  6887. (message "Time-grid turned %s"
  6888. (if org-agenda-use-time-grid "on" "off")))
  6889. (defun org-agenda-set-mode-name ()
  6890. "Set the mode name to indicate all the small mode settings."
  6891. (setq mode-name
  6892. (concat "Org-Agenda"
  6893. (if (equal org-agenda-ndays 1) " Day" "")
  6894. (if (equal org-agenda-ndays 7) " Week" "")
  6895. (if org-agenda-follow-mode " Follow" "")
  6896. (if org-agenda-include-diary " Diary" "")
  6897. (if org-agenda-use-time-grid " Grid" "")
  6898. (if org-agenda-show-log " Log" "")))
  6899. (force-mode-line-update))
  6900. (defun org-agenda-post-command-hook ()
  6901. (and (eolp) (not (bolp)) (backward-char 1))
  6902. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  6903. (if (and org-agenda-follow-mode
  6904. (get-text-property (point) 'org-marker))
  6905. (org-agenda-show)))
  6906. (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
  6907. (defun org-get-entries-from-diary (date)
  6908. "Get the (Emacs Calendar) diary entries for DATE."
  6909. (let* ((fancy-diary-buffer "*temporary-fancy-diary-buffer*")
  6910. (diary-display-hook '(fancy-diary-display))
  6911. (list-diary-entries-hook
  6912. (cons 'org-diary-default-entry list-diary-entries-hook))
  6913. (diary-file-name-prefix-function nil) ; turn this feature off
  6914. (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
  6915. entries
  6916. (org-disable-agenda-to-diary t))
  6917. (save-excursion
  6918. (save-window-excursion
  6919. (list-diary-entries date 1))) ;; Keep this name for now, compatibility
  6920. (if (not (get-buffer fancy-diary-buffer))
  6921. (setq entries nil)
  6922. (with-current-buffer fancy-diary-buffer
  6923. (setq buffer-read-only nil)
  6924. (if (= (point-max) 1)
  6925. ;; No entries
  6926. (setq entries nil)
  6927. ;; Omit the date and other unnecessary stuff
  6928. (org-agenda-cleanup-fancy-diary)
  6929. ;; Add prefix to each line and extend the text properties
  6930. (if (= (point-max) 1)
  6931. (setq entries nil)
  6932. (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
  6933. (set-buffer-modified-p nil)
  6934. (kill-buffer fancy-diary-buffer)))
  6935. (when entries
  6936. (setq entries (org-split-string entries "\n"))
  6937. (setq entries
  6938. (mapcar
  6939. (lambda (x)
  6940. (setq x (org-format-agenda-item "" x "Diary" nil 'time))
  6941. ;; Extend the text properties to the beginning of the line
  6942. (org-add-props x (text-properties-at (1- (length x)) x)))
  6943. entries)))))
  6944. (defun org-agenda-cleanup-fancy-diary ()
  6945. "Remove unwanted stuff in buffer created by `fancy-diary-display'.
  6946. This gets rid of the date, the underline under the date, and
  6947. the dummy entry installed by `org-mode' to ensure non-empty diary for each
  6948. date. It also removes lines that contain only whitespace."
  6949. (goto-char (point-min))
  6950. (if (looking-at ".*?:[ \t]*")
  6951. (progn
  6952. (replace-match "")
  6953. (re-search-forward "\n=+$" nil t)
  6954. (replace-match "")
  6955. (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
  6956. (re-search-forward "\n=+$" nil t)
  6957. (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
  6958. (goto-char (point-min))
  6959. (while (re-search-forward "^ +\n" nil t)
  6960. (replace-match ""))
  6961. (goto-char (point-min))
  6962. (if (re-search-forward "^Org-mode dummy\n?" nil t)
  6963. (replace-match "")))
  6964. ;; Make sure entries from the diary have the right text properties.
  6965. (eval-after-load "diary-lib"
  6966. '(if (boundp 'diary-modify-entry-list-string-function)
  6967. ;; We can rely on the hook, nothing to do
  6968. nil
  6969. ;; Hook not avaiable, must use advice to make this work
  6970. (defadvice add-to-diary-list (before org-mark-diary-entry activate)
  6971. "Make the position visible."
  6972. (if (and org-disable-agenda-to-diary ;; called from org-agenda
  6973. (stringp string)
  6974. buffer-file-name)
  6975. (setq string (org-modify-diary-entry-string string))))))
  6976. (defun org-modify-diary-entry-string (string)
  6977. "Add text properties to string, allowing org-mode to act on it."
  6978. (org-add-props string nil
  6979. 'mouse-face 'highlight
  6980. 'keymap org-agenda-keymap
  6981. 'help-echo (format "mouse-2 or RET jump to diary file %s"
  6982. (abbreviate-file-name buffer-file-name))
  6983. 'org-agenda-diary-link t
  6984. 'org-marker (org-agenda-new-marker (point-at-bol))))
  6985. (defun org-diary-default-entry ()
  6986. "Add a dummy entry to the diary.
  6987. Needed to avoid empty dates which mess up holiday display."
  6988. ;; Catch the error if dealing with the new add-to-diary-alist
  6989. (when org-disable-agenda-to-diary
  6990. (condition-case nil
  6991. (add-to-diary-list original-date "Org-mode dummy" "")
  6992. (error
  6993. (add-to-diary-list original-date "Org-mode dummy" "" nil)))))
  6994. (defun org-cycle-agenda-files ()
  6995. "Cycle through the files in `org-agenda-files'.
  6996. If the current buffer visits an agenda file, find the next one in the list.
  6997. If the current buffer does not, find the first agenda file."
  6998. (interactive)
  6999. (let* ((fs (org-agenda-files t))
  7000. (files (append fs (list (car fs))))
  7001. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  7002. file)
  7003. (unless files (error "No agenda files"))
  7004. (catch 'exit
  7005. (while (setq file (pop files))
  7006. (if (equal (file-truename file) tcf)
  7007. (when (car files)
  7008. (find-file (car files))
  7009. (throw 'exit t))))
  7010. (find-file (car fs)))))
  7011. (defun org-agenda-file-to-end ()
  7012. "Move/add the current file to the end of the agenda file list.
  7013. If the file is not present in the list, it is appended to the list. If it is
  7014. present, it is moved there."
  7015. (interactive)
  7016. (org-agenda-file-to-front 'to-end))
  7017. (defun org-agenda-file-to-front (&optional to-end)
  7018. "Move/add the current file to the top of the agenda file list.
  7019. If the file is not present in the list, it is added to the front. If it is
  7020. present, it is moved there. With optional argument TO-END, add/move to the
  7021. end of the list."
  7022. (interactive "P")
  7023. (let ((file-alist (mapcar (lambda (x)
  7024. (cons (file-truename x) x))
  7025. (org-agenda-files t)))
  7026. (ctf (file-truename buffer-file-name))
  7027. x had)
  7028. (setq x (assoc ctf file-alist) had x)
  7029. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  7030. (if to-end
  7031. (setq file-alist (append (delq x file-alist) (list x)))
  7032. (setq file-alist (cons x (delq x file-alist))))
  7033. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  7034. (org-install-agenda-files-menu)
  7035. (message "File %s to %s of agenda file list"
  7036. (if had "moved" "added") (if to-end "end" "front"))))
  7037. (defun org-remove-file (&optional file)
  7038. "Remove current file from the list of files in variable `org-agenda-files'.
  7039. These are the files which are being checked for agenda entries.
  7040. Optional argument FILE means, use this file instead of the current."
  7041. (interactive)
  7042. (let* ((file (or file buffer-file-name))
  7043. (true-file (file-truename file))
  7044. (afile (abbreviate-file-name file))
  7045. (files (delq nil (mapcar
  7046. (lambda (x)
  7047. (if (equal true-file
  7048. (file-truename x))
  7049. nil x))
  7050. (org-agenda-files t)))))
  7051. (if (not (= (length files) (length (org-agenda-files t))))
  7052. (progn
  7053. (org-store-new-agenda-file-list files)
  7054. (org-install-agenda-files-menu)
  7055. (message "Removed file: %s" afile))
  7056. (message "File was not in list: %s" afile))))
  7057. (defun org-file-menu-entry (file)
  7058. (vector file (list 'find-file file) t))
  7059. (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty)
  7060. "Return a list of all relevant day numbers from BEG to END buffer positions.
  7061. If NO-RANGES is non-nil, include only the start and end dates of a range,
  7062. not every single day in the range. If FORCE-TODAY is non-nil, make
  7063. sure that TODAY is included in the list. If INACTIVE is non-nil, also
  7064. inactive time stamps (those in square brackets) are included.
  7065. When EMPTY is non-nil, also include days without any entries."
  7066. (let ((re (if inactive org-ts-regexp-both org-ts-regexp))
  7067. dates dates1 date day day1 day2 ts1 ts2)
  7068. (if force-today
  7069. (setq dates (list (time-to-days (current-time)))))
  7070. (save-excursion
  7071. (goto-char beg)
  7072. (while (re-search-forward re end t)
  7073. (setq day (time-to-days (org-time-string-to-time
  7074. (substring (match-string 1) 0 10))))
  7075. (or (memq day dates) (push day dates)))
  7076. (unless no-ranges
  7077. (goto-char beg)
  7078. (while (re-search-forward org-tr-regexp end t)
  7079. (setq ts1 (substring (match-string 1) 0 10)
  7080. ts2 (substring (match-string 2) 0 10)
  7081. day1 (time-to-days (org-time-string-to-time ts1))
  7082. day2 (time-to-days (org-time-string-to-time ts2)))
  7083. (while (< (setq day1 (1+ day1)) day2)
  7084. (or (memq day1 dates) (push day1 dates)))))
  7085. (setq dates (sort dates '<))
  7086. (when empty
  7087. (while (setq day (pop dates))
  7088. (setq day2 (car dates))
  7089. (push day dates1)
  7090. (when (and day2 empty)
  7091. (if (or (eq empty t)
  7092. (and (numberp empty) (<= (- day2 day) empty)))
  7093. (while (< (setq day (1+ day)) day2)
  7094. (push (list day) dates1))
  7095. (push (cons :omitted (- day2 day)) dates1))))
  7096. (setq dates (nreverse dates1)))
  7097. dates)))
  7098. ;;;###autoload
  7099. (defun org-diary (&rest args)
  7100. "Return diary information from org-files.
  7101. This function can be used in a \"sexp\" diary entry in the Emacs calendar.
  7102. It accesses org files and extracts information from those files to be
  7103. listed in the diary. The function accepts arguments specifying what
  7104. items should be listed. The following arguments are allowed:
  7105. :timestamp List the headlines of items containing a date stamp or
  7106. date range matching the selected date. Deadlines will
  7107. also be listed, on the expiration day.
  7108. :deadline List any deadlines past due, or due within
  7109. `org-deadline-warning-days'. The listing occurs only
  7110. in the diary for *today*, not at any other date. If
  7111. an entry is marked DONE, it is no longer listed.
  7112. :scheduled List all items which are scheduled for the given date.
  7113. The diary for *today* also contains items which were
  7114. scheduled earlier and are not yet marked DONE.
  7115. :todo List all TODO items from the org-file. This may be a
  7116. long list - so this is not turned on by default.
  7117. Like deadlines, these entries only show up in the
  7118. diary for *today*, not at any other date.
  7119. The call in the diary file should look like this:
  7120. &%%(org-diary) ~/path/to/some/orgfile.org
  7121. Use a separate line for each org file to check. Or, if you omit the file name,
  7122. all files listed in `org-agenda-files' will be checked automatically:
  7123. &%%(org-diary)
  7124. If you don't give any arguments (as in the example above), the default
  7125. arguments (:deadline :scheduled :timestamp) are used. So the example above may
  7126. also be written as
  7127. &%%(org-diary :deadline :timestamp :scheduled)
  7128. The function expects the lisp variables `entry' and `date' to be provided
  7129. by the caller, because this is how the calendar works. Don't use this
  7130. function from a program - use `org-agenda-get-day-entries' instead."
  7131. (org-agenda-maybe-reset-markers)
  7132. (org-compile-prefix-format 'agenda)
  7133. (org-set-sorting-strategy 'agenda)
  7134. (setq args (or args '(:deadline :scheduled :timestamp)))
  7135. (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
  7136. (list entry)
  7137. (org-agenda-files t)))
  7138. file rtn results)
  7139. ;; If this is called during org-agenda, don't return any entries to
  7140. ;; the calendar. Org Agenda will list these entries itself.
  7141. (if org-disable-agenda-to-diary (setq files nil))
  7142. (while (setq file (pop files))
  7143. (setq rtn (apply 'org-agenda-get-day-entries file date args))
  7144. (setq results (append results rtn)))
  7145. (if results
  7146. (concat (org-finalize-agenda-entries results) "\n"))))
  7147. (defvar org-category-table nil)
  7148. (defun org-get-category-table ()
  7149. "Get the table of categories and positions in current buffer."
  7150. (let (tbl)
  7151. (save-excursion
  7152. (goto-char (point-min))
  7153. (while (re-search-forward "\\(^\\|\r\\)#\\+CATEGORY:[ \t]*\\(.*\\)" nil t)
  7154. (push (cons (point) (org-trim (match-string 2))) tbl)))
  7155. tbl))
  7156. (defun org-get-category (&optional pos)
  7157. "Get the category applying to position POS."
  7158. (if (not org-category-table)
  7159. (cond
  7160. ((null org-category)
  7161. (setq org-category
  7162. (if buffer-file-name
  7163. (file-name-sans-extension
  7164. (file-name-nondirectory buffer-file-name))
  7165. "???")))
  7166. ((symbolp org-category) (symbol-name org-category))
  7167. (t org-category))
  7168. (let ((tbl org-category-table)
  7169. (pos (or pos (point))))
  7170. (while (and tbl (> (caar tbl) pos))
  7171. (pop tbl))
  7172. (or (cdar tbl) (cdr (nth (1- (length org-category-table))
  7173. org-category-table))))))
  7174. (defun org-agenda-get-day-entries (file date &rest args)
  7175. "Does the work for `org-diary' and `org-agenda'.
  7176. FILE is the path to a file to be checked for entries. DATE is date like
  7177. the one returned by `calendar-current-date'. ARGS are symbols indicating
  7178. which kind of entries should be extracted. For details about these, see
  7179. the documentation of `org-diary'."
  7180. (setq args (or args '(:deadline :scheduled :timestamp)))
  7181. (let* ((org-startup-with-deadline-check nil)
  7182. (org-startup-folded nil)
  7183. (org-startup-align-all-tables nil)
  7184. (buffer (if (file-exists-p file)
  7185. (org-get-agenda-file-buffer file)
  7186. (error "No such file %s" file)))
  7187. arg results rtn)
  7188. (if (not buffer)
  7189. ;; If file does not exist, make sure an error message ends up in diary
  7190. (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  7191. (with-current-buffer buffer
  7192. (unless (org-mode-p)
  7193. (error "Agenda file %s is not in `org-mode'" file))
  7194. (setq org-category-table (org-get-category-table))
  7195. (let ((case-fold-search nil))
  7196. (save-excursion
  7197. (save-restriction
  7198. (if org-agenda-restrict
  7199. (narrow-to-region org-agenda-restrict-begin
  7200. org-agenda-restrict-end)
  7201. (widen))
  7202. ;; The way we repeatedly append to `results' makes it O(n^2) :-(
  7203. (while (setq arg (pop args))
  7204. (cond
  7205. ((and (eq arg :todo)
  7206. (equal date (calendar-current-date)))
  7207. (setq rtn (org-agenda-get-todos))
  7208. (setq results (append results rtn)))
  7209. ((eq arg :timestamp)
  7210. (setq rtn (org-agenda-get-blocks))
  7211. (setq results (append results rtn))
  7212. (setq rtn (org-agenda-get-timestamps))
  7213. (setq results (append results rtn)))
  7214. ((eq arg :scheduled)
  7215. (setq rtn (org-agenda-get-scheduled))
  7216. (setq results (append results rtn)))
  7217. ((eq arg :closed)
  7218. (setq rtn (org-agenda-get-closed))
  7219. (setq results (append results rtn)))
  7220. ((and (eq arg :deadline)
  7221. (equal date (calendar-current-date)))
  7222. (setq rtn (org-agenda-get-deadlines))
  7223. (setq results (append results rtn))))))))
  7224. results))))
  7225. (defun org-entry-is-done-p ()
  7226. "Is the current entry marked DONE?"
  7227. (save-excursion
  7228. (and (re-search-backward "[\r\n]\\*" nil t)
  7229. (looking-at org-nl-done-regexp))))
  7230. (defun org-at-date-range-p ()
  7231. "Is the cursor inside a date range?"
  7232. (interactive)
  7233. (save-excursion
  7234. (catch 'exit
  7235. (let ((pos (point)))
  7236. (skip-chars-backward "^<\r\n")
  7237. (skip-chars-backward "<")
  7238. (and (looking-at org-tr-regexp)
  7239. (>= (match-end 0) pos)
  7240. (throw 'exit t))
  7241. (skip-chars-backward "^<\r\n")
  7242. (skip-chars-backward "<")
  7243. (and (looking-at org-tr-regexp)
  7244. (>= (match-end 0) pos)
  7245. (throw 'exit t)))
  7246. nil)))
  7247. (defun org-agenda-get-todos ()
  7248. "Return the TODO information for agenda display."
  7249. (let* ((props (list 'face nil
  7250. 'done-face 'org-done
  7251. 'org-not-done-regexp org-not-done-regexp
  7252. 'mouse-face 'highlight
  7253. 'keymap org-agenda-keymap
  7254. 'help-echo
  7255. (format "mouse-2 or RET jump to org file %s"
  7256. (abbreviate-file-name buffer-file-name))))
  7257. (regexp (concat "[\n\r]\\*+ *\\("
  7258. (if org-select-this-todo-keyword
  7259. (concat "\\<\\(" org-select-this-todo-keyword
  7260. "\\)\\>")
  7261. org-not-done-regexp)
  7262. "[^\n\r]*\\)"))
  7263. (sched-re (concat ".*\n?.*?" org-scheduled-time-regexp))
  7264. marker priority category tags
  7265. ee txt)
  7266. (goto-char (point-min))
  7267. (while (re-search-forward regexp nil t)
  7268. (catch :skip
  7269. (when (and org-agenda-todo-ignore-scheduled
  7270. (looking-at sched-re))
  7271. ;; FIXME: the following test also happens below, but we need it here
  7272. (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
  7273. (throw :skip nil))
  7274. (org-agenda-skip)
  7275. (goto-char (match-beginning 1))
  7276. (setq marker (org-agenda-new-marker (1+ (match-beginning 0)))
  7277. category (org-get-category)
  7278. tags (org-get-tags-at (point))
  7279. txt (org-format-agenda-item "" (match-string 1) category tags)
  7280. priority
  7281. (+ (org-get-priority txt)
  7282. (if org-todo-kwd-priority-p
  7283. (- org-todo-kwd-max-priority -2
  7284. (length
  7285. (member (match-string 2) org-todo-keywords)))
  7286. 1)))
  7287. (org-add-props txt props
  7288. 'org-marker marker 'org-hd-marker marker
  7289. 'priority priority 'category category)
  7290. (push txt ee)
  7291. (if org-agenda-todo-list-sublevels
  7292. (goto-char (match-end 1))
  7293. (org-end-of-subtree 'invisible))))
  7294. (nreverse ee)))
  7295. (defconst org-agenda-no-heading-message
  7296. "No heading for this item in buffer or region.")
  7297. (defun org-agenda-get-timestamps ()
  7298. "Return the date stamp information for agenda display."
  7299. (let* ((props (list 'face nil
  7300. 'org-not-done-regexp org-not-done-regexp
  7301. 'mouse-face 'highlight
  7302. 'keymap org-agenda-keymap
  7303. 'help-echo
  7304. (format "mouse-2 or RET jump to org file %s"
  7305. (abbreviate-file-name buffer-file-name))))
  7306. (regexp (regexp-quote
  7307. (substring
  7308. (format-time-string
  7309. (car org-time-stamp-formats)
  7310. (apply 'encode-time ; DATE bound by calendar
  7311. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  7312. 0 11)))
  7313. marker hdmarker deadlinep scheduledp donep tmp priority category
  7314. ee txt timestr tags)
  7315. (goto-char (point-min))
  7316. (while (re-search-forward regexp nil t)
  7317. (catch :skip
  7318. (and (save-match-data (org-at-date-range-p)) (throw :skip nil))
  7319. (org-agenda-skip)
  7320. (setq marker (org-agenda-new-marker (match-beginning 0))
  7321. category (org-get-category (match-beginning 0))
  7322. tmp (buffer-substring (max (point-min)
  7323. (- (match-beginning 0)
  7324. org-ds-keyword-length))
  7325. (match-beginning 0))
  7326. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  7327. deadlinep (string-match org-deadline-regexp tmp)
  7328. scheduledp (string-match org-scheduled-regexp tmp)
  7329. donep (org-entry-is-done-p))
  7330. (if (string-match ">" timestr)
  7331. ;; substring should only run to end of time stamp
  7332. (setq timestr (substring timestr 0 (match-end 0))))
  7333. (save-excursion
  7334. (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t)
  7335. (progn
  7336. (goto-char (match-end 1))
  7337. (setq hdmarker (org-agenda-new-marker)
  7338. tags (org-get-tags-at))
  7339. (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
  7340. (setq txt (org-format-agenda-item
  7341. (format "%s%s"
  7342. (if deadlinep "Deadline: " "")
  7343. (if scheduledp "Scheduled: " ""))
  7344. (match-string 1) category tags timestr)))
  7345. (setq txt org-agenda-no-heading-message))
  7346. (setq priority (org-get-priority txt))
  7347. (org-add-props txt props
  7348. 'org-marker marker 'org-hd-marker hdmarker)
  7349. (if deadlinep
  7350. (org-add-props txt nil
  7351. 'face (if donep 'org-done 'org-warning)
  7352. 'undone-face 'org-warning 'done-face 'org-done
  7353. 'category category 'priority (+ 100 priority))
  7354. (if scheduledp
  7355. (org-add-props txt nil
  7356. 'face 'org-scheduled-today
  7357. 'undone-face 'org-scheduled-today 'done-face 'org-done
  7358. 'category category 'priority (+ 99 priority))
  7359. (org-add-props txt nil 'priority priority 'category category)))
  7360. (push txt ee))
  7361. (outline-next-heading)))
  7362. (nreverse ee)))
  7363. (defun org-agenda-get-closed ()
  7364. "Return the logged TODO entries for agenda display."
  7365. (let* ((props (list 'mouse-face 'highlight
  7366. 'org-not-done-regexp org-not-done-regexp
  7367. 'keymap org-agenda-keymap
  7368. 'help-echo
  7369. (format "mouse-2 or RET jump to org file %s"
  7370. (abbreviate-file-name buffer-file-name))))
  7371. (regexp (concat
  7372. "\\<\\(" org-closed-string "\\|" org-clock-string "\\) *\\["
  7373. (regexp-quote
  7374. (substring
  7375. (format-time-string
  7376. (car org-time-stamp-formats)
  7377. (apply 'encode-time ; DATE bound by calendar
  7378. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  7379. 1 11))))
  7380. marker hdmarker priority category tags closedp
  7381. ee txt timestr)
  7382. (goto-char (point-min))
  7383. (while (re-search-forward regexp nil t)
  7384. (catch :skip
  7385. (org-agenda-skip)
  7386. (setq marker (org-agenda-new-marker (match-beginning 0))
  7387. closedp (equal (match-string 1) org-closed-string)
  7388. category (org-get-category (match-beginning 0))
  7389. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  7390. ;; donep (org-entry-is-done-p)
  7391. )
  7392. (if (string-match "\\]" timestr)
  7393. ;; substring should only run to end of time stamp
  7394. (setq timestr (substring timestr 0 (match-end 0))))
  7395. (save-excursion
  7396. (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t)
  7397. (progn
  7398. (goto-char (match-end 1))
  7399. (setq hdmarker (org-agenda-new-marker)
  7400. tags (org-get-tags-at))
  7401. (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
  7402. (setq txt (org-format-agenda-item
  7403. (if closedp "Closed: " "Clocked: ")
  7404. (match-string 1) category tags timestr)))
  7405. (setq txt org-agenda-no-heading-message))
  7406. (setq priority 100000)
  7407. (org-add-props txt props
  7408. 'org-marker marker 'org-hd-marker hdmarker 'face 'org-done
  7409. 'priority priority 'category category
  7410. 'undone-face 'org-warning 'done-face 'org-done)
  7411. (push txt ee))
  7412. (outline-next-heading)))
  7413. (nreverse ee)))
  7414. (defun org-agenda-get-deadlines ()
  7415. "Return the deadline information for agenda display."
  7416. (let* ((wdays org-deadline-warning-days)
  7417. (props (list 'mouse-face 'highlight
  7418. 'org-not-done-regexp org-not-done-regexp
  7419. 'keymap org-agenda-keymap
  7420. 'help-echo
  7421. (format "mouse-2 or RET jump to org file %s"
  7422. (abbreviate-file-name buffer-file-name))))
  7423. (regexp org-deadline-time-regexp)
  7424. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  7425. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  7426. d2 diff pos pos1 category tags
  7427. ee txt head face)
  7428. (goto-char (point-min))
  7429. (while (re-search-forward regexp nil t)
  7430. (catch :skip
  7431. (org-agenda-skip)
  7432. (setq pos (1- (match-beginning 1))
  7433. d2 (time-to-days
  7434. (org-time-string-to-time (match-string 1)))
  7435. diff (- d2 d1))
  7436. ;; When to show a deadline in the calendar:
  7437. ;; If the expiration is within wdays warning time.
  7438. ;; Past-due deadlines are only shown on the current date
  7439. (if (and (< diff wdays) todayp (not (= diff 0)))
  7440. (save-excursion
  7441. (setq category (org-get-category))
  7442. (if (re-search-backward "\\(^\\|\r\\)\\*+[ \t]*" nil t)
  7443. (progn
  7444. (goto-char (match-end 0))
  7445. (setq pos1 (match-end 1))
  7446. (setq tags (org-get-tags-at pos1))
  7447. (setq head (buffer-substring-no-properties
  7448. (point)
  7449. (progn (skip-chars-forward "^\r\n")
  7450. (point))))
  7451. (if (string-match org-looking-at-done-regexp head)
  7452. (setq txt nil)
  7453. (setq txt (org-format-agenda-item
  7454. (format "In %3d d.: " diff) head category tags))))
  7455. (setq txt org-agenda-no-heading-message))
  7456. (when txt
  7457. (setq face (cond ((<= diff 0) 'org-warning)
  7458. ((<= diff 5) 'org-upcoming-deadline)
  7459. (t nil)))
  7460. (org-add-props txt props
  7461. 'org-marker (org-agenda-new-marker pos)
  7462. 'org-hd-marker (org-agenda-new-marker pos1)
  7463. 'priority (+ (- 10 diff) (org-get-priority txt))
  7464. 'category category
  7465. 'face face 'undone-face face 'done-face 'org-done)
  7466. (push txt ee))))))
  7467. ee))
  7468. (defun org-agenda-get-scheduled ()
  7469. "Return the scheduled information for agenda display."
  7470. (let* ((props (list 'face 'org-scheduled-previously
  7471. 'org-not-done-regexp org-not-done-regexp
  7472. 'undone-face 'org-scheduled-previously
  7473. 'done-face 'org-done
  7474. 'mouse-face 'highlight
  7475. 'keymap org-agenda-keymap
  7476. 'help-echo
  7477. (format "mouse-2 or RET jump to org file %s"
  7478. (abbreviate-file-name buffer-file-name))))
  7479. (regexp org-scheduled-time-regexp)
  7480. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  7481. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  7482. d2 diff pos pos1 category tags
  7483. ee txt head)
  7484. (goto-char (point-min))
  7485. (while (re-search-forward regexp nil t)
  7486. (catch :skip
  7487. (org-agenda-skip)
  7488. (setq pos (1- (match-beginning 1))
  7489. d2 (time-to-days
  7490. (org-time-string-to-time (match-string 1)))
  7491. diff (- d2 d1))
  7492. ;; When to show a scheduled item in the calendar:
  7493. ;; If it is on or past the date.
  7494. (if (and (< diff 0) todayp)
  7495. (save-excursion
  7496. (setq category (org-get-category))
  7497. (if (re-search-backward "\\(^\\|\r\\)\\*+[ \t]*" nil t)
  7498. (progn
  7499. (goto-char (match-end 0))
  7500. (setq pos1 (match-end 1))
  7501. (setq tags (org-get-tags-at))
  7502. (setq head (buffer-substring-no-properties
  7503. (point)
  7504. (progn (skip-chars-forward "^\r\n") (point))))
  7505. (if (string-match org-looking-at-done-regexp head)
  7506. (setq txt nil)
  7507. (setq txt (org-format-agenda-item
  7508. (format "Sched.%2dx: " (- 1 diff)) head
  7509. category tags))))
  7510. (setq txt org-agenda-no-heading-message))
  7511. (when txt
  7512. (org-add-props txt props
  7513. 'org-marker (org-agenda-new-marker pos)
  7514. 'org-hd-marker (org-agenda-new-marker pos1)
  7515. 'priority (+ (- 5 diff) (org-get-priority txt))
  7516. 'category category)
  7517. (push txt ee))))))
  7518. ee))
  7519. (defun org-agenda-get-blocks ()
  7520. "Return the date-range information for agenda display."
  7521. (let* ((props (list 'face nil
  7522. 'org-not-done-regexp org-not-done-regexp
  7523. 'mouse-face 'highlight
  7524. 'keymap org-agenda-keymap
  7525. 'help-echo
  7526. (format "mouse-2 or RET jump to org file %s"
  7527. (abbreviate-file-name buffer-file-name))))
  7528. (regexp org-tr-regexp)
  7529. (d0 (calendar-absolute-from-gregorian date))
  7530. marker hdmarker ee txt d1 d2 s1 s2 timestr category tags pos)
  7531. (goto-char (point-min))
  7532. (while (re-search-forward regexp nil t)
  7533. (catch :skip
  7534. (org-agenda-skip)
  7535. (setq pos (point))
  7536. (setq timestr (match-string 0)
  7537. s1 (match-string 1)
  7538. s2 (match-string 2)
  7539. d1 (time-to-days (org-time-string-to-time s1))
  7540. d2 (time-to-days (org-time-string-to-time s2)))
  7541. (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
  7542. ;; Only allow days between the limits, because the normal
  7543. ;; date stamps will catch the limits.
  7544. (save-excursion
  7545. (setq marker (org-agenda-new-marker (point)))
  7546. (setq category (org-get-category))
  7547. (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t)
  7548. (progn
  7549. (setq hdmarker (org-agenda-new-marker (match-end 1)))
  7550. (goto-char (match-end 1))
  7551. (setq tags (org-get-tags-at))
  7552. (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
  7553. (setq txt (org-format-agenda-item
  7554. (format (if (= d1 d2) "" "(%d/%d): ")
  7555. (1+ (- d0 d1)) (1+ (- d2 d1)))
  7556. (match-string 1) category tags
  7557. (if (= d0 d1) timestr))))
  7558. (setq txt org-agenda-no-heading-message))
  7559. (org-add-props txt props
  7560. 'org-marker marker 'org-hd-marker hdmarker
  7561. 'priority (org-get-priority txt) 'category category)
  7562. (push txt ee)))
  7563. (goto-char pos)))
  7564. ;; Sort the entries by expiration date.
  7565. (nreverse ee)))
  7566. (defconst org-plain-time-of-day-regexp
  7567. (concat
  7568. "\\(\\<[012]?[0-9]"
  7569. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  7570. "\\(--?"
  7571. "\\(\\<[012]?[0-9]"
  7572. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  7573. "\\)?")
  7574. "Regular expression to match a plain time or time range.
  7575. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  7576. groups carry important information:
  7577. 0 the full match
  7578. 1 the first time, range or not
  7579. 8 the second time, if it is a range.")
  7580. (defconst org-stamp-time-of-day-regexp
  7581. (concat
  7582. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  7583. "\\([012][0-9]:[0-5][0-9]\\)>"
  7584. "\\(--?"
  7585. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  7586. "Regular expression to match a timestamp time or time range.
  7587. After a match, the following groups carry important information:
  7588. 0 the full match
  7589. 1 date plus weekday, for backreferencing to make sure both times on same day
  7590. 2 the first time, range or not
  7591. 4 the second time, if it is a range.")
  7592. (defvar org-prefix-has-time nil
  7593. "A flag, set by `org-compile-prefix-format'.
  7594. The flag is set if the currently compiled format contains a `%t'.")
  7595. (defvar org-prefix-has-tag nil
  7596. "A flag, set by `org-compile-prefix-format'.
  7597. The flag is set if the currently compiled format contains a `%T'.")
  7598. (defun org-format-agenda-item (extra txt &optional category tags dotime
  7599. noprefix)
  7600. "Format TXT to be inserted into the agenda buffer.
  7601. In particular, it adds the prefix and corresponding text properties. EXTRA
  7602. must be a string and replaces the `%s' specifier in the prefix format.
  7603. CATEGORY (string, symbol or nil) may be used to overrule the default
  7604. category taken from local variable or file name. It will replace the `%c'
  7605. specifier in the format. DOTIME, when non-nil, indicates that a
  7606. time-of-day should be extracted from TXT for sorting of this entry, and for
  7607. the `%t' specifier in the format. When DOTIME is a string, this string is
  7608. searched for a time before TXT is. NOPREFIX is a flag and indicates that
  7609. only the correctly processes TXT should be returned - this is used by
  7610. `org-agenda-change-all-lines'. TAGS can be the tags of the headline."
  7611. (save-match-data
  7612. ;; Diary entries sometimes have extra whitespace at the beginning
  7613. (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
  7614. (let* ((category (or category
  7615. org-category
  7616. (if buffer-file-name
  7617. (file-name-sans-extension
  7618. (file-name-nondirectory buffer-file-name))
  7619. "")))
  7620. (tag (if tags (nth (1- (length tags)) tags) ""))
  7621. time ;; needed for the eval of the prefix format
  7622. (ts (if dotime (concat (if (stringp dotime) dotime "") txt)))
  7623. (time-of-day (and dotime (org-get-time-of-day ts)))
  7624. stamp plain s0 s1 s2 rtn)
  7625. (when (and dotime time-of-day org-prefix-has-time)
  7626. ;; Extract starting and ending time and move them to prefix
  7627. (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
  7628. (setq plain (string-match org-plain-time-of-day-regexp ts)))
  7629. (setq s0 (match-string 0 ts)
  7630. s1 (match-string (if plain 1 2) ts)
  7631. s2 (match-string (if plain 8 4) ts))
  7632. ;; If the times are in TXT (not in DOTIMES), and the prefix will list
  7633. ;; them, we might want to remove them there to avoid duplication.
  7634. ;; The user can turn this off with a variable.
  7635. (if (and org-agenda-remove-times-when-in-prefix (or stamp plain)
  7636. (string-match (concat (regexp-quote s0) " *") txt)
  7637. (if (eq org-agenda-remove-times-when-in-prefix 'beg)
  7638. (= (match-beginning 0) 0)
  7639. t))
  7640. (setq txt (replace-match "" nil nil txt))))
  7641. ;; Normalize the time(s) to 24 hour
  7642. (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
  7643. (if s2 (setq s2 (org-get-time-of-day s2 'string t))))
  7644. (when (string-match "\\([ \t]+\\)\\(:[a-zA-Z_@0-9:]+:\\)[ \t]*$" txt)
  7645. ;; Tags are in the string
  7646. (if (or (eq org-agenda-remove-tags-when-in-prefix t)
  7647. (and org-agenda-remove-tags-when-in-prefix
  7648. org-prefix-has-tag))
  7649. (setq txt (replace-match "" t t txt))
  7650. (setq txt (replace-match
  7651. (concat (make-string (max (- 50 (length txt)) 1) ?\ )
  7652. (match-string 2 txt))
  7653. t t txt))))
  7654. ;; Create the final string
  7655. (if noprefix
  7656. (setq rtn txt)
  7657. ;; Prepare the variables needed in the eval of the compiled format
  7658. (setq time (cond (s2 (concat s1 "-" s2))
  7659. (s1 (concat s1 "......"))
  7660. (t ""))
  7661. extra (or extra "")
  7662. category (if (symbolp category) (symbol-name category) category))
  7663. ;; Evaluate the compiled format
  7664. (setq rtn (concat (eval org-prefix-format-compiled) txt)))
  7665. ;; And finally add the text properties
  7666. (org-add-props rtn nil
  7667. 'category (downcase category) 'tags tags
  7668. 'prefix-length (- (length rtn) (length txt))
  7669. 'time-of-day time-of-day
  7670. 'dotime dotime))))
  7671. (defvar org-agenda-sorting-strategy)
  7672. (defvar org-agenda-sorting-strategy-selected nil)
  7673. (defun org-agenda-add-time-grid-maybe (list ndays todayp)
  7674. (catch 'exit
  7675. (cond ((not org-agenda-use-time-grid) (throw 'exit list))
  7676. ((and todayp (member 'today (car org-agenda-time-grid))))
  7677. ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
  7678. ((member 'weekly (car org-agenda-time-grid)))
  7679. (t (throw 'exit list)))
  7680. (let* ((have (delq nil (mapcar
  7681. (lambda (x) (get-text-property 1 'time-of-day x))
  7682. list)))
  7683. (string (nth 1 org-agenda-time-grid))
  7684. (gridtimes (nth 2 org-agenda-time-grid))
  7685. (req (car org-agenda-time-grid))
  7686. (remove (member 'remove-match req))
  7687. new time)
  7688. (if (and (member 'require-timed req) (not have))
  7689. ;; don't show empty grid
  7690. (throw 'exit list))
  7691. (while (setq time (pop gridtimes))
  7692. (unless (and remove (member time have))
  7693. (setq time (int-to-string time))
  7694. (push (org-format-agenda-item
  7695. nil string "" nil
  7696. (concat (substring time 0 -2) ":" (substring time -2)))
  7697. new)
  7698. (put-text-property
  7699. 1 (length (car new)) 'face 'org-time-grid (car new))))
  7700. (if (member 'time-up org-agenda-sorting-strategy-selected)
  7701. (append new list)
  7702. (append list new)))))
  7703. (defun org-compile-prefix-format (key)
  7704. "Compile the prefix format into a Lisp form that can be evaluated.
  7705. The resulting form is returned and stored in the variable
  7706. `org-prefix-format-compiled'."
  7707. (setq org-prefix-has-time nil org-prefix-has-tag nil)
  7708. (let ((s (cond
  7709. ((stringp org-agenda-prefix-format)
  7710. org-agenda-prefix-format)
  7711. ((assq key org-agenda-prefix-format)
  7712. (cdr (assq key org-agenda-prefix-format)))
  7713. (t " %-12:c%?-12t% s")))
  7714. (start 0)
  7715. varform vars var e c f opt)
  7716. (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cts]\\)"
  7717. s start)
  7718. (setq var (cdr (assoc (match-string 4 s)
  7719. '(("c" . category) ("t" . time) ("s" . extra)
  7720. ("T" . tag))))
  7721. c (or (match-string 3 s) "")
  7722. opt (match-beginning 1)
  7723. start (1+ (match-beginning 0)))
  7724. (if (equal var 'time) (setq org-prefix-has-time t))
  7725. (if (equal var 'tag) (setq org-prefix-has-tag t))
  7726. (setq f (concat "%" (match-string 2 s) "s"))
  7727. (if opt
  7728. (setq varform
  7729. `(if (equal "" ,var)
  7730. ""
  7731. (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
  7732. (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
  7733. (setq s (replace-match "%s" t nil s))
  7734. (push varform vars))
  7735. (setq vars (nreverse vars))
  7736. (setq org-prefix-format-compiled `(format ,s ,@vars))))
  7737. (defun org-set-sorting-strategy (key)
  7738. (if (symbolp (car org-agenda-sorting-strategy))
  7739. ;; the old format
  7740. (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
  7741. (setq org-agenda-sorting-strategy-selected
  7742. (or (cdr (assq key org-agenda-sorting-strategy))
  7743. (cdr (assq 'agenda org-agenda-sorting-strategy))
  7744. '(time-up category-keep priority-down)))))
  7745. (defun org-get-time-of-day (s &optional string mod24)
  7746. "Check string S for a time of day.
  7747. If found, return it as a military time number between 0 and 2400.
  7748. If not found, return nil.
  7749. The optional STRING argument forces conversion into a 5 character wide string
  7750. HH:MM."
  7751. (save-match-data
  7752. (when
  7753. (or
  7754. (string-match
  7755. "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
  7756. (string-match
  7757. "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
  7758. (let* ((h (string-to-number (match-string 1 s)))
  7759. (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
  7760. (ampm (if (match-end 4) (downcase (match-string 4 s))))
  7761. (am-p (equal ampm "am"))
  7762. (h1 (cond ((not ampm) h)
  7763. ((= h 12) (if am-p 0 12))
  7764. (t (+ h (if am-p 0 12)))))
  7765. (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
  7766. (mod h1 24) h1))
  7767. (t0 (+ (* 100 h2) m))
  7768. (t1 (concat (if (>= h1 24) "+" " ")
  7769. (if (< t0 100) "0" "")
  7770. (if (< t0 10) "0" "")
  7771. (int-to-string t0))))
  7772. (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
  7773. (defun org-finalize-agenda-entries (list &optional nosort)
  7774. "Sort and concatenate the agenda items."
  7775. (setq list (mapcar 'org-agenda-highlight-todo list))
  7776. (if nosort
  7777. list
  7778. (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
  7779. (defun org-agenda-highlight-todo (x)
  7780. (let (re pl)
  7781. (if (eq x 'line)
  7782. (save-excursion
  7783. (beginning-of-line 1)
  7784. (setq re (get-text-property (point) 'org-not-done-regexp))
  7785. (goto-char (+ (point) (get-text-property (point) 'prefix-length)))
  7786. (and (looking-at (concat "[ \t]*\\.*" re))
  7787. (add-text-properties (match-beginning 0) (match-end 0)
  7788. '(face org-todo))))
  7789. (setq re (concat (get-text-property 0 'org-not-done-regexp x))
  7790. pl (get-text-property 0 'prefix-length x))
  7791. (and re (equal (string-match (concat "\\(\\.*\\)" re) x pl) pl)
  7792. (add-text-properties (match-end 1) (match-end 0)
  7793. '(face org-todo) x))
  7794. x)))
  7795. (defsubst org-cmp-priority (a b)
  7796. "Compare the priorities of string A and B."
  7797. (let ((pa (or (get-text-property 1 'priority a) 0))
  7798. (pb (or (get-text-property 1 'priority b) 0)))
  7799. (cond ((> pa pb) +1)
  7800. ((< pa pb) -1)
  7801. (t nil))))
  7802. (defsubst org-cmp-category (a b)
  7803. "Compare the string values of categories of strings A and B."
  7804. (let ((ca (or (get-text-property 1 'category a) ""))
  7805. (cb (or (get-text-property 1 'category b) "")))
  7806. (cond ((string-lessp ca cb) -1)
  7807. ((string-lessp cb ca) +1)
  7808. (t nil))))
  7809. (defsubst org-cmp-tag (a b)
  7810. "Compare the string values of categories of strings A and B."
  7811. (let ((ta (car (last (get-text-property 1 'tags a))))
  7812. (tb (car (last (get-text-property 1 'tags b)))))
  7813. (cond ((not ta) +1)
  7814. ((not tb) -1)
  7815. ((string-lessp ta tb) -1)
  7816. ((string-lessp tb ta) +1)
  7817. (t nil))))
  7818. (defsubst org-cmp-time (a b)
  7819. "Compare the time-of-day values of strings A and B."
  7820. (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
  7821. (ta (or (get-text-property 1 'time-of-day a) def))
  7822. (tb (or (get-text-property 1 'time-of-day b) def)))
  7823. (cond ((< ta tb) -1)
  7824. ((< tb ta) +1)
  7825. (t nil))))
  7826. (defun org-entries-lessp (a b)
  7827. "Predicate for sorting agenda entries."
  7828. ;; The following variables will be used when the form is evaluated.
  7829. (let* ((time-up (org-cmp-time a b))
  7830. (time-down (if time-up (- time-up) nil))
  7831. (priority-up (org-cmp-priority a b))
  7832. (priority-down (if priority-up (- priority-up) nil))
  7833. (category-up (org-cmp-category a b))
  7834. (category-down (if category-up (- category-up) nil))
  7835. (category-keep (if category-up +1 nil))
  7836. (tag-up (org-cmp-tag a b))
  7837. (tag-down (if tag-up (- tag-up) nil)))
  7838. (cdr (assoc
  7839. (eval (cons 'or org-agenda-sorting-strategy-selected))
  7840. '((-1 . t) (1 . nil) (nil . nil))))))
  7841. (defun org-agenda-show-priority ()
  7842. "Show the priority of the current item.
  7843. This priority is composed of the main priority given with the [#A] cookies,
  7844. and by additional input from the age of a schedules or deadline entry."
  7845. (interactive)
  7846. (let* ((pri (get-text-property (point-at-bol) 'priority)))
  7847. (message "Priority is %d" (if pri pri -1000))))
  7848. (defun org-agenda-show-tags ()
  7849. "Show the tags applicable to the current item."
  7850. (interactive)
  7851. (let* ((tags (get-text-property (point-at-bol) 'tags)))
  7852. (if tags
  7853. (message "Tags are :%s:"
  7854. (org-no-properties (mapconcat 'identity tags ":")))
  7855. (message "No tags associated with this line"))))
  7856. (defun org-agenda-goto (&optional highlight)
  7857. "Go to the Org-mode file which contains the item at point."
  7858. (interactive)
  7859. (let* ((marker (or (get-text-property (point) 'org-marker)
  7860. (org-agenda-error)))
  7861. (buffer (marker-buffer marker))
  7862. (pos (marker-position marker)))
  7863. (switch-to-buffer-other-window buffer)
  7864. (widen)
  7865. (goto-char pos)
  7866. (when (org-mode-p)
  7867. (org-show-hierarchy-above 'agenda)
  7868. (save-excursion
  7869. (and (outline-next-heading)
  7870. (org-flag-heading nil)))) ; show the next heading
  7871. (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
  7872. (defun org-agenda-switch-to ()
  7873. "Go to the Org-mode file which contains the item at point."
  7874. (interactive)
  7875. (let* ((marker (or (get-text-property (point) 'org-marker)
  7876. (org-agenda-error)))
  7877. (buffer (marker-buffer marker))
  7878. (pos (marker-position marker)))
  7879. (switch-to-buffer buffer)
  7880. (delete-other-windows)
  7881. (widen)
  7882. (goto-char pos)
  7883. (when (org-mode-p)
  7884. (org-show-hierarchy-above 'agenda)
  7885. (save-excursion
  7886. (and (outline-next-heading)
  7887. (org-flag-heading nil)))))) ; show the next heading
  7888. (defun org-agenda-goto-mouse (ev)
  7889. "Go to the Org-mode file which contains the item at the mouse click."
  7890. (interactive "e")
  7891. (mouse-set-point ev)
  7892. (org-agenda-goto))
  7893. (defun org-agenda-show ()
  7894. "Display the Org-mode file which contains the item at point."
  7895. (interactive)
  7896. (let ((win (selected-window)))
  7897. (org-agenda-goto t)
  7898. (select-window win)))
  7899. (defun org-agenda-recenter (arg)
  7900. "Display the Org-mode file which contains the item at point and recenter."
  7901. (interactive "P")
  7902. (let ((win (selected-window)))
  7903. (org-agenda-goto t)
  7904. (recenter arg)
  7905. (select-window win)))
  7906. (defun org-agenda-show-mouse (ev)
  7907. "Display the Org-mode file which contains the item at the mouse click."
  7908. (interactive "e")
  7909. (mouse-set-point ev)
  7910. (org-agenda-show))
  7911. (defun org-agenda-check-no-diary ()
  7912. "Check if the entry is a diary link and abort if yes."
  7913. (if (get-text-property (point) 'org-agenda-diary-link)
  7914. (org-agenda-error)))
  7915. (defun org-agenda-error ()
  7916. (error "Command not allowed in this line"))
  7917. (defvar org-last-heading-marker (make-marker)
  7918. "Marker pointing to the headline that last changed its TODO state
  7919. by a remote command from the agenda.")
  7920. (defun org-agenda-todo (&optional arg)
  7921. "Cycle TODO state of line at point, also in Org-mode file.
  7922. This changes the line at point, all other lines in the agenda referring to
  7923. the same tree node, and the headline of the tree node in the Org-mode file."
  7924. (interactive "P")
  7925. (org-agenda-check-no-diary)
  7926. (let* ((col (current-column))
  7927. (marker (or (get-text-property (point) 'org-marker)
  7928. (org-agenda-error)))
  7929. (buffer (marker-buffer marker))
  7930. (pos (marker-position marker))
  7931. (hdmarker (get-text-property (point) 'org-hd-marker))
  7932. (buffer-read-only nil)
  7933. newhead)
  7934. (with-current-buffer buffer
  7935. (widen)
  7936. (goto-char pos)
  7937. (org-show-hierarchy-above 'agenda)
  7938. (save-excursion
  7939. (and (outline-next-heading)
  7940. (org-flag-heading nil))) ; show the next heading
  7941. (org-todo arg)
  7942. (and (bolp) (forward-char 1))
  7943. (setq newhead (org-get-heading))
  7944. (save-excursion
  7945. (org-back-to-heading)
  7946. (move-marker org-last-heading-marker (point))))
  7947. (beginning-of-line 1)
  7948. (save-excursion
  7949. (org-agenda-change-all-lines newhead hdmarker 'fixface))
  7950. (move-to-column col)))
  7951. (defun org-agenda-change-all-lines (newhead hdmarker &optional fixface)
  7952. "Change all lines in the agenda buffer which match HDMARKER.
  7953. The new content of the line will be NEWHEAD (as modified by
  7954. `org-format-agenda-item'). HDMARKER is checked with
  7955. `equal' against all `org-hd-marker' text properties in the file.
  7956. If FIXFACE is non-nil, the face of each item is modified acording to
  7957. the new TODO state."
  7958. (let* (props m pl undone-face done-face finish new dotime cat tags)
  7959. ; (setq newhead (org-format-agenda-item "x" newhead "x" nil 'noprefix))
  7960. (save-excursion
  7961. (goto-char (point-max))
  7962. (beginning-of-line 1)
  7963. (while (not finish)
  7964. (setq finish (bobp))
  7965. (when (and (setq m (get-text-property (point) 'org-hd-marker))
  7966. (equal m hdmarker))
  7967. (setq props (text-properties-at (point))
  7968. dotime (get-text-property (point) 'dotime)
  7969. cat (get-text-property (point) 'category)
  7970. tags (get-text-property (point) 'tags)
  7971. new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
  7972. pl (get-text-property (point) 'prefix-length)
  7973. undone-face (get-text-property (point) 'undone-face)
  7974. done-face (get-text-property (point) 'done-face))
  7975. (move-to-column pl)
  7976. (if (looking-at ".*")
  7977. (progn
  7978. (replace-match new t t)
  7979. (beginning-of-line 1)
  7980. (add-text-properties (point-at-bol) (point-at-eol) props)
  7981. (when fixface
  7982. (add-text-properties
  7983. (point-at-bol) (point-at-eol)
  7984. (list 'face
  7985. (if org-last-todo-state-is-todo
  7986. undone-face done-face)))
  7987. (org-agenda-highlight-todo 'line))
  7988. ;; (org-agenda-align-tags 'line) ;; done below by finalize
  7989. (beginning-of-line 1))
  7990. (error "Line update did not work")))
  7991. (beginning-of-line 0)))
  7992. (org-finalize-agenda)))
  7993. (defun org-agenda-align-tags (&optional line)
  7994. "Align all tags in agenda items to `org-agenda-align-tags-to-column'."
  7995. (let ((buffer-read-only))
  7996. (save-excursion
  7997. (goto-char (if line (point-at-bol) (point-min)))
  7998. (while (re-search-forward "\\([ \t]+\\):[a-zA-Z0-9_@:]+:[ \t]*$"
  7999. (if line (point-at-eol) nil) t)
  8000. (delete-region (match-beginning 1) (match-end 1))
  8001. (goto-char (match-beginning 1))
  8002. (insert (org-add-props
  8003. (make-string (max 1 (- org-agenda-align-tags-to-column
  8004. (current-column))) ?\ )
  8005. (text-properties-at (point))))))))
  8006. (defun org-agenda-priority-up ()
  8007. "Increase the priority of line at point, also in Org-mode file."
  8008. (interactive)
  8009. (org-agenda-priority 'up))
  8010. (defun org-agenda-priority-down ()
  8011. "Decrease the priority of line at point, also in Org-mode file."
  8012. (interactive)
  8013. (org-agenda-priority 'down))
  8014. (defun org-agenda-priority (&optional force-direction)
  8015. "Set the priority of line at point, also in Org-mode file.
  8016. This changes the line at point, all other lines in the agenda referring to
  8017. the same tree node, and the headline of the tree node in the Org-mode file."
  8018. (interactive)
  8019. (org-agenda-check-no-diary)
  8020. (let* ((marker (or (get-text-property (point) 'org-marker)
  8021. (org-agenda-error)))
  8022. (buffer (marker-buffer marker))
  8023. (pos (marker-position marker))
  8024. (hdmarker (get-text-property (point) 'org-hd-marker))
  8025. (buffer-read-only nil)
  8026. newhead)
  8027. (with-current-buffer buffer
  8028. (widen)
  8029. (goto-char pos)
  8030. (org-show-hierarchy-above 'agenda)
  8031. (save-excursion
  8032. (and (outline-next-heading)
  8033. (org-flag-heading nil))) ; show the next heading
  8034. (funcall 'org-priority force-direction)
  8035. (end-of-line 1)
  8036. (setq newhead (org-get-heading)))
  8037. (org-agenda-change-all-lines newhead hdmarker)
  8038. (beginning-of-line 1)))
  8039. (defun org-get-tags-at (&optional pos)
  8040. "Get a list of all headline tags applicable at POS.
  8041. POS defaults to point. If tags are inherited, the list contains
  8042. the targets in the same sequence as the headlines appear, i.e.
  8043. the tags of the current headline come last."
  8044. (interactive)
  8045. (let (tags)
  8046. (save-excursion
  8047. (goto-char (or pos (point)))
  8048. (save-match-data
  8049. (org-back-to-heading t)
  8050. (condition-case nil
  8051. (while t
  8052. (if (looking-at "[^\r\n]+?:\\([a-zA-Z_@0-9:]+\\):[ \t]*\\([\n\r]\\|\\'\\)")
  8053. (setq tags (append (org-split-string
  8054. (org-match-string-no-properties 1) ":")
  8055. tags)))
  8056. (or org-use-tag-inheritance (error ""))
  8057. (org-up-heading-all 1))
  8058. (error nil))))
  8059. tags))
  8060. (defun org-agenda-set-tags ()
  8061. "Set tags for the current headline."
  8062. (interactive)
  8063. (org-agenda-check-no-diary)
  8064. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  8065. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  8066. (org-agenda-error)))
  8067. (buffer (marker-buffer hdmarker))
  8068. (pos (marker-position hdmarker))
  8069. (buffer-read-only nil)
  8070. newhead)
  8071. (with-current-buffer buffer
  8072. (widen)
  8073. (goto-char pos)
  8074. (org-show-hierarchy-above 'agenda)
  8075. (save-excursion
  8076. (and (outline-next-heading)
  8077. (org-flag-heading nil))) ; show the next heading
  8078. (call-interactively 'org-set-tags)
  8079. (end-of-line 1)
  8080. (setq newhead (org-get-heading)))
  8081. (org-agenda-change-all-lines newhead hdmarker)
  8082. (beginning-of-line 1)))
  8083. (defun org-agenda-date-later (arg &optional what)
  8084. "Change the date of this item to one day later."
  8085. (interactive "p")
  8086. (org-agenda-check-type t 'agenda 'timeline)
  8087. (org-agenda-check-no-diary)
  8088. (let* ((marker (or (get-text-property (point) 'org-marker)
  8089. (org-agenda-error)))
  8090. (buffer (marker-buffer marker))
  8091. (pos (marker-position marker)))
  8092. (with-current-buffer buffer
  8093. (widen)
  8094. (goto-char pos)
  8095. (if (not (org-at-timestamp-p))
  8096. (error "Cannot find time stamp"))
  8097. (org-timestamp-change arg (or what 'day))
  8098. (message "Time stamp changed to %s" org-last-changed-timestamp))))
  8099. (defun org-agenda-date-earlier (arg &optional what)
  8100. "Change the date of this item to one day earlier."
  8101. (interactive "p")
  8102. (org-agenda-date-later (- arg) what))
  8103. (defun org-agenda-date-prompt (arg)
  8104. "Change the date of this item. Date is prompted for, with default today.
  8105. The prefix ARG is passed to the `org-time-stamp' command and can therefore
  8106. be used to request time specification in the time stamp."
  8107. (interactive "P")
  8108. (org-agenda-check-type t 'agenda 'timeline)
  8109. (org-agenda-check-no-diary)
  8110. (let* ((marker (or (get-text-property (point) 'org-marker)
  8111. (org-agenda-error)))
  8112. (buffer (marker-buffer marker))
  8113. (pos (marker-position marker)))
  8114. (with-current-buffer buffer
  8115. (widen)
  8116. (goto-char pos)
  8117. (if (not (org-at-timestamp-p))
  8118. (error "Cannot find time stamp"))
  8119. (org-time-stamp arg)
  8120. (message "Time stamp changed to %s" org-last-changed-timestamp))))
  8121. (defun org-agenda-schedule (arg)
  8122. "Schedule the item at point."
  8123. (interactive "P")
  8124. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  8125. (org-agenda-check-no-diary)
  8126. (let* ((marker (or (get-text-property (point) 'org-marker)
  8127. (org-agenda-error)))
  8128. (buffer (marker-buffer marker))
  8129. (pos (marker-position marker))
  8130. (org-insert-labeled-timestamps-at-point nil)
  8131. ts)
  8132. (with-current-buffer buffer
  8133. (widen)
  8134. (goto-char pos)
  8135. (setq ts (org-schedule))
  8136. (message "Item scheduled for %s" ts))))
  8137. (defun org-agenda-deadline (arg)
  8138. "Schedule the item at point."
  8139. (interactive "P")
  8140. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  8141. (org-agenda-check-no-diary)
  8142. (let* ((marker (or (get-text-property (point) 'org-marker)
  8143. (org-agenda-error)))
  8144. (buffer (marker-buffer marker))
  8145. (pos (marker-position marker))
  8146. (org-insert-labeled-timestamps-at-point nil)
  8147. ts)
  8148. (with-current-buffer buffer
  8149. (widen)
  8150. (goto-char pos)
  8151. (setq ts (org-deadline))
  8152. (message "Deadline for this item set to %s" ts))))
  8153. (defun org-get-heading ()
  8154. "Return the heading of the current entry, without the stars."
  8155. (save-excursion
  8156. (and (memq (char-before) '(?\n ?\r)) (skip-chars-forward "^\n\r"))
  8157. (if (and (re-search-backward "[\r\n]\\*" nil t)
  8158. (looking-at "[\r\n]\\*+[ \t]+\\([^\r\n]*\\)"))
  8159. (match-string 1)
  8160. "")))
  8161. (defun org-agenda-clock-in (&optional arg)
  8162. "Start the clock on the currently selected item."
  8163. (interactive "P")
  8164. (org-agenda-check-no-diary)
  8165. (let* ((marker (or (get-text-property (point) 'org-marker)
  8166. (org-agenda-error)))
  8167. (pos (marker-position marker)))
  8168. (with-current-buffer (marker-buffer marker)
  8169. (widen)
  8170. (goto-char pos)
  8171. (org-clock-in))))
  8172. (defun org-agenda-diary-entry ()
  8173. "Make a diary entry, like the `i' command from the calendar.
  8174. All the standard commands work: block, weekly etc."
  8175. (interactive)
  8176. (org-agenda-check-type t 'agenda 'timeline)
  8177. (require 'diary-lib)
  8178. (let* ((char (progn
  8179. (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
  8180. (read-char-exclusive)))
  8181. (cmd (cdr (assoc char
  8182. '((?d . insert-diary-entry)
  8183. (?w . insert-weekly-diary-entry)
  8184. (?m . insert-monthly-diary-entry)
  8185. (?y . insert-yearly-diary-entry)
  8186. (?a . insert-anniversary-diary-entry)
  8187. (?b . insert-block-diary-entry)
  8188. (?c . insert-cyclic-diary-entry)))))
  8189. (oldf (symbol-function 'calendar-cursor-to-date))
  8190. (point (point))
  8191. (mark (or (mark t) (point))))
  8192. (unless cmd
  8193. (error "No command associated with <%c>" char))
  8194. (unless (and (get-text-property point 'day)
  8195. (or (not (equal ?b char))
  8196. (get-text-property mark 'day)))
  8197. (error "Don't know which date to use for diary entry"))
  8198. ;; We implement this by hacking the `calendar-cursor-to-date' function
  8199. ;; and the `calendar-mark-ring' variable. Saves a lot of code.
  8200. (let ((calendar-mark-ring
  8201. (list (calendar-gregorian-from-absolute
  8202. (or (get-text-property mark 'day)
  8203. (get-text-property point 'day))))))
  8204. (unwind-protect
  8205. (progn
  8206. (fset 'calendar-cursor-to-date
  8207. (lambda (&optional error)
  8208. (calendar-gregorian-from-absolute
  8209. (get-text-property point 'day))))
  8210. (call-interactively cmd))
  8211. (fset 'calendar-cursor-to-date oldf)))))
  8212. (defun org-agenda-execute-calendar-command (cmd)
  8213. "Execute a calendar command from the agenda, with the date associated to
  8214. the cursor position."
  8215. (org-agenda-check-type t 'agenda 'timeline)
  8216. (require 'diary-lib)
  8217. (unless (get-text-property (point) 'day)
  8218. (error "Don't know which date to use for calendar command"))
  8219. (let* ((oldf (symbol-function 'calendar-cursor-to-date))
  8220. (point (point))
  8221. (date (calendar-gregorian-from-absolute
  8222. (get-text-property point 'day)))
  8223. (displayed-day (extract-calendar-day date))
  8224. (displayed-month (extract-calendar-month date))
  8225. (displayed-year (extract-calendar-year date)))
  8226. (unwind-protect
  8227. (progn
  8228. (fset 'calendar-cursor-to-date
  8229. (lambda (&optional error)
  8230. (calendar-gregorian-from-absolute
  8231. (get-text-property point 'day))))
  8232. (call-interactively cmd))
  8233. (fset 'calendar-cursor-to-date oldf))))
  8234. (defun org-agenda-phases-of-moon ()
  8235. "Display the phases of the moon for the 3 months around the cursor date."
  8236. (interactive)
  8237. (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
  8238. (defun org-agenda-holidays ()
  8239. "Display the holidays for the 3 months around the cursor date."
  8240. (interactive)
  8241. (org-agenda-execute-calendar-command 'list-calendar-holidays))
  8242. (defun org-agenda-sunrise-sunset (arg)
  8243. "Display sunrise and sunset for the cursor date.
  8244. Latitude and longitude can be specified with the variables
  8245. `calendar-latitude' and `calendar-longitude'. When called with prefix
  8246. argument, latitude and longitude will be prompted for."
  8247. (interactive "P")
  8248. (let ((calendar-longitude (if arg nil calendar-longitude))
  8249. (calendar-latitude (if arg nil calendar-latitude))
  8250. (calendar-location-name
  8251. (if arg "the given coordinates" calendar-location-name)))
  8252. (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
  8253. (defun org-agenda-goto-calendar ()
  8254. "Open the Emacs calendar with the date at the cursor."
  8255. (interactive)
  8256. (org-agenda-check-type t 'agenda 'timeline)
  8257. (let* ((day (or (get-text-property (point) 'day)
  8258. (error "Don't know which date to open in calendar")))
  8259. (date (calendar-gregorian-from-absolute day))
  8260. (calendar-move-hook nil)
  8261. (view-calendar-holidays-initially nil)
  8262. (view-diary-entries-initially nil))
  8263. (calendar)
  8264. (calendar-goto-date date)))
  8265. (defun org-calendar-goto-agenda ()
  8266. "Compute the Org-mode agenda for the calendar date displayed at the cursor.
  8267. This is a command that has to be installed in `calendar-mode-map'."
  8268. (interactive)
  8269. (org-agenda-list nil (calendar-absolute-from-gregorian
  8270. (calendar-cursor-to-date))
  8271. nil t))
  8272. (defun org-agenda-convert-date ()
  8273. (interactive)
  8274. (org-agenda-check-type t 'agenda 'timeline)
  8275. (let ((day (get-text-property (point) 'day))
  8276. date s)
  8277. (unless day
  8278. (error "Don't know which date to convert"))
  8279. (setq date (calendar-gregorian-from-absolute day))
  8280. (setq s (concat
  8281. "Gregorian: " (calendar-date-string date) "\n"
  8282. "ISO: " (calendar-iso-date-string date) "\n"
  8283. "Day of Yr: " (calendar-day-of-year-string date) "\n"
  8284. "Julian: " (calendar-julian-date-string date) "\n"
  8285. "Astron. JD: " (calendar-astro-date-string date)
  8286. " (Julian date number at noon UTC)\n"
  8287. "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
  8288. "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
  8289. "French: " (calendar-french-date-string date) "\n"
  8290. "Mayan: " (calendar-mayan-date-string date) "\n"
  8291. "Coptic: " (calendar-coptic-date-string date) "\n"
  8292. "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
  8293. "Persian: " (calendar-persian-date-string date) "\n"
  8294. "Chinese: " (calendar-chinese-date-string date) "\n"))
  8295. (with-output-to-temp-buffer "*Dates*"
  8296. (princ s))
  8297. (if (fboundp 'fit-window-to-buffer)
  8298. (fit-window-to-buffer (get-buffer-window "*Dates*")))))
  8299. ;;; Tags
  8300. (defun org-scan-tags (action matcher &optional todo-only)
  8301. "Scan headline tags with inheritance and produce output ACTION.
  8302. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  8303. evaluated, testing if a given set of tags qualifies a headline for
  8304. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  8305. are included in the output."
  8306. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8307. (mapconcat 'regexp-quote
  8308. (nreverse (cdr (reverse org-todo-keywords)))
  8309. "\\|")
  8310. "\\>\\)\\)? *\\(.*?\\)\\(:[A-Za-z_@0-9:]+:\\)?[ \t]*[\n\r]"))
  8311. (props (list 'face nil
  8312. 'done-face 'org-done
  8313. 'undone-face nil
  8314. 'mouse-face 'highlight
  8315. 'org-not-done-regexp org-not-done-regexp
  8316. 'keymap org-agenda-keymap
  8317. 'help-echo
  8318. (format "mouse-2 or RET jump to org file %s"
  8319. (abbreviate-file-name buffer-file-name))))
  8320. lspos
  8321. tags tags-list tags-alist (llast 0) rtn level category i txt
  8322. todo marker)
  8323. (save-excursion
  8324. (goto-char (point-min))
  8325. (when (eq action 'sparse-tree) (org-overview))
  8326. (while (re-search-forward re nil t)
  8327. (catch :skip
  8328. (and (eq action 'agenda) (org-agenda-skip))
  8329. (setq todo (if (match-end 1) (match-string 2))
  8330. tags (if (match-end 4) (match-string 4)))
  8331. (goto-char (setq lspos (1+ (match-beginning 0))))
  8332. (setq level (funcall outline-level)
  8333. category (org-get-category))
  8334. (setq i llast llast level)
  8335. ;; remove tag lists from same and sublevels
  8336. (while (>= i level)
  8337. (when (setq entry (assoc i tags-alist))
  8338. (setq tags-alist (delete entry tags-alist)))
  8339. (setq i (1- i)))
  8340. ;; add the nex tags
  8341. (when tags
  8342. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8343. tags-alist
  8344. (cons (cons level tags) tags-alist)))
  8345. ;; compile tags for current headline
  8346. (setq tags-list
  8347. (if org-use-tag-inheritance
  8348. (apply 'append (mapcar 'cdr tags-alist))
  8349. tags))
  8350. (when (and (or (not todo-only) todo)
  8351. (eval matcher)
  8352. (or (not org-agenda-skip-archived-trees)
  8353. (not (member org-archive-tag tags-list))))
  8354. ;; list this headline
  8355. (if (eq action 'sparse-tree)
  8356. (progn
  8357. (org-show-hierarchy-above 'tags-tree))
  8358. (setq txt (org-format-agenda-item
  8359. ""
  8360. (concat
  8361. (if org-tags-match-list-sublevels
  8362. (make-string (1- level) ?.) "")
  8363. (org-get-heading))
  8364. category tags-list))
  8365. (goto-char lspos)
  8366. (setq marker (org-agenda-new-marker))
  8367. (org-add-props txt props
  8368. 'org-marker marker 'org-hd-marker marker 'category category)
  8369. (push txt rtn))
  8370. ;; if we are to skip sublevels, jump to end of subtree
  8371. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8372. (when (and (eq action 'sparse-tree)
  8373. (not org-sparse-tree-open-archived-trees))
  8374. (org-hide-archived-subtrees (point-min) (point-max)))
  8375. (nreverse rtn)))
  8376. (defun org-tags-sparse-tree (&optional arg match)
  8377. "Create a sparse tree according to tags search string MATCH.
  8378. MATCH can contain positive and negative selection of tags, like
  8379. \"+WORK+URGENT-WITHBOSS\"."
  8380. (interactive "P")
  8381. (let ((org-show-following-heading nil)
  8382. (org-show-hierarchy-above nil))
  8383. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)))))
  8384. (defun org-make-tags-matcher (match)
  8385. "Create the TAGS matcher form for the tags-selecting string MATCH."
  8386. (unless match
  8387. ;; Get a new match request, with completion
  8388. (setq org-last-tags-completion-table
  8389. (or org-tag-alist
  8390. org-last-tags-completion-table))
  8391. (setq match (completing-read
  8392. "Tags: " 'org-tags-completion-function nil nil nil
  8393. 'org-tags-history)))
  8394. ;; parse the string and create a lisp form
  8395. (let ((match0 match) minus tag mm matcher orterms term orlist)
  8396. (setq orterms (org-split-string match "|"))
  8397. (while (setq term (pop orterms))
  8398. (while (string-match "^&?\\([-+:]\\)?\\([A-Za-z_@0-9]+\\)" term)
  8399. (setq minus (and (match-end 1)
  8400. (equal (match-string 1 term) "-"))
  8401. tag (match-string 2 term)
  8402. term (substring term (match-end 0))
  8403. mm (list 'member (downcase tag) 'tags-list)
  8404. mm (if minus (list 'not mm) mm))
  8405. (push mm matcher))
  8406. (push (if (> (length matcher) 1) (cons 'and matcher) (car matcher))
  8407. orlist)
  8408. (setq matcher nil))
  8409. (setq matcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8410. ;; Return the string and lisp forms of the matcher
  8411. (cons match0 matcher)))
  8412. ;;;###autoload
  8413. (defun org-tags-view (&optional todo-only match keep-modes)
  8414. "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
  8415. The prefix arg TODO-ONLY limits the search to TODO entries."
  8416. (interactive "P")
  8417. (org-compile-prefix-format 'tags)
  8418. (org-set-sorting-strategy 'tags)
  8419. (let* ((org-agenda-keep-modes keep-modes)
  8420. (org-tags-match-list-sublevels
  8421. (if todo-only t org-tags-match-list-sublevels))
  8422. (win (selected-window))
  8423. (completion-ignore-case t)
  8424. rtn rtnall files file pos matcher
  8425. buffer)
  8426. (setq matcher (org-make-tags-matcher match)
  8427. match (car matcher) matcher (cdr matcher))
  8428. (org-prepare-agenda)
  8429. (setq org-agenda-redo-command
  8430. (list 'org-tags-view (list 'quote todo-only)
  8431. (list 'if 'current-prefix-arg nil match) t))
  8432. (setq files (org-agenda-files)
  8433. rtnall nil)
  8434. (while (setq file (pop files))
  8435. (catch 'nextfile
  8436. (org-check-agenda-file file)
  8437. (setq buffer (if (file-exists-p file)
  8438. (org-get-agenda-file-buffer file)
  8439. (error "No such file %s" file)))
  8440. (if (not buffer)
  8441. ;; If file does not exist, merror message to agenda
  8442. (setq rtn (list
  8443. (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  8444. rtnall (append rtnall rtn))
  8445. (with-current-buffer buffer
  8446. (unless (org-mode-p)
  8447. (error "Agenda file %s is not in `org-mode'" file))
  8448. (setq org-category-table (org-get-category-table))
  8449. (save-excursion
  8450. (save-restriction
  8451. (if org-agenda-restrict
  8452. (narrow-to-region org-agenda-restrict-begin
  8453. org-agenda-restrict-end)
  8454. (widen))
  8455. (setq rtn (org-scan-tags 'agenda matcher todo-only))
  8456. (setq rtnall (append rtnall rtn))))))))
  8457. (insert "Headlines with TAGS match: ")
  8458. (add-text-properties (point-min) (1- (point))
  8459. (list 'face 'org-level-3))
  8460. (setq pos (point))
  8461. (insert match "\n")
  8462. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  8463. (setq pos (point))
  8464. (unless org-agenda-multi
  8465. (insert "Press `C-u r' to search again with new search string\n"))
  8466. (add-text-properties pos (1- (point)) (list 'face 'org-level-3))
  8467. (when rtnall
  8468. (insert (org-finalize-agenda-entries rtnall) "\n"))
  8469. (goto-char (point-min))
  8470. (org-fit-agenda-window)
  8471. (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
  8472. (org-finalize-agenda)
  8473. (setq buffer-read-only t)
  8474. (if (not org-select-agenda-window) (select-window win))))
  8475. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  8476. (defun org-set-tags (&optional arg just-align)
  8477. "Set the tags for the current headline.
  8478. With prefix ARG, realign all tags in headings in the current buffer."
  8479. (interactive "P")
  8480. (let* ((re (concat "^" outline-regexp))
  8481. (col (current-column))
  8482. (current (org-get-tags))
  8483. table current-tags inherited-tags ; computed below when needed
  8484. tags hd empty invis)
  8485. (if arg
  8486. (save-excursion
  8487. (goto-char (point-min))
  8488. (while (re-search-forward re nil t)
  8489. (org-set-tags nil t))
  8490. (message "All tags realigned to column %d" org-tags-column))
  8491. (if just-align
  8492. (setq tags current)
  8493. (setq table (or org-tag-alist (org-get-buffer-tags))
  8494. org-last-tags-completion-table table
  8495. current-tags (org-split-string current ":")
  8496. inherited-tags (nreverse
  8497. (nthcdr (length current-tags)
  8498. (nreverse (org-get-tags-at))))
  8499. tags
  8500. (if (or (eq t org-use-fast-tag-selection)
  8501. (and org-use-fast-tag-selection
  8502. (delq nil (mapcar 'cdr table))))
  8503. (org-fast-tag-selection current-tags inherited-tags table)
  8504. (let ((org-add-colon-after-tag-completion t))
  8505. (completing-read "Tags: " 'org-tags-completion-function
  8506. nil nil current 'org-tags-history))))
  8507. (while (string-match "[-+&]+" tags)
  8508. (setq tags (replace-match ":" t t tags))))
  8509. (unless (setq empty (string-match "\\`[\t ]*\\'" tags))
  8510. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  8511. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  8512. (if (equal current "")
  8513. (progn
  8514. (end-of-line 1)
  8515. (or empty (insert " ")))
  8516. (beginning-of-line 1)
  8517. (setq invis (org-invisible-p))
  8518. (looking-at (concat ".*?\\([ \t]*" (regexp-quote current) "\\)[ \t]*"))
  8519. (delete-region (match-beginning 1) (match-end 1))
  8520. (goto-char (match-beginning 1))
  8521. (insert (if empty "" " ")))
  8522. (if (equal tags "")
  8523. (save-excursion
  8524. (beginning-of-line 1)
  8525. (skip-chars-forward "*")
  8526. (if (= (char-after) ?\ ) (forward-char 1))
  8527. (and (re-search-forward "[ \t]+$" (point-at-eol) t)
  8528. (replace-match "")))
  8529. (move-to-column (max (current-column)
  8530. (if (> org-tags-column 0)
  8531. org-tags-column
  8532. (- (- org-tags-column) (length tags))))
  8533. t)
  8534. (insert tags)
  8535. (if (and (not invis) (org-invisible-p))
  8536. (outline-flag-region (point-at-bol) (point) nil)))
  8537. (move-to-column col))))
  8538. (defun org-tags-completion-function (string predicate &optional flag)
  8539. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  8540. (confirm (lambda (x) (stringp (car x)))))
  8541. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  8542. (setq s1 (match-string 1 string)
  8543. s2 (match-string 2 string))
  8544. (setq s1 "" s2 string))
  8545. (cond
  8546. ((eq flag nil)
  8547. ;; try completion
  8548. (setq rtn (try-completion s2 ctable confirm))
  8549. (if (stringp rtn)
  8550. (concat s1 s2 (substring rtn (length s2))
  8551. (if (and org-add-colon-after-tag-completion
  8552. (assoc rtn ctable))
  8553. ":" "")))
  8554. )
  8555. ((eq flag t)
  8556. ;; all-completions
  8557. (all-completions s2 ctable confirm)
  8558. )
  8559. ((eq flag 'lambda)
  8560. ;; exact match?
  8561. (assoc s2 ctable)))
  8562. ))
  8563. (defun org-fast-tag-insert (kwd tags face &optional end)
  8564. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  8565. (insert (format "%-12s" (concat kwd ":"))
  8566. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  8567. (or end "")))
  8568. (defun org-fast-tag-selection (current inherited table)
  8569. "Fast tag selection with single keys.
  8570. CURRENT is the current list of tags in the headline, INHERITED is the
  8571. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  8572. possibly with grouping information.
  8573. If the keys are nil, a-z are automatically assigned.
  8574. Returns the new tags string, or nil to not change the current settings."
  8575. (let* ((maxlen (apply 'max (mapcar
  8576. (lambda (x)
  8577. (if (stringp (car x)) (string-width (car x)) 0))
  8578. table)))
  8579. (fwidth (+ maxlen 3 1 3))
  8580. (ncol (/ (- (window-width) 4) fwidth))
  8581. (i-face 'org-done)
  8582. (c-face 'org-tag)
  8583. tg cnt e c char c1 c2 ntable tbl rtn
  8584. groups ingroup)
  8585. (save-window-excursion
  8586. (delete-other-windows)
  8587. (split-window-vertically)
  8588. (switch-to-buffer-other-window (get-buffer-create " *Org tags*"))
  8589. (erase-buffer)
  8590. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  8591. (org-fast-tag-insert "Current" current c-face "\n\n")
  8592. (setq tbl table char ?a cnt 0)
  8593. (while (setq e (pop tbl))
  8594. (cond
  8595. ((equal e '(:startgroup))
  8596. (push '() groups) (setq ingroup t)
  8597. (when (not (= cnt 0))
  8598. (setq cnt 0)
  8599. (insert "\n"))
  8600. (insert "{ "))
  8601. ((equal e '(:endgroup))
  8602. (setq ingroup nil cnt 0)
  8603. (insert "}\n"))
  8604. (t
  8605. (setq tg (car e) c2 nil)
  8606. (if (cdr e)
  8607. (setq c (cdr e))
  8608. ;; automatically assign a character.
  8609. (setq c1 (string-to-char
  8610. (downcase (substring
  8611. tg (if (= (string-to-char tg) ?@) 1 0)))))
  8612. (if (or (rassoc c1 ntable) (rassoc c1 table))
  8613. (while (or (rassoc char ntable) (rassoc char table))
  8614. (setq char (1+ char)))
  8615. (setq c2 c1))
  8616. (setq c (or c2 char)))
  8617. (if ingroup (push tg (car groups)))
  8618. (setq tg (org-add-props tg nil 'face
  8619. (cond
  8620. ((member tg current) c-face)
  8621. ((member tg inherited) i-face)
  8622. (t nil))))
  8623. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8624. (insert "[" c "] " tg (make-string
  8625. (- fwidth 4 (length tg)) ?\ ))
  8626. (push (cons tg c) ntable)
  8627. (when (= (setq cnt (1+ cnt)) ncol)
  8628. (insert "\n")
  8629. (if ingroup (insert " "))
  8630. (setq cnt 0)))))
  8631. (setq ntable (nreverse ntable))
  8632. (insert "\n")
  8633. (goto-char (point-min))
  8634. (if (fboundp 'fit-window-to-buffer) (fit-window-to-buffer))
  8635. (setq rtn
  8636. (catch 'exit
  8637. (while t
  8638. (message "[key]:Toggle SPC: clear current RET accept%s"
  8639. (if groups " [!] ignore goups" ""))
  8640. (setq c (read-char-exclusive))
  8641. (cond
  8642. ((= c ?\r) (throw 'exit t))
  8643. ((= c ?!)
  8644. (setq groups nil)
  8645. (goto-char (point-min))
  8646. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  8647. ((or (= c ?\C-g)
  8648. (and (= c ?q) (not (rassoc c ntable))))
  8649. (setq quit-flag t))
  8650. ((= c ?\ ) (setq current nil))
  8651. ((setq e (rassoc c ntable) tg (car e))
  8652. (if (member tg current)
  8653. (setq current (delete tg current))
  8654. (loop for g in groups do
  8655. (if (member tg g)
  8656. (mapcar (lambda (x)
  8657. (setq current (delete x current)))
  8658. g)))
  8659. (setq current (cons tg current)))))
  8660. ;; Create a sorted list
  8661. (setq current
  8662. (sort current
  8663. (lambda (a b)
  8664. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  8665. (goto-char (point-min))
  8666. (beginning-of-line 2)
  8667. (delete-region (point) (point-at-eol))
  8668. (org-fast-tag-insert "Current" current c-face)
  8669. (while (re-search-forward "\\[.\\] \\([a-zA-Z0-9_@]+\\)" nil t)
  8670. (setq tg (match-string 1))
  8671. (add-text-properties (match-beginning 1) (match-end 1)
  8672. (list 'face
  8673. (cond
  8674. ((member tg current) c-face)
  8675. ((member tg inherited) i-face)
  8676. (t nil)))))
  8677. (goto-char (point-min)))))
  8678. (if rtn
  8679. (mapconcat 'identity current ":")
  8680. nil))))
  8681. (defun org-get-tags ()
  8682. "Get the TAGS string in the current headline."
  8683. (unless (org-on-heading-p t)
  8684. (error "Not on a heading"))
  8685. (save-excursion
  8686. (beginning-of-line 1)
  8687. (if (looking-at ".*[ \t]\\(:[A-Za-z_@0-9:]+:\\)[ \t]*\\(\r\\|$\\)")
  8688. (org-match-string-no-properties 1)
  8689. "")))
  8690. (defun org-get-buffer-tags ()
  8691. "Get a table of all tags used in the buffer, for completion."
  8692. (let (tags)
  8693. (save-excursion
  8694. (goto-char (point-min))
  8695. (while (re-search-forward "[ \t]:\\([A-Za-z_@0-9:]+\\):[ \t\r\n]" nil t)
  8696. (mapc (lambda (x) (add-to-list 'tags x))
  8697. (org-split-string (org-match-string-no-properties 1) ":"))))
  8698. (mapcar 'list tags)))
  8699. ;;; Link Stuff
  8700. (defvar org-create-file-search-functions nil
  8701. "List of functions to construct the right search string for a file link.
  8702. These functions are called in turn with point at the location to
  8703. which the link should point.
  8704. A function in the hook should first test if it would like to
  8705. handle this file type, for example by checking the major-mode or
  8706. the file extension. If it decides not to handle this file, it
  8707. should just return nil to give other functions a chance. If it
  8708. does handle the file, it must return the search string to be used
  8709. when following the link. The search string will be part of the
  8710. file link, given after a double colon, and `org-open-at-point'
  8711. will automatically search for it. If special measures must be
  8712. taken to make the search successful, another function should be
  8713. added to the companion hook `org-execute-file-search-functions',
  8714. which see.
  8715. A function in this hook may also use `setq' to set the variable
  8716. `description' to provide a suggestion for the descriptive text to
  8717. be used for this link when it gets inserted into an Org-mode
  8718. buffer with \\[org-insert-link].")
  8719. (defvar org-execute-file-search-functions nil
  8720. "List of functions to execute a file search triggered by a link.
  8721. Functions added to this hook must accept a single argument, the
  8722. search string that was part of the file link, the part after the
  8723. double colon. The function must first check if it would like to
  8724. handle this search, for example by checking the major-mode or the
  8725. file extension. If it decides not to handle this search, it
  8726. should just return nil to give other functions a chance. If it
  8727. does handle the search, it must return a non-nil value to keep
  8728. other functions from trying.
  8729. Each function can access the current prefix argument through the
  8730. variable `current-prefix-argument'. Note that a single prefix is
  8731. used to force opening a link in Emacs, so it may be good to only
  8732. use a numeric or double prefix to guide the search function.
  8733. In case this is needed, a function in this hook can also restore
  8734. the window configuration before `org-open-at-point' was called using:
  8735. (set-window-configuration org-window-config-before-follow-link)")
  8736. (defun org-find-file-at-mouse (ev)
  8737. "Open file link or URL at mouse."
  8738. (interactive "e")
  8739. (mouse-set-point ev)
  8740. (org-open-at-point 'in-emacs))
  8741. (defun org-open-at-mouse (ev)
  8742. "Open file link or URL at mouse."
  8743. (interactive "e")
  8744. (mouse-set-point ev)
  8745. (org-open-at-point))
  8746. (defvar org-window-config-before-follow-link nil
  8747. "The window configuration before following a link.
  8748. This is saved in case the need arises to restore it.")
  8749. (defun org-open-at-point (&optional in-emacs)
  8750. "Open link at or after point.
  8751. If there is no link at point, this function will search forward up to
  8752. the end of the current subtree.
  8753. Normally, files will be opened by an appropriate application. If the
  8754. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  8755. (interactive "P")
  8756. (setq org-window-config-before-follow-link (current-window-configuration))
  8757. (org-remove-occur-highlights nil nil t)
  8758. (if (org-at-timestamp-p)
  8759. (org-follow-timestamp-link)
  8760. (let (type path link line search (pos (point)))
  8761. (catch 'match
  8762. (save-excursion
  8763. (skip-chars-forward "^]\n\r")
  8764. (when (and (re-search-backward "\\[\\[" nil t)
  8765. (looking-at org-bracket-link-regexp)
  8766. (<= (match-beginning 0) pos)
  8767. (>= (match-end 0) pos))
  8768. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  8769. (while (string-match " *\n *" link)
  8770. (setq link (replace-match " " t t link)))
  8771. (if (string-match org-link-re-with-space2 link)
  8772. (setq type (match-string 1 link)
  8773. path (match-string 2 link))
  8774. (setq type "thisfile"
  8775. path link))
  8776. (throw 'match t)))
  8777. (when (get-text-property (point) 'org-linked-text)
  8778. (setq type "thisfile"
  8779. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8780. (1+ (point)) (point))
  8781. path (buffer-substring
  8782. (previous-single-property-change pos 'org-linked-text)
  8783. (next-single-property-change pos 'org-linked-text)))
  8784. (throw 'match t))
  8785. (save-excursion
  8786. (skip-chars-backward (concat "^[]" org-non-link-chars " "))
  8787. (if (equal (char-before) ?<) (backward-char 1))
  8788. (when (or (looking-at org-angle-link-re)
  8789. (looking-at org-plain-link-re)
  8790. (and (or (re-search-forward org-angle-link-re (point-at-eol) t)
  8791. (re-search-forward org-plain-link-re (point-at-eol) t))
  8792. (<= (match-beginning 0) pos)
  8793. (>= (match-end 0) pos)))
  8794. (setq type (match-string 1)
  8795. path (match-string 2))
  8796. (throw 'match t)))
  8797. (save-excursion
  8798. (skip-chars-backward "^ \t\n\r")
  8799. (when (looking-at "\\(:[A-Za-z_@0-9:]+\\):[ \t\r\n]")
  8800. (setq type "tags"
  8801. path (match-string 1))
  8802. (while (string-match ":" path)
  8803. (setq path (replace-match "+" t t path)))
  8804. (throw 'match t)))
  8805. (save-excursion
  8806. (skip-chars-backward "a-zA-Z_")
  8807. (when (and (memq 'camel org-activate-links)
  8808. (looking-at org-camel-regexp))
  8809. (setq type "camel" path (match-string 0))
  8810. (if (equal (char-before) ?*)
  8811. (setq path (concat "*" path))))
  8812. (throw 'match t)))
  8813. (unless path
  8814. (error "No link found"))
  8815. ;; Remove any trailing spaces in path
  8816. (if (string-match " +\\'" path)
  8817. (setq path (replace-match "" t t path)))
  8818. (cond
  8819. ((equal type "mailto")
  8820. (let ((cmd (car org-link-mailto-program))
  8821. (args (cdr org-link-mailto-program)) args1
  8822. (address path) (subject "") a)
  8823. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  8824. (setq address (match-string 1 path)
  8825. subject (org-link-escape (match-string 2 path))))
  8826. (while args
  8827. (cond
  8828. ((not (stringp (car args))) (push (pop args) args1))
  8829. (t (setq a (pop args))
  8830. (if (string-match "%a" a)
  8831. (setq a (replace-match address t t a)))
  8832. (if (string-match "%s" a)
  8833. (setq a (replace-match subject t t a)))
  8834. (push a args1))))
  8835. (apply cmd (nreverse args1))))
  8836. ((member type '("http" "https" "ftp" "news"))
  8837. (browse-url (concat type ":" path)))
  8838. ((string= type "tags")
  8839. (org-tags-view in-emacs path))
  8840. ((or (string= type "camel")
  8841. (string= type "thisfile"))
  8842. (org-mark-ring-push)
  8843. (org-link-search
  8844. path
  8845. (cond ((equal in-emacs '(4)) 'occur)
  8846. ((equal in-emacs '(16)) 'org-occur)
  8847. (t nil))))
  8848. ((string= type "file")
  8849. (if (string-match "::\\([0-9]+\\)\\'" path)
  8850. (setq line (string-to-number (match-string 1 path))
  8851. path (substring path 0 (match-beginning 0)))
  8852. (if (string-match "::\\(.+\\)\\'" path)
  8853. (setq search (match-string 1 path)
  8854. path (substring path 0 (match-beginning 0)))))
  8855. (org-open-file path in-emacs line search))
  8856. ((string= type "news")
  8857. (org-follow-gnus-link path))
  8858. ((string= type "bbdb")
  8859. (org-follow-bbdb-link path))
  8860. ((string= type "info")
  8861. (org-follow-info-link path))
  8862. ((string= type "gnus")
  8863. (let (group article)
  8864. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  8865. (error "Error in Gnus link"))
  8866. (setq group (match-string 1 path)
  8867. article (match-string 3 path))
  8868. (org-follow-gnus-link group article)))
  8869. ((string= type "vm")
  8870. (let (folder article)
  8871. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  8872. (error "Error in VM link"))
  8873. (setq folder (match-string 1 path)
  8874. article (match-string 3 path))
  8875. ;; in-emacs is the prefix arg, will be interpreted as read-only
  8876. (org-follow-vm-link folder article in-emacs)))
  8877. ((string= type "wl")
  8878. (let (folder article)
  8879. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  8880. (error "Error in Wanderlust link"))
  8881. (setq folder (match-string 1 path)
  8882. article (match-string 3 path))
  8883. (org-follow-wl-link folder article)))
  8884. ((string= type "mhe")
  8885. (let (folder article)
  8886. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  8887. (error "Error in MHE link"))
  8888. (setq folder (match-string 1 path)
  8889. article (match-string 3 path))
  8890. (org-follow-mhe-link folder article)))
  8891. ((string= type "rmail")
  8892. (let (folder article)
  8893. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  8894. (error "Error in RMAIL link"))
  8895. (setq folder (match-string 1 path)
  8896. article (match-string 3 path))
  8897. (org-follow-rmail-link folder article)))
  8898. ((string= type "shell")
  8899. (let ((cmd path))
  8900. (while (string-match "@{" cmd)
  8901. (setq cmd (replace-match "<" t t cmd)))
  8902. (while (string-match "@}" cmd)
  8903. (setq cmd (replace-match ">" t t cmd)))
  8904. (if (or (not org-confirm-shell-link-function)
  8905. (funcall org-confirm-shell-link-function
  8906. (format "Execute \"%s\" in shell? "
  8907. (org-add-props cmd nil
  8908. 'face 'org-warning))))
  8909. (progn
  8910. (message "Executing %s" cmd)
  8911. (shell-command cmd))
  8912. (error "Abort"))))
  8913. ((string= type "elisp")
  8914. (let ((cmd path))
  8915. (if (or (not org-confirm-elisp-link-function)
  8916. (funcall org-confirm-elisp-link-function
  8917. (format "Execute \"%s\" as elisp? "
  8918. (org-add-props cmd nil
  8919. 'face 'org-warning))))
  8920. (message "%s => %s" cmd (eval (read cmd)))
  8921. (error "Abort"))))
  8922. (t
  8923. (browse-url-at-point))))))
  8924. (defun org-link-search (s &optional type)
  8925. "Search for a link search option.
  8926. When S is a CamelCaseWord, search for a target, or for a sentence containing
  8927. the words. If S is surrounded by forward slashes, it is interpreted as a
  8928. regular expression. In org-mode files, this will create an `org-occur'
  8929. sparse tree. In ordinary files, `occur' will be used to list matches.
  8930. If the current buffer is in `dired-mode', grep will be used to search
  8931. in all files."
  8932. (let ((case-fold-search t)
  8933. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  8934. (pos (point))
  8935. (pre "") (post "")
  8936. words re0 re1 re2 re3 re4 re5 re2a reall camel)
  8937. (cond
  8938. ;; First check if there are any special
  8939. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  8940. ;; Now try the builtin stuff
  8941. ((save-excursion
  8942. (goto-char (point-min))
  8943. (and
  8944. (re-search-forward
  8945. (concat "<<" (regexp-quote s0) ">>") nil t)
  8946. (setq pos (match-beginning 0))))
  8947. ;; There is an exact target for this
  8948. (goto-char pos))
  8949. ((string-match "^/\\(.*\\)/$" s)
  8950. ;; A regular expression
  8951. (cond
  8952. ((org-mode-p)
  8953. (org-occur (match-string 1 s)))
  8954. ;;((eq major-mode 'dired-mode)
  8955. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  8956. (t (org-do-occur (match-string 1 s)))))
  8957. ((or (setq camel (string-match (concat "^" org-camel-regexp "$") s))
  8958. t)
  8959. ;; A camel or a normal search string
  8960. (when (equal (string-to-char s) ?*)
  8961. ;; Anchor on headlines, post may include tags.
  8962. (setq pre "^\\*+[ \t]*\\(?:\\sw+\\)?[ \t]*"
  8963. post "[ \t]*\\(?:[ \t]+:[a-zA-Z_@0-9:+]:[ \t]*\\)?$"
  8964. s (substring s 1)))
  8965. (remove-text-properties
  8966. 0 (length s)
  8967. '(face nil mouse-face nil keymap nil fontified nil) s)
  8968. ;; Make a series of regular expressions to find a match
  8969. (setq words
  8970. (if camel
  8971. (org-camel-to-words s)
  8972. (org-split-string s "[ \n\r\t]+"))
  8973. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  8974. re2 (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t]+") "\\)[ \t\r\n]")
  8975. re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  8976. re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  8977. re1 (concat pre re2 post)
  8978. re3 (concat pre re4 post)
  8979. re5 (concat pre ".*" re4)
  8980. re2 (concat pre re2)
  8981. re2a (concat pre re2a)
  8982. re4 (concat pre re4)
  8983. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  8984. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  8985. re5 "\\)"
  8986. ))
  8987. (cond
  8988. ((eq type 'org-occur) (org-occur reall))
  8989. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  8990. (t (goto-char (point-min))
  8991. (if (or (org-search-not-link re0 nil t)
  8992. (org-search-not-link re1 nil t)
  8993. (org-search-not-link re2 nil t)
  8994. (org-search-not-link re2a nil t)
  8995. (org-search-not-link re3 nil t)
  8996. (org-search-not-link re4 nil t)
  8997. (org-search-not-link re5 nil t)
  8998. )
  8999. (goto-char (match-beginning 1))
  9000. (goto-char pos)
  9001. (error "No match")))))
  9002. (t
  9003. ;; Normal string-search
  9004. (goto-char (point-min))
  9005. (if (search-forward s nil t)
  9006. (goto-char (match-beginning 0))
  9007. (error "No match"))))
  9008. (and (org-mode-p) (org-show-hierarchy-above 'link-search))))
  9009. (defun org-search-not-link (&rest args)
  9010. "Execute `re-search-forward', but only accept matches that are not a link."
  9011. (catch 'exit
  9012. (let (p1)
  9013. (while (apply 're-search-forward args)
  9014. (setq p1 (point))
  9015. (if (not (save-match-data
  9016. (and (re-search-backward "\\[\\[" nil t)
  9017. (looking-at org-bracket-link-regexp)
  9018. (<= (match-beginning 0) p1)
  9019. (>= (match-end 0) p1))))
  9020. (progn (goto-char (match-end 0))
  9021. (throw 'exit (point)))
  9022. (goto-char (match-end 0)))))))
  9023. (defun org-do-occur (regexp &optional cleanup)
  9024. "Call the Emacs command `occur'.
  9025. If CLEANUP is non-nil, remove the printout of the regular expression
  9026. in the *Occur* buffer. This is useful if the regex is long and not useful
  9027. to read."
  9028. (occur regexp)
  9029. (when cleanup
  9030. (let ((cwin (selected-window)) win beg end)
  9031. (when (setq win (get-buffer-window "*Occur*"))
  9032. (select-window win))
  9033. (goto-char (point-min))
  9034. (when (re-search-forward "match[a-z]+" nil t)
  9035. (setq beg (match-end 0))
  9036. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9037. (setq end (1- (match-beginning 0)))))
  9038. (and beg end (let ((buffer-read-only)) (delete-region beg end)))
  9039. (goto-char (point-min))
  9040. (select-window cwin))))
  9041. (defvar org-mark-ring nil
  9042. "Mark ring for positions before jumps in Org-mode.")
  9043. (defvar org-mark-ring-last-goto nil
  9044. "Last position in the mark ring used to go back.")
  9045. ;; Fill and close the ring
  9046. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9047. (loop for i from 1 to org-mark-ring-length do
  9048. (push (make-marker) org-mark-ring))
  9049. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9050. org-mark-ring)
  9051. (defun org-mark-ring-push (&optional pos buffer)
  9052. "Put the current position or POS into the mark ring and rotate it."
  9053. (interactive)
  9054. (setq pos (or pos (point)))
  9055. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9056. (move-marker (car org-mark-ring)
  9057. (or pos (point))
  9058. (or buffer (current-buffer)))
  9059. (message
  9060. (substitute-command-keys
  9061. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9062. (defun org-mark-ring-goto (&optional n)
  9063. "Jump to the previous position in the mark ring.
  9064. With prefix arg N, jump back that many stored positions. When
  9065. called several times in succession, walk through the entire ring.
  9066. Org-mode commands jumping to a different position in the current file,
  9067. or to another Org-mode file, automatically push the old position
  9068. onto the ring."
  9069. (interactive "p")
  9070. (let (p m)
  9071. (if (eq last-command this-command)
  9072. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9073. (setq p org-mark-ring))
  9074. (setq org-mark-ring-last-goto p)
  9075. (setq m (car p))
  9076. (switch-to-buffer (marker-buffer m))
  9077. (goto-char m)
  9078. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-hierarchy-above 'mark-goto))))
  9079. (defun org-camel-to-words (s)
  9080. "Split \"CamelCaseWords\" to (\"Camel\" \"Case\" \"Words\")."
  9081. (let ((case-fold-search nil)
  9082. words)
  9083. (while (string-match "[a-z][A-Z]" s)
  9084. (push (substring s 0 (1+ (match-beginning 0))) words)
  9085. (setq s (substring s (1+ (match-beginning 0)))))
  9086. (nreverse (cons s words))))
  9087. (defun org-remove-angle-brackets (s)
  9088. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9089. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9090. s)
  9091. (defun org-add-angle-brackets (s)
  9092. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9093. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9094. s)
  9095. (defun org-follow-timestamp-link ()
  9096. (cond
  9097. ((org-at-date-range-p)
  9098. (let ((org-agenda-start-on-weekday)
  9099. (t1 (match-string 1))
  9100. (t2 (match-string 2)))
  9101. (setq t1 (time-to-days (org-time-string-to-time t1))
  9102. t2 (time-to-days (org-time-string-to-time t2)))
  9103. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  9104. ((org-at-timestamp-p)
  9105. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9106. (substring (match-string 1) 0 10)))
  9107. 1))
  9108. (t (error "This should not happen"))))
  9109. (defun org-follow-bbdb-link (name)
  9110. "Follow a BBDB link to NAME."
  9111. (require 'bbdb)
  9112. (let ((inhibit-redisplay t)
  9113. (bbdb-electric-p nil))
  9114. (catch 'exit
  9115. ;; Exact match on name
  9116. (bbdb-name (concat "\\`" name "\\'") nil)
  9117. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  9118. ;; Exact match on name
  9119. (bbdb-company (concat "\\`" name "\\'") nil)
  9120. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  9121. ;; Partial match on name
  9122. (bbdb-name name nil)
  9123. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  9124. ;; Partial match on company
  9125. (bbdb-company name nil)
  9126. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  9127. ;; General match including network address and notes
  9128. (bbdb name nil)
  9129. (when (= 0 (buffer-size (get-buffer "*BBDB*")))
  9130. (delete-window (get-buffer-window "*BBDB*"))
  9131. (error "No matching BBDB record")))))
  9132. (defun org-follow-info-link (name)
  9133. "Follow an info file & node link to NAME."
  9134. (if (or (string-match "\\(.*\\)::?\\(.*\\)" name)
  9135. (string-match "\\(.*\\)" name))
  9136. (progn
  9137. (require 'info)
  9138. (if (match-string 2 name) ; If there isn't a node, choose "Top"
  9139. (Info-find-node (match-string 1 name) (match-string 2 name))
  9140. (Info-find-node (match-string 1 name) "Top")))
  9141. (message (concat "Could not open: " name))))
  9142. (defun org-follow-gnus-link (&optional group article)
  9143. "Follow a Gnus link to GROUP and ARTICLE."
  9144. (require 'gnus)
  9145. (funcall (cdr (assq 'gnus org-link-frame-setup)))
  9146. (if gnus-other-frame-object (select-frame gnus-other-frame-object))
  9147. (if group (gnus-fetch-group group))
  9148. (if article
  9149. (or (gnus-summary-goto-article article nil 'force)
  9150. (if (fboundp 'gnus-summary-insert-cached-articles)
  9151. (progn
  9152. (gnus-summary-insert-cached-articles)
  9153. (gnus-summary-goto-article article nil 'force))
  9154. (message "Message could not be found.")))))
  9155. (defun org-follow-vm-link (&optional folder article readonly)
  9156. "Follow a VM link to FOLDER and ARTICLE."
  9157. (require 'vm)
  9158. (setq article (org-add-angle-brackets article))
  9159. (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
  9160. ;; ange-ftp or efs or tramp access
  9161. (let ((user (or (match-string 1 folder) (user-login-name)))
  9162. (host (match-string 2 folder))
  9163. (file (match-string 3 folder)))
  9164. (cond
  9165. ((featurep 'tramp)
  9166. ;; use tramp to access the file
  9167. (if (featurep 'xemacs)
  9168. (setq folder (format "[%s@%s]%s" user host file))
  9169. (setq folder (format "/%s@%s:%s" user host file))))
  9170. (t
  9171. ;; use ange-ftp or efs
  9172. (require (if (featurep 'xemacs) 'efs 'ange-ftp))
  9173. (setq folder (format "/%s@%s:%s" user host file))))))
  9174. (when folder
  9175. (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
  9176. (sit-for 0.1)
  9177. (when article
  9178. (vm-select-folder-buffer)
  9179. (widen)
  9180. (let ((case-fold-search t))
  9181. (goto-char (point-min))
  9182. (if (not (re-search-forward
  9183. (concat "^" "message-id: *" (regexp-quote article))))
  9184. (error "Could not find the specified message in this folder"))
  9185. (vm-isearch-update)
  9186. (vm-isearch-narrow)
  9187. (vm-beginning-of-message)
  9188. (vm-summarize)))))
  9189. (defun org-follow-wl-link (folder article)
  9190. "Follow a Wanderlust link to FOLDER and ARTICLE."
  9191. (setq article (org-add-angle-brackets article))
  9192. (wl-summary-goto-folder-subr folder 'no-sync t nil t)
  9193. (if article (wl-summary-jump-to-msg-by-message-id article ">"))
  9194. (wl-summary-redisplay))
  9195. (defun org-follow-rmail-link (folder article)
  9196. "Follow an RMAIL link to FOLDER and ARTICLE."
  9197. (setq article (org-add-angle-brackets article))
  9198. (let (message-number)
  9199. (save-excursion
  9200. (save-window-excursion
  9201. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  9202. (setq message-number
  9203. (save-restriction
  9204. (widen)
  9205. (goto-char (point-max))
  9206. (if (re-search-backward
  9207. (concat "^Message-ID:\\s-+" (regexp-quote
  9208. (or article "")))
  9209. nil t)
  9210. (rmail-what-message))))))
  9211. (if message-number
  9212. (progn
  9213. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  9214. (rmail-show-message message-number)
  9215. message-number)
  9216. (error "Message not found"))))
  9217. ;; mh-e integration based on planner-mode
  9218. (defun org-mhe-get-message-real-folder ()
  9219. "Return the name of the current message real folder, so if you use
  9220. sequences, it will now work."
  9221. (save-excursion
  9222. (let* ((folder
  9223. (if (equal major-mode 'mh-folder-mode)
  9224. mh-current-folder
  9225. ;; Refer to the show buffer
  9226. mh-show-folder-buffer))
  9227. (end-index
  9228. (if (boundp 'mh-index-folder)
  9229. (min (length mh-index-folder) (length folder))))
  9230. )
  9231. ;; a simple test on mh-index-data does not work, because
  9232. ;; mh-index-data is always nil in a show buffer.
  9233. (if (and (boundp 'mh-index-folder)
  9234. (string= mh-index-folder (substring folder 0 end-index)))
  9235. (if (equal major-mode 'mh-show-mode)
  9236. (save-window-excursion
  9237. (when (buffer-live-p (get-buffer folder))
  9238. (progn
  9239. (pop-to-buffer folder)
  9240. (org-mhe-get-message-folder-from-index)
  9241. )
  9242. ))
  9243. (org-mhe-get-message-folder-from-index)
  9244. )
  9245. folder
  9246. )
  9247. )))
  9248. (defun org-mhe-get-message-folder-from-index ()
  9249. "Returns the name of the message folder in a index folder buffer."
  9250. (save-excursion
  9251. (mh-index-previous-folder)
  9252. (re-search-forward "^\\(+.*\\)$" nil t)
  9253. (message (match-string 1))))
  9254. (defun org-mhe-get-message-folder ()
  9255. "Return the name of the current message folder. Be careful if you
  9256. use sequences."
  9257. (save-excursion
  9258. (if (equal major-mode 'mh-folder-mode)
  9259. mh-current-folder
  9260. ;; Refer to the show buffer
  9261. mh-show-folder-buffer)))
  9262. (defun org-mhe-get-message-num ()
  9263. "Return the number of the current message. Be careful if you
  9264. use sequences."
  9265. (save-excursion
  9266. (if (equal major-mode 'mh-folder-mode)
  9267. (mh-get-msg-num nil)
  9268. ;; Refer to the show buffer
  9269. (mh-show-buffer-message-number))))
  9270. (defun org-mhe-get-header (header)
  9271. "Return a header of the message in folder mode. This will create a
  9272. show buffer for the corresponding message. If you have a more clever
  9273. idea..."
  9274. (let* ((folder (org-mhe-get-message-folder))
  9275. (num (org-mhe-get-message-num))
  9276. (buffer (get-buffer-create (concat "show-" folder)))
  9277. (header-field))
  9278. (with-current-buffer buffer
  9279. (mh-display-msg num folder)
  9280. (if (equal major-mode 'mh-folder-mode)
  9281. (mh-header-display)
  9282. (mh-show-header-display))
  9283. (set-buffer buffer)
  9284. (setq header-field (mh-get-header-field header))
  9285. (if (equal major-mode 'mh-folder-mode)
  9286. (mh-show)
  9287. (mh-show-show))
  9288. header-field)))
  9289. (defun org-follow-mhe-link (folder article)
  9290. "Follow an MHE link to FOLDER and ARTICLE.
  9291. If ARTICLE is nil FOLDER is shown. If the configuration variable
  9292. `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
  9293. ARTICLE is searched in all folders. Indexed searches (swish++,
  9294. namazu, and others supported by MH-E) will always search in all
  9295. folders."
  9296. (require 'mh-e)
  9297. (require 'mh-search)
  9298. (require 'mh-utils)
  9299. (mh-find-path)
  9300. (if (not article)
  9301. (mh-visit-folder (mh-normalize-folder-name folder))
  9302. (setq article (org-add-angle-brackets article))
  9303. (mh-search-choose)
  9304. (if (equal mh-searcher 'pick)
  9305. (progn
  9306. (mh-search folder (list "--message-id" article))
  9307. (when (and org-mhe-search-all-folders
  9308. (not (org-mhe-get-message-real-folder)))
  9309. (kill-this-buffer)
  9310. (mh-search "+" (list "--message-id" article))))
  9311. (mh-search "+" article))
  9312. (if (org-mhe-get-message-real-folder)
  9313. (mh-show-msg 1)
  9314. (kill-this-buffer)
  9315. (error "Message not found"))))
  9316. ;; BibTeX links
  9317. ;; Use the custom search meachnism to construct and use search strings for
  9318. ;; file links to BibTeX database entries.
  9319. (defun org-create-file-search-in-bibtex ()
  9320. "Create the search string and description for a BibTeX database entry."
  9321. (when (eq major-mode 'bibtex-mode)
  9322. ;; yes, we want to construct this search string.
  9323. ;; Make a good description for this entry, using names, year and the title
  9324. ;; Put it into the `description' variable which is dynamically scoped.
  9325. (let ((bibtex-autokey-names 1)
  9326. (bibtex-autokey-names-stretch 1)
  9327. (bibtex-autokey-name-case-convert-function 'identity)
  9328. (bibtex-autokey-name-separator " & ")
  9329. (bibtex-autokey-additional-names " et al.")
  9330. (bibtex-autokey-year-length 4)
  9331. (bibtex-autokey-name-year-separator " ")
  9332. (bibtex-autokey-titlewords 3)
  9333. (bibtex-autokey-titleword-separator " ")
  9334. (bibtex-autokey-titleword-case-convert-function 'identity)
  9335. (bibtex-autokey-titleword-length 'infty)
  9336. (bibtex-autokey-year-title-separator ": "))
  9337. (setq description (bibtex-generate-autokey)))
  9338. ;; Now parse the entry, get the key and return it.
  9339. (save-excursion
  9340. (bibtex-beginning-of-entry)
  9341. (cdr (assoc "=key=" (bibtex-parse-entry))))))
  9342. (defun org-execute-file-search-in-bibtex (s)
  9343. "Find the link search string S as a key for a database entry."
  9344. (when (eq major-mode 'bibtex-mode)
  9345. ;; Yes, we want to do the search in this file.
  9346. ;; We construct a regexp that searches for "@entrytype{" followed by the key
  9347. (goto-char (point-min))
  9348. (and (re-search-forward (concat "@[a-zA-Z]+[ \t\n]*{[ \t\n]*"
  9349. (regexp-quote s) "[ \t\n]*,") nil t)
  9350. (goto-char (match-beginning 0)))
  9351. (if (and (match-beginning 0) (equal current-prefix-arg '(16)))
  9352. ;; Use double prefix to indicate that any web link should be browsed
  9353. (let ((b (current-buffer)) (p (point)))
  9354. ;; Restore the window configuration because we just use the web link
  9355. (set-window-configuration org-window-config-before-follow-link)
  9356. (save-excursion (set-buffer b) (goto-char p)
  9357. (bibtex-url)))
  9358. (recenter 0)) ; Move entry start to beginning of window
  9359. ;; return t to indicate that the search is done.
  9360. t))
  9361. ;; Finally add the functions to the right hooks.
  9362. (add-hook 'org-create-file-search-functions 'org-create-file-search-in-bibtex)
  9363. (add-hook 'org-execute-file-search-functions 'org-execute-file-search-in-bibtex)
  9364. ;; end of Bibtex link setup
  9365. (defun org-upgrade-old-links (&optional query-description)
  9366. "Transfer old <...> style links to new [[...]] style links.
  9367. With arg query-description, ask at each match for a description text to use
  9368. for this link."
  9369. (interactive (list (y-or-n-p "Would you like to be queried for a description at each link?")))
  9370. (save-excursion
  9371. (goto-char (point-min))
  9372. (let ((re (concat "\\([^[]\\)<\\("
  9373. "\\(" (mapconcat 'identity org-link-types "\\|")
  9374. "\\):"
  9375. "[^" org-non-link-chars "]+\\)>"))
  9376. l1 l2 (cnt 0))
  9377. (while (re-search-forward re nil t)
  9378. (setq cnt (1+ cnt)
  9379. l1 (org-match-string-no-properties 2)
  9380. l2 (save-match-data (org-link-escape l1)))
  9381. (when query-description (setq l1 (read-string "Desc: " l1)))
  9382. (if (equal l1 l2)
  9383. (replace-match (concat (match-string 1) "[[" l1 "]]") t t)
  9384. (replace-match (concat (match-string 1) "[[" l2 "][" l1 "]]") t t)))
  9385. (message "%d matches have beed treated" cnt))))
  9386. (defun org-open-file (path &optional in-emacs line search)
  9387. "Open the file at PATH.
  9388. First, this expands any special file name abbreviations. Then the
  9389. configuration variable `org-file-apps' is checked if it contains an
  9390. entry for this file type, and if yes, the corresponding command is launched.
  9391. If no application is found, Emacs simply visits the file.
  9392. With optional argument IN-EMACS, Emacs will visit the file.
  9393. Optional LINE specifies a line to go to, optional SEARCH a string to
  9394. search for. If LINE or SEARCH is given, the file will always be
  9395. opened in Emacs.
  9396. If the file does not exist, an error is thrown."
  9397. (setq in-emacs (or in-emacs line search))
  9398. (let* ((file (if (equal path "")
  9399. buffer-file-name
  9400. (substitute-in-file-name (expand-file-name path))))
  9401. (apps (append org-file-apps (org-default-apps)))
  9402. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9403. (dirp (if remp nil (file-directory-p file)))
  9404. (dfile (downcase file))
  9405. (old-buffer (current-buffer))
  9406. (old-pos (point))
  9407. (old-mode major-mode)
  9408. ext cmd)
  9409. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9410. (setq ext (match-string 1 dfile))
  9411. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9412. (setq ext (match-string 1 dfile))))
  9413. (if in-emacs
  9414. (setq cmd 'emacs)
  9415. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9416. (and dirp (cdr (assoc 'directory apps)))
  9417. (cdr (assoc ext apps))
  9418. (cdr (assoc t apps)))))
  9419. (when (eq cmd 'mailcap)
  9420. (require 'mailcap)
  9421. (mailcap-parse-mailcaps)
  9422. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9423. (command (mailcap-mime-info mime-type)))
  9424. (if (stringp command)
  9425. (setq cmd command)
  9426. (setq cmd 'emacs))))
  9427. (if (and (not (eq cmd 'emacs)) ; Emacs has not problems with non-ex files
  9428. (not (file-exists-p file))
  9429. (not org-open-non-existing-files))
  9430. (error "No such file: %s" file))
  9431. (cond
  9432. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9433. ;; Normalize use of quote, this can vary.
  9434. (if (string-match "['\"]%s['\"]" cmd)
  9435. (setq cmd (replace-match "'%s'" t t cmd)))
  9436. (setq cmd (format cmd file))
  9437. (save-window-excursion
  9438. (shell-command (concat cmd " &"))))
  9439. ((or (stringp cmd)
  9440. (eq cmd 'emacs))
  9441. ; (unless (equal (file-truename file) (file-truename (or buffer-file-name "")))
  9442. ; (funcall (cdr (assq 'file org-link-frame-setup)) file))
  9443. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9444. (if line (goto-line line)
  9445. (if search (org-link-search search))))
  9446. ((consp cmd)
  9447. (eval cmd))
  9448. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9449. (and (org-mode-p) (eq old-mode 'org-mode)
  9450. (or (not (equal old-buffer (current-buffer)))
  9451. (not (equal old-pos (point))))
  9452. (org-mark-ring-push old-pos old-buffer))))
  9453. (defun org-default-apps ()
  9454. "Return the default applications for this operating system."
  9455. (cond
  9456. ((eq system-type 'darwin)
  9457. org-file-apps-defaults-macosx)
  9458. ((eq system-type 'windows-nt)
  9459. org-file-apps-defaults-windowsnt)
  9460. (t org-file-apps-defaults-gnu)))
  9461. (defun org-expand-file-name (path)
  9462. "Replace special path abbreviations and expand the file name."
  9463. (expand-file-name path))
  9464. (defun org-file-remote-p (file)
  9465. "Test whether FILE specifies a location on a remote system.
  9466. Return non-nil if the location is indeed remote.
  9467. For example, the filename \"/user@host:/foo\" specifies a location
  9468. on the system \"/user@host:\"."
  9469. (cond ((fboundp 'file-remote-p)
  9470. (file-remote-p file))
  9471. ((fboundp 'tramp-handle-file-remote-p)
  9472. (tramp-handle-file-remote-p file))
  9473. ((and (boundp 'ange-ftp-name-format)
  9474. (string-match ange-ftp-name-format file))
  9475. t)
  9476. (t nil)))
  9477. (defvar org-insert-link-history nil
  9478. "Minibuffer history for links inserted with `org-insert-link'.")
  9479. (defvar org-stored-links nil
  9480. "Contains the links stored with `org-store-link'.")
  9481. ;;;###autoload
  9482. (defun org-store-link (arg)
  9483. "\\<org-mode-map>Store an org-link to the current location.
  9484. This link can later be inserted into an org-buffer with
  9485. \\[org-insert-link].
  9486. For some link types, a prefix arg is interpreted:
  9487. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  9488. For file links, arg negates `org-context-in-file-links'."
  9489. (interactive "P")
  9490. (let (link cpltxt desc description search txt (pos (point)))
  9491. (cond
  9492. ((eq major-mode 'bbdb-mode)
  9493. (setq cpltxt (concat
  9494. "bbdb:"
  9495. (or (bbdb-record-name (bbdb-current-record))
  9496. (bbdb-record-company (bbdb-current-record))))
  9497. link (org-make-link cpltxt)))
  9498. ((eq major-mode 'Info-mode)
  9499. (setq link (org-make-link "info:"
  9500. (file-name-nondirectory Info-current-file)
  9501. ":" Info-current-node))
  9502. (setq cpltxt (concat (file-name-nondirectory Info-current-file)
  9503. ":" Info-current-node)))
  9504. ((eq major-mode 'calendar-mode)
  9505. (let ((cd (calendar-cursor-to-date)))
  9506. (setq link
  9507. (format-time-string
  9508. (car org-time-stamp-formats)
  9509. (apply 'encode-time
  9510. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  9511. nil nil nil))))))
  9512. ((or (eq major-mode 'vm-summary-mode)
  9513. (eq major-mode 'vm-presentation-mode))
  9514. (and (eq major-mode 'vm-presentation-mode) (vm-summarize))
  9515. (vm-follow-summary-cursor)
  9516. (save-excursion
  9517. (vm-select-folder-buffer)
  9518. (let* ((message (car vm-message-pointer))
  9519. (folder buffer-file-name)
  9520. (subject (vm-su-subject message))
  9521. (author (vm-su-full-name message))
  9522. (message-id (vm-su-message-id message)))
  9523. (setq message-id (org-remove-angle-brackets message-id))
  9524. (setq folder (abbreviate-file-name folder))
  9525. (if (string-match (concat "^" (regexp-quote vm-folder-directory))
  9526. folder)
  9527. (setq folder (replace-match "" t t folder)))
  9528. (setq cpltxt (concat author " on: " subject))
  9529. (setq link (org-make-link "vm:" folder "#" message-id)))))
  9530. ((eq major-mode 'wl-summary-mode)
  9531. (let* ((msgnum (wl-summary-message-number))
  9532. (message-id (elmo-message-field wl-summary-buffer-elmo-folder
  9533. msgnum 'message-id))
  9534. (wl-message-entity (elmo-msgdb-overview-get-entity
  9535. msgnum (wl-summary-buffer-msgdb)))
  9536. (author (wl-summary-line-from)) ; FIXME: correct?
  9537. (subject "???")) ; FIXME:
  9538. (setq message-id (org-remove-angle-brackets message-id))
  9539. (setq cpltxt (concat author " on: " subject))
  9540. (setq link (org-make-link "wl:" wl-summary-buffer-folder-name
  9541. "#" message-id))))
  9542. ((or (equal major-mode 'mh-folder-mode)
  9543. (equal major-mode 'mh-show-mode))
  9544. (let ((from-header (org-mhe-get-header "From:"))
  9545. (to-header (org-mhe-get-header "To:"))
  9546. (subject (org-mhe-get-header "Subject:")))
  9547. (setq cpltxt (concat from-header " on: " subject))
  9548. (setq link (org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
  9549. (org-remove-angle-brackets
  9550. (org-mhe-get-header "Message-Id:"))))))
  9551. ((eq major-mode 'rmail-mode)
  9552. (save-excursion
  9553. (save-restriction
  9554. (rmail-narrow-to-non-pruned-header)
  9555. (let ((folder buffer-file-name)
  9556. (message-id (mail-fetch-field "message-id"))
  9557. (author (mail-fetch-field "from"))
  9558. (subject (mail-fetch-field "subject")))
  9559. (setq message-id (org-remove-angle-brackets message-id))
  9560. (setq cpltxt (concat author " on: " subject))
  9561. (setq link (org-make-link "rmail:" folder "#" message-id))))))
  9562. ((eq major-mode 'gnus-group-mode)
  9563. (let ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
  9564. (gnus-group-group-name)) ; version
  9565. ((fboundp 'gnus-group-name)
  9566. (gnus-group-name))
  9567. (t "???"))))
  9568. (setq cpltxt (concat
  9569. (if (org-xor arg org-usenet-links-prefer-google)
  9570. "http://groups.google.com/groups?group="
  9571. "gnus:")
  9572. group)
  9573. link (org-make-link cpltxt))))
  9574. ((memq major-mode '(gnus-summary-mode gnus-article-mode))
  9575. (and (eq major-mode 'gnus-article-mode) (gnus-article-show-summary))
  9576. (gnus-summary-beginning-of-article)
  9577. (let* ((group (car gnus-article-current))
  9578. (article (cdr gnus-article-current))
  9579. (header (gnus-summary-article-header article))
  9580. (author (mail-header-from header))
  9581. (message-id (mail-header-id header))
  9582. (date (mail-header-date header))
  9583. (subject (gnus-summary-subject-string)))
  9584. (setq cpltxt (concat author " on: " subject))
  9585. (if (org-xor arg org-usenet-links-prefer-google)
  9586. (setq link
  9587. (concat
  9588. cpltxt "\n "
  9589. (format "http://groups.google.com/groups?as_umsgid=%s"
  9590. (org-fixup-message-id-for-http message-id))))
  9591. (setq link (org-make-link "gnus:" group
  9592. "#" (number-to-string article))))))
  9593. ((eq major-mode 'w3-mode)
  9594. (setq cpltxt (url-view-url t)
  9595. link (org-make-link cpltxt)))
  9596. ((eq major-mode 'w3m-mode)
  9597. (setq cpltxt (or w3m-current-title w3m-current-url)
  9598. link (org-make-link w3m-current-url)))
  9599. ((setq search (run-hook-with-args-until-success
  9600. 'org-create-file-search-functions))
  9601. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  9602. "::" search))
  9603. (setq cpltxt (or description link)))
  9604. ((eq major-mode 'image-mode)
  9605. (setq cpltxt (concat "file:"
  9606. (abbreviate-file-name buffer-file-name))
  9607. link (org-make-link cpltxt)))
  9608. ((eq major-mode 'dired-mode)
  9609. ;; link to the file in the current line
  9610. (setq cpltxt (concat "file:"
  9611. (abbreviate-file-name
  9612. (expand-file-name
  9613. (dired-get-filename nil t))))
  9614. link (org-make-link cpltxt)))
  9615. ((org-mode-p)
  9616. ;; Just link to current headline
  9617. (setq cpltxt (concat "file:"
  9618. (abbreviate-file-name buffer-file-name)))
  9619. ;; Add a context search string
  9620. (when (org-xor org-context-in-file-links arg)
  9621. ;; Check if we are on a target
  9622. (if (save-excursion
  9623. (skip-chars-forward "^>\n\r")
  9624. (and (re-search-backward "<<" nil t)
  9625. (looking-at "<<\\(.*?\\)>>")
  9626. (<= (match-beginning 0) pos)
  9627. (>= (match-end 0) pos)))
  9628. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  9629. (setq txt (cond
  9630. ((org-on-heading-p) nil)
  9631. ((org-region-active-p)
  9632. (buffer-substring (region-beginning) (region-end)))
  9633. (t (buffer-substring (point-at-bol) (point-at-eol)))))
  9634. (when (or (null txt) (string-match "\\S-" txt))
  9635. (setq cpltxt
  9636. (concat cpltxt "::"
  9637. (if org-file-link-context-use-camel-case
  9638. (org-make-org-heading-camel txt)
  9639. (org-make-org-heading-search-string txt)))
  9640. desc "NONE"))))
  9641. (if (string-match "::\\'" cpltxt)
  9642. (setq cpltxt (substring cpltxt 0 -2)))
  9643. (setq link (org-make-link cpltxt)))
  9644. (buffer-file-name
  9645. ;; Just link to this file here.
  9646. (setq cpltxt (concat "file:"
  9647. (abbreviate-file-name buffer-file-name)))
  9648. ;; Add a context string
  9649. (when (org-xor org-context-in-file-links arg)
  9650. (setq txt (if (org-region-active-p)
  9651. (buffer-substring (region-beginning) (region-end))
  9652. (buffer-substring (point-at-bol) (point-at-eol))))
  9653. ;; Only use search option if there is some text.
  9654. (when (string-match "\\S-" txt)
  9655. (setq cpltxt
  9656. (concat cpltxt "::"
  9657. (if org-file-link-context-use-camel-case
  9658. (org-make-org-heading-camel txt)
  9659. (org-make-org-heading-search-string txt)))
  9660. desc "NONE")))
  9661. (setq link (org-make-link cpltxt)))
  9662. ((interactive-p)
  9663. (error "Cannot link to a buffer which is not visiting a file"))
  9664. (t (setq link nil)))
  9665. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  9666. (setq link (or link cpltxt)
  9667. desc (or desc cpltxt))
  9668. (if (equal desc "NONE") (setq desc nil))
  9669. (if (and (interactive-p) link)
  9670. (progn
  9671. (setq org-stored-links
  9672. (cons (list cpltxt link desc) org-stored-links))
  9673. (message "Stored: %s" (or cpltxt link)))
  9674. (org-make-link-string link desc))))
  9675. (defun org-make-org-heading-search-string (&optional string heading)
  9676. "Make search string for STRING or current headline."
  9677. (interactive)
  9678. (let ((s (or string (org-get-heading))))
  9679. (unless (and string (not heading))
  9680. ;; We are using a headline, clean up garbage in there.
  9681. (if (string-match org-todo-regexp s)
  9682. (setq s (replace-match "" t t s)))
  9683. (if (string-match ":[a-zA-Z_@0-9:]+:[ \t]*$" s)
  9684. (setq s (replace-match "" t t s)))
  9685. (setq s (org-trim s))
  9686. (if (string-match (concat "^\\(" org-quote-string "\\|"
  9687. org-comment-string "\\)") s)
  9688. (setq s (replace-match "" t t s)))
  9689. (while (string-match org-ts-regexp s)
  9690. (setq s (replace-match "" t t s))))
  9691. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  9692. (setq s (replace-match " " t t s)))
  9693. (or string (setq s (concat "*" s))) ; Add * for headlines
  9694. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  9695. (defun org-make-org-heading-camel (&optional string heading)
  9696. "Make a CamelCase string for STRING or the current headline."
  9697. (interactive)
  9698. (let ((s (or string (org-get-heading))))
  9699. (unless (and string (not heading))
  9700. ;; We are using a headline, clean up garbage in there.
  9701. (if (string-match org-todo-regexp s)
  9702. (setq s (replace-match "" t t s)))
  9703. (if (string-match ":[a-zA-Z_@0-9:]+:[ \t]*$" s)
  9704. (setq s (replace-match "" t t s)))
  9705. (setq s (org-trim s))
  9706. (if (string-match (concat "^\\(" org-quote-string "\\|"
  9707. org-comment-string "\\)") s)
  9708. (setq s (replace-match "" t t s)))
  9709. (while (string-match org-ts-regexp s)
  9710. (setq s (replace-match "" t t s))))
  9711. (while (string-match "[^a-zA-Z_ \t]+" s)
  9712. (setq s (replace-match " " t t s)))
  9713. (or string (setq s (concat "*" s))) ; Add * for headlines
  9714. (mapconcat 'capitalize (org-split-string s "[ \t]+") "")))
  9715. (defun org-make-link (&rest strings)
  9716. "Concatenate STRINGS, format resulting string with `org-link-format'."
  9717. (format org-link-format (apply 'concat strings)))
  9718. (defun org-make-link-string (link &optional description)
  9719. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  9720. (if (eq org-link-style 'plain)
  9721. (if (equal description link)
  9722. link
  9723. (concat description "\n" link))
  9724. (when (stringp description)
  9725. ;; Remove brackets from the description, they are fatal.
  9726. (while (string-match "\\[\\|\\]" description)
  9727. (setq description (replace-match "" t t description))))
  9728. (when (equal (org-link-escape link) description)
  9729. ;; No description needed, it is identical
  9730. (setq description nil))
  9731. (when (and (not description)
  9732. (not (equal link (org-link-escape link))))
  9733. (setq description link))
  9734. (concat "[[" (org-link-escape link) "]"
  9735. (if description (concat "[" description "]") "")
  9736. "]")))
  9737. (defconst org-link-escape-chars '(("[" . "%5B") ("]" . "%5D") (" " . "%20"))
  9738. "Association list of escapes for some characters problematic in links.")
  9739. (defun org-link-escape (text)
  9740. "Escape charaters in TEXT that are problematic for links."
  9741. (when text
  9742. (let ((re (mapconcat (lambda (x) (regexp-quote (car x)))
  9743. org-link-escape-chars "\\|")))
  9744. (while (string-match re text)
  9745. (setq text
  9746. (replace-match
  9747. (cdr (assoc (match-string 0 text) org-link-escape-chars))
  9748. t t text)))
  9749. text)))
  9750. (defun org-link-unescape (text)
  9751. "Reverse the action of `org-link-escape'."
  9752. (when text
  9753. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  9754. org-link-escape-chars "\\|")))
  9755. (while (string-match re text)
  9756. (setq text
  9757. (replace-match
  9758. (car (rassoc (match-string 0 text) org-link-escape-chars))
  9759. t t text)))
  9760. text)))
  9761. (defun org-xor (a b)
  9762. "Exclusive or."
  9763. (if a (not b) b))
  9764. (defun org-get-header (header)
  9765. "Find a header field in the current buffer."
  9766. (save-excursion
  9767. (goto-char (point-min))
  9768. (let ((case-fold-search t) s)
  9769. (cond
  9770. ((eq header 'from)
  9771. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  9772. (setq s (match-string 1)))
  9773. (while (string-match "\"" s)
  9774. (setq s (replace-match "" t t s)))
  9775. (if (string-match "[<(].*" s)
  9776. (setq s (replace-match "" t t s))))
  9777. ((eq header 'message-id)
  9778. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  9779. (setq s (match-string 1))))
  9780. ((eq header 'subject)
  9781. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  9782. (setq s (match-string 1)))))
  9783. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  9784. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  9785. s)))
  9786. (defun org-fixup-message-id-for-http (s)
  9787. "Replace special characters in a message id, so it can be used in an http query."
  9788. (while (string-match "<" s)
  9789. (setq s (replace-match "%3C" t t s)))
  9790. (while (string-match ">" s)
  9791. (setq s (replace-match "%3E" t t s)))
  9792. (while (string-match "@" s)
  9793. (setq s (replace-match "%40" t t s)))
  9794. s)
  9795. (defun org-insert-link (&optional complete-file)
  9796. "Insert a link. At the prompt, enter the link.
  9797. Completion can be used to select a link previously stored with
  9798. `org-store-link'. When the empty string is entered (i.e. if you just
  9799. press RET at the prompt), the link defaults to the most recently
  9800. stored link. As SPC triggers completion in the minibuffer, you need to
  9801. use M-SPC or C-q SPC to force the insertion of a space character.
  9802. You will also be prompted for a description, and if one is given, it will
  9803. be displayed in the buffer instead of the link.
  9804. If there is already a link at point, this command will allow you to edit link
  9805. and description parts.
  9806. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
  9807. selected using completion. The path to the file will be relative to
  9808. the current directory if the file is in the current directory or a
  9809. subdirectory. Otherwise, the link will be the absolute path as
  9810. completed in the minibuffer (i.e. normally ~/path/to/file).
  9811. With two \\[universal-argument] prefixes, enforce an absolute path even if the file
  9812. is in the current directory or below.
  9813. With three \\[universal-argument] prefixes, negate the meaning of
  9814. `org-keep-stored-link-after-insertion'."
  9815. (interactive "P")
  9816. (let (link desc entry remove file (pos (point)))
  9817. (cond
  9818. ((save-excursion
  9819. (skip-chars-forward "^]\n\r")
  9820. (and (re-search-backward "\\[\\[" nil t)
  9821. (looking-at org-bracket-link-regexp)
  9822. (<= (match-beginning 0) pos)
  9823. (>= (match-end 0) pos)))
  9824. ;; We do have a link at point, and we are going to edit it.
  9825. (setq remove (list (match-beginning 0) (match-end 0)))
  9826. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  9827. (setq link (read-string "Link: "
  9828. (org-link-unescape
  9829. (org-match-string-no-properties 1)))))
  9830. ((equal complete-file '(4))
  9831. ;; Completing read for file names.
  9832. (setq file (read-file-name "File: "))
  9833. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  9834. (pwd1 (file-name-as-directory (abbreviate-file-name
  9835. (expand-file-name ".")))))
  9836. (cond
  9837. ((equal complete-file '(16))
  9838. (setq link (org-make-link
  9839. "file:"
  9840. (abbreviate-file-name (expand-file-name file)))))
  9841. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9842. (setq link (org-make-link "file:" (match-string 1 file))))
  9843. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9844. (expand-file-name file))
  9845. (setq link (org-make-link
  9846. "file:" (match-string 1 (expand-file-name file)))))
  9847. (t (setq link (org-make-link "file:" file))))))
  9848. (t
  9849. ;; Read link, with completion for stored links.
  9850. (setq link (org-completing-read
  9851. "Link: " org-stored-links nil nil nil
  9852. org-insert-link-history
  9853. (or (car (car org-stored-links)))))
  9854. (setq entry (assoc link org-stored-links))
  9855. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  9856. (not org-keep-stored-link-after-insertion))
  9857. (setq org-stored-links (delq (assoc link org-stored-links)
  9858. org-stored-links)))
  9859. (setq link (if entry (nth 1 entry) link)
  9860. desc (or desc (nth 2 entry)))))
  9861. (if (string-match org-plain-link-re link)
  9862. ;; URL-like link, normalize the use of angular brackets.
  9863. (setq link (org-make-link (org-remove-angle-brackets link))))
  9864. ;; Check if we are linking to the current file with a search option
  9865. ;; If yes, simplify the link by using only the search option.
  9866. (when (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link)
  9867. (let* ((path (match-string 1 link))
  9868. (case-fold-search nil)
  9869. (search (match-string 2 link)))
  9870. (save-match-data
  9871. (if (equal (file-truename buffer-file-name) (file-truename path))
  9872. ;; We are linking to this same file, with a search option
  9873. (setq link search)))))
  9874. ;; Check if we can/should use a relative path. If yes, simplify the link
  9875. (when (string-match "\\<file:\\(.*\\)" link)
  9876. (let* ((path (match-string 1 link))
  9877. (case-fold-search nil))
  9878. (cond
  9879. ((eq org-link-file-path-type 'absolute)
  9880. (setq path (abbreviate-file-name (expand-file-name path))))
  9881. ((eq org-link-file-path-type 'noabbrev)
  9882. (setq path (expand-file-name path)))
  9883. ((eq org-link-file-path-type 'relative)
  9884. (setq path (file-relative-name path)))
  9885. (t
  9886. (save-match-data
  9887. (if (string-match (concat "^" (regexp-quote
  9888. (file-name-as-directory
  9889. (expand-file-name "."))))
  9890. (expand-file-name path))
  9891. ;; We are linking a file with relative path name.
  9892. (setq path (substring (expand-file-name path)
  9893. (match-end 0)))))))
  9894. (setq link (concat "file:" path))))
  9895. (setq desc (read-string "Description: " desc))
  9896. (unless (string-match "\\S-" desc) (setq desc nil))
  9897. (if remove (apply 'delete-region remove))
  9898. (insert (org-make-link-string link desc))))
  9899. (defun org-completing-read (&rest args)
  9900. (let ((minibuffer-local-completion-map
  9901. (copy-keymap minibuffer-local-completion-map)))
  9902. (define-key minibuffer-local-completion-map " " 'self-insert-command)
  9903. (apply 'completing-read args)))
  9904. ;;; Hooks for remember.el
  9905. (defvar org-finish-function nil)
  9906. ;;;###autoload
  9907. (defun org-remember-annotation ()
  9908. "Return a link to the current location as an annotation for remember.el.
  9909. If you are using Org-mode files as target for data storage with
  9910. remember.el, then the annotations should include a link compatible with the
  9911. conventions in Org-mode. This function returns such a link."
  9912. (org-store-link nil))
  9913. (defconst org-remember-help
  9914. "Select a destination location for the note.
  9915. UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
  9916. RET at beg-of-buf -> Append to file as level 2 headline
  9917. RET on headline -> Store as sublevel entry to current headline
  9918. <left>/<right> -> before/after current headline, same headings level")
  9919. ;;;###autoload
  9920. (defun org-remember-apply-template ()
  9921. "Initialize *remember* buffer with template, invoke `org-mode'.
  9922. This function should be placed into `remember-mode-hook' and in fact requires
  9923. to be run from that hook to fucntion properly."
  9924. (if org-remember-templates
  9925. (let* ((entry (if (= (length org-remember-templates) 1)
  9926. (cdar org-remember-templates)
  9927. (message "Select template: %s"
  9928. (mapconcat
  9929. (lambda (x) (char-to-string (car x)))
  9930. org-remember-templates " "))
  9931. (cdr (assoc (read-char-exclusive) org-remember-templates))))
  9932. (tpl (car entry))
  9933. (file (if (consp (cdr entry)) (nth 1 entry)))
  9934. (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
  9935. (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
  9936. (v-u (concat "[" (substring v-t 1 -1) "]"))
  9937. (v-U (concat "[" (substring v-T 1 -1) "]"))
  9938. (v-a annotation) ; defined in `remember-mode'
  9939. (v-i initial) ; defined in `remember-mode'
  9940. (v-n user-full-name)
  9941. )
  9942. (unless tpl (setq tpl "") (message "No template") (ding))
  9943. (insert tpl) (goto-char (point-min))
  9944. (while (re-search-forward "%\\([tTuTai]\\)" nil t)
  9945. (when (and initial (equal (match-string 0) "%i"))
  9946. (save-match-data
  9947. (let* ((lead (buffer-substring
  9948. (point-at-bol) (match-beginning 0))))
  9949. (setq v-i (mapconcat 'identity
  9950. (org-split-string initial "\n")
  9951. (concat "\n" lead))))))
  9952. (replace-match
  9953. (or (eval (intern (concat "v-" (match-string 1)))) "")
  9954. t t))
  9955. (let ((org-startup-folded nil)
  9956. (org-startup-with-deadline-check nil))
  9957. (org-mode))
  9958. (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
  9959. (org-set-local 'org-default-notes-file file))
  9960. (goto-char (point-min))
  9961. (if (re-search-forward "%\\?" nil t) (replace-match "")))
  9962. (let ((org-startup-folded nil)
  9963. (org-startup-with-deadline-check nil))
  9964. (org-mode)))
  9965. (org-set-local 'org-finish-function 'remember-buffer))
  9966. ;;;###autoload
  9967. (defun org-remember-handler ()
  9968. "Store stuff from remember.el into an org file.
  9969. First prompts for an org file. If the user just presses return, the value
  9970. of `org-default-notes-file' is used.
  9971. Then the command offers the headings tree of the selected file in order to
  9972. file the text at a specific location.
  9973. You can either immediately press RET to get the note appended to the
  9974. file, or you can use vertical cursor motion and visibility cycling (TAB) to
  9975. find a better place. Then press RET or <left> or <right> in insert the note.
  9976. Key Cursor position Note gets inserted
  9977. -----------------------------------------------------------------------------
  9978. RET buffer-start as level 2 heading at end of file
  9979. RET on headline as sublevel of the heading at cursor
  9980. RET no heading at cursor position, level taken from context.
  9981. Or use prefix arg to specify level manually.
  9982. <left> on headline as same level, before current heading
  9983. <right> on headline as same level, after current heading
  9984. So the fastest way to store the note is to press RET RET to append it to
  9985. the default file. This way your current train of thought is not
  9986. interrupted, in accordance with the principles of remember.el. But with
  9987. little extra effort, you can push it directly to the correct location.
  9988. Before being stored away, the function ensures that the text has a
  9989. headline, i.e. a first line that starts with a \"*\". If not, a headline
  9990. is constructed from the current date and some additional data.
  9991. If the variable `org-adapt-indentation' is non-nil, the entire text is
  9992. also indented so that it starts in the same column as the headline
  9993. \(i.e. after the stars).
  9994. See also the variable `org-reverse-note-order'."
  9995. (catch 'quit
  9996. (let* ((txt (buffer-substring (point-min) (point-max)))
  9997. (fastp current-prefix-arg)
  9998. (file (if fastp org-default-notes-file (org-get-org-file)))
  9999. (visiting (find-buffer-visiting file))
  10000. (org-startup-with-deadline-check nil)
  10001. (org-startup-folded nil)
  10002. (org-startup-align-all-tables nil)
  10003. spos level indent reversed)
  10004. ;; Modify text so that it becomes a nice subtree which can be inserted
  10005. ;; into an org tree.
  10006. (let* ((lines (split-string txt "\n"))
  10007. first)
  10008. ;; remove empty lines at the beginning
  10009. (while (and lines (string-match "^[ \t]*\n" (car lines)))
  10010. (setq lines (cdr lines)))
  10011. (setq first (car lines) lines (cdr lines))
  10012. (if (string-match "^\\*+" first)
  10013. ;; Is already a headline
  10014. (setq indent nil)
  10015. ;; We need to add a headline: Use time and first buffer line
  10016. (setq lines (cons first lines)
  10017. first (concat "* " (current-time-string)
  10018. " (" (remember-buffer-desc) ")")
  10019. indent " "))
  10020. (if (and org-adapt-indentation indent)
  10021. (setq lines (mapcar (lambda (x) (concat indent x)) lines)))
  10022. (setq txt (concat first "\n"
  10023. (mapconcat 'identity lines "\n"))))
  10024. ;; Find the file
  10025. (if (not visiting)
  10026. (find-file-noselect file))
  10027. (with-current-buffer (get-file-buffer file)
  10028. (save-excursion (and (goto-char (point-min))
  10029. (not (re-search-forward "^\\* " nil t))
  10030. (insert "\n* Notes\n")))
  10031. (setq reversed (org-notes-order-reversed-p))
  10032. (save-excursion
  10033. (save-restriction
  10034. (widen)
  10035. ;; Ask the User for a location
  10036. (setq spos (if fastp 1 (org-get-location
  10037. (current-buffer)
  10038. org-remember-help)))
  10039. (if (not spos) (throw 'quit nil)) ; return nil to show we did
  10040. ; not handle this note
  10041. (goto-char spos)
  10042. (cond ((bobp)
  10043. ;; Put it at the start or end, as level 2
  10044. (save-restriction
  10045. (widen)
  10046. (goto-char (if reversed (point-min) (point-max)))
  10047. (if (not (bolp)) (newline))
  10048. (org-paste-subtree 2 txt)))
  10049. ((and (org-on-heading-p nil) (not current-prefix-arg))
  10050. ;; Put it below this entry, at the beg/end of the subtree
  10051. (org-back-to-heading)
  10052. (setq level (funcall outline-level))
  10053. (if reversed
  10054. (outline-end-of-heading)
  10055. (outline-end-of-subtree))
  10056. (if (not (bolp)) (newline))
  10057. (beginning-of-line 1)
  10058. (org-paste-subtree (1+ level) txt))
  10059. (t
  10060. ;; Put it right there, with automatic level determined by
  10061. ;; org-paste-subtree or from prefix arg
  10062. (org-paste-subtree current-prefix-arg txt)))
  10063. (when remember-save-after-remembering
  10064. (save-buffer)
  10065. (if (not visiting) (kill-buffer (current-buffer)))))))))
  10066. t) ;; return t to indicate that we took care of this note.
  10067. (defun org-get-org-file ()
  10068. "Read a filename, with default directory `org-directory'."
  10069. (let ((default (or org-default-notes-file remember-data-file)))
  10070. (read-file-name (format "File name [%s]: " default)
  10071. (file-name-as-directory org-directory)
  10072. default)))
  10073. (defun org-notes-order-reversed-p ()
  10074. "Check if the current file should receive notes in reversed order."
  10075. (cond
  10076. ((not org-reverse-note-order) nil)
  10077. ((eq t org-reverse-note-order) t)
  10078. ((not (listp org-reverse-note-order)) nil)
  10079. (t (catch 'exit
  10080. (let ((all org-reverse-note-order)
  10081. entry)
  10082. (while (setq entry (pop all))
  10083. (if (string-match (car entry) buffer-file-name)
  10084. (throw 'exit (cdr entry))))
  10085. nil)))))
  10086. ;;; Tables
  10087. ;; Watch out: Here we are talking about two different kind of tables.
  10088. ;; Most of the code is for the tables created with the Org-mode table editor.
  10089. ;; Sometimes, we talk about tables created and edited with the table.el
  10090. ;; Emacs package. We call the former org-type tables, and the latter
  10091. ;; table.el-type tables.
  10092. (defun org-before-change-function (beg end)
  10093. "Every change indicates that a table might need an update."
  10094. (setq org-table-may-need-update t))
  10095. (defconst org-table-line-regexp "^[ \t]*|"
  10096. "Detects an org-type table line.")
  10097. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  10098. "Detects an org-type table line.")
  10099. (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
  10100. "Detects a table line marked for automatic recalculation.")
  10101. (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
  10102. "Detects a table line marked for automatic recalculation.")
  10103. (defconst org-table-calculate-mark-regexp "^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
  10104. "Detects a table line marked for automatic recalculation.")
  10105. (defconst org-table-hline-regexp "^[ \t]*|-"
  10106. "Detects an org-type table hline.")
  10107. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  10108. "Detects a table-type table hline.")
  10109. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  10110. "Detects an org-type or table-type table.")
  10111. (defconst org-table-border-regexp "^[ \t]*[^| \t]"
  10112. "Searching from within a table (any type) this finds the first line
  10113. outside the table.")
  10114. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  10115. "Searching from within a table (any type) this finds the first line
  10116. outside the table.")
  10117. (defun org-table-create-with-table.el ()
  10118. "Use the table.el package to insert a new table.
  10119. If there is already a table at point, convert between Org-mode tables
  10120. and table.el tables."
  10121. (interactive)
  10122. (require 'table)
  10123. (cond
  10124. ((org-at-table.el-p)
  10125. (if (y-or-n-p "Convert table to Org-mode table? ")
  10126. (org-table-convert)))
  10127. ((org-at-table-p)
  10128. (if (y-or-n-p "Convert table to table.el table? ")
  10129. (org-table-convert)))
  10130. (t (call-interactively 'table-insert))))
  10131. (defun org-table-create-or-convert-from-region (arg)
  10132. "Convert region to table, or create an empty table.
  10133. If there is an active region, convert it to a table. If there is no such
  10134. region, create an empty table."
  10135. (interactive "P")
  10136. (if (org-region-active-p)
  10137. (org-table-convert-region (region-beginning) (region-end) arg)
  10138. (org-table-create arg)))
  10139. (defun org-table-create (&optional size)
  10140. "Query for a size and insert a table skeleton.
  10141. SIZE is a string Columns x Rows like for example \"3x2\"."
  10142. (interactive "P")
  10143. (unless size
  10144. (setq size (read-string
  10145. (concat "Table size Columns x Rows [e.g. "
  10146. org-table-default-size "]: ")
  10147. "" nil org-table-default-size)))
  10148. (let* ((pos (point))
  10149. (indent (make-string (current-column) ?\ ))
  10150. (split (org-split-string size " *x *"))
  10151. (rows (string-to-number (nth 1 split)))
  10152. (columns (string-to-number (car split)))
  10153. (line (concat (apply 'concat indent "|" (make-list columns " |"))
  10154. "\n")))
  10155. (if (string-match "^[ \t]*$" (buffer-substring-no-properties
  10156. (point-at-bol) (point)))
  10157. (beginning-of-line 1)
  10158. (newline))
  10159. ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
  10160. (dotimes (i rows) (insert line))
  10161. (goto-char pos)
  10162. (if (> rows 1)
  10163. ;; Insert a hline after the first row.
  10164. (progn
  10165. (end-of-line 1)
  10166. (insert "\n|-")
  10167. (goto-char pos)))
  10168. (org-table-align)))
  10169. (defun org-table-convert-region (beg0 end0 &optional nspace)
  10170. "Convert region to a table.
  10171. The region goes from BEG0 to END0, but these borders will be moved
  10172. slightly, to make sure a beginning of line in the first line is included.
  10173. When NSPACE is non-nil, it indicates the minimum number of spaces that
  10174. separate columns (default: just one space)."
  10175. (interactive "rP")
  10176. (let* ((beg (min beg0 end0))
  10177. (end (max beg0 end0))
  10178. (tabsep t)
  10179. re)
  10180. (goto-char beg)
  10181. (beginning-of-line 1)
  10182. (setq beg (move-marker (make-marker) (point)))
  10183. (goto-char end)
  10184. (if (bolp) (backward-char 1) (end-of-line 1))
  10185. (setq end (move-marker (make-marker) (point)))
  10186. ;; Lets see if this is tab-separated material. If every nonempty line
  10187. ;; contains a tab, we will assume that it is tab-separated material
  10188. (if nspace
  10189. (setq tabsep nil)
  10190. (goto-char beg)
  10191. (and (re-search-forward "^[^\n\t]+$" end t) (setq tabsep nil)))
  10192. (if nspace (setq tabsep nil))
  10193. (if tabsep
  10194. (setq re "^\\|\t")
  10195. (setq re (format "^ *\\| *\t *\\| \\{%d,\\}"
  10196. (max 1 (prefix-numeric-value nspace)))))
  10197. (goto-char beg)
  10198. (while (re-search-forward re end t)
  10199. (replace-match "|" t t))
  10200. (goto-char beg)
  10201. (insert " ")
  10202. (org-table-align)))
  10203. (defun org-table-import (file arg)
  10204. "Import FILE as a table.
  10205. The file is assumed to be tab-separated. Such files can be produced by most
  10206. spreadsheet and database applications. If no tabs (at least one per line)
  10207. are found, lines will be split on whitespace into fields."
  10208. (interactive "f\nP")
  10209. (or (bolp) (newline))
  10210. (let ((beg (point))
  10211. (pm (point-max)))
  10212. (insert-file-contents file)
  10213. (org-table-convert-region beg (+ (point) (- (point-max) pm)) arg)))
  10214. (defun org-table-export ()
  10215. "Export table as a tab-separated file.
  10216. Such a file can be imported into a spreadsheet program like Excel."
  10217. (interactive)
  10218. (let* ((beg (org-table-begin))
  10219. (end (org-table-end))
  10220. (table (buffer-substring beg end))
  10221. (file (read-file-name "Export table to: "))
  10222. buf)
  10223. (unless (or (not (file-exists-p file))
  10224. (y-or-n-p (format "Overwrite file %s? " file)))
  10225. (error "Abort"))
  10226. (with-current-buffer (find-file-noselect file)
  10227. (setq buf (current-buffer))
  10228. (erase-buffer)
  10229. (fundamental-mode)
  10230. (insert table)
  10231. (goto-char (point-min))
  10232. (while (re-search-forward "^[ \t]*|[ \t]*" nil t)
  10233. (replace-match "" t t)
  10234. (end-of-line 1))
  10235. (goto-char (point-min))
  10236. (while (re-search-forward "[ \t]*|[ \t]*$" nil t)
  10237. (replace-match "" t t)
  10238. (goto-char (min (1+ (point)) (point-max))))
  10239. (goto-char (point-min))
  10240. (while (re-search-forward "^-[-+]*$" nil t)
  10241. (replace-match "")
  10242. (if (looking-at "\n")
  10243. (delete-char 1)))
  10244. (goto-char (point-min))
  10245. (while (re-search-forward "[ \t]*|[ \t]*" nil t)
  10246. (replace-match "\t" t t))
  10247. (save-buffer))
  10248. (kill-buffer buf)))
  10249. (defvar org-table-aligned-begin-marker (make-marker)
  10250. "Marker at the beginning of the table last aligned.
  10251. Used to check if cursor still is in that table, to minimize realignment.")
  10252. (defvar org-table-aligned-end-marker (make-marker)
  10253. "Marker at the end of the table last aligned.
  10254. Used to check if cursor still is in that table, to minimize realignment.")
  10255. (defvar org-table-last-alignment nil
  10256. "List of flags for flushright alignment, from the last re-alignment.
  10257. This is being used to correctly align a single field after TAB or RET.")
  10258. (defvar org-table-last-column-widths nil
  10259. "List of max width of fields in each column.
  10260. This is being used to correctly align a single field after TAB or RET.")
  10261. (defvar org-last-recalc-line nil)
  10262. (defconst org-narrow-column-arrow "=>"
  10263. "Used as display property in narrowed table columns.")
  10264. (defun org-table-align ()
  10265. "Align the table at point by aligning all vertical bars."
  10266. (interactive)
  10267. (let* (
  10268. ;; Limits of table
  10269. (beg (org-table-begin))
  10270. (end (org-table-end))
  10271. ;; Current cursor position
  10272. (linepos (org-current-line))
  10273. (colpos (org-table-current-column))
  10274. (winstart (window-start))
  10275. (winstartline (org-current-line (min winstart (1- (point-max)))))
  10276. lines (new "") lengths l typenums ty fields maxfields i
  10277. column
  10278. (indent "") cnt frac
  10279. rfmt hfmt
  10280. (spaces '(1 . 1))
  10281. (sp1 (car spaces))
  10282. (sp2 (cdr spaces))
  10283. (rfmt1 (concat
  10284. (make-string sp2 ?\ ) "%%%s%ds" (make-string sp1 ?\ ) "|"))
  10285. (hfmt1 (concat
  10286. (make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
  10287. emptystrings links narrow fmax f1 len c e)
  10288. (untabify beg end)
  10289. (remove-text-properties beg end '(org-cwidth t display t))
  10290. ;; Check if we have links
  10291. (goto-char beg)
  10292. (setq links (re-search-forward org-bracket-link-regexp end t))
  10293. ;; Make sure the link properties are right
  10294. (when links (goto-char beg) (while (org-activate-bracket-links end)))
  10295. ;; Check if we are narrowing any columns
  10296. (goto-char beg)
  10297. (setq narrow (and org-format-transports-properties-p
  10298. (re-search-forward "<[0-9]+>" end t)))
  10299. ;; Get the rows
  10300. (setq lines (org-split-string
  10301. (buffer-substring beg end) "\n"))
  10302. ;; Store the indentation of the first line
  10303. (if (string-match "^ *" (car lines))
  10304. (setq indent (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  10305. ;; Mark the hlines by setting the corresponding element to nil
  10306. ;; At the same time, we remove trailing space.
  10307. (setq lines (mapcar (lambda (l)
  10308. (if (string-match "^ *|-" l)
  10309. nil
  10310. (if (string-match "[ \t]+$" l)
  10311. (substring l 0 (match-beginning 0))
  10312. l)))
  10313. lines))
  10314. ;; Get the data fields by splitting the lines.
  10315. (setq fields (mapcar
  10316. (lambda (l)
  10317. (org-split-string l " *| *"))
  10318. (delq nil (copy-sequence lines))))
  10319. ;; How many fields in the longest line?
  10320. (condition-case nil
  10321. (setq maxfields (apply 'max (mapcar 'length fields)))
  10322. (error
  10323. (kill-region beg end)
  10324. (org-table-create org-table-default-size)
  10325. (error "Empty table - created default table")))
  10326. ;; A list of empty strings to fill any short rows on output
  10327. (setq emptystrings (make-list maxfields ""))
  10328. ;; Check for special formatting.
  10329. (setq i -1)
  10330. (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns
  10331. (setq column (mapcar (lambda (x) (or (nth i x) "")) fields))
  10332. ;; Check if there is an explicit width specified
  10333. (when (and org-table-limit-column-width narrow)
  10334. (setq c column fmax nil)
  10335. (while c
  10336. (setq e (pop c))
  10337. (if (and (stringp e) (string-match "^<\\([0-9]+\\)>$" e))
  10338. (setq fmax (string-to-number (match-string 1 e)) c nil)))
  10339. ;; Find fields that are wider than fmax, and shorten them
  10340. (when fmax
  10341. (loop for xx in column do
  10342. (when (and (stringp xx)
  10343. (> (org-string-width xx) fmax))
  10344. (org-add-props xx nil
  10345. 'help-echo
  10346. (concat "Clipped table field, use C-c ` to edit. Full value is:\n" (org-no-properties (copy-sequence xx))))
  10347. (setq f1 (min fmax (or (string-match org-bracket-link-regexp xx) fmax)))
  10348. (unless (> f1 1)
  10349. (error "Cannot narrow field starting with wide link \"%s\""
  10350. (match-string 0 xx)))
  10351. (add-text-properties f1 (length xx) (list 'org-cwidth t) xx)
  10352. (add-text-properties (- f1 2) f1
  10353. (list 'display org-narrow-column-arrow)
  10354. xx)))))
  10355. ;; Get the maximum width for each column
  10356. (push (apply 'max 1 (mapcar 'org-string-width column)) lengths)
  10357. ;; Get the fraction of numbers, to decide about alignment of the column
  10358. (setq cnt 0 frac 0.0)
  10359. (loop for x in column do
  10360. (if (equal x "")
  10361. nil
  10362. (setq frac ( / (+ (* frac cnt)
  10363. (if (string-match org-table-number-regexp x) 1 0))
  10364. (setq cnt (1+ cnt))))))
  10365. (push (>= frac org-table-number-fraction) typenums))
  10366. (setq lengths (nreverse lengths) typenums (nreverse typenums))
  10367. ;; Store the alignment of this table, for later editing of single fields
  10368. (setq org-table-last-alignment typenums
  10369. org-table-last-column-widths lengths)
  10370. ;; With invisible characters, `format' does not get the field width right
  10371. ;; So we need to make these fields wide by hand.
  10372. (when links
  10373. (loop for i from 0 upto (1- maxfields) do
  10374. (setq len (nth i lengths))
  10375. (loop for j from 0 upto (1- (length fields)) do
  10376. (setq c (nthcdr i (car (nthcdr j fields))))
  10377. (if (and (stringp (car c))
  10378. (string-match org-bracket-link-regexp (car c))
  10379. (< (org-string-width (car c)) len))
  10380. (setcar c (concat (car c) (make-string (- len (org-string-width (car c))) ?\ )))))))
  10381. ;; Compute the formats needed for output of the table
  10382. (setq rfmt (concat indent "|") hfmt (concat indent "|"))
  10383. (while (setq l (pop lengths))
  10384. (setq ty (if (pop typenums) "" "-")) ; number types flushright
  10385. (setq rfmt (concat rfmt (format rfmt1 ty l))
  10386. hfmt (concat hfmt (format hfmt1 (make-string l ?-)))))
  10387. (setq rfmt (concat rfmt "\n")
  10388. hfmt (concat (substring hfmt 0 -1) "|\n"))
  10389. (setq new (mapconcat
  10390. (lambda (l)
  10391. (if l (apply 'format rfmt
  10392. (append (pop fields) emptystrings))
  10393. hfmt))
  10394. lines ""))
  10395. ;; Replace the old one
  10396. (delete-region beg end)
  10397. (move-marker end nil)
  10398. (move-marker org-table-aligned-begin-marker (point))
  10399. (insert new)
  10400. (move-marker org-table-aligned-end-marker (point))
  10401. (when (and orgtbl-mode (not (org-mode-p)))
  10402. (goto-char org-table-aligned-begin-marker)
  10403. (while (org-hide-wide-columns org-table-aligned-end-marker)))
  10404. ;; Try to move to the old location
  10405. (goto-line winstartline)
  10406. (setq winstart (point-at-bol))
  10407. (goto-line linepos)
  10408. (set-window-start (selected-window) winstart 'noforce)
  10409. (org-table-goto-column colpos)
  10410. (setq org-table-may-need-update nil)
  10411. ))
  10412. (defun org-string-width (s)
  10413. "Compute width of string, ignoring invisible characters.
  10414. This ignores character with invisibility property `org-link', and also
  10415. characters with property `org-cwidth', because these will become invisible
  10416. upon the next fontification round."
  10417. (let (b)
  10418. (when (or (eq t buffer-invisibility-spec)
  10419. (assq 'org-link buffer-invisibility-spec))
  10420. (while (setq b (text-property-any 0 (length s)
  10421. 'invisible 'org-link s))
  10422. (setq s (concat (substring s 0 b)
  10423. (substring s (or (next-single-property-change
  10424. b 'invisible s) (length s)))))))
  10425. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  10426. (setq s (concat (substring s 0 b)
  10427. (substring s (or (next-single-property-change
  10428. b 'org-cwidth s) (length s))))))
  10429. (string-width s)))
  10430. (defun org-table-begin (&optional table-type)
  10431. "Find the beginning of the table and return its position.
  10432. With argument TABLE-TYPE, go to the beginning of a table.el-type table."
  10433. (save-excursion
  10434. (if (not (re-search-backward
  10435. (if table-type org-table-any-border-regexp
  10436. org-table-border-regexp)
  10437. nil t))
  10438. (progn (goto-char (point-min)) (point))
  10439. (goto-char (match-beginning 0))
  10440. (beginning-of-line 2)
  10441. (point))))
  10442. (defun org-table-end (&optional table-type)
  10443. "Find the end of the table and return its position.
  10444. With argument TABLE-TYPE, go to the end of a table.el-type table."
  10445. (save-excursion
  10446. (if (not (re-search-forward
  10447. (if table-type org-table-any-border-regexp
  10448. org-table-border-regexp)
  10449. nil t))
  10450. (goto-char (point-max))
  10451. (goto-char (match-beginning 0)))
  10452. (point-marker)))
  10453. (defun org-table-justify-field-maybe (&optional new)
  10454. "Justify the current field, text to left, number to right.
  10455. Optional argument NEW may specify text to replace the current field content."
  10456. (cond
  10457. ((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
  10458. ((org-at-table-hline-p))
  10459. ((and (not new)
  10460. (or (not (equal (marker-buffer org-table-aligned-begin-marker)
  10461. (current-buffer)))
  10462. (< (point) org-table-aligned-begin-marker)
  10463. (>= (point) org-table-aligned-end-marker)))
  10464. ;; This is not the same table, force a full re-align
  10465. (setq org-table-may-need-update t))
  10466. (t ;; realign the current field, based on previous full realign
  10467. (let* ((pos (point)) s
  10468. (col (org-table-current-column))
  10469. (num (if (> col 0) (nth (1- col) org-table-last-alignment)))
  10470. l f n o e)
  10471. (when (> col 0)
  10472. (skip-chars-backward "^|\n")
  10473. (if (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)")
  10474. (progn
  10475. (setq s (match-string 1)
  10476. o (match-string 0)
  10477. l (max 1 (- (match-end 0) (match-beginning 0) 3))
  10478. e (not (= (match-beginning 2) (match-end 2))))
  10479. (setq f (format (if num " %%%ds %s" " %%-%ds %s")
  10480. l (if e "|" (setq org-table-may-need-update t) ""))
  10481. n (format f s))
  10482. (if new
  10483. (if (<= (length new) l) ;; FIXME: length -> str-width?
  10484. (setq n (format f new))
  10485. (setq n (concat new "|") org-table-may-need-update t)))
  10486. (or (equal n o)
  10487. (let (org-table-may-need-update)
  10488. (replace-match n))))
  10489. (setq org-table-may-need-update t))
  10490. (goto-char pos))))))
  10491. (defun org-table-next-field ()
  10492. "Go to the next field in the current table, creating new lines as needed.
  10493. Before doing so, re-align the table if necessary."
  10494. (interactive)
  10495. (org-table-maybe-eval-formula)
  10496. (org-table-maybe-recalculate-line)
  10497. (if (and org-table-automatic-realign
  10498. org-table-may-need-update)
  10499. (org-table-align))
  10500. (let ((end (org-table-end)))
  10501. (if (org-at-table-hline-p)
  10502. (end-of-line 1))
  10503. (condition-case nil
  10504. (progn
  10505. (re-search-forward "|" end)
  10506. (if (looking-at "[ \t]*$")
  10507. (re-search-forward "|" end))
  10508. (if (and (looking-at "-")
  10509. org-table-tab-jumps-over-hlines
  10510. (re-search-forward "^[ \t]*|\\([^-]\\)" end t))
  10511. (goto-char (match-beginning 1)))
  10512. (if (looking-at "-")
  10513. (progn
  10514. (beginning-of-line 0)
  10515. (org-table-insert-row 'below))
  10516. (if (looking-at " ") (forward-char 1))))
  10517. (error
  10518. (org-table-insert-row 'below)))))
  10519. (defun org-table-previous-field ()
  10520. "Go to the previous field in the table.
  10521. Before doing so, re-align the table if necessary."
  10522. (interactive)
  10523. (org-table-justify-field-maybe)
  10524. (org-table-maybe-recalculate-line)
  10525. (if (and org-table-automatic-realign
  10526. org-table-may-need-update)
  10527. (org-table-align))
  10528. (if (org-at-table-hline-p)
  10529. (end-of-line 1))
  10530. (re-search-backward "|" (org-table-begin))
  10531. (re-search-backward "|" (org-table-begin))
  10532. (while (looking-at "|\\(-\\|[ \t]*$\\)")
  10533. (re-search-backward "|" (org-table-begin)))
  10534. (if (looking-at "| ?")
  10535. (goto-char (match-end 0))))
  10536. (defun org-table-next-row ()
  10537. "Go to the next row (same column) in the current table.
  10538. Before doing so, re-align the table if necessary."
  10539. (interactive)
  10540. (org-table-maybe-eval-formula)
  10541. (org-table-maybe-recalculate-line)
  10542. (if (or (looking-at "[ \t]*$")
  10543. (save-excursion (skip-chars-backward " \t") (bolp)))
  10544. (newline)
  10545. (if (and org-table-automatic-realign
  10546. org-table-may-need-update)
  10547. (org-table-align))
  10548. (let ((col (org-table-current-column)))
  10549. (beginning-of-line 2)
  10550. (if (or (not (org-at-table-p))
  10551. (org-at-table-hline-p))
  10552. (progn
  10553. (beginning-of-line 0)
  10554. (org-table-insert-row 'below)))
  10555. (org-table-goto-column col)
  10556. (skip-chars-backward "^|\n\r")
  10557. (if (looking-at " ") (forward-char 1)))))
  10558. (defun org-table-copy-down (n)
  10559. "Copy a field down in the current column.
  10560. If the field at the cursor is empty, copy into it the content of the nearest
  10561. non-empty field above. With argument N, use the Nth non-empty field.
  10562. If the current field is not empty, it is copied down to the next row, and
  10563. the cursor is moved with it. Therefore, repeating this command causes the
  10564. column to be filled row-by-row.
  10565. If the variable `org-table-copy-increment' is non-nil and the field is an
  10566. integer, it will be incremented while copying."
  10567. (interactive "p")
  10568. (let* ((colpos (org-table-current-column))
  10569. (field (org-table-get-field))
  10570. (non-empty (string-match "[^ \t]" field))
  10571. (beg (org-table-begin))
  10572. txt)
  10573. (org-table-check-inside-data-field)
  10574. (if non-empty
  10575. (progn
  10576. (setq txt (org-trim field))
  10577. (org-table-next-row)
  10578. (org-table-blank-field))
  10579. (save-excursion
  10580. (setq txt
  10581. (catch 'exit
  10582. (while (progn (beginning-of-line 1)
  10583. (re-search-backward org-table-dataline-regexp
  10584. beg t))
  10585. (org-table-goto-column colpos t)
  10586. (if (and (looking-at
  10587. "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
  10588. (= (setq n (1- n)) 0))
  10589. (throw 'exit (match-string 1))))))))
  10590. (if txt
  10591. (progn
  10592. (if (and org-table-copy-increment
  10593. (string-match "^[0-9]+$" txt))
  10594. (setq txt (format "%d" (+ (string-to-number txt) 1))))
  10595. (insert txt)
  10596. (org-table-maybe-recalculate-line)
  10597. (org-table-align))
  10598. (error "No non-empty field found"))))
  10599. (defun org-table-check-inside-data-field ()
  10600. "Is point inside a table data field?
  10601. I.e. not on a hline or before the first or after the last column?
  10602. This actually throws an error, so it aborts the current command."
  10603. (if (or (not (org-at-table-p))
  10604. (= (org-table-current-column) 0)
  10605. (org-at-table-hline-p)
  10606. (looking-at "[ \t]*$"))
  10607. (error "Not in table data field")))
  10608. (defvar org-table-clip nil
  10609. "Clipboard for table regions.")
  10610. (defun org-table-blank-field ()
  10611. "Blank the current table field or active region."
  10612. (interactive)
  10613. (org-table-check-inside-data-field)
  10614. (if (and (interactive-p) (org-region-active-p))
  10615. (let (org-table-clip)
  10616. (org-table-cut-region (region-beginning) (region-end)))
  10617. (skip-chars-backward "^|")
  10618. (backward-char 1)
  10619. (if (looking-at "|[^|\n]+")
  10620. (let* ((pos (match-beginning 0))
  10621. (match (match-string 0))
  10622. (len (org-string-width match)))
  10623. (replace-match (concat "|" (make-string (1- len) ?\ )))
  10624. (goto-char (+ 2 pos))
  10625. (substring match 1)))))
  10626. (defun org-table-get-field (&optional n replace)
  10627. "Return the value of the field in column N of current row.
  10628. N defaults to current field.
  10629. If REPLACE is a string, replace field with this value. The return value
  10630. is always the old value."
  10631. (and n (org-table-goto-column n))
  10632. (skip-chars-backward "^|\n")
  10633. (backward-char 1)
  10634. (if (looking-at "|[^|\r\n]*")
  10635. (let* ((pos (match-beginning 0))
  10636. (val (buffer-substring (1+ pos) (match-end 0))))
  10637. (if replace
  10638. (replace-match (concat "|" replace)))
  10639. (goto-char (min (point-at-eol) (+ 2 pos)))
  10640. val)
  10641. (forward-char 1) ""))
  10642. (defun org-table-current-column ()
  10643. "Find out which column we are in.
  10644. When called interactively, column is also displayed in echo area."
  10645. (interactive)
  10646. (if (interactive-p) (org-table-check-inside-data-field))
  10647. (save-excursion
  10648. (let ((cnt 0) (pos (point)))
  10649. (beginning-of-line 1)
  10650. (while (search-forward "|" pos t)
  10651. (setq cnt (1+ cnt)))
  10652. (if (interactive-p) (message "This is table column %d" cnt))
  10653. cnt)))
  10654. (defun org-table-goto-column (n &optional on-delim force)
  10655. "Move the cursor to the Nth column in the current table line.
  10656. With optional argument ON-DELIM, stop with point before the left delimiter
  10657. of the field.
  10658. If there are less than N fields, just go to after the last delimiter.
  10659. However, when FORCE is non-nil, create new columns if necessary."
  10660. (interactive "p")
  10661. (let ((pos (point-at-eol)))
  10662. (beginning-of-line 1)
  10663. (when (> n 0)
  10664. (while (and (> (setq n (1- n)) -1)
  10665. (or (search-forward "|" pos t)
  10666. (and force
  10667. (progn (end-of-line 1)
  10668. (skip-chars-backward "^|")
  10669. (insert " | "))))))
  10670. ; (backward-char 2) t)))))
  10671. (when (and force (not (looking-at ".*|")))
  10672. (save-excursion (end-of-line 1) (insert " | ")))
  10673. (if on-delim
  10674. (backward-char 1)
  10675. (if (looking-at " ") (forward-char 1))))))
  10676. (defun org-at-table-p (&optional table-type)
  10677. "Return t if the cursor is inside an org-type table.
  10678. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  10679. (if org-enable-table-editor
  10680. (save-excursion
  10681. (beginning-of-line 1)
  10682. (looking-at (if table-type org-table-any-line-regexp
  10683. org-table-line-regexp)))
  10684. nil))
  10685. (defun org-at-table.el-p ()
  10686. "Return t if and only if we are at a table.el table."
  10687. (and (org-at-table-p 'any)
  10688. (save-excursion
  10689. (goto-char (org-table-begin 'any))
  10690. (looking-at org-table1-hline-regexp))))
  10691. (defun org-table-recognize-table.el ()
  10692. "If there is a table.el table nearby, recognize it and move into it."
  10693. (if org-table-tab-recognizes-table.el
  10694. (if (org-at-table.el-p)
  10695. (progn
  10696. (beginning-of-line 1)
  10697. (if (looking-at org-table-dataline-regexp)
  10698. nil
  10699. (if (looking-at org-table1-hline-regexp)
  10700. (progn
  10701. (beginning-of-line 2)
  10702. (if (looking-at org-table-any-border-regexp)
  10703. (beginning-of-line -1)))))
  10704. (if (re-search-forward "|" (org-table-end t) t)
  10705. (progn
  10706. (require 'table)
  10707. (if (table--at-cell-p (point))
  10708. t
  10709. (message "recognizing table.el table...")
  10710. (table-recognize-table)
  10711. (message "recognizing table.el table...done")))
  10712. (error "This should not happen..."))
  10713. t)
  10714. nil)
  10715. nil))
  10716. (defun org-at-table-hline-p ()
  10717. "Return t if the cursor is inside a hline in a table."
  10718. (if org-enable-table-editor
  10719. (save-excursion
  10720. (beginning-of-line 1)
  10721. (looking-at org-table-hline-regexp))
  10722. nil))
  10723. (defun org-table-insert-column ()
  10724. "Insert a new column into the table."
  10725. (interactive)
  10726. (if (not (org-at-table-p))
  10727. (error "Not at a table"))
  10728. (org-table-find-dataline)
  10729. (let* ((col (max 1 (org-table-current-column)))
  10730. (beg (org-table-begin))
  10731. (end (org-table-end))
  10732. ;; Current cursor position
  10733. (linepos (org-current-line))
  10734. (colpos col))
  10735. (goto-char beg)
  10736. (while (< (point) end)
  10737. (if (org-at-table-hline-p)
  10738. nil
  10739. (org-table-goto-column col t)
  10740. (insert "| "))
  10741. (beginning-of-line 2))
  10742. (move-marker end nil)
  10743. (goto-line linepos)
  10744. (org-table-goto-column colpos)
  10745. (org-table-align)
  10746. (org-table-modify-formulas 'insert col)))
  10747. (defun org-table-find-dataline ()
  10748. "Find a dataline in the current table, which is needed for column commands."
  10749. (if (and (org-at-table-p)
  10750. (not (org-at-table-hline-p)))
  10751. t
  10752. (let ((col (current-column))
  10753. (end (org-table-end)))
  10754. (move-to-column col)
  10755. (while (and (< (point) end)
  10756. (or (not (= (current-column) col))
  10757. (org-at-table-hline-p)))
  10758. (beginning-of-line 2)
  10759. (move-to-column col))
  10760. (if (and (org-at-table-p)
  10761. (not (org-at-table-hline-p)))
  10762. t
  10763. (error
  10764. "Please position cursor in a data line for column operations")))))
  10765. (defun org-table-delete-column ()
  10766. "Delete a column from the table."
  10767. (interactive)
  10768. (if (not (org-at-table-p))
  10769. (error "Not at a table"))
  10770. (org-table-find-dataline)
  10771. (org-table-check-inside-data-field)
  10772. (let* ((col (org-table-current-column))
  10773. (beg (org-table-begin))
  10774. (end (org-table-end))
  10775. ;; Current cursor position
  10776. (linepos (org-current-line))
  10777. (colpos col))
  10778. (goto-char beg)
  10779. (while (< (point) end)
  10780. (if (org-at-table-hline-p)
  10781. nil
  10782. (org-table-goto-column col t)
  10783. (and (looking-at "|[^|\n]+|")
  10784. (replace-match "|")))
  10785. (beginning-of-line 2))
  10786. (move-marker end nil)
  10787. (goto-line linepos)
  10788. (org-table-goto-column colpos)
  10789. (org-table-align)
  10790. (org-table-modify-formulas 'remove col)))
  10791. (defun org-table-move-column-right ()
  10792. "Move column to the right."
  10793. (interactive)
  10794. (org-table-move-column nil))
  10795. (defun org-table-move-column-left ()
  10796. "Move column to the left."
  10797. (interactive)
  10798. (org-table-move-column 'left))
  10799. (defun org-table-move-column (&optional left)
  10800. "Move the current column to the right. With arg LEFT, move to the left."
  10801. (interactive "P")
  10802. (if (not (org-at-table-p))
  10803. (error "Not at a table"))
  10804. (org-table-find-dataline)
  10805. (org-table-check-inside-data-field)
  10806. (let* ((col (org-table-current-column))
  10807. (col1 (if left (1- col) col))
  10808. (beg (org-table-begin))
  10809. (end (org-table-end))
  10810. ;; Current cursor position
  10811. (linepos (org-current-line))
  10812. (colpos (if left (1- col) (1+ col))))
  10813. (if (and left (= col 1))
  10814. (error "Cannot move column further left"))
  10815. (if (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
  10816. (error "Cannot move column further right"))
  10817. (goto-char beg)
  10818. (while (< (point) end)
  10819. (if (org-at-table-hline-p)
  10820. nil
  10821. (org-table-goto-column col1 t)
  10822. (and (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
  10823. (replace-match "|\\2|\\1|")))
  10824. (beginning-of-line 2))
  10825. (move-marker end nil)
  10826. (goto-line linepos)
  10827. (org-table-goto-column colpos)
  10828. (org-table-align)
  10829. (org-table-modify-formulas 'swap col (if left (1- col) (1+ col)))))
  10830. (defun org-table-move-row-down ()
  10831. "Move table row down."
  10832. (interactive)
  10833. (org-table-move-row nil))
  10834. (defun org-table-move-row-up ()
  10835. "Move table row up."
  10836. (interactive)
  10837. (org-table-move-row 'up))
  10838. (defun org-table-move-row (&optional up)
  10839. "Move the current table line down. With arg UP, move it up."
  10840. (interactive "P")
  10841. (let ((col (current-column))
  10842. (pos (point))
  10843. (tonew (if up 0 2))
  10844. txt)
  10845. (beginning-of-line tonew)
  10846. (if (not (org-at-table-p))
  10847. (progn
  10848. (goto-char pos)
  10849. (error "Cannot move row further")))
  10850. (goto-char pos)
  10851. (beginning-of-line 1)
  10852. (setq pos (point))
  10853. (setq txt (buffer-substring (point) (1+ (point-at-eol))))
  10854. (delete-region (point) (1+ (point-at-eol)))
  10855. (beginning-of-line tonew)
  10856. (insert txt)
  10857. (beginning-of-line 0)
  10858. (move-to-column col)))
  10859. (defun org-table-insert-row (&optional arg)
  10860. "Insert a new row above the current line into the table.
  10861. With prefix ARG, insert below the current line."
  10862. (interactive "P")
  10863. (if (not (org-at-table-p))
  10864. (error "Not at a table"))
  10865. (let* ((line (buffer-substring (point-at-bol) (point-at-eol)))
  10866. (new (org-table-clean-line line)))
  10867. ;; Fix the first field if necessary
  10868. (if (string-match "^[ \t]*| *[#$] *|" line)
  10869. (setq new (replace-match (match-string 0 line) t t new)))
  10870. (beginning-of-line (if arg 2 1))
  10871. (let (org-table-may-need-update) (insert-before-markers new "\n"))
  10872. (beginning-of-line 0)
  10873. (re-search-forward "| ?" (point-at-eol) t)
  10874. (and org-table-may-need-update (org-table-align))))
  10875. (defun org-table-insert-hline (&optional arg)
  10876. "Insert a horizontal-line below the current line into the table.
  10877. With prefix ARG, insert above the current line."
  10878. (interactive "P")
  10879. (if (not (org-at-table-p))
  10880. (error "Not at a table"))
  10881. (let ((line (org-table-clean-line
  10882. (buffer-substring (point-at-bol) (point-at-eol))))
  10883. (col (current-column)))
  10884. (while (string-match "|\\( +\\)|" line)
  10885. (setq line (replace-match
  10886. (concat "+" (make-string (- (match-end 1) (match-beginning 1))
  10887. ?-) "|") t t line)))
  10888. (and (string-match "\\+" line) (setq line (replace-match "|" t t line)))
  10889. (beginning-of-line (if arg 1 2))
  10890. (insert line "\n")
  10891. (beginning-of-line (if arg 1 -1))
  10892. (move-to-column col)))
  10893. (defun org-table-clean-line (s)
  10894. "Convert a table line S into a string with only \"|\" and space.
  10895. In particular, this does handle wide and invisible characters."
  10896. (if (string-match "^[ \t]*|-" s)
  10897. ;; It's a hline, just map the characters
  10898. (setq s (mapconcat (lambda (x) (if (member x '(?| ?+)) "|" " ")) s ""))
  10899. (while (string-match "|\\([ \t]*?[^ \t\r\n|][^\r\n|]*\\)|" s)
  10900. (setq s (replace-match
  10901. (concat "|" (make-string (org-string-width (match-string 1 s))
  10902. ?\ ) "|")
  10903. t t s)))
  10904. s))
  10905. (defun org-table-kill-row ()
  10906. "Delete the current row or horizontal line from the table."
  10907. (interactive)
  10908. (if (not (org-at-table-p))
  10909. (error "Not at a table"))
  10910. (let ((col (current-column)))
  10911. (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
  10912. (if (not (org-at-table-p)) (beginning-of-line 0))
  10913. (move-to-column col)))
  10914. (defun org-table-sort-lines (beg end numericp)
  10915. "Sort table lines in region.
  10916. Point and mark define the first and last line to include. Both point and
  10917. mark should be in the column that is used for sorting. For example, to
  10918. sort according to column 3, put the mark in the first line to sort, in
  10919. table column 3. Put point into the last line to be included in the sorting,
  10920. also in table column 3. The command will prompt for the sorting method
  10921. \(n for numerical, a for alphanumeric)."
  10922. (interactive "r\nsSorting method: [n]=numeric [a]=alpha: ")
  10923. (setq numericp (string-match "[nN]" numericp))
  10924. (org-table-align) ;; Just to be safe
  10925. (let* (bcol ecol cmp column lns)
  10926. (goto-char beg)
  10927. (org-table-check-inside-data-field)
  10928. (setq column (org-table-current-column)
  10929. beg (move-marker (make-marker) (point-at-bol)))
  10930. (goto-char end)
  10931. (org-table-check-inside-data-field)
  10932. (setq end (move-marker (make-marker) (1+ (point-at-eol))))
  10933. (untabify beg end)
  10934. (goto-char beg)
  10935. (org-table-goto-column column)
  10936. (skip-chars-backward "^|")
  10937. (setq bcol (current-column))
  10938. (org-table-goto-column (1+ column))
  10939. (skip-chars-backward "^|")
  10940. (setq ecol (1- (current-column)))
  10941. (setq cmp (if numericp
  10942. (lambda (a b) (< (car a) (car b)))
  10943. (lambda (a b) (string< (car a) (car b)))))
  10944. (setq lns (mapcar (lambda(x) (cons (org-trim (substring x bcol ecol)) x))
  10945. (org-split-string (buffer-substring beg end) "\n")))
  10946. (if numericp
  10947. (setq lns (mapcar (lambda(x)
  10948. (cons (string-to-number (car x)) (cdr x)))
  10949. lns)))
  10950. (delete-region beg end)
  10951. (move-marker beg nil)
  10952. (move-marker end nil)
  10953. (insert (mapconcat 'cdr (setq lns (sort lns cmp)) "\n") "\n")
  10954. (message "%d lines sorted %s based on column %d"
  10955. (length lns)
  10956. (if numericp "numerically" "alphabetically") column)))
  10957. (defun org-table-cut-region (beg end)
  10958. "Copy region in table to the clipboard and blank all relevant fields."
  10959. (interactive "r")
  10960. (org-table-copy-region beg end 'cut))
  10961. (defun org-table-copy-region (beg end &optional cut)
  10962. "Copy rectangular region in table to clipboard.
  10963. A special clipboard is used which can only be accessed
  10964. with `org-table-paste-rectangle'."
  10965. (interactive "rP")
  10966. (let* (l01 c01 l02 c02 l1 c1 l2 c2 ic1 ic2
  10967. region cols
  10968. (rpl (if cut " " nil)))
  10969. (goto-char beg)
  10970. (org-table-check-inside-data-field)
  10971. (setq l01 (count-lines (point-min) (point))
  10972. c01 (org-table-current-column))
  10973. (goto-char end)
  10974. (org-table-check-inside-data-field)
  10975. (setq l02 (count-lines (point-min) (point))
  10976. c02 (org-table-current-column))
  10977. (setq l1 (min l01 l02) l2 (max l01 l02)
  10978. c1 (min c01 c02) c2 (max c01 c02))
  10979. (catch 'exit
  10980. (while t
  10981. (catch 'nextline
  10982. (if (> l1 l2) (throw 'exit t))
  10983. (goto-line l1)
  10984. (if (org-at-table-hline-p) (throw 'nextline (setq l1 (1+ l1))))
  10985. (setq cols nil ic1 c1 ic2 c2)
  10986. (while (< ic1 (1+ ic2))
  10987. (push (org-table-get-field ic1 rpl) cols)
  10988. (setq ic1 (1+ ic1)))
  10989. (push (nreverse cols) region)
  10990. (setq l1 (1+ l1)))))
  10991. (setq org-table-clip (nreverse region))
  10992. (if cut (org-table-align))
  10993. org-table-clip))
  10994. (defun org-table-paste-rectangle ()
  10995. "Paste a rectangular region into a table.
  10996. The upper right corner ends up in the current field. All involved fields
  10997. will be overwritten. If the rectangle does not fit into the present table,
  10998. the table is enlarged as needed. The process ignores horizontal separator
  10999. lines."
  11000. (interactive)
  11001. (unless (and org-table-clip (listp org-table-clip))
  11002. (error "First cut/copy a region to paste!"))
  11003. (org-table-check-inside-data-field)
  11004. (let* ((clip org-table-clip)
  11005. (line (count-lines (point-min) (point)))
  11006. (col (org-table-current-column))
  11007. (org-enable-table-editor t)
  11008. (org-table-automatic-realign nil)
  11009. c cols field)
  11010. (while (setq cols (pop clip))
  11011. (while (org-at-table-hline-p) (beginning-of-line 2))
  11012. (if (not (org-at-table-p))
  11013. (progn (end-of-line 0) (org-table-next-field)))
  11014. (setq c col)
  11015. (while (setq field (pop cols))
  11016. (org-table-goto-column c nil 'force)
  11017. (org-table-get-field nil field)
  11018. (setq c (1+ c)))
  11019. (beginning-of-line 2))
  11020. (goto-line line)
  11021. (org-table-goto-column col)
  11022. (org-table-align)))
  11023. (defun org-table-convert ()
  11024. "Convert from `org-mode' table to table.el and back.
  11025. Obviously, this only works within limits. When an Org-mode table is
  11026. converted to table.el, all horizontal separator lines get lost, because
  11027. table.el uses these as cell boundaries and has no notion of horizontal lines.
  11028. A table.el table can be converted to an Org-mode table only if it does not
  11029. do row or column spanning. Multiline cells will become multiple cells.
  11030. Beware, Org-mode does not test if the table can be successfully converted - it
  11031. blindly applies a recipe that works for simple tables."
  11032. (interactive)
  11033. (require 'table)
  11034. (if (org-at-table.el-p)
  11035. ;; convert to Org-mode table
  11036. (let ((beg (move-marker (make-marker) (org-table-begin t)))
  11037. (end (move-marker (make-marker) (org-table-end t))))
  11038. (table-unrecognize-region beg end)
  11039. (goto-char beg)
  11040. (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
  11041. (replace-match ""))
  11042. (goto-char beg))
  11043. (if (org-at-table-p)
  11044. ;; convert to table.el table
  11045. (let ((beg (move-marker (make-marker) (org-table-begin)))
  11046. (end (move-marker (make-marker) (org-table-end))))
  11047. ;; first, get rid of all horizontal lines
  11048. (goto-char beg)
  11049. (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t)
  11050. (replace-match ""))
  11051. ;; insert a hline before first
  11052. (goto-char beg)
  11053. (org-table-insert-hline 'above)
  11054. (beginning-of-line -1)
  11055. ;; insert a hline after each line
  11056. (while (progn (beginning-of-line 3) (< (point) end))
  11057. (org-table-insert-hline))
  11058. (goto-char beg)
  11059. (setq end (move-marker end (org-table-end)))
  11060. ;; replace "+" at beginning and ending of hlines
  11061. (while (re-search-forward "^\\([ \t]*\\)|-" end t)
  11062. (replace-match "\\1+-"))
  11063. (goto-char beg)
  11064. (while (re-search-forward "-|[ \t]*$" end t)
  11065. (replace-match "-+"))
  11066. (goto-char beg)))))
  11067. (defun org-table-wrap-region (arg)
  11068. "Wrap several fields in a column like a paragraph.
  11069. This is useful if you'd like to spread the contents of a field over several
  11070. lines, in order to keep the table compact.
  11071. If there is an active region, and both point and mark are in the same column,
  11072. the text in the column is wrapped to minimum width for the given number of
  11073. lines. Generally, this makes the table more compact. A prefix ARG may be
  11074. used to change the number of desired lines. For example, `C-2 \\[org-table-wrap]'
  11075. formats the selected text to two lines. If the region was longer than two
  11076. lines, the remaining lines remain empty. A negative prefix argument reduces
  11077. the current number of lines by that amount. The wrapped text is pasted back
  11078. into the table. If you formatted it to more lines than it was before, fields
  11079. further down in the table get overwritten - so you might need to make space in
  11080. the table first.
  11081. If there is no region, the current field is split at the cursor position and
  11082. the text fragment to the right of the cursor is prepended to the field one
  11083. line down.
  11084. If there is no region, but you specify a prefix ARG, the current field gets
  11085. blank, and the content is appended to the field above."
  11086. (interactive "P")
  11087. (org-table-check-inside-data-field)
  11088. (if (org-region-active-p)
  11089. ;; There is a region: fill as a paragraph
  11090. (let ((beg (region-beginning))
  11091. nlines)
  11092. (org-table-cut-region (region-beginning) (region-end))
  11093. (if (> (length (car org-table-clip)) 1)
  11094. (error "Region must be limited to single column"))
  11095. (setq nlines (if arg
  11096. (if (< arg 1)
  11097. (+ (length org-table-clip) arg)
  11098. arg)
  11099. (length org-table-clip)))
  11100. (setq org-table-clip
  11101. (mapcar 'list (org-wrap (mapconcat 'car org-table-clip " ")
  11102. nil nlines)))
  11103. (goto-char beg)
  11104. (org-table-paste-rectangle))
  11105. ;; No region, split the current field at point
  11106. (if arg
  11107. ;; combine with field above
  11108. (let ((s (org-table-blank-field))
  11109. (col (org-table-current-column)))
  11110. (beginning-of-line 0)
  11111. (while (org-at-table-hline-p) (beginning-of-line 0))
  11112. (org-table-goto-column col)
  11113. (skip-chars-forward "^|")
  11114. (skip-chars-backward " ")
  11115. (insert " " (org-trim s))
  11116. (org-table-align))
  11117. ;; split field
  11118. (when (looking-at "\\([^|]+\\)+|")
  11119. (let ((s (match-string 1)))
  11120. (replace-match " |")
  11121. (goto-char (match-beginning 0))
  11122. (org-table-next-row)
  11123. (insert (org-trim s) " ")
  11124. (org-table-align))))))
  11125. (defvar org-field-marker nil)
  11126. (defun org-table-edit-field (arg)
  11127. "Edit table field in a different window.
  11128. This is mainly useful for fields that contain hidden parts.
  11129. When called with a \\[universal-argument] prefix, just make the full field visible so that
  11130. it can be edited in place."
  11131. (interactive "P")
  11132. (if arg
  11133. (let ((b (save-excursion (skip-chars-backward "^|") (point)))
  11134. (e (save-excursion (skip-chars-forward "^|\r\n") (point))))
  11135. (remove-text-properties b e '(org-cwidth t invisible t
  11136. display t intangible t))
  11137. (if (and (boundp 'font-lock-mode) font-lock-mode)
  11138. (font-lock-fontify-block)))
  11139. (let ((pos (move-marker (make-marker) (point)))
  11140. (field (org-table-get-field))
  11141. (cw (current-window-configuration))
  11142. p)
  11143. (switch-to-buffer-other-window "*Org tmp*")
  11144. (erase-buffer)
  11145. (insert "#\n# Edit field and finish with C-c C-c\n#\n")
  11146. (org-mode)
  11147. (goto-char (setq p (point-max)))
  11148. (insert (org-trim field))
  11149. (remove-text-properties p (point-max)
  11150. '(invisible t org-cwidth t display t
  11151. intangible t))
  11152. (goto-char p)
  11153. (org-set-local 'org-finish-function
  11154. 'org-table-finish-edit-field)
  11155. (org-set-local 'org-window-configuration cw)
  11156. (org-set-local 'org-field-marker pos)
  11157. (message "Edit and finish with C-c C-c"))))
  11158. (defun org-table-finish-edit-field ()
  11159. "Finish editing a table data field.
  11160. Remove all newline characters, insert the result into the table, realign
  11161. the table and kill the editing buffer."
  11162. (let ((pos org-field-marker)
  11163. (cw org-window-configuration)
  11164. (cb (current-buffer))
  11165. text)
  11166. (goto-char (point-min))
  11167. (while (re-search-forward "^#.*\n?" nil t) (replace-match ""))
  11168. (while (re-search-forward "\\([ \t]*\n[ \t]*\\)+" nil t)
  11169. (replace-match " "))
  11170. (setq text (org-trim (buffer-string)))
  11171. (set-window-configuration cw)
  11172. (kill-buffer cb)
  11173. (select-window (get-buffer-window (marker-buffer pos)))
  11174. (goto-char pos)
  11175. (move-marker pos nil)
  11176. (org-table-check-inside-data-field)
  11177. (org-table-get-field nil text)
  11178. (org-table-align)
  11179. (message "New field value inserted")))
  11180. (defun org-trim (s)
  11181. "Remove whitespace at beginning and end of string."
  11182. (if (string-match "^[ \t]+" s) (setq s (replace-match "" t t s)))
  11183. (if (string-match "[ \t]+$" s) (setq s (replace-match "" t t s)))
  11184. s)
  11185. (defun org-wrap (string &optional width lines)
  11186. "Wrap string to either a number of lines, or a width in characters.
  11187. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  11188. that costs. If there is a word longer than WIDTH, the text is actually
  11189. wrapped to the length of that word.
  11190. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  11191. many lines, whatever width that takes.
  11192. The return value is a list of lines, without newlines at the end."
  11193. (let* ((words (org-split-string string "[ \t\n]+"))
  11194. (maxword (apply 'max (mapcar 'org-string-width words)))
  11195. w ll)
  11196. (cond (width
  11197. (org-do-wrap words (max maxword width)))
  11198. (lines
  11199. (setq w maxword)
  11200. (setq ll (org-do-wrap words maxword))
  11201. (if (<= (length ll) lines)
  11202. ll
  11203. (setq ll words)
  11204. (while (> (length ll) lines)
  11205. (setq w (1+ w))
  11206. (setq ll (org-do-wrap words w)))
  11207. ll))
  11208. (t (error "Cannot wrap this")))))
  11209. (defun org-do-wrap (words width)
  11210. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  11211. (let (lines line)
  11212. (while words
  11213. (setq line (pop words))
  11214. (while (and words (< (+ (length line) (length (car words))) width))
  11215. (setq line (concat line " " (pop words))))
  11216. (setq lines (push line lines)))
  11217. (nreverse lines)))
  11218. (defun org-split-string (string &optional separators)
  11219. "Splits STRING into substrings at SEPARATORS.
  11220. No empty strings are returned if there are matches at the beginning
  11221. and end of string."
  11222. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  11223. (start 0)
  11224. notfirst
  11225. (list nil))
  11226. (while (and (string-match rexp string
  11227. (if (and notfirst
  11228. (= start (match-beginning 0))
  11229. (< start (length string)))
  11230. (1+ start) start))
  11231. (< (match-beginning 0) (length string)))
  11232. (setq notfirst t)
  11233. (or (eq (match-beginning 0) 0)
  11234. (and (eq (match-beginning 0) (match-end 0))
  11235. (eq (match-beginning 0) start))
  11236. (setq list
  11237. (cons (substring string start (match-beginning 0))
  11238. list)))
  11239. (setq start (match-end 0)))
  11240. (or (eq start (length string))
  11241. (setq list
  11242. (cons (substring string start)
  11243. list)))
  11244. (nreverse list)))
  11245. (defun org-table-map-tables (function)
  11246. "Apply FUNCTION to the start of all tables in the buffer."
  11247. (save-excursion
  11248. (save-restriction
  11249. (widen)
  11250. (goto-char (point-min))
  11251. (while (re-search-forward org-table-any-line-regexp nil t)
  11252. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  11253. (beginning-of-line 1)
  11254. (if (looking-at org-table-line-regexp)
  11255. (save-excursion (funcall function)))
  11256. (re-search-forward org-table-any-border-regexp nil 1))))
  11257. (message "Mapping tables: done"))
  11258. (defun org-table-sum (&optional beg end nlast)
  11259. "Sum numbers in region of current table column.
  11260. The result will be displayed in the echo area, and will be available
  11261. as kill to be inserted with \\[yank].
  11262. If there is an active region, it is interpreted as a rectangle and all
  11263. numbers in that rectangle will be summed. If there is no active
  11264. region and point is located in a table column, sum all numbers in that
  11265. column.
  11266. If at least one number looks like a time HH:MM or HH:MM:SS, all other
  11267. numbers are assumed to be times as well (in decimal hours) and the
  11268. numbers are added as such.
  11269. If NLAST is a number, only the NLAST fields will actually be summed."
  11270. (interactive)
  11271. (save-excursion
  11272. (let (col (timecnt 0) diff h m s org-table-clip)
  11273. (cond
  11274. ((and beg end)) ; beg and end given explicitly
  11275. ((org-region-active-p)
  11276. (setq beg (region-beginning) end (region-end)))
  11277. (t
  11278. (setq col (org-table-current-column))
  11279. (goto-char (org-table-begin))
  11280. (unless (re-search-forward "^[ \t]*|[^-]" nil t)
  11281. (error "No table data"))
  11282. (org-table-goto-column col)
  11283. ;not needed? (skip-chars-backward "^|")
  11284. (setq beg (point))
  11285. (goto-char (org-table-end))
  11286. (unless (re-search-backward "^[ \t]*|[^-]" nil t)
  11287. (error "No table data"))
  11288. (org-table-goto-column col)
  11289. ;not needed? (skip-chars-forward "^|")
  11290. (setq end (point))))
  11291. (let* ((items (apply 'append (org-table-copy-region beg end)))
  11292. (items1 (cond ((not nlast) items)
  11293. ((>= nlast (length items)) items)
  11294. (t (setq items (reverse items))
  11295. (setcdr (nthcdr (1- nlast) items) nil)
  11296. (nreverse items))))
  11297. (numbers (delq nil (mapcar 'org-table-get-number-for-summing
  11298. items1)))
  11299. (res (apply '+ numbers))
  11300. (sres (if (= timecnt 0)
  11301. (format "%g" res)
  11302. (setq diff (* 3600 res)
  11303. h (floor (/ diff 3600)) diff (mod diff 3600)
  11304. m (floor (/ diff 60)) diff (mod diff 60)
  11305. s diff)
  11306. (format "%d:%02d:%02d" h m s))))
  11307. (kill-new sres)
  11308. (if (interactive-p)
  11309. (message "%s"
  11310. (substitute-command-keys
  11311. (format "Sum of %d items: %-20s (\\[yank] will insert result into buffer)"
  11312. (length numbers) sres))))
  11313. sres))))
  11314. (defun org-table-get-number-for-summing (s)
  11315. (let (n)
  11316. (if (string-match "^ *|? *" s)
  11317. (setq s (replace-match "" nil nil s)))
  11318. (if (string-match " *|? *$" s)
  11319. (setq s (replace-match "" nil nil s)))
  11320. (setq n (string-to-number s))
  11321. (cond
  11322. ((and (string-match "0" s)
  11323. (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
  11324. ((string-match "\\`[ \t]+\\'" s) nil)
  11325. ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
  11326. (let ((h (string-to-number (or (match-string 1 s) "0")))
  11327. (m (string-to-number (or (match-string 2 s) "0")))
  11328. (s (string-to-number (or (match-string 4 s) "0"))))
  11329. (if (boundp 'timecnt) (setq timecnt (1+ timecnt)))
  11330. (* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
  11331. ((equal n 0) nil)
  11332. (t n))))
  11333. (defun org-table-get-vertical-vector (desc &optional tbeg col)
  11334. "Get a calc vector from a column, accorting to descriptor DESC.
  11335. Optional arguments TBEG and COL can give the beginning of the table and
  11336. the current column, to avoid unnecessary parsing."
  11337. (save-excursion
  11338. (or tbeg (setq tbeg (org-table-begin)))
  11339. (or col (setq col (org-table-current-column)))
  11340. (let (beg end nn n n1 n2 l (thisline (org-current-line)) hline-list)
  11341. (cond
  11342. ((string-match "\\(I+\\)\\(-\\(I+\\)\\)?" desc)
  11343. (setq n1 (- (match-end 1) (match-beginning 1)))
  11344. (if (match-beginning 3)
  11345. (setq n2 (- (match-end 2) (match-beginning 3))))
  11346. (setq n (if n2 (max n1 n2) n1))
  11347. (setq n1 (if n2 (min n1 n2)))
  11348. (setq nn n)
  11349. (while (and (> nn 0)
  11350. (re-search-backward org-table-hline-regexp tbeg t))
  11351. (push (org-current-line) hline-list)
  11352. (setq nn (1- nn)))
  11353. (setq hline-list (nreverse hline-list))
  11354. (goto-line (nth (1- n) hline-list))
  11355. (when (re-search-forward org-table-dataline-regexp)
  11356. (org-table-goto-column col)
  11357. (setq beg (point)))
  11358. (goto-line (if n1 (nth (1- n1) hline-list) thisline))
  11359. (when (re-search-backward org-table-dataline-regexp)
  11360. (org-table-goto-column col)
  11361. (setq end (point)))
  11362. (setq l (apply 'append (org-table-copy-region beg end)))
  11363. (concat "[" (mapconcat (lambda (x) (setq x (org-trim x))
  11364. (if (equal x "") "0" x))
  11365. l ",") "]"))
  11366. ((string-match "\\([0-9]+\\)-\\([0-9]+\\)" desc)
  11367. (setq n1 (string-to-number (match-string 1 desc))
  11368. n2 (string-to-number (match-string 2 desc)))
  11369. (beginning-of-line 1)
  11370. (save-excursion
  11371. (when (re-search-backward org-table-dataline-regexp tbeg t n1)
  11372. (org-table-goto-column col)
  11373. (setq beg (point))))
  11374. (when (re-search-backward org-table-dataline-regexp tbeg t n2)
  11375. (org-table-goto-column col)
  11376. (setq end (point)))
  11377. (setq l (apply 'append (org-table-copy-region beg end)))
  11378. (concat "[" (mapconcat
  11379. (lambda (x) (setq x (org-trim x))
  11380. (if (equal x "") "0" x))
  11381. l ",") "]"))
  11382. ((string-match "\\([0-9]+\\)" desc)
  11383. (beginning-of-line 1)
  11384. (when (re-search-backward org-table-dataline-regexp tbeg t
  11385. (string-to-number (match-string 0 desc)))
  11386. (org-table-goto-column col)
  11387. (org-trim (org-table-get-field))))))))
  11388. (defvar org-table-formula-history nil)
  11389. (defvar org-table-column-names nil
  11390. "Alist with column names, derived from the `!' line.")
  11391. (defvar org-table-column-name-regexp nil
  11392. "Regular expression matching the current column names.")
  11393. (defvar org-table-local-parameters nil
  11394. "Alist with parameter names, derived from the `$' line.")
  11395. (defvar org-table-named-field-locations nil
  11396. "Alist with locations of named fields.")
  11397. (defun org-table-get-formula (&optional equation named)
  11398. "Read a formula from the minibuffer, offer stored formula as default."
  11399. (let* ((name (car (rassoc (list (org-current-line)
  11400. (org-table-current-column))
  11401. org-table-named-field-locations)))
  11402. (scol (if named
  11403. (if name name
  11404. (error "Not in a named field"))
  11405. (int-to-string (org-table-current-column))))
  11406. (dummy (and name (not named)
  11407. (not (y-or-n-p "Replace named-field formula with column equation? " ))
  11408. (error "Abort")))
  11409. (org-table-may-need-update nil)
  11410. (stored-list (org-table-get-stored-formulas))
  11411. (stored (cdr (assoc scol stored-list)))
  11412. (eq (cond
  11413. ((and stored equation (string-match "^ *=? *$" equation))
  11414. stored)
  11415. ((stringp equation)
  11416. equation)
  11417. (t (read-string
  11418. (format "%s formula $%s=" (if named "Field" "Column") scol)
  11419. (or stored "") 'org-table-formula-history
  11420. ;stored
  11421. ))))
  11422. mustsave)
  11423. (when (not (string-match "\\S-" eq))
  11424. ;; remove formula
  11425. (setq stored-list (delq (assoc scol stored-list) stored-list))
  11426. (org-table-store-formulas stored-list)
  11427. (error "Formula removed"))
  11428. (if (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
  11429. (if (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
  11430. (if (and name (not named))
  11431. ;; We set the column equation, delete the named one.
  11432. (setq stored-list (delq (assoc name stored-list) stored-list)
  11433. mustsave t))
  11434. (if stored
  11435. (setcdr (assoc scol stored-list) eq)
  11436. (setq stored-list (cons (cons scol eq) stored-list)))
  11437. (if (or mustsave (not (equal stored eq)))
  11438. (org-table-store-formulas stored-list))
  11439. eq))
  11440. (defun org-table-store-formulas (alist)
  11441. "Store the list of formulas below the current table."
  11442. (setq alist (sort alist (lambda (a b) (string< (car a) (car b)))))
  11443. (save-excursion
  11444. (goto-char (org-table-end))
  11445. (if (looking-at "\\([ \t]*\n\\)*#\\+TBLFM:.*\n?")
  11446. (delete-region (point) (match-end 0)))
  11447. (insert "#+TBLFM: "
  11448. (mapconcat (lambda (x)
  11449. (concat "$" (car x) "=" (cdr x)))
  11450. alist "::")
  11451. "\n")))
  11452. (defun org-table-get-stored-formulas ()
  11453. "Return an alist with the stored formulas directly after current table."
  11454. (interactive)
  11455. (let (scol eq eq-alist strings string seen)
  11456. (save-excursion
  11457. (goto-char (org-table-end))
  11458. (when (looking-at "\\([ \t]*\n\\)*#\\+TBLFM: *\\(.*\\)")
  11459. (setq strings (org-split-string (match-string 2) " *:: *"))
  11460. (while (setq string (pop strings))
  11461. (when (string-match "\\$\\([a-zA-Z0-9]+\\) *= *\\(.*[^ \t]\\)" string)
  11462. (setq scol (match-string 1 string)
  11463. eq (match-string 2 string)
  11464. eq-alist (cons (cons scol eq) eq-alist))
  11465. (if (member scol seen)
  11466. (error "Double definition `$%s=' in TBLFM line, please fix by hand" scol)
  11467. (push scol seen))))))
  11468. (nreverse eq-alist)))
  11469. (defun org-table-modify-formulas (action &rest columns)
  11470. "Modify the formulas stored below the current table.
  11471. ACTION can be `remove', `insert', `swap'. For `swap', two column numbers are
  11472. expected, for the other actions only a single column number is needed."
  11473. (let ((list (org-table-get-stored-formulas))
  11474. (nmax (length (org-split-string
  11475. (buffer-substring (point-at-bol) (point-at-eol))
  11476. "|")))
  11477. col col1 col2 scol si sc1 sc2)
  11478. (cond
  11479. ((null list)) ; No action needed if there are no stored formulas
  11480. ((eq action 'remove)
  11481. (setq col (car columns)
  11482. scol (int-to-string col))
  11483. (org-table-replace-in-formulas list scol "INVALID")
  11484. (if (assoc scol list) (setq list (delq (assoc scol list) list)))
  11485. (loop for i from (1+ col) upto nmax by 1 do
  11486. (setq si (int-to-string i))
  11487. (org-table-replace-in-formulas list si (int-to-string (1- i)))
  11488. (if (assoc si list) (setcar (assoc si list)
  11489. (int-to-string (1- i))))))
  11490. ((eq action 'insert)
  11491. (setq col (car columns))
  11492. (loop for i from nmax downto col by 1 do
  11493. (setq si (int-to-string i))
  11494. (org-table-replace-in-formulas list si (int-to-string (1+ i)))
  11495. (if (assoc si list) (setcar (assoc si list)
  11496. (int-to-string (1+ i))))))
  11497. ((eq action 'swap)
  11498. (setq col1 (car columns) col2 (nth 1 columns)
  11499. sc1 (int-to-string col1) sc2 (int-to-string col2))
  11500. ;; Hopefully, ZqZtZ will never be a name in a table
  11501. (org-table-replace-in-formulas list sc1 "ZqZtZ")
  11502. (org-table-replace-in-formulas list sc2 sc1)
  11503. (org-table-replace-in-formulas list "ZqZtZ" sc2)
  11504. (if (assoc sc1 list) (setcar (assoc sc1 list) "ZqZtZ"))
  11505. (if (assoc sc2 list) (setcar (assoc sc2 list) sc1))
  11506. (if (assoc "ZqZtZ" list) (setcar (assoc "ZqZtZ" list) sc2)))
  11507. (t (error "Invalid action in `org-table-modify-formulas'")))
  11508. (if list (org-table-store-formulas list))))
  11509. (defun org-table-replace-in-formulas (list s1 s2)
  11510. (let (elt re s)
  11511. (setq s1 (concat "$" (if (integerp s1) (int-to-string s1) s1))
  11512. s2 (concat "$" (if (integerp s2) (int-to-string s2) s2))
  11513. re (concat (regexp-quote s1) "\\>"))
  11514. (while (setq elt (pop list))
  11515. (setq s (cdr elt))
  11516. (while (string-match re s)
  11517. (setq s (replace-match s2 t t s)))
  11518. (setcdr elt s))))
  11519. (defun org-table-get-specials ()
  11520. "Get the column names and local parameters for this table."
  11521. (save-excursion
  11522. (let ((beg (org-table-begin)) (end (org-table-end))
  11523. names name fields fields1 field cnt c v line col)
  11524. (setq org-table-column-names nil
  11525. org-table-local-parameters nil
  11526. org-table-named-field-locations nil)
  11527. (goto-char beg)
  11528. (when (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t)
  11529. (setq names (org-split-string (match-string 1) " *| *")
  11530. cnt 1)
  11531. (while (setq name (pop names))
  11532. (setq cnt (1+ cnt))
  11533. (if (string-match "^[a-zA-Z][a-zA-Z0-9]*$" name)
  11534. (push (cons name (int-to-string cnt)) org-table-column-names))))
  11535. (setq org-table-column-names (nreverse org-table-column-names))
  11536. (setq org-table-column-name-regexp
  11537. (concat "\\$\\(" (mapconcat 'car org-table-column-names "\\|") "\\)\\>"))
  11538. (goto-char beg)
  11539. (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t)
  11540. (setq fields (org-split-string (match-string 1) " *| *"))
  11541. (while (setq field (pop fields))
  11542. (if (string-match "^\\([a-zA-Z][a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field)
  11543. (push (cons (match-string 1 field) (match-string 2 field))
  11544. org-table-local-parameters))))
  11545. (goto-char beg)
  11546. (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t)
  11547. (setq c (match-string 1)
  11548. fields (org-split-string (match-string 2) " *| *"))
  11549. (save-excursion
  11550. (beginning-of-line (if (equal c "_") 2 0))
  11551. (setq line (org-current-line) col 1)
  11552. (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
  11553. (setq fields1 (org-split-string (match-string 1) " *| *"))))
  11554. (while (and fields1 (setq field (pop fields)))
  11555. (setq v (pop fields1) col (1+ col))
  11556. (when (and (stringp field) (stringp v)
  11557. (string-match "^[a-zA-Z][a-zA-Z0-9]*$" field))
  11558. (push (cons field v) org-table-local-parameters)
  11559. (push (list field line col) org-table-named-field-locations)))))))
  11560. (defun org-this-word ()
  11561. ;; Get the current word
  11562. (save-excursion
  11563. (let ((beg (progn (skip-chars-backward "^ \t\n") (point)))
  11564. (end (progn (skip-chars-forward "^ \t\n") (point))))
  11565. (buffer-substring-no-properties beg end))))
  11566. (defun org-table-maybe-eval-formula ()
  11567. "Check if the current field starts with \"=\" or \":=\".
  11568. If yes, store the formula and apply it."
  11569. ;; We already know we are in a table. Get field will only return a formula
  11570. ;; when appropriate. It might return a separator line, but no problem.
  11571. (when org-table-formula-evaluate-inline
  11572. (let* ((field (org-trim (or (org-table-get-field) "")))
  11573. named eq)
  11574. (when (string-match "^:?=\\(.*\\)" field)
  11575. (setq named (equal (string-to-char field) ?:)
  11576. eq (match-string 1 field))
  11577. (if (fboundp 'calc-eval)
  11578. (org-table-eval-formula (if named '(4) nil) eq))))))
  11579. (defvar org-recalc-commands nil
  11580. "List of commands triggering the recalculation of a line.
  11581. Will be filled automatically during use.")
  11582. (defvar org-recalc-marks
  11583. '((" " . "Unmarked: no special line, no automatic recalculation")
  11584. ("#" . "Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
  11585. ("*" . "Recalculate only when entire table is recalculated with `C-u C-c *'")
  11586. ("!" . "Column name definition line. Reference in formula as $name.")
  11587. ("$" . "Parameter definition line name=value. Reference in formula as $name.")
  11588. ("_" . "Names for values in row below this one.")
  11589. ("^" . "Names for values in row above this one.")))
  11590. (defun org-table-rotate-recalc-marks (&optional newchar)
  11591. "Rotate the recalculation mark in the first column.
  11592. If in any row, the first field is not consistent with a mark,
  11593. insert a new column for the markers.
  11594. When there is an active region, change all the lines in the region,
  11595. after prompting for the marking character.
  11596. After each change, a message will be displayed indicating the meaning
  11597. of the new mark."
  11598. (interactive)
  11599. (unless (org-at-table-p) (error "Not at a table"))
  11600. (let* ((marks (append (mapcar 'car org-recalc-marks) '(" ")))
  11601. (beg (org-table-begin))
  11602. (end (org-table-end))
  11603. (l (org-current-line))
  11604. (l1 (if (org-region-active-p) (org-current-line (region-beginning))))
  11605. (l2 (if (org-region-active-p) (org-current-line (region-end))))
  11606. (have-col
  11607. (save-excursion
  11608. (goto-char beg)
  11609. (not (re-search-forward "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" end t))))
  11610. (col (org-table-current-column))
  11611. (forcenew (car (assoc newchar org-recalc-marks)))
  11612. epos new)
  11613. (when l1
  11614. (message "Change region to what mark? Type # * ! $ or SPC: ")
  11615. (setq newchar (char-to-string (read-char-exclusive))
  11616. forcenew (car (assoc newchar org-recalc-marks))))
  11617. (if (and newchar (not forcenew))
  11618. (error "Invalid NEWCHAR `%s' in `org-table-rotate-recalc-marks'"
  11619. newchar))
  11620. (if l1 (goto-line l1))
  11621. (save-excursion
  11622. (beginning-of-line 1)
  11623. (unless (looking-at org-table-dataline-regexp)
  11624. (error "Not at a table data line")))
  11625. (unless have-col
  11626. (org-table-goto-column 1)
  11627. (org-table-insert-column)
  11628. (org-table-goto-column (1+ col)))
  11629. (setq epos (point-at-eol))
  11630. (save-excursion
  11631. (beginning-of-line 1)
  11632. (org-table-get-field
  11633. 1 (if (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")
  11634. (concat " "
  11635. (setq new (or forcenew
  11636. (cadr (member (match-string 1) marks))))
  11637. " ")
  11638. " # ")))
  11639. (if (and l1 l2)
  11640. (progn
  11641. (goto-line l1)
  11642. (while (progn (beginning-of-line 2) (not (= (org-current-line) l2)))
  11643. (and (looking-at org-table-dataline-regexp)
  11644. (org-table-get-field 1 (concat " " new " "))))
  11645. (goto-line l1)))
  11646. (if (not (= epos (point-at-eol))) (org-table-align))
  11647. (goto-line l)
  11648. (and (interactive-p) (message (cdr (assoc new org-recalc-marks))))))
  11649. (defun org-table-maybe-recalculate-line ()
  11650. "Recompute the current line if marked for it, and if we haven't just done it."
  11651. (interactive)
  11652. (and org-table-allow-automatic-line-recalculation
  11653. (not (and (memq last-command org-recalc-commands)
  11654. (equal org-last-recalc-line (org-current-line))))
  11655. (save-excursion (beginning-of-line 1)
  11656. (looking-at org-table-auto-recalculate-regexp))
  11657. (fboundp 'calc-eval)
  11658. (org-table-recalculate) t))
  11659. (defvar org-table-formula-debug nil
  11660. "Non-nil means, debug table formulas.
  11661. When nil, simply write \"#ERROR\" in corrupted fields.")
  11662. (defvar modes)
  11663. (defsubst org-set-calc-mode (var &optional value)
  11664. (if (stringp var)
  11665. (setq var (assoc var '(("D" calc-angle-mode deg)
  11666. ("R" calc-angle-mode rad)
  11667. ("F" calc-prefer-frac t)
  11668. ("S" calc-symbolic-mode t)))
  11669. value (nth 2 var) var (nth 1 var)))
  11670. (if (memq var modes)
  11671. (setcar (cdr (memq var modes)) value)
  11672. (cons var (cons value modes)))
  11673. modes)
  11674. (defun org-table-eval-formula (&optional arg equation
  11675. suppress-align suppress-const
  11676. suppress-store)
  11677. "Replace the table field value at the cursor by the result of a calculation.
  11678. This function makes use of Dave Gillespie's Calc package, in my view the
  11679. most exciting program ever written for GNU Emacs. So you need to have Calc
  11680. installed in order to use this function.
  11681. In a table, this command replaces the value in the current field with the
  11682. result of a formula. It also installs the formula as the \"current\" column
  11683. formula, by storing it in a special line below the table. When called
  11684. with a `C-u' prefix, the current field must ba a named field, and the
  11685. formula is installed as valid in only this specific field.
  11686. When called, the command first prompts for a formula, which is read in
  11687. the minibuffer. Previously entered formulas are available through the
  11688. history list, and the last used formula is offered as a default.
  11689. These stored formulas are adapted correctly when moving, inserting, or
  11690. deleting columns with the corresponding commands.
  11691. The formula can be any algebraic expression understood by the Calc package.
  11692. For details, see the Org-mode manual.
  11693. This function can also be called from Lisp programs and offers
  11694. additional arguments: EQUATION can be the formula to apply. If this
  11695. argument is given, the user will not be prompted. SUPPRESS-ALIGN is
  11696. used to speed-up recursive calls by by-passing unnecessary aligns.
  11697. SUPPRESS-CONST suppresses the interpretation of constants in the
  11698. formula, assuming that this has been done already outside the function.
  11699. SUPPRESS-STORE means the formula should not be stored, either because
  11700. it is already stored, or because it is a modified equation that should
  11701. not overwrite the stored one."
  11702. (interactive "P")
  11703. (require 'calc)
  11704. (org-table-check-inside-data-field)
  11705. (org-table-get-specials)
  11706. (let* (fields
  11707. (ndown (if (integerp arg) arg 1))
  11708. (org-table-automatic-realign nil)
  11709. (case-fold-search nil)
  11710. (down (> ndown 1))
  11711. (formula (if (and equation suppress-store)
  11712. equation
  11713. (org-table-get-formula equation (equal arg '(4)))))
  11714. (n0 (org-table-current-column))
  11715. (modes (copy-sequence org-calc-default-modes))
  11716. n form fmt x ev orig c lispp)
  11717. ;; Parse the format string. Since we have a lot of modes, this is
  11718. ;; a lot of work. However, I think calc still uses most of the time.
  11719. (if (string-match ";" formula)
  11720. (let ((tmp (org-split-string formula ";")))
  11721. (setq formula (car tmp)
  11722. fmt (concat (cdr (assoc "%" org-table-local-parameters))
  11723. (nth 1 tmp)))
  11724. (while (string-match "\\([pnfse]\\)\\(-?[0-9]+\\)" fmt)
  11725. (setq c (string-to-char (match-string 1 fmt))
  11726. n (string-to-number (match-string 2 fmt)))
  11727. (if (= c ?p)
  11728. (setq modes (org-set-calc-mode 'calc-internal-prec n))
  11729. (setq modes (org-set-calc-mode
  11730. 'calc-float-format
  11731. (list (cdr (assoc c '((?n . float) (?f . fix)
  11732. (?s . sci) (?e . eng))))
  11733. n))))
  11734. (setq fmt (replace-match "" t t fmt)))
  11735. (while (string-match "[DRFS]" fmt)
  11736. (setq modes (org-set-calc-mode (match-string 0 fmt)))
  11737. (setq fmt (replace-match "" t t fmt)))
  11738. (unless (string-match "\\S-" fmt)
  11739. (setq fmt nil))))
  11740. (if (and (not suppress-const) org-table-formula-use-constants)
  11741. (setq formula (org-table-formula-substitute-names formula)))
  11742. (setq orig (or (get-text-property 1 :orig-formula formula) "?"))
  11743. (while (> ndown 0)
  11744. (setq fields (org-split-string
  11745. (buffer-substring
  11746. (point-at-bol) (point-at-eol)) " *| *"))
  11747. (if org-table-formula-numbers-only
  11748. (setq fields (mapcar
  11749. (lambda (x) (number-to-string (string-to-number x)))
  11750. fields)))
  11751. (setq ndown (1- ndown))
  11752. (setq form (copy-sequence formula)
  11753. lispp (equal (substring form 0 2) "'("))
  11754. ;; Insert the references to fields in same row
  11755. (while (string-match "\\$\\([0-9]+\\)?" form)
  11756. (setq n (if (match-beginning 1)
  11757. (string-to-number (match-string 1 form))
  11758. n0)
  11759. x (nth (1- n) fields))
  11760. (unless x (error "Invalid field specifier \"%s\""
  11761. (match-string 0 form)))
  11762. (if (equal x "") (setq x "0"))
  11763. (setq form (replace-match
  11764. (if lispp x (concat "(" x ")"))
  11765. t t form)))
  11766. ;; Insert ranges in current column
  11767. (while (string-match "\\&[-I0-9]+" form)
  11768. (setq form (replace-match
  11769. (save-match-data
  11770. (org-table-get-vertical-vector (match-string 0 form)
  11771. nil n0))
  11772. t t form)))
  11773. (if lispp
  11774. (setq ev (eval (eval (read form)))
  11775. ev (if (numberp ev) (number-to-string ev) ev))
  11776. (setq ev (calc-eval (cons form modes)
  11777. (if org-table-formula-numbers-only 'num))))
  11778. (when org-table-formula-debug
  11779. (with-output-to-temp-buffer "*Help*"
  11780. (princ (format "Substitution history of formula
  11781. Orig: %s
  11782. $xyz-> %s
  11783. $1-> %s\n" orig formula form))
  11784. (if (listp ev)
  11785. (princ (format " %s^\nError: %s"
  11786. (make-string (car ev) ?\-) (nth 1 ev)))
  11787. (princ (format "Result: %s\nFormat: %s\nFinal: %s"
  11788. ev (or fmt "NONE")
  11789. (if fmt (format fmt (string-to-number ev)) ev)))))
  11790. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  11791. (unless (and (interactive-p) (not ndown))
  11792. (unless (let (inhibit-redisplay)
  11793. (y-or-n-p "Debugging Formula. Continue to next? "))
  11794. (org-table-align)
  11795. (error "Abort"))
  11796. (delete-window (get-buffer-window "*Help*"))
  11797. (message "")))
  11798. (if (listp ev) (setq fmt nil ev "#ERROR"))
  11799. (org-table-justify-field-maybe
  11800. (if fmt (format fmt (string-to-number ev)) ev))
  11801. (if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
  11802. (call-interactively 'org-return)
  11803. (setq ndown 0)))
  11804. (and down (org-table-maybe-recalculate-line))
  11805. (or suppress-align (and org-table-may-need-update
  11806. (org-table-align)))))
  11807. (defun org-table-recalculate (&optional all noalign)
  11808. "Recalculate the current table line by applying all stored formulas.
  11809. With prefix arg ALL, do this for all lines in the table."
  11810. (interactive "P")
  11811. (or (memq this-command org-recalc-commands)
  11812. (setq org-recalc-commands (cons this-command org-recalc-commands)))
  11813. (unless (org-at-table-p) (error "Not at a table"))
  11814. (org-table-get-specials)
  11815. (let* ((eqlist (sort (org-table-get-stored-formulas)
  11816. (lambda (a b) (string< (car a) (car b)))))
  11817. (inhibit-redisplay t)
  11818. (line-re org-table-dataline-regexp)
  11819. (thisline (org-current-line))
  11820. (thiscol (org-table-current-column))
  11821. beg end entry eqlnum eqlname eql (cnt 0) eq a name)
  11822. ;; Insert constants in all formulas
  11823. (setq eqlist
  11824. (mapcar (lambda (x)
  11825. (setcdr x (org-table-formula-substitute-names (cdr x)))
  11826. x)
  11827. eqlist))
  11828. ;; Split the equation list
  11829. (while (setq eq (pop eqlist))
  11830. (if (<= (string-to-char (car eq)) ?9)
  11831. (push eq eqlnum)
  11832. (push eq eqlname)))
  11833. (setq eqlnum (nreverse eqlnum) eqlname (nreverse eqlname))
  11834. (if all
  11835. (progn
  11836. (setq end (move-marker (make-marker) (1+ (org-table-end))))
  11837. (goto-char (setq beg (org-table-begin)))
  11838. (if (re-search-forward org-table-calculate-mark-regexp end t)
  11839. ;; This is a table with marked lines, only compute selected lines
  11840. (setq line-re org-table-recalculate-regexp)
  11841. ;; Move forward to the first non-header line
  11842. (if (and (re-search-forward org-table-dataline-regexp end t)
  11843. (re-search-forward org-table-hline-regexp end t)
  11844. (re-search-forward org-table-dataline-regexp end t))
  11845. (setq beg (match-beginning 0))
  11846. nil))) ;; just leave beg where it is
  11847. (setq beg (point-at-bol)
  11848. end (move-marker (make-marker) (1+ (point-at-eol)))))
  11849. (goto-char beg)
  11850. (and all (message "Re-applying formulas to full table..."))
  11851. (while (re-search-forward line-re end t)
  11852. (unless (string-match "^ *[_^!$] *$" (org-table-get-field 1))
  11853. ;; Unprotected line, recalculate
  11854. (and all (message "Re-applying formulas to full table...(line %d)"
  11855. (setq cnt (1+ cnt))))
  11856. (setq org-last-recalc-line (org-current-line))
  11857. (setq eql eqlnum)
  11858. (while (setq entry (pop eql))
  11859. (goto-line org-last-recalc-line)
  11860. (org-table-goto-column (string-to-number (car entry)) nil 'force)
  11861. (org-table-eval-formula nil (cdr entry) 'noalign 'nocst 'nostore))))
  11862. (goto-line thisline)
  11863. (org-table-goto-column thiscol)
  11864. (or noalign (and org-table-may-need-update (org-table-align))
  11865. (and all (message "Re-applying formulas to %d lines...done" cnt)))
  11866. ;; Now do the names fields
  11867. (while (setq eq (pop eqlname))
  11868. (setq name (car eq)
  11869. a (assoc name org-table-named-field-locations))
  11870. (when a
  11871. (message "Re-applying formula to named field: %s" name)
  11872. (goto-line (nth 1 a))
  11873. (org-table-goto-column (nth 2 a))
  11874. (org-table-eval-formula nil (cdr eq) 'noalign 'nocst 'nostore)))
  11875. ;; back to initial position
  11876. (goto-line thisline)
  11877. (org-table-goto-column thiscol)
  11878. (or noalign (and org-table-may-need-update (org-table-align))
  11879. (and all (message "Re-applying formulas...done")))))
  11880. (defun org-table-formula-substitute-names (f)
  11881. "Replace $const with values in string F."
  11882. (let ((start 0) a n1 n2 nn1 nn2 s (f1 f))
  11883. ;; First, check for column names
  11884. (while (setq start (string-match org-table-column-name-regexp f start))
  11885. (setq start (1+ start))
  11886. (setq a (assoc (match-string 1 f) org-table-column-names))
  11887. (setq f (replace-match (concat "$" (cdr a)) t t f)))
  11888. ;; Expand ranges to vectors
  11889. (while (string-match "\\$\\([0-9]+\\)\\.\\.\\.?\\$\\([0-9]+\\)" f)
  11890. (setq n1 (string-to-number (match-string 1 f))
  11891. n2 (string-to-number (match-string 2 f))
  11892. nn1 (1+ (min n1 n2)) nn2 (max n1 n2)
  11893. s (concat "[($" (number-to-string (1- nn1)) ")"))
  11894. (loop for i from nn1 upto nn2 do
  11895. (setq s (concat s ",($" (int-to-string i) ")")))
  11896. (setq s (concat s "]"))
  11897. (if (< n2 n1) (setq s (concat "rev(" s ")")))
  11898. (setq f (replace-match s t t f)))
  11899. ;; Parameters and constants
  11900. (setq start 0)
  11901. (while (setq start (string-match "\\$\\([a-zA-Z][a-zA-Z0-9]*\\)" f start))
  11902. (setq start (1+ start))
  11903. (if (setq a (save-match-data
  11904. (org-table-get-constant (match-string 1 f))))
  11905. (setq f (replace-match (concat "(" a ")") t t f))))
  11906. (if org-table-formula-debug
  11907. (put-text-property 0 (length f) :orig-formula f1 f))
  11908. f))
  11909. (defun org-table-get-constant (const)
  11910. "Find the value for a parameter or constant in a formula.
  11911. Parameters get priority."
  11912. (or (cdr (assoc const org-table-local-parameters))
  11913. (cdr (assoc const org-table-formula-constants))
  11914. (and (fboundp 'constants-get) (constants-get const))
  11915. "#UNDEFINED_NAME"))
  11916. (defvar org-edit-formulas-map (make-sparse-keymap))
  11917. (define-key org-edit-formulas-map "\C-c\C-c" 'org-finish-edit-formulas)
  11918. (define-key org-edit-formulas-map "\C-c\C-q" 'org-abort-edit-formulas)
  11919. (define-key org-edit-formulas-map "\C-c?" 'org-show-variable)
  11920. (defvar org-pos)
  11921. (defun org-table-edit-formulas ()
  11922. "Edit the formulas of the current table in a separate buffer."
  11923. (interactive)
  11924. (unless (org-at-table-p)
  11925. (error "Not at a table"))
  11926. (org-table-get-specials)
  11927. (let ((eql (org-table-get-stored-formulas))
  11928. (pos (move-marker (make-marker) (point)))
  11929. (wc (current-window-configuration))
  11930. entry loc s)
  11931. (switch-to-buffer-other-window "*Edit Formulas*")
  11932. (erase-buffer)
  11933. (fundamental-mode)
  11934. (org-set-local 'org-pos pos)
  11935. (org-set-local 'org-window-configuration wc)
  11936. (use-local-map org-edit-formulas-map)
  11937. (setq s "# Edit formulas and finish with `C-c C-c'.
  11938. # Use `C-u C-c C-c' to also appy them immediately to the entire table.
  11939. # Use `C-c ?' to get information about $name at point.
  11940. # To cancel editing, press `C-c C-q'.\n")
  11941. (put-text-property 0 (length s) 'face 'font-lock-comment-face s)
  11942. (insert s)
  11943. (while (setq entry (pop eql))
  11944. (when (setq loc (assoc (car entry) org-table-named-field-locations))
  11945. (setq s (format "# Named formula, referring to column %d in line %d\n"
  11946. (nth 2 loc) (nth 1 loc)))
  11947. (put-text-property 0 (length s) 'face 'font-lock-comment-face s)
  11948. (insert s))
  11949. (setq s (concat "$" (car entry) "=" (cdr entry) "\n"))
  11950. (remove-text-properties 0 (length s) '(face nil) s)
  11951. (insert s))
  11952. (goto-char (point-min))
  11953. (message "Edit formulas and finish with `C-c C-c'.")))
  11954. (defun org-show-variable ()
  11955. "Show the location/value of the $ expression at point."
  11956. (interactive)
  11957. (let (var (pos org-pos) (win (selected-window)) e)
  11958. (save-excursion
  11959. (or (looking-at "\\$") (skip-chars-backward "$a-zA-Z0-9"))
  11960. (if (looking-at "\\$\\([a-zA-Z0-9]+\\)")
  11961. (setq var (match-string 1))
  11962. (error "No variable at point")))
  11963. (cond
  11964. ((setq e (assoc var org-table-named-field-locations))
  11965. (switch-to-buffer-other-window (marker-buffer pos))
  11966. (goto-line (nth 1 e))
  11967. (org-table-goto-column (nth 2 e))
  11968. (select-window win)
  11969. (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
  11970. ((setq e (assoc var org-table-column-names))
  11971. (switch-to-buffer-other-window (marker-buffer pos))
  11972. (goto-char pos)
  11973. (goto-char (org-table-begin))
  11974. (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
  11975. (org-table-end) t)
  11976. (progn
  11977. (goto-char (match-beginning 1))
  11978. (message "Named column (column %s)" (cdr e)))
  11979. (error "Column name not found"))
  11980. (select-window win))
  11981. ((string-match "^[0-9]$" var)
  11982. ;; column number
  11983. (switch-to-buffer-other-window (marker-buffer pos))
  11984. (goto-char pos)
  11985. (goto-char (org-table-begin))
  11986. (recenter 1)
  11987. (if (re-search-forward org-table-dataline-regexp
  11988. (org-table-end) t)
  11989. (progn
  11990. (goto-char (match-beginning 0))
  11991. (org-table-goto-column (string-to-number var))
  11992. (message "Column %s" var))
  11993. (error "Column name not found"))
  11994. (select-window win))
  11995. ((setq e (assoc var org-table-local-parameters))
  11996. (switch-to-buffer-other-window (marker-buffer pos))
  11997. (goto-char pos)
  11998. (goto-char (org-table-begin))
  11999. (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
  12000. (progn
  12001. (goto-char (match-beginning 1))
  12002. (message "Local parameter."))
  12003. (error "Parameter not found"))
  12004. (select-window win))
  12005. (t
  12006. (cond
  12007. ((setq e (assoc var org-table-formula-constants))
  12008. (message "Constant: $%s=%s in `org-table-formula-constants'." var (cdr e)))
  12009. ((setq e (and (fboundp 'constants-get) (constants-get var)))
  12010. (message "Constant: $%s=%s, retrieved from `constants.el'." var e))
  12011. (t (error "Undefined name $%s" var)))))))
  12012. (defun org-finish-edit-formulas (&optional arg)
  12013. "Parse the buffer for formula definitions and install them.
  12014. With prefix ARG, apply the new formulas to the table."
  12015. (interactive "P")
  12016. (let ((pos org-pos) eql)
  12017. (goto-char (point-min))
  12018. (while (re-search-forward "^\\$\\([a-zA-Z0-9]+\\) *= *\\(.*?\\) *$" nil t)
  12019. (push (cons (match-string 1) (match-string 2)) eql))
  12020. (set-window-configuration org-window-configuration)
  12021. (select-window (get-buffer-window (marker-buffer pos)))
  12022. (goto-char pos)
  12023. (unless (org-at-table-p)
  12024. (error "Lost table position - cannot install formulae"))
  12025. (org-table-store-formulas eql)
  12026. (move-marker pos nil)
  12027. (kill-buffer "*Edit Formulas*")
  12028. (if arg
  12029. (org-table-recalculate 'all)
  12030. (message "New formulas installed - press C-u C-c C-c to apply."))))
  12031. (defun org-abort-edit-formulas ()
  12032. "Abort editing formulas, without installing the changes."
  12033. (interactive)
  12034. (let ((pos org-pos))
  12035. (set-window-configuration org-window-configuration)
  12036. (select-window (get-buffer-window (marker-buffer pos)))
  12037. (goto-char pos)
  12038. (message "Formula editing aborted without installing changes")))
  12039. ;;; The orgtbl minor mode
  12040. ;; Define a minor mode which can be used in other modes in order to
  12041. ;; integrate the org-mode table editor.
  12042. ;; This is really a hack, because the org-mode table editor uses several
  12043. ;; keys which normally belong to the major mode, for example the TAB and
  12044. ;; RET keys. Here is how it works: The minor mode defines all the keys
  12045. ;; necessary to operate the table editor, but wraps the commands into a
  12046. ;; function which tests if the cursor is currently inside a table. If that
  12047. ;; is the case, the table editor command is executed. However, when any of
  12048. ;; those keys is used outside a table, the function uses `key-binding' to
  12049. ;; look up if the key has an associated command in another currently active
  12050. ;; keymap (minor modes, major mode, global), and executes that command.
  12051. ;; There might be problems if any of the keys used by the table editor is
  12052. ;; otherwise used as a prefix key.
  12053. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  12054. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  12055. ;; addresses this by checking explicitly for both bindings.
  12056. ;; The optimized version (see variable `orgtbl-optimized') takes over
  12057. ;; all keys which are bound to `self-insert-command' in the *global map*.
  12058. ;; Some modes bind other commands to simple characters, for example
  12059. ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
  12060. ;; active, this binding is ignored inside tables and replaced with a
  12061. ;; modified self-insert.
  12062. (defvar orgtbl-mode nil
  12063. "Variable controlling `orgtbl-mode', a minor mode enabling the `org-mode'
  12064. table editor in arbitrary modes.")
  12065. (make-variable-buffer-local 'orgtbl-mode)
  12066. (defvar orgtbl-mode-map (make-keymap)
  12067. "Keymap for `orgtbl-mode'.")
  12068. ;;;###autoload
  12069. (defun turn-on-orgtbl ()
  12070. "Unconditionally turn on `orgtbl-mode'."
  12071. (orgtbl-mode 1))
  12072. ;;;###autoload
  12073. (defun orgtbl-mode (&optional arg)
  12074. "The `org-mode' table editor as a minor mode for use in other modes."
  12075. (interactive)
  12076. (if (org-mode-p)
  12077. ;; Exit without error, in case some hook functions calls this
  12078. ;; by accident in org-mode.
  12079. (message "Orgtbl-mode is not useful in org-mode, command ignored")
  12080. (setq orgtbl-mode
  12081. (if arg (> (prefix-numeric-value arg) 0) (not orgtbl-mode)))
  12082. (if orgtbl-mode
  12083. (progn
  12084. (and (orgtbl-setup) (defun orgtbl-setup () nil))
  12085. ;; Make sure we are first in minor-mode-map-alist
  12086. (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
  12087. (and c (setq minor-mode-map-alist
  12088. (cons c (delq c minor-mode-map-alist)))))
  12089. (org-set-local (quote org-table-may-need-update) t)
  12090. (org-add-hook 'before-change-functions 'org-before-change-function
  12091. nil 'local)
  12092. (org-set-local 'org-old-auto-fill-inhibit-regexp
  12093. auto-fill-inhibit-regexp)
  12094. (org-set-local 'auto-fill-inhibit-regexp
  12095. (if auto-fill-inhibit-regexp
  12096. (concat "\\([ \t]*|\\|" auto-fill-inhibit-regexp)
  12097. "[ \t]*|"))
  12098. (org-add-to-invisibility-spec '(org-cwidth))
  12099. (easy-menu-add orgtbl-mode-menu)
  12100. (run-hooks 'orgtbl-mode-hook))
  12101. (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
  12102. (org-cleanup-narrow-column-properties)
  12103. (org-remove-from-invisibility-spec '(org-cwidth))
  12104. (remove-hook 'before-change-functions 'org-before-change-function t)
  12105. (easy-menu-remove orgtbl-mode-menu)
  12106. (force-mode-line-update 'all))))
  12107. (defun org-cleanup-narrow-column-properties ()
  12108. "Remove all properties related to narrow-column invisibility."
  12109. (let ((s 1))
  12110. (while (setq s (text-property-any s (point-max)
  12111. 'display org-narrow-column-arrow))
  12112. (remove-text-properties s (1+ s) '(display t)))
  12113. (setq s 1)
  12114. (while (setq s (text-property-any s (point-max) 'org-cwidth 1))
  12115. (remove-text-properties s (1+ s) '(org-cwidth t)))
  12116. (setq s 1)
  12117. (while (setq s (text-property-any s (point-max) 'invisible 'org-cwidth))
  12118. (remove-text-properties s (1+ s) '(invisible t)))))
  12119. ;; Install it as a minor mode.
  12120. (put 'orgtbl-mode :included t)
  12121. (put 'orgtbl-mode :menu-tag "Org Table Mode")
  12122. (add-minor-mode 'orgtbl-mode " OrgTbl" orgtbl-mode-map)
  12123. (defun orgtbl-make-binding (fun n &rest keys)
  12124. "Create a function for binding in the table minor mode.
  12125. FUN is the command to call inside a table. N is used to create a unique
  12126. command name. KEYS are keys that should be checked in for a command
  12127. to execute outside of tables."
  12128. (eval
  12129. (list 'defun
  12130. (intern (concat "orgtbl-hijacker-command-" (int-to-string n)))
  12131. '(arg)
  12132. (concat "In tables, run `" (symbol-name fun) "'.\n"
  12133. "Outside of tables, run the binding of `"
  12134. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  12135. "'.")
  12136. '(interactive "p")
  12137. (list 'if
  12138. '(org-at-table-p)
  12139. (list 'call-interactively (list 'quote fun))
  12140. (list 'let '(orgtbl-mode)
  12141. (list 'call-interactively
  12142. (append '(or)
  12143. (mapcar (lambda (k)
  12144. (list 'key-binding k))
  12145. keys)
  12146. '('orgtbl-error))))))))
  12147. (defun orgtbl-error ()
  12148. "Error when there is no default binding for a table key."
  12149. (interactive)
  12150. (error "This key is has no function outside tables"))
  12151. (defun orgtbl-setup ()
  12152. "Setup orgtbl keymaps."
  12153. (let ((nfunc 0)
  12154. (bindings
  12155. (list
  12156. '([(meta shift left)] org-table-delete-column)
  12157. '([(meta left)] org-table-move-column-left)
  12158. '([(meta right)] org-table-move-column-right)
  12159. '([(meta shift right)] org-table-insert-column)
  12160. '([(meta shift up)] org-table-kill-row)
  12161. '([(meta shift down)] org-table-insert-row)
  12162. '([(meta up)] org-table-move-row-up)
  12163. '([(meta down)] org-table-move-row-down)
  12164. '("\C-c\C-w" org-table-cut-region)
  12165. '("\C-c\M-w" org-table-copy-region)
  12166. '("\C-c\C-y" org-table-paste-rectangle)
  12167. '("\C-c-" org-table-insert-hline)
  12168. ; '([(shift tab)] org-table-previous-field)
  12169. '("\C-m" org-table-next-row)
  12170. (list (org-key 'S-return) 'org-table-copy-down)
  12171. '([(meta return)] org-table-wrap-region)
  12172. '("\C-c\C-q" org-table-wrap-region)
  12173. '("\C-c?" org-table-current-column)
  12174. '("\C-c " org-table-blank-field)
  12175. '("\C-c+" org-table-sum)
  12176. '("\C-c=" org-table-eval-formula)
  12177. '("\C-c'" org-table-edit-formulas)
  12178. '("\C-c`" org-table-edit-field)
  12179. '("\C-c*" org-table-recalculate)
  12180. '("\C-c|" org-table-create-or-convert-from-region)
  12181. '("\C-c^" org-table-sort-lines)
  12182. '([(control ?#)] org-table-rotate-recalc-marks)))
  12183. elt key fun cmd)
  12184. (while (setq elt (pop bindings))
  12185. (setq nfunc (1+ nfunc))
  12186. (setq key (car elt)
  12187. fun (nth 1 elt)
  12188. cmd (orgtbl-make-binding fun nfunc key))
  12189. (define-key orgtbl-mode-map key cmd))
  12190. ;; Special treatment needed for TAB and RET
  12191. (define-key orgtbl-mode-map [(return)]
  12192. (orgtbl-make-binding 'orgtbl-ret 100 [(return)] "\C-m"))
  12193. (define-key orgtbl-mode-map "\C-m"
  12194. (orgtbl-make-binding 'orgtbl-ret 101 "\C-m" [(return)]))
  12195. (define-key orgtbl-mode-map [(tab)]
  12196. (orgtbl-make-binding 'orgtbl-tab 102 [(tab)] "\C-i"))
  12197. (define-key orgtbl-mode-map "\C-i"
  12198. (orgtbl-make-binding 'orgtbl-tab 103 "\C-i" [(tab)])))
  12199. (define-key orgtbl-mode-map "\C-i"
  12200. (orgtbl-make-binding 'orgtbl-tab 104 [(shift tab)]))
  12201. (define-key orgtbl-mode-map "\C-c\C-c"
  12202. (orgtbl-make-binding 'org-ctrl-c-ctrl-c 105 "\C-c\C-c"))
  12203. (when orgtbl-optimized
  12204. ;; If the user wants maximum table support, we need to hijack
  12205. ;; some standard editing functions
  12206. (org-remap orgtbl-mode-map
  12207. 'self-insert-command 'orgtbl-self-insert-command
  12208. 'delete-char 'org-delete-char
  12209. 'delete-backward-char 'org-delete-backward-char)
  12210. (define-key orgtbl-mode-map "|" 'org-force-self-insert))
  12211. (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
  12212. '("OrgTbl"
  12213. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
  12214. ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
  12215. ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
  12216. ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
  12217. "--"
  12218. ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
  12219. ["Edit Field" org-table-edit-field :active (org-at-table-p) :keys "C-c ` "]
  12220. ["Copy Field from Above"
  12221. org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
  12222. "--"
  12223. ("Column"
  12224. ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
  12225. ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
  12226. ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
  12227. ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"]
  12228. "--"
  12229. ["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])
  12230. ("Row"
  12231. ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
  12232. ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
  12233. ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
  12234. ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
  12235. ["Sort lines in region" org-table-sort-lines (org-at-table-p) :keys "C-c ^"]
  12236. "--"
  12237. ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
  12238. ("Rectangle"
  12239. ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
  12240. ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
  12241. ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
  12242. ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
  12243. "--"
  12244. ["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
  12245. ["Set Named Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  12246. ["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
  12247. ["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
  12248. ["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
  12249. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
  12250. ["Sum Column/Rectangle" org-table-sum
  12251. :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
  12252. ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
  12253. ["Debug Formulas"
  12254. (setq org-table-formula-debug (not org-table-formula-debug))
  12255. :style toggle :selected org-table-formula-debug]
  12256. ))
  12257. t)
  12258. (defun orgtbl-tab (arg)
  12259. "Justification and field motion for `orgtbl-mode'."
  12260. (interactive "P")
  12261. (if arg (org-table-edit-field t)
  12262. (org-table-justify-field-maybe)
  12263. (org-table-next-field)))
  12264. (defun orgtbl-ret ()
  12265. "Justification and field motion for `orgtbl-mode'."
  12266. (interactive)
  12267. (org-table-justify-field-maybe)
  12268. (org-table-next-row))
  12269. (defun orgtbl-self-insert-command (N)
  12270. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  12271. If the cursor is in a table looking at whitespace, the whitespace is
  12272. overwritten, and the table is not marked as requiring realignment."
  12273. (interactive "p")
  12274. (if (and (org-at-table-p)
  12275. (or
  12276. (and org-table-auto-blank-field
  12277. (member last-command
  12278. '(orgtbl-hijacker-command-100
  12279. orgtbl-hijacker-command-101
  12280. orgtbl-hijacker-command-102
  12281. orgtbl-hijacker-command-103
  12282. orgtbl-hijacker-command-104
  12283. orgtbl-hijacker-command-105))
  12284. (org-table-blank-field))
  12285. t)
  12286. (eq N 1)
  12287. (looking-at "[^|\n]* +|"))
  12288. (let (org-table-may-need-update)
  12289. (goto-char (1- (match-end 0)))
  12290. (delete-backward-char 1)
  12291. (goto-char (match-beginning 0))
  12292. (self-insert-command N))
  12293. (setq org-table-may-need-update t)
  12294. (let (orgtbl-mode)
  12295. (call-interactively (key-binding (vector last-input-event))))))
  12296. (defun org-force-self-insert (N)
  12297. "Needed to enforce self-insert under remapping."
  12298. (interactive "p")
  12299. (self-insert-command N))
  12300. ;;; Exporting
  12301. (defconst org-level-max 20)
  12302. (defvar org-export-html-preamble nil
  12303. "Preamble, to be inserted just after <body>. Set by publishing functions.")
  12304. (defvar org-export-html-postamble nil
  12305. "Preamble, to be inserted just before </body>. Set by publishing functions.")
  12306. (defvar org-export-html-auto-preamble t
  12307. "Should default preamble be inserted? Set by publishing functions.")
  12308. (defvar org-export-html-auto-postamble t
  12309. "Should default postamble be inserted? Set by publishing functions.")
  12310. (defconst org-export-plist-vars
  12311. '((:language . org-export-default-language)
  12312. (:headline-levels . org-export-headline-levels)
  12313. (:section-numbers . org-export-with-section-numbers)
  12314. (:table-of-contents . org-export-with-toc)
  12315. (:archived-trees . org-export-with-archived-trees)
  12316. (:emphasize . org-export-with-emphasize)
  12317. (:sub-superscript . org-export-with-sub-superscripts)
  12318. (:TeX-macros . org-export-with-TeX-macros)
  12319. (:LaTeX-fragments . org-export-with-LaTeX-fragments)
  12320. (:fixed-width . org-export-with-fixed-width)
  12321. (:timestamps . org-export-with-timestamps)
  12322. (:tables . org-export-with-tables)
  12323. (:table-auto-headline . org-export-highlight-first-table-line)
  12324. (:style . org-export-html-style)
  12325. (:convert-org-links . org-export-html-link-org-files-as-html)
  12326. (:inline-images . org-export-html-inline-images)
  12327. (:expand-quoted-html . org-export-html-expand)
  12328. (:timestamp . org-export-html-with-timestamp)
  12329. (:publishing-directory . org-export-publishing-directory)
  12330. (:preamble . org-export-html-preamble)
  12331. (:postamble . org-export-html-postamble)
  12332. (:auto-preamble . org-export-html-auto-preamble)
  12333. (:auto-postamble . org-export-html-auto-postamble)
  12334. (:author . user-full-name)
  12335. (:email . user-mail-address)))
  12336. (defun org-default-export-plist ()
  12337. "Return the property list with default settings for the export variables."
  12338. (let ((l org-export-plist-vars) rtn e)
  12339. (while (setq e (pop l))
  12340. (setq rtn (cons (car e) (cons (symbol-value (cdr e)) rtn))))
  12341. rtn))
  12342. (defun org-infile-export-plist ()
  12343. "Return the property list with file-local settings for export."
  12344. (save-excursion
  12345. (goto-char 0)
  12346. (let ((re (org-make-options-regexp
  12347. '("TITLE" "AUTHOR" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE")))
  12348. p key val text options)
  12349. (while (re-search-forward re nil t)
  12350. (setq key (org-match-string-no-properties 1)
  12351. val (org-match-string-no-properties 2))
  12352. (cond
  12353. ((string-equal key "TITLE") (setq p (plist-put p :title val)))
  12354. ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
  12355. ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
  12356. ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
  12357. ((string-equal key "TEXT")
  12358. (setq text (if text (concat text "\n" val) val)))
  12359. ((string-equal key "OPTIONS") (setq options val))))
  12360. (setq p (plist-put p :text text))
  12361. (when options
  12362. (let ((op '(("H" . :headline-levels)
  12363. ("num" . :section-numbers)
  12364. ("toc" . :table-of-contents)
  12365. ("\\n" . :preserve-breaks)
  12366. ("@" . :expand-quoted-html)
  12367. (":" . :fixed-width)
  12368. ("|" . :tables)
  12369. ("^" . :sub-superscript)
  12370. ("*" . :emphasize)
  12371. ("TeX" . :TeX-macros)
  12372. ("LaTeX" . :LaTeX-fragments)))
  12373. o)
  12374. (while (setq o (pop op))
  12375. (if (string-match (concat (regexp-quote (car o))
  12376. ":\\([^ \t\n\r;,.]*\\)")
  12377. options)
  12378. (setq p (plist-put p (cdr o)
  12379. (car (read-from-string
  12380. (match-string 1 options)))))))))
  12381. p)))
  12382. (defun org-combine-plists (&rest plists)
  12383. "Create a single property list from all plists in PLISTS.
  12384. The process starts by copying the last list, and then setting properties
  12385. from the other lists. Settings in the first list are the most significant
  12386. ones and overrule settings in the other lists."
  12387. (let ((rtn (copy-sequence (pop plists)))
  12388. p v ls)
  12389. (while plists
  12390. (setq ls (pop plists))
  12391. (while ls
  12392. (setq p (pop ls) v (pop ls))
  12393. (setq rtn (plist-put rtn p v))))
  12394. rtn))
  12395. (defun org-export-directory (type plist)
  12396. (let* ((val (plist-get plist :publishing-directory))
  12397. (dir (if (listp val)
  12398. (or (cdr (assoc type val)) ".")
  12399. val)))
  12400. dir))
  12401. (defun org-export-find-first-heading-line (list)
  12402. "Remove all lines from LIST which are before the first headline."
  12403. (let ((orig-list list)
  12404. (re (concat "^" outline-regexp)))
  12405. (while (and list
  12406. (not (string-match re (car list))))
  12407. (pop list))
  12408. (or list orig-list)))
  12409. (defun org-skip-comments (lines)
  12410. "Skip lines starting with \"#\" and subtrees starting with COMMENT."
  12411. (let ((re1 (concat "^\\(\\*+\\)[ \t]+" org-comment-string))
  12412. (re2 "^\\(\\*+\\)[ \t\n\r]")
  12413. rtn line level)
  12414. (while (setq line (pop lines))
  12415. (cond
  12416. ((and (string-match re1 line)
  12417. (setq level (- (match-end 1) (match-beginning 1))))
  12418. ;; Beginning of a COMMENT subtree. Skip it.
  12419. (while (and (setq line (pop lines))
  12420. (or (not (string-match re2 line))
  12421. (> (- (match-end 1) (match-beginning 1)) level))))
  12422. (setq lines (cons line lines)))
  12423. ((string-match "^#" line)
  12424. ;; an ordinary comment line
  12425. )
  12426. ((and org-export-table-remove-special-lines
  12427. (string-match "^[ \t]*|" line)
  12428. (or (string-match "^[ \t]*| *[!_^] *|" line)
  12429. (and (string-match "| *<[0-9]+> *|" line)
  12430. (not (string-match "| *[^ <|]" line)))))
  12431. ;; a special table line that should be removed
  12432. )
  12433. (t (setq rtn (cons line rtn)))))
  12434. (nreverse rtn)))
  12435. (defun org-export (&optional arg)
  12436. (interactive)
  12437. (let ((help "[t] insert the export option template
  12438. \[v] limit export to visible part of outline tree
  12439. \[a] export as ASCII
  12440. \[h] export as HTML
  12441. \[b] export as HTML and browse immediately
  12442. \[x] export as XOXO
  12443. \[i] export current file as iCalendar file
  12444. \[I] export all agenda files as iCalendar files
  12445. \[c] export agenda files into combined iCalendar file
  12446. \[F] publish current file
  12447. \[P] publish current project
  12448. \[X] publish... (project will be prompted for)
  12449. \[A] publish all projects")
  12450. (cmds
  12451. '((?t . org-insert-export-options-template)
  12452. (?v . org-export-visible)
  12453. (?a . org-export-as-ascii)
  12454. (?h . org-export-as-html)
  12455. (?b . org-export-as-html-and-open)
  12456. (?x . org-export-as-xoxo)
  12457. (?i . org-export-icalendar-this-file)
  12458. (?I . org-export-icalendar-all-agenda-files)
  12459. (?c . org-export-icalendar-combine-agenda-files)
  12460. (?F . org-publish-current-file)
  12461. (?P . org-publish-current-project)
  12462. (?X . org-publish)
  12463. (?A . org-publish-all)))
  12464. r1 r2 ass)
  12465. (save-window-excursion
  12466. (delete-other-windows)
  12467. (with-output-to-temp-buffer "*Org Export/Publishing Help*"
  12468. (princ help))
  12469. (message "Select command: ")
  12470. (setq r1 (read-char-exclusive)))
  12471. (setq r2 (if (< r1 27) (+ r1 96) r1))
  12472. (if (setq ass (assq r2 cmds))
  12473. (call-interactively (cdr ass))
  12474. (error "No command associated with key %c" r1))))
  12475. ;; ASCII
  12476. (defconst org-html-entities
  12477. '(("nbsp")
  12478. ("iexcl")
  12479. ("cent")
  12480. ("pound")
  12481. ("curren")
  12482. ("yen")
  12483. ("brvbar")
  12484. ("sect")
  12485. ("uml")
  12486. ("copy")
  12487. ("ordf")
  12488. ("laquo")
  12489. ("not")
  12490. ("shy")
  12491. ("reg")
  12492. ("macr")
  12493. ("deg")
  12494. ("plusmn")
  12495. ("sup2")
  12496. ("sup3")
  12497. ("acute")
  12498. ("micro")
  12499. ("para")
  12500. ("middot")
  12501. ("odot"."o")
  12502. ("star"."*")
  12503. ("cedil")
  12504. ("sup1")
  12505. ("ordm")
  12506. ("raquo")
  12507. ("frac14")
  12508. ("frac12")
  12509. ("frac34")
  12510. ("iquest")
  12511. ("Agrave")
  12512. ("Aacute")
  12513. ("Acirc")
  12514. ("Atilde")
  12515. ("Auml")
  12516. ("Aring") ("AA"."&Aring;")
  12517. ("AElig")
  12518. ("Ccedil")
  12519. ("Egrave")
  12520. ("Eacute")
  12521. ("Ecirc")
  12522. ("Euml")
  12523. ("Igrave")
  12524. ("Iacute")
  12525. ("Icirc")
  12526. ("Iuml")
  12527. ("ETH")
  12528. ("Ntilde")
  12529. ("Ograve")
  12530. ("Oacute")
  12531. ("Ocirc")
  12532. ("Otilde")
  12533. ("Ouml")
  12534. ("times")
  12535. ("Oslash")
  12536. ("Ugrave")
  12537. ("Uacute")
  12538. ("Ucirc")
  12539. ("Uuml")
  12540. ("Yacute")
  12541. ("THORN")
  12542. ("szlig")
  12543. ("agrave")
  12544. ("aacute")
  12545. ("acirc")
  12546. ("atilde")
  12547. ("auml")
  12548. ("aring")
  12549. ("aelig")
  12550. ("ccedil")
  12551. ("egrave")
  12552. ("eacute")
  12553. ("ecirc")
  12554. ("euml")
  12555. ("igrave")
  12556. ("iacute")
  12557. ("icirc")
  12558. ("iuml")
  12559. ("eth")
  12560. ("ntilde")
  12561. ("ograve")
  12562. ("oacute")
  12563. ("ocirc")
  12564. ("otilde")
  12565. ("ouml")
  12566. ("divide")
  12567. ("oslash")
  12568. ("ugrave")
  12569. ("uacute")
  12570. ("ucirc")
  12571. ("uuml")
  12572. ("yacute")
  12573. ("thorn")
  12574. ("yuml")
  12575. ("fnof")
  12576. ("Alpha")
  12577. ("Beta")
  12578. ("Gamma")
  12579. ("Delta")
  12580. ("Epsilon")
  12581. ("Zeta")
  12582. ("Eta")
  12583. ("Theta")
  12584. ("Iota")
  12585. ("Kappa")
  12586. ("Lambda")
  12587. ("Mu")
  12588. ("Nu")
  12589. ("Xi")
  12590. ("Omicron")
  12591. ("Pi")
  12592. ("Rho")
  12593. ("Sigma")
  12594. ("Tau")
  12595. ("Upsilon")
  12596. ("Phi")
  12597. ("Chi")
  12598. ("Psi")
  12599. ("Omega")
  12600. ("alpha")
  12601. ("beta")
  12602. ("gamma")
  12603. ("delta")
  12604. ("epsilon")
  12605. ("varepsilon"."&epsilon;")
  12606. ("zeta")
  12607. ("eta")
  12608. ("theta")
  12609. ("iota")
  12610. ("kappa")
  12611. ("lambda")
  12612. ("mu")
  12613. ("nu")
  12614. ("xi")
  12615. ("omicron")
  12616. ("pi")
  12617. ("rho")
  12618. ("sigmaf") ("varsigma"."&sigmaf;")
  12619. ("sigma")
  12620. ("tau")
  12621. ("upsilon")
  12622. ("phi")
  12623. ("chi")
  12624. ("psi")
  12625. ("omega")
  12626. ("thetasym") ("vartheta"."&thetasym;")
  12627. ("upsih")
  12628. ("piv")
  12629. ("bull") ("bullet"."&bull;")
  12630. ("hellip") ("dots"."&hellip;")
  12631. ("prime")
  12632. ("Prime")
  12633. ("oline")
  12634. ("frasl")
  12635. ("weierp")
  12636. ("image")
  12637. ("real")
  12638. ("trade")
  12639. ("alefsym")
  12640. ("larr") ("leftarrow"."&larr;") ("gets"."&larr;")
  12641. ("uarr") ("uparrow"."&uarr;")
  12642. ("rarr") ("to"."&rarr;") ("rightarrow"."&rarr;")
  12643. ("darr")("downarrow"."&darr;")
  12644. ("harr") ("leftrightarrow"."&harr;")
  12645. ("crarr") ("hookleftarrow"."&crarr;") ; has round hook, not quite CR
  12646. ("lArr") ("Leftarrow"."&lArr;")
  12647. ("uArr") ("Uparrow"."&uArr;")
  12648. ("rArr") ("Rightarrow"."&rArr;")
  12649. ("dArr") ("Downarrow"."&dArr;")
  12650. ("hArr") ("Leftrightarrow"."&hArr;")
  12651. ("forall")
  12652. ("part") ("partial"."&part;")
  12653. ("exist") ("exists"."&exist;")
  12654. ("empty") ("emptyset"."&empty;")
  12655. ("nabla")
  12656. ("isin") ("in"."&isin;")
  12657. ("notin")
  12658. ("ni")
  12659. ("prod")
  12660. ("sum")
  12661. ("minus")
  12662. ("lowast") ("ast"."&lowast;")
  12663. ("radic")
  12664. ("prop") ("proptp"."&prop;")
  12665. ("infin") ("infty"."&infin;")
  12666. ("ang") ("angle"."&ang;")
  12667. ("and") ("vee"."&and;")
  12668. ("or") ("wedge"."&or;")
  12669. ("cap")
  12670. ("cup")
  12671. ("int")
  12672. ("there4")
  12673. ("sim")
  12674. ("cong") ("simeq"."&cong;")
  12675. ("asymp")("approx"."&asymp;")
  12676. ("ne") ("neq"."&ne;")
  12677. ("equiv")
  12678. ("le")
  12679. ("ge")
  12680. ("sub") ("subset"."&sub;")
  12681. ("sup") ("supset"."&sup;")
  12682. ("nsub")
  12683. ("sube")
  12684. ("supe")
  12685. ("oplus")
  12686. ("otimes")
  12687. ("perp")
  12688. ("sdot") ("cdot"."&sdot;")
  12689. ("lceil")
  12690. ("rceil")
  12691. ("lfloor")
  12692. ("rfloor")
  12693. ("lang")
  12694. ("rang")
  12695. ("loz") ("Diamond"."&loz;")
  12696. ("spades") ("spadesuit"."&spades;")
  12697. ("clubs") ("clubsuit"."&clubs;")
  12698. ("hearts") ("diamondsuit"."&hearts;")
  12699. ("diams") ("diamondsuit"."&diams;")
  12700. ("quot")
  12701. ("amp")
  12702. ("lt")
  12703. ("gt")
  12704. ("OElig")
  12705. ("oelig")
  12706. ("Scaron")
  12707. ("scaron")
  12708. ("Yuml")
  12709. ("circ")
  12710. ("tilde")
  12711. ("ensp")
  12712. ("emsp")
  12713. ("thinsp")
  12714. ("zwnj")
  12715. ("zwj")
  12716. ("lrm")
  12717. ("rlm")
  12718. ("ndash")
  12719. ("mdash")
  12720. ("lsquo")
  12721. ("rsquo")
  12722. ("sbquo")
  12723. ("ldquo")
  12724. ("rdquo")
  12725. ("bdquo")
  12726. ("dagger")
  12727. ("Dagger")
  12728. ("permil")
  12729. ("lsaquo")
  12730. ("rsaquo")
  12731. ("euro")
  12732. ("arccos"."arccos")
  12733. ("arcsin"."arcsin")
  12734. ("arctan"."arctan")
  12735. ("arg"."arg")
  12736. ("cos"."cos")
  12737. ("cosh"."cosh")
  12738. ("cot"."cot")
  12739. ("coth"."coth")
  12740. ("csc"."csc")
  12741. ("deg"."deg")
  12742. ("det"."det")
  12743. ("dim"."dim")
  12744. ("exp"."exp")
  12745. ("gcd"."gcd")
  12746. ("hom"."hom")
  12747. ("inf"."inf")
  12748. ("ker"."ker")
  12749. ("lg"."lg")
  12750. ("lim"."lim")
  12751. ("liminf"."liminf")
  12752. ("limsup"."limsup")
  12753. ("ln"."ln")
  12754. ("log"."log")
  12755. ("max"."max")
  12756. ("min"."min")
  12757. ("Pr"."Pr")
  12758. ("sec"."sec")
  12759. ("sin"."sin")
  12760. ("sinh"."sinh")
  12761. ("sup"."sup")
  12762. ("tan"."tan")
  12763. ("tanh"."tanh")
  12764. )
  12765. "Entities for TeX->HTML translation.
  12766. Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
  12767. \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
  12768. In that case, \"\\ent\" will be translated to \"&other;\".
  12769. The list contains HTML entities for Latin-1, Greek and other symbols.
  12770. It is supplemented by a number of commonly used TeX macros with appropriate
  12771. translations. There is currently no way for users to extend this.")
  12772. (defun org-cleaned-string-for-export (string &rest parameters)
  12773. "Cleanup a buffer substring so that links can be created safely."
  12774. (interactive)
  12775. (let* ((re-radio (and org-target-link-regexp
  12776. (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)")))
  12777. (re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
  12778. (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
  12779. (re-archive (concat ":" org-archive-tag ":"))
  12780. rtn)
  12781. (save-excursion
  12782. (set-buffer (get-buffer-create " org-mode-tmp"))
  12783. (erase-buffer)
  12784. (insert string)
  12785. (let ((org-inhibit-startup t)) (org-mode))
  12786. ;; Get rid of archived trees
  12787. (when (not (eq org-export-with-archived-trees t))
  12788. (goto-char (point-min))
  12789. (while (re-search-forward re-archive nil t)
  12790. (if (not (org-on-heading-p))
  12791. (org-end-of-subtree t)
  12792. (beginning-of-line 1)
  12793. (delete-region
  12794. (if org-export-with-archived-trees (1+ (point-at-eol)) (point))
  12795. (org-end-of-subtree t)))))
  12796. ;; Find targets in comments and move them out of comments,
  12797. ;; but mark them as targets that should be invisible
  12798. (goto-char (point-min))
  12799. (while (re-search-forward "^#.*?\\(<<<?[^>\r\n]+>>>?\\).*" nil t)
  12800. (replace-match "\\1(INVISIBLE)"))
  12801. ;; Find matches for radio targets and turn them into internal links
  12802. (goto-char (point-min))
  12803. (when re-radio
  12804. (while (re-search-forward re-radio nil t)
  12805. (replace-match "\\1[[\\2]]")))
  12806. ;; Find all links that contain a newline and put them into a single line
  12807. (goto-char (point-min))
  12808. (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
  12809. (replace-match "\\1 \\3")
  12810. (goto-char (match-beginning 0)))
  12811. ;; Convert LaTeX fragments to images
  12812. (when (memq :LaTeX-fragments parameters)
  12813. (org-format-latex
  12814. (concat "ltxpng/" (file-name-sans-extension
  12815. (file-name-nondirectory
  12816. org-current-export-file)))
  12817. org-current-export-dir nil "Creating LaTeX image %s"))
  12818. (message "Exporting...")
  12819. ;; Normalize links: Convert angle and plain links into bracket links
  12820. (goto-char (point-min))
  12821. (while (re-search-forward re-plain-link nil t)
  12822. (replace-match
  12823. (concat
  12824. (match-string 1) "[[" (match-string 2) ":" (match-string 3) "]]")
  12825. t t))
  12826. (goto-char (point-min))
  12827. (while (re-search-forward re-angle-link nil t)
  12828. (replace-match
  12829. (concat
  12830. (match-string 1) "[[" (match-string 2) ":" (match-string 3) "]]")
  12831. t t))
  12832. ;; Find multiline emphasis and put them into single line
  12833. (when (memq :emph-multiline parameters)
  12834. (goto-char (point-min))
  12835. (while (re-search-forward org-emph-re nil t)
  12836. (subst-char-in-region (match-beginning 0) (match-end 0) ?\n ?\ t)
  12837. (goto-char (1- (match-end 0)))))
  12838. ;; Remove comments
  12839. (goto-char (point-min))
  12840. (while (re-search-forward "^#.*\n?" nil t)
  12841. (replace-match ""))
  12842. (setq rtn (buffer-string)))
  12843. (kill-buffer " org-mode-tmp")
  12844. rtn))
  12845. (defun org-solidify-link-text (s &optional alist)
  12846. "Take link text and make a safe target out of it."
  12847. (save-match-data
  12848. (let* ((rtn
  12849. (mapconcat
  12850. 'identity
  12851. (org-split-string s "[ \t\r\n]+") "--"))
  12852. (a (assoc rtn alist)))
  12853. (or (cdr a) rtn))))
  12854. (defun org-convert-to-odd-levels ()
  12855. "Convert an org-mode file with all levels allowed to one with odd levels.
  12856. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  12857. level 5 etc."
  12858. (interactive)
  12859. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  12860. (let ((org-odd-levels-only nil) n)
  12861. (save-excursion
  12862. (goto-char (point-min))
  12863. (while (re-search-forward "^\\*\\*+" nil t)
  12864. (setq n (1- (length (match-string 0))))
  12865. (while (>= (setq n (1- n)) 0)
  12866. (org-demote))
  12867. (end-of-line 1))))))
  12868. (defun org-convert-to-oddeven-levels ()
  12869. "Convert an org-mode file with only odd levels to one with odd and even levels.
  12870. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  12871. section with an even level, conversion would destroy the structure of the file. An error
  12872. is signaled in this case."
  12873. (interactive)
  12874. (goto-char (point-min))
  12875. ;; First check if there are no even levels
  12876. (when (re-search-forward "^\\(\\*\\*\\)+[^*]" nil t)
  12877. (org-show-hierarchy-above t)
  12878. (error "Not all levels are odd in this file. Conversion not possible."))
  12879. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  12880. (let ((org-odd-levels-only nil) n)
  12881. (save-excursion
  12882. (goto-char (point-min))
  12883. (while (re-search-forward "^\\*\\*+" nil t)
  12884. (setq n (/ (length (match-string 0)) 2))
  12885. (while (>= (setq n (1- n)) 0)
  12886. (org-promote))
  12887. (end-of-line 1))))))
  12888. (defun org-tr-level (n)
  12889. "Make N odd if required."
  12890. (if org-odd-levels-only (1+ (/ n 2)) n))
  12891. (defvar org-last-level nil) ; dynamically scoped variable
  12892. (defvar org-ascii-current-indentation nil) ; For communication
  12893. (defun org-export-as-ascii (arg)
  12894. "Export the outline as a pretty ASCII file.
  12895. If there is an active region, export only the region.
  12896. The prefix ARG specifies how many levels of the outline should become
  12897. underlined headlines. The default is 3."
  12898. (interactive "P")
  12899. (setq-default org-todo-line-regexp org-todo-line-regexp)
  12900. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  12901. (org-infile-export-plist)))
  12902. (region
  12903. (buffer-substring
  12904. (if (org-region-active-p) (region-beginning) (point-min))
  12905. (if (org-region-active-p) (region-end) (point-max))))
  12906. (lines (org-export-find-first-heading-line
  12907. (org-skip-comments
  12908. (org-split-string
  12909. (org-cleaned-string-for-export region)
  12910. "[\r\n]"))))
  12911. (org-ascii-current-indentation '(0 . 0))
  12912. (org-startup-with-deadline-check nil)
  12913. (level 0) line txt
  12914. (umax nil)
  12915. (case-fold-search nil)
  12916. (filename (concat (file-name-as-directory
  12917. (org-export-directory :ascii opt-plist))
  12918. (file-name-sans-extension
  12919. (file-name-nondirectory buffer-file-name))
  12920. ".txt"))
  12921. (buffer (find-file-noselect filename))
  12922. (levels-open (make-vector org-level-max nil))
  12923. (odd org-odd-levels-only)
  12924. (date (format-time-string "%Y/%m/%d" (current-time)))
  12925. (time (format-time-string "%X" (org-current-time)))
  12926. (author (plist-get opt-plist :author))
  12927. (title (or (plist-get opt-plist :title)
  12928. (file-name-sans-extension
  12929. (file-name-nondirectory buffer-file-name))))
  12930. (email (plist-get opt-plist :email))
  12931. (language (plist-get opt-plist :language))
  12932. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  12933. (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
  12934. (text nil)
  12935. (todo nil)
  12936. (lang-words nil))
  12937. (setq org-last-level 1)
  12938. (org-init-section-numbers)
  12939. (find-file-noselect filename)
  12940. (setq lang-words (or (assoc language org-export-language-setup)
  12941. (assoc "en" org-export-language-setup)))
  12942. (if org-export-ascii-show-new-buffer
  12943. (switch-to-buffer-other-window buffer)
  12944. (set-buffer buffer))
  12945. (erase-buffer)
  12946. (fundamental-mode)
  12947. ;; create local variables for all options, to make sure all called
  12948. ;; functions get the correct information
  12949. (mapcar (lambda (x)
  12950. (set (make-local-variable (cdr x))
  12951. (plist-get opt-plist (car x))))
  12952. org-export-plist-vars)
  12953. (org-set-local 'org-odd-levels-only odd)
  12954. (setq umax (if arg (prefix-numeric-value arg)
  12955. org-export-headline-levels))
  12956. ;; File header
  12957. (if title (org-insert-centered title ?=))
  12958. (insert "\n")
  12959. (if (or author email)
  12960. (insert (concat (nth 1 lang-words) ": " (or author "")
  12961. (if email (concat " <" email ">") "")
  12962. "\n")))
  12963. (if (and date time)
  12964. (insert (concat (nth 2 lang-words) ": " date " " time "\n")))
  12965. (if text (insert (concat (org-html-expand-for-ascii text) "\n\n")))
  12966. (insert "\n\n")
  12967. (if org-export-with-toc
  12968. (progn
  12969. (insert (nth 3 lang-words) "\n"
  12970. (make-string (length (nth 3 lang-words)) ?=) "\n")
  12971. (mapcar '(lambda (line)
  12972. (if (string-match org-todo-line-regexp
  12973. line)
  12974. ;; This is a headline
  12975. (progn
  12976. (setq level (- (match-end 1) (match-beginning 1))
  12977. level (org-tr-level level)
  12978. txt (match-string 3 line)
  12979. todo
  12980. (or (and org-export-mark-todo-in-toc
  12981. (match-beginning 2)
  12982. (not (equal (match-string 2 line)
  12983. org-done-string)))
  12984. ; TODO, not DONE
  12985. (and org-export-mark-todo-in-toc
  12986. (= level umax)
  12987. (org-search-todo-below
  12988. line lines level))))
  12989. (setq txt (org-html-expand-for-ascii txt))
  12990. (if (and (memq org-export-with-tags '(not-in-toc nil))
  12991. (string-match "[ \t]+:[a-zA-Z0-9_@:]+:[ \t]*$" txt))
  12992. (setq txt (replace-match "" t t txt)))
  12993. (if (string-match quote-re0 txt)
  12994. (setq txt (replace-match "" t t txt)))
  12995. (if org-export-with-section-numbers
  12996. (setq txt (concat (org-section-number level)
  12997. " " txt)))
  12998. (if (<= level umax)
  12999. (progn
  13000. (insert
  13001. (make-string (* (1- level) 4) ?\ )
  13002. (format (if todo "%s (*)\n" "%s\n") txt))
  13003. (setq org-last-level level))
  13004. ))))
  13005. lines)))
  13006. (org-init-section-numbers)
  13007. (while (setq line (pop lines))
  13008. ;; Remove the quoted HTML tags.
  13009. (setq line (org-html-expand-for-ascii line))
  13010. ;; Remove targets
  13011. (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line)
  13012. (setq line (replace-match "" t t line)))
  13013. ;; Replace internal links
  13014. (while (string-match org-bracket-link-regexp line)
  13015. (setq line (replace-match
  13016. (if (match-end 3) "[\\3]" "[\\1]")
  13017. t nil line)))
  13018. (cond
  13019. ((string-match "^\\(\\*+\\)[ \t]*\\(.*\\)" line)
  13020. ;; a Headline
  13021. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  13022. txt (match-string 2 line))
  13023. (org-ascii-level-start level txt umax lines))
  13024. (t
  13025. (insert (org-fix-indentation line org-ascii-current-indentation) "\n"))))
  13026. (normal-mode)
  13027. (save-buffer)
  13028. ;; remove display and invisible chars
  13029. (let (beg end)
  13030. (goto-char (point-min))
  13031. (while (setq beg (next-single-property-change (point) 'display))
  13032. (setq end (next-single-property-change beg 'display))
  13033. (delete-region beg end)
  13034. (goto-char beg)
  13035. (insert "=>"))
  13036. (goto-char (point-min))
  13037. (while (setq beg (next-single-property-change (point) 'org-cwidth))
  13038. (setq end (next-single-property-change beg 'org-cwidth))
  13039. (delete-region beg end)
  13040. (goto-char beg)))
  13041. (goto-char (point-min))))
  13042. (defun org-search-todo-below (line lines level)
  13043. "Search the subtree below LINE for any TODO entries."
  13044. (let ((rest (cdr (memq line lines)))
  13045. (re org-todo-line-regexp)
  13046. line lv todo)
  13047. (catch 'exit
  13048. (while (setq line (pop rest))
  13049. (if (string-match re line)
  13050. (progn
  13051. (setq lv (- (match-end 1) (match-beginning 1))
  13052. todo (and (match-beginning 2)
  13053. (not (equal (match-string 2 line)
  13054. org-done-string))))
  13055. ; TODO, not DONE
  13056. (if (<= lv level) (throw 'exit nil))
  13057. (if todo (throw 'exit t))))))))
  13058. (defun org-html-expand-for-ascii (line)
  13059. "Handle quoted HTML for ASCII export."
  13060. (if org-export-html-expand
  13061. (while (string-match "@<[^<>\n]*>" line)
  13062. ;; We just remove the tags for now.
  13063. (setq line (replace-match "" nil nil line))))
  13064. line)
  13065. (defun org-insert-centered (s &optional underline)
  13066. "Insert the string S centered and underline it with character UNDERLINE."
  13067. (let ((ind (max (/ (- 80 (string-width s)) 2) 0)))
  13068. (insert (make-string ind ?\ ) s "\n")
  13069. (if underline
  13070. (insert (make-string ind ?\ )
  13071. (make-string (string-width s) underline)
  13072. "\n"))))
  13073. (defun org-ascii-level-start (level title umax &optional lines)
  13074. "Insert a new level in ASCII export."
  13075. (let (char (n (- level umax 1)) (ind 0))
  13076. (if (> level umax)
  13077. (progn
  13078. (insert (make-string (* 2 n) ?\ )
  13079. (char-to-string (nth (% n (length org-export-ascii-bullets))
  13080. org-export-ascii-bullets))
  13081. " " title "\n")
  13082. ;; find the indentation of the next non-empty line
  13083. (catch 'stop
  13084. (while lines
  13085. (if (string-match "^\\*" (car lines)) (throw 'stop nil))
  13086. (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
  13087. (throw 'stop (setq ind (org-get-indentation (car lines)))))
  13088. (pop lines)))
  13089. (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
  13090. (if (or (not (equal (char-before) ?\n))
  13091. (not (equal (char-before (1- (point))) ?\n)))
  13092. (insert "\n"))
  13093. (setq char (nth (- umax level) (reverse org-export-ascii-underline)))
  13094. (unless org-export-with-tags
  13095. (if (string-match "[ \t]+\\(:[a-zA-Z0-9_@:]+:\\)[ \t]*$" title)
  13096. (setq title (replace-match "" t t title))))
  13097. (if org-export-with-section-numbers
  13098. (setq title (concat (org-section-number level) " " title)))
  13099. (insert title "\n" (make-string (string-width title) char) "\n")
  13100. (setq org-ascii-current-indentation '(0 . 0)))))
  13101. (defun org-export-visible (type arg)
  13102. "Create a copy of the visible part of the current buffer, and export it.
  13103. The copy is created in a temporary buffer and removed after use.
  13104. TYPE is the final key (as a string) that also select the export command in
  13105. the `C-c C-e' export dispatcher.
  13106. As a special case, if the you type SPC at the prompt, the temporary
  13107. org-mode file will not be removed but presented to you so that you can
  13108. continue to use it. The prefix arg ARG is passed through to the exporting
  13109. command."
  13110. (interactive
  13111. (list (progn
  13112. (message "Export visible: [a]SCII [h]tml [b]rowse HTML [x]OXO [ ]keep buffer")
  13113. (read-char-exclusive))
  13114. current-prefix-arg))
  13115. (if (not (member type '(?a ?\C-a ?b ?\C-b ?h ?x ?\ )))
  13116. (error "Invalid export key"))
  13117. (let* ((binding (cdr (assoc type
  13118. '((?a . org-export-as-ascii)
  13119. (?\C-a . org-export-as-ascii)
  13120. (?b . org-export-as-html-and-open)
  13121. (?\C-b . org-export-as-html-and-open)
  13122. (?h . org-export-as-html)
  13123. (?x . org-export-as-xoxo)))))
  13124. (keepp (equal type ?\ ))
  13125. (file buffer-file-name)
  13126. (buffer (get-buffer-create "*Org Export Visible*"))
  13127. s e)
  13128. (with-current-buffer buffer (erase-buffer))
  13129. (save-excursion
  13130. (setq s (goto-char (point-min)))
  13131. (while (not (= (point) (point-max)))
  13132. (goto-char (org-find-invisible))
  13133. (append-to-buffer buffer s (point))
  13134. (setq s (goto-char (org-find-visible))))
  13135. (goto-char (point-min))
  13136. (unless keepp
  13137. ;; Copy all comment lines to the end, to make sure #+ settings are
  13138. ;; still available for the second export step. Kind of a hack, but
  13139. ;; does do the trick.
  13140. (if (looking-at "#[^\r\n]*")
  13141. (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
  13142. (while (re-search-forward "[\n\r]#[^\n\r]*" nil t)
  13143. (append-to-buffer buffer (1+ (match-beginning 0))
  13144. (min (point-max) (1+ (match-end 0))))))
  13145. (set-buffer buffer)
  13146. (let ((buffer-file-name file)
  13147. (org-inhibit-startup t))
  13148. (org-mode)
  13149. (show-all)
  13150. (unless keepp (funcall binding arg))))
  13151. (if (not keepp)
  13152. (kill-buffer buffer)
  13153. (switch-to-buffer-other-window buffer)
  13154. (goto-char (point-min)))))
  13155. (defun org-find-visible ()
  13156. (let ((s (point)))
  13157. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  13158. (get-char-property s 'invisible)))
  13159. s))
  13160. (defun org-find-invisible ()
  13161. (let ((s (point)))
  13162. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  13163. (not (get-char-property s 'invisible))))
  13164. s))
  13165. ;; HTML
  13166. (defun org-get-current-options ()
  13167. "Return a string with current options as keyword options.
  13168. Does include HTML export options as well as TODO and CATEGORY stuff."
  13169. (format
  13170. "#+TITLE: %s
  13171. #+AUTHOR: %s
  13172. #+EMAIL: %s
  13173. #+LANGUAGE: %s
  13174. #+TEXT: Some descriptive text to be emitted. Several lines OK.
  13175. #+OPTIONS: H:%d num:%s toc:%s \\n:%s @:%s ::%s |:%s ^:%s *:%s TeX:%s LaTeX:%s
  13176. #+CATEGORY: %s
  13177. #+SEQ_TODO: %s
  13178. #+TYP_TODO: %s
  13179. #+STARTUP: %s %s %s %s %s %s
  13180. #+TAGS: %s
  13181. #+ARCHIVE: %s
  13182. "
  13183. (buffer-name) (user-full-name) user-mail-address org-export-default-language
  13184. org-export-headline-levels
  13185. org-export-with-section-numbers
  13186. org-export-with-toc
  13187. org-export-preserve-breaks
  13188. org-export-html-expand
  13189. org-export-with-fixed-width
  13190. org-export-with-tables
  13191. org-export-with-sub-superscripts
  13192. org-export-with-emphasize
  13193. org-export-with-TeX-macros
  13194. org-export-with-LaTeX-fragments
  13195. (file-name-nondirectory buffer-file-name)
  13196. (if (equal org-todo-interpretation 'sequence)
  13197. (mapconcat 'identity org-todo-keywords " ")
  13198. "TODO FEEDBACK VERIFY DONE")
  13199. (if (equal org-todo-interpretation 'type)
  13200. (mapconcat 'identity org-todo-keywords " ")
  13201. "Me Jason Marie DONE")
  13202. (cdr (assoc org-startup-folded
  13203. '((nil . "showall") (t . "overview") (content . "content"))))
  13204. (if org-startup-with-deadline-check "dlcheck" "nodlcheck")
  13205. (if org-odd-levels-only "odd" "oddeven")
  13206. (if org-hide-leading-stars "hidestars" "showstars")
  13207. (if org-startup-align-all-tables "align" "noalign")
  13208. (if org-log-done "logging" "nologging")
  13209. (or (mapconcat (lambda (x)
  13210. (cond
  13211. ((equal '(:startgroup) x) "{")
  13212. ((equal '(:endgroup) x) "}")
  13213. ((cdr x) (format "%s(%c)" (car x) (cdr x)))
  13214. (t (car x))))
  13215. (or org-tag-alist (org-get-buffer-tags)) " ") "")
  13216. org-archive-location
  13217. ))
  13218. (defun org-insert-export-options-template ()
  13219. "Insert into the buffer a template with information for exporting."
  13220. (interactive)
  13221. (if (not (bolp)) (newline))
  13222. (let ((s (org-get-current-options)))
  13223. (and (string-match "#\\+CATEGORY" s)
  13224. (setq s (substring s 0 (match-beginning 0))))
  13225. (insert s)))
  13226. (defun org-toggle-fixed-width-section (arg)
  13227. "Toggle the fixed-width export.
  13228. If there is no active region, the QUOTE keyword at the current headline is
  13229. inserted or removed. When present, it causes the text between this headline
  13230. and the next to be exported as fixed-width text, and unmodified.
  13231. If there is an active region, this command adds or removes a colon as the
  13232. first character of this line. If the first character of a line is a colon,
  13233. this line is also exported in fixed-width font."
  13234. (interactive "P")
  13235. (let* ((cc 0)
  13236. (regionp (org-region-active-p))
  13237. (beg (if regionp (region-beginning) (point)))
  13238. (end (if regionp (region-end)))
  13239. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  13240. (re "[ \t]*\\(:\\)")
  13241. off)
  13242. (if regionp
  13243. (save-excursion
  13244. (goto-char beg)
  13245. (setq cc (current-column))
  13246. (beginning-of-line 1)
  13247. (setq off (looking-at re))
  13248. (while (> nlines 0)
  13249. (setq nlines (1- nlines))
  13250. (beginning-of-line 1)
  13251. (cond
  13252. (arg
  13253. (move-to-column cc t)
  13254. (insert ":\n")
  13255. (forward-line -1))
  13256. ((and off (looking-at re))
  13257. (replace-match "" t t nil 1))
  13258. ((not off) (move-to-column cc t) (insert ":")))
  13259. (forward-line 1)))
  13260. (save-excursion
  13261. (org-back-to-heading)
  13262. (if (looking-at (concat outline-regexp
  13263. "\\( +\\<" org-quote-string "\\>\\)"))
  13264. (replace-match "" t t nil 1)
  13265. (if (looking-at outline-regexp)
  13266. (progn
  13267. (goto-char (match-end 0))
  13268. (insert " " org-quote-string))))))))
  13269. (defun org-export-as-html-and-open (arg)
  13270. "Export the outline as HTML and immediately open it with a browser.
  13271. If there is an active region, export only the region.
  13272. The prefix ARG specifies how many levels of the outline should become
  13273. headlines. The default is 3. Lower levels will become bulleted lists."
  13274. (interactive "P")
  13275. (org-export-as-html arg 'hidden)
  13276. (org-open-file buffer-file-name))
  13277. (defun org-export-as-html-batch ()
  13278. "Call `org-export-as-html', may be used in batch processing as
  13279. emacs --batch
  13280. --load=$HOME/lib/emacs/org.el
  13281. --eval \"(setq org-export-headline-levels 2)\"
  13282. --visit=MyFile --funcall org-export-as-html-batch"
  13283. (org-export-as-html org-export-headline-levels 'hidden))
  13284. (defun org-export-as-html (arg &optional hidden ext-plist)
  13285. "Export the outline as a pretty HTML file.
  13286. If there is an active region, export only the region.
  13287. The prefix ARG specifies how many levels of the outline should become
  13288. headlines. The default is 3. Lower levels will become bulleted lists.
  13289. When HIDDEN is non-nil, don't display the HTML buffer.
  13290. EXT-PLIST is a property list with external parameters overriding
  13291. org-mode's default settings, but still inferior to file-local settings."
  13292. (interactive "P")
  13293. (message "Exporting...")
  13294. (setq-default org-todo-line-regexp org-todo-line-regexp)
  13295. (setq-default org-deadline-line-regexp org-deadline-line-regexp)
  13296. (setq-default org-done-string org-done-string)
  13297. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  13298. ext-plist
  13299. (org-infile-export-plist)))
  13300. (style (plist-get opt-plist :style))
  13301. (link-validate (plist-get opt-plist :link-validation-function))
  13302. valid
  13303. (odd org-odd-levels-only)
  13304. (region-p (org-region-active-p))
  13305. (region
  13306. (buffer-substring
  13307. (if region-p (region-beginning) (point-min))
  13308. (if region-p (region-end) (point-max))))
  13309. ;; The following two are dynamically scoped into other
  13310. ;; routines below.
  13311. (org-current-export-dir (org-export-directory :html opt-plist))
  13312. (org-current-export-file buffer-file-name)
  13313. (all_lines
  13314. (org-skip-comments (org-split-string
  13315. (org-cleaned-string-for-export
  13316. region :emph-multiline
  13317. (if (plist-get opt-plist :LaTeX-fragments)
  13318. :LaTeX-fragments))
  13319. "[\r\n]")))
  13320. (lines (org-export-find-first-heading-line all_lines))
  13321. (level 0) (line "") (origline "") txt todo
  13322. (umax nil)
  13323. (filename (concat (file-name-as-directory
  13324. (org-export-directory :html opt-plist))
  13325. (file-name-sans-extension
  13326. (file-name-nondirectory buffer-file-name))
  13327. ".html"))
  13328. (current-dir (file-name-directory buffer-file-name))
  13329. (buffer (find-file-noselect filename))
  13330. (levels-open (make-vector org-level-max nil))
  13331. (date (format-time-string "%Y/%m/%d" (current-time)))
  13332. (time (format-time-string "%X" (org-current-time)))
  13333. (author (plist-get opt-plist :author))
  13334. (title (or (plist-get opt-plist :title)
  13335. (file-name-sans-extension
  13336. (file-name-nondirectory buffer-file-name))))
  13337. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  13338. (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
  13339. (inquote nil)
  13340. (infixed nil)
  13341. (in-local-list nil)
  13342. (local-list-num nil)
  13343. (local-list-indent nil)
  13344. (llt org-plain-list-ordered-item-terminator)
  13345. (email (plist-get opt-plist :email))
  13346. (language (plist-get opt-plist :language))
  13347. (text (plist-get opt-plist :text))
  13348. (lang-words nil)
  13349. (target-alist nil) tg
  13350. (head-count 0) cnt
  13351. (start 0)
  13352. (coding-system (and (boundp 'buffer-file-coding-system)
  13353. buffer-file-coding-system))
  13354. (coding-system-for-write coding-system)
  13355. (save-buffer-coding-system coding-system)
  13356. (charset (and coding-system
  13357. (fboundp 'coding-system-get)
  13358. (coding-system-get coding-system 'mime-charset)))
  13359. table-open type
  13360. table-buffer table-orig-buffer
  13361. ind start-is-num starter
  13362. rpl path desc descp desc1 desc2 link
  13363. )
  13364. (message "Exporting...")
  13365. (setq org-last-level 1)
  13366. (org-init-section-numbers)
  13367. ;; Get the language-dependent settings
  13368. (setq lang-words (or (assoc language org-export-language-setup)
  13369. (assoc "en" org-export-language-setup)))
  13370. ;; Switch to the output buffer
  13371. (if (or hidden (not org-export-html-show-new-buffer))
  13372. (set-buffer buffer)
  13373. (switch-to-buffer-other-window buffer))
  13374. (erase-buffer)
  13375. (fundamental-mode)
  13376. (let ((case-fold-search nil)
  13377. (org-odd-levels-only odd))
  13378. ;; create local variables for all options, to make sure all called
  13379. ;; functions get the correct information
  13380. (mapcar (lambda (x)
  13381. (set (make-local-variable (cdr x))
  13382. (plist-get opt-plist (car x))))
  13383. org-export-plist-vars)
  13384. (setq umax (if arg (prefix-numeric-value arg)
  13385. org-export-headline-levels))
  13386. ;; File header
  13387. (insert (format
  13388. "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
  13389. \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
  13390. <html xmlns=\"http://www.w3.org/1999/xhtml\"
  13391. lang=\"%s\" xml:lang=\"%s\">
  13392. <head>
  13393. <title>%s</title>
  13394. <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
  13395. <meta name=\"generator\" content=\"Org-mode\"/>
  13396. <meta name=\"generated\" content=\"%s %s\"/>
  13397. <meta name=\"author\" content=\"%s\"/>
  13398. %s
  13399. </head><body>
  13400. "
  13401. language language (org-html-expand title) (or charset "iso-8859-1")
  13402. date time author style))
  13403. (insert (or (plist-get opt-plist :preamble) ""))
  13404. (when (plist-get opt-plist :auto-preamble)
  13405. (if title (insert (format org-export-html-title-format
  13406. (org-html-expand title))))
  13407. (if text (insert "<p>\n" (org-html-expand text) "</p>")))
  13408. (if org-export-with-toc
  13409. (progn
  13410. (insert (format "<h%d>%s</h%d>\n"
  13411. org-export-html-toplevel-hlevel
  13412. (nth 3 lang-words)
  13413. org-export-html-toplevel-hlevel))
  13414. (insert "<ul>\n<li>")
  13415. (setq lines
  13416. (mapcar '(lambda (line)
  13417. (if (string-match org-todo-line-regexp line)
  13418. ;; This is a headline
  13419. (progn
  13420. (setq level (- (match-end 1) (match-beginning 1))
  13421. level (org-tr-level level)
  13422. txt (save-match-data
  13423. (org-html-expand
  13424. (org-export-cleanup-toc-line
  13425. (match-string 3 line))))
  13426. todo
  13427. (or (and org-export-mark-todo-in-toc
  13428. (match-beginning 2)
  13429. (not (equal (match-string 2 line)
  13430. org-done-string)))
  13431. ; TODO, not DONE
  13432. (and org-export-mark-todo-in-toc
  13433. (= level umax)
  13434. (org-search-todo-below
  13435. line lines level))))
  13436. (if (and (memq org-export-with-tags '(not-in-toc nil))
  13437. (string-match "[ \t]+:[a-zA-Z0-9_@:]+:[ \t]*$" txt))
  13438. (setq txt (replace-match "" t t txt)))
  13439. (if (string-match quote-re0 txt)
  13440. (setq txt (replace-match "" t t txt)))
  13441. (if org-export-with-section-numbers
  13442. (setq txt (concat (org-section-number level)
  13443. " " txt)))
  13444. (if (<= level umax)
  13445. (progn
  13446. (setq head-count (+ head-count 1))
  13447. (if (> level org-last-level)
  13448. (progn
  13449. (setq cnt (- level org-last-level))
  13450. (while (>= (setq cnt (1- cnt)) 0)
  13451. (insert "\n<ul>\n<li>"))
  13452. (insert "\n")))
  13453. (if (< level org-last-level)
  13454. (progn
  13455. (setq cnt (- org-last-level level))
  13456. (while (>= (setq cnt (1- cnt)) 0)
  13457. (insert "</li>\n</ul>"))
  13458. (insert "\n")))
  13459. ;; Check for targets
  13460. (while (string-match org-target-regexp line)
  13461. (setq tg (match-string 1 line)
  13462. line (replace-match
  13463. (concat "@<span class=\"target\">" tg "@</span> ")
  13464. t t line))
  13465. (push (cons (org-solidify-link-text tg)
  13466. (format "sec-%d" head-count))
  13467. target-alist))
  13468. (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
  13469. (setq txt (replace-match "" t t txt)))
  13470. (insert
  13471. (format
  13472. (if todo
  13473. "</li>\n<li><a href=\"#sec-%d\"><span class=\"todo\">%s</span></a>"
  13474. "</li>\n<li><a href=\"#sec-%d\">%s</a>")
  13475. head-count txt))
  13476. (setq org-last-level level))
  13477. )))
  13478. line)
  13479. lines))
  13480. (while (> org-last-level 0)
  13481. (setq org-last-level (1- org-last-level))
  13482. (insert "</li>\n</ul>\n"))
  13483. ))
  13484. (setq head-count 0)
  13485. (org-init-section-numbers)
  13486. (while (setq line (pop lines) origline line)
  13487. (catch 'nextline
  13488. ;; end of quote section?
  13489. (when (and inquote (string-match "^\\*+" line))
  13490. (insert "</pre>\n")
  13491. (setq inquote nil))
  13492. ;; inside a quote section?
  13493. (when inquote
  13494. (insert (org-html-protect line) "\n")
  13495. (throw 'nextline nil))
  13496. ;; verbatim lines
  13497. (when (and org-export-with-fixed-width
  13498. (string-match "^[ \t]*:\\(.*\\)" line))
  13499. (when (not infixed)
  13500. (setq infixed t)
  13501. (insert "<pre>\n"))
  13502. (insert (org-html-protect (match-string 1 line)) "\n")
  13503. (when (and lines
  13504. (not (string-match "^[ \t]*\\(:.*\\)"
  13505. (car lines))))
  13506. (setq infixed nil)
  13507. (insert "</pre>\n"))
  13508. (throw 'nextline nil))
  13509. ;; make targets to anchors
  13510. (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
  13511. (cond
  13512. ((match-end 2)
  13513. (setq line (replace-match
  13514. (concat "@<a name=\""
  13515. (org-solidify-link-text (match-string 1 line))
  13516. "\">\\nbsp@</a>")
  13517. t t line)))
  13518. ((and org-export-with-toc (equal (string-to-char line) ?*))
  13519. (setq line (replace-match
  13520. (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
  13521. ; (concat "@<i>" (match-string 1 line) "@</i> ")
  13522. t t line)))
  13523. (t
  13524. (setq line (replace-match
  13525. (concat "@<a name=\""
  13526. (org-solidify-link-text (match-string 1 line))
  13527. "\" class=\"target\">" (match-string 1 line) "@</a> ")
  13528. t t line)))))
  13529. (setq line (org-html-handle-time-stamps line))
  13530. ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  13531. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  13532. ;; Also handle sub_superscripts and checkboxes
  13533. (setq line (org-html-expand line))
  13534. ;; Format the links
  13535. (setq start 0)
  13536. (while (string-match org-bracket-link-analytic-regexp line start)
  13537. (setq start (match-beginning 0))
  13538. (setq type (if (match-end 2) (match-string 2 line) "internal"))
  13539. (setq path (match-string 3 line))
  13540. (setq desc1 (if (match-end 5) (match-string 5 line))
  13541. desc2 (if (match-end 2) (concat type ":" path) path)
  13542. descp (and desc1 (not (equal desc1 desc2)))
  13543. desc (or desc1 desc2))
  13544. ;; FIXME: do we need to unescape here somewhere?
  13545. (cond
  13546. ((equal type "internal")
  13547. (setq rpl
  13548. (concat
  13549. "<a href=\"#"
  13550. (org-solidify-link-text path target-alist)
  13551. "\">" desc "</a>")))
  13552. ((member type '("http" "https" "ftp" "mailto" "news"))
  13553. ;; standard URL
  13554. (setq link (concat type ":" path))
  13555. (setq rpl (concat "<a href=\"" link "\">" desc "</a>")))
  13556. ((string= type "file")
  13557. ;; FILE link
  13558. (let* ((filename path)
  13559. (abs-p (file-name-absolute-p filename))
  13560. thefile file-is-image-p search)
  13561. (save-match-data
  13562. (if (string-match "::\\(.*\\)" filename)
  13563. (setq search (match-string 1 filename)
  13564. filename (replace-match "" t nil filename)))
  13565. (setq valid
  13566. (if (functionp link-validate)
  13567. (funcall link-validate filename current-dir)
  13568. t))
  13569. (setq file-is-image-p
  13570. (string-match (org-image-file-name-regexp) filename))
  13571. (setq thefile (if abs-p (expand-file-name filename) filename))
  13572. (when (and org-export-html-link-org-files-as-html
  13573. (string-match "\\.org$" thefile))
  13574. (setq thefile (concat (substring thefile 0
  13575. (match-beginning 0))
  13576. ".html"))
  13577. (if (and search
  13578. ;; make sure this is can be used as target search
  13579. (not (string-match "^[0-9]*$" search))
  13580. (not (string-match "^\\*" search))
  13581. (not (string-match "^/.*/$" search)))
  13582. (setq thefile (concat thefile "#"
  13583. (org-solidify-link-text
  13584. (org-link-unescape search)))))
  13585. (when (string-match "^file:" desc)
  13586. (setq desc (replace-match "" t t desc))
  13587. (if (string-match "\\.org$" desc)
  13588. (setq desc (replace-match "" t t desc))))))
  13589. (setq rpl (if (and file-is-image-p
  13590. (or (eq t org-export-html-inline-images)
  13591. (and org-export-html-inline-images
  13592. (not descp))))
  13593. (concat "<img src=\"" thefile "\"/>")
  13594. (concat "<a href=\"" thefile "\">" desc "</a>")))
  13595. (if (not valid) (setq rpl desc))))
  13596. ((member type '("bbdb" "vm" "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  13597. (setq rpl (concat "<i>&lt;" type ":"
  13598. (save-match-data (org-link-unescape path))
  13599. "&gt;</i>"))))
  13600. (setq line (replace-match rpl t t line)
  13601. start (+ start (length rpl))))
  13602. ;; TODO items
  13603. (if (and (string-match org-todo-line-regexp line)
  13604. (match-beginning 2))
  13605. (if (equal (match-string 2 line) org-done-string)
  13606. (setq line (replace-match
  13607. "<span class=\"done\">\\2</span>"
  13608. t nil line 2))
  13609. (setq line (replace-match "<span class=\"todo\">\\2</span>"
  13610. t nil line 2))))
  13611. (cond
  13612. ((string-match "^\\(\\*+\\)[ \t]*\\(.*\\)" line)
  13613. ;; This is a headline
  13614. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  13615. txt (match-string 2 line))
  13616. (if (string-match quote-re0 txt)
  13617. (setq txt (replace-match "" t t txt)))
  13618. (if (<= level umax) (setq head-count (+ head-count 1)))
  13619. (when in-local-list
  13620. ;; Close any local lists before inserting a new header line
  13621. (while local-list-num
  13622. (org-close-li)
  13623. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  13624. (pop local-list-num))
  13625. (setq local-list-indent nil
  13626. in-local-list nil))
  13627. (org-html-level-start level txt umax
  13628. (and org-export-with-toc (<= level umax))
  13629. head-count)
  13630. ;; QUOTES
  13631. (when (string-match quote-re line)
  13632. (insert "<pre>")
  13633. (setq inquote t)))
  13634. ((and org-export-with-tables
  13635. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  13636. (if (not table-open)
  13637. ;; New table starts
  13638. (setq table-open t table-buffer nil table-orig-buffer nil))
  13639. ;; Accumulate lines
  13640. (setq table-buffer (cons line table-buffer)
  13641. table-orig-buffer (cons origline table-orig-buffer))
  13642. (when (or (not lines)
  13643. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  13644. (car lines))))
  13645. (setq table-open nil
  13646. table-buffer (nreverse table-buffer)
  13647. table-orig-buffer (nreverse table-orig-buffer))
  13648. (org-close-par-maybe)
  13649. (insert (org-format-table-html table-buffer table-orig-buffer))))
  13650. (t
  13651. ;; Normal lines
  13652. (when (string-match
  13653. (cond
  13654. ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  13655. ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  13656. ((= llt ?\)) "^\\( \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  13657. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
  13658. line)
  13659. (setq ind (org-get-string-indentation line)
  13660. start-is-num (match-beginning 4)
  13661. starter (if (match-beginning 2)
  13662. (substring (match-string 2 line) 0 -1))
  13663. line (substring line (match-beginning 5)))
  13664. (unless (string-match "[^ \t]" line)
  13665. ;; empty line. Pretend indentation is large.
  13666. (setq ind (1+ (or (car local-list-indent) 1))))
  13667. (while (and in-local-list
  13668. (or (and (= ind (car local-list-indent))
  13669. (not starter))
  13670. (< ind (car local-list-indent))))
  13671. (org-close-li)
  13672. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  13673. (pop local-list-num) (pop local-list-indent)
  13674. (setq in-local-list local-list-indent))
  13675. (cond
  13676. ((and starter
  13677. (or (not in-local-list)
  13678. (> ind (car local-list-indent))))
  13679. ;; Start new (level of ) list
  13680. (org-close-par-maybe)
  13681. (insert (if start-is-num "<ol>\n<li>\n" "<ul>\n<li>\n"))
  13682. (push start-is-num local-list-num)
  13683. (push ind local-list-indent)
  13684. (setq in-local-list t))
  13685. (starter
  13686. ;; continue current list
  13687. (org-close-li)
  13688. (insert "<li>\n")))
  13689. (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line)
  13690. (setq line
  13691. (replace-match
  13692. (if (equal (match-string 1 line) "X")
  13693. "<b>[X]</b>"
  13694. "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
  13695. t t line))))
  13696. ;; Empty lines start a new paragraph. If hand-formatted lists
  13697. ;; are not fully interpreted, lines starting with "-", "+", "*"
  13698. ;; also start a new paragraph.
  13699. (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
  13700. ;; Check if the line break needs to be conserved
  13701. (cond
  13702. ((string-match "\\\\\\\\[ \t]*$" line)
  13703. (setq line (replace-match "<br/>" t t line)))
  13704. (org-export-preserve-breaks
  13705. (setq line (concat line "<br/>"))))
  13706. (insert line "\n")))))
  13707. ;; Properly close all local lists and other lists
  13708. (when inquote (insert "</pre>\n"))
  13709. (when in-local-list
  13710. ;; Close any local lists before inserting a new header line
  13711. (while local-list-num
  13712. (org-close-li)
  13713. (insert (if (car local-list-num) "</ol>\n" "</ul>\n"))
  13714. (pop local-list-num))
  13715. (setq local-list-indent nil
  13716. in-local-list nil))
  13717. (org-html-level-start 1 nil umax
  13718. (and org-export-with-toc (<= level umax))
  13719. head-count)
  13720. (when (plist-get opt-plist :auto-postamble)
  13721. (when author
  13722. (insert "<p class=\"author\"> "
  13723. (nth 1 lang-words) ": " author "\n")
  13724. (when email
  13725. (insert "<a href=\"mailto:" email "\">&lt;"
  13726. email "&gt;</a>\n"))
  13727. (insert "</p>\n"))
  13728. (when (and date time)
  13729. (insert "<p class=\"date\"> "
  13730. (nth 2 lang-words) ": "
  13731. date " " time "</p>\n")))
  13732. (if org-export-html-with-timestamp
  13733. (insert org-export-html-html-helper-timestamp))
  13734. (insert (or (plist-get opt-plist :postamble) ""))
  13735. (insert "</body>\n</html>\n")
  13736. (normal-mode)
  13737. ;; remove empty paragraphs and lists
  13738. (goto-char (point-min))
  13739. (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
  13740. (replace-match ""))
  13741. (goto-char (point-min))
  13742. (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t)
  13743. (replace-match ""))
  13744. (save-buffer)
  13745. (goto-char (point-min))
  13746. (message "Exporting... done"))))
  13747. (defun org-format-table-html (lines olines)
  13748. "Find out which HTML converter to use and return the HTML code."
  13749. (if (string-match "^[ \t]*|" (car lines))
  13750. ;; A normal org table
  13751. (org-format-org-table-html lines)
  13752. ;; Table made by table.el - test for spanning
  13753. (let* ((hlines (delq nil (mapcar
  13754. (lambda (x)
  13755. (if (string-match "^[ \t]*\\+-" x) x
  13756. nil))
  13757. lines)))
  13758. (first (car hlines))
  13759. (ll (and (string-match "\\S-+" first)
  13760. (match-string 0 first)))
  13761. (re (concat "^[ \t]*" (regexp-quote ll)))
  13762. (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
  13763. hlines))))
  13764. (if (and (not spanning)
  13765. (not org-export-prefer-native-exporter-for-tables))
  13766. ;; We can use my own converter with HTML conversions
  13767. (org-format-table-table-html lines)
  13768. ;; Need to use the code generator in table.el, with the original text.
  13769. (org-format-table-table-html-using-table-generate-source olines)))))
  13770. (defun org-format-org-table-html (lines)
  13771. "Format a table into HTML."
  13772. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  13773. (setq lines (nreverse lines))
  13774. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  13775. (setq lines (nreverse lines))
  13776. (when org-export-table-remove-special-lines
  13777. ;; Check if the table has a marking column. If yes remove the
  13778. ;; column and the special lines
  13779. (let* ((special
  13780. (not
  13781. (memq nil
  13782. (mapcar
  13783. (lambda (x)
  13784. (or (string-match "^[ \t]*|-" x)
  13785. (string-match "^[ \t]*| *\\([#!$*_^ ]\\) *|" x)))
  13786. lines)))))
  13787. (if special
  13788. (setq lines
  13789. (delq nil
  13790. (mapcar
  13791. (lambda (x)
  13792. (if (string-match "^[ \t]*| *[!_^] *|" x)
  13793. nil ; ignore this line
  13794. (and (or (string-match "^[ \t]*|-+\\+" x)
  13795. (string-match "^[ \t]*|[^|]*|" x))
  13796. (replace-match "|" t t x))))
  13797. lines))))))
  13798. (let ((head (and org-export-highlight-first-table-line
  13799. (delq nil (mapcar
  13800. (lambda (x) (string-match "^[ \t]*|-" x))
  13801. (cdr lines)))))
  13802. line fields html)
  13803. (setq html (concat org-export-html-table-tag "\n"))
  13804. (while (setq line (pop lines))
  13805. (catch 'next-line
  13806. (if (string-match "^[ \t]*|-" line)
  13807. (progn
  13808. (setq head nil) ;; head ends here, first time around
  13809. ;; ignore this line
  13810. (throw 'next-line t)))
  13811. ;; Break the line into fields
  13812. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  13813. (setq html (concat
  13814. html
  13815. "<tr>"
  13816. (mapconcat (lambda (x)
  13817. (if head
  13818. (concat "<th>" x "</th>")
  13819. (concat "<td>" x "</td>")))
  13820. fields "")
  13821. "</tr>\n"))))
  13822. (setq html (concat html "</table>\n"))
  13823. html))
  13824. (defun org-fake-empty-table-line (line)
  13825. "Replace everything except \"|\" with spaces."
  13826. (let ((i (length line))
  13827. (newstr (copy-sequence line)))
  13828. (while (> i 0)
  13829. (setq i (1- i))
  13830. (if (not (eq (aref newstr i) ?|))
  13831. (aset newstr i ?\ )))
  13832. newstr))
  13833. (defun org-format-table-table-html (lines)
  13834. "Format a table generated by table.el into HTML.
  13835. This conversion does *not* use `table-generate-source' from table.el.
  13836. This has the advantage that Org-mode's HTML conversions can be used.
  13837. But it has the disadvantage, that no cell- or row-spanning is allowed."
  13838. (let (line field-buffer
  13839. (head org-export-highlight-first-table-line)
  13840. fields html empty)
  13841. (setq html (concat org-export-html-table-tag "\n"))
  13842. (while (setq line (pop lines))
  13843. (setq empty "&nbsp;")
  13844. (catch 'next-line
  13845. (if (string-match "^[ \t]*\\+-" line)
  13846. (progn
  13847. (if field-buffer
  13848. (progn
  13849. (setq html (concat
  13850. html
  13851. "<tr>"
  13852. (mapconcat
  13853. (lambda (x)
  13854. (if (equal x "") (setq x empty))
  13855. (if head
  13856. (concat "<th>" x "</th>\n")
  13857. (concat "<td>" x "</td>\n")))
  13858. field-buffer "\n")
  13859. "</tr>\n"))
  13860. (setq head nil)
  13861. (setq field-buffer nil)))
  13862. ;; Ignore this line
  13863. (throw 'next-line t)))
  13864. ;; Break the line into fields and store the fields
  13865. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  13866. (if field-buffer
  13867. (setq field-buffer (mapcar
  13868. (lambda (x)
  13869. (concat x "<br/>" (pop fields)))
  13870. field-buffer))
  13871. (setq field-buffer fields))))
  13872. (setq html (concat html "</table>\n"))
  13873. html))
  13874. (defun org-format-table-table-html-using-table-generate-source (lines)
  13875. "Format a table into html, using `table-generate-source' from table.el.
  13876. This has the advantage that cell- or row-spanning is allowed.
  13877. But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
  13878. (require 'table)
  13879. (with-current-buffer (get-buffer-create " org-tmp1 ")
  13880. (erase-buffer)
  13881. (insert (mapconcat 'identity lines "\n"))
  13882. (goto-char (point-min))
  13883. (if (not (re-search-forward "|[^+]" nil t))
  13884. (error "Error processing table"))
  13885. (table-recognize-table)
  13886. (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
  13887. (table-generate-source 'html " org-tmp2 ")
  13888. (set-buffer " org-tmp2 ")
  13889. (buffer-substring (point-min) (point-max))))
  13890. (defun org-html-handle-time-stamps (s)
  13891. "Format time stamps in string S, or remove them."
  13892. (catch 'exit
  13893. (let (r b)
  13894. (while (string-match org-maybe-keyword-time-regexp s)
  13895. (if (and (match-end 1) (equal (match-string 1 s) org-clock-string))
  13896. ;; never export CLOCK
  13897. (throw 'exit ""))
  13898. (or b (setq b (substring s 0 (match-beginning 0))))
  13899. (if (not org-export-with-timestamps)
  13900. (setq r (concat r (substring s 0 (match-beginning 0)))
  13901. s (substring s (match-end 0)))
  13902. (setq r (concat
  13903. r (substring s 0 (match-beginning 0))
  13904. (if (match-end 1)
  13905. (format "@<span class=\"timestamp-kwd\">%s @</span>"
  13906. (match-string 1 s)))
  13907. (format " @<span class=\"timestamp\">%s@</span>"
  13908. (substring (match-string 3 s) 1 -1)))
  13909. s (substring s (match-end 0)))))
  13910. ;; Line break if line started and ended with time stamp stuff
  13911. (if (not r)
  13912. s
  13913. (setq r (concat r s))
  13914. (unless (string-match "\\S-" (concat b s))
  13915. (setq r (concat r "@<br/>")))
  13916. r))))
  13917. (defun org-html-protect (s)
  13918. ;; convert & to &amp;, < to &lt; and > to &gt;
  13919. (let ((start 0))
  13920. (while (string-match "&" s start)
  13921. (setq s (replace-match "&amp;" t t s)
  13922. start (1+ (match-beginning 0))))
  13923. (while (string-match "<" s)
  13924. (setq s (replace-match "&lt;" t t s)))
  13925. (while (string-match ">" s)
  13926. (setq s (replace-match "&gt;" t t s))))
  13927. s)
  13928. (defun org-export-cleanup-toc-line (s)
  13929. "Remove tags and time staps from lines going into the toc."
  13930. (if (string-match " +:[a-zA-Z0-9_@:]+: *$" s)
  13931. (setq s (replace-match "" t t s)))
  13932. (when org-export-remove-timestamps-from-toc
  13933. (while (string-match org-maybe-keyword-time-regexp s)
  13934. (setq s (replace-match "" t t s))))
  13935. s)
  13936. (defun org-html-expand (string)
  13937. "Prepare STRING for HTML export. Applies all active conversions.
  13938. If there are links in the string, don't modify these."
  13939. (let* (m s l res)
  13940. (while (setq m (string-match org-bracket-link-regexp string))
  13941. (setq s (substring string 0 m)
  13942. l (match-string 0 string)
  13943. string (substring string (match-end 0)))
  13944. (push (org-html-do-expand s) res)
  13945. (push l res))
  13946. (push (org-html-do-expand string) res)
  13947. (apply 'concat (nreverse res))))
  13948. (defun org-html-do-expand (s)
  13949. "Apply all active conversions to translate special ASCII to HTML."
  13950. (setq s (org-html-protect s))
  13951. (if org-export-html-expand
  13952. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  13953. (setq s (replace-match "<\\1>" t nil s))))
  13954. (if org-export-with-emphasize
  13955. (setq s (org-export-html-convert-emphasize s)))
  13956. (if org-export-with-sub-superscripts
  13957. (setq s (org-export-html-convert-sub-super s)))
  13958. (if org-export-with-TeX-macros
  13959. (let ((start 0) wd ass)
  13960. (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)" s start))
  13961. (setq wd (match-string 1 s))
  13962. (if (setq ass (assoc wd org-html-entities))
  13963. (setq s (replace-match (or (cdr ass)
  13964. (concat "&" (car ass) ";"))
  13965. t t s))
  13966. (setq start (+ start (length wd)))))))
  13967. s)
  13968. (defun org-create-multibrace-regexp (left right n)
  13969. "Create a regular expression which will match a balanced sexp.
  13970. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  13971. as single character strings.
  13972. The regexp returned will match the entire expression including the
  13973. delimiters. It will also define a single group which contains the
  13974. match except for the outermost delimiters. The maximum depth of
  13975. stacked delimiters is N. Escaping delimiters is not possible."
  13976. (let* ((nothing (concat "[^" "\\" left "\\" right "]*?"))
  13977. (or "\\|")
  13978. (re nothing)
  13979. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  13980. (while (> n 1)
  13981. (setq n (1- n)
  13982. re (concat re or next)
  13983. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  13984. (concat left "\\(" re "\\)" right)))
  13985. (defvar org-match-substring-regexp
  13986. (concat
  13987. "\\([^\\]\\)\\([_^]\\)\\("
  13988. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  13989. "\\|"
  13990. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  13991. "\\|"
  13992. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  13993. "The regular expression matching a sub- or superscript.")
  13994. (defun org-export-html-convert-sub-super (string)
  13995. "Convert sub- and superscripts in STRING to HTML."
  13996. (let (key c)
  13997. (while (string-match org-match-substring-regexp string)
  13998. (setq key (if (string= (match-string 2 string) "_") "sub" "sup"))
  13999. (setq c (or (match-string 8 string)
  14000. (match-string 6 string)
  14001. (match-string 5 string)))
  14002. (setq string (replace-match
  14003. (concat (match-string 1 string)
  14004. "<" key ">" c "</" key ">")
  14005. t t string)))
  14006. (while (string-match "\\\\\\([_^]\\)" string)
  14007. (setq string (replace-match (match-string 1 string) t t string))))
  14008. string)
  14009. (defun org-export-html-convert-emphasize (string)
  14010. "Apply emphasis."
  14011. (while (string-match org-emph-re string)
  14012. (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)))
  14013. string)
  14014. (defvar org-par-open nil)
  14015. (defun org-open-par ()
  14016. "Insert <p>, but first close previous paragraph if any."
  14017. (org-close-par-maybe)
  14018. (insert "\n<p>")
  14019. (setq org-par-open t))
  14020. (defun org-close-par-maybe ()
  14021. "Close paragraph if there is one open."
  14022. (when org-par-open
  14023. (insert "</p>")
  14024. (setq org-par-open nil)))
  14025. (defun org-close-li ()
  14026. "Close <li> if necessary."
  14027. (org-close-par-maybe)
  14028. (insert "</li>\n"))
  14029. ; (when (save-excursion
  14030. ; (re-search-backward "</?\\(ul\\|ol\\|li\\|[hH][0-9]\\)>" nil t))
  14031. ; (if (member (match-string 0) '("</ul>" "</ol>" "<li>"))
  14032. ; (insert "</li>"))))
  14033. (defun org-html-level-start (level title umax with-toc head-count)
  14034. "Insert a new level in HTML export.
  14035. When TITLE is nil, just close all open levels."
  14036. (org-close-par-maybe)
  14037. (let ((l (1+ (max level umax))))
  14038. (while (<= l org-level-max)
  14039. (if (aref levels-open (1- l))
  14040. (progn
  14041. (org-html-level-close l)
  14042. (aset levels-open (1- l) nil)))
  14043. (setq l (1+ l)))
  14044. (when title
  14045. ;; If title is nil, this means this function is called to close
  14046. ;; all levels, so the rest is done only if title is given
  14047. (when (string-match "\\(:[a-zA-Z0-9_@:]+:\\)[ \t]*$" title)
  14048. (setq title (replace-match
  14049. (if org-export-with-tags
  14050. (save-match-data
  14051. (concat
  14052. "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
  14053. (mapconcat 'identity (org-split-string
  14054. (match-string 1 title) ":")
  14055. "&nbsp;")
  14056. "</span>"))
  14057. "")
  14058. t t title)))
  14059. (if (> level umax)
  14060. (progn
  14061. (if (aref levels-open (1- level))
  14062. (progn
  14063. (org-close-li)
  14064. (insert "<li>" title "<br/>\n"))
  14065. (aset levels-open (1- level) t)
  14066. (org-close-par-maybe)
  14067. (insert "<ul>\n<li>" title "<br/>\n")))
  14068. (if org-export-with-section-numbers
  14069. (setq title (concat (org-section-number level) " " title)))
  14070. (setq level (+ level org-export-html-toplevel-hlevel -1))
  14071. (if with-toc
  14072. (insert (format "\n<h%d><a name=\"sec-%d\">%s</a></h%d>\n"
  14073. level head-count title level))
  14074. (insert (format "\n<h%d>%s</h%d>\n" level title level)))
  14075. (org-open-par)))))
  14076. (defun org-html-level-close (&rest args)
  14077. "Terminate one level in HTML export."
  14078. (org-close-li)
  14079. (insert "</ul>"))
  14080. ;; Variable holding the vector with section numbers
  14081. (defvar org-section-numbers (make-vector org-level-max 0))
  14082. (defun org-init-section-numbers ()
  14083. "Initialize the vector for the section numbers."
  14084. (let* ((level -1)
  14085. (numbers (nreverse (org-split-string "" "\\.")))
  14086. (depth (1- (length org-section-numbers)))
  14087. (i depth) number-string)
  14088. (while (>= i 0)
  14089. (if (> i level)
  14090. (aset org-section-numbers i 0)
  14091. (setq number-string (or (car numbers) "0"))
  14092. (if (string-match "\\`[A-Z]\\'" number-string)
  14093. (aset org-section-numbers i
  14094. (- (string-to-char number-string) ?A -1))
  14095. (aset org-section-numbers i (string-to-number number-string)))
  14096. (pop numbers))
  14097. (setq i (1- i)))))
  14098. (defun org-section-number (&optional level)
  14099. "Return a string with the current section number.
  14100. When LEVEL is non-nil, increase section numbers on that level."
  14101. (let* ((depth (1- (length org-section-numbers))) idx n (string ""))
  14102. (when level
  14103. (when (> level -1)
  14104. (aset org-section-numbers
  14105. level (1+ (aref org-section-numbers level))))
  14106. (setq idx (1+ level))
  14107. (while (<= idx depth)
  14108. (if (not (= idx 1))
  14109. (aset org-section-numbers idx 0))
  14110. (setq idx (1+ idx))))
  14111. (setq idx 0)
  14112. (while (<= idx depth)
  14113. (setq n (aref org-section-numbers idx))
  14114. (setq string (concat string (if (not (string= string "")) "." "")
  14115. (int-to-string n)))
  14116. (setq idx (1+ idx)))
  14117. (save-match-data
  14118. (if (string-match "\\`\\([@0]\\.\\)+" string)
  14119. (setq string (replace-match "" t nil string)))
  14120. (if (string-match "\\(\\.0\\)+\\'" string)
  14121. (setq string (replace-match "" t nil string))))
  14122. string))
  14123. ;;;###autoload
  14124. (defun org-export-icalendar-this-file ()
  14125. "Export current file as an iCalendar file.
  14126. The iCalendar file will be located in the same directory as the Org-mode
  14127. file, but with extension `.ics'."
  14128. (interactive)
  14129. (org-export-icalendar nil buffer-file-name))
  14130. (defun org-export-as-xoxo-insert-into (buffer &rest output)
  14131. (with-current-buffer buffer
  14132. (apply 'insert output)))
  14133. (put 'org-export-as-xoxo-insert-into 'lisp-indent-function 1)
  14134. (defun org-export-as-xoxo (&optional buffer)
  14135. "Export the org buffer as XOXO.
  14136. The XOXO buffer is named *xoxo-<source buffer name>*"
  14137. (interactive (list (current-buffer)))
  14138. ;; A quickie abstraction
  14139. ;; Output everything as XOXO
  14140. (with-current-buffer (get-buffer buffer)
  14141. (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
  14142. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  14143. (org-infile-export-plist)))
  14144. (filename (concat (file-name-as-directory
  14145. (org-export-directory :xoxo opt-plist))
  14146. (file-name-sans-extension
  14147. (file-name-nondirectory buffer-file-name))
  14148. ".html"))
  14149. (out (find-file-noselect filename))
  14150. (last-level 1)
  14151. (hanging-li nil))
  14152. ;; Check the output buffer is empty.
  14153. (with-current-buffer out (erase-buffer))
  14154. ;; Kick off the output
  14155. (org-export-as-xoxo-insert-into out "<ol class='xoxo'>\n")
  14156. (while (re-search-forward "^\\(\\*+\\) \\(.+\\)" (point-max) 't)
  14157. (let* ((hd (match-string-no-properties 1))
  14158. (level (length hd))
  14159. (text (concat
  14160. (match-string-no-properties 2)
  14161. (save-excursion
  14162. (goto-char (match-end 0))
  14163. (let ((str ""))
  14164. (catch 'loop
  14165. (while 't
  14166. (forward-line)
  14167. (if (looking-at "^[ \t]\\(.*\\)")
  14168. (setq str (concat str (match-string-no-properties 1)))
  14169. (throw 'loop str)))))))))
  14170. ;; Handle level rendering
  14171. (cond
  14172. ((> level last-level)
  14173. (org-export-as-xoxo-insert-into out "\n<ol>\n"))
  14174. ((< level last-level)
  14175. (dotimes (- (- last-level level) 1)
  14176. (if hanging-li
  14177. (org-export-as-xoxo-insert-into out "</li>\n"))
  14178. (org-export-as-xoxo-insert-into out "</ol>\n"))
  14179. (when hanging-li
  14180. (org-export-as-xoxo-insert-into out "</li>\n")
  14181. (setq hanging-li nil)))
  14182. ((equal level last-level)
  14183. (if hanging-li
  14184. (org-export-as-xoxo-insert-into out "</li>\n")))
  14185. )
  14186. (setq last-level level)
  14187. ;; And output the new li
  14188. (setq hanging-li 't)
  14189. (if (equal ?+ (elt text 0))
  14190. (org-export-as-xoxo-insert-into out "<li class='" (substring text 1) "'>")
  14191. (org-export-as-xoxo-insert-into out "<li>" text))))
  14192. ;; Finally finish off the ol
  14193. (dotimes (- last-level 1)
  14194. (if hanging-li
  14195. (org-export-as-xoxo-insert-into out "</li>\n"))
  14196. (org-export-as-xoxo-insert-into out "</ol>\n"))
  14197. ;; Finish the buffer off and clean it up.
  14198. (switch-to-buffer-other-window out)
  14199. (indent-region (point-min) (point-max) nil)
  14200. (save-buffer)
  14201. (goto-char (point-min))
  14202. )))
  14203. ;;;###autoload
  14204. (defun org-export-icalendar-all-agenda-files ()
  14205. "Export all files in `org-agenda-files' to iCalendar .ics files.
  14206. Each iCalendar file will be located in the same directory as the Org-mode
  14207. file, but with extension `.ics'."
  14208. (interactive)
  14209. (apply 'org-export-icalendar nil (org-agenda-files t)))
  14210. ;;;###autoload
  14211. (defun org-export-icalendar-combine-agenda-files ()
  14212. "Export all files in `org-agenda-files' to a single combined iCalendar file.
  14213. The file is stored under the name `org-combined-agenda-icalendar-file'."
  14214. (interactive)
  14215. (apply 'org-export-icalendar t (org-agenda-files t)))
  14216. (defun org-export-icalendar (combine &rest files)
  14217. "Create iCalendar files for all elements of FILES.
  14218. If COMBINE is non-nil, combine all calendar entries into a single large
  14219. file and store it under the name `org-combined-agenda-icalendar-file'."
  14220. (save-excursion
  14221. (let* ((dir (org-export-directory
  14222. :ical (list :publishing-directory
  14223. org-export-publishing-directory)))
  14224. file ical-file ical-buffer category started org-agenda-new-buffers)
  14225. (when combine
  14226. (setq ical-file
  14227. (if (file-name-absolute-p org-combined-agenda-icalendar-file)
  14228. org-combined-agenda-icalendar-file
  14229. (expand-file-name org-combined-agenda-icalendar-file dir))
  14230. ical-buffer (org-get-agenda-file-buffer ical-file))
  14231. (set-buffer ical-buffer) (erase-buffer))
  14232. (while (setq file (pop files))
  14233. (catch 'nextfile
  14234. (org-check-agenda-file file)
  14235. (set-buffer (org-get-agenda-file-buffer file))
  14236. (unless combine
  14237. (setq ical-file (concat (file-name-as-directory dir)
  14238. (file-name-sans-extension
  14239. (file-name-nondirectory buffer-file-name))
  14240. ".ics"))
  14241. (setq ical-buffer (org-get-agenda-file-buffer ical-file))
  14242. (with-current-buffer ical-buffer (erase-buffer)))
  14243. (setq category (or org-category
  14244. (file-name-sans-extension
  14245. (file-name-nondirectory buffer-file-name))))
  14246. (if (symbolp category) (setq category (symbol-name category)))
  14247. (let ((standard-output ical-buffer))
  14248. (if combine
  14249. (and (not started) (setq started t)
  14250. (org-start-icalendar-file org-icalendar-combined-name))
  14251. (org-start-icalendar-file category))
  14252. (org-print-icalendar-entries combine category)
  14253. (when (or (and combine (not files)) (not combine))
  14254. (org-finish-icalendar-file)
  14255. (set-buffer ical-buffer)
  14256. (save-buffer)
  14257. (run-hooks 'org-after-save-iCalendar-file-hook)))))
  14258. (org-release-buffers org-agenda-new-buffers))))
  14259. (defvar org-after-save-iCalendar-file-hook nil
  14260. "Hook run after an iCalendar file has been saved.
  14261. The iCalendar buffer is still current when this hook is run.
  14262. A good way to use this is to tell a desktop calenndar application to re-read
  14263. the iCalendar file.")
  14264. (defun org-print-icalendar-entries (&optional combine category)
  14265. "Print iCalendar entries for the current Org-mode file to `standard-output'.
  14266. When COMBINE is non nil, add the category to each line."
  14267. (let ((re2 (concat "--?-?\\(" org-ts-regexp "\\)"))
  14268. (dts (org-ical-ts-to-string
  14269. (format-time-string (cdr org-time-stamp-formats) (current-time))
  14270. "DTSTART"))
  14271. hd ts ts2 state (inc t) pos scheduledp deadlinep tmp pri)
  14272. (save-excursion
  14273. (goto-char (point-min))
  14274. (while (re-search-forward org-ts-regexp nil t)
  14275. (setq pos (match-beginning 0)
  14276. ts (match-string 0)
  14277. inc t
  14278. hd (org-get-heading))
  14279. (if (looking-at re2)
  14280. (progn
  14281. (goto-char (match-end 0))
  14282. (setq ts2 (match-string 1) inc nil))
  14283. (setq ts2 ts
  14284. tmp (buffer-substring (max (point-min)
  14285. (- pos org-ds-keyword-length))
  14286. pos)
  14287. deadlinep (string-match org-deadline-regexp tmp)
  14288. scheduledp (string-match org-scheduled-regexp tmp)
  14289. ;; donep (org-entry-is-done-p)
  14290. ))
  14291. (if (or (string-match org-tr-regexp hd)
  14292. (string-match org-ts-regexp hd))
  14293. (setq hd (replace-match "" t t hd)))
  14294. (if combine
  14295. (setq hd (concat hd " (category " category ")")))
  14296. (if deadlinep (setq hd (concat "DL: " hd " This is a deadline")))
  14297. (if scheduledp (setq hd (concat "S: " hd " Scheduled for this date")))
  14298. (princ (format "BEGIN:VEVENT
  14299. %s
  14300. %s
  14301. SUMMARY:%s
  14302. END:VEVENT\n"
  14303. (org-ical-ts-to-string ts "DTSTART")
  14304. (org-ical-ts-to-string ts2 "DTEND" inc)
  14305. hd)))
  14306. (when org-icalendar-include-todo
  14307. (goto-char (point-min))
  14308. (while (re-search-forward org-todo-line-regexp nil t)
  14309. (setq state (match-string 1))
  14310. (unless (equal state org-done-string)
  14311. (setq hd (match-string 3))
  14312. (if (string-match org-priority-regexp hd)
  14313. (setq pri (string-to-char (match-string 2 hd))
  14314. hd (concat (substring hd 0 (match-beginning 1))
  14315. (substring hd (- (match-end 1)))))
  14316. (setq pri org-default-priority))
  14317. (setq pri (floor (1+ (* 8. (/ (float (- org-lowest-priority pri))
  14318. (- org-lowest-priority ?A))))))
  14319. (princ (format "BEGIN:VTODO
  14320. %s
  14321. SUMMARY:%s
  14322. SEQUENCE:1
  14323. PRIORITY:%d
  14324. END:VTODO\n"
  14325. dts hd pri))))))))
  14326. (defun org-start-icalendar-file (name)
  14327. "Start an iCalendar file by inserting the header."
  14328. (let ((user user-full-name)
  14329. (name (or name "unknown"))
  14330. (timezone (cadr (current-time-zone))))
  14331. (princ
  14332. (format "BEGIN:VCALENDAR
  14333. VERSION:2.0
  14334. X-WR-CALNAME:%s
  14335. PRODID:-//%s//Emacs with Org-mode//EN
  14336. X-WR-TIMEZONE:%s
  14337. CALSCALE:GREGORIAN\n" name user timezone))))
  14338. (defun org-finish-icalendar-file ()
  14339. "Finish an iCalendar file by inserting the END statement."
  14340. (princ "END:VCALENDAR\n"))
  14341. (defun org-ical-ts-to-string (s keyword &optional inc)
  14342. "Take a time string S and convert it to iCalendar format.
  14343. KEYWORD is added in front, to make a complete line like DTSTART....
  14344. When INC is non-nil, increase the hour by two (if time string contains
  14345. a time), or the day by one (if it does not contain a time)."
  14346. (let ((t1 (org-parse-time-string s 'nodefault))
  14347. t2 fmt have-time time)
  14348. (if (and (car t1) (nth 1 t1) (nth 2 t1))
  14349. (setq t2 t1 have-time t)
  14350. (setq t2 (org-parse-time-string s)))
  14351. (let ((s (car t2)) (mi (nth 1 t2)) (h (nth 2 t2))
  14352. (d (nth 3 t2)) (m (nth 4 t2)) (y (nth 5 t2)))
  14353. (when inc
  14354. (if have-time (setq h (+ 2 h)) (setq d (1+ d))))
  14355. (setq time (encode-time s mi h d m y)))
  14356. (setq fmt (if have-time ":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
  14357. (concat keyword (format-time-string fmt time))))
  14358. ;;; LaTeX stuff
  14359. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  14360. "Keymap for the minor `org-cdlatex-mode'.")
  14361. (define-key org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  14362. (define-key org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  14363. (define-key org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  14364. (define-key org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  14365. (define-key org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  14366. (defvar org-cdlatex-texmathp-advice-is-done nil
  14367. "Flag remembering if we have applied the advice to texmathp already.")
  14368. (define-minor-mode org-cdlatex-mode
  14369. "Toggle the minor `org-cdlatex-mode'.
  14370. This mode supports entering LaTeX environment and math in LaTeX fragments
  14371. in Org-mode.
  14372. \\{org-cdlatex-mode-map}"
  14373. nil " OCDL" nil
  14374. (when org-cdlatex-mode (require 'cdlatex))
  14375. (unless org-cdlatex-texmathp-advice-is-done
  14376. (setq org-cdlatex-texmathp-advice-is-done t)
  14377. (defadvice texmathp (around org-math-always-on activate)
  14378. "Always return t in org-mode buffers.
  14379. This is because we want to insert math symbols without dollars even outside
  14380. the LaTeX math segments. If Orgmode thinks that point is actually inside
  14381. en embedded LaTeX fragement, let texmathp do its job.
  14382. \\[org-cdlatex-mode-map]"
  14383. (interactive)
  14384. (let (p)
  14385. (cond
  14386. ((not (org-mode-p)) ad-do-it)
  14387. ((eq this-command 'cdlatex-math-symbol)
  14388. (setq ad-return-value t
  14389. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  14390. (t
  14391. (let ((p (org-inside-LaTeX-fragment-p)))
  14392. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  14393. (setq ad-return-value t
  14394. texmathp-why '("Org-mode embedded math" . 0))
  14395. (if p ad-do-it)))))))))
  14396. (defun turn-on-org-cdlatex ()
  14397. "Unconditionally turn on `org-cdlatex-mode'."
  14398. (org-cdlatex-mode 1))
  14399. (defun org-inside-LaTeX-fragment-p ()
  14400. "Test if point is inside a LaTeX fragment.
  14401. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  14402. sequence appearing also before point.
  14403. Even though the matchers for math are configurable, this function assumes
  14404. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  14405. delimiters are skipped when they have been removed by customization.
  14406. The return value is nil, or a cons cell with the delimiter and
  14407. and the position of this delimiter.
  14408. This function does a reasonably good job, but can locally be fooled by
  14409. for example currency specifications. For example it will assume being in
  14410. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  14411. fragments that are properly closed, but during editing, we have to live
  14412. with the uncertainty caused by missing closing delimiters. This function
  14413. looks only before point, not after."
  14414. (catch 'exit
  14415. (let ((pos (point))
  14416. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  14417. (lim (progn
  14418. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  14419. (point)))
  14420. dd-on str (start 0) m re)
  14421. (goto-char pos)
  14422. (when dodollar
  14423. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  14424. re (nth 1 (assoc "$" org-latex-regexps)))
  14425. (while (string-match re str start)
  14426. (cond
  14427. ((= (match-end 0) (length str))
  14428. (throw 'exit (cons "$" (+ lim (match-beginning 0)))))
  14429. ((= (match-end 0) (- (length str) 5))
  14430. (throw 'exit nil))
  14431. (t (setq start (match-end 0))))))
  14432. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  14433. (goto-char pos)
  14434. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  14435. (and (match-beginning 2) (throw 'exit nil))
  14436. ;; count $$
  14437. (while (re-search-backward "\\$\\$" lim t)
  14438. (setq dd-on (not dd-on)))
  14439. (goto-char pos)
  14440. (if dd-on (cons "$$" m))))))
  14441. (defun org-try-cdlatex-tab ()
  14442. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  14443. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  14444. - inside a LaTeX fragment, or
  14445. - after the first word in a line, where an abbreviation expansion could
  14446. insert a LaTeX environment."
  14447. (when org-cdlatex-mode
  14448. (cond
  14449. ((save-excursion
  14450. (skip-chars-backward "a-zA-Z0-9*")
  14451. (skip-chars-backward " \t")
  14452. (bolp))
  14453. (cdlatex-tab) t)
  14454. ((org-inside-LaTeX-fragment-p)
  14455. (cdlatex-tab) t)
  14456. (t nil))))
  14457. (defun org-cdlatex-underscore-caret (&optional arg)
  14458. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  14459. Revert to the normal definition outside of these fragments."
  14460. (interactive "P")
  14461. (if (org-inside-LaTeX-fragment-p)
  14462. (call-interactively 'cdlatex-sub-superscript)
  14463. (let (org-cdlatex-mode)
  14464. (call-interactively (key-binding (vector last-input-event))))))
  14465. (defun org-cdlatex-math-modify (&optional arg)
  14466. "Execute `cdlatex-math-modify' in LaTeX fragments.
  14467. Revert to the normal definition outside of these fragments."
  14468. (interactive "P")
  14469. (if (org-inside-LaTeX-fragment-p)
  14470. (call-interactively 'cdlatex-math-modify)
  14471. (let (org-cdlatex-mode)
  14472. (call-interactively (key-binding (vector last-input-event))))))
  14473. (defvar org-latex-fragment-image-overlays nil
  14474. "List of overlays carrying the images of latex fragments.")
  14475. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  14476. (defun org-remove-latex-fragment-image-overlays ()
  14477. "Remove all overlays with LaTeX fragment images in current buffer."
  14478. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  14479. (setq org-latex-fragment-image-overlays nil))
  14480. (defun org-preview-latex-fragment (&optional subtree)
  14481. "Preview the LaTeX fragment at point, or all locally or globally.
  14482. If the cursor is in a LaTeX fragment, create the image and overlay
  14483. it over the source code. If there is no fragment at point, display
  14484. all fragments in the current text, from one headline to the next. With
  14485. prefix SUBTREE, display all fragments in the current subtree. With a
  14486. double prefix `C-u C-u', or when the cursor is before the first headline,
  14487. display all fragments in the buffer.
  14488. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  14489. (interactive "P")
  14490. (org-remove-latex-fragment-image-overlays)
  14491. (save-excursion
  14492. (save-restriction
  14493. (let (beg end at msg)
  14494. (cond
  14495. ((or (equal subtree '(16))
  14496. (not (save-excursion
  14497. (re-search-backward (concat "^" outline-regexp) nil t))))
  14498. (setq beg (point-min) end (point-max)
  14499. msg "Creating images for buffer...%s"))
  14500. ((equal subtree '(4))
  14501. (org-back-to-heading)
  14502. (setq beg (point) end (org-end-of-subtree t)
  14503. msg "Creating images for subtree...%s"))
  14504. (t
  14505. (if (setq at (org-inside-LaTeX-fragment-p))
  14506. (goto-char (max (point-min) (- (cdr at) 2)))
  14507. (org-back-to-heading))
  14508. (setq beg (point) end (progn (outline-next-heading) (point))
  14509. msg (if at "Creating image...%s"
  14510. "Creating images for entry...%s"))))
  14511. (message msg "")
  14512. (narrow-to-region beg end)
  14513. (org-format-latex
  14514. (concat "ltxpng/" (file-name-sans-extension
  14515. (file-name-nondirectory
  14516. buffer-file-name)))
  14517. default-directory 'overlays msg at)
  14518. (message msg "done. Use `C-c C-c' to remove images.")))))
  14519. (defvar org-latex-regexps
  14520. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  14521. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  14522. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  14523. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  14524. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  14525. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  14526. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  14527. "Regular expressions for matching embedded LaTeX.")
  14528. (defun org-format-latex (prefix &optional dir overlays msg at)
  14529. "Replace LaTeX fragments with links to an image, and produce images."
  14530. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  14531. (let* ((prefixnodir (file-name-nondirectory prefix))
  14532. (absprefix (expand-file-name prefix dir))
  14533. (todir (file-name-directory absprefix))
  14534. (opt org-format-latex-options)
  14535. (matchers (plist-get opt :matchers))
  14536. (re-list org-latex-regexps)
  14537. (cnt 0) txt link beg end re e oldfiles
  14538. m n block linkfile movefile ov)
  14539. ;; Make sure the directory exists
  14540. (or (file-directory-p todir) (make-directory todir))
  14541. ;; Check if there are old images files with this prefix, and remove them
  14542. (setq oldfiles (directory-files
  14543. todir 'full
  14544. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$")))
  14545. (while oldfiles (delete-file (pop oldfiles)))
  14546. ;; Check the different regular expressions
  14547. (while (setq e (pop re-list))
  14548. (setq m (car e) re (nth 1 e) n (nth 2 e)
  14549. block (if (nth 3 e) "\n\n" ""))
  14550. (when (member m matchers)
  14551. (goto-char (point-min))
  14552. (while (re-search-forward re nil t)
  14553. (when (or (not at) (equal (cdr at) (match-beginning n)))
  14554. (setq txt (match-string n)
  14555. beg (match-beginning n) end (match-end n)
  14556. cnt (1+ cnt)
  14557. linkfile (format "%s_%04d.png" prefix cnt)
  14558. movefile (format "%s_%04d.png" absprefix cnt)
  14559. link (concat block "[[file:" linkfile "]]" block))
  14560. (if msg (message msg cnt))
  14561. (goto-char beg)
  14562. (org-create-formula-image
  14563. txt movefile opt)
  14564. (if overlays
  14565. (progn
  14566. (setq ov (org-make-overlay beg end))
  14567. (if (featurep 'xemacs)
  14568. (progn
  14569. (org-overlay-put ov 'invisible t)
  14570. (org-overlay-put
  14571. ov 'end-glyph
  14572. (make-glyph (vector 'png :file movefile))))
  14573. (org-overlay-put
  14574. ov 'display
  14575. (list 'image :type 'png :file movefile :ascent 'center)))
  14576. (push ov org-latex-fragment-image-overlays)
  14577. (goto-char end))
  14578. (delete-region beg end)
  14579. (insert link))))))))
  14580. ;; This function borrows from Ganesh Swami's latex2png.el
  14581. (defun org-create-formula-image (string tofile options)
  14582. (let* ((tmpdir (if (featurep 'xemacs)
  14583. (temp-directory)
  14584. temporary-file-directory))
  14585. (texfilebase (make-temp-name
  14586. (expand-file-name "orgtex" tmpdir)))
  14587. ;(texfilebase (make-temp-file "orgtex"))
  14588. ; (dummy (delete-file texfilebase))
  14589. (texfile (concat texfilebase ".tex"))
  14590. (dvifile (concat texfilebase ".dvi"))
  14591. (pngfile (concat texfilebase ".png"))
  14592. (scale (number-to-string (* 1000 (or (plist-get options :scale) 1.0))))
  14593. (fg (or (plist-get options :foreground) "Black"))
  14594. (bg (or (plist-get options :background) "Transparent")))
  14595. (with-temp-file texfile
  14596. (insert "\\documentclass{article}
  14597. \\usepackage{fullpage}
  14598. \\usepackage{amssymb}
  14599. \\usepackage[usenames]{color}
  14600. \\usepackage{amsmath}
  14601. \\usepackage{latexsym}
  14602. \\usepackage[mathscr]{eucal}
  14603. \\pagestyle{empty}
  14604. \\begin{document}\n" string "\n\\end{document}\n"))
  14605. (let ((dir default-directory))
  14606. (condition-case nil
  14607. (progn
  14608. (cd tmpdir)
  14609. (call-process "latex" nil nil nil texfile))
  14610. (error nil))
  14611. (cd dir))
  14612. (if (not (file-exists-p dvifile))
  14613. (progn (message "Failed to create dvi file from %s" texfile) nil)
  14614. (call-process "dvipng" nil nil nil
  14615. "-E" "-fg" fg "-bg" bg
  14616. "-x" scale "-y" scale "-T" "tight"
  14617. "-o" pngfile
  14618. dvifile)
  14619. (if (not (file-exists-p pngfile))
  14620. (progn (message "Failed to create png file from %s" texfile) nil)
  14621. ;; Use the requested file name and clean up
  14622. (copy-file pngfile tofile 'replace)
  14623. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  14624. (delete-file (concat texfilebase e)))
  14625. pngfile))))
  14626. ;;; Key bindings
  14627. ;; - Bindings in Org-mode map are currently
  14628. ;; 0123456789abcdefghijklmnopqrstuvwxyz!?@#$%^&-+*/=()_{}[]:;"|,.<>~`'\t the alphabet
  14629. ;; abcd fgh j lmnopqrstuvwxyz!? #$ ^ -+*/= [] ; |,.<>~ '\t necessary bindings
  14630. ;; e (?) useful from outline-mode
  14631. ;; i k @ expendable from outline-mode
  14632. ;; 0123456789 % & ()_{} " ` free
  14633. ;; Make `C-c C-x' a prefix key
  14634. (define-key org-mode-map "\C-c\C-x" (make-sparse-keymap))
  14635. ;; TAB key with modifiers
  14636. (define-key org-mode-map "\C-i" 'org-cycle)
  14637. (define-key org-mode-map [(tab)] 'org-cycle)
  14638. (define-key org-mode-map [(control tab)] 'org-force-cycle-archived)
  14639. (define-key org-mode-map [(meta tab)] 'org-complete)
  14640. (define-key org-mode-map "\M-\C-i" 'org-complete) ; for tty emacs
  14641. ;; The following line is necessary under Suse GNU/Linux
  14642. (unless (featurep 'xemacs)
  14643. (define-key org-mode-map [S-iso-lefttab] 'org-shifttab))
  14644. (define-key org-mode-map [(shift tab)] 'org-shifttab)
  14645. (define-key org-mode-map (org-key 'S-return) 'org-table-copy-down)
  14646. (define-key org-mode-map "\C-c\C-xc" 'org-table-copy-down) ; tty
  14647. (define-key org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  14648. (define-key org-mode-map "\C-c\C-xM" 'org-insert-todo-heading) ; tty
  14649. (define-key org-mode-map [(meta return)] 'org-meta-return)
  14650. (define-key org-mode-map "\C-c\C-xm" 'org-meta-return) ; tty emacs
  14651. (define-key org-mode-map [?\e (return)] 'org-meta-return) ; tty emacs
  14652. ;; Cursor keys with modifiers
  14653. (define-key org-mode-map [(meta left)] 'org-metaleft)
  14654. (define-key org-mode-map [?\e (left)] 'org-metaleft) ; for tty emacs
  14655. (define-key org-mode-map "\C-c\C-xl" 'org-metaleft) ; for tty emacs
  14656. (define-key org-mode-map [(meta right)] 'org-metaright)
  14657. (define-key org-mode-map [?\e (right)] 'org-metaright) ; for tty emacs
  14658. (define-key org-mode-map "\C-c\C-xr" 'org-metaright) ; for tty emacs
  14659. (define-key org-mode-map [(meta up)] 'org-metaup)
  14660. (define-key org-mode-map [?\e (up)] 'org-metaup) ; for tty emacs
  14661. (define-key org-mode-map "\C-c\C-xu" 'org-metaup) ; for tty emacs
  14662. (define-key org-mode-map [(meta down)] 'org-metadown)
  14663. (define-key org-mode-map [?\e (down)] 'org-metadown) ; for tty emacs
  14664. (define-key org-mode-map "\C-c\C-xd" 'org-metadown) ; for tty emacs
  14665. (define-key org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  14666. (define-key org-mode-map "\C-c\C-xL" 'org-shiftmetaleft) ; tty
  14667. (define-key org-mode-map [(meta shift right)] 'org-shiftmetaright)
  14668. (define-key org-mode-map "\C-c\C-xR" 'org-shiftmetaright) ; tty
  14669. (define-key org-mode-map [(meta shift up)] 'org-shiftmetaup)
  14670. (define-key org-mode-map "\C-c\C-xU" 'org-shiftmetaup) ; tty
  14671. (define-key org-mode-map [(meta shift down)] 'org-shiftmetadown)
  14672. (define-key org-mode-map "\C-c\C-xD" 'org-shiftmetadown) ; tty
  14673. (define-key org-mode-map (org-key 'S-up) 'org-shiftup)
  14674. (define-key org-mode-map [?\C-c ?\C-x (up)] 'org-shiftup)
  14675. (define-key org-mode-map (org-key 'S-down) 'org-shiftdown)
  14676. (define-key org-mode-map [?\C-c ?\C-x (down)] 'org-shiftdown)
  14677. (define-key org-mode-map (org-key 'S-left) 'org-shiftleft)
  14678. (define-key org-mode-map [?\C-c ?\C-x (left)] 'org-shiftleft)
  14679. (define-key org-mode-map (org-key 'S-right) 'org-shiftright)
  14680. (define-key org-mode-map [?\C-c ?\C-x (right)] 'org-shiftright)
  14681. ;; All the other keys
  14682. (define-key org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  14683. (define-key org-mode-map "\C-xns" 'org-narrow-to-subtree)
  14684. (define-key org-mode-map "\C-c$" 'org-archive-subtree)
  14685. (define-key org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  14686. (define-key org-mode-map "\C-c\C-j" 'org-goto)
  14687. (define-key org-mode-map "\C-c\C-t" 'org-todo)
  14688. (define-key org-mode-map "\C-c\C-s" 'org-schedule)
  14689. (define-key org-mode-map "\C-c\C-d" 'org-deadline)
  14690. (define-key org-mode-map "\C-c;" 'org-toggle-comment)
  14691. (define-key org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  14692. (define-key org-mode-map "\C-c\C-w" 'org-check-deadlines)
  14693. (define-key org-mode-map "\C-c/" 'org-occur) ; Minor-mode reserved
  14694. (define-key org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  14695. (define-key org-mode-map "\C-c\C-m" 'org-insert-heading)
  14696. (define-key org-mode-map "\M-\C-m" 'org-insert-heading)
  14697. (define-key org-mode-map "\C-c\C-l" 'org-insert-link)
  14698. (define-key org-mode-map "\C-c\C-o" 'org-open-at-point)
  14699. (define-key org-mode-map "\C-c%" 'org-mark-ring-push)
  14700. (define-key org-mode-map "\C-c&" 'org-mark-ring-goto)
  14701. (define-key org-mode-map "\C-c\C-z" 'org-time-stamp) ; Alternative binding
  14702. (define-key org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  14703. (define-key org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  14704. (define-key org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  14705. (define-key org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  14706. (define-key org-mode-map "\C-c>" 'org-goto-calendar)
  14707. (define-key org-mode-map "\C-c<" 'org-date-from-calendar)
  14708. (define-key org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  14709. (define-key org-mode-map "\C-c[" 'org-agenda-file-to-front)
  14710. (define-key org-mode-map "\C-c]" 'org-remove-file)
  14711. (define-key org-mode-map "\C-c-" 'org-table-insert-hline)
  14712. (define-key org-mode-map "\C-c^" 'org-table-sort-lines)
  14713. (define-key org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  14714. (define-key org-mode-map "\C-c#" 'org-update-checkbox-count)
  14715. (define-key org-mode-map "\C-m" 'org-return)
  14716. (define-key org-mode-map "\C-c?" 'org-table-current-column)
  14717. (define-key org-mode-map "\C-c " 'org-table-blank-field)
  14718. (define-key org-mode-map "\C-c+" 'org-table-sum)
  14719. (define-key org-mode-map "\C-c=" 'org-table-eval-formula)
  14720. (define-key org-mode-map "\C-c'" 'org-table-edit-formulas)
  14721. (define-key org-mode-map "\C-c`" 'org-table-edit-field)
  14722. (define-key org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  14723. (define-key org-mode-map "\C-c*" 'org-table-recalculate)
  14724. (define-key org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  14725. (define-key org-mode-map "\C-c~" 'org-table-create-with-table.el)
  14726. (define-key org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  14727. (define-key org-mode-map "\C-c\C-e" 'org-export)
  14728. (define-key org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  14729. (define-key org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  14730. (define-key org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  14731. (define-key org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  14732. (define-key org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  14733. (define-key org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  14734. (define-key org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  14735. (define-key org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  14736. (define-key org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  14737. (define-key org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  14738. (define-key org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  14739. (define-key org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  14740. (define-key org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  14741. (when (featurep 'xemacs)
  14742. (define-key org-mode-map 'button3 'popup-mode-menu))
  14743. (defsubst org-table-p () (org-at-table-p))
  14744. (defun org-self-insert-command (N)
  14745. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  14746. If the cursor is in a table looking at whitespace, the whitespace is
  14747. overwritten, and the table is not marked as requiring realignment."
  14748. (interactive "p")
  14749. (if (and (org-table-p)
  14750. (progn
  14751. ;; check if we blank the field, and if that triggers align
  14752. (and org-table-auto-blank-field
  14753. (member last-command
  14754. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  14755. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  14756. ;; got extra space, this field does not determine column width
  14757. (let (org-table-may-need-update) (org-table-blank-field))
  14758. ;; no extra space, this field may determine column width
  14759. (org-table-blank-field)))
  14760. t)
  14761. (eq N 1)
  14762. (looking-at "[^|\n]* |"))
  14763. (let (org-table-may-need-update)
  14764. (goto-char (1- (match-end 0)))
  14765. (delete-backward-char 1)
  14766. (goto-char (match-beginning 0))
  14767. (self-insert-command N))
  14768. (setq org-table-may-need-update t)
  14769. (self-insert-command N)))
  14770. (defun org-delete-backward-char (N)
  14771. "Like `delete-backward-char', insert whitespace at field end in tables.
  14772. When deleting backwards, in tables this function will insert whitespace in
  14773. front of the next \"|\" separator, to keep the table aligned. The table will
  14774. still be marked for re-alignment if the field did fill the entire column,
  14775. because, in this case the deletion might narrow the column."
  14776. (interactive "p")
  14777. (if (and (org-table-p)
  14778. (eq N 1)
  14779. (string-match "|" (buffer-substring (point-at-bol) (point)))
  14780. (looking-at ".*?|"))
  14781. (let ((pos (point))
  14782. (noalign (looking-at "[^|\n\r]* |"))
  14783. (c org-table-may-need-update))
  14784. (backward-delete-char N)
  14785. (skip-chars-forward "^|")
  14786. (insert " ")
  14787. (goto-char (1- pos))
  14788. ;; noalign: if there were two spaces at the end, this field
  14789. ;; does not determine the width of the column.
  14790. (if noalign (setq org-table-may-need-update c)))
  14791. (backward-delete-char N)))
  14792. (defun org-delete-char (N)
  14793. "Like `delete-char', but insert whitespace at field end in tables.
  14794. When deleting characters, in tables this function will insert whitespace in
  14795. front of the next \"|\" separator, to keep the table aligned. The table will
  14796. still be marked for re-alignment if the field did fill the entire column,
  14797. because, in this case the deletion might narrow the column."
  14798. (interactive "p")
  14799. (if (and (org-table-p)
  14800. (not (bolp))
  14801. (not (= (char-after) ?|))
  14802. (eq N 1))
  14803. (if (looking-at ".*?|")
  14804. (let ((pos (point))
  14805. (noalign (looking-at "[^|\n\r]* |"))
  14806. (c org-table-may-need-update))
  14807. (replace-match (concat
  14808. (substring (match-string 0) 1 -1)
  14809. " |"))
  14810. (goto-char pos)
  14811. ;; noalign: if there were two spaces at the end, this field
  14812. ;; does not determine the width of the column.
  14813. (if noalign (setq org-table-may-need-update c)))
  14814. (delete-char N))
  14815. (delete-char N)))
  14816. ;; How to do this: Measure non-white length of current string
  14817. ;; If equal to column width, we should realign.
  14818. (defun org-remap (map &rest commands)
  14819. "In MAP, remap the functions given in COMMANDS.
  14820. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  14821. (let (new old)
  14822. (while commands
  14823. (setq old (pop commands) new (pop commands))
  14824. (if (fboundp 'command-remapping)
  14825. (define-key map (vector 'remap old) new)
  14826. (substitute-key-definition old new map global-map)))))
  14827. (when (eq org-enable-table-editor 'optimized)
  14828. ;; If the user wants maximum table support, we need to hijack
  14829. ;; some standard editing functions
  14830. (org-remap org-mode-map
  14831. 'self-insert-command 'org-self-insert-command
  14832. 'delete-char 'org-delete-char
  14833. 'delete-backward-char 'org-delete-backward-char)
  14834. (define-key org-mode-map "|" 'org-force-self-insert))
  14835. (defun org-shiftcursor-error ()
  14836. "Throw an error because Shift-Cursor command was applied in wrong context."
  14837. (error "This command is active in special context like tables, headlines or timestamps"))
  14838. (defun org-shifttab (&optional arg)
  14839. "Global visibility cycling or move to previous table field.
  14840. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  14841. on context.
  14842. See the individual commands for more information."
  14843. (interactive "P")
  14844. (cond
  14845. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  14846. (t (call-interactively 'org-global-cycle))))
  14847. (defun org-shiftmetaleft ()
  14848. "Promote subtree or delete table column.
  14849. Calls `org-promote-subtree' or `org-table-delete-column', depending on context.
  14850. See the individual commands for more information."
  14851. (interactive)
  14852. (cond
  14853. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  14854. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  14855. ((org-at-item-p) (call-interactively 'org-outdent-item))
  14856. (t (org-shiftcursor-error))))
  14857. (defun org-shiftmetaright ()
  14858. "Demote subtree or insert table column.
  14859. Calls `org-demote-subtree' or `org-table-insert-column', depending on context.
  14860. See the individual commands for more information."
  14861. (interactive)
  14862. (cond
  14863. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  14864. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  14865. ((org-at-item-p) (call-interactively 'org-indent-item))
  14866. (t (org-shiftcursor-error))))
  14867. (defun org-shiftmetaup (&optional arg)
  14868. "Move subtree up or kill table row.
  14869. Calls `org-move-subtree-up' or `org-table-kill-row' or
  14870. `org-move-item-up' depending on context. See the individual commands
  14871. for more information."
  14872. (interactive "P")
  14873. (cond
  14874. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  14875. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  14876. ((org-at-item-p) (call-interactively 'org-move-item-up))
  14877. (t (org-shiftcursor-error))))
  14878. (defun org-shiftmetadown (&optional arg)
  14879. "Move subtree down or insert table row.
  14880. Calls `org-move-subtree-down' or `org-table-insert-row' or
  14881. `org-move-item-down', depending on context. See the individual
  14882. commands for more information."
  14883. (interactive "P")
  14884. (cond
  14885. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  14886. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  14887. ((org-at-item-p) (call-interactively 'org-move-item-down))
  14888. (t (org-shiftcursor-error))))
  14889. (defun org-metaleft (&optional arg)
  14890. "Promote heading or move table column to left.
  14891. Calls `org-do-promote' or `org-table-move-column', depending on context.
  14892. With no specific context, calls the Emacs default `backward-word'.
  14893. See the individual commands for more information."
  14894. (interactive "P")
  14895. (cond
  14896. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  14897. ((or (org-on-heading-p) (org-region-active-p))
  14898. (call-interactively 'org-do-promote))
  14899. (t (call-interactively 'backward-word))))
  14900. (defun org-metaright (&optional arg)
  14901. "Demote subtree or move table column to right.
  14902. Calls `org-do-demote' or `org-table-move-column', depending on context.
  14903. With no specific context, calls the Emacs default `forward-word'.
  14904. See the individual commands for more information."
  14905. (interactive "P")
  14906. (cond
  14907. ((org-at-table-p) (call-interactively 'org-table-move-column))
  14908. ((or (org-on-heading-p) (org-region-active-p))
  14909. (call-interactively 'org-do-demote))
  14910. (t (call-interactively 'forward-word))))
  14911. (defun org-metaup (&optional arg)
  14912. "Move subtree up or move table row up.
  14913. Calls `org-move-subtree-up' or `org-table-move-row' or
  14914. `org-move-item-up', depending on context. See the individual commands
  14915. for more information."
  14916. (interactive "P")
  14917. (cond
  14918. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  14919. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  14920. ((org-at-item-p) (call-interactively 'org-move-item-up))
  14921. (t (org-shiftcursor-error))))
  14922. (defun org-metadown (&optional arg)
  14923. "Move subtree down or move table row down.
  14924. Calls `org-move-subtree-down' or `org-table-move-row' or
  14925. `org-move-item-down', depending on context. See the individual
  14926. commands for more information."
  14927. (interactive "P")
  14928. (cond
  14929. ((org-at-table-p) (call-interactively 'org-table-move-row))
  14930. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  14931. ((org-at-item-p) (call-interactively 'org-move-item-down))
  14932. (t (org-shiftcursor-error))))
  14933. (defun org-shiftup (&optional arg)
  14934. "Increase item in timestamp or increase priority of current headline.
  14935. Calls `org-timestamp-up' or `org-priority-up', depending on context.
  14936. See the individual commands for more information."
  14937. (interactive "P")
  14938. (cond
  14939. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up))
  14940. ((org-on-heading-p) (call-interactively 'org-priority-up))
  14941. ((org-at-item-p) (call-interactively 'org-previous-item))
  14942. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  14943. (defun org-shiftdown (&optional arg)
  14944. "Decrease item in timestamp or decrease priority of current headline.
  14945. Calls `org-timestamp-down' or `org-priority-down', depending on context.
  14946. See the individual commands for more information."
  14947. (interactive "P")
  14948. (cond
  14949. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down))
  14950. ((org-on-heading-p) (call-interactively 'org-priority-down))
  14951. (t (call-interactively 'org-next-item))))
  14952. (defun org-shiftright ()
  14953. "Next TODO keyword or timestamp one day later, depending on context."
  14954. (interactive)
  14955. (cond
  14956. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  14957. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  14958. (t (org-shiftcursor-error))))
  14959. (defun org-shiftleft ()
  14960. "Previous TODO keyword or timestamp one day earlier, depending on context."
  14961. (interactive)
  14962. (cond
  14963. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  14964. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  14965. (t (org-shiftcursor-error))))
  14966. (defun org-copy-special ()
  14967. "Copy region in table or copy current subtree.
  14968. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  14969. See the individual commands for more information."
  14970. (interactive)
  14971. (call-interactively
  14972. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  14973. (defun org-cut-special ()
  14974. "Cut region in table or cut current subtree.
  14975. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  14976. See the individual commands for more information."
  14977. (interactive)
  14978. (call-interactively
  14979. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  14980. (defun org-paste-special (arg)
  14981. "Paste rectangular region into table, or past subtree relative to level.
  14982. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  14983. See the individual commands for more information."
  14984. (interactive "P")
  14985. (if (org-at-table-p)
  14986. (org-table-paste-rectangle)
  14987. (org-paste-subtree arg)))
  14988. (defun org-ctrl-c-ctrl-c (&optional arg)
  14989. "Set tags in headline, or update according to changed information at point.
  14990. This command does many different things, depending on context:
  14991. - If the cursor is in a headline, prompt for tags and insert them
  14992. into the current line, aligned to `org-tags-column'. When called
  14993. with prefix arg, realign all tags in the current buffer.
  14994. - If the cursor is in one of the special #+KEYWORD lines, this
  14995. triggers scanning the buffer for these lines and updating the
  14996. information.
  14997. - If the cursor is inside a table, realign the table. This command
  14998. works even if the automatic table editor has been turned off.
  14999. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  15000. the entire table.
  15001. - If the cursor is inside a table created by the table.el package,
  15002. activate that table.
  15003. - If the current buffer is a remember buffer, close note and file it.
  15004. with a prefix argument, file it without further interaction to the default
  15005. location.
  15006. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  15007. links in this buffer.
  15008. - If the cursor is on a numbered item in a plain list, renumber the
  15009. ordered list."
  15010. (interactive "P")
  15011. (let ((org-enable-table-editor t))
  15012. (cond
  15013. ((or org-clock-overlays
  15014. org-occur-highlights
  15015. org-latex-fragment-image-overlays)
  15016. (org-remove-clock-overlays)
  15017. (org-remove-occur-highlights)
  15018. (org-remove-latex-fragment-image-overlays)
  15019. (message "Temporary highlights/overlays removed from current buffer"))
  15020. ((and (local-variable-p 'org-finish-function (current-buffer))
  15021. (fboundp org-finish-function))
  15022. (funcall org-finish-function))
  15023. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  15024. ((org-on-heading-p) (call-interactively 'org-set-tags))
  15025. ((org-at-table.el-p)
  15026. (require 'table)
  15027. (beginning-of-line 1)
  15028. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  15029. (call-interactively 'table-recognize-table))
  15030. ((org-at-table-p)
  15031. (org-table-maybe-eval-formula)
  15032. (if arg
  15033. (call-interactively 'org-table-recalculate)
  15034. (org-table-maybe-recalculate-line))
  15035. (call-interactively 'org-table-align))
  15036. ((org-at-item-checkbox-p)
  15037. (call-interactively 'org-toggle-checkbox))
  15038. ((org-at-item-p)
  15039. (call-interactively 'org-renumber-ordered-list))
  15040. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  15041. (cond
  15042. ((equal (match-string 1) "TBLFM")
  15043. ;; Recalculate the table before this line
  15044. (save-excursion
  15045. (beginning-of-line 1)
  15046. (skip-chars-backward " \r\n\t")
  15047. (if (org-at-table-p)
  15048. (org-call-with-arg 'org-table-recalculate t))))
  15049. (t
  15050. (call-interactively 'org-mode-restart))))
  15051. (t (error "C-c C-c can do nothing useful at this location.")))))
  15052. (defun org-mode-restart ()
  15053. "Restart Org-mode, to scan again for special lines.
  15054. Also updates the keyword regular expressions."
  15055. (interactive)
  15056. (let ((org-inhibit-startup t)) (org-mode))
  15057. (message "Org-mode restarted to refresh keyword and special line setup"))
  15058. (defun org-return ()
  15059. "Goto next table row or insert a newline.
  15060. Calls `org-table-next-row' or `newline', depending on context.
  15061. See the individual commands for more information."
  15062. (interactive)
  15063. (cond
  15064. ((org-at-table-p)
  15065. (org-table-justify-field-maybe)
  15066. (call-interactively 'org-table-next-row))
  15067. (t (newline))))
  15068. (defun org-meta-return (&optional arg)
  15069. "Insert a new heading or wrap a region in a table.
  15070. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  15071. See the individual commands for more information."
  15072. (interactive "P")
  15073. (cond
  15074. ((org-at-table-p)
  15075. (call-interactively 'org-table-wrap-region))
  15076. (t (call-interactively 'org-insert-heading))))
  15077. ;;; Menu entries
  15078. ;; Define the Org-mode menus
  15079. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  15080. '("Tbl"
  15081. ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
  15082. ["Next Field" org-cycle (org-at-table-p)]
  15083. ["Previous Field" org-shifttab (org-at-table-p)]
  15084. ["Next Row" org-return (org-at-table-p)]
  15085. "--"
  15086. ["Blank Field" org-table-blank-field (org-at-table-p)]
  15087. ["Edit Field" org-table-edit-field (org-at-table-p)]
  15088. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  15089. "--"
  15090. ("Column"
  15091. ["Move Column Left" org-metaleft (org-at-table-p)]
  15092. ["Move Column Right" org-metaright (org-at-table-p)]
  15093. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  15094. ["Insert Column" org-shiftmetaright (org-at-table-p)]
  15095. "--"
  15096. ["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])
  15097. ("Row"
  15098. ["Move Row Up" org-metaup (org-at-table-p)]
  15099. ["Move Row Down" org-metadown (org-at-table-p)]
  15100. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  15101. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  15102. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  15103. "--"
  15104. ["Insert Hline" org-table-insert-hline (org-at-table-p)])
  15105. ("Rectangle"
  15106. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  15107. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  15108. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  15109. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  15110. "--"
  15111. ("Calculate"
  15112. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  15113. ["Set Named Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  15114. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  15115. "--"
  15116. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  15117. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  15118. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  15119. "--"
  15120. ["Sum Column/Rectangle" org-table-sum
  15121. (or (org-at-table-p) (org-region-active-p))]
  15122. ["Which Column?" org-table-current-column (org-at-table-p)])
  15123. ["Debug Formulas"
  15124. (setq org-table-formula-debug (not org-table-formula-debug))
  15125. :style toggle :selected org-table-formula-debug]
  15126. "--"
  15127. ["Create" org-table-create (and (not (org-at-table-p))
  15128. org-enable-table-editor)]
  15129. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  15130. ["Import from File" org-table-import (not (org-at-table-p))]
  15131. ["Export to File" org-table-export (org-at-table-p)]
  15132. "--"
  15133. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  15134. (easy-menu-define org-org-menu org-mode-map "Org menu"
  15135. '("Org"
  15136. ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
  15137. ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
  15138. ["Sparse Tree" org-occur t]
  15139. ["Show All" show-all t]
  15140. "--"
  15141. ["New Heading" org-insert-heading t]
  15142. ("Navigate Headings"
  15143. ["Up" outline-up-heading t]
  15144. ["Next" outline-next-visible-heading t]
  15145. ["Previous" outline-previous-visible-heading t]
  15146. ["Next Same Level" outline-forward-same-level t]
  15147. ["Previous Same Level" outline-backward-same-level t]
  15148. "--"
  15149. ["Jump" org-goto t])
  15150. ("Edit Structure"
  15151. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  15152. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  15153. "--"
  15154. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  15155. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  15156. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  15157. "--"
  15158. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  15159. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  15160. ["Demote Heading" org-metaright (not (org-at-table-p))]
  15161. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  15162. "--"
  15163. ["Convert to odd levels" org-convert-to-odd-levels t]
  15164. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  15165. ("Archive"
  15166. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  15167. ["Check and Tag Children" (org-toggle-archive-tag (4))
  15168. :active t :keys "C-u C-c C-x C-a"]
  15169. ["Sparse trees open ARCHIVE trees"
  15170. (setq org-sparse-tree-open-archived-trees
  15171. (not org-sparse-tree-open-archived-trees))
  15172. :style toggle :selected org-sparse-tree-open-archived-trees]
  15173. ["Cycling opens ARCHIVE trees"
  15174. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  15175. :style toggle :selected org-cycle-open-archived-trees]
  15176. ["Agenda includes ARCHIVE trees"
  15177. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  15178. :style toggle :selected (not org-agenda-skip-archived-trees)]
  15179. "--"
  15180. ["Move Subtree to Archive" org-archive-subtree t]
  15181. ["Check and Move Children" (org-archive-subtree '(4))
  15182. :active t :keys "C-u C-c $"])
  15183. "--"
  15184. ("TODO Lists"
  15185. ["TODO/DONE/-" org-todo t]
  15186. ("Select keyword"
  15187. ["Next keyword" org-shiftright (org-on-heading-p)]
  15188. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  15189. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))])
  15190. ["Show TODO Tree" org-show-todo-tree t]
  15191. ["Global TODO list" org-todo-list t]
  15192. "--"
  15193. ["Set Priority" org-priority t]
  15194. ["Priority Up" org-shiftup t]
  15195. ["Priority Down" org-shiftdown t]
  15196. "--"
  15197. ; ["Insert Checkbox" org-insert-todo-heading (org-in-item-p)]
  15198. ; ["Toggle Checkbox" org-ctrl-c-ctrl-c (org-at-item-checkbox-p)]
  15199. ; ["Insert [n/m] cookie" (progn (insert "[/]") (org-update-checkbox-count))
  15200. ; (or (org-on-heading-p) (org-at-item-p))]
  15201. ; ["Insert [%] cookie" (progn (insert "[%]") (org-update-checkbox-count))
  15202. ; (or (org-on-heading-p) (org-at-item-p))]
  15203. ; ["Update Statistics" org-update-checkbox-count t]
  15204. )
  15205. ("Dates and Scheduling"
  15206. ["Timestamp" org-time-stamp t]
  15207. ["Timestamp (inactive)" org-time-stamp-inactive t]
  15208. ("Change Date"
  15209. ["1 Day Later" org-timestamp-up-day t]
  15210. ["1 Day Earlier" org-timestamp-down-day t]
  15211. ["1 ... Later" org-shiftup t]
  15212. ["1 ... Earlier" org-shiftdown t])
  15213. ["Compute Time Range" org-evaluate-time-range t]
  15214. ["Schedule Item" org-schedule t]
  15215. ["Deadline" org-deadline t]
  15216. "--"
  15217. ["Goto Calendar" org-goto-calendar t]
  15218. ["Date from Calendar" org-date-from-calendar t])
  15219. ("Logging work"
  15220. ["Clock in" org-clock-in t]
  15221. ["Clock out" org-clock-out t]
  15222. ["Clock cancel" org-clock-cancel t]
  15223. ["Display times" org-clock-display t]
  15224. ["Create clock table" org-clock-report t]
  15225. "--"
  15226. ["Record DONE time"
  15227. (progn (setq org-log-done (not org-log-done))
  15228. (message "Switching to %s will %s record a timestamp"
  15229. org-done-string
  15230. (if org-log-done "automatically" "not")))
  15231. :style toggle :selected org-log-done])
  15232. "--"
  15233. ["Agenda Command..." org-agenda t]
  15234. ("File List for Agenda")
  15235. ("Special views current file"
  15236. ["TODO Tree" org-show-todo-tree t]
  15237. ["Check Deadlines" org-check-deadlines t]
  15238. ["Timeline" org-timeline t]
  15239. ["Tags Tree" org-tags-sparse-tree t])
  15240. "--"
  15241. ("Hyperlinks"
  15242. ["Store Link (Global)" org-store-link t]
  15243. ["Insert Link" org-insert-link t]
  15244. ["Follow Link" org-open-at-point t]
  15245. "--"
  15246. ["Descriptive Links"
  15247. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  15248. :style radio :selected (member '(org-link) buffer-invisibility-spec)]
  15249. ["Literal Links"
  15250. (progn
  15251. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  15252. :style radio :selected (not (member '(org-link) buffer-invisibility-spec))]
  15253. "--"
  15254. ["Upgrade all <link> to [[link][desc]]" org-upgrade-old-links
  15255. (save-excursion (goto-char (point-min))
  15256. (re-search-forward "<[a-z]+:" nil t))])
  15257. "--"
  15258. ["Export/Publish..." org-export t]
  15259. ("LaTeX"
  15260. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  15261. :selected org-cdlatex-mode]
  15262. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  15263. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  15264. ["Modify math symbol" org-cdlatex-math-modify
  15265. (org-inside-LaTeX-fragment-p)]
  15266. ["Export LaTeX fragments as images"
  15267. (setq org-export-with-LaTeX-fragments (not org-export-with-LaTeX-fragments))
  15268. :style toggle :selected org-export-with-LaTeX-fragments])
  15269. "--"
  15270. ("Documentation"
  15271. ["Show Version" org-version t]
  15272. ["Info Documentation" org-info t])
  15273. ("Customize"
  15274. ["Browse Org Group" org-customize t]
  15275. "--"
  15276. ["Expand This Menu" org-create-customize-menu
  15277. (fboundp 'customize-menu-create)])
  15278. "--"
  15279. ["Refresh setup" org-mode-restart t]
  15280. ))
  15281. (defun org-info (&optional node)
  15282. "Read documentation for Org-mode in the info system.
  15283. With optional NODE, go directly to that node."
  15284. (interactive)
  15285. (require 'info)
  15286. (Info-goto-node (format "(org)%s" (or node ""))))
  15287. (defun org-install-agenda-files-menu ()
  15288. (let ((bl (buffer-list)))
  15289. (save-excursion
  15290. (while bl
  15291. (set-buffer (pop bl))
  15292. (if (org-mode-p) (setq bl nil)))
  15293. (when (org-mode-p)
  15294. (easy-menu-change
  15295. '("Org") "File List for Agenda"
  15296. (append
  15297. (list
  15298. ["Edit File List" (org-edit-agenda-file-list) t]
  15299. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  15300. ["Remove Current File from List" org-remove-file t]
  15301. ["Cycle through agenda files" org-cycle-agenda-files t]
  15302. "--")
  15303. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  15304. ;;; Documentation
  15305. (defun org-customize ()
  15306. "Call the customize function with org as argument."
  15307. (interactive)
  15308. (customize-browse 'org))
  15309. (defun org-create-customize-menu ()
  15310. "Create a full customization menu for Org-mode, insert it into the menu."
  15311. (interactive)
  15312. (if (fboundp 'customize-menu-create)
  15313. (progn
  15314. (easy-menu-change
  15315. '("Org") "Customize"
  15316. `(["Browse Org group" org-customize t]
  15317. "--"
  15318. ,(customize-menu-create 'org)
  15319. ["Set" Custom-set t]
  15320. ["Save" Custom-save t]
  15321. ["Reset to Current" Custom-reset-current t]
  15322. ["Reset to Saved" Custom-reset-saved t]
  15323. ["Reset to Standard Settings" Custom-reset-standard t]))
  15324. (message "\"Org\"-menu now contains full customization menu"))
  15325. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  15326. ;;; Miscellaneous stuff
  15327. (defun org-context ()
  15328. "Return a list of contexts of the current cursor position.
  15329. If several contexts apply, all are returned.
  15330. Each context entry is a list with a symbol naming the context, and
  15331. two positions indicating start and end of the context. Possible
  15332. contexts are:
  15333. :headline anywhere in a headline
  15334. :headline-stars on the leading stars in a headline
  15335. :todo-keyword on a TODO keyword (including DONE) in a headline
  15336. :tags on the TAGS in a headline
  15337. :priority on the priority cookie in a headline
  15338. :item on the first line of a plain list item
  15339. :item-bullet on the bullet/number of a plain list item
  15340. :checkbox on the checkbox in a plain list item
  15341. :table in an org-mode table
  15342. :table-special on a special filed in a table
  15343. :table-table in a table.el table
  15344. :link on a hyperline
  15345. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  15346. :target on a <<target>>
  15347. :radio-target on a <<<radio-target>>>
  15348. :latex-fragment on a LaTeX fragment
  15349. :latex-preview on a LaTeX fragment with overlayed preview image
  15350. This function expects the position to be visible because it uses font-lock
  15351. faces as a help to recognize the following contexts: :table-special, :link,
  15352. and :keyword."
  15353. (let* ((f (get-text-property (point) 'face))
  15354. (faces (if (listp f) f (list f)))
  15355. (p (point)) clist o)
  15356. ;; First the large context
  15357. (cond
  15358. ((org-on-heading-p)
  15359. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  15360. (when (progn
  15361. (beginning-of-line 1)
  15362. (looking-at org-todo-line-tags-regexp))
  15363. (push (org-point-in-group p 1 :headline-stars) clist)
  15364. (push (org-point-in-group p 2 :todo-keyword) clist)
  15365. (push (org-point-in-group p 4 :tags) clist))
  15366. (goto-char p)
  15367. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  15368. (if (looking-at "\\[#[A-Z]\\]")
  15369. (push (org-point-in-group p 0 :priority) clist)))
  15370. ((org-at-item-p)
  15371. (push (org-point-in-group p 2 :item-bullet) clist)
  15372. (push (list :item (point-at-bol)
  15373. (save-excursion (org-end-of-item) (point)))
  15374. clist)
  15375. (and (org-at-item-checkbox-p)
  15376. (push (org-point-in-group p 0 :checkbox) clist)))
  15377. ((org-at-table-p)
  15378. (push (list :table (org-table-begin) (org-table-end)) clist)
  15379. (if (memq 'org-formula faces)
  15380. (push (list :table-special
  15381. (previous-single-property-change p 'face)
  15382. (next-single-property-change p 'face)) clist)))
  15383. ((org-at-table-p 'any)
  15384. (push (list :table-table) clist)))
  15385. (goto-char p)
  15386. ;; Now the small context
  15387. (cond
  15388. ((org-at-timestamp-p)
  15389. (push (org-point-in-group p 0 :timestamp) clist))
  15390. ((memq 'org-link faces)
  15391. (push (list :link
  15392. (previous-single-property-change p 'face)
  15393. (next-single-property-change p 'face)) clist))
  15394. ((memq 'org-special-keyword faces)
  15395. (push (list :keyword
  15396. (previous-single-property-change p 'face)
  15397. (next-single-property-change p 'face)) clist))
  15398. ((org-on-target-p)
  15399. (push (org-point-in-group p 0 :target) clist)
  15400. (goto-char (1- (match-beginning 0)))
  15401. (if (looking-at org-radio-target-regexp)
  15402. (push (org-point-in-group p 0 :radio-target) clist))
  15403. (goto-char p))
  15404. ((setq o (car (delq nil
  15405. (mapcar
  15406. (lambda (x)
  15407. (if (memq x org-latex-fragment-image-overlays) x))
  15408. (org-overlays-at (point))))))
  15409. (push (list :latex-fragment
  15410. (org-overlay-start o) (org-overlay-end o)) clist)
  15411. (push (list :latex-preview
  15412. (org-overlay-start o) (org-overlay-end o)) clist))
  15413. ((org-inside-LaTeX-fragment-p)
  15414. ;; FIXME: positions wrong.
  15415. (push (list :latex-fragment (point) (point)) clist)))
  15416. (setq clist (nreverse (delq nil clist)))
  15417. clist))
  15418. (defun org-point-in-group (point group &optional context)
  15419. "Check if POINT is in match-group GROUP.
  15420. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  15421. match. If the match group does ot exist or point is not inside it,
  15422. return nil."
  15423. (and (match-beginning group)
  15424. (>= point (match-beginning group))
  15425. (<= point (match-end group))
  15426. (if context
  15427. (list context (match-beginning group) (match-end group))
  15428. t)))
  15429. (defun org-move-line-down (arg)
  15430. "Move the current line down. With prefix argument, move it past ARG lines."
  15431. (interactive "p")
  15432. (let ((col (current-column))
  15433. beg end pos)
  15434. (beginning-of-line 1) (setq beg (point))
  15435. (beginning-of-line 2) (setq end (point))
  15436. (beginning-of-line (+ 1 arg))
  15437. (setq pos (move-marker (make-marker) (point)))
  15438. (insert (delete-and-extract-region beg end))
  15439. (goto-char pos)
  15440. (move-to-column col)))
  15441. (defun org-move-line-up (arg)
  15442. "Move the current line up. With prefix argument, move it past ARG lines."
  15443. (interactive "p")
  15444. (let ((col (current-column))
  15445. beg end pos)
  15446. (beginning-of-line 1) (setq beg (point))
  15447. (beginning-of-line 2) (setq end (point))
  15448. (beginning-of-line (- arg))
  15449. (setq pos (move-marker (make-marker) (point)))
  15450. (insert (delete-and-extract-region beg end))
  15451. (goto-char pos)
  15452. (move-to-column col)))
  15453. ;; Paragraph filling stuff.
  15454. ;; We want this to be just right, so use the full arsenal.
  15455. (defun org-set-autofill-regexps ()
  15456. (interactive)
  15457. ;; In the paragraph separator we include headlines, because filling
  15458. ;; text in a line directly attached to a headline would otherwise
  15459. ;; fill the headline as well.
  15460. (org-set-local 'comment-start-skip "^#+[ \t]*")
  15461. (org-set-local 'paragraph-separate "\f\\|\\*\\|[ ]*$\\|[ \t]*[:|]")
  15462. ;; The paragraph starter includes hand-formatted lists.
  15463. (org-set-local 'paragraph-start
  15464. "\f\\|[ ]*$\\|\\([*\f]+\\)\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  15465. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  15466. ;; But only if the user has not turned off tables or fixed-width regions
  15467. (org-set-local
  15468. 'auto-fill-inhibit-regexp
  15469. (concat "\\*\\|#"
  15470. "\\|[ \t]*" org-keyword-time-regexp
  15471. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  15472. (concat
  15473. "\\|[ \t]*["
  15474. (if org-enable-table-editor "|" "")
  15475. (if org-enable-fixed-width-editor ":" "")
  15476. "]"))))
  15477. ;; We use our own fill-paragraph function, to make sure that tables
  15478. ;; and fixed-width regions are not wrapped. That function will pass
  15479. ;; through to `fill-paragraph' when appropriate.
  15480. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  15481. ; Adaptive filling: To get full control, first make sure that
  15482. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  15483. (org-set-local 'adaptive-fill-regexp "\000")
  15484. (org-set-local 'adaptive-fill-function
  15485. 'org-adaptive-fill-function))
  15486. (defun org-fill-paragraph (&optional justify)
  15487. "Re-align a table, pass through to fill-paragraph if no table."
  15488. (let ((table-p (org-at-table-p))
  15489. (table.el-p (org-at-table.el-p)))
  15490. (cond ((equal (char-after (point-at-bol)) ?*) t) ; skip headlines
  15491. (table.el-p t) ; skip table.el tables
  15492. (table-p (org-table-align) t) ; align org-mode tables
  15493. (t nil)))) ; call paragraph-fill
  15494. ;; For reference, this is the default value of adaptive-fill-regexp
  15495. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  15496. (defun org-adaptive-fill-function ()
  15497. "Return a fill prefix for org-mode files.
  15498. In particular, this makes sure hanging paragraphs for hand-formatted lists
  15499. work correctly."
  15500. (if (looking-at " *\\([-*+] \\|[0-9]+[.)] \\)?")
  15501. (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  15502. ;; Functions needed for Emacs/XEmacs region compatibility
  15503. (defun org-add-hook (hook function &optional append local)
  15504. "Add-hook, compatible with both Emacsen."
  15505. (if (and local (featurep 'xemacs))
  15506. (add-local-hook hook function append)
  15507. (add-hook hook function append local)))
  15508. (defun org-region-active-p ()
  15509. "Is `transient-mark-mode' on and the region active?
  15510. Works on both Emacs and XEmacs."
  15511. (if org-ignore-region
  15512. nil
  15513. (if (featurep 'xemacs)
  15514. (and zmacs-regions (region-active-p))
  15515. (and transient-mark-mode mark-active))))
  15516. (defun org-add-to-invisibility-spec (arg)
  15517. "Add elements to `buffer-invisibility-spec'.
  15518. See documentation for `buffer-invisibility-spec' for the kind of elements
  15519. that can be added."
  15520. (cond
  15521. ((fboundp 'add-to-invisibility-spec)
  15522. (add-to-invisibility-spec arg))
  15523. ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
  15524. (setq buffer-invisibility-spec (list arg)))
  15525. (t
  15526. (setq buffer-invisibility-spec
  15527. (cons arg buffer-invisibility-spec)))))
  15528. (defun org-remove-from-invisibility-spec (arg)
  15529. "Remove elements from `buffer-invisibility-spec'."
  15530. (if (fboundp 'remove-from-invisibility-spec)
  15531. (remove-from-invisibility-spec arg)
  15532. (if (consp buffer-invisibility-spec)
  15533. (setq buffer-invisibility-spec
  15534. (delete arg buffer-invisibility-spec)))))
  15535. (defun org-in-invisibility-spec-p (arg)
  15536. "Is ARG a member of `buffer-invisibility-spec'?"
  15537. (if (consp buffer-invisibility-spec)
  15538. (member arg buffer-invisibility-spec)
  15539. nil))
  15540. (defun org-image-file-name-regexp ()
  15541. "Return regexp matching the file names of images."
  15542. (if (fboundp 'image-file-name-regexp)
  15543. (image-file-name-regexp)
  15544. (let ((image-file-name-extensions
  15545. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  15546. "xbm" "xpm" "pbm" "pgm" "ppm")))
  15547. (concat "\\."
  15548. (regexp-opt (nconc (mapcar 'upcase
  15549. image-file-name-extensions)
  15550. image-file-name-extensions)
  15551. t)
  15552. "\\'"))))
  15553. ;; Functions extending outline functionality
  15554. ;; C-a should go to the beginning of a *visible* line, also in the
  15555. ;; new outline.el. I guess this should be patched into Emacs?
  15556. (defun org-beginning-of-line ()
  15557. "Go to the beginning of the current line. If that is invisible, continue
  15558. to a visible line beginning. This makes the function of C-a more intuitive."
  15559. (interactive)
  15560. (beginning-of-line 1)
  15561. (if (bobp)
  15562. nil
  15563. (backward-char 1)
  15564. (if (org-invisible-p)
  15565. (while (and (not (bobp)) (org-invisible-p))
  15566. (backward-char 1)
  15567. (beginning-of-line 1))
  15568. (forward-char 1))))
  15569. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  15570. (defun org-invisible-p ()
  15571. "Check if point is at a character currently not visible."
  15572. ;; Early versions of noutline don't have `outline-invisible-p'.
  15573. (if (fboundp 'outline-invisible-p)
  15574. (outline-invisible-p)
  15575. (get-char-property (point) 'invisible)))
  15576. (defun org-invisible-p2 ()
  15577. "Check if point is at a character currently not visible."
  15578. (save-excursion
  15579. (if (and (eolp) (not (bobp))) (backward-char 1))
  15580. ;; Early versions of noutline don't have `outline-invisible-p'.
  15581. (if (fboundp 'outline-invisible-p)
  15582. (outline-invisible-p)
  15583. (get-char-property (point) 'invisible))))
  15584. (defalias 'org-back-to-heading 'outline-back-to-heading)
  15585. (defalias 'org-on-heading-p 'outline-on-heading-p)
  15586. (defun org-on-target-p ()
  15587. (let ((pos (point)))
  15588. (save-excursion
  15589. (skip-chars-forward "<")
  15590. (and (re-search-backward "<<" nil t)
  15591. (or (looking-at org-radio-target-regexp)
  15592. (looking-at org-target-regexp))
  15593. (<= (match-beginning 0) pos)
  15594. (>= (1+ (match-end 0)) pos)))))
  15595. (defun org-up-heading-all (arg)
  15596. "Move to the heading line of which the present line is a subheading.
  15597. This function considers both visible and invisible heading lines.
  15598. With argument, move up ARG levels."
  15599. (if (fboundp 'outline-up-heading-all)
  15600. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  15601. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  15602. (defun org-show-hidden-entry ()
  15603. "Show an entry where even the heading is hidden."
  15604. (save-excursion
  15605. (org-show-entry)))
  15606. (defun org-flag-heading (flag &optional entry)
  15607. "Flag the current heading. FLAG non-nil means make invisible.
  15608. When ENTRY is non-nil, show the entire entry."
  15609. (save-excursion
  15610. (org-back-to-heading t)
  15611. ;; Check if we should show the entire entry
  15612. (if entry
  15613. (progn
  15614. (org-show-entry)
  15615. (save-excursion
  15616. (and (outline-next-heading)
  15617. (org-flag-heading nil))))
  15618. (outline-flag-region (max 1 (1- (point)))
  15619. (save-excursion (outline-end-of-heading) (point))
  15620. flag))))
  15621. (defun org-end-of-subtree (&optional invisible-OK)
  15622. ;; This is an exact copy of the original function, but it uses
  15623. ;; `org-back-to-heading', to make it work also in invisible
  15624. ;; trees. And is uses an invisible-OK argument.
  15625. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  15626. (org-back-to-heading invisible-OK)
  15627. (let ((first t)
  15628. (level (funcall outline-level)))
  15629. (while (and (not (eobp))
  15630. (or first (> (funcall outline-level) level)))
  15631. (setq first nil)
  15632. (outline-next-heading))
  15633. (if (memq (preceding-char) '(?\n ?\^M))
  15634. (progn
  15635. ;; Go to end of line before heading
  15636. (forward-char -1)
  15637. (if (memq (preceding-char) '(?\n ?\^M))
  15638. ;; leave blank line before heading
  15639. (forward-char -1)))))
  15640. (point))
  15641. (defun org-show-subtree ()
  15642. "Show everything after this heading at deeper levels."
  15643. (outline-flag-region
  15644. (point)
  15645. (save-excursion
  15646. (outline-end-of-subtree) (outline-next-heading) (point))
  15647. nil))
  15648. (defun org-show-entry ()
  15649. "Show the body directly following this heading.
  15650. Show the heading too, if it is currently invisible."
  15651. (interactive)
  15652. (save-excursion
  15653. (org-back-to-heading t)
  15654. (outline-flag-region
  15655. (max 1 (1- (point)))
  15656. (save-excursion
  15657. (re-search-forward (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  15658. (or (match-beginning 1) (point-max)))
  15659. nil)))
  15660. (defun org-make-options-regexp (kwds)
  15661. "Make a regular expression for keyword lines."
  15662. (concat
  15663. "^"
  15664. "#?[ \t]*\\+\\("
  15665. (mapconcat 'regexp-quote kwds "\\|")
  15666. "\\):[ \t]*"
  15667. "\\(.+\\)"))
  15668. ;; Make `bookmark-jump' show the jump location if it was hidden.
  15669. (eval-after-load "bookmark"
  15670. '(if (boundp 'bookmark-after-jump-hook)
  15671. ;; We can use the hook
  15672. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  15673. ;; Hook not available, use advice
  15674. (defadvice bookmark-jump (after org-make-visible activate)
  15675. "Make the position visible."
  15676. (org-bookmark-jump-unhide))))
  15677. (defun org-bookmark-jump-unhide ()
  15678. "Unhide the current position, to show the bookmark location."
  15679. (and (org-mode-p)
  15680. (or (org-invisible-p)
  15681. (save-excursion (goto-char (max (point-min) (1- (point))))
  15682. (org-invisible-p)))
  15683. (org-show-hierarchy-above 'bookmark-jump)))
  15684. ;;; Experimental code
  15685. ;(add-hook 'org-load-hook
  15686. ; (lambda ()
  15687. ; (defun outline-show-heading ()
  15688. ; "Show the current heading and move to its end."
  15689. ; (outline-flag-region
  15690. ; (- (point)
  15691. ; (cond
  15692. ; ((bobp) 0)
  15693. ; ((equal (buffer-substring
  15694. ; (max (point-min) (- (point) 3)) (point))
  15695. ; "\n\n\n")
  15696. ; 2)
  15697. ; (t 1)))
  15698. ; (progn (outline-end-of-heading) (point))
  15699. ; nil))))
  15700. ;;; Finish up
  15701. (provide 'org)
  15702. (run-hooks 'org-load-hook)
  15703. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  15704. ;;; org.el ends here