org.el 581 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744
  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-startup-options
  2568. '(("fold" org-startup-folded t)
  2569. ("overview" org-startup-folded t)
  2570. ("nofold" org-startup-folded nil)
  2571. ("showall" org-startup-folded nil)
  2572. ("content" org-startup-folded content)
  2573. ("hidestars" org-hide-leading-stars t)
  2574. ("showstars" org-hide-leading-stars nil)
  2575. ("odd" org-odd-levels-only t)
  2576. ("oddeven" org-odd-levels-only nil)
  2577. ("align" org-startup-align-all-tables t)
  2578. ("noalign" org-startup-align-all-tables nil)
  2579. ("customtime" org-display-custom-times t)
  2580. ("logdone" org-log-done time)
  2581. ("lognotedone" org-log-done note)
  2582. ("nologdone" org-log-done nil)
  2583. ("lognoteclock-out" org-log-note-clock-out t)
  2584. ("nolognoteclock-out" org-log-note-clock-out nil)
  2585. ("logrepeat" org-log-repeat state)
  2586. ("lognoterepeat" org-log-repeat note)
  2587. ("nologrepeat" org-log-repeat nil)
  2588. ("constcgs" constants-unit-system cgs)
  2589. ("constSI" constants-unit-system SI))
  2590. "Variable associated with STARTUP options for org-mode.
  2591. Each element is a list of three items: The startup options as written
  2592. in the #+STARTUP line, the corresponding variable, and the value to
  2593. set this variable to if the option is found. An optional forth element PUSH
  2594. means to push this value onto the list in the variable.")
  2595. (defun org-set-regexps-and-options ()
  2596. "Precompute regular expressions for current buffer."
  2597. (when (org-mode-p)
  2598. (org-set-local 'org-todo-kwd-alist nil)
  2599. (org-set-local 'org-todo-key-alist nil)
  2600. (org-set-local 'org-todo-key-trigger nil)
  2601. (org-set-local 'org-todo-keywords-1 nil)
  2602. (org-set-local 'org-done-keywords nil)
  2603. (org-set-local 'org-todo-heads nil)
  2604. (org-set-local 'org-todo-sets nil)
  2605. (org-set-local 'org-todo-log-states nil)
  2606. (let ((re (org-make-options-regexp
  2607. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  2608. "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES"
  2609. "CONSTANTS" "PROPERTY" "DRAWERS")))
  2610. (splitre "[ \t]+")
  2611. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2612. tail sep kws1 prio props drawers)
  2613. (save-excursion
  2614. (save-restriction
  2615. (widen)
  2616. (goto-char (point-min))
  2617. (while (re-search-forward re nil t)
  2618. (setq key (match-string 1) value (org-match-string-no-properties 2))
  2619. (cond
  2620. ((equal key "CATEGORY")
  2621. (if (string-match "[ \t]+$" value)
  2622. (setq value (replace-match "" t t value)))
  2623. (setq cat value))
  2624. ((member key '("SEQ_TODO" "TODO"))
  2625. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2626. ((equal key "TYP_TODO")
  2627. (push (cons 'type (org-split-string value splitre)) kwds))
  2628. ((equal key "TAGS")
  2629. (setq tags (append tags (org-split-string value splitre))))
  2630. ((equal key "COLUMNS")
  2631. (org-set-local 'org-columns-default-format value))
  2632. ((equal key "LINK")
  2633. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2634. (push (cons (match-string 1 value)
  2635. (org-trim (match-string 2 value)))
  2636. links)))
  2637. ((equal key "PRIORITIES")
  2638. (setq prio (org-split-string value " +")))
  2639. ((equal key "PROPERTY")
  2640. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2641. (push (cons (match-string 1 value) (match-string 2 value))
  2642. props)))
  2643. ((equal key "DRAWERS")
  2644. (setq drawers (org-split-string value splitre)))
  2645. ((equal key "CONSTANTS")
  2646. (setq const (append const (org-split-string value splitre))))
  2647. ((equal key "STARTUP")
  2648. (let ((opts (org-split-string value splitre))
  2649. l var val)
  2650. (while (setq l (pop opts))
  2651. (when (setq l (assoc l org-startup-options))
  2652. (setq var (nth 1 l) val (nth 2 l))
  2653. (if (not (nth 3 l))
  2654. (set (make-local-variable var) val)
  2655. (if (not (listp (symbol-value var)))
  2656. (set (make-local-variable var) nil))
  2657. (set (make-local-variable var) (symbol-value var))
  2658. (add-to-list var val))))))
  2659. ((equal key "ARCHIVE")
  2660. (string-match " *$" value)
  2661. (setq arch (replace-match "" t t value))
  2662. (remove-text-properties 0 (length arch)
  2663. '(face t fontified t) arch)))
  2664. )))
  2665. (when cat
  2666. (org-set-local 'org-category (intern cat))
  2667. (push (cons "CATEGORY" cat) props))
  2668. (when prio
  2669. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2670. (setq prio (mapcar 'string-to-char prio))
  2671. (org-set-local 'org-highest-priority (nth 0 prio))
  2672. (org-set-local 'org-lowest-priority (nth 1 prio))
  2673. (org-set-local 'org-default-priority (nth 2 prio)))
  2674. (and props (org-set-local 'org-local-properties (nreverse props)))
  2675. (and drawers (org-set-local 'org-drawers drawers))
  2676. (and arch (org-set-local 'org-archive-location arch))
  2677. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2678. ;; Process the TODO keywords
  2679. (unless kwds
  2680. ;; Use the global values as if they had been given locally.
  2681. (setq kwds (default-value 'org-todo-keywords))
  2682. (if (stringp (car kwds))
  2683. (setq kwds (list (cons org-todo-interpretation
  2684. (default-value 'org-todo-keywords)))))
  2685. (setq kwds (reverse kwds)))
  2686. (setq kwds (nreverse kwds))
  2687. (let (inter kws kw)
  2688. (while (setq kws (pop kwds))
  2689. (setq inter (pop kws) sep (member "|" kws)
  2690. kws0 (delete "|" (copy-sequence kws))
  2691. kwsa nil
  2692. kws1 (mapcar
  2693. (lambda (x)
  2694. ;; 1 2
  2695. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2696. (progn
  2697. (setq kw (match-string 1 x)
  2698. key (and (match-end 2) (match-string 2 x))
  2699. log (org-extract-log-state-settings x))
  2700. (push (cons kw (and key (string-to-char key))) kwsa)
  2701. (and log (push log org-todo-log-states))
  2702. kw)
  2703. (error "Invalid TODO keyword %s" x)))
  2704. kws0)
  2705. kwsa (if kwsa (append '((:startgroup))
  2706. (nreverse kwsa)
  2707. '((:endgroup))))
  2708. hw (car kws1)
  2709. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2710. tail (list inter hw (car dws) (org-last dws)))
  2711. (add-to-list 'org-todo-heads hw 'append)
  2712. (push kws1 org-todo-sets)
  2713. (setq org-done-keywords (append org-done-keywords dws nil))
  2714. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2715. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2716. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2717. (setq org-todo-sets (nreverse org-todo-sets)
  2718. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2719. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2720. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2721. ;; Process the constants
  2722. (when const
  2723. (let (e cst)
  2724. (while (setq e (pop const))
  2725. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2726. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2727. (setq org-table-formula-constants-local cst)))
  2728. ;; Process the tags.
  2729. (when tags
  2730. (let (e tgs)
  2731. (while (setq e (pop tags))
  2732. (cond
  2733. ((equal e "{") (push '(:startgroup) tgs))
  2734. ((equal e "}") (push '(:endgroup) tgs))
  2735. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2736. (push (cons (match-string 1 e)
  2737. (string-to-char (match-string 2 e)))
  2738. tgs))
  2739. (t (push (list e) tgs))))
  2740. (org-set-local 'org-tag-alist nil)
  2741. (while (setq e (pop tgs))
  2742. (or (and (stringp (car e))
  2743. (assoc (car e) org-tag-alist))
  2744. (push e org-tag-alist))))))
  2745. ;; Compute the regular expressions and other local variables
  2746. (if (not org-done-keywords)
  2747. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  2748. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2749. (length org-scheduled-string)))
  2750. org-drawer-regexp
  2751. (concat "^[ \t]*:\\("
  2752. (mapconcat 'regexp-quote org-drawers "\\|")
  2753. "\\):[ \t]*$")
  2754. org-not-done-keywords
  2755. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  2756. org-todo-regexp
  2757. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  2758. "\\|") "\\)\\>")
  2759. org-not-done-regexp
  2760. (concat "\\<\\("
  2761. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  2762. "\\)\\>")
  2763. org-todo-line-regexp
  2764. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2765. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2766. "\\)\\>\\)?[ \t]*\\(.*\\)")
  2767. org-complex-heading-regexp
  2768. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  2769. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2770. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  2771. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  2772. org-nl-done-regexp
  2773. (concat "\n\\*+[ \t]+"
  2774. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  2775. "\\)" "\\>")
  2776. org-todo-line-tags-regexp
  2777. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2778. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2779. (org-re
  2780. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  2781. org-looking-at-done-regexp
  2782. (concat "^" "\\(?:"
  2783. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  2784. "\\>")
  2785. org-deadline-regexp (concat "\\<" org-deadline-string)
  2786. org-deadline-time-regexp
  2787. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2788. org-deadline-line-regexp
  2789. (concat "\\<\\(" org-deadline-string "\\).*")
  2790. org-scheduled-regexp
  2791. (concat "\\<" org-scheduled-string)
  2792. org-scheduled-time-regexp
  2793. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2794. org-closed-time-regexp
  2795. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2796. org-keyword-time-regexp
  2797. (concat "\\<\\(" org-scheduled-string
  2798. "\\|" org-deadline-string
  2799. "\\|" org-closed-string
  2800. "\\|" org-clock-string "\\)"
  2801. " *[[<]\\([^]>]+\\)[]>]")
  2802. org-keyword-time-not-clock-regexp
  2803. (concat "\\<\\(" org-scheduled-string
  2804. "\\|" org-deadline-string
  2805. "\\|" org-closed-string
  2806. "\\)"
  2807. " *[[<]\\([^]>]+\\)[]>]")
  2808. org-maybe-keyword-time-regexp
  2809. (concat "\\(\\<\\(" org-scheduled-string
  2810. "\\|" org-deadline-string
  2811. "\\|" org-closed-string
  2812. "\\|" org-clock-string "\\)\\)?"
  2813. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  2814. org-planning-or-clock-line-re
  2815. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  2816. "\\|" org-deadline-string
  2817. "\\|" org-closed-string "\\|" org-clock-string
  2818. "\\)\\>\\)")
  2819. )
  2820. (org-compute-latex-and-specials-regexp)
  2821. (org-set-font-lock-defaults)))
  2822. (defun org-extract-log-state-settings (x)
  2823. "Extract the log state setting from a TODO keyword string.
  2824. This will extract info from a string like \"WAIT(w@/!)\"."
  2825. (let (kw key log1 log2)
  2826. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  2827. (setq kw (match-string 1 x)
  2828. key (and (match-end 2) (match-string 2 x))
  2829. log1 (and (match-end 3) (match-string 3 x))
  2830. log2 (and (match-end 4) (match-string 4 x)))
  2831. (and (or log1 log2)
  2832. (list kw
  2833. (and log1 (if (equal log1 "!") 'time 'note))
  2834. (and log2 (if (equal log2 "!") 'time 'note)))))))
  2835. (defun org-remove-keyword-keys (list)
  2836. "Remove a pair of parenthesis at the end of each string in LIST."
  2837. (mapcar (lambda (x)
  2838. (if (string-match "(.*)$" x)
  2839. (substring x 0 (match-beginning 0))
  2840. x))
  2841. list))
  2842. ;; FIXME: this could be done much better, using second characters etc.
  2843. (defun org-assign-fast-keys (alist)
  2844. "Assign fast keys to a keyword-key alist.
  2845. Respect keys that are already there."
  2846. (let (new e k c c1 c2 (char ?a))
  2847. (while (setq e (pop alist))
  2848. (cond
  2849. ((equal e '(:startgroup)) (push e new))
  2850. ((equal e '(:endgroup)) (push e new))
  2851. (t
  2852. (setq k (car e) c2 nil)
  2853. (if (cdr e)
  2854. (setq c (cdr e))
  2855. ;; automatically assign a character.
  2856. (setq c1 (string-to-char
  2857. (downcase (substring
  2858. k (if (= (string-to-char k) ?@) 1 0)))))
  2859. (if (or (rassoc c1 new) (rassoc c1 alist))
  2860. (while (or (rassoc char new) (rassoc char alist))
  2861. (setq char (1+ char)))
  2862. (setq c2 c1))
  2863. (setq c (or c2 char)))
  2864. (push (cons k c) new))))
  2865. (nreverse new)))
  2866. ;;; Some variables ujsed in various places
  2867. (defvar org-window-configuration nil
  2868. "Used in various places to store a window configuration.")
  2869. (defvar org-finish-function nil
  2870. "Function to be called when `C-c C-c' is used.
  2871. This is for getting out of special buffers like remember.")
  2872. ;; FIXME: Occasionally check by commenting these, to make sure
  2873. ;; no other functions uses these, forgetting to let-bind them.
  2874. (defvar entry)
  2875. (defvar state)
  2876. (defvar last-state)
  2877. (defvar date)
  2878. (defvar description)
  2879. ;; Defined somewhere in this file, but used before definition.
  2880. (defvar org-html-entities)
  2881. (defvar org-struct-menu)
  2882. (defvar org-org-menu)
  2883. (defvar org-tbl-menu)
  2884. (defvar org-agenda-keymap)
  2885. ;;;; Define the Org-mode
  2886. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  2887. (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."))
  2888. ;; We use a before-change function to check if a table might need
  2889. ;; an update.
  2890. (defvar org-table-may-need-update t
  2891. "Indicates that a table might need an update.
  2892. This variable is set by `org-before-change-function'.
  2893. `org-table-align' sets it back to nil.")
  2894. (defun org-before-change-function (beg end)
  2895. "Every change indicates that a table might need an update."
  2896. (setq org-table-may-need-update t))
  2897. (defvar org-mode-map)
  2898. (defvar org-mode-hook nil)
  2899. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  2900. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  2901. (defvar org-table-buffer-is-an nil)
  2902. (defconst org-outline-regexp "\\*+ ")
  2903. ;;;###autoload
  2904. (define-derived-mode org-mode outline-mode "Org"
  2905. "Outline-based notes management and organizer, alias
  2906. \"Carsten's outline-mode for keeping track of everything.\"
  2907. Org-mode develops organizational tasks around a NOTES file which
  2908. contains information about projects as plain text. Org-mode is
  2909. implemented on top of outline-mode, which is ideal to keep the content
  2910. of large files well structured. It supports ToDo items, deadlines and
  2911. time stamps, which magically appear in the diary listing of the Emacs
  2912. calendar. Tables are easily created with a built-in table editor.
  2913. Plain text URL-like links connect to websites, emails (VM), Usenet
  2914. messages (Gnus), BBDB entries, and any files related to the project.
  2915. For printing and sharing of notes, an Org-mode file (or a part of it)
  2916. can be exported as a structured ASCII or HTML file.
  2917. The following commands are available:
  2918. \\{org-mode-map}"
  2919. ;; Get rid of Outline menus, they are not needed
  2920. ;; Need to do this here because define-derived-mode sets up
  2921. ;; the keymap so late. Still, it is a waste to call this each time
  2922. ;; we switch another buffer into org-mode.
  2923. (if (featurep 'xemacs)
  2924. (when (boundp 'outline-mode-menu-heading)
  2925. ;; Assume this is Greg's port, it used easymenu
  2926. (easy-menu-remove outline-mode-menu-heading)
  2927. (easy-menu-remove outline-mode-menu-show)
  2928. (easy-menu-remove outline-mode-menu-hide))
  2929. (define-key org-mode-map [menu-bar headings] 'undefined)
  2930. (define-key org-mode-map [menu-bar hide] 'undefined)
  2931. (define-key org-mode-map [menu-bar show] 'undefined))
  2932. (org-load-modules-maybe)
  2933. (easy-menu-add org-org-menu)
  2934. (easy-menu-add org-tbl-menu)
  2935. (org-install-agenda-files-menu)
  2936. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  2937. (org-add-to-invisibility-spec '(org-cwidth))
  2938. (when (featurep 'xemacs)
  2939. (org-set-local 'line-move-ignore-invisible t))
  2940. (org-set-local 'outline-regexp org-outline-regexp)
  2941. (org-set-local 'outline-level 'org-outline-level)
  2942. (when (and org-ellipsis
  2943. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  2944. (fboundp 'make-glyph-code))
  2945. (unless org-display-table
  2946. (setq org-display-table (make-display-table)))
  2947. (set-display-table-slot
  2948. org-display-table 4
  2949. (vconcat (mapcar
  2950. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  2951. org-ellipsis)))
  2952. (if (stringp org-ellipsis) org-ellipsis "..."))))
  2953. (setq buffer-display-table org-display-table))
  2954. (org-set-regexps-and-options)
  2955. ;; Calc embedded
  2956. (org-set-local 'calc-embedded-open-mode "# ")
  2957. (modify-syntax-entry ?# "<")
  2958. (modify-syntax-entry ?@ "w")
  2959. (if org-startup-truncated (setq truncate-lines t))
  2960. (org-set-local 'font-lock-unfontify-region-function
  2961. 'org-unfontify-region)
  2962. ;; Activate before-change-function
  2963. (org-set-local 'org-table-may-need-update t)
  2964. (org-add-hook 'before-change-functions 'org-before-change-function nil
  2965. 'local)
  2966. ;; Check for running clock before killing a buffer
  2967. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  2968. ;; Paragraphs and auto-filling
  2969. (org-set-autofill-regexps)
  2970. (setq indent-line-function 'org-indent-line-function)
  2971. (org-update-radio-target-regexp)
  2972. ;; Comment characters
  2973. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  2974. (org-set-local 'comment-padding " ")
  2975. ;; Align options lines
  2976. (org-set-local
  2977. 'align-mode-rules-list
  2978. '((org-in-buffer-settings
  2979. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  2980. (modes . '(org-mode)))))
  2981. ;; Imenu
  2982. (org-set-local 'imenu-create-index-function
  2983. 'org-imenu-get-tree)
  2984. ;; Make isearch reveal context
  2985. (if (or (featurep 'xemacs)
  2986. (not (boundp 'outline-isearch-open-invisible-function)))
  2987. ;; Emacs 21 and XEmacs make use of the hook
  2988. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  2989. ;; Emacs 22 deals with this through a special variable
  2990. (org-set-local 'outline-isearch-open-invisible-function
  2991. (lambda (&rest ignore) (org-show-context 'isearch))))
  2992. ;; If empty file that did not turn on org-mode automatically, make it to.
  2993. (if (and org-insert-mode-line-in-empty-file
  2994. (interactive-p)
  2995. (= (point-min) (point-max)))
  2996. (insert "# -*- mode: org -*-\n\n"))
  2997. (unless org-inhibit-startup
  2998. (when org-startup-align-all-tables
  2999. (let ((bmp (buffer-modified-p)))
  3000. (org-table-map-tables 'org-table-align)
  3001. (set-buffer-modified-p bmp)))
  3002. (org-cycle-hide-drawers 'all)
  3003. (cond
  3004. ((eq org-startup-folded t)
  3005. (org-cycle '(4)))
  3006. ((eq org-startup-folded 'content)
  3007. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  3008. (org-cycle '(4)) (org-cycle '(4)))))))
  3009. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3010. (defun org-current-time ()
  3011. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3012. (if (> (car org-time-stamp-rounding-minutes) 1)
  3013. (let ((r (car org-time-stamp-rounding-minutes))
  3014. (time (decode-time)))
  3015. (apply 'encode-time
  3016. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3017. (nthcdr 2 time))))
  3018. (current-time)))
  3019. ;;;; Font-Lock stuff, including the activators
  3020. (defvar org-mouse-map (make-sparse-keymap))
  3021. (org-defkey org-mouse-map
  3022. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3023. (org-defkey org-mouse-map
  3024. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3025. (when org-mouse-1-follows-link
  3026. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3027. (when org-tab-follows-link
  3028. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3029. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3030. (when org-return-follows-link
  3031. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  3032. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  3033. (require 'font-lock)
  3034. (defconst org-non-link-chars "]\t\n\r<>")
  3035. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3036. "shell" "elisp"))
  3037. (defvar org-link-re-with-space nil
  3038. "Matches a link with spaces, optional angular brackets around it.")
  3039. (defvar org-link-re-with-space2 nil
  3040. "Matches a link with spaces, optional angular brackets around it.")
  3041. (defvar org-angle-link-re nil
  3042. "Matches link with angular brackets, spaces are allowed.")
  3043. (defvar org-plain-link-re nil
  3044. "Matches plain link, without spaces.")
  3045. (defvar org-bracket-link-regexp nil
  3046. "Matches a link in double brackets.")
  3047. (defvar org-bracket-link-analytic-regexp nil
  3048. "Regular expression used to analyze links.
  3049. Here is what the match groups contain after a match:
  3050. 1: http:
  3051. 2: http
  3052. 3: path
  3053. 4: [desc]
  3054. 5: desc")
  3055. (defvar org-any-link-re nil
  3056. "Regular expression matching any link.")
  3057. (defun org-make-link-regexps ()
  3058. "Update the link regular expressions.
  3059. This should be called after the variable `org-link-types' has changed."
  3060. (setq org-link-re-with-space
  3061. (concat
  3062. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3063. "\\([^" org-non-link-chars " ]"
  3064. "[^" org-non-link-chars "]*"
  3065. "[^" org-non-link-chars " ]\\)>?")
  3066. org-link-re-with-space2
  3067. (concat
  3068. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3069. "\\([^" org-non-link-chars " ]"
  3070. "[^]\t\n\r]*"
  3071. "[^" org-non-link-chars " ]\\)>?")
  3072. org-angle-link-re
  3073. (concat
  3074. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3075. "\\([^" org-non-link-chars " ]"
  3076. "[^" org-non-link-chars "]*"
  3077. "\\)>")
  3078. org-plain-link-re
  3079. (concat
  3080. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3081. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3082. org-bracket-link-regexp
  3083. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3084. org-bracket-link-analytic-regexp
  3085. (concat
  3086. "\\[\\["
  3087. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3088. "\\([^]]+\\)"
  3089. "\\]"
  3090. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3091. "\\]")
  3092. org-any-link-re
  3093. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3094. org-angle-link-re "\\)\\|\\("
  3095. org-plain-link-re "\\)")))
  3096. (org-make-link-regexps)
  3097. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3098. "Regular expression for fast time stamp matching.")
  3099. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3100. "Regular expression for fast time stamp matching.")
  3101. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3102. "Regular expression matching time strings for analysis.
  3103. This one does not require the space after the date, so it can be used
  3104. on a string that terminates immediately after the date.")
  3105. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3106. "Regular expression matching time strings for analysis.")
  3107. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3108. "Regular expression matching time stamps, with groups.")
  3109. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3110. "Regular expression matching time stamps (also [..]), with groups.")
  3111. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3112. "Regular expression matching a time stamp range.")
  3113. (defconst org-tr-regexp-both
  3114. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3115. "Regular expression matching a time stamp range.")
  3116. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3117. org-ts-regexp "\\)?")
  3118. "Regular expression matching a time stamp or time stamp range.")
  3119. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3120. org-ts-regexp-both "\\)?")
  3121. "Regular expression matching a time stamp or time stamp range.
  3122. The time stamps may be either active or inactive.")
  3123. (defvar org-emph-face nil)
  3124. (defun org-do-emphasis-faces (limit)
  3125. "Run through the buffer and add overlays to links."
  3126. (let (rtn)
  3127. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3128. (if (not (= (char-after (match-beginning 3))
  3129. (char-after (match-beginning 4))))
  3130. (progn
  3131. (setq rtn t)
  3132. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3133. 'face
  3134. (nth 1 (assoc (match-string 3)
  3135. org-emphasis-alist)))
  3136. (add-text-properties (match-beginning 2) (match-end 2)
  3137. '(font-lock-multiline t))
  3138. (when org-hide-emphasis-markers
  3139. (add-text-properties (match-end 4) (match-beginning 5)
  3140. '(invisible org-link))
  3141. (add-text-properties (match-beginning 3) (match-end 3)
  3142. '(invisible org-link)))))
  3143. (backward-char 1))
  3144. rtn))
  3145. (defun org-emphasize (&optional char)
  3146. "Insert or change an emphasis, i.e. a font like bold or italic.
  3147. If there is an active region, change that region to a new emphasis.
  3148. If there is no region, just insert the marker characters and position
  3149. the cursor between them.
  3150. CHAR should be either the marker character, or the first character of the
  3151. HTML tag associated with that emphasis. If CHAR is a space, the means
  3152. to remove the emphasis of the selected region.
  3153. If char is not given (for example in an interactive call) it
  3154. will be prompted for."
  3155. (interactive)
  3156. (let ((eal org-emphasis-alist) e det
  3157. (erc org-emphasis-regexp-components)
  3158. (prompt "")
  3159. (string "") beg end move tag c s)
  3160. (if (org-region-active-p)
  3161. (setq beg (region-beginning) end (region-end)
  3162. string (buffer-substring beg end))
  3163. (setq move t))
  3164. (while (setq e (pop eal))
  3165. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3166. c (aref tag 0))
  3167. (push (cons c (string-to-char (car e))) det)
  3168. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3169. (substring tag 1)))))
  3170. (unless char
  3171. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3172. (setq char (read-char-exclusive)))
  3173. (setq char (or (cdr (assoc char det)) char))
  3174. (if (equal char ?\ )
  3175. (setq s "" move nil)
  3176. (unless (assoc (char-to-string char) org-emphasis-alist)
  3177. (error "No such emphasis marker: \"%c\"" char))
  3178. (setq s (char-to-string char)))
  3179. (while (and (> (length string) 1)
  3180. (equal (substring string 0 1) (substring string -1))
  3181. (assoc (substring string 0 1) org-emphasis-alist))
  3182. (setq string (substring string 1 -1)))
  3183. (setq string (concat s string s))
  3184. (if beg (delete-region beg end))
  3185. (unless (or (bolp)
  3186. (string-match (concat "[" (nth 0 erc) "\n]")
  3187. (char-to-string (char-before (point)))))
  3188. (insert " "))
  3189. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3190. (char-to-string (char-after (point))))
  3191. (insert " ") (backward-char 1))
  3192. (insert string)
  3193. (and move (backward-char 1))))
  3194. (defconst org-nonsticky-props
  3195. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3196. (defun org-activate-plain-links (limit)
  3197. "Run through the buffer and add overlays to links."
  3198. (catch 'exit
  3199. (let (f)
  3200. (while (re-search-forward org-plain-link-re limit t)
  3201. (setq f (get-text-property (match-beginning 0) 'face))
  3202. (if (or (eq f 'org-tag)
  3203. (and (listp f) (memq 'org-tag f)))
  3204. nil
  3205. (add-text-properties (match-beginning 0) (match-end 0)
  3206. (list 'mouse-face 'highlight
  3207. 'rear-nonsticky org-nonsticky-props
  3208. 'keymap org-mouse-map
  3209. ))
  3210. (throw 'exit t))))))
  3211. (defun org-activate-code (limit)
  3212. (if (re-search-forward "^[ \t]*\\(:.*\\)" limit t)
  3213. (unless (get-text-property (match-beginning 1) 'face)
  3214. (remove-text-properties (match-beginning 0) (match-end 0)
  3215. '(display t invisible t intangible t))
  3216. t)))
  3217. (defun org-activate-angle-links (limit)
  3218. "Run through the buffer and add overlays to links."
  3219. (if (re-search-forward org-angle-link-re limit t)
  3220. (progn
  3221. (add-text-properties (match-beginning 0) (match-end 0)
  3222. (list 'mouse-face 'highlight
  3223. 'rear-nonsticky org-nonsticky-props
  3224. 'keymap org-mouse-map
  3225. ))
  3226. t)))
  3227. (defun org-activate-bracket-links (limit)
  3228. "Run through the buffer and add overlays to bracketed links."
  3229. (if (re-search-forward org-bracket-link-regexp limit t)
  3230. (let* ((help (concat "LINK: "
  3231. (org-match-string-no-properties 1)))
  3232. ;; FIXME: above we should remove the escapes.
  3233. ;; but that requires another match, protecting match data,
  3234. ;; a lot of overhead for font-lock.
  3235. (ip (org-maybe-intangible
  3236. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3237. 'keymap org-mouse-map 'mouse-face 'highlight
  3238. 'font-lock-multiline t 'help-echo help)))
  3239. (vp (list 'rear-nonsticky org-nonsticky-props
  3240. 'keymap org-mouse-map 'mouse-face 'highlight
  3241. ' font-lock-multiline t 'help-echo help)))
  3242. ;; We need to remove the invisible property here. Table narrowing
  3243. ;; may have made some of this invisible.
  3244. (remove-text-properties (match-beginning 0) (match-end 0)
  3245. '(invisible nil))
  3246. (if (match-end 3)
  3247. (progn
  3248. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3249. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3250. (add-text-properties (match-end 3) (match-end 0) ip))
  3251. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3252. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3253. (add-text-properties (match-end 1) (match-end 0) ip))
  3254. t)))
  3255. (defun org-activate-dates (limit)
  3256. "Run through the buffer and add overlays to dates."
  3257. (if (re-search-forward org-tsr-regexp-both limit t)
  3258. (progn
  3259. (add-text-properties (match-beginning 0) (match-end 0)
  3260. (list 'mouse-face 'highlight
  3261. 'rear-nonsticky org-nonsticky-props
  3262. 'keymap org-mouse-map))
  3263. (when org-display-custom-times
  3264. (if (match-end 3)
  3265. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3266. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3267. t)))
  3268. (defvar org-target-link-regexp nil
  3269. "Regular expression matching radio targets in plain text.")
  3270. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3271. "Regular expression matching a link target.")
  3272. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3273. "Regular expression matching a radio target.")
  3274. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3275. "Regular expression matching any target.")
  3276. (defun org-activate-target-links (limit)
  3277. "Run through the buffer and add overlays to target matches."
  3278. (when org-target-link-regexp
  3279. (let ((case-fold-search t))
  3280. (if (re-search-forward org-target-link-regexp limit t)
  3281. (progn
  3282. (add-text-properties (match-beginning 0) (match-end 0)
  3283. (list 'mouse-face 'highlight
  3284. 'rear-nonsticky org-nonsticky-props
  3285. 'keymap org-mouse-map
  3286. 'help-echo "Radio target link"
  3287. 'org-linked-text t))
  3288. t)))))
  3289. (defun org-update-radio-target-regexp ()
  3290. "Find all radio targets in this file and update the regular expression."
  3291. (interactive)
  3292. (when (memq 'radio org-activate-links)
  3293. (setq org-target-link-regexp
  3294. (org-make-target-link-regexp (org-all-targets 'radio)))
  3295. (org-restart-font-lock)))
  3296. (defun org-hide-wide-columns (limit)
  3297. (let (s e)
  3298. (setq s (text-property-any (point) (or limit (point-max))
  3299. 'org-cwidth t))
  3300. (when s
  3301. (setq e (next-single-property-change s 'org-cwidth))
  3302. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3303. (goto-char e)
  3304. t)))
  3305. (defvar org-latex-and-specials-regexp nil
  3306. "Regular expression for highlighting export special stuff.")
  3307. (defvar org-match-substring-regexp)
  3308. (defvar org-match-substring-with-braces-regexp)
  3309. (defvar org-export-html-special-string-regexps)
  3310. (defun org-compute-latex-and-specials-regexp ()
  3311. "Compute regular expression for stuff treated specially by exporters."
  3312. (if (not org-highlight-latex-fragments-and-specials)
  3313. (org-set-local 'org-latex-and-specials-regexp nil)
  3314. (require 'org-exp)
  3315. (let*
  3316. ((matchers (plist-get org-format-latex-options :matchers))
  3317. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3318. org-latex-regexps)))
  3319. (options (org-combine-plists (org-default-export-plist)
  3320. (org-infile-export-plist)))
  3321. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3322. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3323. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3324. (org-export-html-expand (plist-get options :expand-quoted-html))
  3325. (org-export-with-special-strings (plist-get options :special-strings))
  3326. (re-sub
  3327. (cond
  3328. ((equal org-export-with-sub-superscripts '{})
  3329. (list org-match-substring-with-braces-regexp))
  3330. (org-export-with-sub-superscripts
  3331. (list org-match-substring-regexp))
  3332. (t nil)))
  3333. (re-latex
  3334. (if org-export-with-LaTeX-fragments
  3335. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3336. (re-macros
  3337. (if org-export-with-TeX-macros
  3338. (list (concat "\\\\"
  3339. (regexp-opt
  3340. (append (mapcar 'car org-html-entities)
  3341. (if (boundp 'org-latex-entities)
  3342. org-latex-entities nil))
  3343. 'words))) ; FIXME
  3344. ))
  3345. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3346. (re-special (if org-export-with-special-strings
  3347. (mapcar (lambda (x) (car x))
  3348. org-export-html-special-string-regexps)))
  3349. (re-rest
  3350. (delq nil
  3351. (list
  3352. (if org-export-html-expand "@<[^>\n]+>")
  3353. ))))
  3354. (org-set-local
  3355. 'org-latex-and-specials-regexp
  3356. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3357. re-rest) "\\|")))))
  3358. (defface org-latex-and-export-specials
  3359. (let ((font (cond ((assq :inherit custom-face-attributes)
  3360. '(:inherit underline))
  3361. (t '(:underline t)))))
  3362. `((((class grayscale) (background light))
  3363. (:foreground "DimGray" ,@font))
  3364. (((class grayscale) (background dark))
  3365. (:foreground "LightGray" ,@font))
  3366. (((class color) (background light))
  3367. (:foreground "SaddleBrown"))
  3368. (((class color) (background dark))
  3369. (:foreground "burlywood"))
  3370. (t (,@font))))
  3371. "Face used to highlight math latex and other special exporter stuff."
  3372. :group 'org-faces)
  3373. (defun org-do-latex-and-special-faces (limit)
  3374. "Run through the buffer and add overlays to links."
  3375. (when org-latex-and-specials-regexp
  3376. (let (rtn d)
  3377. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3378. limit t))
  3379. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3380. 'face))
  3381. '(org-code org-verbatim underline)))
  3382. (progn
  3383. (setq rtn t
  3384. d (cond ((member (char-after (1+ (match-beginning 0)))
  3385. '(?_ ?^)) 1)
  3386. (t 0)))
  3387. (font-lock-prepend-text-property
  3388. (+ d (match-beginning 0)) (match-end 0)
  3389. 'face 'org-latex-and-export-specials)
  3390. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3391. '(font-lock-multiline t)))))
  3392. rtn)))
  3393. (defun org-restart-font-lock ()
  3394. "Restart font-lock-mode, to force refontification."
  3395. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3396. (font-lock-mode -1)
  3397. (font-lock-mode 1)))
  3398. (defun org-all-targets (&optional radio)
  3399. "Return a list of all targets in this file.
  3400. With optional argument RADIO, only find radio targets."
  3401. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3402. rtn)
  3403. (save-excursion
  3404. (goto-char (point-min))
  3405. (while (re-search-forward re nil t)
  3406. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3407. rtn)))
  3408. (defun org-make-target-link-regexp (targets)
  3409. "Make regular expression matching all strings in TARGETS.
  3410. The regular expression finds the targets also if there is a line break
  3411. between words."
  3412. (and targets
  3413. (concat
  3414. "\\<\\("
  3415. (mapconcat
  3416. (lambda (x)
  3417. (while (string-match " +" x)
  3418. (setq x (replace-match "\\s-+" t t x)))
  3419. x)
  3420. targets
  3421. "\\|")
  3422. "\\)\\>")))
  3423. (defun org-activate-tags (limit)
  3424. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3425. (progn
  3426. (add-text-properties (match-beginning 1) (match-end 1)
  3427. (list 'mouse-face 'highlight
  3428. 'rear-nonsticky org-nonsticky-props
  3429. 'keymap org-mouse-map))
  3430. t)))
  3431. (defun org-outline-level ()
  3432. (save-excursion
  3433. (looking-at outline-regexp)
  3434. (if (match-beginning 1)
  3435. (+ (org-get-string-indentation (match-string 1)) 1000)
  3436. (1- (- (match-end 0) (match-beginning 0))))))
  3437. (defvar org-font-lock-keywords nil)
  3438. (defconst org-property-re (org-re "^[ \t]*\\(:\\([[:alnum:]_]+\\):\\)[ \t]*\\(\\S-.*\\)")
  3439. "Regular expression matching a property line.")
  3440. (defun org-set-font-lock-defaults ()
  3441. (let* ((em org-fontify-emphasized-text)
  3442. (lk org-activate-links)
  3443. (org-font-lock-extra-keywords
  3444. (list
  3445. ;; Headlines
  3446. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3447. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3448. ;; Table lines
  3449. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3450. (1 'org-table t))
  3451. ;; Table internals
  3452. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3453. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3454. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3455. ;; Drawers
  3456. (list org-drawer-regexp '(0 'org-special-keyword t))
  3457. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3458. ;; Properties
  3459. (list org-property-re
  3460. '(1 'org-special-keyword t)
  3461. '(3 'org-property-value t))
  3462. (if org-format-transports-properties-p
  3463. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3464. ;; Links
  3465. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3466. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3467. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3468. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3469. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3470. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3471. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3472. '(org-hide-wide-columns (0 nil append))
  3473. ;; TODO lines
  3474. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3475. '(1 (org-get-todo-face 1) t))
  3476. ;; DONE
  3477. (if org-fontify-done-headline
  3478. (list (concat "^[*]+ +\\<\\("
  3479. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3480. "\\)\\(.*\\)")
  3481. '(2 'org-headline-done t))
  3482. nil)
  3483. ;; Priorities
  3484. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3485. ;; Special keywords
  3486. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3487. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3488. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3489. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3490. ;; Emphasis
  3491. (if em
  3492. (if (featurep 'xemacs)
  3493. '(org-do-emphasis-faces (0 nil append))
  3494. '(org-do-emphasis-faces)))
  3495. ;; Checkboxes
  3496. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3497. 2 'bold prepend)
  3498. (if org-provide-checkbox-statistics
  3499. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3500. (0 (org-get-checkbox-statistics-face) t)))
  3501. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3502. '(1 'org-archived prepend))
  3503. ;; Specials
  3504. '(org-do-latex-and-special-faces)
  3505. ;; Code
  3506. '(org-activate-code (1 'org-code t))
  3507. ;; COMMENT
  3508. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3509. "\\|" org-quote-string "\\)\\>")
  3510. '(1 'org-special-keyword t))
  3511. '("^#.*" (0 'font-lock-comment-face t))
  3512. )))
  3513. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3514. ;; Now set the full font-lock-keywords
  3515. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3516. (org-set-local 'font-lock-defaults
  3517. '(org-font-lock-keywords t nil nil backward-paragraph))
  3518. (kill-local-variable 'font-lock-keywords) nil))
  3519. (defvar org-m nil)
  3520. (defvar org-l nil)
  3521. (defvar org-f nil)
  3522. (defun org-get-level-face (n)
  3523. "Get the right face for match N in font-lock matching of healdines."
  3524. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3525. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3526. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3527. (cond
  3528. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3529. ((eq n 2) org-f)
  3530. (t (if org-level-color-stars-only nil org-f))))
  3531. (defun org-get-todo-face (kwd)
  3532. "Get the right face for a TODO keyword KWD.
  3533. If KWD is a number, get the corresponding match group."
  3534. (if (numberp kwd) (setq kwd (match-string kwd)))
  3535. (or (cdr (assoc kwd org-todo-keyword-faces))
  3536. (and (member kwd org-done-keywords) 'org-done)
  3537. 'org-todo))
  3538. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3539. "Remove fontification and activation overlays from links."
  3540. (font-lock-default-unfontify-region beg end)
  3541. (let* ((buffer-undo-list t)
  3542. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3543. (inhibit-modification-hooks t)
  3544. deactivate-mark buffer-file-name buffer-file-truename)
  3545. (remove-text-properties beg end
  3546. '(mouse-face t keymap t org-linked-text t
  3547. invisible t intangible t))))
  3548. ;;;; Visibility cycling, including org-goto and indirect buffer
  3549. ;;; Cycling
  3550. (defvar org-cycle-global-status nil)
  3551. (make-variable-buffer-local 'org-cycle-global-status)
  3552. (defvar org-cycle-subtree-status nil)
  3553. (make-variable-buffer-local 'org-cycle-subtree-status)
  3554. ;;;###autoload
  3555. (defun org-cycle (&optional arg)
  3556. "Visibility cycling for Org-mode.
  3557. - When this function is called with a prefix argument, rotate the entire
  3558. buffer through 3 states (global cycling)
  3559. 1. OVERVIEW: Show only top-level headlines.
  3560. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3561. 3. SHOW ALL: Show everything.
  3562. - When point is at the beginning of a headline, rotate the subtree started
  3563. by this line through 3 different states (local cycling)
  3564. 1. FOLDED: Only the main headline is shown.
  3565. 2. CHILDREN: The main headline and the direct children are shown.
  3566. From this state, you can move to one of the children
  3567. and zoom in further.
  3568. 3. SUBTREE: Show the entire subtree, including body text.
  3569. - When there is a numeric prefix, go up to a heading with level ARG, do
  3570. a `show-subtree' and return to the previous cursor position. If ARG
  3571. is negative, go up that many levels.
  3572. - When point is not at the beginning of a headline, execute
  3573. `indent-relative', like TAB normally does. See the option
  3574. `org-cycle-emulate-tab' for details.
  3575. - Special case: if point is at the beginning of the buffer and there is
  3576. no headline in line 1, this function will act as if called with prefix arg.
  3577. But only if also the variable `org-cycle-global-at-bob' is t."
  3578. (interactive "P")
  3579. (org-load-modules-maybe)
  3580. (let* ((outline-regexp
  3581. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3582. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3583. outline-regexp))
  3584. (bob-special (and org-cycle-global-at-bob (bobp)
  3585. (not (looking-at outline-regexp))))
  3586. (org-cycle-hook
  3587. (if bob-special
  3588. (delq 'org-optimize-window-after-visibility-change
  3589. (copy-sequence org-cycle-hook))
  3590. org-cycle-hook))
  3591. (pos (point)))
  3592. (if (or bob-special (equal arg '(4)))
  3593. ;; special case: use global cycling
  3594. (setq arg t))
  3595. (cond
  3596. ((org-at-table-p 'any)
  3597. ;; Enter the table or move to the next field in the table
  3598. (or (org-table-recognize-table.el)
  3599. (progn
  3600. (if arg (org-table-edit-field t)
  3601. (org-table-justify-field-maybe)
  3602. (call-interactively 'org-table-next-field)))))
  3603. ((eq arg t) ;; Global cycling
  3604. (cond
  3605. ((and (eq last-command this-command)
  3606. (eq org-cycle-global-status 'overview))
  3607. ;; We just created the overview - now do table of contents
  3608. ;; This can be slow in very large buffers, so indicate action
  3609. (message "CONTENTS...")
  3610. (org-content)
  3611. (message "CONTENTS...done")
  3612. (setq org-cycle-global-status 'contents)
  3613. (run-hook-with-args 'org-cycle-hook 'contents))
  3614. ((and (eq last-command this-command)
  3615. (eq org-cycle-global-status 'contents))
  3616. ;; We just showed the table of contents - now show everything
  3617. (show-all)
  3618. (message "SHOW ALL")
  3619. (setq org-cycle-global-status 'all)
  3620. (run-hook-with-args 'org-cycle-hook 'all))
  3621. (t
  3622. ;; Default action: go to overview
  3623. (org-overview)
  3624. (message "OVERVIEW")
  3625. (setq org-cycle-global-status 'overview)
  3626. (run-hook-with-args 'org-cycle-hook 'overview))))
  3627. ((and org-drawers org-drawer-regexp
  3628. (save-excursion
  3629. (beginning-of-line 1)
  3630. (looking-at org-drawer-regexp)))
  3631. ;; Toggle block visibility
  3632. (org-flag-drawer
  3633. (not (get-char-property (match-end 0) 'invisible))))
  3634. ((integerp arg)
  3635. ;; Show-subtree, ARG levels up from here.
  3636. (save-excursion
  3637. (org-back-to-heading)
  3638. (outline-up-heading (if (< arg 0) (- arg)
  3639. (- (funcall outline-level) arg)))
  3640. (org-show-subtree)))
  3641. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3642. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3643. ;; At a heading: rotate between three different views
  3644. (org-back-to-heading)
  3645. (let ((goal-column 0) eoh eol eos)
  3646. ;; First, some boundaries
  3647. (save-excursion
  3648. (org-back-to-heading)
  3649. (save-excursion
  3650. (beginning-of-line 2)
  3651. (while (and (not (eobp)) ;; this is like `next-line'
  3652. (get-char-property (1- (point)) 'invisible))
  3653. (beginning-of-line 2)) (setq eol (point)))
  3654. (outline-end-of-heading) (setq eoh (point))
  3655. (org-end-of-subtree t)
  3656. (unless (eobp)
  3657. (skip-chars-forward " \t\n")
  3658. (beginning-of-line 1) ; in case this is an item
  3659. )
  3660. (setq eos (1- (point))))
  3661. ;; Find out what to do next and set `this-command'
  3662. (cond
  3663. ((= eos eoh)
  3664. ;; Nothing is hidden behind this heading
  3665. (message "EMPTY ENTRY")
  3666. (setq org-cycle-subtree-status nil)
  3667. (save-excursion
  3668. (goto-char eos)
  3669. (outline-next-heading)
  3670. (if (org-invisible-p) (org-flag-heading nil))))
  3671. ((or (>= eol eos)
  3672. (not (string-match "\\S-" (buffer-substring eol eos))))
  3673. ;; Entire subtree is hidden in one line: open it
  3674. (org-show-entry)
  3675. (show-children)
  3676. (message "CHILDREN")
  3677. (save-excursion
  3678. (goto-char eos)
  3679. (outline-next-heading)
  3680. (if (org-invisible-p) (org-flag-heading nil)))
  3681. (setq org-cycle-subtree-status 'children)
  3682. (run-hook-with-args 'org-cycle-hook 'children))
  3683. ((and (eq last-command this-command)
  3684. (eq org-cycle-subtree-status 'children))
  3685. ;; We just showed the children, now show everything.
  3686. (org-show-subtree)
  3687. (message "SUBTREE")
  3688. (setq org-cycle-subtree-status 'subtree)
  3689. (run-hook-with-args 'org-cycle-hook 'subtree))
  3690. (t
  3691. ;; Default action: hide the subtree.
  3692. (hide-subtree)
  3693. (message "FOLDED")
  3694. (setq org-cycle-subtree-status 'folded)
  3695. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3696. ;; TAB emulation
  3697. (buffer-read-only (org-back-to-heading))
  3698. ((org-try-cdlatex-tab))
  3699. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  3700. (or (not (bolp))
  3701. (not (looking-at outline-regexp))))
  3702. (call-interactively (global-key-binding "\t")))
  3703. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3704. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3705. (or (and (eq org-cycle-emulate-tab 'white)
  3706. (= (match-end 0) (point-at-eol)))
  3707. (and (eq org-cycle-emulate-tab 'whitestart)
  3708. (>= (match-end 0) pos))))
  3709. t
  3710. (eq org-cycle-emulate-tab t))
  3711. ; (if (and (looking-at "[ \n\r\t]")
  3712. ; (string-match "^[ \t]*$" (buffer-substring
  3713. ; (point-at-bol) (point))))
  3714. ; (progn
  3715. ; (beginning-of-line 1)
  3716. ; (and (looking-at "[ \t]+") (replace-match ""))))
  3717. (call-interactively (global-key-binding "\t")))
  3718. (t (save-excursion
  3719. (org-back-to-heading)
  3720. (org-cycle))))))
  3721. ;;;###autoload
  3722. (defun org-global-cycle (&optional arg)
  3723. "Cycle the global visibility. For details see `org-cycle'."
  3724. (interactive "P")
  3725. (let ((org-cycle-include-plain-lists
  3726. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  3727. (if (integerp arg)
  3728. (progn
  3729. (show-all)
  3730. (hide-sublevels arg)
  3731. (setq org-cycle-global-status 'contents))
  3732. (org-cycle '(4)))))
  3733. (defun org-overview ()
  3734. "Switch to overview mode, shoing only top-level headlines.
  3735. Really, this shows all headlines with level equal or greater than the level
  3736. of the first headline in the buffer. This is important, because if the
  3737. first headline is not level one, then (hide-sublevels 1) gives confusing
  3738. results."
  3739. (interactive)
  3740. (let ((level (save-excursion
  3741. (goto-char (point-min))
  3742. (if (re-search-forward (concat "^" outline-regexp) nil t)
  3743. (progn
  3744. (goto-char (match-beginning 0))
  3745. (funcall outline-level))))))
  3746. (and level (hide-sublevels level))))
  3747. (defun org-content (&optional arg)
  3748. "Show all headlines in the buffer, like a table of contents.
  3749. With numerical argument N, show content up to level N."
  3750. (interactive "P")
  3751. (save-excursion
  3752. ;; Visit all headings and show their offspring
  3753. (and (integerp arg) (org-overview))
  3754. (goto-char (point-max))
  3755. (catch 'exit
  3756. (while (and (progn (condition-case nil
  3757. (outline-previous-visible-heading 1)
  3758. (error (goto-char (point-min))))
  3759. t)
  3760. (looking-at outline-regexp))
  3761. (if (integerp arg)
  3762. (show-children (1- arg))
  3763. (show-branches))
  3764. (if (bobp) (throw 'exit nil))))))
  3765. (defun org-optimize-window-after-visibility-change (state)
  3766. "Adjust the window after a change in outline visibility.
  3767. This function is the default value of the hook `org-cycle-hook'."
  3768. (when (get-buffer-window (current-buffer))
  3769. (cond
  3770. ; ((eq state 'overview) (org-first-headline-recenter 1))
  3771. ; ((eq state 'overview) (org-beginning-of-line))
  3772. ((eq state 'content) nil)
  3773. ((eq state 'all) nil)
  3774. ((eq state 'folded) nil)
  3775. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  3776. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  3777. (defun org-compact-display-after-subtree-move ()
  3778. (let (beg end)
  3779. (save-excursion
  3780. (if (org-up-heading-safe)
  3781. (progn
  3782. (hide-subtree)
  3783. (show-entry)
  3784. (show-children)
  3785. (org-cycle-show-empty-lines 'children)
  3786. (org-cycle-hide-drawers 'children))
  3787. (org-overview)))))
  3788. (defun org-cycle-show-empty-lines (state)
  3789. "Show empty lines above all visible headlines.
  3790. The region to be covered depends on STATE when called through
  3791. `org-cycle-hook'. Lisp program can use t for STATE to get the
  3792. entire buffer covered. Note that an empty line is only shown if there
  3793. are at least `org-cycle-separator-lines' empty lines before the headeline."
  3794. (when (> org-cycle-separator-lines 0)
  3795. (save-excursion
  3796. (let* ((n org-cycle-separator-lines)
  3797. (re (cond
  3798. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  3799. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  3800. (t (let ((ns (number-to-string (- n 2))))
  3801. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  3802. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  3803. beg end)
  3804. (cond
  3805. ((memq state '(overview contents t))
  3806. (setq beg (point-min) end (point-max)))
  3807. ((memq state '(children folded))
  3808. (setq beg (point) end (progn (org-end-of-subtree t t)
  3809. (beginning-of-line 2)
  3810. (point)))))
  3811. (when beg
  3812. (goto-char beg)
  3813. (while (re-search-forward re end t)
  3814. (if (not (get-char-property (match-end 1) 'invisible))
  3815. (outline-flag-region
  3816. (match-beginning 1) (match-end 1) nil)))))))
  3817. ;; Never hide empty lines at the end of the file.
  3818. (save-excursion
  3819. (goto-char (point-max))
  3820. (outline-previous-heading)
  3821. (outline-end-of-heading)
  3822. (if (and (looking-at "[ \t\n]+")
  3823. (= (match-end 0) (point-max)))
  3824. (outline-flag-region (point) (match-end 0) nil))))
  3825. (defun org-subtree-end-visible-p ()
  3826. "Is the end of the current subtree visible?"
  3827. (pos-visible-in-window-p
  3828. (save-excursion (org-end-of-subtree t) (point))))
  3829. (defun org-first-headline-recenter (&optional N)
  3830. "Move cursor to the first headline and recenter the headline.
  3831. Optional argument N means, put the headline into the Nth line of the window."
  3832. (goto-char (point-min))
  3833. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  3834. (beginning-of-line)
  3835. (recenter (prefix-numeric-value N))))
  3836. ;;; Org-goto
  3837. (defvar org-goto-window-configuration nil)
  3838. (defvar org-goto-marker nil)
  3839. (defvar org-goto-map
  3840. (let ((map (make-sparse-keymap)))
  3841. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  3842. (while (setq cmd (pop cmds))
  3843. (substitute-key-definition cmd cmd map global-map)))
  3844. (suppress-keymap map)
  3845. (org-defkey map "\C-m" 'org-goto-ret)
  3846. (org-defkey map [(return)] 'org-goto-ret)
  3847. (org-defkey map [(left)] 'org-goto-left)
  3848. (org-defkey map [(right)] 'org-goto-right)
  3849. (org-defkey map [(control ?g)] 'org-goto-quit)
  3850. (org-defkey map "\C-i" 'org-cycle)
  3851. (org-defkey map [(tab)] 'org-cycle)
  3852. (org-defkey map [(down)] 'outline-next-visible-heading)
  3853. (org-defkey map [(up)] 'outline-previous-visible-heading)
  3854. (if org-goto-auto-isearch
  3855. (if (fboundp 'define-key-after)
  3856. (define-key-after map [t] 'org-goto-local-auto-isearch)
  3857. nil)
  3858. (org-defkey map "q" 'org-goto-quit)
  3859. (org-defkey map "n" 'outline-next-visible-heading)
  3860. (org-defkey map "p" 'outline-previous-visible-heading)
  3861. (org-defkey map "f" 'outline-forward-same-level)
  3862. (org-defkey map "b" 'outline-backward-same-level)
  3863. (org-defkey map "u" 'outline-up-heading))
  3864. (org-defkey map "/" 'org-occur)
  3865. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  3866. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  3867. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  3868. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  3869. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  3870. map))
  3871. (defconst org-goto-help
  3872. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  3873. RET=jump to location [Q]uit and return to previous location
  3874. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  3875. (defvar org-goto-start-pos) ; dynamically scoped parameter
  3876. (defun org-goto (&optional alternative-interface)
  3877. "Look up a different location in the current file, keeping current visibility.
  3878. When you want look-up or go to a different location in a document, the
  3879. fastest way is often to fold the entire buffer and then dive into the tree.
  3880. This method has the disadvantage, that the previous location will be folded,
  3881. which may not be what you want.
  3882. This command works around this by showing a copy of the current buffer
  3883. in an indirect buffer, in overview mode. You can dive into the tree in
  3884. that copy, use org-occur and incremental search to find a location.
  3885. When pressing RET or `Q', the command returns to the original buffer in
  3886. which the visibility is still unchanged. After RET is will also jump to
  3887. the location selected in the indirect buffer and expose the
  3888. the headline hierarchy above."
  3889. (interactive "P")
  3890. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  3891. (org-refile-use-outline-path t)
  3892. (interface
  3893. (if (not alternative-interface)
  3894. org-goto-interface
  3895. (if (eq org-goto-interface 'outline)
  3896. 'outline-path-completion
  3897. 'outline)))
  3898. (org-goto-start-pos (point))
  3899. (selected-point
  3900. (if (eq interface 'outline)
  3901. (car (org-get-location (current-buffer) org-goto-help))
  3902. (nth 3 (org-refile-get-location "Goto: ")))))
  3903. (if selected-point
  3904. (progn
  3905. (org-mark-ring-push org-goto-start-pos)
  3906. (goto-char selected-point)
  3907. (if (or (org-invisible-p) (org-invisible-p2))
  3908. (org-show-context 'org-goto)))
  3909. (message "Quit"))))
  3910. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  3911. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  3912. (defvar org-goto-local-auto-isearch-map) ; defined below
  3913. (defun org-get-location (buf help)
  3914. "Let the user select a location in the Org-mode buffer BUF.
  3915. This function uses a recursive edit. It returns the selected position
  3916. or nil."
  3917. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  3918. (isearch-hide-immediately nil)
  3919. (isearch-search-fun-function
  3920. (lambda () 'org-goto-local-search-forward-headings))
  3921. (org-goto-selected-point org-goto-exit-command))
  3922. (save-excursion
  3923. (save-window-excursion
  3924. (delete-other-windows)
  3925. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  3926. (switch-to-buffer
  3927. (condition-case nil
  3928. (make-indirect-buffer (current-buffer) "*org-goto*")
  3929. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  3930. (with-output-to-temp-buffer "*Help*"
  3931. (princ help))
  3932. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  3933. (setq buffer-read-only nil)
  3934. (let ((org-startup-truncated t)
  3935. (org-startup-folded nil)
  3936. (org-startup-align-all-tables nil))
  3937. (org-mode)
  3938. (org-overview))
  3939. (setq buffer-read-only t)
  3940. (if (and (boundp 'org-goto-start-pos)
  3941. (integer-or-marker-p org-goto-start-pos))
  3942. (let ((org-show-hierarchy-above t)
  3943. (org-show-siblings t)
  3944. (org-show-following-heading t))
  3945. (goto-char org-goto-start-pos)
  3946. (and (org-invisible-p) (org-show-context)))
  3947. (goto-char (point-min)))
  3948. (org-beginning-of-line)
  3949. (message "Select location and press RET")
  3950. (use-local-map org-goto-map)
  3951. (recursive-edit)
  3952. ))
  3953. (kill-buffer "*org-goto*")
  3954. (cons org-goto-selected-point org-goto-exit-command)))
  3955. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  3956. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  3957. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  3958. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  3959. (defun org-goto-local-search-forward-headings (string bound noerror)
  3960. "Search and make sure that anu matches are in headlines."
  3961. (catch 'return
  3962. (while (search-forward string bound noerror)
  3963. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  3964. (and (member :headline context)
  3965. (not (member :tags context))))
  3966. (throw 'return (point))))))
  3967. (defun org-goto-local-auto-isearch ()
  3968. "Start isearch."
  3969. (interactive)
  3970. (goto-char (point-min))
  3971. (let ((keys (this-command-keys)))
  3972. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  3973. (isearch-mode t)
  3974. (isearch-process-search-char (string-to-char keys)))))
  3975. (defun org-goto-ret (&optional arg)
  3976. "Finish `org-goto' by going to the new location."
  3977. (interactive "P")
  3978. (setq org-goto-selected-point (point)
  3979. org-goto-exit-command 'return)
  3980. (throw 'exit nil))
  3981. (defun org-goto-left ()
  3982. "Finish `org-goto' by going to the new location."
  3983. (interactive)
  3984. (if (org-on-heading-p)
  3985. (progn
  3986. (beginning-of-line 1)
  3987. (setq org-goto-selected-point (point)
  3988. org-goto-exit-command 'left)
  3989. (throw 'exit nil))
  3990. (error "Not on a heading")))
  3991. (defun org-goto-right ()
  3992. "Finish `org-goto' by going to the new location."
  3993. (interactive)
  3994. (if (org-on-heading-p)
  3995. (progn
  3996. (setq org-goto-selected-point (point)
  3997. org-goto-exit-command 'right)
  3998. (throw 'exit nil))
  3999. (error "Not on a heading")))
  4000. (defun org-goto-quit ()
  4001. "Finish `org-goto' without cursor motion."
  4002. (interactive)
  4003. (setq org-goto-selected-point nil)
  4004. (setq org-goto-exit-command 'quit)
  4005. (throw 'exit nil))
  4006. ;;; Indirect buffer display of subtrees
  4007. (defvar org-indirect-dedicated-frame nil
  4008. "This is the frame being used for indirect tree display.")
  4009. (defvar org-last-indirect-buffer nil)
  4010. (defun org-tree-to-indirect-buffer (&optional arg)
  4011. "Create indirect buffer and narrow it to current subtree.
  4012. With numerical prefix ARG, go up to this level and then take that tree.
  4013. If ARG is negative, go up that many levels.
  4014. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4015. indirect buffer previously made with this command, to avoid proliferation of
  4016. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4017. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4018. is kept so that you can work with several indirect buffers at the same time.
  4019. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4020. requests that a new frame be made for the new buffer, so that the dedicated
  4021. frame is not changed."
  4022. (interactive "P")
  4023. (let ((cbuf (current-buffer))
  4024. (cwin (selected-window))
  4025. (pos (point))
  4026. beg end level heading ibuf)
  4027. (save-excursion
  4028. (org-back-to-heading t)
  4029. (when (numberp arg)
  4030. (setq level (org-outline-level))
  4031. (if (< arg 0) (setq arg (+ level arg)))
  4032. (while (> (setq level (org-outline-level)) arg)
  4033. (outline-up-heading 1 t)))
  4034. (setq beg (point)
  4035. heading (org-get-heading))
  4036. (org-end-of-subtree t) (setq end (point)))
  4037. (if (and (buffer-live-p org-last-indirect-buffer)
  4038. (not (eq org-indirect-buffer-display 'new-frame))
  4039. (not arg))
  4040. (kill-buffer org-last-indirect-buffer))
  4041. (setq ibuf (org-get-indirect-buffer cbuf)
  4042. org-last-indirect-buffer ibuf)
  4043. (cond
  4044. ((or (eq org-indirect-buffer-display 'new-frame)
  4045. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4046. (select-frame (make-frame))
  4047. (delete-other-windows)
  4048. (switch-to-buffer ibuf)
  4049. (org-set-frame-title heading))
  4050. ((eq org-indirect-buffer-display 'dedicated-frame)
  4051. (raise-frame
  4052. (select-frame (or (and org-indirect-dedicated-frame
  4053. (frame-live-p org-indirect-dedicated-frame)
  4054. org-indirect-dedicated-frame)
  4055. (setq org-indirect-dedicated-frame (make-frame)))))
  4056. (delete-other-windows)
  4057. (switch-to-buffer ibuf)
  4058. (org-set-frame-title (concat "Indirect: " heading)))
  4059. ((eq org-indirect-buffer-display 'current-window)
  4060. (switch-to-buffer ibuf))
  4061. ((eq org-indirect-buffer-display 'other-window)
  4062. (pop-to-buffer ibuf))
  4063. (t (error "Invalid value.")))
  4064. (if (featurep 'xemacs)
  4065. (save-excursion (org-mode) (turn-on-font-lock)))
  4066. (narrow-to-region beg end)
  4067. (show-all)
  4068. (goto-char pos)
  4069. (and (window-live-p cwin) (select-window cwin))))
  4070. (defun org-get-indirect-buffer (&optional buffer)
  4071. (setq buffer (or buffer (current-buffer)))
  4072. (let ((n 1) (base (buffer-name buffer)) bname)
  4073. (while (buffer-live-p
  4074. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4075. (setq n (1+ n)))
  4076. (condition-case nil
  4077. (make-indirect-buffer buffer bname 'clone)
  4078. (error (make-indirect-buffer buffer bname)))))
  4079. (defun org-set-frame-title (title)
  4080. "Set the title of the current frame to the string TITLE."
  4081. ;; FIXME: how to name a single frame in XEmacs???
  4082. (unless (featurep 'xemacs)
  4083. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4084. ;;;; Structure editing
  4085. ;;; Inserting headlines
  4086. (defun org-insert-heading (&optional force-heading)
  4087. "Insert a new heading or item with same depth at point.
  4088. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4089. If point is at the beginning of a headline, insert a sibling before the
  4090. current headline. If point is not at the beginning, do not split the line,
  4091. but create the new hedline after the current line."
  4092. (interactive "P")
  4093. (if (= (buffer-size) 0)
  4094. (insert "\n* ")
  4095. (when (or force-heading (not (org-insert-item)))
  4096. (let* ((head (save-excursion
  4097. (condition-case nil
  4098. (progn
  4099. (org-back-to-heading)
  4100. (match-string 0))
  4101. (error "*"))))
  4102. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  4103. pos)
  4104. (cond
  4105. ((and (org-on-heading-p) (bolp)
  4106. (or (bobp)
  4107. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4108. ;; insert before the current line
  4109. (open-line (if blank 2 1)))
  4110. ((and (bolp)
  4111. (or (bobp)
  4112. (save-excursion
  4113. (backward-char 1) (not (org-invisible-p)))))
  4114. ;; insert right here
  4115. nil)
  4116. (t
  4117. ; ;; in the middle of the line
  4118. ; (org-show-entry)
  4119. ; (if (org-get-alist-option org-M-RET-may-split-line 'headline)
  4120. ; (if (and
  4121. ; (org-on-heading-p)
  4122. ; (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \r\n]"))
  4123. ; ;; protect the tags
  4124. ;; (let ((tags (match-string 2)) pos)
  4125. ; (delete-region (match-beginning 1) (match-end 1))
  4126. ; (setq pos (point-at-bol))
  4127. ; (newline (if blank 2 1))
  4128. ; (save-excursion
  4129. ; (goto-char pos)
  4130. ; (end-of-line 1)
  4131. ; (insert " " tags)
  4132. ; (org-set-tags nil 'align)))
  4133. ; (newline (if blank 2 1)))
  4134. ; (newline (if blank 2 1))))
  4135. ;; in the middle of the line
  4136. (org-show-entry)
  4137. (let ((split
  4138. (org-get-alist-option org-M-RET-may-split-line 'headline))
  4139. tags pos)
  4140. (if (org-on-heading-p)
  4141. (progn
  4142. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4143. (setq tags (and (match-end 2) (match-string 2)))
  4144. (and (match-end 1)
  4145. (delete-region (match-beginning 1) (match-end 1)))
  4146. (setq pos (point-at-bol))
  4147. (or split (end-of-line 1))
  4148. (delete-horizontal-space)
  4149. (newline (if blank 2 1))
  4150. (when tags
  4151. (save-excursion
  4152. (goto-char pos)
  4153. (end-of-line 1)
  4154. (insert " " tags)
  4155. (org-set-tags nil 'align))))
  4156. (or split (end-of-line 1))
  4157. (newline (if blank 2 1))))))
  4158. (insert head) (just-one-space)
  4159. (setq pos (point))
  4160. (end-of-line 1)
  4161. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4162. (run-hooks 'org-insert-heading-hook)))))
  4163. (defun org-insert-heading-after-current ()
  4164. "Insert a new heading with same level as current, after current subtree."
  4165. (interactive)
  4166. (org-back-to-heading)
  4167. (org-insert-heading)
  4168. (org-move-subtree-down)
  4169. (end-of-line 1))
  4170. (defun org-insert-todo-heading (arg)
  4171. "Insert a new heading with the same level and TODO state as current heading.
  4172. If the heading has no TODO state, or if the state is DONE, use the first
  4173. state (TODO by default). Also with prefix arg, force first state."
  4174. (interactive "P")
  4175. (when (not (org-insert-item 'checkbox))
  4176. (org-insert-heading)
  4177. (save-excursion
  4178. (org-back-to-heading)
  4179. (outline-previous-heading)
  4180. (looking-at org-todo-line-regexp))
  4181. (if (or arg
  4182. (not (match-beginning 2))
  4183. (member (match-string 2) org-done-keywords))
  4184. (insert (car org-todo-keywords-1) " ")
  4185. (insert (match-string 2) " "))))
  4186. (defun org-insert-subheading (arg)
  4187. "Insert a new subheading and demote it.
  4188. Works for outline headings and for plain lists alike."
  4189. (interactive "P")
  4190. (org-insert-heading arg)
  4191. (cond
  4192. ((org-on-heading-p) (org-do-demote))
  4193. ((org-at-item-p) (org-indent-item 1))))
  4194. (defun org-insert-todo-subheading (arg)
  4195. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4196. Works for outline headings and for plain lists alike."
  4197. (interactive "P")
  4198. (org-insert-todo-heading arg)
  4199. (cond
  4200. ((org-on-heading-p) (org-do-demote))
  4201. ((org-at-item-p) (org-indent-item 1))))
  4202. ;;; Promotion and Demotion
  4203. (defun org-promote-subtree ()
  4204. "Promote the entire subtree.
  4205. See also `org-promote'."
  4206. (interactive)
  4207. (save-excursion
  4208. (org-map-tree 'org-promote))
  4209. (org-fix-position-after-promote))
  4210. (defun org-demote-subtree ()
  4211. "Demote the entire subtree. See `org-demote'.
  4212. See also `org-promote'."
  4213. (interactive)
  4214. (save-excursion
  4215. (org-map-tree 'org-demote))
  4216. (org-fix-position-after-promote))
  4217. (defun org-do-promote ()
  4218. "Promote the current heading higher up the tree.
  4219. If the region is active in `transient-mark-mode', promote all headings
  4220. in the region."
  4221. (interactive)
  4222. (save-excursion
  4223. (if (org-region-active-p)
  4224. (org-map-region 'org-promote (region-beginning) (region-end))
  4225. (org-promote)))
  4226. (org-fix-position-after-promote))
  4227. (defun org-do-demote ()
  4228. "Demote the current heading lower down the tree.
  4229. If the region is active in `transient-mark-mode', demote all headings
  4230. in the region."
  4231. (interactive)
  4232. (save-excursion
  4233. (if (org-region-active-p)
  4234. (org-map-region 'org-demote (region-beginning) (region-end))
  4235. (org-demote)))
  4236. (org-fix-position-after-promote))
  4237. (defun org-fix-position-after-promote ()
  4238. "Make sure that after pro/demotion cursor position is right."
  4239. (let ((pos (point)))
  4240. (when (save-excursion
  4241. (beginning-of-line 1)
  4242. (looking-at org-todo-line-regexp)
  4243. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4244. (cond ((eobp) (insert " "))
  4245. ((eolp) (insert " "))
  4246. ((equal (char-after) ?\ ) (forward-char 1))))))
  4247. (defun org-reduced-level (l)
  4248. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4249. (defun org-get-valid-level (level &optional change)
  4250. "Rectify a level change under the influence of `org-odd-levels-only'
  4251. LEVEL is a current level, CHANGE is by how much the level should be
  4252. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4253. even level numbers will become the next higher odd number."
  4254. (if org-odd-levels-only
  4255. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4256. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4257. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4258. (max 1 (+ level change))))
  4259. (if (featurep 'xemacs)
  4260. (define-obsolete-function-alias 'org-get-legal-level
  4261. 'org-get-valid-level)
  4262. (define-obsolete-function-alias 'org-get-legal-level
  4263. 'org-get-valid-level "23.1"))
  4264. (defun org-promote ()
  4265. "Promote the current heading higher up the tree.
  4266. If the region is active in `transient-mark-mode', promote all headings
  4267. in the region."
  4268. (org-back-to-heading t)
  4269. (let* ((level (save-match-data (funcall outline-level)))
  4270. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4271. (diff (abs (- level (length up-head) -1))))
  4272. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4273. (replace-match up-head nil t)
  4274. ;; Fixup tag positioning
  4275. (and org-auto-align-tags (org-set-tags nil t))
  4276. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4277. (defun org-demote ()
  4278. "Demote the current heading lower down the tree.
  4279. If the region is active in `transient-mark-mode', demote all headings
  4280. in the region."
  4281. (org-back-to-heading t)
  4282. (let* ((level (save-match-data (funcall outline-level)))
  4283. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4284. (diff (abs (- level (length down-head) -1))))
  4285. (replace-match down-head nil t)
  4286. ;; Fixup tag positioning
  4287. (and org-auto-align-tags (org-set-tags nil t))
  4288. (if org-adapt-indentation (org-fixup-indentation diff))))
  4289. (defun org-map-tree (fun)
  4290. "Call FUN for every heading underneath the current one."
  4291. (org-back-to-heading)
  4292. (let ((level (funcall outline-level)))
  4293. (save-excursion
  4294. (funcall fun)
  4295. (while (and (progn
  4296. (outline-next-heading)
  4297. (> (funcall outline-level) level))
  4298. (not (eobp)))
  4299. (funcall fun)))))
  4300. (defun org-map-region (fun beg end)
  4301. "Call FUN for every heading between BEG and END."
  4302. (let ((org-ignore-region t))
  4303. (save-excursion
  4304. (setq end (copy-marker end))
  4305. (goto-char beg)
  4306. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4307. (< (point) end))
  4308. (funcall fun))
  4309. (while (and (progn
  4310. (outline-next-heading)
  4311. (< (point) end))
  4312. (not (eobp)))
  4313. (funcall fun)))))
  4314. (defun org-fixup-indentation (diff)
  4315. "Change the indentation in the current entry by DIFF
  4316. However, if any line in the current entry has no indentation, or if it
  4317. would end up with no indentation after the change, nothing at all is done."
  4318. (save-excursion
  4319. (let ((end (save-excursion (outline-next-heading)
  4320. (point-marker)))
  4321. (prohibit (if (> diff 0)
  4322. "^\\S-"
  4323. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4324. col)
  4325. (unless (save-excursion (end-of-line 1)
  4326. (re-search-forward prohibit end t))
  4327. (while (and (< (point) end)
  4328. (re-search-forward "^[ \t]+" end t))
  4329. (goto-char (match-end 0))
  4330. (setq col (current-column))
  4331. (if (< diff 0) (replace-match ""))
  4332. (indent-to (+ diff col))))
  4333. (move-marker end nil))))
  4334. (defun org-convert-to-odd-levels ()
  4335. "Convert an org-mode file with all levels allowed to one with odd levels.
  4336. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4337. level 5 etc."
  4338. (interactive)
  4339. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4340. (let ((org-odd-levels-only nil) n)
  4341. (save-excursion
  4342. (goto-char (point-min))
  4343. (while (re-search-forward "^\\*\\*+ " nil t)
  4344. (setq n (- (length (match-string 0)) 2))
  4345. (while (>= (setq n (1- n)) 0)
  4346. (org-demote))
  4347. (end-of-line 1))))))
  4348. (defun org-convert-to-oddeven-levels ()
  4349. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4350. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4351. section with an even level, conversion would destroy the structure of the file. An error
  4352. is signaled in this case."
  4353. (interactive)
  4354. (goto-char (point-min))
  4355. ;; First check if there are no even levels
  4356. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4357. (org-show-context t)
  4358. (error "Not all levels are odd in this file. Conversion not possible."))
  4359. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4360. (let ((org-odd-levels-only nil) n)
  4361. (save-excursion
  4362. (goto-char (point-min))
  4363. (while (re-search-forward "^\\*\\*+ " nil t)
  4364. (setq n (/ (1- (length (match-string 0))) 2))
  4365. (while (>= (setq n (1- n)) 0)
  4366. (org-promote))
  4367. (end-of-line 1))))))
  4368. (defun org-tr-level (n)
  4369. "Make N odd if required."
  4370. (if org-odd-levels-only (1+ (/ n 2)) n))
  4371. ;;; Vertical tree motion, cutting and pasting of subtrees
  4372. (defun org-move-subtree-up (&optional arg)
  4373. "Move the current subtree up past ARG headlines of the same level."
  4374. (interactive "p")
  4375. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4376. (defun org-move-subtree-down (&optional arg)
  4377. "Move the current subtree down past ARG headlines of the same level."
  4378. (interactive "p")
  4379. (setq arg (prefix-numeric-value arg))
  4380. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4381. 'outline-get-last-sibling))
  4382. (ins-point (make-marker))
  4383. (cnt (abs arg))
  4384. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4385. ;; Select the tree
  4386. (org-back-to-heading)
  4387. (setq beg0 (point))
  4388. (save-excursion
  4389. (setq ne-beg (org-back-over-empty-lines))
  4390. (setq beg (point)))
  4391. (save-match-data
  4392. (save-excursion (outline-end-of-heading)
  4393. (setq folded (org-invisible-p)))
  4394. (outline-end-of-subtree))
  4395. (outline-next-heading)
  4396. (setq ne-end (org-back-over-empty-lines))
  4397. (setq end (point))
  4398. (goto-char beg0)
  4399. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4400. ;; include less whitespace
  4401. (save-excursion
  4402. (goto-char beg)
  4403. (forward-line (- ne-beg ne-end))
  4404. (setq beg (point))))
  4405. ;; Find insertion point, with error handling
  4406. (while (> cnt 0)
  4407. (or (and (funcall movfunc) (looking-at outline-regexp))
  4408. (progn (goto-char beg0)
  4409. (error "Cannot move past superior level or buffer limit")))
  4410. (setq cnt (1- cnt)))
  4411. (if (> arg 0)
  4412. ;; Moving forward - still need to move over subtree
  4413. (progn (org-end-of-subtree t t)
  4414. (save-excursion
  4415. (org-back-over-empty-lines)
  4416. (or (bolp) (newline)))))
  4417. (setq ne-ins (org-back-over-empty-lines))
  4418. (move-marker ins-point (point))
  4419. (setq txt (buffer-substring beg end))
  4420. (delete-region beg end)
  4421. (outline-flag-region (1- beg) beg nil)
  4422. (outline-flag-region (1- (point)) (point) nil)
  4423. (insert txt)
  4424. (or (bolp) (insert "\n"))
  4425. (setq ins-end (point))
  4426. (goto-char ins-point)
  4427. (org-skip-whitespace)
  4428. (when (and (< arg 0)
  4429. (org-first-sibling-p)
  4430. (> ne-ins ne-beg))
  4431. ;; Move whitespace back to beginning
  4432. (save-excursion
  4433. (goto-char ins-end)
  4434. (let ((kill-whole-line t))
  4435. (kill-line (- ne-ins ne-beg)) (point)))
  4436. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4437. (move-marker ins-point nil)
  4438. (org-compact-display-after-subtree-move)
  4439. (unless folded
  4440. (org-show-entry)
  4441. (show-children)
  4442. (org-cycle-hide-drawers 'children))))
  4443. (defvar org-subtree-clip ""
  4444. "Clipboard for cut and paste of subtrees.
  4445. This is actually only a copy of the kill, because we use the normal kill
  4446. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4447. (defvar org-subtree-clip-folded nil
  4448. "Was the last copied subtree folded?
  4449. This is used to fold the tree back after pasting.")
  4450. (defun org-cut-subtree (&optional n)
  4451. "Cut the current subtree into the clipboard.
  4452. With prefix arg N, cut this many sequential subtrees.
  4453. This is a short-hand for marking the subtree and then cutting it."
  4454. (interactive "p")
  4455. (org-copy-subtree n 'cut))
  4456. (defun org-copy-subtree (&optional n cut)
  4457. "Cut the current subtree into the clipboard.
  4458. With prefix arg N, cut this many sequential subtrees.
  4459. This is a short-hand for marking the subtree and then copying it.
  4460. If CUT is non-nil, actually cut the subtree."
  4461. (interactive "p")
  4462. (let (beg end folded (beg0 (point)))
  4463. (if (interactive-p)
  4464. (org-back-to-heading nil) ; take what looks like a subtree
  4465. (org-back-to-heading t)) ; take what is really there
  4466. (org-back-over-empty-lines)
  4467. (setq beg (point))
  4468. (skip-chars-forward " \t\r\n")
  4469. (save-match-data
  4470. (save-excursion (outline-end-of-heading)
  4471. (setq folded (org-invisible-p)))
  4472. (condition-case nil
  4473. (outline-forward-same-level (1- n))
  4474. (error nil))
  4475. (org-end-of-subtree t t))
  4476. (org-back-over-empty-lines)
  4477. (setq end (point))
  4478. (goto-char beg0)
  4479. (when (> end beg)
  4480. (setq org-subtree-clip-folded folded)
  4481. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4482. (setq org-subtree-clip (current-kill 0))
  4483. (message "%s: Subtree(s) with %d characters"
  4484. (if cut "Cut" "Copied")
  4485. (length org-subtree-clip)))))
  4486. (defun org-paste-subtree (&optional level tree)
  4487. "Paste the clipboard as a subtree, with modification of headline level.
  4488. The entire subtree is promoted or demoted in order to match a new headline
  4489. level. By default, the new level is derived from the visible headings
  4490. before and after the insertion point, and taken to be the inferior headline
  4491. level of the two. So if the previous visible heading is level 3 and the
  4492. next is level 4 (or vice versa), level 4 will be used for insertion.
  4493. This makes sure that the subtree remains an independent subtree and does
  4494. not swallow low level entries.
  4495. You can also force a different level, either by using a numeric prefix
  4496. argument, or by inserting the heading marker by hand. For example, if the
  4497. cursor is after \"*****\", then the tree will be shifted to level 5.
  4498. If you want to insert the tree as is, just use \\[yank].
  4499. If optional TREE is given, use this text instead of the kill ring."
  4500. (interactive "P")
  4501. (unless (org-kill-is-subtree-p tree)
  4502. (error "%s"
  4503. (substitute-command-keys
  4504. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4505. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  4506. (^re (concat "^\\(" outline-regexp "\\)"))
  4507. (re (concat "\\(" outline-regexp "\\)"))
  4508. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  4509. (old-level (if (string-match ^re txt)
  4510. (- (match-end 0) (match-beginning 0) 1)
  4511. -1))
  4512. (force-level (cond (level (prefix-numeric-value level))
  4513. ((string-match
  4514. ^re_ (buffer-substring (point-at-bol) (point)))
  4515. (- (match-end 1) (match-beginning 1)))
  4516. (t nil)))
  4517. (previous-level (save-excursion
  4518. (condition-case nil
  4519. (progn
  4520. (outline-previous-visible-heading 1)
  4521. (if (looking-at re)
  4522. (- (match-end 0) (match-beginning 0) 1)
  4523. 1))
  4524. (error 1))))
  4525. (next-level (save-excursion
  4526. (condition-case nil
  4527. (progn
  4528. (or (looking-at outline-regexp)
  4529. (outline-next-visible-heading 1))
  4530. (if (looking-at re)
  4531. (- (match-end 0) (match-beginning 0) 1)
  4532. 1))
  4533. (error 1))))
  4534. (new-level (or force-level (max previous-level next-level)))
  4535. (shift (if (or (= old-level -1)
  4536. (= new-level -1)
  4537. (= old-level new-level))
  4538. 0
  4539. (- new-level old-level)))
  4540. (delta (if (> shift 0) -1 1))
  4541. (func (if (> shift 0) 'org-demote 'org-promote))
  4542. (org-odd-levels-only nil)
  4543. beg end)
  4544. ;; Remove the forced level indicator
  4545. (if force-level
  4546. (delete-region (point-at-bol) (point)))
  4547. ;; Paste
  4548. (beginning-of-line 1)
  4549. (org-back-over-empty-lines) ;; FIXME: correct fix????
  4550. (setq beg (point))
  4551. (insert-before-markers txt) ;; FIXME: correct fix????
  4552. (unless (string-match "\n\\'" txt) (insert "\n"))
  4553. (setq end (point))
  4554. (goto-char beg)
  4555. (skip-chars-forward " \t\n\r")
  4556. (setq beg (point))
  4557. ;; Shift if necessary
  4558. (unless (= shift 0)
  4559. (save-restriction
  4560. (narrow-to-region beg end)
  4561. (while (not (= shift 0))
  4562. (org-map-region func (point-min) (point-max))
  4563. (setq shift (+ delta shift)))
  4564. (goto-char (point-min))))
  4565. (when (interactive-p)
  4566. (message "Clipboard pasted as level %d subtree" new-level))
  4567. (if (and kill-ring
  4568. (eq org-subtree-clip (current-kill 0))
  4569. org-subtree-clip-folded)
  4570. ;; The tree was folded before it was killed/copied
  4571. (hide-subtree))))
  4572. (defun org-kill-is-subtree-p (&optional txt)
  4573. "Check if the current kill is an outline subtree, or a set of trees.
  4574. Returns nil if kill does not start with a headline, or if the first
  4575. headline level is not the largest headline level in the tree.
  4576. So this will actually accept several entries of equal levels as well,
  4577. which is OK for `org-paste-subtree'.
  4578. If optional TXT is given, check this string instead of the current kill."
  4579. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  4580. (start-level (and kill
  4581. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  4582. org-outline-regexp "\\)")
  4583. kill)
  4584. (- (match-end 2) (match-beginning 2) 1)))
  4585. (re (concat "^" org-outline-regexp))
  4586. (start (1+ (match-beginning 2))))
  4587. (if (not start-level)
  4588. (progn
  4589. nil) ;; does not even start with a heading
  4590. (catch 'exit
  4591. (while (setq start (string-match re kill (1+ start)))
  4592. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  4593. (throw 'exit nil)))
  4594. t))))
  4595. (defun org-narrow-to-subtree ()
  4596. "Narrow buffer to the current subtree."
  4597. (interactive)
  4598. (save-excursion
  4599. (save-match-data
  4600. (narrow-to-region
  4601. (progn (org-back-to-heading) (point))
  4602. (progn (org-end-of-subtree t t) (point))))))
  4603. ;;; Outline Sorting
  4604. (defun org-sort (with-case)
  4605. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  4606. Optional argument WITH-CASE means sort case-sensitively."
  4607. (interactive "P")
  4608. (if (org-at-table-p)
  4609. (org-call-with-arg 'org-table-sort-lines with-case)
  4610. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  4611. (defun org-sort-remove-invisible (s)
  4612. (remove-text-properties 0 (length s) org-rm-props s)
  4613. (while (string-match org-bracket-link-regexp s)
  4614. (setq s (replace-match (if (match-end 2)
  4615. (match-string 3 s)
  4616. (match-string 1 s)) t t s)))
  4617. s)
  4618. (defvar org-priority-regexp) ; defined later in the file
  4619. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  4620. "Sort entries on a certain level of an outline tree.
  4621. If there is an active region, the entries in the region are sorted.
  4622. Else, if the cursor is before the first entry, sort the top-level items.
  4623. Else, the children of the entry at point are sorted.
  4624. Sorting can be alphabetically, numerically, and by date/time as given by
  4625. the first time stamp in the entry. The command prompts for the sorting
  4626. type unless it has been given to the function through the SORTING-TYPE
  4627. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  4628. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  4629. called with point at the beginning of the record. It must return either
  4630. a string or a number that should serve as the sorting key for that record.
  4631. Comparing entries ignores case by default. However, with an optional argument
  4632. WITH-CASE, the sorting considers case as well."
  4633. (interactive "P")
  4634. (let ((case-func (if with-case 'identity 'downcase))
  4635. start beg end stars re re2
  4636. txt what tmp plain-list-p)
  4637. ;; Find beginning and end of region to sort
  4638. (cond
  4639. ((org-region-active-p)
  4640. ;; we will sort the region
  4641. (setq end (region-end)
  4642. what "region")
  4643. (goto-char (region-beginning))
  4644. (if (not (org-on-heading-p)) (outline-next-heading))
  4645. (setq start (point)))
  4646. ((org-at-item-p)
  4647. ;; we will sort this plain list
  4648. (org-beginning-of-item-list) (setq start (point))
  4649. (org-end-of-item-list) (setq end (point))
  4650. (goto-char start)
  4651. (setq plain-list-p t
  4652. what "plain list"))
  4653. ((or (org-on-heading-p)
  4654. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  4655. ;; we will sort the children of the current headline
  4656. (org-back-to-heading)
  4657. (setq start (point)
  4658. end (progn (org-end-of-subtree t t)
  4659. (org-back-over-empty-lines)
  4660. (point))
  4661. what "children")
  4662. (goto-char start)
  4663. (show-subtree)
  4664. (outline-next-heading))
  4665. (t
  4666. ;; we will sort the top-level entries in this file
  4667. (goto-char (point-min))
  4668. (or (org-on-heading-p) (outline-next-heading))
  4669. (setq start (point) end (point-max) what "top-level")
  4670. (goto-char start)
  4671. (show-all)))
  4672. (setq beg (point))
  4673. (if (>= beg end) (error "Nothing to sort"))
  4674. (unless plain-list-p
  4675. (looking-at "\\(\\*+\\)")
  4676. (setq stars (match-string 1)
  4677. re (concat "^" (regexp-quote stars) " +")
  4678. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  4679. txt (buffer-substring beg end))
  4680. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  4681. (if (and (not (equal stars "*")) (string-match re2 txt))
  4682. (error "Region to sort contains a level above the first entry")))
  4683. (unless sorting-type
  4684. (message
  4685. (if plain-list-p
  4686. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  4687. "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty [f]unc A/N/T/P/F means reversed:")
  4688. what)
  4689. (setq sorting-type (read-char-exclusive))
  4690. (and (= (downcase sorting-type) ?f)
  4691. (setq getkey-func
  4692. (completing-read "Sort using function: "
  4693. obarray 'fboundp t nil nil))
  4694. (setq getkey-func (intern getkey-func)))
  4695. (and (= (downcase sorting-type) ?r)
  4696. (setq property
  4697. (completing-read "Property: "
  4698. (mapcar 'list (org-buffer-property-keys t))
  4699. nil t))))
  4700. (message "Sorting entries...")
  4701. (save-restriction
  4702. (narrow-to-region start end)
  4703. (let ((dcst (downcase sorting-type))
  4704. (now (current-time)))
  4705. (sort-subr
  4706. (/= dcst sorting-type)
  4707. ;; This function moves to the beginning character of the "record" to
  4708. ;; be sorted.
  4709. (if plain-list-p
  4710. (lambda nil
  4711. (if (org-at-item-p) t (goto-char (point-max))))
  4712. (lambda nil
  4713. (if (re-search-forward re nil t)
  4714. (goto-char (match-beginning 0))
  4715. (goto-char (point-max)))))
  4716. ;; This function moves to the last character of the "record" being
  4717. ;; sorted.
  4718. (if plain-list-p
  4719. 'org-end-of-item
  4720. (lambda nil
  4721. (save-match-data
  4722. (condition-case nil
  4723. (outline-forward-same-level 1)
  4724. (error
  4725. (goto-char (point-max)))))))
  4726. ;; This function returns the value that gets sorted against.
  4727. (if plain-list-p
  4728. (lambda nil
  4729. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  4730. (cond
  4731. ((= dcst ?n)
  4732. (string-to-number (buffer-substring (match-end 0)
  4733. (point-at-eol))))
  4734. ((= dcst ?a)
  4735. (buffer-substring (match-end 0) (point-at-eol)))
  4736. ((= dcst ?t)
  4737. (if (re-search-forward org-ts-regexp
  4738. (point-at-eol) t)
  4739. (org-time-string-to-time (match-string 0))
  4740. now))
  4741. ((= dcst ?f)
  4742. (if getkey-func
  4743. (progn
  4744. (setq tmp (funcall getkey-func))
  4745. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4746. tmp)
  4747. (error "Invalid key function `%s'" getkey-func)))
  4748. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4749. (lambda nil
  4750. (cond
  4751. ((= dcst ?n)
  4752. (if (looking-at outline-regexp)
  4753. (string-to-number (buffer-substring (match-end 0)
  4754. (point-at-eol)))
  4755. nil))
  4756. ((= dcst ?a)
  4757. (funcall case-func (buffer-substring (point-at-bol)
  4758. (point-at-eol))))
  4759. ((= dcst ?t)
  4760. (if (re-search-forward org-ts-regexp
  4761. (save-excursion
  4762. (forward-line 2)
  4763. (point)) t)
  4764. (org-time-string-to-time (match-string 0))
  4765. now))
  4766. ((= dcst ?p)
  4767. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  4768. (string-to-char (match-string 2))
  4769. org-default-priority))
  4770. ((= dcst ?r)
  4771. (or (org-entry-get nil property) ""))
  4772. ((= dcst ?f)
  4773. (if getkey-func
  4774. (progn
  4775. (setq tmp (funcall getkey-func))
  4776. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4777. tmp)
  4778. (error "Invalid key function `%s'" getkey-func)))
  4779. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4780. nil
  4781. (cond
  4782. ((= dcst ?a) 'string<)
  4783. ((= dcst ?t) 'time-less-p)
  4784. (t nil)))))
  4785. (message "Sorting entries...done")))
  4786. (defun org-do-sort (table what &optional with-case sorting-type)
  4787. "Sort TABLE of WHAT according to SORTING-TYPE.
  4788. The user will be prompted for the SORTING-TYPE if the call to this
  4789. function does not specify it. WHAT is only for the prompt, to indicate
  4790. what is being sorted. The sorting key will be extracted from
  4791. the car of the elements of the table.
  4792. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  4793. (unless sorting-type
  4794. (message
  4795. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  4796. what)
  4797. (setq sorting-type (read-char-exclusive)))
  4798. (let ((dcst (downcase sorting-type))
  4799. extractfun comparefun)
  4800. ;; Define the appropriate functions
  4801. (cond
  4802. ((= dcst ?n)
  4803. (setq extractfun 'string-to-number
  4804. comparefun (if (= dcst sorting-type) '< '>)))
  4805. ((= dcst ?a)
  4806. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  4807. (lambda(x) (downcase (org-sort-remove-invisible x))))
  4808. comparefun (if (= dcst sorting-type)
  4809. 'string<
  4810. (lambda (a b) (and (not (string< a b))
  4811. (not (string= a b)))))))
  4812. ((= dcst ?t)
  4813. (setq extractfun
  4814. (lambda (x)
  4815. (if (string-match org-ts-regexp x)
  4816. (time-to-seconds
  4817. (org-time-string-to-time (match-string 0 x)))
  4818. 0))
  4819. comparefun (if (= dcst sorting-type) '< '>)))
  4820. (t (error "Invalid sorting type `%c'" sorting-type)))
  4821. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  4822. table)
  4823. (lambda (a b) (funcall comparefun (car a) (car b))))))
  4824. ;;;; Plain list items, including checkboxes
  4825. ;;; Plain list items
  4826. (defun org-at-item-p ()
  4827. "Is point in a line starting a hand-formatted item?"
  4828. (let ((llt org-plain-list-ordered-item-terminator))
  4829. (save-excursion
  4830. (goto-char (point-at-bol))
  4831. (looking-at
  4832. (cond
  4833. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4834. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4835. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+))\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4836. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  4837. (defun org-in-item-p ()
  4838. "It the cursor inside a plain list item.
  4839. Does not have to be the first line."
  4840. (save-excursion
  4841. (condition-case nil
  4842. (progn
  4843. (org-beginning-of-item)
  4844. (org-at-item-p)
  4845. t)
  4846. (error nil))))
  4847. (defun org-insert-item (&optional checkbox)
  4848. "Insert a new item at the current level.
  4849. Return t when things worked, nil when we are not in an item."
  4850. (when (save-excursion
  4851. (condition-case nil
  4852. (progn
  4853. (org-beginning-of-item)
  4854. (org-at-item-p)
  4855. (if (org-invisible-p) (error "Invisible item"))
  4856. t)
  4857. (error nil)))
  4858. (let* ((bul (match-string 0))
  4859. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  4860. (match-end 0)))
  4861. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  4862. pos)
  4863. (cond
  4864. ((and (org-at-item-p) (<= (point) eow))
  4865. ;; before the bullet
  4866. (beginning-of-line 1)
  4867. (open-line (if blank 2 1)))
  4868. ((<= (point) eow)
  4869. (beginning-of-line 1))
  4870. (t
  4871. (unless (org-get-alist-option org-M-RET-may-split-line 'item)
  4872. (end-of-line 1)
  4873. (delete-horizontal-space))
  4874. (newline (if blank 2 1))))
  4875. (insert bul (if checkbox "[ ]" ""))
  4876. (just-one-space)
  4877. (setq pos (point))
  4878. (end-of-line 1)
  4879. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  4880. (org-maybe-renumber-ordered-list)
  4881. (and checkbox (org-update-checkbox-count-maybe))
  4882. t))
  4883. ;;; Checkboxes
  4884. (defun org-at-item-checkbox-p ()
  4885. "Is point at a line starting a plain-list item with a checklet?"
  4886. (and (org-at-item-p)
  4887. (save-excursion
  4888. (goto-char (match-end 0))
  4889. (skip-chars-forward " \t")
  4890. (looking-at "\\[[- X]\\]"))))
  4891. (defun org-toggle-checkbox (&optional arg)
  4892. "Toggle the checkbox in the current line."
  4893. (interactive "P")
  4894. (catch 'exit
  4895. (let (beg end status (firstnew 'unknown))
  4896. (cond
  4897. ((org-region-active-p)
  4898. (setq beg (region-beginning) end (region-end)))
  4899. ((org-on-heading-p)
  4900. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  4901. ((org-at-item-checkbox-p)
  4902. (let ((pos (point)))
  4903. (replace-match
  4904. (cond (arg "[-]")
  4905. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  4906. (t "[ ]"))
  4907. t t)
  4908. (goto-char pos))
  4909. (throw 'exit t))
  4910. (t (error "Not at a checkbox or heading, and no active region")))
  4911. (save-excursion
  4912. (goto-char beg)
  4913. (while (< (point) end)
  4914. (when (org-at-item-checkbox-p)
  4915. (setq status (equal (match-string 0) "[X]"))
  4916. (when (eq firstnew 'unknown)
  4917. (setq firstnew (not status)))
  4918. (replace-match
  4919. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  4920. (beginning-of-line 2)))))
  4921. (org-update-checkbox-count-maybe))
  4922. (defun org-update-checkbox-count-maybe ()
  4923. "Update checkbox statistics unless turned off by user."
  4924. (when org-provide-checkbox-statistics
  4925. (org-update-checkbox-count)))
  4926. (defun org-update-checkbox-count (&optional all)
  4927. "Update the checkbox statistics in the current section.
  4928. This will find all statistic cookies like [57%] and [6/12] and update them
  4929. with the current numbers. With optional prefix argument ALL, do this for
  4930. the whole buffer."
  4931. (interactive "P")
  4932. (save-excursion
  4933. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  4934. (beg (condition-case nil
  4935. (progn (outline-back-to-heading) (point))
  4936. (error (point-min))))
  4937. (end (move-marker (make-marker)
  4938. (progn (outline-next-heading) (point))))
  4939. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  4940. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  4941. (re-find (concat re "\\|" re-box))
  4942. beg-cookie end-cookie is-percent c-on c-off lim
  4943. eline curr-ind next-ind continue-from startsearch
  4944. (cstat 0)
  4945. )
  4946. (when all
  4947. (goto-char (point-min))
  4948. (outline-next-heading)
  4949. (setq beg (point) end (point-max)))
  4950. (goto-char end)
  4951. ;; find each statistic cookie
  4952. (while (re-search-backward re-find beg t)
  4953. (setq beg-cookie (match-beginning 1)
  4954. end-cookie (match-end 1)
  4955. cstat (+ cstat (if end-cookie 1 0))
  4956. startsearch (point-at-eol)
  4957. continue-from (point-at-bol)
  4958. is-percent (match-beginning 2)
  4959. lim (cond
  4960. ((org-on-heading-p) (outline-next-heading) (point))
  4961. ((org-at-item-p) (org-end-of-item) (point))
  4962. (t nil))
  4963. c-on 0
  4964. c-off 0)
  4965. (when lim
  4966. ;; find first checkbox for this cookie and gather
  4967. ;; statistics from all that are at this indentation level
  4968. (goto-char startsearch)
  4969. (if (re-search-forward re-box lim t)
  4970. (progn
  4971. (org-beginning-of-item)
  4972. (setq curr-ind (org-get-indentation))
  4973. (setq next-ind curr-ind)
  4974. (while (= curr-ind next-ind)
  4975. (save-excursion (end-of-line) (setq eline (point)))
  4976. (if (re-search-forward re-box eline t)
  4977. (if (member (match-string 2) '("[ ]" "[-]"))
  4978. (setq c-off (1+ c-off))
  4979. (setq c-on (1+ c-on))
  4980. )
  4981. )
  4982. (org-end-of-item)
  4983. (setq next-ind (org-get-indentation))
  4984. )))
  4985. (goto-char continue-from)
  4986. ;; update cookie
  4987. (when end-cookie
  4988. (delete-region beg-cookie end-cookie)
  4989. (goto-char beg-cookie)
  4990. (insert
  4991. (if is-percent
  4992. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  4993. (format "[%d/%d]" c-on (+ c-on c-off)))))
  4994. ;; update items checkbox if it has one
  4995. (when (org-at-item-p)
  4996. (org-beginning-of-item)
  4997. (when (and (> (+ c-on c-off) 0)
  4998. (re-search-forward re-box (point-at-eol) t))
  4999. (setq beg-cookie (match-beginning 2)
  5000. end-cookie (match-end 2))
  5001. (delete-region beg-cookie end-cookie)
  5002. (goto-char beg-cookie)
  5003. (cond ((= c-off 0) (insert "[X]"))
  5004. ((= c-on 0) (insert "[ ]"))
  5005. (t (insert "[-]")))
  5006. )))
  5007. (goto-char continue-from))
  5008. (when (interactive-p)
  5009. (message "Checkbox satistics updated %s (%d places)"
  5010. (if all "in entire file" "in current outline entry") cstat)))))
  5011. (defun org-get-checkbox-statistics-face ()
  5012. "Select the face for checkbox statistics.
  5013. The face will be `org-done' when all relevant boxes are checked. Otherwise
  5014. it will be `org-todo'."
  5015. (if (match-end 1)
  5016. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  5017. (if (and (> (match-end 2) (match-beginning 2))
  5018. (equal (match-string 2) (match-string 3)))
  5019. 'org-done
  5020. 'org-todo)))
  5021. (defun org-get-indentation (&optional line)
  5022. "Get the indentation of the current line, interpreting tabs.
  5023. When LINE is given, assume it represents a line and compute its indentation."
  5024. (if line
  5025. (if (string-match "^ *" (org-remove-tabs line))
  5026. (match-end 0))
  5027. (save-excursion
  5028. (beginning-of-line 1)
  5029. (skip-chars-forward " \t")
  5030. (current-column))))
  5031. (defun org-remove-tabs (s &optional width)
  5032. "Replace tabulators in S with spaces.
  5033. Assumes that s is a single line, starting in column 0."
  5034. (setq width (or width tab-width))
  5035. (while (string-match "\t" s)
  5036. (setq s (replace-match
  5037. (make-string
  5038. (- (* width (/ (+ (match-beginning 0) width) width))
  5039. (match-beginning 0)) ?\ )
  5040. t t s)))
  5041. s)
  5042. (defun org-fix-indentation (line ind)
  5043. "Fix indentation in LINE.
  5044. IND is a cons cell with target and minimum indentation.
  5045. If the current indenation in LINE is smaller than the minimum,
  5046. leave it alone. If it is larger than ind, set it to the target."
  5047. (let* ((l (org-remove-tabs line))
  5048. (i (org-get-indentation l))
  5049. (i1 (car ind)) (i2 (cdr ind)))
  5050. (if (>= i i2) (setq l (substring line i2)))
  5051. (if (> i1 0)
  5052. (concat (make-string i1 ?\ ) l)
  5053. l)))
  5054. (defcustom org-empty-line-terminates-plain-lists nil
  5055. "Non-nil means, an empty line ends all plain list levels.
  5056. When nil, empty lines are part of the preceeding item."
  5057. :group 'org-plain-lists
  5058. :type 'boolean)
  5059. (defun org-beginning-of-item ()
  5060. "Go to the beginning of the current hand-formatted item.
  5061. If the cursor is not in an item, throw an error."
  5062. (interactive)
  5063. (let ((pos (point))
  5064. (limit (save-excursion
  5065. (condition-case nil
  5066. (progn
  5067. (org-back-to-heading)
  5068. (beginning-of-line 2) (point))
  5069. (error (point-min)))))
  5070. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  5071. ind ind1)
  5072. (if (org-at-item-p)
  5073. (beginning-of-line 1)
  5074. (beginning-of-line 1)
  5075. (skip-chars-forward " \t")
  5076. (setq ind (current-column))
  5077. (if (catch 'exit
  5078. (while t
  5079. (beginning-of-line 0)
  5080. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  5081. (if (looking-at "[ \t]*$")
  5082. (setq ind1 ind-empty)
  5083. (skip-chars-forward " \t")
  5084. (setq ind1 (current-column)))
  5085. (if (< ind1 ind)
  5086. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  5087. nil
  5088. (goto-char pos)
  5089. (error "Not in an item")))))
  5090. (defun org-end-of-item ()
  5091. "Go to the end of the current hand-formatted item.
  5092. If the cursor is not in an item, throw an error."
  5093. (interactive)
  5094. (let* ((pos (point))
  5095. ind1
  5096. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  5097. (limit (save-excursion (outline-next-heading) (point)))
  5098. (ind (save-excursion
  5099. (org-beginning-of-item)
  5100. (skip-chars-forward " \t")
  5101. (current-column)))
  5102. (end (catch 'exit
  5103. (while t
  5104. (beginning-of-line 2)
  5105. (if (eobp) (throw 'exit (point)))
  5106. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  5107. (if (looking-at "[ \t]*$")
  5108. (setq ind1 ind-empty)
  5109. (skip-chars-forward " \t")
  5110. (setq ind1 (current-column)))
  5111. (if (<= ind1 ind)
  5112. (throw 'exit (point-at-bol)))))))
  5113. (if end
  5114. (goto-char end)
  5115. (goto-char pos)
  5116. (error "Not in an item"))))
  5117. (defun org-next-item ()
  5118. "Move to the beginning of the next item in the current plain list.
  5119. Error if not at a plain list, or if this is the last item in the list."
  5120. (interactive)
  5121. (let (ind ind1 (pos (point)))
  5122. (org-beginning-of-item)
  5123. (setq ind (org-get-indentation))
  5124. (org-end-of-item)
  5125. (setq ind1 (org-get-indentation))
  5126. (unless (and (org-at-item-p) (= ind ind1))
  5127. (goto-char pos)
  5128. (error "On last item"))))
  5129. (defun org-previous-item ()
  5130. "Move to the beginning of the previous item in the current plain list.
  5131. Error if not at a plain list, or if this is the first item in the list."
  5132. (interactive)
  5133. (let (beg ind ind1 (pos (point)))
  5134. (org-beginning-of-item)
  5135. (setq beg (point))
  5136. (setq ind (org-get-indentation))
  5137. (goto-char beg)
  5138. (catch 'exit
  5139. (while t
  5140. (beginning-of-line 0)
  5141. (if (looking-at "[ \t]*$")
  5142. nil
  5143. (if (<= (setq ind1 (org-get-indentation)) ind)
  5144. (throw 'exit t)))))
  5145. (condition-case nil
  5146. (if (or (not (org-at-item-p))
  5147. (< ind1 (1- ind)))
  5148. (error "")
  5149. (org-beginning-of-item))
  5150. (error (goto-char pos)
  5151. (error "On first item")))))
  5152. (defun org-first-list-item-p ()
  5153. "Is this heading the item in a plain list?"
  5154. (unless (org-at-item-p)
  5155. (error "Not at a plain list item"))
  5156. (org-beginning-of-item)
  5157. (= (point) (save-excursion (org-beginning-of-item-list))))
  5158. (defun org-move-item-down ()
  5159. "Move the plain list item at point down, i.e. swap with following item.
  5160. Subitems (items with larger indentation) are considered part of the item,
  5161. so this really moves item trees."
  5162. (interactive)
  5163. (let (beg beg0 end end0 ind ind1 (pos (point)) txt ne-end ne-beg)
  5164. (org-beginning-of-item)
  5165. (setq beg0 (point))
  5166. (save-excursion
  5167. (setq ne-beg (org-back-over-empty-lines))
  5168. (setq beg (point)))
  5169. (goto-char beg0)
  5170. (setq ind (org-get-indentation))
  5171. (org-end-of-item)
  5172. (setq end0 (point))
  5173. (setq ind1 (org-get-indentation))
  5174. (setq ne-end (org-back-over-empty-lines))
  5175. (setq end (point))
  5176. (goto-char beg0)
  5177. (when (and (org-first-list-item-p) (< ne-end ne-beg))
  5178. ;; include less whitespace
  5179. (save-excursion
  5180. (goto-char beg)
  5181. (forward-line (- ne-beg ne-end))
  5182. (setq beg (point))))
  5183. (goto-char end0)
  5184. (if (and (org-at-item-p) (= ind ind1))
  5185. (progn
  5186. (org-end-of-item)
  5187. (org-back-over-empty-lines)
  5188. (setq txt (buffer-substring beg end))
  5189. (save-excursion
  5190. (delete-region beg end))
  5191. (setq pos (point))
  5192. (insert txt)
  5193. (goto-char pos) (org-skip-whitespace)
  5194. (org-maybe-renumber-ordered-list))
  5195. (goto-char pos)
  5196. (error "Cannot move this item further down"))))
  5197. (defun org-move-item-up (arg)
  5198. "Move the plain list item at point up, i.e. swap with previous item.
  5199. Subitems (items with larger indentation) are considered part of the item,
  5200. so this really moves item trees."
  5201. (interactive "p")
  5202. (let (beg beg0 end ind ind1 (pos (point)) txt
  5203. ne-beg ne-ins ins-end)
  5204. (org-beginning-of-item)
  5205. (setq beg0 (point))
  5206. (setq ind (org-get-indentation))
  5207. (save-excursion
  5208. (setq ne-beg (org-back-over-empty-lines))
  5209. (setq beg (point)))
  5210. (goto-char beg0)
  5211. (org-end-of-item)
  5212. (setq end (point))
  5213. (goto-char beg0)
  5214. (catch 'exit
  5215. (while t
  5216. (beginning-of-line 0)
  5217. (if (looking-at "[ \t]*$")
  5218. (if org-empty-line-terminates-plain-lists
  5219. (progn
  5220. (goto-char pos)
  5221. (error "Cannot move this item further up"))
  5222. nil)
  5223. (if (<= (setq ind1 (org-get-indentation)) ind)
  5224. (throw 'exit t)))))
  5225. (condition-case nil
  5226. (org-beginning-of-item)
  5227. (error (goto-char beg)
  5228. (error "Cannot move this item further up")))
  5229. (setq ind1 (org-get-indentation))
  5230. (if (and (org-at-item-p) (= ind ind1))
  5231. (progn
  5232. (setq ne-ins (org-back-over-empty-lines))
  5233. (setq txt (buffer-substring beg end))
  5234. (save-excursion
  5235. (delete-region beg end))
  5236. (setq pos (point))
  5237. (insert txt)
  5238. (setq ins-end (point))
  5239. (goto-char pos) (org-skip-whitespace)
  5240. (when (and (org-first-list-item-p) (> ne-ins ne-beg))
  5241. ;; Move whitespace back to beginning
  5242. (save-excursion
  5243. (goto-char ins-end)
  5244. (let ((kill-whole-line t))
  5245. (kill-line (- ne-ins ne-beg)) (point)))
  5246. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5247. (org-maybe-renumber-ordered-list))
  5248. (goto-char pos)
  5249. (error "Cannot move this item further up"))))
  5250. (defun org-maybe-renumber-ordered-list ()
  5251. "Renumber the ordered list at point if setup allows it.
  5252. This tests the user option `org-auto-renumber-ordered-lists' before
  5253. doing the renumbering."
  5254. (interactive)
  5255. (when (and org-auto-renumber-ordered-lists
  5256. (org-at-item-p))
  5257. (if (match-beginning 3)
  5258. (org-renumber-ordered-list 1)
  5259. (org-fix-bullet-type))))
  5260. (defun org-maybe-renumber-ordered-list-safe ()
  5261. (condition-case nil
  5262. (save-excursion
  5263. (org-maybe-renumber-ordered-list))
  5264. (error nil)))
  5265. (defun org-cycle-list-bullet (&optional which)
  5266. "Cycle through the different itemize/enumerate bullets.
  5267. This cycle the entire list level through the sequence:
  5268. `-' -> `+' -> `*' -> `1.' -> `1)'
  5269. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  5270. 0 meand `-', 1 means `+' etc."
  5271. (interactive "P")
  5272. (org-preserve-lc
  5273. (org-beginning-of-item-list)
  5274. (org-at-item-p)
  5275. (beginning-of-line 1)
  5276. (let ((current (match-string 0))
  5277. (prevp (eq which 'previous))
  5278. new)
  5279. (setq new (cond
  5280. ((and (numberp which)
  5281. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  5282. ((string-match "-" current) (if prevp "1)" "+"))
  5283. ((string-match "\\+" current)
  5284. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  5285. ((string-match "\\*" current) (if prevp "+" "1."))
  5286. ((string-match "\\." current) (if prevp "*" "1)"))
  5287. ((string-match ")" current) (if prevp "1." "-"))
  5288. (t (error "This should not happen"))))
  5289. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  5290. (org-fix-bullet-type)
  5291. (org-maybe-renumber-ordered-list))))
  5292. (defun org-get-string-indentation (s)
  5293. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  5294. (let ((n -1) (i 0) (w tab-width) c)
  5295. (catch 'exit
  5296. (while (< (setq n (1+ n)) (length s))
  5297. (setq c (aref s n))
  5298. (cond ((= c ?\ ) (setq i (1+ i)))
  5299. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  5300. (t (throw 'exit t)))))
  5301. i))
  5302. (defun org-renumber-ordered-list (arg)
  5303. "Renumber an ordered plain list.
  5304. Cursor needs to be in the first line of an item, the line that starts
  5305. with something like \"1.\" or \"2)\"."
  5306. (interactive "p")
  5307. (unless (and (org-at-item-p)
  5308. (match-beginning 3))
  5309. (error "This is not an ordered list"))
  5310. (let ((line (org-current-line))
  5311. (col (current-column))
  5312. (ind (org-get-string-indentation
  5313. (buffer-substring (point-at-bol) (match-beginning 3))))
  5314. ;; (term (substring (match-string 3) -1))
  5315. ind1 (n (1- arg))
  5316. fmt)
  5317. ;; find where this list begins
  5318. (org-beginning-of-item-list)
  5319. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  5320. (setq fmt (concat "%d" (match-string 1)))
  5321. (beginning-of-line 0)
  5322. ;; walk forward and replace these numbers
  5323. (catch 'exit
  5324. (while t
  5325. (catch 'next
  5326. (beginning-of-line 2)
  5327. (if (eobp) (throw 'exit nil))
  5328. (if (looking-at "[ \t]*$") (throw 'next nil))
  5329. (skip-chars-forward " \t") (setq ind1 (current-column))
  5330. (if (> ind1 ind) (throw 'next t))
  5331. (if (< ind1 ind) (throw 'exit t))
  5332. (if (not (org-at-item-p)) (throw 'exit nil))
  5333. (delete-region (match-beginning 2) (match-end 2))
  5334. (goto-char (match-beginning 2))
  5335. (insert (format fmt (setq n (1+ n)))))))
  5336. (goto-line line)
  5337. (move-to-column col)))
  5338. (defun org-fix-bullet-type ()
  5339. "Make sure all items in this list have the same bullet as the firsst item."
  5340. (interactive)
  5341. (unless (org-at-item-p) (error "This is not a list"))
  5342. (let ((line (org-current-line))
  5343. (col (current-column))
  5344. (ind (current-indentation))
  5345. ind1 bullet)
  5346. ;; find where this list begins
  5347. (org-beginning-of-item-list)
  5348. (beginning-of-line 1)
  5349. ;; find out what the bullet type is
  5350. (looking-at "[ \t]*\\(\\S-+\\)")
  5351. (setq bullet (match-string 1))
  5352. ;; walk forward and replace these numbers
  5353. (beginning-of-line 0)
  5354. (catch 'exit
  5355. (while t
  5356. (catch 'next
  5357. (beginning-of-line 2)
  5358. (if (eobp) (throw 'exit nil))
  5359. (if (looking-at "[ \t]*$") (throw 'next nil))
  5360. (skip-chars-forward " \t") (setq ind1 (current-column))
  5361. (if (> ind1 ind) (throw 'next t))
  5362. (if (< ind1 ind) (throw 'exit t))
  5363. (if (not (org-at-item-p)) (throw 'exit nil))
  5364. (skip-chars-forward " \t")
  5365. (looking-at "\\S-+")
  5366. (replace-match bullet))))
  5367. (goto-line line)
  5368. (move-to-column col)
  5369. (if (string-match "[0-9]" bullet)
  5370. (org-renumber-ordered-list 1))))
  5371. (defun org-beginning-of-item-list ()
  5372. "Go to the beginning of the current item list.
  5373. I.e. to the first item in this list."
  5374. (interactive)
  5375. (org-beginning-of-item)
  5376. (let ((pos (point-at-bol))
  5377. (ind (org-get-indentation))
  5378. ind1)
  5379. ;; find where this list begins
  5380. (catch 'exit
  5381. (while t
  5382. (catch 'next
  5383. (beginning-of-line 0)
  5384. (if (looking-at "[ \t]*$")
  5385. (throw (if (bobp) 'exit 'next) t))
  5386. (skip-chars-forward " \t") (setq ind1 (current-column))
  5387. (if (or (< ind1 ind)
  5388. (and (= ind1 ind)
  5389. (not (org-at-item-p)))
  5390. (bobp))
  5391. (throw 'exit t)
  5392. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  5393. (goto-char pos)))
  5394. (defun org-end-of-item-list ()
  5395. "Go to the end of the current item list.
  5396. I.e. to the text after the last item."
  5397. (interactive)
  5398. (org-beginning-of-item)
  5399. (let ((pos (point-at-bol))
  5400. (ind (org-get-indentation))
  5401. ind1)
  5402. ;; find where this list begins
  5403. (catch 'exit
  5404. (while t
  5405. (catch 'next
  5406. (beginning-of-line 2)
  5407. (if (looking-at "[ \t]*$")
  5408. (throw (if (eobp) 'exit 'next) t))
  5409. (skip-chars-forward " \t") (setq ind1 (current-column))
  5410. (if (or (< ind1 ind)
  5411. (and (= ind1 ind)
  5412. (not (org-at-item-p)))
  5413. (eobp))
  5414. (progn
  5415. (setq pos (point-at-bol))
  5416. (throw 'exit t))))))
  5417. (goto-char pos)))
  5418. (defvar org-last-indent-begin-marker (make-marker))
  5419. (defvar org-last-indent-end-marker (make-marker))
  5420. (defun org-outdent-item (arg)
  5421. "Outdent a local list item."
  5422. (interactive "p")
  5423. (org-indent-item (- arg)))
  5424. (defun org-indent-item (arg)
  5425. "Indent a local list item."
  5426. (interactive "p")
  5427. (unless (org-at-item-p)
  5428. (error "Not on an item"))
  5429. (save-excursion
  5430. (let (beg end ind ind1 tmp delta ind-down ind-up)
  5431. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  5432. (setq beg org-last-indent-begin-marker
  5433. end org-last-indent-end-marker)
  5434. (org-beginning-of-item)
  5435. (setq beg (move-marker org-last-indent-begin-marker (point)))
  5436. (org-end-of-item)
  5437. (setq end (move-marker org-last-indent-end-marker (point))))
  5438. (goto-char beg)
  5439. (setq tmp (org-item-indent-positions)
  5440. ind (car tmp)
  5441. ind-down (nth 2 tmp)
  5442. ind-up (nth 1 tmp)
  5443. delta (if (> arg 0)
  5444. (if ind-down (- ind-down ind) 2)
  5445. (if ind-up (- ind-up ind) -2)))
  5446. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  5447. (while (< (point) end)
  5448. (beginning-of-line 1)
  5449. (skip-chars-forward " \t") (setq ind1 (current-column))
  5450. (delete-region (point-at-bol) (point))
  5451. (or (eolp) (indent-to-column (+ ind1 delta)))
  5452. (beginning-of-line 2))))
  5453. (org-fix-bullet-type)
  5454. (org-maybe-renumber-ordered-list-safe)
  5455. (save-excursion
  5456. (beginning-of-line 0)
  5457. (condition-case nil (org-beginning-of-item) (error nil))
  5458. (org-maybe-renumber-ordered-list-safe)))
  5459. (defun org-item-indent-positions ()
  5460. "Return indentation for plain list items.
  5461. This returns a list with three values: The current indentation, the
  5462. parent indentation and the indentation a child should habe.
  5463. Assumes cursor in item line."
  5464. (let* ((bolpos (point-at-bol))
  5465. (ind (org-get-indentation))
  5466. ind-down ind-up pos)
  5467. (save-excursion
  5468. (org-beginning-of-item-list)
  5469. (skip-chars-backward "\n\r \t")
  5470. (when (org-in-item-p)
  5471. (org-beginning-of-item)
  5472. (setq ind-up (org-get-indentation))))
  5473. (setq pos (point))
  5474. (save-excursion
  5475. (cond
  5476. ((and (condition-case nil (progn (org-previous-item) t)
  5477. (error nil))
  5478. (or (forward-char 1) t)
  5479. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  5480. (setq ind-down (org-get-indentation)))
  5481. ((and (goto-char pos)
  5482. (org-at-item-p))
  5483. (goto-char (match-end 0))
  5484. (skip-chars-forward " \t")
  5485. (setq ind-down (current-column)))))
  5486. (list ind ind-up ind-down)))
  5487. ;;; The orgstruct minor mode
  5488. ;; Define a minor mode which can be used in other modes in order to
  5489. ;; integrate the org-mode structure editing commands.
  5490. ;; This is really a hack, because the org-mode structure commands use
  5491. ;; keys which normally belong to the major mode. Here is how it
  5492. ;; works: The minor mode defines all the keys necessary to operate the
  5493. ;; structure commands, but wraps the commands into a function which
  5494. ;; tests if the cursor is currently at a headline or a plain list
  5495. ;; item. If that is the case, the structure command is used,
  5496. ;; temporarily setting many Org-mode variables like regular
  5497. ;; expressions for filling etc. However, when any of those keys is
  5498. ;; used at a different location, function uses `key-binding' to look
  5499. ;; up if the key has an associated command in another currently active
  5500. ;; keymap (minor modes, major mode, global), and executes that
  5501. ;; command. There might be problems if any of the keys is otherwise
  5502. ;; used as a prefix key.
  5503. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5504. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5505. ;; addresses this by checking explicitly for both bindings.
  5506. (defvar orgstruct-mode-map (make-sparse-keymap)
  5507. "Keymap for the minor `orgstruct-mode'.")
  5508. (defvar org-local-vars nil
  5509. "List of local variables, for use by `orgstruct-mode'")
  5510. ;;;###autoload
  5511. (define-minor-mode orgstruct-mode
  5512. "Toggle the minor more `orgstruct-mode'.
  5513. This mode is for using Org-mode structure commands in other modes.
  5514. The following key behave as if Org-mode was active, if the cursor
  5515. is on a headline, or on a plain list item (both in the definition
  5516. of Org-mode).
  5517. M-up Move entry/item up
  5518. M-down Move entry/item down
  5519. M-left Promote
  5520. M-right Demote
  5521. M-S-up Move entry/item up
  5522. M-S-down Move entry/item down
  5523. M-S-left Promote subtree
  5524. M-S-right Demote subtree
  5525. M-q Fill paragraph and items like in Org-mode
  5526. C-c ^ Sort entries
  5527. C-c - Cycle list bullet
  5528. TAB Cycle item visibility
  5529. M-RET Insert new heading/item
  5530. S-M-RET Insert new TODO heading / Chekbox item
  5531. C-c C-c Set tags / toggle checkbox"
  5532. nil " OrgStruct" nil
  5533. (org-load-modules-maybe)
  5534. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5535. ;;;###autoload
  5536. (defun turn-on-orgstruct ()
  5537. "Unconditionally turn on `orgstruct-mode'."
  5538. (orgstruct-mode 1))
  5539. ;;;###autoload
  5540. (defun turn-on-orgstruct++ ()
  5541. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5542. In addition to setting orgstruct-mode, this also exports all indentation and
  5543. autofilling variables from org-mode into the buffer. Note that turning
  5544. off orgstruct-mode will *not* remove these additional settings."
  5545. (orgstruct-mode 1)
  5546. (let (var val)
  5547. (mapc
  5548. (lambda (x)
  5549. (when (string-match
  5550. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5551. (symbol-name (car x)))
  5552. (setq var (car x) val (nth 1 x))
  5553. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5554. org-local-vars)))
  5555. (defun orgstruct-error ()
  5556. "Error when there is no default binding for a structure key."
  5557. (interactive)
  5558. (error "This key has no function outside structure elements"))
  5559. (defun orgstruct-setup ()
  5560. "Setup orgstruct keymaps."
  5561. (let ((nfunc 0)
  5562. (bindings
  5563. (list
  5564. '([(meta up)] org-metaup)
  5565. '([(meta down)] org-metadown)
  5566. '([(meta left)] org-metaleft)
  5567. '([(meta right)] org-metaright)
  5568. '([(meta shift up)] org-shiftmetaup)
  5569. '([(meta shift down)] org-shiftmetadown)
  5570. '([(meta shift left)] org-shiftmetaleft)
  5571. '([(meta shift right)] org-shiftmetaright)
  5572. '([(shift up)] org-shiftup)
  5573. '([(shift down)] org-shiftdown)
  5574. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5575. '("\M-q" fill-paragraph)
  5576. '("\C-c^" org-sort)
  5577. '("\C-c-" org-cycle-list-bullet)))
  5578. elt key fun cmd)
  5579. (while (setq elt (pop bindings))
  5580. (setq nfunc (1+ nfunc))
  5581. (setq key (org-key (car elt))
  5582. fun (nth 1 elt)
  5583. cmd (orgstruct-make-binding fun nfunc key))
  5584. (org-defkey orgstruct-mode-map key cmd))
  5585. ;; Special treatment needed for TAB and RET
  5586. (org-defkey orgstruct-mode-map [(tab)]
  5587. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5588. (org-defkey orgstruct-mode-map "\C-i"
  5589. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5590. (org-defkey orgstruct-mode-map "\M-\C-m"
  5591. (orgstruct-make-binding 'org-insert-heading 105
  5592. "\M-\C-m" [(meta return)]))
  5593. (org-defkey orgstruct-mode-map [(meta return)]
  5594. (orgstruct-make-binding 'org-insert-heading 106
  5595. [(meta return)] "\M-\C-m"))
  5596. (org-defkey orgstruct-mode-map [(shift meta return)]
  5597. (orgstruct-make-binding 'org-insert-todo-heading 107
  5598. [(meta return)] "\M-\C-m"))
  5599. (unless org-local-vars
  5600. (setq org-local-vars (org-get-local-variables)))
  5601. t))
  5602. (defun orgstruct-make-binding (fun n &rest keys)
  5603. "Create a function for binding in the structure minor mode.
  5604. FUN is the command to call inside a table. N is used to create a unique
  5605. command name. KEYS are keys that should be checked in for a command
  5606. to execute outside of tables."
  5607. (eval
  5608. (list 'defun
  5609. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5610. '(arg)
  5611. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5612. "Outside of structure, run the binding of `"
  5613. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5614. "'.")
  5615. '(interactive "p")
  5616. (list 'if
  5617. '(org-context-p 'headline 'item)
  5618. (list 'org-run-like-in-org-mode (list 'quote fun))
  5619. (list 'let '(orgstruct-mode)
  5620. (list 'call-interactively
  5621. (append '(or)
  5622. (mapcar (lambda (k)
  5623. (list 'key-binding k))
  5624. keys)
  5625. '('orgstruct-error))))))))
  5626. (defun org-context-p (&rest contexts)
  5627. "Check if local context is and of CONTEXTS.
  5628. Possible values in the list of contexts are `table', `headline', and `item'."
  5629. (let ((pos (point)))
  5630. (goto-char (point-at-bol))
  5631. (prog1 (or (and (memq 'table contexts)
  5632. (looking-at "[ \t]*|"))
  5633. (and (memq 'headline contexts)
  5634. (looking-at "\\*+"))
  5635. (and (memq 'item contexts)
  5636. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5637. (goto-char pos))))
  5638. (defun org-get-local-variables ()
  5639. "Return a list of all local variables in an org-mode buffer."
  5640. (let (varlist)
  5641. (with-current-buffer (get-buffer-create "*Org tmp*")
  5642. (erase-buffer)
  5643. (org-mode)
  5644. (setq varlist (buffer-local-variables)))
  5645. (kill-buffer "*Org tmp*")
  5646. (delq nil
  5647. (mapcar
  5648. (lambda (x)
  5649. (setq x
  5650. (if (symbolp x)
  5651. (list x)
  5652. (list (car x) (list 'quote (cdr x)))))
  5653. (if (string-match
  5654. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5655. (symbol-name (car x)))
  5656. x nil))
  5657. varlist))))
  5658. ;;;###autoload
  5659. (defun org-run-like-in-org-mode (cmd)
  5660. (org-load-modules-maybe)
  5661. (unless org-local-vars
  5662. (setq org-local-vars (org-get-local-variables)))
  5663. (eval (list 'let org-local-vars
  5664. (list 'call-interactively (list 'quote cmd)))))
  5665. ;;;; Archiving
  5666. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  5667. (defun org-archive-subtree (&optional find-done)
  5668. "Move the current subtree to the archive.
  5669. The archive can be a certain top-level heading in the current file, or in
  5670. a different file. The tree will be moved to that location, the subtree
  5671. heading be marked DONE, and the current time will be added.
  5672. When called with prefix argument FIND-DONE, find whole trees without any
  5673. open TODO items and archive them (after getting confirmation from the user).
  5674. If the cursor is not at a headline when this comand is called, try all level
  5675. 1 trees. If the cursor is on a headline, only try the direct children of
  5676. this heading."
  5677. (interactive "P")
  5678. (if find-done
  5679. (org-archive-all-done)
  5680. ;; Save all relevant TODO keyword-relatex variables
  5681. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  5682. (tr-org-todo-keywords-1 org-todo-keywords-1)
  5683. (tr-org-todo-kwd-alist org-todo-kwd-alist)
  5684. (tr-org-done-keywords org-done-keywords)
  5685. (tr-org-todo-regexp org-todo-regexp)
  5686. (tr-org-todo-line-regexp org-todo-line-regexp)
  5687. (tr-org-odd-levels-only org-odd-levels-only)
  5688. (this-buffer (current-buffer))
  5689. (org-archive-location org-archive-location)
  5690. (re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
  5691. ;; start of variables that will be used for saving context
  5692. ;; The compiler complains about them - keep them anyway!
  5693. (file (abbreviate-file-name (buffer-file-name)))
  5694. (olpath (mapconcat 'identity (org-get-outline-path) "/"))
  5695. (time (format-time-string
  5696. (substring (cdr org-time-stamp-formats) 1 -1)
  5697. (current-time)))
  5698. afile heading buffer level newfile-p
  5699. category todo priority
  5700. ;; start of variables that will be used for savind context
  5701. ltags itags prop)
  5702. ;; Try to find a local archive location
  5703. (save-excursion
  5704. (save-restriction
  5705. (widen)
  5706. (setq prop (org-entry-get nil "ARCHIVE" 'inherit))
  5707. (if (and prop (string-match "\\S-" prop))
  5708. (setq org-archive-location prop)
  5709. (if (or (re-search-backward re nil t)
  5710. (re-search-forward re nil t))
  5711. (setq org-archive-location (match-string 1))))))
  5712. (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
  5713. (progn
  5714. (setq afile (format (match-string 1 org-archive-location)
  5715. (file-name-nondirectory buffer-file-name))
  5716. heading (match-string 2 org-archive-location)))
  5717. (error "Invalid `org-archive-location'"))
  5718. (if (> (length afile) 0)
  5719. (setq newfile-p (not (file-exists-p afile))
  5720. buffer (find-file-noselect afile))
  5721. (setq buffer (current-buffer)))
  5722. (unless buffer
  5723. (error "Cannot access file \"%s\"" afile))
  5724. (if (and (> (length heading) 0)
  5725. (string-match "^\\*+" heading))
  5726. (setq level (match-end 0))
  5727. (setq heading nil level 0))
  5728. (save-excursion
  5729. (org-back-to-heading t)
  5730. ;; Get context information that will be lost by moving the tree
  5731. (org-refresh-category-properties)
  5732. (setq category (org-get-category)
  5733. todo (and (looking-at org-todo-line-regexp)
  5734. (match-string 2))
  5735. priority (org-get-priority (if (match-end 3) (match-string 3) ""))
  5736. ltags (org-get-tags)
  5737. itags (org-delete-all ltags (org-get-tags-at)))
  5738. (setq ltags (mapconcat 'identity ltags " ")
  5739. itags (mapconcat 'identity itags " "))
  5740. ;; We first only copy, in case something goes wrong
  5741. ;; we need to protect this-command, to avoid kill-region sets it,
  5742. ;; which would lead to duplication of subtrees
  5743. (let (this-command) (org-copy-subtree))
  5744. (set-buffer buffer)
  5745. ;; Enforce org-mode for the archive buffer
  5746. (if (not (org-mode-p))
  5747. ;; Force the mode for future visits.
  5748. (let ((org-insert-mode-line-in-empty-file t)
  5749. (org-inhibit-startup t))
  5750. (call-interactively 'org-mode)))
  5751. (when newfile-p
  5752. (goto-char (point-max))
  5753. (insert (format "\nArchived entries from file %s\n\n"
  5754. (buffer-file-name this-buffer))))
  5755. ;; Force the TODO keywords of the original buffer
  5756. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  5757. (org-todo-keywords-1 tr-org-todo-keywords-1)
  5758. (org-todo-kwd-alist tr-org-todo-kwd-alist)
  5759. (org-done-keywords tr-org-done-keywords)
  5760. (org-todo-regexp tr-org-todo-regexp)
  5761. (org-todo-line-regexp tr-org-todo-line-regexp)
  5762. (org-odd-levels-only
  5763. (if (local-variable-p 'org-odd-levels-only (current-buffer))
  5764. org-odd-levels-only
  5765. tr-org-odd-levels-only)))
  5766. (goto-char (point-min))
  5767. (show-all)
  5768. (if heading
  5769. (progn
  5770. (if (re-search-forward
  5771. (concat "^" (regexp-quote heading)
  5772. (org-re "[ \t]*\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\($\\|\r\\)"))
  5773. nil t)
  5774. (goto-char (match-end 0))
  5775. ;; Heading not found, just insert it at the end
  5776. (goto-char (point-max))
  5777. (or (bolp) (insert "\n"))
  5778. (insert "\n" heading "\n")
  5779. (end-of-line 0))
  5780. ;; Make the subtree visible
  5781. (show-subtree)
  5782. (org-end-of-subtree t)
  5783. (skip-chars-backward " \t\r\n")
  5784. (and (looking-at "[ \t\r\n]*")
  5785. (replace-match "\n\n")))
  5786. ;; No specific heading, just go to end of file.
  5787. (goto-char (point-max)) (insert "\n"))
  5788. ;; Paste
  5789. (org-paste-subtree (org-get-valid-level level 1))
  5790. ;; Mark the entry as done
  5791. (when (and org-archive-mark-done
  5792. (looking-at org-todo-line-regexp)
  5793. (or (not (match-end 2))
  5794. (not (member (match-string 2) org-done-keywords))))
  5795. (let (org-log-done org-todo-log-states)
  5796. (org-todo
  5797. (car (or (member org-archive-mark-done org-done-keywords)
  5798. org-done-keywords)))))
  5799. ;; Add the context info
  5800. (when org-archive-save-context-info
  5801. (let ((l org-archive-save-context-info) e n v)
  5802. (while (setq e (pop l))
  5803. (when (and (setq v (symbol-value e))
  5804. (stringp v) (string-match "\\S-" v))
  5805. (setq n (concat "ARCHIVE_" (upcase (symbol-name e))))
  5806. (org-entry-put (point) n v)))))
  5807. ;; Save and kill the buffer, if it is not the same buffer.
  5808. (if (not (eq this-buffer buffer))
  5809. (progn (save-buffer) (kill-buffer buffer)))))
  5810. ;; Here we are back in the original buffer. Everything seems to have
  5811. ;; worked. So now cut the tree and finish up.
  5812. (let (this-command) (org-cut-subtree))
  5813. (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
  5814. (message "Subtree archived %s"
  5815. (if (eq this-buffer buffer)
  5816. (concat "under heading: " heading)
  5817. (concat "in file: " (abbreviate-file-name afile)))))))
  5818. (defun org-refresh-category-properties ()
  5819. "Refresh category text properties in teh buffer."
  5820. (let ((def-cat (cond
  5821. ((null org-category)
  5822. (if buffer-file-name
  5823. (file-name-sans-extension
  5824. (file-name-nondirectory buffer-file-name))
  5825. "???"))
  5826. ((symbolp org-category) (symbol-name org-category))
  5827. (t org-category)))
  5828. beg end cat pos optionp)
  5829. (org-unmodified
  5830. (save-excursion
  5831. (save-restriction
  5832. (widen)
  5833. (goto-char (point-min))
  5834. (put-text-property (point) (point-max) 'org-category def-cat)
  5835. (while (re-search-forward
  5836. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5837. (setq pos (match-end 0)
  5838. optionp (equal (char-after (match-beginning 0)) ?#)
  5839. cat (org-trim (match-string 2)))
  5840. (if optionp
  5841. (setq beg (point-at-bol) end (point-max))
  5842. (org-back-to-heading t)
  5843. (setq beg (point) end (org-end-of-subtree t t)))
  5844. (put-text-property beg end 'org-category cat)
  5845. (goto-char pos)))))))
  5846. (defun org-archive-all-done (&optional tag)
  5847. "Archive sublevels of the current tree without open TODO items.
  5848. If the cursor is not on a headline, try all level 1 trees. If
  5849. it is on a headline, try all direct children.
  5850. When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
  5851. (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
  5852. (rea (concat ".*:" org-archive-tag ":"))
  5853. (begm (make-marker))
  5854. (endm (make-marker))
  5855. (question (if tag "Set ARCHIVE tag (no open TODO items)? "
  5856. "Move subtree to archive (no open TODO items)? "))
  5857. beg end (cntarch 0))
  5858. (if (org-on-heading-p)
  5859. (progn
  5860. (setq re1 (concat "^" (regexp-quote
  5861. (make-string
  5862. (1+ (- (match-end 0) (match-beginning 0) 1))
  5863. ?*))
  5864. " "))
  5865. (move-marker begm (point))
  5866. (move-marker endm (org-end-of-subtree t)))
  5867. (setq re1 "^* ")
  5868. (move-marker begm (point-min))
  5869. (move-marker endm (point-max)))
  5870. (save-excursion
  5871. (goto-char begm)
  5872. (while (re-search-forward re1 endm t)
  5873. (setq beg (match-beginning 0)
  5874. end (save-excursion (org-end-of-subtree t) (point)))
  5875. (goto-char beg)
  5876. (if (re-search-forward re end t)
  5877. (goto-char end)
  5878. (goto-char beg)
  5879. (if (and (or (not tag) (not (looking-at rea)))
  5880. (y-or-n-p question))
  5881. (progn
  5882. (if tag
  5883. (org-toggle-tag org-archive-tag 'on)
  5884. (org-archive-subtree))
  5885. (setq cntarch (1+ cntarch)))
  5886. (goto-char end)))))
  5887. (message "%d trees archived" cntarch)))
  5888. (defun org-cycle-hide-drawers (state)
  5889. "Re-hide all drawers after a visibility state change."
  5890. (when (and (org-mode-p)
  5891. (not (memq state '(overview folded))))
  5892. (save-excursion
  5893. (let* ((globalp (memq state '(contents all)))
  5894. (beg (if globalp (point-min) (point)))
  5895. (end (if globalp (point-max) (org-end-of-subtree t))))
  5896. (goto-char beg)
  5897. (while (re-search-forward org-drawer-regexp end t)
  5898. (org-flag-drawer t))))))
  5899. (defun org-flag-drawer (flag)
  5900. (save-excursion
  5901. (beginning-of-line 1)
  5902. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5903. (let ((b (match-end 0))
  5904. (outline-regexp org-outline-regexp))
  5905. (if (re-search-forward
  5906. "^[ \t]*:END:"
  5907. (save-excursion (outline-next-heading) (point)) t)
  5908. (outline-flag-region b (point-at-eol) flag)
  5909. (error ":END: line missing"))))))
  5910. (defun org-cycle-hide-archived-subtrees (state)
  5911. "Re-hide all archived subtrees after a visibility state change."
  5912. (when (and (not org-cycle-open-archived-trees)
  5913. (not (memq state '(overview folded))))
  5914. (save-excursion
  5915. (let* ((globalp (memq state '(contents all)))
  5916. (beg (if globalp (point-min) (point)))
  5917. (end (if globalp (point-max) (org-end-of-subtree t))))
  5918. (org-hide-archived-subtrees beg end)
  5919. (goto-char beg)
  5920. (if (looking-at (concat ".*:" org-archive-tag ":"))
  5921. (message "%s" (substitute-command-keys
  5922. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  5923. (defun org-force-cycle-archived ()
  5924. "Cycle subtree even if it is archived."
  5925. (interactive)
  5926. (setq this-command 'org-cycle)
  5927. (let ((org-cycle-open-archived-trees t))
  5928. (call-interactively 'org-cycle)))
  5929. (defun org-hide-archived-subtrees (beg end)
  5930. "Re-hide all archived subtrees after a visibility state change."
  5931. (save-excursion
  5932. (let* ((re (concat ":" org-archive-tag ":")))
  5933. (goto-char beg)
  5934. (while (re-search-forward re end t)
  5935. (and (org-on-heading-p) (hide-subtree))
  5936. (org-end-of-subtree t)))))
  5937. (defun org-toggle-tag (tag &optional onoff)
  5938. "Toggle the tag TAG for the current line.
  5939. If ONOFF is `on' or `off', don't toggle but set to this state."
  5940. (unless (org-on-heading-p t) (error "Not on headling"))
  5941. (let (res current)
  5942. (save-excursion
  5943. (beginning-of-line)
  5944. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  5945. (point-at-eol) t)
  5946. (progn
  5947. (setq current (match-string 1))
  5948. (replace-match ""))
  5949. (setq current ""))
  5950. (setq current (nreverse (org-split-string current ":")))
  5951. (cond
  5952. ((eq onoff 'on)
  5953. (setq res t)
  5954. (or (member tag current) (push tag current)))
  5955. ((eq onoff 'off)
  5956. (or (not (member tag current)) (setq current (delete tag current))))
  5957. (t (if (member tag current)
  5958. (setq current (delete tag current))
  5959. (setq res t)
  5960. (push tag current))))
  5961. (end-of-line 1)
  5962. (if current
  5963. (progn
  5964. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  5965. (org-set-tags nil t))
  5966. (delete-horizontal-space))
  5967. (run-hooks 'org-after-tags-change-hook))
  5968. res))
  5969. (defun org-toggle-archive-tag (&optional arg)
  5970. "Toggle the archive tag for the current headline.
  5971. With prefix ARG, check all children of current headline and offer tagging
  5972. the children that do not contain any open TODO items."
  5973. (interactive "P")
  5974. (if arg
  5975. (org-archive-all-done 'tag)
  5976. (let (set)
  5977. (save-excursion
  5978. (org-back-to-heading t)
  5979. (setq set (org-toggle-tag org-archive-tag))
  5980. (when set (hide-subtree)))
  5981. (and set (beginning-of-line 1))
  5982. (message "Subtree %s" (if set "archived" "unarchived")))))
  5983. ;;;; Link Stuff
  5984. ;;; Link abbreviations
  5985. (defun org-link-expand-abbrev (link)
  5986. "Apply replacements as defined in `org-link-abbrev-alist."
  5987. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5988. (let* ((key (match-string 1 link))
  5989. (as (or (assoc key org-link-abbrev-alist-local)
  5990. (assoc key org-link-abbrev-alist)))
  5991. (tag (and (match-end 2) (match-string 3 link)))
  5992. rpl)
  5993. (if (not as)
  5994. link
  5995. (setq rpl (cdr as))
  5996. (cond
  5997. ((symbolp rpl) (funcall rpl tag))
  5998. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5999. (t (concat rpl tag)))))
  6000. link))
  6001. ;;; Storing and inserting links
  6002. (defvar org-insert-link-history nil
  6003. "Minibuffer history for links inserted with `org-insert-link'.")
  6004. (defvar org-stored-links nil
  6005. "Contains the links stored with `org-store-link'.")
  6006. (defvar org-store-link-plist nil
  6007. "Plist with info about the most recently link created with `org-store-link'.")
  6008. (defvar org-link-protocols nil
  6009. "Link protocols added to Org-mode using `org-add-link-type'.")
  6010. (defvar org-store-link-functions nil
  6011. "List of functions that are called to create and store a link.
  6012. Each function will be called in turn until one returns a non-nil
  6013. value. Each function should check if it is responsible for creating
  6014. this link (for example by looking at the major mode).
  6015. If not, it must exit and return nil.
  6016. If yes, it should return a non-nil value after a calling
  6017. `org-store-link-props' with a list of properties and values.
  6018. Special properties are:
  6019. :type The link prefix. like \"http\". This must be given.
  6020. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6021. This is obligatory as well.
  6022. :description Optional default description for the second pair
  6023. of brackets in an Org-mode link. The user can still change
  6024. this when inserting this link into an Org-mode buffer.
  6025. In addition to these, any additional properties can be specified
  6026. and then used in remember templates.")
  6027. (defun org-add-link-type (type &optional follow export)
  6028. "Add TYPE to the list of `org-link-types'.
  6029. Re-compute all regular expressions depending on `org-link-types'
  6030. FOLLOW and EXPORT are two functions.
  6031. FOLLOW should take the link path as the single argument and do whatever
  6032. is necessary to follow the link, for example find a file or display
  6033. a mail message.
  6034. EXPORT should format the link path for export to one of the export formats.
  6035. It should be a function accepting three arguments:
  6036. path the path of the link, the text after the prefix (like \"http:\")
  6037. desc the description of the link, if any, nil if there was no descripton
  6038. format the export format, a symbol like `html' or `latex'.
  6039. The function may use the FORMAT information to return different values
  6040. depending on the format. The return value will be put literally into
  6041. the exported file.
  6042. Org-mode has a built-in default for exporting links. If you are happy with
  6043. this default, there is no need to define an export function for the link
  6044. type. For a simple example of an export function, see `org-bbdb.el'."
  6045. (add-to-list 'org-link-types type t)
  6046. (org-make-link-regexps)
  6047. (if (assoc type org-link-protocols)
  6048. (setcdr (assoc type org-link-protocols) (list follow export))
  6049. (push (list type follow export) org-link-protocols)))
  6050. ;;;###autoload
  6051. (defun org-store-link (arg)
  6052. "\\<org-mode-map>Store an org-link to the current location.
  6053. This link is added to `org-stored-links' and can later be inserted
  6054. into an org-buffer with \\[org-insert-link].
  6055. For some link types, a prefix arg is interpreted:
  6056. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  6057. For file links, arg negates `org-context-in-file-links'."
  6058. (interactive "P")
  6059. (org-load-modules-maybe)
  6060. (setq org-store-link-plist nil) ; reset
  6061. (let (link cpltxt desc description search txt)
  6062. (cond
  6063. ((run-hook-with-args-until-success 'org-store-link-functions)
  6064. (setq link (plist-get org-store-link-plist :link)
  6065. desc (or (plist-get org-store-link-plist :description) link)))
  6066. ((eq major-mode 'calendar-mode)
  6067. (let ((cd (calendar-cursor-to-date)))
  6068. (setq link
  6069. (format-time-string
  6070. (car org-time-stamp-formats)
  6071. (apply 'encode-time
  6072. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6073. nil nil nil))))
  6074. (org-store-link-props :type "calendar" :date cd)))
  6075. ((eq major-mode 'w3-mode)
  6076. (setq cpltxt (url-view-url t)
  6077. link (org-make-link cpltxt))
  6078. (org-store-link-props :type "w3" :url (url-view-url t)))
  6079. ((eq major-mode 'w3m-mode)
  6080. (setq cpltxt (or w3m-current-title w3m-current-url)
  6081. link (org-make-link w3m-current-url))
  6082. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6083. ((setq search (run-hook-with-args-until-success
  6084. 'org-create-file-search-functions))
  6085. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6086. "::" search))
  6087. (setq cpltxt (or description link)))
  6088. ((eq major-mode 'image-mode)
  6089. (setq cpltxt (concat "file:"
  6090. (abbreviate-file-name buffer-file-name))
  6091. link (org-make-link cpltxt))
  6092. (org-store-link-props :type "image" :file buffer-file-name))
  6093. ((eq major-mode 'dired-mode)
  6094. ;; link to the file in the current line
  6095. (setq cpltxt (concat "file:"
  6096. (abbreviate-file-name
  6097. (expand-file-name
  6098. (dired-get-filename nil t))))
  6099. link (org-make-link cpltxt)))
  6100. ((and buffer-file-name (org-mode-p))
  6101. ;; Just link to current headline
  6102. (setq cpltxt (concat "file:"
  6103. (abbreviate-file-name buffer-file-name)))
  6104. ;; Add a context search string
  6105. (when (org-xor org-context-in-file-links arg)
  6106. ;; Check if we are on a target
  6107. (if (org-in-regexp "<<\\(.*?\\)>>")
  6108. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  6109. (setq txt (cond
  6110. ((org-on-heading-p) nil)
  6111. ((org-region-active-p)
  6112. (buffer-substring (region-beginning) (region-end)))
  6113. (t (buffer-substring (point-at-bol) (point-at-eol)))))
  6114. (when (or (null txt) (string-match "\\S-" txt))
  6115. (setq cpltxt
  6116. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6117. desc "NONE"))))
  6118. (if (string-match "::\\'" cpltxt)
  6119. (setq cpltxt (substring cpltxt 0 -2)))
  6120. (setq link (org-make-link cpltxt)))
  6121. ((buffer-file-name (buffer-base-buffer))
  6122. ;; Just link to this file here.
  6123. (setq cpltxt (concat "file:"
  6124. (abbreviate-file-name
  6125. (buffer-file-name (buffer-base-buffer)))))
  6126. ;; Add a context string
  6127. (when (org-xor org-context-in-file-links arg)
  6128. (setq txt (if (org-region-active-p)
  6129. (buffer-substring (region-beginning) (region-end))
  6130. (buffer-substring (point-at-bol) (point-at-eol))))
  6131. ;; Only use search option if there is some text.
  6132. (when (string-match "\\S-" txt)
  6133. (setq cpltxt
  6134. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6135. desc "NONE")))
  6136. (setq link (org-make-link cpltxt)))
  6137. ((interactive-p)
  6138. (error "Cannot link to a buffer which is not visiting a file"))
  6139. (t (setq link nil)))
  6140. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6141. (setq link (or link cpltxt)
  6142. desc (or desc cpltxt))
  6143. (if (equal desc "NONE") (setq desc nil))
  6144. (if (and (interactive-p) link)
  6145. (progn
  6146. (setq org-stored-links
  6147. (cons (list link desc) org-stored-links))
  6148. (message "Stored: %s" (or desc link)))
  6149. (and link (org-make-link-string link desc)))))
  6150. (defun org-store-link-props (&rest plist)
  6151. "Store link properties, extract names and addresses."
  6152. (let (x adr)
  6153. (when (setq x (plist-get plist :from))
  6154. (setq adr (mail-extract-address-components x))
  6155. (plist-put plist :fromname (car adr))
  6156. (plist-put plist :fromaddress (nth 1 adr)))
  6157. (when (setq x (plist-get plist :to))
  6158. (setq adr (mail-extract-address-components x))
  6159. (plist-put plist :toname (car adr))
  6160. (plist-put plist :toaddress (nth 1 adr))))
  6161. (let ((from (plist-get plist :from))
  6162. (to (plist-get plist :to)))
  6163. (when (and from to org-from-is-user-regexp)
  6164. (plist-put plist :fromto
  6165. (if (string-match org-from-is-user-regexp from)
  6166. (concat "to %t")
  6167. (concat "from %f")))))
  6168. (setq org-store-link-plist plist))
  6169. (defun org-add-link-props (&rest plist)
  6170. "Add these properties to the link property list."
  6171. (let (key value)
  6172. (while plist
  6173. (setq key (pop plist) value (pop plist))
  6174. (setq org-store-link-plist
  6175. (plist-put org-store-link-plist key value)))))
  6176. (defun org-email-link-description (&optional fmt)
  6177. "Return the description part of an email link.
  6178. This takes information from `org-store-link-plist' and formats it
  6179. according to FMT (default from `org-email-link-description-format')."
  6180. (setq fmt (or fmt org-email-link-description-format))
  6181. (let* ((p org-store-link-plist)
  6182. (to (plist-get p :toaddress))
  6183. (from (plist-get p :fromaddress))
  6184. (table
  6185. (list
  6186. (cons "%c" (plist-get p :fromto))
  6187. (cons "%F" (plist-get p :from))
  6188. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6189. (cons "%T" (plist-get p :to))
  6190. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6191. (cons "%s" (plist-get p :subject))
  6192. (cons "%m" (plist-get p :message-id)))))
  6193. (when (string-match "%c" fmt)
  6194. ;; Check if the user wrote this message
  6195. (if (and org-from-is-user-regexp from to
  6196. (save-match-data (string-match org-from-is-user-regexp from)))
  6197. (setq fmt (replace-match "to %t" t t fmt))
  6198. (setq fmt (replace-match "from %f" t t fmt))))
  6199. (org-replace-escapes fmt table)))
  6200. (defun org-make-org-heading-search-string (&optional string heading)
  6201. "Make search string for STRING or current headline."
  6202. (interactive)
  6203. (let ((s (or string (org-get-heading))))
  6204. (unless (and string (not heading))
  6205. ;; We are using a headline, clean up garbage in there.
  6206. (if (string-match org-todo-regexp s)
  6207. (setq s (replace-match "" t t s)))
  6208. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6209. (setq s (replace-match "" t t s)))
  6210. (setq s (org-trim s))
  6211. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6212. org-comment-string "\\)") s)
  6213. (setq s (replace-match "" t t s)))
  6214. (while (string-match org-ts-regexp s)
  6215. (setq s (replace-match "" t t s))))
  6216. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6217. (setq s (replace-match " " t t s)))
  6218. (or string (setq s (concat "*" s))) ; Add * for headlines
  6219. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6220. (defun org-make-link (&rest strings)
  6221. "Concatenate STRINGS."
  6222. (apply 'concat strings))
  6223. (defun org-make-link-string (link &optional description)
  6224. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6225. (unless (string-match "\\S-" link)
  6226. (error "Empty link"))
  6227. (when (stringp description)
  6228. ;; Remove brackets from the description, they are fatal.
  6229. (while (string-match "\\[" description)
  6230. (setq description (replace-match "{" t t description)))
  6231. (while (string-match "\\]" description)
  6232. (setq description (replace-match "}" t t description))))
  6233. (when (equal (org-link-escape link) description)
  6234. ;; No description needed, it is identical
  6235. (setq description nil))
  6236. (when (and (not description)
  6237. (not (equal link (org-link-escape link))))
  6238. (setq description link))
  6239. (concat "[[" (org-link-escape link) "]"
  6240. (if description (concat "[" description "]") "")
  6241. "]"))
  6242. (defconst org-link-escape-chars
  6243. '((?\ . "%20")
  6244. (?\[ . "%5B")
  6245. (?\] . "%5D")
  6246. (?\340 . "%E0") ; `a
  6247. (?\342 . "%E2") ; ^a
  6248. (?\347 . "%E7") ; ,c
  6249. (?\350 . "%E8") ; `e
  6250. (?\351 . "%E9") ; 'e
  6251. (?\352 . "%EA") ; ^e
  6252. (?\356 . "%EE") ; ^i
  6253. (?\364 . "%F4") ; ^o
  6254. (?\371 . "%F9") ; `u
  6255. (?\373 . "%FB") ; ^u
  6256. (?\; . "%3B")
  6257. (?? . "%3F")
  6258. (?= . "%3D")
  6259. (?+ . "%2B")
  6260. )
  6261. "Association list of escapes for some characters problematic in links.
  6262. This is the list that is used for internal purposes.")
  6263. (defconst org-link-escape-chars-browser
  6264. '((?\ . "%20")) ; 32 for the SPC char
  6265. "Association list of escapes for some characters problematic in links.
  6266. This is the list that is used before handing over to the browser.")
  6267. (defun org-link-escape (text &optional table)
  6268. "Escape charaters in TEXT that are problematic for links."
  6269. (setq table (or table org-link-escape-chars))
  6270. (when text
  6271. (let ((re (mapconcat (lambda (x) (regexp-quote
  6272. (char-to-string (car x))))
  6273. table "\\|")))
  6274. (while (string-match re text)
  6275. (setq text
  6276. (replace-match
  6277. (cdr (assoc (string-to-char (match-string 0 text))
  6278. table))
  6279. t t text)))
  6280. text)))
  6281. (defun org-link-unescape (text &optional table)
  6282. "Reverse the action of `org-link-escape'."
  6283. (setq table (or table org-link-escape-chars))
  6284. (when text
  6285. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6286. table "\\|")))
  6287. (while (string-match re text)
  6288. (setq text
  6289. (replace-match
  6290. (char-to-string (car (rassoc (match-string 0 text) table)))
  6291. t t text)))
  6292. text)))
  6293. (defun org-xor (a b)
  6294. "Exclusive or."
  6295. (if a (not b) b))
  6296. (defun org-get-header (header)
  6297. "Find a header field in the current buffer."
  6298. (save-excursion
  6299. (goto-char (point-min))
  6300. (let ((case-fold-search t) s)
  6301. (cond
  6302. ((eq header 'from)
  6303. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  6304. (setq s (match-string 1)))
  6305. (while (string-match "\"" s)
  6306. (setq s (replace-match "" t t s)))
  6307. (if (string-match "[<(].*" s)
  6308. (setq s (replace-match "" t t s))))
  6309. ((eq header 'message-id)
  6310. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  6311. (setq s (match-string 1))))
  6312. ((eq header 'subject)
  6313. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  6314. (setq s (match-string 1)))))
  6315. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  6316. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  6317. s)))
  6318. (defun org-fixup-message-id-for-http (s)
  6319. "Replace special characters in a message id, so it can be used in an http query."
  6320. (while (string-match "<" s)
  6321. (setq s (replace-match "%3C" t t s)))
  6322. (while (string-match ">" s)
  6323. (setq s (replace-match "%3E" t t s)))
  6324. (while (string-match "@" s)
  6325. (setq s (replace-match "%40" t t s)))
  6326. s)
  6327. ;;;###autoload
  6328. (defun org-insert-link-global ()
  6329. "Insert a link like Org-mode does.
  6330. This command can be called in any mode to insert a link in Org-mode syntax."
  6331. (interactive)
  6332. (org-load-modules-maybe)
  6333. (org-run-like-in-org-mode 'org-insert-link))
  6334. (defun org-insert-link (&optional complete-file)
  6335. "Insert a link. At the prompt, enter the link.
  6336. Completion can be used to select a link previously stored with
  6337. `org-store-link'. When the empty string is entered (i.e. if you just
  6338. press RET at the prompt), the link defaults to the most recently
  6339. stored link. As SPC triggers completion in the minibuffer, you need to
  6340. use M-SPC or C-q SPC to force the insertion of a space character.
  6341. You will also be prompted for a description, and if one is given, it will
  6342. be displayed in the buffer instead of the link.
  6343. If there is already a link at point, this command will allow you to edit link
  6344. and description parts.
  6345. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
  6346. selected using completion. The path to the file will be relative to
  6347. the current directory if the file is in the current directory or a
  6348. subdirectory. Otherwise, the link will be the absolute path as
  6349. completed in the minibuffer (i.e. normally ~/path/to/file).
  6350. With two \\[universal-argument] prefixes, enforce an absolute path even if the file
  6351. is in the current directory or below.
  6352. With three \\[universal-argument] prefixes, negate the meaning of
  6353. `org-keep-stored-link-after-insertion'."
  6354. (interactive "P")
  6355. (let* ((wcf (current-window-configuration))
  6356. (region (if (org-region-active-p)
  6357. (buffer-substring (region-beginning) (region-end))))
  6358. (remove (and region (list (region-beginning) (region-end))))
  6359. (desc region)
  6360. tmphist ; byte-compile incorrectly complains about this
  6361. link entry file)
  6362. (cond
  6363. ((org-in-regexp org-bracket-link-regexp 1)
  6364. ;; We do have a link at point, and we are going to edit it.
  6365. (setq remove (list (match-beginning 0) (match-end 0)))
  6366. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6367. (setq link (read-string "Link: "
  6368. (org-link-unescape
  6369. (org-match-string-no-properties 1)))))
  6370. ((or (org-in-regexp org-angle-link-re)
  6371. (org-in-regexp org-plain-link-re))
  6372. ;; Convert to bracket link
  6373. (setq remove (list (match-beginning 0) (match-end 0))
  6374. link (read-string "Link: "
  6375. (org-remove-angle-brackets (match-string 0)))))
  6376. ((equal complete-file '(4))
  6377. ;; Completing read for file names.
  6378. (setq file (read-file-name "File: "))
  6379. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6380. (pwd1 (file-name-as-directory (abbreviate-file-name
  6381. (expand-file-name ".")))))
  6382. (cond
  6383. ((equal complete-file '(16))
  6384. (setq link (org-make-link
  6385. "file:"
  6386. (abbreviate-file-name (expand-file-name file)))))
  6387. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6388. (setq link (org-make-link "file:" (match-string 1 file))))
  6389. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6390. (expand-file-name file))
  6391. (setq link (org-make-link
  6392. "file:" (match-string 1 (expand-file-name file)))))
  6393. (t (setq link (org-make-link "file:" file))))))
  6394. (t
  6395. ;; Read link, with completion for stored links.
  6396. (with-output-to-temp-buffer "*Org Links*"
  6397. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6398. (when org-stored-links
  6399. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6400. (princ (mapconcat
  6401. (lambda (x)
  6402. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6403. (reverse org-stored-links) "\n"))))
  6404. (let ((cw (selected-window)))
  6405. (select-window (get-buffer-window "*Org Links*"))
  6406. (shrink-window-if-larger-than-buffer)
  6407. (setq truncate-lines t)
  6408. (select-window cw))
  6409. ;; Fake a link history, containing the stored links.
  6410. (setq tmphist (append (mapcar 'car org-stored-links)
  6411. org-insert-link-history))
  6412. (unwind-protect
  6413. (setq link (org-completing-read
  6414. "Link: "
  6415. (append
  6416. (mapcar (lambda (x) (list (concat (car x) ":")))
  6417. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6418. (mapcar (lambda (x) (list (concat x ":")))
  6419. org-link-types))
  6420. nil nil nil
  6421. 'tmphist
  6422. (or (car (car org-stored-links)))))
  6423. (set-window-configuration wcf)
  6424. (kill-buffer "*Org Links*"))
  6425. (setq entry (assoc link org-stored-links))
  6426. (or entry (push link org-insert-link-history))
  6427. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6428. (not org-keep-stored-link-after-insertion))
  6429. (setq org-stored-links (delq (assoc link org-stored-links)
  6430. org-stored-links)))
  6431. (setq desc (or desc (nth 1 entry)))))
  6432. (if (string-match org-plain-link-re link)
  6433. ;; URL-like link, normalize the use of angular brackets.
  6434. (setq link (org-make-link (org-remove-angle-brackets link))))
  6435. ;; Check if we are linking to the current file with a search option
  6436. ;; If yes, simplify the link by using only the search option.
  6437. (when (and buffer-file-name
  6438. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  6439. (let* ((path (match-string 1 link))
  6440. (case-fold-search nil)
  6441. (search (match-string 2 link)))
  6442. (save-match-data
  6443. (if (equal (file-truename buffer-file-name) (file-truename path))
  6444. ;; We are linking to this same file, with a search option
  6445. (setq link search)))))
  6446. ;; Check if we can/should use a relative path. If yes, simplify the link
  6447. (when (string-match "\\<file:\\(.*\\)" link)
  6448. (let* ((path (match-string 1 link))
  6449. (origpath path)
  6450. (case-fold-search nil))
  6451. (cond
  6452. ((eq org-link-file-path-type 'absolute)
  6453. (setq path (abbreviate-file-name (expand-file-name path))))
  6454. ((eq org-link-file-path-type 'noabbrev)
  6455. (setq path (expand-file-name path)))
  6456. ((eq org-link-file-path-type 'relative)
  6457. (setq path (file-relative-name path)))
  6458. (t
  6459. (save-match-data
  6460. (if (string-match (concat "^" (regexp-quote
  6461. (file-name-as-directory
  6462. (expand-file-name "."))))
  6463. (expand-file-name path))
  6464. ;; We are linking a file with relative path name.
  6465. (setq path (substring (expand-file-name path)
  6466. (match-end 0)))))))
  6467. (setq link (concat "file:" path))
  6468. (if (equal desc origpath)
  6469. (setq desc path))))
  6470. (setq desc (read-string "Description: " desc))
  6471. (unless (string-match "\\S-" desc) (setq desc nil))
  6472. (if remove (apply 'delete-region remove))
  6473. (insert (org-make-link-string link desc))))
  6474. (defun org-completing-read (&rest args)
  6475. (let ((minibuffer-local-completion-map
  6476. (copy-keymap minibuffer-local-completion-map)))
  6477. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6478. (apply 'completing-read args)))
  6479. ;;; Opening/following a link
  6480. (defvar org-link-search-failed nil)
  6481. (defun org-next-link ()
  6482. "Move forward to the next link.
  6483. If the link is in hidden text, expose it."
  6484. (interactive)
  6485. (when (and org-link-search-failed (eq this-command last-command))
  6486. (goto-char (point-min))
  6487. (message "Link search wrapped back to beginning of buffer"))
  6488. (setq org-link-search-failed nil)
  6489. (let* ((pos (point))
  6490. (ct (org-context))
  6491. (a (assoc :link ct)))
  6492. (if a (goto-char (nth 2 a)))
  6493. (if (re-search-forward org-any-link-re nil t)
  6494. (progn
  6495. (goto-char (match-beginning 0))
  6496. (if (org-invisible-p) (org-show-context)))
  6497. (goto-char pos)
  6498. (setq org-link-search-failed t)
  6499. (error "No further link found"))))
  6500. (defun org-previous-link ()
  6501. "Move backward to the previous link.
  6502. If the link is in hidden text, expose it."
  6503. (interactive)
  6504. (when (and org-link-search-failed (eq this-command last-command))
  6505. (goto-char (point-max))
  6506. (message "Link search wrapped back to end of buffer"))
  6507. (setq org-link-search-failed nil)
  6508. (let* ((pos (point))
  6509. (ct (org-context))
  6510. (a (assoc :link ct)))
  6511. (if a (goto-char (nth 1 a)))
  6512. (if (re-search-backward org-any-link-re nil t)
  6513. (progn
  6514. (goto-char (match-beginning 0))
  6515. (if (org-invisible-p) (org-show-context)))
  6516. (goto-char pos)
  6517. (setq org-link-search-failed t)
  6518. (error "No further link found"))))
  6519. (defun org-find-file-at-mouse (ev)
  6520. "Open file link or URL at mouse."
  6521. (interactive "e")
  6522. (mouse-set-point ev)
  6523. (org-open-at-point 'in-emacs))
  6524. (defun org-open-at-mouse (ev)
  6525. "Open file link or URL at mouse."
  6526. (interactive "e")
  6527. (mouse-set-point ev)
  6528. (org-open-at-point))
  6529. (defvar org-window-config-before-follow-link nil
  6530. "The window configuration before following a link.
  6531. This is saved in case the need arises to restore it.")
  6532. (defvar org-open-link-marker (make-marker)
  6533. "Marker pointing to the location where `org-open-at-point; was called.")
  6534. ;;;###autoload
  6535. (defun org-open-at-point-global ()
  6536. "Follow a link like Org-mode does.
  6537. This command can be called in any mode to follow a link that has
  6538. Org-mode syntax."
  6539. (interactive)
  6540. (org-run-like-in-org-mode 'org-open-at-point))
  6541. (defun org-open-at-point (&optional in-emacs)
  6542. "Open link at or after point.
  6543. If there is no link at point, this function will search forward up to
  6544. the end of the current subtree.
  6545. Normally, files will be opened by an appropriate application. If the
  6546. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  6547. (interactive "P")
  6548. (org-load-modules-maybe)
  6549. (move-marker org-open-link-marker (point))
  6550. (setq org-window-config-before-follow-link (current-window-configuration))
  6551. (org-remove-occur-highlights nil nil t)
  6552. (if (org-at-timestamp-p t)
  6553. (org-follow-timestamp-link)
  6554. (let (type path link line search (pos (point)))
  6555. (catch 'match
  6556. (save-excursion
  6557. (skip-chars-forward "^]\n\r")
  6558. (when (org-in-regexp org-bracket-link-regexp)
  6559. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  6560. (while (string-match " *\n *" link)
  6561. (setq link (replace-match " " t t link)))
  6562. (setq link (org-link-expand-abbrev link))
  6563. (if (string-match org-link-re-with-space2 link)
  6564. (setq type (match-string 1 link) path (match-string 2 link))
  6565. (setq type "thisfile" path link))
  6566. (throw 'match t)))
  6567. (when (get-text-property (point) 'org-linked-text)
  6568. (setq type "thisfile"
  6569. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6570. (1+ (point)) (point))
  6571. path (buffer-substring
  6572. (previous-single-property-change pos 'org-linked-text)
  6573. (next-single-property-change pos 'org-linked-text)))
  6574. (throw 'match t))
  6575. (save-excursion
  6576. (when (or (org-in-regexp org-angle-link-re)
  6577. (org-in-regexp org-plain-link-re))
  6578. (setq type (match-string 1) path (match-string 2))
  6579. (throw 'match t)))
  6580. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6581. (setq type "tree-match"
  6582. path (match-string 1))
  6583. (throw 'match t))
  6584. (save-excursion
  6585. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6586. (setq type "tags"
  6587. path (match-string 1))
  6588. (while (string-match ":" path)
  6589. (setq path (replace-match "+" t t path)))
  6590. (throw 'match t))))
  6591. (unless path
  6592. (error "No link found"))
  6593. ;; Remove any trailing spaces in path
  6594. (if (string-match " +\\'" path)
  6595. (setq path (replace-match "" t t path)))
  6596. (cond
  6597. ((assoc type org-link-protocols)
  6598. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6599. ((equal type "mailto")
  6600. (let ((cmd (car org-link-mailto-program))
  6601. (args (cdr org-link-mailto-program)) args1
  6602. (address path) (subject "") a)
  6603. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6604. (setq address (match-string 1 path)
  6605. subject (org-link-escape (match-string 2 path))))
  6606. (while args
  6607. (cond
  6608. ((not (stringp (car args))) (push (pop args) args1))
  6609. (t (setq a (pop args))
  6610. (if (string-match "%a" a)
  6611. (setq a (replace-match address t t a)))
  6612. (if (string-match "%s" a)
  6613. (setq a (replace-match subject t t a)))
  6614. (push a args1))))
  6615. (apply cmd (nreverse args1))))
  6616. ((member type '("http" "https" "ftp" "news"))
  6617. (browse-url (concat type ":" (org-link-escape
  6618. path org-link-escape-chars-browser))))
  6619. ((member type '("message"))
  6620. (browse-url (concat type ":" path)))
  6621. ((string= type "tags")
  6622. (org-tags-view in-emacs path))
  6623. ((string= type "thisfile")
  6624. (if in-emacs
  6625. (switch-to-buffer-other-window
  6626. (org-get-buffer-for-internal-link (current-buffer)))
  6627. (org-mark-ring-push))
  6628. (let ((cmd `(org-link-search
  6629. ,path
  6630. ,(cond ((equal in-emacs '(4)) 'occur)
  6631. ((equal in-emacs '(16)) 'org-occur)
  6632. (t nil))
  6633. ,pos)))
  6634. (condition-case nil (eval cmd)
  6635. (error (progn (widen) (eval cmd))))))
  6636. ((string= type "tree-match")
  6637. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6638. ((string= type "file")
  6639. (if (string-match "::\\([0-9]+\\)\\'" path)
  6640. (setq line (string-to-number (match-string 1 path))
  6641. path (substring path 0 (match-beginning 0)))
  6642. (if (string-match "::\\(.+\\)\\'" path)
  6643. (setq search (match-string 1 path)
  6644. path (substring path 0 (match-beginning 0)))))
  6645. (if (string-match "[*?{]" (file-name-nondirectory path))
  6646. (dired path)
  6647. (org-open-file path in-emacs line search)))
  6648. ((string= type "news")
  6649. (require 'org-gnus)
  6650. (org-gnus-follow-link path))
  6651. ((string= type "shell")
  6652. (let ((cmd path))
  6653. (if (or (not org-confirm-shell-link-function)
  6654. (funcall org-confirm-shell-link-function
  6655. (format "Execute \"%s\" in shell? "
  6656. (org-add-props cmd nil
  6657. 'face 'org-warning))))
  6658. (progn
  6659. (message "Executing %s" cmd)
  6660. (shell-command cmd))
  6661. (error "Abort"))))
  6662. ((string= type "elisp")
  6663. (let ((cmd path))
  6664. (if (or (not org-confirm-elisp-link-function)
  6665. (funcall org-confirm-elisp-link-function
  6666. (format "Execute \"%s\" as elisp? "
  6667. (org-add-props cmd nil
  6668. 'face 'org-warning))))
  6669. (message "%s => %s" cmd (eval (read cmd)))
  6670. (error "Abort"))))
  6671. (t
  6672. (browse-url-at-point)))))
  6673. (move-marker org-open-link-marker nil)
  6674. (run-hook-with-args 'org-follow-link-hook))
  6675. ;;; File search
  6676. (defvar org-create-file-search-functions nil
  6677. "List of functions to construct the right search string for a file link.
  6678. These functions are called in turn with point at the location to
  6679. which the link should point.
  6680. A function in the hook should first test if it would like to
  6681. handle this file type, for example by checking the major-mode or
  6682. the file extension. If it decides not to handle this file, it
  6683. should just return nil to give other functions a chance. If it
  6684. does handle the file, it must return the search string to be used
  6685. when following the link. The search string will be part of the
  6686. file link, given after a double colon, and `org-open-at-point'
  6687. will automatically search for it. If special measures must be
  6688. taken to make the search successful, another function should be
  6689. added to the companion hook `org-execute-file-search-functions',
  6690. which see.
  6691. A function in this hook may also use `setq' to set the variable
  6692. `description' to provide a suggestion for the descriptive text to
  6693. be used for this link when it gets inserted into an Org-mode
  6694. buffer with \\[org-insert-link].")
  6695. (defvar org-execute-file-search-functions nil
  6696. "List of functions to execute a file search triggered by a link.
  6697. Functions added to this hook must accept a single argument, the
  6698. search string that was part of the file link, the part after the
  6699. double colon. The function must first check if it would like to
  6700. handle this search, for example by checking the major-mode or the
  6701. file extension. If it decides not to handle this search, it
  6702. should just return nil to give other functions a chance. If it
  6703. does handle the search, it must return a non-nil value to keep
  6704. other functions from trying.
  6705. Each function can access the current prefix argument through the
  6706. variable `current-prefix-argument'. Note that a single prefix is
  6707. used to force opening a link in Emacs, so it may be good to only
  6708. use a numeric or double prefix to guide the search function.
  6709. In case this is needed, a function in this hook can also restore
  6710. the window configuration before `org-open-at-point' was called using:
  6711. (set-window-configuration org-window-config-before-follow-link)")
  6712. (defun org-link-search (s &optional type avoid-pos)
  6713. "Search for a link search option.
  6714. If S is surrounded by forward slashes, it is interpreted as a
  6715. regular expression. In org-mode files, this will create an `org-occur'
  6716. sparse tree. In ordinary files, `occur' will be used to list matches.
  6717. If the current buffer is in `dired-mode', grep will be used to search
  6718. in all files. If AVOID-POS is given, ignore matches near that position."
  6719. (let ((case-fold-search t)
  6720. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6721. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6722. (append '(("") (" ") ("\t") ("\n"))
  6723. org-emphasis-alist)
  6724. "\\|") "\\)"))
  6725. (pos (point))
  6726. (pre "") (post "")
  6727. words re0 re1 re2 re3 re4 re5 re2a reall)
  6728. (cond
  6729. ;; First check if there are any special
  6730. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6731. ;; Now try the builtin stuff
  6732. ((save-excursion
  6733. (goto-char (point-min))
  6734. (and
  6735. (re-search-forward
  6736. (concat "<<" (regexp-quote s0) ">>") nil t)
  6737. (setq pos (match-beginning 0))))
  6738. ;; There is an exact target for this
  6739. (goto-char pos))
  6740. ((string-match "^/\\(.*\\)/$" s)
  6741. ;; A regular expression
  6742. (cond
  6743. ((org-mode-p)
  6744. (org-occur (match-string 1 s)))
  6745. ;;((eq major-mode 'dired-mode)
  6746. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6747. (t (org-do-occur (match-string 1 s)))))
  6748. (t
  6749. ;; A normal search strings
  6750. (when (equal (string-to-char s) ?*)
  6751. ;; Anchor on headlines, post may include tags.
  6752. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6753. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6754. s (substring s 1)))
  6755. (remove-text-properties
  6756. 0 (length s)
  6757. '(face nil mouse-face nil keymap nil fontified nil) s)
  6758. ;; Make a series of regular expressions to find a match
  6759. (setq words (org-split-string s "[ \n\r\t]+")
  6760. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6761. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6762. "\\)" markers)
  6763. re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6764. re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6765. re1 (concat pre re2 post)
  6766. re3 (concat pre re4 post)
  6767. re5 (concat pre ".*" re4)
  6768. re2 (concat pre re2)
  6769. re2a (concat pre re2a)
  6770. re4 (concat pre re4)
  6771. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6772. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6773. re5 "\\)"
  6774. ))
  6775. (cond
  6776. ((eq type 'org-occur) (org-occur reall))
  6777. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6778. (t (goto-char (point-min))
  6779. (if (or (org-search-not-self 1 re0 nil t)
  6780. (org-search-not-self 1 re1 nil t)
  6781. (org-search-not-self 1 re2 nil t)
  6782. (org-search-not-self 1 re2a nil t)
  6783. (org-search-not-self 1 re3 nil t)
  6784. (org-search-not-self 1 re4 nil t)
  6785. (org-search-not-self 1 re5 nil t)
  6786. )
  6787. (goto-char (match-beginning 1))
  6788. (goto-char pos)
  6789. (error "No match")))))
  6790. (t
  6791. ;; Normal string-search
  6792. (goto-char (point-min))
  6793. (if (search-forward s nil t)
  6794. (goto-char (match-beginning 0))
  6795. (error "No match"))))
  6796. (and (org-mode-p) (org-show-context 'link-search))))
  6797. (defun org-search-not-self (group &rest args)
  6798. "Execute `re-search-forward', but only accept matches that do not
  6799. enclose the position of `org-open-link-marker'."
  6800. (let ((m org-open-link-marker))
  6801. (catch 'exit
  6802. (while (apply 're-search-forward args)
  6803. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6804. (goto-char (match-end group))
  6805. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6806. (> (match-beginning 0) (marker-position m))
  6807. (< (match-end 0) (marker-position m)))
  6808. (save-match-data
  6809. (or (not (org-in-regexp
  6810. org-bracket-link-analytic-regexp 1))
  6811. (not (match-end 4)) ; no description
  6812. (and (<= (match-beginning 4) (point))
  6813. (>= (match-end 4) (point))))))
  6814. (throw 'exit (point))))))))
  6815. (defun org-get-buffer-for-internal-link (buffer)
  6816. "Return a buffer to be used for displaying the link target of internal links."
  6817. (cond
  6818. ((not org-display-internal-link-with-indirect-buffer)
  6819. buffer)
  6820. ((string-match "(Clone)$" (buffer-name buffer))
  6821. (message "Buffer is already a clone, not making another one")
  6822. ;; we also do not modify visibility in this case
  6823. buffer)
  6824. (t ; make a new indirect buffer for displaying the link
  6825. (let* ((bn (buffer-name buffer))
  6826. (ibn (concat bn "(Clone)"))
  6827. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6828. (with-current-buffer ib (org-overview))
  6829. ib))))
  6830. (defun org-do-occur (regexp &optional cleanup)
  6831. "Call the Emacs command `occur'.
  6832. If CLEANUP is non-nil, remove the printout of the regular expression
  6833. in the *Occur* buffer. This is useful if the regex is long and not useful
  6834. to read."
  6835. (occur regexp)
  6836. (when cleanup
  6837. (let ((cwin (selected-window)) win beg end)
  6838. (when (setq win (get-buffer-window "*Occur*"))
  6839. (select-window win))
  6840. (goto-char (point-min))
  6841. (when (re-search-forward "match[a-z]+" nil t)
  6842. (setq beg (match-end 0))
  6843. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6844. (setq end (1- (match-beginning 0)))))
  6845. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6846. (goto-char (point-min))
  6847. (select-window cwin))))
  6848. ;;; The mark ring for links jumps
  6849. (defvar org-mark-ring nil
  6850. "Mark ring for positions before jumps in Org-mode.")
  6851. (defvar org-mark-ring-last-goto nil
  6852. "Last position in the mark ring used to go back.")
  6853. ;; Fill and close the ring
  6854. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6855. (loop for i from 1 to org-mark-ring-length do
  6856. (push (make-marker) org-mark-ring))
  6857. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6858. org-mark-ring)
  6859. (defun org-mark-ring-push (&optional pos buffer)
  6860. "Put the current position or POS into the mark ring and rotate it."
  6861. (interactive)
  6862. (setq pos (or pos (point)))
  6863. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6864. (move-marker (car org-mark-ring)
  6865. (or pos (point))
  6866. (or buffer (current-buffer)))
  6867. (message "%s"
  6868. (substitute-command-keys
  6869. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6870. (defun org-mark-ring-goto (&optional n)
  6871. "Jump to the previous position in the mark ring.
  6872. With prefix arg N, jump back that many stored positions. When
  6873. called several times in succession, walk through the entire ring.
  6874. Org-mode commands jumping to a different position in the current file,
  6875. or to another Org-mode file, automatically push the old position
  6876. onto the ring."
  6877. (interactive "p")
  6878. (let (p m)
  6879. (if (eq last-command this-command)
  6880. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6881. (setq p org-mark-ring))
  6882. (setq org-mark-ring-last-goto p)
  6883. (setq m (car p))
  6884. (switch-to-buffer (marker-buffer m))
  6885. (goto-char m)
  6886. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6887. (defun org-remove-angle-brackets (s)
  6888. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6889. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6890. s)
  6891. (defun org-add-angle-brackets (s)
  6892. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6893. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6894. s)
  6895. ;;; Following specific links
  6896. (defun org-follow-timestamp-link ()
  6897. (cond
  6898. ((org-at-date-range-p t)
  6899. (let ((org-agenda-start-on-weekday)
  6900. (t1 (match-string 1))
  6901. (t2 (match-string 2)))
  6902. (setq t1 (time-to-days (org-time-string-to-time t1))
  6903. t2 (time-to-days (org-time-string-to-time t2)))
  6904. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6905. ((org-at-timestamp-p t)
  6906. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6907. (substring (match-string 1) 0 10)))
  6908. 1))
  6909. (t (error "This should not happen"))))
  6910. ;;; BibTeX links
  6911. ;; Use the custom search meachnism to construct and use search strings for
  6912. ;; file links to BibTeX database entries.
  6913. (defun org-create-file-search-in-bibtex ()
  6914. "Create the search string and description for a BibTeX database entry."
  6915. (when (eq major-mode 'bibtex-mode)
  6916. ;; yes, we want to construct this search string.
  6917. ;; Make a good description for this entry, using names, year and the title
  6918. ;; Put it into the `description' variable which is dynamically scoped.
  6919. (let ((bibtex-autokey-names 1)
  6920. (bibtex-autokey-names-stretch 1)
  6921. (bibtex-autokey-name-case-convert-function 'identity)
  6922. (bibtex-autokey-name-separator " & ")
  6923. (bibtex-autokey-additional-names " et al.")
  6924. (bibtex-autokey-year-length 4)
  6925. (bibtex-autokey-name-year-separator " ")
  6926. (bibtex-autokey-titlewords 3)
  6927. (bibtex-autokey-titleword-separator " ")
  6928. (bibtex-autokey-titleword-case-convert-function 'identity)
  6929. (bibtex-autokey-titleword-length 'infty)
  6930. (bibtex-autokey-year-title-separator ": "))
  6931. (setq description (bibtex-generate-autokey)))
  6932. ;; Now parse the entry, get the key and return it.
  6933. (save-excursion
  6934. (bibtex-beginning-of-entry)
  6935. (cdr (assoc "=key=" (bibtex-parse-entry))))))
  6936. (defun org-execute-file-search-in-bibtex (s)
  6937. "Find the link search string S as a key for a database entry."
  6938. (when (eq major-mode 'bibtex-mode)
  6939. ;; Yes, we want to do the search in this file.
  6940. ;; We construct a regexp that searches for "@entrytype{" followed by the key
  6941. (goto-char (point-min))
  6942. (and (re-search-forward (concat "@[a-zA-Z]+[ \t\n]*{[ \t\n]*"
  6943. (regexp-quote s) "[ \t\n]*,") nil t)
  6944. (goto-char (match-beginning 0)))
  6945. (if (and (match-beginning 0) (equal current-prefix-arg '(16)))
  6946. ;; Use double prefix to indicate that any web link should be browsed
  6947. (let ((b (current-buffer)) (p (point)))
  6948. ;; Restore the window configuration because we just use the web link
  6949. (set-window-configuration org-window-config-before-follow-link)
  6950. (save-excursion (set-buffer b) (goto-char p)
  6951. (bibtex-url)))
  6952. (recenter 0)) ; Move entry start to beginning of window
  6953. ;; return t to indicate that the search is done.
  6954. t))
  6955. ;; Finally add the functions to the right hooks.
  6956. (add-hook 'org-create-file-search-functions 'org-create-file-search-in-bibtex)
  6957. (add-hook 'org-execute-file-search-functions 'org-execute-file-search-in-bibtex)
  6958. ;; end of Bibtex link setup
  6959. ;;; Following file links
  6960. (defun org-open-file (path &optional in-emacs line search)
  6961. "Open the file at PATH.
  6962. First, this expands any special file name abbreviations. Then the
  6963. configuration variable `org-file-apps' is checked if it contains an
  6964. entry for this file type, and if yes, the corresponding command is launched.
  6965. If no application is found, Emacs simply visits the file.
  6966. With optional argument IN-EMACS, Emacs will visit the file.
  6967. Optional LINE specifies a line to go to, optional SEARCH a string to
  6968. search for. If LINE or SEARCH is given, the file will always be
  6969. opened in Emacs.
  6970. If the file does not exist, an error is thrown."
  6971. (setq in-emacs (or in-emacs line search))
  6972. (let* ((file (if (equal path "")
  6973. buffer-file-name
  6974. (substitute-in-file-name (expand-file-name path))))
  6975. (apps (append org-file-apps (org-default-apps)))
  6976. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6977. (dirp (if remp nil (file-directory-p file)))
  6978. (dfile (downcase file))
  6979. (old-buffer (current-buffer))
  6980. (old-pos (point))
  6981. (old-mode major-mode)
  6982. ext cmd)
  6983. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6984. (setq ext (match-string 1 dfile))
  6985. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6986. (setq ext (match-string 1 dfile))))
  6987. (if in-emacs
  6988. (setq cmd 'emacs)
  6989. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6990. (and dirp (cdr (assoc 'directory apps)))
  6991. (cdr (assoc ext apps))
  6992. (cdr (assoc t apps)))))
  6993. (when (eq cmd 'mailcap)
  6994. (require 'mailcap)
  6995. (mailcap-parse-mailcaps)
  6996. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6997. (command (mailcap-mime-info mime-type)))
  6998. (if (stringp command)
  6999. (setq cmd command)
  7000. (setq cmd 'emacs))))
  7001. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  7002. (not (file-exists-p file))
  7003. (not org-open-non-existing-files))
  7004. (error "No such file: %s" file))
  7005. (cond
  7006. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7007. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  7008. (while (string-match "['\"]%s['\"]" cmd)
  7009. (setq cmd (replace-match "%s" t t cmd)))
  7010. (while (string-match "%s" cmd)
  7011. (setq cmd (replace-match
  7012. (save-match-data (shell-quote-argument file))
  7013. t t cmd)))
  7014. (save-window-excursion
  7015. (start-process-shell-command cmd nil cmd)))
  7016. ((or (stringp cmd)
  7017. (eq cmd 'emacs))
  7018. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7019. (widen)
  7020. (if line (goto-line line)
  7021. (if search (org-link-search search))))
  7022. ((consp cmd)
  7023. (eval cmd))
  7024. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7025. (and (org-mode-p) (eq old-mode 'org-mode)
  7026. (or (not (equal old-buffer (current-buffer)))
  7027. (not (equal old-pos (point))))
  7028. (org-mark-ring-push old-pos old-buffer))))
  7029. (defun org-default-apps ()
  7030. "Return the default applications for this operating system."
  7031. (cond
  7032. ((eq system-type 'darwin)
  7033. org-file-apps-defaults-macosx)
  7034. ((eq system-type 'windows-nt)
  7035. org-file-apps-defaults-windowsnt)
  7036. (t org-file-apps-defaults-gnu)))
  7037. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7038. (defun org-file-remote-p (file)
  7039. "Test whether FILE specifies a location on a remote system.
  7040. Return non-nil if the location is indeed remote.
  7041. For example, the filename \"/user@host:/foo\" specifies a location
  7042. on the system \"/user@host:\"."
  7043. (cond ((fboundp 'file-remote-p)
  7044. (file-remote-p file))
  7045. ((fboundp 'tramp-handle-file-remote-p)
  7046. (tramp-handle-file-remote-p file))
  7047. ((and (boundp 'ange-ftp-name-format)
  7048. (string-match (car ange-ftp-name-format) file))
  7049. t)
  7050. (t nil)))
  7051. ;;;; Hooks for remember.el, and refiling
  7052. (defun org-get-org-file ()
  7053. "Read a filename, with default directory `org-directory'."
  7054. (let ((default (or org-default-notes-file remember-data-file)))
  7055. (read-file-name (format "File name [%s]: " default)
  7056. (file-name-as-directory org-directory)
  7057. default)))
  7058. (defun org-notes-order-reversed-p ()
  7059. "Check if the current file should receive notes in reversed order."
  7060. (cond
  7061. ((not org-reverse-note-order) nil)
  7062. ((eq t org-reverse-note-order) t)
  7063. ((not (listp org-reverse-note-order)) nil)
  7064. (t (catch 'exit
  7065. (let ((all org-reverse-note-order)
  7066. entry)
  7067. (while (setq entry (pop all))
  7068. (if (string-match (car entry) buffer-file-name)
  7069. (throw 'exit (cdr entry))))
  7070. nil)))))
  7071. ;;;; Refiling
  7072. (defvar org-refile-target-table nil
  7073. "The list of refile targets, created by `org-refile'.")
  7074. (defvar org-agenda-new-buffers nil
  7075. "Buffers created to visit agenda files.")
  7076. (defun org-get-refile-targets (&optional default-buffer)
  7077. "Produce a table with refile targets."
  7078. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  7079. targets txt re files f desc descre)
  7080. (with-current-buffer (or default-buffer (current-buffer))
  7081. (while (setq entry (pop entries))
  7082. (setq files (car entry) desc (cdr entry))
  7083. (cond
  7084. ((null files) (setq files (list (current-buffer))))
  7085. ((eq files 'org-agenda-files)
  7086. (setq files (org-agenda-files 'unrestricted)))
  7087. ((and (symbolp files) (fboundp files))
  7088. (setq files (funcall files)))
  7089. ((and (symbolp files) (boundp files))
  7090. (setq files (symbol-value files))))
  7091. (if (stringp files) (setq files (list files)))
  7092. (cond
  7093. ((eq (car desc) :tag)
  7094. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7095. ((eq (car desc) :todo)
  7096. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7097. ((eq (car desc) :regexp)
  7098. (setq descre (cdr desc)))
  7099. ((eq (car desc) :level)
  7100. (setq descre (concat "^\\*\\{" (number-to-string
  7101. (if org-odd-levels-only
  7102. (1- (* 2 (cdr desc)))
  7103. (cdr desc)))
  7104. "\\}[ \t]")))
  7105. ((eq (car desc) :maxlevel)
  7106. (setq descre (concat "^\\*\\{1," (number-to-string
  7107. (if org-odd-levels-only
  7108. (1- (* 2 (cdr desc)))
  7109. (cdr desc)))
  7110. "\\}[ \t]")))
  7111. (t (error "Bad refiling target description %s" desc)))
  7112. (while (setq f (pop files))
  7113. (save-excursion
  7114. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7115. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7116. (save-excursion
  7117. (save-restriction
  7118. (widen)
  7119. (goto-char (point-min))
  7120. (while (re-search-forward descre nil t)
  7121. (goto-char (point-at-bol))
  7122. (when (looking-at org-complex-heading-regexp)
  7123. (setq txt (match-string 4)
  7124. re (concat "^" (regexp-quote
  7125. (buffer-substring (match-beginning 1)
  7126. (match-end 4)))))
  7127. (if (match-end 5) (setq re (concat re "[ \t]+"
  7128. (regexp-quote
  7129. (match-string 5)))))
  7130. (setq re (concat re "[ \t]*$"))
  7131. (when org-refile-use-outline-path
  7132. (setq txt (mapconcat 'identity
  7133. (append
  7134. (if (eq org-refile-use-outline-path 'file)
  7135. (list (file-name-nondirectory
  7136. (buffer-file-name (buffer-base-buffer))))
  7137. (if (eq org-refile-use-outline-path 'full-file-path)
  7138. (list (buffer-file-name (buffer-base-buffer)))))
  7139. (org-get-outline-path)
  7140. (list txt))
  7141. "/")))
  7142. (push (list txt f re (point)) targets))
  7143. (goto-char (point-at-eol))))))))
  7144. (nreverse targets))))
  7145. (defun org-get-outline-path ()
  7146. "Return the outline path to the current entry, as a list."
  7147. (let (rtn)
  7148. (save-excursion
  7149. (while (org-up-heading-safe)
  7150. (when (looking-at org-complex-heading-regexp)
  7151. (push (org-match-string-no-properties 4) rtn)))
  7152. rtn)))
  7153. (defvar org-refile-history nil
  7154. "History for refiling operations.")
  7155. (defun org-refile (&optional goto default-buffer)
  7156. "Move the entry at point to another heading.
  7157. The list of target headings is compiled using the information in
  7158. `org-refile-targets', which see. This list is created upon first use, and
  7159. you can update it by calling this command with a double prefix (`C-u C-u').
  7160. FIXME: Can we find a better way of updating?
  7161. At the target location, the entry is filed as a subitem of the target heading.
  7162. Depending on `org-reverse-note-order', the new subitem will either be the
  7163. first of the last subitem.
  7164. With prefix arg GOTO, the command will only visit the target location,
  7165. not actually move anything.
  7166. With a double prefix `C-c C-c', go to the location where the last refiling
  7167. operation has put the subtree.
  7168. With a double prefix argument, the command can be used to jump to any
  7169. heading in the current buffer."
  7170. (interactive "P")
  7171. (let* ((cbuf (current-buffer))
  7172. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7173. pos it nbuf file re level reversed)
  7174. (if (equal goto '(16))
  7175. (org-refile-goto-last-stored)
  7176. (when (setq it (org-refile-get-location
  7177. (if goto "Goto: " "Refile to: ") default-buffer))
  7178. (setq file (nth 1 it)
  7179. re (nth 2 it)
  7180. pos (nth 3 it))
  7181. (setq nbuf (or (find-buffer-visiting file)
  7182. (find-file-noselect file)))
  7183. (if goto
  7184. (progn
  7185. (switch-to-buffer nbuf)
  7186. (goto-char pos)
  7187. (org-show-context 'org-goto))
  7188. (org-copy-special)
  7189. (save-excursion
  7190. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7191. (find-file-noselect file))))
  7192. (setq reversed (org-notes-order-reversed-p))
  7193. (save-excursion
  7194. (save-restriction
  7195. (widen)
  7196. (goto-char pos)
  7197. (looking-at outline-regexp)
  7198. (setq level (org-get-valid-level (funcall outline-level) 1))
  7199. (goto-char
  7200. (if reversed
  7201. (outline-next-heading)
  7202. (or (save-excursion (outline-get-next-sibling))
  7203. (org-end-of-subtree t t)
  7204. (point-max))))
  7205. (bookmark-set "org-refile-last-stored")
  7206. (org-paste-subtree level))))
  7207. (org-cut-special)
  7208. (message "Entry refiled to \"%s\"" (car it)))))))
  7209. (defun org-refile-goto-last-stored ()
  7210. "Go to the location where the last refile was stored."
  7211. (interactive)
  7212. (bookmark-jump "org-refile-last-stored")
  7213. (message "This is the location of the last refile"))
  7214. (defun org-refile-get-location (&optional prompt default-buffer)
  7215. "Prompt the user for a refile location, using PROMPT."
  7216. (let ((org-refile-targets org-refile-targets)
  7217. (org-refile-use-outline-path org-refile-use-outline-path))
  7218. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7219. (unless org-refile-target-table
  7220. (error "No refile targets"))
  7221. (let* ((cbuf (current-buffer))
  7222. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7223. (fname (and filename (file-truename filename)))
  7224. (tbl (mapcar
  7225. (lambda (x)
  7226. (if (not (equal fname (file-truename (nth 1 x))))
  7227. (cons (concat (car x) " (" (file-name-nondirectory
  7228. (nth 1 x)) ")")
  7229. (cdr x))
  7230. x))
  7231. org-refile-target-table))
  7232. (completion-ignore-case t))
  7233. (assoc (completing-read prompt tbl nil t nil 'org-refile-history)
  7234. tbl)))
  7235. ;;;; Dynamic blocks
  7236. (defun org-find-dblock (name)
  7237. "Find the first dynamic block with name NAME in the buffer.
  7238. If not found, stay at current position and return nil."
  7239. (let (pos)
  7240. (save-excursion
  7241. (goto-char (point-min))
  7242. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7243. nil t)
  7244. (match-beginning 0))))
  7245. (if pos (goto-char pos))
  7246. pos))
  7247. (defconst org-dblock-start-re
  7248. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7249. "Matches the startline of a dynamic block, with parameters.")
  7250. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7251. "Matches the end of a dyhamic block.")
  7252. (defun org-create-dblock (plist)
  7253. "Create a dynamic block section, with parameters taken from PLIST.
  7254. PLIST must containe a :name entry which is used as name of the block."
  7255. (unless (bolp) (newline))
  7256. (let ((name (plist-get plist :name)))
  7257. (insert "#+BEGIN: " name)
  7258. (while plist
  7259. (if (eq (car plist) :name)
  7260. (setq plist (cddr plist))
  7261. (insert " " (prin1-to-string (pop plist)))))
  7262. (insert "\n\n#+END:\n")
  7263. (beginning-of-line -2)))
  7264. (defun org-prepare-dblock ()
  7265. "Prepare dynamic block for refresh.
  7266. This empties the block, puts the cursor at the insert position and returns
  7267. the property list including an extra property :name with the block name."
  7268. (unless (looking-at org-dblock-start-re)
  7269. (error "Not at a dynamic block"))
  7270. (let* ((begdel (1+ (match-end 0)))
  7271. (name (org-no-properties (match-string 1)))
  7272. (params (append (list :name name)
  7273. (read (concat "(" (match-string 3) ")")))))
  7274. (unless (re-search-forward org-dblock-end-re nil t)
  7275. (error "Dynamic block not terminated"))
  7276. (setq params
  7277. (append params
  7278. (list :content (buffer-substring
  7279. begdel (match-beginning 0)))))
  7280. (delete-region begdel (match-beginning 0))
  7281. (goto-char begdel)
  7282. (open-line 1)
  7283. params))
  7284. (defun org-map-dblocks (&optional command)
  7285. "Apply COMMAND to all dynamic blocks in the current buffer.
  7286. If COMMAND is not given, use `org-update-dblock'."
  7287. (let ((cmd (or command 'org-update-dblock))
  7288. pos)
  7289. (save-excursion
  7290. (goto-char (point-min))
  7291. (while (re-search-forward org-dblock-start-re nil t)
  7292. (goto-char (setq pos (match-beginning 0)))
  7293. (condition-case nil
  7294. (funcall cmd)
  7295. (error (message "Error during update of dynamic block")))
  7296. (goto-char pos)
  7297. (unless (re-search-forward org-dblock-end-re nil t)
  7298. (error "Dynamic block not terminated"))))))
  7299. (defun org-dblock-update (&optional arg)
  7300. "User command for updating dynamic blocks.
  7301. Update the dynamic block at point. With prefix ARG, update all dynamic
  7302. blocks in the buffer."
  7303. (interactive "P")
  7304. (if arg
  7305. (org-update-all-dblocks)
  7306. (or (looking-at org-dblock-start-re)
  7307. (org-beginning-of-dblock))
  7308. (org-update-dblock)))
  7309. (defun org-update-dblock ()
  7310. "Update the dynamic block at point
  7311. This means to empty the block, parse for parameters and then call
  7312. the correct writing function."
  7313. (save-window-excursion
  7314. (let* ((pos (point))
  7315. (line (org-current-line))
  7316. (params (org-prepare-dblock))
  7317. (name (plist-get params :name))
  7318. (cmd (intern (concat "org-dblock-write:" name))))
  7319. (message "Updating dynamic block `%s' at line %d..." name line)
  7320. (funcall cmd params)
  7321. (message "Updating dynamic block `%s' at line %d...done" name line)
  7322. (goto-char pos))))
  7323. (defun org-beginning-of-dblock ()
  7324. "Find the beginning of the dynamic block at point.
  7325. Error if there is no scuh block at point."
  7326. (let ((pos (point))
  7327. beg)
  7328. (end-of-line 1)
  7329. (if (and (re-search-backward org-dblock-start-re nil t)
  7330. (setq beg (match-beginning 0))
  7331. (re-search-forward org-dblock-end-re nil t)
  7332. (> (match-end 0) pos))
  7333. (goto-char beg)
  7334. (goto-char pos)
  7335. (error "Not in a dynamic block"))))
  7336. (defun org-update-all-dblocks ()
  7337. "Update all dynamic blocks in the buffer.
  7338. This function can be used in a hook."
  7339. (when (org-mode-p)
  7340. (org-map-dblocks 'org-update-dblock)))
  7341. ;;;; Completion
  7342. (defconst org-additional-option-like-keywords
  7343. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7344. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7345. "BEGIN_EXAMPLE" "END_EXAMPLE"))
  7346. (defun org-complete (&optional arg)
  7347. "Perform completion on word at point.
  7348. At the beginning of a headline, this completes TODO keywords as given in
  7349. `org-todo-keywords'.
  7350. If the current word is preceded by a backslash, completes the TeX symbols
  7351. that are supported for HTML support.
  7352. If the current word is preceded by \"#+\", completes special words for
  7353. setting file options.
  7354. In the line after \"#+STARTUP:, complete valid keywords.\"
  7355. At all other locations, this simply calls the value of
  7356. `org-completion-fallback-command'."
  7357. (interactive "P")
  7358. (org-without-partial-completion
  7359. (catch 'exit
  7360. (let* ((end (point))
  7361. (beg1 (save-excursion
  7362. (skip-chars-backward (org-re "[:alnum:]_@"))
  7363. (point)))
  7364. (beg (save-excursion
  7365. (skip-chars-backward "a-zA-Z0-9_:$")
  7366. (point)))
  7367. (confirm (lambda (x) (stringp (car x))))
  7368. (searchhead (equal (char-before beg) ?*))
  7369. (tag (and (equal (char-before beg1) ?:)
  7370. (equal (char-after (point-at-bol)) ?*)))
  7371. (prop (and (equal (char-before beg1) ?:)
  7372. (not (equal (char-after (point-at-bol)) ?*))))
  7373. (texp (equal (char-before beg) ?\\))
  7374. (link (equal (char-before beg) ?\[))
  7375. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7376. beg)
  7377. "#+"))
  7378. (startup (string-match "^#\\+STARTUP:.*"
  7379. (buffer-substring (point-at-bol) (point))))
  7380. (completion-ignore-case opt)
  7381. (type nil)
  7382. (tbl nil)
  7383. (table (cond
  7384. (opt
  7385. (setq type :opt)
  7386. (require 'org-exp)
  7387. (append
  7388. (mapcar
  7389. (lambda (x)
  7390. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7391. (cons (match-string 2 x) (match-string 1 x)))
  7392. (org-split-string (org-get-current-options) "\n"))
  7393. (mapcar 'list org-additional-option-like-keywords)))
  7394. (startup
  7395. (setq type :startup)
  7396. org-startup-options)
  7397. (link (append org-link-abbrev-alist-local
  7398. org-link-abbrev-alist))
  7399. (texp
  7400. (setq type :tex)
  7401. org-html-entities)
  7402. ((string-match "\\`\\*+[ \t]+\\'"
  7403. (buffer-substring (point-at-bol) beg))
  7404. (setq type :todo)
  7405. (mapcar 'list org-todo-keywords-1))
  7406. (searchhead
  7407. (setq type :searchhead)
  7408. (save-excursion
  7409. (goto-char (point-min))
  7410. (while (re-search-forward org-todo-line-regexp nil t)
  7411. (push (list
  7412. (org-make-org-heading-search-string
  7413. (match-string 3) t))
  7414. tbl)))
  7415. tbl)
  7416. (tag (setq type :tag beg beg1)
  7417. (or org-tag-alist (org-get-buffer-tags)))
  7418. (prop (setq type :prop beg beg1)
  7419. (mapcar 'list (org-buffer-property-keys nil t t)))
  7420. (t (progn
  7421. (call-interactively org-completion-fallback-command)
  7422. (throw 'exit nil)))))
  7423. (pattern (buffer-substring-no-properties beg end))
  7424. (completion (try-completion pattern table confirm)))
  7425. (cond ((eq completion t)
  7426. (if (not (assoc (upcase pattern) table))
  7427. (message "Already complete")
  7428. (if (and (equal type :opt)
  7429. (not (member (car (assoc (upcase pattern) table))
  7430. org-additional-option-like-keywords)))
  7431. (insert (substring (cdr (assoc (upcase pattern) table))
  7432. (length pattern)))
  7433. (if (memq type '(:tag :prop)) (insert ":")))))
  7434. ((null completion)
  7435. (message "Can't find completion for \"%s\"" pattern)
  7436. (ding))
  7437. ((not (string= pattern completion))
  7438. (delete-region beg end)
  7439. (if (string-match " +$" completion)
  7440. (setq completion (replace-match "" t t completion)))
  7441. (insert completion)
  7442. (if (get-buffer-window "*Completions*")
  7443. (delete-window (get-buffer-window "*Completions*")))
  7444. (if (assoc completion table)
  7445. (if (eq type :todo) (insert " ")
  7446. (if (memq type '(:tag :prop)) (insert ":"))))
  7447. (if (and (equal type :opt) (assoc completion table))
  7448. (message "%s" (substitute-command-keys
  7449. "Press \\[org-complete] again to insert example settings"))))
  7450. (t
  7451. (message "Making completion list...")
  7452. (let ((list (sort (all-completions pattern table confirm)
  7453. 'string<)))
  7454. (with-output-to-temp-buffer "*Completions*"
  7455. (condition-case nil
  7456. ;; Protection needed for XEmacs and emacs 21
  7457. (display-completion-list list pattern)
  7458. (error (display-completion-list list)))))
  7459. (message "Making completion list...%s" "done")))))))
  7460. ;;;; TODO, DEADLINE, Comments
  7461. (defun org-toggle-comment ()
  7462. "Change the COMMENT state of an entry."
  7463. (interactive)
  7464. (save-excursion
  7465. (org-back-to-heading)
  7466. (let (case-fold-search)
  7467. (if (looking-at (concat outline-regexp
  7468. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7469. (replace-match "" t t nil 1)
  7470. (if (looking-at outline-regexp)
  7471. (progn
  7472. (goto-char (match-end 0))
  7473. (insert org-comment-string " ")))))))
  7474. (defvar org-last-todo-state-is-todo nil
  7475. "This is non-nil when the last TODO state change led to a TODO state.
  7476. If the last change removed the TODO tag or switched to DONE, then
  7477. this is nil.")
  7478. (defvar org-setting-tags nil) ; dynamically skiped
  7479. ;; FIXME: better place
  7480. (defun org-property-or-variable-value (var &optional inherit)
  7481. "Check if there is a property fixing the value of VAR.
  7482. If yes, return this value. If not, return the current value of the variable."
  7483. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  7484. (if (and prop (stringp prop) (string-match "\\S-" prop))
  7485. (read prop)
  7486. (symbol-value var))))
  7487. (defun org-parse-local-options (string var)
  7488. "Parse STRING for startup setting relevant for variable VAR."
  7489. (let ((rtn (symbol-value var))
  7490. e opts)
  7491. (save-match-data
  7492. (if (or (not string) (not (string-match "\\S-" string)))
  7493. rtn
  7494. (setq opts (delq nil (mapcar (lambda (x)
  7495. (setq e (assoc x org-startup-options))
  7496. (if (eq (nth 1 e) var) e nil))
  7497. (org-split-string string "[ \t]+"))))
  7498. (if (not opts)
  7499. rtn
  7500. (setq rtn nil)
  7501. (while (setq e (pop opts))
  7502. (if (not (nth 3 e))
  7503. (setq rtn (nth 2 e))
  7504. (if (not (listp rtn)) (setq rtn nil))
  7505. (push (nth 2 e) rtn)))
  7506. rtn)))))
  7507. (defvar org-blocker-hook nil
  7508. "Hook for functions that are allowed to block a state change.
  7509. Each function gets as its single argument a property list, see
  7510. `org-trigger-hook' for more information about this list.
  7511. If any of the functions in this hook returns nil, the state change
  7512. is blocked.")
  7513. (defvar org-trigger-hook nil
  7514. "Hook for functions that are triggered by a state change.
  7515. Each function gets as its single argument a property list with at least
  7516. the following elements:
  7517. (:type type-of-change :position pos-at-entry-start
  7518. :from old-state :to new-state)
  7519. Depending on the type, more properties may be present.
  7520. This mechanism is currently implemented for:
  7521. TODO state changes
  7522. ------------------
  7523. :type todo-state-change
  7524. :from previous state (keyword as a string), or nil
  7525. :to new state (keyword as a string), or nil")
  7526. (defun org-todo (&optional arg)
  7527. "Change the TODO state of an item.
  7528. The state of an item is given by a keyword at the start of the heading,
  7529. like
  7530. *** TODO Write paper
  7531. *** DONE Call mom
  7532. The different keywords are specified in the variable `org-todo-keywords'.
  7533. By default the available states are \"TODO\" and \"DONE\".
  7534. So for this example: when the item starts with TODO, it is changed to DONE.
  7535. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7536. DONE are present, add TODO at the beginning of the heading.
  7537. With C-u prefix arg, use completion to determine the new state.
  7538. With numeric prefix arg, switch to that state.
  7539. For calling through lisp, arg is also interpreted in the following way:
  7540. 'none -> empty state
  7541. \"\"(empty string) -> switch to empty state
  7542. 'done -> switch to DONE
  7543. 'nextset -> switch to the next set of keywords
  7544. 'previousset -> switch to the previous set of keywords
  7545. \"WAITING\" -> switch to the specified keyword, but only if it
  7546. really is a member of `org-todo-keywords'."
  7547. (interactive "P")
  7548. (save-excursion
  7549. (catch 'exit
  7550. (org-back-to-heading)
  7551. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7552. (or (looking-at (concat " +" org-todo-regexp " *"))
  7553. (looking-at " *"))
  7554. (let* ((match-data (match-data))
  7555. (startpos (point-at-bol))
  7556. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7557. (org-log-done org-log-done)
  7558. (org-log-repeat org-log-repeat)
  7559. (org-todo-log-states org-todo-log-states)
  7560. (this (match-string 1))
  7561. (hl-pos (match-beginning 0))
  7562. (head (org-get-todo-sequence-head this))
  7563. (ass (assoc head org-todo-kwd-alist))
  7564. (interpret (nth 1 ass))
  7565. (done-word (nth 3 ass))
  7566. (final-done-word (nth 4 ass))
  7567. (last-state (or this ""))
  7568. (completion-ignore-case t)
  7569. (member (member this org-todo-keywords-1))
  7570. (tail (cdr member))
  7571. (state (cond
  7572. ((and org-todo-key-trigger
  7573. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  7574. (and (not arg) org-use-fast-todo-selection
  7575. (not (eq org-use-fast-todo-selection 'prefix)))))
  7576. ;; Use fast selection
  7577. (org-fast-todo-selection))
  7578. ((and (equal arg '(4))
  7579. (or (not org-use-fast-todo-selection)
  7580. (not org-todo-key-trigger)))
  7581. ;; Read a state with completion
  7582. (completing-read "State: " (mapcar (lambda(x) (list x))
  7583. org-todo-keywords-1)
  7584. nil t))
  7585. ((eq arg 'right)
  7586. (if this
  7587. (if tail (car tail) nil)
  7588. (car org-todo-keywords-1)))
  7589. ((eq arg 'left)
  7590. (if (equal member org-todo-keywords-1)
  7591. nil
  7592. (if this
  7593. (nth (- (length org-todo-keywords-1) (length tail) 2)
  7594. org-todo-keywords-1)
  7595. (org-last org-todo-keywords-1))))
  7596. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7597. (setq arg nil))) ; hack to fall back to cycling
  7598. (arg
  7599. ;; user or caller requests a specific state
  7600. (cond
  7601. ((equal arg "") nil)
  7602. ((eq arg 'none) nil)
  7603. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7604. ((eq arg 'nextset)
  7605. (or (car (cdr (member head org-todo-heads)))
  7606. (car org-todo-heads)))
  7607. ((eq arg 'previousset)
  7608. (let ((org-todo-heads (reverse org-todo-heads)))
  7609. (or (car (cdr (member head org-todo-heads)))
  7610. (car org-todo-heads))))
  7611. ((car (member arg org-todo-keywords-1)))
  7612. ((nth (1- (prefix-numeric-value arg))
  7613. org-todo-keywords-1))))
  7614. ((null member) (or head (car org-todo-keywords-1)))
  7615. ((equal this final-done-word) nil) ;; -> make empty
  7616. ((null tail) nil) ;; -> first entry
  7617. ((eq interpret 'sequence)
  7618. (car tail))
  7619. ((memq interpret '(type priority))
  7620. (if (eq this-command last-command)
  7621. (car tail)
  7622. (if (> (length tail) 0)
  7623. (or done-word (car org-done-keywords))
  7624. nil)))
  7625. (t nil)))
  7626. (next (if state (concat " " state " ") " "))
  7627. (change-plist (list :type 'todo-state-change :from this :to state
  7628. :position startpos))
  7629. dolog now-done-p)
  7630. (when org-blocker-hook
  7631. (unless (save-excursion
  7632. (save-match-data
  7633. (run-hook-with-args-until-failure
  7634. 'org-blocker-hook change-plist)))
  7635. (if (interactive-p)
  7636. (error "TODO state change from %s to %s blocked" this state)
  7637. ;; fail silently
  7638. (message "TODO state change from %s to %s blocked" this state)
  7639. (throw 'exit nil))))
  7640. (store-match-data match-data)
  7641. (replace-match next t t)
  7642. (unless (pos-visible-in-window-p hl-pos)
  7643. (message "TODO state changed to %s" (org-trim next)))
  7644. (unless head
  7645. (setq head (org-get-todo-sequence-head state)
  7646. ass (assoc head org-todo-kwd-alist)
  7647. interpret (nth 1 ass)
  7648. done-word (nth 3 ass)
  7649. final-done-word (nth 4 ass)))
  7650. (when (memq arg '(nextset previousset))
  7651. (message "Keyword-Set %d/%d: %s"
  7652. (- (length org-todo-sets) -1
  7653. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7654. (length org-todo-sets)
  7655. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7656. (setq org-last-todo-state-is-todo
  7657. (not (member state org-done-keywords)))
  7658. (setq now-done-p (and (member state org-done-keywords)
  7659. (not (member this org-done-keywords))))
  7660. (and logging (org-local-logging logging))
  7661. (when (and (or org-todo-log-states org-log-done)
  7662. (not (memq arg '(nextset previousset))))
  7663. ;; we need to look at recording a time and note
  7664. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7665. (nth 2 (assoc this org-todo-log-states))))
  7666. (when (and state
  7667. (member state org-not-done-keywords)
  7668. (not (member this org-not-done-keywords)))
  7669. ;; This is now a todo state and was not one before
  7670. ;; If there was a CLOSED time stamp, get rid of it.
  7671. (org-add-planning-info nil nil 'closed))
  7672. (when (and now-done-p org-log-done)
  7673. ;; It is now done, and it was not done before
  7674. (org-add-planning-info 'closed (org-current-time))
  7675. (if (and (not dolog) (eq 'note org-log-done))
  7676. (org-add-log-maybe 'done state 'findpos 'note)))
  7677. (when (and state dolog)
  7678. ;; This is a non-nil state, and we need to log it
  7679. (org-add-log-maybe 'state state 'findpos dolog)))
  7680. ;; Fixup tag positioning
  7681. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7682. (run-hooks 'org-after-todo-state-change-hook)
  7683. (if (and arg (not (member state org-done-keywords)))
  7684. (setq head (org-get-todo-sequence-head state)))
  7685. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7686. ;; Do we need to trigger a repeat?
  7687. (when now-done-p (org-auto-repeat-maybe state))
  7688. ;; Fixup cursor location if close to the keyword
  7689. (if (and (outline-on-heading-p)
  7690. (not (bolp))
  7691. (save-excursion (beginning-of-line 1)
  7692. (looking-at org-todo-line-regexp))
  7693. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7694. (progn
  7695. (goto-char (or (match-end 2) (match-end 1)))
  7696. (just-one-space)))
  7697. (when org-trigger-hook
  7698. (save-excursion
  7699. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  7700. (defun org-local-logging (value)
  7701. "Get logging settings from a property VALUE."
  7702. (let* (words w a)
  7703. ;; directly set the variables, they are already local.
  7704. (setq org-log-done nil
  7705. org-log-repeat nil
  7706. org-todo-log-states nil)
  7707. (setq words (org-split-string value))
  7708. (while (setq w (pop words))
  7709. (cond
  7710. ((setq a (assoc w org-startup-options))
  7711. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7712. (set (nth 1 a) (nth 2 a))))
  7713. ((setq a (org-extract-log-state-settings w))
  7714. (and (member (car a) org-todo-keywords-1)
  7715. (push a org-todo-log-states)))))))
  7716. (defun org-get-todo-sequence-head (kwd)
  7717. "Return the head of the TODO sequence to which KWD belongs.
  7718. If KWD is not set, check if there is a text property remembering the
  7719. right sequence."
  7720. (let (p)
  7721. (cond
  7722. ((not kwd)
  7723. (or (get-text-property (point-at-bol) 'org-todo-head)
  7724. (progn
  7725. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  7726. nil (point-at-eol)))
  7727. (get-text-property p 'org-todo-head))))
  7728. ((not (member kwd org-todo-keywords-1))
  7729. (car org-todo-keywords-1))
  7730. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  7731. (defun org-fast-todo-selection ()
  7732. "Fast TODO keyword selection with single keys.
  7733. Returns the new TODO keyword, or nil if no state change should occur."
  7734. (let* ((fulltable org-todo-key-alist)
  7735. (done-keywords org-done-keywords) ;; needed for the faces.
  7736. (maxlen (apply 'max (mapcar
  7737. (lambda (x)
  7738. (if (stringp (car x)) (string-width (car x)) 0))
  7739. fulltable)))
  7740. (expert nil)
  7741. (fwidth (+ maxlen 3 1 3))
  7742. (ncol (/ (- (window-width) 4) fwidth))
  7743. tg cnt e c tbl
  7744. groups ingroup)
  7745. (save-window-excursion
  7746. (if expert
  7747. (set-buffer (get-buffer-create " *Org todo*"))
  7748. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  7749. (erase-buffer)
  7750. (org-set-local 'org-done-keywords done-keywords)
  7751. (setq tbl fulltable cnt 0)
  7752. (while (setq e (pop tbl))
  7753. (cond
  7754. ((equal e '(:startgroup))
  7755. (push '() groups) (setq ingroup t)
  7756. (when (not (= cnt 0))
  7757. (setq cnt 0)
  7758. (insert "\n"))
  7759. (insert "{ "))
  7760. ((equal e '(:endgroup))
  7761. (setq ingroup nil cnt 0)
  7762. (insert "}\n"))
  7763. (t
  7764. (setq tg (car e) c (cdr e))
  7765. (if ingroup (push tg (car groups)))
  7766. (setq tg (org-add-props tg nil 'face
  7767. (org-get-todo-face tg)))
  7768. (if (and (= cnt 0) (not ingroup)) (insert " "))
  7769. (insert "[" c "] " tg (make-string
  7770. (- fwidth 4 (length tg)) ?\ ))
  7771. (when (= (setq cnt (1+ cnt)) ncol)
  7772. (insert "\n")
  7773. (if ingroup (insert " "))
  7774. (setq cnt 0)))))
  7775. (insert "\n")
  7776. (goto-char (point-min))
  7777. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  7778. (fit-window-to-buffer))
  7779. (message "[a-z..]:Set [SPC]:clear")
  7780. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  7781. (cond
  7782. ((or (= c ?\C-g)
  7783. (and (= c ?q) (not (rassoc c fulltable))))
  7784. (setq quit-flag t))
  7785. ((= c ?\ ) nil)
  7786. ((setq e (rassoc c fulltable) tg (car e))
  7787. tg)
  7788. (t (setq quit-flag t))))))
  7789. (defun org-entry-is-todo-p ()
  7790. (member (org-get-todo-state) org-not-done-keywords))
  7791. (defun org-entry-is-done-p ()
  7792. (member (org-get-todo-state) org-done-keywords))
  7793. (defun org-get-todo-state ()
  7794. (save-excursion
  7795. (org-back-to-heading t)
  7796. (and (looking-at org-todo-line-regexp)
  7797. (match-end 2)
  7798. (match-string 2))))
  7799. (defun org-at-date-range-p (&optional inactive-ok)
  7800. "Is the cursor inside a date range?"
  7801. (interactive)
  7802. (save-excursion
  7803. (catch 'exit
  7804. (let ((pos (point)))
  7805. (skip-chars-backward "^[<\r\n")
  7806. (skip-chars-backward "<[")
  7807. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7808. (>= (match-end 0) pos)
  7809. (throw 'exit t))
  7810. (skip-chars-backward "^<[\r\n")
  7811. (skip-chars-backward "<[")
  7812. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7813. (>= (match-end 0) pos)
  7814. (throw 'exit t)))
  7815. nil)))
  7816. (defun org-get-repeat ()
  7817. "Check if tere is a deadline/schedule with repeater in this entry."
  7818. (save-match-data
  7819. (save-excursion
  7820. (org-back-to-heading t)
  7821. (if (re-search-forward
  7822. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  7823. (match-string 1)))))
  7824. (defvar org-last-changed-timestamp)
  7825. (defvar org-log-post-message)
  7826. (defvar org-log-note-purpose)
  7827. (defun org-auto-repeat-maybe (done-word)
  7828. "Check if the current headline contains a repeated deadline/schedule.
  7829. If yes, set TODO state back to what it was and change the base date
  7830. of repeating deadline/scheduled time stamps to new date.
  7831. This function is run automatically after each state change to a DONE state."
  7832. ;; last-state is dynamically scoped into this function
  7833. (let* ((repeat (org-get-repeat))
  7834. (aa (assoc last-state org-todo-kwd-alist))
  7835. (interpret (nth 1 aa))
  7836. (head (nth 2 aa))
  7837. (whata '(("d" . day) ("m" . month) ("y" . year)))
  7838. (msg "Entry repeats: ")
  7839. (org-log-done nil)
  7840. (org-todo-log-states nil)
  7841. (nshiftmax 10) (nshift 0)
  7842. re type n what ts mb0 time)
  7843. (when repeat
  7844. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  7845. (org-todo (if (eq interpret 'type) last-state head))
  7846. (when (and org-log-repeat
  7847. (or (not (memq 'org-add-log-note
  7848. (default-value 'post-command-hook)))
  7849. (eq org-log-note-purpose 'done)))
  7850. ;; Make sure a note is taken;
  7851. (org-add-log-maybe 'state (or done-word (car org-done-keywords))
  7852. 'findpos org-log-repeat))
  7853. (org-back-to-heading t)
  7854. (org-add-planning-info nil nil 'closed)
  7855. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  7856. org-deadline-time-regexp "\\)\\|\\("
  7857. org-ts-regexp "\\)"))
  7858. (while (re-search-forward
  7859. re (save-excursion (outline-next-heading) (point)) t)
  7860. (setq type (if (match-end 1) org-scheduled-string
  7861. (if (match-end 3) org-deadline-string "Plain:"))
  7862. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0)))
  7863. mb0 (match-beginning 0))
  7864. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  7865. (setq n (string-to-number (match-string 2 ts))
  7866. what (match-string 3 ts))
  7867. (if (equal what "w") (setq n (* n 7) what "d"))
  7868. ;; Preparation, see if we need to modify the start date for the change
  7869. (when (match-end 1)
  7870. (setq time (save-match-data (org-time-string-to-time ts)))
  7871. (cond
  7872. ((equal (match-string 1 ts) ".")
  7873. ;; Shift starting date to today
  7874. (org-timestamp-change
  7875. (- (time-to-days (current-time)) (time-to-days time))
  7876. 'day))
  7877. ((equal (match-string 1 ts) "+")
  7878. (while (< (time-to-days time) (time-to-days (current-time)))
  7879. (when (= (incf nshift) nshiftmax)
  7880. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  7881. (error "Abort")))
  7882. (org-timestamp-change n (cdr (assoc what whata)))
  7883. (sit-for .0001) ;; so we can watch the date shifting
  7884. (org-at-timestamp-p t)
  7885. (setq ts (match-string 1))
  7886. (setq time (save-match-data (org-time-string-to-time ts))))
  7887. (org-timestamp-change (- n) (cdr (assoc what whata)))
  7888. ;; rematch, so that we have everything in place for the real shift
  7889. (org-at-timestamp-p t)
  7890. (setq ts (match-string 1))
  7891. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  7892. (org-timestamp-change n (cdr (assoc what whata)))
  7893. (setq msg (concat msg type org-last-changed-timestamp " "))))
  7894. (setq org-log-post-message msg)
  7895. (message "%s" msg))))
  7896. (defun org-show-todo-tree (arg)
  7897. "Make a compact tree which shows all headlines marked with TODO.
  7898. The tree will show the lines where the regexp matches, and all higher
  7899. headlines above the match.
  7900. With a \\[universal-argument] prefix, also show the DONE entries.
  7901. With a numeric prefix N, construct a sparse tree for the Nth element
  7902. of `org-todo-keywords-1'."
  7903. (interactive "P")
  7904. (let ((case-fold-search nil)
  7905. (kwd-re
  7906. (cond ((null arg) org-not-done-regexp)
  7907. ((equal arg '(4))
  7908. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  7909. (mapcar 'list org-todo-keywords-1))))
  7910. (concat "\\("
  7911. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  7912. "\\)\\>")))
  7913. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  7914. (regexp-quote (nth (1- (prefix-numeric-value arg))
  7915. org-todo-keywords-1)))
  7916. (t (error "Invalid prefix argument: %s" arg)))))
  7917. (message "%d TODO entries found"
  7918. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  7919. (defun org-deadline (&optional remove)
  7920. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  7921. With argument REMOVE, remove any deadline from the item."
  7922. (interactive "P")
  7923. (if remove
  7924. (progn
  7925. (org-remove-timestamp-with-keyword org-deadline-string)
  7926. (message "Item no longer has a deadline."))
  7927. (org-add-planning-info 'deadline nil 'closed)))
  7928. (defun org-schedule (&optional remove)
  7929. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  7930. With argument REMOVE, remove any scheduling date from the item."
  7931. (interactive "P")
  7932. (if remove
  7933. (progn
  7934. (org-remove-timestamp-with-keyword org-scheduled-string)
  7935. (message "Item is no longer scheduled."))
  7936. (org-add-planning-info 'scheduled nil 'closed)))
  7937. (defun org-remove-timestamp-with-keyword (keyword)
  7938. "Remove all time stamps with KEYWORD in the current entry."
  7939. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  7940. beg)
  7941. (save-excursion
  7942. (org-back-to-heading t)
  7943. (setq beg (point))
  7944. (org-end-of-subtree t t)
  7945. (while (re-search-backward re beg t)
  7946. (replace-match "")
  7947. (unless (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  7948. (delete-region (point-at-bol) (min (1+ (point)) (point-max))))))))
  7949. (defun org-add-planning-info (what &optional time &rest remove)
  7950. "Insert new timestamp with keyword in the line directly after the headline.
  7951. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  7952. If non is given, the user is prompted for a date.
  7953. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  7954. be removed."
  7955. (interactive)
  7956. (let (org-time-was-given org-end-time-was-given ts
  7957. end default-time default-input)
  7958. (when (and (not time) (memq what '(scheduled deadline)))
  7959. ;; Try to get a default date/time from existing timestamp
  7960. (save-excursion
  7961. (org-back-to-heading t)
  7962. (setq end (save-excursion (outline-next-heading) (point)))
  7963. (when (re-search-forward (if (eq what 'scheduled)
  7964. org-scheduled-time-regexp
  7965. org-deadline-time-regexp)
  7966. end t)
  7967. (setq ts (match-string 1)
  7968. default-time
  7969. (apply 'encode-time (org-parse-time-string ts))
  7970. default-input (and ts (org-get-compact-tod ts))))))
  7971. (when what
  7972. ;; If necessary, get the time from the user
  7973. (setq time (or time (org-read-date nil 'to-time nil nil
  7974. default-time default-input))))
  7975. (when (and org-insert-labeled-timestamps-at-point
  7976. (member what '(scheduled deadline)))
  7977. (insert
  7978. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  7979. (org-insert-time-stamp time org-time-was-given
  7980. nil nil nil (list org-end-time-was-given))
  7981. (setq what nil))
  7982. (save-excursion
  7983. (save-restriction
  7984. (let (col list elt ts buffer-invisibility-spec)
  7985. (org-back-to-heading t)
  7986. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  7987. (goto-char (match-end 1))
  7988. (setq col (current-column))
  7989. (goto-char (match-end 0))
  7990. (if (eobp) (insert "\n") (forward-char 1))
  7991. (if (and (not (looking-at outline-regexp))
  7992. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  7993. "[^\r\n]*"))
  7994. (not (equal (match-string 1) org-clock-string)))
  7995. (narrow-to-region (match-beginning 0) (match-end 0))
  7996. (insert-before-markers "\n")
  7997. (backward-char 1)
  7998. (narrow-to-region (point) (point))
  7999. (indent-to-column col))
  8000. ;; Check if we have to remove something.
  8001. (setq list (cons what remove))
  8002. (while list
  8003. (setq elt (pop list))
  8004. (goto-char (point-min))
  8005. (when (or (and (eq elt 'scheduled)
  8006. (re-search-forward org-scheduled-time-regexp nil t))
  8007. (and (eq elt 'deadline)
  8008. (re-search-forward org-deadline-time-regexp nil t))
  8009. (and (eq elt 'closed)
  8010. (re-search-forward org-closed-time-regexp nil t)))
  8011. (replace-match "")
  8012. (if (looking-at "--+<[^>]+>") (replace-match ""))
  8013. (if (looking-at " +") (replace-match ""))))
  8014. (goto-char (point-max))
  8015. (when what
  8016. (insert
  8017. (if (not (equal (char-before) ?\ )) " " "")
  8018. (cond ((eq what 'scheduled) org-scheduled-string)
  8019. ((eq what 'deadline) org-deadline-string)
  8020. ((eq what 'closed) org-closed-string))
  8021. " ")
  8022. (setq ts (org-insert-time-stamp
  8023. time
  8024. (or org-time-was-given
  8025. (and (eq what 'closed) org-log-done-with-time))
  8026. (eq what 'closed)
  8027. nil nil (list org-end-time-was-given)))
  8028. (end-of-line 1))
  8029. (goto-char (point-min))
  8030. (widen)
  8031. (if (looking-at "[ \t]+\r?\n")
  8032. (replace-match ""))
  8033. ts)))))
  8034. (defvar org-log-note-marker (make-marker))
  8035. (defvar org-log-note-purpose nil)
  8036. (defvar org-log-note-state nil)
  8037. (defvar org-log-note-how nil)
  8038. (defvar org-log-note-window-configuration nil)
  8039. (defvar org-log-note-return-to (make-marker))
  8040. (defvar org-log-post-message nil
  8041. "Message to be displayed after a log note has been stored.
  8042. The auto-repeater uses this.")
  8043. (defun org-add-log-maybe (&optional purpose state findpos how)
  8044. "Set up the post command hook to take a note.
  8045. If this is about to TODO state change, the new state is expected in STATE.
  8046. When FINDPOS is non-nil, find the correct position for the note in
  8047. the current entry. If not, assume that it can be inserted at point."
  8048. (save-excursion
  8049. (when findpos
  8050. (org-back-to-heading t)
  8051. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  8052. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  8053. "[^\r\n]*\\)?"))
  8054. (goto-char (match-end 0))
  8055. (unless org-log-states-order-reversed
  8056. (and (= (char-after) ?\n) (forward-char 1))
  8057. (org-skip-over-state-notes)
  8058. (skip-chars-backward " \t\n\r")))
  8059. (move-marker org-log-note-marker (point))
  8060. (setq org-log-note-purpose purpose
  8061. org-log-note-state state
  8062. org-log-note-how how)
  8063. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  8064. (defun org-skip-over-state-notes ()
  8065. "Skip past the list of State notes in an entry."
  8066. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  8067. (while (looking-at "[ \t]*- State")
  8068. (condition-case nil
  8069. (org-next-item)
  8070. (error (org-end-of-item)))))
  8071. (defun org-add-log-note (&optional purpose)
  8072. "Pop up a window for taking a note, and add this note later at point."
  8073. (remove-hook 'post-command-hook 'org-add-log-note)
  8074. (setq org-log-note-window-configuration (current-window-configuration))
  8075. (delete-other-windows)
  8076. (move-marker org-log-note-return-to (point))
  8077. (switch-to-buffer (marker-buffer org-log-note-marker))
  8078. (goto-char org-log-note-marker)
  8079. (org-switch-to-buffer-other-window "*Org Note*")
  8080. (erase-buffer)
  8081. (if (memq org-log-note-how '(time state)) ; FIXME: time or state????????????
  8082. (org-store-log-note)
  8083. (let ((org-inhibit-startup t)) (org-mode))
  8084. (insert (format "# Insert note for %s.
  8085. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  8086. (cond
  8087. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  8088. ((eq org-log-note-purpose 'done) "closed todo item")
  8089. ((eq org-log-note-purpose 'state)
  8090. (format "state change to \"%s\"" org-log-note-state))
  8091. (t (error "This should not happen")))))
  8092. (org-set-local 'org-finish-function 'org-store-log-note)))
  8093. (defvar org-note-abort nil) ; dynamically scoped
  8094. (defun org-store-log-note ()
  8095. "Finish taking a log note, and insert it to where it belongs."
  8096. (let ((txt (buffer-string))
  8097. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  8098. lines ind)
  8099. (kill-buffer (current-buffer))
  8100. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  8101. (setq txt (replace-match "" t t txt)))
  8102. (if (string-match "\\s-+\\'" txt)
  8103. (setq txt (replace-match "" t t txt)))
  8104. (setq lines (org-split-string txt "\n"))
  8105. (when (and note (string-match "\\S-" note))
  8106. (setq note
  8107. (org-replace-escapes
  8108. note
  8109. (list (cons "%u" (user-login-name))
  8110. (cons "%U" user-full-name)
  8111. (cons "%t" (format-time-string
  8112. (org-time-stamp-format 'long 'inactive)
  8113. (current-time)))
  8114. (cons "%s" (if org-log-note-state
  8115. (concat "\"" org-log-note-state "\"")
  8116. "")))))
  8117. (if lines (setq note (concat note " \\\\")))
  8118. (push note lines))
  8119. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  8120. (when lines
  8121. (save-excursion
  8122. (set-buffer (marker-buffer org-log-note-marker))
  8123. (save-excursion
  8124. (goto-char org-log-note-marker)
  8125. (move-marker org-log-note-marker nil)
  8126. (end-of-line 1)
  8127. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  8128. (indent-relative nil)
  8129. (insert "- " (pop lines))
  8130. (org-indent-line-function)
  8131. (beginning-of-line 1)
  8132. (looking-at "[ \t]*")
  8133. (setq ind (concat (match-string 0) " "))
  8134. (end-of-line 1)
  8135. (while lines (insert "\n" ind (pop lines)))))))
  8136. (set-window-configuration org-log-note-window-configuration)
  8137. (with-current-buffer (marker-buffer org-log-note-return-to)
  8138. (goto-char org-log-note-return-to))
  8139. (move-marker org-log-note-return-to nil)
  8140. (and org-log-post-message (message "%s" org-log-post-message)))
  8141. ;; FIXME: what else would be useful?
  8142. ;; - priority
  8143. ;; - date
  8144. (defun org-sparse-tree (&optional arg)
  8145. "Create a sparse tree, prompt for the details.
  8146. This command can create sparse trees. You first need to select the type
  8147. of match used to create the tree:
  8148. t Show entries with a specific TODO keyword.
  8149. T Show entries selected by a tags match.
  8150. p Enter a property name and its value (both with completion on existing
  8151. names/values) and show entries with that property.
  8152. r Show entries matching a regular expression
  8153. d Show deadlines due within `org-deadline-warning-days'."
  8154. (interactive "P")
  8155. (let (ans kwd value)
  8156. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  8157. (setq ans (read-char-exclusive))
  8158. (cond
  8159. ((equal ans ?d)
  8160. (call-interactively 'org-check-deadlines))
  8161. ((equal ans ?b)
  8162. (call-interactively 'org-check-before-date))
  8163. ((equal ans ?t)
  8164. (org-show-todo-tree '(4)))
  8165. ((equal ans ?T)
  8166. (call-interactively 'org-tags-sparse-tree))
  8167. ((member ans '(?p ?P))
  8168. (setq kwd (completing-read "Property: "
  8169. (mapcar 'list (org-buffer-property-keys))))
  8170. (setq value (completing-read "Value: "
  8171. (mapcar 'list (org-property-values kwd))))
  8172. (unless (string-match "\\`{.*}\\'" value)
  8173. (setq value (concat "\"" value "\"")))
  8174. (org-tags-sparse-tree arg (concat kwd "=" value)))
  8175. ((member ans '(?r ?R ?/))
  8176. (call-interactively 'org-occur))
  8177. (t (error "No such sparse tree command \"%c\"" ans)))))
  8178. (defvar org-occur-highlights nil
  8179. "List of overlays used for occur matches.")
  8180. (make-variable-buffer-local 'org-occur-highlights)
  8181. (defvar org-occur-parameters nil
  8182. "Parameters of the active org-occur calls.
  8183. This is a list, each call to org-occur pushes as cons cell,
  8184. containing the regular expression and the callback, onto the list.
  8185. The list can contain several entries if `org-occur' has been called
  8186. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  8187. will only contain one set of parameters. When the highlights are
  8188. removed (for example with `C-c C-c', or with the next edit (depending
  8189. on `org-remove-highlights-with-change'), this variable is emptied
  8190. as well.")
  8191. (make-variable-buffer-local 'org-occur-parameters)
  8192. (defun org-occur (regexp &optional keep-previous callback)
  8193. "Make a compact tree which shows all matches of REGEXP.
  8194. The tree will show the lines where the regexp matches, and all higher
  8195. headlines above the match. It will also show the heading after the match,
  8196. to make sure editing the matching entry is easy.
  8197. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  8198. call to `org-occur' will be kept, to allow stacking of calls to this
  8199. command.
  8200. If CALLBACK is non-nil, it is a function which is called to confirm
  8201. that the match should indeed be shown."
  8202. (interactive "sRegexp: \nP")
  8203. (unless keep-previous
  8204. (org-remove-occur-highlights nil nil t))
  8205. (push (cons regexp callback) org-occur-parameters)
  8206. (let ((cnt 0))
  8207. (save-excursion
  8208. (goto-char (point-min))
  8209. (if (or (not keep-previous) ; do not want to keep
  8210. (not org-occur-highlights)) ; no previous matches
  8211. ;; hide everything
  8212. (org-overview))
  8213. (while (re-search-forward regexp nil t)
  8214. (when (or (not callback)
  8215. (save-match-data (funcall callback)))
  8216. (setq cnt (1+ cnt))
  8217. (when org-highlight-sparse-tree-matches
  8218. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  8219. (org-show-context 'occur-tree))))
  8220. (when org-remove-highlights-with-change
  8221. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  8222. nil 'local))
  8223. (unless org-sparse-tree-open-archived-trees
  8224. (org-hide-archived-subtrees (point-min) (point-max)))
  8225. (run-hooks 'org-occur-hook)
  8226. (if (interactive-p)
  8227. (message "%d match(es) for regexp %s" cnt regexp))
  8228. cnt))
  8229. (defun org-show-context (&optional key)
  8230. "Make sure point and context and visible.
  8231. How much context is shown depends upon the variables
  8232. `org-show-hierarchy-above', `org-show-following-heading'. and
  8233. `org-show-siblings'."
  8234. (let ((heading-p (org-on-heading-p t))
  8235. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  8236. (following-p (org-get-alist-option org-show-following-heading key))
  8237. (entry-p (org-get-alist-option org-show-entry-below key))
  8238. (siblings-p (org-get-alist-option org-show-siblings key)))
  8239. (catch 'exit
  8240. ;; Show heading or entry text
  8241. (if (and heading-p (not entry-p))
  8242. (org-flag-heading nil) ; only show the heading
  8243. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  8244. (org-show-hidden-entry))) ; show entire entry
  8245. (when following-p
  8246. ;; Show next sibling, or heading below text
  8247. (save-excursion
  8248. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  8249. (org-flag-heading nil))))
  8250. (when siblings-p (org-show-siblings))
  8251. (when hierarchy-p
  8252. ;; show all higher headings, possibly with siblings
  8253. (save-excursion
  8254. (while (and (condition-case nil
  8255. (progn (org-up-heading-all 1) t)
  8256. (error nil))
  8257. (not (bobp)))
  8258. (org-flag-heading nil)
  8259. (when siblings-p (org-show-siblings))))))))
  8260. (defun org-reveal (&optional siblings)
  8261. "Show current entry, hierarchy above it, and the following headline.
  8262. This can be used to show a consistent set of context around locations
  8263. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  8264. not t for the search context.
  8265. With optional argument SIBLINGS, on each level of the hierarchy all
  8266. siblings are shown. This repairs the tree structure to what it would
  8267. look like when opened with hierarchical calls to `org-cycle'."
  8268. (interactive "P")
  8269. (let ((org-show-hierarchy-above t)
  8270. (org-show-following-heading t)
  8271. (org-show-siblings (if siblings t org-show-siblings)))
  8272. (org-show-context nil)))
  8273. (defun org-highlight-new-match (beg end)
  8274. "Highlight from BEG to END and mark the highlight is an occur headline."
  8275. (let ((ov (org-make-overlay beg end)))
  8276. (org-overlay-put ov 'face 'secondary-selection)
  8277. (push ov org-occur-highlights)))
  8278. (defun org-remove-occur-highlights (&optional beg end noremove)
  8279. "Remove the occur highlights from the buffer.
  8280. BEG and END are ignored. If NOREMOVE is nil, remove this function
  8281. from the `before-change-functions' in the current buffer."
  8282. (interactive)
  8283. (unless org-inhibit-highlight-removal
  8284. (mapc 'org-delete-overlay org-occur-highlights)
  8285. (setq org-occur-highlights nil)
  8286. (setq org-occur-parameters nil)
  8287. (unless noremove
  8288. (remove-hook 'before-change-functions
  8289. 'org-remove-occur-highlights 'local))))
  8290. ;;;; Priorities
  8291. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  8292. "Regular expression matching the priority indicator.")
  8293. (defvar org-remove-priority-next-time nil)
  8294. (defun org-priority-up ()
  8295. "Increase the priority of the current item."
  8296. (interactive)
  8297. (org-priority 'up))
  8298. (defun org-priority-down ()
  8299. "Decrease the priority of the current item."
  8300. (interactive)
  8301. (org-priority 'down))
  8302. (defun org-priority (&optional action)
  8303. "Change the priority of an item by ARG.
  8304. ACTION can be `set', `up', `down', or a character."
  8305. (interactive)
  8306. (setq action (or action 'set))
  8307. (let (current new news have remove)
  8308. (save-excursion
  8309. (org-back-to-heading)
  8310. (if (looking-at org-priority-regexp)
  8311. (setq current (string-to-char (match-string 2))
  8312. have t)
  8313. (setq current org-default-priority))
  8314. (cond
  8315. ((or (eq action 'set) (integerp action))
  8316. (if (integerp action)
  8317. (setq new action)
  8318. (message "Priority %c-%c, SPC to remove: " org-highest-priority org-lowest-priority)
  8319. (setq new (read-char-exclusive)))
  8320. (if (and (= (upcase org-highest-priority) org-highest-priority)
  8321. (= (upcase org-lowest-priority) org-lowest-priority))
  8322. (setq new (upcase new)))
  8323. (cond ((equal new ?\ ) (setq remove t))
  8324. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  8325. (error "Priority must be between `%c' and `%c'"
  8326. org-highest-priority org-lowest-priority))))
  8327. ((eq action 'up)
  8328. (if (and (not have) (eq last-command this-command))
  8329. (setq new org-lowest-priority)
  8330. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8331. org-default-priority (1- current)))))
  8332. ((eq action 'down)
  8333. (if (and (not have) (eq last-command this-command))
  8334. (setq new org-highest-priority)
  8335. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8336. org-default-priority (1+ current)))))
  8337. (t (error "Invalid action")))
  8338. (if (or (< (upcase new) org-highest-priority)
  8339. (> (upcase new) org-lowest-priority))
  8340. (setq remove t))
  8341. (setq news (format "%c" new))
  8342. (if have
  8343. (if remove
  8344. (replace-match "" t t nil 1)
  8345. (replace-match news t t nil 2))
  8346. (if remove
  8347. (error "No priority cookie found in line")
  8348. (looking-at org-todo-line-regexp)
  8349. (if (match-end 2)
  8350. (progn
  8351. (goto-char (match-end 2))
  8352. (insert " [#" news "]"))
  8353. (goto-char (match-beginning 3))
  8354. (insert "[#" news "] ")))))
  8355. (org-preserve-lc (org-set-tags nil 'align))
  8356. (if remove
  8357. (message "Priority removed")
  8358. (message "Priority of current item set to %s" news))))
  8359. (defun org-get-priority (s)
  8360. "Find priority cookie and return priority."
  8361. (save-match-data
  8362. (if (not (string-match org-priority-regexp s))
  8363. (* 1000 (- org-lowest-priority org-default-priority))
  8364. (* 1000 (- org-lowest-priority
  8365. (string-to-char (match-string 2 s)))))))
  8366. ;;;; Tags
  8367. (defun org-scan-tags (action matcher &optional todo-only)
  8368. "Scan headline tags with inheritance and produce output ACTION.
  8369. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  8370. evaluated, testing if a given set of tags qualifies a headline for
  8371. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  8372. are included in the output."
  8373. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8374. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  8375. (org-re
  8376. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  8377. (props (list 'face nil
  8378. 'done-face 'org-done
  8379. 'undone-face nil
  8380. 'mouse-face 'highlight
  8381. 'org-not-done-regexp org-not-done-regexp
  8382. 'org-todo-regexp org-todo-regexp
  8383. 'keymap org-agenda-keymap
  8384. 'help-echo
  8385. (format "mouse-2 or RET jump to org file %s"
  8386. (abbreviate-file-name
  8387. (or (buffer-file-name (buffer-base-buffer))
  8388. (buffer-name (buffer-base-buffer)))))))
  8389. (case-fold-search nil)
  8390. lspos
  8391. tags tags-list tags-alist (llast 0) rtn level category i txt
  8392. todo marker entry priority)
  8393. (save-excursion
  8394. (goto-char (point-min))
  8395. (when (eq action 'sparse-tree)
  8396. (org-overview)
  8397. (org-remove-occur-highlights))
  8398. (while (re-search-forward re nil t)
  8399. (catch :skip
  8400. (setq todo (if (match-end 1) (match-string 2))
  8401. tags (if (match-end 4) (match-string 4)))
  8402. (goto-char (setq lspos (1+ (match-beginning 0))))
  8403. (setq level (org-reduced-level (funcall outline-level))
  8404. category (org-get-category))
  8405. (setq i llast llast level)
  8406. ;; remove tag lists from same and sublevels
  8407. (while (>= i level)
  8408. (when (setq entry (assoc i tags-alist))
  8409. (setq tags-alist (delete entry tags-alist)))
  8410. (setq i (1- i)))
  8411. ;; add the next tags
  8412. (when tags
  8413. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8414. tags-alist
  8415. (cons (cons level tags) tags-alist)))
  8416. ;; compile tags for current headline
  8417. (setq tags-list
  8418. (if org-use-tag-inheritance
  8419. (apply 'append (mapcar 'cdr tags-alist))
  8420. tags))
  8421. (when (and tags org-use-tag-inheritance
  8422. (not (eq t org-use-tag-inheritance)))
  8423. ;; selective inheritance, remove uninherited ones
  8424. (setcdr (car tags-alist)
  8425. (org-remove-uniherited-tags (cdar tags-alist))))
  8426. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  8427. (eval matcher)
  8428. (or (not org-agenda-skip-archived-trees)
  8429. (not (member org-archive-tag tags-list))))
  8430. (and (eq action 'agenda) (org-agenda-skip))
  8431. ;; list this headline
  8432. (if (eq action 'sparse-tree)
  8433. (progn
  8434. (and org-highlight-sparse-tree-matches
  8435. (org-get-heading) (match-end 0)
  8436. (org-highlight-new-match
  8437. (match-beginning 0) (match-beginning 1)))
  8438. (org-show-context 'tags-tree))
  8439. (setq txt (org-format-agenda-item
  8440. ""
  8441. (concat
  8442. (if org-tags-match-list-sublevels
  8443. (make-string (1- level) ?.) "")
  8444. (org-get-heading))
  8445. category tags-list)
  8446. priority (org-get-priority txt))
  8447. (goto-char lspos)
  8448. (setq marker (org-agenda-new-marker))
  8449. (org-add-props txt props
  8450. 'org-marker marker 'org-hd-marker marker 'org-category category
  8451. 'priority priority 'type "tagsmatch")
  8452. (push txt rtn))
  8453. ;; if we are to skip sublevels, jump to end of subtree
  8454. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8455. (when (and (eq action 'sparse-tree)
  8456. (not org-sparse-tree-open-archived-trees))
  8457. (org-hide-archived-subtrees (point-min) (point-max)))
  8458. (nreverse rtn)))
  8459. (defun org-remove-uniherited-tags (tags)
  8460. "Remove all tags that are not inherited from the list TAGS."
  8461. (cond
  8462. ((eq org-use-tag-inheritance t) tags)
  8463. ((not org-use-tag-inheritance) nil)
  8464. ((stringp org-use-tag-inheritance)
  8465. (delq nil (mapcar
  8466. (lambda (x) (if (string-match org-use-tag-inheritance x) x nil))
  8467. tags)))
  8468. ((listp org-use-tag-inheritance)
  8469. (org-delete-all org-use-tag-inheritance tags))))
  8470. (defvar todo-only) ;; dynamically scoped
  8471. (defun org-tags-sparse-tree (&optional todo-only match)
  8472. "Create a sparse tree according to tags string MATCH.
  8473. MATCH can contain positive and negative selection of tags, like
  8474. \"+WORK+URGENT-WITHBOSS\".
  8475. If optional argument TODO_ONLY is non-nil, only select lines that are
  8476. also TODO lines."
  8477. (interactive "P")
  8478. (org-prepare-agenda-buffers (list (current-buffer)))
  8479. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  8480. (defvar org-cached-props nil)
  8481. (defun org-cached-entry-get (pom property)
  8482. (if (or (eq t org-use-property-inheritance)
  8483. (and (stringp org-use-property-inheritance)
  8484. (string-match org-use-property-inheritance property))
  8485. (and (listp org-use-property-inheritance)
  8486. (member property org-use-property-inheritance)))
  8487. ;; Caching is not possible, check it directly
  8488. (org-entry-get pom property 'inherit)
  8489. ;; Get all properties, so that we can do complicated checks easily
  8490. (cdr (assoc property (or org-cached-props
  8491. (setq org-cached-props
  8492. (org-entry-properties pom)))))))
  8493. (defun org-global-tags-completion-table (&optional files)
  8494. "Return the list of all tags in all agenda buffer/files."
  8495. (save-excursion
  8496. (org-uniquify
  8497. (delq nil
  8498. (apply 'append
  8499. (mapcar
  8500. (lambda (file)
  8501. (set-buffer (find-file-noselect file))
  8502. (append (org-get-buffer-tags)
  8503. (mapcar (lambda (x) (if (stringp (car-safe x))
  8504. (list (car-safe x)) nil))
  8505. org-tag-alist)))
  8506. (if (and files (car files))
  8507. files
  8508. (org-agenda-files))))))))
  8509. (defun org-make-tags-matcher (match)
  8510. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8511. ;; todo-only is scoped dynamically into this function, and the function
  8512. ;; may change it it the matcher asksk for it.
  8513. (unless match
  8514. ;; Get a new match request, with completion
  8515. (let ((org-last-tags-completion-table
  8516. (org-global-tags-completion-table)))
  8517. (setq match (completing-read
  8518. "Match: " 'org-tags-completion-function nil nil nil
  8519. 'org-tags-history))))
  8520. ;; Parse the string and create a lisp form
  8521. (let ((match0 match)
  8522. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL=\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)=\\({[^}]+}\\|\"[^\"]*\"\\)\\|[[:alnum:]_@]+\\)"))
  8523. minus tag mm
  8524. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  8525. orterms term orlist re-p level-p prop-p pn pv cat-p gv)
  8526. (if (string-match "/+" match)
  8527. ;; match contains also a todo-matching request
  8528. (progn
  8529. (setq tagsmatch (substring match 0 (match-beginning 0))
  8530. todomatch (substring match (match-end 0)))
  8531. (if (string-match "^!" todomatch)
  8532. (setq todo-only t todomatch (substring todomatch 1)))
  8533. (if (string-match "^\\s-*$" todomatch)
  8534. (setq todomatch nil)))
  8535. ;; only matching tags
  8536. (setq tagsmatch match todomatch nil))
  8537. ;; Make the tags matcher
  8538. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8539. (setq tagsmatcher t)
  8540. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8541. (while (setq term (pop orterms))
  8542. (while (and (equal (substring term -1) "\\") orterms)
  8543. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8544. (while (string-match re term)
  8545. (setq minus (and (match-end 1)
  8546. (equal (match-string 1 term) "-"))
  8547. tag (match-string 2 term)
  8548. re-p (equal (string-to-char tag) ?{)
  8549. level-p (match-end 3)
  8550. prop-p (match-end 4)
  8551. mm (cond
  8552. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8553. (level-p `(= level ,(string-to-number
  8554. (match-string 3 term))))
  8555. (prop-p
  8556. (setq pn (match-string 4 term)
  8557. pv (match-string 5 term)
  8558. cat-p (equal pn "CATEGORY")
  8559. re-p (equal (string-to-char pv) ?{)
  8560. pv (substring pv 1 -1))
  8561. (if (equal pn "CATEGORY")
  8562. (setq gv '(get-text-property (point) 'org-category))
  8563. (setq gv `(org-cached-entry-get nil ,pn)))
  8564. (if re-p
  8565. `(string-match ,pv (or ,gv ""))
  8566. `(equal ,pv (or ,gv ""))))
  8567. (t `(member ,(downcase tag) tags-list)))
  8568. mm (if minus (list 'not mm) mm)
  8569. term (substring term (match-end 0)))
  8570. (push mm tagsmatcher))
  8571. (push (if (> (length tagsmatcher) 1)
  8572. (cons 'and tagsmatcher)
  8573. (car tagsmatcher))
  8574. orlist)
  8575. (setq tagsmatcher nil))
  8576. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8577. (setq tagsmatcher
  8578. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  8579. ;; Make the todo matcher
  8580. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8581. (setq todomatcher t)
  8582. (setq orterms (org-split-string todomatch "|") orlist nil)
  8583. (while (setq term (pop orterms))
  8584. (while (string-match re term)
  8585. (setq minus (and (match-end 1)
  8586. (equal (match-string 1 term) "-"))
  8587. kwd (match-string 2 term)
  8588. re-p (equal (string-to-char kwd) ?{)
  8589. term (substring term (match-end 0))
  8590. mm (if re-p
  8591. `(string-match ,(substring kwd 1 -1) todo)
  8592. (list 'equal 'todo kwd))
  8593. mm (if minus (list 'not mm) mm))
  8594. (push mm todomatcher))
  8595. (push (if (> (length todomatcher) 1)
  8596. (cons 'and todomatcher)
  8597. (car todomatcher))
  8598. orlist)
  8599. (setq todomatcher nil))
  8600. (setq todomatcher (if (> (length orlist) 1)
  8601. (cons 'or orlist) (car orlist))))
  8602. ;; Return the string and lisp forms of the matcher
  8603. (setq matcher (if todomatcher
  8604. (list 'and tagsmatcher todomatcher)
  8605. tagsmatcher))
  8606. (cons match0 matcher)))
  8607. (defun org-match-any-p (re list)
  8608. "Does re match any element of list?"
  8609. (setq list (mapcar (lambda (x) (string-match re x)) list))
  8610. (delq nil list))
  8611. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  8612. (defvar org-tags-overlay (org-make-overlay 1 1))
  8613. (org-detach-overlay org-tags-overlay)
  8614. (defun org-align-tags-here (to-col)
  8615. ;; Assumes that this is a headline
  8616. (let ((pos (point)) (col (current-column)) tags)
  8617. (beginning-of-line 1)
  8618. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8619. (< pos (match-beginning 2)))
  8620. (progn
  8621. (setq tags (match-string 2))
  8622. (goto-char (match-beginning 1))
  8623. (insert " ")
  8624. (delete-region (point) (1+ (match-end 0)))
  8625. (backward-char 1)
  8626. (move-to-column
  8627. (max (1+ (current-column))
  8628. (1+ col)
  8629. (if (> to-col 0)
  8630. to-col
  8631. (- (abs to-col) (length tags))))
  8632. t)
  8633. (insert tags)
  8634. (move-to-column (min (current-column) col) t))
  8635. (goto-char pos))))
  8636. (defun org-set-tags (&optional arg just-align)
  8637. "Set the tags for the current headline.
  8638. With prefix ARG, realign all tags in headings in the current buffer."
  8639. (interactive "P")
  8640. (let* ((re (concat "^" outline-regexp))
  8641. (current (org-get-tags-string))
  8642. (col (current-column))
  8643. (org-setting-tags t)
  8644. table current-tags inherited-tags ; computed below when needed
  8645. tags p0 c0 c1 rpl)
  8646. (if arg
  8647. (save-excursion
  8648. (goto-char (point-min))
  8649. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  8650. (while (re-search-forward re nil t)
  8651. (org-set-tags nil t)
  8652. (end-of-line 1)))
  8653. (message "All tags realigned to column %d" org-tags-column))
  8654. (if just-align
  8655. (setq tags current)
  8656. ;; Get a new set of tags from the user
  8657. (save-excursion
  8658. (setq table (or org-tag-alist (org-get-buffer-tags))
  8659. org-last-tags-completion-table table
  8660. current-tags (org-split-string current ":")
  8661. inherited-tags (nreverse
  8662. (nthcdr (length current-tags)
  8663. (nreverse (org-get-tags-at))))
  8664. tags
  8665. (if (or (eq t org-use-fast-tag-selection)
  8666. (and org-use-fast-tag-selection
  8667. (delq nil (mapcar 'cdr table))))
  8668. (org-fast-tag-selection
  8669. current-tags inherited-tags table
  8670. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  8671. (let ((org-add-colon-after-tag-completion t))
  8672. (org-trim
  8673. (org-without-partial-completion
  8674. (completing-read "Tags: " 'org-tags-completion-function
  8675. nil nil current 'org-tags-history)))))))
  8676. (while (string-match "[-+&]+" tags)
  8677. ;; No boolean logic, just a list
  8678. (setq tags (replace-match ":" t t tags))))
  8679. (if (string-match "\\`[\t ]*\\'" tags)
  8680. (setq tags "")
  8681. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  8682. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  8683. ;; Insert new tags at the correct column
  8684. (beginning-of-line 1)
  8685. (cond
  8686. ((and (equal current "") (equal tags "")))
  8687. ((re-search-forward
  8688. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  8689. (point-at-eol) t)
  8690. (if (equal tags "")
  8691. (setq rpl "")
  8692. (goto-char (match-beginning 0))
  8693. (setq c0 (current-column) p0 (point)
  8694. c1 (max (1+ c0) (if (> org-tags-column 0)
  8695. org-tags-column
  8696. (- (- org-tags-column) (length tags))))
  8697. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  8698. (replace-match rpl t t)
  8699. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  8700. tags)
  8701. (t (error "Tags alignment failed")))
  8702. (move-to-column col)
  8703. (unless just-align
  8704. (run-hooks 'org-after-tags-change-hook)))))
  8705. (defun org-change-tag-in-region (beg end tag off)
  8706. "Add or remove TAG for each entry in the region.
  8707. This works in the agenda, and also in an org-mode buffer."
  8708. (interactive
  8709. (list (region-beginning) (region-end)
  8710. (let ((org-last-tags-completion-table
  8711. (if (org-mode-p)
  8712. (org-get-buffer-tags)
  8713. (org-global-tags-completion-table))))
  8714. (completing-read
  8715. "Tag: " 'org-tags-completion-function nil nil nil
  8716. 'org-tags-history))
  8717. (progn
  8718. (message "[s]et or [r]emove? ")
  8719. (equal (read-char-exclusive) ?r))))
  8720. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8721. (let ((agendap (equal major-mode 'org-agenda-mode))
  8722. l1 l2 m buf pos newhead (cnt 0))
  8723. (goto-char end)
  8724. (setq l2 (1- (org-current-line)))
  8725. (goto-char beg)
  8726. (setq l1 (org-current-line))
  8727. (loop for l from l1 to l2 do
  8728. (goto-line l)
  8729. (setq m (get-text-property (point) 'org-hd-marker))
  8730. (when (or (and (org-mode-p) (org-on-heading-p))
  8731. (and agendap m))
  8732. (setq buf (if agendap (marker-buffer m) (current-buffer))
  8733. pos (if agendap m (point)))
  8734. (with-current-buffer buf
  8735. (save-excursion
  8736. (save-restriction
  8737. (goto-char pos)
  8738. (setq cnt (1+ cnt))
  8739. (org-toggle-tag tag (if off 'off 'on))
  8740. (setq newhead (org-get-heading)))))
  8741. (and agendap (org-agenda-change-all-lines newhead m))))
  8742. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  8743. (defun org-tags-completion-function (string predicate &optional flag)
  8744. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  8745. (confirm (lambda (x) (stringp (car x)))))
  8746. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  8747. (setq s1 (match-string 1 string)
  8748. s2 (match-string 2 string))
  8749. (setq s1 "" s2 string))
  8750. (cond
  8751. ((eq flag nil)
  8752. ;; try completion
  8753. (setq rtn (try-completion s2 ctable confirm))
  8754. (if (stringp rtn)
  8755. (setq rtn
  8756. (concat s1 s2 (substring rtn (length s2))
  8757. (if (and org-add-colon-after-tag-completion
  8758. (assoc rtn ctable))
  8759. ":" ""))))
  8760. rtn)
  8761. ((eq flag t)
  8762. ;; all-completions
  8763. (all-completions s2 ctable confirm)
  8764. )
  8765. ((eq flag 'lambda)
  8766. ;; exact match?
  8767. (assoc s2 ctable)))
  8768. ))
  8769. (defun org-fast-tag-insert (kwd tags face &optional end)
  8770. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  8771. (insert (format "%-12s" (concat kwd ":"))
  8772. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  8773. (or end "")))
  8774. (defun org-fast-tag-show-exit (flag)
  8775. (save-excursion
  8776. (goto-line 3)
  8777. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  8778. (replace-match ""))
  8779. (when flag
  8780. (end-of-line 1)
  8781. (move-to-column (- (window-width) 19) t)
  8782. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  8783. (defun org-set-current-tags-overlay (current prefix)
  8784. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  8785. (if (featurep 'xemacs)
  8786. (org-overlay-display org-tags-overlay (concat prefix s)
  8787. 'secondary-selection)
  8788. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  8789. (org-overlay-display org-tags-overlay (concat prefix s)))))
  8790. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  8791. "Fast tag selection with single keys.
  8792. CURRENT is the current list of tags in the headline, INHERITED is the
  8793. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  8794. possibly with grouping information. TODO-TABLE is a similar table with
  8795. TODO keywords, should these have keys assigned to them.
  8796. If the keys are nil, a-z are automatically assigned.
  8797. Returns the new tags string, or nil to not change the current settings."
  8798. (let* ((fulltable (append table todo-table))
  8799. (maxlen (apply 'max (mapcar
  8800. (lambda (x)
  8801. (if (stringp (car x)) (string-width (car x)) 0))
  8802. fulltable)))
  8803. (buf (current-buffer))
  8804. (expert (eq org-fast-tag-selection-single-key 'expert))
  8805. (buffer-tags nil)
  8806. (fwidth (+ maxlen 3 1 3))
  8807. (ncol (/ (- (window-width) 4) fwidth))
  8808. (i-face 'org-done)
  8809. (c-face 'org-todo)
  8810. tg cnt e c char c1 c2 ntable tbl rtn
  8811. ov-start ov-end ov-prefix
  8812. (exit-after-next org-fast-tag-selection-single-key)
  8813. (done-keywords org-done-keywords)
  8814. groups ingroup)
  8815. (save-excursion
  8816. (beginning-of-line 1)
  8817. (if (looking-at
  8818. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8819. (setq ov-start (match-beginning 1)
  8820. ov-end (match-end 1)
  8821. ov-prefix "")
  8822. (setq ov-start (1- (point-at-eol))
  8823. ov-end (1+ ov-start))
  8824. (skip-chars-forward "^\n\r")
  8825. (setq ov-prefix
  8826. (concat
  8827. (buffer-substring (1- (point)) (point))
  8828. (if (> (current-column) org-tags-column)
  8829. " "
  8830. (make-string (- org-tags-column (current-column)) ?\ ))))))
  8831. (org-move-overlay org-tags-overlay ov-start ov-end)
  8832. (save-window-excursion
  8833. (if expert
  8834. (set-buffer (get-buffer-create " *Org tags*"))
  8835. (delete-other-windows)
  8836. (split-window-vertically)
  8837. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  8838. (erase-buffer)
  8839. (org-set-local 'org-done-keywords done-keywords)
  8840. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  8841. (org-fast-tag-insert "Current" current c-face "\n\n")
  8842. (org-fast-tag-show-exit exit-after-next)
  8843. (org-set-current-tags-overlay current ov-prefix)
  8844. (setq tbl fulltable char ?a cnt 0)
  8845. (while (setq e (pop tbl))
  8846. (cond
  8847. ((equal e '(:startgroup))
  8848. (push '() groups) (setq ingroup t)
  8849. (when (not (= cnt 0))
  8850. (setq cnt 0)
  8851. (insert "\n"))
  8852. (insert "{ "))
  8853. ((equal e '(:endgroup))
  8854. (setq ingroup nil cnt 0)
  8855. (insert "}\n"))
  8856. (t
  8857. (setq tg (car e) c2 nil)
  8858. (if (cdr e)
  8859. (setq c (cdr e))
  8860. ;; automatically assign a character.
  8861. (setq c1 (string-to-char
  8862. (downcase (substring
  8863. tg (if (= (string-to-char tg) ?@) 1 0)))))
  8864. (if (or (rassoc c1 ntable) (rassoc c1 table))
  8865. (while (or (rassoc char ntable) (rassoc char table))
  8866. (setq char (1+ char)))
  8867. (setq c2 c1))
  8868. (setq c (or c2 char)))
  8869. (if ingroup (push tg (car groups)))
  8870. (setq tg (org-add-props tg nil 'face
  8871. (cond
  8872. ((not (assoc tg table))
  8873. (org-get-todo-face tg))
  8874. ((member tg current) c-face)
  8875. ((member tg inherited) i-face)
  8876. (t nil))))
  8877. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8878. (insert "[" c "] " tg (make-string
  8879. (- fwidth 4 (length tg)) ?\ ))
  8880. (push (cons tg c) ntable)
  8881. (when (= (setq cnt (1+ cnt)) ncol)
  8882. (insert "\n")
  8883. (if ingroup (insert " "))
  8884. (setq cnt 0)))))
  8885. (setq ntable (nreverse ntable))
  8886. (insert "\n")
  8887. (goto-char (point-min))
  8888. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  8889. (fit-window-to-buffer))
  8890. (setq rtn
  8891. (catch 'exit
  8892. (while t
  8893. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  8894. (if groups " [!] no groups" " [!]groups")
  8895. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  8896. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8897. (cond
  8898. ((= c ?\r) (throw 'exit t))
  8899. ((= c ?!)
  8900. (setq groups (not groups))
  8901. (goto-char (point-min))
  8902. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  8903. ((= c ?\C-c)
  8904. (if (not expert)
  8905. (org-fast-tag-show-exit
  8906. (setq exit-after-next (not exit-after-next)))
  8907. (setq expert nil)
  8908. (delete-other-windows)
  8909. (split-window-vertically)
  8910. (org-switch-to-buffer-other-window " *Org tags*")
  8911. (and (fboundp 'fit-window-to-buffer)
  8912. (fit-window-to-buffer))))
  8913. ((or (= c ?\C-g)
  8914. (and (= c ?q) (not (rassoc c ntable))))
  8915. (org-detach-overlay org-tags-overlay)
  8916. (setq quit-flag t))
  8917. ((= c ?\ )
  8918. (setq current nil)
  8919. (if exit-after-next (setq exit-after-next 'now)))
  8920. ((= c ?\t)
  8921. (condition-case nil
  8922. (setq tg (completing-read
  8923. "Tag: "
  8924. (or buffer-tags
  8925. (with-current-buffer buf
  8926. (org-get-buffer-tags)))))
  8927. (quit (setq tg "")))
  8928. (when (string-match "\\S-" tg)
  8929. (add-to-list 'buffer-tags (list tg))
  8930. (if (member tg current)
  8931. (setq current (delete tg current))
  8932. (push tg current)))
  8933. (if exit-after-next (setq exit-after-next 'now)))
  8934. ((setq e (rassoc c todo-table) tg (car e))
  8935. (with-current-buffer buf
  8936. (save-excursion (org-todo tg)))
  8937. (if exit-after-next (setq exit-after-next 'now)))
  8938. ((setq e (rassoc c ntable) tg (car e))
  8939. (if (member tg current)
  8940. (setq current (delete tg current))
  8941. (loop for g in groups do
  8942. (if (member tg g)
  8943. (mapc (lambda (x)
  8944. (setq current (delete x current)))
  8945. g)))
  8946. (push tg current))
  8947. (if exit-after-next (setq exit-after-next 'now))))
  8948. ;; Create a sorted list
  8949. (setq current
  8950. (sort current
  8951. (lambda (a b)
  8952. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  8953. (if (eq exit-after-next 'now) (throw 'exit t))
  8954. (goto-char (point-min))
  8955. (beginning-of-line 2)
  8956. (delete-region (point) (point-at-eol))
  8957. (org-fast-tag-insert "Current" current c-face)
  8958. (org-set-current-tags-overlay current ov-prefix)
  8959. (while (re-search-forward
  8960. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  8961. (setq tg (match-string 1))
  8962. (add-text-properties
  8963. (match-beginning 1) (match-end 1)
  8964. (list 'face
  8965. (cond
  8966. ((member tg current) c-face)
  8967. ((member tg inherited) i-face)
  8968. (t (get-text-property (match-beginning 1) 'face))))))
  8969. (goto-char (point-min)))))
  8970. (org-detach-overlay org-tags-overlay)
  8971. (if rtn
  8972. (mapconcat 'identity current ":")
  8973. nil))))
  8974. (defun org-get-tags-string ()
  8975. "Get the TAGS string in the current headline."
  8976. (unless (org-on-heading-p t)
  8977. (error "Not on a heading"))
  8978. (save-excursion
  8979. (beginning-of-line 1)
  8980. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8981. (org-match-string-no-properties 1)
  8982. "")))
  8983. (defun org-get-tags ()
  8984. "Get the list of tags specified in the current headline."
  8985. (org-split-string (org-get-tags-string) ":"))
  8986. (defun org-get-buffer-tags ()
  8987. "Get a table of all tags used in the buffer, for completion."
  8988. (let (tags)
  8989. (save-excursion
  8990. (goto-char (point-min))
  8991. (while (re-search-forward
  8992. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  8993. (when (equal (char-after (point-at-bol 0)) ?*)
  8994. (mapc (lambda (x) (add-to-list 'tags x))
  8995. (org-split-string (org-match-string-no-properties 1) ":")))))
  8996. (mapcar 'list tags)))
  8997. ;;;; Properties
  8998. ;;; Setting and retrieving properties
  8999. (defconst org-special-properties
  9000. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
  9001. "TIMESTAMP" "TIMESTAMP_IA")
  9002. "The special properties valid in Org-mode.
  9003. These are properties that are not defined in the property drawer,
  9004. but in some other way.")
  9005. (defconst org-default-properties
  9006. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  9007. "LOCATION" "LOGGING" "COLUMNS")
  9008. "Some properties that are used by Org-mode for various purposes.
  9009. Being in this list makes sure that they are offered for completion.")
  9010. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  9011. "Regular expression matching the first line of a property drawer.")
  9012. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  9013. "Regular expression matching the first line of a property drawer.")
  9014. (defun org-property-action ()
  9015. "Do an action on properties."
  9016. (interactive)
  9017. (let (c)
  9018. (org-at-property-p)
  9019. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  9020. (setq c (read-char-exclusive))
  9021. (cond
  9022. ((equal c ?s)
  9023. (call-interactively 'org-set-property))
  9024. ((equal c ?d)
  9025. (call-interactively 'org-delete-property))
  9026. ((equal c ?D)
  9027. (call-interactively 'org-delete-property-globally))
  9028. ((equal c ?c)
  9029. (call-interactively 'org-compute-property-at-point))
  9030. (t (error "No such property action %c" c)))))
  9031. (defun org-at-property-p ()
  9032. "Is the cursor in a property line?"
  9033. ;; FIXME: Does not check if we are actually in the drawer.
  9034. ;; FIXME: also returns true on any drawers.....
  9035. ;; This is used by C-c C-c for property action.
  9036. (save-excursion
  9037. (beginning-of-line 1)
  9038. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  9039. (defun org-get-property-block (&optional beg end force)
  9040. "Return the (beg . end) range of the body of the property drawer.
  9041. BEG and END can be beginning and end of subtree, if not given
  9042. they will be found.
  9043. If the drawer does not exist and FORCE is non-nil, create the drawer."
  9044. (catch 'exit
  9045. (save-excursion
  9046. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  9047. (end (or end (progn (outline-next-heading) (point)))))
  9048. (goto-char beg)
  9049. (if (re-search-forward org-property-start-re end t)
  9050. (setq beg (1+ (match-end 0)))
  9051. (if force
  9052. (save-excursion
  9053. (org-insert-property-drawer)
  9054. (setq end (progn (outline-next-heading) (point))))
  9055. (throw 'exit nil))
  9056. (goto-char beg)
  9057. (if (re-search-forward org-property-start-re end t)
  9058. (setq beg (1+ (match-end 0)))))
  9059. (if (re-search-forward org-property-end-re end t)
  9060. (setq end (match-beginning 0))
  9061. (or force (throw 'exit nil))
  9062. (goto-char beg)
  9063. (setq end beg)
  9064. (org-indent-line-function)
  9065. (insert ":END:\n"))
  9066. (cons beg end)))))
  9067. (defun org-entry-properties (&optional pom which)
  9068. "Get all properties of the entry at point-or-marker POM.
  9069. This includes the TODO keyword, the tags, time strings for deadline,
  9070. scheduled, and clocking, and any additional properties defined in the
  9071. entry. The return value is an alist, keys may occur multiple times
  9072. if the property key was used several times.
  9073. POM may also be nil, in which case the current entry is used.
  9074. If WHICH is nil or `all', get all properties. If WHICH is
  9075. `special' or `standard', only get that subclass."
  9076. (setq which (or which 'all))
  9077. (org-with-point-at pom
  9078. (let ((clockstr (substring org-clock-string 0 -1))
  9079. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  9080. beg end range props sum-props key value string clocksum)
  9081. (save-excursion
  9082. (when (condition-case nil (org-back-to-heading t) (error nil))
  9083. (setq beg (point))
  9084. (setq sum-props (get-text-property (point) 'org-summaries))
  9085. (setq clocksum (get-text-property (point) :org-clock-minutes))
  9086. (outline-next-heading)
  9087. (setq end (point))
  9088. (when (memq which '(all special))
  9089. ;; Get the special properties, like TODO and tags
  9090. (goto-char beg)
  9091. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  9092. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  9093. (when (looking-at org-priority-regexp)
  9094. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  9095. (when (and (setq value (org-get-tags-string))
  9096. (string-match "\\S-" value))
  9097. (push (cons "TAGS" value) props))
  9098. (when (setq value (org-get-tags-at))
  9099. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  9100. props))
  9101. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  9102. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  9103. string (if (equal key clockstr)
  9104. (org-no-properties
  9105. (org-trim
  9106. (buffer-substring
  9107. (match-beginning 3) (goto-char (point-at-eol)))))
  9108. (substring (org-match-string-no-properties 3) 1 -1)))
  9109. (unless key
  9110. (if (= (char-after (match-beginning 3)) ?\[)
  9111. (setq key "TIMESTAMP_IA")
  9112. (setq key "TIMESTAMP")))
  9113. (when (or (equal key clockstr) (not (assoc key props)))
  9114. (push (cons key string) props)))
  9115. )
  9116. (when (memq which '(all standard))
  9117. ;; Get the standard properties, like :PORP: ...
  9118. (setq range (org-get-property-block beg end))
  9119. (when range
  9120. (goto-char (car range))
  9121. (while (re-search-forward
  9122. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  9123. (cdr range) t)
  9124. (setq key (org-match-string-no-properties 1)
  9125. value (org-trim (or (org-match-string-no-properties 2) "")))
  9126. (unless (member key excluded)
  9127. (push (cons key (or value "")) props)))))
  9128. (if clocksum
  9129. (push (cons "CLOCKSUM"
  9130. (org-column-number-to-string (/ (float clocksum) 60.)
  9131. 'add_times))
  9132. props))
  9133. (append sum-props (nreverse props)))))))
  9134. (defun org-entry-get (pom property &optional inherit)
  9135. "Get value of PROPERTY for entry at point-or-marker POM.
  9136. If INHERIT is non-nil and the entry does not have the property,
  9137. then also check higher levels of the hierarchy.
  9138. If INHERIT is the symbol `selective', use inheritance only if the setting
  9139. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  9140. If the property is present but empty, the return value is the empty string.
  9141. If the property is not present at all, nil is returned."
  9142. (org-with-point-at pom
  9143. (if (and inherit (if (eq inherit 'selective)
  9144. (org-property-inherit-p property)
  9145. t))
  9146. (org-entry-get-with-inheritance property)
  9147. (if (member property org-special-properties)
  9148. ;; We need a special property. Use brute force, get all properties.
  9149. (cdr (assoc property (org-entry-properties nil 'special)))
  9150. (let ((range (org-get-property-block)))
  9151. (if (and range
  9152. (goto-char (car range))
  9153. (re-search-forward
  9154. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  9155. (cdr range) t))
  9156. ;; Found the property, return it.
  9157. (if (match-end 1)
  9158. (org-match-string-no-properties 1)
  9159. "")))))))
  9160. (defun org-entry-delete (pom property)
  9161. "Delete the property PROPERTY from entry at point-or-marker POM."
  9162. (org-with-point-at pom
  9163. (if (member property org-special-properties)
  9164. nil ; cannot delete these properties.
  9165. (let ((range (org-get-property-block)))
  9166. (if (and range
  9167. (goto-char (car range))
  9168. (re-search-forward
  9169. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  9170. (cdr range) t))
  9171. (progn
  9172. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  9173. t)
  9174. nil)))))
  9175. ;; Multi-values properties are properties that contain multiple values
  9176. ;; These values are assumed to be single words, separated by whitespace.
  9177. (defun org-entry-add-to-multivalued-property (pom property value)
  9178. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  9179. (let* ((old (org-entry-get pom property))
  9180. (values (and old (org-split-string old "[ \t]"))))
  9181. (unless (member value values)
  9182. (setq values (cons value values))
  9183. (org-entry-put pom property
  9184. (mapconcat 'identity values " ")))))
  9185. (defun org-entry-remove-from-multivalued-property (pom property value)
  9186. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  9187. (let* ((old (org-entry-get pom property))
  9188. (values (and old (org-split-string old "[ \t]"))))
  9189. (when (member value values)
  9190. (setq values (delete value values))
  9191. (org-entry-put pom property
  9192. (mapconcat 'identity values " ")))))
  9193. (defun org-entry-member-in-multivalued-property (pom property value)
  9194. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  9195. (let* ((old (org-entry-get pom property))
  9196. (values (and old (org-split-string old "[ \t]"))))
  9197. (member value values)))
  9198. (defvar org-entry-property-inherited-from (make-marker))
  9199. (defun org-entry-get-with-inheritance (property)
  9200. "Get entry property, and search higher levels if not present."
  9201. (let (tmp)
  9202. (save-excursion
  9203. (save-restriction
  9204. (widen)
  9205. (catch 'ex
  9206. (while t
  9207. (when (setq tmp (org-entry-get nil property))
  9208. (org-back-to-heading t)
  9209. (move-marker org-entry-property-inherited-from (point))
  9210. (throw 'ex tmp))
  9211. (or (org-up-heading-safe) (throw 'ex nil)))))
  9212. (or tmp (cdr (assoc property org-local-properties))
  9213. (cdr (assoc property org-global-properties))))))
  9214. (defun org-entry-put (pom property value)
  9215. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  9216. (org-with-point-at pom
  9217. (org-back-to-heading t)
  9218. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  9219. range)
  9220. (cond
  9221. ((equal property "TODO")
  9222. (when (and (stringp value) (string-match "\\S-" value)
  9223. (not (member value org-todo-keywords-1)))
  9224. (error "\"%s\" is not a valid TODO state" value))
  9225. (if (or (not value)
  9226. (not (string-match "\\S-" value)))
  9227. (setq value 'none))
  9228. (org-todo value)
  9229. (org-set-tags nil 'align))
  9230. ((equal property "PRIORITY")
  9231. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  9232. (string-to-char value) ?\ ))
  9233. (org-set-tags nil 'align))
  9234. ((equal property "SCHEDULED")
  9235. (if (re-search-forward org-scheduled-time-regexp end t)
  9236. (cond
  9237. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9238. ((eq value 'later) (org-timestamp-change 1 'day))
  9239. (t (call-interactively 'org-schedule)))
  9240. (call-interactively 'org-schedule)))
  9241. ((equal property "DEADLINE")
  9242. (if (re-search-forward org-deadline-time-regexp end t)
  9243. (cond
  9244. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9245. ((eq value 'later) (org-timestamp-change 1 'day))
  9246. (t (call-interactively 'org-deadline)))
  9247. (call-interactively 'org-deadline)))
  9248. ((member property org-special-properties)
  9249. (error "The %s property can not yet be set with `org-entry-put'"
  9250. property))
  9251. (t ; a non-special property
  9252. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  9253. (setq range (org-get-property-block beg end 'force))
  9254. (goto-char (car range))
  9255. (if (re-search-forward
  9256. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  9257. (progn
  9258. (delete-region (match-beginning 1) (match-end 1))
  9259. (goto-char (match-beginning 1)))
  9260. (goto-char (cdr range))
  9261. (insert "\n")
  9262. (backward-char 1)
  9263. (org-indent-line-function)
  9264. (insert ":" property ":"))
  9265. (and value (insert " " value))
  9266. (org-indent-line-function)))))))
  9267. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  9268. "Get all property keys in the current buffer.
  9269. With INCLUDE-SPECIALS, also list the special properties that relect things
  9270. like tags and TODO state.
  9271. With INCLUDE-DEFAULTS, also include properties that has special meaning
  9272. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  9273. With INCLUDE-COLUMNS, also include property names given in COLUMN
  9274. formats in the current buffer."
  9275. (let (rtn range cfmt cols s p)
  9276. (save-excursion
  9277. (save-restriction
  9278. (widen)
  9279. (goto-char (point-min))
  9280. (while (re-search-forward org-property-start-re nil t)
  9281. (setq range (org-get-property-block))
  9282. (goto-char (car range))
  9283. (while (re-search-forward
  9284. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  9285. (cdr range) t)
  9286. (add-to-list 'rtn (org-match-string-no-properties 1)))
  9287. (outline-next-heading))))
  9288. (when include-specials
  9289. (setq rtn (append org-special-properties rtn)))
  9290. (when include-defaults
  9291. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  9292. (when include-columns
  9293. (save-excursion
  9294. (save-restriction
  9295. (widen)
  9296. (goto-char (point-min))
  9297. (while (re-search-forward
  9298. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  9299. nil t)
  9300. (setq cfmt (match-string 2) s 0)
  9301. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  9302. cfmt s)
  9303. (setq s (match-end 0)
  9304. p (match-string 1 cfmt))
  9305. (unless (or (equal p "ITEM")
  9306. (member p org-special-properties))
  9307. (add-to-list 'rtn (match-string 1 cfmt))))))))
  9308. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  9309. (defun org-property-values (key)
  9310. "Return a list of all values of property KEY."
  9311. (save-excursion
  9312. (save-restriction
  9313. (widen)
  9314. (goto-char (point-min))
  9315. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  9316. values)
  9317. (while (re-search-forward re nil t)
  9318. (add-to-list 'values (org-trim (match-string 1))))
  9319. (delete "" values)))))
  9320. (defun org-insert-property-drawer ()
  9321. "Insert a property drawer into the current entry."
  9322. (interactive)
  9323. (org-back-to-heading t)
  9324. (looking-at outline-regexp)
  9325. (let ((indent (- (match-end 0)(match-beginning 0)))
  9326. (beg (point))
  9327. (re (concat "^[ \t]*" org-keyword-time-regexp))
  9328. end hiddenp)
  9329. (outline-next-heading)
  9330. (setq end (point))
  9331. (goto-char beg)
  9332. (while (re-search-forward re end t))
  9333. (setq hiddenp (org-invisible-p))
  9334. (end-of-line 1)
  9335. (and (equal (char-after) ?\n) (forward-char 1))
  9336. (org-skip-over-state-notes)
  9337. (skip-chars-backward " \t\n\r")
  9338. (if (eq (char-before) ?*) (forward-char 1))
  9339. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  9340. (beginning-of-line 0)
  9341. (indent-to-column indent)
  9342. (beginning-of-line 2)
  9343. (indent-to-column indent)
  9344. (beginning-of-line 0)
  9345. (if hiddenp
  9346. (save-excursion
  9347. (org-back-to-heading t)
  9348. (hide-entry))
  9349. (org-flag-drawer t))))
  9350. (defun org-set-property (property value)
  9351. "In the current entry, set PROPERTY to VALUE.
  9352. When called interactively, this will prompt for a property name, offering
  9353. completion on existing and default properties. And then it will prompt
  9354. for a value, offering competion either on allowed values (via an inherited
  9355. xxx_ALL property) or on existing values in other instances of this property
  9356. in the current file."
  9357. (interactive
  9358. (let* ((prop (completing-read
  9359. "Property: " (mapcar 'list (org-buffer-property-keys nil t t))))
  9360. (cur (org-entry-get nil prop))
  9361. (allowed (org-property-get-allowed-values nil prop 'table))
  9362. (existing (mapcar 'list (org-property-values prop)))
  9363. (val (if allowed
  9364. (completing-read "Value: " allowed nil 'req-match)
  9365. (completing-read
  9366. (concat "Value" (if (and cur (string-match "\\S-" cur))
  9367. (concat "[" cur "]") "")
  9368. ": ")
  9369. existing nil nil "" nil cur))))
  9370. (list prop (if (equal val "") cur val))))
  9371. (unless (equal (org-entry-get nil property) value)
  9372. (org-entry-put nil property value)))
  9373. (defun org-delete-property (property)
  9374. "In the current entry, delete PROPERTY."
  9375. (interactive
  9376. (let* ((prop (completing-read
  9377. "Property: " (org-entry-properties nil 'standard))))
  9378. (list prop)))
  9379. (message "Property %s %s" property
  9380. (if (org-entry-delete nil property)
  9381. "deleted"
  9382. "was not present in the entry")))
  9383. (defun org-delete-property-globally (property)
  9384. "Remove PROPERTY globally, from all entries."
  9385. (interactive
  9386. (let* ((prop (completing-read
  9387. "Globally remove property: "
  9388. (mapcar 'list (org-buffer-property-keys)))))
  9389. (list prop)))
  9390. (save-excursion
  9391. (save-restriction
  9392. (widen)
  9393. (goto-char (point-min))
  9394. (let ((cnt 0))
  9395. (while (re-search-forward
  9396. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  9397. nil t)
  9398. (setq cnt (1+ cnt))
  9399. (replace-match ""))
  9400. (message "Property \"%s\" removed from %d entries" property cnt)))))
  9401. (defvar org-columns-current-fmt-compiled) ; defined below
  9402. (defun org-compute-property-at-point ()
  9403. "Compute the property at point.
  9404. This looks for an enclosing column format, extracts the operator and
  9405. then applies it to the proerty in the column format's scope."
  9406. (interactive)
  9407. (unless (org-at-property-p)
  9408. (error "Not at a property"))
  9409. (let ((prop (org-match-string-no-properties 2)))
  9410. (org-columns-get-format-and-top-level)
  9411. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  9412. (error "No operator defined for property %s" prop))
  9413. (org-columns-compute prop)))
  9414. (defun org-property-get-allowed-values (pom property &optional table)
  9415. "Get allowed values for the property PROPERTY.
  9416. When TABLE is non-nil, return an alist that can directly be used for
  9417. completion."
  9418. (let (vals)
  9419. (cond
  9420. ((equal property "TODO")
  9421. (setq vals (org-with-point-at pom
  9422. (append org-todo-keywords-1 '("")))))
  9423. ((equal property "PRIORITY")
  9424. (let ((n org-lowest-priority))
  9425. (while (>= n org-highest-priority)
  9426. (push (char-to-string n) vals)
  9427. (setq n (1- n)))))
  9428. ((member property org-special-properties))
  9429. (t
  9430. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  9431. (when (and vals (string-match "\\S-" vals))
  9432. (setq vals (car (read-from-string (concat "(" vals ")"))))
  9433. (setq vals (mapcar (lambda (x)
  9434. (cond ((stringp x) x)
  9435. ((numberp x) (number-to-string x))
  9436. ((symbolp x) (symbol-name x))
  9437. (t "???")))
  9438. vals)))))
  9439. (if table (mapcar 'list vals) vals)))
  9440. (defun org-property-previous-allowed-value (&optional previous)
  9441. "Switch to the next allowed value for this property."
  9442. (interactive)
  9443. (org-property-next-allowed-value t))
  9444. (defun org-property-next-allowed-value (&optional previous)
  9445. "Switch to the next allowed value for this property."
  9446. (interactive)
  9447. (unless (org-at-property-p)
  9448. (error "Not at a property"))
  9449. (let* ((key (match-string 2))
  9450. (value (match-string 3))
  9451. (allowed (or (org-property-get-allowed-values (point) key)
  9452. (and (member value '("[ ]" "[-]" "[X]"))
  9453. '("[ ]" "[X]"))))
  9454. nval)
  9455. (unless allowed
  9456. (error "Allowed values for this property have not been defined"))
  9457. (if previous (setq allowed (reverse allowed)))
  9458. (if (member value allowed)
  9459. (setq nval (car (cdr (member value allowed)))))
  9460. (setq nval (or nval (car allowed)))
  9461. (if (equal nval value)
  9462. (error "Only one allowed value for this property"))
  9463. (org-at-property-p)
  9464. (replace-match (concat " :" key ": " nval) t t)
  9465. (org-indent-line-function)
  9466. (beginning-of-line 1)
  9467. (skip-chars-forward " \t")))
  9468. (defun org-find-entry-with-id (ident)
  9469. "Locate the entry that contains the ID property with exact value IDENT.
  9470. IDENT can be a string, a symbol or a number, this function will search for
  9471. the string representation of it.
  9472. Return the position where this entry starts, or nil if there is no such entry."
  9473. (let ((id (cond
  9474. ((stringp ident) ident)
  9475. ((symbol-name ident) (symbol-name ident))
  9476. ((numberp ident) (number-to-string ident))
  9477. (t (error "IDENT %s must be a string, symbol or number" ident))))
  9478. (case-fold-search nil))
  9479. (save-excursion
  9480. (save-restriction
  9481. (widen)
  9482. (goto-char (point-min))
  9483. (when (re-search-forward
  9484. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  9485. nil t)
  9486. (org-back-to-heading)
  9487. (point))))))
  9488. ;;; Column View
  9489. (defvar org-columns-overlays nil
  9490. "Holds the list of current column overlays.")
  9491. (defvar org-columns-current-fmt nil
  9492. "Local variable, holds the currently active column format.")
  9493. (defvar org-columns-current-fmt-compiled nil
  9494. "Local variable, holds the currently active column format.
  9495. This is the compiled version of the format.")
  9496. (defvar org-columns-current-widths nil
  9497. "Loval variable, holds the currently widths of fields.")
  9498. (defvar org-columns-current-maxwidths nil
  9499. "Loval variable, holds the currently active maximum column widths.")
  9500. (defvar org-columns-begin-marker (make-marker)
  9501. "Points to the position where last a column creation command was called.")
  9502. (defvar org-columns-top-level-marker (make-marker)
  9503. "Points to the position where current columns region starts.")
  9504. (defvar org-columns-map (make-sparse-keymap)
  9505. "The keymap valid in column display.")
  9506. (defun org-columns-content ()
  9507. "Switch to contents view while in columns view."
  9508. (interactive)
  9509. (org-overview)
  9510. (org-content))
  9511. (org-defkey org-columns-map "c" 'org-columns-content)
  9512. (org-defkey org-columns-map "o" 'org-overview)
  9513. (org-defkey org-columns-map "e" 'org-columns-edit-value)
  9514. (org-defkey org-columns-map "\C-c\C-t" 'org-columns-todo)
  9515. (org-defkey org-columns-map "\C-c\C-c" 'org-columns-set-tags-or-toggle)
  9516. (org-defkey org-columns-map "\C-c\C-o" 'org-columns-open-link)
  9517. (org-defkey org-columns-map "v" 'org-columns-show-value)
  9518. (org-defkey org-columns-map "q" 'org-columns-quit)
  9519. (org-defkey org-columns-map "r" 'org-columns-redo)
  9520. (org-defkey org-columns-map "g" 'org-columns-redo)
  9521. (org-defkey org-columns-map [left] 'backward-char)
  9522. (org-defkey org-columns-map "\M-b" 'backward-char)
  9523. (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
  9524. (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
  9525. (org-defkey org-columns-map "\M-f" (lambda () (interactive) (goto-char (1+ (point)))))
  9526. (org-defkey org-columns-map [right] (lambda () (interactive) (goto-char (1+ (point)))))
  9527. (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
  9528. (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
  9529. (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
  9530. (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
  9531. (org-defkey org-columns-map "<" 'org-columns-narrow)
  9532. (org-defkey org-columns-map ">" 'org-columns-widen)
  9533. (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
  9534. (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
  9535. (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
  9536. (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
  9537. (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
  9538. '("Column"
  9539. ["Edit property" org-columns-edit-value t]
  9540. ["Next allowed value" org-columns-next-allowed-value t]
  9541. ["Previous allowed value" org-columns-previous-allowed-value t]
  9542. ["Show full value" org-columns-show-value t]
  9543. ["Edit allowed values" org-columns-edit-allowed t]
  9544. "--"
  9545. ["Edit column attributes" org-columns-edit-attributes t]
  9546. ["Increase column width" org-columns-widen t]
  9547. ["Decrease column width" org-columns-narrow t]
  9548. "--"
  9549. ["Move column right" org-columns-move-right t]
  9550. ["Move column left" org-columns-move-left t]
  9551. ["Add column" org-columns-new t]
  9552. ["Delete column" org-columns-delete t]
  9553. "--"
  9554. ["CONTENTS" org-columns-content t]
  9555. ["OVERVIEW" org-overview t]
  9556. ["Refresh columns display" org-columns-redo t]
  9557. "--"
  9558. ["Open link" org-columns-open-link t]
  9559. "--"
  9560. ["Quit" org-columns-quit t]))
  9561. (defun org-columns-new-overlay (beg end &optional string face)
  9562. "Create a new column overlay and add it to the list."
  9563. (let ((ov (org-make-overlay beg end)))
  9564. (org-overlay-put ov 'face (or face 'secondary-selection))
  9565. (org-overlay-display ov string face)
  9566. (push ov org-columns-overlays)
  9567. ov))
  9568. (defun org-columns-display-here (&optional props)
  9569. "Overlay the current line with column display."
  9570. (interactive)
  9571. (let* ((fmt org-columns-current-fmt-compiled)
  9572. (beg (point-at-bol))
  9573. (level-face (save-excursion
  9574. (beginning-of-line 1)
  9575. (and (looking-at "\\(\\**\\)\\(\\* \\)")
  9576. (org-get-level-face 2))))
  9577. (color (list :foreground
  9578. (face-attribute (or level-face 'default) :foreground)))
  9579. props pom property ass width f string ov column val modval)
  9580. ;; Check if the entry is in another buffer.
  9581. (unless props
  9582. (if (eq major-mode 'org-agenda-mode)
  9583. (setq pom (or (get-text-property (point) 'org-hd-marker)
  9584. (get-text-property (point) 'org-marker))
  9585. props (if pom (org-entry-properties pom) nil))
  9586. (setq props (org-entry-properties nil))))
  9587. ;; Walk the format
  9588. (while (setq column (pop fmt))
  9589. (setq property (car column)
  9590. ass (if (equal property "ITEM")
  9591. (cons "ITEM"
  9592. (save-match-data
  9593. (org-no-properties
  9594. (org-remove-tabs
  9595. (buffer-substring-no-properties
  9596. (point-at-bol) (point-at-eol))))))
  9597. (assoc property props))
  9598. width (or (cdr (assoc property org-columns-current-maxwidths))
  9599. (nth 2 column)
  9600. (length property))
  9601. f (format "%%-%d.%ds | " width width)
  9602. val (or (cdr ass) "")
  9603. modval (if (equal property "ITEM")
  9604. (org-columns-cleanup-item val org-columns-current-fmt-compiled))
  9605. string (format f (or modval val)))
  9606. ;; Create the overlay
  9607. (org-unmodified
  9608. (setq ov (org-columns-new-overlay
  9609. beg (setq beg (1+ beg)) string
  9610. (list color 'org-column)))
  9611. ;;; (list (get-text-property (point-at-bol) 'face) 'org-column)))
  9612. (org-overlay-put ov 'keymap org-columns-map)
  9613. (org-overlay-put ov 'org-columns-key property)
  9614. (org-overlay-put ov 'org-columns-value (cdr ass))
  9615. (org-overlay-put ov 'org-columns-value-modified modval)
  9616. (org-overlay-put ov 'org-columns-pom pom)
  9617. (org-overlay-put ov 'org-columns-format f))
  9618. (if (or (not (char-after beg))
  9619. (equal (char-after beg) ?\n))
  9620. (let ((inhibit-read-only t))
  9621. (save-excursion
  9622. (goto-char beg)
  9623. (org-unmodified (insert " ")))))) ;; FIXME: add props and remove later?
  9624. ;; Make the rest of the line disappear.
  9625. (org-unmodified
  9626. (setq ov (org-columns-new-overlay beg (point-at-eol)))
  9627. (org-overlay-put ov 'invisible t)
  9628. (org-overlay-put ov 'keymap org-columns-map)
  9629. (org-overlay-put ov 'intangible t)
  9630. (push ov org-columns-overlays)
  9631. (setq ov (org-make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
  9632. (org-overlay-put ov 'keymap org-columns-map)
  9633. (push ov org-columns-overlays)
  9634. (let ((inhibit-read-only t))
  9635. (put-text-property (max (point-min) (1- (point-at-bol)))
  9636. (min (point-max) (1+ (point-at-eol)))
  9637. 'read-only "Type `e' to edit property")))))
  9638. (defvar org-previous-header-line-format nil
  9639. "The header line format before column view was turned on.")
  9640. (defvar org-columns-inhibit-recalculation nil
  9641. "Inhibit recomputing of columns on column view startup.")
  9642. (defvar header-line-format)
  9643. (defun org-columns-display-here-title ()
  9644. "Overlay the newline before the current line with the table title."
  9645. (interactive)
  9646. (let ((fmt org-columns-current-fmt-compiled)
  9647. string (title "")
  9648. property width f column str widths)
  9649. (while (setq column (pop fmt))
  9650. (setq property (car column)
  9651. str (or (nth 1 column) property)
  9652. width (or (cdr (assoc property org-columns-current-maxwidths))
  9653. (nth 2 column)
  9654. (length str))
  9655. widths (push width widths)
  9656. f (format "%%-%d.%ds | " width width)
  9657. string (format f str)
  9658. title (concat title string)))
  9659. (setq title (concat
  9660. (org-add-props " " nil 'display '(space :align-to 0))
  9661. (org-add-props title nil 'face '(:weight bold :underline t))))
  9662. (org-set-local 'org-previous-header-line-format header-line-format)
  9663. (org-set-local 'org-columns-current-widths (nreverse widths))
  9664. (setq header-line-format title)))
  9665. (defun org-columns-remove-overlays ()
  9666. "Remove all currently active column overlays."
  9667. (interactive)
  9668. (when (marker-buffer org-columns-begin-marker)
  9669. (with-current-buffer (marker-buffer org-columns-begin-marker)
  9670. (when (local-variable-p 'org-previous-header-line-format)
  9671. (setq header-line-format org-previous-header-line-format)
  9672. (kill-local-variable 'org-previous-header-line-format))
  9673. (move-marker org-columns-begin-marker nil)
  9674. (move-marker org-columns-top-level-marker nil)
  9675. (org-unmodified
  9676. (mapc 'org-delete-overlay org-columns-overlays)
  9677. (setq org-columns-overlays nil)
  9678. (let ((inhibit-read-only t))
  9679. (remove-text-properties (point-min) (point-max) '(read-only t)))))))
  9680. (defun org-columns-cleanup-item (item fmt)
  9681. "Remove from ITEM what is a column in the format FMT."
  9682. (if (not org-complex-heading-regexp)
  9683. item
  9684. (when (string-match org-complex-heading-regexp item)
  9685. (concat
  9686. (org-add-props (concat (match-string 1 item) " ") nil
  9687. 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
  9688. (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
  9689. (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
  9690. " " (match-string 4 item)
  9691. (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))))
  9692. (defun org-columns-show-value ()
  9693. "Show the full value of the property."
  9694. (interactive)
  9695. (let ((value (get-char-property (point) 'org-columns-value)))
  9696. (message "Value is: %s" (or value ""))))
  9697. (defun org-columns-quit ()
  9698. "Remove the column overlays and in this way exit column editing."
  9699. (interactive)
  9700. (org-unmodified
  9701. (org-columns-remove-overlays)
  9702. (let ((inhibit-read-only t))
  9703. (remove-text-properties (point-min) (point-max) '(read-only t))))
  9704. (when (eq major-mode 'org-agenda-mode)
  9705. (message
  9706. "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
  9707. (defun org-columns-check-computed ()
  9708. "Check if this column value is computed.
  9709. If yes, throw an error indicating that changing it does not make sense."
  9710. (let ((val (get-char-property (point) 'org-columns-value)))
  9711. (when (and (stringp val)
  9712. (get-char-property 0 'org-computed val))
  9713. (error "This value is computed from the entry's children"))))
  9714. (defun org-columns-todo (&optional arg)
  9715. "Change the TODO state during column view."
  9716. (interactive "P")
  9717. (org-columns-edit-value "TODO"))
  9718. (defun org-columns-set-tags-or-toggle (&optional arg)
  9719. "Toggle checkbox at point, or set tags for current headline."
  9720. (interactive "P")
  9721. (if (string-match "\\`\\[[ xX-]\\]\\'"
  9722. (get-char-property (point) 'org-columns-value))
  9723. (org-columns-next-allowed-value)
  9724. (org-columns-edit-value "TAGS")))
  9725. (defun org-columns-edit-value (&optional key)
  9726. "Edit the value of the property at point in column view.
  9727. Where possible, use the standard interface for changing this line."
  9728. (interactive)
  9729. (org-columns-check-computed)
  9730. (let* ((external-key key)
  9731. (col (current-column))
  9732. (key (or key (get-char-property (point) 'org-columns-key)))
  9733. (value (get-char-property (point) 'org-columns-value))
  9734. (bol (point-at-bol)) (eol (point-at-eol))
  9735. (pom (or (get-text-property bol 'org-hd-marker)
  9736. (point))) ; keep despite of compiler waring
  9737. (line-overlays
  9738. (delq nil (mapcar (lambda (x)
  9739. (and (eq (overlay-buffer x) (current-buffer))
  9740. (>= (overlay-start x) bol)
  9741. (<= (overlay-start x) eol)
  9742. x))
  9743. org-columns-overlays)))
  9744. nval eval allowed)
  9745. (cond
  9746. ((equal key "CLOCKSUM")
  9747. (error "This special column cannot be edited"))
  9748. ((equal key "ITEM")
  9749. (setq eval '(org-with-point-at pom
  9750. (org-edit-headline))))
  9751. ((equal key "TODO")
  9752. (setq eval '(org-with-point-at pom
  9753. (let ((current-prefix-arg
  9754. (if external-key current-prefix-arg '(4))))
  9755. (call-interactively 'org-todo)))))
  9756. ((equal key "PRIORITY")
  9757. (setq eval '(org-with-point-at pom
  9758. (call-interactively 'org-priority))))
  9759. ((equal key "TAGS")
  9760. (setq eval '(org-with-point-at pom
  9761. (let ((org-fast-tag-selection-single-key
  9762. (if (eq org-fast-tag-selection-single-key 'expert)
  9763. t org-fast-tag-selection-single-key)))
  9764. (call-interactively 'org-set-tags)))))
  9765. ((equal key "DEADLINE")
  9766. (setq eval '(org-with-point-at pom
  9767. (call-interactively 'org-deadline))))
  9768. ((equal key "SCHEDULED")
  9769. (setq eval '(org-with-point-at pom
  9770. (call-interactively 'org-schedule))))
  9771. (t
  9772. (setq allowed (org-property-get-allowed-values pom key 'table))
  9773. (if allowed
  9774. (setq nval (completing-read "Value: " allowed nil t))
  9775. (setq nval (read-string "Edit: " value)))
  9776. (setq nval (org-trim nval))
  9777. (when (not (equal nval value))
  9778. (setq eval '(org-entry-put pom key nval)))))
  9779. (when eval
  9780. (let ((inhibit-read-only t))
  9781. (remove-text-properties (max (point-min) (1- bol)) eol '(read-only t))
  9782. (unwind-protect
  9783. (progn
  9784. (setq org-columns-overlays
  9785. (org-delete-all line-overlays org-columns-overlays))
  9786. (mapc 'org-delete-overlay line-overlays)
  9787. (org-columns-eval eval))
  9788. (org-columns-display-here))))
  9789. (move-to-column col)
  9790. (if (and (org-mode-p)
  9791. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  9792. (org-columns-update key))))
  9793. (defun org-edit-headline () ; FIXME: this is not columns specific
  9794. "Edit the current headline, the part without TODO keyword, TAGS."
  9795. (org-back-to-heading)
  9796. (when (looking-at org-todo-line-regexp)
  9797. (let ((pre (buffer-substring (match-beginning 0) (match-beginning 3)))
  9798. (txt (match-string 3))
  9799. (post "")
  9800. txt2)
  9801. (if (string-match (org-re "[ \t]+:[[:alnum:]:_@]+:[ \t]*$") txt)
  9802. (setq post (match-string 0 txt)
  9803. txt (substring txt 0 (match-beginning 0))))
  9804. (setq txt2 (read-string "Edit: " txt))
  9805. (when (not (equal txt txt2))
  9806. (beginning-of-line 1)
  9807. (insert pre txt2 post)
  9808. (delete-region (point) (point-at-eol))
  9809. (org-set-tags nil t)))))
  9810. (defun org-columns-edit-allowed ()
  9811. "Edit the list of allowed values for the current property."
  9812. (interactive)
  9813. (let* ((key (get-char-property (point) 'org-columns-key))
  9814. (key1 (concat key "_ALL"))
  9815. (allowed (org-entry-get (point) key1 t))
  9816. nval)
  9817. ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
  9818. (setq nval (read-string "Allowed: " allowed))
  9819. (org-entry-put
  9820. (cond ((marker-position org-entry-property-inherited-from)
  9821. org-entry-property-inherited-from)
  9822. ((marker-position org-columns-top-level-marker)
  9823. org-columns-top-level-marker))
  9824. key1 nval)))
  9825. (defun org-columns-eval (form)
  9826. (let (hidep)
  9827. (save-excursion
  9828. (beginning-of-line 1)
  9829. ;; `next-line' is needed here, because it skips invisible line.
  9830. (condition-case nil (org-no-warnings (next-line 1)) (error nil))
  9831. (setq hidep (org-on-heading-p 1)))
  9832. (eval form)
  9833. (and hidep (hide-entry))))
  9834. (defun org-columns-previous-allowed-value ()
  9835. "Switch to the previous allowed value for this column."
  9836. (interactive)
  9837. (org-columns-next-allowed-value t))
  9838. (defun org-columns-next-allowed-value (&optional previous)
  9839. "Switch to the next allowed value for this column."
  9840. (interactive)
  9841. (org-columns-check-computed)
  9842. (let* ((col (current-column))
  9843. (key (get-char-property (point) 'org-columns-key))
  9844. (value (get-char-property (point) 'org-columns-value))
  9845. (bol (point-at-bol)) (eol (point-at-eol))
  9846. (pom (or (get-text-property bol 'org-hd-marker)
  9847. (point))) ; keep despite of compiler waring
  9848. (line-overlays
  9849. (delq nil (mapcar (lambda (x)
  9850. (and (eq (overlay-buffer x) (current-buffer))
  9851. (>= (overlay-start x) bol)
  9852. (<= (overlay-start x) eol)
  9853. x))
  9854. org-columns-overlays)))
  9855. (allowed (or (org-property-get-allowed-values pom key)
  9856. (and (memq
  9857. (nth 4 (assoc key org-columns-current-fmt-compiled))
  9858. '(checkbox checkbox-n-of-m checkbox-percent))
  9859. '("[ ]" "[X]"))))
  9860. nval)
  9861. (when (equal key "ITEM")
  9862. (error "Cannot edit item headline from here"))
  9863. (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
  9864. (error "Allowed values for this property have not been defined"))
  9865. (if (member key '("SCHEDULED" "DEADLINE"))
  9866. (setq nval (if previous 'earlier 'later))
  9867. (if previous (setq allowed (reverse allowed)))
  9868. (if (member value allowed)
  9869. (setq nval (car (cdr (member value allowed)))))
  9870. (setq nval (or nval (car allowed)))
  9871. (if (equal nval value)
  9872. (error "Only one allowed value for this property")))
  9873. (let ((inhibit-read-only t))
  9874. (remove-text-properties (1- bol) eol '(read-only t))
  9875. (unwind-protect
  9876. (progn
  9877. (setq org-columns-overlays
  9878. (org-delete-all line-overlays org-columns-overlays))
  9879. (mapc 'org-delete-overlay line-overlays)
  9880. (org-columns-eval '(org-entry-put pom key nval)))
  9881. (org-columns-display-here)))
  9882. (move-to-column col)
  9883. (if (and (org-mode-p)
  9884. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  9885. (org-columns-update key))))
  9886. (defun org-verify-version (task)
  9887. (cond
  9888. ((eq task 'columns)
  9889. (if (or (featurep 'xemacs)
  9890. (< emacs-major-version 22))
  9891. (error "Emacs 22 is required for the columns feature")))))
  9892. (defun org-columns-open-link (&optional arg)
  9893. (interactive "P")
  9894. (let ((value (get-char-property (point) 'org-columns-value)))
  9895. (org-open-link-from-string value arg)))
  9896. (defun org-open-link-from-string (s &optional arg)
  9897. "Open a link in the string S, as if it was in Org-mode."
  9898. (interactive)
  9899. (with-temp-buffer
  9900. (let ((org-inhibit-startup t))
  9901. (org-mode)
  9902. (insert s)
  9903. (goto-char (point-min))
  9904. (org-open-at-point arg))))
  9905. (defun org-columns-get-format-and-top-level ()
  9906. (let (fmt)
  9907. (when (condition-case nil (org-back-to-heading) (error nil))
  9908. (move-marker org-entry-property-inherited-from nil)
  9909. (setq fmt (org-entry-get nil "COLUMNS" t)))
  9910. (setq fmt (or fmt org-columns-default-format))
  9911. (org-set-local 'org-columns-current-fmt fmt)
  9912. (org-columns-compile-format fmt)
  9913. (if (marker-position org-entry-property-inherited-from)
  9914. (move-marker org-columns-top-level-marker
  9915. org-entry-property-inherited-from)
  9916. (move-marker org-columns-top-level-marker (point)))
  9917. fmt))
  9918. (defun org-columns ()
  9919. "Turn on column view on an org-mode file."
  9920. (interactive)
  9921. (org-verify-version 'columns)
  9922. (org-columns-remove-overlays)
  9923. (move-marker org-columns-begin-marker (point))
  9924. (let (beg end fmt cache maxwidths)
  9925. (setq fmt (org-columns-get-format-and-top-level))
  9926. (save-excursion
  9927. (goto-char org-columns-top-level-marker)
  9928. (setq beg (point))
  9929. (unless org-columns-inhibit-recalculation
  9930. (org-columns-compute-all))
  9931. (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
  9932. (point-max)))
  9933. ;; Get and cache the properties
  9934. (goto-char beg)
  9935. (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
  9936. (save-excursion
  9937. (save-restriction
  9938. (narrow-to-region beg end)
  9939. (org-clock-sum))))
  9940. (while (re-search-forward (concat "^" outline-regexp) end t)
  9941. (push (cons (org-current-line) (org-entry-properties)) cache))
  9942. (when cache
  9943. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  9944. (org-set-local 'org-columns-current-maxwidths maxwidths)
  9945. (org-columns-display-here-title)
  9946. (mapc (lambda (x)
  9947. (goto-line (car x))
  9948. (org-columns-display-here (cdr x)))
  9949. cache)))))
  9950. (defun org-columns-new (&optional prop title width op fmt &rest rest)
  9951. "Insert a new column, to the left of the current column."
  9952. (interactive)
  9953. (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
  9954. cell)
  9955. (setq prop (completing-read
  9956. "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
  9957. nil nil prop))
  9958. (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
  9959. (setq width (read-string "Column width: " (if width (number-to-string width))))
  9960. (if (string-match "\\S-" width)
  9961. (setq width (string-to-number width))
  9962. (setq width nil))
  9963. (setq fmt (completing-read "Summary [none]: "
  9964. '(("none") ("add_numbers") ("currency") ("add_times") ("checkbox") ("checkbox-n-of-m") ("checkbox-percent"))
  9965. nil t))
  9966. (if (string-match "\\S-" fmt)
  9967. (setq fmt (intern fmt))
  9968. (setq fmt nil))
  9969. (if (eq fmt 'none) (setq fmt nil))
  9970. (if editp
  9971. (progn
  9972. (setcar editp prop)
  9973. (setcdr editp (list title width nil fmt)))
  9974. (setq cell (nthcdr (1- (current-column))
  9975. org-columns-current-fmt-compiled))
  9976. (setcdr cell (cons (list prop title width nil fmt)
  9977. (cdr cell))))
  9978. (org-columns-store-format)
  9979. (org-columns-redo)))
  9980. (defun org-columns-delete ()
  9981. "Delete the column at point from columns view."
  9982. (interactive)
  9983. (let* ((n (current-column))
  9984. (title (nth 1 (nth n org-columns-current-fmt-compiled))))
  9985. (when (y-or-n-p
  9986. (format "Are you sure you want to remove column \"%s\"? " title))
  9987. (setq org-columns-current-fmt-compiled
  9988. (delq (nth n org-columns-current-fmt-compiled)
  9989. org-columns-current-fmt-compiled))
  9990. (org-columns-store-format)
  9991. (org-columns-redo)
  9992. (if (>= (current-column) (length org-columns-current-fmt-compiled))
  9993. (backward-char 1)))))
  9994. (defun org-columns-edit-attributes ()
  9995. "Edit the attributes of the current column."
  9996. (interactive)
  9997. (let* ((n (current-column))
  9998. (info (nth n org-columns-current-fmt-compiled)))
  9999. (apply 'org-columns-new info)))
  10000. (defun org-columns-widen (arg)
  10001. "Make the column wider by ARG characters."
  10002. (interactive "p")
  10003. (let* ((n (current-column))
  10004. (entry (nth n org-columns-current-fmt-compiled))
  10005. (width (or (nth 2 entry)
  10006. (cdr (assoc (car entry) org-columns-current-maxwidths)))))
  10007. (setq width (max 1 (+ width arg)))
  10008. (setcar (nthcdr 2 entry) width)
  10009. (org-columns-store-format)
  10010. (org-columns-redo)))
  10011. (defun org-columns-narrow (arg)
  10012. "Make the column nrrower by ARG characters."
  10013. (interactive "p")
  10014. (org-columns-widen (- arg)))
  10015. (defun org-columns-move-right ()
  10016. "Swap this column with the one to the right."
  10017. (interactive)
  10018. (let* ((n (current-column))
  10019. (cell (nthcdr n org-columns-current-fmt-compiled))
  10020. e)
  10021. (when (>= n (1- (length org-columns-current-fmt-compiled)))
  10022. (error "Cannot shift this column further to the right"))
  10023. (setq e (car cell))
  10024. (setcar cell (car (cdr cell)))
  10025. (setcdr cell (cons e (cdr (cdr cell))))
  10026. (org-columns-store-format)
  10027. (org-columns-redo)
  10028. (forward-char 1)))
  10029. (defun org-columns-move-left ()
  10030. "Swap this column with the one to the left."
  10031. (interactive)
  10032. (let* ((n (current-column)))
  10033. (when (= n 0)
  10034. (error "Cannot shift this column further to the left"))
  10035. (backward-char 1)
  10036. (org-columns-move-right)
  10037. (backward-char 1)))
  10038. (defun org-columns-store-format ()
  10039. "Store the text version of the current columns format in appropriate place.
  10040. This is either in the COLUMNS property of the node starting the current column
  10041. display, or in the #+COLUMNS line of the current buffer."
  10042. (let (fmt (cnt 0))
  10043. (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
  10044. (org-set-local 'org-columns-current-fmt fmt)
  10045. (if (marker-position org-columns-top-level-marker)
  10046. (save-excursion
  10047. (goto-char org-columns-top-level-marker)
  10048. (if (and (org-at-heading-p)
  10049. (org-entry-get nil "COLUMNS"))
  10050. (org-entry-put nil "COLUMNS" fmt)
  10051. (goto-char (point-min))
  10052. ;; Overwrite all #+COLUMNS lines....
  10053. (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
  10054. (setq cnt (1+ cnt))
  10055. (replace-match (concat "#+COLUMNS: " fmt) t t))
  10056. (unless (> cnt 0)
  10057. (goto-char (point-min))
  10058. (or (org-on-heading-p t) (outline-next-heading))
  10059. (let ((inhibit-read-only t))
  10060. (insert-before-markers "#+COLUMNS: " fmt "\n")))
  10061. (org-set-local 'org-columns-default-format fmt))))))
  10062. (defvar org-overriding-columns-format nil
  10063. "When set, overrides any other definition.")
  10064. (defvar org-agenda-view-columns-initially nil
  10065. "When set, switch to columns view immediately after creating the agenda.")
  10066. (defun org-agenda-columns ()
  10067. "Turn on column view in the agenda."
  10068. (interactive)
  10069. (org-verify-version 'columns)
  10070. (org-columns-remove-overlays)
  10071. (move-marker org-columns-begin-marker (point))
  10072. (let (fmt cache maxwidths m)
  10073. (cond
  10074. ((and (local-variable-p 'org-overriding-columns-format)
  10075. org-overriding-columns-format)
  10076. (setq fmt org-overriding-columns-format))
  10077. ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
  10078. (setq fmt (org-entry-get m "COLUMNS" t)))
  10079. ((and (boundp 'org-columns-current-fmt)
  10080. (local-variable-p 'org-columns-current-fmt)
  10081. org-columns-current-fmt)
  10082. (setq fmt org-columns-current-fmt))
  10083. ((setq m (next-single-property-change (point-min) 'org-hd-marker))
  10084. (setq m (get-text-property m 'org-hd-marker))
  10085. (setq fmt (org-entry-get m "COLUMNS" t))))
  10086. (setq fmt (or fmt org-columns-default-format))
  10087. (org-set-local 'org-columns-current-fmt fmt)
  10088. (org-columns-compile-format fmt)
  10089. (save-excursion
  10090. ;; Get and cache the properties
  10091. (goto-char (point-min))
  10092. (while (not (eobp))
  10093. (when (setq m (or (get-text-property (point) 'org-hd-marker)
  10094. (get-text-property (point) 'org-marker)))
  10095. (push (cons (org-current-line) (org-entry-properties m)) cache))
  10096. (beginning-of-line 2))
  10097. (when cache
  10098. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  10099. (org-set-local 'org-columns-current-maxwidths maxwidths)
  10100. (org-columns-display-here-title)
  10101. (mapc (lambda (x)
  10102. (goto-line (car x))
  10103. (org-columns-display-here (cdr x)))
  10104. cache)))))
  10105. (defun org-columns-get-autowidth-alist (s cache)
  10106. "Derive the maximum column widths from the format and the cache."
  10107. (let ((start 0) rtn)
  10108. (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
  10109. (push (cons (match-string 1 s) 1) rtn)
  10110. (setq start (match-end 0)))
  10111. (mapc (lambda (x)
  10112. (setcdr x (apply 'max
  10113. (mapcar
  10114. (lambda (y)
  10115. (length (or (cdr (assoc (car x) (cdr y))) " ")))
  10116. cache))))
  10117. rtn)
  10118. rtn))
  10119. (defun org-columns-compute-all ()
  10120. "Compute all columns that have operators defined."
  10121. (org-unmodified
  10122. (remove-text-properties (point-min) (point-max) '(org-summaries t)))
  10123. (let ((columns org-columns-current-fmt-compiled) col)
  10124. (while (setq col (pop columns))
  10125. (when (nth 3 col)
  10126. (save-excursion
  10127. (org-columns-compute (car col)))))))
  10128. (defun org-columns-update (property)
  10129. "Recompute PROPERTY, and update the columns display for it."
  10130. (org-columns-compute property)
  10131. (let (fmt val pos)
  10132. (save-excursion
  10133. (mapc (lambda (ov)
  10134. (when (equal (org-overlay-get ov 'org-columns-key) property)
  10135. (setq pos (org-overlay-start ov))
  10136. (goto-char pos)
  10137. (when (setq val (cdr (assoc property
  10138. (get-text-property
  10139. (point-at-bol) 'org-summaries))))
  10140. (setq fmt (org-overlay-get ov 'org-columns-format))
  10141. (org-overlay-put ov 'org-columns-value val)
  10142. (org-overlay-put ov 'display (format fmt val)))))
  10143. org-columns-overlays))))
  10144. (defun org-columns-compute (property)
  10145. "Sum the values of property PROPERTY hierarchically, for the entire buffer."
  10146. (interactive)
  10147. (let* ((re (concat "^" outline-regexp))
  10148. (lmax 30) ; Does anyone use deeper levels???
  10149. (lsum (make-vector lmax 0))
  10150. (lflag (make-vector lmax nil))
  10151. (level 0)
  10152. (ass (assoc property org-columns-current-fmt-compiled))
  10153. (format (nth 4 ass))
  10154. (printf (nth 5 ass))
  10155. (beg org-columns-top-level-marker)
  10156. last-level val valflag flag end sumpos sum-alist sum str str1 useval)
  10157. (save-excursion
  10158. ;; Find the region to compute
  10159. (goto-char beg)
  10160. (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
  10161. (goto-char end)
  10162. ;; Walk the tree from the back and do the computations
  10163. (while (re-search-backward re beg t)
  10164. (setq sumpos (match-beginning 0)
  10165. last-level level
  10166. level (org-outline-level)
  10167. val (org-entry-get nil property)
  10168. valflag (and val (string-match "\\S-" val)))
  10169. (cond
  10170. ((< level last-level)
  10171. ;; put the sum of lower levels here as a property
  10172. (setq sum (aref lsum last-level) ; current sum
  10173. flag (aref lflag last-level) ; any valid entries from children?
  10174. str (org-column-number-to-string sum format printf)
  10175. str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
  10176. useval (if flag str1 (if valflag val ""))
  10177. sum-alist (get-text-property sumpos 'org-summaries))
  10178. (if (assoc property sum-alist)
  10179. (setcdr (assoc property sum-alist) useval)
  10180. (push (cons property useval) sum-alist)
  10181. (org-unmodified
  10182. (add-text-properties sumpos (1+ sumpos)
  10183. (list 'org-summaries sum-alist))))
  10184. (when val
  10185. (org-entry-put nil property (if flag str val)))
  10186. ;; add current to current level accumulator
  10187. (when (or flag valflag)
  10188. (aset lsum level (+ (aref lsum level)
  10189. (if flag sum (org-column-string-to-number
  10190. (if flag str val) format))))
  10191. (aset lflag level t))
  10192. ;; clear accumulators for deeper levels
  10193. (loop for l from (1+ level) to (1- lmax) do
  10194. (aset lsum l 0)
  10195. (aset lflag l nil)))
  10196. ((>= level last-level)
  10197. ;; add what we have here to the accumulator for this level
  10198. (aset lsum level (+ (aref lsum level)
  10199. (org-column-string-to-number (or val "0") format)))
  10200. (and valflag (aset lflag level t)))
  10201. (t (error "This should not happen")))))))
  10202. (defun org-columns-redo ()
  10203. "Construct the column display again."
  10204. (interactive)
  10205. (message "Recomputing columns...")
  10206. (save-excursion
  10207. (if (marker-position org-columns-begin-marker)
  10208. (goto-char org-columns-begin-marker))
  10209. (org-columns-remove-overlays)
  10210. (if (org-mode-p)
  10211. (call-interactively 'org-columns)
  10212. (call-interactively 'org-agenda-columns)))
  10213. (message "Recomputing columns...done"))
  10214. (defun org-columns-not-in-agenda ()
  10215. (if (eq major-mode 'org-agenda-mode)
  10216. (error "This command is only allowed in Org-mode buffers")))
  10217. (defun org-string-to-number (s)
  10218. "Convert string to number, and interpret hh:mm:ss."
  10219. (if (not (string-match ":" s))
  10220. (string-to-number s)
  10221. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  10222. (while l
  10223. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  10224. sum)))
  10225. (defun org-column-number-to-string (n fmt &optional printf)
  10226. "Convert a computed column number to a string value, according to FMT."
  10227. (cond
  10228. ((eq fmt 'add_times)
  10229. (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
  10230. (format "%d:%02d" h m)))
  10231. ((eq fmt 'checkbox)
  10232. (cond ((= n (floor n)) "[X]")
  10233. ((> n 1.) "[-]")
  10234. (t "[ ]")))
  10235. ((memq fmt '(checkbox-n-of-m checkbox-percent))
  10236. (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
  10237. (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
  10238. (printf (format printf n))
  10239. ((eq fmt 'currency)
  10240. (format "%.2f" n))
  10241. (t (number-to-string n))))
  10242. (defun org-nofm-to-completion (n m &optional percent)
  10243. (if (not percent)
  10244. (format "[%d/%d]" n m)
  10245. (format "[%d%%]"(floor (+ 0.5 (* 100. (/ (* 1.0 n) m)))))))
  10246. (defun org-column-string-to-number (s fmt)
  10247. "Convert a column value to a number that can be used for column computing."
  10248. (cond
  10249. ((string-match ":" s)
  10250. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  10251. (while l
  10252. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  10253. sum))
  10254. ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
  10255. (if (equal s "[X]") 1. 0.000001))
  10256. (t (string-to-number s))))
  10257. (defun org-columns-uncompile-format (cfmt)
  10258. "Turn the compiled columns format back into a string representation."
  10259. (let ((rtn "") e s prop title op width fmt printf)
  10260. (while (setq e (pop cfmt))
  10261. (setq prop (car e)
  10262. title (nth 1 e)
  10263. width (nth 2 e)
  10264. op (nth 3 e)
  10265. fmt (nth 4 e)
  10266. printf (nth 5 e))
  10267. (cond
  10268. ((eq fmt 'add_times) (setq op ":"))
  10269. ((eq fmt 'checkbox) (setq op "X"))
  10270. ((eq fmt 'checkbox-n-of-m) (setq op "X/"))
  10271. ((eq fmt 'checkbox-percent) (setq op "X%"))
  10272. ((eq fmt 'add_numbers) (setq op "+"))
  10273. ((eq fmt 'currency) (setq op "$")))
  10274. (if (and op printf) (setq op (concat op ";" printf)))
  10275. (if (equal title prop) (setq title nil))
  10276. (setq s (concat "%" (if width (number-to-string width))
  10277. prop
  10278. (if title (concat "(" title ")"))
  10279. (if op (concat "{" op "}"))))
  10280. (setq rtn (concat rtn " " s)))
  10281. (org-trim rtn)))
  10282. (defun org-columns-compile-format (fmt)
  10283. "Turn a column format string into an alist of specifications.
  10284. The alist has one entry for each column in the format. The elements of
  10285. that list are:
  10286. property the property
  10287. title the title field for the columns
  10288. width the column width in characters, can be nil for automatic
  10289. operator the operator if any
  10290. format the output format for computed results, derived from operator
  10291. printf a printf format for computed values"
  10292. (let ((start 0) width prop title op f printf)
  10293. (setq org-columns-current-fmt-compiled nil)
  10294. (while (string-match
  10295. (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
  10296. fmt start)
  10297. (setq start (match-end 0)
  10298. width (match-string 1 fmt)
  10299. prop (match-string 2 fmt)
  10300. title (or (match-string 3 fmt) prop)
  10301. op (match-string 4 fmt)
  10302. f nil
  10303. printf nil)
  10304. (if width (setq width (string-to-number width)))
  10305. (when (and op (string-match ";" op))
  10306. (setq printf (substring op (match-end 0))
  10307. op (substring op 0 (match-beginning 0))))
  10308. (cond
  10309. ((equal op "+") (setq f 'add_numbers))
  10310. ((equal op "$") (setq f 'currency))
  10311. ((equal op ":") (setq f 'add_times))
  10312. ((equal op "X") (setq f 'checkbox))
  10313. ((equal op "X/") (setq f 'checkbox-n-of-m))
  10314. ((equal op "X%") (setq f 'checkbox-percent))
  10315. )
  10316. (push (list prop title width op f printf) org-columns-current-fmt-compiled))
  10317. (setq org-columns-current-fmt-compiled
  10318. (nreverse org-columns-current-fmt-compiled))))
  10319. ;;; Dynamic block for Column view
  10320. (defun org-columns-capture-view (&optional maxlevel skip-empty-rows)
  10321. "Get the column view of the current buffer or subtree.
  10322. The first optional argument MAXLEVEL sets the level limit. A
  10323. second optional argument SKIP-EMPTY-ROWS tells whether to skip
  10324. empty rows, an empty row being one where all the column view
  10325. specifiers except ITEM are empty. This function returns a list
  10326. containing the title row and all other rows. Each row is a list
  10327. of fields."
  10328. (save-excursion
  10329. (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
  10330. (n (length title)) row tbl)
  10331. (goto-char (point-min))
  10332. (while (and (re-search-forward "^\\(\\*+\\) " nil t)
  10333. (or (null maxlevel)
  10334. (>= maxlevel
  10335. (if org-odd-levels-only
  10336. (/ (1+ (length (match-string 1))) 2)
  10337. (length (match-string 1))))))
  10338. (when (get-char-property (match-beginning 0) 'org-columns-key)
  10339. (setq row nil)
  10340. (loop for i from 0 to (1- n) do
  10341. (push (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
  10342. (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
  10343. "")
  10344. row))
  10345. (setq row (nreverse row))
  10346. (unless (and skip-empty-rows
  10347. (eq 1 (length (delete "" (delete-dups row)))))
  10348. (push row tbl))))
  10349. (append (list title 'hline) (nreverse tbl)))))
  10350. (defun org-dblock-write:columnview (params)
  10351. "Write the column view table.
  10352. PARAMS is a property list of parameters:
  10353. :width enforce same column widths with <N> specifiers.
  10354. :id the :ID: property of the entry where the columns view
  10355. should be built, as a string. When `local', call locally.
  10356. When `global' call column view with the cursor at the beginning
  10357. of the buffer (usually this means that the whole buffer switches
  10358. to column view).
  10359. :hlines When t, insert a hline before each item. When a number, insert
  10360. a hline before each level <= that number.
  10361. :vlines When t, make each column a colgroup to enforce vertical lines.
  10362. :maxlevel When set to a number, don't capture headlines below this level.
  10363. :skip-empty-rows
  10364. When t, skip rows where all specifiers other than ITEM are empty."
  10365. (let ((pos (move-marker (make-marker) (point)))
  10366. (hlines (plist-get params :hlines))
  10367. (vlines (plist-get params :vlines))
  10368. (maxlevel (plist-get params :maxlevel))
  10369. (skip-empty-rows (plist-get params :skip-empty-rows))
  10370. tbl id idpos nfields tmp)
  10371. (save-excursion
  10372. (save-restriction
  10373. (when (setq id (plist-get params :id))
  10374. (cond ((not id) nil)
  10375. ((eq id 'global) (goto-char (point-min)))
  10376. ((eq id 'local) nil)
  10377. ((setq idpos (org-find-entry-with-id id))
  10378. (goto-char idpos))
  10379. (t (error "Cannot find entry with :ID: %s" id))))
  10380. (org-columns)
  10381. (setq tbl (org-columns-capture-view maxlevel skip-empty-rows))
  10382. (setq nfields (length (car tbl)))
  10383. (org-columns-quit)))
  10384. (goto-char pos)
  10385. (move-marker pos nil)
  10386. (when tbl
  10387. (when (plist-get params :hlines)
  10388. (setq tmp nil)
  10389. (while tbl
  10390. (if (eq (car tbl) 'hline)
  10391. (push (pop tbl) tmp)
  10392. (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
  10393. (if (and (not (eq (car tmp) 'hline))
  10394. (or (eq hlines t)
  10395. (and (numberp hlines) (<= (- (match-end 1) (match-beginning 1)) hlines))))
  10396. (push 'hline tmp)))
  10397. (push (pop tbl) tmp)))
  10398. (setq tbl (nreverse tmp)))
  10399. (when vlines
  10400. (setq tbl (mapcar (lambda (x)
  10401. (if (eq 'hline x) x (cons "" x)))
  10402. tbl))
  10403. (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
  10404. (setq pos (point))
  10405. (insert (org-listtable-to-string tbl))
  10406. (when (plist-get params :width)
  10407. (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
  10408. org-columns-current-widths "|")))
  10409. (goto-char pos)
  10410. (org-table-align))))
  10411. (defun org-listtable-to-string (tbl)
  10412. "Convert a listtable TBL to a string that contains the Org-mode table.
  10413. The table still need to be alligned. The resulting string has no leading
  10414. and tailing newline characters."
  10415. (mapconcat
  10416. (lambda (x)
  10417. (cond
  10418. ((listp x)
  10419. (concat "|" (mapconcat 'identity x "|") "|"))
  10420. ((eq x 'hline) "|-|")
  10421. (t (error "Garbage in listtable: %s" x))))
  10422. tbl "\n"))
  10423. (defun org-insert-columns-dblock ()
  10424. "Create a dynamic block capturing a column view table."
  10425. (interactive)
  10426. (let ((defaults '(:name "columnview" :hlines 1))
  10427. (id (completing-read
  10428. "Capture columns (local, global, entry with :ID: property) [local]: "
  10429. (append '(("global") ("local"))
  10430. (mapcar 'list (org-property-values "ID"))))))
  10431. (if (equal id "") (setq id 'local))
  10432. (if (equal id "global") (setq id 'global))
  10433. (setq defaults (append defaults (list :id id)))
  10434. (org-create-dblock defaults)
  10435. (org-update-dblock)))
  10436. ;;;; Timestamps
  10437. (defvar org-last-changed-timestamp nil)
  10438. (defvar org-time-was-given) ; dynamically scoped parameter
  10439. (defvar org-end-time-was-given) ; dynamically scoped parameter
  10440. (defvar org-ts-what) ; dynamically scoped parameter
  10441. (defun org-time-stamp (arg)
  10442. "Prompt for a date/time and insert a time stamp.
  10443. If the user specifies a time like HH:MM, or if this command is called
  10444. with a prefix argument, the time stamp will contain date and time.
  10445. Otherwise, only the date will be included. All parts of a date not
  10446. specified by the user will be filled in from the current date/time.
  10447. So if you press just return without typing anything, the time stamp
  10448. will represent the current date/time. If there is already a timestamp
  10449. at the cursor, it will be modified."
  10450. (interactive "P")
  10451. (let* ((ts nil)
  10452. (default-time
  10453. ;; Default time is either today, or, when entering a range,
  10454. ;; the range start.
  10455. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  10456. (save-excursion
  10457. (re-search-backward
  10458. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  10459. (- (point) 20) t)))
  10460. (apply 'encode-time (org-parse-time-string (match-string 1)))
  10461. (current-time)))
  10462. (default-input (and ts (org-get-compact-tod ts)))
  10463. org-time-was-given org-end-time-was-given time)
  10464. (cond
  10465. ((and (org-at-timestamp-p)
  10466. (eq last-command 'org-time-stamp)
  10467. (eq this-command 'org-time-stamp))
  10468. (insert "--")
  10469. (setq time (let ((this-command this-command))
  10470. (org-read-date arg 'totime nil nil default-time default-input)))
  10471. (org-insert-time-stamp time (or org-time-was-given arg)))
  10472. ((org-at-timestamp-p)
  10473. (setq time (let ((this-command this-command))
  10474. (org-read-date arg 'totime nil nil default-time default-input)))
  10475. (when (org-at-timestamp-p) ; just to get the match data
  10476. (replace-match "")
  10477. (setq org-last-changed-timestamp
  10478. (org-insert-time-stamp
  10479. time (or org-time-was-given arg)
  10480. nil nil nil (list org-end-time-was-given))))
  10481. (message "Timestamp updated"))
  10482. (t
  10483. (setq time (let ((this-command this-command))
  10484. (org-read-date arg 'totime nil nil default-time default-input)))
  10485. (org-insert-time-stamp time (or org-time-was-given arg)
  10486. nil nil nil (list org-end-time-was-given))))))
  10487. ;; FIXME: can we use this for something else????
  10488. ;; like computing time differences?????
  10489. (defun org-get-compact-tod (s)
  10490. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  10491. (let* ((t1 (match-string 1 s))
  10492. (h1 (string-to-number (match-string 2 s)))
  10493. (m1 (string-to-number (match-string 3 s)))
  10494. (t2 (and (match-end 4) (match-string 5 s)))
  10495. (h2 (and t2 (string-to-number (match-string 6 s))))
  10496. (m2 (and t2 (string-to-number (match-string 7 s))))
  10497. dh dm)
  10498. (if (not t2)
  10499. t1
  10500. (setq dh (- h2 h1) dm (- m2 m1))
  10501. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  10502. (concat t1 "+" (number-to-string dh)
  10503. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  10504. (defun org-time-stamp-inactive (&optional arg)
  10505. "Insert an inactive time stamp.
  10506. An inactive time stamp is enclosed in square brackets instead of angle
  10507. brackets. It is inactive in the sense that it does not trigger agenda entries,
  10508. does not link to the calendar and cannot be changed with the S-cursor keys.
  10509. So these are more for recording a certain time/date."
  10510. (interactive "P")
  10511. (let (org-time-was-given org-end-time-was-given time)
  10512. (setq time (org-read-date arg 'totime))
  10513. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  10514. nil nil (list org-end-time-was-given))))
  10515. (defvar org-date-ovl (org-make-overlay 1 1))
  10516. (org-overlay-put org-date-ovl 'face 'org-warning)
  10517. (org-detach-overlay org-date-ovl)
  10518. (defvar org-ans1) ; dynamically scoped parameter
  10519. (defvar org-ans2) ; dynamically scoped parameter
  10520. (defvar org-plain-time-of-day-regexp) ; defined below
  10521. (defvar org-read-date-overlay nil)
  10522. (defvar org-dcst nil) ; dynamically scoped
  10523. (defun org-read-date (&optional with-time to-time from-string prompt
  10524. default-time default-input)
  10525. "Read a date, possibly a time, and make things smooth for the user.
  10526. The prompt will suggest to enter an ISO date, but you can also enter anything
  10527. which will at least partially be understood by `parse-time-string'.
  10528. Unrecognized parts of the date will default to the current day, month, year,
  10529. hour and minute. If this command is called to replace a timestamp at point,
  10530. of to enter the second timestamp of a range, the default time is taken from the
  10531. existing stamp. For example,
  10532. 3-2-5 --> 2003-02-05
  10533. feb 15 --> currentyear-02-15
  10534. sep 12 9 --> 2009-09-12
  10535. 12:45 --> today 12:45
  10536. 22 sept 0:34 --> currentyear-09-22 0:34
  10537. 12 --> currentyear-currentmonth-12
  10538. Fri --> nearest Friday (today or later)
  10539. etc.
  10540. Furthermore you can specify a relative date by giving, as the *first* thing
  10541. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  10542. change in days weeks, months, years.
  10543. With a single plus or minus, the date is relative to today. With a double
  10544. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  10545. +4d --> four days from today
  10546. +4 --> same as above
  10547. +2w --> two weeks from today
  10548. ++5 --> five days from default date
  10549. The function understands only English month and weekday abbreviations,
  10550. but this can be configured with the variables `parse-time-months' and
  10551. `parse-time-weekdays'.
  10552. While prompting, a calendar is popped up - you can also select the
  10553. date with the mouse (button 1). The calendar shows a period of three
  10554. months. To scroll it to other months, use the keys `>' and `<'.
  10555. If you don't like the calendar, turn it off with
  10556. \(setq org-read-date-popup-calendar nil)
  10557. With optional argument TO-TIME, the date will immediately be converted
  10558. to an internal time.
  10559. With an optional argument WITH-TIME, the prompt will suggest to also
  10560. insert a time. Note that when WITH-TIME is not set, you can still
  10561. enter a time, and this function will inform the calling routine about
  10562. this change. The calling routine may then choose to change the format
  10563. used to insert the time stamp into the buffer to include the time.
  10564. With optional argument FROM-STRING, read from this string instead from
  10565. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  10566. the time/date that is used for everything that is not specified by the
  10567. user."
  10568. (require 'parse-time)
  10569. (let* ((org-time-stamp-rounding-minutes
  10570. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  10571. (org-dcst org-display-custom-times)
  10572. (ct (org-current-time))
  10573. (def (or default-time ct))
  10574. (defdecode (decode-time def))
  10575. (dummy (progn
  10576. (when (< (nth 2 defdecode) org-extend-today-until)
  10577. (setcar (nthcdr 2 defdecode) -1)
  10578. (setcar (nthcdr 1 defdecode) 59)
  10579. (setq def (apply 'encode-time defdecode)
  10580. defdecode (decode-time def)))))
  10581. (calendar-move-hook nil)
  10582. (view-diary-entries-initially nil)
  10583. (view-calendar-holidays-initially nil)
  10584. (timestr (format-time-string
  10585. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  10586. (prompt (concat (if prompt (concat prompt " ") "")
  10587. (format "Date+time [%s]: " timestr)))
  10588. ans (org-ans0 "") org-ans1 org-ans2 final)
  10589. (cond
  10590. (from-string (setq ans from-string))
  10591. (org-read-date-popup-calendar
  10592. (save-excursion
  10593. (save-window-excursion
  10594. (calendar)
  10595. (calendar-forward-day (- (time-to-days def)
  10596. (calendar-absolute-from-gregorian
  10597. (calendar-current-date))))
  10598. (org-eval-in-calendar nil t)
  10599. (let* ((old-map (current-local-map))
  10600. (map (copy-keymap calendar-mode-map))
  10601. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  10602. (org-defkey map (kbd "RET") 'org-calendar-select)
  10603. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  10604. 'org-calendar-select-mouse)
  10605. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  10606. 'org-calendar-select-mouse)
  10607. (org-defkey minibuffer-local-map [(meta shift left)]
  10608. (lambda () (interactive)
  10609. (org-eval-in-calendar '(calendar-backward-month 1))))
  10610. (org-defkey minibuffer-local-map [(meta shift right)]
  10611. (lambda () (interactive)
  10612. (org-eval-in-calendar '(calendar-forward-month 1))))
  10613. (org-defkey minibuffer-local-map [(meta shift up)]
  10614. (lambda () (interactive)
  10615. (org-eval-in-calendar '(calendar-backward-year 1))))
  10616. (org-defkey minibuffer-local-map [(meta shift down)]
  10617. (lambda () (interactive)
  10618. (org-eval-in-calendar '(calendar-forward-year 1))))
  10619. (org-defkey minibuffer-local-map [(shift up)]
  10620. (lambda () (interactive)
  10621. (org-eval-in-calendar '(calendar-backward-week 1))))
  10622. (org-defkey minibuffer-local-map [(shift down)]
  10623. (lambda () (interactive)
  10624. (org-eval-in-calendar '(calendar-forward-week 1))))
  10625. (org-defkey minibuffer-local-map [(shift left)]
  10626. (lambda () (interactive)
  10627. (org-eval-in-calendar '(calendar-backward-day 1))))
  10628. (org-defkey minibuffer-local-map [(shift right)]
  10629. (lambda () (interactive)
  10630. (org-eval-in-calendar '(calendar-forward-day 1))))
  10631. (org-defkey minibuffer-local-map ">"
  10632. (lambda () (interactive)
  10633. (org-eval-in-calendar '(scroll-calendar-left 1))))
  10634. (org-defkey minibuffer-local-map "<"
  10635. (lambda () (interactive)
  10636. (org-eval-in-calendar '(scroll-calendar-right 1))))
  10637. (unwind-protect
  10638. (progn
  10639. (use-local-map map)
  10640. (add-hook 'post-command-hook 'org-read-date-display)
  10641. (setq org-ans0 (read-string prompt default-input nil nil))
  10642. ;; org-ans0: from prompt
  10643. ;; org-ans1: from mouse click
  10644. ;; org-ans2: from calendar motion
  10645. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  10646. (remove-hook 'post-command-hook 'org-read-date-display)
  10647. (use-local-map old-map)
  10648. (when org-read-date-overlay
  10649. (org-delete-overlay org-read-date-overlay)
  10650. (setq org-read-date-overlay nil)))))))
  10651. (t ; Naked prompt only
  10652. (unwind-protect
  10653. (setq ans (read-string prompt default-input nil timestr))
  10654. (when org-read-date-overlay
  10655. (org-delete-overlay org-read-date-overlay)
  10656. (setq org-read-date-overlay nil)))))
  10657. (setq final (org-read-date-analyze ans def defdecode))
  10658. (if to-time
  10659. (apply 'encode-time final)
  10660. (if (and (boundp 'org-time-was-given) org-time-was-given)
  10661. (format "%04d-%02d-%02d %02d:%02d"
  10662. (nth 5 final) (nth 4 final) (nth 3 final)
  10663. (nth 2 final) (nth 1 final))
  10664. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  10665. (defvar def)
  10666. (defvar defdecode)
  10667. (defvar with-time)
  10668. (defun org-read-date-display ()
  10669. "Display the currrent date prompt interpretation in the minibuffer."
  10670. (when org-read-date-display-live
  10671. (when org-read-date-overlay
  10672. (org-delete-overlay org-read-date-overlay))
  10673. (let ((p (point)))
  10674. (end-of-line 1)
  10675. (while (not (equal (buffer-substring
  10676. (max (point-min) (- (point) 4)) (point))
  10677. " "))
  10678. (insert " "))
  10679. (goto-char p))
  10680. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  10681. " " (or org-ans1 org-ans2)))
  10682. (org-end-time-was-given nil)
  10683. (f (org-read-date-analyze ans def defdecode))
  10684. (fmts (if org-dcst
  10685. org-time-stamp-custom-formats
  10686. org-time-stamp-formats))
  10687. (fmt (if (or with-time
  10688. (and (boundp 'org-time-was-given) org-time-was-given))
  10689. (cdr fmts)
  10690. (car fmts)))
  10691. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  10692. (when (and org-end-time-was-given
  10693. (string-match org-plain-time-of-day-regexp txt))
  10694. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  10695. org-end-time-was-given
  10696. (substring txt (match-end 0)))))
  10697. (setq org-read-date-overlay
  10698. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  10699. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  10700. (defun org-read-date-analyze (ans def defdecode)
  10701. "Analyze the combined answer of the date prompt."
  10702. ;; FIXME: cleanup and comment
  10703. (let (delta deltan deltaw deltadef year month day
  10704. hour minute second wday pm h2 m2 tl wday1
  10705. iso-year iso-weekday iso-week iso-year iso-date)
  10706. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  10707. (setq ans (replace-match "" t t ans)
  10708. deltan (car delta)
  10709. deltaw (nth 1 delta)
  10710. deltadef (nth 2 delta)))
  10711. ;; Check if there is an iso week date in there
  10712. ;; If yes, sore the info and ostpone interpreting it until the rest
  10713. ;; of the parsing is done
  10714. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  10715. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  10716. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  10717. iso-week (string-to-number (match-string 2 ans)))
  10718. (setq ans (replace-match "" t t ans)))
  10719. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  10720. (when (string-match
  10721. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  10722. (setq year (if (match-end 2)
  10723. (string-to-number (match-string 2 ans))
  10724. (string-to-number (format-time-string "%Y")))
  10725. month (string-to-number (match-string 3 ans))
  10726. day (string-to-number (match-string 4 ans)))
  10727. (if (< year 100) (setq year (+ 2000 year)))
  10728. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  10729. t nil ans)))
  10730. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  10731. ;; If there is a time with am/pm, and *no* time without it, we convert
  10732. ;; so that matching will be successful.
  10733. (loop for i from 1 to 2 do ; twice, for end time as well
  10734. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  10735. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  10736. (setq hour (string-to-number (match-string 1 ans))
  10737. minute (if (match-end 3)
  10738. (string-to-number (match-string 3 ans))
  10739. 0)
  10740. pm (equal ?p
  10741. (string-to-char (downcase (match-string 4 ans)))))
  10742. (if (and (= hour 12) (not pm))
  10743. (setq hour 0)
  10744. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  10745. (setq ans (replace-match (format "%02d:%02d" hour minute)
  10746. t t ans))))
  10747. ;; Check if a time range is given as a duration
  10748. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  10749. (setq hour (string-to-number (match-string 1 ans))
  10750. h2 (+ hour (string-to-number (match-string 3 ans)))
  10751. minute (string-to-number (match-string 2 ans))
  10752. m2 (+ minute (if (match-end 5) (string-to-number
  10753. (match-string 5 ans))0)))
  10754. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  10755. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  10756. t t ans)))
  10757. ;; Check if there is a time range
  10758. (when (boundp 'org-end-time-was-given)
  10759. (setq org-time-was-given nil)
  10760. (when (and (string-match org-plain-time-of-day-regexp ans)
  10761. (match-end 8))
  10762. (setq org-end-time-was-given (match-string 8 ans))
  10763. (setq ans (concat (substring ans 0 (match-beginning 7))
  10764. (substring ans (match-end 7))))))
  10765. (setq tl (parse-time-string ans)
  10766. day (or (nth 3 tl) (nth 3 defdecode))
  10767. month (or (nth 4 tl)
  10768. (if (and org-read-date-prefer-future
  10769. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  10770. (1+ (nth 4 defdecode))
  10771. (nth 4 defdecode)))
  10772. year (or (nth 5 tl)
  10773. (if (and org-read-date-prefer-future
  10774. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  10775. (1+ (nth 5 defdecode))
  10776. (nth 5 defdecode)))
  10777. hour (or (nth 2 tl) (nth 2 defdecode))
  10778. minute (or (nth 1 tl) (nth 1 defdecode))
  10779. second (or (nth 0 tl) 0)
  10780. wday (nth 6 tl))
  10781. ;; Special date definitions below
  10782. (cond
  10783. (iso-week
  10784. ;; There was an iso week
  10785. (setq year (or iso-year year)
  10786. day (or iso-weekday wday 1)
  10787. wday nil ; to make sure that the trigger below does not match
  10788. iso-date (calendar-gregorian-from-absolute
  10789. (calendar-absolute-from-iso
  10790. (list iso-week day year))))
  10791. ; FIXME: Should we also push ISO weeks into the future?
  10792. ; (when (and org-read-date-prefer-future
  10793. ; (not iso-year)
  10794. ; (< (calendar-absolute-from-gregorian iso-date)
  10795. ; (time-to-days (current-time))))
  10796. ; (setq year (1+ year)
  10797. ; iso-date (calendar-gregorian-from-absolute
  10798. ; (calendar-absolute-from-iso
  10799. ; (list iso-week day year)))))
  10800. (setq month (car iso-date)
  10801. year (nth 2 iso-date)
  10802. day (nth 1 iso-date)))
  10803. (deltan
  10804. (unless deltadef
  10805. (let ((now (decode-time (current-time))))
  10806. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  10807. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  10808. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  10809. ((equal deltaw "m") (setq month (+ month deltan)))
  10810. ((equal deltaw "y") (setq year (+ year deltan)))))
  10811. ((and wday (not (nth 3 tl)))
  10812. ;; Weekday was given, but no day, so pick that day in the week
  10813. ;; on or after the derived date.
  10814. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  10815. (unless (equal wday wday1)
  10816. (setq day (+ day (% (- wday wday1 -7) 7))))))
  10817. (if (and (boundp 'org-time-was-given)
  10818. (nth 2 tl))
  10819. (setq org-time-was-given t))
  10820. (if (< year 100) (setq year (+ 2000 year)))
  10821. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  10822. (list second minute hour day month year)))
  10823. (defvar parse-time-weekdays)
  10824. (defun org-read-date-get-relative (s today default)
  10825. "Check string S for special relative date string.
  10826. TODAY and DEFAULT are internal times, for today and for a default.
  10827. Return shift list (N what def-flag)
  10828. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  10829. N is the number of WHATs to shift.
  10830. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  10831. the DEFAULT date rather than TODAY."
  10832. (when (string-match
  10833. (concat
  10834. "\\`[ \t]*\\([-+]\\{1,2\\}\\)"
  10835. "\\([0-9]+\\)?"
  10836. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  10837. "\\([ \t]\\|$\\)") s)
  10838. (let* ((dir (if (match-end 1)
  10839. (string-to-char (substring (match-string 1 s) -1))
  10840. ?+))
  10841. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  10842. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  10843. (what (if (match-end 3) (match-string 3 s) "d"))
  10844. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  10845. (date (if rel default today))
  10846. (wday (nth 6 (decode-time date)))
  10847. delta)
  10848. (if wday1
  10849. (progn
  10850. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  10851. (if (= dir ?-) (setq delta (- delta 7)))
  10852. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  10853. (list delta "d" rel))
  10854. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  10855. (defun org-eval-in-calendar (form &optional keepdate)
  10856. "Eval FORM in the calendar window and return to current window.
  10857. Also, store the cursor date in variable org-ans2."
  10858. (let ((sw (selected-window)))
  10859. (select-window (get-buffer-window "*Calendar*"))
  10860. (eval form)
  10861. (when (and (not keepdate) (calendar-cursor-to-date))
  10862. (let* ((date (calendar-cursor-to-date))
  10863. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10864. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  10865. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  10866. (select-window sw)))
  10867. ; ;; Update the prompt to show new default date
  10868. ; (save-excursion
  10869. ; (goto-char (point-min))
  10870. ; (when (and org-ans2
  10871. ; (re-search-forward "\\[[-0-9]+\\]" nil t)
  10872. ; (get-text-property (match-end 0) 'field))
  10873. ; (let ((inhibit-read-only t))
  10874. ; (replace-match (concat "[" org-ans2 "]") t t)
  10875. ; (add-text-properties (point-min) (1+ (match-end 0))
  10876. ; (text-properties-at (1+ (point-min)))))))))
  10877. (defun org-calendar-select ()
  10878. "Return to `org-read-date' with the date currently selected.
  10879. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10880. (interactive)
  10881. (when (calendar-cursor-to-date)
  10882. (let* ((date (calendar-cursor-to-date))
  10883. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10884. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10885. (if (active-minibuffer-window) (exit-minibuffer))))
  10886. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  10887. "Insert a date stamp for the date given by the internal TIME.
  10888. WITH-HM means, use the stamp format that includes the time of the day.
  10889. INACTIVE means use square brackets instead of angular ones, so that the
  10890. stamp will not contribute to the agenda.
  10891. PRE and POST are optional strings to be inserted before and after the
  10892. stamp.
  10893. The command returns the inserted time stamp."
  10894. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  10895. stamp)
  10896. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  10897. (insert-before-markers (or pre ""))
  10898. (insert-before-markers (setq stamp (format-time-string fmt time)))
  10899. (when (listp extra)
  10900. (setq extra (car extra))
  10901. (if (and (stringp extra)
  10902. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  10903. (setq extra (format "-%02d:%02d"
  10904. (string-to-number (match-string 1 extra))
  10905. (string-to-number (match-string 2 extra))))
  10906. (setq extra nil)))
  10907. (when extra
  10908. (backward-char 1)
  10909. (insert-before-markers extra)
  10910. (forward-char 1))
  10911. (insert-before-markers (or post ""))
  10912. stamp))
  10913. (defun org-toggle-time-stamp-overlays ()
  10914. "Toggle the use of custom time stamp formats."
  10915. (interactive)
  10916. (setq org-display-custom-times (not org-display-custom-times))
  10917. (unless org-display-custom-times
  10918. (let ((p (point-min)) (bmp (buffer-modified-p)))
  10919. (while (setq p (next-single-property-change p 'display))
  10920. (if (and (get-text-property p 'display)
  10921. (eq (get-text-property p 'face) 'org-date))
  10922. (remove-text-properties
  10923. p (setq p (next-single-property-change p 'display))
  10924. '(display t))))
  10925. (set-buffer-modified-p bmp)))
  10926. (if (featurep 'xemacs)
  10927. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  10928. (org-restart-font-lock)
  10929. (setq org-table-may-need-update t)
  10930. (if org-display-custom-times
  10931. (message "Time stamps are overlayed with custom format")
  10932. (message "Time stamp overlays removed")))
  10933. (defun org-display-custom-time (beg end)
  10934. "Overlay modified time stamp format over timestamp between BED and END."
  10935. (let* ((ts (buffer-substring beg end))
  10936. t1 w1 with-hm tf time str w2 (off 0))
  10937. (save-match-data
  10938. (setq t1 (org-parse-time-string ts t))
  10939. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  10940. (setq off (- (match-end 0) (match-beginning 0)))))
  10941. (setq end (- end off))
  10942. (setq w1 (- end beg)
  10943. with-hm (and (nth 1 t1) (nth 2 t1))
  10944. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  10945. time (org-fix-decoded-time t1)
  10946. str (org-add-props
  10947. (format-time-string
  10948. (substring tf 1 -1) (apply 'encode-time time))
  10949. nil 'mouse-face 'highlight)
  10950. w2 (length str))
  10951. (if (not (= w2 w1))
  10952. (add-text-properties (1+ beg) (+ 2 beg)
  10953. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  10954. (if (featurep 'xemacs)
  10955. (progn
  10956. (put-text-property beg end 'invisible t)
  10957. (put-text-property beg end 'end-glyph (make-glyph str)))
  10958. (put-text-property beg end 'display str))))
  10959. (defun org-translate-time (string)
  10960. "Translate all timestamps in STRING to custom format.
  10961. But do this only if the variable `org-display-custom-times' is set."
  10962. (when org-display-custom-times
  10963. (save-match-data
  10964. (let* ((start 0)
  10965. (re org-ts-regexp-both)
  10966. t1 with-hm inactive tf time str beg end)
  10967. (while (setq start (string-match re string start))
  10968. (setq beg (match-beginning 0)
  10969. end (match-end 0)
  10970. t1 (save-match-data
  10971. (org-parse-time-string (substring string beg end) t))
  10972. with-hm (and (nth 1 t1) (nth 2 t1))
  10973. inactive (equal (substring string beg (1+ beg)) "[")
  10974. tf (funcall (if with-hm 'cdr 'car)
  10975. org-time-stamp-custom-formats)
  10976. time (org-fix-decoded-time t1)
  10977. str (format-time-string
  10978. (concat
  10979. (if inactive "[" "<") (substring tf 1 -1)
  10980. (if inactive "]" ">"))
  10981. (apply 'encode-time time))
  10982. string (replace-match str t t string)
  10983. start (+ start (length str)))))))
  10984. string)
  10985. (defun org-fix-decoded-time (time)
  10986. "Set 0 instead of nil for the first 6 elements of time.
  10987. Don't touch the rest."
  10988. (let ((n 0))
  10989. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  10990. (defun org-days-to-time (timestamp-string)
  10991. "Difference between TIMESTAMP-STRING and now in days."
  10992. (- (time-to-days (org-time-string-to-time timestamp-string))
  10993. (time-to-days (current-time))))
  10994. (defun org-deadline-close (timestamp-string &optional ndays)
  10995. "Is the time in TIMESTAMP-STRING close to the current date?"
  10996. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  10997. (and (< (org-days-to-time timestamp-string) ndays)
  10998. (not (org-entry-is-done-p))))
  10999. (defun org-get-wdays (ts)
  11000. "Get the deadline lead time appropriate for timestring TS."
  11001. (cond
  11002. ((<= org-deadline-warning-days 0)
  11003. ;; 0 or negative, enforce this value no matter what
  11004. (- org-deadline-warning-days))
  11005. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  11006. ;; lead time is specified.
  11007. (floor (* (string-to-number (match-string 1 ts))
  11008. (cdr (assoc (match-string 2 ts)
  11009. '(("d" . 1) ("w" . 7)
  11010. ("m" . 30.4) ("y" . 365.25)))))))
  11011. ;; go for the default.
  11012. (t org-deadline-warning-days)))
  11013. (defun org-calendar-select-mouse (ev)
  11014. "Return to `org-read-date' with the date currently selected.
  11015. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11016. (interactive "e")
  11017. (mouse-set-point ev)
  11018. (when (calendar-cursor-to-date)
  11019. (let* ((date (calendar-cursor-to-date))
  11020. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11021. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11022. (if (active-minibuffer-window) (exit-minibuffer))))
  11023. (defun org-check-deadlines (ndays)
  11024. "Check if there are any deadlines due or past due.
  11025. A deadline is considered due if it happens within `org-deadline-warning-days'
  11026. days from today's date. If the deadline appears in an entry marked DONE,
  11027. it is not shown. The prefix arg NDAYS can be used to test that many
  11028. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  11029. (interactive "P")
  11030. (let* ((org-warn-days
  11031. (cond
  11032. ((equal ndays '(4)) 100000)
  11033. (ndays (prefix-numeric-value ndays))
  11034. (t (abs org-deadline-warning-days))))
  11035. (case-fold-search nil)
  11036. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  11037. (callback
  11038. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  11039. (message "%d deadlines past-due or due within %d days"
  11040. (org-occur regexp nil callback)
  11041. org-warn-days)))
  11042. (defun org-check-before-date (date)
  11043. "Check if there are deadlines or scheduled entries before DATE."
  11044. (interactive (list (org-read-date)))
  11045. (let ((case-fold-search nil)
  11046. (regexp (concat "\\<\\(" org-deadline-string
  11047. "\\|" org-scheduled-string
  11048. "\\) *<\\([^>]+\\)>"))
  11049. (callback
  11050. (lambda () (time-less-p
  11051. (org-time-string-to-time (match-string 2))
  11052. (org-time-string-to-time date)))))
  11053. (message "%d entries before %s"
  11054. (org-occur regexp nil callback) date)))
  11055. (defun org-evaluate-time-range (&optional to-buffer)
  11056. "Evaluate a time range by computing the difference between start and end.
  11057. Normally the result is just printed in the echo area, but with prefix arg
  11058. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  11059. If the time range is actually in a table, the result is inserted into the
  11060. next column.
  11061. For time difference computation, a year is assumed to be exactly 365
  11062. days in order to avoid rounding problems."
  11063. (interactive "P")
  11064. (or
  11065. (org-clock-update-time-maybe)
  11066. (save-excursion
  11067. (unless (org-at-date-range-p t)
  11068. (goto-char (point-at-bol))
  11069. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  11070. (if (not (org-at-date-range-p t))
  11071. (error "Not at a time-stamp range, and none found in current line")))
  11072. (let* ((ts1 (match-string 1))
  11073. (ts2 (match-string 2))
  11074. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  11075. (match-end (match-end 0))
  11076. (time1 (org-time-string-to-time ts1))
  11077. (time2 (org-time-string-to-time ts2))
  11078. (t1 (time-to-seconds time1))
  11079. (t2 (time-to-seconds time2))
  11080. (diff (abs (- t2 t1)))
  11081. (negative (< (- t2 t1) 0))
  11082. ;; (ys (floor (* 365 24 60 60)))
  11083. (ds (* 24 60 60))
  11084. (hs (* 60 60))
  11085. (fy "%dy %dd %02d:%02d")
  11086. (fy1 "%dy %dd")
  11087. (fd "%dd %02d:%02d")
  11088. (fd1 "%dd")
  11089. (fh "%02d:%02d")
  11090. y d h m align)
  11091. (if havetime
  11092. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11093. y 0
  11094. d (floor (/ diff ds)) diff (mod diff ds)
  11095. h (floor (/ diff hs)) diff (mod diff hs)
  11096. m (floor (/ diff 60)))
  11097. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11098. y 0
  11099. d (floor (+ (/ diff ds) 0.5))
  11100. h 0 m 0))
  11101. (if (not to-buffer)
  11102. (message "%s" (org-make-tdiff-string y d h m))
  11103. (if (org-at-table-p)
  11104. (progn
  11105. (goto-char match-end)
  11106. (setq align t)
  11107. (and (looking-at " *|") (goto-char (match-end 0))))
  11108. (goto-char match-end))
  11109. (if (looking-at
  11110. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  11111. (replace-match ""))
  11112. (if negative (insert " -"))
  11113. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  11114. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  11115. (insert " " (format fh h m))))
  11116. (if align (org-table-align))
  11117. (message "Time difference inserted")))))
  11118. (defun org-make-tdiff-string (y d h m)
  11119. (let ((fmt "")
  11120. (l nil))
  11121. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  11122. l (push y l)))
  11123. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  11124. l (push d l)))
  11125. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  11126. l (push h l)))
  11127. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  11128. l (push m l)))
  11129. (apply 'format fmt (nreverse l))))
  11130. (defun org-time-string-to-time (s)
  11131. (apply 'encode-time (org-parse-time-string s)))
  11132. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  11133. "Convert a time stamp to an absolute day number.
  11134. If there is a specifyer for a cyclic time stamp, get the closest date to
  11135. DAYNR.
  11136. PREFER and SHOW_ALL are passed through to `org-closest-date'."
  11137. (cond
  11138. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  11139. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  11140. daynr
  11141. (+ daynr 1000)))
  11142. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  11143. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  11144. (time-to-days (current-time))) (match-string 0 s)
  11145. prefer show-all))
  11146. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  11147. (defun org-time-from-absolute (d)
  11148. "Return the time corresponding to date D.
  11149. D may be an absolute day number, or a calendar-type list (month day year)."
  11150. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  11151. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  11152. (defun org-calendar-holiday ()
  11153. "List of holidays, for Diary display in Org-mode."
  11154. (require 'holidays)
  11155. (let ((hl (funcall
  11156. (if (fboundp 'calendar-check-holidays)
  11157. 'calendar-check-holidays 'check-calendar-holidays) date)))
  11158. (if hl (mapconcat 'identity hl "; "))))
  11159. (defun org-diary-sexp-entry (sexp entry date)
  11160. "Process a SEXP diary ENTRY for DATE."
  11161. (require 'diary-lib)
  11162. (let ((result (if calendar-debug-sexp
  11163. (let ((stack-trace-on-error t))
  11164. (eval (car (read-from-string sexp))))
  11165. (condition-case nil
  11166. (eval (car (read-from-string sexp)))
  11167. (error
  11168. (beep)
  11169. (message "Bad sexp at line %d in %s: %s"
  11170. (org-current-line)
  11171. (buffer-file-name) sexp)
  11172. (sleep-for 2))))))
  11173. (cond ((stringp result) result)
  11174. ((and (consp result)
  11175. (stringp (cdr result))) (cdr result))
  11176. (result entry)
  11177. (t nil))))
  11178. (defun org-diary-to-ical-string (frombuf)
  11179. "Get iCalendar entries from diary entries in buffer FROMBUF.
  11180. This uses the icalendar.el library."
  11181. (let* ((tmpdir (if (featurep 'xemacs)
  11182. (temp-directory)
  11183. temporary-file-directory))
  11184. (tmpfile (make-temp-name
  11185. (expand-file-name "orgics" tmpdir)))
  11186. buf rtn b e)
  11187. (save-excursion
  11188. (set-buffer frombuf)
  11189. (icalendar-export-region (point-min) (point-max) tmpfile)
  11190. (setq buf (find-buffer-visiting tmpfile))
  11191. (set-buffer buf)
  11192. (goto-char (point-min))
  11193. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  11194. (setq b (match-beginning 0)))
  11195. (goto-char (point-max))
  11196. (if (re-search-backward "^END:VEVENT" nil t)
  11197. (setq e (match-end 0)))
  11198. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  11199. (kill-buffer buf)
  11200. (kill-buffer frombuf)
  11201. (delete-file tmpfile)
  11202. rtn))
  11203. (defun org-closest-date (start current change prefer show-all)
  11204. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  11205. When PREFER is `past' return a date that is either CURRENT or past.
  11206. When PREFER is `future', return a date that is either CURRENT or future.
  11207. When SHOW-ALL is nil, only return the current occurence of a time stamp."
  11208. ;; Make the proper lists from the dates
  11209. (catch 'exit
  11210. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  11211. dn dw sday cday n1 n2
  11212. d m y y1 y2 date1 date2 nmonths nm ny m2)
  11213. (setq start (org-date-to-gregorian start)
  11214. current (org-date-to-gregorian
  11215. (if show-all
  11216. current
  11217. (time-to-days (current-time))))
  11218. sday (calendar-absolute-from-gregorian start)
  11219. cday (calendar-absolute-from-gregorian current))
  11220. (if (<= cday sday) (throw 'exit sday))
  11221. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  11222. (setq dn (string-to-number (match-string 1 change))
  11223. dw (cdr (assoc (match-string 2 change) a1)))
  11224. (error "Invalid change specifyer: %s" change))
  11225. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  11226. (cond
  11227. ((eq dw 'day)
  11228. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  11229. n2 (+ n1 dn)))
  11230. ((eq dw 'year)
  11231. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  11232. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  11233. (setq date1 (list m d y1)
  11234. n1 (calendar-absolute-from-gregorian date1)
  11235. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  11236. n2 (calendar-absolute-from-gregorian date2)))
  11237. ((eq dw 'month)
  11238. ;; approx number of month between the tow dates
  11239. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  11240. ;; How often does dn fit in there?
  11241. (setq d (nth 1 start) m (car start) y (nth 2 start)
  11242. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  11243. m (+ m nm)
  11244. ny (floor (/ m 12))
  11245. y (+ y ny)
  11246. m (- m (* ny 12)))
  11247. (while (> m 12) (setq m (- m 12) y (1+ y)))
  11248. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  11249. (setq m2 (+ m dn) y2 y)
  11250. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11251. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  11252. (while (< n2 cday)
  11253. (setq n1 n2 m m2 y y2)
  11254. (setq m2 (+ m dn) y2 y)
  11255. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11256. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  11257. (if show-all
  11258. (cond
  11259. ((eq prefer 'past) n1)
  11260. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11261. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  11262. (cond
  11263. ((eq prefer 'past) n1)
  11264. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11265. (t (if (= cday n1) n1 n2)))))))
  11266. (defun org-date-to-gregorian (date)
  11267. "Turn any specification of DATE into a gregorian date for the calendar."
  11268. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  11269. ((and (listp date) (= (length date) 3)) date)
  11270. ((stringp date)
  11271. (setq date (org-parse-time-string date))
  11272. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  11273. ((listp date)
  11274. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  11275. (defun org-parse-time-string (s &optional nodefault)
  11276. "Parse the standard Org-mode time string.
  11277. This should be a lot faster than the normal `parse-time-string'.
  11278. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  11279. hour and minute fields will be nil if not given."
  11280. (if (string-match org-ts-regexp0 s)
  11281. (list 0
  11282. (if (or (match-beginning 8) (not nodefault))
  11283. (string-to-number (or (match-string 8 s) "0")))
  11284. (if (or (match-beginning 7) (not nodefault))
  11285. (string-to-number (or (match-string 7 s) "0")))
  11286. (string-to-number (match-string 4 s))
  11287. (string-to-number (match-string 3 s))
  11288. (string-to-number (match-string 2 s))
  11289. nil nil nil)
  11290. (make-list 9 0)))
  11291. (defun org-timestamp-up (&optional arg)
  11292. "Increase the date item at the cursor by one.
  11293. If the cursor is on the year, change the year. If it is on the month or
  11294. the day, change that.
  11295. With prefix ARG, change by that many units."
  11296. (interactive "p")
  11297. (org-timestamp-change (prefix-numeric-value arg)))
  11298. (defun org-timestamp-down (&optional arg)
  11299. "Decrease the date item at the cursor by one.
  11300. If the cursor is on the year, change the year. If it is on the month or
  11301. the day, change that.
  11302. With prefix ARG, change by that many units."
  11303. (interactive "p")
  11304. (org-timestamp-change (- (prefix-numeric-value arg))))
  11305. (defun org-timestamp-up-day (&optional arg)
  11306. "Increase the date in the time stamp by one day.
  11307. With prefix ARG, change that many days."
  11308. (interactive "p")
  11309. (if (and (not (org-at-timestamp-p t))
  11310. (org-on-heading-p))
  11311. (org-todo 'up)
  11312. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  11313. (defun org-timestamp-down-day (&optional arg)
  11314. "Decrease the date in the time stamp by one day.
  11315. With prefix ARG, change that many days."
  11316. (interactive "p")
  11317. (if (and (not (org-at-timestamp-p t))
  11318. (org-on-heading-p))
  11319. (org-todo 'down)
  11320. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  11321. (defun org-at-timestamp-p (&optional inactive-ok)
  11322. "Determine if the cursor is in or at a timestamp."
  11323. (interactive)
  11324. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  11325. (pos (point))
  11326. (ans (or (looking-at tsr)
  11327. (save-excursion
  11328. (skip-chars-backward "^[<\n\r\t")
  11329. (if (> (point) (point-min)) (backward-char 1))
  11330. (and (looking-at tsr)
  11331. (> (- (match-end 0) pos) -1))))))
  11332. (and ans
  11333. (boundp 'org-ts-what)
  11334. (setq org-ts-what
  11335. (cond
  11336. ((= pos (match-beginning 0)) 'bracket)
  11337. ((= pos (1- (match-end 0))) 'bracket)
  11338. ((org-pos-in-match-range pos 2) 'year)
  11339. ((org-pos-in-match-range pos 3) 'month)
  11340. ((org-pos-in-match-range pos 7) 'hour)
  11341. ((org-pos-in-match-range pos 8) 'minute)
  11342. ((or (org-pos-in-match-range pos 4)
  11343. (org-pos-in-match-range pos 5)) 'day)
  11344. ((and (> pos (or (match-end 8) (match-end 5)))
  11345. (< pos (match-end 0)))
  11346. (- pos (or (match-end 8) (match-end 5))))
  11347. (t 'day))))
  11348. ans))
  11349. (defun org-toggle-timestamp-type ()
  11350. "Toggle the type (<active> or [inactive]) of a time stamp."
  11351. (interactive)
  11352. (when (org-at-timestamp-p t)
  11353. (save-excursion
  11354. (goto-char (match-beginning 0))
  11355. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  11356. (goto-char (1- (match-end 0)))
  11357. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  11358. (message "Timestamp is now %sactive"
  11359. (if (equal (char-before) ?>) "in" ""))))
  11360. (defun org-timestamp-change (n &optional what)
  11361. "Change the date in the time stamp at point.
  11362. The date will be changed by N times WHAT. WHAT can be `day', `month',
  11363. `year', `minute', `second'. If WHAT is not given, the cursor position
  11364. in the timestamp determines what will be changed."
  11365. (let ((pos (point))
  11366. with-hm inactive
  11367. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  11368. org-ts-what
  11369. extra rem
  11370. ts time time0)
  11371. (if (not (org-at-timestamp-p t))
  11372. (error "Not at a timestamp"))
  11373. (if (and (not what) (eq org-ts-what 'bracket))
  11374. (org-toggle-timestamp-type)
  11375. (if (and (not what) (not (eq org-ts-what 'day))
  11376. org-display-custom-times
  11377. (get-text-property (point) 'display)
  11378. (not (get-text-property (1- (point)) 'display)))
  11379. (setq org-ts-what 'day))
  11380. (setq org-ts-what (or what org-ts-what)
  11381. inactive (= (char-after (match-beginning 0)) ?\[)
  11382. ts (match-string 0))
  11383. (replace-match "")
  11384. (if (string-match
  11385. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  11386. ts)
  11387. (setq extra (match-string 1 ts)))
  11388. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  11389. (setq with-hm t))
  11390. (setq time0 (org-parse-time-string ts))
  11391. (when (and (eq org-ts-what 'minute)
  11392. (eq current-prefix-arg nil))
  11393. (setq n (* dm (org-no-warnings (signum n))))
  11394. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  11395. (setcar (cdr time0) (+ (nth 1 time0)
  11396. (if (> n 0) (- rem) (- dm rem))))))
  11397. (setq time
  11398. (encode-time (or (car time0) 0)
  11399. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  11400. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  11401. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  11402. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  11403. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  11404. (nthcdr 6 time0)))
  11405. (when (integerp org-ts-what)
  11406. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  11407. (if (eq what 'calendar)
  11408. (let ((cal-date (org-get-date-from-calendar)))
  11409. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  11410. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  11411. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  11412. (setcar time0 (or (car time0) 0))
  11413. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  11414. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  11415. (setq time (apply 'encode-time time0))))
  11416. (setq org-last-changed-timestamp
  11417. (org-insert-time-stamp time with-hm inactive nil nil extra))
  11418. (org-clock-update-time-maybe)
  11419. (goto-char pos)
  11420. ;; Try to recenter the calendar window, if any
  11421. (if (and org-calendar-follow-timestamp-change
  11422. (get-buffer-window "*Calendar*" t)
  11423. (memq org-ts-what '(day month year)))
  11424. (org-recenter-calendar (time-to-days time))))))
  11425. ;; FIXME: does not yet work for lead times
  11426. (defun org-modify-ts-extra (s pos n dm)
  11427. "Change the different parts of the lead-time and repeat fields in timestamp."
  11428. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  11429. ng h m new rem)
  11430. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  11431. (cond
  11432. ((or (org-pos-in-match-range pos 2)
  11433. (org-pos-in-match-range pos 3))
  11434. (setq m (string-to-number (match-string 3 s))
  11435. h (string-to-number (match-string 2 s)))
  11436. (if (org-pos-in-match-range pos 2)
  11437. (setq h (+ h n))
  11438. (setq n (* dm (org-no-warnings (signum n))))
  11439. (when (not (= 0 (setq rem (% m dm))))
  11440. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  11441. (setq m (+ m n)))
  11442. (if (< m 0) (setq m (+ m 60) h (1- h)))
  11443. (if (> m 59) (setq m (- m 60) h (1+ h)))
  11444. (setq h (min 24 (max 0 h)))
  11445. (setq ng 1 new (format "-%02d:%02d" h m)))
  11446. ((org-pos-in-match-range pos 6)
  11447. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  11448. ((org-pos-in-match-range pos 5)
  11449. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  11450. ((org-pos-in-match-range pos 9)
  11451. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  11452. ((org-pos-in-match-range pos 8)
  11453. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  11454. (when ng
  11455. (setq s (concat
  11456. (substring s 0 (match-beginning ng))
  11457. new
  11458. (substring s (match-end ng))))))
  11459. s))
  11460. (defun org-recenter-calendar (date)
  11461. "If the calendar is visible, recenter it to DATE."
  11462. (let* ((win (selected-window))
  11463. (cwin (get-buffer-window "*Calendar*" t))
  11464. (calendar-move-hook nil))
  11465. (when cwin
  11466. (select-window cwin)
  11467. (calendar-goto-date (if (listp date) date
  11468. (calendar-gregorian-from-absolute date)))
  11469. (select-window win))))
  11470. (defun org-goto-calendar (&optional arg)
  11471. "Go to the Emacs calendar at the current date.
  11472. If there is a time stamp in the current line, go to that date.
  11473. A prefix ARG can be used to force the current date."
  11474. (interactive "P")
  11475. (let ((tsr org-ts-regexp) diff
  11476. (calendar-move-hook nil)
  11477. (view-calendar-holidays-initially nil)
  11478. (view-diary-entries-initially nil))
  11479. (if (or (org-at-timestamp-p)
  11480. (save-excursion
  11481. (beginning-of-line 1)
  11482. (looking-at (concat ".*" tsr))))
  11483. (let ((d1 (time-to-days (current-time)))
  11484. (d2 (time-to-days
  11485. (org-time-string-to-time (match-string 1)))))
  11486. (setq diff (- d2 d1))))
  11487. (calendar)
  11488. (calendar-goto-today)
  11489. (if (and diff (not arg)) (calendar-forward-day diff))))
  11490. (defun org-get-date-from-calendar ()
  11491. "Return a list (month day year) of date at point in calendar."
  11492. (with-current-buffer "*Calendar*"
  11493. (save-match-data
  11494. (calendar-cursor-to-date))))
  11495. (defun org-date-from-calendar ()
  11496. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  11497. If there is already a time stamp at the cursor position, update it."
  11498. (interactive)
  11499. (if (org-at-timestamp-p t)
  11500. (org-timestamp-change 0 'calendar)
  11501. (let ((cal-date (org-get-date-from-calendar)))
  11502. (org-insert-time-stamp
  11503. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  11504. ;;; The clock for measuring work time.
  11505. (defvar org-mode-line-string "")
  11506. (put 'org-mode-line-string 'risky-local-variable t)
  11507. (defvar org-mode-line-timer nil)
  11508. (defvar org-clock-heading "")
  11509. (defvar org-clock-start-time "")
  11510. (defun org-update-mode-line ()
  11511. (let* ((delta (- (time-to-seconds (current-time))
  11512. (time-to-seconds org-clock-start-time)))
  11513. (h (floor delta 3600))
  11514. (m (floor (- delta (* 3600 h)) 60)))
  11515. (setq org-mode-line-string
  11516. (propertize (format "-[%d:%02d (%s)]" h m org-clock-heading)
  11517. 'help-echo "Org-mode clock is running"))
  11518. (force-mode-line-update)))
  11519. (defvar org-clock-marker (make-marker)
  11520. "Marker recording the last clock-in.")
  11521. (defvar org-clock-mode-line-entry nil
  11522. "Information for the modeline about the running clock.")
  11523. (defun org-clock-in ()
  11524. "Start the clock on the current item.
  11525. If necessary, clock-out of the currently active clock."
  11526. (interactive)
  11527. (org-clock-out t)
  11528. (let (ts)
  11529. (save-excursion
  11530. (org-back-to-heading t)
  11531. (when (and org-clock-in-switch-to-state
  11532. (not (looking-at (concat outline-regexp "[ \t]*"
  11533. org-clock-in-switch-to-state
  11534. "\\>"))))
  11535. (org-todo org-clock-in-switch-to-state))
  11536. (if (and org-clock-heading-function
  11537. (functionp org-clock-heading-function))
  11538. (setq org-clock-heading (funcall org-clock-heading-function))
  11539. (if (looking-at org-complex-heading-regexp)
  11540. (setq org-clock-heading (match-string 4))
  11541. (setq org-clock-heading "???")))
  11542. (setq org-clock-heading (propertize org-clock-heading 'face nil))
  11543. (org-clock-find-position)
  11544. (insert "\n") (backward-char 1)
  11545. (indent-relative)
  11546. (insert org-clock-string " ")
  11547. (setq org-clock-start-time (current-time))
  11548. (setq ts (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  11549. (move-marker org-clock-marker (point) (buffer-base-buffer))
  11550. (or global-mode-string (setq global-mode-string '("")))
  11551. (or (memq 'org-mode-line-string global-mode-string)
  11552. (setq global-mode-string
  11553. (append global-mode-string '(org-mode-line-string))))
  11554. (org-update-mode-line)
  11555. (setq org-mode-line-timer (run-with-timer 60 60 'org-update-mode-line))
  11556. (message "Clock started at %s" ts))))
  11557. (defun org-clock-find-position ()
  11558. "Find the location where the next clock line should be inserted."
  11559. (org-back-to-heading t)
  11560. (catch 'exit
  11561. (let ((beg (save-excursion
  11562. (beginning-of-line 2)
  11563. (or (bolp) (newline))
  11564. (point)))
  11565. (end (progn (outline-next-heading) (point)))
  11566. (re (concat "^[ \t]*" org-clock-string))
  11567. (cnt 0)
  11568. first last)
  11569. (goto-char beg)
  11570. (when (eobp) (newline) (setq end (max (point) end)))
  11571. (when (re-search-forward "^[ \t]*:CLOCK:" end t)
  11572. ;; we seem to have a CLOCK drawer, so go there.
  11573. (beginning-of-line 2)
  11574. (throw 'exit t))
  11575. ;; Lets count the CLOCK lines
  11576. (goto-char beg)
  11577. (while (re-search-forward re end t)
  11578. (setq first (or first (match-beginning 0))
  11579. last (match-beginning 0)
  11580. cnt (1+ cnt)))
  11581. (when (and (integerp org-clock-into-drawer)
  11582. (>= (1+ cnt) org-clock-into-drawer))
  11583. ;; Wrap current entries into a new drawer
  11584. (goto-char last)
  11585. (beginning-of-line 2)
  11586. (if (org-at-item-p) (org-end-of-item))
  11587. (insert ":END:\n")
  11588. (beginning-of-line 0)
  11589. (org-indent-line-function)
  11590. (goto-char first)
  11591. (insert ":CLOCK:\n")
  11592. (beginning-of-line 0)
  11593. (org-indent-line-function)
  11594. (org-flag-drawer t)
  11595. (beginning-of-line 2)
  11596. (throw 'exit nil))
  11597. (goto-char beg)
  11598. (while (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  11599. (not (equal (match-string 1) org-clock-string)))
  11600. ;; Planning info, skip to after it
  11601. (beginning-of-line 2)
  11602. (or (bolp) (newline)))
  11603. (when (eq t org-clock-into-drawer)
  11604. (insert ":CLOCK:\n:END:\n")
  11605. (beginning-of-line -1)
  11606. (org-indent-line-function)
  11607. (org-flag-drawer t)
  11608. (beginning-of-line 2)
  11609. (org-indent-line-function)))))
  11610. (defun org-clock-out (&optional fail-quietly)
  11611. "Stop the currently running clock.
  11612. If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
  11613. (interactive)
  11614. (catch 'exit
  11615. (if (not (marker-buffer org-clock-marker))
  11616. (if fail-quietly (throw 'exit t) (error "No active clock")))
  11617. (let (ts te s h m)
  11618. (save-excursion
  11619. (set-buffer (marker-buffer org-clock-marker))
  11620. (goto-char org-clock-marker)
  11621. (beginning-of-line 1)
  11622. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  11623. (equal (match-string 1) org-clock-string))
  11624. (setq ts (match-string 2))
  11625. (if fail-quietly (throw 'exit nil) (error "Clock start time is gone")))
  11626. (goto-char (match-end 0))
  11627. (delete-region (point) (point-at-eol))
  11628. (insert "--")
  11629. (setq te (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  11630. (setq s (- (time-to-seconds (apply 'encode-time (org-parse-time-string te)))
  11631. (time-to-seconds (apply 'encode-time (org-parse-time-string ts))))
  11632. h (floor (/ s 3600))
  11633. s (- s (* 3600 h))
  11634. m (floor (/ s 60))
  11635. s (- s (* 60 s)))
  11636. (insert " => " (format "%2d:%02d" h m))
  11637. (move-marker org-clock-marker nil)
  11638. (when org-log-note-clock-out
  11639. (org-add-log-maybe 'clock-out))
  11640. (when org-mode-line-timer
  11641. (cancel-timer org-mode-line-timer)
  11642. (setq org-mode-line-timer nil))
  11643. (setq global-mode-string
  11644. (delq 'org-mode-line-string global-mode-string))
  11645. (force-mode-line-update)
  11646. (message "Clock stopped at %s after HH:MM = %d:%02d" te h m)))))
  11647. (defun org-clock-cancel ()
  11648. "Cancel the running clock be removing the start timestamp."
  11649. (interactive)
  11650. (if (not (marker-buffer org-clock-marker))
  11651. (error "No active clock"))
  11652. (save-excursion
  11653. (set-buffer (marker-buffer org-clock-marker))
  11654. (goto-char org-clock-marker)
  11655. (delete-region (1- (point-at-bol)) (point-at-eol)))
  11656. (setq global-mode-string
  11657. (delq 'org-mode-line-string global-mode-string))
  11658. (force-mode-line-update)
  11659. (message "Clock canceled"))
  11660. (defun org-clock-goto (&optional delete-windows)
  11661. "Go to the currently clocked-in entry."
  11662. (interactive "P")
  11663. (if (not (marker-buffer org-clock-marker))
  11664. (error "No active clock"))
  11665. (switch-to-buffer-other-window
  11666. (marker-buffer org-clock-marker))
  11667. (if delete-windows (delete-other-windows))
  11668. (goto-char org-clock-marker)
  11669. (org-show-entry)
  11670. (org-back-to-heading)
  11671. (recenter))
  11672. (defvar org-clock-file-total-minutes nil
  11673. "Holds the file total time in minutes, after a call to `org-clock-sum'.")
  11674. (make-variable-buffer-local 'org-clock-file-total-minutes)
  11675. (defun org-clock-sum (&optional tstart tend)
  11676. "Sum the times for each subtree.
  11677. Puts the resulting times in minutes as a text property on each headline."
  11678. (interactive)
  11679. (let* ((bmp (buffer-modified-p))
  11680. (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
  11681. org-clock-string
  11682. "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
  11683. (lmax 30)
  11684. (ltimes (make-vector lmax 0))
  11685. (t1 0)
  11686. (level 0)
  11687. ts te dt
  11688. time)
  11689. (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t))
  11690. (save-excursion
  11691. (goto-char (point-max))
  11692. (while (re-search-backward re nil t)
  11693. (cond
  11694. ((match-end 2)
  11695. ;; Two time stamps
  11696. (setq ts (match-string 2)
  11697. te (match-string 3)
  11698. ts (time-to-seconds
  11699. (apply 'encode-time (org-parse-time-string ts)))
  11700. te (time-to-seconds
  11701. (apply 'encode-time (org-parse-time-string te)))
  11702. ts (if tstart (max ts tstart) ts)
  11703. te (if tend (min te tend) te)
  11704. dt (- te ts)
  11705. t1 (if (> dt 0) (+ t1 (floor (/ dt 60))) t1)))
  11706. ((match-end 4)
  11707. ;; A naket time
  11708. (setq t1 (+ t1 (string-to-number (match-string 5))
  11709. (* 60 (string-to-number (match-string 4))))))
  11710. (t ;; A headline
  11711. (setq level (- (match-end 1) (match-beginning 1)))
  11712. (when (or (> t1 0) (> (aref ltimes level) 0))
  11713. (loop for l from 0 to level do
  11714. (aset ltimes l (+ (aref ltimes l) t1)))
  11715. (setq t1 0 time (aref ltimes level))
  11716. (loop for l from level to (1- lmax) do
  11717. (aset ltimes l 0))
  11718. (goto-char (match-beginning 0))
  11719. (put-text-property (point) (point-at-eol) :org-clock-minutes time)))))
  11720. (setq org-clock-file-total-minutes (aref ltimes 0)))
  11721. (set-buffer-modified-p bmp)))
  11722. (defun org-clock-display (&optional total-only)
  11723. "Show subtree times in the entire buffer.
  11724. If TOTAL-ONLY is non-nil, only show the total time for the entire file
  11725. in the echo area."
  11726. (interactive)
  11727. (org-remove-clock-overlays)
  11728. (let (time h m p)
  11729. (org-clock-sum)
  11730. (unless total-only
  11731. (save-excursion
  11732. (goto-char (point-min))
  11733. (while (or (and (equal (setq p (point)) (point-min))
  11734. (get-text-property p :org-clock-minutes))
  11735. (setq p (next-single-property-change
  11736. (point) :org-clock-minutes)))
  11737. (goto-char p)
  11738. (when (setq time (get-text-property p :org-clock-minutes))
  11739. (org-put-clock-overlay time (funcall outline-level))))
  11740. (setq h (/ org-clock-file-total-minutes 60)
  11741. m (- org-clock-file-total-minutes (* 60 h)))
  11742. ;; Arrange to remove the overlays upon next change.
  11743. (when org-remove-highlights-with-change
  11744. (org-add-hook 'before-change-functions 'org-remove-clock-overlays
  11745. nil 'local))))
  11746. (message "Total file time: %d:%02d (%d hours and %d minutes)" h m h m)))
  11747. (defvar org-clock-overlays nil)
  11748. (make-variable-buffer-local 'org-clock-overlays)
  11749. (defun org-put-clock-overlay (time &optional level)
  11750. "Put an overlays on the current line, displaying TIME.
  11751. If LEVEL is given, prefix time with a corresponding number of stars.
  11752. This creates a new overlay and stores it in `org-clock-overlays', so that it
  11753. will be easy to remove."
  11754. (let* ((c 60) (h (floor (/ time 60))) (m (- time (* 60 h)))
  11755. (l (if level (org-get-valid-level level 0) 0))
  11756. (off 0)
  11757. ov tx)
  11758. (move-to-column c)
  11759. (unless (eolp) (skip-chars-backward "^ \t"))
  11760. (skip-chars-backward " \t")
  11761. (setq ov (org-make-overlay (1- (point)) (point-at-eol))
  11762. tx (concat (buffer-substring (1- (point)) (point))
  11763. (make-string (+ off (max 0 (- c (current-column)))) ?.)
  11764. (org-add-props (format "%s %2d:%02d%s"
  11765. (make-string l ?*) h m
  11766. (make-string (- 16 l) ?\ ))
  11767. '(face secondary-selection))
  11768. ""))
  11769. (if (not (featurep 'xemacs))
  11770. (org-overlay-put ov 'display tx)
  11771. (org-overlay-put ov 'invisible t)
  11772. (org-overlay-put ov 'end-glyph (make-glyph tx)))
  11773. (push ov org-clock-overlays)))
  11774. (defun org-remove-clock-overlays (&optional beg end noremove)
  11775. "Remove the occur highlights from the buffer.
  11776. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11777. from the `before-change-functions' in the current buffer."
  11778. (interactive)
  11779. (unless org-inhibit-highlight-removal
  11780. (mapc 'org-delete-overlay org-clock-overlays)
  11781. (setq org-clock-overlays nil)
  11782. (unless noremove
  11783. (remove-hook 'before-change-functions
  11784. 'org-remove-clock-overlays 'local))))
  11785. (defun org-clock-out-if-current ()
  11786. "Clock out if the current entry contains the running clock.
  11787. This is used to stop the clock after a TODO entry is marked DONE,
  11788. and is only done if the variable `org-clock-out-when-done' is not nil."
  11789. (when (and org-clock-out-when-done
  11790. (member state org-done-keywords)
  11791. (equal (marker-buffer org-clock-marker) (current-buffer))
  11792. (< (point) org-clock-marker)
  11793. (> (save-excursion (outline-next-heading) (point))
  11794. org-clock-marker))
  11795. ;; Clock out, but don't accept a logging message for this.
  11796. (let ((org-log-note-clock-out nil))
  11797. (org-clock-out))))
  11798. (add-hook 'org-after-todo-state-change-hook
  11799. 'org-clock-out-if-current)
  11800. (defun org-check-running-clock ()
  11801. "Check if the current buffer contains the running clock.
  11802. If yes, offer to stop it and to save the buffer with the changes."
  11803. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  11804. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  11805. (buffer-name))))
  11806. (org-clock-out)
  11807. (when (y-or-n-p "Save changed buffer?")
  11808. (save-buffer))))
  11809. (defun org-clock-report (&optional arg)
  11810. "Create a table containing a report about clocked time.
  11811. If the cursor is inside an existing clocktable block, then the table
  11812. will be updated. If not, a new clocktable will be inserted.
  11813. When called with a prefix argument, move to the first clock table in the
  11814. buffer and update it."
  11815. (interactive "P")
  11816. (org-remove-clock-overlays)
  11817. (when arg
  11818. (org-find-dblock "clocktable")
  11819. (org-show-entry))
  11820. (if (org-in-clocktable-p)
  11821. (goto-char (org-in-clocktable-p))
  11822. (org-create-dblock (list :name "clocktable"
  11823. :maxlevel 2 :scope 'file)))
  11824. (org-update-dblock))
  11825. (defun org-in-clocktable-p ()
  11826. "Check if the cursor is in a clocktable."
  11827. (let ((pos (point)) start)
  11828. (save-excursion
  11829. (end-of-line 1)
  11830. (and (re-search-backward "^#\\+BEGIN:[ \t]+clocktable" nil t)
  11831. (setq start (match-beginning 0))
  11832. (re-search-forward "^#\\+END:.*" nil t)
  11833. (>= (match-end 0) pos)
  11834. start))))
  11835. (defun org-clock-update-time-maybe ()
  11836. "If this is a CLOCK line, update it and return t.
  11837. Otherwise, return nil."
  11838. (interactive)
  11839. (save-excursion
  11840. (beginning-of-line 1)
  11841. (skip-chars-forward " \t")
  11842. (when (looking-at org-clock-string)
  11843. (let ((re (concat "[ \t]*" org-clock-string
  11844. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  11845. "\\([ \t]*=>.*\\)?"))
  11846. ts te h m s)
  11847. (if (not (looking-at re))
  11848. nil
  11849. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  11850. (end-of-line 1)
  11851. (setq ts (match-string 1)
  11852. te (match-string 2))
  11853. (setq s (- (time-to-seconds
  11854. (apply 'encode-time (org-parse-time-string te)))
  11855. (time-to-seconds
  11856. (apply 'encode-time (org-parse-time-string ts))))
  11857. h (floor (/ s 3600))
  11858. s (- s (* 3600 h))
  11859. m (floor (/ s 60))
  11860. s (- s (* 60 s)))
  11861. (insert " => " (format "%2d:%02d" h m))
  11862. t)))))
  11863. (defun org-clock-special-range (key &optional time as-strings)
  11864. "Return two times bordering a special time range.
  11865. Key is a symbol specifying the range and can be one of `today', `yesterday',
  11866. `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
  11867. A week starts Monday 0:00 and ends Sunday 24:00.
  11868. The range is determined relative to TIME. TIME defaults to the current time.
  11869. The return value is a cons cell with two internal times like the ones
  11870. returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
  11871. the returned times will be formatted strings."
  11872. (let* ((tm (decode-time (or time (current-time))))
  11873. (s 0) (m (nth 1 tm)) (h (nth 2 tm))
  11874. (d (nth 3 tm)) (month (nth 4 tm)) (y (nth 5 tm))
  11875. (dow (nth 6 tm))
  11876. s1 m1 h1 d1 month1 y1 diff ts te fm)
  11877. (cond
  11878. ((eq key 'today)
  11879. (setq h 0 m 0 h1 24 m1 0))
  11880. ((eq key 'yesterday)
  11881. (setq d (1- d) h 0 m 0 h1 24 m1 0))
  11882. ((eq key 'thisweek)
  11883. (setq diff (if (= dow 0) 6 (1- dow))
  11884. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  11885. ((eq key 'lastweek)
  11886. (setq diff (+ 7 (if (= dow 0) 6 (1- dow)))
  11887. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  11888. ((eq key 'thismonth)
  11889. (setq d 1 h 0 m 0 d1 1 month1 (1+ month) h1 0 m1 0))
  11890. ((eq key 'lastmonth)
  11891. (setq d 1 h 0 m 0 d1 1 month (1- month) month1 (1+ month) h1 0 m1 0))
  11892. ((eq key 'thisyear)
  11893. (setq m 0 h 0 d 1 month 1 y1 (1+ y)))
  11894. ((eq key 'lastyear)
  11895. (setq m 0 h 0 d 1 month 1 y (1- y) y1 (1+ y)))
  11896. (t (error "No such time block %s" key)))
  11897. (setq ts (encode-time s m h d month y)
  11898. te (encode-time (or s1 s) (or m1 m) (or h1 h)
  11899. (or d1 d) (or month1 month) (or y1 y)))
  11900. (setq fm (cdr org-time-stamp-formats))
  11901. (if as-strings
  11902. (cons (format-time-string fm ts) (format-time-string fm te))
  11903. (cons ts te))))
  11904. (defun org-dblock-write:clocktable (params)
  11905. "Write the standard clocktable."
  11906. (catch 'exit
  11907. (let* ((hlchars '((1 . "*") (2 . "/")))
  11908. (ins (make-marker))
  11909. (total-time nil)
  11910. (scope (plist-get params :scope))
  11911. (tostring (plist-get params :tostring))
  11912. (multifile (plist-get params :multifile))
  11913. (header (plist-get params :header))
  11914. (maxlevel (or (plist-get params :maxlevel) 3))
  11915. (step (plist-get params :step))
  11916. (emph (plist-get params :emphasize))
  11917. (ts (plist-get params :tstart))
  11918. (te (plist-get params :tend))
  11919. (block (plist-get params :block))
  11920. (link (plist-get params :link))
  11921. ipos time h m p level hlc hdl
  11922. cc beg end pos tbl)
  11923. (when step
  11924. (org-clocktable-steps params)
  11925. (throw 'exit nil))
  11926. (when block
  11927. (setq cc (org-clock-special-range block nil t)
  11928. ts (car cc) te (cdr cc)))
  11929. (if ts (setq ts (time-to-seconds
  11930. (apply 'encode-time (org-parse-time-string ts)))))
  11931. (if te (setq te (time-to-seconds
  11932. (apply 'encode-time (org-parse-time-string te)))))
  11933. (move-marker ins (point))
  11934. (setq ipos (point))
  11935. ;; Get the right scope
  11936. (setq pos (point))
  11937. (save-restriction
  11938. (cond
  11939. ((not scope))
  11940. ((eq scope 'file) (widen))
  11941. ((eq scope 'subtree) (org-narrow-to-subtree))
  11942. ((eq scope 'tree)
  11943. (while (org-up-heading-safe))
  11944. (org-narrow-to-subtree))
  11945. ((and (symbolp scope) (string-match "^tree\\([0-9]+\\)$"
  11946. (symbol-name scope)))
  11947. (setq level (string-to-number (match-string 1 (symbol-name scope))))
  11948. (catch 'exit
  11949. (while (org-up-heading-safe)
  11950. (looking-at outline-regexp)
  11951. (if (<= (org-reduced-level (funcall outline-level)) level)
  11952. (throw 'exit nil))))
  11953. (org-narrow-to-subtree))
  11954. ((or (listp scope) (eq scope 'agenda))
  11955. (let* ((files (if (listp scope) scope (org-agenda-files)))
  11956. (scope 'agenda)
  11957. (p1 (copy-sequence params))
  11958. file)
  11959. (plist-put p1 :tostring t)
  11960. (plist-put p1 :multifile t)
  11961. (plist-put p1 :scope 'file)
  11962. (org-prepare-agenda-buffers files)
  11963. (while (setq file (pop files))
  11964. (with-current-buffer (find-buffer-visiting file)
  11965. (push (org-clocktable-add-file
  11966. file (org-dblock-write:clocktable p1)) tbl)
  11967. (setq total-time (+ (or total-time 0)
  11968. org-clock-file-total-minutes)))))))
  11969. (goto-char pos)
  11970. (unless (eq scope 'agenda)
  11971. (org-clock-sum ts te)
  11972. (goto-char (point-min))
  11973. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  11974. (goto-char p)
  11975. (when (setq time (get-text-property p :org-clock-minutes))
  11976. (save-excursion
  11977. (beginning-of-line 1)
  11978. (when (and (looking-at (org-re "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[[:alnum:]_@:]+:\\)?[ \t]*$"))
  11979. (setq level (org-reduced-level
  11980. (- (match-end 1) (match-beginning 1))))
  11981. (<= level maxlevel))
  11982. (setq hlc (if emph (or (cdr (assoc level hlchars)) "") "")
  11983. hdl (if (not link)
  11984. (match-string 2)
  11985. (org-make-link-string
  11986. (format "file:%s::%s"
  11987. (buffer-file-name)
  11988. (save-match-data
  11989. (org-make-org-heading-search-string
  11990. (match-string 2))))
  11991. (match-string 2)))
  11992. h (/ time 60)
  11993. m (- time (* 60 h)))
  11994. (if (and (not multifile) (= level 1)) (push "|-" tbl))
  11995. (push (concat
  11996. "| " (int-to-string level) "|" hlc hdl hlc " |"
  11997. (make-string (1- level) ?|)
  11998. hlc (format "%d:%02d" h m) hlc
  11999. " |") tbl))))))
  12000. (setq tbl (nreverse tbl))
  12001. (if tostring
  12002. (if tbl (mapconcat 'identity tbl "\n") nil)
  12003. (goto-char ins)
  12004. (insert-before-markers
  12005. (or header
  12006. (concat
  12007. "Clock summary at ["
  12008. (substring
  12009. (format-time-string (cdr org-time-stamp-formats))
  12010. 1 -1)
  12011. "]."
  12012. (if block
  12013. (format " Considered range is /%s/." block)
  12014. "")
  12015. "\n\n"))
  12016. (if (eq scope 'agenda) "|File" "")
  12017. "|L|Headline|Time|\n")
  12018. (setq total-time (or total-time org-clock-file-total-minutes)
  12019. h (/ total-time 60)
  12020. m (- total-time (* 60 h)))
  12021. (insert-before-markers
  12022. "|-\n|"
  12023. (if (eq scope 'agenda) "|" "")
  12024. "|"
  12025. "*Total time*| "
  12026. (format "*%d:%02d*" h m)
  12027. "|\n|-\n")
  12028. (setq tbl (delq nil tbl))
  12029. (if (and (stringp (car tbl)) (> (length (car tbl)) 1)
  12030. (equal (substring (car tbl) 0 2) "|-"))
  12031. (pop tbl))
  12032. (insert-before-markers (mapconcat
  12033. 'identity (delq nil tbl)
  12034. (if (eq scope 'agenda) "\n|-\n" "\n")))
  12035. (backward-delete-char 1)
  12036. (goto-char ipos)
  12037. (skip-chars-forward "^|")
  12038. (org-table-align))))))
  12039. (defun org-clocktable-steps (params)
  12040. (let* ((p1 (copy-sequence params))
  12041. (ts (plist-get p1 :tstart))
  12042. (te (plist-get p1 :tend))
  12043. (step0 (plist-get p1 :step))
  12044. (step (cdr (assoc step0 '((day . 86400) (week . 604800)))))
  12045. (block (plist-get p1 :block))
  12046. cc)
  12047. (when block
  12048. (setq cc (org-clock-special-range block nil t)
  12049. ts (car cc) te (cdr cc)))
  12050. (if ts (setq ts (time-to-seconds
  12051. (apply 'encode-time (org-parse-time-string ts)))))
  12052. (if te (setq te (time-to-seconds
  12053. (apply 'encode-time (org-parse-time-string te)))))
  12054. (plist-put p1 :header "")
  12055. (plist-put p1 :step nil)
  12056. (plist-put p1 :block nil)
  12057. (while (< ts te)
  12058. (or (bolp) (insert "\n"))
  12059. (plist-put p1 :tstart (format-time-string
  12060. (car org-time-stamp-formats)
  12061. (seconds-to-time ts)))
  12062. (plist-put p1 :tend (format-time-string
  12063. (car org-time-stamp-formats)
  12064. (seconds-to-time (setq ts (+ ts step)))))
  12065. (insert "\n" (if (eq step0 'day) "Daily report: " "Weekly report starting on: ")
  12066. (plist-get p1 :tstart) "\n")
  12067. (org-dblock-write:clocktable p1)
  12068. (re-search-forward "#\\+END:")
  12069. (end-of-line 0))))
  12070. (defun org-clocktable-add-file (file table)
  12071. (if table
  12072. (let ((lines (org-split-string table "\n"))
  12073. (ff (file-name-nondirectory file)))
  12074. (mapconcat 'identity
  12075. (mapcar (lambda (x)
  12076. (if (string-match org-table-dataline-regexp x)
  12077. (concat "|" ff x)
  12078. x))
  12079. lines)
  12080. "\n"))))
  12081. ;; FIXME: I don't think anybody uses this, ask David
  12082. (defun org-collect-clock-time-entries ()
  12083. "Return an internal list with clocking information.
  12084. This list has one entry for each CLOCK interval.
  12085. FIXME: describe the elements."
  12086. (interactive)
  12087. (let ((re (concat "^[ \t]*" org-clock-string
  12088. " *\\[\\(.*?\\)\\]--\\[\\(.*?\\)\\]"))
  12089. rtn beg end next cont level title total closedp leafp
  12090. clockpos titlepos h m donep)
  12091. (save-excursion
  12092. (org-clock-sum)
  12093. (goto-char (point-min))
  12094. (while (re-search-forward re nil t)
  12095. (setq clockpos (match-beginning 0)
  12096. beg (match-string 1) end (match-string 2)
  12097. cont (match-end 0))
  12098. (setq beg (apply 'encode-time (org-parse-time-string beg))
  12099. end (apply 'encode-time (org-parse-time-string end)))
  12100. (org-back-to-heading t)
  12101. (setq donep (org-entry-is-done-p))
  12102. (setq titlepos (point)
  12103. total (or (get-text-property (1+ (point)) :org-clock-minutes) 0)
  12104. h (/ total 60) m (- total (* 60 h))
  12105. total (cons h m))
  12106. (looking-at "\\(\\*+\\) +\\(.*\\)")
  12107. (setq level (- (match-end 1) (match-beginning 1))
  12108. title (org-match-string-no-properties 2))
  12109. (save-excursion (outline-next-heading) (setq next (point)))
  12110. (setq closedp (re-search-forward org-closed-time-regexp next t))
  12111. (goto-char next)
  12112. (setq leafp (and (looking-at "^\\*+ ")
  12113. (<= (- (match-end 0) (point)) level)))
  12114. (push (list beg end clockpos closedp donep
  12115. total title titlepos level leafp)
  12116. rtn)
  12117. (goto-char cont)))
  12118. (nreverse rtn)))
  12119. ;;;; Agenda files
  12120. (defun org-agenda-files (&optional unrestricted)
  12121. "Get the list of agenda files.
  12122. Optional UNRESTRICTED means return the full list even if a restriction
  12123. is currently in place."
  12124. (let ((files
  12125. (cond
  12126. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  12127. ((stringp org-agenda-files) (org-read-agenda-file-list))
  12128. ((listp org-agenda-files) org-agenda-files)
  12129. (t (error "Invalid value of `org-agenda-files'")))))
  12130. (setq files (apply 'append
  12131. (mapcar (lambda (f)
  12132. (if (file-directory-p f)
  12133. (directory-files f t
  12134. org-agenda-file-regexp)
  12135. (list f)))
  12136. files)))
  12137. (if org-agenda-skip-unavailable-files
  12138. (delq nil
  12139. (mapcar (function
  12140. (lambda (file)
  12141. (and (file-readable-p file) file)))
  12142. files))
  12143. files))) ; `org-check-agenda-file' will remove them from the list
  12144. (defun org-edit-agenda-file-list ()
  12145. "Edit the list of agenda files.
  12146. Depending on setup, this either uses customize to edit the variable
  12147. `org-agenda-files', or it visits the file that is holding the list. In the
  12148. latter case, the buffer is set up in a way that saving it automatically kills
  12149. the buffer and restores the previous window configuration."
  12150. (interactive)
  12151. (if (stringp org-agenda-files)
  12152. (let ((cw (current-window-configuration)))
  12153. (find-file org-agenda-files)
  12154. (org-set-local 'org-window-configuration cw)
  12155. (org-add-hook 'after-save-hook
  12156. (lambda ()
  12157. (set-window-configuration
  12158. (prog1 org-window-configuration
  12159. (kill-buffer (current-buffer))))
  12160. (org-install-agenda-files-menu)
  12161. (message "New agenda file list installed"))
  12162. nil 'local)
  12163. (message "%s" (substitute-command-keys
  12164. "Edit list and finish with \\[save-buffer]")))
  12165. (customize-variable 'org-agenda-files)))
  12166. (defun org-store-new-agenda-file-list (list)
  12167. "Set new value for the agenda file list and save it correcly."
  12168. (if (stringp org-agenda-files)
  12169. (let ((f org-agenda-files) b)
  12170. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  12171. (with-temp-file f
  12172. (insert (mapconcat 'identity list "\n") "\n")))
  12173. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  12174. (setq org-agenda-files list)
  12175. (customize-save-variable 'org-agenda-files org-agenda-files))))
  12176. (defun org-read-agenda-file-list ()
  12177. "Read the list of agenda files from a file."
  12178. (when (stringp org-agenda-files)
  12179. (with-temp-buffer
  12180. (insert-file-contents org-agenda-files)
  12181. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  12182. ;;;###autoload
  12183. (defun org-cycle-agenda-files ()
  12184. "Cycle through the files in `org-agenda-files'.
  12185. If the current buffer visits an agenda file, find the next one in the list.
  12186. If the current buffer does not, find the first agenda file."
  12187. (interactive)
  12188. (let* ((fs (org-agenda-files t))
  12189. (files (append fs (list (car fs))))
  12190. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  12191. file)
  12192. (unless files (error "No agenda files"))
  12193. (catch 'exit
  12194. (while (setq file (pop files))
  12195. (if (equal (file-truename file) tcf)
  12196. (when (car files)
  12197. (find-file (car files))
  12198. (throw 'exit t))))
  12199. (find-file (car fs)))
  12200. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  12201. (defun org-agenda-file-to-front (&optional to-end)
  12202. "Move/add the current file to the top of the agenda file list.
  12203. If the file is not present in the list, it is added to the front. If it is
  12204. present, it is moved there. With optional argument TO-END, add/move to the
  12205. end of the list."
  12206. (interactive "P")
  12207. (let ((org-agenda-skip-unavailable-files nil)
  12208. (file-alist (mapcar (lambda (x)
  12209. (cons (file-truename x) x))
  12210. (org-agenda-files t)))
  12211. (ctf (file-truename buffer-file-name))
  12212. x had)
  12213. (setq x (assoc ctf file-alist) had x)
  12214. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  12215. (if to-end
  12216. (setq file-alist (append (delq x file-alist) (list x)))
  12217. (setq file-alist (cons x (delq x file-alist))))
  12218. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  12219. (org-install-agenda-files-menu)
  12220. (message "File %s to %s of agenda file list"
  12221. (if had "moved" "added") (if to-end "end" "front"))))
  12222. (defun org-remove-file (&optional file)
  12223. "Remove current file from the list of files in variable `org-agenda-files'.
  12224. These are the files which are being checked for agenda entries.
  12225. Optional argument FILE means, use this file instead of the current."
  12226. (interactive)
  12227. (let* ((org-agenda-skip-unavailable-files nil)
  12228. (file (or file buffer-file-name))
  12229. (true-file (file-truename file))
  12230. (afile (abbreviate-file-name file))
  12231. (files (delq nil (mapcar
  12232. (lambda (x)
  12233. (if (equal true-file
  12234. (file-truename x))
  12235. nil x))
  12236. (org-agenda-files t)))))
  12237. (if (not (= (length files) (length (org-agenda-files t))))
  12238. (progn
  12239. (org-store-new-agenda-file-list files)
  12240. (org-install-agenda-files-menu)
  12241. (message "Removed file: %s" afile))
  12242. (message "File was not in list: %s (not removed)" afile))))
  12243. (defun org-file-menu-entry (file)
  12244. (vector file (list 'find-file file) t))
  12245. (defun org-check-agenda-file (file)
  12246. "Make sure FILE exists. If not, ask user what to do."
  12247. (when (not (file-exists-p file))
  12248. (message "non-existent file %s. [R]emove from list or [A]bort?"
  12249. (abbreviate-file-name file))
  12250. (let ((r (downcase (read-char-exclusive))))
  12251. (cond
  12252. ((equal r ?r)
  12253. (org-remove-file file)
  12254. (throw 'nextfile t))
  12255. (t (error "Abort"))))))
  12256. (defun org-get-agenda-file-buffer (file)
  12257. "Get a buffer visiting FILE. If the buffer needs to be created, add
  12258. it to the list of buffers which might be released later."
  12259. (let ((buf (org-find-base-buffer-visiting file)))
  12260. (if buf
  12261. buf ; just return it
  12262. ;; Make a new buffer and remember it
  12263. (setq buf (find-file-noselect file))
  12264. (if buf (push buf org-agenda-new-buffers))
  12265. buf)))
  12266. (defun org-release-buffers (blist)
  12267. "Release all buffers in list, asking the user for confirmation when needed.
  12268. When a buffer is unmodified, it is just killed. When modified, it is saved
  12269. \(if the user agrees) and then killed."
  12270. (let (buf file)
  12271. (while (setq buf (pop blist))
  12272. (setq file (buffer-file-name buf))
  12273. (when (and (buffer-modified-p buf)
  12274. file
  12275. (y-or-n-p (format "Save file %s? " file)))
  12276. (with-current-buffer buf (save-buffer)))
  12277. (kill-buffer buf))))
  12278. (defun org-prepare-agenda-buffers (files)
  12279. "Create buffers for all agenda files, protect archived trees and comments."
  12280. (interactive)
  12281. (let ((pa '(:org-archived t))
  12282. (pc '(:org-comment t))
  12283. (pall '(:org-archived t :org-comment t))
  12284. (inhibit-read-only t)
  12285. (rea (concat ":" org-archive-tag ":"))
  12286. bmp file re)
  12287. (save-excursion
  12288. (save-restriction
  12289. (while (setq file (pop files))
  12290. (if (bufferp file)
  12291. (set-buffer file)
  12292. (org-check-agenda-file file)
  12293. (set-buffer (org-get-agenda-file-buffer file)))
  12294. (widen)
  12295. (setq bmp (buffer-modified-p))
  12296. (org-refresh-category-properties)
  12297. (setq org-todo-keywords-for-agenda
  12298. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  12299. (setq org-done-keywords-for-agenda
  12300. (append org-done-keywords-for-agenda org-done-keywords))
  12301. (save-excursion
  12302. (remove-text-properties (point-min) (point-max) pall)
  12303. (when org-agenda-skip-archived-trees
  12304. (goto-char (point-min))
  12305. (while (re-search-forward rea nil t)
  12306. (if (org-on-heading-p t)
  12307. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  12308. (goto-char (point-min))
  12309. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  12310. (while (re-search-forward re nil t)
  12311. (add-text-properties
  12312. (match-beginning 0) (org-end-of-subtree t) pc)))
  12313. (set-buffer-modified-p bmp))))))
  12314. ;;;; Embedded LaTeX
  12315. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  12316. "Keymap for the minor `org-cdlatex-mode'.")
  12317. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  12318. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  12319. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  12320. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  12321. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  12322. (defvar org-cdlatex-texmathp-advice-is-done nil
  12323. "Flag remembering if we have applied the advice to texmathp already.")
  12324. (define-minor-mode org-cdlatex-mode
  12325. "Toggle the minor `org-cdlatex-mode'.
  12326. This mode supports entering LaTeX environment and math in LaTeX fragments
  12327. in Org-mode.
  12328. \\{org-cdlatex-mode-map}"
  12329. nil " OCDL" nil
  12330. (when org-cdlatex-mode (require 'cdlatex))
  12331. (unless org-cdlatex-texmathp-advice-is-done
  12332. (setq org-cdlatex-texmathp-advice-is-done t)
  12333. (defadvice texmathp (around org-math-always-on activate)
  12334. "Always return t in org-mode buffers.
  12335. This is because we want to insert math symbols without dollars even outside
  12336. the LaTeX math segments. If Orgmode thinks that point is actually inside
  12337. en embedded LaTeX fragement, let texmathp do its job.
  12338. \\[org-cdlatex-mode-map]"
  12339. (interactive)
  12340. (let (p)
  12341. (cond
  12342. ((not (org-mode-p)) ad-do-it)
  12343. ((eq this-command 'cdlatex-math-symbol)
  12344. (setq ad-return-value t
  12345. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  12346. (t
  12347. (let ((p (org-inside-LaTeX-fragment-p)))
  12348. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  12349. (setq ad-return-value t
  12350. texmathp-why '("Org-mode embedded math" . 0))
  12351. (if p ad-do-it)))))))))
  12352. (defun turn-on-org-cdlatex ()
  12353. "Unconditionally turn on `org-cdlatex-mode'."
  12354. (org-cdlatex-mode 1))
  12355. (defun org-inside-LaTeX-fragment-p ()
  12356. "Test if point is inside a LaTeX fragment.
  12357. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  12358. sequence appearing also before point.
  12359. Even though the matchers for math are configurable, this function assumes
  12360. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  12361. delimiters are skipped when they have been removed by customization.
  12362. The return value is nil, or a cons cell with the delimiter and
  12363. and the position of this delimiter.
  12364. This function does a reasonably good job, but can locally be fooled by
  12365. for example currency specifications. For example it will assume being in
  12366. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  12367. fragments that are properly closed, but during editing, we have to live
  12368. with the uncertainty caused by missing closing delimiters. This function
  12369. looks only before point, not after."
  12370. (catch 'exit
  12371. (let ((pos (point))
  12372. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  12373. (lim (progn
  12374. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  12375. (point)))
  12376. dd-on str (start 0) m re)
  12377. (goto-char pos)
  12378. (when dodollar
  12379. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  12380. re (nth 1 (assoc "$" org-latex-regexps)))
  12381. (while (string-match re str start)
  12382. (cond
  12383. ((= (match-end 0) (length str))
  12384. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  12385. ((= (match-end 0) (- (length str) 5))
  12386. (throw 'exit nil))
  12387. (t (setq start (match-end 0))))))
  12388. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  12389. (goto-char pos)
  12390. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  12391. (and (match-beginning 2) (throw 'exit nil))
  12392. ;; count $$
  12393. (while (re-search-backward "\\$\\$" lim t)
  12394. (setq dd-on (not dd-on)))
  12395. (goto-char pos)
  12396. (if dd-on (cons "$$" m))))))
  12397. (defun org-try-cdlatex-tab ()
  12398. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  12399. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  12400. - inside a LaTeX fragment, or
  12401. - after the first word in a line, where an abbreviation expansion could
  12402. insert a LaTeX environment."
  12403. (when org-cdlatex-mode
  12404. (cond
  12405. ((save-excursion
  12406. (skip-chars-backward "a-zA-Z0-9*")
  12407. (skip-chars-backward " \t")
  12408. (bolp))
  12409. (cdlatex-tab) t)
  12410. ((org-inside-LaTeX-fragment-p)
  12411. (cdlatex-tab) t)
  12412. (t nil))))
  12413. (defun org-cdlatex-underscore-caret (&optional arg)
  12414. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  12415. Revert to the normal definition outside of these fragments."
  12416. (interactive "P")
  12417. (if (org-inside-LaTeX-fragment-p)
  12418. (call-interactively 'cdlatex-sub-superscript)
  12419. (let (org-cdlatex-mode)
  12420. (call-interactively (key-binding (vector last-input-event))))))
  12421. (defun org-cdlatex-math-modify (&optional arg)
  12422. "Execute `cdlatex-math-modify' in LaTeX fragments.
  12423. Revert to the normal definition outside of these fragments."
  12424. (interactive "P")
  12425. (if (org-inside-LaTeX-fragment-p)
  12426. (call-interactively 'cdlatex-math-modify)
  12427. (let (org-cdlatex-mode)
  12428. (call-interactively (key-binding (vector last-input-event))))))
  12429. (defvar org-latex-fragment-image-overlays nil
  12430. "List of overlays carrying the images of latex fragments.")
  12431. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  12432. (defun org-remove-latex-fragment-image-overlays ()
  12433. "Remove all overlays with LaTeX fragment images in current buffer."
  12434. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  12435. (setq org-latex-fragment-image-overlays nil))
  12436. (defun org-preview-latex-fragment (&optional subtree)
  12437. "Preview the LaTeX fragment at point, or all locally or globally.
  12438. If the cursor is in a LaTeX fragment, create the image and overlay
  12439. it over the source code. If there is no fragment at point, display
  12440. all fragments in the current text, from one headline to the next. With
  12441. prefix SUBTREE, display all fragments in the current subtree. With a
  12442. double prefix `C-u C-u', or when the cursor is before the first headline,
  12443. display all fragments in the buffer.
  12444. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  12445. (interactive "P")
  12446. (org-remove-latex-fragment-image-overlays)
  12447. (save-excursion
  12448. (save-restriction
  12449. (let (beg end at msg)
  12450. (cond
  12451. ((or (equal subtree '(16))
  12452. (not (save-excursion
  12453. (re-search-backward (concat "^" outline-regexp) nil t))))
  12454. (setq beg (point-min) end (point-max)
  12455. msg "Creating images for buffer...%s"))
  12456. ((equal subtree '(4))
  12457. (org-back-to-heading)
  12458. (setq beg (point) end (org-end-of-subtree t)
  12459. msg "Creating images for subtree...%s"))
  12460. (t
  12461. (if (setq at (org-inside-LaTeX-fragment-p))
  12462. (goto-char (max (point-min) (- (cdr at) 2)))
  12463. (org-back-to-heading))
  12464. (setq beg (point) end (progn (outline-next-heading) (point))
  12465. msg (if at "Creating image...%s"
  12466. "Creating images for entry...%s"))))
  12467. (message msg "")
  12468. (narrow-to-region beg end)
  12469. (goto-char beg)
  12470. (org-format-latex
  12471. (concat "ltxpng/" (file-name-sans-extension
  12472. (file-name-nondirectory
  12473. buffer-file-name)))
  12474. default-directory 'overlays msg at 'forbuffer)
  12475. (message msg "done. Use `C-c C-c' to remove images.")))))
  12476. (defvar org-latex-regexps
  12477. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  12478. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  12479. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  12480. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  12481. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  12482. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  12483. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  12484. "Regular expressions for matching embedded LaTeX.")
  12485. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  12486. "Replace LaTeX fragments with links to an image, and produce images."
  12487. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  12488. (let* ((prefixnodir (file-name-nondirectory prefix))
  12489. (absprefix (expand-file-name prefix dir))
  12490. (todir (file-name-directory absprefix))
  12491. (opt org-format-latex-options)
  12492. (matchers (plist-get opt :matchers))
  12493. (re-list org-latex-regexps)
  12494. (cnt 0) txt link beg end re e checkdir
  12495. m n block linkfile movefile ov)
  12496. ;; Check if there are old images files with this prefix, and remove them
  12497. (when (file-directory-p todir)
  12498. (mapc 'delete-file
  12499. (directory-files
  12500. todir 'full
  12501. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  12502. ;; Check the different regular expressions
  12503. (while (setq e (pop re-list))
  12504. (setq m (car e) re (nth 1 e) n (nth 2 e)
  12505. block (if (nth 3 e) "\n\n" ""))
  12506. (when (member m matchers)
  12507. (goto-char (point-min))
  12508. (while (re-search-forward re nil t)
  12509. (when (or (not at) (equal (cdr at) (match-beginning n)))
  12510. (setq txt (match-string n)
  12511. beg (match-beginning n) end (match-end n)
  12512. cnt (1+ cnt)
  12513. linkfile (format "%s_%04d.png" prefix cnt)
  12514. movefile (format "%s_%04d.png" absprefix cnt)
  12515. link (concat block "[[file:" linkfile "]]" block))
  12516. (if msg (message msg cnt))
  12517. (goto-char beg)
  12518. (unless checkdir ; make sure the directory exists
  12519. (setq checkdir t)
  12520. (or (file-directory-p todir) (make-directory todir)))
  12521. (org-create-formula-image
  12522. txt movefile opt forbuffer)
  12523. (if overlays
  12524. (progn
  12525. (setq ov (org-make-overlay beg end))
  12526. (if (featurep 'xemacs)
  12527. (progn
  12528. (org-overlay-put ov 'invisible t)
  12529. (org-overlay-put
  12530. ov 'end-glyph
  12531. (make-glyph (vector 'png :file movefile))))
  12532. (org-overlay-put
  12533. ov 'display
  12534. (list 'image :type 'png :file movefile :ascent 'center)))
  12535. (push ov org-latex-fragment-image-overlays)
  12536. (goto-char end))
  12537. (delete-region beg end)
  12538. (insert link))))))))
  12539. ;; This function borrows from Ganesh Swami's latex2png.el
  12540. (defun org-create-formula-image (string tofile options buffer)
  12541. (let* ((tmpdir (if (featurep 'xemacs)
  12542. (temp-directory)
  12543. temporary-file-directory))
  12544. (texfilebase (make-temp-name
  12545. (expand-file-name "orgtex" tmpdir)))
  12546. (texfile (concat texfilebase ".tex"))
  12547. (dvifile (concat texfilebase ".dvi"))
  12548. (pngfile (concat texfilebase ".png"))
  12549. (fnh (face-attribute 'default :height nil))
  12550. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  12551. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  12552. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  12553. "Black"))
  12554. (bg (or (plist-get options (if buffer :background :html-background))
  12555. "Transparent")))
  12556. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  12557. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  12558. (with-temp-file texfile
  12559. (insert org-format-latex-header
  12560. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  12561. (let ((dir default-directory))
  12562. (condition-case nil
  12563. (progn
  12564. (cd tmpdir)
  12565. (call-process "latex" nil nil nil texfile))
  12566. (error nil))
  12567. (cd dir))
  12568. (if (not (file-exists-p dvifile))
  12569. (progn (message "Failed to create dvi file from %s" texfile) nil)
  12570. (call-process "dvipng" nil nil nil
  12571. "-E" "-fg" fg "-bg" bg
  12572. "-D" dpi
  12573. ;;"-x" scale "-y" scale
  12574. "-T" "tight"
  12575. "-o" pngfile
  12576. dvifile)
  12577. (if (not (file-exists-p pngfile))
  12578. (progn (message "Failed to create png file from %s" texfile) nil)
  12579. ;; Use the requested file name and clean up
  12580. (copy-file pngfile tofile 'replace)
  12581. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  12582. (delete-file (concat texfilebase e)))
  12583. pngfile))))
  12584. (defun org-dvipng-color (attr)
  12585. "Return an rgb color specification for dvipng."
  12586. (apply 'format "rgb %s %s %s"
  12587. (mapcar 'org-normalize-color
  12588. (color-values (face-attribute 'default attr nil)))))
  12589. (defun org-normalize-color (value)
  12590. "Return string to be used as color value for an RGB component."
  12591. (format "%g" (/ value 65535.0)))
  12592. ;;;; Key bindings
  12593. ;; Make `C-c C-x' a prefix key
  12594. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  12595. ;; TAB key with modifiers
  12596. (org-defkey org-mode-map "\C-i" 'org-cycle)
  12597. (org-defkey org-mode-map [(tab)] 'org-cycle)
  12598. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  12599. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  12600. (org-defkey org-mode-map "\M-\t" 'org-complete)
  12601. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  12602. ;; The following line is necessary under Suse GNU/Linux
  12603. (unless (featurep 'xemacs)
  12604. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  12605. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  12606. (define-key org-mode-map [backtab] 'org-shifttab)
  12607. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  12608. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  12609. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  12610. ;; Cursor keys with modifiers
  12611. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  12612. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  12613. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  12614. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  12615. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  12616. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  12617. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  12618. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  12619. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  12620. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  12621. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  12622. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  12623. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  12624. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  12625. ;;; Extra keys for tty access.
  12626. ;; We only set them when really needed because otherwise the
  12627. ;; menus don't show the simple keys
  12628. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  12629. (not window-system))
  12630. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  12631. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  12632. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  12633. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  12634. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  12635. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  12636. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  12637. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  12638. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  12639. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  12640. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  12641. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  12642. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  12643. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  12644. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  12645. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  12646. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  12647. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  12648. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  12649. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  12650. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  12651. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  12652. ;; All the other keys
  12653. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  12654. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  12655. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  12656. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  12657. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  12658. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  12659. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  12660. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  12661. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  12662. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  12663. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  12664. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  12665. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  12666. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  12667. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  12668. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  12669. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  12670. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  12671. (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
  12672. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  12673. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  12674. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  12675. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  12676. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  12677. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  12678. (org-defkey org-mode-map "\C-c\C-z" 'org-time-stamp) ; Alternative binding
  12679. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  12680. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  12681. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  12682. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  12683. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  12684. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  12685. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  12686. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  12687. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  12688. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  12689. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  12690. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12691. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  12692. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  12693. (org-defkey org-mode-map "\C-c^" 'org-sort)
  12694. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  12695. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  12696. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  12697. (org-defkey org-mode-map "\C-m" 'org-return)
  12698. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  12699. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  12700. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  12701. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  12702. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  12703. (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
  12704. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  12705. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  12706. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  12707. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  12708. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  12709. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  12710. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  12711. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  12712. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  12713. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  12714. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  12715. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  12716. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  12717. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  12718. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  12719. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  12720. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  12721. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  12722. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  12723. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  12724. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  12725. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  12726. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  12727. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  12728. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  12729. (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
  12730. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  12731. (when (featurep 'xemacs)
  12732. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  12733. (defvar org-table-auto-blank-field) ; defined in org-table.el
  12734. (defun org-self-insert-command (N)
  12735. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  12736. If the cursor is in a table looking at whitespace, the whitespace is
  12737. overwritten, and the table is not marked as requiring realignment."
  12738. (interactive "p")
  12739. (if (and (org-table-p)
  12740. (progn
  12741. ;; check if we blank the field, and if that triggers align
  12742. (and (featurep 'org-table) org-table-auto-blank-field
  12743. (member last-command
  12744. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  12745. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  12746. ;; got extra space, this field does not determine column width
  12747. (let (org-table-may-need-update) (org-table-blank-field))
  12748. ;; no extra space, this field may determine column width
  12749. (org-table-blank-field)))
  12750. t)
  12751. (eq N 1)
  12752. (looking-at "[^|\n]* |"))
  12753. (let (org-table-may-need-update)
  12754. (goto-char (1- (match-end 0)))
  12755. (delete-backward-char 1)
  12756. (goto-char (match-beginning 0))
  12757. (self-insert-command N))
  12758. (setq org-table-may-need-update t)
  12759. (self-insert-command N)
  12760. (org-fix-tags-on-the-fly)))
  12761. (defun org-fix-tags-on-the-fly ()
  12762. (when (and (equal (char-after (point-at-bol)) ?*)
  12763. (org-on-heading-p))
  12764. (org-align-tags-here org-tags-column)))
  12765. (defun org-delete-backward-char (N)
  12766. "Like `delete-backward-char', insert whitespace at field end in tables.
  12767. When deleting backwards, in tables this function will insert whitespace in
  12768. front of the next \"|\" separator, to keep the table aligned. The table will
  12769. still be marked for re-alignment if the field did fill the entire column,
  12770. because, in this case the deletion might narrow the column."
  12771. (interactive "p")
  12772. (if (and (org-table-p)
  12773. (eq N 1)
  12774. (string-match "|" (buffer-substring (point-at-bol) (point)))
  12775. (looking-at ".*?|"))
  12776. (let ((pos (point))
  12777. (noalign (looking-at "[^|\n\r]* |"))
  12778. (c org-table-may-need-update))
  12779. (backward-delete-char N)
  12780. (skip-chars-forward "^|")
  12781. (insert " ")
  12782. (goto-char (1- pos))
  12783. ;; noalign: if there were two spaces at the end, this field
  12784. ;; does not determine the width of the column.
  12785. (if noalign (setq org-table-may-need-update c)))
  12786. (backward-delete-char N)
  12787. (org-fix-tags-on-the-fly)))
  12788. (defun org-delete-char (N)
  12789. "Like `delete-char', but insert whitespace at field end in tables.
  12790. When deleting characters, in tables this function will insert whitespace in
  12791. front of the next \"|\" separator, to keep the table aligned. The table will
  12792. still be marked for re-alignment if the field did fill the entire column,
  12793. because, in this case the deletion might narrow the column."
  12794. (interactive "p")
  12795. (if (and (org-table-p)
  12796. (not (bolp))
  12797. (not (= (char-after) ?|))
  12798. (eq N 1))
  12799. (if (looking-at ".*?|")
  12800. (let ((pos (point))
  12801. (noalign (looking-at "[^|\n\r]* |"))
  12802. (c org-table-may-need-update))
  12803. (replace-match (concat
  12804. (substring (match-string 0) 1 -1)
  12805. " |"))
  12806. (goto-char pos)
  12807. ;; noalign: if there were two spaces at the end, this field
  12808. ;; does not determine the width of the column.
  12809. (if noalign (setq org-table-may-need-update c)))
  12810. (delete-char N))
  12811. (delete-char N)
  12812. (org-fix-tags-on-the-fly)))
  12813. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  12814. (put 'org-self-insert-command 'delete-selection t)
  12815. (put 'orgtbl-self-insert-command 'delete-selection t)
  12816. (put 'org-delete-char 'delete-selection 'supersede)
  12817. (put 'org-delete-backward-char 'delete-selection 'supersede)
  12818. ;; Make `flyspell-mode' delay after some commands
  12819. (put 'org-self-insert-command 'flyspell-delayed t)
  12820. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  12821. (put 'org-delete-char 'flyspell-delayed t)
  12822. (put 'org-delete-backward-char 'flyspell-delayed t)
  12823. ;; Make pabbrev-mode expand after org-mode commands
  12824. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  12825. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  12826. ;; How to do this: Measure non-white length of current string
  12827. ;; If equal to column width, we should realign.
  12828. (defun org-remap (map &rest commands)
  12829. "In MAP, remap the functions given in COMMANDS.
  12830. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  12831. (let (new old)
  12832. (while commands
  12833. (setq old (pop commands) new (pop commands))
  12834. (if (fboundp 'command-remapping)
  12835. (org-defkey map (vector 'remap old) new)
  12836. (substitute-key-definition old new map global-map)))))
  12837. (when (eq org-enable-table-editor 'optimized)
  12838. ;; If the user wants maximum table support, we need to hijack
  12839. ;; some standard editing functions
  12840. (org-remap org-mode-map
  12841. 'self-insert-command 'org-self-insert-command
  12842. 'delete-char 'org-delete-char
  12843. 'delete-backward-char 'org-delete-backward-char)
  12844. (org-defkey org-mode-map "|" 'org-force-self-insert))
  12845. (defun org-shiftcursor-error ()
  12846. "Throw an error because Shift-Cursor command was applied in wrong context."
  12847. (error "This command is active in special context like tables, headlines or timestamps"))
  12848. (defun org-shifttab (&optional arg)
  12849. "Global visibility cycling or move to previous table field.
  12850. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  12851. on context.
  12852. See the individual commands for more information."
  12853. (interactive "P")
  12854. (cond
  12855. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  12856. (arg (message "Content view to level: ")
  12857. (org-content (prefix-numeric-value arg))
  12858. (setq org-cycle-global-status 'overview))
  12859. (t (call-interactively 'org-global-cycle))))
  12860. (defun org-shiftmetaleft ()
  12861. "Promote subtree or delete table column.
  12862. Calls `org-promote-subtree', `org-outdent-item',
  12863. or `org-table-delete-column', depending on context.
  12864. See the individual commands for more information."
  12865. (interactive)
  12866. (cond
  12867. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  12868. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  12869. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12870. (t (org-shiftcursor-error))))
  12871. (defun org-shiftmetaright ()
  12872. "Demote subtree or insert table column.
  12873. Calls `org-demote-subtree', `org-indent-item',
  12874. or `org-table-insert-column', depending on context.
  12875. See the individual commands for more information."
  12876. (interactive)
  12877. (cond
  12878. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  12879. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  12880. ((org-at-item-p) (call-interactively 'org-indent-item))
  12881. (t (org-shiftcursor-error))))
  12882. (defun org-shiftmetaup (&optional arg)
  12883. "Move subtree up or kill table row.
  12884. Calls `org-move-subtree-up' or `org-table-kill-row' or
  12885. `org-move-item-up' depending on context. See the individual commands
  12886. for more information."
  12887. (interactive "P")
  12888. (cond
  12889. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  12890. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12891. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12892. (t (org-shiftcursor-error))))
  12893. (defun org-shiftmetadown (&optional arg)
  12894. "Move subtree down or insert table row.
  12895. Calls `org-move-subtree-down' or `org-table-insert-row' or
  12896. `org-move-item-down', depending on context. See the individual
  12897. commands for more information."
  12898. (interactive "P")
  12899. (cond
  12900. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  12901. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12902. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12903. (t (org-shiftcursor-error))))
  12904. (defun org-metaleft (&optional arg)
  12905. "Promote heading or move table column to left.
  12906. Calls `org-do-promote' or `org-table-move-column', depending on context.
  12907. With no specific context, calls the Emacs default `backward-word'.
  12908. See the individual commands for more information."
  12909. (interactive "P")
  12910. (cond
  12911. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  12912. ((or (org-on-heading-p) (org-region-active-p))
  12913. (call-interactively 'org-do-promote))
  12914. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12915. (t (call-interactively 'backward-word))))
  12916. (defun org-metaright (&optional arg)
  12917. "Demote subtree or move table column to right.
  12918. Calls `org-do-demote' or `org-table-move-column', depending on context.
  12919. With no specific context, calls the Emacs default `forward-word'.
  12920. See the individual commands for more information."
  12921. (interactive "P")
  12922. (cond
  12923. ((org-at-table-p) (call-interactively 'org-table-move-column))
  12924. ((or (org-on-heading-p) (org-region-active-p))
  12925. (call-interactively 'org-do-demote))
  12926. ((org-at-item-p) (call-interactively 'org-indent-item))
  12927. (t (call-interactively 'forward-word))))
  12928. (defun org-metaup (&optional arg)
  12929. "Move subtree up or move table row up.
  12930. Calls `org-move-subtree-up' or `org-table-move-row' or
  12931. `org-move-item-up', depending on context. See the individual commands
  12932. for more information."
  12933. (interactive "P")
  12934. (cond
  12935. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  12936. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12937. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12938. (t (transpose-lines 1) (beginning-of-line -1))))
  12939. (defun org-metadown (&optional arg)
  12940. "Move subtree down or move table row down.
  12941. Calls `org-move-subtree-down' or `org-table-move-row' or
  12942. `org-move-item-down', depending on context. See the individual
  12943. commands for more information."
  12944. (interactive "P")
  12945. (cond
  12946. ((org-at-table-p) (call-interactively 'org-table-move-row))
  12947. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12948. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12949. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  12950. (defun org-shiftup (&optional arg)
  12951. "Increase item in timestamp or increase priority of current headline.
  12952. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  12953. depending on context. See the individual commands for more information."
  12954. (interactive "P")
  12955. (cond
  12956. ((org-at-timestamp-p t)
  12957. (call-interactively (if org-edit-timestamp-down-means-later
  12958. 'org-timestamp-down 'org-timestamp-up)))
  12959. ((org-on-heading-p) (call-interactively 'org-priority-up))
  12960. ((org-at-item-p) (call-interactively 'org-previous-item))
  12961. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  12962. (defun org-shiftdown (&optional arg)
  12963. "Decrease item in timestamp or decrease priority of current headline.
  12964. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  12965. depending on context. See the individual commands for more information."
  12966. (interactive "P")
  12967. (cond
  12968. ((org-at-timestamp-p t)
  12969. (call-interactively (if org-edit-timestamp-down-means-later
  12970. 'org-timestamp-up 'org-timestamp-down)))
  12971. ((org-on-heading-p) (call-interactively 'org-priority-down))
  12972. (t (call-interactively 'org-next-item))))
  12973. (defun org-shiftright ()
  12974. "Next TODO keyword or timestamp one day later, depending on context."
  12975. (interactive)
  12976. (cond
  12977. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  12978. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  12979. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  12980. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  12981. (t (org-shiftcursor-error))))
  12982. (defun org-shiftleft ()
  12983. "Previous TODO keyword or timestamp one day earlier, depending on context."
  12984. (interactive)
  12985. (cond
  12986. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  12987. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  12988. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  12989. ((org-at-property-p)
  12990. (call-interactively 'org-property-previous-allowed-value))
  12991. (t (org-shiftcursor-error))))
  12992. (defun org-shiftcontrolright ()
  12993. "Switch to next TODO set."
  12994. (interactive)
  12995. (cond
  12996. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  12997. (t (org-shiftcursor-error))))
  12998. (defun org-shiftcontrolleft ()
  12999. "Switch to previous TODO set."
  13000. (interactive)
  13001. (cond
  13002. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  13003. (t (org-shiftcursor-error))))
  13004. (defun org-ctrl-c-ret ()
  13005. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  13006. (interactive)
  13007. (cond
  13008. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  13009. (t (call-interactively 'org-insert-heading))))
  13010. (defun org-copy-special ()
  13011. "Copy region in table or copy current subtree.
  13012. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  13013. See the individual commands for more information."
  13014. (interactive)
  13015. (call-interactively
  13016. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  13017. (defun org-cut-special ()
  13018. "Cut region in table or cut current subtree.
  13019. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  13020. See the individual commands for more information."
  13021. (interactive)
  13022. (call-interactively
  13023. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  13024. (defun org-paste-special (arg)
  13025. "Paste rectangular region into table, or past subtree relative to level.
  13026. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  13027. See the individual commands for more information."
  13028. (interactive "P")
  13029. (if (org-at-table-p)
  13030. (org-table-paste-rectangle)
  13031. (org-paste-subtree arg)))
  13032. (defun org-ctrl-c-ctrl-c (&optional arg)
  13033. "Set tags in headline, or update according to changed information at point.
  13034. This command does many different things, depending on context:
  13035. - If the cursor is in a headline, prompt for tags and insert them
  13036. into the current line, aligned to `org-tags-column'. When called
  13037. with prefix arg, realign all tags in the current buffer.
  13038. - If the cursor is in one of the special #+KEYWORD lines, this
  13039. triggers scanning the buffer for these lines and updating the
  13040. information.
  13041. - If the cursor is inside a table, realign the table. This command
  13042. works even if the automatic table editor has been turned off.
  13043. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  13044. the entire table.
  13045. - If the cursor is a the beginning of a dynamic block, update it.
  13046. - If the cursor is inside a table created by the table.el package,
  13047. activate that table.
  13048. - If the current buffer is a remember buffer, close note and file it.
  13049. with a prefix argument, file it without further interaction to the default
  13050. location.
  13051. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  13052. links in this buffer.
  13053. - If the cursor is on a numbered item in a plain list, renumber the
  13054. ordered list.
  13055. - If the cursor is on a checkbox, toggle it."
  13056. (interactive "P")
  13057. (let ((org-enable-table-editor t))
  13058. (cond
  13059. ((or org-clock-overlays
  13060. org-occur-highlights
  13061. org-latex-fragment-image-overlays)
  13062. (org-remove-clock-overlays)
  13063. (org-remove-occur-highlights)
  13064. (org-remove-latex-fragment-image-overlays)
  13065. (message "Temporary highlights/overlays removed from current buffer"))
  13066. ((and (local-variable-p 'org-finish-function (current-buffer))
  13067. (fboundp org-finish-function))
  13068. (funcall org-finish-function))
  13069. ((org-at-property-p)
  13070. (call-interactively 'org-property-action))
  13071. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  13072. ((org-on-heading-p) (call-interactively 'org-set-tags))
  13073. ((org-at-table.el-p)
  13074. (require 'table)
  13075. (beginning-of-line 1)
  13076. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  13077. (call-interactively 'table-recognize-table))
  13078. ((org-at-table-p)
  13079. (org-table-maybe-eval-formula)
  13080. (if arg
  13081. (call-interactively 'org-table-recalculate)
  13082. (org-table-maybe-recalculate-line))
  13083. (call-interactively 'org-table-align))
  13084. ((org-at-item-checkbox-p)
  13085. (call-interactively 'org-toggle-checkbox))
  13086. ((org-at-item-p)
  13087. (call-interactively 'org-maybe-renumber-ordered-list))
  13088. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  13089. ;; Dynamic block
  13090. (beginning-of-line 1)
  13091. (org-update-dblock))
  13092. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  13093. (cond
  13094. ((equal (match-string 1) "TBLFM")
  13095. ;; Recalculate the table before this line
  13096. (save-excursion
  13097. (beginning-of-line 1)
  13098. (skip-chars-backward " \r\n\t")
  13099. (if (org-at-table-p)
  13100. (org-call-with-arg 'org-table-recalculate t))))
  13101. (t
  13102. (call-interactively 'org-mode-restart))))
  13103. (t (error "C-c C-c can do nothing useful at this location.")))))
  13104. (defun org-mode-restart ()
  13105. "Restart Org-mode, to scan again for special lines.
  13106. Also updates the keyword regular expressions."
  13107. (interactive)
  13108. (let ((org-inhibit-startup t)) (org-mode))
  13109. (message "Org-mode restarted to refresh keyword and special line setup"))
  13110. (defun org-kill-note-or-show-branches ()
  13111. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  13112. (interactive)
  13113. (if (not org-finish-function)
  13114. (call-interactively 'show-branches)
  13115. (let ((org-note-abort t))
  13116. (funcall org-finish-function))))
  13117. (defun org-return (&optional indent)
  13118. "Goto next table row or insert a newline.
  13119. Calls `org-table-next-row' or `newline', depending on context.
  13120. See the individual commands for more information."
  13121. (interactive)
  13122. (cond
  13123. ((bobp) (if indent (newline-and-indent) (newline)))
  13124. ((and (org-at-heading-p)
  13125. (looking-at
  13126. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  13127. (org-show-entry)
  13128. (end-of-line 1)
  13129. (newline))
  13130. ((org-at-table-p)
  13131. (org-table-justify-field-maybe)
  13132. (call-interactively 'org-table-next-row))
  13133. (t (if indent (newline-and-indent) (newline)))))
  13134. (defun org-return-indent ()
  13135. "Goto next table row or insert a newline and indent.
  13136. Calls `org-table-next-row' or `newline-and-indent', depending on
  13137. context. See the individual commands for more information."
  13138. (interactive)
  13139. (org-return t))
  13140. (defun org-ctrl-c-star ()
  13141. "Compute table, or change heading status of lines.
  13142. Calls `org-table-recalculate' or `org-toggle-region-headlines',
  13143. depending on context. This will also turn a plain list item or a normal
  13144. line into a subheading."
  13145. (interactive)
  13146. (cond
  13147. ((org-at-table-p)
  13148. (call-interactively 'org-table-recalculate))
  13149. ((org-region-active-p)
  13150. ;; Convert all lines in region to list items
  13151. (call-interactively 'org-toggle-region-headings))
  13152. ((org-on-heading-p)
  13153. (org-toggle-region-headings (point-at-bol)
  13154. (min (1+ (point-at-eol)) (point-max))))
  13155. ((org-at-item-p)
  13156. ;; Convert to heading
  13157. (let ((level (save-match-data
  13158. (save-excursion
  13159. (condition-case nil
  13160. (progn
  13161. (org-back-to-heading t)
  13162. (funcall outline-level))
  13163. (error 0))))))
  13164. (replace-match
  13165. (concat (make-string (org-get-valid-level level 1) ?*) " ") t t)))
  13166. (t (org-toggle-region-headings (point-at-bol)
  13167. (min (1+ (point-at-eol)) (point-max))))))
  13168. (defun org-ctrl-c-minus ()
  13169. "Insert separator line in table or modify bullet status of line.
  13170. Also turns a plain line or a region of lines into list items.
  13171. Calls `org-table-insert-hline', `org-toggle-region-items', or
  13172. `org-cycle-list-bullet', depending on context."
  13173. (interactive)
  13174. (cond
  13175. ((org-at-table-p)
  13176. (call-interactively 'org-table-insert-hline))
  13177. ((org-on-heading-p)
  13178. ;; Convert to item
  13179. (save-excursion
  13180. (beginning-of-line 1)
  13181. (if (looking-at "\\*+ ")
  13182. (replace-match (concat (make-string (- (match-end 0) (point) 1) ?\ ) "- ")))))
  13183. ((org-region-active-p)
  13184. ;; Convert all lines in region to list items
  13185. (call-interactively 'org-toggle-region-items))
  13186. ((org-in-item-p)
  13187. (call-interactively 'org-cycle-list-bullet))
  13188. (t (org-toggle-region-items (point-at-bol)
  13189. (min (1+ (point-at-eol)) (point-max))))))
  13190. (defun org-toggle-region-items (beg end)
  13191. "Convert all lines in region to list items.
  13192. If the first line is already an item, convert all list items in the region
  13193. to normal lines."
  13194. (interactive "r")
  13195. (let (l2 l)
  13196. (save-excursion
  13197. (goto-char end)
  13198. (setq l2 (org-current-line))
  13199. (goto-char beg)
  13200. (beginning-of-line 1)
  13201. (setq l (1- (org-current-line)))
  13202. (if (org-at-item-p)
  13203. ;; We already have items, de-itemize
  13204. (while (< (setq l (1+ l)) l2)
  13205. (when (org-at-item-p)
  13206. (goto-char (match-beginning 2))
  13207. (delete-region (match-beginning 2) (match-end 2))
  13208. (and (looking-at "[ \t]+") (replace-match "")))
  13209. (beginning-of-line 2))
  13210. (while (< (setq l (1+ l)) l2)
  13211. (unless (org-at-item-p)
  13212. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13213. (replace-match "\\1- \\2")))
  13214. (beginning-of-line 2))))))
  13215. (defun org-toggle-region-headings (beg end)
  13216. "Convert all lines in region to list items.
  13217. If the first line is already an item, convert all list items in the region
  13218. to normal lines."
  13219. (interactive "r")
  13220. (let (l2 l)
  13221. (save-excursion
  13222. (goto-char end)
  13223. (setq l2 (org-current-line))
  13224. (goto-char beg)
  13225. (beginning-of-line 1)
  13226. (setq l (1- (org-current-line)))
  13227. (if (org-on-heading-p)
  13228. ;; We already have headlines, de-star them
  13229. (while (< (setq l (1+ l)) l2)
  13230. (when (org-on-heading-p t)
  13231. (and (looking-at outline-regexp) (replace-match "")))
  13232. (beginning-of-line 2))
  13233. (let* ((stars (save-excursion
  13234. (re-search-backward org-complex-heading-regexp nil t)
  13235. (or (match-string 1) "*")))
  13236. (add-stars (if org-odd-levels-only "**" "*"))
  13237. (rpl (concat stars add-stars " \\2")))
  13238. (while (< (setq l (1+ l)) l2)
  13239. (unless (org-on-heading-p)
  13240. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13241. (replace-match rpl)))
  13242. (beginning-of-line 2)))))))
  13243. (defun org-meta-return (&optional arg)
  13244. "Insert a new heading or wrap a region in a table.
  13245. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  13246. See the individual commands for more information."
  13247. (interactive "P")
  13248. (cond
  13249. ((org-at-table-p)
  13250. (call-interactively 'org-table-wrap-region))
  13251. (t (call-interactively 'org-insert-heading))))
  13252. ;;; Menu entries
  13253. ;; Define the Org-mode menus
  13254. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  13255. '("Tbl"
  13256. ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
  13257. ["Next Field" org-cycle (org-at-table-p)]
  13258. ["Previous Field" org-shifttab (org-at-table-p)]
  13259. ["Next Row" org-return (org-at-table-p)]
  13260. "--"
  13261. ["Blank Field" org-table-blank-field (org-at-table-p)]
  13262. ["Edit Field" org-table-edit-field (org-at-table-p)]
  13263. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  13264. "--"
  13265. ("Column"
  13266. ["Move Column Left" org-metaleft (org-at-table-p)]
  13267. ["Move Column Right" org-metaright (org-at-table-p)]
  13268. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  13269. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  13270. ("Row"
  13271. ["Move Row Up" org-metaup (org-at-table-p)]
  13272. ["Move Row Down" org-metadown (org-at-table-p)]
  13273. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  13274. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  13275. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  13276. "--"
  13277. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  13278. ("Rectangle"
  13279. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  13280. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  13281. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  13282. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  13283. "--"
  13284. ("Calculate"
  13285. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  13286. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  13287. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  13288. "--"
  13289. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  13290. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  13291. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  13292. "--"
  13293. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  13294. "--"
  13295. ["Sum Column/Rectangle" org-table-sum
  13296. (or (org-at-table-p) (org-region-active-p))]
  13297. ["Which Column?" org-table-current-column (org-at-table-p)])
  13298. ["Debug Formulas"
  13299. org-table-toggle-formula-debugger
  13300. :style toggle :selected org-table-formula-debug]
  13301. ["Show Col/Row Numbers"
  13302. org-table-toggle-coordinate-overlays
  13303. :style toggle :selected org-table-overlay-coordinates]
  13304. "--"
  13305. ["Create" org-table-create (and (not (org-at-table-p))
  13306. org-enable-table-editor)]
  13307. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  13308. ["Import from File" org-table-import (not (org-at-table-p))]
  13309. ["Export to File" org-table-export (org-at-table-p)]
  13310. "--"
  13311. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  13312. (easy-menu-define org-org-menu org-mode-map "Org menu"
  13313. '("Org"
  13314. ("Show/Hide"
  13315. ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
  13316. ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
  13317. ["Sparse Tree" org-occur t]
  13318. ["Reveal Context" org-reveal t]
  13319. ["Show All" show-all t]
  13320. "--"
  13321. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  13322. "--"
  13323. ["New Heading" org-insert-heading t]
  13324. ("Navigate Headings"
  13325. ["Up" outline-up-heading t]
  13326. ["Next" outline-next-visible-heading t]
  13327. ["Previous" outline-previous-visible-heading t]
  13328. ["Next Same Level" outline-forward-same-level t]
  13329. ["Previous Same Level" outline-backward-same-level t]
  13330. "--"
  13331. ["Jump" org-goto t])
  13332. ("Edit Structure"
  13333. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  13334. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  13335. "--"
  13336. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  13337. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  13338. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  13339. "--"
  13340. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  13341. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  13342. ["Demote Heading" org-metaright (not (org-at-table-p))]
  13343. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  13344. "--"
  13345. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  13346. "--"
  13347. ["Convert to odd levels" org-convert-to-odd-levels t]
  13348. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  13349. ("Editing"
  13350. ["Emphasis..." org-emphasize t])
  13351. ("Archive"
  13352. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  13353. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  13354. ; :active t :keys "C-u C-c C-x C-a"]
  13355. ["Sparse trees open ARCHIVE trees"
  13356. (setq org-sparse-tree-open-archived-trees
  13357. (not org-sparse-tree-open-archived-trees))
  13358. :style toggle :selected org-sparse-tree-open-archived-trees]
  13359. ["Cycling opens ARCHIVE trees"
  13360. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  13361. :style toggle :selected org-cycle-open-archived-trees]
  13362. ["Agenda includes ARCHIVE trees"
  13363. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  13364. :style toggle :selected (not org-agenda-skip-archived-trees)]
  13365. "--"
  13366. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  13367. ; ["Check and Move Children" (org-archive-subtree '(4))
  13368. ; :active t :keys "C-u C-c C-x C-s"]
  13369. )
  13370. "--"
  13371. ("TODO Lists"
  13372. ["TODO/DONE/-" org-todo t]
  13373. ("Select keyword"
  13374. ["Next keyword" org-shiftright (org-on-heading-p)]
  13375. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  13376. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  13377. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  13378. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  13379. ["Show TODO Tree" org-show-todo-tree t]
  13380. ["Global TODO list" org-todo-list t]
  13381. "--"
  13382. ["Set Priority" org-priority t]
  13383. ["Priority Up" org-shiftup t]
  13384. ["Priority Down" org-shiftdown t])
  13385. ("TAGS and Properties"
  13386. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  13387. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  13388. "--"
  13389. ["Set property" 'org-set-property t]
  13390. ["Column view of properties" org-columns t]
  13391. ["Insert Column View DBlock" org-insert-columns-dblock t])
  13392. ("Dates and Scheduling"
  13393. ["Timestamp" org-time-stamp t]
  13394. ["Timestamp (inactive)" org-time-stamp-inactive t]
  13395. ("Change Date"
  13396. ["1 Day Later" org-shiftright t]
  13397. ["1 Day Earlier" org-shiftleft t]
  13398. ["1 ... Later" org-shiftup t]
  13399. ["1 ... Earlier" org-shiftdown t])
  13400. ["Compute Time Range" org-evaluate-time-range t]
  13401. ["Schedule Item" org-schedule t]
  13402. ["Deadline" org-deadline t]
  13403. "--"
  13404. ["Custom time format" org-toggle-time-stamp-overlays
  13405. :style radio :selected org-display-custom-times]
  13406. "--"
  13407. ["Goto Calendar" org-goto-calendar t]
  13408. ["Date from Calendar" org-date-from-calendar t])
  13409. ("Logging work"
  13410. ["Clock in" org-clock-in t]
  13411. ["Clock out" org-clock-out t]
  13412. ["Clock cancel" org-clock-cancel t]
  13413. ["Goto running clock" org-clock-goto t]
  13414. ["Display times" org-clock-display t]
  13415. ["Create clock table" org-clock-report t]
  13416. "--"
  13417. ["Record DONE time"
  13418. (progn (setq org-log-done (not org-log-done))
  13419. (message "Switching to %s will %s record a timestamp"
  13420. (car org-done-keywords)
  13421. (if org-log-done "automatically" "not")))
  13422. :style toggle :selected org-log-done])
  13423. "--"
  13424. ["Agenda Command..." org-agenda t]
  13425. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  13426. ("File List for Agenda")
  13427. ("Special views current file"
  13428. ["TODO Tree" org-show-todo-tree t]
  13429. ["Check Deadlines" org-check-deadlines t]
  13430. ["Timeline" org-timeline t]
  13431. ["Tags Tree" org-tags-sparse-tree t])
  13432. "--"
  13433. ("Hyperlinks"
  13434. ["Store Link (Global)" org-store-link t]
  13435. ["Insert Link" org-insert-link t]
  13436. ["Follow Link" org-open-at-point t]
  13437. "--"
  13438. ["Next link" org-next-link t]
  13439. ["Previous link" org-previous-link t]
  13440. "--"
  13441. ["Descriptive Links"
  13442. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  13443. :style radio :selected (member '(org-link) buffer-invisibility-spec)]
  13444. ["Literal Links"
  13445. (progn
  13446. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  13447. :style radio :selected (not (member '(org-link) buffer-invisibility-spec))])
  13448. "--"
  13449. ["Export/Publish..." org-export t]
  13450. ("LaTeX"
  13451. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  13452. :selected org-cdlatex-mode]
  13453. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  13454. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  13455. ["Modify math symbol" org-cdlatex-math-modify
  13456. (org-inside-LaTeX-fragment-p)]
  13457. ["Export LaTeX fragments as images"
  13458. (setq org-export-with-LaTeX-fragments (not org-export-with-LaTeX-fragments))
  13459. :style toggle :selected org-export-with-LaTeX-fragments])
  13460. "--"
  13461. ("Documentation"
  13462. ["Show Version" org-version t]
  13463. ["Info Documentation" org-info t])
  13464. ("Customize"
  13465. ["Browse Org Group" org-customize t]
  13466. "--"
  13467. ["Expand This Menu" org-create-customize-menu
  13468. (fboundp 'customize-menu-create)])
  13469. "--"
  13470. ["Refresh setup" org-mode-restart t]
  13471. ))
  13472. (defun org-info (&optional node)
  13473. "Read documentation for Org-mode in the info system.
  13474. With optional NODE, go directly to that node."
  13475. (interactive)
  13476. (info (format "(org)%s" (or node ""))))
  13477. (defun org-install-agenda-files-menu ()
  13478. (let ((bl (buffer-list)))
  13479. (save-excursion
  13480. (while bl
  13481. (set-buffer (pop bl))
  13482. (if (org-mode-p) (setq bl nil)))
  13483. (when (org-mode-p)
  13484. (easy-menu-change
  13485. '("Org") "File List for Agenda"
  13486. (append
  13487. (list
  13488. ["Edit File List" (org-edit-agenda-file-list) t]
  13489. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  13490. ["Remove Current File from List" org-remove-file t]
  13491. ["Cycle through agenda files" org-cycle-agenda-files t]
  13492. ["Occur in all agenda files" org-occur-in-agenda-files t]
  13493. "--")
  13494. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  13495. ;;;; Documentation
  13496. (defun org-customize ()
  13497. "Call the customize function with org as argument."
  13498. (interactive)
  13499. (customize-browse 'org))
  13500. (defun org-create-customize-menu ()
  13501. "Create a full customization menu for Org-mode, insert it into the menu."
  13502. (interactive)
  13503. (if (fboundp 'customize-menu-create)
  13504. (progn
  13505. (easy-menu-change
  13506. '("Org") "Customize"
  13507. `(["Browse Org group" org-customize t]
  13508. "--"
  13509. ,(customize-menu-create 'org)
  13510. ["Set" Custom-set t]
  13511. ["Save" Custom-save t]
  13512. ["Reset to Current" Custom-reset-current t]
  13513. ["Reset to Saved" Custom-reset-saved t]
  13514. ["Reset to Standard Settings" Custom-reset-standard t]))
  13515. (message "\"Org\"-menu now contains full customization menu"))
  13516. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  13517. ;;;; Miscellaneous stuff
  13518. ;;; Generally useful functions
  13519. (defun org-force-self-insert (N)
  13520. "Needed to enforce self-insert under remapping."
  13521. (interactive "p")
  13522. (self-insert-command N))
  13523. (defun org-string-width (s)
  13524. "Compute width of string, ignoring invisible characters.
  13525. This ignores character with invisibility property `org-link', and also
  13526. characters with property `org-cwidth', because these will become invisible
  13527. upon the next fontification round."
  13528. (let (b l)
  13529. (when (or (eq t buffer-invisibility-spec)
  13530. (assq 'org-link buffer-invisibility-spec))
  13531. (while (setq b (text-property-any 0 (length s)
  13532. 'invisible 'org-link s))
  13533. (setq s (concat (substring s 0 b)
  13534. (substring s (or (next-single-property-change
  13535. b 'invisible s) (length s)))))))
  13536. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  13537. (setq s (concat (substring s 0 b)
  13538. (substring s (or (next-single-property-change
  13539. b 'org-cwidth s) (length s))))))
  13540. (setq l (string-width s) b -1)
  13541. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  13542. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  13543. l))
  13544. (defun org-trim (s)
  13545. "Remove whitespace at beginning and end of string."
  13546. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  13547. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  13548. s)
  13549. (defun org-wrap (string &optional width lines)
  13550. "Wrap string to either a number of lines, or a width in characters.
  13551. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  13552. that costs. If there is a word longer than WIDTH, the text is actually
  13553. wrapped to the length of that word.
  13554. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  13555. many lines, whatever width that takes.
  13556. The return value is a list of lines, without newlines at the end."
  13557. (let* ((words (org-split-string string "[ \t\n]+"))
  13558. (maxword (apply 'max (mapcar 'org-string-width words)))
  13559. w ll)
  13560. (cond (width
  13561. (org-do-wrap words (max maxword width)))
  13562. (lines
  13563. (setq w maxword)
  13564. (setq ll (org-do-wrap words maxword))
  13565. (if (<= (length ll) lines)
  13566. ll
  13567. (setq ll words)
  13568. (while (> (length ll) lines)
  13569. (setq w (1+ w))
  13570. (setq ll (org-do-wrap words w)))
  13571. ll))
  13572. (t (error "Cannot wrap this")))))
  13573. (defun org-do-wrap (words width)
  13574. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  13575. (let (lines line)
  13576. (while words
  13577. (setq line (pop words))
  13578. (while (and words (< (+ (length line) (length (car words))) width))
  13579. (setq line (concat line " " (pop words))))
  13580. (setq lines (push line lines)))
  13581. (nreverse lines)))
  13582. (defun org-split-string (string &optional separators)
  13583. "Splits STRING into substrings at SEPARATORS.
  13584. No empty strings are returned if there are matches at the beginning
  13585. and end of string."
  13586. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  13587. (start 0)
  13588. notfirst
  13589. (list nil))
  13590. (while (and (string-match rexp string
  13591. (if (and notfirst
  13592. (= start (match-beginning 0))
  13593. (< start (length string)))
  13594. (1+ start) start))
  13595. (< (match-beginning 0) (length string)))
  13596. (setq notfirst t)
  13597. (or (eq (match-beginning 0) 0)
  13598. (and (eq (match-beginning 0) (match-end 0))
  13599. (eq (match-beginning 0) start))
  13600. (setq list
  13601. (cons (substring string start (match-beginning 0))
  13602. list)))
  13603. (setq start (match-end 0)))
  13604. (or (eq start (length string))
  13605. (setq list
  13606. (cons (substring string start)
  13607. list)))
  13608. (nreverse list)))
  13609. (defun org-context ()
  13610. "Return a list of contexts of the current cursor position.
  13611. If several contexts apply, all are returned.
  13612. Each context entry is a list with a symbol naming the context, and
  13613. two positions indicating start and end of the context. Possible
  13614. contexts are:
  13615. :headline anywhere in a headline
  13616. :headline-stars on the leading stars in a headline
  13617. :todo-keyword on a TODO keyword (including DONE) in a headline
  13618. :tags on the TAGS in a headline
  13619. :priority on the priority cookie in a headline
  13620. :item on the first line of a plain list item
  13621. :item-bullet on the bullet/number of a plain list item
  13622. :checkbox on the checkbox in a plain list item
  13623. :table in an org-mode table
  13624. :table-special on a special filed in a table
  13625. :table-table in a table.el table
  13626. :link on a hyperlink
  13627. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  13628. :target on a <<target>>
  13629. :radio-target on a <<<radio-target>>>
  13630. :latex-fragment on a LaTeX fragment
  13631. :latex-preview on a LaTeX fragment with overlayed preview image
  13632. This function expects the position to be visible because it uses font-lock
  13633. faces as a help to recognize the following contexts: :table-special, :link,
  13634. and :keyword."
  13635. (let* ((f (get-text-property (point) 'face))
  13636. (faces (if (listp f) f (list f)))
  13637. (p (point)) clist o)
  13638. ;; First the large context
  13639. (cond
  13640. ((org-on-heading-p t)
  13641. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  13642. (when (progn
  13643. (beginning-of-line 1)
  13644. (looking-at org-todo-line-tags-regexp))
  13645. (push (org-point-in-group p 1 :headline-stars) clist)
  13646. (push (org-point-in-group p 2 :todo-keyword) clist)
  13647. (push (org-point-in-group p 4 :tags) clist))
  13648. (goto-char p)
  13649. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  13650. (if (looking-at "\\[#[A-Z0-9]\\]")
  13651. (push (org-point-in-group p 0 :priority) clist)))
  13652. ((org-at-item-p)
  13653. (push (org-point-in-group p 2 :item-bullet) clist)
  13654. (push (list :item (point-at-bol)
  13655. (save-excursion (org-end-of-item) (point)))
  13656. clist)
  13657. (and (org-at-item-checkbox-p)
  13658. (push (org-point-in-group p 0 :checkbox) clist)))
  13659. ((org-at-table-p)
  13660. (push (list :table (org-table-begin) (org-table-end)) clist)
  13661. (if (memq 'org-formula faces)
  13662. (push (list :table-special
  13663. (previous-single-property-change p 'face)
  13664. (next-single-property-change p 'face)) clist)))
  13665. ((org-at-table-p 'any)
  13666. (push (list :table-table) clist)))
  13667. (goto-char p)
  13668. ;; Now the small context
  13669. (cond
  13670. ((org-at-timestamp-p)
  13671. (push (org-point-in-group p 0 :timestamp) clist))
  13672. ((memq 'org-link faces)
  13673. (push (list :link
  13674. (previous-single-property-change p 'face)
  13675. (next-single-property-change p 'face)) clist))
  13676. ((memq 'org-special-keyword faces)
  13677. (push (list :keyword
  13678. (previous-single-property-change p 'face)
  13679. (next-single-property-change p 'face)) clist))
  13680. ((org-on-target-p)
  13681. (push (org-point-in-group p 0 :target) clist)
  13682. (goto-char (1- (match-beginning 0)))
  13683. (if (looking-at org-radio-target-regexp)
  13684. (push (org-point-in-group p 0 :radio-target) clist))
  13685. (goto-char p))
  13686. ((setq o (car (delq nil
  13687. (mapcar
  13688. (lambda (x)
  13689. (if (memq x org-latex-fragment-image-overlays) x))
  13690. (org-overlays-at (point))))))
  13691. (push (list :latex-fragment
  13692. (org-overlay-start o) (org-overlay-end o)) clist)
  13693. (push (list :latex-preview
  13694. (org-overlay-start o) (org-overlay-end o)) clist))
  13695. ((org-inside-LaTeX-fragment-p)
  13696. ;; FIXME: positions wrong.
  13697. (push (list :latex-fragment (point) (point)) clist)))
  13698. (setq clist (nreverse (delq nil clist)))
  13699. clist))
  13700. ;; FIXME: Compare with at-regexp-p Do we need both?
  13701. (defun org-in-regexp (re &optional nlines visually)
  13702. "Check if point is inside a match of regexp.
  13703. Normally only the current line is checked, but you can include NLINES extra
  13704. lines both before and after point into the search.
  13705. If VISUALLY is set, require that the cursor is not after the match but
  13706. really on, so that the block visually is on the match."
  13707. (catch 'exit
  13708. (let ((pos (point))
  13709. (eol (point-at-eol (+ 1 (or nlines 0))))
  13710. (inc (if visually 1 0)))
  13711. (save-excursion
  13712. (beginning-of-line (- 1 (or nlines 0)))
  13713. (while (re-search-forward re eol t)
  13714. (if (and (<= (match-beginning 0) pos)
  13715. (>= (+ inc (match-end 0)) pos))
  13716. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  13717. (defun org-at-regexp-p (regexp)
  13718. "Is point inside a match of REGEXP in the current line?"
  13719. (catch 'exit
  13720. (save-excursion
  13721. (let ((pos (point)) (end (point-at-eol)))
  13722. (beginning-of-line 1)
  13723. (while (re-search-forward regexp end t)
  13724. (if (and (<= (match-beginning 0) pos)
  13725. (>= (match-end 0) pos))
  13726. (throw 'exit t)))
  13727. nil))))
  13728. (defun org-occur-in-agenda-files (regexp &optional nlines)
  13729. "Call `multi-occur' with buffers for all agenda files."
  13730. (interactive "sOrg-files matching: \np")
  13731. (let* ((files (org-agenda-files))
  13732. (tnames (mapcar 'file-truename files))
  13733. (extra org-agenda-text-search-extra-files)
  13734. f)
  13735. (while (setq f (pop extra))
  13736. (unless (member (file-truename f) tnames)
  13737. (add-to-list 'files f 'append)
  13738. (add-to-list 'tnames (file-truename f) 'append)))
  13739. (multi-occur
  13740. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  13741. regexp)))
  13742. (if (boundp 'occur-mode-find-occurrence-hook)
  13743. ;; Emacs 23
  13744. (add-hook 'occur-mode-find-occurrence-hook
  13745. (lambda ()
  13746. (when (org-mode-p)
  13747. (org-reveal))))
  13748. ;; Emacs 22
  13749. (defadvice occur-mode-goto-occurrence
  13750. (after org-occur-reveal activate)
  13751. (and (org-mode-p) (org-reveal)))
  13752. (defadvice occur-mode-goto-occurrence-other-window
  13753. (after org-occur-reveal activate)
  13754. (and (org-mode-p) (org-reveal)))
  13755. (defadvice occur-mode-display-occurrence
  13756. (after org-occur-reveal activate)
  13757. (when (org-mode-p)
  13758. (let ((pos (occur-mode-find-occurrence)))
  13759. (with-current-buffer (marker-buffer pos)
  13760. (save-excursion
  13761. (goto-char pos)
  13762. (org-reveal)))))))
  13763. (defun org-uniquify (list)
  13764. "Remove duplicate elements from LIST."
  13765. (let (res)
  13766. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  13767. res))
  13768. (defun org-delete-all (elts list)
  13769. "Remove all elements in ELTS from LIST."
  13770. (while elts
  13771. (setq list (delete (pop elts) list)))
  13772. list)
  13773. (defun org-back-over-empty-lines ()
  13774. "Move backwards over witespace, to the beginning of the first empty line.
  13775. Returns the number of empty lines passed."
  13776. (let ((pos (point)))
  13777. (skip-chars-backward " \t\n\r")
  13778. (beginning-of-line 2)
  13779. (goto-char (min (point) pos))
  13780. (count-lines (point) pos)))
  13781. (defun org-skip-whitespace ()
  13782. (skip-chars-forward " \t\n\r"))
  13783. (defun org-point-in-group (point group &optional context)
  13784. "Check if POINT is in match-group GROUP.
  13785. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  13786. match. If the match group does ot exist or point is not inside it,
  13787. return nil."
  13788. (and (match-beginning group)
  13789. (>= point (match-beginning group))
  13790. (<= point (match-end group))
  13791. (if context
  13792. (list context (match-beginning group) (match-end group))
  13793. t)))
  13794. (defun org-switch-to-buffer-other-window (&rest args)
  13795. "Switch to buffer in a second window on the current frame.
  13796. In particular, do not allow pop-up frames."
  13797. (let (pop-up-frames special-display-buffer-names special-display-regexps
  13798. special-display-function)
  13799. (apply 'switch-to-buffer-other-window args)))
  13800. (defun org-combine-plists (&rest plists)
  13801. "Create a single property list from all plists in PLISTS.
  13802. The process starts by copying the first list, and then setting properties
  13803. from the other lists. Settings in the last list are the most significant
  13804. ones and overrule settings in the other lists."
  13805. (let ((rtn (copy-sequence (pop plists)))
  13806. p v ls)
  13807. (while plists
  13808. (setq ls (pop plists))
  13809. (while ls
  13810. (setq p (pop ls) v (pop ls))
  13811. (setq rtn (plist-put rtn p v))))
  13812. rtn))
  13813. (defun org-move-line-down (arg)
  13814. "Move the current line down. With prefix argument, move it past ARG lines."
  13815. (interactive "p")
  13816. (let ((col (current-column))
  13817. beg end pos)
  13818. (beginning-of-line 1) (setq beg (point))
  13819. (beginning-of-line 2) (setq end (point))
  13820. (beginning-of-line (+ 1 arg))
  13821. (setq pos (move-marker (make-marker) (point)))
  13822. (insert (delete-and-extract-region beg end))
  13823. (goto-char pos)
  13824. (move-to-column col)))
  13825. (defun org-move-line-up (arg)
  13826. "Move the current line up. With prefix argument, move it past ARG lines."
  13827. (interactive "p")
  13828. (let ((col (current-column))
  13829. beg end pos)
  13830. (beginning-of-line 1) (setq beg (point))
  13831. (beginning-of-line 2) (setq end (point))
  13832. (beginning-of-line (- arg))
  13833. (setq pos (move-marker (make-marker) (point)))
  13834. (insert (delete-and-extract-region beg end))
  13835. (goto-char pos)
  13836. (move-to-column col)))
  13837. (defun org-replace-escapes (string table)
  13838. "Replace %-escapes in STRING with values in TABLE.
  13839. TABLE is an association list with keys like \"%a\" and string values.
  13840. The sequences in STRING may contain normal field width and padding information,
  13841. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  13842. so values can contain further %-escapes if they are define later in TABLE."
  13843. (let ((case-fold-search nil)
  13844. e re rpl)
  13845. (while (setq e (pop table))
  13846. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  13847. (while (string-match re string)
  13848. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  13849. (cdr e)))
  13850. (setq string (replace-match rpl t t string))))
  13851. string))
  13852. (defun org-sublist (list start end)
  13853. "Return a section of LIST, from START to END.
  13854. Counting starts at 1."
  13855. (let (rtn (c start))
  13856. (setq list (nthcdr (1- start) list))
  13857. (while (and list (<= c end))
  13858. (push (pop list) rtn)
  13859. (setq c (1+ c)))
  13860. (nreverse rtn)))
  13861. (defun org-find-base-buffer-visiting (file)
  13862. "Like `find-buffer-visiting' but alway return the base buffer and
  13863. not an indirect buffer."
  13864. (let ((buf (find-buffer-visiting file)))
  13865. (if buf
  13866. (or (buffer-base-buffer buf) buf)
  13867. nil)))
  13868. (defun org-image-file-name-regexp ()
  13869. "Return regexp matching the file names of images."
  13870. (if (fboundp 'image-file-name-regexp)
  13871. (image-file-name-regexp)
  13872. (let ((image-file-name-extensions
  13873. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  13874. "xbm" "xpm" "pbm" "pgm" "ppm")))
  13875. (concat "\\."
  13876. (regexp-opt (nconc (mapcar 'upcase
  13877. image-file-name-extensions)
  13878. image-file-name-extensions)
  13879. t)
  13880. "\\'"))))
  13881. (defun org-file-image-p (file)
  13882. "Return non-nil if FILE is an image."
  13883. (save-match-data
  13884. (string-match (org-image-file-name-regexp) file)))
  13885. ;;; Paragraph filling stuff.
  13886. ;; We want this to be just right, so use the full arsenal.
  13887. (defun org-indent-line-function ()
  13888. "Indent line like previous, but further if previous was headline or item."
  13889. (interactive)
  13890. (let* ((pos (point))
  13891. (itemp (org-at-item-p))
  13892. column bpos bcol tpos tcol bullet btype bullet-type)
  13893. ;; Find the previous relevant line
  13894. (beginning-of-line 1)
  13895. (cond
  13896. ((looking-at "#") (setq column 0))
  13897. ((looking-at "\\*+ ") (setq column 0))
  13898. (t
  13899. (beginning-of-line 0)
  13900. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  13901. (beginning-of-line 0))
  13902. (cond
  13903. ((looking-at "\\*+[ \t]+")
  13904. (goto-char (match-end 0))
  13905. (setq column (current-column)))
  13906. ((org-in-item-p)
  13907. (org-beginning-of-item)
  13908. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  13909. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\)?")
  13910. (setq bpos (match-beginning 1) tpos (match-end 0)
  13911. bcol (progn (goto-char bpos) (current-column))
  13912. tcol (progn (goto-char tpos) (current-column))
  13913. bullet (match-string 1)
  13914. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  13915. (if (not itemp)
  13916. (setq column tcol)
  13917. (goto-char pos)
  13918. (beginning-of-line 1)
  13919. (if (looking-at "\\S-")
  13920. (progn
  13921. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  13922. (setq bullet (match-string 1)
  13923. btype (if (string-match "[0-9]" bullet) "n" bullet))
  13924. (setq column (if (equal btype bullet-type) bcol tcol)))
  13925. (setq column (org-get-indentation)))))
  13926. (t (setq column (org-get-indentation))))))
  13927. (goto-char pos)
  13928. (if (<= (current-column) (current-indentation))
  13929. (indent-line-to column)
  13930. (save-excursion (indent-line-to column)))
  13931. (setq column (current-column))
  13932. (beginning-of-line 1)
  13933. (if (looking-at
  13934. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  13935. (replace-match (concat "\\1" (format org-property-format
  13936. (match-string 2) (match-string 3)))
  13937. t nil))
  13938. (move-to-column column)))
  13939. (defun org-set-autofill-regexps ()
  13940. (interactive)
  13941. ;; In the paragraph separator we include headlines, because filling
  13942. ;; text in a line directly attached to a headline would otherwise
  13943. ;; fill the headline as well.
  13944. (org-set-local 'comment-start-skip "^#+[ \t]*")
  13945. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  13946. ;; The paragraph starter includes hand-formatted lists.
  13947. (org-set-local 'paragraph-start
  13948. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  13949. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  13950. ;; But only if the user has not turned off tables or fixed-width regions
  13951. (org-set-local
  13952. 'auto-fill-inhibit-regexp
  13953. (concat "\\*+ \\|#\\+"
  13954. "\\|[ \t]*" org-keyword-time-regexp
  13955. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  13956. (concat
  13957. "\\|[ \t]*["
  13958. (if org-enable-table-editor "|" "")
  13959. (if org-enable-fixed-width-editor ":" "")
  13960. "]"))))
  13961. ;; We use our own fill-paragraph function, to make sure that tables
  13962. ;; and fixed-width regions are not wrapped. That function will pass
  13963. ;; through to `fill-paragraph' when appropriate.
  13964. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  13965. ; Adaptive filling: To get full control, first make sure that
  13966. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  13967. (org-set-local 'adaptive-fill-regexp "\000")
  13968. (org-set-local 'adaptive-fill-function
  13969. 'org-adaptive-fill-function)
  13970. (org-set-local
  13971. 'align-mode-rules-list
  13972. '((org-in-buffer-settings
  13973. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  13974. (modes . '(org-mode))))))
  13975. (defun org-fill-paragraph (&optional justify)
  13976. "Re-align a table, pass through to fill-paragraph if no table."
  13977. (let ((table-p (org-at-table-p))
  13978. (table.el-p (org-at-table.el-p)))
  13979. (cond ((and (equal (char-after (point-at-bol)) ?*)
  13980. (save-excursion (goto-char (point-at-bol))
  13981. (looking-at outline-regexp)))
  13982. t) ; skip headlines
  13983. (table.el-p t) ; skip table.el tables
  13984. (table-p (org-table-align) t) ; align org-mode tables
  13985. (t nil)))) ; call paragraph-fill
  13986. ;; For reference, this is the default value of adaptive-fill-regexp
  13987. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  13988. (defun org-adaptive-fill-function ()
  13989. "Return a fill prefix for org-mode files.
  13990. In particular, this makes sure hanging paragraphs for hand-formatted lists
  13991. work correctly."
  13992. (cond ((looking-at "#[ \t]+")
  13993. (match-string 0))
  13994. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  13995. (save-excursion
  13996. (goto-char (match-end 0))
  13997. (make-string (current-column) ?\ )))
  13998. (t nil)))
  13999. ;;; Other stuff.
  14000. (defun org-toggle-fixed-width-section (arg)
  14001. "Toggle the fixed-width export.
  14002. If there is no active region, the QUOTE keyword at the current headline is
  14003. inserted or removed. When present, it causes the text between this headline
  14004. and the next to be exported as fixed-width text, and unmodified.
  14005. If there is an active region, this command adds or removes a colon as the
  14006. first character of this line. If the first character of a line is a colon,
  14007. this line is also exported in fixed-width font."
  14008. (interactive "P")
  14009. (let* ((cc 0)
  14010. (regionp (org-region-active-p))
  14011. (beg (if regionp (region-beginning) (point)))
  14012. (end (if regionp (region-end)))
  14013. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  14014. (case-fold-search nil)
  14015. (re "[ \t]*\\(:\\)")
  14016. off)
  14017. (if regionp
  14018. (save-excursion
  14019. (goto-char beg)
  14020. (setq cc (current-column))
  14021. (beginning-of-line 1)
  14022. (setq off (looking-at re))
  14023. (while (> nlines 0)
  14024. (setq nlines (1- nlines))
  14025. (beginning-of-line 1)
  14026. (cond
  14027. (arg
  14028. (move-to-column cc t)
  14029. (insert ":\n")
  14030. (forward-line -1))
  14031. ((and off (looking-at re))
  14032. (replace-match "" t t nil 1))
  14033. ((not off) (move-to-column cc t) (insert ":")))
  14034. (forward-line 1)))
  14035. (save-excursion
  14036. (org-back-to-heading)
  14037. (if (looking-at (concat outline-regexp
  14038. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  14039. (replace-match "" t t nil 1)
  14040. (if (looking-at outline-regexp)
  14041. (progn
  14042. (goto-char (match-end 0))
  14043. (insert org-quote-string " "))))))))
  14044. ;;;; Functions extending outline functionality
  14045. (defun org-beginning-of-line (&optional arg)
  14046. "Go to the beginning of the current line. If that is invisible, continue
  14047. to a visible line beginning. This makes the function of C-a more intuitive.
  14048. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14049. first attempt, and only move to after the tags when the cursor is already
  14050. beyond the end of the headline."
  14051. (interactive "P")
  14052. (let ((pos (point)))
  14053. (beginning-of-line 1)
  14054. (if (bobp)
  14055. nil
  14056. (backward-char 1)
  14057. (if (org-invisible-p)
  14058. (while (and (not (bobp)) (org-invisible-p))
  14059. (backward-char 1)
  14060. (beginning-of-line 1))
  14061. (forward-char 1)))
  14062. (when org-special-ctrl-a/e
  14063. (cond
  14064. ((and (looking-at org-todo-line-regexp)
  14065. (= (char-after (match-end 1)) ?\ ))
  14066. (goto-char
  14067. (if (eq org-special-ctrl-a/e t)
  14068. (cond ((> pos (match-beginning 3)) (match-beginning 3))
  14069. ((= pos (point)) (match-beginning 3))
  14070. (t (point)))
  14071. (cond ((> pos (point)) (point))
  14072. ((not (eq last-command this-command)) (point))
  14073. (t (match-beginning 3))))))
  14074. ((org-at-item-p)
  14075. (goto-char
  14076. (if (eq org-special-ctrl-a/e t)
  14077. (cond ((> pos (match-end 4)) (match-end 4))
  14078. ((= pos (point)) (match-end 4))
  14079. (t (point)))
  14080. (cond ((> pos (point)) (point))
  14081. ((not (eq last-command this-command)) (point))
  14082. (t (match-end 4))))))))))
  14083. (defun org-end-of-line (&optional arg)
  14084. "Go to the end of the line.
  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. (if (or (not org-special-ctrl-a/e)
  14090. (not (org-on-heading-p)))
  14091. (end-of-line arg)
  14092. (let ((pos (point)))
  14093. (beginning-of-line 1)
  14094. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14095. (if (eq org-special-ctrl-a/e t)
  14096. (if (or (< pos (match-beginning 1))
  14097. (= pos (match-end 0)))
  14098. (goto-char (match-beginning 1))
  14099. (goto-char (match-end 0)))
  14100. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  14101. (goto-char (match-end 0))
  14102. (goto-char (match-beginning 1))))
  14103. (end-of-line arg)))))
  14104. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  14105. (define-key org-mode-map "\C-e" 'org-end-of-line)
  14106. (defun org-kill-line (&optional arg)
  14107. "Kill line, to tags or end of line."
  14108. (interactive "P")
  14109. (cond
  14110. ((or (not org-special-ctrl-k)
  14111. (bolp)
  14112. (not (org-on-heading-p)))
  14113. (call-interactively 'kill-line))
  14114. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  14115. (kill-region (point) (match-beginning 1))
  14116. (org-set-tags nil t))
  14117. (t (kill-region (point) (point-at-eol)))))
  14118. (define-key org-mode-map "\C-k" 'org-kill-line)
  14119. (defun org-invisible-p ()
  14120. "Check if point is at a character currently not visible."
  14121. ;; Early versions of noutline don't have `outline-invisible-p'.
  14122. (if (fboundp 'outline-invisible-p)
  14123. (outline-invisible-p)
  14124. (get-char-property (point) 'invisible)))
  14125. (defun org-invisible-p2 ()
  14126. "Check if point is at a character currently not visible."
  14127. (save-excursion
  14128. (if (and (eolp) (not (bobp))) (backward-char 1))
  14129. ;; Early versions of noutline don't have `outline-invisible-p'.
  14130. (if (fboundp 'outline-invisible-p)
  14131. (outline-invisible-p)
  14132. (get-char-property (point) 'invisible))))
  14133. (defalias 'org-back-to-heading 'outline-back-to-heading)
  14134. (defalias 'org-on-heading-p 'outline-on-heading-p)
  14135. (defalias 'org-at-heading-p 'outline-on-heading-p)
  14136. (defun org-at-heading-or-item-p ()
  14137. (or (org-on-heading-p) (org-at-item-p)))
  14138. (defun org-on-target-p ()
  14139. (or (org-in-regexp org-radio-target-regexp)
  14140. (org-in-regexp org-target-regexp)))
  14141. (defun org-up-heading-all (arg)
  14142. "Move to the heading line of which the present line is a subheading.
  14143. This function considers both visible and invisible heading lines.
  14144. With argument, move up ARG levels."
  14145. (if (fboundp 'outline-up-heading-all)
  14146. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  14147. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  14148. (defun org-up-heading-safe ()
  14149. "Move to the heading line of which the present line is a subheading.
  14150. This version will not throw an error. It will return the level of the
  14151. headline found, or nil if no higher level is found."
  14152. (let ((pos (point)) start-level level
  14153. (re (concat "^" outline-regexp)))
  14154. (catch 'exit
  14155. (outline-back-to-heading t)
  14156. (setq start-level (funcall outline-level))
  14157. (if (equal start-level 1) (throw 'exit nil))
  14158. (while (re-search-backward re nil t)
  14159. (setq level (funcall outline-level))
  14160. (if (< level start-level) (throw 'exit level)))
  14161. nil)))
  14162. (defun org-first-sibling-p ()
  14163. "Is this heading the first child of its parents?"
  14164. (interactive)
  14165. (let ((re (concat "^" outline-regexp))
  14166. level l)
  14167. (unless (org-at-heading-p t)
  14168. (error "Not at a heading"))
  14169. (setq level (funcall outline-level))
  14170. (save-excursion
  14171. (if (not (re-search-backward re nil t))
  14172. t
  14173. (setq l (funcall outline-level))
  14174. (< l level)))))
  14175. (defun org-goto-sibling (&optional previous)
  14176. "Goto the next sibling, even if it is invisible.
  14177. When PREVIOUS is set, go to the previous sibling instead. Returns t
  14178. when a sibling was found. When none is found, return nil and don't
  14179. move point."
  14180. (let ((fun (if previous 're-search-backward 're-search-forward))
  14181. (pos (point))
  14182. (re (concat "^" outline-regexp))
  14183. level l)
  14184. (when (condition-case nil (org-back-to-heading t) (error nil))
  14185. (setq level (funcall outline-level))
  14186. (catch 'exit
  14187. (or previous (forward-char 1))
  14188. (while (funcall fun re nil t)
  14189. (setq l (funcall outline-level))
  14190. (when (< l level) (goto-char pos) (throw 'exit nil))
  14191. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  14192. (goto-char pos)
  14193. nil))))
  14194. (defun org-show-siblings ()
  14195. "Show all siblings of the current headline."
  14196. (save-excursion
  14197. (while (org-goto-sibling) (org-flag-heading nil)))
  14198. (save-excursion
  14199. (while (org-goto-sibling 'previous)
  14200. (org-flag-heading nil))))
  14201. (defun org-show-hidden-entry ()
  14202. "Show an entry where even the heading is hidden."
  14203. (save-excursion
  14204. (org-show-entry)))
  14205. (defun org-flag-heading (flag &optional entry)
  14206. "Flag the current heading. FLAG non-nil means make invisible.
  14207. When ENTRY is non-nil, show the entire entry."
  14208. (save-excursion
  14209. (org-back-to-heading t)
  14210. ;; Check if we should show the entire entry
  14211. (if entry
  14212. (progn
  14213. (org-show-entry)
  14214. (save-excursion
  14215. (and (outline-next-heading)
  14216. (org-flag-heading nil))))
  14217. (outline-flag-region (max (point-min) (1- (point)))
  14218. (save-excursion (outline-end-of-heading) (point))
  14219. flag))))
  14220. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  14221. ;; This is an exact copy of the original function, but it uses
  14222. ;; `org-back-to-heading', to make it work also in invisible
  14223. ;; trees. And is uses an invisible-OK argument.
  14224. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  14225. (org-back-to-heading invisible-OK)
  14226. (let ((first t)
  14227. (level (funcall outline-level)))
  14228. (while (and (not (eobp))
  14229. (or first (> (funcall outline-level) level)))
  14230. (setq first nil)
  14231. (outline-next-heading))
  14232. (unless to-heading
  14233. (if (memq (preceding-char) '(?\n ?\^M))
  14234. (progn
  14235. ;; Go to end of line before heading
  14236. (forward-char -1)
  14237. (if (memq (preceding-char) '(?\n ?\^M))
  14238. ;; leave blank line before heading
  14239. (forward-char -1))))))
  14240. (point))
  14241. (defun org-show-subtree ()
  14242. "Show everything after this heading at deeper levels."
  14243. (outline-flag-region
  14244. (point)
  14245. (save-excursion
  14246. (outline-end-of-subtree) (outline-next-heading) (point))
  14247. nil))
  14248. (defun org-show-entry ()
  14249. "Show the body directly following this heading.
  14250. Show the heading too, if it is currently invisible."
  14251. (interactive)
  14252. (save-excursion
  14253. (condition-case nil
  14254. (progn
  14255. (org-back-to-heading t)
  14256. (outline-flag-region
  14257. (max (point-min) (1- (point)))
  14258. (save-excursion
  14259. (re-search-forward
  14260. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  14261. (or (match-beginning 1) (point-max)))
  14262. nil))
  14263. (error nil))))
  14264. (defun org-make-options-regexp (kwds)
  14265. "Make a regular expression for keyword lines."
  14266. (concat
  14267. "^"
  14268. "#?[ \t]*\\+\\("
  14269. (mapconcat 'regexp-quote kwds "\\|")
  14270. "\\):[ \t]*"
  14271. "\\(.+\\)"))
  14272. ;; Make isearch reveal the necessary context
  14273. (defun org-isearch-end ()
  14274. "Reveal context after isearch exits."
  14275. (when isearch-success ; only if search was successful
  14276. (if (featurep 'xemacs)
  14277. ;; Under XEmacs, the hook is run in the correct place,
  14278. ;; we directly show the context.
  14279. (org-show-context 'isearch)
  14280. ;; In Emacs the hook runs *before* restoring the overlays.
  14281. ;; So we have to use a one-time post-command-hook to do this.
  14282. ;; (Emacs 22 has a special variable, see function `org-mode')
  14283. (unless (and (boundp 'isearch-mode-end-hook-quit)
  14284. isearch-mode-end-hook-quit)
  14285. ;; Only when the isearch was not quitted.
  14286. (org-add-hook 'post-command-hook 'org-isearch-post-command
  14287. 'append 'local)))))
  14288. (defun org-isearch-post-command ()
  14289. "Remove self from hook, and show context."
  14290. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  14291. (org-show-context 'isearch))
  14292. ;;;; Integration with and fixes for other packages
  14293. ;;; Imenu support
  14294. (defvar org-imenu-markers nil
  14295. "All markers currently used by Imenu.")
  14296. (make-variable-buffer-local 'org-imenu-markers)
  14297. (defun org-imenu-new-marker (&optional pos)
  14298. "Return a new marker for use by Imenu, and remember the marker."
  14299. (let ((m (make-marker)))
  14300. (move-marker m (or pos (point)))
  14301. (push m org-imenu-markers)
  14302. m))
  14303. (defun org-imenu-get-tree ()
  14304. "Produce the index for Imenu."
  14305. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  14306. (setq org-imenu-markers nil)
  14307. (let* ((n org-imenu-depth)
  14308. (re (concat "^" outline-regexp))
  14309. (subs (make-vector (1+ n) nil))
  14310. (last-level 0)
  14311. m tree level head)
  14312. (save-excursion
  14313. (save-restriction
  14314. (widen)
  14315. (goto-char (point-max))
  14316. (while (re-search-backward re nil t)
  14317. (setq level (org-reduced-level (funcall outline-level)))
  14318. (when (<= level n)
  14319. (looking-at org-complex-heading-regexp)
  14320. (setq head (org-match-string-no-properties 4)
  14321. m (org-imenu-new-marker))
  14322. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  14323. (if (>= level last-level)
  14324. (push (cons head m) (aref subs level))
  14325. (push (cons head (aref subs (1+ level))) (aref subs level))
  14326. (loop for i from (1+ level) to n do (aset subs i nil)))
  14327. (setq last-level level)))))
  14328. (aref subs 1)))
  14329. (eval-after-load "imenu"
  14330. '(progn
  14331. (add-hook 'imenu-after-jump-hook
  14332. (lambda () (org-show-context 'org-goto)))))
  14333. ;; Speedbar support
  14334. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  14335. "Overlay marking the agenda restriction line in speedbar.")
  14336. (org-overlay-put org-speedbar-restriction-lock-overlay
  14337. 'face 'org-agenda-restriction-lock)
  14338. (org-overlay-put org-speedbar-restriction-lock-overlay
  14339. 'help-echo "Agendas are currently limited to this item.")
  14340. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  14341. (defun org-speedbar-set-agenda-restriction ()
  14342. "Restrict future agenda commands to the location at point in speedbar.
  14343. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  14344. (interactive)
  14345. (let (p m tp np dir txt w)
  14346. (cond
  14347. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14348. 'org-imenu t))
  14349. (setq m (get-text-property p 'org-imenu-marker))
  14350. (save-excursion
  14351. (save-restriction
  14352. (set-buffer (marker-buffer m))
  14353. (goto-char m)
  14354. (org-agenda-set-restriction-lock 'subtree))))
  14355. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14356. 'speedbar-function 'speedbar-find-file))
  14357. (setq tp (previous-single-property-change
  14358. (1+ p) 'speedbar-function)
  14359. np (next-single-property-change
  14360. tp 'speedbar-function)
  14361. dir (speedbar-line-directory)
  14362. txt (buffer-substring-no-properties (or tp (point-min))
  14363. (or np (point-max))))
  14364. (save-excursion
  14365. (save-restriction
  14366. (set-buffer (find-file-noselect
  14367. (let ((default-directory dir))
  14368. (expand-file-name txt))))
  14369. (unless (org-mode-p)
  14370. (error "Cannot restrict to non-Org-mode file"))
  14371. (org-agenda-set-restriction-lock 'file))))
  14372. (t (error "Don't know how to restrict Org-mode's agenda")))
  14373. (org-move-overlay org-speedbar-restriction-lock-overlay
  14374. (point-at-bol) (point-at-eol))
  14375. (setq current-prefix-arg nil)
  14376. (org-agenda-maybe-redo)))
  14377. (eval-after-load "speedbar"
  14378. '(progn
  14379. (speedbar-add-supported-extension ".org")
  14380. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  14381. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  14382. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  14383. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  14384. (add-hook 'speedbar-visiting-tag-hook
  14385. (lambda () (org-show-context 'org-goto)))))
  14386. ;;; Fixes and Hacks
  14387. ;; Make flyspell not check words in links, to not mess up our keymap
  14388. (defun org-mode-flyspell-verify ()
  14389. "Don't let flyspell put overlays at active buttons."
  14390. (not (get-text-property (point) 'keymap)))
  14391. ;; Make `bookmark-jump' show the jump location if it was hidden.
  14392. (eval-after-load "bookmark"
  14393. '(if (boundp 'bookmark-after-jump-hook)
  14394. ;; We can use the hook
  14395. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  14396. ;; Hook not available, use advice
  14397. (defadvice bookmark-jump (after org-make-visible activate)
  14398. "Make the position visible."
  14399. (org-bookmark-jump-unhide))))
  14400. (defun org-bookmark-jump-unhide ()
  14401. "Unhide the current position, to show the bookmark location."
  14402. (and (org-mode-p)
  14403. (or (org-invisible-p)
  14404. (save-excursion (goto-char (max (point-min) (1- (point))))
  14405. (org-invisible-p)))
  14406. (org-show-context 'bookmark-jump)))
  14407. ;; Make session.el ignore our circular variable
  14408. (eval-after-load "session"
  14409. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  14410. ;;;; Experimental code
  14411. (defun org-closed-in-range ()
  14412. "Sparse tree of items closed in a certain time range.
  14413. Still experimental, may disappear in the future."
  14414. (interactive)
  14415. ;; Get the time interval from the user.
  14416. (let* ((time1 (time-to-seconds
  14417. (org-read-date nil 'to-time nil "Starting date: ")))
  14418. (time2 (time-to-seconds
  14419. (org-read-date nil 'to-time nil "End date:")))
  14420. ;; callback function
  14421. (callback (lambda ()
  14422. (let ((time
  14423. (time-to-seconds
  14424. (apply 'encode-time
  14425. (org-parse-time-string
  14426. (match-string 1))))))
  14427. ;; check if time in interval
  14428. (and (>= time time1) (<= time time2))))))
  14429. ;; make tree, check each match with the callback
  14430. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  14431. ;;;; Finish up
  14432. (provide 'org)
  14433. (run-hooks 'org-load-hook)
  14434. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  14435. ;;; org.el ends here
  14436. (defun org-get-heading (&optional no-tags)
  14437. "Return the heading of the current entry, without the stars."
  14438. (save-excursion
  14439. (org-back-to-heading t)
  14440. (if (looking-at
  14441. (if no-tags
  14442. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  14443. "\\*+[ \t]+\\([^\r\n]*\\)"))
  14444. (match-string 1) "")))
  14445. (defun org-get-category (&optional pos)
  14446. "Get the category applying to position POS."
  14447. (get-text-property (or pos (point)) 'org-category))
  14448. (defun org-get-tags-at (&optional pos)
  14449. "Get a list of all headline tags applicable at POS.
  14450. POS defaults to point. If tags are inherited, the list contains
  14451. the targets in the same sequence as the headlines appear, i.e.
  14452. sthe tags of the current headline come last."
  14453. (interactive)
  14454. (let (tags ltags lastpos parent)
  14455. (save-excursion
  14456. (save-restriction
  14457. (widen)
  14458. (goto-char (or pos (point)))
  14459. (save-match-data
  14460. (condition-case nil
  14461. (progn
  14462. (org-back-to-heading t)
  14463. (while (not (equal lastpos (point)))
  14464. (setq lastpos (point))
  14465. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  14466. (setq ltags (org-split-string
  14467. (org-match-string-no-properties 1) ":"))
  14468. (setq tags (append (org-remove-uniherited-tags ltags)
  14469. tags)))
  14470. (or org-use-tag-inheritance (error ""))
  14471. (org-up-heading-all 1)
  14472. (setq parent t)))
  14473. (error nil))))
  14474. tags)))
  14475. (defun org-days-to-iso-week (days)
  14476. "Return the iso week number."
  14477. (require 'cal-iso)
  14478. (car (calendar-iso-from-absolute days)))
  14479. (defun org-small-year-to-year (year)
  14480. "Convert 2-digit years into 4-digit years.
  14481. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  14482. The year 2000 cannot be abbreviated. Any year lager than 99
  14483. is retrned unchanged."
  14484. (if (< year 38)
  14485. (setq year (+ 2000 year))
  14486. (if (< year 100)
  14487. (setq year (+ 1900 year))))
  14488. year)