org.el 583 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Keywords: outlines, hypermedia, calendar, wp
  7. ;; Homepage: http://orgmode.org
  8. ;; Version: 6.00pre-1
  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 3, 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://orgmode.org/org.html#Installation
  51. ;;
  52. ;; Documentation
  53. ;; -------------
  54. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  55. ;; distribution also contains a PDF version of it. At the homepage of
  56. ;; Org-mode, you can read the same text online as HTML. There is also an
  57. ;; excellent reference card made by Philip Rooke. This card can be found
  58. ;; in the etc/ directory of Emacs 22.
  59. ;;
  60. ;; A list of recent changes can be found at
  61. ;; http://orgmode.org/Changes.html
  62. ;;
  63. ;;; Code:
  64. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  65. (defvar org-table-formula-constants-local nil
  66. "Local version of `org-table-formula-constants'.")
  67. (make-variable-buffer-local 'org-table-formula-constants-local)
  68. ;;;; Require other packages
  69. (eval-when-compile
  70. (require 'cl)
  71. (require 'gnus-sum)
  72. (require 'calendar))
  73. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  74. ;; the file noutline.el being loaded.
  75. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  76. ;; We require noutline, which might be provided in outline.el
  77. (require 'outline) (require 'noutline)
  78. ;; Other stuff we need.
  79. (require 'time-date)
  80. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  81. (require 'easymenu)
  82. (require 'org-macs)
  83. (require 'org-compat)
  84. ;;;; Customization variables
  85. ;;; Version
  86. (defconst org-version "6.00pre-1"
  87. "The version number of the file org.el.")
  88. (defun org-version (&optional here)
  89. "Show the org-mode version in the echo area.
  90. With prefix arg HERE, insert it at point."
  91. (interactive "P")
  92. (let ((version (format "Org-mode version %s" org-version)))
  93. (message version)
  94. (if here
  95. (insert version))))
  96. ;;; Compatibility constants
  97. ;;; The custom variables
  98. (defgroup org nil
  99. "Outline-based notes management and organizer."
  100. :tag "Org"
  101. :group 'outlines
  102. :group 'hypermedia
  103. :group 'calendar)
  104. (defcustom org-load-hook nil
  105. "Hook that is run after org.el has been loaded."
  106. :group 'org
  107. :type 'hook)
  108. (defvar org-modules) ; defined below
  109. (defvar org-modules-loaded nil
  110. "Have the modules been loaded already?")
  111. (defun org-load-modules-maybe (&optional force)
  112. "Load all extensions listed in `org-default-extensions'."
  113. (when (or force (not org-modules-loaded))
  114. (mapc (lambda (ext)
  115. (condition-case nil (require ext)
  116. (error (message "Problems while trying to load feature `%s'" ext))))
  117. org-modules)
  118. (setq org-modules-loaded t)))
  119. (defun org-set-modules (var value)
  120. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  121. (set var value)
  122. (when (featurep 'org)
  123. (org-load-modules-maybe 'force)))
  124. (defcustom org-modules '(org-bbdb org-gnus org-info org-irc org-mhe org-rmail org-vm org-wl)
  125. "Modules that should always be loaded together with org.el.
  126. If the description starts with <A>, this means the extension
  127. will be autoloaded when needed, preloading is not necessary.
  128. If a description starts with <C>, the file is not part of emacs
  129. and loading it will require that you have downloaded and properly installed
  130. the org-mode distribution."
  131. :group 'org
  132. :set 'org-set-modules
  133. :type
  134. '(set :greedy t
  135. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  136. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  137. (const :tag " info: Links to Info nodes" org-info)
  138. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  139. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  140. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  141. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  142. (const :tag " vm: Links to VM folders/messages" org-vm)
  143. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  144. (const :tag " mouse: Additional mouse support" org-mouse)
  145. ; (const :tag "A export-latex: LaTeX export" org-export-latex)
  146. ; (const :tag "A publish: Publishing" org-publish)
  147. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  148. (const :tag "C bibtex: Org links to BibTeX entries" org-bibtex)
  149. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  150. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  151. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  152. (const :tag "C id: Global id's for identifying entries" org-id)
  153. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  154. (const :tag "C iswitchb: Use iswitchb to select Org buffer" org-iswitchb)
  155. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  156. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  157. (const :tag "C mew: Support for links to messages in Mew" org-mew)
  158. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  159. (const :tag "C registry: A registry for Org links" org-registry)
  160. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  161. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  162. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)))
  163. ;; FIXME: Needs a separate group...
  164. (defcustom org-completion-fallback-command 'hippie-expand
  165. "The expansion command called by \\[org-complete] in normal context.
  166. Normal means, no org-mode-specific context."
  167. :group 'org
  168. :type 'function)
  169. (defgroup org-startup nil
  170. "Options concerning startup of Org-mode."
  171. :tag "Org Startup"
  172. :group 'org)
  173. (defcustom org-startup-folded t
  174. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  175. This can also be configured on a per-file basis by adding one of
  176. the following lines anywhere in the buffer:
  177. #+STARTUP: fold
  178. #+STARTUP: nofold
  179. #+STARTUP: content"
  180. :group 'org-startup
  181. :type '(choice
  182. (const :tag "nofold: show all" nil)
  183. (const :tag "fold: overview" t)
  184. (const :tag "content: all headlines" content)))
  185. (defcustom org-startup-truncated t
  186. "Non-nil means, entering Org-mode will set `truncate-lines'.
  187. This is useful since some lines containing links can be very long and
  188. uninteresting. Also tables look terrible when wrapped."
  189. :group 'org-startup
  190. :type 'boolean)
  191. (defcustom org-startup-align-all-tables nil
  192. "Non-nil means, align all tables when visiting a file.
  193. This is useful when the column width in tables is forced with <N> cookies
  194. in table fields. Such tables will look correct only after the first re-align.
  195. This can also be configured on a per-file basis by adding one of
  196. the following lines anywhere in the buffer:
  197. #+STARTUP: align
  198. #+STARTUP: noalign"
  199. :group 'org-startup
  200. :type 'boolean)
  201. (defcustom org-insert-mode-line-in-empty-file nil
  202. "Non-nil means insert the first line setting Org-mode in empty files.
  203. When the function `org-mode' is called interactively in an empty file, this
  204. normally means that the file name does not automatically trigger Org-mode.
  205. To ensure that the file will always be in Org-mode in the future, a
  206. line enforcing Org-mode will be inserted into the buffer, if this option
  207. has been set."
  208. :group 'org-startup
  209. :type 'boolean)
  210. (defcustom org-replace-disputed-keys nil
  211. "Non-nil means use alternative key bindings for some keys.
  212. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  213. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  214. If you want to use Org-mode together with one of these other modes,
  215. or more generally if you would like to move some Org-mode commands to
  216. other keys, set this variable and configure the keys with the variable
  217. `org-disputed-keys'.
  218. This option is only relevant at load-time of Org-mode, and must be set
  219. *before* org.el is loaded. Changing it requires a restart of Emacs to
  220. become effective."
  221. :group 'org-startup
  222. :type 'boolean)
  223. (if (fboundp 'defvaralias)
  224. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  225. (defcustom org-disputed-keys
  226. '(([(shift up)] . [(meta p)])
  227. ([(shift down)] . [(meta n)])
  228. ([(shift left)] . [(meta -)])
  229. ([(shift right)] . [(meta +)])
  230. ([(control shift right)] . [(meta shift +)])
  231. ([(control shift left)] . [(meta shift -)]))
  232. "Keys for which Org-mode and other modes compete.
  233. This is an alist, cars are the default keys, second element specifies
  234. the alternative to use when `org-replace-disputed-keys' is t.
  235. Keys can be specified in any syntax supported by `define-key'.
  236. The value of this option takes effect only at Org-mode's startup,
  237. therefore you'll have to restart Emacs to apply it after changing."
  238. :group 'org-startup
  239. :type 'alist)
  240. (defun org-key (key)
  241. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  242. Or return the original if not disputed."
  243. (if org-replace-disputed-keys
  244. (let* ((nkey (key-description key))
  245. (x (org-find-if (lambda (x)
  246. (equal (key-description (car x)) nkey))
  247. org-disputed-keys)))
  248. (if x (cdr x) key))
  249. key))
  250. (defun org-find-if (predicate seq)
  251. (catch 'exit
  252. (while seq
  253. (if (funcall predicate (car seq))
  254. (throw 'exit (car seq))
  255. (pop seq)))))
  256. (defun org-defkey (keymap key def)
  257. "Define a key, possibly translated, as returned by `org-key'."
  258. (define-key keymap (org-key key) def))
  259. (defcustom org-ellipsis nil
  260. "The ellipsis to use in the Org-mode outline.
  261. When nil, just use the standard three dots. When a string, use that instead,
  262. When a face, use the standart 3 dots, but with the specified face.
  263. The change affects only Org-mode (which will then use its own display table).
  264. Changing this requires executing `M-x org-mode' in a buffer to become
  265. effective."
  266. :group 'org-startup
  267. :type '(choice (const :tag "Default" nil)
  268. (face :tag "Face" :value org-warning)
  269. (string :tag "String" :value "...#")))
  270. (defvar org-display-table nil
  271. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  272. (defgroup org-keywords nil
  273. "Keywords in Org-mode."
  274. :tag "Org Keywords"
  275. :group 'org)
  276. (defcustom org-deadline-string "DEADLINE:"
  277. "String to mark deadline entries.
  278. A deadline is this string, followed by a time stamp. Should be a word,
  279. terminated by a colon. You can insert a schedule keyword and
  280. a timestamp with \\[org-deadline].
  281. Changes become only effective after restarting Emacs."
  282. :group 'org-keywords
  283. :type 'string)
  284. (defcustom org-scheduled-string "SCHEDULED:"
  285. "String to mark scheduled TODO entries.
  286. A schedule is this string, followed by a time stamp. Should be a word,
  287. terminated by a colon. You can insert a schedule keyword and
  288. a timestamp with \\[org-schedule].
  289. Changes become only effective after restarting Emacs."
  290. :group 'org-keywords
  291. :type 'string)
  292. (defcustom org-closed-string "CLOSED:"
  293. "String used as the prefix for timestamps logging closing a TODO entry."
  294. :group 'org-keywords
  295. :type 'string)
  296. (defcustom org-clock-string "CLOCK:"
  297. "String used as prefix for timestamps clocking work hours on an item."
  298. :group 'org-keywords
  299. :type 'string)
  300. (defcustom org-comment-string "COMMENT"
  301. "Entries starting with this keyword will never be exported.
  302. An entry can be toggled between COMMENT and normal with
  303. \\[org-toggle-comment].
  304. Changes become only effective after restarting Emacs."
  305. :group 'org-keywords
  306. :type 'string)
  307. (defcustom org-quote-string "QUOTE"
  308. "Entries starting with this keyword will be exported in fixed-width font.
  309. Quoting applies only to the text in the entry following the headline, and does
  310. not extend beyond the next headline, even if that is lower level.
  311. An entry can be toggled between QUOTE and normal with
  312. \\[org-toggle-fixed-width-section]."
  313. :group 'org-keywords
  314. :type 'string)
  315. (defconst org-repeat-re
  316. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  317. "Regular expression for specifying repeated events.
  318. After a match, group 1 contains the repeat expression.")
  319. (defgroup org-structure nil
  320. "Options concerning the general structure of Org-mode files."
  321. :tag "Org Structure"
  322. :group 'org)
  323. (defgroup org-reveal-location nil
  324. "Options about how to make context of a location visible."
  325. :tag "Org Reveal Location"
  326. :group 'org-structure)
  327. (defconst org-context-choice
  328. '(choice
  329. (const :tag "Always" t)
  330. (const :tag "Never" nil)
  331. (repeat :greedy t :tag "Individual contexts"
  332. (cons
  333. (choice :tag "Context"
  334. (const agenda)
  335. (const org-goto)
  336. (const occur-tree)
  337. (const tags-tree)
  338. (const link-search)
  339. (const mark-goto)
  340. (const bookmark-jump)
  341. (const isearch)
  342. (const default))
  343. (boolean))))
  344. "Contexts for the reveal options.")
  345. (defcustom org-show-hierarchy-above '((default . t))
  346. "Non-nil means, show full hierarchy when revealing a location.
  347. Org-mode often shows locations in an org-mode file which might have
  348. been invisible before. When this is set, the hierarchy of headings
  349. above the exposed location is shown.
  350. Turning this off for example for sparse trees makes them very compact.
  351. Instead of t, this can also be an alist specifying this option for different
  352. contexts. Valid contexts are
  353. agenda when exposing an entry from the agenda
  354. org-goto when using the command `org-goto' on key C-c C-j
  355. occur-tree when using the command `org-occur' on key C-c /
  356. tags-tree when constructing a sparse tree based on tags matches
  357. link-search when exposing search matches associated with a link
  358. mark-goto when exposing the jump goal of a mark
  359. bookmark-jump when exposing a bookmark location
  360. isearch when exiting from an incremental search
  361. default default for all contexts not set explicitly"
  362. :group 'org-reveal-location
  363. :type org-context-choice)
  364. (defcustom org-show-following-heading '((default . nil))
  365. "Non-nil means, show following heading when revealing a location.
  366. Org-mode often shows locations in an org-mode file which might have
  367. been invisible before. When this is set, the heading following the
  368. match is shown.
  369. Turning this off for example for sparse trees makes them very compact,
  370. but makes it harder to edit the location of the match. In such a case,
  371. use the command \\[org-reveal] to show more context.
  372. Instead of t, this can also be an alist specifying this option for different
  373. contexts. See `org-show-hierarchy-above' for valid contexts."
  374. :group 'org-reveal-location
  375. :type org-context-choice)
  376. (defcustom org-show-siblings '((default . nil) (isearch t))
  377. "Non-nil means, show all sibling heading when revealing a location.
  378. Org-mode often shows locations in an org-mode file which might have
  379. been invisible before. When this is set, the sibling of the current entry
  380. heading are all made visible. If `org-show-hierarchy-above' is t,
  381. the same happens on each level of the hierarchy above the current entry.
  382. By default this is on for the isearch context, off for all other contexts.
  383. Turning this off for example for sparse trees makes them very compact,
  384. but makes it harder to edit the location of the match. In such a case,
  385. use the command \\[org-reveal] to show more context.
  386. Instead of t, this can also be an alist specifying this option for different
  387. contexts. See `org-show-hierarchy-above' for valid contexts."
  388. :group 'org-reveal-location
  389. :type org-context-choice)
  390. (defcustom org-show-entry-below '((default . nil))
  391. "Non-nil means, show the entry below a headline when revealing a location.
  392. Org-mode often shows locations in an org-mode file which might have
  393. been invisible before. When this is set, the text below the headline that is
  394. exposed is also shown.
  395. By default this is off for all contexts.
  396. Instead of t, this can also be an alist specifying this option for different
  397. contexts. See `org-show-hierarchy-above' for valid contexts."
  398. :group 'org-reveal-location
  399. :type org-context-choice)
  400. (defcustom org-indirect-buffer-display 'other-window
  401. "How should indirect tree buffers be displayed?
  402. This applies to indirect buffers created with the commands
  403. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  404. Valid values are:
  405. current-window Display in the current window
  406. other-window Just display in another window.
  407. dedicated-frame Create one new frame, and re-use it each time.
  408. new-frame Make a new frame each time. Note that in this case
  409. previously-made indirect buffers are kept, and you need to
  410. kill these buffers yourself."
  411. :group 'org-structure
  412. :group 'org-agenda-windows
  413. :type '(choice
  414. (const :tag "In current window" current-window)
  415. (const :tag "In current frame, other window" other-window)
  416. (const :tag "Each time a new frame" new-frame)
  417. (const :tag "One dedicated frame" dedicated-frame)))
  418. (defgroup org-cycle nil
  419. "Options concerning visibility cycling in Org-mode."
  420. :tag "Org Cycle"
  421. :group 'org-structure)
  422. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  423. "Names of drawers. Drawers are not opened by cycling on the headline above.
  424. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  425. this:
  426. :DRAWERNAME:
  427. .....
  428. :END:
  429. The drawer \"PROPERTIES\" is special for capturing properties through
  430. the property API.
  431. Drawers can be defined on the per-file basis with a line like:
  432. #+DRAWERS: HIDDEN STATE PROPERTIES"
  433. :group 'org-structure
  434. :type '(repeat (string :tag "Drawer Name")))
  435. (defcustom org-cycle-global-at-bob nil
  436. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  437. This makes it possible to do global cycling without having to use S-TAB or
  438. C-u TAB. For this special case to work, the first line of the buffer
  439. must not be a headline - it may be empty ot some other text. When used in
  440. this way, `org-cycle-hook' is disables temporarily, to make sure the
  441. cursor stays at the beginning of the buffer.
  442. When this option is nil, don't do anything special at the beginning
  443. of the buffer."
  444. :group 'org-cycle
  445. :type 'boolean)
  446. (defcustom org-cycle-emulate-tab t
  447. "Where should `org-cycle' emulate TAB.
  448. nil Never
  449. white Only in completely white lines
  450. whitestart Only at the beginning of lines, before the first non-white char
  451. t Everywhere except in headlines
  452. exc-hl-bol Everywhere except at the start of a headline
  453. If TAB is used in a place where it does not emulate TAB, the current subtree
  454. visibility is cycled."
  455. :group 'org-cycle
  456. :type '(choice (const :tag "Never" nil)
  457. (const :tag "Only in completely white lines" white)
  458. (const :tag "Before first char in a line" whitestart)
  459. (const :tag "Everywhere except in headlines" t)
  460. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  461. ))
  462. (defcustom org-cycle-separator-lines 2
  463. "Number of empty lines needed to keep an empty line between collapsed trees.
  464. If you leave an empty line between the end of a subtree and the following
  465. headline, this empty line is hidden when the subtree is folded.
  466. Org-mode will leave (exactly) one empty line visible if the number of
  467. empty lines is equal or larger to the number given in this variable.
  468. So the default 2 means, at least 2 empty lines after the end of a subtree
  469. are needed to produce free space between a collapsed subtree and the
  470. following headline.
  471. Special case: when 0, never leave empty lines in collapsed view."
  472. :group 'org-cycle
  473. :type 'integer)
  474. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  475. org-cycle-hide-drawers
  476. org-cycle-show-empty-lines
  477. org-optimize-window-after-visibility-change)
  478. "Hook that is run after `org-cycle' has changed the buffer visibility.
  479. The function(s) in this hook must accept a single argument which indicates
  480. the new state that was set by the most recent `org-cycle' command. The
  481. argument is a symbol. After a global state change, it can have the values
  482. `overview', `content', or `all'. After a local state change, it can have
  483. the values `folded', `children', or `subtree'."
  484. :group 'org-cycle
  485. :type 'hook)
  486. (defgroup org-edit-structure nil
  487. "Options concerning structure editing in Org-mode."
  488. :tag "Org Edit Structure"
  489. :group 'org-structure)
  490. (defcustom org-odd-levels-only nil
  491. "Non-nil means, skip even levels and only use odd levels for the outline.
  492. This has the effect that two stars are being added/taken away in
  493. promotion/demotion commands. It also influences how levels are
  494. handled by the exporters.
  495. Changing it requires restart of `font-lock-mode' to become effective
  496. for fontification also in regions already fontified.
  497. You may also set this on a per-file basis by adding one of the following
  498. lines to the buffer:
  499. #+STARTUP: odd
  500. #+STARTUP: oddeven"
  501. :group 'org-edit-structure
  502. :group 'org-font-lock
  503. :type 'boolean)
  504. (defcustom org-adapt-indentation t
  505. "Non-nil means, adapt indentation when promoting and demoting.
  506. When this is set and the *entire* text in an entry is indented, the
  507. indentation is increased by one space in a demotion command, and
  508. decreased by one in a promotion command. If any line in the entry
  509. body starts at column 0, indentation is not changed at all."
  510. :group 'org-edit-structure
  511. :type 'boolean)
  512. (defcustom org-special-ctrl-a/e nil
  513. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  514. When t, `C-a' will bring back the cursor to the beginning of the
  515. headline text, i.e. after the stars and after a possible TODO keyword.
  516. In an item, this will be the position after the bullet.
  517. When the cursor is already at that position, another `C-a' will bring
  518. it to the beginning of the line.
  519. `C-e' will jump to the end of the headline, ignoring the presence of tags
  520. in the headline. A second `C-e' will then jump to the true end of the
  521. line, after any tags.
  522. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  523. and only a directly following, identical keypress will bring the cursor
  524. to the special positions."
  525. :group 'org-edit-structure
  526. :type '(choice
  527. (const :tag "off" nil)
  528. (const :tag "after bullet first" t)
  529. (const :tag "border first" reversed)))
  530. (if (fboundp 'defvaralias)
  531. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  532. (defcustom org-special-ctrl-k nil
  533. "Non-nil means `C-k' will behave specially in headlines.
  534. When nil, `C-k' will call the default `kill-line' command.
  535. When t, the following will happen while the cursor is in the headline:
  536. - When the cursor is at the beginning of a headline, kill the entire
  537. line and possible the folded subtree below the line.
  538. - When in the middle of the headline text, kill the headline up to the tags.
  539. - When after the headline text, kill the tags."
  540. :group 'org-edit-structure
  541. :type 'boolean)
  542. (defcustom org-M-RET-may-split-line '((default . t))
  543. "Non-nil means, M-RET will split the line at the cursor position.
  544. When nil, it will go to the end of the line before making a
  545. new line.
  546. You may also set this option in a different way for different
  547. contexts. Valid contexts are:
  548. headline when creating a new headline
  549. item when creating a new item
  550. table in a table field
  551. default the value to be used for all contexts not explicitly
  552. customized"
  553. :group 'org-structure
  554. :group 'org-table
  555. :type '(choice
  556. (const :tag "Always" t)
  557. (const :tag "Never" nil)
  558. (repeat :greedy t :tag "Individual contexts"
  559. (cons
  560. (choice :tag "Context"
  561. (const headline)
  562. (const item)
  563. (const table)
  564. (const default))
  565. (boolean)))))
  566. (defcustom org-blank-before-new-entry '((heading . nil)
  567. (plain-list-item . nil))
  568. "Should `org-insert-heading' leave a blank line before new heading/item?
  569. The value is an alist, with `heading' and `plain-list-item' as car,
  570. and a boolean flag as cdr."
  571. :group 'org-edit-structure
  572. :type '(list
  573. (cons (const heading) (boolean))
  574. (cons (const plain-list-item) (boolean))))
  575. (defcustom org-insert-heading-hook nil
  576. "Hook being run after inserting a new heading."
  577. :group 'org-edit-structure
  578. :type 'hook)
  579. (defcustom org-enable-fixed-width-editor t
  580. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  581. This currently only means, they are never auto-wrapped.
  582. When nil, such lines will be treated like ordinary lines.
  583. See also the QUOTE keyword."
  584. :group 'org-edit-structure
  585. :type 'boolean)
  586. (defcustom org-goto-auto-isearch t
  587. "Non-nil means, typing characters in org-goto starts incremental search."
  588. :group 'org-edit-structure
  589. :type 'boolean)
  590. (defgroup org-sparse-trees nil
  591. "Options concerning sparse trees in Org-mode."
  592. :tag "Org Sparse Trees"
  593. :group 'org-structure)
  594. (defcustom org-highlight-sparse-tree-matches t
  595. "Non-nil means, highlight all matches that define a sparse tree.
  596. The highlights will automatically disappear the next time the buffer is
  597. changed by an edit command."
  598. :group 'org-sparse-trees
  599. :type 'boolean)
  600. (defcustom org-remove-highlights-with-change t
  601. "Non-nil means, any change to the buffer will remove temporary highlights.
  602. Such highlights are created by `org-occur' and `org-clock-display'.
  603. When nil, `C-c C-c needs to be used to get rid of the highlights.
  604. The highlights created by `org-preview-latex-fragment' always need
  605. `C-c C-c' to be removed."
  606. :group 'org-sparse-trees
  607. :group 'org-time
  608. :type 'boolean)
  609. (defcustom org-occur-hook '(org-first-headline-recenter)
  610. "Hook that is run after `org-occur' has constructed a sparse tree.
  611. This can be used to recenter the window to show as much of the structure
  612. as possible."
  613. :group 'org-sparse-trees
  614. :type 'hook)
  615. (defgroup org-plain-lists nil
  616. "Options concerning plain lists in Org-mode."
  617. :tag "Org Plain lists"
  618. :group 'org-structure)
  619. (defcustom org-cycle-include-plain-lists nil
  620. "Non-nil means, include plain lists into visibility cycling.
  621. This means that during cycling, plain list items will *temporarily* be
  622. interpreted as outline headlines with a level given by 1000+i where i is the
  623. indentation of the bullet. In all other operations, plain list items are
  624. not seen as headlines. For example, you cannot assign a TODO keyword to
  625. such an item."
  626. :group 'org-plain-lists
  627. :type 'boolean)
  628. (defcustom org-plain-list-ordered-item-terminator t
  629. "The character that makes a line with leading number an ordered list item.
  630. Valid values are ?. and ?\). To get both terminators, use t. While
  631. ?. may look nicer, it creates the danger that a line with leading
  632. number may be incorrectly interpreted as an item. ?\) therefore is
  633. the safe choice."
  634. :group 'org-plain-lists
  635. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  636. (const :tag "paren like in \"2)\"" ?\))
  637. (const :tab "both" t)))
  638. (defcustom org-auto-renumber-ordered-lists t
  639. "Non-nil means, automatically renumber ordered plain lists.
  640. Renumbering happens when the sequence have been changed with
  641. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  642. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  643. :group 'org-plain-lists
  644. :type 'boolean)
  645. (defcustom org-provide-checkbox-statistics t
  646. "Non-nil means, update checkbox statistics after insert and toggle.
  647. When this is set, checkbox statistics is updated each time you either insert
  648. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  649. with \\[org-ctrl-c-ctrl-c\\]."
  650. :group 'org-plain-lists
  651. :type 'boolean)
  652. (defgroup org-archive nil
  653. "Options concerning archiving in Org-mode."
  654. :tag "Org Archive"
  655. :group 'org-structure)
  656. (defcustom org-archive-tag "ARCHIVE"
  657. "The tag that marks a subtree as archived.
  658. An archived subtree does not open during visibility cycling, and does
  659. not contribute to the agenda listings.
  660. After changing this, font-lock must be restarted in the relevant buffers to
  661. get the proper fontification."
  662. :group 'org-archive
  663. :group 'org-keywords
  664. :type 'string)
  665. (defcustom org-agenda-skip-archived-trees t
  666. "Non-nil means, the agenda will skip any items located in archived trees.
  667. An archived tree is a tree marked with the tag ARCHIVE."
  668. :group 'org-archive
  669. :group 'org-agenda-skip
  670. :type 'boolean)
  671. (defcustom org-cycle-open-archived-trees nil
  672. "Non-nil means, `org-cycle' will open archived trees.
  673. An archived tree is a tree marked with the tag ARCHIVE.
  674. When nil, archived trees will stay folded. You can still open them with
  675. normal outline commands like `show-all', but not with the cycling commands."
  676. :group 'org-archive
  677. :group 'org-cycle
  678. :type 'boolean)
  679. (defcustom org-sparse-tree-open-archived-trees nil
  680. "Non-nil means sparse tree construction shows matches in archived trees.
  681. When nil, matches in these trees are highlighted, but the trees are kept in
  682. collapsed state."
  683. :group 'org-archive
  684. :group 'org-sparse-trees
  685. :type 'boolean)
  686. (defcustom org-archive-location "%s_archive::"
  687. "The location where subtrees should be archived.
  688. This string consists of two parts, separated by a double-colon.
  689. The first part is a file name - when omitted, archiving happens in the same
  690. file. %s will be replaced by the current file name (without directory part).
  691. Archiving to a different file is useful to keep archived entries from
  692. contributing to the Org-mode Agenda.
  693. The part after the double colon is a headline. The archived entries will be
  694. filed under that headline. When omitted, the subtrees are simply filed away
  695. at the end of the file, as top-level entries.
  696. Here are a few examples:
  697. \"%s_archive::\"
  698. If the current file is Projects.org, archive in file
  699. Projects.org_archive, as top-level trees. This is the default.
  700. \"::* Archived Tasks\"
  701. Archive in the current file, under the top-level headline
  702. \"* Archived Tasks\".
  703. \"~/org/archive.org::\"
  704. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  705. \"basement::** Finished Tasks\"
  706. Archive in file ./basement (relative path), as level 3 trees
  707. below the level 2 heading \"** Finished Tasks\".
  708. You may set this option on a per-file basis by adding to the buffer a
  709. line like
  710. #+ARCHIVE: basement::** Finished Tasks"
  711. :group 'org-archive
  712. :type 'string)
  713. (defcustom org-archive-mark-done t
  714. "Non-nil means, mark entries as DONE when they are moved to the archive file.
  715. This can be a string to set the keyword to use. When t, Org-mode will
  716. use the first keyword in its list that means done."
  717. :group 'org-archive
  718. :type '(choice
  719. (const :tag "No" nil)
  720. (const :tag "Yes" t)
  721. (string :tag "Use this keyword")))
  722. (defcustom org-archive-stamp-time t
  723. "Non-nil means, add a time stamp to entries moved to an archive file.
  724. This variable is obsolete and has no effect anymore, instead add ot remove
  725. `time' from the variablle `org-archive-save-context-info'."
  726. :group 'org-archive
  727. :type 'boolean)
  728. (defcustom org-archive-save-context-info '(time file olpath category todo itags)
  729. "Parts of context info that should be stored as properties when archiving.
  730. When a subtree is moved to an archive file, it looses information given by
  731. context, like inherited tags, the category, and possibly also the TODO
  732. state (depending on the variable `org-archive-mark-done').
  733. This variable can be a list of any of the following symbols:
  734. time The time of archiving.
  735. file The file where the entry originates.
  736. itags The local tags, in the headline of the subtree.
  737. ltags The tags the subtree inherits from further up the hierarchy.
  738. todo The pre-archive TODO state.
  739. category The category, taken from file name or #+CATEGORY lines.
  740. olpath The outline path to the item. These are all headlines above
  741. the current item, separated by /, like a file path.
  742. For each symbol present in the list, a property will be created in
  743. the archived entry, with a prefix \"PRE_ARCHIVE_\", to remember this
  744. information."
  745. :group 'org-archive
  746. :type '(set :greedy t
  747. (const :tag "Time" time)
  748. (const :tag "File" file)
  749. (const :tag "Category" category)
  750. (const :tag "TODO state" todo)
  751. (const :tag "TODO state" priority)
  752. (const :tag "Inherited tags" itags)
  753. (const :tag "Outline path" olpath)
  754. (const :tag "Local tags" ltags)))
  755. (defgroup org-imenu-and-speedbar nil
  756. "Options concerning imenu and speedbar in Org-mode."
  757. :tag "Org Imenu and Speedbar"
  758. :group 'org-structure)
  759. (defcustom org-imenu-depth 2
  760. "The maximum level for Imenu access to Org-mode headlines.
  761. This also applied for speedbar access."
  762. :group 'org-imenu-and-speedbar
  763. :type 'number)
  764. (defgroup org-table nil
  765. "Options concerning tables in Org-mode."
  766. :tag "Org Table"
  767. :group 'org)
  768. (defcustom org-enable-table-editor 'optimized
  769. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  770. When nil, such lines will be treated like ordinary lines.
  771. When equal to the symbol `optimized', the table editor will be optimized to
  772. do the following:
  773. - Automatic overwrite mode in front of whitespace in table fields.
  774. This makes the structure of the table stay in tact as long as the edited
  775. field does not exceed the column width.
  776. - Minimize the number of realigns. Normally, the table is aligned each time
  777. TAB or RET are pressed to move to another field. With optimization this
  778. happens only if changes to a field might have changed the column width.
  779. Optimization requires replacing the functions `self-insert-command',
  780. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  781. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  782. very good at guessing when a re-align will be necessary, but you can always
  783. force one with \\[org-ctrl-c-ctrl-c].
  784. If you would like to use the optimized version in Org-mode, but the
  785. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  786. This variable can be used to turn on and off the table editor during a session,
  787. but in order to toggle optimization, a restart is required.
  788. See also the variable `org-table-auto-blank-field'."
  789. :group 'org-table
  790. :type '(choice
  791. (const :tag "off" nil)
  792. (const :tag "on" t)
  793. (const :tag "on, optimized" optimized)))
  794. (defcustom org-table-tab-recognizes-table.el t
  795. "Non-nil means, TAB will automatically notice a table.el table.
  796. When it sees such a table, it moves point into it and - if necessary -
  797. calls `table-recognize-table'."
  798. :group 'org-table-editing
  799. :type 'boolean)
  800. (defgroup org-link nil
  801. "Options concerning links in Org-mode."
  802. :tag "Org Link"
  803. :group 'org)
  804. (defvar org-link-abbrev-alist-local nil
  805. "Buffer-local version of `org-link-abbrev-alist', which see.
  806. The value of this is taken from the #+LINK lines.")
  807. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  808. (defcustom org-link-abbrev-alist nil
  809. "Alist of link abbreviations.
  810. The car of each element is a string, to be replaced at the start of a link.
  811. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  812. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  813. [[linkkey:tag][description]]
  814. If REPLACE is a string, the tag will simply be appended to create the link.
  815. If the string contains \"%s\", the tag will be inserted there.
  816. REPLACE may also be a function that will be called with the tag as the
  817. only argument to create the link, which should be returned as a string.
  818. See the manual for examples."
  819. :group 'org-link
  820. :type 'alist)
  821. (defcustom org-descriptive-links t
  822. "Non-nil means, hide link part and only show description of bracket links.
  823. Bracket links are like [[link][descritpion]]. This variable sets the initial
  824. state in new org-mode buffers. The setting can then be toggled on a
  825. per-buffer basis from the Org->Hyperlinks menu."
  826. :group 'org-link
  827. :type 'boolean)
  828. (defcustom org-link-file-path-type 'adaptive
  829. "How the path name in file links should be stored.
  830. Valid values are:
  831. relative Relative to the current directory, i.e. the directory of the file
  832. into which the link is being inserted.
  833. absolute Absolute path, if possible with ~ for home directory.
  834. noabbrev Absolute path, no abbreviation of home directory.
  835. adaptive Use relative path for files in the current directory and sub-
  836. directories of it. For other files, use an absolute path."
  837. :group 'org-link
  838. :type '(choice
  839. (const relative)
  840. (const absolute)
  841. (const noabbrev)
  842. (const adaptive)))
  843. (defcustom org-activate-links '(bracket angle plain radio tag date)
  844. "Types of links that should be activated in Org-mode files.
  845. This is a list of symbols, each leading to the activation of a certain link
  846. type. In principle, it does not hurt to turn on most link types - there may
  847. be a small gain when turning off unused link types. The types are:
  848. bracket The recommended [[link][description]] or [[link]] links with hiding.
  849. angular Links in angular brackes that may contain whitespace like
  850. <bbdb:Carsten Dominik>.
  851. plain Plain links in normal text, no whitespace, like http://google.com.
  852. radio Text that is matched by a radio target, see manual for details.
  853. tag Tag settings in a headline (link to tag search).
  854. date Time stamps (link to calendar).
  855. Changing this variable requires a restart of Emacs to become effective."
  856. :group 'org-link
  857. :type '(set (const :tag "Double bracket links (new style)" bracket)
  858. (const :tag "Angular bracket links (old style)" angular)
  859. (const :tag "Plain text links" plain)
  860. (const :tag "Radio target matches" radio)
  861. (const :tag "Tags" tag)
  862. (const :tag "Timestamps" date)))
  863. (defgroup org-link-store nil
  864. "Options concerning storing links in Org-mode."
  865. :tag "Org Store Link"
  866. :group 'org-link)
  867. (defcustom org-email-link-description-format "Email %c: %.30s"
  868. "Format of the description part of a link to an email or usenet message.
  869. The following %-excapes will be replaced by corresponding information:
  870. %F full \"From\" field
  871. %f name, taken from \"From\" field, address if no name
  872. %T full \"To\" field
  873. %t first name in \"To\" field, address if no name
  874. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  875. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  876. %s subject
  877. %m message-id.
  878. You may use normal field width specification between the % and the letter.
  879. This is for example useful to limit the length of the subject.
  880. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  881. :group 'org-link-store
  882. :type 'string)
  883. (defcustom org-from-is-user-regexp
  884. (let (r1 r2)
  885. (when (and user-mail-address (not (string= user-mail-address "")))
  886. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  887. (when (and user-full-name (not (string= user-full-name "")))
  888. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  889. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  890. "Regexp mached against the \"From:\" header of an email or usenet message.
  891. It should match if the message is from the user him/herself."
  892. :group 'org-link-store
  893. :type 'regexp)
  894. (defcustom org-context-in-file-links t
  895. "Non-nil means, file links from `org-store-link' contain context.
  896. A search string will be added to the file name with :: as separator and
  897. used to find the context when the link is activated by the command
  898. `org-open-at-point'.
  899. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  900. negates this setting for the duration of the command."
  901. :group 'org-link-store
  902. :type 'boolean)
  903. (defcustom org-keep-stored-link-after-insertion nil
  904. "Non-nil means, keep link in list for entire session.
  905. The command `org-store-link' adds a link pointing to the current
  906. location to an internal list. These links accumulate during a session.
  907. The command `org-insert-link' can be used to insert links into any
  908. Org-mode file (offering completion for all stored links). When this
  909. option is nil, every link which has been inserted once using \\[org-insert-link]
  910. will be removed from the list, to make completing the unused links
  911. more efficient."
  912. :group 'org-link-store
  913. :type 'boolean)
  914. (defgroup org-link-follow nil
  915. "Options concerning following links in Org-mode."
  916. :tag "Org Follow Link"
  917. :group 'org-link)
  918. (defcustom org-follow-link-hook nil
  919. "Hook that is run after a link has been followed."
  920. :group 'org-link-follow
  921. :type 'hook)
  922. (defcustom org-tab-follows-link nil
  923. "Non-nil means, on links TAB will follow the link.
  924. Needs to be set before org.el is loaded."
  925. :group 'org-link-follow
  926. :type 'boolean)
  927. (defcustom org-return-follows-link nil
  928. "Non-nil means, on links RET will follow the link.
  929. Needs to be set before org.el is loaded."
  930. :group 'org-link-follow
  931. :type 'boolean)
  932. (defcustom org-mouse-1-follows-link
  933. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  934. "Non-nil means, mouse-1 on a link will follow the link.
  935. A longer mouse click will still set point. Does not work on XEmacs.
  936. Needs to be set before org.el is loaded."
  937. :group 'org-link-follow
  938. :type 'boolean)
  939. (defcustom org-mark-ring-length 4
  940. "Number of different positions to be recorded in the ring
  941. Changing this requires a restart of Emacs to work correctly."
  942. :group 'org-link-follow
  943. :type 'interger)
  944. (defcustom org-link-frame-setup
  945. '((vm . vm-visit-folder-other-frame)
  946. (gnus . gnus-other-frame)
  947. (file . find-file-other-window))
  948. "Setup the frame configuration for following links.
  949. When following a link with Emacs, it may often be useful to display
  950. this link in another window or frame. This variable can be used to
  951. set this up for the different types of links.
  952. For VM, use any of
  953. `vm-visit-folder'
  954. `vm-visit-folder-other-frame'
  955. For Gnus, use any of
  956. `gnus'
  957. `gnus-other-frame'
  958. For FILE, use any of
  959. `find-file'
  960. `find-file-other-window'
  961. `find-file-other-frame'
  962. For the calendar, use the variable `calendar-setup'.
  963. For BBDB, it is currently only possible to display the matches in
  964. another window."
  965. :group 'org-link-follow
  966. :type '(list
  967. (cons (const vm)
  968. (choice
  969. (const vm-visit-folder)
  970. (const vm-visit-folder-other-window)
  971. (const vm-visit-folder-other-frame)))
  972. (cons (const gnus)
  973. (choice
  974. (const gnus)
  975. (const gnus-other-frame)))
  976. (cons (const file)
  977. (choice
  978. (const find-file)
  979. (const find-file-other-window)
  980. (const find-file-other-frame)))))
  981. (defcustom org-display-internal-link-with-indirect-buffer nil
  982. "Non-nil means, use indirect buffer to display infile links.
  983. Activating internal links (from one location in a file to another location
  984. in the same file) normally just jumps to the location. When the link is
  985. activated with a C-u prefix (or with mouse-3), the link is displayed in
  986. another window. When this option is set, the other window actually displays
  987. an indirect buffer clone of the current buffer, to avoid any visibility
  988. changes to the current buffer."
  989. :group 'org-link-follow
  990. :type 'boolean)
  991. (defcustom org-open-non-existing-files nil
  992. "Non-nil means, `org-open-file' will open non-existing files.
  993. When nil, an error will be generated."
  994. :group 'org-link-follow
  995. :type 'boolean)
  996. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  997. "Function and arguments to call for following mailto links.
  998. This is a list with the first element being a lisp function, and the
  999. remaining elements being arguments to the function. In string arguments,
  1000. %a will be replaced by the address, and %s will be replaced by the subject
  1001. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1002. :group 'org-link-follow
  1003. :type '(choice
  1004. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1005. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1006. (const :tag "message-mail" (message-mail "%a" "%s"))
  1007. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1008. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1009. "Non-nil means, ask for confirmation before executing shell links.
  1010. Shell links can be dangerous: just think about a link
  1011. [[shell:rm -rf ~/*][Google Search]]
  1012. This link would show up in your Org-mode document as \"Google Search\",
  1013. but really it would remove your entire home directory.
  1014. Therefore we advise against setting this variable to nil.
  1015. Just change it to `y-or-n-p' of you want to confirm with a
  1016. single keystroke rather than having to type \"yes\"."
  1017. :group 'org-link-follow
  1018. :type '(choice
  1019. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1020. (const :tag "with y-or-n (faster)" y-or-n-p)
  1021. (const :tag "no confirmation (dangerous)" nil)))
  1022. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1023. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1024. Elisp links can be dangerous: just think about a link
  1025. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1026. This link would show up in your Org-mode document as \"Google Search\",
  1027. but really it would remove your entire home directory.
  1028. Therefore we advise against setting this variable to nil.
  1029. Just change it to `y-or-n-p' of you want to confirm with a
  1030. single keystroke rather than having to type \"yes\"."
  1031. :group 'org-link-follow
  1032. :type '(choice
  1033. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1034. (const :tag "with y-or-n (faster)" y-or-n-p)
  1035. (const :tag "no confirmation (dangerous)" nil)))
  1036. (defconst org-file-apps-defaults-gnu
  1037. '((remote . emacs)
  1038. (t . mailcap))
  1039. "Default file applications on a UNIX or GNU/Linux system.
  1040. See `org-file-apps'.")
  1041. (defconst org-file-apps-defaults-macosx
  1042. '((remote . emacs)
  1043. (t . "open %s")
  1044. ("ps" . "gv %s")
  1045. ("ps.gz" . "gv %s")
  1046. ("eps" . "gv %s")
  1047. ("eps.gz" . "gv %s")
  1048. ("dvi" . "xdvi %s")
  1049. ("fig" . "xfig %s"))
  1050. "Default file applications on a MacOS X system.
  1051. The system \"open\" is known as a default, but we use X11 applications
  1052. for some files for which the OS does not have a good default.
  1053. See `org-file-apps'.")
  1054. (defconst org-file-apps-defaults-windowsnt
  1055. (list
  1056. '(remote . emacs)
  1057. (cons t
  1058. (list (if (featurep 'xemacs)
  1059. 'mswindows-shell-execute
  1060. 'w32-shell-execute)
  1061. "open" 'file)))
  1062. "Default file applications on a Windows NT system.
  1063. The system \"open\" is used for most files.
  1064. See `org-file-apps'.")
  1065. (defcustom org-file-apps
  1066. '(
  1067. ("txt" . emacs)
  1068. ("tex" . emacs)
  1069. ("ltx" . emacs)
  1070. ("org" . emacs)
  1071. ("el" . emacs)
  1072. ("bib" . emacs)
  1073. )
  1074. "External applications for opening `file:path' items in a document.
  1075. Org-mode uses system defaults for different file types, but
  1076. you can use this variable to set the application for a given file
  1077. extension. The entries in this list are cons cells where the car identifies
  1078. files and the cdr the corresponding command. Possible values for the
  1079. file identifier are
  1080. \"ext\" A string identifying an extension
  1081. `directory' Matches a directory
  1082. `remote' Matches a remote file, accessible through tramp or efs.
  1083. Remote files most likely should be visited through Emacs
  1084. because external applications cannot handle such paths.
  1085. t Default for all remaining files
  1086. Possible values for the command are:
  1087. `emacs' The file will be visited by the current Emacs process.
  1088. `default' Use the default application for this file type.
  1089. string A command to be executed by a shell; %s will be replaced
  1090. by the path to the file.
  1091. sexp A Lisp form which will be evaluated. The file path will
  1092. be available in the Lisp variable `file'.
  1093. For more examples, see the system specific constants
  1094. `org-file-apps-defaults-macosx'
  1095. `org-file-apps-defaults-windowsnt'
  1096. `org-file-apps-defaults-gnu'."
  1097. :group 'org-link-follow
  1098. :type '(repeat
  1099. (cons (choice :value ""
  1100. (string :tag "Extension")
  1101. (const :tag "Default for unrecognized files" t)
  1102. (const :tag "Remote file" remote)
  1103. (const :tag "Links to a directory" directory))
  1104. (choice :value ""
  1105. (const :tag "Visit with Emacs" emacs)
  1106. (const :tag "Use system default" default)
  1107. (string :tag "Command")
  1108. (sexp :tag "Lisp form")))))
  1109. (defgroup org-refile nil
  1110. "Options concerning refiling entries in Org-mode."
  1111. :tag "Org Remember"
  1112. :group 'org)
  1113. (defcustom org-directory "~/org"
  1114. "Directory with org files.
  1115. This directory will be used as default to prompt for org files.
  1116. Used by the hooks for remember.el."
  1117. :group 'org-refile
  1118. :group 'org-remember
  1119. :type 'directory)
  1120. (defcustom org-default-notes-file "~/.notes"
  1121. "Default target for storing notes.
  1122. Used by the hooks for remember.el. This can be a string, or nil to mean
  1123. the value of `remember-data-file'.
  1124. You can set this on a per-template basis with the variable
  1125. `org-remember-templates'."
  1126. :group 'org-refile
  1127. :group 'org-remember
  1128. :type '(choice
  1129. (const :tag "Default from remember-data-file" nil)
  1130. file))
  1131. (defcustom org-goto-interface 'outline
  1132. "The default interface to be used for `org-goto'.
  1133. Allowed vaues are:
  1134. outline The interface shows an outline of the relevant file
  1135. and the correct heading is found by moving through
  1136. the outline or by searching with incremental search.
  1137. outline-path-completion Headlines in the current buffer are offered via
  1138. completion."
  1139. :group 'org-refile
  1140. :type '(choice
  1141. (const :tag "Outline" outline)
  1142. (const :tag "Outline-path-completion" outline-path-completion)))
  1143. (defcustom org-reverse-note-order nil
  1144. "Non-nil means, store new notes at the beginning of a file or entry.
  1145. When nil, new notes will be filed to the end of a file or entry.
  1146. This can also be a list with cons cells of regular expressions that
  1147. are matched against file names, and values."
  1148. :group 'org-remember
  1149. :type '(choice
  1150. (const :tag "Reverse always" t)
  1151. (const :tag "Reverse never" nil)
  1152. (repeat :tag "By file name regexp"
  1153. (cons regexp boolean))))
  1154. (defcustom org-refile-targets nil
  1155. "Targets for refiling entries with \\[org-refile].
  1156. This is list of cons cells. Each cell contains:
  1157. - a specification of the files to be considered, either a list of files,
  1158. or a symbol whose function or value fields will be used to retrieve
  1159. a file name or a list of file names. Nil means, refile to a different
  1160. heading in the current buffer.
  1161. - A specification of how to find candidate refile targets. This may be
  1162. any of
  1163. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1164. This tag has to be present in all target headlines, inheritance will
  1165. not be considered.
  1166. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1167. todo keyword.
  1168. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1169. headlines that are refiling targets.
  1170. - a cons cell (:level . N). Any headline of level N is considered a target.
  1171. - a cons cell (:maxlevel . N). Any headline with level <= N is a target."
  1172. ;; FIXME: what if there are a var and func with same name???
  1173. :group 'org-remember
  1174. :type '(repeat
  1175. (cons
  1176. (choice :value org-agenda-files
  1177. (const :tag "All agenda files" org-agenda-files)
  1178. (const :tag "Current buffer" nil)
  1179. (function) (variable) (file))
  1180. (choice :tag "Identify target headline by"
  1181. (cons :tag "Specific tag" (const :tag) (string))
  1182. (cons :tag "TODO keyword" (const :todo) (string))
  1183. (cons :tag "Regular expression" (const :regexp) (regexp))
  1184. (cons :tag "Level number" (const :level) (integer))
  1185. (cons :tag "Max Level number" (const :maxlevel) (integer))))))
  1186. (defcustom org-refile-use-outline-path nil
  1187. "Non-nil means, provide refile targets as paths.
  1188. So a level 3 headline will be available as level1/level2/level3.
  1189. When the value is `file', also include the file name (without directory)
  1190. into the path. When `full-file-path', include the full file path."
  1191. :group 'org-remember
  1192. :type '(choice
  1193. (const :tag "Not" nil)
  1194. (const :tag "Yes" t)
  1195. (const :tag "Start with file name" file)
  1196. (const :tag "Start with full file path" full-file-path)))
  1197. (defgroup org-todo nil
  1198. "Options concerning TODO items in Org-mode."
  1199. :tag "Org TODO"
  1200. :group 'org)
  1201. (defgroup org-progress nil
  1202. "Options concerning Progress logging in Org-mode."
  1203. :tag "Org Progress"
  1204. :group 'org-time)
  1205. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1206. "List of TODO entry keyword sequences and their interpretation.
  1207. \\<org-mode-map>This is a list of sequences.
  1208. Each sequence starts with a symbol, either `sequence' or `type',
  1209. indicating if the keywords should be interpreted as a sequence of
  1210. action steps, or as different types of TODO items. The first
  1211. keywords are states requiring action - these states will select a headline
  1212. for inclusion into the global TODO list Org-mode produces. If one of
  1213. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1214. signify that no further action is necessary. If \"|\" is not found,
  1215. the last keyword is treated as the only DONE state of the sequence.
  1216. The command \\[org-todo] cycles an entry through these states, and one
  1217. additional state where no keyword is present. For details about this
  1218. cycling, see the manual.
  1219. TODO keywords and interpretation can also be set on a per-file basis with
  1220. the special #+SEQ_TODO and #+TYP_TODO lines.
  1221. Each keyword can optionally specify a character for fast state selection
  1222. \(in combination with the variable `org-use-fast-todo-selection')
  1223. and specifiers for state change logging, using the same syntax
  1224. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1225. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1226. indicates to record a time stamp each time this state is selected.
  1227. Each keyword may also specify if a timestamp or a note should be
  1228. recorded when entering or leaving the state, by adding additional
  1229. characters in the parenthesis after the keyword. This looks like this:
  1230. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1231. record only the time of the state change. With X and Y being either
  1232. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1233. Y when leaving the state if and only if the *target* state does not
  1234. define X. You may omit any of the fast-selection key or X or /Y,
  1235. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1236. For backward compatibility, this variable may also be just a list
  1237. of keywords - in this case the interptetation (sequence or type) will be
  1238. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1239. :group 'org-todo
  1240. :group 'org-keywords
  1241. :type '(choice
  1242. (repeat :tag "Old syntax, just keywords"
  1243. (string :tag "Keyword"))
  1244. (repeat :tag "New syntax"
  1245. (cons
  1246. (choice
  1247. :tag "Interpretation"
  1248. (const :tag "Sequence (cycling hits every state)" sequence)
  1249. (const :tag "Type (cycling directly to DONE)" type))
  1250. (repeat
  1251. (string :tag "Keyword"))))))
  1252. (defvar org-todo-keywords-1 nil
  1253. "All TODO and DONE keywords active in a buffer.")
  1254. (make-variable-buffer-local 'org-todo-keywords-1)
  1255. (defvar org-todo-keywords-for-agenda nil)
  1256. (defvar org-done-keywords-for-agenda nil)
  1257. (defvar org-not-done-keywords nil)
  1258. (make-variable-buffer-local 'org-not-done-keywords)
  1259. (defvar org-done-keywords nil)
  1260. (make-variable-buffer-local 'org-done-keywords)
  1261. (defvar org-todo-heads nil)
  1262. (make-variable-buffer-local 'org-todo-heads)
  1263. (defvar org-todo-sets nil)
  1264. (make-variable-buffer-local 'org-todo-sets)
  1265. (defvar org-todo-log-states nil)
  1266. (make-variable-buffer-local 'org-todo-log-states)
  1267. (defvar org-todo-kwd-alist nil)
  1268. (make-variable-buffer-local 'org-todo-kwd-alist)
  1269. (defvar org-todo-key-alist nil)
  1270. (make-variable-buffer-local 'org-todo-key-alist)
  1271. (defvar org-todo-key-trigger nil)
  1272. (make-variable-buffer-local 'org-todo-key-trigger)
  1273. (defcustom org-todo-interpretation 'sequence
  1274. "Controls how TODO keywords are interpreted.
  1275. This variable is in principle obsolete and is only used for
  1276. backward compatibility, if the interpretation of todo keywords is
  1277. not given already in `org-todo-keywords'. See that variable for
  1278. more information."
  1279. :group 'org-todo
  1280. :group 'org-keywords
  1281. :type '(choice (const sequence)
  1282. (const type)))
  1283. (defcustom org-use-fast-todo-selection 'prefix
  1284. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1285. This variable describes if and under what circumstances the cycling
  1286. mechanism for TODO keywords will be replaced by a single-key, direct
  1287. selection scheme.
  1288. When nil, fast selection is never used.
  1289. When the symbol `prefix', it will be used when `org-todo' is called with
  1290. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1291. in an agenda buffer.
  1292. When t, fast selection is used by default. In this case, the prefix
  1293. argument forces cycling instead.
  1294. In all cases, the special interface is only used if access keys have actually
  1295. been assigned by the user, i.e. if keywords in the configuration are followed
  1296. by a letter in parenthesis, like TODO(t)."
  1297. :group 'org-todo
  1298. :type '(choice
  1299. (const :tag "Never" nil)
  1300. (const :tag "By default" t)
  1301. (const :tag "Only with C-u C-c C-t" prefix)))
  1302. (defcustom org-after-todo-state-change-hook nil
  1303. "Hook which is run after the state of a TODO item was changed.
  1304. The new state (a string with a TODO keyword, or nil) is available in the
  1305. Lisp variable `state'."
  1306. :group 'org-todo
  1307. :type 'hook)
  1308. (defcustom org-log-done nil
  1309. "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
  1310. When equal to the list (done), also prompt for a closing note.
  1311. This can also be configured on a per-file basis by adding one of
  1312. the following lines anywhere in the buffer:
  1313. #+STARTUP: logdone
  1314. #+STARTUP: lognotedone
  1315. #+STARTUP: nologdone"
  1316. :group 'org-todo
  1317. :group 'org-progress
  1318. :type '(choice
  1319. (const :tag "No logging" nil)
  1320. (const :tag "Record CLOSED timestamp" time)
  1321. (const :tag "Record CLOSED timestamp with closing note." note)))
  1322. ;; Normalize old uses of org-log-done.
  1323. (cond
  1324. ((eq org-log-done t) (setq org-log-done 'time))
  1325. ((and (listp org-log-done) (memq 'done org-log-done))
  1326. (setq org-log-done 'note)))
  1327. ;; FIXME: document
  1328. (defcustom org-log-note-clock-out nil
  1329. "Non-nil means, recored a note when clocking out of an item.
  1330. This can also be configured on a per-file basis by adding one of
  1331. the following lines anywhere in the buffer:
  1332. #+STARTUP: lognoteclock-out
  1333. #+STARTUP: nolognoteclock-out"
  1334. :group 'org-todo
  1335. :group 'org-progress
  1336. :type 'boolean)
  1337. (defcustom org-log-done-with-time t
  1338. "Non-nil means, the CLOSED time stamp will contain date and time.
  1339. When nil, only the date will be recorded."
  1340. :group 'org-progress
  1341. :type 'boolean)
  1342. (defcustom org-log-note-headings
  1343. '((done . "CLOSING NOTE %t")
  1344. (state . "State %-12s %t")
  1345. (clock-out . ""))
  1346. "Headings for notes added when clocking out or closing TODO items.
  1347. The value is an alist, with the car being a symbol indicating the note
  1348. context, and the cdr is the heading to be used. The heading may also be the
  1349. empty string.
  1350. %t in the heading will be replaced by a time stamp.
  1351. %s will be replaced by the new TODO state, in double quotes.
  1352. %u will be replaced by the user name.
  1353. %U will be replaced by the full user name."
  1354. :group 'org-todo
  1355. :group 'org-progress
  1356. :type '(list :greedy t
  1357. (cons (const :tag "Heading when closing an item" done) string)
  1358. (cons (const :tag
  1359. "Heading when changing todo state (todo sequence only)"
  1360. state) string)
  1361. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1362. (defcustom org-log-states-order-reversed t
  1363. "Non-nil means, the latest state change note will be directly after heading.
  1364. When nil, the notes will be orderer according to time."
  1365. :group 'org-todo
  1366. :group 'org-progress
  1367. :type 'boolean)
  1368. (defcustom org-log-repeat 'time
  1369. "Non-nil means, record moving through the DONE state when triggering repeat.
  1370. An auto-repeating tasks is immediately switched back to TODO when marked
  1371. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1372. the TODO keyword definition, or recording a cloing note by setting
  1373. `org-log-done', there will be no record of the task moving trhough DONE.
  1374. This variable forces taking a note anyway. Possible values are:
  1375. nil Don't force a record
  1376. time Record a time stamp
  1377. note Record a note
  1378. This option can also be set with on a per-file-basis with
  1379. #+STARTUP: logrepeat
  1380. #+STARTUP: lognoterepeat
  1381. #+STARTUP: nologrepeat
  1382. You can have local logging settings for a subtree by setting the LOGGING
  1383. property to one or more of these keywords."
  1384. :group 'org-todo
  1385. :group 'org-progress
  1386. :type '(choice
  1387. (const :tag "Don't force a record" nil)
  1388. (const :tag "Force recording the DONE state" time)
  1389. (const :tag "Force recording a note with the DONE state" note)))
  1390. (defcustom org-clock-into-drawer 2
  1391. "Should clocking info be wrapped into a drawer?
  1392. When t, clocking info will always be inserted into a :CLOCK: drawer.
  1393. If necessary, the drawer will be created.
  1394. When nil, the drawer will not be created, but used when present.
  1395. When an integer and the number of clocking entries in an item
  1396. reaches or exceeds this number, a drawer will be created."
  1397. :group 'org-todo
  1398. :group 'org-progress
  1399. :type '(choice
  1400. (const :tag "Always" t)
  1401. (const :tag "Only when drawer exists" nil)
  1402. (integer :tag "When at least N clock entries")))
  1403. (defcustom org-clock-out-when-done t
  1404. "When t, the clock will be stopped when the relevant entry is marked DONE.
  1405. Nil means, clock will keep running until stopped explicitly with
  1406. `C-c C-x C-o', or until the clock is started in a different item."
  1407. :group 'org-progress
  1408. :type 'boolean)
  1409. (defcustom org-clock-in-switch-to-state nil
  1410. "Set task to a special todo state while clocking it.
  1411. The value should be the state to which the entry should be switched."
  1412. :group 'org-progress
  1413. :group 'org-todo
  1414. :type '(choice
  1415. (const :tag "Don't force a state" nil)
  1416. (string :tag "State")))
  1417. (defgroup org-priorities nil
  1418. "Priorities in Org-mode."
  1419. :tag "Org Priorities"
  1420. :group 'org-todo)
  1421. (defcustom org-highest-priority ?A
  1422. "The highest priority of TODO items. A character like ?A, ?B etc.
  1423. Must have a smaller ASCII number than `org-lowest-priority'."
  1424. :group 'org-priorities
  1425. :type 'character)
  1426. (defcustom org-lowest-priority ?C
  1427. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1428. Must have a larger ASCII number than `org-highest-priority'."
  1429. :group 'org-priorities
  1430. :type 'character)
  1431. (defcustom org-default-priority ?B
  1432. "The default priority of TODO items.
  1433. This is the priority an item get if no explicit priority is given."
  1434. :group 'org-priorities
  1435. :type 'character)
  1436. (defcustom org-priority-start-cycle-with-default t
  1437. "Non-nil means, start with default priority when starting to cycle.
  1438. When this is nil, the first step in the cycle will be (depending on the
  1439. command used) one higher or lower that the default priority."
  1440. :group 'org-priorities
  1441. :type 'boolean)
  1442. (defgroup org-time nil
  1443. "Options concerning time stamps and deadlines in Org-mode."
  1444. :tag "Org Time"
  1445. :group 'org)
  1446. (defcustom org-insert-labeled-timestamps-at-point nil
  1447. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1448. When nil, these labeled time stamps are forces into the second line of an
  1449. entry, just after the headline. When scheduling from the global TODO list,
  1450. the time stamp will always be forced into the second line."
  1451. :group 'org-time
  1452. :type 'boolean)
  1453. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1454. "Formats for `format-time-string' which are used for time stamps.
  1455. It is not recommended to change this constant.")
  1456. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1457. "Number of minutes to round time stamps to.
  1458. These are two values, the first applies when first creating a time stamp.
  1459. The second applies when changing it with the commands `S-up' and `S-down'.
  1460. When changing the time stamp, this means that it will change in steps
  1461. of N minutes, as given by the second value.
  1462. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1463. numbers should be factors of 60, so for example 5, 10, 15.
  1464. When this is larger than 1, you can still force an exact time-stamp by using
  1465. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1466. and by using a prefix arg to `S-up/down' to specify the exact number
  1467. of minutes to shift."
  1468. :group 'org-time
  1469. :get '(lambda (var) ; Make sure all entries have 5 elements
  1470. (if (integerp (default-value var))
  1471. (list (default-value var) 5)
  1472. (default-value var)))
  1473. :type '(list
  1474. (integer :tag "when inserting times")
  1475. (integer :tag "when modifying times")))
  1476. ;; Make sure old customizations of this variable don't lead to problems.
  1477. (when (integerp org-time-stamp-rounding-minutes)
  1478. (setq org-time-stamp-rounding-minutes
  1479. (list org-time-stamp-rounding-minutes
  1480. org-time-stamp-rounding-minutes)))
  1481. (defcustom org-display-custom-times nil
  1482. "Non-nil means, overlay custom formats over all time stamps.
  1483. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1484. To turn this on on a per-file basis, insert anywhere in the file:
  1485. #+STARTUP: customtime"
  1486. :group 'org-time
  1487. :set 'set-default
  1488. :type 'sexp)
  1489. (make-variable-buffer-local 'org-display-custom-times)
  1490. (defcustom org-time-stamp-custom-formats
  1491. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1492. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1493. These are overlayed over the default ISO format if the variable
  1494. `org-display-custom-times' is set. Time like %H:%M should be at the
  1495. end of the second format."
  1496. :group 'org-time
  1497. :type 'sexp)
  1498. (defun org-time-stamp-format (&optional long inactive)
  1499. "Get the right format for a time string."
  1500. (let ((f (if long (cdr org-time-stamp-formats)
  1501. (car org-time-stamp-formats))))
  1502. (if inactive
  1503. (concat "[" (substring f 1 -1) "]")
  1504. f)))
  1505. (defcustom org-deadline-warning-days 14
  1506. "No. of days before expiration during which a deadline becomes active.
  1507. This variable governs the display in sparse trees and in the agenda.
  1508. When 0 or negative, it means use this number (the absolute value of it)
  1509. even if a deadline has a different individual lead time specified."
  1510. :group 'org-time
  1511. :group 'org-agenda-daily/weekly
  1512. :type 'number)
  1513. (defcustom org-read-date-prefer-future t
  1514. "Non-nil means, assume future for incomplete date input from user.
  1515. This affects the following situations:
  1516. 1. The user gives a day, but no month.
  1517. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1518. read this as the third of *next* month. However, if you enter \"17\",
  1519. it will be considered as *this* month.
  1520. 2. The user gives a month but not a year.
  1521. For example, if it is april and you enter \"feb 2\", this will be read
  1522. as feb 2, *next* year. \"May 5\", however, will be this year.
  1523. Currently this does not work for ISO week specifications.
  1524. When this option is nil, the current month and year will always be used
  1525. as defaults."
  1526. :group 'org-time
  1527. :type 'boolean)
  1528. (defcustom org-read-date-display-live t
  1529. "Non-nil means, display current interpretation of date prompt live.
  1530. This display will be in an overlay, in the minibuffer."
  1531. :group 'org-time
  1532. :type 'boolean)
  1533. (defcustom org-read-date-popup-calendar t
  1534. "Non-nil means, pop up a calendar when prompting for a date.
  1535. In the calendar, the date can be selected with mouse-1. However, the
  1536. minibuffer will also be active, and you can simply enter the date as well.
  1537. When nil, only the minibuffer will be available."
  1538. :group 'org-time
  1539. :type 'boolean)
  1540. (if (fboundp 'defvaralias)
  1541. (defvaralias 'org-popup-calendar-for-date-prompt
  1542. 'org-read-date-popup-calendar))
  1543. (defcustom org-extend-today-until 0
  1544. "The hour when your day really ends.
  1545. This has influence for the following applications:
  1546. - When switching the agenda to \"today\". It it is still earlier than
  1547. the time given here, the day recognized as TODAY is actually yesterday.
  1548. - When a date is read from the user and it is still before the time given
  1549. here, the current date and time will be assumed to be yesterday, 23:59.
  1550. FIXME:
  1551. IMPORTANT: This is still a very experimental feature, it may disappear
  1552. again or it may be extended to mean more things."
  1553. :group 'org-time
  1554. :type 'number)
  1555. (defcustom org-edit-timestamp-down-means-later nil
  1556. "Non-nil means, S-down will increase the time in a time stamp.
  1557. When nil, S-up will increase."
  1558. :group 'org-time
  1559. :type 'boolean)
  1560. (defcustom org-calendar-follow-timestamp-change t
  1561. "Non-nil means, make the calendar window follow timestamp changes.
  1562. When a timestamp is modified and the calendar window is visible, it will be
  1563. moved to the new date."
  1564. :group 'org-time
  1565. :type 'boolean)
  1566. (defcustom org-clock-heading-function nil
  1567. "When non-nil, should be a function to create `org-clock-heading'.
  1568. This is the string shown in the mode line when a clock is running.
  1569. The function is called with point at the beginning of the headline."
  1570. :group 'org-time ; FIXME: Should we have a separate group????
  1571. :type 'function)
  1572. (defgroup org-tags nil
  1573. "Options concerning tags in Org-mode."
  1574. :tag "Org Tags"
  1575. :group 'org)
  1576. (defcustom org-tag-alist nil
  1577. "List of tags allowed in Org-mode files.
  1578. When this list is nil, Org-mode will base TAG input on what is already in the
  1579. buffer.
  1580. The value of this variable is an alist, the car of each entry must be a
  1581. keyword as a string, the cdr may be a character that is used to select
  1582. that tag through the fast-tag-selection interface.
  1583. See the manual for details."
  1584. :group 'org-tags
  1585. :type '(repeat
  1586. (choice
  1587. (cons (string :tag "Tag name")
  1588. (character :tag "Access char"))
  1589. (const :tag "Start radio group" (:startgroup))
  1590. (const :tag "End radio group" (:endgroup)))))
  1591. (defcustom org-use-fast-tag-selection 'auto
  1592. "Non-nil means, use fast tag selection scheme.
  1593. This is a special interface to select and deselect tags with single keys.
  1594. When nil, fast selection is never used.
  1595. When the symbol `auto', fast selection is used if and only if selection
  1596. characters for tags have been configured, either through the variable
  1597. `org-tag-alist' or through a #+TAGS line in the buffer.
  1598. When t, fast selection is always used and selection keys are assigned
  1599. automatically if necessary."
  1600. :group 'org-tags
  1601. :type '(choice
  1602. (const :tag "Always" t)
  1603. (const :tag "Never" nil)
  1604. (const :tag "When selection characters are configured" 'auto)))
  1605. (defcustom org-fast-tag-selection-single-key nil
  1606. "Non-nil means, fast tag selection exits after first change.
  1607. When nil, you have to press RET to exit it.
  1608. During fast tag selection, you can toggle this flag with `C-c'.
  1609. This variable can also have the value `expert'. In this case, the window
  1610. displaying the tags menu is not even shown, until you press C-c again."
  1611. :group 'org-tags
  1612. :type '(choice
  1613. (const :tag "No" nil)
  1614. (const :tag "Yes" t)
  1615. (const :tag "Expert" expert)))
  1616. (defvar org-fast-tag-selection-include-todo nil
  1617. "Non-nil means, fast tags selection interface will also offer TODO states.
  1618. This is an undocumented feature, you should not rely on it.")
  1619. (defcustom org-tags-column -80
  1620. "The column to which tags should be indented in a headline.
  1621. If this number is positive, it specifies the column. If it is negative,
  1622. it means that the tags should be flushright to that column. For example,
  1623. -80 works well for a normal 80 character screen."
  1624. :group 'org-tags
  1625. :type 'integer)
  1626. (defcustom org-auto-align-tags t
  1627. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1628. These operations change the length of a headline and therefore shift
  1629. the tags around. With this options turned on, after each such operation
  1630. the tags are again aligned to `org-tags-column'."
  1631. :group 'org-tags
  1632. :type 'boolean)
  1633. (defcustom org-use-tag-inheritance t
  1634. "Non-nil means, tags in levels apply also for sublevels.
  1635. When nil, only the tags directly given in a specific line apply there.
  1636. If you turn off this option, you very likely want to turn on the
  1637. companion option `org-tags-match-list-sublevels'.
  1638. This may also be a list of tags that should be inherited, or a regexp that
  1639. matches tags that should be inherited."
  1640. :group 'org-tags
  1641. :type '(choice
  1642. (const :tag "Not" nil)
  1643. (const :tag "Always" t)
  1644. (repeat :tag "Specific tags" (string :tag "Tag"))
  1645. (regexp :tag "Tags matched by regexp")))
  1646. (defun org-tag-inherit-p (tag)
  1647. "Check if TAG is one that should be inherited."
  1648. (cond
  1649. ((eq org-use-tag-inheritance t) t)
  1650. ((not org-use-tag-inheritance) nil)
  1651. ((stringp org-use-tag-inheritance)
  1652. (string-match org-use-tag-inheritance tag))
  1653. ((listp org-use-tag-inheritance)
  1654. (member tag org-use-tag-inheritance))
  1655. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1656. (defcustom org-tags-match-list-sublevels nil
  1657. "Non-nil means list also sublevels of headlines matching tag search.
  1658. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1659. the sublevels of a headline matching a tag search often also match
  1660. the same search. Listing all of them can create very long lists.
  1661. Setting this variable to nil causes subtrees of a match to be skipped.
  1662. This option is off by default, because inheritance in on. If you turn
  1663. inheritance off, you very likely want to turn this option on.
  1664. As a special case, if the tag search is restricted to TODO items, the
  1665. value of this variable is ignored and sublevels are always checked, to
  1666. make sure all corresponding TODO items find their way into the list."
  1667. :group 'org-tags
  1668. :type 'boolean)
  1669. (defvar org-tags-history nil
  1670. "History of minibuffer reads for tags.")
  1671. (defvar org-last-tags-completion-table nil
  1672. "The last used completion table for tags.")
  1673. (defvar org-after-tags-change-hook nil
  1674. "Hook that is run after the tags in a line have changed.")
  1675. (defgroup org-properties nil
  1676. "Options concerning properties in Org-mode."
  1677. :tag "Org Properties"
  1678. :group 'org)
  1679. (defcustom org-property-format "%-10s %s"
  1680. "How property key/value pairs should be formatted by `indent-line'.
  1681. When `indent-line' hits a property definition, it will format the line
  1682. according to this format, mainly to make sure that the values are
  1683. lined-up with respect to each other."
  1684. :group 'org-properties
  1685. :type 'string)
  1686. (defcustom org-use-property-inheritance nil
  1687. "Non-nil means, properties apply also for sublevels.
  1688. This setting is chiefly used during property searches. Turning it on can
  1689. cause significant overhead when doing a search, which is why it is not
  1690. on by default.
  1691. When nil, only the properties directly given in the current entry count.
  1692. When t, every property is inherited. The value may also be a list of
  1693. properties that should have inheritance, or a regular expression matching
  1694. properties that should be inherited.
  1695. However, note that some special properties use inheritance under special
  1696. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1697. and the properties ending in \"_ALL\" when they are used as descriptor
  1698. for valid values of a property.
  1699. Note for programmers:
  1700. When querying an entry with `org-entry-get', you can control if inheritance
  1701. should be used. By default, `org-entry-get' looks only at the local
  1702. properties. You can request inheritance by setting the inherit argument
  1703. to t (to force inheritance) or to `selective' (to respect the setting
  1704. in this variable)."
  1705. :group 'org-properties
  1706. :type '(choice
  1707. (const :tag "Not" nil)
  1708. (const :tag "Always" t)
  1709. (repeat :tag "Specific properties" (string :tag "Property"))
  1710. (regexp :tag "Properties matched by regexp")))
  1711. (defun org-property-inherit-p (property)
  1712. "Check if PROPERTY is one that should be inherited."
  1713. (cond
  1714. ((eq org-use-property-inheritance t) t)
  1715. ((not org-use-property-inheritance) nil)
  1716. ((stringp org-use-property-inheritance)
  1717. (string-match org-use-property-inheritance property))
  1718. ((listp org-use-property-inheritance)
  1719. (member property org-use-property-inheritance))
  1720. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1721. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1722. "The default column format, if no other format has been defined.
  1723. This variable can be set on the per-file basis by inserting a line
  1724. #+COLUMNS: %25ITEM ....."
  1725. :group 'org-properties
  1726. :type 'string)
  1727. (defcustom org-global-properties nil
  1728. "List of property/value pairs that can be inherited by any entry.
  1729. You can set buffer-local values for this by adding lines like
  1730. #+PROPERTY: NAME VALUE"
  1731. :group 'org-properties
  1732. :type '(repeat
  1733. (cons (string :tag "Property")
  1734. (string :tag "Value"))))
  1735. (defvar org-local-properties nil
  1736. "List of property/value pairs that can be inherited by any entry.
  1737. Valid for the current buffer.
  1738. This variable is populated from #+PROPERTY lines.")
  1739. (defgroup org-agenda nil
  1740. "Options concerning agenda views in Org-mode."
  1741. :tag "Org Agenda"
  1742. :group 'org)
  1743. (defvar org-category nil
  1744. "Variable used by org files to set a category for agenda display.
  1745. Such files should use a file variable to set it, for example
  1746. # -*- mode: org; org-category: \"ELisp\"
  1747. or contain a special line
  1748. #+CATEGORY: ELisp
  1749. If the file does not specify a category, then file's base name
  1750. is used instead.")
  1751. (make-variable-buffer-local 'org-category)
  1752. (defcustom org-agenda-files nil
  1753. "The files to be used for agenda display.
  1754. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1755. \\[org-remove-file]. You can also use customize to edit the list.
  1756. If an entry is a directory, all files in that directory that are matched by
  1757. `org-agenda-file-regexp' will be part of the file list.
  1758. If the value of the variable is not a list but a single file name, then
  1759. the list of agenda files is actually stored and maintained in that file, one
  1760. agenda file per line."
  1761. :group 'org-agenda
  1762. :type '(choice
  1763. (repeat :tag "List of files and directories" file)
  1764. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1765. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1766. "Regular expression to match files for `org-agenda-files'.
  1767. If any element in the list in that variable contains a directory instead
  1768. of a normal file, all files in that directory that are matched by this
  1769. regular expression will be included."
  1770. :group 'org-agenda
  1771. :type 'regexp)
  1772. (defcustom org-agenda-text-search-extra-files nil
  1773. "List of extra files to be searched by text search commands.
  1774. These files will be search in addition to the agenda files bu the
  1775. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  1776. Note that these files will only be searched for text search commands,
  1777. not for the other agenda views like todo lists, tag earches or the weekly
  1778. agenda. This variable is intended to list notes and possibly archive files
  1779. that should also be searched by these two commands."
  1780. :group 'org-agenda
  1781. :type '(repeat file))
  1782. (if (fboundp 'defvaralias)
  1783. (defvaralias 'org-agenda-multi-occur-extra-files
  1784. 'org-agenda-text-search-extra-files))
  1785. (defcustom org-agenda-skip-unavailable-files nil
  1786. "t means to just skip non-reachable files in `org-agenda-files'.
  1787. Nil means to remove them, after a query, from the list."
  1788. :group 'org-agenda
  1789. :type 'boolean)
  1790. (defcustom org-calendar-to-agenda-key [?c]
  1791. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1792. The command `org-calendar-goto-agenda' will be bound to this key. The
  1793. default is the character `c' because then `c' can be used to switch back and
  1794. forth between agenda and calendar."
  1795. :group 'org-agenda
  1796. :type 'sexp)
  1797. (eval-after-load "calendar"
  1798. '(org-defkey calendar-mode-map org-calendar-to-agenda-key
  1799. 'org-calendar-goto-agenda))
  1800. (defgroup org-latex nil
  1801. "Options for embedding LaTeX code into Org-mode."
  1802. :tag "Org LaTeX"
  1803. :group 'org)
  1804. (defcustom org-format-latex-options
  1805. '(:foreground default :background default :scale 1.0
  1806. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  1807. :matchers ("begin" "$" "$$" "\\(" "\\["))
  1808. "Options for creating images from LaTeX fragments.
  1809. This is a property list with the following properties:
  1810. :foreground the foreground color for images embedded in emacs, e.g. \"Black\".
  1811. `default' means use the forground of the default face.
  1812. :background the background color, or \"Transparent\".
  1813. `default' means use the background of the default face.
  1814. :scale a scaling factor for the size of the images
  1815. :html-foreground, :html-background, :html-scale
  1816. The same numbers for HTML export.
  1817. :matchers a list indicating which matchers should be used to
  1818. find LaTeX fragments. Valid members of this list are:
  1819. \"begin\" find environments
  1820. \"$\" find math expressions surrounded by $...$
  1821. \"$$\" find math expressions surrounded by $$....$$
  1822. \"\\(\" find math expressions surrounded by \\(...\\)
  1823. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  1824. :group 'org-latex
  1825. :type 'plist)
  1826. (defcustom org-format-latex-header "\\documentclass{article}
  1827. \\usepackage{fullpage} % do not remove
  1828. \\usepackage{amssymb}
  1829. \\usepackage[usenames]{color}
  1830. \\usepackage{amsmath}
  1831. \\usepackage{latexsym}
  1832. \\usepackage[mathscr]{eucal}
  1833. \\pagestyle{empty} % do not remove"
  1834. "The document header used for processing LaTeX fragments."
  1835. :group 'org-latex
  1836. :type 'string)
  1837. (defgroup org-font-lock nil
  1838. "Font-lock settings for highlighting in Org-mode."
  1839. :tag "Org Font Lock"
  1840. :group 'org)
  1841. (defcustom org-level-color-stars-only nil
  1842. "Non-nil means fontify only the stars in each headline.
  1843. When nil, the entire headline is fontified.
  1844. Changing it requires restart of `font-lock-mode' to become effective
  1845. also in regions already fontified."
  1846. :group 'org-font-lock
  1847. :type 'boolean)
  1848. (defcustom org-hide-leading-stars nil
  1849. "Non-nil means, hide the first N-1 stars in a headline.
  1850. This works by using the face `org-hide' for these stars. This
  1851. face is white for a light background, and black for a dark
  1852. background. You may have to customize the face `org-hide' to
  1853. make this work.
  1854. Changing it requires restart of `font-lock-mode' to become effective
  1855. also in regions already fontified.
  1856. You may also set this on a per-file basis by adding one of the following
  1857. lines to the buffer:
  1858. #+STARTUP: hidestars
  1859. #+STARTUP: showstars"
  1860. :group 'org-font-lock
  1861. :type 'boolean)
  1862. (defcustom org-fontify-done-headline nil
  1863. "Non-nil means, change the face of a headline if it is marked DONE.
  1864. Normally, only the TODO/DONE keyword indicates the state of a headline.
  1865. When this is non-nil, the headline after the keyword is set to the
  1866. `org-headline-done' as an additional indication."
  1867. :group 'org-font-lock
  1868. :type 'boolean)
  1869. (defcustom org-fontify-emphasized-text t
  1870. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  1871. Changing this variable requires a restart of Emacs to take effect."
  1872. :group 'org-font-lock
  1873. :type 'boolean)
  1874. (defcustom org-highlight-latex-fragments-and-specials nil
  1875. "Non-nil means, fontify what is treated specially by the exporters."
  1876. :group 'org-font-lock
  1877. :type 'boolean)
  1878. (defcustom org-hide-emphasis-markers nil
  1879. "Non-nil mean font-lock should hide the emphasis marker characters."
  1880. :group 'org-font-lock
  1881. :type 'boolean)
  1882. (defvar org-emph-re nil
  1883. "Regular expression for matching emphasis.")
  1884. (defvar org-verbatim-re nil
  1885. "Regular expression for matching verbatim text.")
  1886. (defvar org-emphasis-regexp-components) ; defined just below
  1887. (defvar org-emphasis-alist) ; defined just below
  1888. (defun org-set-emph-re (var val)
  1889. "Set variable and compute the emphasis regular expression."
  1890. (set var val)
  1891. (when (and (boundp 'org-emphasis-alist)
  1892. (boundp 'org-emphasis-regexp-components)
  1893. org-emphasis-alist org-emphasis-regexp-components)
  1894. (let* ((e org-emphasis-regexp-components)
  1895. (pre (car e))
  1896. (post (nth 1 e))
  1897. (border (nth 2 e))
  1898. (body (nth 3 e))
  1899. (nl (nth 4 e))
  1900. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  1901. (body1 (concat body "*?"))
  1902. (markers (mapconcat 'car org-emphasis-alist ""))
  1903. (vmarkers (mapconcat
  1904. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  1905. org-emphasis-alist "")))
  1906. ;; make sure special characters appear at the right position in the class
  1907. (if (string-match "\\^" markers)
  1908. (setq markers (concat (replace-match "" t t markers) "^")))
  1909. (if (string-match "-" markers)
  1910. (setq markers (concat (replace-match "" t t markers) "-")))
  1911. (if (string-match "\\^" vmarkers)
  1912. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  1913. (if (string-match "-" vmarkers)
  1914. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  1915. (if (> nl 0)
  1916. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  1917. (int-to-string nl) "\\}")))
  1918. ;; Make the regexp
  1919. (setq org-emph-re
  1920. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  1921. "\\("
  1922. "\\([" markers "]\\)"
  1923. "\\("
  1924. "[^" border "]\\|"
  1925. "[^" border (if (and nil stacked) markers) "]"
  1926. body1
  1927. "[^" border (if (and nil stacked) markers) "]"
  1928. "\\)"
  1929. "\\3\\)"
  1930. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  1931. (setq org-verbatim-re
  1932. (concat "\\([" pre "]\\|^\\)"
  1933. "\\("
  1934. "\\([" vmarkers "]\\)"
  1935. "\\("
  1936. "[^" border "]\\|"
  1937. "[^" border "]"
  1938. body1
  1939. "[^" border "]"
  1940. "\\)"
  1941. "\\3\\)"
  1942. "\\([" post "]\\|$\\)")))))
  1943. (defcustom org-emphasis-regexp-components
  1944. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  1945. "Components used to build the regular expression for emphasis.
  1946. This is a list with 6 entries. Terminology: In an emphasis string
  1947. like \" *strong word* \", we call the initial space PREMATCH, the final
  1948. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  1949. and \"trong wor\" is the body. The different components in this variable
  1950. specify what is allowed/forbidden in each part:
  1951. pre Chars allowed as prematch. Beginning of line will be allowed too.
  1952. post Chars allowed as postmatch. End of line will be allowed too.
  1953. border The chars *forbidden* as border characters.
  1954. body-regexp A regexp like \".\" to match a body character. Don't use
  1955. non-shy groups here, and don't allow newline here.
  1956. newline The maximum number of newlines allowed in an emphasis exp.
  1957. Use customize to modify this, or restart Emacs after changing it."
  1958. :group 'org-font-lock
  1959. :set 'org-set-emph-re
  1960. :type '(list
  1961. (sexp :tag "Allowed chars in pre ")
  1962. (sexp :tag "Allowed chars in post ")
  1963. (sexp :tag "Forbidden chars in border ")
  1964. (sexp :tag "Regexp for body ")
  1965. (integer :tag "number of newlines allowed")
  1966. (option (boolean :tag "Stacking (DISABLED) "))))
  1967. (defcustom org-emphasis-alist
  1968. '(("*" bold "<b>" "</b>")
  1969. ("/" italic "<i>" "</i>")
  1970. ("_" underline "<u>" "</u>")
  1971. ("=" org-code "<code>" "</code>" verbatim)
  1972. ("~" org-verbatim "" "" verbatim)
  1973. ("+" (:strike-through t) "<del>" "</del>")
  1974. )
  1975. "Special syntax for emphasized text.
  1976. Text starting and ending with a special character will be emphasized, for
  1977. example *bold*, _underlined_ and /italic/. This variable sets the marker
  1978. characters, the face to be used by font-lock for highlighting in Org-mode
  1979. Emacs buffers, and the HTML tags to be used for this.
  1980. Use customize to modify this, or restart Emacs after changing it."
  1981. :group 'org-font-lock
  1982. :set 'org-set-emph-re
  1983. :type '(repeat
  1984. (list
  1985. (string :tag "Marker character")
  1986. (choice
  1987. (face :tag "Font-lock-face")
  1988. (plist :tag "Face property list"))
  1989. (string :tag "HTML start tag")
  1990. (string :tag "HTML end tag")
  1991. (option (const verbatim)))))
  1992. ;;; The faces
  1993. (defgroup org-faces nil
  1994. "Faces in Org-mode."
  1995. :tag "Org Faces"
  1996. :group 'org-font-lock)
  1997. (defface org-hide
  1998. '((((background light)) (:foreground "white"))
  1999. (((background dark)) (:foreground "black")))
  2000. "Face used to hide leading stars in headlines.
  2001. The forground color of this face should be equal to the background
  2002. color of the frame."
  2003. :group 'org-faces)
  2004. (defface org-level-1 ;; font-lock-function-name-face
  2005. (org-compatible-face 'outline-1
  2006. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2007. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2008. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2009. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2010. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  2011. (t (:bold t))))
  2012. "Face used for level 1 headlines."
  2013. :group 'org-faces)
  2014. (defface org-level-2 ;; font-lock-variable-name-face
  2015. (org-compatible-face 'outline-2
  2016. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  2017. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  2018. (((class color) (min-colors 8) (background light)) (:foreground "yellow"))
  2019. (((class color) (min-colors 8) (background dark)) (:foreground "yellow" :bold t))
  2020. (t (:bold t))))
  2021. "Face used for level 2 headlines."
  2022. :group 'org-faces)
  2023. (defface org-level-3 ;; font-lock-keyword-face
  2024. (org-compatible-face 'outline-3
  2025. '((((class color) (min-colors 88) (background light)) (:foreground "Purple"))
  2026. (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
  2027. (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
  2028. (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
  2029. (((class color) (min-colors 8) (background light)) (:foreground "purple" :bold t))
  2030. (((class color) (min-colors 8) (background dark)) (:foreground "cyan" :bold t))
  2031. (t (:bold t))))
  2032. "Face used for level 3 headlines."
  2033. :group 'org-faces)
  2034. (defface org-level-4 ;; font-lock-comment-face
  2035. (org-compatible-face 'outline-4
  2036. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2037. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2038. (((class color) (min-colors 16) (background light)) (:foreground "red"))
  2039. (((class color) (min-colors 16) (background dark)) (:foreground "red1"))
  2040. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2041. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2042. (t (:bold t))))
  2043. "Face used for level 4 headlines."
  2044. :group 'org-faces)
  2045. (defface org-level-5 ;; font-lock-type-face
  2046. (org-compatible-face 'outline-5
  2047. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
  2048. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
  2049. (((class color) (min-colors 8)) (:foreground "green"))))
  2050. "Face used for level 5 headlines."
  2051. :group 'org-faces)
  2052. (defface org-level-6 ;; font-lock-constant-face
  2053. (org-compatible-face 'outline-6
  2054. '((((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
  2055. (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
  2056. (((class color) (min-colors 8)) (:foreground "magenta"))))
  2057. "Face used for level 6 headlines."
  2058. :group 'org-faces)
  2059. (defface org-level-7 ;; font-lock-builtin-face
  2060. (org-compatible-face 'outline-7
  2061. '((((class color) (min-colors 16) (background light)) (:foreground "Orchid"))
  2062. (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue"))
  2063. (((class color) (min-colors 8)) (:foreground "blue"))))
  2064. "Face used for level 7 headlines."
  2065. :group 'org-faces)
  2066. (defface org-level-8 ;; font-lock-string-face
  2067. (org-compatible-face 'outline-8
  2068. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2069. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2070. (((class color) (min-colors 8)) (:foreground "green"))))
  2071. "Face used for level 8 headlines."
  2072. :group 'org-faces)
  2073. (defface org-special-keyword ;; font-lock-string-face
  2074. (org-compatible-face nil
  2075. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2076. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2077. (t (:italic t))))
  2078. "Face used for special keywords."
  2079. :group 'org-faces)
  2080. (defface org-drawer ;; font-lock-function-name-face
  2081. (org-compatible-face nil
  2082. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2083. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2084. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2085. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2086. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  2087. (t (:bold t))))
  2088. "Face used for drawers."
  2089. :group 'org-faces)
  2090. (defface org-property-value nil
  2091. "Face used for the value of a property."
  2092. :group 'org-faces)
  2093. (defface org-column
  2094. (org-compatible-face nil
  2095. '((((class color) (min-colors 16) (background light))
  2096. (:background "grey90"))
  2097. (((class color) (min-colors 16) (background dark))
  2098. (:background "grey30"))
  2099. (((class color) (min-colors 8))
  2100. (:background "cyan" :foreground "black"))
  2101. (t (:inverse-video t))))
  2102. "Face for column display of entry properties."
  2103. :group 'org-faces)
  2104. (when (fboundp 'set-face-attribute)
  2105. ;; Make sure that a fixed-width face is used when we have a column table.
  2106. (set-face-attribute 'org-column nil
  2107. :height (face-attribute 'default :height)
  2108. :family (face-attribute 'default :family)))
  2109. (defface org-warning
  2110. (org-compatible-face 'font-lock-warning-face
  2111. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  2112. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  2113. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2114. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2115. (t (:bold t))))
  2116. "Face for deadlines and TODO keywords."
  2117. :group 'org-faces)
  2118. (defface org-archived ; similar to shadow
  2119. (org-compatible-face 'shadow
  2120. '((((class color grayscale) (min-colors 88) (background light))
  2121. (:foreground "grey50"))
  2122. (((class color grayscale) (min-colors 88) (background dark))
  2123. (:foreground "grey70"))
  2124. (((class color) (min-colors 8) (background light))
  2125. (:foreground "green"))
  2126. (((class color) (min-colors 8) (background dark))
  2127. (:foreground "yellow"))))
  2128. "Face for headline with the ARCHIVE tag."
  2129. :group 'org-faces)
  2130. (defface org-link
  2131. '((((class color) (background light)) (:foreground "Purple" :underline t))
  2132. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  2133. (t (:underline t)))
  2134. "Face for links."
  2135. :group 'org-faces)
  2136. (defface org-ellipsis
  2137. '((((class color) (background light)) (:foreground "DarkGoldenrod" :underline t))
  2138. (((class color) (background dark)) (:foreground "LightGoldenrod" :underline t))
  2139. (t (:strike-through t)))
  2140. "Face for the ellipsis in folded text."
  2141. :group 'org-faces)
  2142. (defface org-target
  2143. '((((class color) (background light)) (:underline t))
  2144. (((class color) (background dark)) (:underline t))
  2145. (t (:underline t)))
  2146. "Face for links."
  2147. :group 'org-faces)
  2148. (defface org-date
  2149. '((((class color) (background light)) (:foreground "Purple" :underline t))
  2150. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  2151. (t (:underline t)))
  2152. "Face for links."
  2153. :group 'org-faces)
  2154. (defface org-sexp-date
  2155. '((((class color) (background light)) (:foreground "Purple"))
  2156. (((class color) (background dark)) (:foreground "Cyan"))
  2157. (t (:underline t)))
  2158. "Face for links."
  2159. :group 'org-faces)
  2160. (defface org-tag
  2161. '((t (:bold t)))
  2162. "Face for tags."
  2163. :group 'org-faces)
  2164. (defface org-todo ; font-lock-warning-face
  2165. (org-compatible-face nil
  2166. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  2167. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  2168. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2169. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2170. (t (:inverse-video t :bold t))))
  2171. "Face for TODO keywords."
  2172. :group 'org-faces)
  2173. (defface org-done ;; font-lock-type-face
  2174. (org-compatible-face nil
  2175. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen" :bold t))
  2176. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen" :bold t))
  2177. (((class color) (min-colors 8)) (:foreground "green"))
  2178. (t (:bold t))))
  2179. "Face used for todo keywords that indicate DONE items."
  2180. :group 'org-faces)
  2181. (defface org-headline-done ;; font-lock-string-face
  2182. (org-compatible-face nil
  2183. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2184. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2185. (((class color) (min-colors 8) (background light)) (:bold nil))))
  2186. "Face used to indicate that a headline is DONE.
  2187. This face is only used if `org-fontify-done-headline' is set. If applies
  2188. to the part of the headline after the DONE keyword."
  2189. :group 'org-faces)
  2190. (defcustom org-todo-keyword-faces nil
  2191. "Faces for specific TODO keywords.
  2192. This is a list of cons cells, with TODO keywords in the car
  2193. and faces in the cdr. The face can be a symbol, or a property
  2194. list of attributes, like (:foreground \"blue\" :weight bold :underline t)."
  2195. :group 'org-faces
  2196. :group 'org-todo
  2197. :type '(repeat
  2198. (cons
  2199. (string :tag "keyword")
  2200. (sexp :tag "face"))))
  2201. (defface org-table ;; font-lock-function-name-face
  2202. (org-compatible-face nil
  2203. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2204. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2205. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2206. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2207. (((class color) (min-colors 8) (background light)) (:foreground "blue"))
  2208. (((class color) (min-colors 8) (background dark)))))
  2209. "Face used for tables."
  2210. :group 'org-faces)
  2211. (defface org-formula
  2212. (org-compatible-face nil
  2213. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2214. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2215. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2216. (((class color) (min-colors 8) (background dark)) (:foreground "red"))
  2217. (t (:bold t :italic t))))
  2218. "Face for formulas."
  2219. :group 'org-faces)
  2220. (defface org-code
  2221. (org-compatible-face nil
  2222. '((((class color grayscale) (min-colors 88) (background light))
  2223. (:foreground "grey50"))
  2224. (((class color grayscale) (min-colors 88) (background dark))
  2225. (:foreground "grey70"))
  2226. (((class color) (min-colors 8) (background light))
  2227. (:foreground "green"))
  2228. (((class color) (min-colors 8) (background dark))
  2229. (:foreground "yellow"))))
  2230. "Face for fixed-with text like code snippets."
  2231. :group 'org-faces
  2232. :version "22.1")
  2233. (defface org-verbatim
  2234. (org-compatible-face nil
  2235. '((((class color grayscale) (min-colors 88) (background light))
  2236. (:foreground "grey50" :underline t))
  2237. (((class color grayscale) (min-colors 88) (background dark))
  2238. (:foreground "grey70" :underline t))
  2239. (((class color) (min-colors 8) (background light))
  2240. (:foreground "green" :underline t))
  2241. (((class color) (min-colors 8) (background dark))
  2242. (:foreground "yellow" :underline t))))
  2243. "Face for fixed-with text like code snippets."
  2244. :group 'org-faces
  2245. :version "22.1")
  2246. (defface org-agenda-structure ;; font-lock-function-name-face
  2247. (org-compatible-face nil
  2248. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2249. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2250. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2251. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2252. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  2253. (t (:bold t))))
  2254. "Face used in agenda for captions and dates."
  2255. :group 'org-faces)
  2256. (defface org-scheduled-today
  2257. (org-compatible-face nil
  2258. '((((class color) (min-colors 88) (background light)) (:foreground "DarkGreen"))
  2259. (((class color) (min-colors 88) (background dark)) (:foreground "PaleGreen"))
  2260. (((class color) (min-colors 8)) (:foreground "green"))
  2261. (t (:bold t :italic t))))
  2262. "Face for items scheduled for a certain day."
  2263. :group 'org-faces)
  2264. (defface org-scheduled-previously
  2265. (org-compatible-face nil
  2266. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2267. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2268. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2269. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2270. (t (:bold t))))
  2271. "Face for items scheduled previously, and not yet done."
  2272. :group 'org-faces)
  2273. (defface org-upcoming-deadline
  2274. (org-compatible-face nil
  2275. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2276. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2277. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2278. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2279. (t (:bold t))))
  2280. "Face for items scheduled previously, and not yet done."
  2281. :group 'org-faces)
  2282. (defcustom org-agenda-deadline-faces
  2283. '((1.0 . org-warning)
  2284. (0.5 . org-upcoming-deadline)
  2285. (0.0 . default))
  2286. "Faces for showing deadlines in the agenda.
  2287. This is a list of cons cells. The cdr of each cell is a face to be used,
  2288. and it can also just be like '(:foreground \"yellow\").
  2289. Each car is a fraction of the head-warning time that must have passed for
  2290. this the face in the cdr to be used for display. The numbers must be
  2291. given in descending order. The head-warning time is normally taken
  2292. from `org-deadline-warning-days', but can also be specified in the deadline
  2293. timestamp itself, like this:
  2294. DEADLINE: <2007-08-13 Mon -8d>
  2295. You may use d for days, w for weeks, m for months and y for years. Months
  2296. and years will only be treated in an approximate fashion (30.4 days for a
  2297. month and 365.24 days for a year)."
  2298. :group 'org-faces
  2299. :group 'org-agenda-daily/weekly
  2300. :type '(repeat
  2301. (cons
  2302. (number :tag "Fraction of head-warning time passed")
  2303. (sexp :tag "Face"))))
  2304. ;; FIXME: this is not a good face yet.
  2305. (defface org-agenda-restriction-lock
  2306. (org-compatible-face nil
  2307. '((((class color) (min-colors 88) (background light)) (:background "yellow1"))
  2308. (((class color) (min-colors 88) (background dark)) (:background "skyblue4"))
  2309. (((class color) (min-colors 16) (background light)) (:background "yellow1"))
  2310. (((class color) (min-colors 16) (background dark)) (:background "skyblue4"))
  2311. (((class color) (min-colors 8)) (:background "cyan" :foreground "black"))
  2312. (t (:inverse-video t))))
  2313. "Face for showing the agenda restriction lock."
  2314. :group 'org-faces)
  2315. (defface org-time-grid ;; font-lock-variable-name-face
  2316. (org-compatible-face nil
  2317. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  2318. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  2319. (((class color) (min-colors 8)) (:foreground "yellow" :weight light))))
  2320. "Face used for time grids."
  2321. :group 'org-faces)
  2322. (defconst org-level-faces
  2323. '(org-level-1 org-level-2 org-level-3 org-level-4
  2324. org-level-5 org-level-6 org-level-7 org-level-8
  2325. ))
  2326. (defcustom org-n-level-faces (length org-level-faces)
  2327. "The number of different faces to be used for headlines.
  2328. Org-mode defines 8 different headline faces, so this can be at most 8.
  2329. If it is less than 8, the level-1 face gets re-used for level N+1 etc."
  2330. :type 'number
  2331. :group 'org-faces)
  2332. ;;; Functions and variables from ther packages
  2333. ;; Declared here to avoid compiler warnings
  2334. ;; XEmacs only
  2335. (defvar outline-mode-menu-heading)
  2336. (defvar outline-mode-menu-show)
  2337. (defvar outline-mode-menu-hide)
  2338. (defvar zmacs-regions) ; XEmacs regions
  2339. ;; Emacs only
  2340. (defvar mark-active)
  2341. ;; Various packages
  2342. ;; FIXME: get the argument lists for the UNKNOWN stuff
  2343. (declare-function bibtex-beginning-of-entry "bibtex" ())
  2344. (declare-function bibtex-generate-autokey "bibtex" ())
  2345. (declare-function bibtex-parse-entry "bibtex" (&optional content))
  2346. (declare-function bibtex-url "bibtex" (&optional pos no-browse))
  2347. (declare-function calendar-absolute-from-iso "cal-iso" (&optional date))
  2348. (declare-function calendar-forward-day "cal-move" (arg))
  2349. (declare-function calendar-goto-date "cal-move" (date))
  2350. (declare-function calendar-goto-today "cal-move" ())
  2351. (declare-function calendar-iso-from-absolute "cal-iso" (&optional date))
  2352. (defvar calc-embedded-close-formula)
  2353. (defvar calc-embedded-open-formula)
  2354. (declare-function cdlatex-tab "ext:cdlatex" ())
  2355. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2356. (defvar font-lock-unfontify-region-function)
  2357. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2358. (declare-function org-agenda-skip "org-agenda" ())
  2359. (declare-function org-format-agenda-item "org-agenda"
  2360. (extra txt &optional category tags dotime noprefix remove-re))
  2361. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2362. (declare-function org-agenda-change-all-lines "org-agenda"
  2363. (newhead hdmarker &optional fixface))
  2364. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2365. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2366. (declare-function parse-time-string "parse-time" (string))
  2367. (declare-function remember "remember" (&optional initial))
  2368. (declare-function remember-buffer-desc "remember" ())
  2369. (declare-function remember-finalize "remember" ())
  2370. (defvar remember-save-after-remembering)
  2371. (defvar remember-data-file)
  2372. (defvar remember-register)
  2373. (defvar remember-buffer)
  2374. (defvar remember-handler-functions)
  2375. (defvar remember-annotation-functions)
  2376. (defvar texmathp-why)
  2377. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2378. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2379. (defvar w3m-current-url)
  2380. (defvar w3m-current-title)
  2381. (defvar org-latex-regexps)
  2382. ;;; Autoload and prepare some org modules
  2383. ;; Some table stuff that needs to be defined here, because it is used
  2384. ;; by the functions setting up org-mode or checking for table context.
  2385. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2386. "Detects an org-type or table-type table.")
  2387. (defconst org-table-line-regexp "^[ \t]*|"
  2388. "Detects an org-type table line.")
  2389. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2390. "Detects an org-type table line.")
  2391. (defconst org-table-hline-regexp "^[ \t]*|-"
  2392. "Detects an org-type table hline.")
  2393. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2394. "Detects a table-type table hline.")
  2395. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2396. "Searching from within a table (any type) this finds the first line
  2397. outside the table.")
  2398. ;; Autoload the functions in org-table.el that are needed by functions here.
  2399. (eval-and-compile
  2400. (org-autoload "org-table"
  2401. '(org-table-align org-table-begin org-table-blank-field
  2402. org-table-convert org-table-convert-region org-table-copy-down
  2403. org-table-copy-region org-table-create
  2404. org-table-create-or-convert-from-region
  2405. org-table-create-with-table.el org-table-current-dline
  2406. org-table-cut-region org-table-delete-column org-table-edit-field
  2407. org-table-edit-formulas org-table-end org-table-eval-formula
  2408. org-table-export org-table-field-info
  2409. org-table-get-stored-formulas org-table-goto-column
  2410. org-table-hline-and-move org-table-import org-table-insert-column
  2411. org-table-insert-hline org-table-insert-row org-table-iterate
  2412. org-table-justify-field-maybe org-table-kill-row
  2413. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2414. org-table-move-column org-table-move-column-left
  2415. org-table-move-column-right org-table-move-row
  2416. org-table-move-row-down org-table-move-row-up
  2417. org-table-next-field org-table-next-row org-table-paste-rectangle
  2418. org-table-previous-field org-table-recalculate
  2419. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2420. org-table-toggle-coordinate-overlays
  2421. org-table-toggle-formula-debugger org-table-wrap-region
  2422. orgtbl-mode turn-on-orgtbl)))
  2423. (defun org-at-table-p (&optional table-type)
  2424. "Return t if the cursor is inside an org-type table.
  2425. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2426. (if org-enable-table-editor
  2427. (save-excursion
  2428. (beginning-of-line 1)
  2429. (looking-at (if table-type org-table-any-line-regexp
  2430. org-table-line-regexp)))
  2431. nil))
  2432. (defsubst org-table-p () (org-at-table-p))
  2433. (defun org-at-table.el-p ()
  2434. "Return t if and only if we are at a table.el table."
  2435. (and (org-at-table-p 'any)
  2436. (save-excursion
  2437. (goto-char (org-table-begin 'any))
  2438. (looking-at org-table1-hline-regexp))))
  2439. (defun org-table-recognize-table.el ()
  2440. "If there is a table.el table nearby, recognize it and move into it."
  2441. (if org-table-tab-recognizes-table.el
  2442. (if (org-at-table.el-p)
  2443. (progn
  2444. (beginning-of-line 1)
  2445. (if (looking-at org-table-dataline-regexp)
  2446. nil
  2447. (if (looking-at org-table1-hline-regexp)
  2448. (progn
  2449. (beginning-of-line 2)
  2450. (if (looking-at org-table-any-border-regexp)
  2451. (beginning-of-line -1)))))
  2452. (if (re-search-forward "|" (org-table-end t) t)
  2453. (progn
  2454. (require 'table)
  2455. (if (table--at-cell-p (point))
  2456. t
  2457. (message "recognizing table.el table...")
  2458. (table-recognize-table)
  2459. (message "recognizing table.el table...done")))
  2460. (error "This should not happen..."))
  2461. t)
  2462. nil)
  2463. nil))
  2464. (defun org-at-table-hline-p ()
  2465. "Return t if the cursor is inside a hline in a table."
  2466. (if org-enable-table-editor
  2467. (save-excursion
  2468. (beginning-of-line 1)
  2469. (looking-at org-table-hline-regexp))
  2470. nil))
  2471. (defvar org-table-clean-did-remove-column nil)
  2472. (defun org-table-map-tables (function)
  2473. "Apply FUNCTION to the start of all tables in the buffer."
  2474. (save-excursion
  2475. (save-restriction
  2476. (widen)
  2477. (goto-char (point-min))
  2478. (while (re-search-forward org-table-any-line-regexp nil t)
  2479. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2480. (beginning-of-line 1)
  2481. (if (looking-at org-table-line-regexp)
  2482. (save-excursion (funcall function)))
  2483. (re-search-forward org-table-any-border-regexp nil 1))))
  2484. (message "Mapping tables: done"))
  2485. ;; Declare and autoload functions from org-exp.el
  2486. (declare-function org-default-export-plist "org-exp")
  2487. (declare-function org-infile-export-plist "org-exp")
  2488. (declare-function org-get-current-options "org-exp")
  2489. (eval-and-compile
  2490. (org-autoload "org-exp"
  2491. '(org-export org-export-as-ascii org-export-visible
  2492. org-insert-export-options-template org-export-as-html-and-open
  2493. org-export-as-html-batch org-export-as-html-to-buffer
  2494. org-replace-region-by-html org-export-region-as-html
  2495. org-export-as-html org-export-icalendar-this-file
  2496. org-export-icalendar-all-agenda-files
  2497. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2498. ;; Autoload org-remember
  2499. (eval-and-compile
  2500. (org-autoload "org-remember"
  2501. '(org-remember-insinuate org-remember-annotation
  2502. org-remember-apply-template org-remember org-remember-handler)))
  2503. ;;; Variables for pre-computed regular expressions, all buffer local
  2504. (defvar org-drawer-regexp nil
  2505. "Matches first line of a hidden block.")
  2506. (make-variable-buffer-local 'org-drawer-regexp)
  2507. (defvar org-todo-regexp nil
  2508. "Matches any of the TODO state keywords.")
  2509. (make-variable-buffer-local 'org-todo-regexp)
  2510. (defvar org-not-done-regexp nil
  2511. "Matches any of the TODO state keywords except the last one.")
  2512. (make-variable-buffer-local 'org-not-done-regexp)
  2513. (defvar org-todo-line-regexp nil
  2514. "Matches a headline and puts TODO state into group 2 if present.")
  2515. (make-variable-buffer-local 'org-todo-line-regexp)
  2516. (defvar org-complex-heading-regexp nil
  2517. "Matches a headline and puts everything into groups:
  2518. group 1: the stars
  2519. group 2: The todo keyword, maybe
  2520. group 3: Priority cookie
  2521. group 4: True headline
  2522. group 5: Tags")
  2523. (make-variable-buffer-local 'org-complex-heading-regexp)
  2524. (defvar org-todo-line-tags-regexp nil
  2525. "Matches a headline and puts TODO state into group 2 if present.
  2526. Also put tags into group 4 if tags are present.")
  2527. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2528. (defvar org-nl-done-regexp nil
  2529. "Matches newline followed by a headline with the DONE keyword.")
  2530. (make-variable-buffer-local 'org-nl-done-regexp)
  2531. (defvar org-looking-at-done-regexp nil
  2532. "Matches the DONE keyword a point.")
  2533. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2534. (defvar org-ds-keyword-length 12
  2535. "Maximum length of the Deadline and SCHEDULED keywords.")
  2536. (make-variable-buffer-local 'org-ds-keyword-length)
  2537. (defvar org-deadline-regexp nil
  2538. "Matches the DEADLINE keyword.")
  2539. (make-variable-buffer-local 'org-deadline-regexp)
  2540. (defvar org-deadline-time-regexp nil
  2541. "Matches the DEADLINE keyword together with a time stamp.")
  2542. (make-variable-buffer-local 'org-deadline-time-regexp)
  2543. (defvar org-deadline-line-regexp nil
  2544. "Matches the DEADLINE keyword and the rest of the line.")
  2545. (make-variable-buffer-local 'org-deadline-line-regexp)
  2546. (defvar org-scheduled-regexp nil
  2547. "Matches the SCHEDULED keyword.")
  2548. (make-variable-buffer-local 'org-scheduled-regexp)
  2549. (defvar org-scheduled-time-regexp nil
  2550. "Matches the SCHEDULED keyword together with a time stamp.")
  2551. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2552. (defvar org-closed-time-regexp nil
  2553. "Matches the CLOSED keyword together with a time stamp.")
  2554. (make-variable-buffer-local 'org-closed-time-regexp)
  2555. (defvar org-keyword-time-regexp nil
  2556. "Matches any of the 4 keywords, together with the time stamp.")
  2557. (make-variable-buffer-local 'org-keyword-time-regexp)
  2558. (defvar org-keyword-time-not-clock-regexp nil
  2559. "Matches any of the 3 keywords, together with the time stamp.")
  2560. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2561. (defvar org-maybe-keyword-time-regexp nil
  2562. "Matches a timestamp, possibly preceeded by a keyword.")
  2563. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2564. (defvar org-planning-or-clock-line-re nil
  2565. "Matches a line with planning or clock info.")
  2566. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2567. (defconst org-plain-time-of-day-regexp
  2568. (concat
  2569. "\\(\\<[012]?[0-9]"
  2570. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2571. "\\(--?"
  2572. "\\(\\<[012]?[0-9]"
  2573. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2574. "\\)?")
  2575. "Regular expression to match a plain time or time range.
  2576. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2577. groups carry important information:
  2578. 0 the full match
  2579. 1 the first time, range or not
  2580. 8 the second time, if it is a range.")
  2581. (defconst org-plain-time-extension-regexp
  2582. (concat
  2583. "\\(\\<[012]?[0-9]"
  2584. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2585. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2586. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2587. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2588. groups carry important information:
  2589. 0 the full match
  2590. 7 hours of duration
  2591. 9 minutes of duration")
  2592. (defconst org-stamp-time-of-day-regexp
  2593. (concat
  2594. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2595. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2596. "\\(--?"
  2597. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2598. "Regular expression to match a timestamp time or time range.
  2599. After a match, the following groups carry important information:
  2600. 0 the full match
  2601. 1 date plus weekday, for backreferencing to make sure both times on same day
  2602. 2 the first time, range or not
  2603. 4 the second time, if it is a range.")
  2604. (defconst org-startup-options
  2605. '(("fold" org-startup-folded t)
  2606. ("overview" org-startup-folded t)
  2607. ("nofold" org-startup-folded nil)
  2608. ("showall" org-startup-folded nil)
  2609. ("content" org-startup-folded content)
  2610. ("hidestars" org-hide-leading-stars t)
  2611. ("showstars" org-hide-leading-stars nil)
  2612. ("odd" org-odd-levels-only t)
  2613. ("oddeven" org-odd-levels-only nil)
  2614. ("align" org-startup-align-all-tables t)
  2615. ("noalign" org-startup-align-all-tables nil)
  2616. ("customtime" org-display-custom-times t)
  2617. ("logdone" org-log-done time)
  2618. ("lognotedone" org-log-done note)
  2619. ("nologdone" org-log-done nil)
  2620. ("lognoteclock-out" org-log-note-clock-out t)
  2621. ("nolognoteclock-out" org-log-note-clock-out nil)
  2622. ("logrepeat" org-log-repeat state)
  2623. ("lognoterepeat" org-log-repeat note)
  2624. ("nologrepeat" org-log-repeat nil)
  2625. ("constcgs" constants-unit-system cgs)
  2626. ("constSI" constants-unit-system SI))
  2627. "Variable associated with STARTUP options for org-mode.
  2628. Each element is a list of three items: The startup options as written
  2629. in the #+STARTUP line, the corresponding variable, and the value to
  2630. set this variable to if the option is found. An optional forth element PUSH
  2631. means to push this value onto the list in the variable.")
  2632. (defun org-set-regexps-and-options ()
  2633. "Precompute regular expressions for current buffer."
  2634. (when (org-mode-p)
  2635. (org-set-local 'org-todo-kwd-alist nil)
  2636. (org-set-local 'org-todo-key-alist nil)
  2637. (org-set-local 'org-todo-key-trigger nil)
  2638. (org-set-local 'org-todo-keywords-1 nil)
  2639. (org-set-local 'org-done-keywords nil)
  2640. (org-set-local 'org-todo-heads nil)
  2641. (org-set-local 'org-todo-sets nil)
  2642. (org-set-local 'org-todo-log-states nil)
  2643. (let ((re (org-make-options-regexp
  2644. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  2645. "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES"
  2646. "CONSTANTS" "PROPERTY" "DRAWERS")))
  2647. (splitre "[ \t]+")
  2648. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2649. tail sep kws1 prio props drawers)
  2650. (save-excursion
  2651. (save-restriction
  2652. (widen)
  2653. (goto-char (point-min))
  2654. (while (re-search-forward re nil t)
  2655. (setq key (match-string 1) value (org-match-string-no-properties 2))
  2656. (cond
  2657. ((equal key "CATEGORY")
  2658. (if (string-match "[ \t]+$" value)
  2659. (setq value (replace-match "" t t value)))
  2660. (setq cat value))
  2661. ((member key '("SEQ_TODO" "TODO"))
  2662. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2663. ((equal key "TYP_TODO")
  2664. (push (cons 'type (org-split-string value splitre)) kwds))
  2665. ((equal key "TAGS")
  2666. (setq tags (append tags (org-split-string value splitre))))
  2667. ((equal key "COLUMNS")
  2668. (org-set-local 'org-columns-default-format value))
  2669. ((equal key "LINK")
  2670. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2671. (push (cons (match-string 1 value)
  2672. (org-trim (match-string 2 value)))
  2673. links)))
  2674. ((equal key "PRIORITIES")
  2675. (setq prio (org-split-string value " +")))
  2676. ((equal key "PROPERTY")
  2677. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2678. (push (cons (match-string 1 value) (match-string 2 value))
  2679. props)))
  2680. ((equal key "DRAWERS")
  2681. (setq drawers (org-split-string value splitre)))
  2682. ((equal key "CONSTANTS")
  2683. (setq const (append const (org-split-string value splitre))))
  2684. ((equal key "STARTUP")
  2685. (let ((opts (org-split-string value splitre))
  2686. l var val)
  2687. (while (setq l (pop opts))
  2688. (when (setq l (assoc l org-startup-options))
  2689. (setq var (nth 1 l) val (nth 2 l))
  2690. (if (not (nth 3 l))
  2691. (set (make-local-variable var) val)
  2692. (if (not (listp (symbol-value var)))
  2693. (set (make-local-variable var) nil))
  2694. (set (make-local-variable var) (symbol-value var))
  2695. (add-to-list var val))))))
  2696. ((equal key "ARCHIVE")
  2697. (string-match " *$" value)
  2698. (setq arch (replace-match "" t t value))
  2699. (remove-text-properties 0 (length arch)
  2700. '(face t fontified t) arch)))
  2701. )))
  2702. (when cat
  2703. (org-set-local 'org-category (intern cat))
  2704. (push (cons "CATEGORY" cat) props))
  2705. (when prio
  2706. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2707. (setq prio (mapcar 'string-to-char prio))
  2708. (org-set-local 'org-highest-priority (nth 0 prio))
  2709. (org-set-local 'org-lowest-priority (nth 1 prio))
  2710. (org-set-local 'org-default-priority (nth 2 prio)))
  2711. (and props (org-set-local 'org-local-properties (nreverse props)))
  2712. (and drawers (org-set-local 'org-drawers drawers))
  2713. (and arch (org-set-local 'org-archive-location arch))
  2714. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2715. ;; Process the TODO keywords
  2716. (unless kwds
  2717. ;; Use the global values as if they had been given locally.
  2718. (setq kwds (default-value 'org-todo-keywords))
  2719. (if (stringp (car kwds))
  2720. (setq kwds (list (cons org-todo-interpretation
  2721. (default-value 'org-todo-keywords)))))
  2722. (setq kwds (reverse kwds)))
  2723. (setq kwds (nreverse kwds))
  2724. (let (inter kws kw)
  2725. (while (setq kws (pop kwds))
  2726. (setq inter (pop kws) sep (member "|" kws)
  2727. kws0 (delete "|" (copy-sequence kws))
  2728. kwsa nil
  2729. kws1 (mapcar
  2730. (lambda (x)
  2731. ;; 1 2
  2732. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2733. (progn
  2734. (setq kw (match-string 1 x)
  2735. key (and (match-end 2) (match-string 2 x))
  2736. log (org-extract-log-state-settings x))
  2737. (push (cons kw (and key (string-to-char key))) kwsa)
  2738. (and log (push log org-todo-log-states))
  2739. kw)
  2740. (error "Invalid TODO keyword %s" x)))
  2741. kws0)
  2742. kwsa (if kwsa (append '((:startgroup))
  2743. (nreverse kwsa)
  2744. '((:endgroup))))
  2745. hw (car kws1)
  2746. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2747. tail (list inter hw (car dws) (org-last dws)))
  2748. (add-to-list 'org-todo-heads hw 'append)
  2749. (push kws1 org-todo-sets)
  2750. (setq org-done-keywords (append org-done-keywords dws nil))
  2751. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2752. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2753. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2754. (setq org-todo-sets (nreverse org-todo-sets)
  2755. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2756. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2757. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2758. ;; Process the constants
  2759. (when const
  2760. (let (e cst)
  2761. (while (setq e (pop const))
  2762. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2763. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2764. (setq org-table-formula-constants-local cst)))
  2765. ;; Process the tags.
  2766. (when tags
  2767. (let (e tgs)
  2768. (while (setq e (pop tags))
  2769. (cond
  2770. ((equal e "{") (push '(:startgroup) tgs))
  2771. ((equal e "}") (push '(:endgroup) tgs))
  2772. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2773. (push (cons (match-string 1 e)
  2774. (string-to-char (match-string 2 e)))
  2775. tgs))
  2776. (t (push (list e) tgs))))
  2777. (org-set-local 'org-tag-alist nil)
  2778. (while (setq e (pop tgs))
  2779. (or (and (stringp (car e))
  2780. (assoc (car e) org-tag-alist))
  2781. (push e org-tag-alist))))))
  2782. ;; Compute the regular expressions and other local variables
  2783. (if (not org-done-keywords)
  2784. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  2785. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2786. (length org-scheduled-string)))
  2787. org-drawer-regexp
  2788. (concat "^[ \t]*:\\("
  2789. (mapconcat 'regexp-quote org-drawers "\\|")
  2790. "\\):[ \t]*$")
  2791. org-not-done-keywords
  2792. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  2793. org-todo-regexp
  2794. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  2795. "\\|") "\\)\\>")
  2796. org-not-done-regexp
  2797. (concat "\\<\\("
  2798. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  2799. "\\)\\>")
  2800. org-todo-line-regexp
  2801. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2802. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2803. "\\)\\>\\)?[ \t]*\\(.*\\)")
  2804. org-complex-heading-regexp
  2805. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  2806. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2807. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  2808. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  2809. org-nl-done-regexp
  2810. (concat "\n\\*+[ \t]+"
  2811. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  2812. "\\)" "\\>")
  2813. org-todo-line-tags-regexp
  2814. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2815. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2816. (org-re
  2817. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  2818. org-looking-at-done-regexp
  2819. (concat "^" "\\(?:"
  2820. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  2821. "\\>")
  2822. org-deadline-regexp (concat "\\<" org-deadline-string)
  2823. org-deadline-time-regexp
  2824. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2825. org-deadline-line-regexp
  2826. (concat "\\<\\(" org-deadline-string "\\).*")
  2827. org-scheduled-regexp
  2828. (concat "\\<" org-scheduled-string)
  2829. org-scheduled-time-regexp
  2830. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2831. org-closed-time-regexp
  2832. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2833. org-keyword-time-regexp
  2834. (concat "\\<\\(" org-scheduled-string
  2835. "\\|" org-deadline-string
  2836. "\\|" org-closed-string
  2837. "\\|" org-clock-string "\\)"
  2838. " *[[<]\\([^]>]+\\)[]>]")
  2839. org-keyword-time-not-clock-regexp
  2840. (concat "\\<\\(" org-scheduled-string
  2841. "\\|" org-deadline-string
  2842. "\\|" org-closed-string
  2843. "\\)"
  2844. " *[[<]\\([^]>]+\\)[]>]")
  2845. org-maybe-keyword-time-regexp
  2846. (concat "\\(\\<\\(" org-scheduled-string
  2847. "\\|" org-deadline-string
  2848. "\\|" org-closed-string
  2849. "\\|" org-clock-string "\\)\\)?"
  2850. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  2851. org-planning-or-clock-line-re
  2852. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  2853. "\\|" org-deadline-string
  2854. "\\|" org-closed-string "\\|" org-clock-string
  2855. "\\)\\>\\)")
  2856. )
  2857. (org-compute-latex-and-specials-regexp)
  2858. (org-set-font-lock-defaults)))
  2859. (defun org-extract-log-state-settings (x)
  2860. "Extract the log state setting from a TODO keyword string.
  2861. This will extract info from a string like \"WAIT(w@/!)\"."
  2862. (let (kw key log1 log2)
  2863. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  2864. (setq kw (match-string 1 x)
  2865. key (and (match-end 2) (match-string 2 x))
  2866. log1 (and (match-end 3) (match-string 3 x))
  2867. log2 (and (match-end 4) (match-string 4 x)))
  2868. (and (or log1 log2)
  2869. (list kw
  2870. (and log1 (if (equal log1 "!") 'time 'note))
  2871. (and log2 (if (equal log2 "!") 'time 'note)))))))
  2872. (defun org-remove-keyword-keys (list)
  2873. "Remove a pair of parenthesis at the end of each string in LIST."
  2874. (mapcar (lambda (x)
  2875. (if (string-match "(.*)$" x)
  2876. (substring x 0 (match-beginning 0))
  2877. x))
  2878. list))
  2879. ;; FIXME: this could be done much better, using second characters etc.
  2880. (defun org-assign-fast-keys (alist)
  2881. "Assign fast keys to a keyword-key alist.
  2882. Respect keys that are already there."
  2883. (let (new e k c c1 c2 (char ?a))
  2884. (while (setq e (pop alist))
  2885. (cond
  2886. ((equal e '(:startgroup)) (push e new))
  2887. ((equal e '(:endgroup)) (push e new))
  2888. (t
  2889. (setq k (car e) c2 nil)
  2890. (if (cdr e)
  2891. (setq c (cdr e))
  2892. ;; automatically assign a character.
  2893. (setq c1 (string-to-char
  2894. (downcase (substring
  2895. k (if (= (string-to-char k) ?@) 1 0)))))
  2896. (if (or (rassoc c1 new) (rassoc c1 alist))
  2897. (while (or (rassoc char new) (rassoc char alist))
  2898. (setq char (1+ char)))
  2899. (setq c2 c1))
  2900. (setq c (or c2 char)))
  2901. (push (cons k c) new))))
  2902. (nreverse new)))
  2903. ;;; Some variables ujsed in various places
  2904. (defvar org-window-configuration nil
  2905. "Used in various places to store a window configuration.")
  2906. (defvar org-finish-function nil
  2907. "Function to be called when `C-c C-c' is used.
  2908. This is for getting out of special buffers like remember.")
  2909. ;; FIXME: Occasionally check by commenting these, to make sure
  2910. ;; no other functions uses these, forgetting to let-bind them.
  2911. (defvar entry)
  2912. (defvar state)
  2913. (defvar last-state)
  2914. (defvar date)
  2915. (defvar description)
  2916. ;; Defined somewhere in this file, but used before definition.
  2917. (defvar org-html-entities)
  2918. (defvar org-struct-menu)
  2919. (defvar org-org-menu)
  2920. (defvar org-tbl-menu)
  2921. (defvar org-agenda-keymap)
  2922. ;;;; Define the Org-mode
  2923. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  2924. (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."))
  2925. ;; We use a before-change function to check if a table might need
  2926. ;; an update.
  2927. (defvar org-table-may-need-update t
  2928. "Indicates that a table might need an update.
  2929. This variable is set by `org-before-change-function'.
  2930. `org-table-align' sets it back to nil.")
  2931. (defun org-before-change-function (beg end)
  2932. "Every change indicates that a table might need an update."
  2933. (setq org-table-may-need-update t))
  2934. (defvar org-mode-map)
  2935. (defvar org-mode-hook nil)
  2936. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  2937. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  2938. (defvar org-table-buffer-is-an nil)
  2939. (defconst org-outline-regexp "\\*+ ")
  2940. ;;;###autoload
  2941. (define-derived-mode org-mode outline-mode "Org"
  2942. "Outline-based notes management and organizer, alias
  2943. \"Carsten's outline-mode for keeping track of everything.\"
  2944. Org-mode develops organizational tasks around a NOTES file which
  2945. contains information about projects as plain text. Org-mode is
  2946. implemented on top of outline-mode, which is ideal to keep the content
  2947. of large files well structured. It supports ToDo items, deadlines and
  2948. time stamps, which magically appear in the diary listing of the Emacs
  2949. calendar. Tables are easily created with a built-in table editor.
  2950. Plain text URL-like links connect to websites, emails (VM), Usenet
  2951. messages (Gnus), BBDB entries, and any files related to the project.
  2952. For printing and sharing of notes, an Org-mode file (or a part of it)
  2953. can be exported as a structured ASCII or HTML file.
  2954. The following commands are available:
  2955. \\{org-mode-map}"
  2956. ;; Get rid of Outline menus, they are not needed
  2957. ;; Need to do this here because define-derived-mode sets up
  2958. ;; the keymap so late. Still, it is a waste to call this each time
  2959. ;; we switch another buffer into org-mode.
  2960. (if (featurep 'xemacs)
  2961. (when (boundp 'outline-mode-menu-heading)
  2962. ;; Assume this is Greg's port, it used easymenu
  2963. (easy-menu-remove outline-mode-menu-heading)
  2964. (easy-menu-remove outline-mode-menu-show)
  2965. (easy-menu-remove outline-mode-menu-hide))
  2966. (define-key org-mode-map [menu-bar headings] 'undefined)
  2967. (define-key org-mode-map [menu-bar hide] 'undefined)
  2968. (define-key org-mode-map [menu-bar show] 'undefined))
  2969. (org-load-modules-maybe)
  2970. (easy-menu-add org-org-menu)
  2971. (easy-menu-add org-tbl-menu)
  2972. (org-install-agenda-files-menu)
  2973. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  2974. (org-add-to-invisibility-spec '(org-cwidth))
  2975. (when (featurep 'xemacs)
  2976. (org-set-local 'line-move-ignore-invisible t))
  2977. (org-set-local 'outline-regexp org-outline-regexp)
  2978. (org-set-local 'outline-level 'org-outline-level)
  2979. (when (and org-ellipsis
  2980. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  2981. (fboundp 'make-glyph-code))
  2982. (unless org-display-table
  2983. (setq org-display-table (make-display-table)))
  2984. (set-display-table-slot
  2985. org-display-table 4
  2986. (vconcat (mapcar
  2987. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  2988. org-ellipsis)))
  2989. (if (stringp org-ellipsis) org-ellipsis "..."))))
  2990. (setq buffer-display-table org-display-table))
  2991. (org-set-regexps-and-options)
  2992. ;; Calc embedded
  2993. (org-set-local 'calc-embedded-open-mode "# ")
  2994. (modify-syntax-entry ?# "<")
  2995. (modify-syntax-entry ?@ "w")
  2996. (if org-startup-truncated (setq truncate-lines t))
  2997. (org-set-local 'font-lock-unfontify-region-function
  2998. 'org-unfontify-region)
  2999. ;; Activate before-change-function
  3000. (org-set-local 'org-table-may-need-update t)
  3001. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3002. 'local)
  3003. ;; Check for running clock before killing a buffer
  3004. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3005. ;; Paragraphs and auto-filling
  3006. (org-set-autofill-regexps)
  3007. (setq indent-line-function 'org-indent-line-function)
  3008. (org-update-radio-target-regexp)
  3009. ;; Comment characters
  3010. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3011. (org-set-local 'comment-padding " ")
  3012. ;; Align options lines
  3013. (org-set-local
  3014. 'align-mode-rules-list
  3015. '((org-in-buffer-settings
  3016. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3017. (modes . '(org-mode)))))
  3018. ;; Imenu
  3019. (org-set-local 'imenu-create-index-function
  3020. 'org-imenu-get-tree)
  3021. ;; Make isearch reveal context
  3022. (if (or (featurep 'xemacs)
  3023. (not (boundp 'outline-isearch-open-invisible-function)))
  3024. ;; Emacs 21 and XEmacs make use of the hook
  3025. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3026. ;; Emacs 22 deals with this through a special variable
  3027. (org-set-local 'outline-isearch-open-invisible-function
  3028. (lambda (&rest ignore) (org-show-context 'isearch))))
  3029. ;; If empty file that did not turn on org-mode automatically, make it to.
  3030. (if (and org-insert-mode-line-in-empty-file
  3031. (interactive-p)
  3032. (= (point-min) (point-max)))
  3033. (insert "# -*- mode: org -*-\n\n"))
  3034. (unless org-inhibit-startup
  3035. (when org-startup-align-all-tables
  3036. (let ((bmp (buffer-modified-p)))
  3037. (org-table-map-tables 'org-table-align)
  3038. (set-buffer-modified-p bmp)))
  3039. (org-cycle-hide-drawers 'all)
  3040. (cond
  3041. ((eq org-startup-folded t)
  3042. (org-cycle '(4)))
  3043. ((eq org-startup-folded 'content)
  3044. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  3045. (org-cycle '(4)) (org-cycle '(4)))))))
  3046. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3047. (defun org-current-time ()
  3048. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3049. (if (> (car org-time-stamp-rounding-minutes) 1)
  3050. (let ((r (car org-time-stamp-rounding-minutes))
  3051. (time (decode-time)))
  3052. (apply 'encode-time
  3053. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3054. (nthcdr 2 time))))
  3055. (current-time)))
  3056. ;;;; Font-Lock stuff, including the activators
  3057. (defvar org-mouse-map (make-sparse-keymap))
  3058. (org-defkey org-mouse-map
  3059. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3060. (org-defkey org-mouse-map
  3061. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3062. (when org-mouse-1-follows-link
  3063. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3064. (when org-tab-follows-link
  3065. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3066. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3067. (when org-return-follows-link
  3068. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  3069. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  3070. (require 'font-lock)
  3071. (defconst org-non-link-chars "]\t\n\r<>")
  3072. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3073. "shell" "elisp"))
  3074. (defvar org-link-re-with-space nil
  3075. "Matches a link with spaces, optional angular brackets around it.")
  3076. (defvar org-link-re-with-space2 nil
  3077. "Matches a link with spaces, optional angular brackets around it.")
  3078. (defvar org-angle-link-re nil
  3079. "Matches link with angular brackets, spaces are allowed.")
  3080. (defvar org-plain-link-re nil
  3081. "Matches plain link, without spaces.")
  3082. (defvar org-bracket-link-regexp nil
  3083. "Matches a link in double brackets.")
  3084. (defvar org-bracket-link-analytic-regexp nil
  3085. "Regular expression used to analyze links.
  3086. Here is what the match groups contain after a match:
  3087. 1: http:
  3088. 2: http
  3089. 3: path
  3090. 4: [desc]
  3091. 5: desc")
  3092. (defvar org-any-link-re nil
  3093. "Regular expression matching any link.")
  3094. (defun org-make-link-regexps ()
  3095. "Update the link regular expressions.
  3096. This should be called after the variable `org-link-types' has changed."
  3097. (setq org-link-re-with-space
  3098. (concat
  3099. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3100. "\\([^" org-non-link-chars " ]"
  3101. "[^" org-non-link-chars "]*"
  3102. "[^" org-non-link-chars " ]\\)>?")
  3103. org-link-re-with-space2
  3104. (concat
  3105. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3106. "\\([^" org-non-link-chars " ]"
  3107. "[^]\t\n\r]*"
  3108. "[^" org-non-link-chars " ]\\)>?")
  3109. org-angle-link-re
  3110. (concat
  3111. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3112. "\\([^" org-non-link-chars " ]"
  3113. "[^" org-non-link-chars "]*"
  3114. "\\)>")
  3115. org-plain-link-re
  3116. (concat
  3117. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3118. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3119. org-bracket-link-regexp
  3120. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3121. org-bracket-link-analytic-regexp
  3122. (concat
  3123. "\\[\\["
  3124. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3125. "\\([^]]+\\)"
  3126. "\\]"
  3127. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3128. "\\]")
  3129. org-any-link-re
  3130. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3131. org-angle-link-re "\\)\\|\\("
  3132. org-plain-link-re "\\)")))
  3133. (org-make-link-regexps)
  3134. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3135. "Regular expression for fast time stamp matching.")
  3136. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3137. "Regular expression for fast time stamp matching.")
  3138. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3139. "Regular expression matching time strings for analysis.
  3140. This one does not require the space after the date, so it can be used
  3141. on a string that terminates immediately after the date.")
  3142. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3143. "Regular expression matching time strings for analysis.")
  3144. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3145. "Regular expression matching time stamps, with groups.")
  3146. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3147. "Regular expression matching time stamps (also [..]), with groups.")
  3148. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3149. "Regular expression matching a time stamp range.")
  3150. (defconst org-tr-regexp-both
  3151. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3152. "Regular expression matching a time stamp range.")
  3153. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3154. org-ts-regexp "\\)?")
  3155. "Regular expression matching a time stamp or time stamp range.")
  3156. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3157. org-ts-regexp-both "\\)?")
  3158. "Regular expression matching a time stamp or time stamp range.
  3159. The time stamps may be either active or inactive.")
  3160. (defvar org-emph-face nil)
  3161. (defun org-do-emphasis-faces (limit)
  3162. "Run through the buffer and add overlays to links."
  3163. (let (rtn)
  3164. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3165. (if (not (= (char-after (match-beginning 3))
  3166. (char-after (match-beginning 4))))
  3167. (progn
  3168. (setq rtn t)
  3169. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3170. 'face
  3171. (nth 1 (assoc (match-string 3)
  3172. org-emphasis-alist)))
  3173. (add-text-properties (match-beginning 2) (match-end 2)
  3174. '(font-lock-multiline t))
  3175. (when org-hide-emphasis-markers
  3176. (add-text-properties (match-end 4) (match-beginning 5)
  3177. '(invisible org-link))
  3178. (add-text-properties (match-beginning 3) (match-end 3)
  3179. '(invisible org-link)))))
  3180. (backward-char 1))
  3181. rtn))
  3182. (defun org-emphasize (&optional char)
  3183. "Insert or change an emphasis, i.e. a font like bold or italic.
  3184. If there is an active region, change that region to a new emphasis.
  3185. If there is no region, just insert the marker characters and position
  3186. the cursor between them.
  3187. CHAR should be either the marker character, or the first character of the
  3188. HTML tag associated with that emphasis. If CHAR is a space, the means
  3189. to remove the emphasis of the selected region.
  3190. If char is not given (for example in an interactive call) it
  3191. will be prompted for."
  3192. (interactive)
  3193. (let ((eal org-emphasis-alist) e det
  3194. (erc org-emphasis-regexp-components)
  3195. (prompt "")
  3196. (string "") beg end move tag c s)
  3197. (if (org-region-active-p)
  3198. (setq beg (region-beginning) end (region-end)
  3199. string (buffer-substring beg end))
  3200. (setq move t))
  3201. (while (setq e (pop eal))
  3202. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3203. c (aref tag 0))
  3204. (push (cons c (string-to-char (car e))) det)
  3205. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3206. (substring tag 1)))))
  3207. (unless char
  3208. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3209. (setq char (read-char-exclusive)))
  3210. (setq char (or (cdr (assoc char det)) char))
  3211. (if (equal char ?\ )
  3212. (setq s "" move nil)
  3213. (unless (assoc (char-to-string char) org-emphasis-alist)
  3214. (error "No such emphasis marker: \"%c\"" char))
  3215. (setq s (char-to-string char)))
  3216. (while (and (> (length string) 1)
  3217. (equal (substring string 0 1) (substring string -1))
  3218. (assoc (substring string 0 1) org-emphasis-alist))
  3219. (setq string (substring string 1 -1)))
  3220. (setq string (concat s string s))
  3221. (if beg (delete-region beg end))
  3222. (unless (or (bolp)
  3223. (string-match (concat "[" (nth 0 erc) "\n]")
  3224. (char-to-string (char-before (point)))))
  3225. (insert " "))
  3226. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3227. (char-to-string (char-after (point))))
  3228. (insert " ") (backward-char 1))
  3229. (insert string)
  3230. (and move (backward-char 1))))
  3231. (defconst org-nonsticky-props
  3232. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3233. (defun org-activate-plain-links (limit)
  3234. "Run through the buffer and add overlays to links."
  3235. (catch 'exit
  3236. (let (f)
  3237. (while (re-search-forward org-plain-link-re limit t)
  3238. (setq f (get-text-property (match-beginning 0) 'face))
  3239. (if (or (eq f 'org-tag)
  3240. (and (listp f) (memq 'org-tag f)))
  3241. nil
  3242. (add-text-properties (match-beginning 0) (match-end 0)
  3243. (list 'mouse-face 'highlight
  3244. 'rear-nonsticky org-nonsticky-props
  3245. 'keymap org-mouse-map
  3246. ))
  3247. (throw 'exit t))))))
  3248. (defun org-activate-code (limit)
  3249. (if (re-search-forward "^[ \t]*\\(:.*\\)" limit t)
  3250. (unless (get-text-property (match-beginning 1) 'face)
  3251. (remove-text-properties (match-beginning 0) (match-end 0)
  3252. '(display t invisible t intangible t))
  3253. t)))
  3254. (defun org-activate-angle-links (limit)
  3255. "Run through the buffer and add overlays to links."
  3256. (if (re-search-forward org-angle-link-re limit t)
  3257. (progn
  3258. (add-text-properties (match-beginning 0) (match-end 0)
  3259. (list 'mouse-face 'highlight
  3260. 'rear-nonsticky org-nonsticky-props
  3261. 'keymap org-mouse-map
  3262. ))
  3263. t)))
  3264. (defun org-activate-bracket-links (limit)
  3265. "Run through the buffer and add overlays to bracketed links."
  3266. (if (re-search-forward org-bracket-link-regexp limit t)
  3267. (let* ((help (concat "LINK: "
  3268. (org-match-string-no-properties 1)))
  3269. ;; FIXME: above we should remove the escapes.
  3270. ;; but that requires another match, protecting match data,
  3271. ;; a lot of overhead for font-lock.
  3272. (ip (org-maybe-intangible
  3273. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3274. 'keymap org-mouse-map 'mouse-face 'highlight
  3275. 'font-lock-multiline t 'help-echo help)))
  3276. (vp (list 'rear-nonsticky org-nonsticky-props
  3277. 'keymap org-mouse-map 'mouse-face 'highlight
  3278. ' font-lock-multiline t 'help-echo help)))
  3279. ;; We need to remove the invisible property here. Table narrowing
  3280. ;; may have made some of this invisible.
  3281. (remove-text-properties (match-beginning 0) (match-end 0)
  3282. '(invisible nil))
  3283. (if (match-end 3)
  3284. (progn
  3285. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3286. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3287. (add-text-properties (match-end 3) (match-end 0) ip))
  3288. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3289. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3290. (add-text-properties (match-end 1) (match-end 0) ip))
  3291. t)))
  3292. (defun org-activate-dates (limit)
  3293. "Run through the buffer and add overlays to dates."
  3294. (if (re-search-forward org-tsr-regexp-both limit t)
  3295. (progn
  3296. (add-text-properties (match-beginning 0) (match-end 0)
  3297. (list 'mouse-face 'highlight
  3298. 'rear-nonsticky org-nonsticky-props
  3299. 'keymap org-mouse-map))
  3300. (when org-display-custom-times
  3301. (if (match-end 3)
  3302. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3303. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3304. t)))
  3305. (defvar org-target-link-regexp nil
  3306. "Regular expression matching radio targets in plain text.")
  3307. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3308. "Regular expression matching a link target.")
  3309. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3310. "Regular expression matching a radio target.")
  3311. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3312. "Regular expression matching any target.")
  3313. (defun org-activate-target-links (limit)
  3314. "Run through the buffer and add overlays to target matches."
  3315. (when org-target-link-regexp
  3316. (let ((case-fold-search t))
  3317. (if (re-search-forward org-target-link-regexp limit t)
  3318. (progn
  3319. (add-text-properties (match-beginning 0) (match-end 0)
  3320. (list 'mouse-face 'highlight
  3321. 'rear-nonsticky org-nonsticky-props
  3322. 'keymap org-mouse-map
  3323. 'help-echo "Radio target link"
  3324. 'org-linked-text t))
  3325. t)))))
  3326. (defun org-update-radio-target-regexp ()
  3327. "Find all radio targets in this file and update the regular expression."
  3328. (interactive)
  3329. (when (memq 'radio org-activate-links)
  3330. (setq org-target-link-regexp
  3331. (org-make-target-link-regexp (org-all-targets 'radio)))
  3332. (org-restart-font-lock)))
  3333. (defun org-hide-wide-columns (limit)
  3334. (let (s e)
  3335. (setq s (text-property-any (point) (or limit (point-max))
  3336. 'org-cwidth t))
  3337. (when s
  3338. (setq e (next-single-property-change s 'org-cwidth))
  3339. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3340. (goto-char e)
  3341. t)))
  3342. (defvar org-latex-and-specials-regexp nil
  3343. "Regular expression for highlighting export special stuff.")
  3344. (defvar org-match-substring-regexp)
  3345. (defvar org-match-substring-with-braces-regexp)
  3346. (defvar org-export-html-special-string-regexps)
  3347. (defun org-compute-latex-and-specials-regexp ()
  3348. "Compute regular expression for stuff treated specially by exporters."
  3349. (if (not org-highlight-latex-fragments-and-specials)
  3350. (org-set-local 'org-latex-and-specials-regexp nil)
  3351. (require 'org-exp)
  3352. (let*
  3353. ((matchers (plist-get org-format-latex-options :matchers))
  3354. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3355. org-latex-regexps)))
  3356. (options (org-combine-plists (org-default-export-plist)
  3357. (org-infile-export-plist)))
  3358. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3359. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3360. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3361. (org-export-html-expand (plist-get options :expand-quoted-html))
  3362. (org-export-with-special-strings (plist-get options :special-strings))
  3363. (re-sub
  3364. (cond
  3365. ((equal org-export-with-sub-superscripts '{})
  3366. (list org-match-substring-with-braces-regexp))
  3367. (org-export-with-sub-superscripts
  3368. (list org-match-substring-regexp))
  3369. (t nil)))
  3370. (re-latex
  3371. (if org-export-with-LaTeX-fragments
  3372. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3373. (re-macros
  3374. (if org-export-with-TeX-macros
  3375. (list (concat "\\\\"
  3376. (regexp-opt
  3377. (append (mapcar 'car org-html-entities)
  3378. (if (boundp 'org-latex-entities)
  3379. org-latex-entities nil))
  3380. 'words))) ; FIXME
  3381. ))
  3382. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3383. (re-special (if org-export-with-special-strings
  3384. (mapcar (lambda (x) (car x))
  3385. org-export-html-special-string-regexps)))
  3386. (re-rest
  3387. (delq nil
  3388. (list
  3389. (if org-export-html-expand "@<[^>\n]+>")
  3390. ))))
  3391. (org-set-local
  3392. 'org-latex-and-specials-regexp
  3393. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3394. re-rest) "\\|")))))
  3395. (defface org-latex-and-export-specials
  3396. (let ((font (cond ((assq :inherit custom-face-attributes)
  3397. '(:inherit underline))
  3398. (t '(:underline t)))))
  3399. `((((class grayscale) (background light))
  3400. (:foreground "DimGray" ,@font))
  3401. (((class grayscale) (background dark))
  3402. (:foreground "LightGray" ,@font))
  3403. (((class color) (background light))
  3404. (:foreground "SaddleBrown"))
  3405. (((class color) (background dark))
  3406. (:foreground "burlywood"))
  3407. (t (,@font))))
  3408. "Face used to highlight math latex and other special exporter stuff."
  3409. :group 'org-faces)
  3410. (defun org-do-latex-and-special-faces (limit)
  3411. "Run through the buffer and add overlays to links."
  3412. (when org-latex-and-specials-regexp
  3413. (let (rtn d)
  3414. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3415. limit t))
  3416. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3417. 'face))
  3418. '(org-code org-verbatim underline)))
  3419. (progn
  3420. (setq rtn t
  3421. d (cond ((member (char-after (1+ (match-beginning 0)))
  3422. '(?_ ?^)) 1)
  3423. (t 0)))
  3424. (font-lock-prepend-text-property
  3425. (+ d (match-beginning 0)) (match-end 0)
  3426. 'face 'org-latex-and-export-specials)
  3427. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3428. '(font-lock-multiline t)))))
  3429. rtn)))
  3430. (defun org-restart-font-lock ()
  3431. "Restart font-lock-mode, to force refontification."
  3432. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3433. (font-lock-mode -1)
  3434. (font-lock-mode 1)))
  3435. (defun org-all-targets (&optional radio)
  3436. "Return a list of all targets in this file.
  3437. With optional argument RADIO, only find radio targets."
  3438. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3439. rtn)
  3440. (save-excursion
  3441. (goto-char (point-min))
  3442. (while (re-search-forward re nil t)
  3443. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3444. rtn)))
  3445. (defun org-make-target-link-regexp (targets)
  3446. "Make regular expression matching all strings in TARGETS.
  3447. The regular expression finds the targets also if there is a line break
  3448. between words."
  3449. (and targets
  3450. (concat
  3451. "\\<\\("
  3452. (mapconcat
  3453. (lambda (x)
  3454. (while (string-match " +" x)
  3455. (setq x (replace-match "\\s-+" t t x)))
  3456. x)
  3457. targets
  3458. "\\|")
  3459. "\\)\\>")))
  3460. (defun org-activate-tags (limit)
  3461. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3462. (progn
  3463. (add-text-properties (match-beginning 1) (match-end 1)
  3464. (list 'mouse-face 'highlight
  3465. 'rear-nonsticky org-nonsticky-props
  3466. 'keymap org-mouse-map))
  3467. t)))
  3468. (defun org-outline-level ()
  3469. (save-excursion
  3470. (looking-at outline-regexp)
  3471. (if (match-beginning 1)
  3472. (+ (org-get-string-indentation (match-string 1)) 1000)
  3473. (1- (- (match-end 0) (match-beginning 0))))))
  3474. (defvar org-font-lock-keywords nil)
  3475. (defconst org-property-re (org-re "^[ \t]*\\(:\\([[:alnum:]_]+\\):\\)[ \t]*\\(\\S-.*\\)")
  3476. "Regular expression matching a property line.")
  3477. (defun org-set-font-lock-defaults ()
  3478. (let* ((em org-fontify-emphasized-text)
  3479. (lk org-activate-links)
  3480. (org-font-lock-extra-keywords
  3481. (list
  3482. ;; Headlines
  3483. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3484. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3485. ;; Table lines
  3486. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3487. (1 'org-table t))
  3488. ;; Table internals
  3489. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3490. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3491. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3492. ;; Drawers
  3493. (list org-drawer-regexp '(0 'org-special-keyword t))
  3494. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3495. ;; Properties
  3496. (list org-property-re
  3497. '(1 'org-special-keyword t)
  3498. '(3 'org-property-value t))
  3499. (if org-format-transports-properties-p
  3500. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3501. ;; Links
  3502. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3503. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3504. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3505. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3506. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3507. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3508. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3509. '(org-hide-wide-columns (0 nil append))
  3510. ;; TODO lines
  3511. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3512. '(1 (org-get-todo-face 1) t))
  3513. ;; DONE
  3514. (if org-fontify-done-headline
  3515. (list (concat "^[*]+ +\\<\\("
  3516. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3517. "\\)\\(.*\\)")
  3518. '(2 'org-headline-done t))
  3519. nil)
  3520. ;; Priorities
  3521. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3522. ;; Special keywords
  3523. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3524. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3525. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3526. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3527. ;; Emphasis
  3528. (if em
  3529. (if (featurep 'xemacs)
  3530. '(org-do-emphasis-faces (0 nil append))
  3531. '(org-do-emphasis-faces)))
  3532. ;; Checkboxes
  3533. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3534. 2 'bold prepend)
  3535. (if org-provide-checkbox-statistics
  3536. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3537. (0 (org-get-checkbox-statistics-face) t)))
  3538. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3539. '(1 'org-archived prepend))
  3540. ;; Specials
  3541. '(org-do-latex-and-special-faces)
  3542. ;; Code
  3543. '(org-activate-code (1 'org-code t))
  3544. ;; COMMENT
  3545. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3546. "\\|" org-quote-string "\\)\\>")
  3547. '(1 'org-special-keyword t))
  3548. '("^#.*" (0 'font-lock-comment-face t))
  3549. )))
  3550. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3551. ;; Now set the full font-lock-keywords
  3552. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3553. (org-set-local 'font-lock-defaults
  3554. '(org-font-lock-keywords t nil nil backward-paragraph))
  3555. (kill-local-variable 'font-lock-keywords) nil))
  3556. (defvar org-m nil)
  3557. (defvar org-l nil)
  3558. (defvar org-f nil)
  3559. (defun org-get-level-face (n)
  3560. "Get the right face for match N in font-lock matching of healdines."
  3561. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3562. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3563. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3564. (cond
  3565. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3566. ((eq n 2) org-f)
  3567. (t (if org-level-color-stars-only nil org-f))))
  3568. (defun org-get-todo-face (kwd)
  3569. "Get the right face for a TODO keyword KWD.
  3570. If KWD is a number, get the corresponding match group."
  3571. (if (numberp kwd) (setq kwd (match-string kwd)))
  3572. (or (cdr (assoc kwd org-todo-keyword-faces))
  3573. (and (member kwd org-done-keywords) 'org-done)
  3574. 'org-todo))
  3575. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3576. "Remove fontification and activation overlays from links."
  3577. (font-lock-default-unfontify-region beg end)
  3578. (let* ((buffer-undo-list t)
  3579. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3580. (inhibit-modification-hooks t)
  3581. deactivate-mark buffer-file-name buffer-file-truename)
  3582. (remove-text-properties beg end
  3583. '(mouse-face t keymap t org-linked-text t
  3584. invisible t intangible t))))
  3585. ;;;; Visibility cycling, including org-goto and indirect buffer
  3586. ;;; Cycling
  3587. (defvar org-cycle-global-status nil)
  3588. (make-variable-buffer-local 'org-cycle-global-status)
  3589. (defvar org-cycle-subtree-status nil)
  3590. (make-variable-buffer-local 'org-cycle-subtree-status)
  3591. ;;;###autoload
  3592. (defun org-cycle (&optional arg)
  3593. "Visibility cycling for Org-mode.
  3594. - When this function is called with a prefix argument, rotate the entire
  3595. buffer through 3 states (global cycling)
  3596. 1. OVERVIEW: Show only top-level headlines.
  3597. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3598. 3. SHOW ALL: Show everything.
  3599. - When point is at the beginning of a headline, rotate the subtree started
  3600. by this line through 3 different states (local cycling)
  3601. 1. FOLDED: Only the main headline is shown.
  3602. 2. CHILDREN: The main headline and the direct children are shown.
  3603. From this state, you can move to one of the children
  3604. and zoom in further.
  3605. 3. SUBTREE: Show the entire subtree, including body text.
  3606. - When there is a numeric prefix, go up to a heading with level ARG, do
  3607. a `show-subtree' and return to the previous cursor position. If ARG
  3608. is negative, go up that many levels.
  3609. - When point is not at the beginning of a headline, execute
  3610. `indent-relative', like TAB normally does. See the option
  3611. `org-cycle-emulate-tab' for details.
  3612. - Special case: if point is at the beginning of the buffer and there is
  3613. no headline in line 1, this function will act as if called with prefix arg.
  3614. But only if also the variable `org-cycle-global-at-bob' is t."
  3615. (interactive "P")
  3616. (org-load-modules-maybe)
  3617. (let* ((outline-regexp
  3618. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3619. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3620. outline-regexp))
  3621. (bob-special (and org-cycle-global-at-bob (bobp)
  3622. (not (looking-at outline-regexp))))
  3623. (org-cycle-hook
  3624. (if bob-special
  3625. (delq 'org-optimize-window-after-visibility-change
  3626. (copy-sequence org-cycle-hook))
  3627. org-cycle-hook))
  3628. (pos (point)))
  3629. (if (or bob-special (equal arg '(4)))
  3630. ;; special case: use global cycling
  3631. (setq arg t))
  3632. (cond
  3633. ((org-at-table-p 'any)
  3634. ;; Enter the table or move to the next field in the table
  3635. (or (org-table-recognize-table.el)
  3636. (progn
  3637. (if arg (org-table-edit-field t)
  3638. (org-table-justify-field-maybe)
  3639. (call-interactively 'org-table-next-field)))))
  3640. ((eq arg t) ;; Global cycling
  3641. (cond
  3642. ((and (eq last-command this-command)
  3643. (eq org-cycle-global-status 'overview))
  3644. ;; We just created the overview - now do table of contents
  3645. ;; This can be slow in very large buffers, so indicate action
  3646. (message "CONTENTS...")
  3647. (org-content)
  3648. (message "CONTENTS...done")
  3649. (setq org-cycle-global-status 'contents)
  3650. (run-hook-with-args 'org-cycle-hook 'contents))
  3651. ((and (eq last-command this-command)
  3652. (eq org-cycle-global-status 'contents))
  3653. ;; We just showed the table of contents - now show everything
  3654. (show-all)
  3655. (message "SHOW ALL")
  3656. (setq org-cycle-global-status 'all)
  3657. (run-hook-with-args 'org-cycle-hook 'all))
  3658. (t
  3659. ;; Default action: go to overview
  3660. (org-overview)
  3661. (message "OVERVIEW")
  3662. (setq org-cycle-global-status 'overview)
  3663. (run-hook-with-args 'org-cycle-hook 'overview))))
  3664. ((and org-drawers org-drawer-regexp
  3665. (save-excursion
  3666. (beginning-of-line 1)
  3667. (looking-at org-drawer-regexp)))
  3668. ;; Toggle block visibility
  3669. (org-flag-drawer
  3670. (not (get-char-property (match-end 0) 'invisible))))
  3671. ((integerp arg)
  3672. ;; Show-subtree, ARG levels up from here.
  3673. (save-excursion
  3674. (org-back-to-heading)
  3675. (outline-up-heading (if (< arg 0) (- arg)
  3676. (- (funcall outline-level) arg)))
  3677. (org-show-subtree)))
  3678. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3679. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3680. ;; At a heading: rotate between three different views
  3681. (org-back-to-heading)
  3682. (let ((goal-column 0) eoh eol eos)
  3683. ;; First, some boundaries
  3684. (save-excursion
  3685. (org-back-to-heading)
  3686. (save-excursion
  3687. (beginning-of-line 2)
  3688. (while (and (not (eobp)) ;; this is like `next-line'
  3689. (get-char-property (1- (point)) 'invisible))
  3690. (beginning-of-line 2)) (setq eol (point)))
  3691. (outline-end-of-heading) (setq eoh (point))
  3692. (org-end-of-subtree t)
  3693. (unless (eobp)
  3694. (skip-chars-forward " \t\n")
  3695. (beginning-of-line 1) ; in case this is an item
  3696. )
  3697. (setq eos (1- (point))))
  3698. ;; Find out what to do next and set `this-command'
  3699. (cond
  3700. ((= eos eoh)
  3701. ;; Nothing is hidden behind this heading
  3702. (message "EMPTY ENTRY")
  3703. (setq org-cycle-subtree-status nil)
  3704. (save-excursion
  3705. (goto-char eos)
  3706. (outline-next-heading)
  3707. (if (org-invisible-p) (org-flag-heading nil))))
  3708. ((or (>= eol eos)
  3709. (not (string-match "\\S-" (buffer-substring eol eos))))
  3710. ;; Entire subtree is hidden in one line: open it
  3711. (org-show-entry)
  3712. (show-children)
  3713. (message "CHILDREN")
  3714. (save-excursion
  3715. (goto-char eos)
  3716. (outline-next-heading)
  3717. (if (org-invisible-p) (org-flag-heading nil)))
  3718. (setq org-cycle-subtree-status 'children)
  3719. (run-hook-with-args 'org-cycle-hook 'children))
  3720. ((and (eq last-command this-command)
  3721. (eq org-cycle-subtree-status 'children))
  3722. ;; We just showed the children, now show everything.
  3723. (org-show-subtree)
  3724. (message "SUBTREE")
  3725. (setq org-cycle-subtree-status 'subtree)
  3726. (run-hook-with-args 'org-cycle-hook 'subtree))
  3727. (t
  3728. ;; Default action: hide the subtree.
  3729. (hide-subtree)
  3730. (message "FOLDED")
  3731. (setq org-cycle-subtree-status 'folded)
  3732. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3733. ;; TAB emulation
  3734. (buffer-read-only (org-back-to-heading))
  3735. ((org-try-cdlatex-tab))
  3736. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  3737. (or (not (bolp))
  3738. (not (looking-at outline-regexp))))
  3739. (call-interactively (global-key-binding "\t")))
  3740. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3741. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3742. (or (and (eq org-cycle-emulate-tab 'white)
  3743. (= (match-end 0) (point-at-eol)))
  3744. (and (eq org-cycle-emulate-tab 'whitestart)
  3745. (>= (match-end 0) pos))))
  3746. t
  3747. (eq org-cycle-emulate-tab t))
  3748. ; (if (and (looking-at "[ \n\r\t]")
  3749. ; (string-match "^[ \t]*$" (buffer-substring
  3750. ; (point-at-bol) (point))))
  3751. ; (progn
  3752. ; (beginning-of-line 1)
  3753. ; (and (looking-at "[ \t]+") (replace-match ""))))
  3754. (call-interactively (global-key-binding "\t")))
  3755. (t (save-excursion
  3756. (org-back-to-heading)
  3757. (org-cycle))))))
  3758. ;;;###autoload
  3759. (defun org-global-cycle (&optional arg)
  3760. "Cycle the global visibility. For details see `org-cycle'."
  3761. (interactive "P")
  3762. (let ((org-cycle-include-plain-lists
  3763. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  3764. (if (integerp arg)
  3765. (progn
  3766. (show-all)
  3767. (hide-sublevels arg)
  3768. (setq org-cycle-global-status 'contents))
  3769. (org-cycle '(4)))))
  3770. (defun org-overview ()
  3771. "Switch to overview mode, shoing only top-level headlines.
  3772. Really, this shows all headlines with level equal or greater than the level
  3773. of the first headline in the buffer. This is important, because if the
  3774. first headline is not level one, then (hide-sublevels 1) gives confusing
  3775. results."
  3776. (interactive)
  3777. (let ((level (save-excursion
  3778. (goto-char (point-min))
  3779. (if (re-search-forward (concat "^" outline-regexp) nil t)
  3780. (progn
  3781. (goto-char (match-beginning 0))
  3782. (funcall outline-level))))))
  3783. (and level (hide-sublevels level))))
  3784. (defun org-content (&optional arg)
  3785. "Show all headlines in the buffer, like a table of contents.
  3786. With numerical argument N, show content up to level N."
  3787. (interactive "P")
  3788. (save-excursion
  3789. ;; Visit all headings and show their offspring
  3790. (and (integerp arg) (org-overview))
  3791. (goto-char (point-max))
  3792. (catch 'exit
  3793. (while (and (progn (condition-case nil
  3794. (outline-previous-visible-heading 1)
  3795. (error (goto-char (point-min))))
  3796. t)
  3797. (looking-at outline-regexp))
  3798. (if (integerp arg)
  3799. (show-children (1- arg))
  3800. (show-branches))
  3801. (if (bobp) (throw 'exit nil))))))
  3802. (defun org-optimize-window-after-visibility-change (state)
  3803. "Adjust the window after a change in outline visibility.
  3804. This function is the default value of the hook `org-cycle-hook'."
  3805. (when (get-buffer-window (current-buffer))
  3806. (cond
  3807. ; ((eq state 'overview) (org-first-headline-recenter 1))
  3808. ; ((eq state 'overview) (org-beginning-of-line))
  3809. ((eq state 'content) nil)
  3810. ((eq state 'all) nil)
  3811. ((eq state 'folded) nil)
  3812. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  3813. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  3814. (defun org-compact-display-after-subtree-move ()
  3815. (let (beg end)
  3816. (save-excursion
  3817. (if (org-up-heading-safe)
  3818. (progn
  3819. (hide-subtree)
  3820. (show-entry)
  3821. (show-children)
  3822. (org-cycle-show-empty-lines 'children)
  3823. (org-cycle-hide-drawers 'children))
  3824. (org-overview)))))
  3825. (defun org-cycle-show-empty-lines (state)
  3826. "Show empty lines above all visible headlines.
  3827. The region to be covered depends on STATE when called through
  3828. `org-cycle-hook'. Lisp program can use t for STATE to get the
  3829. entire buffer covered. Note that an empty line is only shown if there
  3830. are at least `org-cycle-separator-lines' empty lines before the headeline."
  3831. (when (> org-cycle-separator-lines 0)
  3832. (save-excursion
  3833. (let* ((n org-cycle-separator-lines)
  3834. (re (cond
  3835. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  3836. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  3837. (t (let ((ns (number-to-string (- n 2))))
  3838. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  3839. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  3840. beg end)
  3841. (cond
  3842. ((memq state '(overview contents t))
  3843. (setq beg (point-min) end (point-max)))
  3844. ((memq state '(children folded))
  3845. (setq beg (point) end (progn (org-end-of-subtree t t)
  3846. (beginning-of-line 2)
  3847. (point)))))
  3848. (when beg
  3849. (goto-char beg)
  3850. (while (re-search-forward re end t)
  3851. (if (not (get-char-property (match-end 1) 'invisible))
  3852. (outline-flag-region
  3853. (match-beginning 1) (match-end 1) nil)))))))
  3854. ;; Never hide empty lines at the end of the file.
  3855. (save-excursion
  3856. (goto-char (point-max))
  3857. (outline-previous-heading)
  3858. (outline-end-of-heading)
  3859. (if (and (looking-at "[ \t\n]+")
  3860. (= (match-end 0) (point-max)))
  3861. (outline-flag-region (point) (match-end 0) nil))))
  3862. (defun org-subtree-end-visible-p ()
  3863. "Is the end of the current subtree visible?"
  3864. (pos-visible-in-window-p
  3865. (save-excursion (org-end-of-subtree t) (point))))
  3866. (defun org-first-headline-recenter (&optional N)
  3867. "Move cursor to the first headline and recenter the headline.
  3868. Optional argument N means, put the headline into the Nth line of the window."
  3869. (goto-char (point-min))
  3870. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  3871. (beginning-of-line)
  3872. (recenter (prefix-numeric-value N))))
  3873. ;;; Org-goto
  3874. (defvar org-goto-window-configuration nil)
  3875. (defvar org-goto-marker nil)
  3876. (defvar org-goto-map
  3877. (let ((map (make-sparse-keymap)))
  3878. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  3879. (while (setq cmd (pop cmds))
  3880. (substitute-key-definition cmd cmd map global-map)))
  3881. (suppress-keymap map)
  3882. (org-defkey map "\C-m" 'org-goto-ret)
  3883. (org-defkey map [(return)] 'org-goto-ret)
  3884. (org-defkey map [(left)] 'org-goto-left)
  3885. (org-defkey map [(right)] 'org-goto-right)
  3886. (org-defkey map [(control ?g)] 'org-goto-quit)
  3887. (org-defkey map "\C-i" 'org-cycle)
  3888. (org-defkey map [(tab)] 'org-cycle)
  3889. (org-defkey map [(down)] 'outline-next-visible-heading)
  3890. (org-defkey map [(up)] 'outline-previous-visible-heading)
  3891. (if org-goto-auto-isearch
  3892. (if (fboundp 'define-key-after)
  3893. (define-key-after map [t] 'org-goto-local-auto-isearch)
  3894. nil)
  3895. (org-defkey map "q" 'org-goto-quit)
  3896. (org-defkey map "n" 'outline-next-visible-heading)
  3897. (org-defkey map "p" 'outline-previous-visible-heading)
  3898. (org-defkey map "f" 'outline-forward-same-level)
  3899. (org-defkey map "b" 'outline-backward-same-level)
  3900. (org-defkey map "u" 'outline-up-heading))
  3901. (org-defkey map "/" 'org-occur)
  3902. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  3903. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  3904. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  3905. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  3906. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  3907. map))
  3908. (defconst org-goto-help
  3909. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  3910. RET=jump to location [Q]uit and return to previous location
  3911. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  3912. (defvar org-goto-start-pos) ; dynamically scoped parameter
  3913. (defun org-goto (&optional alternative-interface)
  3914. "Look up a different location in the current file, keeping current visibility.
  3915. When you want look-up or go to a different location in a document, the
  3916. fastest way is often to fold the entire buffer and then dive into the tree.
  3917. This method has the disadvantage, that the previous location will be folded,
  3918. which may not be what you want.
  3919. This command works around this by showing a copy of the current buffer
  3920. in an indirect buffer, in overview mode. You can dive into the tree in
  3921. that copy, use org-occur and incremental search to find a location.
  3922. When pressing RET or `Q', the command returns to the original buffer in
  3923. which the visibility is still unchanged. After RET is will also jump to
  3924. the location selected in the indirect buffer and expose the
  3925. the headline hierarchy above."
  3926. (interactive "P")
  3927. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  3928. (org-refile-use-outline-path t)
  3929. (interface
  3930. (if (not alternative-interface)
  3931. org-goto-interface
  3932. (if (eq org-goto-interface 'outline)
  3933. 'outline-path-completion
  3934. 'outline)))
  3935. (org-goto-start-pos (point))
  3936. (selected-point
  3937. (if (eq interface 'outline)
  3938. (car (org-get-location (current-buffer) org-goto-help))
  3939. (nth 3 (org-refile-get-location "Goto: ")))))
  3940. (if selected-point
  3941. (progn
  3942. (org-mark-ring-push org-goto-start-pos)
  3943. (goto-char selected-point)
  3944. (if (or (org-invisible-p) (org-invisible-p2))
  3945. (org-show-context 'org-goto)))
  3946. (message "Quit"))))
  3947. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  3948. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  3949. (defvar org-goto-local-auto-isearch-map) ; defined below
  3950. (defun org-get-location (buf help)
  3951. "Let the user select a location in the Org-mode buffer BUF.
  3952. This function uses a recursive edit. It returns the selected position
  3953. or nil."
  3954. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  3955. (isearch-hide-immediately nil)
  3956. (isearch-search-fun-function
  3957. (lambda () 'org-goto-local-search-forward-headings))
  3958. (org-goto-selected-point org-goto-exit-command))
  3959. (save-excursion
  3960. (save-window-excursion
  3961. (delete-other-windows)
  3962. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  3963. (switch-to-buffer
  3964. (condition-case nil
  3965. (make-indirect-buffer (current-buffer) "*org-goto*")
  3966. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  3967. (with-output-to-temp-buffer "*Help*"
  3968. (princ help))
  3969. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  3970. (setq buffer-read-only nil)
  3971. (let ((org-startup-truncated t)
  3972. (org-startup-folded nil)
  3973. (org-startup-align-all-tables nil))
  3974. (org-mode)
  3975. (org-overview))
  3976. (setq buffer-read-only t)
  3977. (if (and (boundp 'org-goto-start-pos)
  3978. (integer-or-marker-p org-goto-start-pos))
  3979. (let ((org-show-hierarchy-above t)
  3980. (org-show-siblings t)
  3981. (org-show-following-heading t))
  3982. (goto-char org-goto-start-pos)
  3983. (and (org-invisible-p) (org-show-context)))
  3984. (goto-char (point-min)))
  3985. (org-beginning-of-line)
  3986. (message "Select location and press RET")
  3987. (use-local-map org-goto-map)
  3988. (recursive-edit)
  3989. ))
  3990. (kill-buffer "*org-goto*")
  3991. (cons org-goto-selected-point org-goto-exit-command)))
  3992. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  3993. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  3994. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  3995. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  3996. (defun org-goto-local-search-forward-headings (string bound noerror)
  3997. "Search and make sure that anu matches are in headlines."
  3998. (catch 'return
  3999. (while (search-forward string bound noerror)
  4000. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4001. (and (member :headline context)
  4002. (not (member :tags context))))
  4003. (throw 'return (point))))))
  4004. (defun org-goto-local-auto-isearch ()
  4005. "Start isearch."
  4006. (interactive)
  4007. (goto-char (point-min))
  4008. (let ((keys (this-command-keys)))
  4009. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4010. (isearch-mode t)
  4011. (isearch-process-search-char (string-to-char keys)))))
  4012. (defun org-goto-ret (&optional arg)
  4013. "Finish `org-goto' by going to the new location."
  4014. (interactive "P")
  4015. (setq org-goto-selected-point (point)
  4016. org-goto-exit-command 'return)
  4017. (throw 'exit nil))
  4018. (defun org-goto-left ()
  4019. "Finish `org-goto' by going to the new location."
  4020. (interactive)
  4021. (if (org-on-heading-p)
  4022. (progn
  4023. (beginning-of-line 1)
  4024. (setq org-goto-selected-point (point)
  4025. org-goto-exit-command 'left)
  4026. (throw 'exit nil))
  4027. (error "Not on a heading")))
  4028. (defun org-goto-right ()
  4029. "Finish `org-goto' by going to the new location."
  4030. (interactive)
  4031. (if (org-on-heading-p)
  4032. (progn
  4033. (setq org-goto-selected-point (point)
  4034. org-goto-exit-command 'right)
  4035. (throw 'exit nil))
  4036. (error "Not on a heading")))
  4037. (defun org-goto-quit ()
  4038. "Finish `org-goto' without cursor motion."
  4039. (interactive)
  4040. (setq org-goto-selected-point nil)
  4041. (setq org-goto-exit-command 'quit)
  4042. (throw 'exit nil))
  4043. ;;; Indirect buffer display of subtrees
  4044. (defvar org-indirect-dedicated-frame nil
  4045. "This is the frame being used for indirect tree display.")
  4046. (defvar org-last-indirect-buffer nil)
  4047. (defun org-tree-to-indirect-buffer (&optional arg)
  4048. "Create indirect buffer and narrow it to current subtree.
  4049. With numerical prefix ARG, go up to this level and then take that tree.
  4050. If ARG is negative, go up that many levels.
  4051. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4052. indirect buffer previously made with this command, to avoid proliferation of
  4053. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4054. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4055. is kept so that you can work with several indirect buffers at the same time.
  4056. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4057. requests that a new frame be made for the new buffer, so that the dedicated
  4058. frame is not changed."
  4059. (interactive "P")
  4060. (let ((cbuf (current-buffer))
  4061. (cwin (selected-window))
  4062. (pos (point))
  4063. beg end level heading ibuf)
  4064. (save-excursion
  4065. (org-back-to-heading t)
  4066. (when (numberp arg)
  4067. (setq level (org-outline-level))
  4068. (if (< arg 0) (setq arg (+ level arg)))
  4069. (while (> (setq level (org-outline-level)) arg)
  4070. (outline-up-heading 1 t)))
  4071. (setq beg (point)
  4072. heading (org-get-heading))
  4073. (org-end-of-subtree t) (setq end (point)))
  4074. (if (and (buffer-live-p org-last-indirect-buffer)
  4075. (not (eq org-indirect-buffer-display 'new-frame))
  4076. (not arg))
  4077. (kill-buffer org-last-indirect-buffer))
  4078. (setq ibuf (org-get-indirect-buffer cbuf)
  4079. org-last-indirect-buffer ibuf)
  4080. (cond
  4081. ((or (eq org-indirect-buffer-display 'new-frame)
  4082. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4083. (select-frame (make-frame))
  4084. (delete-other-windows)
  4085. (switch-to-buffer ibuf)
  4086. (org-set-frame-title heading))
  4087. ((eq org-indirect-buffer-display 'dedicated-frame)
  4088. (raise-frame
  4089. (select-frame (or (and org-indirect-dedicated-frame
  4090. (frame-live-p org-indirect-dedicated-frame)
  4091. org-indirect-dedicated-frame)
  4092. (setq org-indirect-dedicated-frame (make-frame)))))
  4093. (delete-other-windows)
  4094. (switch-to-buffer ibuf)
  4095. (org-set-frame-title (concat "Indirect: " heading)))
  4096. ((eq org-indirect-buffer-display 'current-window)
  4097. (switch-to-buffer ibuf))
  4098. ((eq org-indirect-buffer-display 'other-window)
  4099. (pop-to-buffer ibuf))
  4100. (t (error "Invalid value.")))
  4101. (if (featurep 'xemacs)
  4102. (save-excursion (org-mode) (turn-on-font-lock)))
  4103. (narrow-to-region beg end)
  4104. (show-all)
  4105. (goto-char pos)
  4106. (and (window-live-p cwin) (select-window cwin))))
  4107. (defun org-get-indirect-buffer (&optional buffer)
  4108. (setq buffer (or buffer (current-buffer)))
  4109. (let ((n 1) (base (buffer-name buffer)) bname)
  4110. (while (buffer-live-p
  4111. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4112. (setq n (1+ n)))
  4113. (condition-case nil
  4114. (make-indirect-buffer buffer bname 'clone)
  4115. (error (make-indirect-buffer buffer bname)))))
  4116. (defun org-set-frame-title (title)
  4117. "Set the title of the current frame to the string TITLE."
  4118. ;; FIXME: how to name a single frame in XEmacs???
  4119. (unless (featurep 'xemacs)
  4120. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4121. ;;;; Structure editing
  4122. ;;; Inserting headlines
  4123. (defun org-insert-heading (&optional force-heading)
  4124. "Insert a new heading or item with same depth at point.
  4125. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4126. If point is at the beginning of a headline, insert a sibling before the
  4127. current headline. If point is not at the beginning, do not split the line,
  4128. but create the new hedline after the current line."
  4129. (interactive "P")
  4130. (if (= (buffer-size) 0)
  4131. (insert "\n* ")
  4132. (when (or force-heading (not (org-insert-item)))
  4133. (let* ((head (save-excursion
  4134. (condition-case nil
  4135. (progn
  4136. (org-back-to-heading)
  4137. (match-string 0))
  4138. (error "*"))))
  4139. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  4140. pos)
  4141. (cond
  4142. ((and (org-on-heading-p) (bolp)
  4143. (or (bobp)
  4144. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4145. ;; insert before the current line
  4146. (open-line (if blank 2 1)))
  4147. ((and (bolp)
  4148. (or (bobp)
  4149. (save-excursion
  4150. (backward-char 1) (not (org-invisible-p)))))
  4151. ;; insert right here
  4152. nil)
  4153. (t
  4154. ; ;; in the middle of the line
  4155. ; (org-show-entry)
  4156. ; (if (org-get-alist-option org-M-RET-may-split-line 'headline)
  4157. ; (if (and
  4158. ; (org-on-heading-p)
  4159. ; (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \r\n]"))
  4160. ; ;; protect the tags
  4161. ;; (let ((tags (match-string 2)) pos)
  4162. ; (delete-region (match-beginning 1) (match-end 1))
  4163. ; (setq pos (point-at-bol))
  4164. ; (newline (if blank 2 1))
  4165. ; (save-excursion
  4166. ; (goto-char pos)
  4167. ; (end-of-line 1)
  4168. ; (insert " " tags)
  4169. ; (org-set-tags nil 'align)))
  4170. ; (newline (if blank 2 1)))
  4171. ; (newline (if blank 2 1))))
  4172. ;; in the middle of the line
  4173. (org-show-entry)
  4174. (let ((split
  4175. (org-get-alist-option org-M-RET-may-split-line 'headline))
  4176. tags pos)
  4177. (if (org-on-heading-p)
  4178. (progn
  4179. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4180. (setq tags (and (match-end 2) (match-string 2)))
  4181. (and (match-end 1)
  4182. (delete-region (match-beginning 1) (match-end 1)))
  4183. (setq pos (point-at-bol))
  4184. (or split (end-of-line 1))
  4185. (delete-horizontal-space)
  4186. (newline (if blank 2 1))
  4187. (when tags
  4188. (save-excursion
  4189. (goto-char pos)
  4190. (end-of-line 1)
  4191. (insert " " tags)
  4192. (org-set-tags nil 'align))))
  4193. (or split (end-of-line 1))
  4194. (newline (if blank 2 1))))))
  4195. (insert head) (just-one-space)
  4196. (setq pos (point))
  4197. (end-of-line 1)
  4198. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4199. (run-hooks 'org-insert-heading-hook)))))
  4200. (defun org-insert-heading-after-current ()
  4201. "Insert a new heading with same level as current, after current subtree."
  4202. (interactive)
  4203. (org-back-to-heading)
  4204. (org-insert-heading)
  4205. (org-move-subtree-down)
  4206. (end-of-line 1))
  4207. (defun org-insert-todo-heading (arg)
  4208. "Insert a new heading with the same level and TODO state as current heading.
  4209. If the heading has no TODO state, or if the state is DONE, use the first
  4210. state (TODO by default). Also with prefix arg, force first state."
  4211. (interactive "P")
  4212. (when (not (org-insert-item 'checkbox))
  4213. (org-insert-heading)
  4214. (save-excursion
  4215. (org-back-to-heading)
  4216. (outline-previous-heading)
  4217. (looking-at org-todo-line-regexp))
  4218. (if (or arg
  4219. (not (match-beginning 2))
  4220. (member (match-string 2) org-done-keywords))
  4221. (insert (car org-todo-keywords-1) " ")
  4222. (insert (match-string 2) " "))))
  4223. (defun org-insert-subheading (arg)
  4224. "Insert a new subheading and demote it.
  4225. Works for outline headings and for plain lists alike."
  4226. (interactive "P")
  4227. (org-insert-heading arg)
  4228. (cond
  4229. ((org-on-heading-p) (org-do-demote))
  4230. ((org-at-item-p) (org-indent-item 1))))
  4231. (defun org-insert-todo-subheading (arg)
  4232. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4233. Works for outline headings and for plain lists alike."
  4234. (interactive "P")
  4235. (org-insert-todo-heading arg)
  4236. (cond
  4237. ((org-on-heading-p) (org-do-demote))
  4238. ((org-at-item-p) (org-indent-item 1))))
  4239. ;;; Promotion and Demotion
  4240. (defun org-promote-subtree ()
  4241. "Promote the entire subtree.
  4242. See also `org-promote'."
  4243. (interactive)
  4244. (save-excursion
  4245. (org-map-tree 'org-promote))
  4246. (org-fix-position-after-promote))
  4247. (defun org-demote-subtree ()
  4248. "Demote the entire subtree. See `org-demote'.
  4249. See also `org-promote'."
  4250. (interactive)
  4251. (save-excursion
  4252. (org-map-tree 'org-demote))
  4253. (org-fix-position-after-promote))
  4254. (defun org-do-promote ()
  4255. "Promote the current heading higher up the tree.
  4256. If the region is active in `transient-mark-mode', promote all headings
  4257. in the region."
  4258. (interactive)
  4259. (save-excursion
  4260. (if (org-region-active-p)
  4261. (org-map-region 'org-promote (region-beginning) (region-end))
  4262. (org-promote)))
  4263. (org-fix-position-after-promote))
  4264. (defun org-do-demote ()
  4265. "Demote the current heading lower down the tree.
  4266. If the region is active in `transient-mark-mode', demote all headings
  4267. in the region."
  4268. (interactive)
  4269. (save-excursion
  4270. (if (org-region-active-p)
  4271. (org-map-region 'org-demote (region-beginning) (region-end))
  4272. (org-demote)))
  4273. (org-fix-position-after-promote))
  4274. (defun org-fix-position-after-promote ()
  4275. "Make sure that after pro/demotion cursor position is right."
  4276. (let ((pos (point)))
  4277. (when (save-excursion
  4278. (beginning-of-line 1)
  4279. (looking-at org-todo-line-regexp)
  4280. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4281. (cond ((eobp) (insert " "))
  4282. ((eolp) (insert " "))
  4283. ((equal (char-after) ?\ ) (forward-char 1))))))
  4284. (defun org-reduced-level (l)
  4285. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4286. (defun org-get-valid-level (level &optional change)
  4287. "Rectify a level change under the influence of `org-odd-levels-only'
  4288. LEVEL is a current level, CHANGE is by how much the level should be
  4289. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4290. even level numbers will become the next higher odd number."
  4291. (if org-odd-levels-only
  4292. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4293. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4294. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4295. (max 1 (+ level change))))
  4296. (if (featurep 'xemacs)
  4297. (define-obsolete-function-alias 'org-get-legal-level
  4298. 'org-get-valid-level)
  4299. (define-obsolete-function-alias 'org-get-legal-level
  4300. 'org-get-valid-level "23.1"))
  4301. (defun org-promote ()
  4302. "Promote the current heading higher up the tree.
  4303. If the region is active in `transient-mark-mode', promote all headings
  4304. in the region."
  4305. (org-back-to-heading t)
  4306. (let* ((level (save-match-data (funcall outline-level)))
  4307. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4308. (diff (abs (- level (length up-head) -1))))
  4309. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4310. (replace-match up-head nil t)
  4311. ;; Fixup tag positioning
  4312. (and org-auto-align-tags (org-set-tags nil t))
  4313. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4314. (defun org-demote ()
  4315. "Demote the current heading lower down the tree.
  4316. If the region is active in `transient-mark-mode', demote all headings
  4317. in the region."
  4318. (org-back-to-heading t)
  4319. (let* ((level (save-match-data (funcall outline-level)))
  4320. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4321. (diff (abs (- level (length down-head) -1))))
  4322. (replace-match down-head nil t)
  4323. ;; Fixup tag positioning
  4324. (and org-auto-align-tags (org-set-tags nil t))
  4325. (if org-adapt-indentation (org-fixup-indentation diff))))
  4326. (defun org-map-tree (fun)
  4327. "Call FUN for every heading underneath the current one."
  4328. (org-back-to-heading)
  4329. (let ((level (funcall outline-level)))
  4330. (save-excursion
  4331. (funcall fun)
  4332. (while (and (progn
  4333. (outline-next-heading)
  4334. (> (funcall outline-level) level))
  4335. (not (eobp)))
  4336. (funcall fun)))))
  4337. (defun org-map-region (fun beg end)
  4338. "Call FUN for every heading between BEG and END."
  4339. (let ((org-ignore-region t))
  4340. (save-excursion
  4341. (setq end (copy-marker end))
  4342. (goto-char beg)
  4343. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4344. (< (point) end))
  4345. (funcall fun))
  4346. (while (and (progn
  4347. (outline-next-heading)
  4348. (< (point) end))
  4349. (not (eobp)))
  4350. (funcall fun)))))
  4351. (defun org-fixup-indentation (diff)
  4352. "Change the indentation in the current entry by DIFF
  4353. However, if any line in the current entry has no indentation, or if it
  4354. would end up with no indentation after the change, nothing at all is done."
  4355. (save-excursion
  4356. (let ((end (save-excursion (outline-next-heading)
  4357. (point-marker)))
  4358. (prohibit (if (> diff 0)
  4359. "^\\S-"
  4360. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4361. col)
  4362. (unless (save-excursion (end-of-line 1)
  4363. (re-search-forward prohibit end t))
  4364. (while (and (< (point) end)
  4365. (re-search-forward "^[ \t]+" end t))
  4366. (goto-char (match-end 0))
  4367. (setq col (current-column))
  4368. (if (< diff 0) (replace-match ""))
  4369. (indent-to (+ diff col))))
  4370. (move-marker end nil))))
  4371. (defun org-convert-to-odd-levels ()
  4372. "Convert an org-mode file with all levels allowed to one with odd levels.
  4373. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4374. level 5 etc."
  4375. (interactive)
  4376. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4377. (let ((org-odd-levels-only nil) n)
  4378. (save-excursion
  4379. (goto-char (point-min))
  4380. (while (re-search-forward "^\\*\\*+ " nil t)
  4381. (setq n (- (length (match-string 0)) 2))
  4382. (while (>= (setq n (1- n)) 0)
  4383. (org-demote))
  4384. (end-of-line 1))))))
  4385. (defun org-convert-to-oddeven-levels ()
  4386. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4387. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4388. section with an even level, conversion would destroy the structure of the file. An error
  4389. is signaled in this case."
  4390. (interactive)
  4391. (goto-char (point-min))
  4392. ;; First check if there are no even levels
  4393. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4394. (org-show-context t)
  4395. (error "Not all levels are odd in this file. Conversion not possible."))
  4396. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4397. (let ((org-odd-levels-only nil) n)
  4398. (save-excursion
  4399. (goto-char (point-min))
  4400. (while (re-search-forward "^\\*\\*+ " nil t)
  4401. (setq n (/ (1- (length (match-string 0))) 2))
  4402. (while (>= (setq n (1- n)) 0)
  4403. (org-promote))
  4404. (end-of-line 1))))))
  4405. (defun org-tr-level (n)
  4406. "Make N odd if required."
  4407. (if org-odd-levels-only (1+ (/ n 2)) n))
  4408. ;;; Vertical tree motion, cutting and pasting of subtrees
  4409. (defun org-move-subtree-up (&optional arg)
  4410. "Move the current subtree up past ARG headlines of the same level."
  4411. (interactive "p")
  4412. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4413. (defun org-move-subtree-down (&optional arg)
  4414. "Move the current subtree down past ARG headlines of the same level."
  4415. (interactive "p")
  4416. (setq arg (prefix-numeric-value arg))
  4417. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4418. 'outline-get-last-sibling))
  4419. (ins-point (make-marker))
  4420. (cnt (abs arg))
  4421. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4422. ;; Select the tree
  4423. (org-back-to-heading)
  4424. (setq beg0 (point))
  4425. (save-excursion
  4426. (setq ne-beg (org-back-over-empty-lines))
  4427. (setq beg (point)))
  4428. (save-match-data
  4429. (save-excursion (outline-end-of-heading)
  4430. (setq folded (org-invisible-p)))
  4431. (outline-end-of-subtree))
  4432. (outline-next-heading)
  4433. (setq ne-end (org-back-over-empty-lines))
  4434. (setq end (point))
  4435. (goto-char beg0)
  4436. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4437. ;; include less whitespace
  4438. (save-excursion
  4439. (goto-char beg)
  4440. (forward-line (- ne-beg ne-end))
  4441. (setq beg (point))))
  4442. ;; Find insertion point, with error handling
  4443. (while (> cnt 0)
  4444. (or (and (funcall movfunc) (looking-at outline-regexp))
  4445. (progn (goto-char beg0)
  4446. (error "Cannot move past superior level or buffer limit")))
  4447. (setq cnt (1- cnt)))
  4448. (if (> arg 0)
  4449. ;; Moving forward - still need to move over subtree
  4450. (progn (org-end-of-subtree t t)
  4451. (save-excursion
  4452. (org-back-over-empty-lines)
  4453. (or (bolp) (newline)))))
  4454. (setq ne-ins (org-back-over-empty-lines))
  4455. (move-marker ins-point (point))
  4456. (setq txt (buffer-substring beg end))
  4457. (delete-region beg end)
  4458. (outline-flag-region (1- beg) beg nil)
  4459. (outline-flag-region (1- (point)) (point) nil)
  4460. (insert txt)
  4461. (or (bolp) (insert "\n"))
  4462. (setq ins-end (point))
  4463. (goto-char ins-point)
  4464. (org-skip-whitespace)
  4465. (when (and (< arg 0)
  4466. (org-first-sibling-p)
  4467. (> ne-ins ne-beg))
  4468. ;; Move whitespace back to beginning
  4469. (save-excursion
  4470. (goto-char ins-end)
  4471. (let ((kill-whole-line t))
  4472. (kill-line (- ne-ins ne-beg)) (point)))
  4473. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4474. (move-marker ins-point nil)
  4475. (org-compact-display-after-subtree-move)
  4476. (unless folded
  4477. (org-show-entry)
  4478. (show-children)
  4479. (org-cycle-hide-drawers 'children))))
  4480. (defvar org-subtree-clip ""
  4481. "Clipboard for cut and paste of subtrees.
  4482. This is actually only a copy of the kill, because we use the normal kill
  4483. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4484. (defvar org-subtree-clip-folded nil
  4485. "Was the last copied subtree folded?
  4486. This is used to fold the tree back after pasting.")
  4487. (defun org-cut-subtree (&optional n)
  4488. "Cut the current subtree into the clipboard.
  4489. With prefix arg N, cut this many sequential subtrees.
  4490. This is a short-hand for marking the subtree and then cutting it."
  4491. (interactive "p")
  4492. (org-copy-subtree n 'cut))
  4493. (defun org-copy-subtree (&optional n cut)
  4494. "Cut the current subtree into the clipboard.
  4495. With prefix arg N, cut this many sequential subtrees.
  4496. This is a short-hand for marking the subtree and then copying it.
  4497. If CUT is non-nil, actually cut the subtree."
  4498. (interactive "p")
  4499. (let (beg end folded (beg0 (point)))
  4500. (if (interactive-p)
  4501. (org-back-to-heading nil) ; take what looks like a subtree
  4502. (org-back-to-heading t)) ; take what is really there
  4503. (org-back-over-empty-lines)
  4504. (setq beg (point))
  4505. (skip-chars-forward " \t\r\n")
  4506. (save-match-data
  4507. (save-excursion (outline-end-of-heading)
  4508. (setq folded (org-invisible-p)))
  4509. (condition-case nil
  4510. (outline-forward-same-level (1- n))
  4511. (error nil))
  4512. (org-end-of-subtree t t))
  4513. (org-back-over-empty-lines)
  4514. (setq end (point))
  4515. (goto-char beg0)
  4516. (when (> end beg)
  4517. (setq org-subtree-clip-folded folded)
  4518. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4519. (setq org-subtree-clip (current-kill 0))
  4520. (message "%s: Subtree(s) with %d characters"
  4521. (if cut "Cut" "Copied")
  4522. (length org-subtree-clip)))))
  4523. (defun org-paste-subtree (&optional level tree)
  4524. "Paste the clipboard as a subtree, with modification of headline level.
  4525. The entire subtree is promoted or demoted in order to match a new headline
  4526. level. By default, the new level is derived from the visible headings
  4527. before and after the insertion point, and taken to be the inferior headline
  4528. level of the two. So if the previous visible heading is level 3 and the
  4529. next is level 4 (or vice versa), level 4 will be used for insertion.
  4530. This makes sure that the subtree remains an independent subtree and does
  4531. not swallow low level entries.
  4532. You can also force a different level, either by using a numeric prefix
  4533. argument, or by inserting the heading marker by hand. For example, if the
  4534. cursor is after \"*****\", then the tree will be shifted to level 5.
  4535. If you want to insert the tree as is, just use \\[yank].
  4536. If optional TREE is given, use this text instead of the kill ring."
  4537. (interactive "P")
  4538. (unless (org-kill-is-subtree-p tree)
  4539. (error "%s"
  4540. (substitute-command-keys
  4541. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4542. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  4543. (^re (concat "^\\(" outline-regexp "\\)"))
  4544. (re (concat "\\(" outline-regexp "\\)"))
  4545. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  4546. (old-level (if (string-match ^re txt)
  4547. (- (match-end 0) (match-beginning 0) 1)
  4548. -1))
  4549. (force-level (cond (level (prefix-numeric-value level))
  4550. ((string-match
  4551. ^re_ (buffer-substring (point-at-bol) (point)))
  4552. (- (match-end 1) (match-beginning 1)))
  4553. (t nil)))
  4554. (previous-level (save-excursion
  4555. (condition-case nil
  4556. (progn
  4557. (outline-previous-visible-heading 1)
  4558. (if (looking-at re)
  4559. (- (match-end 0) (match-beginning 0) 1)
  4560. 1))
  4561. (error 1))))
  4562. (next-level (save-excursion
  4563. (condition-case nil
  4564. (progn
  4565. (or (looking-at outline-regexp)
  4566. (outline-next-visible-heading 1))
  4567. (if (looking-at re)
  4568. (- (match-end 0) (match-beginning 0) 1)
  4569. 1))
  4570. (error 1))))
  4571. (new-level (or force-level (max previous-level next-level)))
  4572. (shift (if (or (= old-level -1)
  4573. (= new-level -1)
  4574. (= old-level new-level))
  4575. 0
  4576. (- new-level old-level)))
  4577. (delta (if (> shift 0) -1 1))
  4578. (func (if (> shift 0) 'org-demote 'org-promote))
  4579. (org-odd-levels-only nil)
  4580. beg end)
  4581. ;; Remove the forced level indicator
  4582. (if force-level
  4583. (delete-region (point-at-bol) (point)))
  4584. ;; Paste
  4585. (beginning-of-line 1)
  4586. (org-back-over-empty-lines) ;; FIXME: correct fix????
  4587. (setq beg (point))
  4588. (insert-before-markers txt) ;; FIXME: correct fix????
  4589. (unless (string-match "\n\\'" txt) (insert "\n"))
  4590. (setq end (point))
  4591. (goto-char beg)
  4592. (skip-chars-forward " \t\n\r")
  4593. (setq beg (point))
  4594. ;; Shift if necessary
  4595. (unless (= shift 0)
  4596. (save-restriction
  4597. (narrow-to-region beg end)
  4598. (while (not (= shift 0))
  4599. (org-map-region func (point-min) (point-max))
  4600. (setq shift (+ delta shift)))
  4601. (goto-char (point-min))))
  4602. (when (interactive-p)
  4603. (message "Clipboard pasted as level %d subtree" new-level))
  4604. (if (and kill-ring
  4605. (eq org-subtree-clip (current-kill 0))
  4606. org-subtree-clip-folded)
  4607. ;; The tree was folded before it was killed/copied
  4608. (hide-subtree))))
  4609. (defun org-kill-is-subtree-p (&optional txt)
  4610. "Check if the current kill is an outline subtree, or a set of trees.
  4611. Returns nil if kill does not start with a headline, or if the first
  4612. headline level is not the largest headline level in the tree.
  4613. So this will actually accept several entries of equal levels as well,
  4614. which is OK for `org-paste-subtree'.
  4615. If optional TXT is given, check this string instead of the current kill."
  4616. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  4617. (start-level (and kill
  4618. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  4619. org-outline-regexp "\\)")
  4620. kill)
  4621. (- (match-end 2) (match-beginning 2) 1)))
  4622. (re (concat "^" org-outline-regexp))
  4623. (start (1+ (match-beginning 2))))
  4624. (if (not start-level)
  4625. (progn
  4626. nil) ;; does not even start with a heading
  4627. (catch 'exit
  4628. (while (setq start (string-match re kill (1+ start)))
  4629. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  4630. (throw 'exit nil)))
  4631. t))))
  4632. (defun org-narrow-to-subtree ()
  4633. "Narrow buffer to the current subtree."
  4634. (interactive)
  4635. (save-excursion
  4636. (save-match-data
  4637. (narrow-to-region
  4638. (progn (org-back-to-heading) (point))
  4639. (progn (org-end-of-subtree t t) (point))))))
  4640. ;;; Outline Sorting
  4641. (defun org-sort (with-case)
  4642. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  4643. Optional argument WITH-CASE means sort case-sensitively."
  4644. (interactive "P")
  4645. (if (org-at-table-p)
  4646. (org-call-with-arg 'org-table-sort-lines with-case)
  4647. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  4648. (defun org-sort-remove-invisible (s)
  4649. (remove-text-properties 0 (length s) org-rm-props s)
  4650. (while (string-match org-bracket-link-regexp s)
  4651. (setq s (replace-match (if (match-end 2)
  4652. (match-string 3 s)
  4653. (match-string 1 s)) t t s)))
  4654. s)
  4655. (defvar org-priority-regexp) ; defined later in the file
  4656. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  4657. "Sort entries on a certain level of an outline tree.
  4658. If there is an active region, the entries in the region are sorted.
  4659. Else, if the cursor is before the first entry, sort the top-level items.
  4660. Else, the children of the entry at point are sorted.
  4661. Sorting can be alphabetically, numerically, and by date/time as given by
  4662. the first time stamp in the entry. The command prompts for the sorting
  4663. type unless it has been given to the function through the SORTING-TYPE
  4664. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  4665. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  4666. called with point at the beginning of the record. It must return either
  4667. a string or a number that should serve as the sorting key for that record.
  4668. Comparing entries ignores case by default. However, with an optional argument
  4669. WITH-CASE, the sorting considers case as well."
  4670. (interactive "P")
  4671. (let ((case-func (if with-case 'identity 'downcase))
  4672. start beg end stars re re2
  4673. txt what tmp plain-list-p)
  4674. ;; Find beginning and end of region to sort
  4675. (cond
  4676. ((org-region-active-p)
  4677. ;; we will sort the region
  4678. (setq end (region-end)
  4679. what "region")
  4680. (goto-char (region-beginning))
  4681. (if (not (org-on-heading-p)) (outline-next-heading))
  4682. (setq start (point)))
  4683. ((org-at-item-p)
  4684. ;; we will sort this plain list
  4685. (org-beginning-of-item-list) (setq start (point))
  4686. (org-end-of-item-list) (setq end (point))
  4687. (goto-char start)
  4688. (setq plain-list-p t
  4689. what "plain list"))
  4690. ((or (org-on-heading-p)
  4691. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  4692. ;; we will sort the children of the current headline
  4693. (org-back-to-heading)
  4694. (setq start (point)
  4695. end (progn (org-end-of-subtree t t)
  4696. (org-back-over-empty-lines)
  4697. (point))
  4698. what "children")
  4699. (goto-char start)
  4700. (show-subtree)
  4701. (outline-next-heading))
  4702. (t
  4703. ;; we will sort the top-level entries in this file
  4704. (goto-char (point-min))
  4705. (or (org-on-heading-p) (outline-next-heading))
  4706. (setq start (point) end (point-max) what "top-level")
  4707. (goto-char start)
  4708. (show-all)))
  4709. (setq beg (point))
  4710. (if (>= beg end) (error "Nothing to sort"))
  4711. (unless plain-list-p
  4712. (looking-at "\\(\\*+\\)")
  4713. (setq stars (match-string 1)
  4714. re (concat "^" (regexp-quote stars) " +")
  4715. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  4716. txt (buffer-substring beg end))
  4717. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  4718. (if (and (not (equal stars "*")) (string-match re2 txt))
  4719. (error "Region to sort contains a level above the first entry")))
  4720. (unless sorting-type
  4721. (message
  4722. (if plain-list-p
  4723. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  4724. "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty [f]unc A/N/T/P/F means reversed:")
  4725. what)
  4726. (setq sorting-type (read-char-exclusive))
  4727. (and (= (downcase sorting-type) ?f)
  4728. (setq getkey-func
  4729. (completing-read "Sort using function: "
  4730. obarray 'fboundp t nil nil))
  4731. (setq getkey-func (intern getkey-func)))
  4732. (and (= (downcase sorting-type) ?r)
  4733. (setq property
  4734. (completing-read "Property: "
  4735. (mapcar 'list (org-buffer-property-keys t))
  4736. nil t))))
  4737. (message "Sorting entries...")
  4738. (save-restriction
  4739. (narrow-to-region start end)
  4740. (let ((dcst (downcase sorting-type))
  4741. (now (current-time)))
  4742. (sort-subr
  4743. (/= dcst sorting-type)
  4744. ;; This function moves to the beginning character of the "record" to
  4745. ;; be sorted.
  4746. (if plain-list-p
  4747. (lambda nil
  4748. (if (org-at-item-p) t (goto-char (point-max))))
  4749. (lambda nil
  4750. (if (re-search-forward re nil t)
  4751. (goto-char (match-beginning 0))
  4752. (goto-char (point-max)))))
  4753. ;; This function moves to the last character of the "record" being
  4754. ;; sorted.
  4755. (if plain-list-p
  4756. 'org-end-of-item
  4757. (lambda nil
  4758. (save-match-data
  4759. (condition-case nil
  4760. (outline-forward-same-level 1)
  4761. (error
  4762. (goto-char (point-max)))))))
  4763. ;; This function returns the value that gets sorted against.
  4764. (if plain-list-p
  4765. (lambda nil
  4766. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  4767. (cond
  4768. ((= dcst ?n)
  4769. (string-to-number (buffer-substring (match-end 0)
  4770. (point-at-eol))))
  4771. ((= dcst ?a)
  4772. (buffer-substring (match-end 0) (point-at-eol)))
  4773. ((= dcst ?t)
  4774. (if (re-search-forward org-ts-regexp
  4775. (point-at-eol) t)
  4776. (org-time-string-to-time (match-string 0))
  4777. now))
  4778. ((= dcst ?f)
  4779. (if getkey-func
  4780. (progn
  4781. (setq tmp (funcall getkey-func))
  4782. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4783. tmp)
  4784. (error "Invalid key function `%s'" getkey-func)))
  4785. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4786. (lambda nil
  4787. (cond
  4788. ((= dcst ?n)
  4789. (if (looking-at outline-regexp)
  4790. (string-to-number (buffer-substring (match-end 0)
  4791. (point-at-eol)))
  4792. nil))
  4793. ((= dcst ?a)
  4794. (funcall case-func (buffer-substring (point-at-bol)
  4795. (point-at-eol))))
  4796. ((= dcst ?t)
  4797. (if (re-search-forward org-ts-regexp
  4798. (save-excursion
  4799. (forward-line 2)
  4800. (point)) t)
  4801. (org-time-string-to-time (match-string 0))
  4802. now))
  4803. ((= dcst ?p)
  4804. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  4805. (string-to-char (match-string 2))
  4806. org-default-priority))
  4807. ((= dcst ?r)
  4808. (or (org-entry-get nil property) ""))
  4809. ((= dcst ?f)
  4810. (if getkey-func
  4811. (progn
  4812. (setq tmp (funcall getkey-func))
  4813. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4814. tmp)
  4815. (error "Invalid key function `%s'" getkey-func)))
  4816. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4817. nil
  4818. (cond
  4819. ((= dcst ?a) 'string<)
  4820. ((= dcst ?t) 'time-less-p)
  4821. (t nil)))))
  4822. (message "Sorting entries...done")))
  4823. (defun org-do-sort (table what &optional with-case sorting-type)
  4824. "Sort TABLE of WHAT according to SORTING-TYPE.
  4825. The user will be prompted for the SORTING-TYPE if the call to this
  4826. function does not specify it. WHAT is only for the prompt, to indicate
  4827. what is being sorted. The sorting key will be extracted from
  4828. the car of the elements of the table.
  4829. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  4830. (unless sorting-type
  4831. (message
  4832. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  4833. what)
  4834. (setq sorting-type (read-char-exclusive)))
  4835. (let ((dcst (downcase sorting-type))
  4836. extractfun comparefun)
  4837. ;; Define the appropriate functions
  4838. (cond
  4839. ((= dcst ?n)
  4840. (setq extractfun 'string-to-number
  4841. comparefun (if (= dcst sorting-type) '< '>)))
  4842. ((= dcst ?a)
  4843. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  4844. (lambda(x) (downcase (org-sort-remove-invisible x))))
  4845. comparefun (if (= dcst sorting-type)
  4846. 'string<
  4847. (lambda (a b) (and (not (string< a b))
  4848. (not (string= a b)))))))
  4849. ((= dcst ?t)
  4850. (setq extractfun
  4851. (lambda (x)
  4852. (if (string-match org-ts-regexp x)
  4853. (time-to-seconds
  4854. (org-time-string-to-time (match-string 0 x)))
  4855. 0))
  4856. comparefun (if (= dcst sorting-type) '< '>)))
  4857. (t (error "Invalid sorting type `%c'" sorting-type)))
  4858. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  4859. table)
  4860. (lambda (a b) (funcall comparefun (car a) (car b))))))
  4861. ;;;; Plain list items, including checkboxes
  4862. ;;; Plain list items
  4863. (defun org-at-item-p ()
  4864. "Is point in a line starting a hand-formatted item?"
  4865. (let ((llt org-plain-list-ordered-item-terminator))
  4866. (save-excursion
  4867. (goto-char (point-at-bol))
  4868. (looking-at
  4869. (cond
  4870. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4871. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4872. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+))\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4873. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  4874. (defun org-in-item-p ()
  4875. "It the cursor inside a plain list item.
  4876. Does not have to be the first line."
  4877. (save-excursion
  4878. (condition-case nil
  4879. (progn
  4880. (org-beginning-of-item)
  4881. (org-at-item-p)
  4882. t)
  4883. (error nil))))
  4884. (defun org-insert-item (&optional checkbox)
  4885. "Insert a new item at the current level.
  4886. Return t when things worked, nil when we are not in an item."
  4887. (when (save-excursion
  4888. (condition-case nil
  4889. (progn
  4890. (org-beginning-of-item)
  4891. (org-at-item-p)
  4892. (if (org-invisible-p) (error "Invisible item"))
  4893. t)
  4894. (error nil)))
  4895. (let* ((bul (match-string 0))
  4896. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  4897. (match-end 0)))
  4898. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  4899. pos)
  4900. (cond
  4901. ((and (org-at-item-p) (<= (point) eow))
  4902. ;; before the bullet
  4903. (beginning-of-line 1)
  4904. (open-line (if blank 2 1)))
  4905. ((<= (point) eow)
  4906. (beginning-of-line 1))
  4907. (t
  4908. (unless (org-get-alist-option org-M-RET-may-split-line 'item)
  4909. (end-of-line 1)
  4910. (delete-horizontal-space))
  4911. (newline (if blank 2 1))))
  4912. (insert bul (if checkbox "[ ]" ""))
  4913. (just-one-space)
  4914. (setq pos (point))
  4915. (end-of-line 1)
  4916. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  4917. (org-maybe-renumber-ordered-list)
  4918. (and checkbox (org-update-checkbox-count-maybe))
  4919. t))
  4920. ;;; Checkboxes
  4921. (defun org-at-item-checkbox-p ()
  4922. "Is point at a line starting a plain-list item with a checklet?"
  4923. (and (org-at-item-p)
  4924. (save-excursion
  4925. (goto-char (match-end 0))
  4926. (skip-chars-forward " \t")
  4927. (looking-at "\\[[- X]\\]"))))
  4928. (defun org-toggle-checkbox (&optional arg)
  4929. "Toggle the checkbox in the current line."
  4930. (interactive "P")
  4931. (catch 'exit
  4932. (let (beg end status (firstnew 'unknown))
  4933. (cond
  4934. ((org-region-active-p)
  4935. (setq beg (region-beginning) end (region-end)))
  4936. ((org-on-heading-p)
  4937. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  4938. ((org-at-item-checkbox-p)
  4939. (let ((pos (point)))
  4940. (replace-match
  4941. (cond (arg "[-]")
  4942. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  4943. (t "[ ]"))
  4944. t t)
  4945. (goto-char pos))
  4946. (throw 'exit t))
  4947. (t (error "Not at a checkbox or heading, and no active region")))
  4948. (save-excursion
  4949. (goto-char beg)
  4950. (while (< (point) end)
  4951. (when (org-at-item-checkbox-p)
  4952. (setq status (equal (match-string 0) "[X]"))
  4953. (when (eq firstnew 'unknown)
  4954. (setq firstnew (not status)))
  4955. (replace-match
  4956. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  4957. (beginning-of-line 2)))))
  4958. (org-update-checkbox-count-maybe))
  4959. (defun org-update-checkbox-count-maybe ()
  4960. "Update checkbox statistics unless turned off by user."
  4961. (when org-provide-checkbox-statistics
  4962. (org-update-checkbox-count)))
  4963. (defun org-update-checkbox-count (&optional all)
  4964. "Update the checkbox statistics in the current section.
  4965. This will find all statistic cookies like [57%] and [6/12] and update them
  4966. with the current numbers. With optional prefix argument ALL, do this for
  4967. the whole buffer."
  4968. (interactive "P")
  4969. (save-excursion
  4970. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  4971. (beg (condition-case nil
  4972. (progn (outline-back-to-heading) (point))
  4973. (error (point-min))))
  4974. (end (move-marker (make-marker)
  4975. (progn (outline-next-heading) (point))))
  4976. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  4977. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  4978. (re-find (concat re "\\|" re-box))
  4979. beg-cookie end-cookie is-percent c-on c-off lim
  4980. eline curr-ind next-ind continue-from startsearch
  4981. (cstat 0)
  4982. )
  4983. (when all
  4984. (goto-char (point-min))
  4985. (outline-next-heading)
  4986. (setq beg (point) end (point-max)))
  4987. (goto-char end)
  4988. ;; find each statistic cookie
  4989. (while (re-search-backward re-find beg t)
  4990. (setq beg-cookie (match-beginning 1)
  4991. end-cookie (match-end 1)
  4992. cstat (+ cstat (if end-cookie 1 0))
  4993. startsearch (point-at-eol)
  4994. continue-from (point-at-bol)
  4995. is-percent (match-beginning 2)
  4996. lim (cond
  4997. ((org-on-heading-p) (outline-next-heading) (point))
  4998. ((org-at-item-p) (org-end-of-item) (point))
  4999. (t nil))
  5000. c-on 0
  5001. c-off 0)
  5002. (when lim
  5003. ;; find first checkbox for this cookie and gather
  5004. ;; statistics from all that are at this indentation level
  5005. (goto-char startsearch)
  5006. (if (re-search-forward re-box lim t)
  5007. (progn
  5008. (org-beginning-of-item)
  5009. (setq curr-ind (org-get-indentation))
  5010. (setq next-ind curr-ind)
  5011. (while (= curr-ind next-ind)
  5012. (save-excursion (end-of-line) (setq eline (point)))
  5013. (if (re-search-forward re-box eline t)
  5014. (if (member (match-string 2) '("[ ]" "[-]"))
  5015. (setq c-off (1+ c-off))
  5016. (setq c-on (1+ c-on))
  5017. )
  5018. )
  5019. (org-end-of-item)
  5020. (setq next-ind (org-get-indentation))
  5021. )))
  5022. (goto-char continue-from)
  5023. ;; update cookie
  5024. (when end-cookie
  5025. (delete-region beg-cookie end-cookie)
  5026. (goto-char beg-cookie)
  5027. (insert
  5028. (if is-percent
  5029. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  5030. (format "[%d/%d]" c-on (+ c-on c-off)))))
  5031. ;; update items checkbox if it has one
  5032. (when (org-at-item-p)
  5033. (org-beginning-of-item)
  5034. (when (and (> (+ c-on c-off) 0)
  5035. (re-search-forward re-box (point-at-eol) t))
  5036. (setq beg-cookie (match-beginning 2)
  5037. end-cookie (match-end 2))
  5038. (delete-region beg-cookie end-cookie)
  5039. (goto-char beg-cookie)
  5040. (cond ((= c-off 0) (insert "[X]"))
  5041. ((= c-on 0) (insert "[ ]"))
  5042. (t (insert "[-]")))
  5043. )))
  5044. (goto-char continue-from))
  5045. (when (interactive-p)
  5046. (message "Checkbox satistics updated %s (%d places)"
  5047. (if all "in entire file" "in current outline entry") cstat)))))
  5048. (defun org-get-checkbox-statistics-face ()
  5049. "Select the face for checkbox statistics.
  5050. The face will be `org-done' when all relevant boxes are checked. Otherwise
  5051. it will be `org-todo'."
  5052. (if (match-end 1)
  5053. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  5054. (if (and (> (match-end 2) (match-beginning 2))
  5055. (equal (match-string 2) (match-string 3)))
  5056. 'org-done
  5057. 'org-todo)))
  5058. (defun org-get-indentation (&optional line)
  5059. "Get the indentation of the current line, interpreting tabs.
  5060. When LINE is given, assume it represents a line and compute its indentation."
  5061. (if line
  5062. (if (string-match "^ *" (org-remove-tabs line))
  5063. (match-end 0))
  5064. (save-excursion
  5065. (beginning-of-line 1)
  5066. (skip-chars-forward " \t")
  5067. (current-column))))
  5068. (defun org-remove-tabs (s &optional width)
  5069. "Replace tabulators in S with spaces.
  5070. Assumes that s is a single line, starting in column 0."
  5071. (setq width (or width tab-width))
  5072. (while (string-match "\t" s)
  5073. (setq s (replace-match
  5074. (make-string
  5075. (- (* width (/ (+ (match-beginning 0) width) width))
  5076. (match-beginning 0)) ?\ )
  5077. t t s)))
  5078. s)
  5079. (defun org-fix-indentation (line ind)
  5080. "Fix indentation in LINE.
  5081. IND is a cons cell with target and minimum indentation.
  5082. If the current indenation in LINE is smaller than the minimum,
  5083. leave it alone. If it is larger than ind, set it to the target."
  5084. (let* ((l (org-remove-tabs line))
  5085. (i (org-get-indentation l))
  5086. (i1 (car ind)) (i2 (cdr ind)))
  5087. (if (>= i i2) (setq l (substring line i2)))
  5088. (if (> i1 0)
  5089. (concat (make-string i1 ?\ ) l)
  5090. l)))
  5091. (defcustom org-empty-line-terminates-plain-lists nil
  5092. "Non-nil means, an empty line ends all plain list levels.
  5093. When nil, empty lines are part of the preceeding item."
  5094. :group 'org-plain-lists
  5095. :type 'boolean)
  5096. (defun org-beginning-of-item ()
  5097. "Go to the beginning of the current hand-formatted item.
  5098. If the cursor is not in an item, throw an error."
  5099. (interactive)
  5100. (let ((pos (point))
  5101. (limit (save-excursion
  5102. (condition-case nil
  5103. (progn
  5104. (org-back-to-heading)
  5105. (beginning-of-line 2) (point))
  5106. (error (point-min)))))
  5107. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  5108. ind ind1)
  5109. (if (org-at-item-p)
  5110. (beginning-of-line 1)
  5111. (beginning-of-line 1)
  5112. (skip-chars-forward " \t")
  5113. (setq ind (current-column))
  5114. (if (catch 'exit
  5115. (while t
  5116. (beginning-of-line 0)
  5117. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  5118. (if (looking-at "[ \t]*$")
  5119. (setq ind1 ind-empty)
  5120. (skip-chars-forward " \t")
  5121. (setq ind1 (current-column)))
  5122. (if (< ind1 ind)
  5123. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  5124. nil
  5125. (goto-char pos)
  5126. (error "Not in an item")))))
  5127. (defun org-end-of-item ()
  5128. "Go to the end of the current hand-formatted item.
  5129. If the cursor is not in an item, throw an error."
  5130. (interactive)
  5131. (let* ((pos (point))
  5132. ind1
  5133. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  5134. (limit (save-excursion (outline-next-heading) (point)))
  5135. (ind (save-excursion
  5136. (org-beginning-of-item)
  5137. (skip-chars-forward " \t")
  5138. (current-column)))
  5139. (end (catch 'exit
  5140. (while t
  5141. (beginning-of-line 2)
  5142. (if (eobp) (throw 'exit (point)))
  5143. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  5144. (if (looking-at "[ \t]*$")
  5145. (setq ind1 ind-empty)
  5146. (skip-chars-forward " \t")
  5147. (setq ind1 (current-column)))
  5148. (if (<= ind1 ind)
  5149. (throw 'exit (point-at-bol)))))))
  5150. (if end
  5151. (goto-char end)
  5152. (goto-char pos)
  5153. (error "Not in an item"))))
  5154. (defun org-next-item ()
  5155. "Move to the beginning of the next item in the current plain list.
  5156. Error if not at a plain list, or if this is the last item in the list."
  5157. (interactive)
  5158. (let (ind ind1 (pos (point)))
  5159. (org-beginning-of-item)
  5160. (setq ind (org-get-indentation))
  5161. (org-end-of-item)
  5162. (setq ind1 (org-get-indentation))
  5163. (unless (and (org-at-item-p) (= ind ind1))
  5164. (goto-char pos)
  5165. (error "On last item"))))
  5166. (defun org-previous-item ()
  5167. "Move to the beginning of the previous item in the current plain list.
  5168. Error if not at a plain list, or if this is the first item in the list."
  5169. (interactive)
  5170. (let (beg ind ind1 (pos (point)))
  5171. (org-beginning-of-item)
  5172. (setq beg (point))
  5173. (setq ind (org-get-indentation))
  5174. (goto-char beg)
  5175. (catch 'exit
  5176. (while t
  5177. (beginning-of-line 0)
  5178. (if (looking-at "[ \t]*$")
  5179. nil
  5180. (if (<= (setq ind1 (org-get-indentation)) ind)
  5181. (throw 'exit t)))))
  5182. (condition-case nil
  5183. (if (or (not (org-at-item-p))
  5184. (< ind1 (1- ind)))
  5185. (error "")
  5186. (org-beginning-of-item))
  5187. (error (goto-char pos)
  5188. (error "On first item")))))
  5189. (defun org-first-list-item-p ()
  5190. "Is this heading the item in a plain list?"
  5191. (unless (org-at-item-p)
  5192. (error "Not at a plain list item"))
  5193. (org-beginning-of-item)
  5194. (= (point) (save-excursion (org-beginning-of-item-list))))
  5195. (defun org-move-item-down ()
  5196. "Move the plain list item at point down, i.e. swap with following item.
  5197. Subitems (items with larger indentation) are considered part of the item,
  5198. so this really moves item trees."
  5199. (interactive)
  5200. (let (beg beg0 end end0 ind ind1 (pos (point)) txt ne-end ne-beg)
  5201. (org-beginning-of-item)
  5202. (setq beg0 (point))
  5203. (save-excursion
  5204. (setq ne-beg (org-back-over-empty-lines))
  5205. (setq beg (point)))
  5206. (goto-char beg0)
  5207. (setq ind (org-get-indentation))
  5208. (org-end-of-item)
  5209. (setq end0 (point))
  5210. (setq ind1 (org-get-indentation))
  5211. (setq ne-end (org-back-over-empty-lines))
  5212. (setq end (point))
  5213. (goto-char beg0)
  5214. (when (and (org-first-list-item-p) (< ne-end ne-beg))
  5215. ;; include less whitespace
  5216. (save-excursion
  5217. (goto-char beg)
  5218. (forward-line (- ne-beg ne-end))
  5219. (setq beg (point))))
  5220. (goto-char end0)
  5221. (if (and (org-at-item-p) (= ind ind1))
  5222. (progn
  5223. (org-end-of-item)
  5224. (org-back-over-empty-lines)
  5225. (setq txt (buffer-substring beg end))
  5226. (save-excursion
  5227. (delete-region beg end))
  5228. (setq pos (point))
  5229. (insert txt)
  5230. (goto-char pos) (org-skip-whitespace)
  5231. (org-maybe-renumber-ordered-list))
  5232. (goto-char pos)
  5233. (error "Cannot move this item further down"))))
  5234. (defun org-move-item-up (arg)
  5235. "Move the plain list item at point up, i.e. swap with previous item.
  5236. Subitems (items with larger indentation) are considered part of the item,
  5237. so this really moves item trees."
  5238. (interactive "p")
  5239. (let (beg beg0 end ind ind1 (pos (point)) txt
  5240. ne-beg ne-ins ins-end)
  5241. (org-beginning-of-item)
  5242. (setq beg0 (point))
  5243. (setq ind (org-get-indentation))
  5244. (save-excursion
  5245. (setq ne-beg (org-back-over-empty-lines))
  5246. (setq beg (point)))
  5247. (goto-char beg0)
  5248. (org-end-of-item)
  5249. (setq end (point))
  5250. (goto-char beg0)
  5251. (catch 'exit
  5252. (while t
  5253. (beginning-of-line 0)
  5254. (if (looking-at "[ \t]*$")
  5255. (if org-empty-line-terminates-plain-lists
  5256. (progn
  5257. (goto-char pos)
  5258. (error "Cannot move this item further up"))
  5259. nil)
  5260. (if (<= (setq ind1 (org-get-indentation)) ind)
  5261. (throw 'exit t)))))
  5262. (condition-case nil
  5263. (org-beginning-of-item)
  5264. (error (goto-char beg)
  5265. (error "Cannot move this item further up")))
  5266. (setq ind1 (org-get-indentation))
  5267. (if (and (org-at-item-p) (= ind ind1))
  5268. (progn
  5269. (setq ne-ins (org-back-over-empty-lines))
  5270. (setq txt (buffer-substring beg end))
  5271. (save-excursion
  5272. (delete-region beg end))
  5273. (setq pos (point))
  5274. (insert txt)
  5275. (setq ins-end (point))
  5276. (goto-char pos) (org-skip-whitespace)
  5277. (when (and (org-first-list-item-p) (> ne-ins ne-beg))
  5278. ;; Move whitespace back to beginning
  5279. (save-excursion
  5280. (goto-char ins-end)
  5281. (let ((kill-whole-line t))
  5282. (kill-line (- ne-ins ne-beg)) (point)))
  5283. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5284. (org-maybe-renumber-ordered-list))
  5285. (goto-char pos)
  5286. (error "Cannot move this item further up"))))
  5287. (defun org-maybe-renumber-ordered-list ()
  5288. "Renumber the ordered list at point if setup allows it.
  5289. This tests the user option `org-auto-renumber-ordered-lists' before
  5290. doing the renumbering."
  5291. (interactive)
  5292. (when (and org-auto-renumber-ordered-lists
  5293. (org-at-item-p))
  5294. (if (match-beginning 3)
  5295. (org-renumber-ordered-list 1)
  5296. (org-fix-bullet-type))))
  5297. (defun org-maybe-renumber-ordered-list-safe ()
  5298. (condition-case nil
  5299. (save-excursion
  5300. (org-maybe-renumber-ordered-list))
  5301. (error nil)))
  5302. (defun org-cycle-list-bullet (&optional which)
  5303. "Cycle through the different itemize/enumerate bullets.
  5304. This cycle the entire list level through the sequence:
  5305. `-' -> `+' -> `*' -> `1.' -> `1)'
  5306. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  5307. 0 meand `-', 1 means `+' etc."
  5308. (interactive "P")
  5309. (org-preserve-lc
  5310. (org-beginning-of-item-list)
  5311. (org-at-item-p)
  5312. (beginning-of-line 1)
  5313. (let ((current (match-string 0))
  5314. (prevp (eq which 'previous))
  5315. new)
  5316. (setq new (cond
  5317. ((and (numberp which)
  5318. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  5319. ((string-match "-" current) (if prevp "1)" "+"))
  5320. ((string-match "\\+" current)
  5321. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  5322. ((string-match "\\*" current) (if prevp "+" "1."))
  5323. ((string-match "\\." current) (if prevp "*" "1)"))
  5324. ((string-match ")" current) (if prevp "1." "-"))
  5325. (t (error "This should not happen"))))
  5326. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  5327. (org-fix-bullet-type)
  5328. (org-maybe-renumber-ordered-list))))
  5329. (defun org-get-string-indentation (s)
  5330. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  5331. (let ((n -1) (i 0) (w tab-width) c)
  5332. (catch 'exit
  5333. (while (< (setq n (1+ n)) (length s))
  5334. (setq c (aref s n))
  5335. (cond ((= c ?\ ) (setq i (1+ i)))
  5336. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  5337. (t (throw 'exit t)))))
  5338. i))
  5339. (defun org-renumber-ordered-list (arg)
  5340. "Renumber an ordered plain list.
  5341. Cursor needs to be in the first line of an item, the line that starts
  5342. with something like \"1.\" or \"2)\"."
  5343. (interactive "p")
  5344. (unless (and (org-at-item-p)
  5345. (match-beginning 3))
  5346. (error "This is not an ordered list"))
  5347. (let ((line (org-current-line))
  5348. (col (current-column))
  5349. (ind (org-get-string-indentation
  5350. (buffer-substring (point-at-bol) (match-beginning 3))))
  5351. ;; (term (substring (match-string 3) -1))
  5352. ind1 (n (1- arg))
  5353. fmt)
  5354. ;; find where this list begins
  5355. (org-beginning-of-item-list)
  5356. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  5357. (setq fmt (concat "%d" (match-string 1)))
  5358. (beginning-of-line 0)
  5359. ;; walk forward and replace these numbers
  5360. (catch 'exit
  5361. (while t
  5362. (catch 'next
  5363. (beginning-of-line 2)
  5364. (if (eobp) (throw 'exit nil))
  5365. (if (looking-at "[ \t]*$") (throw 'next nil))
  5366. (skip-chars-forward " \t") (setq ind1 (current-column))
  5367. (if (> ind1 ind) (throw 'next t))
  5368. (if (< ind1 ind) (throw 'exit t))
  5369. (if (not (org-at-item-p)) (throw 'exit nil))
  5370. (delete-region (match-beginning 2) (match-end 2))
  5371. (goto-char (match-beginning 2))
  5372. (insert (format fmt (setq n (1+ n)))))))
  5373. (goto-line line)
  5374. (move-to-column col)))
  5375. (defun org-fix-bullet-type ()
  5376. "Make sure all items in this list have the same bullet as the firsst item."
  5377. (interactive)
  5378. (unless (org-at-item-p) (error "This is not a list"))
  5379. (let ((line (org-current-line))
  5380. (col (current-column))
  5381. (ind (current-indentation))
  5382. ind1 bullet)
  5383. ;; find where this list begins
  5384. (org-beginning-of-item-list)
  5385. (beginning-of-line 1)
  5386. ;; find out what the bullet type is
  5387. (looking-at "[ \t]*\\(\\S-+\\)")
  5388. (setq bullet (match-string 1))
  5389. ;; walk forward and replace these numbers
  5390. (beginning-of-line 0)
  5391. (catch 'exit
  5392. (while t
  5393. (catch 'next
  5394. (beginning-of-line 2)
  5395. (if (eobp) (throw 'exit nil))
  5396. (if (looking-at "[ \t]*$") (throw 'next nil))
  5397. (skip-chars-forward " \t") (setq ind1 (current-column))
  5398. (if (> ind1 ind) (throw 'next t))
  5399. (if (< ind1 ind) (throw 'exit t))
  5400. (if (not (org-at-item-p)) (throw 'exit nil))
  5401. (skip-chars-forward " \t")
  5402. (looking-at "\\S-+")
  5403. (replace-match bullet))))
  5404. (goto-line line)
  5405. (move-to-column col)
  5406. (if (string-match "[0-9]" bullet)
  5407. (org-renumber-ordered-list 1))))
  5408. (defun org-beginning-of-item-list ()
  5409. "Go to the beginning of the current item list.
  5410. I.e. to the first item in this list."
  5411. (interactive)
  5412. (org-beginning-of-item)
  5413. (let ((pos (point-at-bol))
  5414. (ind (org-get-indentation))
  5415. ind1)
  5416. ;; find where this list begins
  5417. (catch 'exit
  5418. (while t
  5419. (catch 'next
  5420. (beginning-of-line 0)
  5421. (if (looking-at "[ \t]*$")
  5422. (throw (if (bobp) 'exit 'next) t))
  5423. (skip-chars-forward " \t") (setq ind1 (current-column))
  5424. (if (or (< ind1 ind)
  5425. (and (= ind1 ind)
  5426. (not (org-at-item-p)))
  5427. (bobp))
  5428. (throw 'exit t)
  5429. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  5430. (goto-char pos)))
  5431. (defun org-end-of-item-list ()
  5432. "Go to the end of the current item list.
  5433. I.e. to the text after the last item."
  5434. (interactive)
  5435. (org-beginning-of-item)
  5436. (let ((pos (point-at-bol))
  5437. (ind (org-get-indentation))
  5438. ind1)
  5439. ;; find where this list begins
  5440. (catch 'exit
  5441. (while t
  5442. (catch 'next
  5443. (beginning-of-line 2)
  5444. (if (looking-at "[ \t]*$")
  5445. (throw (if (eobp) 'exit 'next) t))
  5446. (skip-chars-forward " \t") (setq ind1 (current-column))
  5447. (if (or (< ind1 ind)
  5448. (and (= ind1 ind)
  5449. (not (org-at-item-p)))
  5450. (eobp))
  5451. (progn
  5452. (setq pos (point-at-bol))
  5453. (throw 'exit t))))))
  5454. (goto-char pos)))
  5455. (defvar org-last-indent-begin-marker (make-marker))
  5456. (defvar org-last-indent-end-marker (make-marker))
  5457. (defun org-outdent-item (arg)
  5458. "Outdent a local list item."
  5459. (interactive "p")
  5460. (org-indent-item (- arg)))
  5461. (defun org-indent-item (arg)
  5462. "Indent a local list item."
  5463. (interactive "p")
  5464. (unless (org-at-item-p)
  5465. (error "Not on an item"))
  5466. (save-excursion
  5467. (let (beg end ind ind1 tmp delta ind-down ind-up)
  5468. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  5469. (setq beg org-last-indent-begin-marker
  5470. end org-last-indent-end-marker)
  5471. (org-beginning-of-item)
  5472. (setq beg (move-marker org-last-indent-begin-marker (point)))
  5473. (org-end-of-item)
  5474. (setq end (move-marker org-last-indent-end-marker (point))))
  5475. (goto-char beg)
  5476. (setq tmp (org-item-indent-positions)
  5477. ind (car tmp)
  5478. ind-down (nth 2 tmp)
  5479. ind-up (nth 1 tmp)
  5480. delta (if (> arg 0)
  5481. (if ind-down (- ind-down ind) 2)
  5482. (if ind-up (- ind-up ind) -2)))
  5483. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  5484. (while (< (point) end)
  5485. (beginning-of-line 1)
  5486. (skip-chars-forward " \t") (setq ind1 (current-column))
  5487. (delete-region (point-at-bol) (point))
  5488. (or (eolp) (indent-to-column (+ ind1 delta)))
  5489. (beginning-of-line 2))))
  5490. (org-fix-bullet-type)
  5491. (org-maybe-renumber-ordered-list-safe)
  5492. (save-excursion
  5493. (beginning-of-line 0)
  5494. (condition-case nil (org-beginning-of-item) (error nil))
  5495. (org-maybe-renumber-ordered-list-safe)))
  5496. (defun org-item-indent-positions ()
  5497. "Return indentation for plain list items.
  5498. This returns a list with three values: The current indentation, the
  5499. parent indentation and the indentation a child should habe.
  5500. Assumes cursor in item line."
  5501. (let* ((bolpos (point-at-bol))
  5502. (ind (org-get-indentation))
  5503. ind-down ind-up pos)
  5504. (save-excursion
  5505. (org-beginning-of-item-list)
  5506. (skip-chars-backward "\n\r \t")
  5507. (when (org-in-item-p)
  5508. (org-beginning-of-item)
  5509. (setq ind-up (org-get-indentation))))
  5510. (setq pos (point))
  5511. (save-excursion
  5512. (cond
  5513. ((and (condition-case nil (progn (org-previous-item) t)
  5514. (error nil))
  5515. (or (forward-char 1) t)
  5516. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  5517. (setq ind-down (org-get-indentation)))
  5518. ((and (goto-char pos)
  5519. (org-at-item-p))
  5520. (goto-char (match-end 0))
  5521. (skip-chars-forward " \t")
  5522. (setq ind-down (current-column)))))
  5523. (list ind ind-up ind-down)))
  5524. ;;; The orgstruct minor mode
  5525. ;; Define a minor mode which can be used in other modes in order to
  5526. ;; integrate the org-mode structure editing commands.
  5527. ;; This is really a hack, because the org-mode structure commands use
  5528. ;; keys which normally belong to the major mode. Here is how it
  5529. ;; works: The minor mode defines all the keys necessary to operate the
  5530. ;; structure commands, but wraps the commands into a function which
  5531. ;; tests if the cursor is currently at a headline or a plain list
  5532. ;; item. If that is the case, the structure command is used,
  5533. ;; temporarily setting many Org-mode variables like regular
  5534. ;; expressions for filling etc. However, when any of those keys is
  5535. ;; used at a different location, function uses `key-binding' to look
  5536. ;; up if the key has an associated command in another currently active
  5537. ;; keymap (minor modes, major mode, global), and executes that
  5538. ;; command. There might be problems if any of the keys is otherwise
  5539. ;; used as a prefix key.
  5540. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5541. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5542. ;; addresses this by checking explicitly for both bindings.
  5543. (defvar orgstruct-mode-map (make-sparse-keymap)
  5544. "Keymap for the minor `orgstruct-mode'.")
  5545. (defvar org-local-vars nil
  5546. "List of local variables, for use by `orgstruct-mode'")
  5547. ;;;###autoload
  5548. (define-minor-mode orgstruct-mode
  5549. "Toggle the minor more `orgstruct-mode'.
  5550. This mode is for using Org-mode structure commands in other modes.
  5551. The following key behave as if Org-mode was active, if the cursor
  5552. is on a headline, or on a plain list item (both in the definition
  5553. of Org-mode).
  5554. M-up Move entry/item up
  5555. M-down Move entry/item down
  5556. M-left Promote
  5557. M-right Demote
  5558. M-S-up Move entry/item up
  5559. M-S-down Move entry/item down
  5560. M-S-left Promote subtree
  5561. M-S-right Demote subtree
  5562. M-q Fill paragraph and items like in Org-mode
  5563. C-c ^ Sort entries
  5564. C-c - Cycle list bullet
  5565. TAB Cycle item visibility
  5566. M-RET Insert new heading/item
  5567. S-M-RET Insert new TODO heading / Chekbox item
  5568. C-c C-c Set tags / toggle checkbox"
  5569. nil " OrgStruct" nil
  5570. (org-load-modules-maybe)
  5571. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5572. ;;;###autoload
  5573. (defun turn-on-orgstruct ()
  5574. "Unconditionally turn on `orgstruct-mode'."
  5575. (orgstruct-mode 1))
  5576. ;;;###autoload
  5577. (defun turn-on-orgstruct++ ()
  5578. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5579. In addition to setting orgstruct-mode, this also exports all indentation and
  5580. autofilling variables from org-mode into the buffer. Note that turning
  5581. off orgstruct-mode will *not* remove these additional settings."
  5582. (orgstruct-mode 1)
  5583. (let (var val)
  5584. (mapc
  5585. (lambda (x)
  5586. (when (string-match
  5587. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5588. (symbol-name (car x)))
  5589. (setq var (car x) val (nth 1 x))
  5590. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5591. org-local-vars)))
  5592. (defun orgstruct-error ()
  5593. "Error when there is no default binding for a structure key."
  5594. (interactive)
  5595. (error "This key has no function outside structure elements"))
  5596. (defun orgstruct-setup ()
  5597. "Setup orgstruct keymaps."
  5598. (let ((nfunc 0)
  5599. (bindings
  5600. (list
  5601. '([(meta up)] org-metaup)
  5602. '([(meta down)] org-metadown)
  5603. '([(meta left)] org-metaleft)
  5604. '([(meta right)] org-metaright)
  5605. '([(meta shift up)] org-shiftmetaup)
  5606. '([(meta shift down)] org-shiftmetadown)
  5607. '([(meta shift left)] org-shiftmetaleft)
  5608. '([(meta shift right)] org-shiftmetaright)
  5609. '([(shift up)] org-shiftup)
  5610. '([(shift down)] org-shiftdown)
  5611. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5612. '("\M-q" fill-paragraph)
  5613. '("\C-c^" org-sort)
  5614. '("\C-c-" org-cycle-list-bullet)))
  5615. elt key fun cmd)
  5616. (while (setq elt (pop bindings))
  5617. (setq nfunc (1+ nfunc))
  5618. (setq key (org-key (car elt))
  5619. fun (nth 1 elt)
  5620. cmd (orgstruct-make-binding fun nfunc key))
  5621. (org-defkey orgstruct-mode-map key cmd))
  5622. ;; Special treatment needed for TAB and RET
  5623. (org-defkey orgstruct-mode-map [(tab)]
  5624. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5625. (org-defkey orgstruct-mode-map "\C-i"
  5626. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5627. (org-defkey orgstruct-mode-map "\M-\C-m"
  5628. (orgstruct-make-binding 'org-insert-heading 105
  5629. "\M-\C-m" [(meta return)]))
  5630. (org-defkey orgstruct-mode-map [(meta return)]
  5631. (orgstruct-make-binding 'org-insert-heading 106
  5632. [(meta return)] "\M-\C-m"))
  5633. (org-defkey orgstruct-mode-map [(shift meta return)]
  5634. (orgstruct-make-binding 'org-insert-todo-heading 107
  5635. [(meta return)] "\M-\C-m"))
  5636. (unless org-local-vars
  5637. (setq org-local-vars (org-get-local-variables)))
  5638. t))
  5639. (defun orgstruct-make-binding (fun n &rest keys)
  5640. "Create a function for binding in the structure minor mode.
  5641. FUN is the command to call inside a table. N is used to create a unique
  5642. command name. KEYS are keys that should be checked in for a command
  5643. to execute outside of tables."
  5644. (eval
  5645. (list 'defun
  5646. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5647. '(arg)
  5648. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5649. "Outside of structure, run the binding of `"
  5650. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5651. "'.")
  5652. '(interactive "p")
  5653. (list 'if
  5654. '(org-context-p 'headline 'item)
  5655. (list 'org-run-like-in-org-mode (list 'quote fun))
  5656. (list 'let '(orgstruct-mode)
  5657. (list 'call-interactively
  5658. (append '(or)
  5659. (mapcar (lambda (k)
  5660. (list 'key-binding k))
  5661. keys)
  5662. '('orgstruct-error))))))))
  5663. (defun org-context-p (&rest contexts)
  5664. "Check if local context is and of CONTEXTS.
  5665. Possible values in the list of contexts are `table', `headline', and `item'."
  5666. (let ((pos (point)))
  5667. (goto-char (point-at-bol))
  5668. (prog1 (or (and (memq 'table contexts)
  5669. (looking-at "[ \t]*|"))
  5670. (and (memq 'headline contexts)
  5671. (looking-at "\\*+"))
  5672. (and (memq 'item contexts)
  5673. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5674. (goto-char pos))))
  5675. (defun org-get-local-variables ()
  5676. "Return a list of all local variables in an org-mode buffer."
  5677. (let (varlist)
  5678. (with-current-buffer (get-buffer-create "*Org tmp*")
  5679. (erase-buffer)
  5680. (org-mode)
  5681. (setq varlist (buffer-local-variables)))
  5682. (kill-buffer "*Org tmp*")
  5683. (delq nil
  5684. (mapcar
  5685. (lambda (x)
  5686. (setq x
  5687. (if (symbolp x)
  5688. (list x)
  5689. (list (car x) (list 'quote (cdr x)))))
  5690. (if (string-match
  5691. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5692. (symbol-name (car x)))
  5693. x nil))
  5694. varlist))))
  5695. ;;;###autoload
  5696. (defun org-run-like-in-org-mode (cmd)
  5697. (org-load-modules-maybe)
  5698. (unless org-local-vars
  5699. (setq org-local-vars (org-get-local-variables)))
  5700. (eval (list 'let org-local-vars
  5701. (list 'call-interactively (list 'quote cmd)))))
  5702. ;;;; Archiving
  5703. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  5704. (defun org-archive-subtree (&optional find-done)
  5705. "Move the current subtree to the archive.
  5706. The archive can be a certain top-level heading in the current file, or in
  5707. a different file. The tree will be moved to that location, the subtree
  5708. heading be marked DONE, and the current time will be added.
  5709. When called with prefix argument FIND-DONE, find whole trees without any
  5710. open TODO items and archive them (after getting confirmation from the user).
  5711. If the cursor is not at a headline when this comand is called, try all level
  5712. 1 trees. If the cursor is on a headline, only try the direct children of
  5713. this heading."
  5714. (interactive "P")
  5715. (if find-done
  5716. (org-archive-all-done)
  5717. ;; Save all relevant TODO keyword-relatex variables
  5718. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  5719. (tr-org-todo-keywords-1 org-todo-keywords-1)
  5720. (tr-org-todo-kwd-alist org-todo-kwd-alist)
  5721. (tr-org-done-keywords org-done-keywords)
  5722. (tr-org-todo-regexp org-todo-regexp)
  5723. (tr-org-todo-line-regexp org-todo-line-regexp)
  5724. (tr-org-odd-levels-only org-odd-levels-only)
  5725. (this-buffer (current-buffer))
  5726. (org-archive-location org-archive-location)
  5727. (re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
  5728. ;; start of variables that will be used for saving context
  5729. ;; The compiler complains about them - keep them anyway!
  5730. (file (abbreviate-file-name (buffer-file-name)))
  5731. (olpath (mapconcat 'identity (org-get-outline-path) "/"))
  5732. (time (format-time-string
  5733. (substring (cdr org-time-stamp-formats) 1 -1)
  5734. (current-time)))
  5735. afile heading buffer level newfile-p
  5736. category todo priority
  5737. ;; start of variables that will be used for savind context
  5738. ltags itags prop)
  5739. ;; Try to find a local archive location
  5740. (save-excursion
  5741. (save-restriction
  5742. (widen)
  5743. (setq prop (org-entry-get nil "ARCHIVE" 'inherit))
  5744. (if (and prop (string-match "\\S-" prop))
  5745. (setq org-archive-location prop)
  5746. (if (or (re-search-backward re nil t)
  5747. (re-search-forward re nil t))
  5748. (setq org-archive-location (match-string 1))))))
  5749. (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
  5750. (progn
  5751. (setq afile (format (match-string 1 org-archive-location)
  5752. (file-name-nondirectory buffer-file-name))
  5753. heading (match-string 2 org-archive-location)))
  5754. (error "Invalid `org-archive-location'"))
  5755. (if (> (length afile) 0)
  5756. (setq newfile-p (not (file-exists-p afile))
  5757. buffer (find-file-noselect afile))
  5758. (setq buffer (current-buffer)))
  5759. (unless buffer
  5760. (error "Cannot access file \"%s\"" afile))
  5761. (if (and (> (length heading) 0)
  5762. (string-match "^\\*+" heading))
  5763. (setq level (match-end 0))
  5764. (setq heading nil level 0))
  5765. (save-excursion
  5766. (org-back-to-heading t)
  5767. ;; Get context information that will be lost by moving the tree
  5768. (org-refresh-category-properties)
  5769. (setq category (org-get-category)
  5770. todo (and (looking-at org-todo-line-regexp)
  5771. (match-string 2))
  5772. priority (org-get-priority (if (match-end 3) (match-string 3) ""))
  5773. ltags (org-get-tags)
  5774. itags (org-delete-all ltags (org-get-tags-at)))
  5775. (setq ltags (mapconcat 'identity ltags " ")
  5776. itags (mapconcat 'identity itags " "))
  5777. ;; We first only copy, in case something goes wrong
  5778. ;; we need to protect this-command, to avoid kill-region sets it,
  5779. ;; which would lead to duplication of subtrees
  5780. (let (this-command) (org-copy-subtree))
  5781. (set-buffer buffer)
  5782. ;; Enforce org-mode for the archive buffer
  5783. (if (not (org-mode-p))
  5784. ;; Force the mode for future visits.
  5785. (let ((org-insert-mode-line-in-empty-file t)
  5786. (org-inhibit-startup t))
  5787. (call-interactively 'org-mode)))
  5788. (when newfile-p
  5789. (goto-char (point-max))
  5790. (insert (format "\nArchived entries from file %s\n\n"
  5791. (buffer-file-name this-buffer))))
  5792. ;; Force the TODO keywords of the original buffer
  5793. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  5794. (org-todo-keywords-1 tr-org-todo-keywords-1)
  5795. (org-todo-kwd-alist tr-org-todo-kwd-alist)
  5796. (org-done-keywords tr-org-done-keywords)
  5797. (org-todo-regexp tr-org-todo-regexp)
  5798. (org-todo-line-regexp tr-org-todo-line-regexp)
  5799. (org-odd-levels-only
  5800. (if (local-variable-p 'org-odd-levels-only (current-buffer))
  5801. org-odd-levels-only
  5802. tr-org-odd-levels-only)))
  5803. (goto-char (point-min))
  5804. (show-all)
  5805. (if heading
  5806. (progn
  5807. (if (re-search-forward
  5808. (concat "^" (regexp-quote heading)
  5809. (org-re "[ \t]*\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\($\\|\r\\)"))
  5810. nil t)
  5811. (goto-char (match-end 0))
  5812. ;; Heading not found, just insert it at the end
  5813. (goto-char (point-max))
  5814. (or (bolp) (insert "\n"))
  5815. (insert "\n" heading "\n")
  5816. (end-of-line 0))
  5817. ;; Make the subtree visible
  5818. (show-subtree)
  5819. (org-end-of-subtree t)
  5820. (skip-chars-backward " \t\r\n")
  5821. (and (looking-at "[ \t\r\n]*")
  5822. (replace-match "\n\n")))
  5823. ;; No specific heading, just go to end of file.
  5824. (goto-char (point-max)) (insert "\n"))
  5825. ;; Paste
  5826. (org-paste-subtree (org-get-valid-level level 1))
  5827. ;; Mark the entry as done
  5828. (when (and org-archive-mark-done
  5829. (looking-at org-todo-line-regexp)
  5830. (or (not (match-end 2))
  5831. (not (member (match-string 2) org-done-keywords))))
  5832. (let (org-log-done org-todo-log-states)
  5833. (org-todo
  5834. (car (or (member org-archive-mark-done org-done-keywords)
  5835. org-done-keywords)))))
  5836. ;; Add the context info
  5837. (when org-archive-save-context-info
  5838. (let ((l org-archive-save-context-info) e n v)
  5839. (while (setq e (pop l))
  5840. (when (and (setq v (symbol-value e))
  5841. (stringp v) (string-match "\\S-" v))
  5842. (setq n (concat "ARCHIVE_" (upcase (symbol-name e))))
  5843. (org-entry-put (point) n v)))))
  5844. ;; Save and kill the buffer, if it is not the same buffer.
  5845. (if (not (eq this-buffer buffer))
  5846. (progn (save-buffer) (kill-buffer buffer)))))
  5847. ;; Here we are back in the original buffer. Everything seems to have
  5848. ;; worked. So now cut the tree and finish up.
  5849. (let (this-command) (org-cut-subtree))
  5850. (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
  5851. (message "Subtree archived %s"
  5852. (if (eq this-buffer buffer)
  5853. (concat "under heading: " heading)
  5854. (concat "in file: " (abbreviate-file-name afile)))))))
  5855. (defun org-refresh-category-properties ()
  5856. "Refresh category text properties in teh buffer."
  5857. (let ((def-cat (cond
  5858. ((null org-category)
  5859. (if buffer-file-name
  5860. (file-name-sans-extension
  5861. (file-name-nondirectory buffer-file-name))
  5862. "???"))
  5863. ((symbolp org-category) (symbol-name org-category))
  5864. (t org-category)))
  5865. beg end cat pos optionp)
  5866. (org-unmodified
  5867. (save-excursion
  5868. (save-restriction
  5869. (widen)
  5870. (goto-char (point-min))
  5871. (put-text-property (point) (point-max) 'org-category def-cat)
  5872. (while (re-search-forward
  5873. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5874. (setq pos (match-end 0)
  5875. optionp (equal (char-after (match-beginning 0)) ?#)
  5876. cat (org-trim (match-string 2)))
  5877. (if optionp
  5878. (setq beg (point-at-bol) end (point-max))
  5879. (org-back-to-heading t)
  5880. (setq beg (point) end (org-end-of-subtree t t)))
  5881. (put-text-property beg end 'org-category cat)
  5882. (goto-char pos)))))))
  5883. (defun org-archive-all-done (&optional tag)
  5884. "Archive sublevels of the current tree without open TODO items.
  5885. If the cursor is not on a headline, try all level 1 trees. If
  5886. it is on a headline, try all direct children.
  5887. When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
  5888. (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
  5889. (rea (concat ".*:" org-archive-tag ":"))
  5890. (begm (make-marker))
  5891. (endm (make-marker))
  5892. (question (if tag "Set ARCHIVE tag (no open TODO items)? "
  5893. "Move subtree to archive (no open TODO items)? "))
  5894. beg end (cntarch 0))
  5895. (if (org-on-heading-p)
  5896. (progn
  5897. (setq re1 (concat "^" (regexp-quote
  5898. (make-string
  5899. (1+ (- (match-end 0) (match-beginning 0) 1))
  5900. ?*))
  5901. " "))
  5902. (move-marker begm (point))
  5903. (move-marker endm (org-end-of-subtree t)))
  5904. (setq re1 "^* ")
  5905. (move-marker begm (point-min))
  5906. (move-marker endm (point-max)))
  5907. (save-excursion
  5908. (goto-char begm)
  5909. (while (re-search-forward re1 endm t)
  5910. (setq beg (match-beginning 0)
  5911. end (save-excursion (org-end-of-subtree t) (point)))
  5912. (goto-char beg)
  5913. (if (re-search-forward re end t)
  5914. (goto-char end)
  5915. (goto-char beg)
  5916. (if (and (or (not tag) (not (looking-at rea)))
  5917. (y-or-n-p question))
  5918. (progn
  5919. (if tag
  5920. (org-toggle-tag org-archive-tag 'on)
  5921. (org-archive-subtree))
  5922. (setq cntarch (1+ cntarch)))
  5923. (goto-char end)))))
  5924. (message "%d trees archived" cntarch)))
  5925. (defun org-cycle-hide-drawers (state)
  5926. "Re-hide all drawers after a visibility state change."
  5927. (when (and (org-mode-p)
  5928. (not (memq state '(overview folded))))
  5929. (save-excursion
  5930. (let* ((globalp (memq state '(contents all)))
  5931. (beg (if globalp (point-min) (point)))
  5932. (end (if globalp (point-max) (org-end-of-subtree t))))
  5933. (goto-char beg)
  5934. (while (re-search-forward org-drawer-regexp end t)
  5935. (org-flag-drawer t))))))
  5936. (defun org-flag-drawer (flag)
  5937. (save-excursion
  5938. (beginning-of-line 1)
  5939. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5940. (let ((b (match-end 0))
  5941. (outline-regexp org-outline-regexp))
  5942. (if (re-search-forward
  5943. "^[ \t]*:END:"
  5944. (save-excursion (outline-next-heading) (point)) t)
  5945. (outline-flag-region b (point-at-eol) flag)
  5946. (error ":END: line missing"))))))
  5947. (defun org-cycle-hide-archived-subtrees (state)
  5948. "Re-hide all archived subtrees after a visibility state change."
  5949. (when (and (not org-cycle-open-archived-trees)
  5950. (not (memq state '(overview folded))))
  5951. (save-excursion
  5952. (let* ((globalp (memq state '(contents all)))
  5953. (beg (if globalp (point-min) (point)))
  5954. (end (if globalp (point-max) (org-end-of-subtree t))))
  5955. (org-hide-archived-subtrees beg end)
  5956. (goto-char beg)
  5957. (if (looking-at (concat ".*:" org-archive-tag ":"))
  5958. (message "%s" (substitute-command-keys
  5959. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  5960. (defun org-force-cycle-archived ()
  5961. "Cycle subtree even if it is archived."
  5962. (interactive)
  5963. (setq this-command 'org-cycle)
  5964. (let ((org-cycle-open-archived-trees t))
  5965. (call-interactively 'org-cycle)))
  5966. (defun org-hide-archived-subtrees (beg end)
  5967. "Re-hide all archived subtrees after a visibility state change."
  5968. (save-excursion
  5969. (let* ((re (concat ":" org-archive-tag ":")))
  5970. (goto-char beg)
  5971. (while (re-search-forward re end t)
  5972. (and (org-on-heading-p) (hide-subtree))
  5973. (org-end-of-subtree t)))))
  5974. (defun org-toggle-tag (tag &optional onoff)
  5975. "Toggle the tag TAG for the current line.
  5976. If ONOFF is `on' or `off', don't toggle but set to this state."
  5977. (unless (org-on-heading-p t) (error "Not on headling"))
  5978. (let (res current)
  5979. (save-excursion
  5980. (beginning-of-line)
  5981. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  5982. (point-at-eol) t)
  5983. (progn
  5984. (setq current (match-string 1))
  5985. (replace-match ""))
  5986. (setq current ""))
  5987. (setq current (nreverse (org-split-string current ":")))
  5988. (cond
  5989. ((eq onoff 'on)
  5990. (setq res t)
  5991. (or (member tag current) (push tag current)))
  5992. ((eq onoff 'off)
  5993. (or (not (member tag current)) (setq current (delete tag current))))
  5994. (t (if (member tag current)
  5995. (setq current (delete tag current))
  5996. (setq res t)
  5997. (push tag current))))
  5998. (end-of-line 1)
  5999. (if current
  6000. (progn
  6001. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  6002. (org-set-tags nil t))
  6003. (delete-horizontal-space))
  6004. (run-hooks 'org-after-tags-change-hook))
  6005. res))
  6006. (defun org-toggle-archive-tag (&optional arg)
  6007. "Toggle the archive tag for the current headline.
  6008. With prefix ARG, check all children of current headline and offer tagging
  6009. the children that do not contain any open TODO items."
  6010. (interactive "P")
  6011. (if arg
  6012. (org-archive-all-done 'tag)
  6013. (let (set)
  6014. (save-excursion
  6015. (org-back-to-heading t)
  6016. (setq set (org-toggle-tag org-archive-tag))
  6017. (when set (hide-subtree)))
  6018. (and set (beginning-of-line 1))
  6019. (message "Subtree %s" (if set "archived" "unarchived")))))
  6020. ;;;; Link Stuff
  6021. ;;; Link abbreviations
  6022. (defun org-link-expand-abbrev (link)
  6023. "Apply replacements as defined in `org-link-abbrev-alist."
  6024. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6025. (let* ((key (match-string 1 link))
  6026. (as (or (assoc key org-link-abbrev-alist-local)
  6027. (assoc key org-link-abbrev-alist)))
  6028. (tag (and (match-end 2) (match-string 3 link)))
  6029. rpl)
  6030. (if (not as)
  6031. link
  6032. (setq rpl (cdr as))
  6033. (cond
  6034. ((symbolp rpl) (funcall rpl tag))
  6035. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6036. (t (concat rpl tag)))))
  6037. link))
  6038. ;;; Storing and inserting links
  6039. (defvar org-insert-link-history nil
  6040. "Minibuffer history for links inserted with `org-insert-link'.")
  6041. (defvar org-stored-links nil
  6042. "Contains the links stored with `org-store-link'.")
  6043. (defvar org-store-link-plist nil
  6044. "Plist with info about the most recently link created with `org-store-link'.")
  6045. (defvar org-link-protocols nil
  6046. "Link protocols added to Org-mode using `org-add-link-type'.")
  6047. (defvar org-store-link-functions nil
  6048. "List of functions that are called to create and store a link.
  6049. Each function will be called in turn until one returns a non-nil
  6050. value. Each function should check if it is responsible for creating
  6051. this link (for example by looking at the major mode).
  6052. If not, it must exit and return nil.
  6053. If yes, it should return a non-nil value after a calling
  6054. `org-store-link-props' with a list of properties and values.
  6055. Special properties are:
  6056. :type The link prefix. like \"http\". This must be given.
  6057. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6058. This is obligatory as well.
  6059. :description Optional default description for the second pair
  6060. of brackets in an Org-mode link. The user can still change
  6061. this when inserting this link into an Org-mode buffer.
  6062. In addition to these, any additional properties can be specified
  6063. and then used in remember templates.")
  6064. (defun org-add-link-type (type &optional follow export)
  6065. "Add TYPE to the list of `org-link-types'.
  6066. Re-compute all regular expressions depending on `org-link-types'
  6067. FOLLOW and EXPORT are two functions.
  6068. FOLLOW should take the link path as the single argument and do whatever
  6069. is necessary to follow the link, for example find a file or display
  6070. a mail message.
  6071. EXPORT should format the link path for export to one of the export formats.
  6072. It should be a function accepting three arguments:
  6073. path the path of the link, the text after the prefix (like \"http:\")
  6074. desc the description of the link, if any, nil if there was no descripton
  6075. format the export format, a symbol like `html' or `latex'.
  6076. The function may use the FORMAT information to return different values
  6077. depending on the format. The return value will be put literally into
  6078. the exported file.
  6079. Org-mode has a built-in default for exporting links. If you are happy with
  6080. this default, there is no need to define an export function for the link
  6081. type. For a simple example of an export function, see `org-bbdb.el'."
  6082. (add-to-list 'org-link-types type t)
  6083. (org-make-link-regexps)
  6084. (if (assoc type org-link-protocols)
  6085. (setcdr (assoc type org-link-protocols) (list follow export))
  6086. (push (list type follow export) org-link-protocols)))
  6087. ;;;###autoload
  6088. (defun org-store-link (arg)
  6089. "\\<org-mode-map>Store an org-link to the current location.
  6090. This link is added to `org-stored-links' and can later be inserted
  6091. into an org-buffer with \\[org-insert-link].
  6092. For some link types, a prefix arg is interpreted:
  6093. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  6094. For file links, arg negates `org-context-in-file-links'."
  6095. (interactive "P")
  6096. (org-load-modules-maybe)
  6097. (setq org-store-link-plist nil) ; reset
  6098. (let (link cpltxt desc description search txt)
  6099. (cond
  6100. ((run-hook-with-args-until-success 'org-store-link-functions)
  6101. (setq link (plist-get org-store-link-plist :link)
  6102. desc (or (plist-get org-store-link-plist :description) link)))
  6103. ((eq major-mode 'calendar-mode)
  6104. (let ((cd (calendar-cursor-to-date)))
  6105. (setq link
  6106. (format-time-string
  6107. (car org-time-stamp-formats)
  6108. (apply 'encode-time
  6109. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6110. nil nil nil))))
  6111. (org-store-link-props :type "calendar" :date cd)))
  6112. ((eq major-mode 'w3-mode)
  6113. (setq cpltxt (url-view-url t)
  6114. link (org-make-link cpltxt))
  6115. (org-store-link-props :type "w3" :url (url-view-url t)))
  6116. ((eq major-mode 'w3m-mode)
  6117. (setq cpltxt (or w3m-current-title w3m-current-url)
  6118. link (org-make-link w3m-current-url))
  6119. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6120. ((setq search (run-hook-with-args-until-success
  6121. 'org-create-file-search-functions))
  6122. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6123. "::" search))
  6124. (setq cpltxt (or description link)))
  6125. ((eq major-mode 'image-mode)
  6126. (setq cpltxt (concat "file:"
  6127. (abbreviate-file-name buffer-file-name))
  6128. link (org-make-link cpltxt))
  6129. (org-store-link-props :type "image" :file buffer-file-name))
  6130. ((eq major-mode 'dired-mode)
  6131. ;; link to the file in the current line
  6132. (setq cpltxt (concat "file:"
  6133. (abbreviate-file-name
  6134. (expand-file-name
  6135. (dired-get-filename nil t))))
  6136. link (org-make-link cpltxt)))
  6137. ((and buffer-file-name (org-mode-p))
  6138. ;; Just link to current headline
  6139. (setq cpltxt (concat "file:"
  6140. (abbreviate-file-name buffer-file-name)))
  6141. ;; Add a context search string
  6142. (when (org-xor org-context-in-file-links arg)
  6143. ;; Check if we are on a target
  6144. (if (org-in-regexp "<<\\(.*?\\)>>")
  6145. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  6146. (setq txt (cond
  6147. ((org-on-heading-p) nil)
  6148. ((org-region-active-p)
  6149. (buffer-substring (region-beginning) (region-end)))
  6150. (t (buffer-substring (point-at-bol) (point-at-eol)))))
  6151. (when (or (null txt) (string-match "\\S-" txt))
  6152. (setq cpltxt
  6153. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6154. desc "NONE"))))
  6155. (if (string-match "::\\'" cpltxt)
  6156. (setq cpltxt (substring cpltxt 0 -2)))
  6157. (setq link (org-make-link cpltxt)))
  6158. ((buffer-file-name (buffer-base-buffer))
  6159. ;; Just link to this file here.
  6160. (setq cpltxt (concat "file:"
  6161. (abbreviate-file-name
  6162. (buffer-file-name (buffer-base-buffer)))))
  6163. ;; Add a context string
  6164. (when (org-xor org-context-in-file-links arg)
  6165. (setq txt (if (org-region-active-p)
  6166. (buffer-substring (region-beginning) (region-end))
  6167. (buffer-substring (point-at-bol) (point-at-eol))))
  6168. ;; Only use search option if there is some text.
  6169. (when (string-match "\\S-" txt)
  6170. (setq cpltxt
  6171. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6172. desc "NONE")))
  6173. (setq link (org-make-link cpltxt)))
  6174. ((interactive-p)
  6175. (error "Cannot link to a buffer which is not visiting a file"))
  6176. (t (setq link nil)))
  6177. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6178. (setq link (or link cpltxt)
  6179. desc (or desc cpltxt))
  6180. (if (equal desc "NONE") (setq desc nil))
  6181. (if (and (interactive-p) link)
  6182. (progn
  6183. (setq org-stored-links
  6184. (cons (list link desc) org-stored-links))
  6185. (message "Stored: %s" (or desc link)))
  6186. (and link (org-make-link-string link desc)))))
  6187. (defun org-store-link-props (&rest plist)
  6188. "Store link properties, extract names and addresses."
  6189. (let (x adr)
  6190. (when (setq x (plist-get plist :from))
  6191. (setq adr (mail-extract-address-components x))
  6192. (plist-put plist :fromname (car adr))
  6193. (plist-put plist :fromaddress (nth 1 adr)))
  6194. (when (setq x (plist-get plist :to))
  6195. (setq adr (mail-extract-address-components x))
  6196. (plist-put plist :toname (car adr))
  6197. (plist-put plist :toaddress (nth 1 adr))))
  6198. (let ((from (plist-get plist :from))
  6199. (to (plist-get plist :to)))
  6200. (when (and from to org-from-is-user-regexp)
  6201. (plist-put plist :fromto
  6202. (if (string-match org-from-is-user-regexp from)
  6203. (concat "to %t")
  6204. (concat "from %f")))))
  6205. (setq org-store-link-plist plist))
  6206. (defun org-add-link-props (&rest plist)
  6207. "Add these properties to the link property list."
  6208. (let (key value)
  6209. (while plist
  6210. (setq key (pop plist) value (pop plist))
  6211. (setq org-store-link-plist
  6212. (plist-put org-store-link-plist key value)))))
  6213. (defun org-email-link-description (&optional fmt)
  6214. "Return the description part of an email link.
  6215. This takes information from `org-store-link-plist' and formats it
  6216. according to FMT (default from `org-email-link-description-format')."
  6217. (setq fmt (or fmt org-email-link-description-format))
  6218. (let* ((p org-store-link-plist)
  6219. (to (plist-get p :toaddress))
  6220. (from (plist-get p :fromaddress))
  6221. (table
  6222. (list
  6223. (cons "%c" (plist-get p :fromto))
  6224. (cons "%F" (plist-get p :from))
  6225. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6226. (cons "%T" (plist-get p :to))
  6227. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6228. (cons "%s" (plist-get p :subject))
  6229. (cons "%m" (plist-get p :message-id)))))
  6230. (when (string-match "%c" fmt)
  6231. ;; Check if the user wrote this message
  6232. (if (and org-from-is-user-regexp from to
  6233. (save-match-data (string-match org-from-is-user-regexp from)))
  6234. (setq fmt (replace-match "to %t" t t fmt))
  6235. (setq fmt (replace-match "from %f" t t fmt))))
  6236. (org-replace-escapes fmt table)))
  6237. (defun org-make-org-heading-search-string (&optional string heading)
  6238. "Make search string for STRING or current headline."
  6239. (interactive)
  6240. (let ((s (or string (org-get-heading))))
  6241. (unless (and string (not heading))
  6242. ;; We are using a headline, clean up garbage in there.
  6243. (if (string-match org-todo-regexp s)
  6244. (setq s (replace-match "" t t s)))
  6245. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6246. (setq s (replace-match "" t t s)))
  6247. (setq s (org-trim s))
  6248. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6249. org-comment-string "\\)") s)
  6250. (setq s (replace-match "" t t s)))
  6251. (while (string-match org-ts-regexp s)
  6252. (setq s (replace-match "" t t s))))
  6253. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6254. (setq s (replace-match " " t t s)))
  6255. (or string (setq s (concat "*" s))) ; Add * for headlines
  6256. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6257. (defun org-make-link (&rest strings)
  6258. "Concatenate STRINGS."
  6259. (apply 'concat strings))
  6260. (defun org-make-link-string (link &optional description)
  6261. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6262. (unless (string-match "\\S-" link)
  6263. (error "Empty link"))
  6264. (when (stringp description)
  6265. ;; Remove brackets from the description, they are fatal.
  6266. (while (string-match "\\[" description)
  6267. (setq description (replace-match "{" t t description)))
  6268. (while (string-match "\\]" description)
  6269. (setq description (replace-match "}" t t description))))
  6270. (when (equal (org-link-escape link) description)
  6271. ;; No description needed, it is identical
  6272. (setq description nil))
  6273. (when (and (not description)
  6274. (not (equal link (org-link-escape link))))
  6275. (setq description link))
  6276. (concat "[[" (org-link-escape link) "]"
  6277. (if description (concat "[" description "]") "")
  6278. "]"))
  6279. (defconst org-link-escape-chars
  6280. '((?\ . "%20")
  6281. (?\[ . "%5B")
  6282. (?\] . "%5D")
  6283. (?\340 . "%E0") ; `a
  6284. (?\342 . "%E2") ; ^a
  6285. (?\347 . "%E7") ; ,c
  6286. (?\350 . "%E8") ; `e
  6287. (?\351 . "%E9") ; 'e
  6288. (?\352 . "%EA") ; ^e
  6289. (?\356 . "%EE") ; ^i
  6290. (?\364 . "%F4") ; ^o
  6291. (?\371 . "%F9") ; `u
  6292. (?\373 . "%FB") ; ^u
  6293. (?\; . "%3B")
  6294. (?? . "%3F")
  6295. (?= . "%3D")
  6296. (?+ . "%2B")
  6297. )
  6298. "Association list of escapes for some characters problematic in links.
  6299. This is the list that is used for internal purposes.")
  6300. (defconst org-link-escape-chars-browser
  6301. '((?\ . "%20")) ; 32 for the SPC char
  6302. "Association list of escapes for some characters problematic in links.
  6303. This is the list that is used before handing over to the browser.")
  6304. (defun org-link-escape (text &optional table)
  6305. "Escape charaters in TEXT that are problematic for links."
  6306. (setq table (or table org-link-escape-chars))
  6307. (when text
  6308. (let ((re (mapconcat (lambda (x) (regexp-quote
  6309. (char-to-string (car x))))
  6310. table "\\|")))
  6311. (while (string-match re text)
  6312. (setq text
  6313. (replace-match
  6314. (cdr (assoc (string-to-char (match-string 0 text))
  6315. table))
  6316. t t text)))
  6317. text)))
  6318. (defun org-link-unescape (text &optional table)
  6319. "Reverse the action of `org-link-escape'."
  6320. (setq table (or table org-link-escape-chars))
  6321. (when text
  6322. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6323. table "\\|")))
  6324. (while (string-match re text)
  6325. (setq text
  6326. (replace-match
  6327. (char-to-string (car (rassoc (match-string 0 text) table)))
  6328. t t text)))
  6329. text)))
  6330. (defun org-xor (a b)
  6331. "Exclusive or."
  6332. (if a (not b) b))
  6333. (defun org-get-header (header)
  6334. "Find a header field in the current buffer."
  6335. (save-excursion
  6336. (goto-char (point-min))
  6337. (let ((case-fold-search t) s)
  6338. (cond
  6339. ((eq header 'from)
  6340. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  6341. (setq s (match-string 1)))
  6342. (while (string-match "\"" s)
  6343. (setq s (replace-match "" t t s)))
  6344. (if (string-match "[<(].*" s)
  6345. (setq s (replace-match "" t t s))))
  6346. ((eq header 'message-id)
  6347. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  6348. (setq s (match-string 1))))
  6349. ((eq header 'subject)
  6350. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  6351. (setq s (match-string 1)))))
  6352. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  6353. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  6354. s)))
  6355. (defun org-fixup-message-id-for-http (s)
  6356. "Replace special characters in a message id, so it can be used in an http query."
  6357. (while (string-match "<" s)
  6358. (setq s (replace-match "%3C" t t s)))
  6359. (while (string-match ">" s)
  6360. (setq s (replace-match "%3E" t t s)))
  6361. (while (string-match "@" s)
  6362. (setq s (replace-match "%40" t t s)))
  6363. s)
  6364. ;;;###autoload
  6365. (defun org-insert-link-global ()
  6366. "Insert a link like Org-mode does.
  6367. This command can be called in any mode to insert a link in Org-mode syntax."
  6368. (interactive)
  6369. (org-load-modules-maybe)
  6370. (org-run-like-in-org-mode 'org-insert-link))
  6371. (defun org-insert-link (&optional complete-file)
  6372. "Insert a link. At the prompt, enter the link.
  6373. Completion can be used to select a link previously stored with
  6374. `org-store-link'. When the empty string is entered (i.e. if you just
  6375. press RET at the prompt), the link defaults to the most recently
  6376. stored link. As SPC triggers completion in the minibuffer, you need to
  6377. use M-SPC or C-q SPC to force the insertion of a space character.
  6378. You will also be prompted for a description, and if one is given, it will
  6379. be displayed in the buffer instead of the link.
  6380. If there is already a link at point, this command will allow you to edit link
  6381. and description parts.
  6382. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
  6383. selected using completion. The path to the file will be relative to
  6384. the current directory if the file is in the current directory or a
  6385. subdirectory. Otherwise, the link will be the absolute path as
  6386. completed in the minibuffer (i.e. normally ~/path/to/file).
  6387. With two \\[universal-argument] prefixes, enforce an absolute path even if the file
  6388. is in the current directory or below.
  6389. With three \\[universal-argument] prefixes, negate the meaning of
  6390. `org-keep-stored-link-after-insertion'."
  6391. (interactive "P")
  6392. (let* ((wcf (current-window-configuration))
  6393. (region (if (org-region-active-p)
  6394. (buffer-substring (region-beginning) (region-end))))
  6395. (remove (and region (list (region-beginning) (region-end))))
  6396. (desc region)
  6397. tmphist ; byte-compile incorrectly complains about this
  6398. link entry file)
  6399. (cond
  6400. ((org-in-regexp org-bracket-link-regexp 1)
  6401. ;; We do have a link at point, and we are going to edit it.
  6402. (setq remove (list (match-beginning 0) (match-end 0)))
  6403. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6404. (setq link (read-string "Link: "
  6405. (org-link-unescape
  6406. (org-match-string-no-properties 1)))))
  6407. ((or (org-in-regexp org-angle-link-re)
  6408. (org-in-regexp org-plain-link-re))
  6409. ;; Convert to bracket link
  6410. (setq remove (list (match-beginning 0) (match-end 0))
  6411. link (read-string "Link: "
  6412. (org-remove-angle-brackets (match-string 0)))))
  6413. ((equal complete-file '(4))
  6414. ;; Completing read for file names.
  6415. (setq file (read-file-name "File: "))
  6416. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6417. (pwd1 (file-name-as-directory (abbreviate-file-name
  6418. (expand-file-name ".")))))
  6419. (cond
  6420. ((equal complete-file '(16))
  6421. (setq link (org-make-link
  6422. "file:"
  6423. (abbreviate-file-name (expand-file-name file)))))
  6424. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6425. (setq link (org-make-link "file:" (match-string 1 file))))
  6426. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6427. (expand-file-name file))
  6428. (setq link (org-make-link
  6429. "file:" (match-string 1 (expand-file-name file)))))
  6430. (t (setq link (org-make-link "file:" file))))))
  6431. (t
  6432. ;; Read link, with completion for stored links.
  6433. (with-output-to-temp-buffer "*Org Links*"
  6434. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6435. (when org-stored-links
  6436. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6437. (princ (mapconcat
  6438. (lambda (x)
  6439. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6440. (reverse org-stored-links) "\n"))))
  6441. (let ((cw (selected-window)))
  6442. (select-window (get-buffer-window "*Org Links*"))
  6443. (shrink-window-if-larger-than-buffer)
  6444. (setq truncate-lines t)
  6445. (select-window cw))
  6446. ;; Fake a link history, containing the stored links.
  6447. (setq tmphist (append (mapcar 'car org-stored-links)
  6448. org-insert-link-history))
  6449. (unwind-protect
  6450. (setq link (org-completing-read
  6451. "Link: "
  6452. (append
  6453. (mapcar (lambda (x) (list (concat (car x) ":")))
  6454. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6455. (mapcar (lambda (x) (list (concat x ":")))
  6456. org-link-types))
  6457. nil nil nil
  6458. 'tmphist
  6459. (or (car (car org-stored-links)))))
  6460. (set-window-configuration wcf)
  6461. (kill-buffer "*Org Links*"))
  6462. (setq entry (assoc link org-stored-links))
  6463. (or entry (push link org-insert-link-history))
  6464. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6465. (not org-keep-stored-link-after-insertion))
  6466. (setq org-stored-links (delq (assoc link org-stored-links)
  6467. org-stored-links)))
  6468. (setq desc (or desc (nth 1 entry)))))
  6469. (if (string-match org-plain-link-re link)
  6470. ;; URL-like link, normalize the use of angular brackets.
  6471. (setq link (org-make-link (org-remove-angle-brackets link))))
  6472. ;; Check if we are linking to the current file with a search option
  6473. ;; If yes, simplify the link by using only the search option.
  6474. (when (and buffer-file-name
  6475. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  6476. (let* ((path (match-string 1 link))
  6477. (case-fold-search nil)
  6478. (search (match-string 2 link)))
  6479. (save-match-data
  6480. (if (equal (file-truename buffer-file-name) (file-truename path))
  6481. ;; We are linking to this same file, with a search option
  6482. (setq link search)))))
  6483. ;; Check if we can/should use a relative path. If yes, simplify the link
  6484. (when (string-match "\\<file:\\(.*\\)" link)
  6485. (let* ((path (match-string 1 link))
  6486. (origpath path)
  6487. (case-fold-search nil))
  6488. (cond
  6489. ((eq org-link-file-path-type 'absolute)
  6490. (setq path (abbreviate-file-name (expand-file-name path))))
  6491. ((eq org-link-file-path-type 'noabbrev)
  6492. (setq path (expand-file-name path)))
  6493. ((eq org-link-file-path-type 'relative)
  6494. (setq path (file-relative-name path)))
  6495. (t
  6496. (save-match-data
  6497. (if (string-match (concat "^" (regexp-quote
  6498. (file-name-as-directory
  6499. (expand-file-name "."))))
  6500. (expand-file-name path))
  6501. ;; We are linking a file with relative path name.
  6502. (setq path (substring (expand-file-name path)
  6503. (match-end 0)))))))
  6504. (setq link (concat "file:" path))
  6505. (if (equal desc origpath)
  6506. (setq desc path))))
  6507. (setq desc (read-string "Description: " desc))
  6508. (unless (string-match "\\S-" desc) (setq desc nil))
  6509. (if remove (apply 'delete-region remove))
  6510. (insert (org-make-link-string link desc))))
  6511. (defun org-completing-read (&rest args)
  6512. (let ((minibuffer-local-completion-map
  6513. (copy-keymap minibuffer-local-completion-map)))
  6514. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6515. (apply 'completing-read args)))
  6516. ;;; Opening/following a link
  6517. (defvar org-link-search-failed nil)
  6518. (defun org-next-link ()
  6519. "Move forward to the next link.
  6520. If the link is in hidden text, expose it."
  6521. (interactive)
  6522. (when (and org-link-search-failed (eq this-command last-command))
  6523. (goto-char (point-min))
  6524. (message "Link search wrapped back to beginning of buffer"))
  6525. (setq org-link-search-failed nil)
  6526. (let* ((pos (point))
  6527. (ct (org-context))
  6528. (a (assoc :link ct)))
  6529. (if a (goto-char (nth 2 a)))
  6530. (if (re-search-forward org-any-link-re nil t)
  6531. (progn
  6532. (goto-char (match-beginning 0))
  6533. (if (org-invisible-p) (org-show-context)))
  6534. (goto-char pos)
  6535. (setq org-link-search-failed t)
  6536. (error "No further link found"))))
  6537. (defun org-previous-link ()
  6538. "Move backward to the previous link.
  6539. If the link is in hidden text, expose it."
  6540. (interactive)
  6541. (when (and org-link-search-failed (eq this-command last-command))
  6542. (goto-char (point-max))
  6543. (message "Link search wrapped back to end of buffer"))
  6544. (setq org-link-search-failed nil)
  6545. (let* ((pos (point))
  6546. (ct (org-context))
  6547. (a (assoc :link ct)))
  6548. (if a (goto-char (nth 1 a)))
  6549. (if (re-search-backward org-any-link-re nil t)
  6550. (progn
  6551. (goto-char (match-beginning 0))
  6552. (if (org-invisible-p) (org-show-context)))
  6553. (goto-char pos)
  6554. (setq org-link-search-failed t)
  6555. (error "No further link found"))))
  6556. (defun org-find-file-at-mouse (ev)
  6557. "Open file link or URL at mouse."
  6558. (interactive "e")
  6559. (mouse-set-point ev)
  6560. (org-open-at-point 'in-emacs))
  6561. (defun org-open-at-mouse (ev)
  6562. "Open file link or URL at mouse."
  6563. (interactive "e")
  6564. (mouse-set-point ev)
  6565. (org-open-at-point))
  6566. (defvar org-window-config-before-follow-link nil
  6567. "The window configuration before following a link.
  6568. This is saved in case the need arises to restore it.")
  6569. (defvar org-open-link-marker (make-marker)
  6570. "Marker pointing to the location where `org-open-at-point; was called.")
  6571. ;;;###autoload
  6572. (defun org-open-at-point-global ()
  6573. "Follow a link like Org-mode does.
  6574. This command can be called in any mode to follow a link that has
  6575. Org-mode syntax."
  6576. (interactive)
  6577. (org-run-like-in-org-mode 'org-open-at-point))
  6578. (defun org-open-at-point (&optional in-emacs)
  6579. "Open link at or after point.
  6580. If there is no link at point, this function will search forward up to
  6581. the end of the current subtree.
  6582. Normally, files will be opened by an appropriate application. If the
  6583. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  6584. (interactive "P")
  6585. (org-load-modules-maybe)
  6586. (move-marker org-open-link-marker (point))
  6587. (setq org-window-config-before-follow-link (current-window-configuration))
  6588. (org-remove-occur-highlights nil nil t)
  6589. (if (org-at-timestamp-p t)
  6590. (org-follow-timestamp-link)
  6591. (let (type path link line search (pos (point)))
  6592. (catch 'match
  6593. (save-excursion
  6594. (skip-chars-forward "^]\n\r")
  6595. (when (org-in-regexp org-bracket-link-regexp)
  6596. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  6597. (while (string-match " *\n *" link)
  6598. (setq link (replace-match " " t t link)))
  6599. (setq link (org-link-expand-abbrev link))
  6600. (if (string-match org-link-re-with-space2 link)
  6601. (setq type (match-string 1 link) path (match-string 2 link))
  6602. (setq type "thisfile" path link))
  6603. (throw 'match t)))
  6604. (when (get-text-property (point) 'org-linked-text)
  6605. (setq type "thisfile"
  6606. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6607. (1+ (point)) (point))
  6608. path (buffer-substring
  6609. (previous-single-property-change pos 'org-linked-text)
  6610. (next-single-property-change pos 'org-linked-text)))
  6611. (throw 'match t))
  6612. (save-excursion
  6613. (when (or (org-in-regexp org-angle-link-re)
  6614. (org-in-regexp org-plain-link-re))
  6615. (setq type (match-string 1) path (match-string 2))
  6616. (throw 'match t)))
  6617. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6618. (setq type "tree-match"
  6619. path (match-string 1))
  6620. (throw 'match t))
  6621. (save-excursion
  6622. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6623. (setq type "tags"
  6624. path (match-string 1))
  6625. (while (string-match ":" path)
  6626. (setq path (replace-match "+" t t path)))
  6627. (throw 'match t))))
  6628. (unless path
  6629. (error "No link found"))
  6630. ;; Remove any trailing spaces in path
  6631. (if (string-match " +\\'" path)
  6632. (setq path (replace-match "" t t path)))
  6633. (cond
  6634. ((assoc type org-link-protocols)
  6635. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6636. ((equal type "mailto")
  6637. (let ((cmd (car org-link-mailto-program))
  6638. (args (cdr org-link-mailto-program)) args1
  6639. (address path) (subject "") a)
  6640. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6641. (setq address (match-string 1 path)
  6642. subject (org-link-escape (match-string 2 path))))
  6643. (while args
  6644. (cond
  6645. ((not (stringp (car args))) (push (pop args) args1))
  6646. (t (setq a (pop args))
  6647. (if (string-match "%a" a)
  6648. (setq a (replace-match address t t a)))
  6649. (if (string-match "%s" a)
  6650. (setq a (replace-match subject t t a)))
  6651. (push a args1))))
  6652. (apply cmd (nreverse args1))))
  6653. ((member type '("http" "https" "ftp" "news"))
  6654. (browse-url (concat type ":" (org-link-escape
  6655. path org-link-escape-chars-browser))))
  6656. ((member type '("message"))
  6657. (browse-url (concat type ":" path)))
  6658. ((string= type "tags")
  6659. (org-tags-view in-emacs path))
  6660. ((string= type "thisfile")
  6661. (if in-emacs
  6662. (switch-to-buffer-other-window
  6663. (org-get-buffer-for-internal-link (current-buffer)))
  6664. (org-mark-ring-push))
  6665. (let ((cmd `(org-link-search
  6666. ,path
  6667. ,(cond ((equal in-emacs '(4)) 'occur)
  6668. ((equal in-emacs '(16)) 'org-occur)
  6669. (t nil))
  6670. ,pos)))
  6671. (condition-case nil (eval cmd)
  6672. (error (progn (widen) (eval cmd))))))
  6673. ((string= type "tree-match")
  6674. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6675. ((string= type "file")
  6676. (if (string-match "::\\([0-9]+\\)\\'" path)
  6677. (setq line (string-to-number (match-string 1 path))
  6678. path (substring path 0 (match-beginning 0)))
  6679. (if (string-match "::\\(.+\\)\\'" path)
  6680. (setq search (match-string 1 path)
  6681. path (substring path 0 (match-beginning 0)))))
  6682. (if (string-match "[*?{]" (file-name-nondirectory path))
  6683. (dired path)
  6684. (org-open-file path in-emacs line search)))
  6685. ((string= type "news")
  6686. (require 'org-gnus)
  6687. (org-gnus-follow-link path))
  6688. ((string= type "shell")
  6689. (let ((cmd path))
  6690. (if (or (not org-confirm-shell-link-function)
  6691. (funcall org-confirm-shell-link-function
  6692. (format "Execute \"%s\" in shell? "
  6693. (org-add-props cmd nil
  6694. 'face 'org-warning))))
  6695. (progn
  6696. (message "Executing %s" cmd)
  6697. (shell-command cmd))
  6698. (error "Abort"))))
  6699. ((string= type "elisp")
  6700. (let ((cmd path))
  6701. (if (or (not org-confirm-elisp-link-function)
  6702. (funcall org-confirm-elisp-link-function
  6703. (format "Execute \"%s\" as elisp? "
  6704. (org-add-props cmd nil
  6705. 'face 'org-warning))))
  6706. (message "%s => %s" cmd (eval (read cmd)))
  6707. (error "Abort"))))
  6708. (t
  6709. (browse-url-at-point)))))
  6710. (move-marker org-open-link-marker nil)
  6711. (run-hook-with-args 'org-follow-link-hook))
  6712. ;;; File search
  6713. (defvar org-create-file-search-functions nil
  6714. "List of functions to construct the right search string for a file link.
  6715. These functions are called in turn with point at the location to
  6716. which the link should point.
  6717. A function in the hook should first test if it would like to
  6718. handle this file type, for example by checking the major-mode or
  6719. the file extension. If it decides not to handle this file, it
  6720. should just return nil to give other functions a chance. If it
  6721. does handle the file, it must return the search string to be used
  6722. when following the link. The search string will be part of the
  6723. file link, given after a double colon, and `org-open-at-point'
  6724. will automatically search for it. If special measures must be
  6725. taken to make the search successful, another function should be
  6726. added to the companion hook `org-execute-file-search-functions',
  6727. which see.
  6728. A function in this hook may also use `setq' to set the variable
  6729. `description' to provide a suggestion for the descriptive text to
  6730. be used for this link when it gets inserted into an Org-mode
  6731. buffer with \\[org-insert-link].")
  6732. (defvar org-execute-file-search-functions nil
  6733. "List of functions to execute a file search triggered by a link.
  6734. Functions added to this hook must accept a single argument, the
  6735. search string that was part of the file link, the part after the
  6736. double colon. The function must first check if it would like to
  6737. handle this search, for example by checking the major-mode or the
  6738. file extension. If it decides not to handle this search, it
  6739. should just return nil to give other functions a chance. If it
  6740. does handle the search, it must return a non-nil value to keep
  6741. other functions from trying.
  6742. Each function can access the current prefix argument through the
  6743. variable `current-prefix-argument'. Note that a single prefix is
  6744. used to force opening a link in Emacs, so it may be good to only
  6745. use a numeric or double prefix to guide the search function.
  6746. In case this is needed, a function in this hook can also restore
  6747. the window configuration before `org-open-at-point' was called using:
  6748. (set-window-configuration org-window-config-before-follow-link)")
  6749. (defun org-link-search (s &optional type avoid-pos)
  6750. "Search for a link search option.
  6751. If S is surrounded by forward slashes, it is interpreted as a
  6752. regular expression. In org-mode files, this will create an `org-occur'
  6753. sparse tree. In ordinary files, `occur' will be used to list matches.
  6754. If the current buffer is in `dired-mode', grep will be used to search
  6755. in all files. If AVOID-POS is given, ignore matches near that position."
  6756. (let ((case-fold-search t)
  6757. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6758. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6759. (append '(("") (" ") ("\t") ("\n"))
  6760. org-emphasis-alist)
  6761. "\\|") "\\)"))
  6762. (pos (point))
  6763. (pre "") (post "")
  6764. words re0 re1 re2 re3 re4 re5 re2a reall)
  6765. (cond
  6766. ;; First check if there are any special
  6767. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6768. ;; Now try the builtin stuff
  6769. ((save-excursion
  6770. (goto-char (point-min))
  6771. (and
  6772. (re-search-forward
  6773. (concat "<<" (regexp-quote s0) ">>") nil t)
  6774. (setq pos (match-beginning 0))))
  6775. ;; There is an exact target for this
  6776. (goto-char pos))
  6777. ((string-match "^/\\(.*\\)/$" s)
  6778. ;; A regular expression
  6779. (cond
  6780. ((org-mode-p)
  6781. (org-occur (match-string 1 s)))
  6782. ;;((eq major-mode 'dired-mode)
  6783. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6784. (t (org-do-occur (match-string 1 s)))))
  6785. (t
  6786. ;; A normal search strings
  6787. (when (equal (string-to-char s) ?*)
  6788. ;; Anchor on headlines, post may include tags.
  6789. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6790. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6791. s (substring s 1)))
  6792. (remove-text-properties
  6793. 0 (length s)
  6794. '(face nil mouse-face nil keymap nil fontified nil) s)
  6795. ;; Make a series of regular expressions to find a match
  6796. (setq words (org-split-string s "[ \n\r\t]+")
  6797. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6798. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6799. "\\)" markers)
  6800. re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6801. re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6802. re1 (concat pre re2 post)
  6803. re3 (concat pre re4 post)
  6804. re5 (concat pre ".*" re4)
  6805. re2 (concat pre re2)
  6806. re2a (concat pre re2a)
  6807. re4 (concat pre re4)
  6808. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6809. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6810. re5 "\\)"
  6811. ))
  6812. (cond
  6813. ((eq type 'org-occur) (org-occur reall))
  6814. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6815. (t (goto-char (point-min))
  6816. (if (or (org-search-not-self 1 re0 nil t)
  6817. (org-search-not-self 1 re1 nil t)
  6818. (org-search-not-self 1 re2 nil t)
  6819. (org-search-not-self 1 re2a nil t)
  6820. (org-search-not-self 1 re3 nil t)
  6821. (org-search-not-self 1 re4 nil t)
  6822. (org-search-not-self 1 re5 nil t)
  6823. )
  6824. (goto-char (match-beginning 1))
  6825. (goto-char pos)
  6826. (error "No match")))))
  6827. (t
  6828. ;; Normal string-search
  6829. (goto-char (point-min))
  6830. (if (search-forward s nil t)
  6831. (goto-char (match-beginning 0))
  6832. (error "No match"))))
  6833. (and (org-mode-p) (org-show-context 'link-search))))
  6834. (defun org-search-not-self (group &rest args)
  6835. "Execute `re-search-forward', but only accept matches that do not
  6836. enclose the position of `org-open-link-marker'."
  6837. (let ((m org-open-link-marker))
  6838. (catch 'exit
  6839. (while (apply 're-search-forward args)
  6840. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6841. (goto-char (match-end group))
  6842. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6843. (> (match-beginning 0) (marker-position m))
  6844. (< (match-end 0) (marker-position m)))
  6845. (save-match-data
  6846. (or (not (org-in-regexp
  6847. org-bracket-link-analytic-regexp 1))
  6848. (not (match-end 4)) ; no description
  6849. (and (<= (match-beginning 4) (point))
  6850. (>= (match-end 4) (point))))))
  6851. (throw 'exit (point))))))))
  6852. (defun org-get-buffer-for-internal-link (buffer)
  6853. "Return a buffer to be used for displaying the link target of internal links."
  6854. (cond
  6855. ((not org-display-internal-link-with-indirect-buffer)
  6856. buffer)
  6857. ((string-match "(Clone)$" (buffer-name buffer))
  6858. (message "Buffer is already a clone, not making another one")
  6859. ;; we also do not modify visibility in this case
  6860. buffer)
  6861. (t ; make a new indirect buffer for displaying the link
  6862. (let* ((bn (buffer-name buffer))
  6863. (ibn (concat bn "(Clone)"))
  6864. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6865. (with-current-buffer ib (org-overview))
  6866. ib))))
  6867. (defun org-do-occur (regexp &optional cleanup)
  6868. "Call the Emacs command `occur'.
  6869. If CLEANUP is non-nil, remove the printout of the regular expression
  6870. in the *Occur* buffer. This is useful if the regex is long and not useful
  6871. to read."
  6872. (occur regexp)
  6873. (when cleanup
  6874. (let ((cwin (selected-window)) win beg end)
  6875. (when (setq win (get-buffer-window "*Occur*"))
  6876. (select-window win))
  6877. (goto-char (point-min))
  6878. (when (re-search-forward "match[a-z]+" nil t)
  6879. (setq beg (match-end 0))
  6880. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6881. (setq end (1- (match-beginning 0)))))
  6882. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6883. (goto-char (point-min))
  6884. (select-window cwin))))
  6885. ;;; The mark ring for links jumps
  6886. (defvar org-mark-ring nil
  6887. "Mark ring for positions before jumps in Org-mode.")
  6888. (defvar org-mark-ring-last-goto nil
  6889. "Last position in the mark ring used to go back.")
  6890. ;; Fill and close the ring
  6891. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6892. (loop for i from 1 to org-mark-ring-length do
  6893. (push (make-marker) org-mark-ring))
  6894. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6895. org-mark-ring)
  6896. (defun org-mark-ring-push (&optional pos buffer)
  6897. "Put the current position or POS into the mark ring and rotate it."
  6898. (interactive)
  6899. (setq pos (or pos (point)))
  6900. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6901. (move-marker (car org-mark-ring)
  6902. (or pos (point))
  6903. (or buffer (current-buffer)))
  6904. (message "%s"
  6905. (substitute-command-keys
  6906. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6907. (defun org-mark-ring-goto (&optional n)
  6908. "Jump to the previous position in the mark ring.
  6909. With prefix arg N, jump back that many stored positions. When
  6910. called several times in succession, walk through the entire ring.
  6911. Org-mode commands jumping to a different position in the current file,
  6912. or to another Org-mode file, automatically push the old position
  6913. onto the ring."
  6914. (interactive "p")
  6915. (let (p m)
  6916. (if (eq last-command this-command)
  6917. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6918. (setq p org-mark-ring))
  6919. (setq org-mark-ring-last-goto p)
  6920. (setq m (car p))
  6921. (switch-to-buffer (marker-buffer m))
  6922. (goto-char m)
  6923. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6924. (defun org-remove-angle-brackets (s)
  6925. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6926. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6927. s)
  6928. (defun org-add-angle-brackets (s)
  6929. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6930. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6931. s)
  6932. ;;; Following specific links
  6933. (defun org-follow-timestamp-link ()
  6934. (cond
  6935. ((org-at-date-range-p t)
  6936. (let ((org-agenda-start-on-weekday)
  6937. (t1 (match-string 1))
  6938. (t2 (match-string 2)))
  6939. (setq t1 (time-to-days (org-time-string-to-time t1))
  6940. t2 (time-to-days (org-time-string-to-time t2)))
  6941. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6942. ((org-at-timestamp-p t)
  6943. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6944. (substring (match-string 1) 0 10)))
  6945. 1))
  6946. (t (error "This should not happen"))))
  6947. ;;; BibTeX links
  6948. ;; Use the custom search meachnism to construct and use search strings for
  6949. ;; file links to BibTeX database entries.
  6950. (defun org-create-file-search-in-bibtex ()
  6951. "Create the search string and description for a BibTeX database entry."
  6952. (when (eq major-mode 'bibtex-mode)
  6953. ;; yes, we want to construct this search string.
  6954. ;; Make a good description for this entry, using names, year and the title
  6955. ;; Put it into the `description' variable which is dynamically scoped.
  6956. (let ((bibtex-autokey-names 1)
  6957. (bibtex-autokey-names-stretch 1)
  6958. (bibtex-autokey-name-case-convert-function 'identity)
  6959. (bibtex-autokey-name-separator " & ")
  6960. (bibtex-autokey-additional-names " et al.")
  6961. (bibtex-autokey-year-length 4)
  6962. (bibtex-autokey-name-year-separator " ")
  6963. (bibtex-autokey-titlewords 3)
  6964. (bibtex-autokey-titleword-separator " ")
  6965. (bibtex-autokey-titleword-case-convert-function 'identity)
  6966. (bibtex-autokey-titleword-length 'infty)
  6967. (bibtex-autokey-year-title-separator ": "))
  6968. (setq description (bibtex-generate-autokey)))
  6969. ;; Now parse the entry, get the key and return it.
  6970. (save-excursion
  6971. (bibtex-beginning-of-entry)
  6972. (cdr (assoc "=key=" (bibtex-parse-entry))))))
  6973. (defun org-execute-file-search-in-bibtex (s)
  6974. "Find the link search string S as a key for a database entry."
  6975. (when (eq major-mode 'bibtex-mode)
  6976. ;; Yes, we want to do the search in this file.
  6977. ;; We construct a regexp that searches for "@entrytype{" followed by the key
  6978. (goto-char (point-min))
  6979. (and (re-search-forward (concat "@[a-zA-Z]+[ \t\n]*{[ \t\n]*"
  6980. (regexp-quote s) "[ \t\n]*,") nil t)
  6981. (goto-char (match-beginning 0)))
  6982. (if (and (match-beginning 0) (equal current-prefix-arg '(16)))
  6983. ;; Use double prefix to indicate that any web link should be browsed
  6984. (let ((b (current-buffer)) (p (point)))
  6985. ;; Restore the window configuration because we just use the web link
  6986. (set-window-configuration org-window-config-before-follow-link)
  6987. (save-excursion (set-buffer b) (goto-char p)
  6988. (bibtex-url)))
  6989. (recenter 0)) ; Move entry start to beginning of window
  6990. ;; return t to indicate that the search is done.
  6991. t))
  6992. ;; Finally add the functions to the right hooks.
  6993. (add-hook 'org-create-file-search-functions 'org-create-file-search-in-bibtex)
  6994. (add-hook 'org-execute-file-search-functions 'org-execute-file-search-in-bibtex)
  6995. ;; end of Bibtex link setup
  6996. ;;; Following file links
  6997. (defun org-open-file (path &optional in-emacs line search)
  6998. "Open the file at PATH.
  6999. First, this expands any special file name abbreviations. Then the
  7000. configuration variable `org-file-apps' is checked if it contains an
  7001. entry for this file type, and if yes, the corresponding command is launched.
  7002. If no application is found, Emacs simply visits the file.
  7003. With optional argument IN-EMACS, Emacs will visit the file.
  7004. Optional LINE specifies a line to go to, optional SEARCH a string to
  7005. search for. If LINE or SEARCH is given, the file will always be
  7006. opened in Emacs.
  7007. If the file does not exist, an error is thrown."
  7008. (setq in-emacs (or in-emacs line search))
  7009. (let* ((file (if (equal path "")
  7010. buffer-file-name
  7011. (substitute-in-file-name (expand-file-name path))))
  7012. (apps (append org-file-apps (org-default-apps)))
  7013. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  7014. (dirp (if remp nil (file-directory-p file)))
  7015. (dfile (downcase file))
  7016. (old-buffer (current-buffer))
  7017. (old-pos (point))
  7018. (old-mode major-mode)
  7019. ext cmd)
  7020. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  7021. (setq ext (match-string 1 dfile))
  7022. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  7023. (setq ext (match-string 1 dfile))))
  7024. (if in-emacs
  7025. (setq cmd 'emacs)
  7026. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  7027. (and dirp (cdr (assoc 'directory apps)))
  7028. (cdr (assoc ext apps))
  7029. (cdr (assoc t apps)))))
  7030. (when (eq cmd 'mailcap)
  7031. (require 'mailcap)
  7032. (mailcap-parse-mailcaps)
  7033. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  7034. (command (mailcap-mime-info mime-type)))
  7035. (if (stringp command)
  7036. (setq cmd command)
  7037. (setq cmd 'emacs))))
  7038. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  7039. (not (file-exists-p file))
  7040. (not org-open-non-existing-files))
  7041. (error "No such file: %s" file))
  7042. (cond
  7043. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7044. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  7045. (while (string-match "['\"]%s['\"]" cmd)
  7046. (setq cmd (replace-match "%s" t t cmd)))
  7047. (while (string-match "%s" cmd)
  7048. (setq cmd (replace-match
  7049. (save-match-data (shell-quote-argument file))
  7050. t t cmd)))
  7051. (save-window-excursion
  7052. (start-process-shell-command cmd nil cmd)))
  7053. ((or (stringp cmd)
  7054. (eq cmd 'emacs))
  7055. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7056. (widen)
  7057. (if line (goto-line line)
  7058. (if search (org-link-search search))))
  7059. ((consp cmd)
  7060. (eval cmd))
  7061. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7062. (and (org-mode-p) (eq old-mode 'org-mode)
  7063. (or (not (equal old-buffer (current-buffer)))
  7064. (not (equal old-pos (point))))
  7065. (org-mark-ring-push old-pos old-buffer))))
  7066. (defun org-default-apps ()
  7067. "Return the default applications for this operating system."
  7068. (cond
  7069. ((eq system-type 'darwin)
  7070. org-file-apps-defaults-macosx)
  7071. ((eq system-type 'windows-nt)
  7072. org-file-apps-defaults-windowsnt)
  7073. (t org-file-apps-defaults-gnu)))
  7074. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7075. (defun org-file-remote-p (file)
  7076. "Test whether FILE specifies a location on a remote system.
  7077. Return non-nil if the location is indeed remote.
  7078. For example, the filename \"/user@host:/foo\" specifies a location
  7079. on the system \"/user@host:\"."
  7080. (cond ((fboundp 'file-remote-p)
  7081. (file-remote-p file))
  7082. ((fboundp 'tramp-handle-file-remote-p)
  7083. (tramp-handle-file-remote-p file))
  7084. ((and (boundp 'ange-ftp-name-format)
  7085. (string-match (car ange-ftp-name-format) file))
  7086. t)
  7087. (t nil)))
  7088. ;;;; Hooks for remember.el, and refiling
  7089. (defun org-get-org-file ()
  7090. "Read a filename, with default directory `org-directory'."
  7091. (let ((default (or org-default-notes-file remember-data-file)))
  7092. (read-file-name (format "File name [%s]: " default)
  7093. (file-name-as-directory org-directory)
  7094. default)))
  7095. (defun org-notes-order-reversed-p ()
  7096. "Check if the current file should receive notes in reversed order."
  7097. (cond
  7098. ((not org-reverse-note-order) nil)
  7099. ((eq t org-reverse-note-order) t)
  7100. ((not (listp org-reverse-note-order)) nil)
  7101. (t (catch 'exit
  7102. (let ((all org-reverse-note-order)
  7103. entry)
  7104. (while (setq entry (pop all))
  7105. (if (string-match (car entry) buffer-file-name)
  7106. (throw 'exit (cdr entry))))
  7107. nil)))))
  7108. ;;;; Refiling
  7109. (defvar org-refile-target-table nil
  7110. "The list of refile targets, created by `org-refile'.")
  7111. (defvar org-agenda-new-buffers nil
  7112. "Buffers created to visit agenda files.")
  7113. (defun org-get-refile-targets (&optional default-buffer)
  7114. "Produce a table with refile targets."
  7115. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  7116. targets txt re files f desc descre)
  7117. (with-current-buffer (or default-buffer (current-buffer))
  7118. (while (setq entry (pop entries))
  7119. (setq files (car entry) desc (cdr entry))
  7120. (cond
  7121. ((null files) (setq files (list (current-buffer))))
  7122. ((eq files 'org-agenda-files)
  7123. (setq files (org-agenda-files 'unrestricted)))
  7124. ((and (symbolp files) (fboundp files))
  7125. (setq files (funcall files)))
  7126. ((and (symbolp files) (boundp files))
  7127. (setq files (symbol-value files))))
  7128. (if (stringp files) (setq files (list files)))
  7129. (cond
  7130. ((eq (car desc) :tag)
  7131. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7132. ((eq (car desc) :todo)
  7133. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7134. ((eq (car desc) :regexp)
  7135. (setq descre (cdr desc)))
  7136. ((eq (car desc) :level)
  7137. (setq descre (concat "^\\*\\{" (number-to-string
  7138. (if org-odd-levels-only
  7139. (1- (* 2 (cdr desc)))
  7140. (cdr desc)))
  7141. "\\}[ \t]")))
  7142. ((eq (car desc) :maxlevel)
  7143. (setq descre (concat "^\\*\\{1," (number-to-string
  7144. (if org-odd-levels-only
  7145. (1- (* 2 (cdr desc)))
  7146. (cdr desc)))
  7147. "\\}[ \t]")))
  7148. (t (error "Bad refiling target description %s" desc)))
  7149. (while (setq f (pop files))
  7150. (save-excursion
  7151. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7152. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7153. (save-excursion
  7154. (save-restriction
  7155. (widen)
  7156. (goto-char (point-min))
  7157. (while (re-search-forward descre nil t)
  7158. (goto-char (point-at-bol))
  7159. (when (looking-at org-complex-heading-regexp)
  7160. (setq txt (match-string 4)
  7161. re (concat "^" (regexp-quote
  7162. (buffer-substring (match-beginning 1)
  7163. (match-end 4)))))
  7164. (if (match-end 5) (setq re (concat re "[ \t]+"
  7165. (regexp-quote
  7166. (match-string 5)))))
  7167. (setq re (concat re "[ \t]*$"))
  7168. (when org-refile-use-outline-path
  7169. (setq txt (mapconcat 'identity
  7170. (append
  7171. (if (eq org-refile-use-outline-path 'file)
  7172. (list (file-name-nondirectory
  7173. (buffer-file-name (buffer-base-buffer))))
  7174. (if (eq org-refile-use-outline-path 'full-file-path)
  7175. (list (buffer-file-name (buffer-base-buffer)))))
  7176. (org-get-outline-path)
  7177. (list txt))
  7178. "/")))
  7179. (push (list txt f re (point)) targets))
  7180. (goto-char (point-at-eol))))))))
  7181. (nreverse targets))))
  7182. (defun org-get-outline-path ()
  7183. "Return the outline path to the current entry, as a list."
  7184. (let (rtn)
  7185. (save-excursion
  7186. (while (org-up-heading-safe)
  7187. (when (looking-at org-complex-heading-regexp)
  7188. (push (org-match-string-no-properties 4) rtn)))
  7189. rtn)))
  7190. (defvar org-refile-history nil
  7191. "History for refiling operations.")
  7192. (defun org-refile (&optional goto default-buffer)
  7193. "Move the entry at point to another heading.
  7194. The list of target headings is compiled using the information in
  7195. `org-refile-targets', which see. This list is created upon first use, and
  7196. you can update it by calling this command with a double prefix (`C-u C-u').
  7197. FIXME: Can we find a better way of updating?
  7198. At the target location, the entry is filed as a subitem of the target heading.
  7199. Depending on `org-reverse-note-order', the new subitem will either be the
  7200. first of the last subitem.
  7201. With prefix arg GOTO, the command will only visit the target location,
  7202. not actually move anything.
  7203. With a double prefix `C-c C-c', go to the location where the last refiling
  7204. operation has put the subtree.
  7205. With a double prefix argument, the command can be used to jump to any
  7206. heading in the current buffer."
  7207. (interactive "P")
  7208. (let* ((cbuf (current-buffer))
  7209. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7210. pos it nbuf file re level reversed)
  7211. (if (equal goto '(16))
  7212. (org-refile-goto-last-stored)
  7213. (when (setq it (org-refile-get-location
  7214. (if goto "Goto: " "Refile to: ") default-buffer))
  7215. (setq file (nth 1 it)
  7216. re (nth 2 it)
  7217. pos (nth 3 it))
  7218. (setq nbuf (or (find-buffer-visiting file)
  7219. (find-file-noselect file)))
  7220. (if goto
  7221. (progn
  7222. (switch-to-buffer nbuf)
  7223. (goto-char pos)
  7224. (org-show-context 'org-goto))
  7225. (org-copy-special)
  7226. (save-excursion
  7227. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7228. (find-file-noselect file))))
  7229. (setq reversed (org-notes-order-reversed-p))
  7230. (save-excursion
  7231. (save-restriction
  7232. (widen)
  7233. (goto-char pos)
  7234. (looking-at outline-regexp)
  7235. (setq level (org-get-valid-level (funcall outline-level) 1))
  7236. (goto-char
  7237. (if reversed
  7238. (outline-next-heading)
  7239. (or (save-excursion (outline-get-next-sibling))
  7240. (org-end-of-subtree t t)
  7241. (point-max))))
  7242. (bookmark-set "org-refile-last-stored")
  7243. (org-paste-subtree level))))
  7244. (org-cut-special)
  7245. (message "Entry refiled to \"%s\"" (car it)))))))
  7246. (defun org-refile-goto-last-stored ()
  7247. "Go to the location where the last refile was stored."
  7248. (interactive)
  7249. (bookmark-jump "org-refile-last-stored")
  7250. (message "This is the location of the last refile"))
  7251. (defun org-refile-get-location (&optional prompt default-buffer)
  7252. "Prompt the user for a refile location, using PROMPT."
  7253. (let ((org-refile-targets org-refile-targets)
  7254. (org-refile-use-outline-path org-refile-use-outline-path))
  7255. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7256. (unless org-refile-target-table
  7257. (error "No refile targets"))
  7258. (let* ((cbuf (current-buffer))
  7259. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7260. (fname (and filename (file-truename filename)))
  7261. (tbl (mapcar
  7262. (lambda (x)
  7263. (if (not (equal fname (file-truename (nth 1 x))))
  7264. (cons (concat (car x) " (" (file-name-nondirectory
  7265. (nth 1 x)) ")")
  7266. (cdr x))
  7267. x))
  7268. org-refile-target-table))
  7269. (completion-ignore-case t))
  7270. (assoc (completing-read prompt tbl nil t nil 'org-refile-history)
  7271. tbl)))
  7272. ;;;; Dynamic blocks
  7273. (defun org-find-dblock (name)
  7274. "Find the first dynamic block with name NAME in the buffer.
  7275. If not found, stay at current position and return nil."
  7276. (let (pos)
  7277. (save-excursion
  7278. (goto-char (point-min))
  7279. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7280. nil t)
  7281. (match-beginning 0))))
  7282. (if pos (goto-char pos))
  7283. pos))
  7284. (defconst org-dblock-start-re
  7285. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7286. "Matches the startline of a dynamic block, with parameters.")
  7287. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7288. "Matches the end of a dyhamic block.")
  7289. (defun org-create-dblock (plist)
  7290. "Create a dynamic block section, with parameters taken from PLIST.
  7291. PLIST must containe a :name entry which is used as name of the block."
  7292. (unless (bolp) (newline))
  7293. (let ((name (plist-get plist :name)))
  7294. (insert "#+BEGIN: " name)
  7295. (while plist
  7296. (if (eq (car plist) :name)
  7297. (setq plist (cddr plist))
  7298. (insert " " (prin1-to-string (pop plist)))))
  7299. (insert "\n\n#+END:\n")
  7300. (beginning-of-line -2)))
  7301. (defun org-prepare-dblock ()
  7302. "Prepare dynamic block for refresh.
  7303. This empties the block, puts the cursor at the insert position and returns
  7304. the property list including an extra property :name with the block name."
  7305. (unless (looking-at org-dblock-start-re)
  7306. (error "Not at a dynamic block"))
  7307. (let* ((begdel (1+ (match-end 0)))
  7308. (name (org-no-properties (match-string 1)))
  7309. (params (append (list :name name)
  7310. (read (concat "(" (match-string 3) ")")))))
  7311. (unless (re-search-forward org-dblock-end-re nil t)
  7312. (error "Dynamic block not terminated"))
  7313. (setq params
  7314. (append params
  7315. (list :content (buffer-substring
  7316. begdel (match-beginning 0)))))
  7317. (delete-region begdel (match-beginning 0))
  7318. (goto-char begdel)
  7319. (open-line 1)
  7320. params))
  7321. (defun org-map-dblocks (&optional command)
  7322. "Apply COMMAND to all dynamic blocks in the current buffer.
  7323. If COMMAND is not given, use `org-update-dblock'."
  7324. (let ((cmd (or command 'org-update-dblock))
  7325. pos)
  7326. (save-excursion
  7327. (goto-char (point-min))
  7328. (while (re-search-forward org-dblock-start-re nil t)
  7329. (goto-char (setq pos (match-beginning 0)))
  7330. (condition-case nil
  7331. (funcall cmd)
  7332. (error (message "Error during update of dynamic block")))
  7333. (goto-char pos)
  7334. (unless (re-search-forward org-dblock-end-re nil t)
  7335. (error "Dynamic block not terminated"))))))
  7336. (defun org-dblock-update (&optional arg)
  7337. "User command for updating dynamic blocks.
  7338. Update the dynamic block at point. With prefix ARG, update all dynamic
  7339. blocks in the buffer."
  7340. (interactive "P")
  7341. (if arg
  7342. (org-update-all-dblocks)
  7343. (or (looking-at org-dblock-start-re)
  7344. (org-beginning-of-dblock))
  7345. (org-update-dblock)))
  7346. (defun org-update-dblock ()
  7347. "Update the dynamic block at point
  7348. This means to empty the block, parse for parameters and then call
  7349. the correct writing function."
  7350. (save-window-excursion
  7351. (let* ((pos (point))
  7352. (line (org-current-line))
  7353. (params (org-prepare-dblock))
  7354. (name (plist-get params :name))
  7355. (cmd (intern (concat "org-dblock-write:" name))))
  7356. (message "Updating dynamic block `%s' at line %d..." name line)
  7357. (funcall cmd params)
  7358. (message "Updating dynamic block `%s' at line %d...done" name line)
  7359. (goto-char pos))))
  7360. (defun org-beginning-of-dblock ()
  7361. "Find the beginning of the dynamic block at point.
  7362. Error if there is no scuh block at point."
  7363. (let ((pos (point))
  7364. beg)
  7365. (end-of-line 1)
  7366. (if (and (re-search-backward org-dblock-start-re nil t)
  7367. (setq beg (match-beginning 0))
  7368. (re-search-forward org-dblock-end-re nil t)
  7369. (> (match-end 0) pos))
  7370. (goto-char beg)
  7371. (goto-char pos)
  7372. (error "Not in a dynamic block"))))
  7373. (defun org-update-all-dblocks ()
  7374. "Update all dynamic blocks in the buffer.
  7375. This function can be used in a hook."
  7376. (when (org-mode-p)
  7377. (org-map-dblocks 'org-update-dblock)))
  7378. ;;;; Completion
  7379. (defconst org-additional-option-like-keywords
  7380. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7381. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7382. "BEGIN_EXAMPLE" "END_EXAMPLE"))
  7383. (defun org-complete (&optional arg)
  7384. "Perform completion on word at point.
  7385. At the beginning of a headline, this completes TODO keywords as given in
  7386. `org-todo-keywords'.
  7387. If the current word is preceded by a backslash, completes the TeX symbols
  7388. that are supported for HTML support.
  7389. If the current word is preceded by \"#+\", completes special words for
  7390. setting file options.
  7391. In the line after \"#+STARTUP:, complete valid keywords.\"
  7392. At all other locations, this simply calls the value of
  7393. `org-completion-fallback-command'."
  7394. (interactive "P")
  7395. (org-without-partial-completion
  7396. (catch 'exit
  7397. (let* ((end (point))
  7398. (beg1 (save-excursion
  7399. (skip-chars-backward (org-re "[:alnum:]_@"))
  7400. (point)))
  7401. (beg (save-excursion
  7402. (skip-chars-backward "a-zA-Z0-9_:$")
  7403. (point)))
  7404. (confirm (lambda (x) (stringp (car x))))
  7405. (searchhead (equal (char-before beg) ?*))
  7406. (tag (and (equal (char-before beg1) ?:)
  7407. (equal (char-after (point-at-bol)) ?*)))
  7408. (prop (and (equal (char-before beg1) ?:)
  7409. (not (equal (char-after (point-at-bol)) ?*))))
  7410. (texp (equal (char-before beg) ?\\))
  7411. (link (equal (char-before beg) ?\[))
  7412. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7413. beg)
  7414. "#+"))
  7415. (startup (string-match "^#\\+STARTUP:.*"
  7416. (buffer-substring (point-at-bol) (point))))
  7417. (completion-ignore-case opt)
  7418. (type nil)
  7419. (tbl nil)
  7420. (table (cond
  7421. (opt
  7422. (setq type :opt)
  7423. (require 'org-exp)
  7424. (append
  7425. (mapcar
  7426. (lambda (x)
  7427. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7428. (cons (match-string 2 x) (match-string 1 x)))
  7429. (org-split-string (org-get-current-options) "\n"))
  7430. (mapcar 'list org-additional-option-like-keywords)))
  7431. (startup
  7432. (setq type :startup)
  7433. org-startup-options)
  7434. (link (append org-link-abbrev-alist-local
  7435. org-link-abbrev-alist))
  7436. (texp
  7437. (setq type :tex)
  7438. org-html-entities)
  7439. ((string-match "\\`\\*+[ \t]+\\'"
  7440. (buffer-substring (point-at-bol) beg))
  7441. (setq type :todo)
  7442. (mapcar 'list org-todo-keywords-1))
  7443. (searchhead
  7444. (setq type :searchhead)
  7445. (save-excursion
  7446. (goto-char (point-min))
  7447. (while (re-search-forward org-todo-line-regexp nil t)
  7448. (push (list
  7449. (org-make-org-heading-search-string
  7450. (match-string 3) t))
  7451. tbl)))
  7452. tbl)
  7453. (tag (setq type :tag beg beg1)
  7454. (or org-tag-alist (org-get-buffer-tags)))
  7455. (prop (setq type :prop beg beg1)
  7456. (mapcar 'list (org-buffer-property-keys nil t t)))
  7457. (t (progn
  7458. (call-interactively org-completion-fallback-command)
  7459. (throw 'exit nil)))))
  7460. (pattern (buffer-substring-no-properties beg end))
  7461. (completion (try-completion pattern table confirm)))
  7462. (cond ((eq completion t)
  7463. (if (not (assoc (upcase pattern) table))
  7464. (message "Already complete")
  7465. (if (and (equal type :opt)
  7466. (not (member (car (assoc (upcase pattern) table))
  7467. org-additional-option-like-keywords)))
  7468. (insert (substring (cdr (assoc (upcase pattern) table))
  7469. (length pattern)))
  7470. (if (memq type '(:tag :prop)) (insert ":")))))
  7471. ((null completion)
  7472. (message "Can't find completion for \"%s\"" pattern)
  7473. (ding))
  7474. ((not (string= pattern completion))
  7475. (delete-region beg end)
  7476. (if (string-match " +$" completion)
  7477. (setq completion (replace-match "" t t completion)))
  7478. (insert completion)
  7479. (if (get-buffer-window "*Completions*")
  7480. (delete-window (get-buffer-window "*Completions*")))
  7481. (if (assoc completion table)
  7482. (if (eq type :todo) (insert " ")
  7483. (if (memq type '(:tag :prop)) (insert ":"))))
  7484. (if (and (equal type :opt) (assoc completion table))
  7485. (message "%s" (substitute-command-keys
  7486. "Press \\[org-complete] again to insert example settings"))))
  7487. (t
  7488. (message "Making completion list...")
  7489. (let ((list (sort (all-completions pattern table confirm)
  7490. 'string<)))
  7491. (with-output-to-temp-buffer "*Completions*"
  7492. (condition-case nil
  7493. ;; Protection needed for XEmacs and emacs 21
  7494. (display-completion-list list pattern)
  7495. (error (display-completion-list list)))))
  7496. (message "Making completion list...%s" "done")))))))
  7497. ;;;; TODO, DEADLINE, Comments
  7498. (defun org-toggle-comment ()
  7499. "Change the COMMENT state of an entry."
  7500. (interactive)
  7501. (save-excursion
  7502. (org-back-to-heading)
  7503. (let (case-fold-search)
  7504. (if (looking-at (concat outline-regexp
  7505. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7506. (replace-match "" t t nil 1)
  7507. (if (looking-at outline-regexp)
  7508. (progn
  7509. (goto-char (match-end 0))
  7510. (insert org-comment-string " ")))))))
  7511. (defvar org-last-todo-state-is-todo nil
  7512. "This is non-nil when the last TODO state change led to a TODO state.
  7513. If the last change removed the TODO tag or switched to DONE, then
  7514. this is nil.")
  7515. (defvar org-setting-tags nil) ; dynamically skiped
  7516. ;; FIXME: better place
  7517. (defun org-property-or-variable-value (var &optional inherit)
  7518. "Check if there is a property fixing the value of VAR.
  7519. If yes, return this value. If not, return the current value of the variable."
  7520. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  7521. (if (and prop (stringp prop) (string-match "\\S-" prop))
  7522. (read prop)
  7523. (symbol-value var))))
  7524. (defun org-parse-local-options (string var)
  7525. "Parse STRING for startup setting relevant for variable VAR."
  7526. (let ((rtn (symbol-value var))
  7527. e opts)
  7528. (save-match-data
  7529. (if (or (not string) (not (string-match "\\S-" string)))
  7530. rtn
  7531. (setq opts (delq nil (mapcar (lambda (x)
  7532. (setq e (assoc x org-startup-options))
  7533. (if (eq (nth 1 e) var) e nil))
  7534. (org-split-string string "[ \t]+"))))
  7535. (if (not opts)
  7536. rtn
  7537. (setq rtn nil)
  7538. (while (setq e (pop opts))
  7539. (if (not (nth 3 e))
  7540. (setq rtn (nth 2 e))
  7541. (if (not (listp rtn)) (setq rtn nil))
  7542. (push (nth 2 e) rtn)))
  7543. rtn)))))
  7544. (defvar org-blocker-hook nil
  7545. "Hook for functions that are allowed to block a state change.
  7546. Each function gets as its single argument a property list, see
  7547. `org-trigger-hook' for more information about this list.
  7548. If any of the functions in this hook returns nil, the state change
  7549. is blocked.")
  7550. (defvar org-trigger-hook nil
  7551. "Hook for functions that are triggered by a state change.
  7552. Each function gets as its single argument a property list with at least
  7553. the following elements:
  7554. (:type type-of-change :position pos-at-entry-start
  7555. :from old-state :to new-state)
  7556. Depending on the type, more properties may be present.
  7557. This mechanism is currently implemented for:
  7558. TODO state changes
  7559. ------------------
  7560. :type todo-state-change
  7561. :from previous state (keyword as a string), or nil
  7562. :to new state (keyword as a string), or nil")
  7563. (defun org-todo (&optional arg)
  7564. "Change the TODO state of an item.
  7565. The state of an item is given by a keyword at the start of the heading,
  7566. like
  7567. *** TODO Write paper
  7568. *** DONE Call mom
  7569. The different keywords are specified in the variable `org-todo-keywords'.
  7570. By default the available states are \"TODO\" and \"DONE\".
  7571. So for this example: when the item starts with TODO, it is changed to DONE.
  7572. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7573. DONE are present, add TODO at the beginning of the heading.
  7574. With C-u prefix arg, use completion to determine the new state.
  7575. With numeric prefix arg, switch to that state.
  7576. For calling through lisp, arg is also interpreted in the following way:
  7577. 'none -> empty state
  7578. \"\"(empty string) -> switch to empty state
  7579. 'done -> switch to DONE
  7580. 'nextset -> switch to the next set of keywords
  7581. 'previousset -> switch to the previous set of keywords
  7582. \"WAITING\" -> switch to the specified keyword, but only if it
  7583. really is a member of `org-todo-keywords'."
  7584. (interactive "P")
  7585. (save-excursion
  7586. (catch 'exit
  7587. (org-back-to-heading)
  7588. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7589. (or (looking-at (concat " +" org-todo-regexp " *"))
  7590. (looking-at " *"))
  7591. (let* ((match-data (match-data))
  7592. (startpos (point-at-bol))
  7593. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7594. (org-log-done org-log-done)
  7595. (org-log-repeat org-log-repeat)
  7596. (org-todo-log-states org-todo-log-states)
  7597. (this (match-string 1))
  7598. (hl-pos (match-beginning 0))
  7599. (head (org-get-todo-sequence-head this))
  7600. (ass (assoc head org-todo-kwd-alist))
  7601. (interpret (nth 1 ass))
  7602. (done-word (nth 3 ass))
  7603. (final-done-word (nth 4 ass))
  7604. (last-state (or this ""))
  7605. (completion-ignore-case t)
  7606. (member (member this org-todo-keywords-1))
  7607. (tail (cdr member))
  7608. (state (cond
  7609. ((and org-todo-key-trigger
  7610. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  7611. (and (not arg) org-use-fast-todo-selection
  7612. (not (eq org-use-fast-todo-selection 'prefix)))))
  7613. ;; Use fast selection
  7614. (org-fast-todo-selection))
  7615. ((and (equal arg '(4))
  7616. (or (not org-use-fast-todo-selection)
  7617. (not org-todo-key-trigger)))
  7618. ;; Read a state with completion
  7619. (completing-read "State: " (mapcar (lambda(x) (list x))
  7620. org-todo-keywords-1)
  7621. nil t))
  7622. ((eq arg 'right)
  7623. (if this
  7624. (if tail (car tail) nil)
  7625. (car org-todo-keywords-1)))
  7626. ((eq arg 'left)
  7627. (if (equal member org-todo-keywords-1)
  7628. nil
  7629. (if this
  7630. (nth (- (length org-todo-keywords-1) (length tail) 2)
  7631. org-todo-keywords-1)
  7632. (org-last org-todo-keywords-1))))
  7633. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7634. (setq arg nil))) ; hack to fall back to cycling
  7635. (arg
  7636. ;; user or caller requests a specific state
  7637. (cond
  7638. ((equal arg "") nil)
  7639. ((eq arg 'none) nil)
  7640. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7641. ((eq arg 'nextset)
  7642. (or (car (cdr (member head org-todo-heads)))
  7643. (car org-todo-heads)))
  7644. ((eq arg 'previousset)
  7645. (let ((org-todo-heads (reverse org-todo-heads)))
  7646. (or (car (cdr (member head org-todo-heads)))
  7647. (car org-todo-heads))))
  7648. ((car (member arg org-todo-keywords-1)))
  7649. ((nth (1- (prefix-numeric-value arg))
  7650. org-todo-keywords-1))))
  7651. ((null member) (or head (car org-todo-keywords-1)))
  7652. ((equal this final-done-word) nil) ;; -> make empty
  7653. ((null tail) nil) ;; -> first entry
  7654. ((eq interpret 'sequence)
  7655. (car tail))
  7656. ((memq interpret '(type priority))
  7657. (if (eq this-command last-command)
  7658. (car tail)
  7659. (if (> (length tail) 0)
  7660. (or done-word (car org-done-keywords))
  7661. nil)))
  7662. (t nil)))
  7663. (next (if state (concat " " state " ") " "))
  7664. (change-plist (list :type 'todo-state-change :from this :to state
  7665. :position startpos))
  7666. dolog now-done-p)
  7667. (when org-blocker-hook
  7668. (unless (save-excursion
  7669. (save-match-data
  7670. (run-hook-with-args-until-failure
  7671. 'org-blocker-hook change-plist)))
  7672. (if (interactive-p)
  7673. (error "TODO state change from %s to %s blocked" this state)
  7674. ;; fail silently
  7675. (message "TODO state change from %s to %s blocked" this state)
  7676. (throw 'exit nil))))
  7677. (store-match-data match-data)
  7678. (replace-match next t t)
  7679. (unless (pos-visible-in-window-p hl-pos)
  7680. (message "TODO state changed to %s" (org-trim next)))
  7681. (unless head
  7682. (setq head (org-get-todo-sequence-head state)
  7683. ass (assoc head org-todo-kwd-alist)
  7684. interpret (nth 1 ass)
  7685. done-word (nth 3 ass)
  7686. final-done-word (nth 4 ass)))
  7687. (when (memq arg '(nextset previousset))
  7688. (message "Keyword-Set %d/%d: %s"
  7689. (- (length org-todo-sets) -1
  7690. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7691. (length org-todo-sets)
  7692. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7693. (setq org-last-todo-state-is-todo
  7694. (not (member state org-done-keywords)))
  7695. (setq now-done-p (and (member state org-done-keywords)
  7696. (not (member this org-done-keywords))))
  7697. (and logging (org-local-logging logging))
  7698. (when (and (or org-todo-log-states org-log-done)
  7699. (not (memq arg '(nextset previousset))))
  7700. ;; we need to look at recording a time and note
  7701. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7702. (nth 2 (assoc this org-todo-log-states))))
  7703. (when (and state
  7704. (member state org-not-done-keywords)
  7705. (not (member this org-not-done-keywords)))
  7706. ;; This is now a todo state and was not one before
  7707. ;; If there was a CLOSED time stamp, get rid of it.
  7708. (org-add-planning-info nil nil 'closed))
  7709. (when (and now-done-p org-log-done)
  7710. ;; It is now done, and it was not done before
  7711. (org-add-planning-info 'closed (org-current-time))
  7712. (if (and (not dolog) (eq 'note org-log-done))
  7713. (org-add-log-maybe 'done state 'findpos 'note)))
  7714. (when (and state dolog)
  7715. ;; This is a non-nil state, and we need to log it
  7716. (org-add-log-maybe 'state state 'findpos dolog)))
  7717. ;; Fixup tag positioning
  7718. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7719. (run-hooks 'org-after-todo-state-change-hook)
  7720. (if (and arg (not (member state org-done-keywords)))
  7721. (setq head (org-get-todo-sequence-head state)))
  7722. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7723. ;; Do we need to trigger a repeat?
  7724. (when now-done-p (org-auto-repeat-maybe state))
  7725. ;; Fixup cursor location if close to the keyword
  7726. (if (and (outline-on-heading-p)
  7727. (not (bolp))
  7728. (save-excursion (beginning-of-line 1)
  7729. (looking-at org-todo-line-regexp))
  7730. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7731. (progn
  7732. (goto-char (or (match-end 2) (match-end 1)))
  7733. (just-one-space)))
  7734. (when org-trigger-hook
  7735. (save-excursion
  7736. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  7737. (defun org-local-logging (value)
  7738. "Get logging settings from a property VALUE."
  7739. (let* (words w a)
  7740. ;; directly set the variables, they are already local.
  7741. (setq org-log-done nil
  7742. org-log-repeat nil
  7743. org-todo-log-states nil)
  7744. (setq words (org-split-string value))
  7745. (while (setq w (pop words))
  7746. (cond
  7747. ((setq a (assoc w org-startup-options))
  7748. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7749. (set (nth 1 a) (nth 2 a))))
  7750. ((setq a (org-extract-log-state-settings w))
  7751. (and (member (car a) org-todo-keywords-1)
  7752. (push a org-todo-log-states)))))))
  7753. (defun org-get-todo-sequence-head (kwd)
  7754. "Return the head of the TODO sequence to which KWD belongs.
  7755. If KWD is not set, check if there is a text property remembering the
  7756. right sequence."
  7757. (let (p)
  7758. (cond
  7759. ((not kwd)
  7760. (or (get-text-property (point-at-bol) 'org-todo-head)
  7761. (progn
  7762. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  7763. nil (point-at-eol)))
  7764. (get-text-property p 'org-todo-head))))
  7765. ((not (member kwd org-todo-keywords-1))
  7766. (car org-todo-keywords-1))
  7767. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  7768. (defun org-fast-todo-selection ()
  7769. "Fast TODO keyword selection with single keys.
  7770. Returns the new TODO keyword, or nil if no state change should occur."
  7771. (let* ((fulltable org-todo-key-alist)
  7772. (done-keywords org-done-keywords) ;; needed for the faces.
  7773. (maxlen (apply 'max (mapcar
  7774. (lambda (x)
  7775. (if (stringp (car x)) (string-width (car x)) 0))
  7776. fulltable)))
  7777. (expert nil)
  7778. (fwidth (+ maxlen 3 1 3))
  7779. (ncol (/ (- (window-width) 4) fwidth))
  7780. tg cnt e c tbl
  7781. groups ingroup)
  7782. (save-window-excursion
  7783. (if expert
  7784. (set-buffer (get-buffer-create " *Org todo*"))
  7785. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  7786. (erase-buffer)
  7787. (org-set-local 'org-done-keywords done-keywords)
  7788. (setq tbl fulltable cnt 0)
  7789. (while (setq e (pop tbl))
  7790. (cond
  7791. ((equal e '(:startgroup))
  7792. (push '() groups) (setq ingroup t)
  7793. (when (not (= cnt 0))
  7794. (setq cnt 0)
  7795. (insert "\n"))
  7796. (insert "{ "))
  7797. ((equal e '(:endgroup))
  7798. (setq ingroup nil cnt 0)
  7799. (insert "}\n"))
  7800. (t
  7801. (setq tg (car e) c (cdr e))
  7802. (if ingroup (push tg (car groups)))
  7803. (setq tg (org-add-props tg nil 'face
  7804. (org-get-todo-face tg)))
  7805. (if (and (= cnt 0) (not ingroup)) (insert " "))
  7806. (insert "[" c "] " tg (make-string
  7807. (- fwidth 4 (length tg)) ?\ ))
  7808. (when (= (setq cnt (1+ cnt)) ncol)
  7809. (insert "\n")
  7810. (if ingroup (insert " "))
  7811. (setq cnt 0)))))
  7812. (insert "\n")
  7813. (goto-char (point-min))
  7814. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  7815. (fit-window-to-buffer))
  7816. (message "[a-z..]:Set [SPC]:clear")
  7817. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  7818. (cond
  7819. ((or (= c ?\C-g)
  7820. (and (= c ?q) (not (rassoc c fulltable))))
  7821. (setq quit-flag t))
  7822. ((= c ?\ ) nil)
  7823. ((setq e (rassoc c fulltable) tg (car e))
  7824. tg)
  7825. (t (setq quit-flag t))))))
  7826. (defun org-entry-is-todo-p ()
  7827. (member (org-get-todo-state) org-not-done-keywords))
  7828. (defun org-entry-is-done-p ()
  7829. (member (org-get-todo-state) org-done-keywords))
  7830. (defun org-get-todo-state ()
  7831. (save-excursion
  7832. (org-back-to-heading t)
  7833. (and (looking-at org-todo-line-regexp)
  7834. (match-end 2)
  7835. (match-string 2))))
  7836. (defun org-at-date-range-p (&optional inactive-ok)
  7837. "Is the cursor inside a date range?"
  7838. (interactive)
  7839. (save-excursion
  7840. (catch 'exit
  7841. (let ((pos (point)))
  7842. (skip-chars-backward "^[<\r\n")
  7843. (skip-chars-backward "<[")
  7844. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7845. (>= (match-end 0) pos)
  7846. (throw 'exit t))
  7847. (skip-chars-backward "^<[\r\n")
  7848. (skip-chars-backward "<[")
  7849. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7850. (>= (match-end 0) pos)
  7851. (throw 'exit t)))
  7852. nil)))
  7853. (defun org-get-repeat ()
  7854. "Check if tere is a deadline/schedule with repeater in this entry."
  7855. (save-match-data
  7856. (save-excursion
  7857. (org-back-to-heading t)
  7858. (if (re-search-forward
  7859. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  7860. (match-string 1)))))
  7861. (defvar org-last-changed-timestamp)
  7862. (defvar org-log-post-message)
  7863. (defvar org-log-note-purpose)
  7864. (defun org-auto-repeat-maybe (done-word)
  7865. "Check if the current headline contains a repeated deadline/schedule.
  7866. If yes, set TODO state back to what it was and change the base date
  7867. of repeating deadline/scheduled time stamps to new date.
  7868. This function is run automatically after each state change to a DONE state."
  7869. ;; last-state is dynamically scoped into this function
  7870. (let* ((repeat (org-get-repeat))
  7871. (aa (assoc last-state org-todo-kwd-alist))
  7872. (interpret (nth 1 aa))
  7873. (head (nth 2 aa))
  7874. (whata '(("d" . day) ("m" . month) ("y" . year)))
  7875. (msg "Entry repeats: ")
  7876. (org-log-done nil)
  7877. (org-todo-log-states nil)
  7878. (nshiftmax 10) (nshift 0)
  7879. re type n what ts mb0 time)
  7880. (when repeat
  7881. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  7882. (org-todo (if (eq interpret 'type) last-state head))
  7883. (when (and org-log-repeat
  7884. (or (not (memq 'org-add-log-note
  7885. (default-value 'post-command-hook)))
  7886. (eq org-log-note-purpose 'done)))
  7887. ;; Make sure a note is taken;
  7888. (org-add-log-maybe 'state (or done-word (car org-done-keywords))
  7889. 'findpos org-log-repeat))
  7890. (org-back-to-heading t)
  7891. (org-add-planning-info nil nil 'closed)
  7892. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  7893. org-deadline-time-regexp "\\)\\|\\("
  7894. org-ts-regexp "\\)"))
  7895. (while (re-search-forward
  7896. re (save-excursion (outline-next-heading) (point)) t)
  7897. (setq type (if (match-end 1) org-scheduled-string
  7898. (if (match-end 3) org-deadline-string "Plain:"))
  7899. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0)))
  7900. mb0 (match-beginning 0))
  7901. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  7902. (setq n (string-to-number (match-string 2 ts))
  7903. what (match-string 3 ts))
  7904. (if (equal what "w") (setq n (* n 7) what "d"))
  7905. ;; Preparation, see if we need to modify the start date for the change
  7906. (when (match-end 1)
  7907. (setq time (save-match-data (org-time-string-to-time ts)))
  7908. (cond
  7909. ((equal (match-string 1 ts) ".")
  7910. ;; Shift starting date to today
  7911. (org-timestamp-change
  7912. (- (time-to-days (current-time)) (time-to-days time))
  7913. 'day))
  7914. ((equal (match-string 1 ts) "+")
  7915. (while (< (time-to-days time) (time-to-days (current-time)))
  7916. (when (= (incf nshift) nshiftmax)
  7917. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  7918. (error "Abort")))
  7919. (org-timestamp-change n (cdr (assoc what whata)))
  7920. (sit-for .0001) ;; so we can watch the date shifting
  7921. (org-at-timestamp-p t)
  7922. (setq ts (match-string 1))
  7923. (setq time (save-match-data (org-time-string-to-time ts))))
  7924. (org-timestamp-change (- n) (cdr (assoc what whata)))
  7925. ;; rematch, so that we have everything in place for the real shift
  7926. (org-at-timestamp-p t)
  7927. (setq ts (match-string 1))
  7928. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  7929. (org-timestamp-change n (cdr (assoc what whata)))
  7930. (setq msg (concat msg type org-last-changed-timestamp " "))))
  7931. (setq org-log-post-message msg)
  7932. (message "%s" msg))))
  7933. (defun org-show-todo-tree (arg)
  7934. "Make a compact tree which shows all headlines marked with TODO.
  7935. The tree will show the lines where the regexp matches, and all higher
  7936. headlines above the match.
  7937. With a \\[universal-argument] prefix, also show the DONE entries.
  7938. With a numeric prefix N, construct a sparse tree for the Nth element
  7939. of `org-todo-keywords-1'."
  7940. (interactive "P")
  7941. (let ((case-fold-search nil)
  7942. (kwd-re
  7943. (cond ((null arg) org-not-done-regexp)
  7944. ((equal arg '(4))
  7945. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  7946. (mapcar 'list org-todo-keywords-1))))
  7947. (concat "\\("
  7948. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  7949. "\\)\\>")))
  7950. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  7951. (regexp-quote (nth (1- (prefix-numeric-value arg))
  7952. org-todo-keywords-1)))
  7953. (t (error "Invalid prefix argument: %s" arg)))))
  7954. (message "%d TODO entries found"
  7955. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  7956. (defun org-deadline (&optional remove)
  7957. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  7958. With argument REMOVE, remove any deadline from the item."
  7959. (interactive "P")
  7960. (if remove
  7961. (progn
  7962. (org-remove-timestamp-with-keyword org-deadline-string)
  7963. (message "Item no longer has a deadline."))
  7964. (org-add-planning-info 'deadline nil 'closed)))
  7965. (defun org-schedule (&optional remove)
  7966. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  7967. With argument REMOVE, remove any scheduling date from the item."
  7968. (interactive "P")
  7969. (if remove
  7970. (progn
  7971. (org-remove-timestamp-with-keyword org-scheduled-string)
  7972. (message "Item is no longer scheduled."))
  7973. (org-add-planning-info 'scheduled nil 'closed)))
  7974. (defun org-remove-timestamp-with-keyword (keyword)
  7975. "Remove all time stamps with KEYWORD in the current entry."
  7976. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  7977. beg)
  7978. (save-excursion
  7979. (org-back-to-heading t)
  7980. (setq beg (point))
  7981. (org-end-of-subtree t t)
  7982. (while (re-search-backward re beg t)
  7983. (replace-match "")
  7984. (unless (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  7985. (delete-region (point-at-bol) (min (1+ (point)) (point-max))))))))
  7986. (defun org-add-planning-info (what &optional time &rest remove)
  7987. "Insert new timestamp with keyword in the line directly after the headline.
  7988. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  7989. If non is given, the user is prompted for a date.
  7990. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  7991. be removed."
  7992. (interactive)
  7993. (let (org-time-was-given org-end-time-was-given ts
  7994. end default-time default-input)
  7995. (when (and (not time) (memq what '(scheduled deadline)))
  7996. ;; Try to get a default date/time from existing timestamp
  7997. (save-excursion
  7998. (org-back-to-heading t)
  7999. (setq end (save-excursion (outline-next-heading) (point)))
  8000. (when (re-search-forward (if (eq what 'scheduled)
  8001. org-scheduled-time-regexp
  8002. org-deadline-time-regexp)
  8003. end t)
  8004. (setq ts (match-string 1)
  8005. default-time
  8006. (apply 'encode-time (org-parse-time-string ts))
  8007. default-input (and ts (org-get-compact-tod ts))))))
  8008. (when what
  8009. ;; If necessary, get the time from the user
  8010. (setq time (or time (org-read-date nil 'to-time nil nil
  8011. default-time default-input))))
  8012. (when (and org-insert-labeled-timestamps-at-point
  8013. (member what '(scheduled deadline)))
  8014. (insert
  8015. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  8016. (org-insert-time-stamp time org-time-was-given
  8017. nil nil nil (list org-end-time-was-given))
  8018. (setq what nil))
  8019. (save-excursion
  8020. (save-restriction
  8021. (let (col list elt ts buffer-invisibility-spec)
  8022. (org-back-to-heading t)
  8023. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  8024. (goto-char (match-end 1))
  8025. (setq col (current-column))
  8026. (goto-char (match-end 0))
  8027. (if (eobp) (insert "\n") (forward-char 1))
  8028. (if (and (not (looking-at outline-regexp))
  8029. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  8030. "[^\r\n]*"))
  8031. (not (equal (match-string 1) org-clock-string)))
  8032. (narrow-to-region (match-beginning 0) (match-end 0))
  8033. (insert-before-markers "\n")
  8034. (backward-char 1)
  8035. (narrow-to-region (point) (point))
  8036. (indent-to-column col))
  8037. ;; Check if we have to remove something.
  8038. (setq list (cons what remove))
  8039. (while list
  8040. (setq elt (pop list))
  8041. (goto-char (point-min))
  8042. (when (or (and (eq elt 'scheduled)
  8043. (re-search-forward org-scheduled-time-regexp nil t))
  8044. (and (eq elt 'deadline)
  8045. (re-search-forward org-deadline-time-regexp nil t))
  8046. (and (eq elt 'closed)
  8047. (re-search-forward org-closed-time-regexp nil t)))
  8048. (replace-match "")
  8049. (if (looking-at "--+<[^>]+>") (replace-match ""))
  8050. (if (looking-at " +") (replace-match ""))))
  8051. (goto-char (point-max))
  8052. (when what
  8053. (insert
  8054. (if (not (equal (char-before) ?\ )) " " "")
  8055. (cond ((eq what 'scheduled) org-scheduled-string)
  8056. ((eq what 'deadline) org-deadline-string)
  8057. ((eq what 'closed) org-closed-string))
  8058. " ")
  8059. (setq ts (org-insert-time-stamp
  8060. time
  8061. (or org-time-was-given
  8062. (and (eq what 'closed) org-log-done-with-time))
  8063. (eq what 'closed)
  8064. nil nil (list org-end-time-was-given)))
  8065. (end-of-line 1))
  8066. (goto-char (point-min))
  8067. (widen)
  8068. (if (looking-at "[ \t]+\r?\n")
  8069. (replace-match ""))
  8070. ts)))))
  8071. (defvar org-log-note-marker (make-marker))
  8072. (defvar org-log-note-purpose nil)
  8073. (defvar org-log-note-state nil)
  8074. (defvar org-log-note-how nil)
  8075. (defvar org-log-note-window-configuration nil)
  8076. (defvar org-log-note-return-to (make-marker))
  8077. (defvar org-log-post-message nil
  8078. "Message to be displayed after a log note has been stored.
  8079. The auto-repeater uses this.")
  8080. (defun org-add-log-maybe (&optional purpose state findpos how)
  8081. "Set up the post command hook to take a note.
  8082. If this is about to TODO state change, the new state is expected in STATE.
  8083. When FINDPOS is non-nil, find the correct position for the note in
  8084. the current entry. If not, assume that it can be inserted at point."
  8085. (save-excursion
  8086. (when findpos
  8087. (org-back-to-heading t)
  8088. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  8089. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  8090. "[^\r\n]*\\)?"))
  8091. (goto-char (match-end 0))
  8092. (unless org-log-states-order-reversed
  8093. (and (= (char-after) ?\n) (forward-char 1))
  8094. (org-skip-over-state-notes)
  8095. (skip-chars-backward " \t\n\r")))
  8096. (move-marker org-log-note-marker (point))
  8097. (setq org-log-note-purpose purpose
  8098. org-log-note-state state
  8099. org-log-note-how how)
  8100. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  8101. (defun org-skip-over-state-notes ()
  8102. "Skip past the list of State notes in an entry."
  8103. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  8104. (while (looking-at "[ \t]*- State")
  8105. (condition-case nil
  8106. (org-next-item)
  8107. (error (org-end-of-item)))))
  8108. (defun org-add-log-note (&optional purpose)
  8109. "Pop up a window for taking a note, and add this note later at point."
  8110. (remove-hook 'post-command-hook 'org-add-log-note)
  8111. (setq org-log-note-window-configuration (current-window-configuration))
  8112. (delete-other-windows)
  8113. (move-marker org-log-note-return-to (point))
  8114. (switch-to-buffer (marker-buffer org-log-note-marker))
  8115. (goto-char org-log-note-marker)
  8116. (org-switch-to-buffer-other-window "*Org Note*")
  8117. (erase-buffer)
  8118. (if (memq org-log-note-how '(time state)) ; FIXME: time or state????????????
  8119. (org-store-log-note)
  8120. (let ((org-inhibit-startup t)) (org-mode))
  8121. (insert (format "# Insert note for %s.
  8122. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  8123. (cond
  8124. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  8125. ((eq org-log-note-purpose 'done) "closed todo item")
  8126. ((eq org-log-note-purpose 'state)
  8127. (format "state change to \"%s\"" org-log-note-state))
  8128. (t (error "This should not happen")))))
  8129. (org-set-local 'org-finish-function 'org-store-log-note)))
  8130. (defvar org-note-abort nil) ; dynamically scoped
  8131. (defun org-store-log-note ()
  8132. "Finish taking a log note, and insert it to where it belongs."
  8133. (let ((txt (buffer-string))
  8134. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  8135. lines ind)
  8136. (kill-buffer (current-buffer))
  8137. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  8138. (setq txt (replace-match "" t t txt)))
  8139. (if (string-match "\\s-+\\'" txt)
  8140. (setq txt (replace-match "" t t txt)))
  8141. (setq lines (org-split-string txt "\n"))
  8142. (when (and note (string-match "\\S-" note))
  8143. (setq note
  8144. (org-replace-escapes
  8145. note
  8146. (list (cons "%u" (user-login-name))
  8147. (cons "%U" user-full-name)
  8148. (cons "%t" (format-time-string
  8149. (org-time-stamp-format 'long 'inactive)
  8150. (current-time)))
  8151. (cons "%s" (if org-log-note-state
  8152. (concat "\"" org-log-note-state "\"")
  8153. "")))))
  8154. (if lines (setq note (concat note " \\\\")))
  8155. (push note lines))
  8156. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  8157. (when lines
  8158. (save-excursion
  8159. (set-buffer (marker-buffer org-log-note-marker))
  8160. (save-excursion
  8161. (goto-char org-log-note-marker)
  8162. (move-marker org-log-note-marker nil)
  8163. (end-of-line 1)
  8164. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  8165. (indent-relative nil)
  8166. (insert "- " (pop lines))
  8167. (org-indent-line-function)
  8168. (beginning-of-line 1)
  8169. (looking-at "[ \t]*")
  8170. (setq ind (concat (match-string 0) " "))
  8171. (end-of-line 1)
  8172. (while lines (insert "\n" ind (pop lines)))))))
  8173. (set-window-configuration org-log-note-window-configuration)
  8174. (with-current-buffer (marker-buffer org-log-note-return-to)
  8175. (goto-char org-log-note-return-to))
  8176. (move-marker org-log-note-return-to nil)
  8177. (and org-log-post-message (message "%s" org-log-post-message)))
  8178. ;; FIXME: what else would be useful?
  8179. ;; - priority
  8180. ;; - date
  8181. (defun org-sparse-tree (&optional arg)
  8182. "Create a sparse tree, prompt for the details.
  8183. This command can create sparse trees. You first need to select the type
  8184. of match used to create the tree:
  8185. t Show entries with a specific TODO keyword.
  8186. T Show entries selected by a tags match.
  8187. p Enter a property name and its value (both with completion on existing
  8188. names/values) and show entries with that property.
  8189. r Show entries matching a regular expression
  8190. d Show deadlines due within `org-deadline-warning-days'."
  8191. (interactive "P")
  8192. (let (ans kwd value)
  8193. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  8194. (setq ans (read-char-exclusive))
  8195. (cond
  8196. ((equal ans ?d)
  8197. (call-interactively 'org-check-deadlines))
  8198. ((equal ans ?b)
  8199. (call-interactively 'org-check-before-date))
  8200. ((equal ans ?t)
  8201. (org-show-todo-tree '(4)))
  8202. ((equal ans ?T)
  8203. (call-interactively 'org-tags-sparse-tree))
  8204. ((member ans '(?p ?P))
  8205. (setq kwd (completing-read "Property: "
  8206. (mapcar 'list (org-buffer-property-keys))))
  8207. (setq value (completing-read "Value: "
  8208. (mapcar 'list (org-property-values kwd))))
  8209. (unless (string-match "\\`{.*}\\'" value)
  8210. (setq value (concat "\"" value "\"")))
  8211. (org-tags-sparse-tree arg (concat kwd "=" value)))
  8212. ((member ans '(?r ?R ?/))
  8213. (call-interactively 'org-occur))
  8214. (t (error "No such sparse tree command \"%c\"" ans)))))
  8215. (defvar org-occur-highlights nil
  8216. "List of overlays used for occur matches.")
  8217. (make-variable-buffer-local 'org-occur-highlights)
  8218. (defvar org-occur-parameters nil
  8219. "Parameters of the active org-occur calls.
  8220. This is a list, each call to org-occur pushes as cons cell,
  8221. containing the regular expression and the callback, onto the list.
  8222. The list can contain several entries if `org-occur' has been called
  8223. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  8224. will only contain one set of parameters. When the highlights are
  8225. removed (for example with `C-c C-c', or with the next edit (depending
  8226. on `org-remove-highlights-with-change'), this variable is emptied
  8227. as well.")
  8228. (make-variable-buffer-local 'org-occur-parameters)
  8229. (defun org-occur (regexp &optional keep-previous callback)
  8230. "Make a compact tree which shows all matches of REGEXP.
  8231. The tree will show the lines where the regexp matches, and all higher
  8232. headlines above the match. It will also show the heading after the match,
  8233. to make sure editing the matching entry is easy.
  8234. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  8235. call to `org-occur' will be kept, to allow stacking of calls to this
  8236. command.
  8237. If CALLBACK is non-nil, it is a function which is called to confirm
  8238. that the match should indeed be shown."
  8239. (interactive "sRegexp: \nP")
  8240. (unless keep-previous
  8241. (org-remove-occur-highlights nil nil t))
  8242. (push (cons regexp callback) org-occur-parameters)
  8243. (let ((cnt 0))
  8244. (save-excursion
  8245. (goto-char (point-min))
  8246. (if (or (not keep-previous) ; do not want to keep
  8247. (not org-occur-highlights)) ; no previous matches
  8248. ;; hide everything
  8249. (org-overview))
  8250. (while (re-search-forward regexp nil t)
  8251. (when (or (not callback)
  8252. (save-match-data (funcall callback)))
  8253. (setq cnt (1+ cnt))
  8254. (when org-highlight-sparse-tree-matches
  8255. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  8256. (org-show-context 'occur-tree))))
  8257. (when org-remove-highlights-with-change
  8258. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  8259. nil 'local))
  8260. (unless org-sparse-tree-open-archived-trees
  8261. (org-hide-archived-subtrees (point-min) (point-max)))
  8262. (run-hooks 'org-occur-hook)
  8263. (if (interactive-p)
  8264. (message "%d match(es) for regexp %s" cnt regexp))
  8265. cnt))
  8266. (defun org-show-context (&optional key)
  8267. "Make sure point and context and visible.
  8268. How much context is shown depends upon the variables
  8269. `org-show-hierarchy-above', `org-show-following-heading'. and
  8270. `org-show-siblings'."
  8271. (let ((heading-p (org-on-heading-p t))
  8272. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  8273. (following-p (org-get-alist-option org-show-following-heading key))
  8274. (entry-p (org-get-alist-option org-show-entry-below key))
  8275. (siblings-p (org-get-alist-option org-show-siblings key)))
  8276. (catch 'exit
  8277. ;; Show heading or entry text
  8278. (if (and heading-p (not entry-p))
  8279. (org-flag-heading nil) ; only show the heading
  8280. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  8281. (org-show-hidden-entry))) ; show entire entry
  8282. (when following-p
  8283. ;; Show next sibling, or heading below text
  8284. (save-excursion
  8285. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  8286. (org-flag-heading nil))))
  8287. (when siblings-p (org-show-siblings))
  8288. (when hierarchy-p
  8289. ;; show all higher headings, possibly with siblings
  8290. (save-excursion
  8291. (while (and (condition-case nil
  8292. (progn (org-up-heading-all 1) t)
  8293. (error nil))
  8294. (not (bobp)))
  8295. (org-flag-heading nil)
  8296. (when siblings-p (org-show-siblings))))))))
  8297. (defun org-reveal (&optional siblings)
  8298. "Show current entry, hierarchy above it, and the following headline.
  8299. This can be used to show a consistent set of context around locations
  8300. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  8301. not t for the search context.
  8302. With optional argument SIBLINGS, on each level of the hierarchy all
  8303. siblings are shown. This repairs the tree structure to what it would
  8304. look like when opened with hierarchical calls to `org-cycle'."
  8305. (interactive "P")
  8306. (let ((org-show-hierarchy-above t)
  8307. (org-show-following-heading t)
  8308. (org-show-siblings (if siblings t org-show-siblings)))
  8309. (org-show-context nil)))
  8310. (defun org-highlight-new-match (beg end)
  8311. "Highlight from BEG to END and mark the highlight is an occur headline."
  8312. (let ((ov (org-make-overlay beg end)))
  8313. (org-overlay-put ov 'face 'secondary-selection)
  8314. (push ov org-occur-highlights)))
  8315. (defun org-remove-occur-highlights (&optional beg end noremove)
  8316. "Remove the occur highlights from the buffer.
  8317. BEG and END are ignored. If NOREMOVE is nil, remove this function
  8318. from the `before-change-functions' in the current buffer."
  8319. (interactive)
  8320. (unless org-inhibit-highlight-removal
  8321. (mapc 'org-delete-overlay org-occur-highlights)
  8322. (setq org-occur-highlights nil)
  8323. (setq org-occur-parameters nil)
  8324. (unless noremove
  8325. (remove-hook 'before-change-functions
  8326. 'org-remove-occur-highlights 'local))))
  8327. ;;;; Priorities
  8328. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  8329. "Regular expression matching the priority indicator.")
  8330. (defvar org-remove-priority-next-time nil)
  8331. (defun org-priority-up ()
  8332. "Increase the priority of the current item."
  8333. (interactive)
  8334. (org-priority 'up))
  8335. (defun org-priority-down ()
  8336. "Decrease the priority of the current item."
  8337. (interactive)
  8338. (org-priority 'down))
  8339. (defun org-priority (&optional action)
  8340. "Change the priority of an item by ARG.
  8341. ACTION can be `set', `up', `down', or a character."
  8342. (interactive)
  8343. (setq action (or action 'set))
  8344. (let (current new news have remove)
  8345. (save-excursion
  8346. (org-back-to-heading)
  8347. (if (looking-at org-priority-regexp)
  8348. (setq current (string-to-char (match-string 2))
  8349. have t)
  8350. (setq current org-default-priority))
  8351. (cond
  8352. ((or (eq action 'set) (integerp action))
  8353. (if (integerp action)
  8354. (setq new action)
  8355. (message "Priority %c-%c, SPC to remove: " org-highest-priority org-lowest-priority)
  8356. (setq new (read-char-exclusive)))
  8357. (if (and (= (upcase org-highest-priority) org-highest-priority)
  8358. (= (upcase org-lowest-priority) org-lowest-priority))
  8359. (setq new (upcase new)))
  8360. (cond ((equal new ?\ ) (setq remove t))
  8361. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  8362. (error "Priority must be between `%c' and `%c'"
  8363. org-highest-priority org-lowest-priority))))
  8364. ((eq action 'up)
  8365. (if (and (not have) (eq last-command this-command))
  8366. (setq new org-lowest-priority)
  8367. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8368. org-default-priority (1- current)))))
  8369. ((eq action 'down)
  8370. (if (and (not have) (eq last-command this-command))
  8371. (setq new org-highest-priority)
  8372. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8373. org-default-priority (1+ current)))))
  8374. (t (error "Invalid action")))
  8375. (if (or (< (upcase new) org-highest-priority)
  8376. (> (upcase new) org-lowest-priority))
  8377. (setq remove t))
  8378. (setq news (format "%c" new))
  8379. (if have
  8380. (if remove
  8381. (replace-match "" t t nil 1)
  8382. (replace-match news t t nil 2))
  8383. (if remove
  8384. (error "No priority cookie found in line")
  8385. (looking-at org-todo-line-regexp)
  8386. (if (match-end 2)
  8387. (progn
  8388. (goto-char (match-end 2))
  8389. (insert " [#" news "]"))
  8390. (goto-char (match-beginning 3))
  8391. (insert "[#" news "] ")))))
  8392. (org-preserve-lc (org-set-tags nil 'align))
  8393. (if remove
  8394. (message "Priority removed")
  8395. (message "Priority of current item set to %s" news))))
  8396. (defun org-get-priority (s)
  8397. "Find priority cookie and return priority."
  8398. (save-match-data
  8399. (if (not (string-match org-priority-regexp s))
  8400. (* 1000 (- org-lowest-priority org-default-priority))
  8401. (* 1000 (- org-lowest-priority
  8402. (string-to-char (match-string 2 s)))))))
  8403. ;;;; Tags
  8404. (defun org-scan-tags (action matcher &optional todo-only)
  8405. "Scan headline tags with inheritance and produce output ACTION.
  8406. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  8407. evaluated, testing if a given set of tags qualifies a headline for
  8408. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  8409. are included in the output."
  8410. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8411. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  8412. (org-re
  8413. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  8414. (props (list 'face nil
  8415. 'done-face 'org-done
  8416. 'undone-face nil
  8417. 'mouse-face 'highlight
  8418. 'org-not-done-regexp org-not-done-regexp
  8419. 'org-todo-regexp org-todo-regexp
  8420. 'keymap org-agenda-keymap
  8421. 'help-echo
  8422. (format "mouse-2 or RET jump to org file %s"
  8423. (abbreviate-file-name
  8424. (or (buffer-file-name (buffer-base-buffer))
  8425. (buffer-name (buffer-base-buffer)))))))
  8426. (case-fold-search nil)
  8427. lspos
  8428. tags tags-list tags-alist (llast 0) rtn level category i txt
  8429. todo marker entry priority)
  8430. (save-excursion
  8431. (goto-char (point-min))
  8432. (when (eq action 'sparse-tree)
  8433. (org-overview)
  8434. (org-remove-occur-highlights))
  8435. (while (re-search-forward re nil t)
  8436. (catch :skip
  8437. (setq todo (if (match-end 1) (match-string 2))
  8438. tags (if (match-end 4) (match-string 4)))
  8439. (goto-char (setq lspos (1+ (match-beginning 0))))
  8440. (setq level (org-reduced-level (funcall outline-level))
  8441. category (org-get-category))
  8442. (setq i llast llast level)
  8443. ;; remove tag lists from same and sublevels
  8444. (while (>= i level)
  8445. (when (setq entry (assoc i tags-alist))
  8446. (setq tags-alist (delete entry tags-alist)))
  8447. (setq i (1- i)))
  8448. ;; add the next tags
  8449. (when tags
  8450. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8451. tags-alist
  8452. (cons (cons level tags) tags-alist)))
  8453. ;; compile tags for current headline
  8454. (setq tags-list
  8455. (if org-use-tag-inheritance
  8456. (apply 'append (mapcar 'cdr tags-alist))
  8457. tags))
  8458. (when (and tags org-use-tag-inheritance
  8459. (not (eq t org-use-tag-inheritance)))
  8460. ;; selective inheritance, remove uninherited ones
  8461. (setcdr (car tags-alist)
  8462. (org-remove-uniherited-tags (cdar tags-alist))))
  8463. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  8464. (eval matcher)
  8465. (or (not org-agenda-skip-archived-trees)
  8466. (not (member org-archive-tag tags-list))))
  8467. (and (eq action 'agenda) (org-agenda-skip))
  8468. ;; list this headline
  8469. (if (eq action 'sparse-tree)
  8470. (progn
  8471. (and org-highlight-sparse-tree-matches
  8472. (org-get-heading) (match-end 0)
  8473. (org-highlight-new-match
  8474. (match-beginning 0) (match-beginning 1)))
  8475. (org-show-context 'tags-tree))
  8476. (setq txt (org-format-agenda-item
  8477. ""
  8478. (concat
  8479. (if org-tags-match-list-sublevels
  8480. (make-string (1- level) ?.) "")
  8481. (org-get-heading))
  8482. category tags-list)
  8483. priority (org-get-priority txt))
  8484. (goto-char lspos)
  8485. (setq marker (org-agenda-new-marker))
  8486. (org-add-props txt props
  8487. 'org-marker marker 'org-hd-marker marker 'org-category category
  8488. 'priority priority 'type "tagsmatch")
  8489. (push txt rtn))
  8490. ;; if we are to skip sublevels, jump to end of subtree
  8491. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8492. (when (and (eq action 'sparse-tree)
  8493. (not org-sparse-tree-open-archived-trees))
  8494. (org-hide-archived-subtrees (point-min) (point-max)))
  8495. (nreverse rtn)))
  8496. (defun org-remove-uniherited-tags (tags)
  8497. "Remove all tags that are not inherited from the list TAGS."
  8498. (cond
  8499. ((eq org-use-tag-inheritance t) tags)
  8500. ((not org-use-tag-inheritance) nil)
  8501. ((stringp org-use-tag-inheritance)
  8502. (delq nil (mapcar
  8503. (lambda (x) (if (string-match org-use-tag-inheritance x) x nil))
  8504. tags)))
  8505. ((listp org-use-tag-inheritance)
  8506. (org-delete-all org-use-tag-inheritance tags))))
  8507. (defvar todo-only) ;; dynamically scoped
  8508. (defun org-tags-sparse-tree (&optional todo-only match)
  8509. "Create a sparse tree according to tags string MATCH.
  8510. MATCH can contain positive and negative selection of tags, like
  8511. \"+WORK+URGENT-WITHBOSS\".
  8512. If optional argument TODO_ONLY is non-nil, only select lines that are
  8513. also TODO lines."
  8514. (interactive "P")
  8515. (org-prepare-agenda-buffers (list (current-buffer)))
  8516. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  8517. (defvar org-cached-props nil)
  8518. (defun org-cached-entry-get (pom property)
  8519. (if (or (eq t org-use-property-inheritance)
  8520. (and (stringp org-use-property-inheritance)
  8521. (string-match org-use-property-inheritance property))
  8522. (and (listp org-use-property-inheritance)
  8523. (member property org-use-property-inheritance)))
  8524. ;; Caching is not possible, check it directly
  8525. (org-entry-get pom property 'inherit)
  8526. ;; Get all properties, so that we can do complicated checks easily
  8527. (cdr (assoc property (or org-cached-props
  8528. (setq org-cached-props
  8529. (org-entry-properties pom)))))))
  8530. (defun org-global-tags-completion-table (&optional files)
  8531. "Return the list of all tags in all agenda buffer/files."
  8532. (save-excursion
  8533. (org-uniquify
  8534. (delq nil
  8535. (apply 'append
  8536. (mapcar
  8537. (lambda (file)
  8538. (set-buffer (find-file-noselect file))
  8539. (append (org-get-buffer-tags)
  8540. (mapcar (lambda (x) (if (stringp (car-safe x))
  8541. (list (car-safe x)) nil))
  8542. org-tag-alist)))
  8543. (if (and files (car files))
  8544. files
  8545. (org-agenda-files))))))))
  8546. (defun org-make-tags-matcher (match)
  8547. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8548. ;; todo-only is scoped dynamically into this function, and the function
  8549. ;; may change it it the matcher asksk for it.
  8550. (unless match
  8551. ;; Get a new match request, with completion
  8552. (let ((org-last-tags-completion-table
  8553. (org-global-tags-completion-table)))
  8554. (setq match (completing-read
  8555. "Match: " 'org-tags-completion-function nil nil nil
  8556. 'org-tags-history))))
  8557. ;; Parse the string and create a lisp form
  8558. (let ((match0 match)
  8559. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL=\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)=\\({[^}]+}\\|\"[^\"]*\"\\)\\|[[:alnum:]_@]+\\)"))
  8560. minus tag mm
  8561. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  8562. orterms term orlist re-p level-p prop-p pn pv cat-p gv)
  8563. (if (string-match "/+" match)
  8564. ;; match contains also a todo-matching request
  8565. (progn
  8566. (setq tagsmatch (substring match 0 (match-beginning 0))
  8567. todomatch (substring match (match-end 0)))
  8568. (if (string-match "^!" todomatch)
  8569. (setq todo-only t todomatch (substring todomatch 1)))
  8570. (if (string-match "^\\s-*$" todomatch)
  8571. (setq todomatch nil)))
  8572. ;; only matching tags
  8573. (setq tagsmatch match todomatch nil))
  8574. ;; Make the tags matcher
  8575. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8576. (setq tagsmatcher t)
  8577. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8578. (while (setq term (pop orterms))
  8579. (while (and (equal (substring term -1) "\\") orterms)
  8580. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8581. (while (string-match re term)
  8582. (setq minus (and (match-end 1)
  8583. (equal (match-string 1 term) "-"))
  8584. tag (match-string 2 term)
  8585. re-p (equal (string-to-char tag) ?{)
  8586. level-p (match-end 3)
  8587. prop-p (match-end 4)
  8588. mm (cond
  8589. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8590. (level-p `(= level ,(string-to-number
  8591. (match-string 3 term))))
  8592. (prop-p
  8593. (setq pn (match-string 4 term)
  8594. pv (match-string 5 term)
  8595. cat-p (equal pn "CATEGORY")
  8596. re-p (equal (string-to-char pv) ?{)
  8597. pv (substring pv 1 -1))
  8598. (if (equal pn "CATEGORY")
  8599. (setq gv '(get-text-property (point) 'org-category))
  8600. (setq gv `(org-cached-entry-get nil ,pn)))
  8601. (if re-p
  8602. `(string-match ,pv (or ,gv ""))
  8603. `(equal ,pv (or ,gv ""))))
  8604. (t `(member ,(downcase tag) tags-list)))
  8605. mm (if minus (list 'not mm) mm)
  8606. term (substring term (match-end 0)))
  8607. (push mm tagsmatcher))
  8608. (push (if (> (length tagsmatcher) 1)
  8609. (cons 'and tagsmatcher)
  8610. (car tagsmatcher))
  8611. orlist)
  8612. (setq tagsmatcher nil))
  8613. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8614. (setq tagsmatcher
  8615. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  8616. ;; Make the todo matcher
  8617. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8618. (setq todomatcher t)
  8619. (setq orterms (org-split-string todomatch "|") orlist nil)
  8620. (while (setq term (pop orterms))
  8621. (while (string-match re term)
  8622. (setq minus (and (match-end 1)
  8623. (equal (match-string 1 term) "-"))
  8624. kwd (match-string 2 term)
  8625. re-p (equal (string-to-char kwd) ?{)
  8626. term (substring term (match-end 0))
  8627. mm (if re-p
  8628. `(string-match ,(substring kwd 1 -1) todo)
  8629. (list 'equal 'todo kwd))
  8630. mm (if minus (list 'not mm) mm))
  8631. (push mm todomatcher))
  8632. (push (if (> (length todomatcher) 1)
  8633. (cons 'and todomatcher)
  8634. (car todomatcher))
  8635. orlist)
  8636. (setq todomatcher nil))
  8637. (setq todomatcher (if (> (length orlist) 1)
  8638. (cons 'or orlist) (car orlist))))
  8639. ;; Return the string and lisp forms of the matcher
  8640. (setq matcher (if todomatcher
  8641. (list 'and tagsmatcher todomatcher)
  8642. tagsmatcher))
  8643. (cons match0 matcher)))
  8644. (defun org-match-any-p (re list)
  8645. "Does re match any element of list?"
  8646. (setq list (mapcar (lambda (x) (string-match re x)) list))
  8647. (delq nil list))
  8648. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  8649. (defvar org-tags-overlay (org-make-overlay 1 1))
  8650. (org-detach-overlay org-tags-overlay)
  8651. (defun org-align-tags-here (to-col)
  8652. ;; Assumes that this is a headline
  8653. (let ((pos (point)) (col (current-column)) tags)
  8654. (beginning-of-line 1)
  8655. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8656. (< pos (match-beginning 2)))
  8657. (progn
  8658. (setq tags (match-string 2))
  8659. (goto-char (match-beginning 1))
  8660. (insert " ")
  8661. (delete-region (point) (1+ (match-end 0)))
  8662. (backward-char 1)
  8663. (move-to-column
  8664. (max (1+ (current-column))
  8665. (1+ col)
  8666. (if (> to-col 0)
  8667. to-col
  8668. (- (abs to-col) (length tags))))
  8669. t)
  8670. (insert tags)
  8671. (move-to-column (min (current-column) col) t))
  8672. (goto-char pos))))
  8673. (defun org-set-tags (&optional arg just-align)
  8674. "Set the tags for the current headline.
  8675. With prefix ARG, realign all tags in headings in the current buffer."
  8676. (interactive "P")
  8677. (let* ((re (concat "^" outline-regexp))
  8678. (current (org-get-tags-string))
  8679. (col (current-column))
  8680. (org-setting-tags t)
  8681. table current-tags inherited-tags ; computed below when needed
  8682. tags p0 c0 c1 rpl)
  8683. (if arg
  8684. (save-excursion
  8685. (goto-char (point-min))
  8686. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  8687. (while (re-search-forward re nil t)
  8688. (org-set-tags nil t)
  8689. (end-of-line 1)))
  8690. (message "All tags realigned to column %d" org-tags-column))
  8691. (if just-align
  8692. (setq tags current)
  8693. ;; Get a new set of tags from the user
  8694. (save-excursion
  8695. (setq table (or org-tag-alist (org-get-buffer-tags))
  8696. org-last-tags-completion-table table
  8697. current-tags (org-split-string current ":")
  8698. inherited-tags (nreverse
  8699. (nthcdr (length current-tags)
  8700. (nreverse (org-get-tags-at))))
  8701. tags
  8702. (if (or (eq t org-use-fast-tag-selection)
  8703. (and org-use-fast-tag-selection
  8704. (delq nil (mapcar 'cdr table))))
  8705. (org-fast-tag-selection
  8706. current-tags inherited-tags table
  8707. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  8708. (let ((org-add-colon-after-tag-completion t))
  8709. (org-trim
  8710. (org-without-partial-completion
  8711. (completing-read "Tags: " 'org-tags-completion-function
  8712. nil nil current 'org-tags-history)))))))
  8713. (while (string-match "[-+&]+" tags)
  8714. ;; No boolean logic, just a list
  8715. (setq tags (replace-match ":" t t tags))))
  8716. (if (string-match "\\`[\t ]*\\'" tags)
  8717. (setq tags "")
  8718. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  8719. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  8720. ;; Insert new tags at the correct column
  8721. (beginning-of-line 1)
  8722. (cond
  8723. ((and (equal current "") (equal tags "")))
  8724. ((re-search-forward
  8725. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  8726. (point-at-eol) t)
  8727. (if (equal tags "")
  8728. (setq rpl "")
  8729. (goto-char (match-beginning 0))
  8730. (setq c0 (current-column) p0 (point)
  8731. c1 (max (1+ c0) (if (> org-tags-column 0)
  8732. org-tags-column
  8733. (- (- org-tags-column) (length tags))))
  8734. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  8735. (replace-match rpl t t)
  8736. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  8737. tags)
  8738. (t (error "Tags alignment failed")))
  8739. (move-to-column col)
  8740. (unless just-align
  8741. (run-hooks 'org-after-tags-change-hook)))))
  8742. (defun org-change-tag-in-region (beg end tag off)
  8743. "Add or remove TAG for each entry in the region.
  8744. This works in the agenda, and also in an org-mode buffer."
  8745. (interactive
  8746. (list (region-beginning) (region-end)
  8747. (let ((org-last-tags-completion-table
  8748. (if (org-mode-p)
  8749. (org-get-buffer-tags)
  8750. (org-global-tags-completion-table))))
  8751. (completing-read
  8752. "Tag: " 'org-tags-completion-function nil nil nil
  8753. 'org-tags-history))
  8754. (progn
  8755. (message "[s]et or [r]emove? ")
  8756. (equal (read-char-exclusive) ?r))))
  8757. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8758. (let ((agendap (equal major-mode 'org-agenda-mode))
  8759. l1 l2 m buf pos newhead (cnt 0))
  8760. (goto-char end)
  8761. (setq l2 (1- (org-current-line)))
  8762. (goto-char beg)
  8763. (setq l1 (org-current-line))
  8764. (loop for l from l1 to l2 do
  8765. (goto-line l)
  8766. (setq m (get-text-property (point) 'org-hd-marker))
  8767. (when (or (and (org-mode-p) (org-on-heading-p))
  8768. (and agendap m))
  8769. (setq buf (if agendap (marker-buffer m) (current-buffer))
  8770. pos (if agendap m (point)))
  8771. (with-current-buffer buf
  8772. (save-excursion
  8773. (save-restriction
  8774. (goto-char pos)
  8775. (setq cnt (1+ cnt))
  8776. (org-toggle-tag tag (if off 'off 'on))
  8777. (setq newhead (org-get-heading)))))
  8778. (and agendap (org-agenda-change-all-lines newhead m))))
  8779. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  8780. (defun org-tags-completion-function (string predicate &optional flag)
  8781. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  8782. (confirm (lambda (x) (stringp (car x)))))
  8783. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  8784. (setq s1 (match-string 1 string)
  8785. s2 (match-string 2 string))
  8786. (setq s1 "" s2 string))
  8787. (cond
  8788. ((eq flag nil)
  8789. ;; try completion
  8790. (setq rtn (try-completion s2 ctable confirm))
  8791. (if (stringp rtn)
  8792. (setq rtn
  8793. (concat s1 s2 (substring rtn (length s2))
  8794. (if (and org-add-colon-after-tag-completion
  8795. (assoc rtn ctable))
  8796. ":" ""))))
  8797. rtn)
  8798. ((eq flag t)
  8799. ;; all-completions
  8800. (all-completions s2 ctable confirm)
  8801. )
  8802. ((eq flag 'lambda)
  8803. ;; exact match?
  8804. (assoc s2 ctable)))
  8805. ))
  8806. (defun org-fast-tag-insert (kwd tags face &optional end)
  8807. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  8808. (insert (format "%-12s" (concat kwd ":"))
  8809. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  8810. (or end "")))
  8811. (defun org-fast-tag-show-exit (flag)
  8812. (save-excursion
  8813. (goto-line 3)
  8814. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  8815. (replace-match ""))
  8816. (when flag
  8817. (end-of-line 1)
  8818. (move-to-column (- (window-width) 19) t)
  8819. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  8820. (defun org-set-current-tags-overlay (current prefix)
  8821. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  8822. (if (featurep 'xemacs)
  8823. (org-overlay-display org-tags-overlay (concat prefix s)
  8824. 'secondary-selection)
  8825. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  8826. (org-overlay-display org-tags-overlay (concat prefix s)))))
  8827. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  8828. "Fast tag selection with single keys.
  8829. CURRENT is the current list of tags in the headline, INHERITED is the
  8830. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  8831. possibly with grouping information. TODO-TABLE is a similar table with
  8832. TODO keywords, should these have keys assigned to them.
  8833. If the keys are nil, a-z are automatically assigned.
  8834. Returns the new tags string, or nil to not change the current settings."
  8835. (let* ((fulltable (append table todo-table))
  8836. (maxlen (apply 'max (mapcar
  8837. (lambda (x)
  8838. (if (stringp (car x)) (string-width (car x)) 0))
  8839. fulltable)))
  8840. (buf (current-buffer))
  8841. (expert (eq org-fast-tag-selection-single-key 'expert))
  8842. (buffer-tags nil)
  8843. (fwidth (+ maxlen 3 1 3))
  8844. (ncol (/ (- (window-width) 4) fwidth))
  8845. (i-face 'org-done)
  8846. (c-face 'org-todo)
  8847. tg cnt e c char c1 c2 ntable tbl rtn
  8848. ov-start ov-end ov-prefix
  8849. (exit-after-next org-fast-tag-selection-single-key)
  8850. (done-keywords org-done-keywords)
  8851. groups ingroup)
  8852. (save-excursion
  8853. (beginning-of-line 1)
  8854. (if (looking-at
  8855. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8856. (setq ov-start (match-beginning 1)
  8857. ov-end (match-end 1)
  8858. ov-prefix "")
  8859. (setq ov-start (1- (point-at-eol))
  8860. ov-end (1+ ov-start))
  8861. (skip-chars-forward "^\n\r")
  8862. (setq ov-prefix
  8863. (concat
  8864. (buffer-substring (1- (point)) (point))
  8865. (if (> (current-column) org-tags-column)
  8866. " "
  8867. (make-string (- org-tags-column (current-column)) ?\ ))))))
  8868. (org-move-overlay org-tags-overlay ov-start ov-end)
  8869. (save-window-excursion
  8870. (if expert
  8871. (set-buffer (get-buffer-create " *Org tags*"))
  8872. (delete-other-windows)
  8873. (split-window-vertically)
  8874. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  8875. (erase-buffer)
  8876. (org-set-local 'org-done-keywords done-keywords)
  8877. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  8878. (org-fast-tag-insert "Current" current c-face "\n\n")
  8879. (org-fast-tag-show-exit exit-after-next)
  8880. (org-set-current-tags-overlay current ov-prefix)
  8881. (setq tbl fulltable char ?a cnt 0)
  8882. (while (setq e (pop tbl))
  8883. (cond
  8884. ((equal e '(:startgroup))
  8885. (push '() groups) (setq ingroup t)
  8886. (when (not (= cnt 0))
  8887. (setq cnt 0)
  8888. (insert "\n"))
  8889. (insert "{ "))
  8890. ((equal e '(:endgroup))
  8891. (setq ingroup nil cnt 0)
  8892. (insert "}\n"))
  8893. (t
  8894. (setq tg (car e) c2 nil)
  8895. (if (cdr e)
  8896. (setq c (cdr e))
  8897. ;; automatically assign a character.
  8898. (setq c1 (string-to-char
  8899. (downcase (substring
  8900. tg (if (= (string-to-char tg) ?@) 1 0)))))
  8901. (if (or (rassoc c1 ntable) (rassoc c1 table))
  8902. (while (or (rassoc char ntable) (rassoc char table))
  8903. (setq char (1+ char)))
  8904. (setq c2 c1))
  8905. (setq c (or c2 char)))
  8906. (if ingroup (push tg (car groups)))
  8907. (setq tg (org-add-props tg nil 'face
  8908. (cond
  8909. ((not (assoc tg table))
  8910. (org-get-todo-face tg))
  8911. ((member tg current) c-face)
  8912. ((member tg inherited) i-face)
  8913. (t nil))))
  8914. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8915. (insert "[" c "] " tg (make-string
  8916. (- fwidth 4 (length tg)) ?\ ))
  8917. (push (cons tg c) ntable)
  8918. (when (= (setq cnt (1+ cnt)) ncol)
  8919. (insert "\n")
  8920. (if ingroup (insert " "))
  8921. (setq cnt 0)))))
  8922. (setq ntable (nreverse ntable))
  8923. (insert "\n")
  8924. (goto-char (point-min))
  8925. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  8926. (fit-window-to-buffer))
  8927. (setq rtn
  8928. (catch 'exit
  8929. (while t
  8930. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  8931. (if groups " [!] no groups" " [!]groups")
  8932. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  8933. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8934. (cond
  8935. ((= c ?\r) (throw 'exit t))
  8936. ((= c ?!)
  8937. (setq groups (not groups))
  8938. (goto-char (point-min))
  8939. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  8940. ((= c ?\C-c)
  8941. (if (not expert)
  8942. (org-fast-tag-show-exit
  8943. (setq exit-after-next (not exit-after-next)))
  8944. (setq expert nil)
  8945. (delete-other-windows)
  8946. (split-window-vertically)
  8947. (org-switch-to-buffer-other-window " *Org tags*")
  8948. (and (fboundp 'fit-window-to-buffer)
  8949. (fit-window-to-buffer))))
  8950. ((or (= c ?\C-g)
  8951. (and (= c ?q) (not (rassoc c ntable))))
  8952. (org-detach-overlay org-tags-overlay)
  8953. (setq quit-flag t))
  8954. ((= c ?\ )
  8955. (setq current nil)
  8956. (if exit-after-next (setq exit-after-next 'now)))
  8957. ((= c ?\t)
  8958. (condition-case nil
  8959. (setq tg (completing-read
  8960. "Tag: "
  8961. (or buffer-tags
  8962. (with-current-buffer buf
  8963. (org-get-buffer-tags)))))
  8964. (quit (setq tg "")))
  8965. (when (string-match "\\S-" tg)
  8966. (add-to-list 'buffer-tags (list tg))
  8967. (if (member tg current)
  8968. (setq current (delete tg current))
  8969. (push tg current)))
  8970. (if exit-after-next (setq exit-after-next 'now)))
  8971. ((setq e (rassoc c todo-table) tg (car e))
  8972. (with-current-buffer buf
  8973. (save-excursion (org-todo tg)))
  8974. (if exit-after-next (setq exit-after-next 'now)))
  8975. ((setq e (rassoc c ntable) tg (car e))
  8976. (if (member tg current)
  8977. (setq current (delete tg current))
  8978. (loop for g in groups do
  8979. (if (member tg g)
  8980. (mapc (lambda (x)
  8981. (setq current (delete x current)))
  8982. g)))
  8983. (push tg current))
  8984. (if exit-after-next (setq exit-after-next 'now))))
  8985. ;; Create a sorted list
  8986. (setq current
  8987. (sort current
  8988. (lambda (a b)
  8989. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  8990. (if (eq exit-after-next 'now) (throw 'exit t))
  8991. (goto-char (point-min))
  8992. (beginning-of-line 2)
  8993. (delete-region (point) (point-at-eol))
  8994. (org-fast-tag-insert "Current" current c-face)
  8995. (org-set-current-tags-overlay current ov-prefix)
  8996. (while (re-search-forward
  8997. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  8998. (setq tg (match-string 1))
  8999. (add-text-properties
  9000. (match-beginning 1) (match-end 1)
  9001. (list 'face
  9002. (cond
  9003. ((member tg current) c-face)
  9004. ((member tg inherited) i-face)
  9005. (t (get-text-property (match-beginning 1) 'face))))))
  9006. (goto-char (point-min)))))
  9007. (org-detach-overlay org-tags-overlay)
  9008. (if rtn
  9009. (mapconcat 'identity current ":")
  9010. nil))))
  9011. (defun org-get-tags-string ()
  9012. "Get the TAGS string in the current headline."
  9013. (unless (org-on-heading-p t)
  9014. (error "Not on a heading"))
  9015. (save-excursion
  9016. (beginning-of-line 1)
  9017. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9018. (org-match-string-no-properties 1)
  9019. "")))
  9020. (defun org-get-tags ()
  9021. "Get the list of tags specified in the current headline."
  9022. (org-split-string (org-get-tags-string) ":"))
  9023. (defun org-get-buffer-tags ()
  9024. "Get a table of all tags used in the buffer, for completion."
  9025. (let (tags)
  9026. (save-excursion
  9027. (goto-char (point-min))
  9028. (while (re-search-forward
  9029. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  9030. (when (equal (char-after (point-at-bol 0)) ?*)
  9031. (mapc (lambda (x) (add-to-list 'tags x))
  9032. (org-split-string (org-match-string-no-properties 1) ":")))))
  9033. (mapcar 'list tags)))
  9034. ;;;; Properties
  9035. ;;; Setting and retrieving properties
  9036. (defconst org-special-properties
  9037. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
  9038. "TIMESTAMP" "TIMESTAMP_IA")
  9039. "The special properties valid in Org-mode.
  9040. These are properties that are not defined in the property drawer,
  9041. but in some other way.")
  9042. (defconst org-default-properties
  9043. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  9044. "LOCATION" "LOGGING" "COLUMNS")
  9045. "Some properties that are used by Org-mode for various purposes.
  9046. Being in this list makes sure that they are offered for completion.")
  9047. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  9048. "Regular expression matching the first line of a property drawer.")
  9049. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  9050. "Regular expression matching the first line of a property drawer.")
  9051. (defun org-property-action ()
  9052. "Do an action on properties."
  9053. (interactive)
  9054. (let (c)
  9055. (org-at-property-p)
  9056. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  9057. (setq c (read-char-exclusive))
  9058. (cond
  9059. ((equal c ?s)
  9060. (call-interactively 'org-set-property))
  9061. ((equal c ?d)
  9062. (call-interactively 'org-delete-property))
  9063. ((equal c ?D)
  9064. (call-interactively 'org-delete-property-globally))
  9065. ((equal c ?c)
  9066. (call-interactively 'org-compute-property-at-point))
  9067. (t (error "No such property action %c" c)))))
  9068. (defun org-at-property-p ()
  9069. "Is the cursor in a property line?"
  9070. ;; FIXME: Does not check if we are actually in the drawer.
  9071. ;; FIXME: also returns true on any drawers.....
  9072. ;; This is used by C-c C-c for property action.
  9073. (save-excursion
  9074. (beginning-of-line 1)
  9075. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  9076. (defun org-get-property-block (&optional beg end force)
  9077. "Return the (beg . end) range of the body of the property drawer.
  9078. BEG and END can be beginning and end of subtree, if not given
  9079. they will be found.
  9080. If the drawer does not exist and FORCE is non-nil, create the drawer."
  9081. (catch 'exit
  9082. (save-excursion
  9083. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  9084. (end (or end (progn (outline-next-heading) (point)))))
  9085. (goto-char beg)
  9086. (if (re-search-forward org-property-start-re end t)
  9087. (setq beg (1+ (match-end 0)))
  9088. (if force
  9089. (save-excursion
  9090. (org-insert-property-drawer)
  9091. (setq end (progn (outline-next-heading) (point))))
  9092. (throw 'exit nil))
  9093. (goto-char beg)
  9094. (if (re-search-forward org-property-start-re end t)
  9095. (setq beg (1+ (match-end 0)))))
  9096. (if (re-search-forward org-property-end-re end t)
  9097. (setq end (match-beginning 0))
  9098. (or force (throw 'exit nil))
  9099. (goto-char beg)
  9100. (setq end beg)
  9101. (org-indent-line-function)
  9102. (insert ":END:\n"))
  9103. (cons beg end)))))
  9104. (defun org-entry-properties (&optional pom which)
  9105. "Get all properties of the entry at point-or-marker POM.
  9106. This includes the TODO keyword, the tags, time strings for deadline,
  9107. scheduled, and clocking, and any additional properties defined in the
  9108. entry. The return value is an alist, keys may occur multiple times
  9109. if the property key was used several times.
  9110. POM may also be nil, in which case the current entry is used.
  9111. If WHICH is nil or `all', get all properties. If WHICH is
  9112. `special' or `standard', only get that subclass."
  9113. (setq which (or which 'all))
  9114. (org-with-point-at pom
  9115. (let ((clockstr (substring org-clock-string 0 -1))
  9116. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  9117. beg end range props sum-props key value string clocksum)
  9118. (save-excursion
  9119. (when (condition-case nil (org-back-to-heading t) (error nil))
  9120. (setq beg (point))
  9121. (setq sum-props (get-text-property (point) 'org-summaries))
  9122. (setq clocksum (get-text-property (point) :org-clock-minutes))
  9123. (outline-next-heading)
  9124. (setq end (point))
  9125. (when (memq which '(all special))
  9126. ;; Get the special properties, like TODO and tags
  9127. (goto-char beg)
  9128. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  9129. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  9130. (when (looking-at org-priority-regexp)
  9131. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  9132. (when (and (setq value (org-get-tags-string))
  9133. (string-match "\\S-" value))
  9134. (push (cons "TAGS" value) props))
  9135. (when (setq value (org-get-tags-at))
  9136. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  9137. props))
  9138. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  9139. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  9140. string (if (equal key clockstr)
  9141. (org-no-properties
  9142. (org-trim
  9143. (buffer-substring
  9144. (match-beginning 3) (goto-char (point-at-eol)))))
  9145. (substring (org-match-string-no-properties 3) 1 -1)))
  9146. (unless key
  9147. (if (= (char-after (match-beginning 3)) ?\[)
  9148. (setq key "TIMESTAMP_IA")
  9149. (setq key "TIMESTAMP")))
  9150. (when (or (equal key clockstr) (not (assoc key props)))
  9151. (push (cons key string) props)))
  9152. )
  9153. (when (memq which '(all standard))
  9154. ;; Get the standard properties, like :PORP: ...
  9155. (setq range (org-get-property-block beg end))
  9156. (when range
  9157. (goto-char (car range))
  9158. (while (re-search-forward
  9159. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  9160. (cdr range) t)
  9161. (setq key (org-match-string-no-properties 1)
  9162. value (org-trim (or (org-match-string-no-properties 2) "")))
  9163. (unless (member key excluded)
  9164. (push (cons key (or value "")) props)))))
  9165. (if clocksum
  9166. (push (cons "CLOCKSUM"
  9167. (org-column-number-to-string (/ (float clocksum) 60.)
  9168. 'add_times))
  9169. props))
  9170. (append sum-props (nreverse props)))))))
  9171. (defun org-entry-get (pom property &optional inherit)
  9172. "Get value of PROPERTY for entry at point-or-marker POM.
  9173. If INHERIT is non-nil and the entry does not have the property,
  9174. then also check higher levels of the hierarchy.
  9175. If INHERIT is the symbol `selective', use inheritance only if the setting
  9176. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  9177. If the property is present but empty, the return value is the empty string.
  9178. If the property is not present at all, nil is returned."
  9179. (org-with-point-at pom
  9180. (if (and inherit (if (eq inherit 'selective)
  9181. (org-property-inherit-p property)
  9182. t))
  9183. (org-entry-get-with-inheritance property)
  9184. (if (member property org-special-properties)
  9185. ;; We need a special property. Use brute force, get all properties.
  9186. (cdr (assoc property (org-entry-properties nil 'special)))
  9187. (let ((range (org-get-property-block)))
  9188. (if (and range
  9189. (goto-char (car range))
  9190. (re-search-forward
  9191. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  9192. (cdr range) t))
  9193. ;; Found the property, return it.
  9194. (if (match-end 1)
  9195. (org-match-string-no-properties 1)
  9196. "")))))))
  9197. (defun org-entry-delete (pom property)
  9198. "Delete the property PROPERTY from entry at point-or-marker POM."
  9199. (org-with-point-at pom
  9200. (if (member property org-special-properties)
  9201. nil ; cannot delete these properties.
  9202. (let ((range (org-get-property-block)))
  9203. (if (and range
  9204. (goto-char (car range))
  9205. (re-search-forward
  9206. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  9207. (cdr range) t))
  9208. (progn
  9209. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  9210. t)
  9211. nil)))))
  9212. ;; Multi-values properties are properties that contain multiple values
  9213. ;; These values are assumed to be single words, separated by whitespace.
  9214. (defun org-entry-add-to-multivalued-property (pom property value)
  9215. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  9216. (let* ((old (org-entry-get pom property))
  9217. (values (and old (org-split-string old "[ \t]"))))
  9218. (unless (member value values)
  9219. (setq values (cons value values))
  9220. (org-entry-put pom property
  9221. (mapconcat 'identity values " ")))))
  9222. (defun org-entry-remove-from-multivalued-property (pom property value)
  9223. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  9224. (let* ((old (org-entry-get pom property))
  9225. (values (and old (org-split-string old "[ \t]"))))
  9226. (when (member value values)
  9227. (setq values (delete value values))
  9228. (org-entry-put pom property
  9229. (mapconcat 'identity values " ")))))
  9230. (defun org-entry-member-in-multivalued-property (pom property value)
  9231. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  9232. (let* ((old (org-entry-get pom property))
  9233. (values (and old (org-split-string old "[ \t]"))))
  9234. (member value values)))
  9235. (defvar org-entry-property-inherited-from (make-marker))
  9236. (defun org-entry-get-with-inheritance (property)
  9237. "Get entry property, and search higher levels if not present."
  9238. (let (tmp)
  9239. (save-excursion
  9240. (save-restriction
  9241. (widen)
  9242. (catch 'ex
  9243. (while t
  9244. (when (setq tmp (org-entry-get nil property))
  9245. (org-back-to-heading t)
  9246. (move-marker org-entry-property-inherited-from (point))
  9247. (throw 'ex tmp))
  9248. (or (org-up-heading-safe) (throw 'ex nil)))))
  9249. (or tmp (cdr (assoc property org-local-properties))
  9250. (cdr (assoc property org-global-properties))))))
  9251. (defun org-entry-put (pom property value)
  9252. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  9253. (org-with-point-at pom
  9254. (org-back-to-heading t)
  9255. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  9256. range)
  9257. (cond
  9258. ((equal property "TODO")
  9259. (when (and (stringp value) (string-match "\\S-" value)
  9260. (not (member value org-todo-keywords-1)))
  9261. (error "\"%s\" is not a valid TODO state" value))
  9262. (if (or (not value)
  9263. (not (string-match "\\S-" value)))
  9264. (setq value 'none))
  9265. (org-todo value)
  9266. (org-set-tags nil 'align))
  9267. ((equal property "PRIORITY")
  9268. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  9269. (string-to-char value) ?\ ))
  9270. (org-set-tags nil 'align))
  9271. ((equal property "SCHEDULED")
  9272. (if (re-search-forward org-scheduled-time-regexp end t)
  9273. (cond
  9274. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9275. ((eq value 'later) (org-timestamp-change 1 'day))
  9276. (t (call-interactively 'org-schedule)))
  9277. (call-interactively 'org-schedule)))
  9278. ((equal property "DEADLINE")
  9279. (if (re-search-forward org-deadline-time-regexp end t)
  9280. (cond
  9281. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9282. ((eq value 'later) (org-timestamp-change 1 'day))
  9283. (t (call-interactively 'org-deadline)))
  9284. (call-interactively 'org-deadline)))
  9285. ((member property org-special-properties)
  9286. (error "The %s property can not yet be set with `org-entry-put'"
  9287. property))
  9288. (t ; a non-special property
  9289. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  9290. (setq range (org-get-property-block beg end 'force))
  9291. (goto-char (car range))
  9292. (if (re-search-forward
  9293. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  9294. (progn
  9295. (delete-region (match-beginning 1) (match-end 1))
  9296. (goto-char (match-beginning 1)))
  9297. (goto-char (cdr range))
  9298. (insert "\n")
  9299. (backward-char 1)
  9300. (org-indent-line-function)
  9301. (insert ":" property ":"))
  9302. (and value (insert " " value))
  9303. (org-indent-line-function)))))))
  9304. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  9305. "Get all property keys in the current buffer.
  9306. With INCLUDE-SPECIALS, also list the special properties that relect things
  9307. like tags and TODO state.
  9308. With INCLUDE-DEFAULTS, also include properties that has special meaning
  9309. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  9310. With INCLUDE-COLUMNS, also include property names given in COLUMN
  9311. formats in the current buffer."
  9312. (let (rtn range cfmt cols s p)
  9313. (save-excursion
  9314. (save-restriction
  9315. (widen)
  9316. (goto-char (point-min))
  9317. (while (re-search-forward org-property-start-re nil t)
  9318. (setq range (org-get-property-block))
  9319. (goto-char (car range))
  9320. (while (re-search-forward
  9321. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  9322. (cdr range) t)
  9323. (add-to-list 'rtn (org-match-string-no-properties 1)))
  9324. (outline-next-heading))))
  9325. (when include-specials
  9326. (setq rtn (append org-special-properties rtn)))
  9327. (when include-defaults
  9328. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  9329. (when include-columns
  9330. (save-excursion
  9331. (save-restriction
  9332. (widen)
  9333. (goto-char (point-min))
  9334. (while (re-search-forward
  9335. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  9336. nil t)
  9337. (setq cfmt (match-string 2) s 0)
  9338. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  9339. cfmt s)
  9340. (setq s (match-end 0)
  9341. p (match-string 1 cfmt))
  9342. (unless (or (equal p "ITEM")
  9343. (member p org-special-properties))
  9344. (add-to-list 'rtn (match-string 1 cfmt))))))))
  9345. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  9346. (defun org-property-values (key)
  9347. "Return a list of all values of property KEY."
  9348. (save-excursion
  9349. (save-restriction
  9350. (widen)
  9351. (goto-char (point-min))
  9352. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  9353. values)
  9354. (while (re-search-forward re nil t)
  9355. (add-to-list 'values (org-trim (match-string 1))))
  9356. (delete "" values)))))
  9357. (defun org-insert-property-drawer ()
  9358. "Insert a property drawer into the current entry."
  9359. (interactive)
  9360. (org-back-to-heading t)
  9361. (looking-at outline-regexp)
  9362. (let ((indent (- (match-end 0)(match-beginning 0)))
  9363. (beg (point))
  9364. (re (concat "^[ \t]*" org-keyword-time-regexp))
  9365. end hiddenp)
  9366. (outline-next-heading)
  9367. (setq end (point))
  9368. (goto-char beg)
  9369. (while (re-search-forward re end t))
  9370. (setq hiddenp (org-invisible-p))
  9371. (end-of-line 1)
  9372. (and (equal (char-after) ?\n) (forward-char 1))
  9373. (org-skip-over-state-notes)
  9374. (skip-chars-backward " \t\n\r")
  9375. (if (eq (char-before) ?*) (forward-char 1))
  9376. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  9377. (beginning-of-line 0)
  9378. (indent-to-column indent)
  9379. (beginning-of-line 2)
  9380. (indent-to-column indent)
  9381. (beginning-of-line 0)
  9382. (if hiddenp
  9383. (save-excursion
  9384. (org-back-to-heading t)
  9385. (hide-entry))
  9386. (org-flag-drawer t))))
  9387. (defun org-set-property (property value)
  9388. "In the current entry, set PROPERTY to VALUE.
  9389. When called interactively, this will prompt for a property name, offering
  9390. completion on existing and default properties. And then it will prompt
  9391. for a value, offering competion either on allowed values (via an inherited
  9392. xxx_ALL property) or on existing values in other instances of this property
  9393. in the current file."
  9394. (interactive
  9395. (let* ((prop (completing-read
  9396. "Property: " (mapcar 'list (org-buffer-property-keys nil t t))))
  9397. (cur (org-entry-get nil prop))
  9398. (allowed (org-property-get-allowed-values nil prop 'table))
  9399. (existing (mapcar 'list (org-property-values prop)))
  9400. (val (if allowed
  9401. (completing-read "Value: " allowed nil 'req-match)
  9402. (completing-read
  9403. (concat "Value" (if (and cur (string-match "\\S-" cur))
  9404. (concat "[" cur "]") "")
  9405. ": ")
  9406. existing nil nil "" nil cur))))
  9407. (list prop (if (equal val "") cur val))))
  9408. (unless (equal (org-entry-get nil property) value)
  9409. (org-entry-put nil property value)))
  9410. (defun org-delete-property (property)
  9411. "In the current entry, delete PROPERTY."
  9412. (interactive
  9413. (let* ((prop (completing-read
  9414. "Property: " (org-entry-properties nil 'standard))))
  9415. (list prop)))
  9416. (message "Property %s %s" property
  9417. (if (org-entry-delete nil property)
  9418. "deleted"
  9419. "was not present in the entry")))
  9420. (defun org-delete-property-globally (property)
  9421. "Remove PROPERTY globally, from all entries."
  9422. (interactive
  9423. (let* ((prop (completing-read
  9424. "Globally remove property: "
  9425. (mapcar 'list (org-buffer-property-keys)))))
  9426. (list prop)))
  9427. (save-excursion
  9428. (save-restriction
  9429. (widen)
  9430. (goto-char (point-min))
  9431. (let ((cnt 0))
  9432. (while (re-search-forward
  9433. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  9434. nil t)
  9435. (setq cnt (1+ cnt))
  9436. (replace-match ""))
  9437. (message "Property \"%s\" removed from %d entries" property cnt)))))
  9438. (defvar org-columns-current-fmt-compiled) ; defined below
  9439. (defun org-compute-property-at-point ()
  9440. "Compute the property at point.
  9441. This looks for an enclosing column format, extracts the operator and
  9442. then applies it to the proerty in the column format's scope."
  9443. (interactive)
  9444. (unless (org-at-property-p)
  9445. (error "Not at a property"))
  9446. (let ((prop (org-match-string-no-properties 2)))
  9447. (org-columns-get-format-and-top-level)
  9448. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  9449. (error "No operator defined for property %s" prop))
  9450. (org-columns-compute prop)))
  9451. (defun org-property-get-allowed-values (pom property &optional table)
  9452. "Get allowed values for the property PROPERTY.
  9453. When TABLE is non-nil, return an alist that can directly be used for
  9454. completion."
  9455. (let (vals)
  9456. (cond
  9457. ((equal property "TODO")
  9458. (setq vals (org-with-point-at pom
  9459. (append org-todo-keywords-1 '("")))))
  9460. ((equal property "PRIORITY")
  9461. (let ((n org-lowest-priority))
  9462. (while (>= n org-highest-priority)
  9463. (push (char-to-string n) vals)
  9464. (setq n (1- n)))))
  9465. ((member property org-special-properties))
  9466. (t
  9467. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  9468. (when (and vals (string-match "\\S-" vals))
  9469. (setq vals (car (read-from-string (concat "(" vals ")"))))
  9470. (setq vals (mapcar (lambda (x)
  9471. (cond ((stringp x) x)
  9472. ((numberp x) (number-to-string x))
  9473. ((symbolp x) (symbol-name x))
  9474. (t "???")))
  9475. vals)))))
  9476. (if table (mapcar 'list vals) vals)))
  9477. (defun org-property-previous-allowed-value (&optional previous)
  9478. "Switch to the next allowed value for this property."
  9479. (interactive)
  9480. (org-property-next-allowed-value t))
  9481. (defun org-property-next-allowed-value (&optional previous)
  9482. "Switch to the next allowed value for this property."
  9483. (interactive)
  9484. (unless (org-at-property-p)
  9485. (error "Not at a property"))
  9486. (let* ((key (match-string 2))
  9487. (value (match-string 3))
  9488. (allowed (or (org-property-get-allowed-values (point) key)
  9489. (and (member value '("[ ]" "[-]" "[X]"))
  9490. '("[ ]" "[X]"))))
  9491. nval)
  9492. (unless allowed
  9493. (error "Allowed values for this property have not been defined"))
  9494. (if previous (setq allowed (reverse allowed)))
  9495. (if (member value allowed)
  9496. (setq nval (car (cdr (member value allowed)))))
  9497. (setq nval (or nval (car allowed)))
  9498. (if (equal nval value)
  9499. (error "Only one allowed value for this property"))
  9500. (org-at-property-p)
  9501. (replace-match (concat " :" key ": " nval) t t)
  9502. (org-indent-line-function)
  9503. (beginning-of-line 1)
  9504. (skip-chars-forward " \t")))
  9505. (defun org-find-entry-with-id (ident)
  9506. "Locate the entry that contains the ID property with exact value IDENT.
  9507. IDENT can be a string, a symbol or a number, this function will search for
  9508. the string representation of it.
  9509. Return the position where this entry starts, or nil if there is no such entry."
  9510. (let ((id (cond
  9511. ((stringp ident) ident)
  9512. ((symbol-name ident) (symbol-name ident))
  9513. ((numberp ident) (number-to-string ident))
  9514. (t (error "IDENT %s must be a string, symbol or number" ident))))
  9515. (case-fold-search nil))
  9516. (save-excursion
  9517. (save-restriction
  9518. (widen)
  9519. (goto-char (point-min))
  9520. (when (re-search-forward
  9521. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  9522. nil t)
  9523. (org-back-to-heading)
  9524. (point))))))
  9525. ;;; Column View
  9526. (defvar org-columns-overlays nil
  9527. "Holds the list of current column overlays.")
  9528. (defvar org-columns-current-fmt nil
  9529. "Local variable, holds the currently active column format.")
  9530. (defvar org-columns-current-fmt-compiled nil
  9531. "Local variable, holds the currently active column format.
  9532. This is the compiled version of the format.")
  9533. (defvar org-columns-current-widths nil
  9534. "Loval variable, holds the currently widths of fields.")
  9535. (defvar org-columns-current-maxwidths nil
  9536. "Loval variable, holds the currently active maximum column widths.")
  9537. (defvar org-columns-begin-marker (make-marker)
  9538. "Points to the position where last a column creation command was called.")
  9539. (defvar org-columns-top-level-marker (make-marker)
  9540. "Points to the position where current columns region starts.")
  9541. (defvar org-columns-map (make-sparse-keymap)
  9542. "The keymap valid in column display.")
  9543. (defun org-columns-content ()
  9544. "Switch to contents view while in columns view."
  9545. (interactive)
  9546. (org-overview)
  9547. (org-content))
  9548. (org-defkey org-columns-map "c" 'org-columns-content)
  9549. (org-defkey org-columns-map "o" 'org-overview)
  9550. (org-defkey org-columns-map "e" 'org-columns-edit-value)
  9551. (org-defkey org-columns-map "\C-c\C-t" 'org-columns-todo)
  9552. (org-defkey org-columns-map "\C-c\C-c" 'org-columns-set-tags-or-toggle)
  9553. (org-defkey org-columns-map "\C-c\C-o" 'org-columns-open-link)
  9554. (org-defkey org-columns-map "v" 'org-columns-show-value)
  9555. (org-defkey org-columns-map "q" 'org-columns-quit)
  9556. (org-defkey org-columns-map "r" 'org-columns-redo)
  9557. (org-defkey org-columns-map "g" 'org-columns-redo)
  9558. (org-defkey org-columns-map [left] 'backward-char)
  9559. (org-defkey org-columns-map "\M-b" 'backward-char)
  9560. (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
  9561. (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
  9562. (org-defkey org-columns-map "\M-f" (lambda () (interactive) (goto-char (1+ (point)))))
  9563. (org-defkey org-columns-map [right] (lambda () (interactive) (goto-char (1+ (point)))))
  9564. (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
  9565. (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
  9566. (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
  9567. (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
  9568. (org-defkey org-columns-map "<" 'org-columns-narrow)
  9569. (org-defkey org-columns-map ">" 'org-columns-widen)
  9570. (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
  9571. (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
  9572. (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
  9573. (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
  9574. (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
  9575. '("Column"
  9576. ["Edit property" org-columns-edit-value t]
  9577. ["Next allowed value" org-columns-next-allowed-value t]
  9578. ["Previous allowed value" org-columns-previous-allowed-value t]
  9579. ["Show full value" org-columns-show-value t]
  9580. ["Edit allowed values" org-columns-edit-allowed t]
  9581. "--"
  9582. ["Edit column attributes" org-columns-edit-attributes t]
  9583. ["Increase column width" org-columns-widen t]
  9584. ["Decrease column width" org-columns-narrow t]
  9585. "--"
  9586. ["Move column right" org-columns-move-right t]
  9587. ["Move column left" org-columns-move-left t]
  9588. ["Add column" org-columns-new t]
  9589. ["Delete column" org-columns-delete t]
  9590. "--"
  9591. ["CONTENTS" org-columns-content t]
  9592. ["OVERVIEW" org-overview t]
  9593. ["Refresh columns display" org-columns-redo t]
  9594. "--"
  9595. ["Open link" org-columns-open-link t]
  9596. "--"
  9597. ["Quit" org-columns-quit t]))
  9598. (defun org-columns-new-overlay (beg end &optional string face)
  9599. "Create a new column overlay and add it to the list."
  9600. (let ((ov (org-make-overlay beg end)))
  9601. (org-overlay-put ov 'face (or face 'secondary-selection))
  9602. (org-overlay-display ov string face)
  9603. (push ov org-columns-overlays)
  9604. ov))
  9605. (defun org-columns-display-here (&optional props)
  9606. "Overlay the current line with column display."
  9607. (interactive)
  9608. (let* ((fmt org-columns-current-fmt-compiled)
  9609. (beg (point-at-bol))
  9610. (level-face (save-excursion
  9611. (beginning-of-line 1)
  9612. (and (looking-at "\\(\\**\\)\\(\\* \\)")
  9613. (org-get-level-face 2))))
  9614. (color (list :foreground
  9615. (face-attribute (or level-face 'default) :foreground)))
  9616. props pom property ass width f string ov column val modval)
  9617. ;; Check if the entry is in another buffer.
  9618. (unless props
  9619. (if (eq major-mode 'org-agenda-mode)
  9620. (setq pom (or (get-text-property (point) 'org-hd-marker)
  9621. (get-text-property (point) 'org-marker))
  9622. props (if pom (org-entry-properties pom) nil))
  9623. (setq props (org-entry-properties nil))))
  9624. ;; Walk the format
  9625. (while (setq column (pop fmt))
  9626. (setq property (car column)
  9627. ass (if (equal property "ITEM")
  9628. (cons "ITEM"
  9629. (save-match-data
  9630. (org-no-properties
  9631. (org-remove-tabs
  9632. (buffer-substring-no-properties
  9633. (point-at-bol) (point-at-eol))))))
  9634. (assoc property props))
  9635. width (or (cdr (assoc property org-columns-current-maxwidths))
  9636. (nth 2 column)
  9637. (length property))
  9638. f (format "%%-%d.%ds | " width width)
  9639. val (or (cdr ass) "")
  9640. modval (if (equal property "ITEM")
  9641. (org-columns-cleanup-item val org-columns-current-fmt-compiled))
  9642. string (format f (or modval val)))
  9643. ;; Create the overlay
  9644. (org-unmodified
  9645. (setq ov (org-columns-new-overlay
  9646. beg (setq beg (1+ beg)) string
  9647. (list color 'org-column)))
  9648. ;;; (list (get-text-property (point-at-bol) 'face) 'org-column)))
  9649. (org-overlay-put ov 'keymap org-columns-map)
  9650. (org-overlay-put ov 'org-columns-key property)
  9651. (org-overlay-put ov 'org-columns-value (cdr ass))
  9652. (org-overlay-put ov 'org-columns-value-modified modval)
  9653. (org-overlay-put ov 'org-columns-pom pom)
  9654. (org-overlay-put ov 'org-columns-format f))
  9655. (if (or (not (char-after beg))
  9656. (equal (char-after beg) ?\n))
  9657. (let ((inhibit-read-only t))
  9658. (save-excursion
  9659. (goto-char beg)
  9660. (org-unmodified (insert " ")))))) ;; FIXME: add props and remove later?
  9661. ;; Make the rest of the line disappear.
  9662. (org-unmodified
  9663. (setq ov (org-columns-new-overlay beg (point-at-eol)))
  9664. (org-overlay-put ov 'invisible t)
  9665. (org-overlay-put ov 'keymap org-columns-map)
  9666. (org-overlay-put ov 'intangible t)
  9667. (push ov org-columns-overlays)
  9668. (setq ov (org-make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
  9669. (org-overlay-put ov 'keymap org-columns-map)
  9670. (push ov org-columns-overlays)
  9671. (let ((inhibit-read-only t))
  9672. (put-text-property (max (point-min) (1- (point-at-bol)))
  9673. (min (point-max) (1+ (point-at-eol)))
  9674. 'read-only "Type `e' to edit property")))))
  9675. (defvar org-previous-header-line-format nil
  9676. "The header line format before column view was turned on.")
  9677. (defvar org-columns-inhibit-recalculation nil
  9678. "Inhibit recomputing of columns on column view startup.")
  9679. (defvar header-line-format)
  9680. (defun org-columns-display-here-title ()
  9681. "Overlay the newline before the current line with the table title."
  9682. (interactive)
  9683. (let ((fmt org-columns-current-fmt-compiled)
  9684. string (title "")
  9685. property width f column str widths)
  9686. (while (setq column (pop fmt))
  9687. (setq property (car column)
  9688. str (or (nth 1 column) property)
  9689. width (or (cdr (assoc property org-columns-current-maxwidths))
  9690. (nth 2 column)
  9691. (length str))
  9692. widths (push width widths)
  9693. f (format "%%-%d.%ds | " width width)
  9694. string (format f str)
  9695. title (concat title string)))
  9696. (setq title (concat
  9697. (org-add-props " " nil 'display '(space :align-to 0))
  9698. (org-add-props title nil 'face '(:weight bold :underline t))))
  9699. (org-set-local 'org-previous-header-line-format header-line-format)
  9700. (org-set-local 'org-columns-current-widths (nreverse widths))
  9701. (setq header-line-format title)))
  9702. (defun org-columns-remove-overlays ()
  9703. "Remove all currently active column overlays."
  9704. (interactive)
  9705. (when (marker-buffer org-columns-begin-marker)
  9706. (with-current-buffer (marker-buffer org-columns-begin-marker)
  9707. (when (local-variable-p 'org-previous-header-line-format)
  9708. (setq header-line-format org-previous-header-line-format)
  9709. (kill-local-variable 'org-previous-header-line-format))
  9710. (move-marker org-columns-begin-marker nil)
  9711. (move-marker org-columns-top-level-marker nil)
  9712. (org-unmodified
  9713. (mapc 'org-delete-overlay org-columns-overlays)
  9714. (setq org-columns-overlays nil)
  9715. (let ((inhibit-read-only t))
  9716. (remove-text-properties (point-min) (point-max) '(read-only t)))))))
  9717. (defun org-columns-cleanup-item (item fmt)
  9718. "Remove from ITEM what is a column in the format FMT."
  9719. (if (not org-complex-heading-regexp)
  9720. item
  9721. (when (string-match org-complex-heading-regexp item)
  9722. (concat
  9723. (org-add-props (concat (match-string 1 item) " ") nil
  9724. 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
  9725. (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
  9726. (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
  9727. " " (match-string 4 item)
  9728. (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))))
  9729. (defun org-columns-show-value ()
  9730. "Show the full value of the property."
  9731. (interactive)
  9732. (let ((value (get-char-property (point) 'org-columns-value)))
  9733. (message "Value is: %s" (or value ""))))
  9734. (defun org-columns-quit ()
  9735. "Remove the column overlays and in this way exit column editing."
  9736. (interactive)
  9737. (org-unmodified
  9738. (org-columns-remove-overlays)
  9739. (let ((inhibit-read-only t))
  9740. (remove-text-properties (point-min) (point-max) '(read-only t))))
  9741. (when (eq major-mode 'org-agenda-mode)
  9742. (message
  9743. "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
  9744. (defun org-columns-check-computed ()
  9745. "Check if this column value is computed.
  9746. If yes, throw an error indicating that changing it does not make sense."
  9747. (let ((val (get-char-property (point) 'org-columns-value)))
  9748. (when (and (stringp val)
  9749. (get-char-property 0 'org-computed val))
  9750. (error "This value is computed from the entry's children"))))
  9751. (defun org-columns-todo (&optional arg)
  9752. "Change the TODO state during column view."
  9753. (interactive "P")
  9754. (org-columns-edit-value "TODO"))
  9755. (defun org-columns-set-tags-or-toggle (&optional arg)
  9756. "Toggle checkbox at point, or set tags for current headline."
  9757. (interactive "P")
  9758. (if (string-match "\\`\\[[ xX-]\\]\\'"
  9759. (get-char-property (point) 'org-columns-value))
  9760. (org-columns-next-allowed-value)
  9761. (org-columns-edit-value "TAGS")))
  9762. (defun org-columns-edit-value (&optional key)
  9763. "Edit the value of the property at point in column view.
  9764. Where possible, use the standard interface for changing this line."
  9765. (interactive)
  9766. (org-columns-check-computed)
  9767. (let* ((external-key key)
  9768. (col (current-column))
  9769. (key (or key (get-char-property (point) 'org-columns-key)))
  9770. (value (get-char-property (point) 'org-columns-value))
  9771. (bol (point-at-bol)) (eol (point-at-eol))
  9772. (pom (or (get-text-property bol 'org-hd-marker)
  9773. (point))) ; keep despite of compiler waring
  9774. (line-overlays
  9775. (delq nil (mapcar (lambda (x)
  9776. (and (eq (overlay-buffer x) (current-buffer))
  9777. (>= (overlay-start x) bol)
  9778. (<= (overlay-start x) eol)
  9779. x))
  9780. org-columns-overlays)))
  9781. nval eval allowed)
  9782. (cond
  9783. ((equal key "CLOCKSUM")
  9784. (error "This special column cannot be edited"))
  9785. ((equal key "ITEM")
  9786. (setq eval '(org-with-point-at pom
  9787. (org-edit-headline))))
  9788. ((equal key "TODO")
  9789. (setq eval '(org-with-point-at pom
  9790. (let ((current-prefix-arg
  9791. (if external-key current-prefix-arg '(4))))
  9792. (call-interactively 'org-todo)))))
  9793. ((equal key "PRIORITY")
  9794. (setq eval '(org-with-point-at pom
  9795. (call-interactively 'org-priority))))
  9796. ((equal key "TAGS")
  9797. (setq eval '(org-with-point-at pom
  9798. (let ((org-fast-tag-selection-single-key
  9799. (if (eq org-fast-tag-selection-single-key 'expert)
  9800. t org-fast-tag-selection-single-key)))
  9801. (call-interactively 'org-set-tags)))))
  9802. ((equal key "DEADLINE")
  9803. (setq eval '(org-with-point-at pom
  9804. (call-interactively 'org-deadline))))
  9805. ((equal key "SCHEDULED")
  9806. (setq eval '(org-with-point-at pom
  9807. (call-interactively 'org-schedule))))
  9808. (t
  9809. (setq allowed (org-property-get-allowed-values pom key 'table))
  9810. (if allowed
  9811. (setq nval (completing-read "Value: " allowed nil t))
  9812. (setq nval (read-string "Edit: " value)))
  9813. (setq nval (org-trim nval))
  9814. (when (not (equal nval value))
  9815. (setq eval '(org-entry-put pom key nval)))))
  9816. (when eval
  9817. (let ((inhibit-read-only t))
  9818. (remove-text-properties (max (point-min) (1- bol)) eol '(read-only t))
  9819. (unwind-protect
  9820. (progn
  9821. (setq org-columns-overlays
  9822. (org-delete-all line-overlays org-columns-overlays))
  9823. (mapc 'org-delete-overlay line-overlays)
  9824. (org-columns-eval eval))
  9825. (org-columns-display-here))))
  9826. (move-to-column col)
  9827. (if (and (org-mode-p)
  9828. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  9829. (org-columns-update key))))
  9830. (defun org-edit-headline () ; FIXME: this is not columns specific
  9831. "Edit the current headline, the part without TODO keyword, TAGS."
  9832. (org-back-to-heading)
  9833. (when (looking-at org-todo-line-regexp)
  9834. (let ((pre (buffer-substring (match-beginning 0) (match-beginning 3)))
  9835. (txt (match-string 3))
  9836. (post "")
  9837. txt2)
  9838. (if (string-match (org-re "[ \t]+:[[:alnum:]:_@]+:[ \t]*$") txt)
  9839. (setq post (match-string 0 txt)
  9840. txt (substring txt 0 (match-beginning 0))))
  9841. (setq txt2 (read-string "Edit: " txt))
  9842. (when (not (equal txt txt2))
  9843. (beginning-of-line 1)
  9844. (insert pre txt2 post)
  9845. (delete-region (point) (point-at-eol))
  9846. (org-set-tags nil t)))))
  9847. (defun org-columns-edit-allowed ()
  9848. "Edit the list of allowed values for the current property."
  9849. (interactive)
  9850. (let* ((key (get-char-property (point) 'org-columns-key))
  9851. (key1 (concat key "_ALL"))
  9852. (allowed (org-entry-get (point) key1 t))
  9853. nval)
  9854. ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
  9855. (setq nval (read-string "Allowed: " allowed))
  9856. (org-entry-put
  9857. (cond ((marker-position org-entry-property-inherited-from)
  9858. org-entry-property-inherited-from)
  9859. ((marker-position org-columns-top-level-marker)
  9860. org-columns-top-level-marker))
  9861. key1 nval)))
  9862. (defun org-columns-eval (form)
  9863. (let (hidep)
  9864. (save-excursion
  9865. (beginning-of-line 1)
  9866. ;; `next-line' is needed here, because it skips invisible line.
  9867. (condition-case nil (org-no-warnings (next-line 1)) (error nil))
  9868. (setq hidep (org-on-heading-p 1)))
  9869. (eval form)
  9870. (and hidep (hide-entry))))
  9871. (defun org-columns-previous-allowed-value ()
  9872. "Switch to the previous allowed value for this column."
  9873. (interactive)
  9874. (org-columns-next-allowed-value t))
  9875. (defun org-columns-next-allowed-value (&optional previous)
  9876. "Switch to the next allowed value for this column."
  9877. (interactive)
  9878. (org-columns-check-computed)
  9879. (let* ((col (current-column))
  9880. (key (get-char-property (point) 'org-columns-key))
  9881. (value (get-char-property (point) 'org-columns-value))
  9882. (bol (point-at-bol)) (eol (point-at-eol))
  9883. (pom (or (get-text-property bol 'org-hd-marker)
  9884. (point))) ; keep despite of compiler waring
  9885. (line-overlays
  9886. (delq nil (mapcar (lambda (x)
  9887. (and (eq (overlay-buffer x) (current-buffer))
  9888. (>= (overlay-start x) bol)
  9889. (<= (overlay-start x) eol)
  9890. x))
  9891. org-columns-overlays)))
  9892. (allowed (or (org-property-get-allowed-values pom key)
  9893. (and (memq
  9894. (nth 4 (assoc key org-columns-current-fmt-compiled))
  9895. '(checkbox checkbox-n-of-m checkbox-percent))
  9896. '("[ ]" "[X]"))))
  9897. nval)
  9898. (when (equal key "ITEM")
  9899. (error "Cannot edit item headline from here"))
  9900. (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
  9901. (error "Allowed values for this property have not been defined"))
  9902. (if (member key '("SCHEDULED" "DEADLINE"))
  9903. (setq nval (if previous 'earlier 'later))
  9904. (if previous (setq allowed (reverse allowed)))
  9905. (if (member value allowed)
  9906. (setq nval (car (cdr (member value allowed)))))
  9907. (setq nval (or nval (car allowed)))
  9908. (if (equal nval value)
  9909. (error "Only one allowed value for this property")))
  9910. (let ((inhibit-read-only t))
  9911. (remove-text-properties (1- bol) eol '(read-only t))
  9912. (unwind-protect
  9913. (progn
  9914. (setq org-columns-overlays
  9915. (org-delete-all line-overlays org-columns-overlays))
  9916. (mapc 'org-delete-overlay line-overlays)
  9917. (org-columns-eval '(org-entry-put pom key nval)))
  9918. (org-columns-display-here)))
  9919. (move-to-column col)
  9920. (if (and (org-mode-p)
  9921. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  9922. (org-columns-update key))))
  9923. (defun org-verify-version (task)
  9924. (cond
  9925. ((eq task 'columns)
  9926. (if (or (featurep 'xemacs)
  9927. (< emacs-major-version 22))
  9928. (error "Emacs 22 is required for the columns feature")))))
  9929. (defun org-columns-open-link (&optional arg)
  9930. (interactive "P")
  9931. (let ((value (get-char-property (point) 'org-columns-value)))
  9932. (org-open-link-from-string value arg)))
  9933. (defun org-open-link-from-string (s &optional arg)
  9934. "Open a link in the string S, as if it was in Org-mode."
  9935. (interactive)
  9936. (with-temp-buffer
  9937. (let ((org-inhibit-startup t))
  9938. (org-mode)
  9939. (insert s)
  9940. (goto-char (point-min))
  9941. (org-open-at-point arg))))
  9942. (defun org-columns-get-format-and-top-level ()
  9943. (let (fmt)
  9944. (when (condition-case nil (org-back-to-heading) (error nil))
  9945. (move-marker org-entry-property-inherited-from nil)
  9946. (setq fmt (org-entry-get nil "COLUMNS" t)))
  9947. (setq fmt (or fmt org-columns-default-format))
  9948. (org-set-local 'org-columns-current-fmt fmt)
  9949. (org-columns-compile-format fmt)
  9950. (if (marker-position org-entry-property-inherited-from)
  9951. (move-marker org-columns-top-level-marker
  9952. org-entry-property-inherited-from)
  9953. (move-marker org-columns-top-level-marker (point)))
  9954. fmt))
  9955. (defun org-columns ()
  9956. "Turn on column view on an org-mode file."
  9957. (interactive)
  9958. (org-verify-version 'columns)
  9959. (org-columns-remove-overlays)
  9960. (move-marker org-columns-begin-marker (point))
  9961. (let (beg end fmt cache maxwidths)
  9962. (setq fmt (org-columns-get-format-and-top-level))
  9963. (save-excursion
  9964. (goto-char org-columns-top-level-marker)
  9965. (setq beg (point))
  9966. (unless org-columns-inhibit-recalculation
  9967. (org-columns-compute-all))
  9968. (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
  9969. (point-max)))
  9970. ;; Get and cache the properties
  9971. (goto-char beg)
  9972. (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
  9973. (save-excursion
  9974. (save-restriction
  9975. (narrow-to-region beg end)
  9976. (org-clock-sum))))
  9977. (while (re-search-forward (concat "^" outline-regexp) end t)
  9978. (push (cons (org-current-line) (org-entry-properties)) cache))
  9979. (when cache
  9980. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  9981. (org-set-local 'org-columns-current-maxwidths maxwidths)
  9982. (org-columns-display-here-title)
  9983. (mapc (lambda (x)
  9984. (goto-line (car x))
  9985. (org-columns-display-here (cdr x)))
  9986. cache)))))
  9987. (defun org-columns-new (&optional prop title width op fmt &rest rest)
  9988. "Insert a new column, to the left of the current column."
  9989. (interactive)
  9990. (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
  9991. cell)
  9992. (setq prop (completing-read
  9993. "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
  9994. nil nil prop))
  9995. (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
  9996. (setq width (read-string "Column width: " (if width (number-to-string width))))
  9997. (if (string-match "\\S-" width)
  9998. (setq width (string-to-number width))
  9999. (setq width nil))
  10000. (setq fmt (completing-read "Summary [none]: "
  10001. '(("none") ("add_numbers") ("currency") ("add_times") ("checkbox") ("checkbox-n-of-m") ("checkbox-percent"))
  10002. nil t))
  10003. (if (string-match "\\S-" fmt)
  10004. (setq fmt (intern fmt))
  10005. (setq fmt nil))
  10006. (if (eq fmt 'none) (setq fmt nil))
  10007. (if editp
  10008. (progn
  10009. (setcar editp prop)
  10010. (setcdr editp (list title width nil fmt)))
  10011. (setq cell (nthcdr (1- (current-column))
  10012. org-columns-current-fmt-compiled))
  10013. (setcdr cell (cons (list prop title width nil fmt)
  10014. (cdr cell))))
  10015. (org-columns-store-format)
  10016. (org-columns-redo)))
  10017. (defun org-columns-delete ()
  10018. "Delete the column at point from columns view."
  10019. (interactive)
  10020. (let* ((n (current-column))
  10021. (title (nth 1 (nth n org-columns-current-fmt-compiled))))
  10022. (when (y-or-n-p
  10023. (format "Are you sure you want to remove column \"%s\"? " title))
  10024. (setq org-columns-current-fmt-compiled
  10025. (delq (nth n org-columns-current-fmt-compiled)
  10026. org-columns-current-fmt-compiled))
  10027. (org-columns-store-format)
  10028. (org-columns-redo)
  10029. (if (>= (current-column) (length org-columns-current-fmt-compiled))
  10030. (backward-char 1)))))
  10031. (defun org-columns-edit-attributes ()
  10032. "Edit the attributes of the current column."
  10033. (interactive)
  10034. (let* ((n (current-column))
  10035. (info (nth n org-columns-current-fmt-compiled)))
  10036. (apply 'org-columns-new info)))
  10037. (defun org-columns-widen (arg)
  10038. "Make the column wider by ARG characters."
  10039. (interactive "p")
  10040. (let* ((n (current-column))
  10041. (entry (nth n org-columns-current-fmt-compiled))
  10042. (width (or (nth 2 entry)
  10043. (cdr (assoc (car entry) org-columns-current-maxwidths)))))
  10044. (setq width (max 1 (+ width arg)))
  10045. (setcar (nthcdr 2 entry) width)
  10046. (org-columns-store-format)
  10047. (org-columns-redo)))
  10048. (defun org-columns-narrow (arg)
  10049. "Make the column nrrower by ARG characters."
  10050. (interactive "p")
  10051. (org-columns-widen (- arg)))
  10052. (defun org-columns-move-right ()
  10053. "Swap this column with the one to the right."
  10054. (interactive)
  10055. (let* ((n (current-column))
  10056. (cell (nthcdr n org-columns-current-fmt-compiled))
  10057. e)
  10058. (when (>= n (1- (length org-columns-current-fmt-compiled)))
  10059. (error "Cannot shift this column further to the right"))
  10060. (setq e (car cell))
  10061. (setcar cell (car (cdr cell)))
  10062. (setcdr cell (cons e (cdr (cdr cell))))
  10063. (org-columns-store-format)
  10064. (org-columns-redo)
  10065. (forward-char 1)))
  10066. (defun org-columns-move-left ()
  10067. "Swap this column with the one to the left."
  10068. (interactive)
  10069. (let* ((n (current-column)))
  10070. (when (= n 0)
  10071. (error "Cannot shift this column further to the left"))
  10072. (backward-char 1)
  10073. (org-columns-move-right)
  10074. (backward-char 1)))
  10075. (defun org-columns-store-format ()
  10076. "Store the text version of the current columns format in appropriate place.
  10077. This is either in the COLUMNS property of the node starting the current column
  10078. display, or in the #+COLUMNS line of the current buffer."
  10079. (let (fmt (cnt 0))
  10080. (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
  10081. (org-set-local 'org-columns-current-fmt fmt)
  10082. (if (marker-position org-columns-top-level-marker)
  10083. (save-excursion
  10084. (goto-char org-columns-top-level-marker)
  10085. (if (and (org-at-heading-p)
  10086. (org-entry-get nil "COLUMNS"))
  10087. (org-entry-put nil "COLUMNS" fmt)
  10088. (goto-char (point-min))
  10089. ;; Overwrite all #+COLUMNS lines....
  10090. (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
  10091. (setq cnt (1+ cnt))
  10092. (replace-match (concat "#+COLUMNS: " fmt) t t))
  10093. (unless (> cnt 0)
  10094. (goto-char (point-min))
  10095. (or (org-on-heading-p t) (outline-next-heading))
  10096. (let ((inhibit-read-only t))
  10097. (insert-before-markers "#+COLUMNS: " fmt "\n")))
  10098. (org-set-local 'org-columns-default-format fmt))))))
  10099. (defvar org-overriding-columns-format nil
  10100. "When set, overrides any other definition.")
  10101. (defvar org-agenda-view-columns-initially nil
  10102. "When set, switch to columns view immediately after creating the agenda.")
  10103. (defun org-agenda-columns ()
  10104. "Turn on column view in the agenda."
  10105. (interactive)
  10106. (org-verify-version 'columns)
  10107. (org-columns-remove-overlays)
  10108. (move-marker org-columns-begin-marker (point))
  10109. (let (fmt cache maxwidths m)
  10110. (cond
  10111. ((and (local-variable-p 'org-overriding-columns-format)
  10112. org-overriding-columns-format)
  10113. (setq fmt org-overriding-columns-format))
  10114. ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
  10115. (setq fmt (org-entry-get m "COLUMNS" t)))
  10116. ((and (boundp 'org-columns-current-fmt)
  10117. (local-variable-p 'org-columns-current-fmt)
  10118. org-columns-current-fmt)
  10119. (setq fmt org-columns-current-fmt))
  10120. ((setq m (next-single-property-change (point-min) 'org-hd-marker))
  10121. (setq m (get-text-property m 'org-hd-marker))
  10122. (setq fmt (org-entry-get m "COLUMNS" t))))
  10123. (setq fmt (or fmt org-columns-default-format))
  10124. (org-set-local 'org-columns-current-fmt fmt)
  10125. (org-columns-compile-format fmt)
  10126. (save-excursion
  10127. ;; Get and cache the properties
  10128. (goto-char (point-min))
  10129. (while (not (eobp))
  10130. (when (setq m (or (get-text-property (point) 'org-hd-marker)
  10131. (get-text-property (point) 'org-marker)))
  10132. (push (cons (org-current-line) (org-entry-properties m)) cache))
  10133. (beginning-of-line 2))
  10134. (when cache
  10135. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  10136. (org-set-local 'org-columns-current-maxwidths maxwidths)
  10137. (org-columns-display-here-title)
  10138. (mapc (lambda (x)
  10139. (goto-line (car x))
  10140. (org-columns-display-here (cdr x)))
  10141. cache)))))
  10142. (defun org-columns-get-autowidth-alist (s cache)
  10143. "Derive the maximum column widths from the format and the cache."
  10144. (let ((start 0) rtn)
  10145. (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
  10146. (push (cons (match-string 1 s) 1) rtn)
  10147. (setq start (match-end 0)))
  10148. (mapc (lambda (x)
  10149. (setcdr x (apply 'max
  10150. (mapcar
  10151. (lambda (y)
  10152. (length (or (cdr (assoc (car x) (cdr y))) " ")))
  10153. cache))))
  10154. rtn)
  10155. rtn))
  10156. (defun org-columns-compute-all ()
  10157. "Compute all columns that have operators defined."
  10158. (org-unmodified
  10159. (remove-text-properties (point-min) (point-max) '(org-summaries t)))
  10160. (let ((columns org-columns-current-fmt-compiled) col)
  10161. (while (setq col (pop columns))
  10162. (when (nth 3 col)
  10163. (save-excursion
  10164. (org-columns-compute (car col)))))))
  10165. (defun org-columns-update (property)
  10166. "Recompute PROPERTY, and update the columns display for it."
  10167. (org-columns-compute property)
  10168. (let (fmt val pos)
  10169. (save-excursion
  10170. (mapc (lambda (ov)
  10171. (when (equal (org-overlay-get ov 'org-columns-key) property)
  10172. (setq pos (org-overlay-start ov))
  10173. (goto-char pos)
  10174. (when (setq val (cdr (assoc property
  10175. (get-text-property
  10176. (point-at-bol) 'org-summaries))))
  10177. (setq fmt (org-overlay-get ov 'org-columns-format))
  10178. (org-overlay-put ov 'org-columns-value val)
  10179. (org-overlay-put ov 'display (format fmt val)))))
  10180. org-columns-overlays))))
  10181. (defun org-columns-compute (property)
  10182. "Sum the values of property PROPERTY hierarchically, for the entire buffer."
  10183. (interactive)
  10184. (let* ((re (concat "^" outline-regexp))
  10185. (lmax 30) ; Does anyone use deeper levels???
  10186. (lsum (make-vector lmax 0))
  10187. (lflag (make-vector lmax nil))
  10188. (level 0)
  10189. (ass (assoc property org-columns-current-fmt-compiled))
  10190. (format (nth 4 ass))
  10191. (printf (nth 5 ass))
  10192. (beg org-columns-top-level-marker)
  10193. last-level val valflag flag end sumpos sum-alist sum str str1 useval)
  10194. (save-excursion
  10195. ;; Find the region to compute
  10196. (goto-char beg)
  10197. (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
  10198. (goto-char end)
  10199. ;; Walk the tree from the back and do the computations
  10200. (while (re-search-backward re beg t)
  10201. (setq sumpos (match-beginning 0)
  10202. last-level level
  10203. level (org-outline-level)
  10204. val (org-entry-get nil property)
  10205. valflag (and val (string-match "\\S-" val)))
  10206. (cond
  10207. ((< level last-level)
  10208. ;; put the sum of lower levels here as a property
  10209. (setq sum (aref lsum last-level) ; current sum
  10210. flag (aref lflag last-level) ; any valid entries from children?
  10211. str (org-column-number-to-string sum format printf)
  10212. str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
  10213. useval (if flag str1 (if valflag val ""))
  10214. sum-alist (get-text-property sumpos 'org-summaries))
  10215. (if (assoc property sum-alist)
  10216. (setcdr (assoc property sum-alist) useval)
  10217. (push (cons property useval) sum-alist)
  10218. (org-unmodified
  10219. (add-text-properties sumpos (1+ sumpos)
  10220. (list 'org-summaries sum-alist))))
  10221. (when val
  10222. (org-entry-put nil property (if flag str val)))
  10223. ;; add current to current level accumulator
  10224. (when (or flag valflag)
  10225. (aset lsum level (+ (aref lsum level)
  10226. (if flag sum (org-column-string-to-number
  10227. (if flag str val) format))))
  10228. (aset lflag level t))
  10229. ;; clear accumulators for deeper levels
  10230. (loop for l from (1+ level) to (1- lmax) do
  10231. (aset lsum l 0)
  10232. (aset lflag l nil)))
  10233. ((>= level last-level)
  10234. ;; add what we have here to the accumulator for this level
  10235. (aset lsum level (+ (aref lsum level)
  10236. (org-column-string-to-number (or val "0") format)))
  10237. (and valflag (aset lflag level t)))
  10238. (t (error "This should not happen")))))))
  10239. (defun org-columns-redo ()
  10240. "Construct the column display again."
  10241. (interactive)
  10242. (message "Recomputing columns...")
  10243. (save-excursion
  10244. (if (marker-position org-columns-begin-marker)
  10245. (goto-char org-columns-begin-marker))
  10246. (org-columns-remove-overlays)
  10247. (if (org-mode-p)
  10248. (call-interactively 'org-columns)
  10249. (call-interactively 'org-agenda-columns)))
  10250. (message "Recomputing columns...done"))
  10251. (defun org-columns-not-in-agenda ()
  10252. (if (eq major-mode 'org-agenda-mode)
  10253. (error "This command is only allowed in Org-mode buffers")))
  10254. (defun org-string-to-number (s)
  10255. "Convert string to number, and interpret hh:mm:ss."
  10256. (if (not (string-match ":" s))
  10257. (string-to-number s)
  10258. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  10259. (while l
  10260. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  10261. sum)))
  10262. (defun org-column-number-to-string (n fmt &optional printf)
  10263. "Convert a computed column number to a string value, according to FMT."
  10264. (cond
  10265. ((eq fmt 'add_times)
  10266. (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
  10267. (format "%d:%02d" h m)))
  10268. ((eq fmt 'checkbox)
  10269. (cond ((= n (floor n)) "[X]")
  10270. ((> n 1.) "[-]")
  10271. (t "[ ]")))
  10272. ((memq fmt '(checkbox-n-of-m checkbox-percent))
  10273. (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
  10274. (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
  10275. (printf (format printf n))
  10276. ((eq fmt 'currency)
  10277. (format "%.2f" n))
  10278. (t (number-to-string n))))
  10279. (defun org-nofm-to-completion (n m &optional percent)
  10280. (if (not percent)
  10281. (format "[%d/%d]" n m)
  10282. (format "[%d%%]"(floor (+ 0.5 (* 100. (/ (* 1.0 n) m)))))))
  10283. (defun org-column-string-to-number (s fmt)
  10284. "Convert a column value to a number that can be used for column computing."
  10285. (cond
  10286. ((string-match ":" s)
  10287. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  10288. (while l
  10289. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  10290. sum))
  10291. ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
  10292. (if (equal s "[X]") 1. 0.000001))
  10293. (t (string-to-number s))))
  10294. (defun org-columns-uncompile-format (cfmt)
  10295. "Turn the compiled columns format back into a string representation."
  10296. (let ((rtn "") e s prop title op width fmt printf)
  10297. (while (setq e (pop cfmt))
  10298. (setq prop (car e)
  10299. title (nth 1 e)
  10300. width (nth 2 e)
  10301. op (nth 3 e)
  10302. fmt (nth 4 e)
  10303. printf (nth 5 e))
  10304. (cond
  10305. ((eq fmt 'add_times) (setq op ":"))
  10306. ((eq fmt 'checkbox) (setq op "X"))
  10307. ((eq fmt 'checkbox-n-of-m) (setq op "X/"))
  10308. ((eq fmt 'checkbox-percent) (setq op "X%"))
  10309. ((eq fmt 'add_numbers) (setq op "+"))
  10310. ((eq fmt 'currency) (setq op "$")))
  10311. (if (and op printf) (setq op (concat op ";" printf)))
  10312. (if (equal title prop) (setq title nil))
  10313. (setq s (concat "%" (if width (number-to-string width))
  10314. prop
  10315. (if title (concat "(" title ")"))
  10316. (if op (concat "{" op "}"))))
  10317. (setq rtn (concat rtn " " s)))
  10318. (org-trim rtn)))
  10319. (defun org-columns-compile-format (fmt)
  10320. "Turn a column format string into an alist of specifications.
  10321. The alist has one entry for each column in the format. The elements of
  10322. that list are:
  10323. property the property
  10324. title the title field for the columns
  10325. width the column width in characters, can be nil for automatic
  10326. operator the operator if any
  10327. format the output format for computed results, derived from operator
  10328. printf a printf format for computed values"
  10329. (let ((start 0) width prop title op f printf)
  10330. (setq org-columns-current-fmt-compiled nil)
  10331. (while (string-match
  10332. (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
  10333. fmt start)
  10334. (setq start (match-end 0)
  10335. width (match-string 1 fmt)
  10336. prop (match-string 2 fmt)
  10337. title (or (match-string 3 fmt) prop)
  10338. op (match-string 4 fmt)
  10339. f nil
  10340. printf nil)
  10341. (if width (setq width (string-to-number width)))
  10342. (when (and op (string-match ";" op))
  10343. (setq printf (substring op (match-end 0))
  10344. op (substring op 0 (match-beginning 0))))
  10345. (cond
  10346. ((equal op "+") (setq f 'add_numbers))
  10347. ((equal op "$") (setq f 'currency))
  10348. ((equal op ":") (setq f 'add_times))
  10349. ((equal op "X") (setq f 'checkbox))
  10350. ((equal op "X/") (setq f 'checkbox-n-of-m))
  10351. ((equal op "X%") (setq f 'checkbox-percent))
  10352. )
  10353. (push (list prop title width op f printf) org-columns-current-fmt-compiled))
  10354. (setq org-columns-current-fmt-compiled
  10355. (nreverse org-columns-current-fmt-compiled))))
  10356. ;;; Dynamic block for Column view
  10357. (defun org-columns-capture-view (&optional maxlevel skip-empty-rows)
  10358. "Get the column view of the current buffer or subtree.
  10359. The first optional argument MAXLEVEL sets the level limit. A
  10360. second optional argument SKIP-EMPTY-ROWS tells whether to skip
  10361. empty rows, an empty row being one where all the column view
  10362. specifiers except ITEM are empty. This function returns a list
  10363. containing the title row and all other rows. Each row is a list
  10364. of fields."
  10365. (save-excursion
  10366. (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
  10367. (n (length title)) row tbl)
  10368. (goto-char (point-min))
  10369. (while (and (re-search-forward "^\\(\\*+\\) " nil t)
  10370. (or (null maxlevel)
  10371. (>= maxlevel
  10372. (if org-odd-levels-only
  10373. (/ (1+ (length (match-string 1))) 2)
  10374. (length (match-string 1))))))
  10375. (when (get-char-property (match-beginning 0) 'org-columns-key)
  10376. (setq row nil)
  10377. (loop for i from 0 to (1- n) do
  10378. (push (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
  10379. (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
  10380. "")
  10381. row))
  10382. (setq row (nreverse row))
  10383. (unless (and skip-empty-rows
  10384. (eq 1 (length (delete "" (delete-dups row)))))
  10385. (push row tbl))))
  10386. (append (list title 'hline) (nreverse tbl)))))
  10387. (defun org-dblock-write:columnview (params)
  10388. "Write the column view table.
  10389. PARAMS is a property list of parameters:
  10390. :width enforce same column widths with <N> specifiers.
  10391. :id the :ID: property of the entry where the columns view
  10392. should be built, as a string. When `local', call locally.
  10393. When `global' call column view with the cursor at the beginning
  10394. of the buffer (usually this means that the whole buffer switches
  10395. to column view).
  10396. :hlines When t, insert a hline before each item. When a number, insert
  10397. a hline before each level <= that number.
  10398. :vlines When t, make each column a colgroup to enforce vertical lines.
  10399. :maxlevel When set to a number, don't capture headlines below this level.
  10400. :skip-empty-rows
  10401. When t, skip rows where all specifiers other than ITEM are empty."
  10402. (let ((pos (move-marker (make-marker) (point)))
  10403. (hlines (plist-get params :hlines))
  10404. (vlines (plist-get params :vlines))
  10405. (maxlevel (plist-get params :maxlevel))
  10406. (skip-empty-rows (plist-get params :skip-empty-rows))
  10407. tbl id idpos nfields tmp)
  10408. (save-excursion
  10409. (save-restriction
  10410. (when (setq id (plist-get params :id))
  10411. (cond ((not id) nil)
  10412. ((eq id 'global) (goto-char (point-min)))
  10413. ((eq id 'local) nil)
  10414. ((setq idpos (org-find-entry-with-id id))
  10415. (goto-char idpos))
  10416. (t (error "Cannot find entry with :ID: %s" id))))
  10417. (org-columns)
  10418. (setq tbl (org-columns-capture-view maxlevel skip-empty-rows))
  10419. (setq nfields (length (car tbl)))
  10420. (org-columns-quit)))
  10421. (goto-char pos)
  10422. (move-marker pos nil)
  10423. (when tbl
  10424. (when (plist-get params :hlines)
  10425. (setq tmp nil)
  10426. (while tbl
  10427. (if (eq (car tbl) 'hline)
  10428. (push (pop tbl) tmp)
  10429. (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
  10430. (if (and (not (eq (car tmp) 'hline))
  10431. (or (eq hlines t)
  10432. (and (numberp hlines) (<= (- (match-end 1) (match-beginning 1)) hlines))))
  10433. (push 'hline tmp)))
  10434. (push (pop tbl) tmp)))
  10435. (setq tbl (nreverse tmp)))
  10436. (when vlines
  10437. (setq tbl (mapcar (lambda (x)
  10438. (if (eq 'hline x) x (cons "" x)))
  10439. tbl))
  10440. (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
  10441. (setq pos (point))
  10442. (insert (org-listtable-to-string tbl))
  10443. (when (plist-get params :width)
  10444. (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
  10445. org-columns-current-widths "|")))
  10446. (goto-char pos)
  10447. (org-table-align))))
  10448. (defun org-listtable-to-string (tbl)
  10449. "Convert a listtable TBL to a string that contains the Org-mode table.
  10450. The table still need to be alligned. The resulting string has no leading
  10451. and tailing newline characters."
  10452. (mapconcat
  10453. (lambda (x)
  10454. (cond
  10455. ((listp x)
  10456. (concat "|" (mapconcat 'identity x "|") "|"))
  10457. ((eq x 'hline) "|-|")
  10458. (t (error "Garbage in listtable: %s" x))))
  10459. tbl "\n"))
  10460. (defun org-insert-columns-dblock ()
  10461. "Create a dynamic block capturing a column view table."
  10462. (interactive)
  10463. (let ((defaults '(:name "columnview" :hlines 1))
  10464. (id (completing-read
  10465. "Capture columns (local, global, entry with :ID: property) [local]: "
  10466. (append '(("global") ("local"))
  10467. (mapcar 'list (org-property-values "ID"))))))
  10468. (if (equal id "") (setq id 'local))
  10469. (if (equal id "global") (setq id 'global))
  10470. (setq defaults (append defaults (list :id id)))
  10471. (org-create-dblock defaults)
  10472. (org-update-dblock)))
  10473. ;;;; Timestamps
  10474. (defvar org-last-changed-timestamp nil)
  10475. (defvar org-time-was-given) ; dynamically scoped parameter
  10476. (defvar org-end-time-was-given) ; dynamically scoped parameter
  10477. (defvar org-ts-what) ; dynamically scoped parameter
  10478. (defun org-time-stamp (arg)
  10479. "Prompt for a date/time and insert a time stamp.
  10480. If the user specifies a time like HH:MM, or if this command is called
  10481. with a prefix argument, the time stamp will contain date and time.
  10482. Otherwise, only the date will be included. All parts of a date not
  10483. specified by the user will be filled in from the current date/time.
  10484. So if you press just return without typing anything, the time stamp
  10485. will represent the current date/time. If there is already a timestamp
  10486. at the cursor, it will be modified."
  10487. (interactive "P")
  10488. (let* ((ts nil)
  10489. (default-time
  10490. ;; Default time is either today, or, when entering a range,
  10491. ;; the range start.
  10492. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  10493. (save-excursion
  10494. (re-search-backward
  10495. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  10496. (- (point) 20) t)))
  10497. (apply 'encode-time (org-parse-time-string (match-string 1)))
  10498. (current-time)))
  10499. (default-input (and ts (org-get-compact-tod ts)))
  10500. org-time-was-given org-end-time-was-given time)
  10501. (cond
  10502. ((and (org-at-timestamp-p)
  10503. (eq last-command 'org-time-stamp)
  10504. (eq this-command 'org-time-stamp))
  10505. (insert "--")
  10506. (setq time (let ((this-command this-command))
  10507. (org-read-date arg 'totime nil nil default-time default-input)))
  10508. (org-insert-time-stamp time (or org-time-was-given arg)))
  10509. ((org-at-timestamp-p)
  10510. (setq time (let ((this-command this-command))
  10511. (org-read-date arg 'totime nil nil default-time default-input)))
  10512. (when (org-at-timestamp-p) ; just to get the match data
  10513. (replace-match "")
  10514. (setq org-last-changed-timestamp
  10515. (org-insert-time-stamp
  10516. time (or org-time-was-given arg)
  10517. nil nil nil (list org-end-time-was-given))))
  10518. (message "Timestamp updated"))
  10519. (t
  10520. (setq time (let ((this-command this-command))
  10521. (org-read-date arg 'totime nil nil default-time default-input)))
  10522. (org-insert-time-stamp time (or org-time-was-given arg)
  10523. nil nil nil (list org-end-time-was-given))))))
  10524. ;; FIXME: can we use this for something else????
  10525. ;; like computing time differences?????
  10526. (defun org-get-compact-tod (s)
  10527. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  10528. (let* ((t1 (match-string 1 s))
  10529. (h1 (string-to-number (match-string 2 s)))
  10530. (m1 (string-to-number (match-string 3 s)))
  10531. (t2 (and (match-end 4) (match-string 5 s)))
  10532. (h2 (and t2 (string-to-number (match-string 6 s))))
  10533. (m2 (and t2 (string-to-number (match-string 7 s))))
  10534. dh dm)
  10535. (if (not t2)
  10536. t1
  10537. (setq dh (- h2 h1) dm (- m2 m1))
  10538. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  10539. (concat t1 "+" (number-to-string dh)
  10540. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  10541. (defun org-time-stamp-inactive (&optional arg)
  10542. "Insert an inactive time stamp.
  10543. An inactive time stamp is enclosed in square brackets instead of angle
  10544. brackets. It is inactive in the sense that it does not trigger agenda entries,
  10545. does not link to the calendar and cannot be changed with the S-cursor keys.
  10546. So these are more for recording a certain time/date."
  10547. (interactive "P")
  10548. (let (org-time-was-given org-end-time-was-given time)
  10549. (setq time (org-read-date arg 'totime))
  10550. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  10551. nil nil (list org-end-time-was-given))))
  10552. (defvar org-date-ovl (org-make-overlay 1 1))
  10553. (org-overlay-put org-date-ovl 'face 'org-warning)
  10554. (org-detach-overlay org-date-ovl)
  10555. (defvar org-ans1) ; dynamically scoped parameter
  10556. (defvar org-ans2) ; dynamically scoped parameter
  10557. (defvar org-plain-time-of-day-regexp) ; defined below
  10558. (defvar org-read-date-overlay nil)
  10559. (defvar org-dcst nil) ; dynamically scoped
  10560. (defun org-read-date (&optional with-time to-time from-string prompt
  10561. default-time default-input)
  10562. "Read a date, possibly a time, and make things smooth for the user.
  10563. The prompt will suggest to enter an ISO date, but you can also enter anything
  10564. which will at least partially be understood by `parse-time-string'.
  10565. Unrecognized parts of the date will default to the current day, month, year,
  10566. hour and minute. If this command is called to replace a timestamp at point,
  10567. of to enter the second timestamp of a range, the default time is taken from the
  10568. existing stamp. For example,
  10569. 3-2-5 --> 2003-02-05
  10570. feb 15 --> currentyear-02-15
  10571. sep 12 9 --> 2009-09-12
  10572. 12:45 --> today 12:45
  10573. 22 sept 0:34 --> currentyear-09-22 0:34
  10574. 12 --> currentyear-currentmonth-12
  10575. Fri --> nearest Friday (today or later)
  10576. etc.
  10577. Furthermore you can specify a relative date by giving, as the *first* thing
  10578. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  10579. change in days weeks, months, years.
  10580. With a single plus or minus, the date is relative to today. With a double
  10581. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  10582. +4d --> four days from today
  10583. +4 --> same as above
  10584. +2w --> two weeks from today
  10585. ++5 --> five days from default date
  10586. The function understands only English month and weekday abbreviations,
  10587. but this can be configured with the variables `parse-time-months' and
  10588. `parse-time-weekdays'.
  10589. While prompting, a calendar is popped up - you can also select the
  10590. date with the mouse (button 1). The calendar shows a period of three
  10591. months. To scroll it to other months, use the keys `>' and `<'.
  10592. If you don't like the calendar, turn it off with
  10593. \(setq org-read-date-popup-calendar nil)
  10594. With optional argument TO-TIME, the date will immediately be converted
  10595. to an internal time.
  10596. With an optional argument WITH-TIME, the prompt will suggest to also
  10597. insert a time. Note that when WITH-TIME is not set, you can still
  10598. enter a time, and this function will inform the calling routine about
  10599. this change. The calling routine may then choose to change the format
  10600. used to insert the time stamp into the buffer to include the time.
  10601. With optional argument FROM-STRING, read from this string instead from
  10602. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  10603. the time/date that is used for everything that is not specified by the
  10604. user."
  10605. (require 'parse-time)
  10606. (let* ((org-time-stamp-rounding-minutes
  10607. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  10608. (org-dcst org-display-custom-times)
  10609. (ct (org-current-time))
  10610. (def (or default-time ct))
  10611. (defdecode (decode-time def))
  10612. (dummy (progn
  10613. (when (< (nth 2 defdecode) org-extend-today-until)
  10614. (setcar (nthcdr 2 defdecode) -1)
  10615. (setcar (nthcdr 1 defdecode) 59)
  10616. (setq def (apply 'encode-time defdecode)
  10617. defdecode (decode-time def)))))
  10618. (calendar-move-hook nil)
  10619. (view-diary-entries-initially nil)
  10620. (view-calendar-holidays-initially nil)
  10621. (timestr (format-time-string
  10622. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  10623. (prompt (concat (if prompt (concat prompt " ") "")
  10624. (format "Date+time [%s]: " timestr)))
  10625. ans (org-ans0 "") org-ans1 org-ans2 final)
  10626. (cond
  10627. (from-string (setq ans from-string))
  10628. (org-read-date-popup-calendar
  10629. (save-excursion
  10630. (save-window-excursion
  10631. (calendar)
  10632. (calendar-forward-day (- (time-to-days def)
  10633. (calendar-absolute-from-gregorian
  10634. (calendar-current-date))))
  10635. (org-eval-in-calendar nil t)
  10636. (let* ((old-map (current-local-map))
  10637. (map (copy-keymap calendar-mode-map))
  10638. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  10639. (org-defkey map (kbd "RET") 'org-calendar-select)
  10640. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  10641. 'org-calendar-select-mouse)
  10642. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  10643. 'org-calendar-select-mouse)
  10644. (org-defkey minibuffer-local-map [(meta shift left)]
  10645. (lambda () (interactive)
  10646. (org-eval-in-calendar '(calendar-backward-month 1))))
  10647. (org-defkey minibuffer-local-map [(meta shift right)]
  10648. (lambda () (interactive)
  10649. (org-eval-in-calendar '(calendar-forward-month 1))))
  10650. (org-defkey minibuffer-local-map [(meta shift up)]
  10651. (lambda () (interactive)
  10652. (org-eval-in-calendar '(calendar-backward-year 1))))
  10653. (org-defkey minibuffer-local-map [(meta shift down)]
  10654. (lambda () (interactive)
  10655. (org-eval-in-calendar '(calendar-forward-year 1))))
  10656. (org-defkey minibuffer-local-map [(shift up)]
  10657. (lambda () (interactive)
  10658. (org-eval-in-calendar '(calendar-backward-week 1))))
  10659. (org-defkey minibuffer-local-map [(shift down)]
  10660. (lambda () (interactive)
  10661. (org-eval-in-calendar '(calendar-forward-week 1))))
  10662. (org-defkey minibuffer-local-map [(shift left)]
  10663. (lambda () (interactive)
  10664. (org-eval-in-calendar '(calendar-backward-day 1))))
  10665. (org-defkey minibuffer-local-map [(shift right)]
  10666. (lambda () (interactive)
  10667. (org-eval-in-calendar '(calendar-forward-day 1))))
  10668. (org-defkey minibuffer-local-map ">"
  10669. (lambda () (interactive)
  10670. (org-eval-in-calendar '(scroll-calendar-left 1))))
  10671. (org-defkey minibuffer-local-map "<"
  10672. (lambda () (interactive)
  10673. (org-eval-in-calendar '(scroll-calendar-right 1))))
  10674. (unwind-protect
  10675. (progn
  10676. (use-local-map map)
  10677. (add-hook 'post-command-hook 'org-read-date-display)
  10678. (setq org-ans0 (read-string prompt default-input nil nil))
  10679. ;; org-ans0: from prompt
  10680. ;; org-ans1: from mouse click
  10681. ;; org-ans2: from calendar motion
  10682. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  10683. (remove-hook 'post-command-hook 'org-read-date-display)
  10684. (use-local-map old-map)
  10685. (when org-read-date-overlay
  10686. (org-delete-overlay org-read-date-overlay)
  10687. (setq org-read-date-overlay nil)))))))
  10688. (t ; Naked prompt only
  10689. (unwind-protect
  10690. (setq ans (read-string prompt default-input nil timestr))
  10691. (when org-read-date-overlay
  10692. (org-delete-overlay org-read-date-overlay)
  10693. (setq org-read-date-overlay nil)))))
  10694. (setq final (org-read-date-analyze ans def defdecode))
  10695. (if to-time
  10696. (apply 'encode-time final)
  10697. (if (and (boundp 'org-time-was-given) org-time-was-given)
  10698. (format "%04d-%02d-%02d %02d:%02d"
  10699. (nth 5 final) (nth 4 final) (nth 3 final)
  10700. (nth 2 final) (nth 1 final))
  10701. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  10702. (defvar def)
  10703. (defvar defdecode)
  10704. (defvar with-time)
  10705. (defun org-read-date-display ()
  10706. "Display the currrent date prompt interpretation in the minibuffer."
  10707. (when org-read-date-display-live
  10708. (when org-read-date-overlay
  10709. (org-delete-overlay org-read-date-overlay))
  10710. (let ((p (point)))
  10711. (end-of-line 1)
  10712. (while (not (equal (buffer-substring
  10713. (max (point-min) (- (point) 4)) (point))
  10714. " "))
  10715. (insert " "))
  10716. (goto-char p))
  10717. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  10718. " " (or org-ans1 org-ans2)))
  10719. (org-end-time-was-given nil)
  10720. (f (org-read-date-analyze ans def defdecode))
  10721. (fmts (if org-dcst
  10722. org-time-stamp-custom-formats
  10723. org-time-stamp-formats))
  10724. (fmt (if (or with-time
  10725. (and (boundp 'org-time-was-given) org-time-was-given))
  10726. (cdr fmts)
  10727. (car fmts)))
  10728. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  10729. (when (and org-end-time-was-given
  10730. (string-match org-plain-time-of-day-regexp txt))
  10731. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  10732. org-end-time-was-given
  10733. (substring txt (match-end 0)))))
  10734. (setq org-read-date-overlay
  10735. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  10736. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  10737. (defun org-read-date-analyze (ans def defdecode)
  10738. "Analyze the combined answer of the date prompt."
  10739. ;; FIXME: cleanup and comment
  10740. (let (delta deltan deltaw deltadef year month day
  10741. hour minute second wday pm h2 m2 tl wday1
  10742. iso-year iso-weekday iso-week iso-year iso-date)
  10743. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  10744. (setq ans (replace-match "" t t ans)
  10745. deltan (car delta)
  10746. deltaw (nth 1 delta)
  10747. deltadef (nth 2 delta)))
  10748. ;; Check if there is an iso week date in there
  10749. ;; If yes, sore the info and ostpone interpreting it until the rest
  10750. ;; of the parsing is done
  10751. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  10752. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  10753. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  10754. iso-week (string-to-number (match-string 2 ans)))
  10755. (setq ans (replace-match "" t t ans)))
  10756. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  10757. (when (string-match
  10758. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  10759. (setq year (if (match-end 2)
  10760. (string-to-number (match-string 2 ans))
  10761. (string-to-number (format-time-string "%Y")))
  10762. month (string-to-number (match-string 3 ans))
  10763. day (string-to-number (match-string 4 ans)))
  10764. (if (< year 100) (setq year (+ 2000 year)))
  10765. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  10766. t nil ans)))
  10767. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  10768. ;; If there is a time with am/pm, and *no* time without it, we convert
  10769. ;; so that matching will be successful.
  10770. (loop for i from 1 to 2 do ; twice, for end time as well
  10771. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  10772. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  10773. (setq hour (string-to-number (match-string 1 ans))
  10774. minute (if (match-end 3)
  10775. (string-to-number (match-string 3 ans))
  10776. 0)
  10777. pm (equal ?p
  10778. (string-to-char (downcase (match-string 4 ans)))))
  10779. (if (and (= hour 12) (not pm))
  10780. (setq hour 0)
  10781. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  10782. (setq ans (replace-match (format "%02d:%02d" hour minute)
  10783. t t ans))))
  10784. ;; Check if a time range is given as a duration
  10785. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  10786. (setq hour (string-to-number (match-string 1 ans))
  10787. h2 (+ hour (string-to-number (match-string 3 ans)))
  10788. minute (string-to-number (match-string 2 ans))
  10789. m2 (+ minute (if (match-end 5) (string-to-number
  10790. (match-string 5 ans))0)))
  10791. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  10792. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  10793. t t ans)))
  10794. ;; Check if there is a time range
  10795. (when (boundp 'org-end-time-was-given)
  10796. (setq org-time-was-given nil)
  10797. (when (and (string-match org-plain-time-of-day-regexp ans)
  10798. (match-end 8))
  10799. (setq org-end-time-was-given (match-string 8 ans))
  10800. (setq ans (concat (substring ans 0 (match-beginning 7))
  10801. (substring ans (match-end 7))))))
  10802. (setq tl (parse-time-string ans)
  10803. day (or (nth 3 tl) (nth 3 defdecode))
  10804. month (or (nth 4 tl)
  10805. (if (and org-read-date-prefer-future
  10806. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  10807. (1+ (nth 4 defdecode))
  10808. (nth 4 defdecode)))
  10809. year (or (nth 5 tl)
  10810. (if (and org-read-date-prefer-future
  10811. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  10812. (1+ (nth 5 defdecode))
  10813. (nth 5 defdecode)))
  10814. hour (or (nth 2 tl) (nth 2 defdecode))
  10815. minute (or (nth 1 tl) (nth 1 defdecode))
  10816. second (or (nth 0 tl) 0)
  10817. wday (nth 6 tl))
  10818. ;; Special date definitions below
  10819. (cond
  10820. (iso-week
  10821. ;; There was an iso week
  10822. (setq year (or iso-year year)
  10823. day (or iso-weekday wday 1)
  10824. wday nil ; to make sure that the trigger below does not match
  10825. iso-date (calendar-gregorian-from-absolute
  10826. (calendar-absolute-from-iso
  10827. (list iso-week day year))))
  10828. ; FIXME: Should we also push ISO weeks into the future?
  10829. ; (when (and org-read-date-prefer-future
  10830. ; (not iso-year)
  10831. ; (< (calendar-absolute-from-gregorian iso-date)
  10832. ; (time-to-days (current-time))))
  10833. ; (setq year (1+ year)
  10834. ; iso-date (calendar-gregorian-from-absolute
  10835. ; (calendar-absolute-from-iso
  10836. ; (list iso-week day year)))))
  10837. (setq month (car iso-date)
  10838. year (nth 2 iso-date)
  10839. day (nth 1 iso-date)))
  10840. (deltan
  10841. (unless deltadef
  10842. (let ((now (decode-time (current-time))))
  10843. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  10844. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  10845. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  10846. ((equal deltaw "m") (setq month (+ month deltan)))
  10847. ((equal deltaw "y") (setq year (+ year deltan)))))
  10848. ((and wday (not (nth 3 tl)))
  10849. ;; Weekday was given, but no day, so pick that day in the week
  10850. ;; on or after the derived date.
  10851. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  10852. (unless (equal wday wday1)
  10853. (setq day (+ day (% (- wday wday1 -7) 7))))))
  10854. (if (and (boundp 'org-time-was-given)
  10855. (nth 2 tl))
  10856. (setq org-time-was-given t))
  10857. (if (< year 100) (setq year (+ 2000 year)))
  10858. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  10859. (list second minute hour day month year)))
  10860. (defvar parse-time-weekdays)
  10861. (defun org-read-date-get-relative (s today default)
  10862. "Check string S for special relative date string.
  10863. TODAY and DEFAULT are internal times, for today and for a default.
  10864. Return shift list (N what def-flag)
  10865. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  10866. N is the number of WHATs to shift.
  10867. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  10868. the DEFAULT date rather than TODAY."
  10869. (when (string-match
  10870. (concat
  10871. "\\`[ \t]*\\([-+]\\{1,2\\}\\)"
  10872. "\\([0-9]+\\)?"
  10873. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  10874. "\\([ \t]\\|$\\)") s)
  10875. (let* ((dir (if (match-end 1)
  10876. (string-to-char (substring (match-string 1 s) -1))
  10877. ?+))
  10878. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  10879. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  10880. (what (if (match-end 3) (match-string 3 s) "d"))
  10881. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  10882. (date (if rel default today))
  10883. (wday (nth 6 (decode-time date)))
  10884. delta)
  10885. (if wday1
  10886. (progn
  10887. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  10888. (if (= dir ?-) (setq delta (- delta 7)))
  10889. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  10890. (list delta "d" rel))
  10891. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  10892. (defun org-eval-in-calendar (form &optional keepdate)
  10893. "Eval FORM in the calendar window and return to current window.
  10894. Also, store the cursor date in variable org-ans2."
  10895. (let ((sw (selected-window)))
  10896. (select-window (get-buffer-window "*Calendar*"))
  10897. (eval form)
  10898. (when (and (not keepdate) (calendar-cursor-to-date))
  10899. (let* ((date (calendar-cursor-to-date))
  10900. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10901. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  10902. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  10903. (select-window sw)))
  10904. ; ;; Update the prompt to show new default date
  10905. ; (save-excursion
  10906. ; (goto-char (point-min))
  10907. ; (when (and org-ans2
  10908. ; (re-search-forward "\\[[-0-9]+\\]" nil t)
  10909. ; (get-text-property (match-end 0) 'field))
  10910. ; (let ((inhibit-read-only t))
  10911. ; (replace-match (concat "[" org-ans2 "]") t t)
  10912. ; (add-text-properties (point-min) (1+ (match-end 0))
  10913. ; (text-properties-at (1+ (point-min)))))))))
  10914. (defun org-calendar-select ()
  10915. "Return to `org-read-date' with the date currently selected.
  10916. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10917. (interactive)
  10918. (when (calendar-cursor-to-date)
  10919. (let* ((date (calendar-cursor-to-date))
  10920. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10921. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10922. (if (active-minibuffer-window) (exit-minibuffer))))
  10923. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  10924. "Insert a date stamp for the date given by the internal TIME.
  10925. WITH-HM means, use the stamp format that includes the time of the day.
  10926. INACTIVE means use square brackets instead of angular ones, so that the
  10927. stamp will not contribute to the agenda.
  10928. PRE and POST are optional strings to be inserted before and after the
  10929. stamp.
  10930. The command returns the inserted time stamp."
  10931. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  10932. stamp)
  10933. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  10934. (insert-before-markers (or pre ""))
  10935. (insert-before-markers (setq stamp (format-time-string fmt time)))
  10936. (when (listp extra)
  10937. (setq extra (car extra))
  10938. (if (and (stringp extra)
  10939. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  10940. (setq extra (format "-%02d:%02d"
  10941. (string-to-number (match-string 1 extra))
  10942. (string-to-number (match-string 2 extra))))
  10943. (setq extra nil)))
  10944. (when extra
  10945. (backward-char 1)
  10946. (insert-before-markers extra)
  10947. (forward-char 1))
  10948. (insert-before-markers (or post ""))
  10949. stamp))
  10950. (defun org-toggle-time-stamp-overlays ()
  10951. "Toggle the use of custom time stamp formats."
  10952. (interactive)
  10953. (setq org-display-custom-times (not org-display-custom-times))
  10954. (unless org-display-custom-times
  10955. (let ((p (point-min)) (bmp (buffer-modified-p)))
  10956. (while (setq p (next-single-property-change p 'display))
  10957. (if (and (get-text-property p 'display)
  10958. (eq (get-text-property p 'face) 'org-date))
  10959. (remove-text-properties
  10960. p (setq p (next-single-property-change p 'display))
  10961. '(display t))))
  10962. (set-buffer-modified-p bmp)))
  10963. (if (featurep 'xemacs)
  10964. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  10965. (org-restart-font-lock)
  10966. (setq org-table-may-need-update t)
  10967. (if org-display-custom-times
  10968. (message "Time stamps are overlayed with custom format")
  10969. (message "Time stamp overlays removed")))
  10970. (defun org-display-custom-time (beg end)
  10971. "Overlay modified time stamp format over timestamp between BED and END."
  10972. (let* ((ts (buffer-substring beg end))
  10973. t1 w1 with-hm tf time str w2 (off 0))
  10974. (save-match-data
  10975. (setq t1 (org-parse-time-string ts t))
  10976. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  10977. (setq off (- (match-end 0) (match-beginning 0)))))
  10978. (setq end (- end off))
  10979. (setq w1 (- end beg)
  10980. with-hm (and (nth 1 t1) (nth 2 t1))
  10981. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  10982. time (org-fix-decoded-time t1)
  10983. str (org-add-props
  10984. (format-time-string
  10985. (substring tf 1 -1) (apply 'encode-time time))
  10986. nil 'mouse-face 'highlight)
  10987. w2 (length str))
  10988. (if (not (= w2 w1))
  10989. (add-text-properties (1+ beg) (+ 2 beg)
  10990. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  10991. (if (featurep 'xemacs)
  10992. (progn
  10993. (put-text-property beg end 'invisible t)
  10994. (put-text-property beg end 'end-glyph (make-glyph str)))
  10995. (put-text-property beg end 'display str))))
  10996. (defun org-translate-time (string)
  10997. "Translate all timestamps in STRING to custom format.
  10998. But do this only if the variable `org-display-custom-times' is set."
  10999. (when org-display-custom-times
  11000. (save-match-data
  11001. (let* ((start 0)
  11002. (re org-ts-regexp-both)
  11003. t1 with-hm inactive tf time str beg end)
  11004. (while (setq start (string-match re string start))
  11005. (setq beg (match-beginning 0)
  11006. end (match-end 0)
  11007. t1 (save-match-data
  11008. (org-parse-time-string (substring string beg end) t))
  11009. with-hm (and (nth 1 t1) (nth 2 t1))
  11010. inactive (equal (substring string beg (1+ beg)) "[")
  11011. tf (funcall (if with-hm 'cdr 'car)
  11012. org-time-stamp-custom-formats)
  11013. time (org-fix-decoded-time t1)
  11014. str (format-time-string
  11015. (concat
  11016. (if inactive "[" "<") (substring tf 1 -1)
  11017. (if inactive "]" ">"))
  11018. (apply 'encode-time time))
  11019. string (replace-match str t t string)
  11020. start (+ start (length str)))))))
  11021. string)
  11022. (defun org-fix-decoded-time (time)
  11023. "Set 0 instead of nil for the first 6 elements of time.
  11024. Don't touch the rest."
  11025. (let ((n 0))
  11026. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  11027. (defun org-days-to-time (timestamp-string)
  11028. "Difference between TIMESTAMP-STRING and now in days."
  11029. (- (time-to-days (org-time-string-to-time timestamp-string))
  11030. (time-to-days (current-time))))
  11031. (defun org-deadline-close (timestamp-string &optional ndays)
  11032. "Is the time in TIMESTAMP-STRING close to the current date?"
  11033. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  11034. (and (< (org-days-to-time timestamp-string) ndays)
  11035. (not (org-entry-is-done-p))))
  11036. (defun org-get-wdays (ts)
  11037. "Get the deadline lead time appropriate for timestring TS."
  11038. (cond
  11039. ((<= org-deadline-warning-days 0)
  11040. ;; 0 or negative, enforce this value no matter what
  11041. (- org-deadline-warning-days))
  11042. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  11043. ;; lead time is specified.
  11044. (floor (* (string-to-number (match-string 1 ts))
  11045. (cdr (assoc (match-string 2 ts)
  11046. '(("d" . 1) ("w" . 7)
  11047. ("m" . 30.4) ("y" . 365.25)))))))
  11048. ;; go for the default.
  11049. (t org-deadline-warning-days)))
  11050. (defun org-calendar-select-mouse (ev)
  11051. "Return to `org-read-date' with the date currently selected.
  11052. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11053. (interactive "e")
  11054. (mouse-set-point ev)
  11055. (when (calendar-cursor-to-date)
  11056. (let* ((date (calendar-cursor-to-date))
  11057. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11058. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11059. (if (active-minibuffer-window) (exit-minibuffer))))
  11060. (defun org-check-deadlines (ndays)
  11061. "Check if there are any deadlines due or past due.
  11062. A deadline is considered due if it happens within `org-deadline-warning-days'
  11063. days from today's date. If the deadline appears in an entry marked DONE,
  11064. it is not shown. The prefix arg NDAYS can be used to test that many
  11065. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  11066. (interactive "P")
  11067. (let* ((org-warn-days
  11068. (cond
  11069. ((equal ndays '(4)) 100000)
  11070. (ndays (prefix-numeric-value ndays))
  11071. (t (abs org-deadline-warning-days))))
  11072. (case-fold-search nil)
  11073. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  11074. (callback
  11075. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  11076. (message "%d deadlines past-due or due within %d days"
  11077. (org-occur regexp nil callback)
  11078. org-warn-days)))
  11079. (defun org-check-before-date (date)
  11080. "Check if there are deadlines or scheduled entries before DATE."
  11081. (interactive (list (org-read-date)))
  11082. (let ((case-fold-search nil)
  11083. (regexp (concat "\\<\\(" org-deadline-string
  11084. "\\|" org-scheduled-string
  11085. "\\) *<\\([^>]+\\)>"))
  11086. (callback
  11087. (lambda () (time-less-p
  11088. (org-time-string-to-time (match-string 2))
  11089. (org-time-string-to-time date)))))
  11090. (message "%d entries before %s"
  11091. (org-occur regexp nil callback) date)))
  11092. (defun org-evaluate-time-range (&optional to-buffer)
  11093. "Evaluate a time range by computing the difference between start and end.
  11094. Normally the result is just printed in the echo area, but with prefix arg
  11095. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  11096. If the time range is actually in a table, the result is inserted into the
  11097. next column.
  11098. For time difference computation, a year is assumed to be exactly 365
  11099. days in order to avoid rounding problems."
  11100. (interactive "P")
  11101. (or
  11102. (org-clock-update-time-maybe)
  11103. (save-excursion
  11104. (unless (org-at-date-range-p t)
  11105. (goto-char (point-at-bol))
  11106. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  11107. (if (not (org-at-date-range-p t))
  11108. (error "Not at a time-stamp range, and none found in current line")))
  11109. (let* ((ts1 (match-string 1))
  11110. (ts2 (match-string 2))
  11111. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  11112. (match-end (match-end 0))
  11113. (time1 (org-time-string-to-time ts1))
  11114. (time2 (org-time-string-to-time ts2))
  11115. (t1 (time-to-seconds time1))
  11116. (t2 (time-to-seconds time2))
  11117. (diff (abs (- t2 t1)))
  11118. (negative (< (- t2 t1) 0))
  11119. ;; (ys (floor (* 365 24 60 60)))
  11120. (ds (* 24 60 60))
  11121. (hs (* 60 60))
  11122. (fy "%dy %dd %02d:%02d")
  11123. (fy1 "%dy %dd")
  11124. (fd "%dd %02d:%02d")
  11125. (fd1 "%dd")
  11126. (fh "%02d:%02d")
  11127. y d h m align)
  11128. (if havetime
  11129. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11130. y 0
  11131. d (floor (/ diff ds)) diff (mod diff ds)
  11132. h (floor (/ diff hs)) diff (mod diff hs)
  11133. m (floor (/ diff 60)))
  11134. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11135. y 0
  11136. d (floor (+ (/ diff ds) 0.5))
  11137. h 0 m 0))
  11138. (if (not to-buffer)
  11139. (message "%s" (org-make-tdiff-string y d h m))
  11140. (if (org-at-table-p)
  11141. (progn
  11142. (goto-char match-end)
  11143. (setq align t)
  11144. (and (looking-at " *|") (goto-char (match-end 0))))
  11145. (goto-char match-end))
  11146. (if (looking-at
  11147. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  11148. (replace-match ""))
  11149. (if negative (insert " -"))
  11150. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  11151. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  11152. (insert " " (format fh h m))))
  11153. (if align (org-table-align))
  11154. (message "Time difference inserted")))))
  11155. (defun org-make-tdiff-string (y d h m)
  11156. (let ((fmt "")
  11157. (l nil))
  11158. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  11159. l (push y l)))
  11160. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  11161. l (push d l)))
  11162. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  11163. l (push h l)))
  11164. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  11165. l (push m l)))
  11166. (apply 'format fmt (nreverse l))))
  11167. (defun org-time-string-to-time (s)
  11168. (apply 'encode-time (org-parse-time-string s)))
  11169. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  11170. "Convert a time stamp to an absolute day number.
  11171. If there is a specifyer for a cyclic time stamp, get the closest date to
  11172. DAYNR.
  11173. PREFER and SHOW_ALL are passed through to `org-closest-date'."
  11174. (cond
  11175. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  11176. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  11177. daynr
  11178. (+ daynr 1000)))
  11179. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  11180. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  11181. (time-to-days (current-time))) (match-string 0 s)
  11182. prefer show-all))
  11183. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  11184. (defun org-time-from-absolute (d)
  11185. "Return the time corresponding to date D.
  11186. D may be an absolute day number, or a calendar-type list (month day year)."
  11187. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  11188. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  11189. (defun org-calendar-holiday ()
  11190. "List of holidays, for Diary display in Org-mode."
  11191. (require 'holidays)
  11192. (let ((hl (funcall
  11193. (if (fboundp 'calendar-check-holidays)
  11194. 'calendar-check-holidays 'check-calendar-holidays) date)))
  11195. (if hl (mapconcat 'identity hl "; "))))
  11196. (defun org-diary-sexp-entry (sexp entry date)
  11197. "Process a SEXP diary ENTRY for DATE."
  11198. (require 'diary-lib)
  11199. (let ((result (if calendar-debug-sexp
  11200. (let ((stack-trace-on-error t))
  11201. (eval (car (read-from-string sexp))))
  11202. (condition-case nil
  11203. (eval (car (read-from-string sexp)))
  11204. (error
  11205. (beep)
  11206. (message "Bad sexp at line %d in %s: %s"
  11207. (org-current-line)
  11208. (buffer-file-name) sexp)
  11209. (sleep-for 2))))))
  11210. (cond ((stringp result) result)
  11211. ((and (consp result)
  11212. (stringp (cdr result))) (cdr result))
  11213. (result entry)
  11214. (t nil))))
  11215. (defun org-diary-to-ical-string (frombuf)
  11216. "Get iCalendar entries from diary entries in buffer FROMBUF.
  11217. This uses the icalendar.el library."
  11218. (let* ((tmpdir (if (featurep 'xemacs)
  11219. (temp-directory)
  11220. temporary-file-directory))
  11221. (tmpfile (make-temp-name
  11222. (expand-file-name "orgics" tmpdir)))
  11223. buf rtn b e)
  11224. (save-excursion
  11225. (set-buffer frombuf)
  11226. (icalendar-export-region (point-min) (point-max) tmpfile)
  11227. (setq buf (find-buffer-visiting tmpfile))
  11228. (set-buffer buf)
  11229. (goto-char (point-min))
  11230. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  11231. (setq b (match-beginning 0)))
  11232. (goto-char (point-max))
  11233. (if (re-search-backward "^END:VEVENT" nil t)
  11234. (setq e (match-end 0)))
  11235. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  11236. (kill-buffer buf)
  11237. (kill-buffer frombuf)
  11238. (delete-file tmpfile)
  11239. rtn))
  11240. (defun org-closest-date (start current change prefer show-all)
  11241. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  11242. When PREFER is `past' return a date that is either CURRENT or past.
  11243. When PREFER is `future', return a date that is either CURRENT or future.
  11244. When SHOW-ALL is nil, only return the current occurence of a time stamp."
  11245. ;; Make the proper lists from the dates
  11246. (catch 'exit
  11247. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  11248. dn dw sday cday n1 n2
  11249. d m y y1 y2 date1 date2 nmonths nm ny m2)
  11250. (setq start (org-date-to-gregorian start)
  11251. current (org-date-to-gregorian
  11252. (if show-all
  11253. current
  11254. (time-to-days (current-time))))
  11255. sday (calendar-absolute-from-gregorian start)
  11256. cday (calendar-absolute-from-gregorian current))
  11257. (if (<= cday sday) (throw 'exit sday))
  11258. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  11259. (setq dn (string-to-number (match-string 1 change))
  11260. dw (cdr (assoc (match-string 2 change) a1)))
  11261. (error "Invalid change specifyer: %s" change))
  11262. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  11263. (cond
  11264. ((eq dw 'day)
  11265. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  11266. n2 (+ n1 dn)))
  11267. ((eq dw 'year)
  11268. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  11269. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  11270. (setq date1 (list m d y1)
  11271. n1 (calendar-absolute-from-gregorian date1)
  11272. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  11273. n2 (calendar-absolute-from-gregorian date2)))
  11274. ((eq dw 'month)
  11275. ;; approx number of month between the tow dates
  11276. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  11277. ;; How often does dn fit in there?
  11278. (setq d (nth 1 start) m (car start) y (nth 2 start)
  11279. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  11280. m (+ m nm)
  11281. ny (floor (/ m 12))
  11282. y (+ y ny)
  11283. m (- m (* ny 12)))
  11284. (while (> m 12) (setq m (- m 12) y (1+ y)))
  11285. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  11286. (setq m2 (+ m dn) y2 y)
  11287. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11288. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  11289. (while (< n2 cday)
  11290. (setq n1 n2 m m2 y y2)
  11291. (setq m2 (+ m dn) y2 y)
  11292. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11293. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  11294. (if show-all
  11295. (cond
  11296. ((eq prefer 'past) n1)
  11297. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11298. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  11299. (cond
  11300. ((eq prefer 'past) n1)
  11301. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11302. (t (if (= cday n1) n1 n2)))))))
  11303. (defun org-date-to-gregorian (date)
  11304. "Turn any specification of DATE into a gregorian date for the calendar."
  11305. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  11306. ((and (listp date) (= (length date) 3)) date)
  11307. ((stringp date)
  11308. (setq date (org-parse-time-string date))
  11309. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  11310. ((listp date)
  11311. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  11312. (defun org-parse-time-string (s &optional nodefault)
  11313. "Parse the standard Org-mode time string.
  11314. This should be a lot faster than the normal `parse-time-string'.
  11315. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  11316. hour and minute fields will be nil if not given."
  11317. (if (string-match org-ts-regexp0 s)
  11318. (list 0
  11319. (if (or (match-beginning 8) (not nodefault))
  11320. (string-to-number (or (match-string 8 s) "0")))
  11321. (if (or (match-beginning 7) (not nodefault))
  11322. (string-to-number (or (match-string 7 s) "0")))
  11323. (string-to-number (match-string 4 s))
  11324. (string-to-number (match-string 3 s))
  11325. (string-to-number (match-string 2 s))
  11326. nil nil nil)
  11327. (make-list 9 0)))
  11328. (defun org-timestamp-up (&optional arg)
  11329. "Increase the date item at the cursor by one.
  11330. If the cursor is on the year, change the year. If it is on the month or
  11331. the day, change that.
  11332. With prefix ARG, change by that many units."
  11333. (interactive "p")
  11334. (org-timestamp-change (prefix-numeric-value arg)))
  11335. (defun org-timestamp-down (&optional arg)
  11336. "Decrease the date item at the cursor by one.
  11337. If the cursor is on the year, change the year. If it is on the month or
  11338. the day, change that.
  11339. With prefix ARG, change by that many units."
  11340. (interactive "p")
  11341. (org-timestamp-change (- (prefix-numeric-value arg))))
  11342. (defun org-timestamp-up-day (&optional arg)
  11343. "Increase the date in the time stamp by one day.
  11344. With prefix ARG, change that many days."
  11345. (interactive "p")
  11346. (if (and (not (org-at-timestamp-p t))
  11347. (org-on-heading-p))
  11348. (org-todo 'up)
  11349. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  11350. (defun org-timestamp-down-day (&optional arg)
  11351. "Decrease the date in the time stamp by one day.
  11352. With prefix ARG, change that many days."
  11353. (interactive "p")
  11354. (if (and (not (org-at-timestamp-p t))
  11355. (org-on-heading-p))
  11356. (org-todo 'down)
  11357. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  11358. (defun org-at-timestamp-p (&optional inactive-ok)
  11359. "Determine if the cursor is in or at a timestamp."
  11360. (interactive)
  11361. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  11362. (pos (point))
  11363. (ans (or (looking-at tsr)
  11364. (save-excursion
  11365. (skip-chars-backward "^[<\n\r\t")
  11366. (if (> (point) (point-min)) (backward-char 1))
  11367. (and (looking-at tsr)
  11368. (> (- (match-end 0) pos) -1))))))
  11369. (and ans
  11370. (boundp 'org-ts-what)
  11371. (setq org-ts-what
  11372. (cond
  11373. ((= pos (match-beginning 0)) 'bracket)
  11374. ((= pos (1- (match-end 0))) 'bracket)
  11375. ((org-pos-in-match-range pos 2) 'year)
  11376. ((org-pos-in-match-range pos 3) 'month)
  11377. ((org-pos-in-match-range pos 7) 'hour)
  11378. ((org-pos-in-match-range pos 8) 'minute)
  11379. ((or (org-pos-in-match-range pos 4)
  11380. (org-pos-in-match-range pos 5)) 'day)
  11381. ((and (> pos (or (match-end 8) (match-end 5)))
  11382. (< pos (match-end 0)))
  11383. (- pos (or (match-end 8) (match-end 5))))
  11384. (t 'day))))
  11385. ans))
  11386. (defun org-toggle-timestamp-type ()
  11387. "Toggle the type (<active> or [inactive]) of a time stamp."
  11388. (interactive)
  11389. (when (org-at-timestamp-p t)
  11390. (save-excursion
  11391. (goto-char (match-beginning 0))
  11392. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  11393. (goto-char (1- (match-end 0)))
  11394. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  11395. (message "Timestamp is now %sactive"
  11396. (if (equal (char-before) ?>) "in" ""))))
  11397. (defun org-timestamp-change (n &optional what)
  11398. "Change the date in the time stamp at point.
  11399. The date will be changed by N times WHAT. WHAT can be `day', `month',
  11400. `year', `minute', `second'. If WHAT is not given, the cursor position
  11401. in the timestamp determines what will be changed."
  11402. (let ((pos (point))
  11403. with-hm inactive
  11404. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  11405. org-ts-what
  11406. extra rem
  11407. ts time time0)
  11408. (if (not (org-at-timestamp-p t))
  11409. (error "Not at a timestamp"))
  11410. (if (and (not what) (eq org-ts-what 'bracket))
  11411. (org-toggle-timestamp-type)
  11412. (if (and (not what) (not (eq org-ts-what 'day))
  11413. org-display-custom-times
  11414. (get-text-property (point) 'display)
  11415. (not (get-text-property (1- (point)) 'display)))
  11416. (setq org-ts-what 'day))
  11417. (setq org-ts-what (or what org-ts-what)
  11418. inactive (= (char-after (match-beginning 0)) ?\[)
  11419. ts (match-string 0))
  11420. (replace-match "")
  11421. (if (string-match
  11422. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  11423. ts)
  11424. (setq extra (match-string 1 ts)))
  11425. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  11426. (setq with-hm t))
  11427. (setq time0 (org-parse-time-string ts))
  11428. (when (and (eq org-ts-what 'minute)
  11429. (eq current-prefix-arg nil))
  11430. (setq n (* dm (org-no-warnings (signum n))))
  11431. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  11432. (setcar (cdr time0) (+ (nth 1 time0)
  11433. (if (> n 0) (- rem) (- dm rem))))))
  11434. (setq time
  11435. (encode-time (or (car time0) 0)
  11436. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  11437. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  11438. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  11439. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  11440. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  11441. (nthcdr 6 time0)))
  11442. (when (integerp org-ts-what)
  11443. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  11444. (if (eq what 'calendar)
  11445. (let ((cal-date (org-get-date-from-calendar)))
  11446. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  11447. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  11448. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  11449. (setcar time0 (or (car time0) 0))
  11450. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  11451. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  11452. (setq time (apply 'encode-time time0))))
  11453. (setq org-last-changed-timestamp
  11454. (org-insert-time-stamp time with-hm inactive nil nil extra))
  11455. (org-clock-update-time-maybe)
  11456. (goto-char pos)
  11457. ;; Try to recenter the calendar window, if any
  11458. (if (and org-calendar-follow-timestamp-change
  11459. (get-buffer-window "*Calendar*" t)
  11460. (memq org-ts-what '(day month year)))
  11461. (org-recenter-calendar (time-to-days time))))))
  11462. ;; FIXME: does not yet work for lead times
  11463. (defun org-modify-ts-extra (s pos n dm)
  11464. "Change the different parts of the lead-time and repeat fields in timestamp."
  11465. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  11466. ng h m new rem)
  11467. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  11468. (cond
  11469. ((or (org-pos-in-match-range pos 2)
  11470. (org-pos-in-match-range pos 3))
  11471. (setq m (string-to-number (match-string 3 s))
  11472. h (string-to-number (match-string 2 s)))
  11473. (if (org-pos-in-match-range pos 2)
  11474. (setq h (+ h n))
  11475. (setq n (* dm (org-no-warnings (signum n))))
  11476. (when (not (= 0 (setq rem (% m dm))))
  11477. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  11478. (setq m (+ m n)))
  11479. (if (< m 0) (setq m (+ m 60) h (1- h)))
  11480. (if (> m 59) (setq m (- m 60) h (1+ h)))
  11481. (setq h (min 24 (max 0 h)))
  11482. (setq ng 1 new (format "-%02d:%02d" h m)))
  11483. ((org-pos-in-match-range pos 6)
  11484. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  11485. ((org-pos-in-match-range pos 5)
  11486. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  11487. ((org-pos-in-match-range pos 9)
  11488. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  11489. ((org-pos-in-match-range pos 8)
  11490. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  11491. (when ng
  11492. (setq s (concat
  11493. (substring s 0 (match-beginning ng))
  11494. new
  11495. (substring s (match-end ng))))))
  11496. s))
  11497. (defun org-recenter-calendar (date)
  11498. "If the calendar is visible, recenter it to DATE."
  11499. (let* ((win (selected-window))
  11500. (cwin (get-buffer-window "*Calendar*" t))
  11501. (calendar-move-hook nil))
  11502. (when cwin
  11503. (select-window cwin)
  11504. (calendar-goto-date (if (listp date) date
  11505. (calendar-gregorian-from-absolute date)))
  11506. (select-window win))))
  11507. (defun org-goto-calendar (&optional arg)
  11508. "Go to the Emacs calendar at the current date.
  11509. If there is a time stamp in the current line, go to that date.
  11510. A prefix ARG can be used to force the current date."
  11511. (interactive "P")
  11512. (let ((tsr org-ts-regexp) diff
  11513. (calendar-move-hook nil)
  11514. (view-calendar-holidays-initially nil)
  11515. (view-diary-entries-initially nil))
  11516. (if (or (org-at-timestamp-p)
  11517. (save-excursion
  11518. (beginning-of-line 1)
  11519. (looking-at (concat ".*" tsr))))
  11520. (let ((d1 (time-to-days (current-time)))
  11521. (d2 (time-to-days
  11522. (org-time-string-to-time (match-string 1)))))
  11523. (setq diff (- d2 d1))))
  11524. (calendar)
  11525. (calendar-goto-today)
  11526. (if (and diff (not arg)) (calendar-forward-day diff))))
  11527. (defun org-get-date-from-calendar ()
  11528. "Return a list (month day year) of date at point in calendar."
  11529. (with-current-buffer "*Calendar*"
  11530. (save-match-data
  11531. (calendar-cursor-to-date))))
  11532. (defun org-date-from-calendar ()
  11533. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  11534. If there is already a time stamp at the cursor position, update it."
  11535. (interactive)
  11536. (if (org-at-timestamp-p t)
  11537. (org-timestamp-change 0 'calendar)
  11538. (let ((cal-date (org-get-date-from-calendar)))
  11539. (org-insert-time-stamp
  11540. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  11541. ;;; The clock for measuring work time.
  11542. (defvar org-mode-line-string "")
  11543. (put 'org-mode-line-string 'risky-local-variable t)
  11544. (defvar org-mode-line-timer nil)
  11545. (defvar org-clock-heading "")
  11546. (defvar org-clock-start-time "")
  11547. (defun org-update-mode-line ()
  11548. (let* ((delta (- (time-to-seconds (current-time))
  11549. (time-to-seconds org-clock-start-time)))
  11550. (h (floor delta 3600))
  11551. (m (floor (- delta (* 3600 h)) 60)))
  11552. (setq org-mode-line-string
  11553. (propertize (format "-[%d:%02d (%s)]" h m org-clock-heading)
  11554. 'help-echo "Org-mode clock is running"))
  11555. (force-mode-line-update)))
  11556. (defvar org-clock-marker (make-marker)
  11557. "Marker recording the last clock-in.")
  11558. (defvar org-clock-mode-line-entry nil
  11559. "Information for the modeline about the running clock.")
  11560. (defun org-clock-in ()
  11561. "Start the clock on the current item.
  11562. If necessary, clock-out of the currently active clock."
  11563. (interactive)
  11564. (org-clock-out t)
  11565. (let (ts)
  11566. (save-excursion
  11567. (org-back-to-heading t)
  11568. (when (and org-clock-in-switch-to-state
  11569. (not (looking-at (concat outline-regexp "[ \t]*"
  11570. org-clock-in-switch-to-state
  11571. "\\>"))))
  11572. (org-todo org-clock-in-switch-to-state))
  11573. (if (and org-clock-heading-function
  11574. (functionp org-clock-heading-function))
  11575. (setq org-clock-heading (funcall org-clock-heading-function))
  11576. (if (looking-at org-complex-heading-regexp)
  11577. (setq org-clock-heading (match-string 4))
  11578. (setq org-clock-heading "???")))
  11579. (setq org-clock-heading (propertize org-clock-heading 'face nil))
  11580. (org-clock-find-position)
  11581. (insert "\n") (backward-char 1)
  11582. (indent-relative)
  11583. (insert org-clock-string " ")
  11584. (setq org-clock-start-time (current-time))
  11585. (setq ts (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  11586. (move-marker org-clock-marker (point) (buffer-base-buffer))
  11587. (or global-mode-string (setq global-mode-string '("")))
  11588. (or (memq 'org-mode-line-string global-mode-string)
  11589. (setq global-mode-string
  11590. (append global-mode-string '(org-mode-line-string))))
  11591. (org-update-mode-line)
  11592. (setq org-mode-line-timer (run-with-timer 60 60 'org-update-mode-line))
  11593. (message "Clock started at %s" ts))))
  11594. (defun org-clock-find-position ()
  11595. "Find the location where the next clock line should be inserted."
  11596. (org-back-to-heading t)
  11597. (catch 'exit
  11598. (let ((beg (save-excursion
  11599. (beginning-of-line 2)
  11600. (or (bolp) (newline))
  11601. (point)))
  11602. (end (progn (outline-next-heading) (point)))
  11603. (re (concat "^[ \t]*" org-clock-string))
  11604. (cnt 0)
  11605. first last)
  11606. (goto-char beg)
  11607. (when (eobp) (newline) (setq end (max (point) end)))
  11608. (when (re-search-forward "^[ \t]*:CLOCK:" end t)
  11609. ;; we seem to have a CLOCK drawer, so go there.
  11610. (beginning-of-line 2)
  11611. (throw 'exit t))
  11612. ;; Lets count the CLOCK lines
  11613. (goto-char beg)
  11614. (while (re-search-forward re end t)
  11615. (setq first (or first (match-beginning 0))
  11616. last (match-beginning 0)
  11617. cnt (1+ cnt)))
  11618. (when (and (integerp org-clock-into-drawer)
  11619. (>= (1+ cnt) org-clock-into-drawer))
  11620. ;; Wrap current entries into a new drawer
  11621. (goto-char last)
  11622. (beginning-of-line 2)
  11623. (if (org-at-item-p) (org-end-of-item))
  11624. (insert ":END:\n")
  11625. (beginning-of-line 0)
  11626. (org-indent-line-function)
  11627. (goto-char first)
  11628. (insert ":CLOCK:\n")
  11629. (beginning-of-line 0)
  11630. (org-indent-line-function)
  11631. (org-flag-drawer t)
  11632. (beginning-of-line 2)
  11633. (throw 'exit nil))
  11634. (goto-char beg)
  11635. (while (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  11636. (not (equal (match-string 1) org-clock-string)))
  11637. ;; Planning info, skip to after it
  11638. (beginning-of-line 2)
  11639. (or (bolp) (newline)))
  11640. (when (eq t org-clock-into-drawer)
  11641. (insert ":CLOCK:\n:END:\n")
  11642. (beginning-of-line -1)
  11643. (org-indent-line-function)
  11644. (org-flag-drawer t)
  11645. (beginning-of-line 2)
  11646. (org-indent-line-function)))))
  11647. (defun org-clock-out (&optional fail-quietly)
  11648. "Stop the currently running clock.
  11649. If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
  11650. (interactive)
  11651. (catch 'exit
  11652. (if (not (marker-buffer org-clock-marker))
  11653. (if fail-quietly (throw 'exit t) (error "No active clock")))
  11654. (let (ts te s h m)
  11655. (save-excursion
  11656. (set-buffer (marker-buffer org-clock-marker))
  11657. (goto-char org-clock-marker)
  11658. (beginning-of-line 1)
  11659. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  11660. (equal (match-string 1) org-clock-string))
  11661. (setq ts (match-string 2))
  11662. (if fail-quietly (throw 'exit nil) (error "Clock start time is gone")))
  11663. (goto-char (match-end 0))
  11664. (delete-region (point) (point-at-eol))
  11665. (insert "--")
  11666. (setq te (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  11667. (setq s (- (time-to-seconds (apply 'encode-time (org-parse-time-string te)))
  11668. (time-to-seconds (apply 'encode-time (org-parse-time-string ts))))
  11669. h (floor (/ s 3600))
  11670. s (- s (* 3600 h))
  11671. m (floor (/ s 60))
  11672. s (- s (* 60 s)))
  11673. (insert " => " (format "%2d:%02d" h m))
  11674. (move-marker org-clock-marker nil)
  11675. (when org-log-note-clock-out
  11676. (org-add-log-maybe 'clock-out))
  11677. (when org-mode-line-timer
  11678. (cancel-timer org-mode-line-timer)
  11679. (setq org-mode-line-timer nil))
  11680. (setq global-mode-string
  11681. (delq 'org-mode-line-string global-mode-string))
  11682. (force-mode-line-update)
  11683. (message "Clock stopped at %s after HH:MM = %d:%02d" te h m)))))
  11684. (defun org-clock-cancel ()
  11685. "Cancel the running clock be removing the start timestamp."
  11686. (interactive)
  11687. (if (not (marker-buffer org-clock-marker))
  11688. (error "No active clock"))
  11689. (save-excursion
  11690. (set-buffer (marker-buffer org-clock-marker))
  11691. (goto-char org-clock-marker)
  11692. (delete-region (1- (point-at-bol)) (point-at-eol)))
  11693. (setq global-mode-string
  11694. (delq 'org-mode-line-string global-mode-string))
  11695. (force-mode-line-update)
  11696. (message "Clock canceled"))
  11697. (defun org-clock-goto (&optional delete-windows)
  11698. "Go to the currently clocked-in entry."
  11699. (interactive "P")
  11700. (if (not (marker-buffer org-clock-marker))
  11701. (error "No active clock"))
  11702. (switch-to-buffer-other-window
  11703. (marker-buffer org-clock-marker))
  11704. (if delete-windows (delete-other-windows))
  11705. (goto-char org-clock-marker)
  11706. (org-show-entry)
  11707. (org-back-to-heading)
  11708. (recenter))
  11709. (defvar org-clock-file-total-minutes nil
  11710. "Holds the file total time in minutes, after a call to `org-clock-sum'.")
  11711. (make-variable-buffer-local 'org-clock-file-total-minutes)
  11712. (defun org-clock-sum (&optional tstart tend)
  11713. "Sum the times for each subtree.
  11714. Puts the resulting times in minutes as a text property on each headline."
  11715. (interactive)
  11716. (let* ((bmp (buffer-modified-p))
  11717. (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
  11718. org-clock-string
  11719. "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
  11720. (lmax 30)
  11721. (ltimes (make-vector lmax 0))
  11722. (t1 0)
  11723. (level 0)
  11724. ts te dt
  11725. time)
  11726. (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t))
  11727. (save-excursion
  11728. (goto-char (point-max))
  11729. (while (re-search-backward re nil t)
  11730. (cond
  11731. ((match-end 2)
  11732. ;; Two time stamps
  11733. (setq ts (match-string 2)
  11734. te (match-string 3)
  11735. ts (time-to-seconds
  11736. (apply 'encode-time (org-parse-time-string ts)))
  11737. te (time-to-seconds
  11738. (apply 'encode-time (org-parse-time-string te)))
  11739. ts (if tstart (max ts tstart) ts)
  11740. te (if tend (min te tend) te)
  11741. dt (- te ts)
  11742. t1 (if (> dt 0) (+ t1 (floor (/ dt 60))) t1)))
  11743. ((match-end 4)
  11744. ;; A naket time
  11745. (setq t1 (+ t1 (string-to-number (match-string 5))
  11746. (* 60 (string-to-number (match-string 4))))))
  11747. (t ;; A headline
  11748. (setq level (- (match-end 1) (match-beginning 1)))
  11749. (when (or (> t1 0) (> (aref ltimes level) 0))
  11750. (loop for l from 0 to level do
  11751. (aset ltimes l (+ (aref ltimes l) t1)))
  11752. (setq t1 0 time (aref ltimes level))
  11753. (loop for l from level to (1- lmax) do
  11754. (aset ltimes l 0))
  11755. (goto-char (match-beginning 0))
  11756. (put-text-property (point) (point-at-eol) :org-clock-minutes time)))))
  11757. (setq org-clock-file-total-minutes (aref ltimes 0)))
  11758. (set-buffer-modified-p bmp)))
  11759. (defun org-clock-display (&optional total-only)
  11760. "Show subtree times in the entire buffer.
  11761. If TOTAL-ONLY is non-nil, only show the total time for the entire file
  11762. in the echo area."
  11763. (interactive)
  11764. (org-remove-clock-overlays)
  11765. (let (time h m p)
  11766. (org-clock-sum)
  11767. (unless total-only
  11768. (save-excursion
  11769. (goto-char (point-min))
  11770. (while (or (and (equal (setq p (point)) (point-min))
  11771. (get-text-property p :org-clock-minutes))
  11772. (setq p (next-single-property-change
  11773. (point) :org-clock-minutes)))
  11774. (goto-char p)
  11775. (when (setq time (get-text-property p :org-clock-minutes))
  11776. (org-put-clock-overlay time (funcall outline-level))))
  11777. (setq h (/ org-clock-file-total-minutes 60)
  11778. m (- org-clock-file-total-minutes (* 60 h)))
  11779. ;; Arrange to remove the overlays upon next change.
  11780. (when org-remove-highlights-with-change
  11781. (org-add-hook 'before-change-functions 'org-remove-clock-overlays
  11782. nil 'local))))
  11783. (message "Total file time: %d:%02d (%d hours and %d minutes)" h m h m)))
  11784. (defvar org-clock-overlays nil)
  11785. (make-variable-buffer-local 'org-clock-overlays)
  11786. (defun org-put-clock-overlay (time &optional level)
  11787. "Put an overlays on the current line, displaying TIME.
  11788. If LEVEL is given, prefix time with a corresponding number of stars.
  11789. This creates a new overlay and stores it in `org-clock-overlays', so that it
  11790. will be easy to remove."
  11791. (let* ((c 60) (h (floor (/ time 60))) (m (- time (* 60 h)))
  11792. (l (if level (org-get-valid-level level 0) 0))
  11793. (off 0)
  11794. ov tx)
  11795. (move-to-column c)
  11796. (unless (eolp) (skip-chars-backward "^ \t"))
  11797. (skip-chars-backward " \t")
  11798. (setq ov (org-make-overlay (1- (point)) (point-at-eol))
  11799. tx (concat (buffer-substring (1- (point)) (point))
  11800. (make-string (+ off (max 0 (- c (current-column)))) ?.)
  11801. (org-add-props (format "%s %2d:%02d%s"
  11802. (make-string l ?*) h m
  11803. (make-string (- 16 l) ?\ ))
  11804. '(face secondary-selection))
  11805. ""))
  11806. (if (not (featurep 'xemacs))
  11807. (org-overlay-put ov 'display tx)
  11808. (org-overlay-put ov 'invisible t)
  11809. (org-overlay-put ov 'end-glyph (make-glyph tx)))
  11810. (push ov org-clock-overlays)))
  11811. (defun org-remove-clock-overlays (&optional beg end noremove)
  11812. "Remove the occur highlights from the buffer.
  11813. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11814. from the `before-change-functions' in the current buffer."
  11815. (interactive)
  11816. (unless org-inhibit-highlight-removal
  11817. (mapc 'org-delete-overlay org-clock-overlays)
  11818. (setq org-clock-overlays nil)
  11819. (unless noremove
  11820. (remove-hook 'before-change-functions
  11821. 'org-remove-clock-overlays 'local))))
  11822. (defun org-clock-out-if-current ()
  11823. "Clock out if the current entry contains the running clock.
  11824. This is used to stop the clock after a TODO entry is marked DONE,
  11825. and is only done if the variable `org-clock-out-when-done' is not nil."
  11826. (when (and org-clock-out-when-done
  11827. (member state org-done-keywords)
  11828. (equal (marker-buffer org-clock-marker) (current-buffer))
  11829. (< (point) org-clock-marker)
  11830. (> (save-excursion (outline-next-heading) (point))
  11831. org-clock-marker))
  11832. ;; Clock out, but don't accept a logging message for this.
  11833. (let ((org-log-note-clock-out nil))
  11834. (org-clock-out))))
  11835. (add-hook 'org-after-todo-state-change-hook
  11836. 'org-clock-out-if-current)
  11837. (defun org-check-running-clock ()
  11838. "Check if the current buffer contains the running clock.
  11839. If yes, offer to stop it and to save the buffer with the changes."
  11840. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  11841. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  11842. (buffer-name))))
  11843. (org-clock-out)
  11844. (when (y-or-n-p "Save changed buffer?")
  11845. (save-buffer))))
  11846. (defun org-clock-report (&optional arg)
  11847. "Create a table containing a report about clocked time.
  11848. If the cursor is inside an existing clocktable block, then the table
  11849. will be updated. If not, a new clocktable will be inserted.
  11850. When called with a prefix argument, move to the first clock table in the
  11851. buffer and update it."
  11852. (interactive "P")
  11853. (org-remove-clock-overlays)
  11854. (when arg
  11855. (org-find-dblock "clocktable")
  11856. (org-show-entry))
  11857. (if (org-in-clocktable-p)
  11858. (goto-char (org-in-clocktable-p))
  11859. (org-create-dblock (list :name "clocktable"
  11860. :maxlevel 2 :scope 'file)))
  11861. (org-update-dblock))
  11862. (defun org-in-clocktable-p ()
  11863. "Check if the cursor is in a clocktable."
  11864. (let ((pos (point)) start)
  11865. (save-excursion
  11866. (end-of-line 1)
  11867. (and (re-search-backward "^#\\+BEGIN:[ \t]+clocktable" nil t)
  11868. (setq start (match-beginning 0))
  11869. (re-search-forward "^#\\+END:.*" nil t)
  11870. (>= (match-end 0) pos)
  11871. start))))
  11872. (defun org-clock-update-time-maybe ()
  11873. "If this is a CLOCK line, update it and return t.
  11874. Otherwise, return nil."
  11875. (interactive)
  11876. (save-excursion
  11877. (beginning-of-line 1)
  11878. (skip-chars-forward " \t")
  11879. (when (looking-at org-clock-string)
  11880. (let ((re (concat "[ \t]*" org-clock-string
  11881. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  11882. "\\([ \t]*=>.*\\)?"))
  11883. ts te h m s)
  11884. (if (not (looking-at re))
  11885. nil
  11886. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  11887. (end-of-line 1)
  11888. (setq ts (match-string 1)
  11889. te (match-string 2))
  11890. (setq s (- (time-to-seconds
  11891. (apply 'encode-time (org-parse-time-string te)))
  11892. (time-to-seconds
  11893. (apply 'encode-time (org-parse-time-string ts))))
  11894. h (floor (/ s 3600))
  11895. s (- s (* 3600 h))
  11896. m (floor (/ s 60))
  11897. s (- s (* 60 s)))
  11898. (insert " => " (format "%2d:%02d" h m))
  11899. t)))))
  11900. (defun org-clock-special-range (key &optional time as-strings)
  11901. "Return two times bordering a special time range.
  11902. Key is a symbol specifying the range and can be one of `today', `yesterday',
  11903. `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
  11904. A week starts Monday 0:00 and ends Sunday 24:00.
  11905. The range is determined relative to TIME. TIME defaults to the current time.
  11906. The return value is a cons cell with two internal times like the ones
  11907. returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
  11908. the returned times will be formatted strings."
  11909. (let* ((tm (decode-time (or time (current-time))))
  11910. (s 0) (m (nth 1 tm)) (h (nth 2 tm))
  11911. (d (nth 3 tm)) (month (nth 4 tm)) (y (nth 5 tm))
  11912. (dow (nth 6 tm))
  11913. s1 m1 h1 d1 month1 y1 diff ts te fm)
  11914. (cond
  11915. ((eq key 'today)
  11916. (setq h 0 m 0 h1 24 m1 0))
  11917. ((eq key 'yesterday)
  11918. (setq d (1- d) h 0 m 0 h1 24 m1 0))
  11919. ((eq key 'thisweek)
  11920. (setq diff (if (= dow 0) 6 (1- dow))
  11921. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  11922. ((eq key 'lastweek)
  11923. (setq diff (+ 7 (if (= dow 0) 6 (1- dow)))
  11924. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  11925. ((eq key 'thismonth)
  11926. (setq d 1 h 0 m 0 d1 1 month1 (1+ month) h1 0 m1 0))
  11927. ((eq key 'lastmonth)
  11928. (setq d 1 h 0 m 0 d1 1 month (1- month) month1 (1+ month) h1 0 m1 0))
  11929. ((eq key 'thisyear)
  11930. (setq m 0 h 0 d 1 month 1 y1 (1+ y)))
  11931. ((eq key 'lastyear)
  11932. (setq m 0 h 0 d 1 month 1 y (1- y) y1 (1+ y)))
  11933. (t (error "No such time block %s" key)))
  11934. (setq ts (encode-time s m h d month y)
  11935. te (encode-time (or s1 s) (or m1 m) (or h1 h)
  11936. (or d1 d) (or month1 month) (or y1 y)))
  11937. (setq fm (cdr org-time-stamp-formats))
  11938. (if as-strings
  11939. (cons (format-time-string fm ts) (format-time-string fm te))
  11940. (cons ts te))))
  11941. (defun org-dblock-write:clocktable (params)
  11942. "Write the standard clocktable."
  11943. (catch 'exit
  11944. (let* ((hlchars '((1 . "*") (2 . "/")))
  11945. (ins (make-marker))
  11946. (total-time nil)
  11947. (scope (plist-get params :scope))
  11948. (tostring (plist-get params :tostring))
  11949. (multifile (plist-get params :multifile))
  11950. (header (plist-get params :header))
  11951. (maxlevel (or (plist-get params :maxlevel) 3))
  11952. (step (plist-get params :step))
  11953. (emph (plist-get params :emphasize))
  11954. (ts (plist-get params :tstart))
  11955. (te (plist-get params :tend))
  11956. (block (plist-get params :block))
  11957. (link (plist-get params :link))
  11958. ipos time h m p level hlc hdl
  11959. cc beg end pos tbl)
  11960. (when step
  11961. (org-clocktable-steps params)
  11962. (throw 'exit nil))
  11963. (when block
  11964. (setq cc (org-clock-special-range block nil t)
  11965. ts (car cc) te (cdr cc)))
  11966. (if ts (setq ts (time-to-seconds
  11967. (apply 'encode-time (org-parse-time-string ts)))))
  11968. (if te (setq te (time-to-seconds
  11969. (apply 'encode-time (org-parse-time-string te)))))
  11970. (move-marker ins (point))
  11971. (setq ipos (point))
  11972. ;; Get the right scope
  11973. (setq pos (point))
  11974. (save-restriction
  11975. (cond
  11976. ((not scope))
  11977. ((eq scope 'file) (widen))
  11978. ((eq scope 'subtree) (org-narrow-to-subtree))
  11979. ((eq scope 'tree)
  11980. (while (org-up-heading-safe))
  11981. (org-narrow-to-subtree))
  11982. ((and (symbolp scope) (string-match "^tree\\([0-9]+\\)$"
  11983. (symbol-name scope)))
  11984. (setq level (string-to-number (match-string 1 (symbol-name scope))))
  11985. (catch 'exit
  11986. (while (org-up-heading-safe)
  11987. (looking-at outline-regexp)
  11988. (if (<= (org-reduced-level (funcall outline-level)) level)
  11989. (throw 'exit nil))))
  11990. (org-narrow-to-subtree))
  11991. ((or (listp scope) (eq scope 'agenda))
  11992. (let* ((files (if (listp scope) scope (org-agenda-files)))
  11993. (scope 'agenda)
  11994. (p1 (copy-sequence params))
  11995. file)
  11996. (plist-put p1 :tostring t)
  11997. (plist-put p1 :multifile t)
  11998. (plist-put p1 :scope 'file)
  11999. (org-prepare-agenda-buffers files)
  12000. (while (setq file (pop files))
  12001. (with-current-buffer (find-buffer-visiting file)
  12002. (push (org-clocktable-add-file
  12003. file (org-dblock-write:clocktable p1)) tbl)
  12004. (setq total-time (+ (or total-time 0)
  12005. org-clock-file-total-minutes)))))))
  12006. (goto-char pos)
  12007. (unless (eq scope 'agenda)
  12008. (org-clock-sum ts te)
  12009. (goto-char (point-min))
  12010. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  12011. (goto-char p)
  12012. (when (setq time (get-text-property p :org-clock-minutes))
  12013. (save-excursion
  12014. (beginning-of-line 1)
  12015. (when (and (looking-at (org-re "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[[:alnum:]_@:]+:\\)?[ \t]*$"))
  12016. (setq level (org-reduced-level
  12017. (- (match-end 1) (match-beginning 1))))
  12018. (<= level maxlevel))
  12019. (setq hlc (if emph (or (cdr (assoc level hlchars)) "") "")
  12020. hdl (if (not link)
  12021. (match-string 2)
  12022. (org-make-link-string
  12023. (format "file:%s::%s"
  12024. (buffer-file-name)
  12025. (save-match-data
  12026. (org-make-org-heading-search-string
  12027. (match-string 2))))
  12028. (match-string 2)))
  12029. h (/ time 60)
  12030. m (- time (* 60 h)))
  12031. (if (and (not multifile) (= level 1)) (push "|-" tbl))
  12032. (push (concat
  12033. "| " (int-to-string level) "|" hlc hdl hlc " |"
  12034. (make-string (1- level) ?|)
  12035. hlc (format "%d:%02d" h m) hlc
  12036. " |") tbl))))))
  12037. (setq tbl (nreverse tbl))
  12038. (if tostring
  12039. (if tbl (mapconcat 'identity tbl "\n") nil)
  12040. (goto-char ins)
  12041. (insert-before-markers
  12042. (or header
  12043. (concat
  12044. "Clock summary at ["
  12045. (substring
  12046. (format-time-string (cdr org-time-stamp-formats))
  12047. 1 -1)
  12048. "]."
  12049. (if block
  12050. (format " Considered range is /%s/." block)
  12051. "")
  12052. "\n\n"))
  12053. (if (eq scope 'agenda) "|File" "")
  12054. "|L|Headline|Time|\n")
  12055. (setq total-time (or total-time org-clock-file-total-minutes)
  12056. h (/ total-time 60)
  12057. m (- total-time (* 60 h)))
  12058. (insert-before-markers
  12059. "|-\n|"
  12060. (if (eq scope 'agenda) "|" "")
  12061. "|"
  12062. "*Total time*| "
  12063. (format "*%d:%02d*" h m)
  12064. "|\n|-\n")
  12065. (setq tbl (delq nil tbl))
  12066. (if (and (stringp (car tbl)) (> (length (car tbl)) 1)
  12067. (equal (substring (car tbl) 0 2) "|-"))
  12068. (pop tbl))
  12069. (insert-before-markers (mapconcat
  12070. 'identity (delq nil tbl)
  12071. (if (eq scope 'agenda) "\n|-\n" "\n")))
  12072. (backward-delete-char 1)
  12073. (goto-char ipos)
  12074. (skip-chars-forward "^|")
  12075. (org-table-align))))))
  12076. (defun org-clocktable-steps (params)
  12077. (let* ((p1 (copy-sequence params))
  12078. (ts (plist-get p1 :tstart))
  12079. (te (plist-get p1 :tend))
  12080. (step0 (plist-get p1 :step))
  12081. (step (cdr (assoc step0 '((day . 86400) (week . 604800)))))
  12082. (block (plist-get p1 :block))
  12083. cc)
  12084. (when block
  12085. (setq cc (org-clock-special-range block nil t)
  12086. ts (car cc) te (cdr cc)))
  12087. (if ts (setq ts (time-to-seconds
  12088. (apply 'encode-time (org-parse-time-string ts)))))
  12089. (if te (setq te (time-to-seconds
  12090. (apply 'encode-time (org-parse-time-string te)))))
  12091. (plist-put p1 :header "")
  12092. (plist-put p1 :step nil)
  12093. (plist-put p1 :block nil)
  12094. (while (< ts te)
  12095. (or (bolp) (insert "\n"))
  12096. (plist-put p1 :tstart (format-time-string
  12097. (car org-time-stamp-formats)
  12098. (seconds-to-time ts)))
  12099. (plist-put p1 :tend (format-time-string
  12100. (car org-time-stamp-formats)
  12101. (seconds-to-time (setq ts (+ ts step)))))
  12102. (insert "\n" (if (eq step0 'day) "Daily report: " "Weekly report starting on: ")
  12103. (plist-get p1 :tstart) "\n")
  12104. (org-dblock-write:clocktable p1)
  12105. (re-search-forward "#\\+END:")
  12106. (end-of-line 0))))
  12107. (defun org-clocktable-add-file (file table)
  12108. (if table
  12109. (let ((lines (org-split-string table "\n"))
  12110. (ff (file-name-nondirectory file)))
  12111. (mapconcat 'identity
  12112. (mapcar (lambda (x)
  12113. (if (string-match org-table-dataline-regexp x)
  12114. (concat "|" ff x)
  12115. x))
  12116. lines)
  12117. "\n"))))
  12118. ;; FIXME: I don't think anybody uses this, ask David
  12119. (defun org-collect-clock-time-entries ()
  12120. "Return an internal list with clocking information.
  12121. This list has one entry for each CLOCK interval.
  12122. FIXME: describe the elements."
  12123. (interactive)
  12124. (let ((re (concat "^[ \t]*" org-clock-string
  12125. " *\\[\\(.*?\\)\\]--\\[\\(.*?\\)\\]"))
  12126. rtn beg end next cont level title total closedp leafp
  12127. clockpos titlepos h m donep)
  12128. (save-excursion
  12129. (org-clock-sum)
  12130. (goto-char (point-min))
  12131. (while (re-search-forward re nil t)
  12132. (setq clockpos (match-beginning 0)
  12133. beg (match-string 1) end (match-string 2)
  12134. cont (match-end 0))
  12135. (setq beg (apply 'encode-time (org-parse-time-string beg))
  12136. end (apply 'encode-time (org-parse-time-string end)))
  12137. (org-back-to-heading t)
  12138. (setq donep (org-entry-is-done-p))
  12139. (setq titlepos (point)
  12140. total (or (get-text-property (1+ (point)) :org-clock-minutes) 0)
  12141. h (/ total 60) m (- total (* 60 h))
  12142. total (cons h m))
  12143. (looking-at "\\(\\*+\\) +\\(.*\\)")
  12144. (setq level (- (match-end 1) (match-beginning 1))
  12145. title (org-match-string-no-properties 2))
  12146. (save-excursion (outline-next-heading) (setq next (point)))
  12147. (setq closedp (re-search-forward org-closed-time-regexp next t))
  12148. (goto-char next)
  12149. (setq leafp (and (looking-at "^\\*+ ")
  12150. (<= (- (match-end 0) (point)) level)))
  12151. (push (list beg end clockpos closedp donep
  12152. total title titlepos level leafp)
  12153. rtn)
  12154. (goto-char cont)))
  12155. (nreverse rtn)))
  12156. ;;;; Agenda files
  12157. (defun org-agenda-files (&optional unrestricted)
  12158. "Get the list of agenda files.
  12159. Optional UNRESTRICTED means return the full list even if a restriction
  12160. is currently in place."
  12161. (let ((files
  12162. (cond
  12163. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  12164. ((stringp org-agenda-files) (org-read-agenda-file-list))
  12165. ((listp org-agenda-files) org-agenda-files)
  12166. (t (error "Invalid value of `org-agenda-files'")))))
  12167. (setq files (apply 'append
  12168. (mapcar (lambda (f)
  12169. (if (file-directory-p f)
  12170. (directory-files f t
  12171. org-agenda-file-regexp)
  12172. (list f)))
  12173. files)))
  12174. (if org-agenda-skip-unavailable-files
  12175. (delq nil
  12176. (mapcar (function
  12177. (lambda (file)
  12178. (and (file-readable-p file) file)))
  12179. files))
  12180. files))) ; `org-check-agenda-file' will remove them from the list
  12181. (defun org-edit-agenda-file-list ()
  12182. "Edit the list of agenda files.
  12183. Depending on setup, this either uses customize to edit the variable
  12184. `org-agenda-files', or it visits the file that is holding the list. In the
  12185. latter case, the buffer is set up in a way that saving it automatically kills
  12186. the buffer and restores the previous window configuration."
  12187. (interactive)
  12188. (if (stringp org-agenda-files)
  12189. (let ((cw (current-window-configuration)))
  12190. (find-file org-agenda-files)
  12191. (org-set-local 'org-window-configuration cw)
  12192. (org-add-hook 'after-save-hook
  12193. (lambda ()
  12194. (set-window-configuration
  12195. (prog1 org-window-configuration
  12196. (kill-buffer (current-buffer))))
  12197. (org-install-agenda-files-menu)
  12198. (message "New agenda file list installed"))
  12199. nil 'local)
  12200. (message "%s" (substitute-command-keys
  12201. "Edit list and finish with \\[save-buffer]")))
  12202. (customize-variable 'org-agenda-files)))
  12203. (defun org-store-new-agenda-file-list (list)
  12204. "Set new value for the agenda file list and save it correcly."
  12205. (if (stringp org-agenda-files)
  12206. (let ((f org-agenda-files) b)
  12207. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  12208. (with-temp-file f
  12209. (insert (mapconcat 'identity list "\n") "\n")))
  12210. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  12211. (setq org-agenda-files list)
  12212. (customize-save-variable 'org-agenda-files org-agenda-files))))
  12213. (defun org-read-agenda-file-list ()
  12214. "Read the list of agenda files from a file."
  12215. (when (stringp org-agenda-files)
  12216. (with-temp-buffer
  12217. (insert-file-contents org-agenda-files)
  12218. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  12219. ;;;###autoload
  12220. (defun org-cycle-agenda-files ()
  12221. "Cycle through the files in `org-agenda-files'.
  12222. If the current buffer visits an agenda file, find the next one in the list.
  12223. If the current buffer does not, find the first agenda file."
  12224. (interactive)
  12225. (let* ((fs (org-agenda-files t))
  12226. (files (append fs (list (car fs))))
  12227. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  12228. file)
  12229. (unless files (error "No agenda files"))
  12230. (catch 'exit
  12231. (while (setq file (pop files))
  12232. (if (equal (file-truename file) tcf)
  12233. (when (car files)
  12234. (find-file (car files))
  12235. (throw 'exit t))))
  12236. (find-file (car fs)))
  12237. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  12238. (defun org-agenda-file-to-front (&optional to-end)
  12239. "Move/add the current file to the top of the agenda file list.
  12240. If the file is not present in the list, it is added to the front. If it is
  12241. present, it is moved there. With optional argument TO-END, add/move to the
  12242. end of the list."
  12243. (interactive "P")
  12244. (let ((org-agenda-skip-unavailable-files nil)
  12245. (file-alist (mapcar (lambda (x)
  12246. (cons (file-truename x) x))
  12247. (org-agenda-files t)))
  12248. (ctf (file-truename buffer-file-name))
  12249. x had)
  12250. (setq x (assoc ctf file-alist) had x)
  12251. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  12252. (if to-end
  12253. (setq file-alist (append (delq x file-alist) (list x)))
  12254. (setq file-alist (cons x (delq x file-alist))))
  12255. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  12256. (org-install-agenda-files-menu)
  12257. (message "File %s to %s of agenda file list"
  12258. (if had "moved" "added") (if to-end "end" "front"))))
  12259. (defun org-remove-file (&optional file)
  12260. "Remove current file from the list of files in variable `org-agenda-files'.
  12261. These are the files which are being checked for agenda entries.
  12262. Optional argument FILE means, use this file instead of the current."
  12263. (interactive)
  12264. (let* ((org-agenda-skip-unavailable-files nil)
  12265. (file (or file buffer-file-name))
  12266. (true-file (file-truename file))
  12267. (afile (abbreviate-file-name file))
  12268. (files (delq nil (mapcar
  12269. (lambda (x)
  12270. (if (equal true-file
  12271. (file-truename x))
  12272. nil x))
  12273. (org-agenda-files t)))))
  12274. (if (not (= (length files) (length (org-agenda-files t))))
  12275. (progn
  12276. (org-store-new-agenda-file-list files)
  12277. (org-install-agenda-files-menu)
  12278. (message "Removed file: %s" afile))
  12279. (message "File was not in list: %s (not removed)" afile))))
  12280. (defun org-file-menu-entry (file)
  12281. (vector file (list 'find-file file) t))
  12282. (defun org-check-agenda-file (file)
  12283. "Make sure FILE exists. If not, ask user what to do."
  12284. (when (not (file-exists-p file))
  12285. (message "non-existent file %s. [R]emove from list or [A]bort?"
  12286. (abbreviate-file-name file))
  12287. (let ((r (downcase (read-char-exclusive))))
  12288. (cond
  12289. ((equal r ?r)
  12290. (org-remove-file file)
  12291. (throw 'nextfile t))
  12292. (t (error "Abort"))))))
  12293. (defun org-get-agenda-file-buffer (file)
  12294. "Get a buffer visiting FILE. If the buffer needs to be created, add
  12295. it to the list of buffers which might be released later."
  12296. (let ((buf (org-find-base-buffer-visiting file)))
  12297. (if buf
  12298. buf ; just return it
  12299. ;; Make a new buffer and remember it
  12300. (setq buf (find-file-noselect file))
  12301. (if buf (push buf org-agenda-new-buffers))
  12302. buf)))
  12303. (defun org-release-buffers (blist)
  12304. "Release all buffers in list, asking the user for confirmation when needed.
  12305. When a buffer is unmodified, it is just killed. When modified, it is saved
  12306. \(if the user agrees) and then killed."
  12307. (let (buf file)
  12308. (while (setq buf (pop blist))
  12309. (setq file (buffer-file-name buf))
  12310. (when (and (buffer-modified-p buf)
  12311. file
  12312. (y-or-n-p (format "Save file %s? " file)))
  12313. (with-current-buffer buf (save-buffer)))
  12314. (kill-buffer buf))))
  12315. (defun org-prepare-agenda-buffers (files)
  12316. "Create buffers for all agenda files, protect archived trees and comments."
  12317. (interactive)
  12318. (let ((pa '(:org-archived t))
  12319. (pc '(:org-comment t))
  12320. (pall '(:org-archived t :org-comment t))
  12321. (inhibit-read-only t)
  12322. (rea (concat ":" org-archive-tag ":"))
  12323. bmp file re)
  12324. (save-excursion
  12325. (save-restriction
  12326. (while (setq file (pop files))
  12327. (if (bufferp file)
  12328. (set-buffer file)
  12329. (org-check-agenda-file file)
  12330. (set-buffer (org-get-agenda-file-buffer file)))
  12331. (widen)
  12332. (setq bmp (buffer-modified-p))
  12333. (org-refresh-category-properties)
  12334. (setq org-todo-keywords-for-agenda
  12335. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  12336. (setq org-done-keywords-for-agenda
  12337. (append org-done-keywords-for-agenda org-done-keywords))
  12338. (save-excursion
  12339. (remove-text-properties (point-min) (point-max) pall)
  12340. (when org-agenda-skip-archived-trees
  12341. (goto-char (point-min))
  12342. (while (re-search-forward rea nil t)
  12343. (if (org-on-heading-p t)
  12344. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  12345. (goto-char (point-min))
  12346. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  12347. (while (re-search-forward re nil t)
  12348. (add-text-properties
  12349. (match-beginning 0) (org-end-of-subtree t) pc)))
  12350. (set-buffer-modified-p bmp))))))
  12351. ;;;; Embedded LaTeX
  12352. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  12353. "Keymap for the minor `org-cdlatex-mode'.")
  12354. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  12355. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  12356. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  12357. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  12358. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  12359. (defvar org-cdlatex-texmathp-advice-is-done nil
  12360. "Flag remembering if we have applied the advice to texmathp already.")
  12361. (define-minor-mode org-cdlatex-mode
  12362. "Toggle the minor `org-cdlatex-mode'.
  12363. This mode supports entering LaTeX environment and math in LaTeX fragments
  12364. in Org-mode.
  12365. \\{org-cdlatex-mode-map}"
  12366. nil " OCDL" nil
  12367. (when org-cdlatex-mode (require 'cdlatex))
  12368. (unless org-cdlatex-texmathp-advice-is-done
  12369. (setq org-cdlatex-texmathp-advice-is-done t)
  12370. (defadvice texmathp (around org-math-always-on activate)
  12371. "Always return t in org-mode buffers.
  12372. This is because we want to insert math symbols without dollars even outside
  12373. the LaTeX math segments. If Orgmode thinks that point is actually inside
  12374. en embedded LaTeX fragement, let texmathp do its job.
  12375. \\[org-cdlatex-mode-map]"
  12376. (interactive)
  12377. (let (p)
  12378. (cond
  12379. ((not (org-mode-p)) ad-do-it)
  12380. ((eq this-command 'cdlatex-math-symbol)
  12381. (setq ad-return-value t
  12382. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  12383. (t
  12384. (let ((p (org-inside-LaTeX-fragment-p)))
  12385. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  12386. (setq ad-return-value t
  12387. texmathp-why '("Org-mode embedded math" . 0))
  12388. (if p ad-do-it)))))))))
  12389. (defun turn-on-org-cdlatex ()
  12390. "Unconditionally turn on `org-cdlatex-mode'."
  12391. (org-cdlatex-mode 1))
  12392. (defun org-inside-LaTeX-fragment-p ()
  12393. "Test if point is inside a LaTeX fragment.
  12394. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  12395. sequence appearing also before point.
  12396. Even though the matchers for math are configurable, this function assumes
  12397. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  12398. delimiters are skipped when they have been removed by customization.
  12399. The return value is nil, or a cons cell with the delimiter and
  12400. and the position of this delimiter.
  12401. This function does a reasonably good job, but can locally be fooled by
  12402. for example currency specifications. For example it will assume being in
  12403. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  12404. fragments that are properly closed, but during editing, we have to live
  12405. with the uncertainty caused by missing closing delimiters. This function
  12406. looks only before point, not after."
  12407. (catch 'exit
  12408. (let ((pos (point))
  12409. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  12410. (lim (progn
  12411. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  12412. (point)))
  12413. dd-on str (start 0) m re)
  12414. (goto-char pos)
  12415. (when dodollar
  12416. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  12417. re (nth 1 (assoc "$" org-latex-regexps)))
  12418. (while (string-match re str start)
  12419. (cond
  12420. ((= (match-end 0) (length str))
  12421. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  12422. ((= (match-end 0) (- (length str) 5))
  12423. (throw 'exit nil))
  12424. (t (setq start (match-end 0))))))
  12425. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  12426. (goto-char pos)
  12427. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  12428. (and (match-beginning 2) (throw 'exit nil))
  12429. ;; count $$
  12430. (while (re-search-backward "\\$\\$" lim t)
  12431. (setq dd-on (not dd-on)))
  12432. (goto-char pos)
  12433. (if dd-on (cons "$$" m))))))
  12434. (defun org-try-cdlatex-tab ()
  12435. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  12436. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  12437. - inside a LaTeX fragment, or
  12438. - after the first word in a line, where an abbreviation expansion could
  12439. insert a LaTeX environment."
  12440. (when org-cdlatex-mode
  12441. (cond
  12442. ((save-excursion
  12443. (skip-chars-backward "a-zA-Z0-9*")
  12444. (skip-chars-backward " \t")
  12445. (bolp))
  12446. (cdlatex-tab) t)
  12447. ((org-inside-LaTeX-fragment-p)
  12448. (cdlatex-tab) t)
  12449. (t nil))))
  12450. (defun org-cdlatex-underscore-caret (&optional arg)
  12451. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  12452. Revert to the normal definition outside of these fragments."
  12453. (interactive "P")
  12454. (if (org-inside-LaTeX-fragment-p)
  12455. (call-interactively 'cdlatex-sub-superscript)
  12456. (let (org-cdlatex-mode)
  12457. (call-interactively (key-binding (vector last-input-event))))))
  12458. (defun org-cdlatex-math-modify (&optional arg)
  12459. "Execute `cdlatex-math-modify' in LaTeX fragments.
  12460. Revert to the normal definition outside of these fragments."
  12461. (interactive "P")
  12462. (if (org-inside-LaTeX-fragment-p)
  12463. (call-interactively 'cdlatex-math-modify)
  12464. (let (org-cdlatex-mode)
  12465. (call-interactively (key-binding (vector last-input-event))))))
  12466. (defvar org-latex-fragment-image-overlays nil
  12467. "List of overlays carrying the images of latex fragments.")
  12468. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  12469. (defun org-remove-latex-fragment-image-overlays ()
  12470. "Remove all overlays with LaTeX fragment images in current buffer."
  12471. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  12472. (setq org-latex-fragment-image-overlays nil))
  12473. (defun org-preview-latex-fragment (&optional subtree)
  12474. "Preview the LaTeX fragment at point, or all locally or globally.
  12475. If the cursor is in a LaTeX fragment, create the image and overlay
  12476. it over the source code. If there is no fragment at point, display
  12477. all fragments in the current text, from one headline to the next. With
  12478. prefix SUBTREE, display all fragments in the current subtree. With a
  12479. double prefix `C-u C-u', or when the cursor is before the first headline,
  12480. display all fragments in the buffer.
  12481. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  12482. (interactive "P")
  12483. (org-remove-latex-fragment-image-overlays)
  12484. (save-excursion
  12485. (save-restriction
  12486. (let (beg end at msg)
  12487. (cond
  12488. ((or (equal subtree '(16))
  12489. (not (save-excursion
  12490. (re-search-backward (concat "^" outline-regexp) nil t))))
  12491. (setq beg (point-min) end (point-max)
  12492. msg "Creating images for buffer...%s"))
  12493. ((equal subtree '(4))
  12494. (org-back-to-heading)
  12495. (setq beg (point) end (org-end-of-subtree t)
  12496. msg "Creating images for subtree...%s"))
  12497. (t
  12498. (if (setq at (org-inside-LaTeX-fragment-p))
  12499. (goto-char (max (point-min) (- (cdr at) 2)))
  12500. (org-back-to-heading))
  12501. (setq beg (point) end (progn (outline-next-heading) (point))
  12502. msg (if at "Creating image...%s"
  12503. "Creating images for entry...%s"))))
  12504. (message msg "")
  12505. (narrow-to-region beg end)
  12506. (goto-char beg)
  12507. (org-format-latex
  12508. (concat "ltxpng/" (file-name-sans-extension
  12509. (file-name-nondirectory
  12510. buffer-file-name)))
  12511. default-directory 'overlays msg at 'forbuffer)
  12512. (message msg "done. Use `C-c C-c' to remove images.")))))
  12513. (defvar org-latex-regexps
  12514. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  12515. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  12516. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  12517. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  12518. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  12519. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  12520. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  12521. "Regular expressions for matching embedded LaTeX.")
  12522. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  12523. "Replace LaTeX fragments with links to an image, and produce images."
  12524. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  12525. (let* ((prefixnodir (file-name-nondirectory prefix))
  12526. (absprefix (expand-file-name prefix dir))
  12527. (todir (file-name-directory absprefix))
  12528. (opt org-format-latex-options)
  12529. (matchers (plist-get opt :matchers))
  12530. (re-list org-latex-regexps)
  12531. (cnt 0) txt link beg end re e checkdir
  12532. m n block linkfile movefile ov)
  12533. ;; Check if there are old images files with this prefix, and remove them
  12534. (when (file-directory-p todir)
  12535. (mapc 'delete-file
  12536. (directory-files
  12537. todir 'full
  12538. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  12539. ;; Check the different regular expressions
  12540. (while (setq e (pop re-list))
  12541. (setq m (car e) re (nth 1 e) n (nth 2 e)
  12542. block (if (nth 3 e) "\n\n" ""))
  12543. (when (member m matchers)
  12544. (goto-char (point-min))
  12545. (while (re-search-forward re nil t)
  12546. (when (or (not at) (equal (cdr at) (match-beginning n)))
  12547. (setq txt (match-string n)
  12548. beg (match-beginning n) end (match-end n)
  12549. cnt (1+ cnt)
  12550. linkfile (format "%s_%04d.png" prefix cnt)
  12551. movefile (format "%s_%04d.png" absprefix cnt)
  12552. link (concat block "[[file:" linkfile "]]" block))
  12553. (if msg (message msg cnt))
  12554. (goto-char beg)
  12555. (unless checkdir ; make sure the directory exists
  12556. (setq checkdir t)
  12557. (or (file-directory-p todir) (make-directory todir)))
  12558. (org-create-formula-image
  12559. txt movefile opt forbuffer)
  12560. (if overlays
  12561. (progn
  12562. (setq ov (org-make-overlay beg end))
  12563. (if (featurep 'xemacs)
  12564. (progn
  12565. (org-overlay-put ov 'invisible t)
  12566. (org-overlay-put
  12567. ov 'end-glyph
  12568. (make-glyph (vector 'png :file movefile))))
  12569. (org-overlay-put
  12570. ov 'display
  12571. (list 'image :type 'png :file movefile :ascent 'center)))
  12572. (push ov org-latex-fragment-image-overlays)
  12573. (goto-char end))
  12574. (delete-region beg end)
  12575. (insert link))))))))
  12576. ;; This function borrows from Ganesh Swami's latex2png.el
  12577. (defun org-create-formula-image (string tofile options buffer)
  12578. (let* ((tmpdir (if (featurep 'xemacs)
  12579. (temp-directory)
  12580. temporary-file-directory))
  12581. (texfilebase (make-temp-name
  12582. (expand-file-name "orgtex" tmpdir)))
  12583. (texfile (concat texfilebase ".tex"))
  12584. (dvifile (concat texfilebase ".dvi"))
  12585. (pngfile (concat texfilebase ".png"))
  12586. (fnh (face-attribute 'default :height nil))
  12587. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  12588. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  12589. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  12590. "Black"))
  12591. (bg (or (plist-get options (if buffer :background :html-background))
  12592. "Transparent")))
  12593. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  12594. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  12595. (with-temp-file texfile
  12596. (insert org-format-latex-header
  12597. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  12598. (let ((dir default-directory))
  12599. (condition-case nil
  12600. (progn
  12601. (cd tmpdir)
  12602. (call-process "latex" nil nil nil texfile))
  12603. (error nil))
  12604. (cd dir))
  12605. (if (not (file-exists-p dvifile))
  12606. (progn (message "Failed to create dvi file from %s" texfile) nil)
  12607. (call-process "dvipng" nil nil nil
  12608. "-E" "-fg" fg "-bg" bg
  12609. "-D" dpi
  12610. ;;"-x" scale "-y" scale
  12611. "-T" "tight"
  12612. "-o" pngfile
  12613. dvifile)
  12614. (if (not (file-exists-p pngfile))
  12615. (progn (message "Failed to create png file from %s" texfile) nil)
  12616. ;; Use the requested file name and clean up
  12617. (copy-file pngfile tofile 'replace)
  12618. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  12619. (delete-file (concat texfilebase e)))
  12620. pngfile))))
  12621. (defun org-dvipng-color (attr)
  12622. "Return an rgb color specification for dvipng."
  12623. (apply 'format "rgb %s %s %s"
  12624. (mapcar 'org-normalize-color
  12625. (color-values (face-attribute 'default attr nil)))))
  12626. (defun org-normalize-color (value)
  12627. "Return string to be used as color value for an RGB component."
  12628. (format "%g" (/ value 65535.0)))
  12629. ;;;; Key bindings
  12630. ;; Make `C-c C-x' a prefix key
  12631. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  12632. ;; TAB key with modifiers
  12633. (org-defkey org-mode-map "\C-i" 'org-cycle)
  12634. (org-defkey org-mode-map [(tab)] 'org-cycle)
  12635. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  12636. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  12637. (org-defkey org-mode-map "\M-\t" 'org-complete)
  12638. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  12639. ;; The following line is necessary under Suse GNU/Linux
  12640. (unless (featurep 'xemacs)
  12641. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  12642. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  12643. (define-key org-mode-map [backtab] 'org-shifttab)
  12644. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  12645. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  12646. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  12647. ;; Cursor keys with modifiers
  12648. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  12649. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  12650. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  12651. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  12652. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  12653. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  12654. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  12655. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  12656. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  12657. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  12658. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  12659. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  12660. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  12661. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  12662. ;;; Extra keys for tty access.
  12663. ;; We only set them when really needed because otherwise the
  12664. ;; menus don't show the simple keys
  12665. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  12666. (not window-system))
  12667. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  12668. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  12669. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  12670. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  12671. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  12672. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  12673. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  12674. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  12675. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  12676. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  12677. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  12678. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  12679. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  12680. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  12681. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  12682. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  12683. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  12684. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  12685. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  12686. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  12687. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  12688. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  12689. ;; All the other keys
  12690. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  12691. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  12692. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  12693. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  12694. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  12695. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  12696. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  12697. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  12698. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  12699. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  12700. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  12701. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  12702. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  12703. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  12704. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  12705. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  12706. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  12707. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  12708. (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
  12709. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  12710. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  12711. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  12712. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  12713. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  12714. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  12715. (org-defkey org-mode-map "\C-c\C-z" 'org-time-stamp) ; Alternative binding
  12716. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  12717. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  12718. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  12719. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  12720. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  12721. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  12722. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  12723. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  12724. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  12725. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  12726. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  12727. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12728. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  12729. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  12730. (org-defkey org-mode-map "\C-c^" 'org-sort)
  12731. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  12732. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  12733. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  12734. (org-defkey org-mode-map "\C-m" 'org-return)
  12735. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  12736. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  12737. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  12738. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  12739. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  12740. (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
  12741. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  12742. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  12743. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  12744. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  12745. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  12746. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  12747. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  12748. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  12749. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  12750. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  12751. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  12752. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  12753. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  12754. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  12755. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  12756. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  12757. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  12758. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  12759. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  12760. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  12761. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  12762. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  12763. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  12764. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  12765. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  12766. (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
  12767. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  12768. (when (featurep 'xemacs)
  12769. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  12770. (defvar org-table-auto-blank-field) ; defined in org-table.el
  12771. (defun org-self-insert-command (N)
  12772. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  12773. If the cursor is in a table looking at whitespace, the whitespace is
  12774. overwritten, and the table is not marked as requiring realignment."
  12775. (interactive "p")
  12776. (if (and (org-table-p)
  12777. (progn
  12778. ;; check if we blank the field, and if that triggers align
  12779. (and (featurep 'org-table) org-table-auto-blank-field
  12780. (member last-command
  12781. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  12782. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  12783. ;; got extra space, this field does not determine column width
  12784. (let (org-table-may-need-update) (org-table-blank-field))
  12785. ;; no extra space, this field may determine column width
  12786. (org-table-blank-field)))
  12787. t)
  12788. (eq N 1)
  12789. (looking-at "[^|\n]* |"))
  12790. (let (org-table-may-need-update)
  12791. (goto-char (1- (match-end 0)))
  12792. (delete-backward-char 1)
  12793. (goto-char (match-beginning 0))
  12794. (self-insert-command N))
  12795. (setq org-table-may-need-update t)
  12796. (self-insert-command N)
  12797. (org-fix-tags-on-the-fly)))
  12798. (defun org-fix-tags-on-the-fly ()
  12799. (when (and (equal (char-after (point-at-bol)) ?*)
  12800. (org-on-heading-p))
  12801. (org-align-tags-here org-tags-column)))
  12802. (defun org-delete-backward-char (N)
  12803. "Like `delete-backward-char', insert whitespace at field end in tables.
  12804. When deleting backwards, in tables this function will insert whitespace in
  12805. front of the next \"|\" separator, to keep the table aligned. The table will
  12806. still be marked for re-alignment if the field did fill the entire column,
  12807. because, in this case the deletion might narrow the column."
  12808. (interactive "p")
  12809. (if (and (org-table-p)
  12810. (eq N 1)
  12811. (string-match "|" (buffer-substring (point-at-bol) (point)))
  12812. (looking-at ".*?|"))
  12813. (let ((pos (point))
  12814. (noalign (looking-at "[^|\n\r]* |"))
  12815. (c org-table-may-need-update))
  12816. (backward-delete-char N)
  12817. (skip-chars-forward "^|")
  12818. (insert " ")
  12819. (goto-char (1- pos))
  12820. ;; noalign: if there were two spaces at the end, this field
  12821. ;; does not determine the width of the column.
  12822. (if noalign (setq org-table-may-need-update c)))
  12823. (backward-delete-char N)
  12824. (org-fix-tags-on-the-fly)))
  12825. (defun org-delete-char (N)
  12826. "Like `delete-char', but insert whitespace at field end in tables.
  12827. When deleting characters, in tables this function will insert whitespace in
  12828. front of the next \"|\" separator, to keep the table aligned. The table will
  12829. still be marked for re-alignment if the field did fill the entire column,
  12830. because, in this case the deletion might narrow the column."
  12831. (interactive "p")
  12832. (if (and (org-table-p)
  12833. (not (bolp))
  12834. (not (= (char-after) ?|))
  12835. (eq N 1))
  12836. (if (looking-at ".*?|")
  12837. (let ((pos (point))
  12838. (noalign (looking-at "[^|\n\r]* |"))
  12839. (c org-table-may-need-update))
  12840. (replace-match (concat
  12841. (substring (match-string 0) 1 -1)
  12842. " |"))
  12843. (goto-char pos)
  12844. ;; noalign: if there were two spaces at the end, this field
  12845. ;; does not determine the width of the column.
  12846. (if noalign (setq org-table-may-need-update c)))
  12847. (delete-char N))
  12848. (delete-char N)
  12849. (org-fix-tags-on-the-fly)))
  12850. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  12851. (put 'org-self-insert-command 'delete-selection t)
  12852. (put 'orgtbl-self-insert-command 'delete-selection t)
  12853. (put 'org-delete-char 'delete-selection 'supersede)
  12854. (put 'org-delete-backward-char 'delete-selection 'supersede)
  12855. ;; Make `flyspell-mode' delay after some commands
  12856. (put 'org-self-insert-command 'flyspell-delayed t)
  12857. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  12858. (put 'org-delete-char 'flyspell-delayed t)
  12859. (put 'org-delete-backward-char 'flyspell-delayed t)
  12860. ;; Make pabbrev-mode expand after org-mode commands
  12861. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  12862. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  12863. ;; How to do this: Measure non-white length of current string
  12864. ;; If equal to column width, we should realign.
  12865. (defun org-remap (map &rest commands)
  12866. "In MAP, remap the functions given in COMMANDS.
  12867. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  12868. (let (new old)
  12869. (while commands
  12870. (setq old (pop commands) new (pop commands))
  12871. (if (fboundp 'command-remapping)
  12872. (org-defkey map (vector 'remap old) new)
  12873. (substitute-key-definition old new map global-map)))))
  12874. (when (eq org-enable-table-editor 'optimized)
  12875. ;; If the user wants maximum table support, we need to hijack
  12876. ;; some standard editing functions
  12877. (org-remap org-mode-map
  12878. 'self-insert-command 'org-self-insert-command
  12879. 'delete-char 'org-delete-char
  12880. 'delete-backward-char 'org-delete-backward-char)
  12881. (org-defkey org-mode-map "|" 'org-force-self-insert))
  12882. (defun org-shiftcursor-error ()
  12883. "Throw an error because Shift-Cursor command was applied in wrong context."
  12884. (error "This command is active in special context like tables, headlines or timestamps"))
  12885. (defun org-shifttab (&optional arg)
  12886. "Global visibility cycling or move to previous table field.
  12887. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  12888. on context.
  12889. See the individual commands for more information."
  12890. (interactive "P")
  12891. (cond
  12892. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  12893. (arg (message "Content view to level: ")
  12894. (org-content (prefix-numeric-value arg))
  12895. (setq org-cycle-global-status 'overview))
  12896. (t (call-interactively 'org-global-cycle))))
  12897. (defun org-shiftmetaleft ()
  12898. "Promote subtree or delete table column.
  12899. Calls `org-promote-subtree', `org-outdent-item',
  12900. or `org-table-delete-column', depending on context.
  12901. See the individual commands for more information."
  12902. (interactive)
  12903. (cond
  12904. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  12905. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  12906. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12907. (t (org-shiftcursor-error))))
  12908. (defun org-shiftmetaright ()
  12909. "Demote subtree or insert table column.
  12910. Calls `org-demote-subtree', `org-indent-item',
  12911. or `org-table-insert-column', depending on context.
  12912. See the individual commands for more information."
  12913. (interactive)
  12914. (cond
  12915. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  12916. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  12917. ((org-at-item-p) (call-interactively 'org-indent-item))
  12918. (t (org-shiftcursor-error))))
  12919. (defun org-shiftmetaup (&optional arg)
  12920. "Move subtree up or kill table row.
  12921. Calls `org-move-subtree-up' or `org-table-kill-row' or
  12922. `org-move-item-up' depending on context. See the individual commands
  12923. for more information."
  12924. (interactive "P")
  12925. (cond
  12926. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  12927. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12928. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12929. (t (org-shiftcursor-error))))
  12930. (defun org-shiftmetadown (&optional arg)
  12931. "Move subtree down or insert table row.
  12932. Calls `org-move-subtree-down' or `org-table-insert-row' or
  12933. `org-move-item-down', depending on context. See the individual
  12934. commands for more information."
  12935. (interactive "P")
  12936. (cond
  12937. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  12938. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12939. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12940. (t (org-shiftcursor-error))))
  12941. (defun org-metaleft (&optional arg)
  12942. "Promote heading or move table column to left.
  12943. Calls `org-do-promote' or `org-table-move-column', depending on context.
  12944. With no specific context, calls the Emacs default `backward-word'.
  12945. See the individual commands for more information."
  12946. (interactive "P")
  12947. (cond
  12948. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  12949. ((or (org-on-heading-p) (org-region-active-p))
  12950. (call-interactively 'org-do-promote))
  12951. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12952. (t (call-interactively 'backward-word))))
  12953. (defun org-metaright (&optional arg)
  12954. "Demote subtree or move table column to right.
  12955. Calls `org-do-demote' or `org-table-move-column', depending on context.
  12956. With no specific context, calls the Emacs default `forward-word'.
  12957. See the individual commands for more information."
  12958. (interactive "P")
  12959. (cond
  12960. ((org-at-table-p) (call-interactively 'org-table-move-column))
  12961. ((or (org-on-heading-p) (org-region-active-p))
  12962. (call-interactively 'org-do-demote))
  12963. ((org-at-item-p) (call-interactively 'org-indent-item))
  12964. (t (call-interactively 'forward-word))))
  12965. (defun org-metaup (&optional arg)
  12966. "Move subtree up or move table row up.
  12967. Calls `org-move-subtree-up' or `org-table-move-row' or
  12968. `org-move-item-up', depending on context. See the individual commands
  12969. for more information."
  12970. (interactive "P")
  12971. (cond
  12972. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  12973. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12974. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12975. (t (transpose-lines 1) (beginning-of-line -1))))
  12976. (defun org-metadown (&optional arg)
  12977. "Move subtree down or move table row down.
  12978. Calls `org-move-subtree-down' or `org-table-move-row' or
  12979. `org-move-item-down', depending on context. See the individual
  12980. commands for more information."
  12981. (interactive "P")
  12982. (cond
  12983. ((org-at-table-p) (call-interactively 'org-table-move-row))
  12984. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12985. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12986. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  12987. (defun org-shiftup (&optional arg)
  12988. "Increase item in timestamp or increase priority of current headline.
  12989. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  12990. depending on context. See the individual commands for more information."
  12991. (interactive "P")
  12992. (cond
  12993. ((org-at-timestamp-p t)
  12994. (call-interactively (if org-edit-timestamp-down-means-later
  12995. 'org-timestamp-down 'org-timestamp-up)))
  12996. ((org-on-heading-p) (call-interactively 'org-priority-up))
  12997. ((org-at-item-p) (call-interactively 'org-previous-item))
  12998. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  12999. (defun org-shiftdown (&optional arg)
  13000. "Decrease item in timestamp or decrease priority of current headline.
  13001. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  13002. depending on context. See the individual commands for more information."
  13003. (interactive "P")
  13004. (cond
  13005. ((org-at-timestamp-p t)
  13006. (call-interactively (if org-edit-timestamp-down-means-later
  13007. 'org-timestamp-up 'org-timestamp-down)))
  13008. ((org-on-heading-p) (call-interactively 'org-priority-down))
  13009. (t (call-interactively 'org-next-item))))
  13010. (defun org-shiftright ()
  13011. "Next TODO keyword or timestamp one day later, depending on context."
  13012. (interactive)
  13013. (cond
  13014. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  13015. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  13016. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  13017. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  13018. (t (org-shiftcursor-error))))
  13019. (defun org-shiftleft ()
  13020. "Previous TODO keyword or timestamp one day earlier, depending on context."
  13021. (interactive)
  13022. (cond
  13023. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  13024. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  13025. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  13026. ((org-at-property-p)
  13027. (call-interactively 'org-property-previous-allowed-value))
  13028. (t (org-shiftcursor-error))))
  13029. (defun org-shiftcontrolright ()
  13030. "Switch to next TODO set."
  13031. (interactive)
  13032. (cond
  13033. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  13034. (t (org-shiftcursor-error))))
  13035. (defun org-shiftcontrolleft ()
  13036. "Switch to previous TODO set."
  13037. (interactive)
  13038. (cond
  13039. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  13040. (t (org-shiftcursor-error))))
  13041. (defun org-ctrl-c-ret ()
  13042. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  13043. (interactive)
  13044. (cond
  13045. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  13046. (t (call-interactively 'org-insert-heading))))
  13047. (defun org-copy-special ()
  13048. "Copy region in table or copy current subtree.
  13049. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  13050. See the individual commands for more information."
  13051. (interactive)
  13052. (call-interactively
  13053. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  13054. (defun org-cut-special ()
  13055. "Cut region in table or cut current subtree.
  13056. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  13057. See the individual commands for more information."
  13058. (interactive)
  13059. (call-interactively
  13060. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  13061. (defun org-paste-special (arg)
  13062. "Paste rectangular region into table, or past subtree relative to level.
  13063. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  13064. See the individual commands for more information."
  13065. (interactive "P")
  13066. (if (org-at-table-p)
  13067. (org-table-paste-rectangle)
  13068. (org-paste-subtree arg)))
  13069. (defun org-ctrl-c-ctrl-c (&optional arg)
  13070. "Set tags in headline, or update according to changed information at point.
  13071. This command does many different things, depending on context:
  13072. - If the cursor is in a headline, prompt for tags and insert them
  13073. into the current line, aligned to `org-tags-column'. When called
  13074. with prefix arg, realign all tags in the current buffer.
  13075. - If the cursor is in one of the special #+KEYWORD lines, this
  13076. triggers scanning the buffer for these lines and updating the
  13077. information.
  13078. - If the cursor is inside a table, realign the table. This command
  13079. works even if the automatic table editor has been turned off.
  13080. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  13081. the entire table.
  13082. - If the cursor is a the beginning of a dynamic block, update it.
  13083. - If the cursor is inside a table created by the table.el package,
  13084. activate that table.
  13085. - If the current buffer is a remember buffer, close note and file it.
  13086. with a prefix argument, file it without further interaction to the default
  13087. location.
  13088. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  13089. links in this buffer.
  13090. - If the cursor is on a numbered item in a plain list, renumber the
  13091. ordered list.
  13092. - If the cursor is on a checkbox, toggle it."
  13093. (interactive "P")
  13094. (let ((org-enable-table-editor t))
  13095. (cond
  13096. ((or org-clock-overlays
  13097. org-occur-highlights
  13098. org-latex-fragment-image-overlays)
  13099. (org-remove-clock-overlays)
  13100. (org-remove-occur-highlights)
  13101. (org-remove-latex-fragment-image-overlays)
  13102. (message "Temporary highlights/overlays removed from current buffer"))
  13103. ((and (local-variable-p 'org-finish-function (current-buffer))
  13104. (fboundp org-finish-function))
  13105. (funcall org-finish-function))
  13106. ((org-at-property-p)
  13107. (call-interactively 'org-property-action))
  13108. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  13109. ((org-on-heading-p) (call-interactively 'org-set-tags))
  13110. ((org-at-table.el-p)
  13111. (require 'table)
  13112. (beginning-of-line 1)
  13113. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  13114. (call-interactively 'table-recognize-table))
  13115. ((org-at-table-p)
  13116. (org-table-maybe-eval-formula)
  13117. (if arg
  13118. (call-interactively 'org-table-recalculate)
  13119. (org-table-maybe-recalculate-line))
  13120. (call-interactively 'org-table-align))
  13121. ((org-at-item-checkbox-p)
  13122. (call-interactively 'org-toggle-checkbox))
  13123. ((org-at-item-p)
  13124. (call-interactively 'org-maybe-renumber-ordered-list))
  13125. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  13126. ;; Dynamic block
  13127. (beginning-of-line 1)
  13128. (org-update-dblock))
  13129. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  13130. (cond
  13131. ((equal (match-string 1) "TBLFM")
  13132. ;; Recalculate the table before this line
  13133. (save-excursion
  13134. (beginning-of-line 1)
  13135. (skip-chars-backward " \r\n\t")
  13136. (if (org-at-table-p)
  13137. (org-call-with-arg 'org-table-recalculate t))))
  13138. (t
  13139. (call-interactively 'org-mode-restart))))
  13140. (t (error "C-c C-c can do nothing useful at this location.")))))
  13141. (defun org-mode-restart ()
  13142. "Restart Org-mode, to scan again for special lines.
  13143. Also updates the keyword regular expressions."
  13144. (interactive)
  13145. (let ((org-inhibit-startup t)) (org-mode))
  13146. (message "Org-mode restarted to refresh keyword and special line setup"))
  13147. (defun org-kill-note-or-show-branches ()
  13148. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  13149. (interactive)
  13150. (if (not org-finish-function)
  13151. (call-interactively 'show-branches)
  13152. (let ((org-note-abort t))
  13153. (funcall org-finish-function))))
  13154. (defun org-return (&optional indent)
  13155. "Goto next table row or insert a newline.
  13156. Calls `org-table-next-row' or `newline', depending on context.
  13157. See the individual commands for more information."
  13158. (interactive)
  13159. (cond
  13160. ((bobp) (if indent (newline-and-indent) (newline)))
  13161. ((and (org-at-heading-p)
  13162. (looking-at
  13163. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  13164. (org-show-entry)
  13165. (end-of-line 1)
  13166. (newline))
  13167. ((org-at-table-p)
  13168. (org-table-justify-field-maybe)
  13169. (call-interactively 'org-table-next-row))
  13170. (t (if indent (newline-and-indent) (newline)))))
  13171. (defun org-return-indent ()
  13172. "Goto next table row or insert a newline and indent.
  13173. Calls `org-table-next-row' or `newline-and-indent', depending on
  13174. context. See the individual commands for more information."
  13175. (interactive)
  13176. (org-return t))
  13177. (defun org-ctrl-c-star ()
  13178. "Compute table, or change heading status of lines.
  13179. Calls `org-table-recalculate' or `org-toggle-region-headlines',
  13180. depending on context. This will also turn a plain list item or a normal
  13181. line into a subheading."
  13182. (interactive)
  13183. (cond
  13184. ((org-at-table-p)
  13185. (call-interactively 'org-table-recalculate))
  13186. ((org-region-active-p)
  13187. ;; Convert all lines in region to list items
  13188. (call-interactively 'org-toggle-region-headings))
  13189. ((org-on-heading-p)
  13190. (org-toggle-region-headings (point-at-bol)
  13191. (min (1+ (point-at-eol)) (point-max))))
  13192. ((org-at-item-p)
  13193. ;; Convert to heading
  13194. (let ((level (save-match-data
  13195. (save-excursion
  13196. (condition-case nil
  13197. (progn
  13198. (org-back-to-heading t)
  13199. (funcall outline-level))
  13200. (error 0))))))
  13201. (replace-match
  13202. (concat (make-string (org-get-valid-level level 1) ?*) " ") t t)))
  13203. (t (org-toggle-region-headings (point-at-bol)
  13204. (min (1+ (point-at-eol)) (point-max))))))
  13205. (defun org-ctrl-c-minus ()
  13206. "Insert separator line in table or modify bullet status of line.
  13207. Also turns a plain line or a region of lines into list items.
  13208. Calls `org-table-insert-hline', `org-toggle-region-items', or
  13209. `org-cycle-list-bullet', depending on context."
  13210. (interactive)
  13211. (cond
  13212. ((org-at-table-p)
  13213. (call-interactively 'org-table-insert-hline))
  13214. ((org-on-heading-p)
  13215. ;; Convert to item
  13216. (save-excursion
  13217. (beginning-of-line 1)
  13218. (if (looking-at "\\*+ ")
  13219. (replace-match (concat (make-string (- (match-end 0) (point) 1) ?\ ) "- ")))))
  13220. ((org-region-active-p)
  13221. ;; Convert all lines in region to list items
  13222. (call-interactively 'org-toggle-region-items))
  13223. ((org-in-item-p)
  13224. (call-interactively 'org-cycle-list-bullet))
  13225. (t (org-toggle-region-items (point-at-bol)
  13226. (min (1+ (point-at-eol)) (point-max))))))
  13227. (defun org-toggle-region-items (beg end)
  13228. "Convert all lines in region to list items.
  13229. If the first line is already an item, convert all list items in the region
  13230. to normal lines."
  13231. (interactive "r")
  13232. (let (l2 l)
  13233. (save-excursion
  13234. (goto-char end)
  13235. (setq l2 (org-current-line))
  13236. (goto-char beg)
  13237. (beginning-of-line 1)
  13238. (setq l (1- (org-current-line)))
  13239. (if (org-at-item-p)
  13240. ;; We already have items, de-itemize
  13241. (while (< (setq l (1+ l)) l2)
  13242. (when (org-at-item-p)
  13243. (goto-char (match-beginning 2))
  13244. (delete-region (match-beginning 2) (match-end 2))
  13245. (and (looking-at "[ \t]+") (replace-match "")))
  13246. (beginning-of-line 2))
  13247. (while (< (setq l (1+ l)) l2)
  13248. (unless (org-at-item-p)
  13249. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13250. (replace-match "\\1- \\2")))
  13251. (beginning-of-line 2))))))
  13252. (defun org-toggle-region-headings (beg end)
  13253. "Convert all lines in region to list items.
  13254. If the first line is already an item, convert all list items in the region
  13255. to normal lines."
  13256. (interactive "r")
  13257. (let (l2 l)
  13258. (save-excursion
  13259. (goto-char end)
  13260. (setq l2 (org-current-line))
  13261. (goto-char beg)
  13262. (beginning-of-line 1)
  13263. (setq l (1- (org-current-line)))
  13264. (if (org-on-heading-p)
  13265. ;; We already have headlines, de-star them
  13266. (while (< (setq l (1+ l)) l2)
  13267. (when (org-on-heading-p t)
  13268. (and (looking-at outline-regexp) (replace-match "")))
  13269. (beginning-of-line 2))
  13270. (let* ((stars (save-excursion
  13271. (re-search-backward org-complex-heading-regexp nil t)
  13272. (or (match-string 1) "*")))
  13273. (add-stars (if org-odd-levels-only "**" "*"))
  13274. (rpl (concat stars add-stars " \\2")))
  13275. (while (< (setq l (1+ l)) l2)
  13276. (unless (org-on-heading-p)
  13277. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13278. (replace-match rpl)))
  13279. (beginning-of-line 2)))))))
  13280. (defun org-meta-return (&optional arg)
  13281. "Insert a new heading or wrap a region in a table.
  13282. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  13283. See the individual commands for more information."
  13284. (interactive "P")
  13285. (cond
  13286. ((org-at-table-p)
  13287. (call-interactively 'org-table-wrap-region))
  13288. (t (call-interactively 'org-insert-heading))))
  13289. ;;; Menu entries
  13290. ;; Define the Org-mode menus
  13291. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  13292. '("Tbl"
  13293. ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
  13294. ["Next Field" org-cycle (org-at-table-p)]
  13295. ["Previous Field" org-shifttab (org-at-table-p)]
  13296. ["Next Row" org-return (org-at-table-p)]
  13297. "--"
  13298. ["Blank Field" org-table-blank-field (org-at-table-p)]
  13299. ["Edit Field" org-table-edit-field (org-at-table-p)]
  13300. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  13301. "--"
  13302. ("Column"
  13303. ["Move Column Left" org-metaleft (org-at-table-p)]
  13304. ["Move Column Right" org-metaright (org-at-table-p)]
  13305. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  13306. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  13307. ("Row"
  13308. ["Move Row Up" org-metaup (org-at-table-p)]
  13309. ["Move Row Down" org-metadown (org-at-table-p)]
  13310. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  13311. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  13312. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  13313. "--"
  13314. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  13315. ("Rectangle"
  13316. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  13317. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  13318. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  13319. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  13320. "--"
  13321. ("Calculate"
  13322. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  13323. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  13324. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  13325. "--"
  13326. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  13327. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  13328. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  13329. "--"
  13330. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  13331. "--"
  13332. ["Sum Column/Rectangle" org-table-sum
  13333. (or (org-at-table-p) (org-region-active-p))]
  13334. ["Which Column?" org-table-current-column (org-at-table-p)])
  13335. ["Debug Formulas"
  13336. org-table-toggle-formula-debugger
  13337. :style toggle :selected org-table-formula-debug]
  13338. ["Show Col/Row Numbers"
  13339. org-table-toggle-coordinate-overlays
  13340. :style toggle :selected org-table-overlay-coordinates]
  13341. "--"
  13342. ["Create" org-table-create (and (not (org-at-table-p))
  13343. org-enable-table-editor)]
  13344. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  13345. ["Import from File" org-table-import (not (org-at-table-p))]
  13346. ["Export to File" org-table-export (org-at-table-p)]
  13347. "--"
  13348. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  13349. (easy-menu-define org-org-menu org-mode-map "Org menu"
  13350. '("Org"
  13351. ("Show/Hide"
  13352. ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
  13353. ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
  13354. ["Sparse Tree" org-occur t]
  13355. ["Reveal Context" org-reveal t]
  13356. ["Show All" show-all t]
  13357. "--"
  13358. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  13359. "--"
  13360. ["New Heading" org-insert-heading t]
  13361. ("Navigate Headings"
  13362. ["Up" outline-up-heading t]
  13363. ["Next" outline-next-visible-heading t]
  13364. ["Previous" outline-previous-visible-heading t]
  13365. ["Next Same Level" outline-forward-same-level t]
  13366. ["Previous Same Level" outline-backward-same-level t]
  13367. "--"
  13368. ["Jump" org-goto t])
  13369. ("Edit Structure"
  13370. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  13371. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  13372. "--"
  13373. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  13374. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  13375. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  13376. "--"
  13377. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  13378. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  13379. ["Demote Heading" org-metaright (not (org-at-table-p))]
  13380. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  13381. "--"
  13382. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  13383. "--"
  13384. ["Convert to odd levels" org-convert-to-odd-levels t]
  13385. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  13386. ("Editing"
  13387. ["Emphasis..." org-emphasize t])
  13388. ("Archive"
  13389. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  13390. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  13391. ; :active t :keys "C-u C-c C-x C-a"]
  13392. ["Sparse trees open ARCHIVE trees"
  13393. (setq org-sparse-tree-open-archived-trees
  13394. (not org-sparse-tree-open-archived-trees))
  13395. :style toggle :selected org-sparse-tree-open-archived-trees]
  13396. ["Cycling opens ARCHIVE trees"
  13397. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  13398. :style toggle :selected org-cycle-open-archived-trees]
  13399. ["Agenda includes ARCHIVE trees"
  13400. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  13401. :style toggle :selected (not org-agenda-skip-archived-trees)]
  13402. "--"
  13403. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  13404. ; ["Check and Move Children" (org-archive-subtree '(4))
  13405. ; :active t :keys "C-u C-c C-x C-s"]
  13406. )
  13407. "--"
  13408. ("TODO Lists"
  13409. ["TODO/DONE/-" org-todo t]
  13410. ("Select keyword"
  13411. ["Next keyword" org-shiftright (org-on-heading-p)]
  13412. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  13413. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  13414. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  13415. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  13416. ["Show TODO Tree" org-show-todo-tree t]
  13417. ["Global TODO list" org-todo-list t]
  13418. "--"
  13419. ["Set Priority" org-priority t]
  13420. ["Priority Up" org-shiftup t]
  13421. ["Priority Down" org-shiftdown t])
  13422. ("TAGS and Properties"
  13423. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  13424. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  13425. "--"
  13426. ["Set property" 'org-set-property t]
  13427. ["Column view of properties" org-columns t]
  13428. ["Insert Column View DBlock" org-insert-columns-dblock t])
  13429. ("Dates and Scheduling"
  13430. ["Timestamp" org-time-stamp t]
  13431. ["Timestamp (inactive)" org-time-stamp-inactive t]
  13432. ("Change Date"
  13433. ["1 Day Later" org-shiftright t]
  13434. ["1 Day Earlier" org-shiftleft t]
  13435. ["1 ... Later" org-shiftup t]
  13436. ["1 ... Earlier" org-shiftdown t])
  13437. ["Compute Time Range" org-evaluate-time-range t]
  13438. ["Schedule Item" org-schedule t]
  13439. ["Deadline" org-deadline t]
  13440. "--"
  13441. ["Custom time format" org-toggle-time-stamp-overlays
  13442. :style radio :selected org-display-custom-times]
  13443. "--"
  13444. ["Goto Calendar" org-goto-calendar t]
  13445. ["Date from Calendar" org-date-from-calendar t])
  13446. ("Logging work"
  13447. ["Clock in" org-clock-in t]
  13448. ["Clock out" org-clock-out t]
  13449. ["Clock cancel" org-clock-cancel t]
  13450. ["Goto running clock" org-clock-goto t]
  13451. ["Display times" org-clock-display t]
  13452. ["Create clock table" org-clock-report t]
  13453. "--"
  13454. ["Record DONE time"
  13455. (progn (setq org-log-done (not org-log-done))
  13456. (message "Switching to %s will %s record a timestamp"
  13457. (car org-done-keywords)
  13458. (if org-log-done "automatically" "not")))
  13459. :style toggle :selected org-log-done])
  13460. "--"
  13461. ["Agenda Command..." org-agenda t]
  13462. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  13463. ("File List for Agenda")
  13464. ("Special views current file"
  13465. ["TODO Tree" org-show-todo-tree t]
  13466. ["Check Deadlines" org-check-deadlines t]
  13467. ["Timeline" org-timeline t]
  13468. ["Tags Tree" org-tags-sparse-tree t])
  13469. "--"
  13470. ("Hyperlinks"
  13471. ["Store Link (Global)" org-store-link t]
  13472. ["Insert Link" org-insert-link t]
  13473. ["Follow Link" org-open-at-point t]
  13474. "--"
  13475. ["Next link" org-next-link t]
  13476. ["Previous link" org-previous-link t]
  13477. "--"
  13478. ["Descriptive Links"
  13479. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  13480. :style radio :selected (member '(org-link) buffer-invisibility-spec)]
  13481. ["Literal Links"
  13482. (progn
  13483. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  13484. :style radio :selected (not (member '(org-link) buffer-invisibility-spec))])
  13485. "--"
  13486. ["Export/Publish..." org-export t]
  13487. ("LaTeX"
  13488. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  13489. :selected org-cdlatex-mode]
  13490. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  13491. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  13492. ["Modify math symbol" org-cdlatex-math-modify
  13493. (org-inside-LaTeX-fragment-p)]
  13494. ["Export LaTeX fragments as images"
  13495. (setq org-export-with-LaTeX-fragments (not org-export-with-LaTeX-fragments))
  13496. :style toggle :selected org-export-with-LaTeX-fragments])
  13497. "--"
  13498. ("Documentation"
  13499. ["Show Version" org-version t]
  13500. ["Info Documentation" org-info t])
  13501. ("Customize"
  13502. ["Browse Org Group" org-customize t]
  13503. "--"
  13504. ["Expand This Menu" org-create-customize-menu
  13505. (fboundp 'customize-menu-create)])
  13506. "--"
  13507. ["Refresh setup" org-mode-restart t]
  13508. ))
  13509. (defun org-info (&optional node)
  13510. "Read documentation for Org-mode in the info system.
  13511. With optional NODE, go directly to that node."
  13512. (interactive)
  13513. (info (format "(org)%s" (or node ""))))
  13514. (defun org-install-agenda-files-menu ()
  13515. (let ((bl (buffer-list)))
  13516. (save-excursion
  13517. (while bl
  13518. (set-buffer (pop bl))
  13519. (if (org-mode-p) (setq bl nil)))
  13520. (when (org-mode-p)
  13521. (easy-menu-change
  13522. '("Org") "File List for Agenda"
  13523. (append
  13524. (list
  13525. ["Edit File List" (org-edit-agenda-file-list) t]
  13526. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  13527. ["Remove Current File from List" org-remove-file t]
  13528. ["Cycle through agenda files" org-cycle-agenda-files t]
  13529. ["Occur in all agenda files" org-occur-in-agenda-files t]
  13530. "--")
  13531. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  13532. ;;;; Documentation
  13533. (defun org-customize ()
  13534. "Call the customize function with org as argument."
  13535. (interactive)
  13536. (customize-browse 'org))
  13537. (defun org-create-customize-menu ()
  13538. "Create a full customization menu for Org-mode, insert it into the menu."
  13539. (interactive)
  13540. (if (fboundp 'customize-menu-create)
  13541. (progn
  13542. (easy-menu-change
  13543. '("Org") "Customize"
  13544. `(["Browse Org group" org-customize t]
  13545. "--"
  13546. ,(customize-menu-create 'org)
  13547. ["Set" Custom-set t]
  13548. ["Save" Custom-save t]
  13549. ["Reset to Current" Custom-reset-current t]
  13550. ["Reset to Saved" Custom-reset-saved t]
  13551. ["Reset to Standard Settings" Custom-reset-standard t]))
  13552. (message "\"Org\"-menu now contains full customization menu"))
  13553. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  13554. ;;;; Miscellaneous stuff
  13555. ;;; Generally useful functions
  13556. (defun org-force-self-insert (N)
  13557. "Needed to enforce self-insert under remapping."
  13558. (interactive "p")
  13559. (self-insert-command N))
  13560. (defun org-string-width (s)
  13561. "Compute width of string, ignoring invisible characters.
  13562. This ignores character with invisibility property `org-link', and also
  13563. characters with property `org-cwidth', because these will become invisible
  13564. upon the next fontification round."
  13565. (let (b l)
  13566. (when (or (eq t buffer-invisibility-spec)
  13567. (assq 'org-link buffer-invisibility-spec))
  13568. (while (setq b (text-property-any 0 (length s)
  13569. 'invisible 'org-link s))
  13570. (setq s (concat (substring s 0 b)
  13571. (substring s (or (next-single-property-change
  13572. b 'invisible s) (length s)))))))
  13573. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  13574. (setq s (concat (substring s 0 b)
  13575. (substring s (or (next-single-property-change
  13576. b 'org-cwidth s) (length s))))))
  13577. (setq l (string-width s) b -1)
  13578. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  13579. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  13580. l))
  13581. (defun org-trim (s)
  13582. "Remove whitespace at beginning and end of string."
  13583. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  13584. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  13585. s)
  13586. (defun org-wrap (string &optional width lines)
  13587. "Wrap string to either a number of lines, or a width in characters.
  13588. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  13589. that costs. If there is a word longer than WIDTH, the text is actually
  13590. wrapped to the length of that word.
  13591. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  13592. many lines, whatever width that takes.
  13593. The return value is a list of lines, without newlines at the end."
  13594. (let* ((words (org-split-string string "[ \t\n]+"))
  13595. (maxword (apply 'max (mapcar 'org-string-width words)))
  13596. w ll)
  13597. (cond (width
  13598. (org-do-wrap words (max maxword width)))
  13599. (lines
  13600. (setq w maxword)
  13601. (setq ll (org-do-wrap words maxword))
  13602. (if (<= (length ll) lines)
  13603. ll
  13604. (setq ll words)
  13605. (while (> (length ll) lines)
  13606. (setq w (1+ w))
  13607. (setq ll (org-do-wrap words w)))
  13608. ll))
  13609. (t (error "Cannot wrap this")))))
  13610. (defun org-do-wrap (words width)
  13611. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  13612. (let (lines line)
  13613. (while words
  13614. (setq line (pop words))
  13615. (while (and words (< (+ (length line) (length (car words))) width))
  13616. (setq line (concat line " " (pop words))))
  13617. (setq lines (push line lines)))
  13618. (nreverse lines)))
  13619. (defun org-split-string (string &optional separators)
  13620. "Splits STRING into substrings at SEPARATORS.
  13621. No empty strings are returned if there are matches at the beginning
  13622. and end of string."
  13623. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  13624. (start 0)
  13625. notfirst
  13626. (list nil))
  13627. (while (and (string-match rexp string
  13628. (if (and notfirst
  13629. (= start (match-beginning 0))
  13630. (< start (length string)))
  13631. (1+ start) start))
  13632. (< (match-beginning 0) (length string)))
  13633. (setq notfirst t)
  13634. (or (eq (match-beginning 0) 0)
  13635. (and (eq (match-beginning 0) (match-end 0))
  13636. (eq (match-beginning 0) start))
  13637. (setq list
  13638. (cons (substring string start (match-beginning 0))
  13639. list)))
  13640. (setq start (match-end 0)))
  13641. (or (eq start (length string))
  13642. (setq list
  13643. (cons (substring string start)
  13644. list)))
  13645. (nreverse list)))
  13646. (defun org-context ()
  13647. "Return a list of contexts of the current cursor position.
  13648. If several contexts apply, all are returned.
  13649. Each context entry is a list with a symbol naming the context, and
  13650. two positions indicating start and end of the context. Possible
  13651. contexts are:
  13652. :headline anywhere in a headline
  13653. :headline-stars on the leading stars in a headline
  13654. :todo-keyword on a TODO keyword (including DONE) in a headline
  13655. :tags on the TAGS in a headline
  13656. :priority on the priority cookie in a headline
  13657. :item on the first line of a plain list item
  13658. :item-bullet on the bullet/number of a plain list item
  13659. :checkbox on the checkbox in a plain list item
  13660. :table in an org-mode table
  13661. :table-special on a special filed in a table
  13662. :table-table in a table.el table
  13663. :link on a hyperlink
  13664. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  13665. :target on a <<target>>
  13666. :radio-target on a <<<radio-target>>>
  13667. :latex-fragment on a LaTeX fragment
  13668. :latex-preview on a LaTeX fragment with overlayed preview image
  13669. This function expects the position to be visible because it uses font-lock
  13670. faces as a help to recognize the following contexts: :table-special, :link,
  13671. and :keyword."
  13672. (let* ((f (get-text-property (point) 'face))
  13673. (faces (if (listp f) f (list f)))
  13674. (p (point)) clist o)
  13675. ;; First the large context
  13676. (cond
  13677. ((org-on-heading-p t)
  13678. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  13679. (when (progn
  13680. (beginning-of-line 1)
  13681. (looking-at org-todo-line-tags-regexp))
  13682. (push (org-point-in-group p 1 :headline-stars) clist)
  13683. (push (org-point-in-group p 2 :todo-keyword) clist)
  13684. (push (org-point-in-group p 4 :tags) clist))
  13685. (goto-char p)
  13686. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  13687. (if (looking-at "\\[#[A-Z0-9]\\]")
  13688. (push (org-point-in-group p 0 :priority) clist)))
  13689. ((org-at-item-p)
  13690. (push (org-point-in-group p 2 :item-bullet) clist)
  13691. (push (list :item (point-at-bol)
  13692. (save-excursion (org-end-of-item) (point)))
  13693. clist)
  13694. (and (org-at-item-checkbox-p)
  13695. (push (org-point-in-group p 0 :checkbox) clist)))
  13696. ((org-at-table-p)
  13697. (push (list :table (org-table-begin) (org-table-end)) clist)
  13698. (if (memq 'org-formula faces)
  13699. (push (list :table-special
  13700. (previous-single-property-change p 'face)
  13701. (next-single-property-change p 'face)) clist)))
  13702. ((org-at-table-p 'any)
  13703. (push (list :table-table) clist)))
  13704. (goto-char p)
  13705. ;; Now the small context
  13706. (cond
  13707. ((org-at-timestamp-p)
  13708. (push (org-point-in-group p 0 :timestamp) clist))
  13709. ((memq 'org-link faces)
  13710. (push (list :link
  13711. (previous-single-property-change p 'face)
  13712. (next-single-property-change p 'face)) clist))
  13713. ((memq 'org-special-keyword faces)
  13714. (push (list :keyword
  13715. (previous-single-property-change p 'face)
  13716. (next-single-property-change p 'face)) clist))
  13717. ((org-on-target-p)
  13718. (push (org-point-in-group p 0 :target) clist)
  13719. (goto-char (1- (match-beginning 0)))
  13720. (if (looking-at org-radio-target-regexp)
  13721. (push (org-point-in-group p 0 :radio-target) clist))
  13722. (goto-char p))
  13723. ((setq o (car (delq nil
  13724. (mapcar
  13725. (lambda (x)
  13726. (if (memq x org-latex-fragment-image-overlays) x))
  13727. (org-overlays-at (point))))))
  13728. (push (list :latex-fragment
  13729. (org-overlay-start o) (org-overlay-end o)) clist)
  13730. (push (list :latex-preview
  13731. (org-overlay-start o) (org-overlay-end o)) clist))
  13732. ((org-inside-LaTeX-fragment-p)
  13733. ;; FIXME: positions wrong.
  13734. (push (list :latex-fragment (point) (point)) clist)))
  13735. (setq clist (nreverse (delq nil clist)))
  13736. clist))
  13737. ;; FIXME: Compare with at-regexp-p Do we need both?
  13738. (defun org-in-regexp (re &optional nlines visually)
  13739. "Check if point is inside a match of regexp.
  13740. Normally only the current line is checked, but you can include NLINES extra
  13741. lines both before and after point into the search.
  13742. If VISUALLY is set, require that the cursor is not after the match but
  13743. really on, so that the block visually is on the match."
  13744. (catch 'exit
  13745. (let ((pos (point))
  13746. (eol (point-at-eol (+ 1 (or nlines 0))))
  13747. (inc (if visually 1 0)))
  13748. (save-excursion
  13749. (beginning-of-line (- 1 (or nlines 0)))
  13750. (while (re-search-forward re eol t)
  13751. (if (and (<= (match-beginning 0) pos)
  13752. (>= (+ inc (match-end 0)) pos))
  13753. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  13754. (defun org-at-regexp-p (regexp)
  13755. "Is point inside a match of REGEXP in the current line?"
  13756. (catch 'exit
  13757. (save-excursion
  13758. (let ((pos (point)) (end (point-at-eol)))
  13759. (beginning-of-line 1)
  13760. (while (re-search-forward regexp end t)
  13761. (if (and (<= (match-beginning 0) pos)
  13762. (>= (match-end 0) pos))
  13763. (throw 'exit t)))
  13764. nil))))
  13765. (defun org-occur-in-agenda-files (regexp &optional nlines)
  13766. "Call `multi-occur' with buffers for all agenda files."
  13767. (interactive "sOrg-files matching: \np")
  13768. (let* ((files (org-agenda-files))
  13769. (tnames (mapcar 'file-truename files))
  13770. (extra org-agenda-text-search-extra-files)
  13771. f)
  13772. (while (setq f (pop extra))
  13773. (unless (member (file-truename f) tnames)
  13774. (add-to-list 'files f 'append)
  13775. (add-to-list 'tnames (file-truename f) 'append)))
  13776. (multi-occur
  13777. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  13778. regexp)))
  13779. (if (boundp 'occur-mode-find-occurrence-hook)
  13780. ;; Emacs 23
  13781. (add-hook 'occur-mode-find-occurrence-hook
  13782. (lambda ()
  13783. (when (org-mode-p)
  13784. (org-reveal))))
  13785. ;; Emacs 22
  13786. (defadvice occur-mode-goto-occurrence
  13787. (after org-occur-reveal activate)
  13788. (and (org-mode-p) (org-reveal)))
  13789. (defadvice occur-mode-goto-occurrence-other-window
  13790. (after org-occur-reveal activate)
  13791. (and (org-mode-p) (org-reveal)))
  13792. (defadvice occur-mode-display-occurrence
  13793. (after org-occur-reveal activate)
  13794. (when (org-mode-p)
  13795. (let ((pos (occur-mode-find-occurrence)))
  13796. (with-current-buffer (marker-buffer pos)
  13797. (save-excursion
  13798. (goto-char pos)
  13799. (org-reveal)))))))
  13800. (defun org-uniquify (list)
  13801. "Remove duplicate elements from LIST."
  13802. (let (res)
  13803. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  13804. res))
  13805. (defun org-delete-all (elts list)
  13806. "Remove all elements in ELTS from LIST."
  13807. (while elts
  13808. (setq list (delete (pop elts) list)))
  13809. list)
  13810. (defun org-back-over-empty-lines ()
  13811. "Move backwards over witespace, to the beginning of the first empty line.
  13812. Returns the number of empty lines passed."
  13813. (let ((pos (point)))
  13814. (skip-chars-backward " \t\n\r")
  13815. (beginning-of-line 2)
  13816. (goto-char (min (point) pos))
  13817. (count-lines (point) pos)))
  13818. (defun org-skip-whitespace ()
  13819. (skip-chars-forward " \t\n\r"))
  13820. (defun org-point-in-group (point group &optional context)
  13821. "Check if POINT is in match-group GROUP.
  13822. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  13823. match. If the match group does ot exist or point is not inside it,
  13824. return nil."
  13825. (and (match-beginning group)
  13826. (>= point (match-beginning group))
  13827. (<= point (match-end group))
  13828. (if context
  13829. (list context (match-beginning group) (match-end group))
  13830. t)))
  13831. (defun org-switch-to-buffer-other-window (&rest args)
  13832. "Switch to buffer in a second window on the current frame.
  13833. In particular, do not allow pop-up frames."
  13834. (let (pop-up-frames special-display-buffer-names special-display-regexps
  13835. special-display-function)
  13836. (apply 'switch-to-buffer-other-window args)))
  13837. (defun org-combine-plists (&rest plists)
  13838. "Create a single property list from all plists in PLISTS.
  13839. The process starts by copying the first list, and then setting properties
  13840. from the other lists. Settings in the last list are the most significant
  13841. ones and overrule settings in the other lists."
  13842. (let ((rtn (copy-sequence (pop plists)))
  13843. p v ls)
  13844. (while plists
  13845. (setq ls (pop plists))
  13846. (while ls
  13847. (setq p (pop ls) v (pop ls))
  13848. (setq rtn (plist-put rtn p v))))
  13849. rtn))
  13850. (defun org-move-line-down (arg)
  13851. "Move the current line down. With prefix argument, move it past ARG lines."
  13852. (interactive "p")
  13853. (let ((col (current-column))
  13854. beg end pos)
  13855. (beginning-of-line 1) (setq beg (point))
  13856. (beginning-of-line 2) (setq end (point))
  13857. (beginning-of-line (+ 1 arg))
  13858. (setq pos (move-marker (make-marker) (point)))
  13859. (insert (delete-and-extract-region beg end))
  13860. (goto-char pos)
  13861. (move-to-column col)))
  13862. (defun org-move-line-up (arg)
  13863. "Move the current line up. With prefix argument, move it past ARG lines."
  13864. (interactive "p")
  13865. (let ((col (current-column))
  13866. beg end pos)
  13867. (beginning-of-line 1) (setq beg (point))
  13868. (beginning-of-line 2) (setq end (point))
  13869. (beginning-of-line (- arg))
  13870. (setq pos (move-marker (make-marker) (point)))
  13871. (insert (delete-and-extract-region beg end))
  13872. (goto-char pos)
  13873. (move-to-column col)))
  13874. (defun org-replace-escapes (string table)
  13875. "Replace %-escapes in STRING with values in TABLE.
  13876. TABLE is an association list with keys like \"%a\" and string values.
  13877. The sequences in STRING may contain normal field width and padding information,
  13878. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  13879. so values can contain further %-escapes if they are define later in TABLE."
  13880. (let ((case-fold-search nil)
  13881. e re rpl)
  13882. (while (setq e (pop table))
  13883. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  13884. (while (string-match re string)
  13885. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  13886. (cdr e)))
  13887. (setq string (replace-match rpl t t string))))
  13888. string))
  13889. (defun org-sublist (list start end)
  13890. "Return a section of LIST, from START to END.
  13891. Counting starts at 1."
  13892. (let (rtn (c start))
  13893. (setq list (nthcdr (1- start) list))
  13894. (while (and list (<= c end))
  13895. (push (pop list) rtn)
  13896. (setq c (1+ c)))
  13897. (nreverse rtn)))
  13898. (defun org-find-base-buffer-visiting (file)
  13899. "Like `find-buffer-visiting' but alway return the base buffer and
  13900. not an indirect buffer."
  13901. (let ((buf (find-buffer-visiting file)))
  13902. (if buf
  13903. (or (buffer-base-buffer buf) buf)
  13904. nil)))
  13905. (defun org-image-file-name-regexp ()
  13906. "Return regexp matching the file names of images."
  13907. (if (fboundp 'image-file-name-regexp)
  13908. (image-file-name-regexp)
  13909. (let ((image-file-name-extensions
  13910. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  13911. "xbm" "xpm" "pbm" "pgm" "ppm")))
  13912. (concat "\\."
  13913. (regexp-opt (nconc (mapcar 'upcase
  13914. image-file-name-extensions)
  13915. image-file-name-extensions)
  13916. t)
  13917. "\\'"))))
  13918. (defun org-file-image-p (file)
  13919. "Return non-nil if FILE is an image."
  13920. (save-match-data
  13921. (string-match (org-image-file-name-regexp) file)))
  13922. ;;; Paragraph filling stuff.
  13923. ;; We want this to be just right, so use the full arsenal.
  13924. (defun org-indent-line-function ()
  13925. "Indent line like previous, but further if previous was headline or item."
  13926. (interactive)
  13927. (let* ((pos (point))
  13928. (itemp (org-at-item-p))
  13929. column bpos bcol tpos tcol bullet btype bullet-type)
  13930. ;; Find the previous relevant line
  13931. (beginning-of-line 1)
  13932. (cond
  13933. ((looking-at "#") (setq column 0))
  13934. ((looking-at "\\*+ ") (setq column 0))
  13935. (t
  13936. (beginning-of-line 0)
  13937. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  13938. (beginning-of-line 0))
  13939. (cond
  13940. ((looking-at "\\*+[ \t]+")
  13941. (goto-char (match-end 0))
  13942. (setq column (current-column)))
  13943. ((org-in-item-p)
  13944. (org-beginning-of-item)
  13945. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  13946. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\)?")
  13947. (setq bpos (match-beginning 1) tpos (match-end 0)
  13948. bcol (progn (goto-char bpos) (current-column))
  13949. tcol (progn (goto-char tpos) (current-column))
  13950. bullet (match-string 1)
  13951. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  13952. (if (not itemp)
  13953. (setq column tcol)
  13954. (goto-char pos)
  13955. (beginning-of-line 1)
  13956. (if (looking-at "\\S-")
  13957. (progn
  13958. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  13959. (setq bullet (match-string 1)
  13960. btype (if (string-match "[0-9]" bullet) "n" bullet))
  13961. (setq column (if (equal btype bullet-type) bcol tcol)))
  13962. (setq column (org-get-indentation)))))
  13963. (t (setq column (org-get-indentation))))))
  13964. (goto-char pos)
  13965. (if (<= (current-column) (current-indentation))
  13966. (indent-line-to column)
  13967. (save-excursion (indent-line-to column)))
  13968. (setq column (current-column))
  13969. (beginning-of-line 1)
  13970. (if (looking-at
  13971. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  13972. (replace-match (concat "\\1" (format org-property-format
  13973. (match-string 2) (match-string 3)))
  13974. t nil))
  13975. (move-to-column column)))
  13976. (defun org-set-autofill-regexps ()
  13977. (interactive)
  13978. ;; In the paragraph separator we include headlines, because filling
  13979. ;; text in a line directly attached to a headline would otherwise
  13980. ;; fill the headline as well.
  13981. (org-set-local 'comment-start-skip "^#+[ \t]*")
  13982. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  13983. ;; The paragraph starter includes hand-formatted lists.
  13984. (org-set-local 'paragraph-start
  13985. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  13986. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  13987. ;; But only if the user has not turned off tables or fixed-width regions
  13988. (org-set-local
  13989. 'auto-fill-inhibit-regexp
  13990. (concat "\\*+ \\|#\\+"
  13991. "\\|[ \t]*" org-keyword-time-regexp
  13992. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  13993. (concat
  13994. "\\|[ \t]*["
  13995. (if org-enable-table-editor "|" "")
  13996. (if org-enable-fixed-width-editor ":" "")
  13997. "]"))))
  13998. ;; We use our own fill-paragraph function, to make sure that tables
  13999. ;; and fixed-width regions are not wrapped. That function will pass
  14000. ;; through to `fill-paragraph' when appropriate.
  14001. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  14002. ; Adaptive filling: To get full control, first make sure that
  14003. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  14004. (org-set-local 'adaptive-fill-regexp "\000")
  14005. (org-set-local 'adaptive-fill-function
  14006. 'org-adaptive-fill-function)
  14007. (org-set-local
  14008. 'align-mode-rules-list
  14009. '((org-in-buffer-settings
  14010. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  14011. (modes . '(org-mode))))))
  14012. (defun org-fill-paragraph (&optional justify)
  14013. "Re-align a table, pass through to fill-paragraph if no table."
  14014. (let ((table-p (org-at-table-p))
  14015. (table.el-p (org-at-table.el-p)))
  14016. (cond ((and (equal (char-after (point-at-bol)) ?*)
  14017. (save-excursion (goto-char (point-at-bol))
  14018. (looking-at outline-regexp)))
  14019. t) ; skip headlines
  14020. (table.el-p t) ; skip table.el tables
  14021. (table-p (org-table-align) t) ; align org-mode tables
  14022. (t nil)))) ; call paragraph-fill
  14023. ;; For reference, this is the default value of adaptive-fill-regexp
  14024. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  14025. (defun org-adaptive-fill-function ()
  14026. "Return a fill prefix for org-mode files.
  14027. In particular, this makes sure hanging paragraphs for hand-formatted lists
  14028. work correctly."
  14029. (cond ((looking-at "#[ \t]+")
  14030. (match-string 0))
  14031. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  14032. (save-excursion
  14033. (goto-char (match-end 0))
  14034. (make-string (current-column) ?\ )))
  14035. (t nil)))
  14036. ;;; Other stuff.
  14037. (defun org-toggle-fixed-width-section (arg)
  14038. "Toggle the fixed-width export.
  14039. If there is no active region, the QUOTE keyword at the current headline is
  14040. inserted or removed. When present, it causes the text between this headline
  14041. and the next to be exported as fixed-width text, and unmodified.
  14042. If there is an active region, this command adds or removes a colon as the
  14043. first character of this line. If the first character of a line is a colon,
  14044. this line is also exported in fixed-width font."
  14045. (interactive "P")
  14046. (let* ((cc 0)
  14047. (regionp (org-region-active-p))
  14048. (beg (if regionp (region-beginning) (point)))
  14049. (end (if regionp (region-end)))
  14050. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  14051. (case-fold-search nil)
  14052. (re "[ \t]*\\(:\\)")
  14053. off)
  14054. (if regionp
  14055. (save-excursion
  14056. (goto-char beg)
  14057. (setq cc (current-column))
  14058. (beginning-of-line 1)
  14059. (setq off (looking-at re))
  14060. (while (> nlines 0)
  14061. (setq nlines (1- nlines))
  14062. (beginning-of-line 1)
  14063. (cond
  14064. (arg
  14065. (move-to-column cc t)
  14066. (insert ":\n")
  14067. (forward-line -1))
  14068. ((and off (looking-at re))
  14069. (replace-match "" t t nil 1))
  14070. ((not off) (move-to-column cc t) (insert ":")))
  14071. (forward-line 1)))
  14072. (save-excursion
  14073. (org-back-to-heading)
  14074. (if (looking-at (concat outline-regexp
  14075. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  14076. (replace-match "" t t nil 1)
  14077. (if (looking-at outline-regexp)
  14078. (progn
  14079. (goto-char (match-end 0))
  14080. (insert org-quote-string " "))))))))
  14081. ;;;; Functions extending outline functionality
  14082. (defun org-beginning-of-line (&optional arg)
  14083. "Go to the beginning of the current line. If that is invisible, continue
  14084. to a visible line beginning. This makes the function of C-a more intuitive.
  14085. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14086. first attempt, and only move to after the tags when the cursor is already
  14087. beyond the end of the headline."
  14088. (interactive "P")
  14089. (let ((pos (point)))
  14090. (beginning-of-line 1)
  14091. (if (bobp)
  14092. nil
  14093. (backward-char 1)
  14094. (if (org-invisible-p)
  14095. (while (and (not (bobp)) (org-invisible-p))
  14096. (backward-char 1)
  14097. (beginning-of-line 1))
  14098. (forward-char 1)))
  14099. (when org-special-ctrl-a/e
  14100. (cond
  14101. ((and (looking-at org-todo-line-regexp)
  14102. (= (char-after (match-end 1)) ?\ ))
  14103. (goto-char
  14104. (if (eq org-special-ctrl-a/e t)
  14105. (cond ((> pos (match-beginning 3)) (match-beginning 3))
  14106. ((= pos (point)) (match-beginning 3))
  14107. (t (point)))
  14108. (cond ((> pos (point)) (point))
  14109. ((not (eq last-command this-command)) (point))
  14110. (t (match-beginning 3))))))
  14111. ((org-at-item-p)
  14112. (goto-char
  14113. (if (eq org-special-ctrl-a/e t)
  14114. (cond ((> pos (match-end 4)) (match-end 4))
  14115. ((= pos (point)) (match-end 4))
  14116. (t (point)))
  14117. (cond ((> pos (point)) (point))
  14118. ((not (eq last-command this-command)) (point))
  14119. (t (match-end 4))))))))))
  14120. (defun org-end-of-line (&optional arg)
  14121. "Go to the end of the line.
  14122. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14123. first attempt, and only move to after the tags when the cursor is already
  14124. beyond the end of the headline."
  14125. (interactive "P")
  14126. (if (or (not org-special-ctrl-a/e)
  14127. (not (org-on-heading-p)))
  14128. (end-of-line arg)
  14129. (let ((pos (point)))
  14130. (beginning-of-line 1)
  14131. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14132. (if (eq org-special-ctrl-a/e t)
  14133. (if (or (< pos (match-beginning 1))
  14134. (= pos (match-end 0)))
  14135. (goto-char (match-beginning 1))
  14136. (goto-char (match-end 0)))
  14137. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  14138. (goto-char (match-end 0))
  14139. (goto-char (match-beginning 1))))
  14140. (end-of-line arg)))))
  14141. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  14142. (define-key org-mode-map "\C-e" 'org-end-of-line)
  14143. (defun org-kill-line (&optional arg)
  14144. "Kill line, to tags or end of line."
  14145. (interactive "P")
  14146. (cond
  14147. ((or (not org-special-ctrl-k)
  14148. (bolp)
  14149. (not (org-on-heading-p)))
  14150. (call-interactively 'kill-line))
  14151. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  14152. (kill-region (point) (match-beginning 1))
  14153. (org-set-tags nil t))
  14154. (t (kill-region (point) (point-at-eol)))))
  14155. (define-key org-mode-map "\C-k" 'org-kill-line)
  14156. (defun org-invisible-p ()
  14157. "Check if point is at a character currently not visible."
  14158. ;; Early versions of noutline don't have `outline-invisible-p'.
  14159. (if (fboundp 'outline-invisible-p)
  14160. (outline-invisible-p)
  14161. (get-char-property (point) 'invisible)))
  14162. (defun org-invisible-p2 ()
  14163. "Check if point is at a character currently not visible."
  14164. (save-excursion
  14165. (if (and (eolp) (not (bobp))) (backward-char 1))
  14166. ;; Early versions of noutline don't have `outline-invisible-p'.
  14167. (if (fboundp 'outline-invisible-p)
  14168. (outline-invisible-p)
  14169. (get-char-property (point) 'invisible))))
  14170. (defalias 'org-back-to-heading 'outline-back-to-heading)
  14171. (defalias 'org-on-heading-p 'outline-on-heading-p)
  14172. (defalias 'org-at-heading-p 'outline-on-heading-p)
  14173. (defun org-at-heading-or-item-p ()
  14174. (or (org-on-heading-p) (org-at-item-p)))
  14175. (defun org-on-target-p ()
  14176. (or (org-in-regexp org-radio-target-regexp)
  14177. (org-in-regexp org-target-regexp)))
  14178. (defun org-up-heading-all (arg)
  14179. "Move to the heading line of which the present line is a subheading.
  14180. This function considers both visible and invisible heading lines.
  14181. With argument, move up ARG levels."
  14182. (if (fboundp 'outline-up-heading-all)
  14183. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  14184. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  14185. (defun org-up-heading-safe ()
  14186. "Move to the heading line of which the present line is a subheading.
  14187. This version will not throw an error. It will return the level of the
  14188. headline found, or nil if no higher level is found."
  14189. (let ((pos (point)) start-level level
  14190. (re (concat "^" outline-regexp)))
  14191. (catch 'exit
  14192. (outline-back-to-heading t)
  14193. (setq start-level (funcall outline-level))
  14194. (if (equal start-level 1) (throw 'exit nil))
  14195. (while (re-search-backward re nil t)
  14196. (setq level (funcall outline-level))
  14197. (if (< level start-level) (throw 'exit level)))
  14198. nil)))
  14199. (defun org-first-sibling-p ()
  14200. "Is this heading the first child of its parents?"
  14201. (interactive)
  14202. (let ((re (concat "^" outline-regexp))
  14203. level l)
  14204. (unless (org-at-heading-p t)
  14205. (error "Not at a heading"))
  14206. (setq level (funcall outline-level))
  14207. (save-excursion
  14208. (if (not (re-search-backward re nil t))
  14209. t
  14210. (setq l (funcall outline-level))
  14211. (< l level)))))
  14212. (defun org-goto-sibling (&optional previous)
  14213. "Goto the next sibling, even if it is invisible.
  14214. When PREVIOUS is set, go to the previous sibling instead. Returns t
  14215. when a sibling was found. When none is found, return nil and don't
  14216. move point."
  14217. (let ((fun (if previous 're-search-backward 're-search-forward))
  14218. (pos (point))
  14219. (re (concat "^" outline-regexp))
  14220. level l)
  14221. (when (condition-case nil (org-back-to-heading t) (error nil))
  14222. (setq level (funcall outline-level))
  14223. (catch 'exit
  14224. (or previous (forward-char 1))
  14225. (while (funcall fun re nil t)
  14226. (setq l (funcall outline-level))
  14227. (when (< l level) (goto-char pos) (throw 'exit nil))
  14228. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  14229. (goto-char pos)
  14230. nil))))
  14231. (defun org-show-siblings ()
  14232. "Show all siblings of the current headline."
  14233. (save-excursion
  14234. (while (org-goto-sibling) (org-flag-heading nil)))
  14235. (save-excursion
  14236. (while (org-goto-sibling 'previous)
  14237. (org-flag-heading nil))))
  14238. (defun org-show-hidden-entry ()
  14239. "Show an entry where even the heading is hidden."
  14240. (save-excursion
  14241. (org-show-entry)))
  14242. (defun org-flag-heading (flag &optional entry)
  14243. "Flag the current heading. FLAG non-nil means make invisible.
  14244. When ENTRY is non-nil, show the entire entry."
  14245. (save-excursion
  14246. (org-back-to-heading t)
  14247. ;; Check if we should show the entire entry
  14248. (if entry
  14249. (progn
  14250. (org-show-entry)
  14251. (save-excursion
  14252. (and (outline-next-heading)
  14253. (org-flag-heading nil))))
  14254. (outline-flag-region (max (point-min) (1- (point)))
  14255. (save-excursion (outline-end-of-heading) (point))
  14256. flag))))
  14257. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  14258. ;; This is an exact copy of the original function, but it uses
  14259. ;; `org-back-to-heading', to make it work also in invisible
  14260. ;; trees. And is uses an invisible-OK argument.
  14261. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  14262. (org-back-to-heading invisible-OK)
  14263. (let ((first t)
  14264. (level (funcall outline-level)))
  14265. (while (and (not (eobp))
  14266. (or first (> (funcall outline-level) level)))
  14267. (setq first nil)
  14268. (outline-next-heading))
  14269. (unless to-heading
  14270. (if (memq (preceding-char) '(?\n ?\^M))
  14271. (progn
  14272. ;; Go to end of line before heading
  14273. (forward-char -1)
  14274. (if (memq (preceding-char) '(?\n ?\^M))
  14275. ;; leave blank line before heading
  14276. (forward-char -1))))))
  14277. (point))
  14278. (defun org-show-subtree ()
  14279. "Show everything after this heading at deeper levels."
  14280. (outline-flag-region
  14281. (point)
  14282. (save-excursion
  14283. (outline-end-of-subtree) (outline-next-heading) (point))
  14284. nil))
  14285. (defun org-show-entry ()
  14286. "Show the body directly following this heading.
  14287. Show the heading too, if it is currently invisible."
  14288. (interactive)
  14289. (save-excursion
  14290. (condition-case nil
  14291. (progn
  14292. (org-back-to-heading t)
  14293. (outline-flag-region
  14294. (max (point-min) (1- (point)))
  14295. (save-excursion
  14296. (re-search-forward
  14297. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  14298. (or (match-beginning 1) (point-max)))
  14299. nil))
  14300. (error nil))))
  14301. (defun org-make-options-regexp (kwds)
  14302. "Make a regular expression for keyword lines."
  14303. (concat
  14304. "^"
  14305. "#?[ \t]*\\+\\("
  14306. (mapconcat 'regexp-quote kwds "\\|")
  14307. "\\):[ \t]*"
  14308. "\\(.+\\)"))
  14309. ;; Make isearch reveal the necessary context
  14310. (defun org-isearch-end ()
  14311. "Reveal context after isearch exits."
  14312. (when isearch-success ; only if search was successful
  14313. (if (featurep 'xemacs)
  14314. ;; Under XEmacs, the hook is run in the correct place,
  14315. ;; we directly show the context.
  14316. (org-show-context 'isearch)
  14317. ;; In Emacs the hook runs *before* restoring the overlays.
  14318. ;; So we have to use a one-time post-command-hook to do this.
  14319. ;; (Emacs 22 has a special variable, see function `org-mode')
  14320. (unless (and (boundp 'isearch-mode-end-hook-quit)
  14321. isearch-mode-end-hook-quit)
  14322. ;; Only when the isearch was not quitted.
  14323. (org-add-hook 'post-command-hook 'org-isearch-post-command
  14324. 'append 'local)))))
  14325. (defun org-isearch-post-command ()
  14326. "Remove self from hook, and show context."
  14327. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  14328. (org-show-context 'isearch))
  14329. ;;;; Integration with and fixes for other packages
  14330. ;;; Imenu support
  14331. (defvar org-imenu-markers nil
  14332. "All markers currently used by Imenu.")
  14333. (make-variable-buffer-local 'org-imenu-markers)
  14334. (defun org-imenu-new-marker (&optional pos)
  14335. "Return a new marker for use by Imenu, and remember the marker."
  14336. (let ((m (make-marker)))
  14337. (move-marker m (or pos (point)))
  14338. (push m org-imenu-markers)
  14339. m))
  14340. (defun org-imenu-get-tree ()
  14341. "Produce the index for Imenu."
  14342. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  14343. (setq org-imenu-markers nil)
  14344. (let* ((n org-imenu-depth)
  14345. (re (concat "^" outline-regexp))
  14346. (subs (make-vector (1+ n) nil))
  14347. (last-level 0)
  14348. m tree level head)
  14349. (save-excursion
  14350. (save-restriction
  14351. (widen)
  14352. (goto-char (point-max))
  14353. (while (re-search-backward re nil t)
  14354. (setq level (org-reduced-level (funcall outline-level)))
  14355. (when (<= level n)
  14356. (looking-at org-complex-heading-regexp)
  14357. (setq head (org-match-string-no-properties 4)
  14358. m (org-imenu-new-marker))
  14359. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  14360. (if (>= level last-level)
  14361. (push (cons head m) (aref subs level))
  14362. (push (cons head (aref subs (1+ level))) (aref subs level))
  14363. (loop for i from (1+ level) to n do (aset subs i nil)))
  14364. (setq last-level level)))))
  14365. (aref subs 1)))
  14366. (eval-after-load "imenu"
  14367. '(progn
  14368. (add-hook 'imenu-after-jump-hook
  14369. (lambda () (org-show-context 'org-goto)))))
  14370. ;; Speedbar support
  14371. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  14372. "Overlay marking the agenda restriction line in speedbar.")
  14373. (org-overlay-put org-speedbar-restriction-lock-overlay
  14374. 'face 'org-agenda-restriction-lock)
  14375. (org-overlay-put org-speedbar-restriction-lock-overlay
  14376. 'help-echo "Agendas are currently limited to this item.")
  14377. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  14378. (defun org-speedbar-set-agenda-restriction ()
  14379. "Restrict future agenda commands to the location at point in speedbar.
  14380. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  14381. (interactive)
  14382. (let (p m tp np dir txt w)
  14383. (cond
  14384. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14385. 'org-imenu t))
  14386. (setq m (get-text-property p 'org-imenu-marker))
  14387. (save-excursion
  14388. (save-restriction
  14389. (set-buffer (marker-buffer m))
  14390. (goto-char m)
  14391. (org-agenda-set-restriction-lock 'subtree))))
  14392. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14393. 'speedbar-function 'speedbar-find-file))
  14394. (setq tp (previous-single-property-change
  14395. (1+ p) 'speedbar-function)
  14396. np (next-single-property-change
  14397. tp 'speedbar-function)
  14398. dir (speedbar-line-directory)
  14399. txt (buffer-substring-no-properties (or tp (point-min))
  14400. (or np (point-max))))
  14401. (save-excursion
  14402. (save-restriction
  14403. (set-buffer (find-file-noselect
  14404. (let ((default-directory dir))
  14405. (expand-file-name txt))))
  14406. (unless (org-mode-p)
  14407. (error "Cannot restrict to non-Org-mode file"))
  14408. (org-agenda-set-restriction-lock 'file))))
  14409. (t (error "Don't know how to restrict Org-mode's agenda")))
  14410. (org-move-overlay org-speedbar-restriction-lock-overlay
  14411. (point-at-bol) (point-at-eol))
  14412. (setq current-prefix-arg nil)
  14413. (org-agenda-maybe-redo)))
  14414. (eval-after-load "speedbar"
  14415. '(progn
  14416. (speedbar-add-supported-extension ".org")
  14417. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  14418. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  14419. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  14420. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  14421. (add-hook 'speedbar-visiting-tag-hook
  14422. (lambda () (org-show-context 'org-goto)))))
  14423. ;;; Fixes and Hacks
  14424. ;; Make flyspell not check words in links, to not mess up our keymap
  14425. (defun org-mode-flyspell-verify ()
  14426. "Don't let flyspell put overlays at active buttons."
  14427. (not (get-text-property (point) 'keymap)))
  14428. ;; Make `bookmark-jump' show the jump location if it was hidden.
  14429. (eval-after-load "bookmark"
  14430. '(if (boundp 'bookmark-after-jump-hook)
  14431. ;; We can use the hook
  14432. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  14433. ;; Hook not available, use advice
  14434. (defadvice bookmark-jump (after org-make-visible activate)
  14435. "Make the position visible."
  14436. (org-bookmark-jump-unhide))))
  14437. (defun org-bookmark-jump-unhide ()
  14438. "Unhide the current position, to show the bookmark location."
  14439. (and (org-mode-p)
  14440. (or (org-invisible-p)
  14441. (save-excursion (goto-char (max (point-min) (1- (point))))
  14442. (org-invisible-p)))
  14443. (org-show-context 'bookmark-jump)))
  14444. ;; Make session.el ignore our circular variable
  14445. (eval-after-load "session"
  14446. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  14447. ;;;; Experimental code
  14448. (defun org-closed-in-range ()
  14449. "Sparse tree of items closed in a certain time range.
  14450. Still experimental, may disappear in the future."
  14451. (interactive)
  14452. ;; Get the time interval from the user.
  14453. (let* ((time1 (time-to-seconds
  14454. (org-read-date nil 'to-time nil "Starting date: ")))
  14455. (time2 (time-to-seconds
  14456. (org-read-date nil 'to-time nil "End date:")))
  14457. ;; callback function
  14458. (callback (lambda ()
  14459. (let ((time
  14460. (time-to-seconds
  14461. (apply 'encode-time
  14462. (org-parse-time-string
  14463. (match-string 1))))))
  14464. ;; check if time in interval
  14465. (and (>= time time1) (<= time time2))))))
  14466. ;; make tree, check each match with the callback
  14467. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  14468. ;;;; Finish up
  14469. (provide 'org)
  14470. (run-hooks 'org-load-hook)
  14471. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  14472. ;;; org.el ends here
  14473. (defun org-get-heading (&optional no-tags)
  14474. "Return the heading of the current entry, without the stars."
  14475. (save-excursion
  14476. (org-back-to-heading t)
  14477. (if (looking-at
  14478. (if no-tags
  14479. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  14480. "\\*+[ \t]+\\([^\r\n]*\\)"))
  14481. (match-string 1) "")))
  14482. (defun org-get-category (&optional pos)
  14483. "Get the category applying to position POS."
  14484. (get-text-property (or pos (point)) 'org-category))
  14485. (defun org-get-tags-at (&optional pos)
  14486. "Get a list of all headline tags applicable at POS.
  14487. POS defaults to point. If tags are inherited, the list contains
  14488. the targets in the same sequence as the headlines appear, i.e.
  14489. sthe tags of the current headline come last."
  14490. (interactive)
  14491. (let (tags ltags lastpos parent)
  14492. (save-excursion
  14493. (save-restriction
  14494. (widen)
  14495. (goto-char (or pos (point)))
  14496. (save-match-data
  14497. (condition-case nil
  14498. (progn
  14499. (org-back-to-heading t)
  14500. (while (not (equal lastpos (point)))
  14501. (setq lastpos (point))
  14502. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  14503. (setq ltags (org-split-string
  14504. (org-match-string-no-properties 1) ":"))
  14505. (setq tags (append (org-remove-uniherited-tags ltags)
  14506. tags)))
  14507. (or org-use-tag-inheritance (error ""))
  14508. (org-up-heading-all 1)
  14509. (setq parent t)))
  14510. (error nil))))
  14511. tags)))
  14512. (defun org-days-to-iso-week (days)
  14513. "Return the iso week number."
  14514. (require 'cal-iso)
  14515. (car (calendar-iso-from-absolute days)))
  14516. (defun org-small-year-to-year (year)
  14517. "Convert 2-digit years into 4-digit years.
  14518. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  14519. The year 2000 cannot be abbreviated. Any year lager than 99
  14520. is retrned unchanged."
  14521. (if (< year 38)
  14522. (setq year (+ 2000 year))
  14523. (if (< year 100)
  14524. (setq year (+ 1900 year))))
  14525. year)