org.el 586 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867
  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-2
  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-2"
  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-bibtex 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 a description starts with <C>, the file is not part of emacs
  127. and loading it will require that you have downloaded and properly installed
  128. the org-mode distribution.
  129. You can also use this system to load external packages (i.e. neither Org
  130. core modules, not modules from the CONTRIB directory). Just add symbols
  131. to the end of the list. If the package is called org-xyz.e, then you need
  132. to add the symbol `xyz', and the package must have a call to
  133. (provide 'org-xyz)"
  134. :group 'org
  135. :set 'org-set-modules
  136. :type
  137. '(set :greedy t
  138. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  139. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  140. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  141. (const :tag " info: Links to Info nodes" org-info)
  142. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  143. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  144. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  145. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  146. (const :tag " vm: Links to VM folders/messages" org-vm)
  147. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  148. (const :tag " mouse: Additional mouse support" org-mouse)
  149. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  150. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  151. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  152. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  153. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  154. (const :tag "C id: Global id's for identifying entries" org-id)
  155. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  156. (const :tag "C iswitchb: Use iswitchb to select Org buffer" org-iswitchb)
  157. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  158. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  159. (const :tag "C mew: Support for links to messages in Mew" org-mew)
  160. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  161. (const :tag "C registry: A registry for Org links" org-registry)
  162. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  163. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  164. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  165. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  166. ;; FIXME: Needs a separate group...
  167. (defcustom org-completion-fallback-command 'hippie-expand
  168. "The expansion command called by \\[org-complete] in normal context.
  169. Normal means, no org-mode-specific context."
  170. :group 'org
  171. :type 'function)
  172. (defgroup org-startup nil
  173. "Options concerning startup of Org-mode."
  174. :tag "Org Startup"
  175. :group 'org)
  176. (defcustom org-startup-folded t
  177. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  178. This can also be configured on a per-file basis by adding one of
  179. the following lines anywhere in the buffer:
  180. #+STARTUP: fold
  181. #+STARTUP: nofold
  182. #+STARTUP: content"
  183. :group 'org-startup
  184. :type '(choice
  185. (const :tag "nofold: show all" nil)
  186. (const :tag "fold: overview" t)
  187. (const :tag "content: all headlines" content)))
  188. (defcustom org-startup-truncated t
  189. "Non-nil means, entering Org-mode will set `truncate-lines'.
  190. This is useful since some lines containing links can be very long and
  191. uninteresting. Also tables look terrible when wrapped."
  192. :group 'org-startup
  193. :type 'boolean)
  194. (defcustom org-startup-align-all-tables nil
  195. "Non-nil means, align all tables when visiting a file.
  196. This is useful when the column width in tables is forced with <N> cookies
  197. in table fields. Such tables will look correct only after the first re-align.
  198. This can also be configured on a per-file basis by adding one of
  199. the following lines anywhere in the buffer:
  200. #+STARTUP: align
  201. #+STARTUP: noalign"
  202. :group 'org-startup
  203. :type 'boolean)
  204. (defcustom org-insert-mode-line-in-empty-file nil
  205. "Non-nil means insert the first line setting Org-mode in empty files.
  206. When the function `org-mode' is called interactively in an empty file, this
  207. normally means that the file name does not automatically trigger Org-mode.
  208. To ensure that the file will always be in Org-mode in the future, a
  209. line enforcing Org-mode will be inserted into the buffer, if this option
  210. has been set."
  211. :group 'org-startup
  212. :type 'boolean)
  213. (defcustom org-replace-disputed-keys nil
  214. "Non-nil means use alternative key bindings for some keys.
  215. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  216. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  217. If you want to use Org-mode together with one of these other modes,
  218. or more generally if you would like to move some Org-mode commands to
  219. other keys, set this variable and configure the keys with the variable
  220. `org-disputed-keys'.
  221. This option is only relevant at load-time of Org-mode, and must be set
  222. *before* org.el is loaded. Changing it requires a restart of Emacs to
  223. become effective."
  224. :group 'org-startup
  225. :type 'boolean)
  226. (if (fboundp 'defvaralias)
  227. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  228. (defcustom org-disputed-keys
  229. '(([(shift up)] . [(meta p)])
  230. ([(shift down)] . [(meta n)])
  231. ([(shift left)] . [(meta -)])
  232. ([(shift right)] . [(meta +)])
  233. ([(control shift right)] . [(meta shift +)])
  234. ([(control shift left)] . [(meta shift -)]))
  235. "Keys for which Org-mode and other modes compete.
  236. This is an alist, cars are the default keys, second element specifies
  237. the alternative to use when `org-replace-disputed-keys' is t.
  238. Keys can be specified in any syntax supported by `define-key'.
  239. The value of this option takes effect only at Org-mode's startup,
  240. therefore you'll have to restart Emacs to apply it after changing."
  241. :group 'org-startup
  242. :type 'alist)
  243. (defun org-key (key)
  244. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  245. Or return the original if not disputed."
  246. (if org-replace-disputed-keys
  247. (let* ((nkey (key-description key))
  248. (x (org-find-if (lambda (x)
  249. (equal (key-description (car x)) nkey))
  250. org-disputed-keys)))
  251. (if x (cdr x) key))
  252. key))
  253. (defun org-find-if (predicate seq)
  254. (catch 'exit
  255. (while seq
  256. (if (funcall predicate (car seq))
  257. (throw 'exit (car seq))
  258. (pop seq)))))
  259. (defun org-defkey (keymap key def)
  260. "Define a key, possibly translated, as returned by `org-key'."
  261. (define-key keymap (org-key key) def))
  262. (defcustom org-ellipsis nil
  263. "The ellipsis to use in the Org-mode outline.
  264. When nil, just use the standard three dots. When a string, use that instead,
  265. When a face, use the standart 3 dots, but with the specified face.
  266. The change affects only Org-mode (which will then use its own display table).
  267. Changing this requires executing `M-x org-mode' in a buffer to become
  268. effective."
  269. :group 'org-startup
  270. :type '(choice (const :tag "Default" nil)
  271. (face :tag "Face" :value org-warning)
  272. (string :tag "String" :value "...#")))
  273. (defvar org-display-table nil
  274. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  275. (defgroup org-keywords nil
  276. "Keywords in Org-mode."
  277. :tag "Org Keywords"
  278. :group 'org)
  279. (defcustom org-deadline-string "DEADLINE:"
  280. "String to mark deadline entries.
  281. A deadline is this string, followed by a time stamp. Should be a word,
  282. terminated by a colon. You can insert a schedule keyword and
  283. a timestamp with \\[org-deadline].
  284. Changes become only effective after restarting Emacs."
  285. :group 'org-keywords
  286. :type 'string)
  287. (defcustom org-scheduled-string "SCHEDULED:"
  288. "String to mark scheduled TODO entries.
  289. A schedule is this string, followed by a time stamp. Should be a word,
  290. terminated by a colon. You can insert a schedule keyword and
  291. a timestamp with \\[org-schedule].
  292. Changes become only effective after restarting Emacs."
  293. :group 'org-keywords
  294. :type 'string)
  295. (defcustom org-closed-string "CLOSED:"
  296. "String used as the prefix for timestamps logging closing a TODO entry."
  297. :group 'org-keywords
  298. :type 'string)
  299. (defcustom org-clock-string "CLOCK:"
  300. "String used as prefix for timestamps clocking work hours on an item."
  301. :group 'org-keywords
  302. :type 'string)
  303. (defcustom org-comment-string "COMMENT"
  304. "Entries starting with this keyword will never be exported.
  305. An entry can be toggled between COMMENT and normal with
  306. \\[org-toggle-comment].
  307. Changes become only effective after restarting Emacs."
  308. :group 'org-keywords
  309. :type 'string)
  310. (defcustom org-quote-string "QUOTE"
  311. "Entries starting with this keyword will be exported in fixed-width font.
  312. Quoting applies only to the text in the entry following the headline, and does
  313. not extend beyond the next headline, even if that is lower level.
  314. An entry can be toggled between QUOTE and normal with
  315. \\[org-toggle-fixed-width-section]."
  316. :group 'org-keywords
  317. :type 'string)
  318. (defconst org-repeat-re
  319. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  320. "Regular expression for specifying repeated events.
  321. After a match, group 1 contains the repeat expression.")
  322. (defgroup org-structure nil
  323. "Options concerning the general structure of Org-mode files."
  324. :tag "Org Structure"
  325. :group 'org)
  326. (defgroup org-reveal-location nil
  327. "Options about how to make context of a location visible."
  328. :tag "Org Reveal Location"
  329. :group 'org-structure)
  330. (defconst org-context-choice
  331. '(choice
  332. (const :tag "Always" t)
  333. (const :tag "Never" nil)
  334. (repeat :greedy t :tag "Individual contexts"
  335. (cons
  336. (choice :tag "Context"
  337. (const agenda)
  338. (const org-goto)
  339. (const occur-tree)
  340. (const tags-tree)
  341. (const link-search)
  342. (const mark-goto)
  343. (const bookmark-jump)
  344. (const isearch)
  345. (const default))
  346. (boolean))))
  347. "Contexts for the reveal options.")
  348. (defcustom org-show-hierarchy-above '((default . t))
  349. "Non-nil means, show full hierarchy when revealing a location.
  350. Org-mode often shows locations in an org-mode file which might have
  351. been invisible before. When this is set, the hierarchy of headings
  352. above the exposed location is shown.
  353. Turning this off for example for sparse trees makes them very compact.
  354. Instead of t, this can also be an alist specifying this option for different
  355. contexts. Valid contexts are
  356. agenda when exposing an entry from the agenda
  357. org-goto when using the command `org-goto' on key C-c C-j
  358. occur-tree when using the command `org-occur' on key C-c /
  359. tags-tree when constructing a sparse tree based on tags matches
  360. link-search when exposing search matches associated with a link
  361. mark-goto when exposing the jump goal of a mark
  362. bookmark-jump when exposing a bookmark location
  363. isearch when exiting from an incremental search
  364. default default for all contexts not set explicitly"
  365. :group 'org-reveal-location
  366. :type org-context-choice)
  367. (defcustom org-show-following-heading '((default . nil))
  368. "Non-nil means, show following heading when revealing a location.
  369. Org-mode often shows locations in an org-mode file which might have
  370. been invisible before. When this is set, the heading following the
  371. match is shown.
  372. Turning this off for example for sparse trees makes them very compact,
  373. but makes it harder to edit the location of the match. In such a case,
  374. use the command \\[org-reveal] to show more context.
  375. Instead of t, this can also be an alist specifying this option for different
  376. contexts. See `org-show-hierarchy-above' for valid contexts."
  377. :group 'org-reveal-location
  378. :type org-context-choice)
  379. (defcustom org-show-siblings '((default . nil) (isearch t))
  380. "Non-nil means, show all sibling heading when revealing a location.
  381. Org-mode often shows locations in an org-mode file which might have
  382. been invisible before. When this is set, the sibling of the current entry
  383. heading are all made visible. If `org-show-hierarchy-above' is t,
  384. the same happens on each level of the hierarchy above the current entry.
  385. By default this is on for the isearch context, off for all other contexts.
  386. Turning this off for example for sparse trees makes them very compact,
  387. but makes it harder to edit the location of the match. In such a case,
  388. use the command \\[org-reveal] to show more context.
  389. Instead of t, this can also be an alist specifying this option for different
  390. contexts. See `org-show-hierarchy-above' for valid contexts."
  391. :group 'org-reveal-location
  392. :type org-context-choice)
  393. (defcustom org-show-entry-below '((default . nil))
  394. "Non-nil means, show the entry below a headline when revealing a location.
  395. Org-mode often shows locations in an org-mode file which might have
  396. been invisible before. When this is set, the text below the headline that is
  397. exposed is also shown.
  398. By default this is off for all contexts.
  399. Instead of t, this can also be an alist specifying this option for different
  400. contexts. See `org-show-hierarchy-above' for valid contexts."
  401. :group 'org-reveal-location
  402. :type org-context-choice)
  403. (defcustom org-indirect-buffer-display 'other-window
  404. "How should indirect tree buffers be displayed?
  405. This applies to indirect buffers created with the commands
  406. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  407. Valid values are:
  408. current-window Display in the current window
  409. other-window Just display in another window.
  410. dedicated-frame Create one new frame, and re-use it each time.
  411. new-frame Make a new frame each time. Note that in this case
  412. previously-made indirect buffers are kept, and you need to
  413. kill these buffers yourself."
  414. :group 'org-structure
  415. :group 'org-agenda-windows
  416. :type '(choice
  417. (const :tag "In current window" current-window)
  418. (const :tag "In current frame, other window" other-window)
  419. (const :tag "Each time a new frame" new-frame)
  420. (const :tag "One dedicated frame" dedicated-frame)))
  421. (defgroup org-cycle nil
  422. "Options concerning visibility cycling in Org-mode."
  423. :tag "Org Cycle"
  424. :group 'org-structure)
  425. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  426. "Names of drawers. Drawers are not opened by cycling on the headline above.
  427. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  428. this:
  429. :DRAWERNAME:
  430. .....
  431. :END:
  432. The drawer \"PROPERTIES\" is special for capturing properties through
  433. the property API.
  434. Drawers can be defined on the per-file basis with a line like:
  435. #+DRAWERS: HIDDEN STATE PROPERTIES"
  436. :group 'org-structure
  437. :type '(repeat (string :tag "Drawer Name")))
  438. (defcustom org-cycle-global-at-bob nil
  439. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  440. This makes it possible to do global cycling without having to use S-TAB or
  441. C-u TAB. For this special case to work, the first line of the buffer
  442. must not be a headline - it may be empty ot some other text. When used in
  443. this way, `org-cycle-hook' is disables temporarily, to make sure the
  444. cursor stays at the beginning of the buffer.
  445. When this option is nil, don't do anything special at the beginning
  446. of the buffer."
  447. :group 'org-cycle
  448. :type 'boolean)
  449. (defcustom org-cycle-emulate-tab t
  450. "Where should `org-cycle' emulate TAB.
  451. nil Never
  452. white Only in completely white lines
  453. whitestart Only at the beginning of lines, before the first non-white char
  454. t Everywhere except in headlines
  455. exc-hl-bol Everywhere except at the start of a headline
  456. If TAB is used in a place where it does not emulate TAB, the current subtree
  457. visibility is cycled."
  458. :group 'org-cycle
  459. :type '(choice (const :tag "Never" nil)
  460. (const :tag "Only in completely white lines" white)
  461. (const :tag "Before first char in a line" whitestart)
  462. (const :tag "Everywhere except in headlines" t)
  463. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  464. ))
  465. (defcustom org-cycle-separator-lines 2
  466. "Number of empty lines needed to keep an empty line between collapsed trees.
  467. If you leave an empty line between the end of a subtree and the following
  468. headline, this empty line is hidden when the subtree is folded.
  469. Org-mode will leave (exactly) one empty line visible if the number of
  470. empty lines is equal or larger to the number given in this variable.
  471. So the default 2 means, at least 2 empty lines after the end of a subtree
  472. are needed to produce free space between a collapsed subtree and the
  473. following headline.
  474. Special case: when 0, never leave empty lines in collapsed view."
  475. :group 'org-cycle
  476. :type 'integer)
  477. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  478. org-cycle-hide-drawers
  479. org-cycle-show-empty-lines
  480. org-optimize-window-after-visibility-change)
  481. "Hook that is run after `org-cycle' has changed the buffer visibility.
  482. The function(s) in this hook must accept a single argument which indicates
  483. the new state that was set by the most recent `org-cycle' command. The
  484. argument is a symbol. After a global state change, it can have the values
  485. `overview', `content', or `all'. After a local state change, it can have
  486. the values `folded', `children', or `subtree'."
  487. :group 'org-cycle
  488. :type 'hook)
  489. (defgroup org-edit-structure nil
  490. "Options concerning structure editing in Org-mode."
  491. :tag "Org Edit Structure"
  492. :group 'org-structure)
  493. (defcustom org-odd-levels-only nil
  494. "Non-nil means, skip even levels and only use odd levels for the outline.
  495. This has the effect that two stars are being added/taken away in
  496. promotion/demotion commands. It also influences how levels are
  497. handled by the exporters.
  498. Changing it requires restart of `font-lock-mode' to become effective
  499. for fontification also in regions already fontified.
  500. You may also set this on a per-file basis by adding one of the following
  501. lines to the buffer:
  502. #+STARTUP: odd
  503. #+STARTUP: oddeven"
  504. :group 'org-edit-structure
  505. :group 'org-font-lock
  506. :type 'boolean)
  507. (defcustom org-adapt-indentation t
  508. "Non-nil means, adapt indentation when promoting and demoting.
  509. When this is set and the *entire* text in an entry is indented, the
  510. indentation is increased by one space in a demotion command, and
  511. decreased by one in a promotion command. If any line in the entry
  512. body starts at column 0, indentation is not changed at all."
  513. :group 'org-edit-structure
  514. :type 'boolean)
  515. (defcustom org-special-ctrl-a/e nil
  516. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  517. When t, `C-a' will bring back the cursor to the beginning of the
  518. headline text, i.e. after the stars and after a possible TODO keyword.
  519. In an item, this will be the position after the bullet.
  520. When the cursor is already at that position, another `C-a' will bring
  521. it to the beginning of the line.
  522. `C-e' will jump to the end of the headline, ignoring the presence of tags
  523. in the headline. A second `C-e' will then jump to the true end of the
  524. line, after any tags.
  525. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  526. and only a directly following, identical keypress will bring the cursor
  527. to the special positions."
  528. :group 'org-edit-structure
  529. :type '(choice
  530. (const :tag "off" nil)
  531. (const :tag "after bullet first" t)
  532. (const :tag "border first" reversed)))
  533. (if (fboundp 'defvaralias)
  534. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  535. (defcustom org-special-ctrl-k nil
  536. "Non-nil means `C-k' will behave specially in headlines.
  537. When nil, `C-k' will call the default `kill-line' command.
  538. When t, the following will happen while the cursor is in the headline:
  539. - When the cursor is at the beginning of a headline, kill the entire
  540. line and possible the folded subtree below the line.
  541. - When in the middle of the headline text, kill the headline up to the tags.
  542. - When after the headline text, kill the tags."
  543. :group 'org-edit-structure
  544. :type 'boolean)
  545. (defcustom org-M-RET-may-split-line '((default . t))
  546. "Non-nil means, M-RET will split the line at the cursor position.
  547. When nil, it will go to the end of the line before making a
  548. new line.
  549. You may also set this option in a different way for different
  550. contexts. Valid contexts are:
  551. headline when creating a new headline
  552. item when creating a new item
  553. table in a table field
  554. default the value to be used for all contexts not explicitly
  555. customized"
  556. :group 'org-structure
  557. :group 'org-table
  558. :type '(choice
  559. (const :tag "Always" t)
  560. (const :tag "Never" nil)
  561. (repeat :greedy t :tag "Individual contexts"
  562. (cons
  563. (choice :tag "Context"
  564. (const headline)
  565. (const item)
  566. (const table)
  567. (const default))
  568. (boolean)))))
  569. (defcustom org-blank-before-new-entry '((heading . nil)
  570. (plain-list-item . nil))
  571. "Should `org-insert-heading' leave a blank line before new heading/item?
  572. The value is an alist, with `heading' and `plain-list-item' as car,
  573. and a boolean flag as cdr."
  574. :group 'org-edit-structure
  575. :type '(list
  576. (cons (const heading) (boolean))
  577. (cons (const plain-list-item) (boolean))))
  578. (defcustom org-insert-heading-hook nil
  579. "Hook being run after inserting a new heading."
  580. :group 'org-edit-structure
  581. :type 'hook)
  582. (defcustom org-enable-fixed-width-editor t
  583. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  584. This currently only means, they are never auto-wrapped.
  585. When nil, such lines will be treated like ordinary lines.
  586. See also the QUOTE keyword."
  587. :group 'org-edit-structure
  588. :type 'boolean)
  589. (defcustom org-goto-auto-isearch t
  590. "Non-nil means, typing characters in org-goto starts incremental search."
  591. :group 'org-edit-structure
  592. :type 'boolean)
  593. (defgroup org-sparse-trees nil
  594. "Options concerning sparse trees in Org-mode."
  595. :tag "Org Sparse Trees"
  596. :group 'org-structure)
  597. (defcustom org-highlight-sparse-tree-matches t
  598. "Non-nil means, highlight all matches that define a sparse tree.
  599. The highlights will automatically disappear the next time the buffer is
  600. changed by an edit command."
  601. :group 'org-sparse-trees
  602. :type 'boolean)
  603. (defcustom org-remove-highlights-with-change t
  604. "Non-nil means, any change to the buffer will remove temporary highlights.
  605. Such highlights are created by `org-occur' and `org-clock-display'.
  606. When nil, `C-c C-c needs to be used to get rid of the highlights.
  607. The highlights created by `org-preview-latex-fragment' always need
  608. `C-c C-c' to be removed."
  609. :group 'org-sparse-trees
  610. :group 'org-time
  611. :type 'boolean)
  612. (defcustom org-occur-hook '(org-first-headline-recenter)
  613. "Hook that is run after `org-occur' has constructed a sparse tree.
  614. This can be used to recenter the window to show as much of the structure
  615. as possible."
  616. :group 'org-sparse-trees
  617. :type 'hook)
  618. (defgroup org-plain-lists nil
  619. "Options concerning plain lists in Org-mode."
  620. :tag "Org Plain lists"
  621. :group 'org-structure)
  622. (defcustom org-cycle-include-plain-lists nil
  623. "Non-nil means, include plain lists into visibility cycling.
  624. This means that during cycling, plain list items will *temporarily* be
  625. interpreted as outline headlines with a level given by 1000+i where i is the
  626. indentation of the bullet. In all other operations, plain list items are
  627. not seen as headlines. For example, you cannot assign a TODO keyword to
  628. such an item."
  629. :group 'org-plain-lists
  630. :type 'boolean)
  631. (defcustom org-plain-list-ordered-item-terminator t
  632. "The character that makes a line with leading number an ordered list item.
  633. Valid values are ?. and ?\). To get both terminators, use t. While
  634. ?. may look nicer, it creates the danger that a line with leading
  635. number may be incorrectly interpreted as an item. ?\) therefore is
  636. the safe choice."
  637. :group 'org-plain-lists
  638. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  639. (const :tag "paren like in \"2)\"" ?\))
  640. (const :tab "both" t)))
  641. (defcustom org-auto-renumber-ordered-lists t
  642. "Non-nil means, automatically renumber ordered plain lists.
  643. Renumbering happens when the sequence have been changed with
  644. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  645. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  646. :group 'org-plain-lists
  647. :type 'boolean)
  648. (defcustom org-provide-checkbox-statistics t
  649. "Non-nil means, update checkbox statistics after insert and toggle.
  650. When this is set, checkbox statistics is updated each time you either insert
  651. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  652. with \\[org-ctrl-c-ctrl-c\\]."
  653. :group 'org-plain-lists
  654. :type 'boolean)
  655. (defgroup org-archive nil
  656. "Options concerning archiving in Org-mode."
  657. :tag "Org Archive"
  658. :group 'org-structure)
  659. (defcustom org-archive-tag "ARCHIVE"
  660. "The tag that marks a subtree as archived.
  661. An archived subtree does not open during visibility cycling, and does
  662. not contribute to the agenda listings.
  663. After changing this, font-lock must be restarted in the relevant buffers to
  664. get the proper fontification."
  665. :group 'org-archive
  666. :group 'org-keywords
  667. :type 'string)
  668. (defcustom org-agenda-skip-archived-trees t
  669. "Non-nil means, the agenda will skip any items located in archived trees.
  670. An archived tree is a tree marked with the tag ARCHIVE."
  671. :group 'org-archive
  672. :group 'org-agenda-skip
  673. :type 'boolean)
  674. (defcustom org-cycle-open-archived-trees nil
  675. "Non-nil means, `org-cycle' will open archived trees.
  676. An archived tree is a tree marked with the tag ARCHIVE.
  677. When nil, archived trees will stay folded. You can still open them with
  678. normal outline commands like `show-all', but not with the cycling commands."
  679. :group 'org-archive
  680. :group 'org-cycle
  681. :type 'boolean)
  682. (defcustom org-sparse-tree-open-archived-trees nil
  683. "Non-nil means sparse tree construction shows matches in archived trees.
  684. When nil, matches in these trees are highlighted, but the trees are kept in
  685. collapsed state."
  686. :group 'org-archive
  687. :group 'org-sparse-trees
  688. :type 'boolean)
  689. (defcustom org-archive-location "%s_archive::"
  690. "The location where subtrees should be archived.
  691. This string consists of two parts, separated by a double-colon.
  692. The first part is a file name - when omitted, archiving happens in the same
  693. file. %s will be replaced by the current file name (without directory part).
  694. Archiving to a different file is useful to keep archived entries from
  695. contributing to the Org-mode Agenda.
  696. The part after the double colon is a headline. The archived entries will be
  697. filed under that headline. When omitted, the subtrees are simply filed away
  698. at the end of the file, as top-level entries.
  699. Here are a few examples:
  700. \"%s_archive::\"
  701. If the current file is Projects.org, archive in file
  702. Projects.org_archive, as top-level trees. This is the default.
  703. \"::* Archived Tasks\"
  704. Archive in the current file, under the top-level headline
  705. \"* Archived Tasks\".
  706. \"~/org/archive.org::\"
  707. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  708. \"basement::** Finished Tasks\"
  709. Archive in file ./basement (relative path), as level 3 trees
  710. below the level 2 heading \"** Finished Tasks\".
  711. You may set this option on a per-file basis by adding to the buffer a
  712. line like
  713. #+ARCHIVE: basement::** Finished Tasks"
  714. :group 'org-archive
  715. :type 'string)
  716. (defcustom org-archive-mark-done t
  717. "Non-nil means, mark entries as DONE when they are moved to the archive file.
  718. This can be a string to set the keyword to use. When t, Org-mode will
  719. use the first keyword in its list that means done."
  720. :group 'org-archive
  721. :type '(choice
  722. (const :tag "No" nil)
  723. (const :tag "Yes" t)
  724. (string :tag "Use this keyword")))
  725. (defcustom org-archive-stamp-time t
  726. "Non-nil means, add a time stamp to entries moved to an archive file.
  727. This variable is obsolete and has no effect anymore, instead add ot remove
  728. `time' from the variablle `org-archive-save-context-info'."
  729. :group 'org-archive
  730. :type 'boolean)
  731. (defcustom org-archive-save-context-info '(time file olpath category todo itags)
  732. "Parts of context info that should be stored as properties when archiving.
  733. When a subtree is moved to an archive file, it looses information given by
  734. context, like inherited tags, the category, and possibly also the TODO
  735. state (depending on the variable `org-archive-mark-done').
  736. This variable can be a list of any of the following symbols:
  737. time The time of archiving.
  738. file The file where the entry originates.
  739. itags The local tags, in the headline of the subtree.
  740. ltags The tags the subtree inherits from further up the hierarchy.
  741. todo The pre-archive TODO state.
  742. category The category, taken from file name or #+CATEGORY lines.
  743. olpath The outline path to the item. These are all headlines above
  744. the current item, separated by /, like a file path.
  745. For each symbol present in the list, a property will be created in
  746. the archived entry, with a prefix \"PRE_ARCHIVE_\", to remember this
  747. information."
  748. :group 'org-archive
  749. :type '(set :greedy t
  750. (const :tag "Time" time)
  751. (const :tag "File" file)
  752. (const :tag "Category" category)
  753. (const :tag "TODO state" todo)
  754. (const :tag "TODO state" priority)
  755. (const :tag "Inherited tags" itags)
  756. (const :tag "Outline path" olpath)
  757. (const :tag "Local tags" ltags)))
  758. (defgroup org-imenu-and-speedbar nil
  759. "Options concerning imenu and speedbar in Org-mode."
  760. :tag "Org Imenu and Speedbar"
  761. :group 'org-structure)
  762. (defcustom org-imenu-depth 2
  763. "The maximum level for Imenu access to Org-mode headlines.
  764. This also applied for speedbar access."
  765. :group 'org-imenu-and-speedbar
  766. :type 'number)
  767. (defgroup org-table nil
  768. "Options concerning tables in Org-mode."
  769. :tag "Org Table"
  770. :group 'org)
  771. (defcustom org-enable-table-editor 'optimized
  772. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  773. When nil, such lines will be treated like ordinary lines.
  774. When equal to the symbol `optimized', the table editor will be optimized to
  775. do the following:
  776. - Automatic overwrite mode in front of whitespace in table fields.
  777. This makes the structure of the table stay in tact as long as the edited
  778. field does not exceed the column width.
  779. - Minimize the number of realigns. Normally, the table is aligned each time
  780. TAB or RET are pressed to move to another field. With optimization this
  781. happens only if changes to a field might have changed the column width.
  782. Optimization requires replacing the functions `self-insert-command',
  783. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  784. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  785. very good at guessing when a re-align will be necessary, but you can always
  786. force one with \\[org-ctrl-c-ctrl-c].
  787. If you would like to use the optimized version in Org-mode, but the
  788. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  789. This variable can be used to turn on and off the table editor during a session,
  790. but in order to toggle optimization, a restart is required.
  791. See also the variable `org-table-auto-blank-field'."
  792. :group 'org-table
  793. :type '(choice
  794. (const :tag "off" nil)
  795. (const :tag "on" t)
  796. (const :tag "on, optimized" optimized)))
  797. (defcustom org-table-tab-recognizes-table.el t
  798. "Non-nil means, TAB will automatically notice a table.el table.
  799. When it sees such a table, it moves point into it and - if necessary -
  800. calls `table-recognize-table'."
  801. :group 'org-table-editing
  802. :type 'boolean)
  803. (defgroup org-link nil
  804. "Options concerning links in Org-mode."
  805. :tag "Org Link"
  806. :group 'org)
  807. (defvar org-link-abbrev-alist-local nil
  808. "Buffer-local version of `org-link-abbrev-alist', which see.
  809. The value of this is taken from the #+LINK lines.")
  810. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  811. (defcustom org-link-abbrev-alist nil
  812. "Alist of link abbreviations.
  813. The car of each element is a string, to be replaced at the start of a link.
  814. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  815. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  816. [[linkkey:tag][description]]
  817. If REPLACE is a string, the tag will simply be appended to create the link.
  818. If the string contains \"%s\", the tag will be inserted there.
  819. REPLACE may also be a function that will be called with the tag as the
  820. only argument to create the link, which should be returned as a string.
  821. See the manual for examples."
  822. :group 'org-link
  823. :type 'alist)
  824. (defcustom org-descriptive-links t
  825. "Non-nil means, hide link part and only show description of bracket links.
  826. Bracket links are like [[link][descritpion]]. This variable sets the initial
  827. state in new org-mode buffers. The setting can then be toggled on a
  828. per-buffer basis from the Org->Hyperlinks menu."
  829. :group 'org-link
  830. :type 'boolean)
  831. (defcustom org-link-file-path-type 'adaptive
  832. "How the path name in file links should be stored.
  833. Valid values are:
  834. relative Relative to the current directory, i.e. the directory of the file
  835. into which the link is being inserted.
  836. absolute Absolute path, if possible with ~ for home directory.
  837. noabbrev Absolute path, no abbreviation of home directory.
  838. adaptive Use relative path for files in the current directory and sub-
  839. directories of it. For other files, use an absolute path."
  840. :group 'org-link
  841. :type '(choice
  842. (const relative)
  843. (const absolute)
  844. (const noabbrev)
  845. (const adaptive)))
  846. (defcustom org-activate-links '(bracket angle plain radio tag date)
  847. "Types of links that should be activated in Org-mode files.
  848. This is a list of symbols, each leading to the activation of a certain link
  849. type. In principle, it does not hurt to turn on most link types - there may
  850. be a small gain when turning off unused link types. The types are:
  851. bracket The recommended [[link][description]] or [[link]] links with hiding.
  852. angular Links in angular brackes that may contain whitespace like
  853. <bbdb:Carsten Dominik>.
  854. plain Plain links in normal text, no whitespace, like http://google.com.
  855. radio Text that is matched by a radio target, see manual for details.
  856. tag Tag settings in a headline (link to tag search).
  857. date Time stamps (link to calendar).
  858. Changing this variable requires a restart of Emacs to become effective."
  859. :group 'org-link
  860. :type '(set (const :tag "Double bracket links (new style)" bracket)
  861. (const :tag "Angular bracket links (old style)" angular)
  862. (const :tag "Plain text links" plain)
  863. (const :tag "Radio target matches" radio)
  864. (const :tag "Tags" tag)
  865. (const :tag "Timestamps" date)))
  866. (defgroup org-link-store nil
  867. "Options concerning storing links in Org-mode."
  868. :tag "Org Store Link"
  869. :group 'org-link)
  870. (defcustom org-email-link-description-format "Email %c: %.30s"
  871. "Format of the description part of a link to an email or usenet message.
  872. The following %-excapes will be replaced by corresponding information:
  873. %F full \"From\" field
  874. %f name, taken from \"From\" field, address if no name
  875. %T full \"To\" field
  876. %t first name in \"To\" field, address if no name
  877. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  878. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  879. %s subject
  880. %m message-id.
  881. You may use normal field width specification between the % and the letter.
  882. This is for example useful to limit the length of the subject.
  883. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  884. :group 'org-link-store
  885. :type 'string)
  886. (defcustom org-from-is-user-regexp
  887. (let (r1 r2)
  888. (when (and user-mail-address (not (string= user-mail-address "")))
  889. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  890. (when (and user-full-name (not (string= user-full-name "")))
  891. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  892. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  893. "Regexp mached against the \"From:\" header of an email or usenet message.
  894. It should match if the message is from the user him/herself."
  895. :group 'org-link-store
  896. :type 'regexp)
  897. (defcustom org-context-in-file-links t
  898. "Non-nil means, file links from `org-store-link' contain context.
  899. A search string will be added to the file name with :: as separator and
  900. used to find the context when the link is activated by the command
  901. `org-open-at-point'.
  902. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  903. negates this setting for the duration of the command."
  904. :group 'org-link-store
  905. :type 'boolean)
  906. (defcustom org-keep-stored-link-after-insertion nil
  907. "Non-nil means, keep link in list for entire session.
  908. The command `org-store-link' adds a link pointing to the current
  909. location to an internal list. These links accumulate during a session.
  910. The command `org-insert-link' can be used to insert links into any
  911. Org-mode file (offering completion for all stored links). When this
  912. option is nil, every link which has been inserted once using \\[org-insert-link]
  913. will be removed from the list, to make completing the unused links
  914. more efficient."
  915. :group 'org-link-store
  916. :type 'boolean)
  917. (defgroup org-link-follow nil
  918. "Options concerning following links in Org-mode."
  919. :tag "Org Follow Link"
  920. :group 'org-link)
  921. (defcustom org-follow-link-hook nil
  922. "Hook that is run after a link has been followed."
  923. :group 'org-link-follow
  924. :type 'hook)
  925. (defcustom org-tab-follows-link nil
  926. "Non-nil means, on links TAB will follow the link.
  927. Needs to be set before org.el is loaded."
  928. :group 'org-link-follow
  929. :type 'boolean)
  930. (defcustom org-return-follows-link nil
  931. "Non-nil means, on links RET will follow the link.
  932. Needs to be set before org.el is loaded."
  933. :group 'org-link-follow
  934. :type 'boolean)
  935. (defcustom org-mouse-1-follows-link
  936. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  937. "Non-nil means, mouse-1 on a link will follow the link.
  938. A longer mouse click will still set point. Does not work on XEmacs.
  939. Needs to be set before org.el is loaded."
  940. :group 'org-link-follow
  941. :type 'boolean)
  942. (defcustom org-mark-ring-length 4
  943. "Number of different positions to be recorded in the ring
  944. Changing this requires a restart of Emacs to work correctly."
  945. :group 'org-link-follow
  946. :type 'interger)
  947. (defcustom org-link-frame-setup
  948. '((vm . vm-visit-folder-other-frame)
  949. (gnus . gnus-other-frame)
  950. (file . find-file-other-window))
  951. "Setup the frame configuration for following links.
  952. When following a link with Emacs, it may often be useful to display
  953. this link in another window or frame. This variable can be used to
  954. set this up for the different types of links.
  955. For VM, use any of
  956. `vm-visit-folder'
  957. `vm-visit-folder-other-frame'
  958. For Gnus, use any of
  959. `gnus'
  960. `gnus-other-frame'
  961. For FILE, use any of
  962. `find-file'
  963. `find-file-other-window'
  964. `find-file-other-frame'
  965. For the calendar, use the variable `calendar-setup'.
  966. For BBDB, it is currently only possible to display the matches in
  967. another window."
  968. :group 'org-link-follow
  969. :type '(list
  970. (cons (const vm)
  971. (choice
  972. (const vm-visit-folder)
  973. (const vm-visit-folder-other-window)
  974. (const vm-visit-folder-other-frame)))
  975. (cons (const gnus)
  976. (choice
  977. (const gnus)
  978. (const gnus-other-frame)))
  979. (cons (const file)
  980. (choice
  981. (const find-file)
  982. (const find-file-other-window)
  983. (const find-file-other-frame)))))
  984. (defcustom org-display-internal-link-with-indirect-buffer nil
  985. "Non-nil means, use indirect buffer to display infile links.
  986. Activating internal links (from one location in a file to another location
  987. in the same file) normally just jumps to the location. When the link is
  988. activated with a C-u prefix (or with mouse-3), the link is displayed in
  989. another window. When this option is set, the other window actually displays
  990. an indirect buffer clone of the current buffer, to avoid any visibility
  991. changes to the current buffer."
  992. :group 'org-link-follow
  993. :type 'boolean)
  994. (defcustom org-open-non-existing-files nil
  995. "Non-nil means, `org-open-file' will open non-existing files.
  996. When nil, an error will be generated."
  997. :group 'org-link-follow
  998. :type 'boolean)
  999. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1000. "Function and arguments to call for following mailto links.
  1001. This is a list with the first element being a lisp function, and the
  1002. remaining elements being arguments to the function. In string arguments,
  1003. %a will be replaced by the address, and %s will be replaced by the subject
  1004. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1005. :group 'org-link-follow
  1006. :type '(choice
  1007. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1008. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1009. (const :tag "message-mail" (message-mail "%a" "%s"))
  1010. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1011. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1012. "Non-nil means, ask for confirmation before executing shell links.
  1013. Shell links can be dangerous: just think about a link
  1014. [[shell:rm -rf ~/*][Google Search]]
  1015. This link would show up in your Org-mode document as \"Google Search\",
  1016. but really it would remove your entire home directory.
  1017. Therefore we advise against setting this variable to nil.
  1018. Just change it to `y-or-n-p' of you want to confirm with a
  1019. single keystroke rather than having to type \"yes\"."
  1020. :group 'org-link-follow
  1021. :type '(choice
  1022. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1023. (const :tag "with y-or-n (faster)" y-or-n-p)
  1024. (const :tag "no confirmation (dangerous)" nil)))
  1025. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1026. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1027. Elisp links can be dangerous: just think about a link
  1028. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1029. This link would show up in your Org-mode document as \"Google Search\",
  1030. but really it would remove your entire home directory.
  1031. Therefore we advise against setting this variable to nil.
  1032. Just change it to `y-or-n-p' of you want to confirm with a
  1033. single keystroke rather than having to type \"yes\"."
  1034. :group 'org-link-follow
  1035. :type '(choice
  1036. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1037. (const :tag "with y-or-n (faster)" y-or-n-p)
  1038. (const :tag "no confirmation (dangerous)" nil)))
  1039. (defconst org-file-apps-defaults-gnu
  1040. '((remote . emacs)
  1041. (t . mailcap))
  1042. "Default file applications on a UNIX or GNU/Linux system.
  1043. See `org-file-apps'.")
  1044. (defconst org-file-apps-defaults-macosx
  1045. '((remote . emacs)
  1046. (t . "open %s")
  1047. ("ps" . "gv %s")
  1048. ("ps.gz" . "gv %s")
  1049. ("eps" . "gv %s")
  1050. ("eps.gz" . "gv %s")
  1051. ("dvi" . "xdvi %s")
  1052. ("fig" . "xfig %s"))
  1053. "Default file applications on a MacOS X system.
  1054. The system \"open\" is known as a default, but we use X11 applications
  1055. for some files for which the OS does not have a good default.
  1056. See `org-file-apps'.")
  1057. (defconst org-file-apps-defaults-windowsnt
  1058. (list
  1059. '(remote . emacs)
  1060. (cons t
  1061. (list (if (featurep 'xemacs)
  1062. 'mswindows-shell-execute
  1063. 'w32-shell-execute)
  1064. "open" 'file)))
  1065. "Default file applications on a Windows NT system.
  1066. The system \"open\" is used for most files.
  1067. See `org-file-apps'.")
  1068. (defcustom org-file-apps
  1069. '(
  1070. ("txt" . emacs)
  1071. ("tex" . emacs)
  1072. ("ltx" . emacs)
  1073. ("org" . emacs)
  1074. ("el" . emacs)
  1075. ("bib" . emacs)
  1076. )
  1077. "External applications for opening `file:path' items in a document.
  1078. Org-mode uses system defaults for different file types, but
  1079. you can use this variable to set the application for a given file
  1080. extension. The entries in this list are cons cells where the car identifies
  1081. files and the cdr the corresponding command. Possible values for the
  1082. file identifier are
  1083. \"ext\" A string identifying an extension
  1084. `directory' Matches a directory
  1085. `remote' Matches a remote file, accessible through tramp or efs.
  1086. Remote files most likely should be visited through Emacs
  1087. because external applications cannot handle such paths.
  1088. t Default for all remaining files
  1089. Possible values for the command are:
  1090. `emacs' The file will be visited by the current Emacs process.
  1091. `default' Use the default application for this file type.
  1092. string A command to be executed by a shell; %s will be replaced
  1093. by the path to the file.
  1094. sexp A Lisp form which will be evaluated. The file path will
  1095. be available in the Lisp variable `file'.
  1096. For more examples, see the system specific constants
  1097. `org-file-apps-defaults-macosx'
  1098. `org-file-apps-defaults-windowsnt'
  1099. `org-file-apps-defaults-gnu'."
  1100. :group 'org-link-follow
  1101. :type '(repeat
  1102. (cons (choice :value ""
  1103. (string :tag "Extension")
  1104. (const :tag "Default for unrecognized files" t)
  1105. (const :tag "Remote file" remote)
  1106. (const :tag "Links to a directory" directory))
  1107. (choice :value ""
  1108. (const :tag "Visit with Emacs" emacs)
  1109. (const :tag "Use system default" default)
  1110. (string :tag "Command")
  1111. (sexp :tag "Lisp form")))))
  1112. (defgroup org-refile nil
  1113. "Options concerning refiling entries in Org-mode."
  1114. :tag "Org Remember"
  1115. :group 'org)
  1116. (defcustom org-directory "~/org"
  1117. "Directory with org files.
  1118. This directory will be used as default to prompt for org files.
  1119. Used by the hooks for remember.el."
  1120. :group 'org-refile
  1121. :group 'org-remember
  1122. :type 'directory)
  1123. (defcustom org-default-notes-file "~/.notes"
  1124. "Default target for storing notes.
  1125. Used by the hooks for remember.el. This can be a string, or nil to mean
  1126. the value of `remember-data-file'.
  1127. You can set this on a per-template basis with the variable
  1128. `org-remember-templates'."
  1129. :group 'org-refile
  1130. :group 'org-remember
  1131. :type '(choice
  1132. (const :tag "Default from remember-data-file" nil)
  1133. file))
  1134. (defcustom org-goto-interface 'outline
  1135. "The default interface to be used for `org-goto'.
  1136. Allowed vaues are:
  1137. outline The interface shows an outline of the relevant file
  1138. and the correct heading is found by moving through
  1139. the outline or by searching with incremental search.
  1140. outline-path-completion Headlines in the current buffer are offered via
  1141. completion."
  1142. :group 'org-refile
  1143. :type '(choice
  1144. (const :tag "Outline" outline)
  1145. (const :tag "Outline-path-completion" outline-path-completion)))
  1146. (defcustom org-reverse-note-order nil
  1147. "Non-nil means, store new notes at the beginning of a file or entry.
  1148. When nil, new notes will be filed to the end of a file or entry.
  1149. This can also be a list with cons cells of regular expressions that
  1150. are matched against file names, and values."
  1151. :group 'org-remember
  1152. :type '(choice
  1153. (const :tag "Reverse always" t)
  1154. (const :tag "Reverse never" nil)
  1155. (repeat :tag "By file name regexp"
  1156. (cons regexp boolean))))
  1157. (defcustom org-refile-targets nil
  1158. "Targets for refiling entries with \\[org-refile].
  1159. This is list of cons cells. Each cell contains:
  1160. - a specification of the files to be considered, either a list of files,
  1161. or a symbol whose function or value fields will be used to retrieve
  1162. a file name or a list of file names. Nil means, refile to a different
  1163. heading in the current buffer.
  1164. - A specification of how to find candidate refile targets. This may be
  1165. any of
  1166. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1167. This tag has to be present in all target headlines, inheritance will
  1168. not be considered.
  1169. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1170. todo keyword.
  1171. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1172. headlines that are refiling targets.
  1173. - a cons cell (:level . N). Any headline of level N is considered a target.
  1174. - a cons cell (:maxlevel . N). Any headline with level <= N is a target."
  1175. ;; FIXME: what if there are a var and func with same name???
  1176. :group 'org-remember
  1177. :type '(repeat
  1178. (cons
  1179. (choice :value org-agenda-files
  1180. (const :tag "All agenda files" org-agenda-files)
  1181. (const :tag "Current buffer" nil)
  1182. (function) (variable) (file))
  1183. (choice :tag "Identify target headline by"
  1184. (cons :tag "Specific tag" (const :tag) (string))
  1185. (cons :tag "TODO keyword" (const :todo) (string))
  1186. (cons :tag "Regular expression" (const :regexp) (regexp))
  1187. (cons :tag "Level number" (const :level) (integer))
  1188. (cons :tag "Max Level number" (const :maxlevel) (integer))))))
  1189. (defcustom org-refile-use-outline-path nil
  1190. "Non-nil means, provide refile targets as paths.
  1191. So a level 3 headline will be available as level1/level2/level3.
  1192. When the value is `file', also include the file name (without directory)
  1193. into the path. When `full-file-path', include the full file path."
  1194. :group 'org-remember
  1195. :type '(choice
  1196. (const :tag "Not" nil)
  1197. (const :tag "Yes" t)
  1198. (const :tag "Start with file name" file)
  1199. (const :tag "Start with full file path" full-file-path)))
  1200. (defgroup org-todo nil
  1201. "Options concerning TODO items in Org-mode."
  1202. :tag "Org TODO"
  1203. :group 'org)
  1204. (defgroup org-progress nil
  1205. "Options concerning Progress logging in Org-mode."
  1206. :tag "Org Progress"
  1207. :group 'org-time)
  1208. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1209. "List of TODO entry keyword sequences and their interpretation.
  1210. \\<org-mode-map>This is a list of sequences.
  1211. Each sequence starts with a symbol, either `sequence' or `type',
  1212. indicating if the keywords should be interpreted as a sequence of
  1213. action steps, or as different types of TODO items. The first
  1214. keywords are states requiring action - these states will select a headline
  1215. for inclusion into the global TODO list Org-mode produces. If one of
  1216. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1217. signify that no further action is necessary. If \"|\" is not found,
  1218. the last keyword is treated as the only DONE state of the sequence.
  1219. The command \\[org-todo] cycles an entry through these states, and one
  1220. additional state where no keyword is present. For details about this
  1221. cycling, see the manual.
  1222. TODO keywords and interpretation can also be set on a per-file basis with
  1223. the special #+SEQ_TODO and #+TYP_TODO lines.
  1224. Each keyword can optionally specify a character for fast state selection
  1225. \(in combination with the variable `org-use-fast-todo-selection')
  1226. and specifiers for state change logging, using the same syntax
  1227. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1228. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1229. indicates to record a time stamp each time this state is selected.
  1230. Each keyword may also specify if a timestamp or a note should be
  1231. recorded when entering or leaving the state, by adding additional
  1232. characters in the parenthesis after the keyword. This looks like this:
  1233. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1234. record only the time of the state change. With X and Y being either
  1235. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1236. Y when leaving the state if and only if the *target* state does not
  1237. define X. You may omit any of the fast-selection key or X or /Y,
  1238. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1239. For backward compatibility, this variable may also be just a list
  1240. of keywords - in this case the interptetation (sequence or type) will be
  1241. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1242. :group 'org-todo
  1243. :group 'org-keywords
  1244. :type '(choice
  1245. (repeat :tag "Old syntax, just keywords"
  1246. (string :tag "Keyword"))
  1247. (repeat :tag "New syntax"
  1248. (cons
  1249. (choice
  1250. :tag "Interpretation"
  1251. (const :tag "Sequence (cycling hits every state)" sequence)
  1252. (const :tag "Type (cycling directly to DONE)" type))
  1253. (repeat
  1254. (string :tag "Keyword"))))))
  1255. (defvar org-todo-keywords-1 nil
  1256. "All TODO and DONE keywords active in a buffer.")
  1257. (make-variable-buffer-local 'org-todo-keywords-1)
  1258. (defvar org-todo-keywords-for-agenda nil)
  1259. (defvar org-done-keywords-for-agenda nil)
  1260. (defvar org-not-done-keywords nil)
  1261. (make-variable-buffer-local 'org-not-done-keywords)
  1262. (defvar org-done-keywords nil)
  1263. (make-variable-buffer-local 'org-done-keywords)
  1264. (defvar org-todo-heads nil)
  1265. (make-variable-buffer-local 'org-todo-heads)
  1266. (defvar org-todo-sets nil)
  1267. (make-variable-buffer-local 'org-todo-sets)
  1268. (defvar org-todo-log-states nil)
  1269. (make-variable-buffer-local 'org-todo-log-states)
  1270. (defvar org-todo-kwd-alist nil)
  1271. (make-variable-buffer-local 'org-todo-kwd-alist)
  1272. (defvar org-todo-key-alist nil)
  1273. (make-variable-buffer-local 'org-todo-key-alist)
  1274. (defvar org-todo-key-trigger nil)
  1275. (make-variable-buffer-local 'org-todo-key-trigger)
  1276. (defcustom org-todo-interpretation 'sequence
  1277. "Controls how TODO keywords are interpreted.
  1278. This variable is in principle obsolete and is only used for
  1279. backward compatibility, if the interpretation of todo keywords is
  1280. not given already in `org-todo-keywords'. See that variable for
  1281. more information."
  1282. :group 'org-todo
  1283. :group 'org-keywords
  1284. :type '(choice (const sequence)
  1285. (const type)))
  1286. (defcustom org-use-fast-todo-selection 'prefix
  1287. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1288. This variable describes if and under what circumstances the cycling
  1289. mechanism for TODO keywords will be replaced by a single-key, direct
  1290. selection scheme.
  1291. When nil, fast selection is never used.
  1292. When the symbol `prefix', it will be used when `org-todo' is called with
  1293. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1294. in an agenda buffer.
  1295. When t, fast selection is used by default. In this case, the prefix
  1296. argument forces cycling instead.
  1297. In all cases, the special interface is only used if access keys have actually
  1298. been assigned by the user, i.e. if keywords in the configuration are followed
  1299. by a letter in parenthesis, like TODO(t)."
  1300. :group 'org-todo
  1301. :type '(choice
  1302. (const :tag "Never" nil)
  1303. (const :tag "By default" t)
  1304. (const :tag "Only with C-u C-c C-t" prefix)))
  1305. (defcustom org-after-todo-state-change-hook nil
  1306. "Hook which is run after the state of a TODO item was changed.
  1307. The new state (a string with a TODO keyword, or nil) is available in the
  1308. Lisp variable `state'."
  1309. :group 'org-todo
  1310. :type 'hook)
  1311. (defcustom org-log-done nil
  1312. "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
  1313. When equal to the list (done), also prompt for a closing note.
  1314. This can also be configured on a per-file basis by adding one of
  1315. the following lines anywhere in the buffer:
  1316. #+STARTUP: logdone
  1317. #+STARTUP: lognotedone
  1318. #+STARTUP: nologdone"
  1319. :group 'org-todo
  1320. :group 'org-progress
  1321. :type '(choice
  1322. (const :tag "No logging" nil)
  1323. (const :tag "Record CLOSED timestamp" time)
  1324. (const :tag "Record CLOSED timestamp with closing note." note)))
  1325. ;; Normalize old uses of org-log-done.
  1326. (cond
  1327. ((eq org-log-done t) (setq org-log-done 'time))
  1328. ((and (listp org-log-done) (memq 'done org-log-done))
  1329. (setq org-log-done 'note)))
  1330. ;; FIXME: document
  1331. (defcustom org-log-note-clock-out nil
  1332. "Non-nil means, recored a note when clocking out of an item.
  1333. This can also be configured on a per-file basis by adding one of
  1334. the following lines anywhere in the buffer:
  1335. #+STARTUP: lognoteclock-out
  1336. #+STARTUP: nolognoteclock-out"
  1337. :group 'org-todo
  1338. :group 'org-progress
  1339. :type 'boolean)
  1340. (defcustom org-log-done-with-time t
  1341. "Non-nil means, the CLOSED time stamp will contain date and time.
  1342. When nil, only the date will be recorded."
  1343. :group 'org-progress
  1344. :type 'boolean)
  1345. (defcustom org-log-note-headings
  1346. '((done . "CLOSING NOTE %t")
  1347. (state . "State %-12s %t")
  1348. (note . "Note taken on %t")
  1349. (clock-out . ""))
  1350. "Headings for notes added to entries.
  1351. The value is an alist, with the car being a symbol indicating the note
  1352. context, and the cdr is the heading to be used. The heading may also be the
  1353. empty string.
  1354. %t in the heading will be replaced by a time stamp.
  1355. %s will be replaced by the new TODO state, in double quotes.
  1356. %u will be replaced by the user name.
  1357. %U will be replaced by the full user name."
  1358. :group 'org-todo
  1359. :group 'org-progress
  1360. :type '(list :greedy t
  1361. (cons (const :tag "Heading when closing an item" done) string)
  1362. (cons (const :tag
  1363. "Heading when changing todo state (todo sequence only)"
  1364. state) string)
  1365. (cons (const :tag "Heading when just taking a note" note) string)
  1366. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1367. (unless (assq 'note org-log-note-headings)
  1368. (push '(note . "%t") org-log-note-headings))
  1369. (defcustom org-log-states-order-reversed t
  1370. "Non-nil means, the latest state change note will be directly after heading.
  1371. When nil, the notes will be orderer according to time."
  1372. :group 'org-todo
  1373. :group 'org-progress
  1374. :type 'boolean)
  1375. (defcustom org-log-repeat 'time
  1376. "Non-nil means, record moving through the DONE state when triggering repeat.
  1377. An auto-repeating tasks is immediately switched back to TODO when marked
  1378. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1379. the TODO keyword definition, or recording a cloing note by setting
  1380. `org-log-done', there will be no record of the task moving trhough DONE.
  1381. This variable forces taking a note anyway. Possible values are:
  1382. nil Don't force a record
  1383. time Record a time stamp
  1384. note Record a note
  1385. This option can also be set with on a per-file-basis with
  1386. #+STARTUP: logrepeat
  1387. #+STARTUP: lognoterepeat
  1388. #+STARTUP: nologrepeat
  1389. You can have local logging settings for a subtree by setting the LOGGING
  1390. property to one or more of these keywords."
  1391. :group 'org-todo
  1392. :group 'org-progress
  1393. :type '(choice
  1394. (const :tag "Don't force a record" nil)
  1395. (const :tag "Force recording the DONE state" time)
  1396. (const :tag "Force recording a note with the DONE state" note)))
  1397. (defcustom org-clock-into-drawer 2
  1398. "Should clocking info be wrapped into a drawer?
  1399. When t, clocking info will always be inserted into a :CLOCK: drawer.
  1400. If necessary, the drawer will be created.
  1401. When nil, the drawer will not be created, but used when present.
  1402. When an integer and the number of clocking entries in an item
  1403. reaches or exceeds this number, a drawer will be created."
  1404. :group 'org-todo
  1405. :group 'org-progress
  1406. :type '(choice
  1407. (const :tag "Always" t)
  1408. (const :tag "Only when drawer exists" nil)
  1409. (integer :tag "When at least N clock entries")))
  1410. (defcustom org-clock-out-when-done t
  1411. "When t, the clock will be stopped when the relevant entry is marked DONE.
  1412. Nil means, clock will keep running until stopped explicitly with
  1413. `C-c C-x C-o', or until the clock is started in a different item."
  1414. :group 'org-progress
  1415. :type 'boolean)
  1416. (defcustom org-clock-in-switch-to-state nil
  1417. "Set task to a special todo state while clocking it.
  1418. The value should be the state to which the entry should be switched."
  1419. :group 'org-progress
  1420. :group 'org-todo
  1421. :type '(choice
  1422. (const :tag "Don't force a state" nil)
  1423. (string :tag "State")))
  1424. (defgroup org-priorities nil
  1425. "Priorities in Org-mode."
  1426. :tag "Org Priorities"
  1427. :group 'org-todo)
  1428. (defcustom org-highest-priority ?A
  1429. "The highest priority of TODO items. A character like ?A, ?B etc.
  1430. Must have a smaller ASCII number than `org-lowest-priority'."
  1431. :group 'org-priorities
  1432. :type 'character)
  1433. (defcustom org-lowest-priority ?C
  1434. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1435. Must have a larger ASCII number than `org-highest-priority'."
  1436. :group 'org-priorities
  1437. :type 'character)
  1438. (defcustom org-default-priority ?B
  1439. "The default priority of TODO items.
  1440. This is the priority an item get if no explicit priority is given."
  1441. :group 'org-priorities
  1442. :type 'character)
  1443. (defcustom org-priority-start-cycle-with-default t
  1444. "Non-nil means, start with default priority when starting to cycle.
  1445. When this is nil, the first step in the cycle will be (depending on the
  1446. command used) one higher or lower that the default priority."
  1447. :group 'org-priorities
  1448. :type 'boolean)
  1449. (defgroup org-time nil
  1450. "Options concerning time stamps and deadlines in Org-mode."
  1451. :tag "Org Time"
  1452. :group 'org)
  1453. (defcustom org-insert-labeled-timestamps-at-point nil
  1454. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1455. When nil, these labeled time stamps are forces into the second line of an
  1456. entry, just after the headline. When scheduling from the global TODO list,
  1457. the time stamp will always be forced into the second line."
  1458. :group 'org-time
  1459. :type 'boolean)
  1460. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1461. "Formats for `format-time-string' which are used for time stamps.
  1462. It is not recommended to change this constant.")
  1463. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1464. "Number of minutes to round time stamps to.
  1465. These are two values, the first applies when first creating a time stamp.
  1466. The second applies when changing it with the commands `S-up' and `S-down'.
  1467. When changing the time stamp, this means that it will change in steps
  1468. of N minutes, as given by the second value.
  1469. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1470. numbers should be factors of 60, so for example 5, 10, 15.
  1471. When this is larger than 1, you can still force an exact time-stamp by using
  1472. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1473. and by using a prefix arg to `S-up/down' to specify the exact number
  1474. of minutes to shift."
  1475. :group 'org-time
  1476. :get '(lambda (var) ; Make sure all entries have 5 elements
  1477. (if (integerp (default-value var))
  1478. (list (default-value var) 5)
  1479. (default-value var)))
  1480. :type '(list
  1481. (integer :tag "when inserting times")
  1482. (integer :tag "when modifying times")))
  1483. ;; Make sure old customizations of this variable don't lead to problems.
  1484. (when (integerp org-time-stamp-rounding-minutes)
  1485. (setq org-time-stamp-rounding-minutes
  1486. (list org-time-stamp-rounding-minutes
  1487. org-time-stamp-rounding-minutes)))
  1488. (defcustom org-display-custom-times nil
  1489. "Non-nil means, overlay custom formats over all time stamps.
  1490. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1491. To turn this on on a per-file basis, insert anywhere in the file:
  1492. #+STARTUP: customtime"
  1493. :group 'org-time
  1494. :set 'set-default
  1495. :type 'sexp)
  1496. (make-variable-buffer-local 'org-display-custom-times)
  1497. (defcustom org-time-stamp-custom-formats
  1498. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1499. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1500. These are overlayed over the default ISO format if the variable
  1501. `org-display-custom-times' is set. Time like %H:%M should be at the
  1502. end of the second format."
  1503. :group 'org-time
  1504. :type 'sexp)
  1505. (defun org-time-stamp-format (&optional long inactive)
  1506. "Get the right format for a time string."
  1507. (let ((f (if long (cdr org-time-stamp-formats)
  1508. (car org-time-stamp-formats))))
  1509. (if inactive
  1510. (concat "[" (substring f 1 -1) "]")
  1511. f)))
  1512. (defcustom org-deadline-warning-days 14
  1513. "No. of days before expiration during which a deadline becomes active.
  1514. This variable governs the display in sparse trees and in the agenda.
  1515. When 0 or negative, it means use this number (the absolute value of it)
  1516. even if a deadline has a different individual lead time specified."
  1517. :group 'org-time
  1518. :group 'org-agenda-daily/weekly
  1519. :type 'number)
  1520. (defcustom org-read-date-prefer-future t
  1521. "Non-nil means, assume future for incomplete date input from user.
  1522. This affects the following situations:
  1523. 1. The user gives a day, but no month.
  1524. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1525. read this as the third of *next* month. However, if you enter \"17\",
  1526. it will be considered as *this* month.
  1527. 2. The user gives a month but not a year.
  1528. For example, if it is april and you enter \"feb 2\", this will be read
  1529. as feb 2, *next* year. \"May 5\", however, will be this year.
  1530. Currently this does not work for ISO week specifications.
  1531. When this option is nil, the current month and year will always be used
  1532. as defaults."
  1533. :group 'org-time
  1534. :type 'boolean)
  1535. (defcustom org-read-date-display-live t
  1536. "Non-nil means, display current interpretation of date prompt live.
  1537. This display will be in an overlay, in the minibuffer."
  1538. :group 'org-time
  1539. :type 'boolean)
  1540. (defcustom org-read-date-popup-calendar t
  1541. "Non-nil means, pop up a calendar when prompting for a date.
  1542. In the calendar, the date can be selected with mouse-1. However, the
  1543. minibuffer will also be active, and you can simply enter the date as well.
  1544. When nil, only the minibuffer will be available."
  1545. :group 'org-time
  1546. :type 'boolean)
  1547. (if (fboundp 'defvaralias)
  1548. (defvaralias 'org-popup-calendar-for-date-prompt
  1549. 'org-read-date-popup-calendar))
  1550. (defcustom org-extend-today-until 0
  1551. "The hour when your day really ends.
  1552. This has influence for the following applications:
  1553. - When switching the agenda to \"today\". It it is still earlier than
  1554. the time given here, the day recognized as TODAY is actually yesterday.
  1555. - When a date is read from the user and it is still before the time given
  1556. here, the current date and time will be assumed to be yesterday, 23:59.
  1557. FIXME:
  1558. IMPORTANT: This is still a very experimental feature, it may disappear
  1559. again or it may be extended to mean more things."
  1560. :group 'org-time
  1561. :type 'number)
  1562. (defcustom org-edit-timestamp-down-means-later nil
  1563. "Non-nil means, S-down will increase the time in a time stamp.
  1564. When nil, S-up will increase."
  1565. :group 'org-time
  1566. :type 'boolean)
  1567. (defcustom org-calendar-follow-timestamp-change t
  1568. "Non-nil means, make the calendar window follow timestamp changes.
  1569. When a timestamp is modified and the calendar window is visible, it will be
  1570. moved to the new date."
  1571. :group 'org-time
  1572. :type 'boolean)
  1573. (defcustom org-clock-heading-function nil
  1574. "When non-nil, should be a function to create `org-clock-heading'.
  1575. This is the string shown in the mode line when a clock is running.
  1576. The function is called with point at the beginning of the headline."
  1577. :group 'org-time ; FIXME: Should we have a separate group????
  1578. :type 'function)
  1579. (defgroup org-tags nil
  1580. "Options concerning tags in Org-mode."
  1581. :tag "Org Tags"
  1582. :group 'org)
  1583. (defcustom org-tag-alist nil
  1584. "List of tags allowed in Org-mode files.
  1585. When this list is nil, Org-mode will base TAG input on what is already in the
  1586. buffer.
  1587. The value of this variable is an alist, the car of each entry must be a
  1588. keyword as a string, the cdr may be a character that is used to select
  1589. that tag through the fast-tag-selection interface.
  1590. See the manual for details."
  1591. :group 'org-tags
  1592. :type '(repeat
  1593. (choice
  1594. (cons (string :tag "Tag name")
  1595. (character :tag "Access char"))
  1596. (const :tag "Start radio group" (:startgroup))
  1597. (const :tag "End radio group" (:endgroup)))))
  1598. (defcustom org-use-fast-tag-selection 'auto
  1599. "Non-nil means, use fast tag selection scheme.
  1600. This is a special interface to select and deselect tags with single keys.
  1601. When nil, fast selection is never used.
  1602. When the symbol `auto', fast selection is used if and only if selection
  1603. characters for tags have been configured, either through the variable
  1604. `org-tag-alist' or through a #+TAGS line in the buffer.
  1605. When t, fast selection is always used and selection keys are assigned
  1606. automatically if necessary."
  1607. :group 'org-tags
  1608. :type '(choice
  1609. (const :tag "Always" t)
  1610. (const :tag "Never" nil)
  1611. (const :tag "When selection characters are configured" 'auto)))
  1612. (defcustom org-fast-tag-selection-single-key nil
  1613. "Non-nil means, fast tag selection exits after first change.
  1614. When nil, you have to press RET to exit it.
  1615. During fast tag selection, you can toggle this flag with `C-c'.
  1616. This variable can also have the value `expert'. In this case, the window
  1617. displaying the tags menu is not even shown, until you press C-c again."
  1618. :group 'org-tags
  1619. :type '(choice
  1620. (const :tag "No" nil)
  1621. (const :tag "Yes" t)
  1622. (const :tag "Expert" expert)))
  1623. (defvar org-fast-tag-selection-include-todo nil
  1624. "Non-nil means, fast tags selection interface will also offer TODO states.
  1625. This is an undocumented feature, you should not rely on it.")
  1626. (defcustom org-tags-column -80
  1627. "The column to which tags should be indented in a headline.
  1628. If this number is positive, it specifies the column. If it is negative,
  1629. it means that the tags should be flushright to that column. For example,
  1630. -80 works well for a normal 80 character screen."
  1631. :group 'org-tags
  1632. :type 'integer)
  1633. (defcustom org-auto-align-tags t
  1634. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1635. These operations change the length of a headline and therefore shift
  1636. the tags around. With this options turned on, after each such operation
  1637. the tags are again aligned to `org-tags-column'."
  1638. :group 'org-tags
  1639. :type 'boolean)
  1640. (defcustom org-use-tag-inheritance t
  1641. "Non-nil means, tags in levels apply also for sublevels.
  1642. When nil, only the tags directly given in a specific line apply there.
  1643. If you turn off this option, you very likely want to turn on the
  1644. companion option `org-tags-match-list-sublevels'.
  1645. This may also be a list of tags that should be inherited, or a regexp that
  1646. matches tags that should be inherited."
  1647. :group 'org-tags
  1648. :type '(choice
  1649. (const :tag "Not" nil)
  1650. (const :tag "Always" t)
  1651. (repeat :tag "Specific tags" (string :tag "Tag"))
  1652. (regexp :tag "Tags matched by regexp")))
  1653. (defun org-tag-inherit-p (tag)
  1654. "Check if TAG is one that should be inherited."
  1655. (cond
  1656. ((eq org-use-tag-inheritance t) t)
  1657. ((not org-use-tag-inheritance) nil)
  1658. ((stringp org-use-tag-inheritance)
  1659. (string-match org-use-tag-inheritance tag))
  1660. ((listp org-use-tag-inheritance)
  1661. (member tag org-use-tag-inheritance))
  1662. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1663. (defcustom org-tags-match-list-sublevels nil
  1664. "Non-nil means list also sublevels of headlines matching tag search.
  1665. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1666. the sublevels of a headline matching a tag search often also match
  1667. the same search. Listing all of them can create very long lists.
  1668. Setting this variable to nil causes subtrees of a match to be skipped.
  1669. This option is off by default, because inheritance in on. If you turn
  1670. inheritance off, you very likely want to turn this option on.
  1671. As a special case, if the tag search is restricted to TODO items, the
  1672. value of this variable is ignored and sublevels are always checked, to
  1673. make sure all corresponding TODO items find their way into the list."
  1674. :group 'org-tags
  1675. :type 'boolean)
  1676. (defvar org-tags-history nil
  1677. "History of minibuffer reads for tags.")
  1678. (defvar org-last-tags-completion-table nil
  1679. "The last used completion table for tags.")
  1680. (defvar org-after-tags-change-hook nil
  1681. "Hook that is run after the tags in a line have changed.")
  1682. (defgroup org-properties nil
  1683. "Options concerning properties in Org-mode."
  1684. :tag "Org Properties"
  1685. :group 'org)
  1686. (defcustom org-property-format "%-10s %s"
  1687. "How property key/value pairs should be formatted by `indent-line'.
  1688. When `indent-line' hits a property definition, it will format the line
  1689. according to this format, mainly to make sure that the values are
  1690. lined-up with respect to each other."
  1691. :group 'org-properties
  1692. :type 'string)
  1693. (defcustom org-use-property-inheritance nil
  1694. "Non-nil means, properties apply also for sublevels.
  1695. This setting is chiefly used during property searches. Turning it on can
  1696. cause significant overhead when doing a search, which is why it is not
  1697. on by default.
  1698. When nil, only the properties directly given in the current entry count.
  1699. When t, every property is inherited. The value may also be a list of
  1700. properties that should have inheritance, or a regular expression matching
  1701. properties that should be inherited.
  1702. However, note that some special properties use inheritance under special
  1703. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1704. and the properties ending in \"_ALL\" when they are used as descriptor
  1705. for valid values of a property.
  1706. Note for programmers:
  1707. When querying an entry with `org-entry-get', you can control if inheritance
  1708. should be used. By default, `org-entry-get' looks only at the local
  1709. properties. You can request inheritance by setting the inherit argument
  1710. to t (to force inheritance) or to `selective' (to respect the setting
  1711. in this variable)."
  1712. :group 'org-properties
  1713. :type '(choice
  1714. (const :tag "Not" nil)
  1715. (const :tag "Always" t)
  1716. (repeat :tag "Specific properties" (string :tag "Property"))
  1717. (regexp :tag "Properties matched by regexp")))
  1718. (defun org-property-inherit-p (property)
  1719. "Check if PROPERTY is one that should be inherited."
  1720. (cond
  1721. ((eq org-use-property-inheritance t) t)
  1722. ((not org-use-property-inheritance) nil)
  1723. ((stringp org-use-property-inheritance)
  1724. (string-match org-use-property-inheritance property))
  1725. ((listp org-use-property-inheritance)
  1726. (member property org-use-property-inheritance))
  1727. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1728. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1729. "The default column format, if no other format has been defined.
  1730. This variable can be set on the per-file basis by inserting a line
  1731. #+COLUMNS: %25ITEM ....."
  1732. :group 'org-properties
  1733. :type 'string)
  1734. (defcustom org-global-properties nil
  1735. "List of property/value pairs that can be inherited by any entry.
  1736. You can set buffer-local values for this by adding lines like
  1737. #+PROPERTY: NAME VALUE"
  1738. :group 'org-properties
  1739. :type '(repeat
  1740. (cons (string :tag "Property")
  1741. (string :tag "Value"))))
  1742. (defvar org-local-properties nil
  1743. "List of property/value pairs that can be inherited by any entry.
  1744. Valid for the current buffer.
  1745. This variable is populated from #+PROPERTY lines.")
  1746. (defgroup org-agenda nil
  1747. "Options concerning agenda views in Org-mode."
  1748. :tag "Org Agenda"
  1749. :group 'org)
  1750. (defvar org-category nil
  1751. "Variable used by org files to set a category for agenda display.
  1752. Such files should use a file variable to set it, for example
  1753. # -*- mode: org; org-category: \"ELisp\"
  1754. or contain a special line
  1755. #+CATEGORY: ELisp
  1756. If the file does not specify a category, then file's base name
  1757. is used instead.")
  1758. (make-variable-buffer-local 'org-category)
  1759. (defcustom org-agenda-files nil
  1760. "The files to be used for agenda display.
  1761. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1762. \\[org-remove-file]. You can also use customize to edit the list.
  1763. If an entry is a directory, all files in that directory that are matched by
  1764. `org-agenda-file-regexp' will be part of the file list.
  1765. If the value of the variable is not a list but a single file name, then
  1766. the list of agenda files is actually stored and maintained in that file, one
  1767. agenda file per line."
  1768. :group 'org-agenda
  1769. :type '(choice
  1770. (repeat :tag "List of files and directories" file)
  1771. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1772. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1773. "Regular expression to match files for `org-agenda-files'.
  1774. If any element in the list in that variable contains a directory instead
  1775. of a normal file, all files in that directory that are matched by this
  1776. regular expression will be included."
  1777. :group 'org-agenda
  1778. :type 'regexp)
  1779. (defcustom org-agenda-text-search-extra-files nil
  1780. "List of extra files to be searched by text search commands.
  1781. These files will be search in addition to the agenda files bu the
  1782. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  1783. Note that these files will only be searched for text search commands,
  1784. not for the other agenda views like todo lists, tag earches or the weekly
  1785. agenda. This variable is intended to list notes and possibly archive files
  1786. that should also be searched by these two commands."
  1787. :group 'org-agenda
  1788. :type '(repeat file))
  1789. (if (fboundp 'defvaralias)
  1790. (defvaralias 'org-agenda-multi-occur-extra-files
  1791. 'org-agenda-text-search-extra-files))
  1792. (defcustom org-agenda-skip-unavailable-files nil
  1793. "t means to just skip non-reachable files in `org-agenda-files'.
  1794. Nil means to remove them, after a query, from the list."
  1795. :group 'org-agenda
  1796. :type 'boolean)
  1797. (defcustom org-calendar-to-agenda-key [?c]
  1798. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1799. The command `org-calendar-goto-agenda' will be bound to this key. The
  1800. default is the character `c' because then `c' can be used to switch back and
  1801. forth between agenda and calendar."
  1802. :group 'org-agenda
  1803. :type 'sexp)
  1804. (eval-after-load "calendar"
  1805. '(org-defkey calendar-mode-map org-calendar-to-agenda-key
  1806. 'org-calendar-goto-agenda))
  1807. (defgroup org-latex nil
  1808. "Options for embedding LaTeX code into Org-mode."
  1809. :tag "Org LaTeX"
  1810. :group 'org)
  1811. (defcustom org-format-latex-options
  1812. '(:foreground default :background default :scale 1.0
  1813. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  1814. :matchers ("begin" "$" "$$" "\\(" "\\["))
  1815. "Options for creating images from LaTeX fragments.
  1816. This is a property list with the following properties:
  1817. :foreground the foreground color for images embedded in emacs, e.g. \"Black\".
  1818. `default' means use the forground of the default face.
  1819. :background the background color, or \"Transparent\".
  1820. `default' means use the background of the default face.
  1821. :scale a scaling factor for the size of the images
  1822. :html-foreground, :html-background, :html-scale
  1823. The same numbers for HTML export.
  1824. :matchers a list indicating which matchers should be used to
  1825. find LaTeX fragments. Valid members of this list are:
  1826. \"begin\" find environments
  1827. \"$\" find math expressions surrounded by $...$
  1828. \"$$\" find math expressions surrounded by $$....$$
  1829. \"\\(\" find math expressions surrounded by \\(...\\)
  1830. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  1831. :group 'org-latex
  1832. :type 'plist)
  1833. (defcustom org-format-latex-header "\\documentclass{article}
  1834. \\usepackage{fullpage} % do not remove
  1835. \\usepackage{amssymb}
  1836. \\usepackage[usenames]{color}
  1837. \\usepackage{amsmath}
  1838. \\usepackage{latexsym}
  1839. \\usepackage[mathscr]{eucal}
  1840. \\pagestyle{empty} % do not remove"
  1841. "The document header used for processing LaTeX fragments."
  1842. :group 'org-latex
  1843. :type 'string)
  1844. (defgroup org-font-lock nil
  1845. "Font-lock settings for highlighting in Org-mode."
  1846. :tag "Org Font Lock"
  1847. :group 'org)
  1848. (defcustom org-level-color-stars-only nil
  1849. "Non-nil means fontify only the stars in each headline.
  1850. When nil, the entire headline is fontified.
  1851. Changing it requires restart of `font-lock-mode' to become effective
  1852. also in regions already fontified."
  1853. :group 'org-font-lock
  1854. :type 'boolean)
  1855. (defcustom org-hide-leading-stars nil
  1856. "Non-nil means, hide the first N-1 stars in a headline.
  1857. This works by using the face `org-hide' for these stars. This
  1858. face is white for a light background, and black for a dark
  1859. background. You may have to customize the face `org-hide' to
  1860. make this work.
  1861. Changing it requires restart of `font-lock-mode' to become effective
  1862. also in regions already fontified.
  1863. You may also set this on a per-file basis by adding one of the following
  1864. lines to the buffer:
  1865. #+STARTUP: hidestars
  1866. #+STARTUP: showstars"
  1867. :group 'org-font-lock
  1868. :type 'boolean)
  1869. (defcustom org-fontify-done-headline nil
  1870. "Non-nil means, change the face of a headline if it is marked DONE.
  1871. Normally, only the TODO/DONE keyword indicates the state of a headline.
  1872. When this is non-nil, the headline after the keyword is set to the
  1873. `org-headline-done' as an additional indication."
  1874. :group 'org-font-lock
  1875. :type 'boolean)
  1876. (defcustom org-fontify-emphasized-text t
  1877. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  1878. Changing this variable requires a restart of Emacs to take effect."
  1879. :group 'org-font-lock
  1880. :type 'boolean)
  1881. (defcustom org-highlight-latex-fragments-and-specials nil
  1882. "Non-nil means, fontify what is treated specially by the exporters."
  1883. :group 'org-font-lock
  1884. :type 'boolean)
  1885. (defcustom org-hide-emphasis-markers nil
  1886. "Non-nil mean font-lock should hide the emphasis marker characters."
  1887. :group 'org-font-lock
  1888. :type 'boolean)
  1889. (defvar org-emph-re nil
  1890. "Regular expression for matching emphasis.")
  1891. (defvar org-verbatim-re nil
  1892. "Regular expression for matching verbatim text.")
  1893. (defvar org-emphasis-regexp-components) ; defined just below
  1894. (defvar org-emphasis-alist) ; defined just below
  1895. (defun org-set-emph-re (var val)
  1896. "Set variable and compute the emphasis regular expression."
  1897. (set var val)
  1898. (when (and (boundp 'org-emphasis-alist)
  1899. (boundp 'org-emphasis-regexp-components)
  1900. org-emphasis-alist org-emphasis-regexp-components)
  1901. (let* ((e org-emphasis-regexp-components)
  1902. (pre (car e))
  1903. (post (nth 1 e))
  1904. (border (nth 2 e))
  1905. (body (nth 3 e))
  1906. (nl (nth 4 e))
  1907. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  1908. (body1 (concat body "*?"))
  1909. (markers (mapconcat 'car org-emphasis-alist ""))
  1910. (vmarkers (mapconcat
  1911. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  1912. org-emphasis-alist "")))
  1913. ;; make sure special characters appear at the right position in the class
  1914. (if (string-match "\\^" markers)
  1915. (setq markers (concat (replace-match "" t t markers) "^")))
  1916. (if (string-match "-" markers)
  1917. (setq markers (concat (replace-match "" t t markers) "-")))
  1918. (if (string-match "\\^" vmarkers)
  1919. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  1920. (if (string-match "-" vmarkers)
  1921. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  1922. (if (> nl 0)
  1923. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  1924. (int-to-string nl) "\\}")))
  1925. ;; Make the regexp
  1926. (setq org-emph-re
  1927. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  1928. "\\("
  1929. "\\([" markers "]\\)"
  1930. "\\("
  1931. "[^" border "]\\|"
  1932. "[^" border (if (and nil stacked) markers) "]"
  1933. body1
  1934. "[^" border (if (and nil stacked) markers) "]"
  1935. "\\)"
  1936. "\\3\\)"
  1937. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  1938. (setq org-verbatim-re
  1939. (concat "\\([" pre "]\\|^\\)"
  1940. "\\("
  1941. "\\([" vmarkers "]\\)"
  1942. "\\("
  1943. "[^" border "]\\|"
  1944. "[^" border "]"
  1945. body1
  1946. "[^" border "]"
  1947. "\\)"
  1948. "\\3\\)"
  1949. "\\([" post "]\\|$\\)")))))
  1950. (defcustom org-emphasis-regexp-components
  1951. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  1952. "Components used to build the regular expression for emphasis.
  1953. This is a list with 6 entries. Terminology: In an emphasis string
  1954. like \" *strong word* \", we call the initial space PREMATCH, the final
  1955. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  1956. and \"trong wor\" is the body. The different components in this variable
  1957. specify what is allowed/forbidden in each part:
  1958. pre Chars allowed as prematch. Beginning of line will be allowed too.
  1959. post Chars allowed as postmatch. End of line will be allowed too.
  1960. border The chars *forbidden* as border characters.
  1961. body-regexp A regexp like \".\" to match a body character. Don't use
  1962. non-shy groups here, and don't allow newline here.
  1963. newline The maximum number of newlines allowed in an emphasis exp.
  1964. Use customize to modify this, or restart Emacs after changing it."
  1965. :group 'org-font-lock
  1966. :set 'org-set-emph-re
  1967. :type '(list
  1968. (sexp :tag "Allowed chars in pre ")
  1969. (sexp :tag "Allowed chars in post ")
  1970. (sexp :tag "Forbidden chars in border ")
  1971. (sexp :tag "Regexp for body ")
  1972. (integer :tag "number of newlines allowed")
  1973. (option (boolean :tag "Stacking (DISABLED) "))))
  1974. (defcustom org-emphasis-alist
  1975. '(("*" bold "<b>" "</b>")
  1976. ("/" italic "<i>" "</i>")
  1977. ("_" underline "<u>" "</u>")
  1978. ("=" org-code "<code>" "</code>" verbatim)
  1979. ("~" org-verbatim "" "" verbatim)
  1980. ("+" (:strike-through t) "<del>" "</del>")
  1981. )
  1982. "Special syntax for emphasized text.
  1983. Text starting and ending with a special character will be emphasized, for
  1984. example *bold*, _underlined_ and /italic/. This variable sets the marker
  1985. characters, the face to be used by font-lock for highlighting in Org-mode
  1986. Emacs buffers, and the HTML tags to be used for this.
  1987. Use customize to modify this, or restart Emacs after changing it."
  1988. :group 'org-font-lock
  1989. :set 'org-set-emph-re
  1990. :type '(repeat
  1991. (list
  1992. (string :tag "Marker character")
  1993. (choice
  1994. (face :tag "Font-lock-face")
  1995. (plist :tag "Face property list"))
  1996. (string :tag "HTML start tag")
  1997. (string :tag "HTML end tag")
  1998. (option (const verbatim)))))
  1999. ;;; The faces
  2000. (defgroup org-faces nil
  2001. "Faces in Org-mode."
  2002. :tag "Org Faces"
  2003. :group 'org-font-lock)
  2004. (defface org-hide
  2005. '((((background light)) (:foreground "white"))
  2006. (((background dark)) (:foreground "black")))
  2007. "Face used to hide leading stars in headlines.
  2008. The forground color of this face should be equal to the background
  2009. color of the frame."
  2010. :group 'org-faces)
  2011. (defface org-level-1 ;; font-lock-function-name-face
  2012. (org-compatible-face 'outline-1
  2013. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2014. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2015. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2016. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2017. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  2018. (t (:bold t))))
  2019. "Face used for level 1 headlines."
  2020. :group 'org-faces)
  2021. (defface org-level-2 ;; font-lock-variable-name-face
  2022. (org-compatible-face 'outline-2
  2023. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  2024. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  2025. (((class color) (min-colors 8) (background light)) (:foreground "yellow"))
  2026. (((class color) (min-colors 8) (background dark)) (:foreground "yellow" :bold t))
  2027. (t (:bold t))))
  2028. "Face used for level 2 headlines."
  2029. :group 'org-faces)
  2030. (defface org-level-3 ;; font-lock-keyword-face
  2031. (org-compatible-face 'outline-3
  2032. '((((class color) (min-colors 88) (background light)) (:foreground "Purple"))
  2033. (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
  2034. (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
  2035. (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
  2036. (((class color) (min-colors 8) (background light)) (:foreground "purple" :bold t))
  2037. (((class color) (min-colors 8) (background dark)) (:foreground "cyan" :bold t))
  2038. (t (:bold t))))
  2039. "Face used for level 3 headlines."
  2040. :group 'org-faces)
  2041. (defface org-level-4 ;; font-lock-comment-face
  2042. (org-compatible-face 'outline-4
  2043. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2044. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2045. (((class color) (min-colors 16) (background light)) (:foreground "red"))
  2046. (((class color) (min-colors 16) (background dark)) (:foreground "red1"))
  2047. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2048. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2049. (t (:bold t))))
  2050. "Face used for level 4 headlines."
  2051. :group 'org-faces)
  2052. (defface org-level-5 ;; font-lock-type-face
  2053. (org-compatible-face 'outline-5
  2054. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
  2055. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
  2056. (((class color) (min-colors 8)) (:foreground "green"))))
  2057. "Face used for level 5 headlines."
  2058. :group 'org-faces)
  2059. (defface org-level-6 ;; font-lock-constant-face
  2060. (org-compatible-face 'outline-6
  2061. '((((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
  2062. (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
  2063. (((class color) (min-colors 8)) (:foreground "magenta"))))
  2064. "Face used for level 6 headlines."
  2065. :group 'org-faces)
  2066. (defface org-level-7 ;; font-lock-builtin-face
  2067. (org-compatible-face 'outline-7
  2068. '((((class color) (min-colors 16) (background light)) (:foreground "Orchid"))
  2069. (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue"))
  2070. (((class color) (min-colors 8)) (:foreground "blue"))))
  2071. "Face used for level 7 headlines."
  2072. :group 'org-faces)
  2073. (defface org-level-8 ;; font-lock-string-face
  2074. (org-compatible-face 'outline-8
  2075. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2076. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2077. (((class color) (min-colors 8)) (:foreground "green"))))
  2078. "Face used for level 8 headlines."
  2079. :group 'org-faces)
  2080. (defface org-special-keyword ;; font-lock-string-face
  2081. (org-compatible-face nil
  2082. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2083. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2084. (t (:italic t))))
  2085. "Face used for special keywords."
  2086. :group 'org-faces)
  2087. (defface org-drawer ;; font-lock-function-name-face
  2088. (org-compatible-face nil
  2089. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2090. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2091. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2092. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2093. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  2094. (t (:bold t))))
  2095. "Face used for drawers."
  2096. :group 'org-faces)
  2097. (defface org-property-value nil
  2098. "Face used for the value of a property."
  2099. :group 'org-faces)
  2100. (defface org-column
  2101. (org-compatible-face nil
  2102. '((((class color) (min-colors 16) (background light))
  2103. (:background "grey90"))
  2104. (((class color) (min-colors 16) (background dark))
  2105. (:background "grey30"))
  2106. (((class color) (min-colors 8))
  2107. (:background "cyan" :foreground "black"))
  2108. (t (:inverse-video t))))
  2109. "Face for column display of entry properties."
  2110. :group 'org-faces)
  2111. (when (fboundp 'set-face-attribute)
  2112. ;; Make sure that a fixed-width face is used when we have a column table.
  2113. (set-face-attribute 'org-column nil
  2114. :height (face-attribute 'default :height)
  2115. :family (face-attribute 'default :family)))
  2116. (defface org-warning
  2117. (org-compatible-face 'font-lock-warning-face
  2118. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  2119. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  2120. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2121. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2122. (t (:bold t))))
  2123. "Face for deadlines and TODO keywords."
  2124. :group 'org-faces)
  2125. (defface org-archived ; similar to shadow
  2126. (org-compatible-face 'shadow
  2127. '((((class color grayscale) (min-colors 88) (background light))
  2128. (:foreground "grey50"))
  2129. (((class color grayscale) (min-colors 88) (background dark))
  2130. (:foreground "grey70"))
  2131. (((class color) (min-colors 8) (background light))
  2132. (:foreground "green"))
  2133. (((class color) (min-colors 8) (background dark))
  2134. (:foreground "yellow"))))
  2135. "Face for headline with the ARCHIVE tag."
  2136. :group 'org-faces)
  2137. (defface org-link
  2138. '((((class color) (background light)) (:foreground "Purple" :underline t))
  2139. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  2140. (t (:underline t)))
  2141. "Face for links."
  2142. :group 'org-faces)
  2143. (defface org-ellipsis
  2144. '((((class color) (background light)) (:foreground "DarkGoldenrod" :underline t))
  2145. (((class color) (background dark)) (:foreground "LightGoldenrod" :underline t))
  2146. (t (:strike-through t)))
  2147. "Face for the ellipsis in folded text."
  2148. :group 'org-faces)
  2149. (defface org-target
  2150. '((((class color) (background light)) (:underline t))
  2151. (((class color) (background dark)) (:underline t))
  2152. (t (:underline t)))
  2153. "Face for links."
  2154. :group 'org-faces)
  2155. (defface org-date
  2156. '((((class color) (background light)) (:foreground "Purple" :underline t))
  2157. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  2158. (t (:underline t)))
  2159. "Face for links."
  2160. :group 'org-faces)
  2161. (defface org-sexp-date
  2162. '((((class color) (background light)) (:foreground "Purple"))
  2163. (((class color) (background dark)) (:foreground "Cyan"))
  2164. (t (:underline t)))
  2165. "Face for links."
  2166. :group 'org-faces)
  2167. (defface org-tag
  2168. '((t (:bold t)))
  2169. "Face for tags."
  2170. :group 'org-faces)
  2171. (defface org-todo ; font-lock-warning-face
  2172. (org-compatible-face nil
  2173. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  2174. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  2175. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2176. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2177. (t (:inverse-video t :bold t))))
  2178. "Face for TODO keywords."
  2179. :group 'org-faces)
  2180. (defface org-done ;; font-lock-type-face
  2181. (org-compatible-face nil
  2182. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen" :bold t))
  2183. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen" :bold t))
  2184. (((class color) (min-colors 8)) (:foreground "green"))
  2185. (t (:bold t))))
  2186. "Face used for todo keywords that indicate DONE items."
  2187. :group 'org-faces)
  2188. (defface org-headline-done ;; font-lock-string-face
  2189. (org-compatible-face nil
  2190. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2191. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2192. (((class color) (min-colors 8) (background light)) (:bold nil))))
  2193. "Face used to indicate that a headline is DONE.
  2194. This face is only used if `org-fontify-done-headline' is set. If applies
  2195. to the part of the headline after the DONE keyword."
  2196. :group 'org-faces)
  2197. (defcustom org-todo-keyword-faces nil
  2198. "Faces for specific TODO keywords.
  2199. This is a list of cons cells, with TODO keywords in the car
  2200. and faces in the cdr. The face can be a symbol, or a property
  2201. list of attributes, like (:foreground \"blue\" :weight bold :underline t)."
  2202. :group 'org-faces
  2203. :group 'org-todo
  2204. :type '(repeat
  2205. (cons
  2206. (string :tag "keyword")
  2207. (sexp :tag "face"))))
  2208. (defface org-table ;; font-lock-function-name-face
  2209. (org-compatible-face nil
  2210. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2211. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2212. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2213. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2214. (((class color) (min-colors 8) (background light)) (:foreground "blue"))
  2215. (((class color) (min-colors 8) (background dark)))))
  2216. "Face used for tables."
  2217. :group 'org-faces)
  2218. (defface org-formula
  2219. (org-compatible-face nil
  2220. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2221. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2222. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2223. (((class color) (min-colors 8) (background dark)) (:foreground "red"))
  2224. (t (:bold t :italic t))))
  2225. "Face for formulas."
  2226. :group 'org-faces)
  2227. (defface org-code
  2228. (org-compatible-face nil
  2229. '((((class color grayscale) (min-colors 88) (background light))
  2230. (:foreground "grey50"))
  2231. (((class color grayscale) (min-colors 88) (background dark))
  2232. (:foreground "grey70"))
  2233. (((class color) (min-colors 8) (background light))
  2234. (:foreground "green"))
  2235. (((class color) (min-colors 8) (background dark))
  2236. (:foreground "yellow"))))
  2237. "Face for fixed-with text like code snippets."
  2238. :group 'org-faces
  2239. :version "22.1")
  2240. (defface org-verbatim
  2241. (org-compatible-face nil
  2242. '((((class color grayscale) (min-colors 88) (background light))
  2243. (:foreground "grey50" :underline t))
  2244. (((class color grayscale) (min-colors 88) (background dark))
  2245. (:foreground "grey70" :underline t))
  2246. (((class color) (min-colors 8) (background light))
  2247. (:foreground "green" :underline t))
  2248. (((class color) (min-colors 8) (background dark))
  2249. (:foreground "yellow" :underline t))))
  2250. "Face for fixed-with text like code snippets."
  2251. :group 'org-faces
  2252. :version "22.1")
  2253. (defface org-agenda-structure ;; font-lock-function-name-face
  2254. (org-compatible-face nil
  2255. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2256. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2257. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2258. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2259. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  2260. (t (:bold t))))
  2261. "Face used in agenda for captions and dates."
  2262. :group 'org-faces)
  2263. (defface org-scheduled-today
  2264. (org-compatible-face nil
  2265. '((((class color) (min-colors 88) (background light)) (:foreground "DarkGreen"))
  2266. (((class color) (min-colors 88) (background dark)) (:foreground "PaleGreen"))
  2267. (((class color) (min-colors 8)) (:foreground "green"))
  2268. (t (:bold t :italic t))))
  2269. "Face for items scheduled for a certain day."
  2270. :group 'org-faces)
  2271. (defface org-scheduled-previously
  2272. (org-compatible-face nil
  2273. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2274. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2275. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2276. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2277. (t (:bold t))))
  2278. "Face for items scheduled previously, and not yet done."
  2279. :group 'org-faces)
  2280. (defface org-upcoming-deadline
  2281. (org-compatible-face nil
  2282. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2283. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2284. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2285. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2286. (t (:bold t))))
  2287. "Face for items scheduled previously, and not yet done."
  2288. :group 'org-faces)
  2289. (defcustom org-agenda-deadline-faces
  2290. '((1.0 . org-warning)
  2291. (0.5 . org-upcoming-deadline)
  2292. (0.0 . default))
  2293. "Faces for showing deadlines in the agenda.
  2294. This is a list of cons cells. The cdr of each cell is a face to be used,
  2295. and it can also just be like '(:foreground \"yellow\").
  2296. Each car is a fraction of the head-warning time that must have passed for
  2297. this the face in the cdr to be used for display. The numbers must be
  2298. given in descending order. The head-warning time is normally taken
  2299. from `org-deadline-warning-days', but can also be specified in the deadline
  2300. timestamp itself, like this:
  2301. DEADLINE: <2007-08-13 Mon -8d>
  2302. You may use d for days, w for weeks, m for months and y for years. Months
  2303. and years will only be treated in an approximate fashion (30.4 days for a
  2304. month and 365.24 days for a year)."
  2305. :group 'org-faces
  2306. :group 'org-agenda-daily/weekly
  2307. :type '(repeat
  2308. (cons
  2309. (number :tag "Fraction of head-warning time passed")
  2310. (sexp :tag "Face"))))
  2311. ;; FIXME: this is not a good face yet.
  2312. (defface org-agenda-restriction-lock
  2313. (org-compatible-face nil
  2314. '((((class color) (min-colors 88) (background light)) (:background "yellow1"))
  2315. (((class color) (min-colors 88) (background dark)) (:background "skyblue4"))
  2316. (((class color) (min-colors 16) (background light)) (:background "yellow1"))
  2317. (((class color) (min-colors 16) (background dark)) (:background "skyblue4"))
  2318. (((class color) (min-colors 8)) (:background "cyan" :foreground "black"))
  2319. (t (:inverse-video t))))
  2320. "Face for showing the agenda restriction lock."
  2321. :group 'org-faces)
  2322. (defface org-time-grid ;; font-lock-variable-name-face
  2323. (org-compatible-face nil
  2324. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  2325. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  2326. (((class color) (min-colors 8)) (:foreground "yellow" :weight light))))
  2327. "Face used for time grids."
  2328. :group 'org-faces)
  2329. (defconst org-level-faces
  2330. '(org-level-1 org-level-2 org-level-3 org-level-4
  2331. org-level-5 org-level-6 org-level-7 org-level-8
  2332. ))
  2333. (defcustom org-n-level-faces (length org-level-faces)
  2334. "The number of different faces to be used for headlines.
  2335. Org-mode defines 8 different headline faces, so this can be at most 8.
  2336. If it is less than 8, the level-1 face gets re-used for level N+1 etc."
  2337. :type 'number
  2338. :group 'org-faces)
  2339. ;;; Functions and variables from ther packages
  2340. ;; Declared here to avoid compiler warnings
  2341. ;; XEmacs only
  2342. (defvar outline-mode-menu-heading)
  2343. (defvar outline-mode-menu-show)
  2344. (defvar outline-mode-menu-hide)
  2345. (defvar zmacs-regions) ; XEmacs regions
  2346. ;; Emacs only
  2347. (defvar mark-active)
  2348. ;; Various packages
  2349. ;; FIXME: get the argument lists for the UNKNOWN stuff
  2350. (declare-function calendar-absolute-from-iso "cal-iso" (&optional date))
  2351. (declare-function calendar-forward-day "cal-move" (arg))
  2352. (declare-function calendar-goto-date "cal-move" (date))
  2353. (declare-function calendar-goto-today "cal-move" ())
  2354. (declare-function calendar-iso-from-absolute "cal-iso" (&optional date))
  2355. (defvar calc-embedded-close-formula)
  2356. (defvar calc-embedded-open-formula)
  2357. (declare-function cdlatex-tab "ext:cdlatex" ())
  2358. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2359. (defvar font-lock-unfontify-region-function)
  2360. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2361. (declare-function org-agenda-skip "org-agenda" ())
  2362. (declare-function org-format-agenda-item "org-agenda"
  2363. (extra txt &optional category tags dotime noprefix remove-re))
  2364. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2365. (declare-function org-agenda-change-all-lines "org-agenda"
  2366. (newhead hdmarker &optional fixface))
  2367. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2368. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2369. (declare-function parse-time-string "parse-time" (string))
  2370. (declare-function remember "remember" (&optional initial))
  2371. (declare-function remember-buffer-desc "remember" ())
  2372. (declare-function remember-finalize "remember" ())
  2373. (defvar remember-save-after-remembering)
  2374. (defvar remember-data-file)
  2375. (defvar remember-register)
  2376. (defvar remember-buffer)
  2377. (defvar remember-handler-functions)
  2378. (defvar remember-annotation-functions)
  2379. (defvar texmathp-why)
  2380. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2381. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2382. (defvar w3m-current-url)
  2383. (defvar w3m-current-title)
  2384. (defvar org-latex-regexps)
  2385. ;;; Autoload and prepare some org modules
  2386. ;; Some table stuff that needs to be defined here, because it is used
  2387. ;; by the functions setting up org-mode or checking for table context.
  2388. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2389. "Detects an org-type or table-type table.")
  2390. (defconst org-table-line-regexp "^[ \t]*|"
  2391. "Detects an org-type table line.")
  2392. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2393. "Detects an org-type table line.")
  2394. (defconst org-table-hline-regexp "^[ \t]*|-"
  2395. "Detects an org-type table hline.")
  2396. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2397. "Detects a table-type table hline.")
  2398. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2399. "Searching from within a table (any type) this finds the first line
  2400. outside the table.")
  2401. ;; Autoload the functions in org-table.el that are needed by functions here.
  2402. (eval-and-compile
  2403. (org-autoload "org-table"
  2404. '(org-table-align org-table-begin org-table-blank-field
  2405. org-table-convert org-table-convert-region org-table-copy-down
  2406. org-table-copy-region org-table-create
  2407. org-table-create-or-convert-from-region
  2408. org-table-create-with-table.el org-table-current-dline
  2409. org-table-cut-region org-table-delete-column org-table-edit-field
  2410. org-table-edit-formulas org-table-end org-table-eval-formula
  2411. org-table-export org-table-field-info
  2412. org-table-get-stored-formulas org-table-goto-column
  2413. org-table-hline-and-move org-table-import org-table-insert-column
  2414. org-table-insert-hline org-table-insert-row org-table-iterate
  2415. org-table-justify-field-maybe org-table-kill-row
  2416. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2417. org-table-move-column org-table-move-column-left
  2418. org-table-move-column-right org-table-move-row
  2419. org-table-move-row-down org-table-move-row-up
  2420. org-table-next-field org-table-next-row org-table-paste-rectangle
  2421. org-table-previous-field org-table-recalculate
  2422. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2423. org-table-toggle-coordinate-overlays
  2424. org-table-toggle-formula-debugger org-table-wrap-region
  2425. orgtbl-mode turn-on-orgtbl)))
  2426. (defun org-at-table-p (&optional table-type)
  2427. "Return t if the cursor is inside an org-type table.
  2428. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2429. (if org-enable-table-editor
  2430. (save-excursion
  2431. (beginning-of-line 1)
  2432. (looking-at (if table-type org-table-any-line-regexp
  2433. org-table-line-regexp)))
  2434. nil))
  2435. (defsubst org-table-p () (org-at-table-p))
  2436. (defun org-at-table.el-p ()
  2437. "Return t if and only if we are at a table.el table."
  2438. (and (org-at-table-p 'any)
  2439. (save-excursion
  2440. (goto-char (org-table-begin 'any))
  2441. (looking-at org-table1-hline-regexp))))
  2442. (defun org-table-recognize-table.el ()
  2443. "If there is a table.el table nearby, recognize it and move into it."
  2444. (if org-table-tab-recognizes-table.el
  2445. (if (org-at-table.el-p)
  2446. (progn
  2447. (beginning-of-line 1)
  2448. (if (looking-at org-table-dataline-regexp)
  2449. nil
  2450. (if (looking-at org-table1-hline-regexp)
  2451. (progn
  2452. (beginning-of-line 2)
  2453. (if (looking-at org-table-any-border-regexp)
  2454. (beginning-of-line -1)))))
  2455. (if (re-search-forward "|" (org-table-end t) t)
  2456. (progn
  2457. (require 'table)
  2458. (if (table--at-cell-p (point))
  2459. t
  2460. (message "recognizing table.el table...")
  2461. (table-recognize-table)
  2462. (message "recognizing table.el table...done")))
  2463. (error "This should not happen..."))
  2464. t)
  2465. nil)
  2466. nil))
  2467. (defun org-at-table-hline-p ()
  2468. "Return t if the cursor is inside a hline in a table."
  2469. (if org-enable-table-editor
  2470. (save-excursion
  2471. (beginning-of-line 1)
  2472. (looking-at org-table-hline-regexp))
  2473. nil))
  2474. (defvar org-table-clean-did-remove-column nil)
  2475. (defun org-table-map-tables (function)
  2476. "Apply FUNCTION to the start of all tables in the buffer."
  2477. (save-excursion
  2478. (save-restriction
  2479. (widen)
  2480. (goto-char (point-min))
  2481. (while (re-search-forward org-table-any-line-regexp nil t)
  2482. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2483. (beginning-of-line 1)
  2484. (if (looking-at org-table-line-regexp)
  2485. (save-excursion (funcall function)))
  2486. (re-search-forward org-table-any-border-regexp nil 1))))
  2487. (message "Mapping tables: done"))
  2488. ;; Declare and autoload functions from org-exp.el
  2489. (declare-function org-default-export-plist "org-exp")
  2490. (declare-function org-infile-export-plist "org-exp")
  2491. (declare-function org-get-current-options "org-exp")
  2492. (eval-and-compile
  2493. (org-autoload "org-exp"
  2494. '(org-export org-export-as-ascii org-export-visible
  2495. org-insert-export-options-template org-export-as-html-and-open
  2496. org-export-as-html-batch org-export-as-html-to-buffer
  2497. org-replace-region-by-html org-export-region-as-html
  2498. org-export-as-html org-export-icalendar-this-file
  2499. org-export-icalendar-all-agenda-files
  2500. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2501. ;; Autoload org-remember
  2502. (eval-and-compile
  2503. (org-autoload "org-remember"
  2504. '(org-remember-insinuate org-remember-annotation
  2505. org-remember-apply-template org-remember org-remember-handler)))
  2506. ;;; Variables for pre-computed regular expressions, all buffer local
  2507. (defvar org-drawer-regexp nil
  2508. "Matches first line of a hidden block.")
  2509. (make-variable-buffer-local 'org-drawer-regexp)
  2510. (defvar org-todo-regexp nil
  2511. "Matches any of the TODO state keywords.")
  2512. (make-variable-buffer-local 'org-todo-regexp)
  2513. (defvar org-not-done-regexp nil
  2514. "Matches any of the TODO state keywords except the last one.")
  2515. (make-variable-buffer-local 'org-not-done-regexp)
  2516. (defvar org-todo-line-regexp nil
  2517. "Matches a headline and puts TODO state into group 2 if present.")
  2518. (make-variable-buffer-local 'org-todo-line-regexp)
  2519. (defvar org-complex-heading-regexp nil
  2520. "Matches a headline and puts everything into groups:
  2521. group 1: the stars
  2522. group 2: The todo keyword, maybe
  2523. group 3: Priority cookie
  2524. group 4: True headline
  2525. group 5: Tags")
  2526. (make-variable-buffer-local 'org-complex-heading-regexp)
  2527. (defvar org-todo-line-tags-regexp nil
  2528. "Matches a headline and puts TODO state into group 2 if present.
  2529. Also put tags into group 4 if tags are present.")
  2530. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2531. (defvar org-nl-done-regexp nil
  2532. "Matches newline followed by a headline with the DONE keyword.")
  2533. (make-variable-buffer-local 'org-nl-done-regexp)
  2534. (defvar org-looking-at-done-regexp nil
  2535. "Matches the DONE keyword a point.")
  2536. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2537. (defvar org-ds-keyword-length 12
  2538. "Maximum length of the Deadline and SCHEDULED keywords.")
  2539. (make-variable-buffer-local 'org-ds-keyword-length)
  2540. (defvar org-deadline-regexp nil
  2541. "Matches the DEADLINE keyword.")
  2542. (make-variable-buffer-local 'org-deadline-regexp)
  2543. (defvar org-deadline-time-regexp nil
  2544. "Matches the DEADLINE keyword together with a time stamp.")
  2545. (make-variable-buffer-local 'org-deadline-time-regexp)
  2546. (defvar org-deadline-line-regexp nil
  2547. "Matches the DEADLINE keyword and the rest of the line.")
  2548. (make-variable-buffer-local 'org-deadline-line-regexp)
  2549. (defvar org-scheduled-regexp nil
  2550. "Matches the SCHEDULED keyword.")
  2551. (make-variable-buffer-local 'org-scheduled-regexp)
  2552. (defvar org-scheduled-time-regexp nil
  2553. "Matches the SCHEDULED keyword together with a time stamp.")
  2554. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2555. (defvar org-closed-time-regexp nil
  2556. "Matches the CLOSED keyword together with a time stamp.")
  2557. (make-variable-buffer-local 'org-closed-time-regexp)
  2558. (defvar org-keyword-time-regexp nil
  2559. "Matches any of the 4 keywords, together with the time stamp.")
  2560. (make-variable-buffer-local 'org-keyword-time-regexp)
  2561. (defvar org-keyword-time-not-clock-regexp nil
  2562. "Matches any of the 3 keywords, together with the time stamp.")
  2563. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2564. (defvar org-maybe-keyword-time-regexp nil
  2565. "Matches a timestamp, possibly preceeded by a keyword.")
  2566. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2567. (defvar org-planning-or-clock-line-re nil
  2568. "Matches a line with planning or clock info.")
  2569. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2570. (defconst org-plain-time-of-day-regexp
  2571. (concat
  2572. "\\(\\<[012]?[0-9]"
  2573. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2574. "\\(--?"
  2575. "\\(\\<[012]?[0-9]"
  2576. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2577. "\\)?")
  2578. "Regular expression to match a plain time or time range.
  2579. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2580. groups carry important information:
  2581. 0 the full match
  2582. 1 the first time, range or not
  2583. 8 the second time, if it is a range.")
  2584. (defconst org-plain-time-extension-regexp
  2585. (concat
  2586. "\\(\\<[012]?[0-9]"
  2587. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2588. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2589. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2590. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2591. groups carry important information:
  2592. 0 the full match
  2593. 7 hours of duration
  2594. 9 minutes of duration")
  2595. (defconst org-stamp-time-of-day-regexp
  2596. (concat
  2597. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2598. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2599. "\\(--?"
  2600. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2601. "Regular expression to match a timestamp time or time range.
  2602. After a match, the following groups carry important information:
  2603. 0 the full match
  2604. 1 date plus weekday, for backreferencing to make sure both times on same day
  2605. 2 the first time, range or not
  2606. 4 the second time, if it is a range.")
  2607. (defconst org-startup-options
  2608. '(("fold" org-startup-folded t)
  2609. ("overview" org-startup-folded t)
  2610. ("nofold" org-startup-folded nil)
  2611. ("showall" org-startup-folded nil)
  2612. ("content" org-startup-folded content)
  2613. ("hidestars" org-hide-leading-stars t)
  2614. ("showstars" org-hide-leading-stars nil)
  2615. ("odd" org-odd-levels-only t)
  2616. ("oddeven" org-odd-levels-only nil)
  2617. ("align" org-startup-align-all-tables t)
  2618. ("noalign" org-startup-align-all-tables nil)
  2619. ("customtime" org-display-custom-times t)
  2620. ("logdone" org-log-done time)
  2621. ("lognotedone" org-log-done note)
  2622. ("nologdone" org-log-done nil)
  2623. ("lognoteclock-out" org-log-note-clock-out t)
  2624. ("nolognoteclock-out" org-log-note-clock-out nil)
  2625. ("logrepeat" org-log-repeat state)
  2626. ("lognoterepeat" org-log-repeat note)
  2627. ("nologrepeat" org-log-repeat nil)
  2628. ("constcgs" constants-unit-system cgs)
  2629. ("constSI" constants-unit-system SI))
  2630. "Variable associated with STARTUP options for org-mode.
  2631. Each element is a list of three items: The startup options as written
  2632. in the #+STARTUP line, the corresponding variable, and the value to
  2633. set this variable to if the option is found. An optional forth element PUSH
  2634. means to push this value onto the list in the variable.")
  2635. (defun org-set-regexps-and-options ()
  2636. "Precompute regular expressions for current buffer."
  2637. (when (org-mode-p)
  2638. (org-set-local 'org-todo-kwd-alist nil)
  2639. (org-set-local 'org-todo-key-alist nil)
  2640. (org-set-local 'org-todo-key-trigger nil)
  2641. (org-set-local 'org-todo-keywords-1 nil)
  2642. (org-set-local 'org-done-keywords nil)
  2643. (org-set-local 'org-todo-heads nil)
  2644. (org-set-local 'org-todo-sets nil)
  2645. (org-set-local 'org-todo-log-states nil)
  2646. (let ((re (org-make-options-regexp
  2647. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  2648. "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES"
  2649. "CONSTANTS" "PROPERTY" "DRAWERS")))
  2650. (splitre "[ \t]+")
  2651. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2652. tail sep kws1 prio props drawers)
  2653. (save-excursion
  2654. (save-restriction
  2655. (widen)
  2656. (goto-char (point-min))
  2657. (while (re-search-forward re nil t)
  2658. (setq key (match-string 1) value (org-match-string-no-properties 2))
  2659. (cond
  2660. ((equal key "CATEGORY")
  2661. (if (string-match "[ \t]+$" value)
  2662. (setq value (replace-match "" t t value)))
  2663. (setq cat value))
  2664. ((member key '("SEQ_TODO" "TODO"))
  2665. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2666. ((equal key "TYP_TODO")
  2667. (push (cons 'type (org-split-string value splitre)) kwds))
  2668. ((equal key "TAGS")
  2669. (setq tags (append tags (org-split-string value splitre))))
  2670. ((equal key "COLUMNS")
  2671. (org-set-local 'org-columns-default-format value))
  2672. ((equal key "LINK")
  2673. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2674. (push (cons (match-string 1 value)
  2675. (org-trim (match-string 2 value)))
  2676. links)))
  2677. ((equal key "PRIORITIES")
  2678. (setq prio (org-split-string value " +")))
  2679. ((equal key "PROPERTY")
  2680. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2681. (push (cons (match-string 1 value) (match-string 2 value))
  2682. props)))
  2683. ((equal key "DRAWERS")
  2684. (setq drawers (org-split-string value splitre)))
  2685. ((equal key "CONSTANTS")
  2686. (setq const (append const (org-split-string value splitre))))
  2687. ((equal key "STARTUP")
  2688. (let ((opts (org-split-string value splitre))
  2689. l var val)
  2690. (while (setq l (pop opts))
  2691. (when (setq l (assoc l org-startup-options))
  2692. (setq var (nth 1 l) val (nth 2 l))
  2693. (if (not (nth 3 l))
  2694. (set (make-local-variable var) val)
  2695. (if (not (listp (symbol-value var)))
  2696. (set (make-local-variable var) nil))
  2697. (set (make-local-variable var) (symbol-value var))
  2698. (add-to-list var val))))))
  2699. ((equal key "ARCHIVE")
  2700. (string-match " *$" value)
  2701. (setq arch (replace-match "" t t value))
  2702. (remove-text-properties 0 (length arch)
  2703. '(face t fontified t) arch)))
  2704. )))
  2705. (when cat
  2706. (org-set-local 'org-category (intern cat))
  2707. (push (cons "CATEGORY" cat) props))
  2708. (when prio
  2709. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2710. (setq prio (mapcar 'string-to-char prio))
  2711. (org-set-local 'org-highest-priority (nth 0 prio))
  2712. (org-set-local 'org-lowest-priority (nth 1 prio))
  2713. (org-set-local 'org-default-priority (nth 2 prio)))
  2714. (and props (org-set-local 'org-local-properties (nreverse props)))
  2715. (and drawers (org-set-local 'org-drawers drawers))
  2716. (and arch (org-set-local 'org-archive-location arch))
  2717. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2718. ;; Process the TODO keywords
  2719. (unless kwds
  2720. ;; Use the global values as if they had been given locally.
  2721. (setq kwds (default-value 'org-todo-keywords))
  2722. (if (stringp (car kwds))
  2723. (setq kwds (list (cons org-todo-interpretation
  2724. (default-value 'org-todo-keywords)))))
  2725. (setq kwds (reverse kwds)))
  2726. (setq kwds (nreverse kwds))
  2727. (let (inter kws kw)
  2728. (while (setq kws (pop kwds))
  2729. (setq inter (pop kws) sep (member "|" kws)
  2730. kws0 (delete "|" (copy-sequence kws))
  2731. kwsa nil
  2732. kws1 (mapcar
  2733. (lambda (x)
  2734. ;; 1 2
  2735. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2736. (progn
  2737. (setq kw (match-string 1 x)
  2738. key (and (match-end 2) (match-string 2 x))
  2739. log (org-extract-log-state-settings x))
  2740. (push (cons kw (and key (string-to-char key))) kwsa)
  2741. (and log (push log org-todo-log-states))
  2742. kw)
  2743. (error "Invalid TODO keyword %s" x)))
  2744. kws0)
  2745. kwsa (if kwsa (append '((:startgroup))
  2746. (nreverse kwsa)
  2747. '((:endgroup))))
  2748. hw (car kws1)
  2749. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2750. tail (list inter hw (car dws) (org-last dws)))
  2751. (add-to-list 'org-todo-heads hw 'append)
  2752. (push kws1 org-todo-sets)
  2753. (setq org-done-keywords (append org-done-keywords dws nil))
  2754. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2755. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2756. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2757. (setq org-todo-sets (nreverse org-todo-sets)
  2758. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2759. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2760. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2761. ;; Process the constants
  2762. (when const
  2763. (let (e cst)
  2764. (while (setq e (pop const))
  2765. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2766. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2767. (setq org-table-formula-constants-local cst)))
  2768. ;; Process the tags.
  2769. (when tags
  2770. (let (e tgs)
  2771. (while (setq e (pop tags))
  2772. (cond
  2773. ((equal e "{") (push '(:startgroup) tgs))
  2774. ((equal e "}") (push '(:endgroup) tgs))
  2775. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2776. (push (cons (match-string 1 e)
  2777. (string-to-char (match-string 2 e)))
  2778. tgs))
  2779. (t (push (list e) tgs))))
  2780. (org-set-local 'org-tag-alist nil)
  2781. (while (setq e (pop tgs))
  2782. (or (and (stringp (car e))
  2783. (assoc (car e) org-tag-alist))
  2784. (push e org-tag-alist))))))
  2785. ;; Compute the regular expressions and other local variables
  2786. (if (not org-done-keywords)
  2787. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  2788. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2789. (length org-scheduled-string)))
  2790. org-drawer-regexp
  2791. (concat "^[ \t]*:\\("
  2792. (mapconcat 'regexp-quote org-drawers "\\|")
  2793. "\\):[ \t]*$")
  2794. org-not-done-keywords
  2795. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  2796. org-todo-regexp
  2797. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  2798. "\\|") "\\)\\>")
  2799. org-not-done-regexp
  2800. (concat "\\<\\("
  2801. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  2802. "\\)\\>")
  2803. org-todo-line-regexp
  2804. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2805. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2806. "\\)\\>\\)?[ \t]*\\(.*\\)")
  2807. org-complex-heading-regexp
  2808. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  2809. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2810. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  2811. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  2812. org-nl-done-regexp
  2813. (concat "\n\\*+[ \t]+"
  2814. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  2815. "\\)" "\\>")
  2816. org-todo-line-tags-regexp
  2817. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2818. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2819. (org-re
  2820. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  2821. org-looking-at-done-regexp
  2822. (concat "^" "\\(?:"
  2823. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  2824. "\\>")
  2825. org-deadline-regexp (concat "\\<" org-deadline-string)
  2826. org-deadline-time-regexp
  2827. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2828. org-deadline-line-regexp
  2829. (concat "\\<\\(" org-deadline-string "\\).*")
  2830. org-scheduled-regexp
  2831. (concat "\\<" org-scheduled-string)
  2832. org-scheduled-time-regexp
  2833. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2834. org-closed-time-regexp
  2835. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2836. org-keyword-time-regexp
  2837. (concat "\\<\\(" org-scheduled-string
  2838. "\\|" org-deadline-string
  2839. "\\|" org-closed-string
  2840. "\\|" org-clock-string "\\)"
  2841. " *[[<]\\([^]>]+\\)[]>]")
  2842. org-keyword-time-not-clock-regexp
  2843. (concat "\\<\\(" org-scheduled-string
  2844. "\\|" org-deadline-string
  2845. "\\|" org-closed-string
  2846. "\\)"
  2847. " *[[<]\\([^]>]+\\)[]>]")
  2848. org-maybe-keyword-time-regexp
  2849. (concat "\\(\\<\\(" org-scheduled-string
  2850. "\\|" org-deadline-string
  2851. "\\|" org-closed-string
  2852. "\\|" org-clock-string "\\)\\)?"
  2853. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  2854. org-planning-or-clock-line-re
  2855. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  2856. "\\|" org-deadline-string
  2857. "\\|" org-closed-string "\\|" org-clock-string
  2858. "\\)\\>\\)")
  2859. )
  2860. (org-compute-latex-and-specials-regexp)
  2861. (org-set-font-lock-defaults)))
  2862. (defun org-extract-log-state-settings (x)
  2863. "Extract the log state setting from a TODO keyword string.
  2864. This will extract info from a string like \"WAIT(w@/!)\"."
  2865. (let (kw key log1 log2)
  2866. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  2867. (setq kw (match-string 1 x)
  2868. key (and (match-end 2) (match-string 2 x))
  2869. log1 (and (match-end 3) (match-string 3 x))
  2870. log2 (and (match-end 4) (match-string 4 x)))
  2871. (and (or log1 log2)
  2872. (list kw
  2873. (and log1 (if (equal log1 "!") 'time 'note))
  2874. (and log2 (if (equal log2 "!") 'time 'note)))))))
  2875. (defun org-remove-keyword-keys (list)
  2876. "Remove a pair of parenthesis at the end of each string in LIST."
  2877. (mapcar (lambda (x)
  2878. (if (string-match "(.*)$" x)
  2879. (substring x 0 (match-beginning 0))
  2880. x))
  2881. list))
  2882. ;; FIXME: this could be done much better, using second characters etc.
  2883. (defun org-assign-fast-keys (alist)
  2884. "Assign fast keys to a keyword-key alist.
  2885. Respect keys that are already there."
  2886. (let (new e k c c1 c2 (char ?a))
  2887. (while (setq e (pop alist))
  2888. (cond
  2889. ((equal e '(:startgroup)) (push e new))
  2890. ((equal e '(:endgroup)) (push e new))
  2891. (t
  2892. (setq k (car e) c2 nil)
  2893. (if (cdr e)
  2894. (setq c (cdr e))
  2895. ;; automatically assign a character.
  2896. (setq c1 (string-to-char
  2897. (downcase (substring
  2898. k (if (= (string-to-char k) ?@) 1 0)))))
  2899. (if (or (rassoc c1 new) (rassoc c1 alist))
  2900. (while (or (rassoc char new) (rassoc char alist))
  2901. (setq char (1+ char)))
  2902. (setq c2 c1))
  2903. (setq c (or c2 char)))
  2904. (push (cons k c) new))))
  2905. (nreverse new)))
  2906. ;;; Some variables ujsed in various places
  2907. (defvar org-window-configuration nil
  2908. "Used in various places to store a window configuration.")
  2909. (defvar org-finish-function nil
  2910. "Function to be called when `C-c C-c' is used.
  2911. This is for getting out of special buffers like remember.")
  2912. ;; FIXME: Occasionally check by commenting these, to make sure
  2913. ;; no other functions uses these, forgetting to let-bind them.
  2914. (defvar entry)
  2915. (defvar state)
  2916. (defvar last-state)
  2917. (defvar date)
  2918. (defvar description)
  2919. ;; Defined somewhere in this file, but used before definition.
  2920. (defvar org-html-entities)
  2921. (defvar org-struct-menu)
  2922. (defvar org-org-menu)
  2923. (defvar org-tbl-menu)
  2924. (defvar org-agenda-keymap)
  2925. ;;;; Define the Org-mode
  2926. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  2927. (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."))
  2928. ;; We use a before-change function to check if a table might need
  2929. ;; an update.
  2930. (defvar org-table-may-need-update t
  2931. "Indicates that a table might need an update.
  2932. This variable is set by `org-before-change-function'.
  2933. `org-table-align' sets it back to nil.")
  2934. (defun org-before-change-function (beg end)
  2935. "Every change indicates that a table might need an update."
  2936. (setq org-table-may-need-update t))
  2937. (defvar org-mode-map)
  2938. (defvar org-mode-hook nil)
  2939. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  2940. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  2941. (defvar org-table-buffer-is-an nil)
  2942. (defconst org-outline-regexp "\\*+ ")
  2943. ;;;###autoload
  2944. (define-derived-mode org-mode outline-mode "Org"
  2945. "Outline-based notes management and organizer, alias
  2946. \"Carsten's outline-mode for keeping track of everything.\"
  2947. Org-mode develops organizational tasks around a NOTES file which
  2948. contains information about projects as plain text. Org-mode is
  2949. implemented on top of outline-mode, which is ideal to keep the content
  2950. of large files well structured. It supports ToDo items, deadlines and
  2951. time stamps, which magically appear in the diary listing of the Emacs
  2952. calendar. Tables are easily created with a built-in table editor.
  2953. Plain text URL-like links connect to websites, emails (VM), Usenet
  2954. messages (Gnus), BBDB entries, and any files related to the project.
  2955. For printing and sharing of notes, an Org-mode file (or a part of it)
  2956. can be exported as a structured ASCII or HTML file.
  2957. The following commands are available:
  2958. \\{org-mode-map}"
  2959. ;; Get rid of Outline menus, they are not needed
  2960. ;; Need to do this here because define-derived-mode sets up
  2961. ;; the keymap so late. Still, it is a waste to call this each time
  2962. ;; we switch another buffer into org-mode.
  2963. (if (featurep 'xemacs)
  2964. (when (boundp 'outline-mode-menu-heading)
  2965. ;; Assume this is Greg's port, it used easymenu
  2966. (easy-menu-remove outline-mode-menu-heading)
  2967. (easy-menu-remove outline-mode-menu-show)
  2968. (easy-menu-remove outline-mode-menu-hide))
  2969. (define-key org-mode-map [menu-bar headings] 'undefined)
  2970. (define-key org-mode-map [menu-bar hide] 'undefined)
  2971. (define-key org-mode-map [menu-bar show] 'undefined))
  2972. (org-load-modules-maybe)
  2973. (easy-menu-add org-org-menu)
  2974. (easy-menu-add org-tbl-menu)
  2975. (org-install-agenda-files-menu)
  2976. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  2977. (org-add-to-invisibility-spec '(org-cwidth))
  2978. (when (featurep 'xemacs)
  2979. (org-set-local 'line-move-ignore-invisible t))
  2980. (org-set-local 'outline-regexp org-outline-regexp)
  2981. (org-set-local 'outline-level 'org-outline-level)
  2982. (when (and org-ellipsis
  2983. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  2984. (fboundp 'make-glyph-code))
  2985. (unless org-display-table
  2986. (setq org-display-table (make-display-table)))
  2987. (set-display-table-slot
  2988. org-display-table 4
  2989. (vconcat (mapcar
  2990. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  2991. org-ellipsis)))
  2992. (if (stringp org-ellipsis) org-ellipsis "..."))))
  2993. (setq buffer-display-table org-display-table))
  2994. (org-set-regexps-and-options)
  2995. ;; Calc embedded
  2996. (org-set-local 'calc-embedded-open-mode "# ")
  2997. (modify-syntax-entry ?# "<")
  2998. (modify-syntax-entry ?@ "w")
  2999. (if org-startup-truncated (setq truncate-lines t))
  3000. (org-set-local 'font-lock-unfontify-region-function
  3001. 'org-unfontify-region)
  3002. ;; Activate before-change-function
  3003. (org-set-local 'org-table-may-need-update t)
  3004. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3005. 'local)
  3006. ;; Check for running clock before killing a buffer
  3007. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3008. ;; Paragraphs and auto-filling
  3009. (org-set-autofill-regexps)
  3010. (setq indent-line-function 'org-indent-line-function)
  3011. (org-update-radio-target-regexp)
  3012. ;; Comment characters
  3013. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3014. (org-set-local 'comment-padding " ")
  3015. ;; Align options lines
  3016. (org-set-local
  3017. 'align-mode-rules-list
  3018. '((org-in-buffer-settings
  3019. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3020. (modes . '(org-mode)))))
  3021. ;; Imenu
  3022. (org-set-local 'imenu-create-index-function
  3023. 'org-imenu-get-tree)
  3024. ;; Make isearch reveal context
  3025. (if (or (featurep 'xemacs)
  3026. (not (boundp 'outline-isearch-open-invisible-function)))
  3027. ;; Emacs 21 and XEmacs make use of the hook
  3028. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3029. ;; Emacs 22 deals with this through a special variable
  3030. (org-set-local 'outline-isearch-open-invisible-function
  3031. (lambda (&rest ignore) (org-show-context 'isearch))))
  3032. ;; If empty file that did not turn on org-mode automatically, make it to.
  3033. (if (and org-insert-mode-line-in-empty-file
  3034. (interactive-p)
  3035. (= (point-min) (point-max)))
  3036. (insert "# -*- mode: org -*-\n\n"))
  3037. (unless org-inhibit-startup
  3038. (when org-startup-align-all-tables
  3039. (let ((bmp (buffer-modified-p)))
  3040. (org-table-map-tables 'org-table-align)
  3041. (set-buffer-modified-p bmp)))
  3042. (org-cycle-hide-drawers 'all)
  3043. (cond
  3044. ((eq org-startup-folded t)
  3045. (org-cycle '(4)))
  3046. ((eq org-startup-folded 'content)
  3047. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  3048. (org-cycle '(4)) (org-cycle '(4)))))))
  3049. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3050. (defun org-current-time ()
  3051. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3052. (if (> (car org-time-stamp-rounding-minutes) 1)
  3053. (let ((r (car org-time-stamp-rounding-minutes))
  3054. (time (decode-time)))
  3055. (apply 'encode-time
  3056. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3057. (nthcdr 2 time))))
  3058. (current-time)))
  3059. ;;;; Font-Lock stuff, including the activators
  3060. (defvar org-mouse-map (make-sparse-keymap))
  3061. (org-defkey org-mouse-map
  3062. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3063. (org-defkey org-mouse-map
  3064. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3065. (when org-mouse-1-follows-link
  3066. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3067. (when org-tab-follows-link
  3068. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3069. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3070. (when org-return-follows-link
  3071. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  3072. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  3073. (require 'font-lock)
  3074. (defconst org-non-link-chars "]\t\n\r<>")
  3075. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3076. "shell" "elisp"))
  3077. (defvar org-link-re-with-space nil
  3078. "Matches a link with spaces, optional angular brackets around it.")
  3079. (defvar org-link-re-with-space2 nil
  3080. "Matches a link with spaces, optional angular brackets around it.")
  3081. (defvar org-angle-link-re nil
  3082. "Matches link with angular brackets, spaces are allowed.")
  3083. (defvar org-plain-link-re nil
  3084. "Matches plain link, without spaces.")
  3085. (defvar org-bracket-link-regexp nil
  3086. "Matches a link in double brackets.")
  3087. (defvar org-bracket-link-analytic-regexp nil
  3088. "Regular expression used to analyze links.
  3089. Here is what the match groups contain after a match:
  3090. 1: http:
  3091. 2: http
  3092. 3: path
  3093. 4: [desc]
  3094. 5: desc")
  3095. (defvar org-any-link-re nil
  3096. "Regular expression matching any link.")
  3097. (defun org-make-link-regexps ()
  3098. "Update the link regular expressions.
  3099. This should be called after the variable `org-link-types' has changed."
  3100. (setq org-link-re-with-space
  3101. (concat
  3102. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3103. "\\([^" org-non-link-chars " ]"
  3104. "[^" org-non-link-chars "]*"
  3105. "[^" org-non-link-chars " ]\\)>?")
  3106. org-link-re-with-space2
  3107. (concat
  3108. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3109. "\\([^" org-non-link-chars " ]"
  3110. "[^]\t\n\r]*"
  3111. "[^" org-non-link-chars " ]\\)>?")
  3112. org-angle-link-re
  3113. (concat
  3114. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3115. "\\([^" org-non-link-chars " ]"
  3116. "[^" org-non-link-chars "]*"
  3117. "\\)>")
  3118. org-plain-link-re
  3119. (concat
  3120. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3121. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3122. org-bracket-link-regexp
  3123. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3124. org-bracket-link-analytic-regexp
  3125. (concat
  3126. "\\[\\["
  3127. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3128. "\\([^]]+\\)"
  3129. "\\]"
  3130. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3131. "\\]")
  3132. org-any-link-re
  3133. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3134. org-angle-link-re "\\)\\|\\("
  3135. org-plain-link-re "\\)")))
  3136. (org-make-link-regexps)
  3137. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3138. "Regular expression for fast time stamp matching.")
  3139. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3140. "Regular expression for fast time stamp matching.")
  3141. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3142. "Regular expression matching time strings for analysis.
  3143. This one does not require the space after the date, so it can be used
  3144. on a string that terminates immediately after the date.")
  3145. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3146. "Regular expression matching time strings for analysis.")
  3147. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3148. "Regular expression matching time stamps, with groups.")
  3149. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3150. "Regular expression matching time stamps (also [..]), with groups.")
  3151. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3152. "Regular expression matching a time stamp range.")
  3153. (defconst org-tr-regexp-both
  3154. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3155. "Regular expression matching a time stamp range.")
  3156. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3157. org-ts-regexp "\\)?")
  3158. "Regular expression matching a time stamp or time stamp range.")
  3159. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3160. org-ts-regexp-both "\\)?")
  3161. "Regular expression matching a time stamp or time stamp range.
  3162. The time stamps may be either active or inactive.")
  3163. (defvar org-emph-face nil)
  3164. (defun org-do-emphasis-faces (limit)
  3165. "Run through the buffer and add overlays to links."
  3166. (let (rtn)
  3167. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3168. (if (not (= (char-after (match-beginning 3))
  3169. (char-after (match-beginning 4))))
  3170. (progn
  3171. (setq rtn t)
  3172. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3173. 'face
  3174. (nth 1 (assoc (match-string 3)
  3175. org-emphasis-alist)))
  3176. (add-text-properties (match-beginning 2) (match-end 2)
  3177. '(font-lock-multiline t))
  3178. (when org-hide-emphasis-markers
  3179. (add-text-properties (match-end 4) (match-beginning 5)
  3180. '(invisible org-link))
  3181. (add-text-properties (match-beginning 3) (match-end 3)
  3182. '(invisible org-link)))))
  3183. (backward-char 1))
  3184. rtn))
  3185. (defun org-emphasize (&optional char)
  3186. "Insert or change an emphasis, i.e. a font like bold or italic.
  3187. If there is an active region, change that region to a new emphasis.
  3188. If there is no region, just insert the marker characters and position
  3189. the cursor between them.
  3190. CHAR should be either the marker character, or the first character of the
  3191. HTML tag associated with that emphasis. If CHAR is a space, the means
  3192. to remove the emphasis of the selected region.
  3193. If char is not given (for example in an interactive call) it
  3194. will be prompted for."
  3195. (interactive)
  3196. (let ((eal org-emphasis-alist) e det
  3197. (erc org-emphasis-regexp-components)
  3198. (prompt "")
  3199. (string "") beg end move tag c s)
  3200. (if (org-region-active-p)
  3201. (setq beg (region-beginning) end (region-end)
  3202. string (buffer-substring beg end))
  3203. (setq move t))
  3204. (while (setq e (pop eal))
  3205. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3206. c (aref tag 0))
  3207. (push (cons c (string-to-char (car e))) det)
  3208. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3209. (substring tag 1)))))
  3210. (unless char
  3211. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3212. (setq char (read-char-exclusive)))
  3213. (setq char (or (cdr (assoc char det)) char))
  3214. (if (equal char ?\ )
  3215. (setq s "" move nil)
  3216. (unless (assoc (char-to-string char) org-emphasis-alist)
  3217. (error "No such emphasis marker: \"%c\"" char))
  3218. (setq s (char-to-string char)))
  3219. (while (and (> (length string) 1)
  3220. (equal (substring string 0 1) (substring string -1))
  3221. (assoc (substring string 0 1) org-emphasis-alist))
  3222. (setq string (substring string 1 -1)))
  3223. (setq string (concat s string s))
  3224. (if beg (delete-region beg end))
  3225. (unless (or (bolp)
  3226. (string-match (concat "[" (nth 0 erc) "\n]")
  3227. (char-to-string (char-before (point)))))
  3228. (insert " "))
  3229. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3230. (char-to-string (char-after (point))))
  3231. (insert " ") (backward-char 1))
  3232. (insert string)
  3233. (and move (backward-char 1))))
  3234. (defconst org-nonsticky-props
  3235. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3236. (defun org-activate-plain-links (limit)
  3237. "Run through the buffer and add overlays to links."
  3238. (catch 'exit
  3239. (let (f)
  3240. (while (re-search-forward org-plain-link-re limit t)
  3241. (setq f (get-text-property (match-beginning 0) 'face))
  3242. (if (or (eq f 'org-tag)
  3243. (and (listp f) (memq 'org-tag f)))
  3244. nil
  3245. (add-text-properties (match-beginning 0) (match-end 0)
  3246. (list 'mouse-face 'highlight
  3247. 'rear-nonsticky org-nonsticky-props
  3248. 'keymap org-mouse-map
  3249. ))
  3250. (throw 'exit t))))))
  3251. (defun org-activate-code (limit)
  3252. (if (re-search-forward "^[ \t]*\\(:.*\\)" limit t)
  3253. (unless (get-text-property (match-beginning 1) 'face)
  3254. (remove-text-properties (match-beginning 0) (match-end 0)
  3255. '(display t invisible t intangible t))
  3256. t)))
  3257. (defun org-activate-angle-links (limit)
  3258. "Run through the buffer and add overlays to links."
  3259. (if (re-search-forward org-angle-link-re limit t)
  3260. (progn
  3261. (add-text-properties (match-beginning 0) (match-end 0)
  3262. (list 'mouse-face 'highlight
  3263. 'rear-nonsticky org-nonsticky-props
  3264. 'keymap org-mouse-map
  3265. ))
  3266. t)))
  3267. (defun org-activate-bracket-links (limit)
  3268. "Run through the buffer and add overlays to bracketed links."
  3269. (if (re-search-forward org-bracket-link-regexp limit t)
  3270. (let* ((help (concat "LINK: "
  3271. (org-match-string-no-properties 1)))
  3272. ;; FIXME: above we should remove the escapes.
  3273. ;; but that requires another match, protecting match data,
  3274. ;; a lot of overhead for font-lock.
  3275. (ip (org-maybe-intangible
  3276. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3277. 'keymap org-mouse-map 'mouse-face 'highlight
  3278. 'font-lock-multiline t 'help-echo help)))
  3279. (vp (list 'rear-nonsticky org-nonsticky-props
  3280. 'keymap org-mouse-map 'mouse-face 'highlight
  3281. ' font-lock-multiline t 'help-echo help)))
  3282. ;; We need to remove the invisible property here. Table narrowing
  3283. ;; may have made some of this invisible.
  3284. (remove-text-properties (match-beginning 0) (match-end 0)
  3285. '(invisible nil))
  3286. (if (match-end 3)
  3287. (progn
  3288. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3289. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3290. (add-text-properties (match-end 3) (match-end 0) ip))
  3291. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3292. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3293. (add-text-properties (match-end 1) (match-end 0) ip))
  3294. t)))
  3295. (defun org-activate-dates (limit)
  3296. "Run through the buffer and add overlays to dates."
  3297. (if (re-search-forward org-tsr-regexp-both limit t)
  3298. (progn
  3299. (add-text-properties (match-beginning 0) (match-end 0)
  3300. (list 'mouse-face 'highlight
  3301. 'rear-nonsticky org-nonsticky-props
  3302. 'keymap org-mouse-map))
  3303. (when org-display-custom-times
  3304. (if (match-end 3)
  3305. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3306. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3307. t)))
  3308. (defvar org-target-link-regexp nil
  3309. "Regular expression matching radio targets in plain text.")
  3310. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3311. "Regular expression matching a link target.")
  3312. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3313. "Regular expression matching a radio target.")
  3314. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3315. "Regular expression matching any target.")
  3316. (defun org-activate-target-links (limit)
  3317. "Run through the buffer and add overlays to target matches."
  3318. (when org-target-link-regexp
  3319. (let ((case-fold-search t))
  3320. (if (re-search-forward org-target-link-regexp limit t)
  3321. (progn
  3322. (add-text-properties (match-beginning 0) (match-end 0)
  3323. (list 'mouse-face 'highlight
  3324. 'rear-nonsticky org-nonsticky-props
  3325. 'keymap org-mouse-map
  3326. 'help-echo "Radio target link"
  3327. 'org-linked-text t))
  3328. t)))))
  3329. (defun org-update-radio-target-regexp ()
  3330. "Find all radio targets in this file and update the regular expression."
  3331. (interactive)
  3332. (when (memq 'radio org-activate-links)
  3333. (setq org-target-link-regexp
  3334. (org-make-target-link-regexp (org-all-targets 'radio)))
  3335. (org-restart-font-lock)))
  3336. (defun org-hide-wide-columns (limit)
  3337. (let (s e)
  3338. (setq s (text-property-any (point) (or limit (point-max))
  3339. 'org-cwidth t))
  3340. (when s
  3341. (setq e (next-single-property-change s 'org-cwidth))
  3342. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3343. (goto-char e)
  3344. t)))
  3345. (defvar org-latex-and-specials-regexp nil
  3346. "Regular expression for highlighting export special stuff.")
  3347. (defvar org-match-substring-regexp)
  3348. (defvar org-match-substring-with-braces-regexp)
  3349. (defvar org-export-html-special-string-regexps)
  3350. (defun org-compute-latex-and-specials-regexp ()
  3351. "Compute regular expression for stuff treated specially by exporters."
  3352. (if (not org-highlight-latex-fragments-and-specials)
  3353. (org-set-local 'org-latex-and-specials-regexp nil)
  3354. (require 'org-exp)
  3355. (let*
  3356. ((matchers (plist-get org-format-latex-options :matchers))
  3357. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3358. org-latex-regexps)))
  3359. (options (org-combine-plists (org-default-export-plist)
  3360. (org-infile-export-plist)))
  3361. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3362. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3363. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3364. (org-export-html-expand (plist-get options :expand-quoted-html))
  3365. (org-export-with-special-strings (plist-get options :special-strings))
  3366. (re-sub
  3367. (cond
  3368. ((equal org-export-with-sub-superscripts '{})
  3369. (list org-match-substring-with-braces-regexp))
  3370. (org-export-with-sub-superscripts
  3371. (list org-match-substring-regexp))
  3372. (t nil)))
  3373. (re-latex
  3374. (if org-export-with-LaTeX-fragments
  3375. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3376. (re-macros
  3377. (if org-export-with-TeX-macros
  3378. (list (concat "\\\\"
  3379. (regexp-opt
  3380. (append (mapcar 'car org-html-entities)
  3381. (if (boundp 'org-latex-entities)
  3382. org-latex-entities nil))
  3383. 'words))) ; FIXME
  3384. ))
  3385. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3386. (re-special (if org-export-with-special-strings
  3387. (mapcar (lambda (x) (car x))
  3388. org-export-html-special-string-regexps)))
  3389. (re-rest
  3390. (delq nil
  3391. (list
  3392. (if org-export-html-expand "@<[^>\n]+>")
  3393. ))))
  3394. (org-set-local
  3395. 'org-latex-and-specials-regexp
  3396. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3397. re-rest) "\\|")))))
  3398. (defface org-latex-and-export-specials
  3399. (let ((font (cond ((assq :inherit custom-face-attributes)
  3400. '(:inherit underline))
  3401. (t '(:underline t)))))
  3402. `((((class grayscale) (background light))
  3403. (:foreground "DimGray" ,@font))
  3404. (((class grayscale) (background dark))
  3405. (:foreground "LightGray" ,@font))
  3406. (((class color) (background light))
  3407. (:foreground "SaddleBrown"))
  3408. (((class color) (background dark))
  3409. (:foreground "burlywood"))
  3410. (t (,@font))))
  3411. "Face used to highlight math latex and other special exporter stuff."
  3412. :group 'org-faces)
  3413. (defun org-do-latex-and-special-faces (limit)
  3414. "Run through the buffer and add overlays to links."
  3415. (when org-latex-and-specials-regexp
  3416. (let (rtn d)
  3417. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3418. limit t))
  3419. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3420. 'face))
  3421. '(org-code org-verbatim underline)))
  3422. (progn
  3423. (setq rtn t
  3424. d (cond ((member (char-after (1+ (match-beginning 0)))
  3425. '(?_ ?^)) 1)
  3426. (t 0)))
  3427. (font-lock-prepend-text-property
  3428. (+ d (match-beginning 0)) (match-end 0)
  3429. 'face 'org-latex-and-export-specials)
  3430. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3431. '(font-lock-multiline t)))))
  3432. rtn)))
  3433. (defun org-restart-font-lock ()
  3434. "Restart font-lock-mode, to force refontification."
  3435. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3436. (font-lock-mode -1)
  3437. (font-lock-mode 1)))
  3438. (defun org-all-targets (&optional radio)
  3439. "Return a list of all targets in this file.
  3440. With optional argument RADIO, only find radio targets."
  3441. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3442. rtn)
  3443. (save-excursion
  3444. (goto-char (point-min))
  3445. (while (re-search-forward re nil t)
  3446. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3447. rtn)))
  3448. (defun org-make-target-link-regexp (targets)
  3449. "Make regular expression matching all strings in TARGETS.
  3450. The regular expression finds the targets also if there is a line break
  3451. between words."
  3452. (and targets
  3453. (concat
  3454. "\\<\\("
  3455. (mapconcat
  3456. (lambda (x)
  3457. (while (string-match " +" x)
  3458. (setq x (replace-match "\\s-+" t t x)))
  3459. x)
  3460. targets
  3461. "\\|")
  3462. "\\)\\>")))
  3463. (defun org-activate-tags (limit)
  3464. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3465. (progn
  3466. (add-text-properties (match-beginning 1) (match-end 1)
  3467. (list 'mouse-face 'highlight
  3468. 'rear-nonsticky org-nonsticky-props
  3469. 'keymap org-mouse-map))
  3470. t)))
  3471. (defun org-outline-level ()
  3472. (save-excursion
  3473. (looking-at outline-regexp)
  3474. (if (match-beginning 1)
  3475. (+ (org-get-string-indentation (match-string 1)) 1000)
  3476. (1- (- (match-end 0) (match-beginning 0))))))
  3477. (defvar org-font-lock-keywords nil)
  3478. (defconst org-property-re (org-re "^[ \t]*\\(:\\([[:alnum:]_]+\\):\\)[ \t]*\\(\\S-.*\\)")
  3479. "Regular expression matching a property line.")
  3480. (defun org-set-font-lock-defaults ()
  3481. (let* ((em org-fontify-emphasized-text)
  3482. (lk org-activate-links)
  3483. (org-font-lock-extra-keywords
  3484. (list
  3485. ;; Headlines
  3486. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3487. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3488. ;; Table lines
  3489. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3490. (1 'org-table t))
  3491. ;; Table internals
  3492. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3493. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3494. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3495. ;; Drawers
  3496. (list org-drawer-regexp '(0 'org-special-keyword t))
  3497. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3498. ;; Properties
  3499. (list org-property-re
  3500. '(1 'org-special-keyword t)
  3501. '(3 'org-property-value t))
  3502. (if org-format-transports-properties-p
  3503. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3504. ;; Links
  3505. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3506. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3507. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3508. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3509. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3510. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3511. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3512. '(org-hide-wide-columns (0 nil append))
  3513. ;; TODO lines
  3514. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3515. '(1 (org-get-todo-face 1) t))
  3516. ;; DONE
  3517. (if org-fontify-done-headline
  3518. (list (concat "^[*]+ +\\<\\("
  3519. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3520. "\\)\\(.*\\)")
  3521. '(2 'org-headline-done t))
  3522. nil)
  3523. ;; Priorities
  3524. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3525. ;; Special keywords
  3526. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3527. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3528. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3529. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3530. ;; Emphasis
  3531. (if em
  3532. (if (featurep 'xemacs)
  3533. '(org-do-emphasis-faces (0 nil append))
  3534. '(org-do-emphasis-faces)))
  3535. ;; Checkboxes
  3536. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3537. 2 'bold prepend)
  3538. (if org-provide-checkbox-statistics
  3539. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3540. (0 (org-get-checkbox-statistics-face) t)))
  3541. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3542. '(1 'org-archived prepend))
  3543. ;; Specials
  3544. '(org-do-latex-and-special-faces)
  3545. ;; Code
  3546. '(org-activate-code (1 'org-code t))
  3547. ;; COMMENT
  3548. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3549. "\\|" org-quote-string "\\)\\>")
  3550. '(1 'org-special-keyword t))
  3551. '("^#.*" (0 'font-lock-comment-face t))
  3552. )))
  3553. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3554. ;; Now set the full font-lock-keywords
  3555. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3556. (org-set-local 'font-lock-defaults
  3557. '(org-font-lock-keywords t nil nil backward-paragraph))
  3558. (kill-local-variable 'font-lock-keywords) nil))
  3559. (defvar org-m nil)
  3560. (defvar org-l nil)
  3561. (defvar org-f nil)
  3562. (defun org-get-level-face (n)
  3563. "Get the right face for match N in font-lock matching of healdines."
  3564. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3565. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3566. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3567. (cond
  3568. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3569. ((eq n 2) org-f)
  3570. (t (if org-level-color-stars-only nil org-f))))
  3571. (defun org-get-todo-face (kwd)
  3572. "Get the right face for a TODO keyword KWD.
  3573. If KWD is a number, get the corresponding match group."
  3574. (if (numberp kwd) (setq kwd (match-string kwd)))
  3575. (or (cdr (assoc kwd org-todo-keyword-faces))
  3576. (and (member kwd org-done-keywords) 'org-done)
  3577. 'org-todo))
  3578. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3579. "Remove fontification and activation overlays from links."
  3580. (font-lock-default-unfontify-region beg end)
  3581. (let* ((buffer-undo-list t)
  3582. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3583. (inhibit-modification-hooks t)
  3584. deactivate-mark buffer-file-name buffer-file-truename)
  3585. (remove-text-properties beg end
  3586. '(mouse-face t keymap t org-linked-text t
  3587. invisible t intangible t))))
  3588. ;;;; Visibility cycling, including org-goto and indirect buffer
  3589. ;;; Cycling
  3590. (defvar org-cycle-global-status nil)
  3591. (make-variable-buffer-local 'org-cycle-global-status)
  3592. (defvar org-cycle-subtree-status nil)
  3593. (make-variable-buffer-local 'org-cycle-subtree-status)
  3594. ;;;###autoload
  3595. (defun org-cycle (&optional arg)
  3596. "Visibility cycling for Org-mode.
  3597. - When this function is called with a prefix argument, rotate the entire
  3598. buffer through 3 states (global cycling)
  3599. 1. OVERVIEW: Show only top-level headlines.
  3600. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3601. 3. SHOW ALL: Show everything.
  3602. - When point is at the beginning of a headline, rotate the subtree started
  3603. by this line through 3 different states (local cycling)
  3604. 1. FOLDED: Only the main headline is shown.
  3605. 2. CHILDREN: The main headline and the direct children are shown.
  3606. From this state, you can move to one of the children
  3607. and zoom in further.
  3608. 3. SUBTREE: Show the entire subtree, including body text.
  3609. - When there is a numeric prefix, go up to a heading with level ARG, do
  3610. a `show-subtree' and return to the previous cursor position. If ARG
  3611. is negative, go up that many levels.
  3612. - When point is not at the beginning of a headline, execute
  3613. `indent-relative', like TAB normally does. See the option
  3614. `org-cycle-emulate-tab' for details.
  3615. - Special case: if point is at the beginning of the buffer and there is
  3616. no headline in line 1, this function will act as if called with prefix arg.
  3617. But only if also the variable `org-cycle-global-at-bob' is t."
  3618. (interactive "P")
  3619. (org-load-modules-maybe)
  3620. (let* ((outline-regexp
  3621. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3622. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3623. outline-regexp))
  3624. (bob-special (and org-cycle-global-at-bob (bobp)
  3625. (not (looking-at outline-regexp))))
  3626. (org-cycle-hook
  3627. (if bob-special
  3628. (delq 'org-optimize-window-after-visibility-change
  3629. (copy-sequence org-cycle-hook))
  3630. org-cycle-hook))
  3631. (pos (point)))
  3632. (if (or bob-special (equal arg '(4)))
  3633. ;; special case: use global cycling
  3634. (setq arg t))
  3635. (cond
  3636. ((org-at-table-p 'any)
  3637. ;; Enter the table or move to the next field in the table
  3638. (or (org-table-recognize-table.el)
  3639. (progn
  3640. (if arg (org-table-edit-field t)
  3641. (org-table-justify-field-maybe)
  3642. (call-interactively 'org-table-next-field)))))
  3643. ((eq arg t) ;; Global cycling
  3644. (cond
  3645. ((and (eq last-command this-command)
  3646. (eq org-cycle-global-status 'overview))
  3647. ;; We just created the overview - now do table of contents
  3648. ;; This can be slow in very large buffers, so indicate action
  3649. (message "CONTENTS...")
  3650. (org-content)
  3651. (message "CONTENTS...done")
  3652. (setq org-cycle-global-status 'contents)
  3653. (run-hook-with-args 'org-cycle-hook 'contents))
  3654. ((and (eq last-command this-command)
  3655. (eq org-cycle-global-status 'contents))
  3656. ;; We just showed the table of contents - now show everything
  3657. (show-all)
  3658. (message "SHOW ALL")
  3659. (setq org-cycle-global-status 'all)
  3660. (run-hook-with-args 'org-cycle-hook 'all))
  3661. (t
  3662. ;; Default action: go to overview
  3663. (org-overview)
  3664. (message "OVERVIEW")
  3665. (setq org-cycle-global-status 'overview)
  3666. (run-hook-with-args 'org-cycle-hook 'overview))))
  3667. ((and org-drawers org-drawer-regexp
  3668. (save-excursion
  3669. (beginning-of-line 1)
  3670. (looking-at org-drawer-regexp)))
  3671. ;; Toggle block visibility
  3672. (org-flag-drawer
  3673. (not (get-char-property (match-end 0) 'invisible))))
  3674. ((integerp arg)
  3675. ;; Show-subtree, ARG levels up from here.
  3676. (save-excursion
  3677. (org-back-to-heading)
  3678. (outline-up-heading (if (< arg 0) (- arg)
  3679. (- (funcall outline-level) arg)))
  3680. (org-show-subtree)))
  3681. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3682. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3683. ;; At a heading: rotate between three different views
  3684. (org-back-to-heading)
  3685. (let ((goal-column 0) eoh eol eos)
  3686. ;; First, some boundaries
  3687. (save-excursion
  3688. (org-back-to-heading)
  3689. (save-excursion
  3690. (beginning-of-line 2)
  3691. (while (and (not (eobp)) ;; this is like `next-line'
  3692. (get-char-property (1- (point)) 'invisible))
  3693. (beginning-of-line 2)) (setq eol (point)))
  3694. (outline-end-of-heading) (setq eoh (point))
  3695. (org-end-of-subtree t)
  3696. (unless (eobp)
  3697. (skip-chars-forward " \t\n")
  3698. (beginning-of-line 1) ; in case this is an item
  3699. )
  3700. (setq eos (1- (point))))
  3701. ;; Find out what to do next and set `this-command'
  3702. (cond
  3703. ((= eos eoh)
  3704. ;; Nothing is hidden behind this heading
  3705. (message "EMPTY ENTRY")
  3706. (setq org-cycle-subtree-status nil)
  3707. (save-excursion
  3708. (goto-char eos)
  3709. (outline-next-heading)
  3710. (if (org-invisible-p) (org-flag-heading nil))))
  3711. ((or (>= eol eos)
  3712. (not (string-match "\\S-" (buffer-substring eol eos))))
  3713. ;; Entire subtree is hidden in one line: open it
  3714. (org-show-entry)
  3715. (show-children)
  3716. (message "CHILDREN")
  3717. (save-excursion
  3718. (goto-char eos)
  3719. (outline-next-heading)
  3720. (if (org-invisible-p) (org-flag-heading nil)))
  3721. (setq org-cycle-subtree-status 'children)
  3722. (run-hook-with-args 'org-cycle-hook 'children))
  3723. ((and (eq last-command this-command)
  3724. (eq org-cycle-subtree-status 'children))
  3725. ;; We just showed the children, now show everything.
  3726. (org-show-subtree)
  3727. (message "SUBTREE")
  3728. (setq org-cycle-subtree-status 'subtree)
  3729. (run-hook-with-args 'org-cycle-hook 'subtree))
  3730. (t
  3731. ;; Default action: hide the subtree.
  3732. (hide-subtree)
  3733. (message "FOLDED")
  3734. (setq org-cycle-subtree-status 'folded)
  3735. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3736. ;; TAB emulation
  3737. (buffer-read-only (org-back-to-heading))
  3738. ((org-try-cdlatex-tab))
  3739. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  3740. (or (not (bolp))
  3741. (not (looking-at outline-regexp))))
  3742. (call-interactively (global-key-binding "\t")))
  3743. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3744. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3745. (or (and (eq org-cycle-emulate-tab 'white)
  3746. (= (match-end 0) (point-at-eol)))
  3747. (and (eq org-cycle-emulate-tab 'whitestart)
  3748. (>= (match-end 0) pos))))
  3749. t
  3750. (eq org-cycle-emulate-tab t))
  3751. ; (if (and (looking-at "[ \n\r\t]")
  3752. ; (string-match "^[ \t]*$" (buffer-substring
  3753. ; (point-at-bol) (point))))
  3754. ; (progn
  3755. ; (beginning-of-line 1)
  3756. ; (and (looking-at "[ \t]+") (replace-match ""))))
  3757. (call-interactively (global-key-binding "\t")))
  3758. (t (save-excursion
  3759. (org-back-to-heading)
  3760. (org-cycle))))))
  3761. ;;;###autoload
  3762. (defun org-global-cycle (&optional arg)
  3763. "Cycle the global visibility. For details see `org-cycle'."
  3764. (interactive "P")
  3765. (let ((org-cycle-include-plain-lists
  3766. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  3767. (if (integerp arg)
  3768. (progn
  3769. (show-all)
  3770. (hide-sublevels arg)
  3771. (setq org-cycle-global-status 'contents))
  3772. (org-cycle '(4)))))
  3773. (defun org-overview ()
  3774. "Switch to overview mode, shoing only top-level headlines.
  3775. Really, this shows all headlines with level equal or greater than the level
  3776. of the first headline in the buffer. This is important, because if the
  3777. first headline is not level one, then (hide-sublevels 1) gives confusing
  3778. results."
  3779. (interactive)
  3780. (let ((level (save-excursion
  3781. (goto-char (point-min))
  3782. (if (re-search-forward (concat "^" outline-regexp) nil t)
  3783. (progn
  3784. (goto-char (match-beginning 0))
  3785. (funcall outline-level))))))
  3786. (and level (hide-sublevels level))))
  3787. (defun org-content (&optional arg)
  3788. "Show all headlines in the buffer, like a table of contents.
  3789. With numerical argument N, show content up to level N."
  3790. (interactive "P")
  3791. (save-excursion
  3792. ;; Visit all headings and show their offspring
  3793. (and (integerp arg) (org-overview))
  3794. (goto-char (point-max))
  3795. (catch 'exit
  3796. (while (and (progn (condition-case nil
  3797. (outline-previous-visible-heading 1)
  3798. (error (goto-char (point-min))))
  3799. t)
  3800. (looking-at outline-regexp))
  3801. (if (integerp arg)
  3802. (show-children (1- arg))
  3803. (show-branches))
  3804. (if (bobp) (throw 'exit nil))))))
  3805. (defun org-optimize-window-after-visibility-change (state)
  3806. "Adjust the window after a change in outline visibility.
  3807. This function is the default value of the hook `org-cycle-hook'."
  3808. (when (get-buffer-window (current-buffer))
  3809. (cond
  3810. ; ((eq state 'overview) (org-first-headline-recenter 1))
  3811. ; ((eq state 'overview) (org-beginning-of-line))
  3812. ((eq state 'content) nil)
  3813. ((eq state 'all) nil)
  3814. ((eq state 'folded) nil)
  3815. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  3816. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  3817. (defun org-compact-display-after-subtree-move ()
  3818. (let (beg end)
  3819. (save-excursion
  3820. (if (org-up-heading-safe)
  3821. (progn
  3822. (hide-subtree)
  3823. (show-entry)
  3824. (show-children)
  3825. (org-cycle-show-empty-lines 'children)
  3826. (org-cycle-hide-drawers 'children))
  3827. (org-overview)))))
  3828. (defun org-cycle-show-empty-lines (state)
  3829. "Show empty lines above all visible headlines.
  3830. The region to be covered depends on STATE when called through
  3831. `org-cycle-hook'. Lisp program can use t for STATE to get the
  3832. entire buffer covered. Note that an empty line is only shown if there
  3833. are at least `org-cycle-separator-lines' empty lines before the headeline."
  3834. (when (> org-cycle-separator-lines 0)
  3835. (save-excursion
  3836. (let* ((n org-cycle-separator-lines)
  3837. (re (cond
  3838. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  3839. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  3840. (t (let ((ns (number-to-string (- n 2))))
  3841. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  3842. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  3843. beg end)
  3844. (cond
  3845. ((memq state '(overview contents t))
  3846. (setq beg (point-min) end (point-max)))
  3847. ((memq state '(children folded))
  3848. (setq beg (point) end (progn (org-end-of-subtree t t)
  3849. (beginning-of-line 2)
  3850. (point)))))
  3851. (when beg
  3852. (goto-char beg)
  3853. (while (re-search-forward re end t)
  3854. (if (not (get-char-property (match-end 1) 'invisible))
  3855. (outline-flag-region
  3856. (match-beginning 1) (match-end 1) nil)))))))
  3857. ;; Never hide empty lines at the end of the file.
  3858. (save-excursion
  3859. (goto-char (point-max))
  3860. (outline-previous-heading)
  3861. (outline-end-of-heading)
  3862. (if (and (looking-at "[ \t\n]+")
  3863. (= (match-end 0) (point-max)))
  3864. (outline-flag-region (point) (match-end 0) nil))))
  3865. (defun org-subtree-end-visible-p ()
  3866. "Is the end of the current subtree visible?"
  3867. (pos-visible-in-window-p
  3868. (save-excursion (org-end-of-subtree t) (point))))
  3869. (defun org-first-headline-recenter (&optional N)
  3870. "Move cursor to the first headline and recenter the headline.
  3871. Optional argument N means, put the headline into the Nth line of the window."
  3872. (goto-char (point-min))
  3873. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  3874. (beginning-of-line)
  3875. (recenter (prefix-numeric-value N))))
  3876. ;;; Org-goto
  3877. (defvar org-goto-window-configuration nil)
  3878. (defvar org-goto-marker nil)
  3879. (defvar org-goto-map
  3880. (let ((map (make-sparse-keymap)))
  3881. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  3882. (while (setq cmd (pop cmds))
  3883. (substitute-key-definition cmd cmd map global-map)))
  3884. (suppress-keymap map)
  3885. (org-defkey map "\C-m" 'org-goto-ret)
  3886. (org-defkey map [(return)] 'org-goto-ret)
  3887. (org-defkey map [(left)] 'org-goto-left)
  3888. (org-defkey map [(right)] 'org-goto-right)
  3889. (org-defkey map [(control ?g)] 'org-goto-quit)
  3890. (org-defkey map "\C-i" 'org-cycle)
  3891. (org-defkey map [(tab)] 'org-cycle)
  3892. (org-defkey map [(down)] 'outline-next-visible-heading)
  3893. (org-defkey map [(up)] 'outline-previous-visible-heading)
  3894. (if org-goto-auto-isearch
  3895. (if (fboundp 'define-key-after)
  3896. (define-key-after map [t] 'org-goto-local-auto-isearch)
  3897. nil)
  3898. (org-defkey map "q" 'org-goto-quit)
  3899. (org-defkey map "n" 'outline-next-visible-heading)
  3900. (org-defkey map "p" 'outline-previous-visible-heading)
  3901. (org-defkey map "f" 'outline-forward-same-level)
  3902. (org-defkey map "b" 'outline-backward-same-level)
  3903. (org-defkey map "u" 'outline-up-heading))
  3904. (org-defkey map "/" 'org-occur)
  3905. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  3906. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  3907. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  3908. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  3909. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  3910. map))
  3911. (defconst org-goto-help
  3912. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  3913. RET=jump to location [Q]uit and return to previous location
  3914. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  3915. (defvar org-goto-start-pos) ; dynamically scoped parameter
  3916. (defun org-goto (&optional alternative-interface)
  3917. "Look up a different location in the current file, keeping current visibility.
  3918. When you want look-up or go to a different location in a document, the
  3919. fastest way is often to fold the entire buffer and then dive into the tree.
  3920. This method has the disadvantage, that the previous location will be folded,
  3921. which may not be what you want.
  3922. This command works around this by showing a copy of the current buffer
  3923. in an indirect buffer, in overview mode. You can dive into the tree in
  3924. that copy, use org-occur and incremental search to find a location.
  3925. When pressing RET or `Q', the command returns to the original buffer in
  3926. which the visibility is still unchanged. After RET is will also jump to
  3927. the location selected in the indirect buffer and expose the
  3928. the headline hierarchy above."
  3929. (interactive "P")
  3930. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  3931. (org-refile-use-outline-path t)
  3932. (interface
  3933. (if (not alternative-interface)
  3934. org-goto-interface
  3935. (if (eq org-goto-interface 'outline)
  3936. 'outline-path-completion
  3937. 'outline)))
  3938. (org-goto-start-pos (point))
  3939. (selected-point
  3940. (if (eq interface 'outline)
  3941. (car (org-get-location (current-buffer) org-goto-help))
  3942. (nth 3 (org-refile-get-location "Goto: ")))))
  3943. (if selected-point
  3944. (progn
  3945. (org-mark-ring-push org-goto-start-pos)
  3946. (goto-char selected-point)
  3947. (if (or (org-invisible-p) (org-invisible-p2))
  3948. (org-show-context 'org-goto)))
  3949. (message "Quit"))))
  3950. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  3951. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  3952. (defvar org-goto-local-auto-isearch-map) ; defined below
  3953. (defun org-get-location (buf help)
  3954. "Let the user select a location in the Org-mode buffer BUF.
  3955. This function uses a recursive edit. It returns the selected position
  3956. or nil."
  3957. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  3958. (isearch-hide-immediately nil)
  3959. (isearch-search-fun-function
  3960. (lambda () 'org-goto-local-search-forward-headings))
  3961. (org-goto-selected-point org-goto-exit-command))
  3962. (save-excursion
  3963. (save-window-excursion
  3964. (delete-other-windows)
  3965. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  3966. (switch-to-buffer
  3967. (condition-case nil
  3968. (make-indirect-buffer (current-buffer) "*org-goto*")
  3969. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  3970. (with-output-to-temp-buffer "*Help*"
  3971. (princ help))
  3972. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  3973. (setq buffer-read-only nil)
  3974. (let ((org-startup-truncated t)
  3975. (org-startup-folded nil)
  3976. (org-startup-align-all-tables nil))
  3977. (org-mode)
  3978. (org-overview))
  3979. (setq buffer-read-only t)
  3980. (if (and (boundp 'org-goto-start-pos)
  3981. (integer-or-marker-p org-goto-start-pos))
  3982. (let ((org-show-hierarchy-above t)
  3983. (org-show-siblings t)
  3984. (org-show-following-heading t))
  3985. (goto-char org-goto-start-pos)
  3986. (and (org-invisible-p) (org-show-context)))
  3987. (goto-char (point-min)))
  3988. (org-beginning-of-line)
  3989. (message "Select location and press RET")
  3990. (use-local-map org-goto-map)
  3991. (recursive-edit)
  3992. ))
  3993. (kill-buffer "*org-goto*")
  3994. (cons org-goto-selected-point org-goto-exit-command)))
  3995. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  3996. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  3997. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  3998. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  3999. (defun org-goto-local-search-forward-headings (string bound noerror)
  4000. "Search and make sure that anu matches are in headlines."
  4001. (catch 'return
  4002. (while (search-forward string bound noerror)
  4003. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4004. (and (member :headline context)
  4005. (not (member :tags context))))
  4006. (throw 'return (point))))))
  4007. (defun org-goto-local-auto-isearch ()
  4008. "Start isearch."
  4009. (interactive)
  4010. (goto-char (point-min))
  4011. (let ((keys (this-command-keys)))
  4012. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4013. (isearch-mode t)
  4014. (isearch-process-search-char (string-to-char keys)))))
  4015. (defun org-goto-ret (&optional arg)
  4016. "Finish `org-goto' by going to the new location."
  4017. (interactive "P")
  4018. (setq org-goto-selected-point (point)
  4019. org-goto-exit-command 'return)
  4020. (throw 'exit nil))
  4021. (defun org-goto-left ()
  4022. "Finish `org-goto' by going to the new location."
  4023. (interactive)
  4024. (if (org-on-heading-p)
  4025. (progn
  4026. (beginning-of-line 1)
  4027. (setq org-goto-selected-point (point)
  4028. org-goto-exit-command 'left)
  4029. (throw 'exit nil))
  4030. (error "Not on a heading")))
  4031. (defun org-goto-right ()
  4032. "Finish `org-goto' by going to the new location."
  4033. (interactive)
  4034. (if (org-on-heading-p)
  4035. (progn
  4036. (setq org-goto-selected-point (point)
  4037. org-goto-exit-command 'right)
  4038. (throw 'exit nil))
  4039. (error "Not on a heading")))
  4040. (defun org-goto-quit ()
  4041. "Finish `org-goto' without cursor motion."
  4042. (interactive)
  4043. (setq org-goto-selected-point nil)
  4044. (setq org-goto-exit-command 'quit)
  4045. (throw 'exit nil))
  4046. ;;; Indirect buffer display of subtrees
  4047. (defvar org-indirect-dedicated-frame nil
  4048. "This is the frame being used for indirect tree display.")
  4049. (defvar org-last-indirect-buffer nil)
  4050. (defun org-tree-to-indirect-buffer (&optional arg)
  4051. "Create indirect buffer and narrow it to current subtree.
  4052. With numerical prefix ARG, go up to this level and then take that tree.
  4053. If ARG is negative, go up that many levels.
  4054. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4055. indirect buffer previously made with this command, to avoid proliferation of
  4056. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4057. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4058. is kept so that you can work with several indirect buffers at the same time.
  4059. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4060. requests that a new frame be made for the new buffer, so that the dedicated
  4061. frame is not changed."
  4062. (interactive "P")
  4063. (let ((cbuf (current-buffer))
  4064. (cwin (selected-window))
  4065. (pos (point))
  4066. beg end level heading ibuf)
  4067. (save-excursion
  4068. (org-back-to-heading t)
  4069. (when (numberp arg)
  4070. (setq level (org-outline-level))
  4071. (if (< arg 0) (setq arg (+ level arg)))
  4072. (while (> (setq level (org-outline-level)) arg)
  4073. (outline-up-heading 1 t)))
  4074. (setq beg (point)
  4075. heading (org-get-heading))
  4076. (org-end-of-subtree t) (setq end (point)))
  4077. (if (and (buffer-live-p org-last-indirect-buffer)
  4078. (not (eq org-indirect-buffer-display 'new-frame))
  4079. (not arg))
  4080. (kill-buffer org-last-indirect-buffer))
  4081. (setq ibuf (org-get-indirect-buffer cbuf)
  4082. org-last-indirect-buffer ibuf)
  4083. (cond
  4084. ((or (eq org-indirect-buffer-display 'new-frame)
  4085. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4086. (select-frame (make-frame))
  4087. (delete-other-windows)
  4088. (switch-to-buffer ibuf)
  4089. (org-set-frame-title heading))
  4090. ((eq org-indirect-buffer-display 'dedicated-frame)
  4091. (raise-frame
  4092. (select-frame (or (and org-indirect-dedicated-frame
  4093. (frame-live-p org-indirect-dedicated-frame)
  4094. org-indirect-dedicated-frame)
  4095. (setq org-indirect-dedicated-frame (make-frame)))))
  4096. (delete-other-windows)
  4097. (switch-to-buffer ibuf)
  4098. (org-set-frame-title (concat "Indirect: " heading)))
  4099. ((eq org-indirect-buffer-display 'current-window)
  4100. (switch-to-buffer ibuf))
  4101. ((eq org-indirect-buffer-display 'other-window)
  4102. (pop-to-buffer ibuf))
  4103. (t (error "Invalid value.")))
  4104. (if (featurep 'xemacs)
  4105. (save-excursion (org-mode) (turn-on-font-lock)))
  4106. (narrow-to-region beg end)
  4107. (show-all)
  4108. (goto-char pos)
  4109. (and (window-live-p cwin) (select-window cwin))))
  4110. (defun org-get-indirect-buffer (&optional buffer)
  4111. (setq buffer (or buffer (current-buffer)))
  4112. (let ((n 1) (base (buffer-name buffer)) bname)
  4113. (while (buffer-live-p
  4114. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4115. (setq n (1+ n)))
  4116. (condition-case nil
  4117. (make-indirect-buffer buffer bname 'clone)
  4118. (error (make-indirect-buffer buffer bname)))))
  4119. (defun org-set-frame-title (title)
  4120. "Set the title of the current frame to the string TITLE."
  4121. ;; FIXME: how to name a single frame in XEmacs???
  4122. (unless (featurep 'xemacs)
  4123. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4124. ;;;; Structure editing
  4125. ;;; Inserting headlines
  4126. (defun org-insert-heading (&optional force-heading)
  4127. "Insert a new heading or item with same depth at point.
  4128. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4129. If point is at the beginning of a headline, insert a sibling before the
  4130. current headline. If point is not at the beginning, do not split the line,
  4131. but create the new hedline after the current line."
  4132. (interactive "P")
  4133. (if (= (buffer-size) 0)
  4134. (insert "\n* ")
  4135. (when (or force-heading (not (org-insert-item)))
  4136. (let* ((head (save-excursion
  4137. (condition-case nil
  4138. (progn
  4139. (org-back-to-heading)
  4140. (match-string 0))
  4141. (error "*"))))
  4142. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  4143. pos)
  4144. (cond
  4145. ((and (org-on-heading-p) (bolp)
  4146. (or (bobp)
  4147. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4148. ;; insert before the current line
  4149. (open-line (if blank 2 1)))
  4150. ((and (bolp)
  4151. (or (bobp)
  4152. (save-excursion
  4153. (backward-char 1) (not (org-invisible-p)))))
  4154. ;; insert right here
  4155. nil)
  4156. (t
  4157. ; ;; in the middle of the line
  4158. ; (org-show-entry)
  4159. ; (if (org-get-alist-option org-M-RET-may-split-line 'headline)
  4160. ; (if (and
  4161. ; (org-on-heading-p)
  4162. ; (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \r\n]"))
  4163. ; ;; protect the tags
  4164. ;; (let ((tags (match-string 2)) pos)
  4165. ; (delete-region (match-beginning 1) (match-end 1))
  4166. ; (setq pos (point-at-bol))
  4167. ; (newline (if blank 2 1))
  4168. ; (save-excursion
  4169. ; (goto-char pos)
  4170. ; (end-of-line 1)
  4171. ; (insert " " tags)
  4172. ; (org-set-tags nil 'align)))
  4173. ; (newline (if blank 2 1)))
  4174. ; (newline (if blank 2 1))))
  4175. ;; in the middle of the line
  4176. (org-show-entry)
  4177. (let ((split
  4178. (org-get-alist-option org-M-RET-may-split-line 'headline))
  4179. tags pos)
  4180. (if (org-on-heading-p)
  4181. (progn
  4182. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4183. (setq tags (and (match-end 2) (match-string 2)))
  4184. (and (match-end 1)
  4185. (delete-region (match-beginning 1) (match-end 1)))
  4186. (setq pos (point-at-bol))
  4187. (or split (end-of-line 1))
  4188. (delete-horizontal-space)
  4189. (newline (if blank 2 1))
  4190. (when tags
  4191. (save-excursion
  4192. (goto-char pos)
  4193. (end-of-line 1)
  4194. (insert " " tags)
  4195. (org-set-tags nil 'align))))
  4196. (or split (end-of-line 1))
  4197. (newline (if blank 2 1))))))
  4198. (insert head) (just-one-space)
  4199. (setq pos (point))
  4200. (end-of-line 1)
  4201. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4202. (run-hooks 'org-insert-heading-hook)))))
  4203. (defun org-insert-heading-after-current ()
  4204. "Insert a new heading with same level as current, after current subtree."
  4205. (interactive)
  4206. (org-back-to-heading)
  4207. (org-insert-heading)
  4208. (org-move-subtree-down)
  4209. (end-of-line 1))
  4210. (defun org-insert-todo-heading (arg)
  4211. "Insert a new heading with the same level and TODO state as current heading.
  4212. If the heading has no TODO state, or if the state is DONE, use the first
  4213. state (TODO by default). Also with prefix arg, force first state."
  4214. (interactive "P")
  4215. (when (not (org-insert-item 'checkbox))
  4216. (org-insert-heading)
  4217. (save-excursion
  4218. (org-back-to-heading)
  4219. (outline-previous-heading)
  4220. (looking-at org-todo-line-regexp))
  4221. (if (or arg
  4222. (not (match-beginning 2))
  4223. (member (match-string 2) org-done-keywords))
  4224. (insert (car org-todo-keywords-1) " ")
  4225. (insert (match-string 2) " "))))
  4226. (defun org-insert-subheading (arg)
  4227. "Insert a new subheading and demote it.
  4228. Works for outline headings and for plain lists alike."
  4229. (interactive "P")
  4230. (org-insert-heading arg)
  4231. (cond
  4232. ((org-on-heading-p) (org-do-demote))
  4233. ((org-at-item-p) (org-indent-item 1))))
  4234. (defun org-insert-todo-subheading (arg)
  4235. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4236. Works for outline headings and for plain lists alike."
  4237. (interactive "P")
  4238. (org-insert-todo-heading arg)
  4239. (cond
  4240. ((org-on-heading-p) (org-do-demote))
  4241. ((org-at-item-p) (org-indent-item 1))))
  4242. ;;; Promotion and Demotion
  4243. (defun org-promote-subtree ()
  4244. "Promote the entire subtree.
  4245. See also `org-promote'."
  4246. (interactive)
  4247. (save-excursion
  4248. (org-map-tree 'org-promote))
  4249. (org-fix-position-after-promote))
  4250. (defun org-demote-subtree ()
  4251. "Demote the entire subtree. See `org-demote'.
  4252. See also `org-promote'."
  4253. (interactive)
  4254. (save-excursion
  4255. (org-map-tree 'org-demote))
  4256. (org-fix-position-after-promote))
  4257. (defun org-do-promote ()
  4258. "Promote the current heading higher up the tree.
  4259. If the region is active in `transient-mark-mode', promote all headings
  4260. in the region."
  4261. (interactive)
  4262. (save-excursion
  4263. (if (org-region-active-p)
  4264. (org-map-region 'org-promote (region-beginning) (region-end))
  4265. (org-promote)))
  4266. (org-fix-position-after-promote))
  4267. (defun org-do-demote ()
  4268. "Demote the current heading lower down the tree.
  4269. If the region is active in `transient-mark-mode', demote all headings
  4270. in the region."
  4271. (interactive)
  4272. (save-excursion
  4273. (if (org-region-active-p)
  4274. (org-map-region 'org-demote (region-beginning) (region-end))
  4275. (org-demote)))
  4276. (org-fix-position-after-promote))
  4277. (defun org-fix-position-after-promote ()
  4278. "Make sure that after pro/demotion cursor position is right."
  4279. (let ((pos (point)))
  4280. (when (save-excursion
  4281. (beginning-of-line 1)
  4282. (looking-at org-todo-line-regexp)
  4283. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4284. (cond ((eobp) (insert " "))
  4285. ((eolp) (insert " "))
  4286. ((equal (char-after) ?\ ) (forward-char 1))))))
  4287. (defun org-reduced-level (l)
  4288. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4289. (defun org-get-valid-level (level &optional change)
  4290. "Rectify a level change under the influence of `org-odd-levels-only'
  4291. LEVEL is a current level, CHANGE is by how much the level should be
  4292. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4293. even level numbers will become the next higher odd number."
  4294. (if org-odd-levels-only
  4295. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4296. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4297. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4298. (max 1 (+ level change))))
  4299. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4300. (define-obsolete-function-alias 'org-get-legal-level
  4301. 'org-get-valid-level)
  4302. (define-obsolete-function-alias 'org-get-legal-level
  4303. 'org-get-valid-level "23.1"))
  4304. (defun org-promote ()
  4305. "Promote the current heading higher up the tree.
  4306. If the region is active in `transient-mark-mode', promote all headings
  4307. in the region."
  4308. (org-back-to-heading t)
  4309. (let* ((level (save-match-data (funcall outline-level)))
  4310. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4311. (diff (abs (- level (length up-head) -1))))
  4312. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4313. (replace-match up-head nil t)
  4314. ;; Fixup tag positioning
  4315. (and org-auto-align-tags (org-set-tags nil t))
  4316. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4317. (defun org-demote ()
  4318. "Demote the current heading lower down the tree.
  4319. If the region is active in `transient-mark-mode', demote all headings
  4320. in the region."
  4321. (org-back-to-heading t)
  4322. (let* ((level (save-match-data (funcall outline-level)))
  4323. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4324. (diff (abs (- level (length down-head) -1))))
  4325. (replace-match down-head nil t)
  4326. ;; Fixup tag positioning
  4327. (and org-auto-align-tags (org-set-tags nil t))
  4328. (if org-adapt-indentation (org-fixup-indentation diff))))
  4329. (defun org-map-tree (fun)
  4330. "Call FUN for every heading underneath the current one."
  4331. (org-back-to-heading)
  4332. (let ((level (funcall outline-level)))
  4333. (save-excursion
  4334. (funcall fun)
  4335. (while (and (progn
  4336. (outline-next-heading)
  4337. (> (funcall outline-level) level))
  4338. (not (eobp)))
  4339. (funcall fun)))))
  4340. (defun org-map-region (fun beg end)
  4341. "Call FUN for every heading between BEG and END."
  4342. (let ((org-ignore-region t))
  4343. (save-excursion
  4344. (setq end (copy-marker end))
  4345. (goto-char beg)
  4346. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4347. (< (point) end))
  4348. (funcall fun))
  4349. (while (and (progn
  4350. (outline-next-heading)
  4351. (< (point) end))
  4352. (not (eobp)))
  4353. (funcall fun)))))
  4354. (defun org-fixup-indentation (diff)
  4355. "Change the indentation in the current entry by DIFF
  4356. However, if any line in the current entry has no indentation, or if it
  4357. would end up with no indentation after the change, nothing at all is done."
  4358. (save-excursion
  4359. (let ((end (save-excursion (outline-next-heading)
  4360. (point-marker)))
  4361. (prohibit (if (> diff 0)
  4362. "^\\S-"
  4363. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4364. col)
  4365. (unless (save-excursion (end-of-line 1)
  4366. (re-search-forward prohibit end t))
  4367. (while (and (< (point) end)
  4368. (re-search-forward "^[ \t]+" end t))
  4369. (goto-char (match-end 0))
  4370. (setq col (current-column))
  4371. (if (< diff 0) (replace-match ""))
  4372. (indent-to (+ diff col))))
  4373. (move-marker end nil))))
  4374. (defun org-convert-to-odd-levels ()
  4375. "Convert an org-mode file with all levels allowed to one with odd levels.
  4376. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4377. level 5 etc."
  4378. (interactive)
  4379. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4380. (let ((org-odd-levels-only nil) n)
  4381. (save-excursion
  4382. (goto-char (point-min))
  4383. (while (re-search-forward "^\\*\\*+ " nil t)
  4384. (setq n (- (length (match-string 0)) 2))
  4385. (while (>= (setq n (1- n)) 0)
  4386. (org-demote))
  4387. (end-of-line 1))))))
  4388. (defun org-convert-to-oddeven-levels ()
  4389. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4390. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4391. section with an even level, conversion would destroy the structure of the file. An error
  4392. is signaled in this case."
  4393. (interactive)
  4394. (goto-char (point-min))
  4395. ;; First check if there are no even levels
  4396. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4397. (org-show-context t)
  4398. (error "Not all levels are odd in this file. Conversion not possible."))
  4399. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4400. (let ((org-odd-levels-only nil) n)
  4401. (save-excursion
  4402. (goto-char (point-min))
  4403. (while (re-search-forward "^\\*\\*+ " nil t)
  4404. (setq n (/ (1- (length (match-string 0))) 2))
  4405. (while (>= (setq n (1- n)) 0)
  4406. (org-promote))
  4407. (end-of-line 1))))))
  4408. (defun org-tr-level (n)
  4409. "Make N odd if required."
  4410. (if org-odd-levels-only (1+ (/ n 2)) n))
  4411. ;;; Vertical tree motion, cutting and pasting of subtrees
  4412. (defun org-move-subtree-up (&optional arg)
  4413. "Move the current subtree up past ARG headlines of the same level."
  4414. (interactive "p")
  4415. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4416. (defun org-move-subtree-down (&optional arg)
  4417. "Move the current subtree down past ARG headlines of the same level."
  4418. (interactive "p")
  4419. (setq arg (prefix-numeric-value arg))
  4420. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4421. 'outline-get-last-sibling))
  4422. (ins-point (make-marker))
  4423. (cnt (abs arg))
  4424. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4425. ;; Select the tree
  4426. (org-back-to-heading)
  4427. (setq beg0 (point))
  4428. (save-excursion
  4429. (setq ne-beg (org-back-over-empty-lines))
  4430. (setq beg (point)))
  4431. (save-match-data
  4432. (save-excursion (outline-end-of-heading)
  4433. (setq folded (org-invisible-p)))
  4434. (outline-end-of-subtree))
  4435. (outline-next-heading)
  4436. (setq ne-end (org-back-over-empty-lines))
  4437. (setq end (point))
  4438. (goto-char beg0)
  4439. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4440. ;; include less whitespace
  4441. (save-excursion
  4442. (goto-char beg)
  4443. (forward-line (- ne-beg ne-end))
  4444. (setq beg (point))))
  4445. ;; Find insertion point, with error handling
  4446. (while (> cnt 0)
  4447. (or (and (funcall movfunc) (looking-at outline-regexp))
  4448. (progn (goto-char beg0)
  4449. (error "Cannot move past superior level or buffer limit")))
  4450. (setq cnt (1- cnt)))
  4451. (if (> arg 0)
  4452. ;; Moving forward - still need to move over subtree
  4453. (progn (org-end-of-subtree t t)
  4454. (save-excursion
  4455. (org-back-over-empty-lines)
  4456. (or (bolp) (newline)))))
  4457. (setq ne-ins (org-back-over-empty-lines))
  4458. (move-marker ins-point (point))
  4459. (setq txt (buffer-substring beg end))
  4460. (delete-region beg end)
  4461. (outline-flag-region (1- beg) beg nil)
  4462. (outline-flag-region (1- (point)) (point) nil)
  4463. (insert txt)
  4464. (or (bolp) (insert "\n"))
  4465. (setq ins-end (point))
  4466. (goto-char ins-point)
  4467. (org-skip-whitespace)
  4468. (when (and (< arg 0)
  4469. (org-first-sibling-p)
  4470. (> ne-ins ne-beg))
  4471. ;; Move whitespace back to beginning
  4472. (save-excursion
  4473. (goto-char ins-end)
  4474. (let ((kill-whole-line t))
  4475. (kill-line (- ne-ins ne-beg)) (point)))
  4476. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4477. (move-marker ins-point nil)
  4478. (org-compact-display-after-subtree-move)
  4479. (unless folded
  4480. (org-show-entry)
  4481. (show-children)
  4482. (org-cycle-hide-drawers 'children))))
  4483. (defvar org-subtree-clip ""
  4484. "Clipboard for cut and paste of subtrees.
  4485. This is actually only a copy of the kill, because we use the normal kill
  4486. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4487. (defvar org-subtree-clip-folded nil
  4488. "Was the last copied subtree folded?
  4489. This is used to fold the tree back after pasting.")
  4490. (defun org-cut-subtree (&optional n)
  4491. "Cut the current subtree into the clipboard.
  4492. With prefix arg N, cut this many sequential subtrees.
  4493. This is a short-hand for marking the subtree and then cutting it."
  4494. (interactive "p")
  4495. (org-copy-subtree n 'cut))
  4496. (defun org-copy-subtree (&optional n cut)
  4497. "Cut the current subtree into the clipboard.
  4498. With prefix arg N, cut this many sequential subtrees.
  4499. This is a short-hand for marking the subtree and then copying it.
  4500. If CUT is non-nil, actually cut the subtree."
  4501. (interactive "p")
  4502. (let (beg end folded (beg0 (point)))
  4503. (if (interactive-p)
  4504. (org-back-to-heading nil) ; take what looks like a subtree
  4505. (org-back-to-heading t)) ; take what is really there
  4506. (org-back-over-empty-lines)
  4507. (setq beg (point))
  4508. (skip-chars-forward " \t\r\n")
  4509. (save-match-data
  4510. (save-excursion (outline-end-of-heading)
  4511. (setq folded (org-invisible-p)))
  4512. (condition-case nil
  4513. (outline-forward-same-level (1- n))
  4514. (error nil))
  4515. (org-end-of-subtree t t))
  4516. (org-back-over-empty-lines)
  4517. (setq end (point))
  4518. (goto-char beg0)
  4519. (when (> end beg)
  4520. (setq org-subtree-clip-folded folded)
  4521. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4522. (setq org-subtree-clip (current-kill 0))
  4523. (message "%s: Subtree(s) with %d characters"
  4524. (if cut "Cut" "Copied")
  4525. (length org-subtree-clip)))))
  4526. (defun org-paste-subtree (&optional level tree)
  4527. "Paste the clipboard as a subtree, with modification of headline level.
  4528. The entire subtree is promoted or demoted in order to match a new headline
  4529. level. By default, the new level is derived from the visible headings
  4530. before and after the insertion point, and taken to be the inferior headline
  4531. level of the two. So if the previous visible heading is level 3 and the
  4532. next is level 4 (or vice versa), level 4 will be used for insertion.
  4533. This makes sure that the subtree remains an independent subtree and does
  4534. not swallow low level entries.
  4535. You can also force a different level, either by using a numeric prefix
  4536. argument, or by inserting the heading marker by hand. For example, if the
  4537. cursor is after \"*****\", then the tree will be shifted to level 5.
  4538. If you want to insert the tree as is, just use \\[yank].
  4539. If optional TREE is given, use this text instead of the kill ring."
  4540. (interactive "P")
  4541. (unless (org-kill-is-subtree-p tree)
  4542. (error "%s"
  4543. (substitute-command-keys
  4544. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4545. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  4546. (^re (concat "^\\(" outline-regexp "\\)"))
  4547. (re (concat "\\(" outline-regexp "\\)"))
  4548. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  4549. (old-level (if (string-match ^re txt)
  4550. (- (match-end 0) (match-beginning 0) 1)
  4551. -1))
  4552. (force-level (cond (level (prefix-numeric-value level))
  4553. ((string-match
  4554. ^re_ (buffer-substring (point-at-bol) (point)))
  4555. (- (match-end 1) (match-beginning 1)))
  4556. (t nil)))
  4557. (previous-level (save-excursion
  4558. (condition-case nil
  4559. (progn
  4560. (outline-previous-visible-heading 1)
  4561. (if (looking-at re)
  4562. (- (match-end 0) (match-beginning 0) 1)
  4563. 1))
  4564. (error 1))))
  4565. (next-level (save-excursion
  4566. (condition-case nil
  4567. (progn
  4568. (or (looking-at outline-regexp)
  4569. (outline-next-visible-heading 1))
  4570. (if (looking-at re)
  4571. (- (match-end 0) (match-beginning 0) 1)
  4572. 1))
  4573. (error 1))))
  4574. (new-level (or force-level (max previous-level next-level)))
  4575. (shift (if (or (= old-level -1)
  4576. (= new-level -1)
  4577. (= old-level new-level))
  4578. 0
  4579. (- new-level old-level)))
  4580. (delta (if (> shift 0) -1 1))
  4581. (func (if (> shift 0) 'org-demote 'org-promote))
  4582. (org-odd-levels-only nil)
  4583. beg end)
  4584. ;; Remove the forced level indicator
  4585. (if force-level
  4586. (delete-region (point-at-bol) (point)))
  4587. ;; Paste
  4588. (beginning-of-line 1)
  4589. (org-back-over-empty-lines) ;; FIXME: correct fix????
  4590. (setq beg (point))
  4591. (insert-before-markers txt) ;; FIXME: correct fix????
  4592. (unless (string-match "\n\\'" txt) (insert "\n"))
  4593. (setq end (point))
  4594. (goto-char beg)
  4595. (skip-chars-forward " \t\n\r")
  4596. (setq beg (point))
  4597. ;; Shift if necessary
  4598. (unless (= shift 0)
  4599. (save-restriction
  4600. (narrow-to-region beg end)
  4601. (while (not (= shift 0))
  4602. (org-map-region func (point-min) (point-max))
  4603. (setq shift (+ delta shift)))
  4604. (goto-char (point-min))))
  4605. (when (interactive-p)
  4606. (message "Clipboard pasted as level %d subtree" new-level))
  4607. (if (and kill-ring
  4608. (eq org-subtree-clip (current-kill 0))
  4609. org-subtree-clip-folded)
  4610. ;; The tree was folded before it was killed/copied
  4611. (hide-subtree))))
  4612. (defun org-kill-is-subtree-p (&optional txt)
  4613. "Check if the current kill is an outline subtree, or a set of trees.
  4614. Returns nil if kill does not start with a headline, or if the first
  4615. headline level is not the largest headline level in the tree.
  4616. So this will actually accept several entries of equal levels as well,
  4617. which is OK for `org-paste-subtree'.
  4618. If optional TXT is given, check this string instead of the current kill."
  4619. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  4620. (start-level (and kill
  4621. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  4622. org-outline-regexp "\\)")
  4623. kill)
  4624. (- (match-end 2) (match-beginning 2) 1)))
  4625. (re (concat "^" org-outline-regexp))
  4626. (start (1+ (match-beginning 2))))
  4627. (if (not start-level)
  4628. (progn
  4629. nil) ;; does not even start with a heading
  4630. (catch 'exit
  4631. (while (setq start (string-match re kill (1+ start)))
  4632. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  4633. (throw 'exit nil)))
  4634. t))))
  4635. (defun org-narrow-to-subtree ()
  4636. "Narrow buffer to the current subtree."
  4637. (interactive)
  4638. (save-excursion
  4639. (save-match-data
  4640. (narrow-to-region
  4641. (progn (org-back-to-heading) (point))
  4642. (progn (org-end-of-subtree t t) (point))))))
  4643. ;;; Outline Sorting
  4644. (defun org-sort (with-case)
  4645. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  4646. Optional argument WITH-CASE means sort case-sensitively."
  4647. (interactive "P")
  4648. (if (org-at-table-p)
  4649. (org-call-with-arg 'org-table-sort-lines with-case)
  4650. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  4651. (defun org-sort-remove-invisible (s)
  4652. (remove-text-properties 0 (length s) org-rm-props s)
  4653. (while (string-match org-bracket-link-regexp s)
  4654. (setq s (replace-match (if (match-end 2)
  4655. (match-string 3 s)
  4656. (match-string 1 s)) t t s)))
  4657. s)
  4658. (defvar org-priority-regexp) ; defined later in the file
  4659. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  4660. "Sort entries on a certain level of an outline tree.
  4661. If there is an active region, the entries in the region are sorted.
  4662. Else, if the cursor is before the first entry, sort the top-level items.
  4663. Else, the children of the entry at point are sorted.
  4664. Sorting can be alphabetically, numerically, and by date/time as given by
  4665. the first time stamp in the entry. The command prompts for the sorting
  4666. type unless it has been given to the function through the SORTING-TYPE
  4667. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  4668. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  4669. called with point at the beginning of the record. It must return either
  4670. a string or a number that should serve as the sorting key for that record.
  4671. Comparing entries ignores case by default. However, with an optional argument
  4672. WITH-CASE, the sorting considers case as well."
  4673. (interactive "P")
  4674. (let ((case-func (if with-case 'identity 'downcase))
  4675. start beg end stars re re2
  4676. txt what tmp plain-list-p)
  4677. ;; Find beginning and end of region to sort
  4678. (cond
  4679. ((org-region-active-p)
  4680. ;; we will sort the region
  4681. (setq end (region-end)
  4682. what "region")
  4683. (goto-char (region-beginning))
  4684. (if (not (org-on-heading-p)) (outline-next-heading))
  4685. (setq start (point)))
  4686. ((org-at-item-p)
  4687. ;; we will sort this plain list
  4688. (org-beginning-of-item-list) (setq start (point))
  4689. (org-end-of-item-list) (setq end (point))
  4690. (goto-char start)
  4691. (setq plain-list-p t
  4692. what "plain list"))
  4693. ((or (org-on-heading-p)
  4694. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  4695. ;; we will sort the children of the current headline
  4696. (org-back-to-heading)
  4697. (setq start (point)
  4698. end (progn (org-end-of-subtree t t)
  4699. (org-back-over-empty-lines)
  4700. (point))
  4701. what "children")
  4702. (goto-char start)
  4703. (show-subtree)
  4704. (outline-next-heading))
  4705. (t
  4706. ;; we will sort the top-level entries in this file
  4707. (goto-char (point-min))
  4708. (or (org-on-heading-p) (outline-next-heading))
  4709. (setq start (point) end (point-max) what "top-level")
  4710. (goto-char start)
  4711. (show-all)))
  4712. (setq beg (point))
  4713. (if (>= beg end) (error "Nothing to sort"))
  4714. (unless plain-list-p
  4715. (looking-at "\\(\\*+\\)")
  4716. (setq stars (match-string 1)
  4717. re (concat "^" (regexp-quote stars) " +")
  4718. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  4719. txt (buffer-substring beg end))
  4720. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  4721. (if (and (not (equal stars "*")) (string-match re2 txt))
  4722. (error "Region to sort contains a level above the first entry")))
  4723. (unless sorting-type
  4724. (message
  4725. (if plain-list-p
  4726. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  4727. "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty [f]unc A/N/T/P/F means reversed:")
  4728. what)
  4729. (setq sorting-type (read-char-exclusive))
  4730. (and (= (downcase sorting-type) ?f)
  4731. (setq getkey-func
  4732. (completing-read "Sort using function: "
  4733. obarray 'fboundp t nil nil))
  4734. (setq getkey-func (intern getkey-func)))
  4735. (and (= (downcase sorting-type) ?r)
  4736. (setq property
  4737. (completing-read "Property: "
  4738. (mapcar 'list (org-buffer-property-keys t))
  4739. nil t))))
  4740. (message "Sorting entries...")
  4741. (save-restriction
  4742. (narrow-to-region start end)
  4743. (let ((dcst (downcase sorting-type))
  4744. (now (current-time)))
  4745. (sort-subr
  4746. (/= dcst sorting-type)
  4747. ;; This function moves to the beginning character of the "record" to
  4748. ;; be sorted.
  4749. (if plain-list-p
  4750. (lambda nil
  4751. (if (org-at-item-p) t (goto-char (point-max))))
  4752. (lambda nil
  4753. (if (re-search-forward re nil t)
  4754. (goto-char (match-beginning 0))
  4755. (goto-char (point-max)))))
  4756. ;; This function moves to the last character of the "record" being
  4757. ;; sorted.
  4758. (if plain-list-p
  4759. 'org-end-of-item
  4760. (lambda nil
  4761. (save-match-data
  4762. (condition-case nil
  4763. (outline-forward-same-level 1)
  4764. (error
  4765. (goto-char (point-max)))))))
  4766. ;; This function returns the value that gets sorted against.
  4767. (if plain-list-p
  4768. (lambda nil
  4769. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  4770. (cond
  4771. ((= dcst ?n)
  4772. (string-to-number (buffer-substring (match-end 0)
  4773. (point-at-eol))))
  4774. ((= dcst ?a)
  4775. (buffer-substring (match-end 0) (point-at-eol)))
  4776. ((= dcst ?t)
  4777. (if (re-search-forward org-ts-regexp
  4778. (point-at-eol) t)
  4779. (org-time-string-to-time (match-string 0))
  4780. now))
  4781. ((= dcst ?f)
  4782. (if getkey-func
  4783. (progn
  4784. (setq tmp (funcall getkey-func))
  4785. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4786. tmp)
  4787. (error "Invalid key function `%s'" getkey-func)))
  4788. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4789. (lambda nil
  4790. (cond
  4791. ((= dcst ?n)
  4792. (if (looking-at outline-regexp)
  4793. (string-to-number (buffer-substring (match-end 0)
  4794. (point-at-eol)))
  4795. nil))
  4796. ((= dcst ?a)
  4797. (funcall case-func (buffer-substring (point-at-bol)
  4798. (point-at-eol))))
  4799. ((= dcst ?t)
  4800. (if (re-search-forward org-ts-regexp
  4801. (save-excursion
  4802. (forward-line 2)
  4803. (point)) t)
  4804. (org-time-string-to-time (match-string 0))
  4805. now))
  4806. ((= dcst ?p)
  4807. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  4808. (string-to-char (match-string 2))
  4809. org-default-priority))
  4810. ((= dcst ?r)
  4811. (or (org-entry-get nil property) ""))
  4812. ((= dcst ?f)
  4813. (if getkey-func
  4814. (progn
  4815. (setq tmp (funcall getkey-func))
  4816. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4817. tmp)
  4818. (error "Invalid key function `%s'" getkey-func)))
  4819. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4820. nil
  4821. (cond
  4822. ((= dcst ?a) 'string<)
  4823. ((= dcst ?t) 'time-less-p)
  4824. (t nil)))))
  4825. (message "Sorting entries...done")))
  4826. (defun org-do-sort (table what &optional with-case sorting-type)
  4827. "Sort TABLE of WHAT according to SORTING-TYPE.
  4828. The user will be prompted for the SORTING-TYPE if the call to this
  4829. function does not specify it. WHAT is only for the prompt, to indicate
  4830. what is being sorted. The sorting key will be extracted from
  4831. the car of the elements of the table.
  4832. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  4833. (unless sorting-type
  4834. (message
  4835. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  4836. what)
  4837. (setq sorting-type (read-char-exclusive)))
  4838. (let ((dcst (downcase sorting-type))
  4839. extractfun comparefun)
  4840. ;; Define the appropriate functions
  4841. (cond
  4842. ((= dcst ?n)
  4843. (setq extractfun 'string-to-number
  4844. comparefun (if (= dcst sorting-type) '< '>)))
  4845. ((= dcst ?a)
  4846. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  4847. (lambda(x) (downcase (org-sort-remove-invisible x))))
  4848. comparefun (if (= dcst sorting-type)
  4849. 'string<
  4850. (lambda (a b) (and (not (string< a b))
  4851. (not (string= a b)))))))
  4852. ((= dcst ?t)
  4853. (setq extractfun
  4854. (lambda (x)
  4855. (if (string-match org-ts-regexp x)
  4856. (time-to-seconds
  4857. (org-time-string-to-time (match-string 0 x)))
  4858. 0))
  4859. comparefun (if (= dcst sorting-type) '< '>)))
  4860. (t (error "Invalid sorting type `%c'" sorting-type)))
  4861. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  4862. table)
  4863. (lambda (a b) (funcall comparefun (car a) (car b))))))
  4864. ;;;; Plain list items, including checkboxes
  4865. ;;; Plain list items
  4866. (defun org-at-item-p ()
  4867. "Is point in a line starting a hand-formatted item?"
  4868. (let ((llt org-plain-list-ordered-item-terminator))
  4869. (save-excursion
  4870. (goto-char (point-at-bol))
  4871. (looking-at
  4872. (cond
  4873. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4874. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4875. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+))\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4876. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  4877. (defun org-in-item-p ()
  4878. "It the cursor inside a plain list item.
  4879. Does not have to be the first line."
  4880. (save-excursion
  4881. (condition-case nil
  4882. (progn
  4883. (org-beginning-of-item)
  4884. (org-at-item-p)
  4885. t)
  4886. (error nil))))
  4887. (defun org-insert-item (&optional checkbox)
  4888. "Insert a new item at the current level.
  4889. Return t when things worked, nil when we are not in an item."
  4890. (when (save-excursion
  4891. (condition-case nil
  4892. (progn
  4893. (org-beginning-of-item)
  4894. (org-at-item-p)
  4895. (if (org-invisible-p) (error "Invisible item"))
  4896. t)
  4897. (error nil)))
  4898. (let* ((bul (match-string 0))
  4899. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  4900. (match-end 0)))
  4901. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  4902. pos)
  4903. (cond
  4904. ((and (org-at-item-p) (<= (point) eow))
  4905. ;; before the bullet
  4906. (beginning-of-line 1)
  4907. (open-line (if blank 2 1)))
  4908. ((<= (point) eow)
  4909. (beginning-of-line 1))
  4910. (t
  4911. (unless (org-get-alist-option org-M-RET-may-split-line 'item)
  4912. (end-of-line 1)
  4913. (delete-horizontal-space))
  4914. (newline (if blank 2 1))))
  4915. (insert bul (if checkbox "[ ]" ""))
  4916. (just-one-space)
  4917. (setq pos (point))
  4918. (end-of-line 1)
  4919. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  4920. (org-maybe-renumber-ordered-list)
  4921. (and checkbox (org-update-checkbox-count-maybe))
  4922. t))
  4923. ;;; Checkboxes
  4924. (defun org-at-item-checkbox-p ()
  4925. "Is point at a line starting a plain-list item with a checklet?"
  4926. (and (org-at-item-p)
  4927. (save-excursion
  4928. (goto-char (match-end 0))
  4929. (skip-chars-forward " \t")
  4930. (looking-at "\\[[- X]\\]"))))
  4931. (defun org-toggle-checkbox (&optional arg)
  4932. "Toggle the checkbox in the current line."
  4933. (interactive "P")
  4934. (catch 'exit
  4935. (let (beg end status (firstnew 'unknown))
  4936. (cond
  4937. ((org-region-active-p)
  4938. (setq beg (region-beginning) end (region-end)))
  4939. ((org-on-heading-p)
  4940. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  4941. ((org-at-item-checkbox-p)
  4942. (let ((pos (point)))
  4943. (replace-match
  4944. (cond (arg "[-]")
  4945. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  4946. (t "[ ]"))
  4947. t t)
  4948. (goto-char pos))
  4949. (throw 'exit t))
  4950. (t (error "Not at a checkbox or heading, and no active region")))
  4951. (save-excursion
  4952. (goto-char beg)
  4953. (while (< (point) end)
  4954. (when (org-at-item-checkbox-p)
  4955. (setq status (equal (match-string 0) "[X]"))
  4956. (when (eq firstnew 'unknown)
  4957. (setq firstnew (not status)))
  4958. (replace-match
  4959. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  4960. (beginning-of-line 2)))))
  4961. (org-update-checkbox-count-maybe))
  4962. (defun org-update-checkbox-count-maybe ()
  4963. "Update checkbox statistics unless turned off by user."
  4964. (when org-provide-checkbox-statistics
  4965. (org-update-checkbox-count)))
  4966. (defun org-update-checkbox-count (&optional all)
  4967. "Update the checkbox statistics in the current section.
  4968. This will find all statistic cookies like [57%] and [6/12] and update them
  4969. with the current numbers. With optional prefix argument ALL, do this for
  4970. the whole buffer."
  4971. (interactive "P")
  4972. (save-excursion
  4973. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  4974. (beg (condition-case nil
  4975. (progn (outline-back-to-heading) (point))
  4976. (error (point-min))))
  4977. (end (move-marker (make-marker)
  4978. (progn (outline-next-heading) (point))))
  4979. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  4980. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  4981. (re-find (concat re "\\|" re-box))
  4982. beg-cookie end-cookie is-percent c-on c-off lim
  4983. eline curr-ind next-ind continue-from startsearch
  4984. (cstat 0)
  4985. )
  4986. (when all
  4987. (goto-char (point-min))
  4988. (outline-next-heading)
  4989. (setq beg (point) end (point-max)))
  4990. (goto-char end)
  4991. ;; find each statistic cookie
  4992. (while (re-search-backward re-find beg t)
  4993. (setq beg-cookie (match-beginning 1)
  4994. end-cookie (match-end 1)
  4995. cstat (+ cstat (if end-cookie 1 0))
  4996. startsearch (point-at-eol)
  4997. continue-from (point-at-bol)
  4998. is-percent (match-beginning 2)
  4999. lim (cond
  5000. ((org-on-heading-p) (outline-next-heading) (point))
  5001. ((org-at-item-p) (org-end-of-item) (point))
  5002. (t nil))
  5003. c-on 0
  5004. c-off 0)
  5005. (when lim
  5006. ;; find first checkbox for this cookie and gather
  5007. ;; statistics from all that are at this indentation level
  5008. (goto-char startsearch)
  5009. (if (re-search-forward re-box lim t)
  5010. (progn
  5011. (org-beginning-of-item)
  5012. (setq curr-ind (org-get-indentation))
  5013. (setq next-ind curr-ind)
  5014. (while (and (bolp) (org-at-item-p) (= curr-ind next-ind))
  5015. (save-excursion (end-of-line) (setq eline (point)))
  5016. (if (re-search-forward re-box eline t)
  5017. (if (member (match-string 2) '("[ ]" "[-]"))
  5018. (setq c-off (1+ c-off))
  5019. (setq c-on (1+ c-on))
  5020. )
  5021. )
  5022. (org-end-of-item)
  5023. (setq next-ind (org-get-indentation))
  5024. )))
  5025. (goto-char continue-from)
  5026. ;; update cookie
  5027. (when end-cookie
  5028. (delete-region beg-cookie end-cookie)
  5029. (goto-char beg-cookie)
  5030. (insert
  5031. (if is-percent
  5032. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  5033. (format "[%d/%d]" c-on (+ c-on c-off)))))
  5034. ;; update items checkbox if it has one
  5035. (when (org-at-item-p)
  5036. (org-beginning-of-item)
  5037. (when (and (> (+ c-on c-off) 0)
  5038. (re-search-forward re-box (point-at-eol) t))
  5039. (setq beg-cookie (match-beginning 2)
  5040. end-cookie (match-end 2))
  5041. (delete-region beg-cookie end-cookie)
  5042. (goto-char beg-cookie)
  5043. (cond ((= c-off 0) (insert "[X]"))
  5044. ((= c-on 0) (insert "[ ]"))
  5045. (t (insert "[-]")))
  5046. )))
  5047. (goto-char continue-from))
  5048. (when (interactive-p)
  5049. (message "Checkbox satistics updated %s (%d places)"
  5050. (if all "in entire file" "in current outline entry") cstat)))))
  5051. (defun org-get-checkbox-statistics-face ()
  5052. "Select the face for checkbox statistics.
  5053. The face will be `org-done' when all relevant boxes are checked. Otherwise
  5054. it will be `org-todo'."
  5055. (if (match-end 1)
  5056. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  5057. (if (and (> (match-end 2) (match-beginning 2))
  5058. (equal (match-string 2) (match-string 3)))
  5059. 'org-done
  5060. 'org-todo)))
  5061. (defun org-get-indentation (&optional line)
  5062. "Get the indentation of the current line, interpreting tabs.
  5063. When LINE is given, assume it represents a line and compute its indentation."
  5064. (if line
  5065. (if (string-match "^ *" (org-remove-tabs line))
  5066. (match-end 0))
  5067. (save-excursion
  5068. (beginning-of-line 1)
  5069. (skip-chars-forward " \t")
  5070. (current-column))))
  5071. (defun org-remove-tabs (s &optional width)
  5072. "Replace tabulators in S with spaces.
  5073. Assumes that s is a single line, starting in column 0."
  5074. (setq width (or width tab-width))
  5075. (while (string-match "\t" s)
  5076. (setq s (replace-match
  5077. (make-string
  5078. (- (* width (/ (+ (match-beginning 0) width) width))
  5079. (match-beginning 0)) ?\ )
  5080. t t s)))
  5081. s)
  5082. (defun org-fix-indentation (line ind)
  5083. "Fix indentation in LINE.
  5084. IND is a cons cell with target and minimum indentation.
  5085. If the current indenation in LINE is smaller than the minimum,
  5086. leave it alone. If it is larger than ind, set it to the target."
  5087. (let* ((l (org-remove-tabs line))
  5088. (i (org-get-indentation l))
  5089. (i1 (car ind)) (i2 (cdr ind)))
  5090. (if (>= i i2) (setq l (substring line i2)))
  5091. (if (> i1 0)
  5092. (concat (make-string i1 ?\ ) l)
  5093. l)))
  5094. (defcustom org-empty-line-terminates-plain-lists nil
  5095. "Non-nil means, an empty line ends all plain list levels.
  5096. When nil, empty lines are part of the preceeding item."
  5097. :group 'org-plain-lists
  5098. :type 'boolean)
  5099. (defun org-beginning-of-item ()
  5100. "Go to the beginning of the current hand-formatted item.
  5101. If the cursor is not in an item, throw an error."
  5102. (interactive)
  5103. (let ((pos (point))
  5104. (limit (save-excursion
  5105. (condition-case nil
  5106. (progn
  5107. (org-back-to-heading)
  5108. (beginning-of-line 2) (point))
  5109. (error (point-min)))))
  5110. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  5111. ind ind1)
  5112. (if (org-at-item-p)
  5113. (beginning-of-line 1)
  5114. (beginning-of-line 1)
  5115. (skip-chars-forward " \t")
  5116. (setq ind (current-column))
  5117. (if (catch 'exit
  5118. (while t
  5119. (beginning-of-line 0)
  5120. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  5121. (if (looking-at "[ \t]*$")
  5122. (setq ind1 ind-empty)
  5123. (skip-chars-forward " \t")
  5124. (setq ind1 (current-column)))
  5125. (if (< ind1 ind)
  5126. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  5127. nil
  5128. (goto-char pos)
  5129. (error "Not in an item")))))
  5130. (defun org-end-of-item ()
  5131. "Go to the end of the current hand-formatted item.
  5132. If the cursor is not in an item, throw an error."
  5133. (interactive)
  5134. (let* ((pos (point))
  5135. ind1
  5136. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  5137. (limit (save-excursion (outline-next-heading) (point)))
  5138. (ind (save-excursion
  5139. (org-beginning-of-item)
  5140. (skip-chars-forward " \t")
  5141. (current-column)))
  5142. (end (catch 'exit
  5143. (while t
  5144. (beginning-of-line 2)
  5145. (if (eobp) (throw 'exit (point)))
  5146. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  5147. (if (looking-at "[ \t]*$")
  5148. (setq ind1 ind-empty)
  5149. (skip-chars-forward " \t")
  5150. (setq ind1 (current-column)))
  5151. (if (<= ind1 ind)
  5152. (throw 'exit (point-at-bol)))))))
  5153. (if end
  5154. (goto-char end)
  5155. (goto-char pos)
  5156. (error "Not in an item"))))
  5157. (defun org-next-item ()
  5158. "Move to the beginning of the next item in the current plain list.
  5159. Error if not at a plain list, or if this is the last item in the list."
  5160. (interactive)
  5161. (let (ind ind1 (pos (point)))
  5162. (org-beginning-of-item)
  5163. (setq ind (org-get-indentation))
  5164. (org-end-of-item)
  5165. (setq ind1 (org-get-indentation))
  5166. (unless (and (org-at-item-p) (= ind ind1))
  5167. (goto-char pos)
  5168. (error "On last item"))))
  5169. (defun org-previous-item ()
  5170. "Move to the beginning of the previous item in the current plain list.
  5171. Error if not at a plain list, or if this is the first item in the list."
  5172. (interactive)
  5173. (let (beg ind ind1 (pos (point)))
  5174. (org-beginning-of-item)
  5175. (setq beg (point))
  5176. (setq ind (org-get-indentation))
  5177. (goto-char beg)
  5178. (catch 'exit
  5179. (while t
  5180. (beginning-of-line 0)
  5181. (if (looking-at "[ \t]*$")
  5182. nil
  5183. (if (<= (setq ind1 (org-get-indentation)) ind)
  5184. (throw 'exit t)))))
  5185. (condition-case nil
  5186. (if (or (not (org-at-item-p))
  5187. (< ind1 (1- ind)))
  5188. (error "")
  5189. (org-beginning-of-item))
  5190. (error (goto-char pos)
  5191. (error "On first item")))))
  5192. (defun org-first-list-item-p ()
  5193. "Is this heading the item in a plain list?"
  5194. (unless (org-at-item-p)
  5195. (error "Not at a plain list item"))
  5196. (org-beginning-of-item)
  5197. (= (point) (save-excursion (org-beginning-of-item-list))))
  5198. (defun org-move-item-down ()
  5199. "Move the plain list item at point down, i.e. swap with following item.
  5200. Subitems (items with larger indentation) are considered part of the item,
  5201. so this really moves item trees."
  5202. (interactive)
  5203. (let (beg beg0 end end0 ind ind1 (pos (point)) txt ne-end ne-beg)
  5204. (org-beginning-of-item)
  5205. (setq beg0 (point))
  5206. (save-excursion
  5207. (setq ne-beg (org-back-over-empty-lines))
  5208. (setq beg (point)))
  5209. (goto-char beg0)
  5210. (setq ind (org-get-indentation))
  5211. (org-end-of-item)
  5212. (setq end0 (point))
  5213. (setq ind1 (org-get-indentation))
  5214. (setq ne-end (org-back-over-empty-lines))
  5215. (setq end (point))
  5216. (goto-char beg0)
  5217. (when (and (org-first-list-item-p) (< ne-end ne-beg))
  5218. ;; include less whitespace
  5219. (save-excursion
  5220. (goto-char beg)
  5221. (forward-line (- ne-beg ne-end))
  5222. (setq beg (point))))
  5223. (goto-char end0)
  5224. (if (and (org-at-item-p) (= ind ind1))
  5225. (progn
  5226. (org-end-of-item)
  5227. (org-back-over-empty-lines)
  5228. (setq txt (buffer-substring beg end))
  5229. (save-excursion
  5230. (delete-region beg end))
  5231. (setq pos (point))
  5232. (insert txt)
  5233. (goto-char pos) (org-skip-whitespace)
  5234. (org-maybe-renumber-ordered-list))
  5235. (goto-char pos)
  5236. (error "Cannot move this item further down"))))
  5237. (defun org-move-item-up (arg)
  5238. "Move the plain list item at point up, i.e. swap with previous item.
  5239. Subitems (items with larger indentation) are considered part of the item,
  5240. so this really moves item trees."
  5241. (interactive "p")
  5242. (let (beg beg0 end ind ind1 (pos (point)) txt
  5243. ne-beg ne-ins ins-end)
  5244. (org-beginning-of-item)
  5245. (setq beg0 (point))
  5246. (setq ind (org-get-indentation))
  5247. (save-excursion
  5248. (setq ne-beg (org-back-over-empty-lines))
  5249. (setq beg (point)))
  5250. (goto-char beg0)
  5251. (org-end-of-item)
  5252. (setq end (point))
  5253. (goto-char beg0)
  5254. (catch 'exit
  5255. (while t
  5256. (beginning-of-line 0)
  5257. (if (looking-at "[ \t]*$")
  5258. (if org-empty-line-terminates-plain-lists
  5259. (progn
  5260. (goto-char pos)
  5261. (error "Cannot move this item further up"))
  5262. nil)
  5263. (if (<= (setq ind1 (org-get-indentation)) ind)
  5264. (throw 'exit t)))))
  5265. (condition-case nil
  5266. (org-beginning-of-item)
  5267. (error (goto-char beg)
  5268. (error "Cannot move this item further up")))
  5269. (setq ind1 (org-get-indentation))
  5270. (if (and (org-at-item-p) (= ind ind1))
  5271. (progn
  5272. (setq ne-ins (org-back-over-empty-lines))
  5273. (setq txt (buffer-substring beg end))
  5274. (save-excursion
  5275. (delete-region beg end))
  5276. (setq pos (point))
  5277. (insert txt)
  5278. (setq ins-end (point))
  5279. (goto-char pos) (org-skip-whitespace)
  5280. (when (and (org-first-list-item-p) (> ne-ins ne-beg))
  5281. ;; Move whitespace back to beginning
  5282. (save-excursion
  5283. (goto-char ins-end)
  5284. (let ((kill-whole-line t))
  5285. (kill-line (- ne-ins ne-beg)) (point)))
  5286. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5287. (org-maybe-renumber-ordered-list))
  5288. (goto-char pos)
  5289. (error "Cannot move this item further up"))))
  5290. (defun org-maybe-renumber-ordered-list ()
  5291. "Renumber the ordered list at point if setup allows it.
  5292. This tests the user option `org-auto-renumber-ordered-lists' before
  5293. doing the renumbering."
  5294. (interactive)
  5295. (when (and org-auto-renumber-ordered-lists
  5296. (org-at-item-p))
  5297. (if (match-beginning 3)
  5298. (org-renumber-ordered-list 1)
  5299. (org-fix-bullet-type))))
  5300. (defun org-maybe-renumber-ordered-list-safe ()
  5301. (condition-case nil
  5302. (save-excursion
  5303. (org-maybe-renumber-ordered-list))
  5304. (error nil)))
  5305. (defun org-cycle-list-bullet (&optional which)
  5306. "Cycle through the different itemize/enumerate bullets.
  5307. This cycle the entire list level through the sequence:
  5308. `-' -> `+' -> `*' -> `1.' -> `1)'
  5309. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  5310. 0 meand `-', 1 means `+' etc."
  5311. (interactive "P")
  5312. (org-preserve-lc
  5313. (org-beginning-of-item-list)
  5314. (org-at-item-p)
  5315. (beginning-of-line 1)
  5316. (let ((current (match-string 0))
  5317. (prevp (eq which 'previous))
  5318. new)
  5319. (setq new (cond
  5320. ((and (numberp which)
  5321. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  5322. ((string-match "-" current) (if prevp "1)" "+"))
  5323. ((string-match "\\+" current)
  5324. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  5325. ((string-match "\\*" current) (if prevp "+" "1."))
  5326. ((string-match "\\." current) (if prevp "*" "1)"))
  5327. ((string-match ")" current) (if prevp "1." "-"))
  5328. (t (error "This should not happen"))))
  5329. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  5330. (org-fix-bullet-type)
  5331. (org-maybe-renumber-ordered-list))))
  5332. (defun org-get-string-indentation (s)
  5333. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  5334. (let ((n -1) (i 0) (w tab-width) c)
  5335. (catch 'exit
  5336. (while (< (setq n (1+ n)) (length s))
  5337. (setq c (aref s n))
  5338. (cond ((= c ?\ ) (setq i (1+ i)))
  5339. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  5340. (t (throw 'exit t)))))
  5341. i))
  5342. (defun org-renumber-ordered-list (arg)
  5343. "Renumber an ordered plain list.
  5344. Cursor needs to be in the first line of an item, the line that starts
  5345. with something like \"1.\" or \"2)\"."
  5346. (interactive "p")
  5347. (unless (and (org-at-item-p)
  5348. (match-beginning 3))
  5349. (error "This is not an ordered list"))
  5350. (let ((line (org-current-line))
  5351. (col (current-column))
  5352. (ind (org-get-string-indentation
  5353. (buffer-substring (point-at-bol) (match-beginning 3))))
  5354. ;; (term (substring (match-string 3) -1))
  5355. ind1 (n (1- arg))
  5356. fmt)
  5357. ;; find where this list begins
  5358. (org-beginning-of-item-list)
  5359. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  5360. (setq fmt (concat "%d" (match-string 1)))
  5361. (beginning-of-line 0)
  5362. ;; walk forward and replace these numbers
  5363. (catch 'exit
  5364. (while t
  5365. (catch 'next
  5366. (beginning-of-line 2)
  5367. (if (eobp) (throw 'exit nil))
  5368. (if (looking-at "[ \t]*$") (throw 'next nil))
  5369. (skip-chars-forward " \t") (setq ind1 (current-column))
  5370. (if (> ind1 ind) (throw 'next t))
  5371. (if (< ind1 ind) (throw 'exit t))
  5372. (if (not (org-at-item-p)) (throw 'exit nil))
  5373. (delete-region (match-beginning 2) (match-end 2))
  5374. (goto-char (match-beginning 2))
  5375. (insert (format fmt (setq n (1+ n)))))))
  5376. (goto-line line)
  5377. (move-to-column col)))
  5378. (defun org-fix-bullet-type ()
  5379. "Make sure all items in this list have the same bullet as the firsst item."
  5380. (interactive)
  5381. (unless (org-at-item-p) (error "This is not a list"))
  5382. (let ((line (org-current-line))
  5383. (col (current-column))
  5384. (ind (current-indentation))
  5385. ind1 bullet)
  5386. ;; find where this list begins
  5387. (org-beginning-of-item-list)
  5388. (beginning-of-line 1)
  5389. ;; find out what the bullet type is
  5390. (looking-at "[ \t]*\\(\\S-+\\)")
  5391. (setq bullet (match-string 1))
  5392. ;; walk forward and replace these numbers
  5393. (beginning-of-line 0)
  5394. (catch 'exit
  5395. (while t
  5396. (catch 'next
  5397. (beginning-of-line 2)
  5398. (if (eobp) (throw 'exit nil))
  5399. (if (looking-at "[ \t]*$") (throw 'next nil))
  5400. (skip-chars-forward " \t") (setq ind1 (current-column))
  5401. (if (> ind1 ind) (throw 'next t))
  5402. (if (< ind1 ind) (throw 'exit t))
  5403. (if (not (org-at-item-p)) (throw 'exit nil))
  5404. (skip-chars-forward " \t")
  5405. (looking-at "\\S-+")
  5406. (replace-match bullet))))
  5407. (goto-line line)
  5408. (move-to-column col)
  5409. (if (string-match "[0-9]" bullet)
  5410. (org-renumber-ordered-list 1))))
  5411. (defun org-beginning-of-item-list ()
  5412. "Go to the beginning of the current item list.
  5413. I.e. to the first item in this list."
  5414. (interactive)
  5415. (org-beginning-of-item)
  5416. (let ((pos (point-at-bol))
  5417. (ind (org-get-indentation))
  5418. ind1)
  5419. ;; find where this list begins
  5420. (catch 'exit
  5421. (while t
  5422. (catch 'next
  5423. (beginning-of-line 0)
  5424. (if (looking-at "[ \t]*$")
  5425. (throw (if (bobp) 'exit 'next) t))
  5426. (skip-chars-forward " \t") (setq ind1 (current-column))
  5427. (if (or (< ind1 ind)
  5428. (and (= ind1 ind)
  5429. (not (org-at-item-p)))
  5430. (bobp))
  5431. (throw 'exit t)
  5432. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  5433. (goto-char pos)))
  5434. (defun org-end-of-item-list ()
  5435. "Go to the end of the current item list.
  5436. I.e. to the text after the last item."
  5437. (interactive)
  5438. (org-beginning-of-item)
  5439. (let ((pos (point-at-bol))
  5440. (ind (org-get-indentation))
  5441. ind1)
  5442. ;; find where this list begins
  5443. (catch 'exit
  5444. (while t
  5445. (catch 'next
  5446. (beginning-of-line 2)
  5447. (if (looking-at "[ \t]*$")
  5448. (throw (if (eobp) 'exit 'next) t))
  5449. (skip-chars-forward " \t") (setq ind1 (current-column))
  5450. (if (or (< ind1 ind)
  5451. (and (= ind1 ind)
  5452. (not (org-at-item-p)))
  5453. (eobp))
  5454. (progn
  5455. (setq pos (point-at-bol))
  5456. (throw 'exit t))))))
  5457. (goto-char pos)))
  5458. (defvar org-last-indent-begin-marker (make-marker))
  5459. (defvar org-last-indent-end-marker (make-marker))
  5460. (defun org-outdent-item (arg)
  5461. "Outdent a local list item."
  5462. (interactive "p")
  5463. (org-indent-item (- arg)))
  5464. (defun org-indent-item (arg)
  5465. "Indent a local list item."
  5466. (interactive "p")
  5467. (unless (org-at-item-p)
  5468. (error "Not on an item"))
  5469. (save-excursion
  5470. (let (beg end ind ind1 tmp delta ind-down ind-up)
  5471. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  5472. (setq beg org-last-indent-begin-marker
  5473. end org-last-indent-end-marker)
  5474. (org-beginning-of-item)
  5475. (setq beg (move-marker org-last-indent-begin-marker (point)))
  5476. (org-end-of-item)
  5477. (setq end (move-marker org-last-indent-end-marker (point))))
  5478. (goto-char beg)
  5479. (setq tmp (org-item-indent-positions)
  5480. ind (car tmp)
  5481. ind-down (nth 2 tmp)
  5482. ind-up (nth 1 tmp)
  5483. delta (if (> arg 0)
  5484. (if ind-down (- ind-down ind) 2)
  5485. (if ind-up (- ind-up ind) -2)))
  5486. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  5487. (while (< (point) end)
  5488. (beginning-of-line 1)
  5489. (skip-chars-forward " \t") (setq ind1 (current-column))
  5490. (delete-region (point-at-bol) (point))
  5491. (or (eolp) (indent-to-column (+ ind1 delta)))
  5492. (beginning-of-line 2))))
  5493. (org-fix-bullet-type)
  5494. (org-maybe-renumber-ordered-list-safe)
  5495. (save-excursion
  5496. (beginning-of-line 0)
  5497. (condition-case nil (org-beginning-of-item) (error nil))
  5498. (org-maybe-renumber-ordered-list-safe)))
  5499. (defun org-item-indent-positions ()
  5500. "Return indentation for plain list items.
  5501. This returns a list with three values: The current indentation, the
  5502. parent indentation and the indentation a child should habe.
  5503. Assumes cursor in item line."
  5504. (let* ((bolpos (point-at-bol))
  5505. (ind (org-get-indentation))
  5506. ind-down ind-up pos)
  5507. (save-excursion
  5508. (org-beginning-of-item-list)
  5509. (skip-chars-backward "\n\r \t")
  5510. (when (org-in-item-p)
  5511. (org-beginning-of-item)
  5512. (setq ind-up (org-get-indentation))))
  5513. (setq pos (point))
  5514. (save-excursion
  5515. (cond
  5516. ((and (condition-case nil (progn (org-previous-item) t)
  5517. (error nil))
  5518. (or (forward-char 1) t)
  5519. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  5520. (setq ind-down (org-get-indentation)))
  5521. ((and (goto-char pos)
  5522. (org-at-item-p))
  5523. (goto-char (match-end 0))
  5524. (skip-chars-forward " \t")
  5525. (setq ind-down (current-column)))))
  5526. (list ind ind-up ind-down)))
  5527. ;;; The orgstruct minor mode
  5528. ;; Define a minor mode which can be used in other modes in order to
  5529. ;; integrate the org-mode structure editing commands.
  5530. ;; This is really a hack, because the org-mode structure commands use
  5531. ;; keys which normally belong to the major mode. Here is how it
  5532. ;; works: The minor mode defines all the keys necessary to operate the
  5533. ;; structure commands, but wraps the commands into a function which
  5534. ;; tests if the cursor is currently at a headline or a plain list
  5535. ;; item. If that is the case, the structure command is used,
  5536. ;; temporarily setting many Org-mode variables like regular
  5537. ;; expressions for filling etc. However, when any of those keys is
  5538. ;; used at a different location, function uses `key-binding' to look
  5539. ;; up if the key has an associated command in another currently active
  5540. ;; keymap (minor modes, major mode, global), and executes that
  5541. ;; command. There might be problems if any of the keys is otherwise
  5542. ;; used as a prefix key.
  5543. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5544. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5545. ;; addresses this by checking explicitly for both bindings.
  5546. (defvar orgstruct-mode-map (make-sparse-keymap)
  5547. "Keymap for the minor `orgstruct-mode'.")
  5548. (defvar org-local-vars nil
  5549. "List of local variables, for use by `orgstruct-mode'")
  5550. ;;;###autoload
  5551. (define-minor-mode orgstruct-mode
  5552. "Toggle the minor more `orgstruct-mode'.
  5553. This mode is for using Org-mode structure commands in other modes.
  5554. The following key behave as if Org-mode was active, if the cursor
  5555. is on a headline, or on a plain list item (both in the definition
  5556. of Org-mode).
  5557. M-up Move entry/item up
  5558. M-down Move entry/item down
  5559. M-left Promote
  5560. M-right Demote
  5561. M-S-up Move entry/item up
  5562. M-S-down Move entry/item down
  5563. M-S-left Promote subtree
  5564. M-S-right Demote subtree
  5565. M-q Fill paragraph and items like in Org-mode
  5566. C-c ^ Sort entries
  5567. C-c - Cycle list bullet
  5568. TAB Cycle item visibility
  5569. M-RET Insert new heading/item
  5570. S-M-RET Insert new TODO heading / Chekbox item
  5571. C-c C-c Set tags / toggle checkbox"
  5572. nil " OrgStruct" nil
  5573. (org-load-modules-maybe)
  5574. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5575. ;;;###autoload
  5576. (defun turn-on-orgstruct ()
  5577. "Unconditionally turn on `orgstruct-mode'."
  5578. (orgstruct-mode 1))
  5579. ;;;###autoload
  5580. (defun turn-on-orgstruct++ ()
  5581. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5582. In addition to setting orgstruct-mode, this also exports all indentation and
  5583. autofilling variables from org-mode into the buffer. Note that turning
  5584. off orgstruct-mode will *not* remove these additional settings."
  5585. (orgstruct-mode 1)
  5586. (let (var val)
  5587. (mapc
  5588. (lambda (x)
  5589. (when (string-match
  5590. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5591. (symbol-name (car x)))
  5592. (setq var (car x) val (nth 1 x))
  5593. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5594. org-local-vars)))
  5595. (defun orgstruct-error ()
  5596. "Error when there is no default binding for a structure key."
  5597. (interactive)
  5598. (error "This key has no function outside structure elements"))
  5599. (defun orgstruct-setup ()
  5600. "Setup orgstruct keymaps."
  5601. (let ((nfunc 0)
  5602. (bindings
  5603. (list
  5604. '([(meta up)] org-metaup)
  5605. '([(meta down)] org-metadown)
  5606. '([(meta left)] org-metaleft)
  5607. '([(meta right)] org-metaright)
  5608. '([(meta shift up)] org-shiftmetaup)
  5609. '([(meta shift down)] org-shiftmetadown)
  5610. '([(meta shift left)] org-shiftmetaleft)
  5611. '([(meta shift right)] org-shiftmetaright)
  5612. '([(shift up)] org-shiftup)
  5613. '([(shift down)] org-shiftdown)
  5614. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5615. '("\M-q" fill-paragraph)
  5616. '("\C-c^" org-sort)
  5617. '("\C-c-" org-cycle-list-bullet)))
  5618. elt key fun cmd)
  5619. (while (setq elt (pop bindings))
  5620. (setq nfunc (1+ nfunc))
  5621. (setq key (org-key (car elt))
  5622. fun (nth 1 elt)
  5623. cmd (orgstruct-make-binding fun nfunc key))
  5624. (org-defkey orgstruct-mode-map key cmd))
  5625. ;; Special treatment needed for TAB and RET
  5626. (org-defkey orgstruct-mode-map [(tab)]
  5627. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5628. (org-defkey orgstruct-mode-map "\C-i"
  5629. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5630. (org-defkey orgstruct-mode-map "\M-\C-m"
  5631. (orgstruct-make-binding 'org-insert-heading 105
  5632. "\M-\C-m" [(meta return)]))
  5633. (org-defkey orgstruct-mode-map [(meta return)]
  5634. (orgstruct-make-binding 'org-insert-heading 106
  5635. [(meta return)] "\M-\C-m"))
  5636. (org-defkey orgstruct-mode-map [(shift meta return)]
  5637. (orgstruct-make-binding 'org-insert-todo-heading 107
  5638. [(meta return)] "\M-\C-m"))
  5639. (unless org-local-vars
  5640. (setq org-local-vars (org-get-local-variables)))
  5641. t))
  5642. (defun orgstruct-make-binding (fun n &rest keys)
  5643. "Create a function for binding in the structure minor mode.
  5644. FUN is the command to call inside a table. N is used to create a unique
  5645. command name. KEYS are keys that should be checked in for a command
  5646. to execute outside of tables."
  5647. (eval
  5648. (list 'defun
  5649. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5650. '(arg)
  5651. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5652. "Outside of structure, run the binding of `"
  5653. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5654. "'.")
  5655. '(interactive "p")
  5656. (list 'if
  5657. '(org-context-p 'headline 'item)
  5658. (list 'org-run-like-in-org-mode (list 'quote fun))
  5659. (list 'let '(orgstruct-mode)
  5660. (list 'call-interactively
  5661. (append '(or)
  5662. (mapcar (lambda (k)
  5663. (list 'key-binding k))
  5664. keys)
  5665. '('orgstruct-error))))))))
  5666. (defun org-context-p (&rest contexts)
  5667. "Check if local context is and of CONTEXTS.
  5668. Possible values in the list of contexts are `table', `headline', and `item'."
  5669. (let ((pos (point)))
  5670. (goto-char (point-at-bol))
  5671. (prog1 (or (and (memq 'table contexts)
  5672. (looking-at "[ \t]*|"))
  5673. (and (memq 'headline contexts)
  5674. (looking-at "\\*+"))
  5675. (and (memq 'item contexts)
  5676. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5677. (goto-char pos))))
  5678. (defun org-get-local-variables ()
  5679. "Return a list of all local variables in an org-mode buffer."
  5680. (let (varlist)
  5681. (with-current-buffer (get-buffer-create "*Org tmp*")
  5682. (erase-buffer)
  5683. (org-mode)
  5684. (setq varlist (buffer-local-variables)))
  5685. (kill-buffer "*Org tmp*")
  5686. (delq nil
  5687. (mapcar
  5688. (lambda (x)
  5689. (setq x
  5690. (if (symbolp x)
  5691. (list x)
  5692. (list (car x) (list 'quote (cdr x)))))
  5693. (if (string-match
  5694. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5695. (symbol-name (car x)))
  5696. x nil))
  5697. varlist))))
  5698. ;;;###autoload
  5699. (defun org-run-like-in-org-mode (cmd)
  5700. (org-load-modules-maybe)
  5701. (unless org-local-vars
  5702. (setq org-local-vars (org-get-local-variables)))
  5703. (eval (list 'let org-local-vars
  5704. (list 'call-interactively (list 'quote cmd)))))
  5705. ;;;; Archiving
  5706. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  5707. (defun org-archive-subtree (&optional find-done)
  5708. "Move the current subtree to the archive.
  5709. The archive can be a certain top-level heading in the current file, or in
  5710. a different file. The tree will be moved to that location, the subtree
  5711. heading be marked DONE, and the current time will be added.
  5712. When called with prefix argument FIND-DONE, find whole trees without any
  5713. open TODO items and archive them (after getting confirmation from the user).
  5714. If the cursor is not at a headline when this comand is called, try all level
  5715. 1 trees. If the cursor is on a headline, only try the direct children of
  5716. this heading."
  5717. (interactive "P")
  5718. (if find-done
  5719. (org-archive-all-done)
  5720. ;; Save all relevant TODO keyword-relatex variables
  5721. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  5722. (tr-org-todo-keywords-1 org-todo-keywords-1)
  5723. (tr-org-todo-kwd-alist org-todo-kwd-alist)
  5724. (tr-org-done-keywords org-done-keywords)
  5725. (tr-org-todo-regexp org-todo-regexp)
  5726. (tr-org-todo-line-regexp org-todo-line-regexp)
  5727. (tr-org-odd-levels-only org-odd-levels-only)
  5728. (this-buffer (current-buffer))
  5729. (org-archive-location org-archive-location)
  5730. (re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
  5731. ;; start of variables that will be used for saving context
  5732. ;; The compiler complains about them - keep them anyway!
  5733. (file (abbreviate-file-name (buffer-file-name)))
  5734. (olpath (mapconcat 'identity (org-get-outline-path) "/"))
  5735. (time (format-time-string
  5736. (substring (cdr org-time-stamp-formats) 1 -1)
  5737. (current-time)))
  5738. afile heading buffer level newfile-p
  5739. category todo priority
  5740. ;; start of variables that will be used for savind context
  5741. ltags itags prop)
  5742. ;; Try to find a local archive location
  5743. (save-excursion
  5744. (save-restriction
  5745. (widen)
  5746. (setq prop (org-entry-get nil "ARCHIVE" 'inherit))
  5747. (if (and prop (string-match "\\S-" prop))
  5748. (setq org-archive-location prop)
  5749. (if (or (re-search-backward re nil t)
  5750. (re-search-forward re nil t))
  5751. (setq org-archive-location (match-string 1))))))
  5752. (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
  5753. (progn
  5754. (setq afile (format (match-string 1 org-archive-location)
  5755. (file-name-nondirectory buffer-file-name))
  5756. heading (match-string 2 org-archive-location)))
  5757. (error "Invalid `org-archive-location'"))
  5758. (if (> (length afile) 0)
  5759. (setq newfile-p (not (file-exists-p afile))
  5760. buffer (find-file-noselect afile))
  5761. (setq buffer (current-buffer)))
  5762. (unless buffer
  5763. (error "Cannot access file \"%s\"" afile))
  5764. (if (and (> (length heading) 0)
  5765. (string-match "^\\*+" heading))
  5766. (setq level (match-end 0))
  5767. (setq heading nil level 0))
  5768. (save-excursion
  5769. (org-back-to-heading t)
  5770. ;; Get context information that will be lost by moving the tree
  5771. (org-refresh-category-properties)
  5772. (setq category (org-get-category)
  5773. todo (and (looking-at org-todo-line-regexp)
  5774. (match-string 2))
  5775. priority (org-get-priority (if (match-end 3) (match-string 3) ""))
  5776. ltags (org-get-tags)
  5777. itags (org-delete-all ltags (org-get-tags-at)))
  5778. (setq ltags (mapconcat 'identity ltags " ")
  5779. itags (mapconcat 'identity itags " "))
  5780. ;; We first only copy, in case something goes wrong
  5781. ;; we need to protect this-command, to avoid kill-region sets it,
  5782. ;; which would lead to duplication of subtrees
  5783. (let (this-command) (org-copy-subtree))
  5784. (set-buffer buffer)
  5785. ;; Enforce org-mode for the archive buffer
  5786. (if (not (org-mode-p))
  5787. ;; Force the mode for future visits.
  5788. (let ((org-insert-mode-line-in-empty-file t)
  5789. (org-inhibit-startup t))
  5790. (call-interactively 'org-mode)))
  5791. (when newfile-p
  5792. (goto-char (point-max))
  5793. (insert (format "\nArchived entries from file %s\n\n"
  5794. (buffer-file-name this-buffer))))
  5795. ;; Force the TODO keywords of the original buffer
  5796. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  5797. (org-todo-keywords-1 tr-org-todo-keywords-1)
  5798. (org-todo-kwd-alist tr-org-todo-kwd-alist)
  5799. (org-done-keywords tr-org-done-keywords)
  5800. (org-todo-regexp tr-org-todo-regexp)
  5801. (org-todo-line-regexp tr-org-todo-line-regexp)
  5802. (org-odd-levels-only
  5803. (if (local-variable-p 'org-odd-levels-only (current-buffer))
  5804. org-odd-levels-only
  5805. tr-org-odd-levels-only)))
  5806. (goto-char (point-min))
  5807. (show-all)
  5808. (if heading
  5809. (progn
  5810. (if (re-search-forward
  5811. (concat "^" (regexp-quote heading)
  5812. (org-re "[ \t]*\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\($\\|\r\\)"))
  5813. nil t)
  5814. (goto-char (match-end 0))
  5815. ;; Heading not found, just insert it at the end
  5816. (goto-char (point-max))
  5817. (or (bolp) (insert "\n"))
  5818. (insert "\n" heading "\n")
  5819. (end-of-line 0))
  5820. ;; Make the subtree visible
  5821. (show-subtree)
  5822. (org-end-of-subtree t)
  5823. (skip-chars-backward " \t\r\n")
  5824. (and (looking-at "[ \t\r\n]*")
  5825. (replace-match "\n\n")))
  5826. ;; No specific heading, just go to end of file.
  5827. (goto-char (point-max)) (insert "\n"))
  5828. ;; Paste
  5829. (org-paste-subtree (org-get-valid-level level 1))
  5830. ;; Mark the entry as done
  5831. (when (and org-archive-mark-done
  5832. (looking-at org-todo-line-regexp)
  5833. (or (not (match-end 2))
  5834. (not (member (match-string 2) org-done-keywords))))
  5835. (let (org-log-done org-todo-log-states)
  5836. (org-todo
  5837. (car (or (member org-archive-mark-done org-done-keywords)
  5838. org-done-keywords)))))
  5839. ;; Add the context info
  5840. (when org-archive-save-context-info
  5841. (let ((l org-archive-save-context-info) e n v)
  5842. (while (setq e (pop l))
  5843. (when (and (setq v (symbol-value e))
  5844. (stringp v) (string-match "\\S-" v))
  5845. (setq n (concat "ARCHIVE_" (upcase (symbol-name e))))
  5846. (org-entry-put (point) n v)))))
  5847. ;; Save and kill the buffer, if it is not the same buffer.
  5848. (if (not (eq this-buffer buffer))
  5849. (progn (save-buffer) (kill-buffer buffer)))))
  5850. ;; Here we are back in the original buffer. Everything seems to have
  5851. ;; worked. So now cut the tree and finish up.
  5852. (let (this-command) (org-cut-subtree))
  5853. (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
  5854. (message "Subtree archived %s"
  5855. (if (eq this-buffer buffer)
  5856. (concat "under heading: " heading)
  5857. (concat "in file: " (abbreviate-file-name afile)))))))
  5858. (defun org-refresh-category-properties ()
  5859. "Refresh category text properties in teh buffer."
  5860. (let ((def-cat (cond
  5861. ((null org-category)
  5862. (if buffer-file-name
  5863. (file-name-sans-extension
  5864. (file-name-nondirectory buffer-file-name))
  5865. "???"))
  5866. ((symbolp org-category) (symbol-name org-category))
  5867. (t org-category)))
  5868. beg end cat pos optionp)
  5869. (org-unmodified
  5870. (save-excursion
  5871. (save-restriction
  5872. (widen)
  5873. (goto-char (point-min))
  5874. (put-text-property (point) (point-max) 'org-category def-cat)
  5875. (while (re-search-forward
  5876. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5877. (setq pos (match-end 0)
  5878. optionp (equal (char-after (match-beginning 0)) ?#)
  5879. cat (org-trim (match-string 2)))
  5880. (if optionp
  5881. (setq beg (point-at-bol) end (point-max))
  5882. (org-back-to-heading t)
  5883. (setq beg (point) end (org-end-of-subtree t t)))
  5884. (put-text-property beg end 'org-category cat)
  5885. (goto-char pos)))))))
  5886. (defun org-archive-all-done (&optional tag)
  5887. "Archive sublevels of the current tree without open TODO items.
  5888. If the cursor is not on a headline, try all level 1 trees. If
  5889. it is on a headline, try all direct children.
  5890. When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
  5891. (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
  5892. (rea (concat ".*:" org-archive-tag ":"))
  5893. (begm (make-marker))
  5894. (endm (make-marker))
  5895. (question (if tag "Set ARCHIVE tag (no open TODO items)? "
  5896. "Move subtree to archive (no open TODO items)? "))
  5897. beg end (cntarch 0))
  5898. (if (org-on-heading-p)
  5899. (progn
  5900. (setq re1 (concat "^" (regexp-quote
  5901. (make-string
  5902. (1+ (- (match-end 0) (match-beginning 0) 1))
  5903. ?*))
  5904. " "))
  5905. (move-marker begm (point))
  5906. (move-marker endm (org-end-of-subtree t)))
  5907. (setq re1 "^* ")
  5908. (move-marker begm (point-min))
  5909. (move-marker endm (point-max)))
  5910. (save-excursion
  5911. (goto-char begm)
  5912. (while (re-search-forward re1 endm t)
  5913. (setq beg (match-beginning 0)
  5914. end (save-excursion (org-end-of-subtree t) (point)))
  5915. (goto-char beg)
  5916. (if (re-search-forward re end t)
  5917. (goto-char end)
  5918. (goto-char beg)
  5919. (if (and (or (not tag) (not (looking-at rea)))
  5920. (y-or-n-p question))
  5921. (progn
  5922. (if tag
  5923. (org-toggle-tag org-archive-tag 'on)
  5924. (org-archive-subtree))
  5925. (setq cntarch (1+ cntarch)))
  5926. (goto-char end)))))
  5927. (message "%d trees archived" cntarch)))
  5928. (defun org-cycle-hide-drawers (state)
  5929. "Re-hide all drawers after a visibility state change."
  5930. (when (and (org-mode-p)
  5931. (not (memq state '(overview folded))))
  5932. (save-excursion
  5933. (let* ((globalp (memq state '(contents all)))
  5934. (beg (if globalp (point-min) (point)))
  5935. (end (if globalp (point-max) (org-end-of-subtree t))))
  5936. (goto-char beg)
  5937. (while (re-search-forward org-drawer-regexp end t)
  5938. (org-flag-drawer t))))))
  5939. (defun org-flag-drawer (flag)
  5940. (save-excursion
  5941. (beginning-of-line 1)
  5942. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5943. (let ((b (match-end 0))
  5944. (outline-regexp org-outline-regexp))
  5945. (if (re-search-forward
  5946. "^[ \t]*:END:"
  5947. (save-excursion (outline-next-heading) (point)) t)
  5948. (outline-flag-region b (point-at-eol) flag)
  5949. (error ":END: line missing"))))))
  5950. (defun org-cycle-hide-archived-subtrees (state)
  5951. "Re-hide all archived subtrees after a visibility state change."
  5952. (when (and (not org-cycle-open-archived-trees)
  5953. (not (memq state '(overview folded))))
  5954. (save-excursion
  5955. (let* ((globalp (memq state '(contents all)))
  5956. (beg (if globalp (point-min) (point)))
  5957. (end (if globalp (point-max) (org-end-of-subtree t))))
  5958. (org-hide-archived-subtrees beg end)
  5959. (goto-char beg)
  5960. (if (looking-at (concat ".*:" org-archive-tag ":"))
  5961. (message "%s" (substitute-command-keys
  5962. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  5963. (defun org-force-cycle-archived ()
  5964. "Cycle subtree even if it is archived."
  5965. (interactive)
  5966. (setq this-command 'org-cycle)
  5967. (let ((org-cycle-open-archived-trees t))
  5968. (call-interactively 'org-cycle)))
  5969. (defun org-hide-archived-subtrees (beg end)
  5970. "Re-hide all archived subtrees after a visibility state change."
  5971. (save-excursion
  5972. (let* ((re (concat ":" org-archive-tag ":")))
  5973. (goto-char beg)
  5974. (while (re-search-forward re end t)
  5975. (and (org-on-heading-p) (hide-subtree))
  5976. (org-end-of-subtree t)))))
  5977. (defun org-toggle-tag (tag &optional onoff)
  5978. "Toggle the tag TAG for the current line.
  5979. If ONOFF is `on' or `off', don't toggle but set to this state."
  5980. (unless (org-on-heading-p t) (error "Not on headling"))
  5981. (let (res current)
  5982. (save-excursion
  5983. (beginning-of-line)
  5984. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  5985. (point-at-eol) t)
  5986. (progn
  5987. (setq current (match-string 1))
  5988. (replace-match ""))
  5989. (setq current ""))
  5990. (setq current (nreverse (org-split-string current ":")))
  5991. (cond
  5992. ((eq onoff 'on)
  5993. (setq res t)
  5994. (or (member tag current) (push tag current)))
  5995. ((eq onoff 'off)
  5996. (or (not (member tag current)) (setq current (delete tag current))))
  5997. (t (if (member tag current)
  5998. (setq current (delete tag current))
  5999. (setq res t)
  6000. (push tag current))))
  6001. (end-of-line 1)
  6002. (if current
  6003. (progn
  6004. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  6005. (org-set-tags nil t))
  6006. (delete-horizontal-space))
  6007. (run-hooks 'org-after-tags-change-hook))
  6008. res))
  6009. (defun org-toggle-archive-tag (&optional arg)
  6010. "Toggle the archive tag for the current headline.
  6011. With prefix ARG, check all children of current headline and offer tagging
  6012. the children that do not contain any open TODO items."
  6013. (interactive "P")
  6014. (if arg
  6015. (org-archive-all-done 'tag)
  6016. (let (set)
  6017. (save-excursion
  6018. (org-back-to-heading t)
  6019. (setq set (org-toggle-tag org-archive-tag))
  6020. (when set (hide-subtree)))
  6021. (and set (beginning-of-line 1))
  6022. (message "Subtree %s" (if set "archived" "unarchived")))))
  6023. ;;;; Link Stuff
  6024. ;;; Link abbreviations
  6025. (defun org-link-expand-abbrev (link)
  6026. "Apply replacements as defined in `org-link-abbrev-alist."
  6027. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6028. (let* ((key (match-string 1 link))
  6029. (as (or (assoc key org-link-abbrev-alist-local)
  6030. (assoc key org-link-abbrev-alist)))
  6031. (tag (and (match-end 2) (match-string 3 link)))
  6032. rpl)
  6033. (if (not as)
  6034. link
  6035. (setq rpl (cdr as))
  6036. (cond
  6037. ((symbolp rpl) (funcall rpl tag))
  6038. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6039. (t (concat rpl tag)))))
  6040. link))
  6041. ;;; Storing and inserting links
  6042. (defvar org-insert-link-history nil
  6043. "Minibuffer history for links inserted with `org-insert-link'.")
  6044. (defvar org-stored-links nil
  6045. "Contains the links stored with `org-store-link'.")
  6046. (defvar org-store-link-plist nil
  6047. "Plist with info about the most recently link created with `org-store-link'.")
  6048. (defvar org-link-protocols nil
  6049. "Link protocols added to Org-mode using `org-add-link-type'.")
  6050. (defvar org-store-link-functions nil
  6051. "List of functions that are called to create and store a link.
  6052. Each function will be called in turn until one returns a non-nil
  6053. value. Each function should check if it is responsible for creating
  6054. this link (for example by looking at the major mode).
  6055. If not, it must exit and return nil.
  6056. If yes, it should return a non-nil value after a calling
  6057. `org-store-link-props' with a list of properties and values.
  6058. Special properties are:
  6059. :type The link prefix. like \"http\". This must be given.
  6060. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6061. This is obligatory as well.
  6062. :description Optional default description for the second pair
  6063. of brackets in an Org-mode link. The user can still change
  6064. this when inserting this link into an Org-mode buffer.
  6065. In addition to these, any additional properties can be specified
  6066. and then used in remember templates.")
  6067. (defun org-add-link-type (type &optional follow export)
  6068. "Add TYPE to the list of `org-link-types'.
  6069. Re-compute all regular expressions depending on `org-link-types'
  6070. FOLLOW and EXPORT are two functions.
  6071. FOLLOW should take the link path as the single argument and do whatever
  6072. is necessary to follow the link, for example find a file or display
  6073. a mail message.
  6074. EXPORT should format the link path for export to one of the export formats.
  6075. It should be a function accepting three arguments:
  6076. path the path of the link, the text after the prefix (like \"http:\")
  6077. desc the description of the link, if any, nil if there was no descripton
  6078. format the export format, a symbol like `html' or `latex'.
  6079. The function may use the FORMAT information to return different values
  6080. depending on the format. The return value will be put literally into
  6081. the exported file.
  6082. Org-mode has a built-in default for exporting links. If you are happy with
  6083. this default, there is no need to define an export function for the link
  6084. type. For a simple example of an export function, see `org-bbdb.el'."
  6085. (add-to-list 'org-link-types type t)
  6086. (org-make-link-regexps)
  6087. (if (assoc type org-link-protocols)
  6088. (setcdr (assoc type org-link-protocols) (list follow export))
  6089. (push (list type follow export) org-link-protocols)))
  6090. ;;;###autoload
  6091. (defun org-store-link (arg)
  6092. "\\<org-mode-map>Store an org-link to the current location.
  6093. This link is added to `org-stored-links' and can later be inserted
  6094. into an org-buffer with \\[org-insert-link].
  6095. For some link types, a prefix arg is interpreted:
  6096. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  6097. For file links, arg negates `org-context-in-file-links'."
  6098. (interactive "P")
  6099. (org-load-modules-maybe)
  6100. (setq org-store-link-plist nil) ; reset
  6101. (let (link cpltxt desc description search txt)
  6102. (cond
  6103. ((run-hook-with-args-until-success 'org-store-link-functions)
  6104. (setq link (plist-get org-store-link-plist :link)
  6105. desc (or (plist-get org-store-link-plist :description) link)))
  6106. ((eq major-mode 'calendar-mode)
  6107. (let ((cd (calendar-cursor-to-date)))
  6108. (setq link
  6109. (format-time-string
  6110. (car org-time-stamp-formats)
  6111. (apply 'encode-time
  6112. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6113. nil nil nil))))
  6114. (org-store-link-props :type "calendar" :date cd)))
  6115. ((eq major-mode 'w3-mode)
  6116. (setq cpltxt (url-view-url t)
  6117. link (org-make-link cpltxt))
  6118. (org-store-link-props :type "w3" :url (url-view-url t)))
  6119. ((eq major-mode 'w3m-mode)
  6120. (setq cpltxt (or w3m-current-title w3m-current-url)
  6121. link (org-make-link w3m-current-url))
  6122. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6123. ((setq search (run-hook-with-args-until-success
  6124. 'org-create-file-search-functions))
  6125. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6126. "::" search))
  6127. (setq cpltxt (or description link)))
  6128. ((eq major-mode 'image-mode)
  6129. (setq cpltxt (concat "file:"
  6130. (abbreviate-file-name buffer-file-name))
  6131. link (org-make-link cpltxt))
  6132. (org-store-link-props :type "image" :file buffer-file-name))
  6133. ((eq major-mode 'dired-mode)
  6134. ;; link to the file in the current line
  6135. (setq cpltxt (concat "file:"
  6136. (abbreviate-file-name
  6137. (expand-file-name
  6138. (dired-get-filename nil t))))
  6139. link (org-make-link cpltxt)))
  6140. ((and buffer-file-name (org-mode-p))
  6141. ;; Just link to current headline
  6142. (setq cpltxt (concat "file:"
  6143. (abbreviate-file-name buffer-file-name)))
  6144. ;; Add a context search string
  6145. (when (org-xor org-context-in-file-links arg)
  6146. ;; Check if we are on a target
  6147. (if (org-in-regexp "<<\\(.*?\\)>>")
  6148. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  6149. (setq txt (cond
  6150. ((org-on-heading-p) nil)
  6151. ((org-region-active-p)
  6152. (buffer-substring (region-beginning) (region-end)))
  6153. (t (buffer-substring (point-at-bol) (point-at-eol)))))
  6154. (when (or (null txt) (string-match "\\S-" txt))
  6155. (setq cpltxt
  6156. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6157. desc "NONE"))))
  6158. (if (string-match "::\\'" cpltxt)
  6159. (setq cpltxt (substring cpltxt 0 -2)))
  6160. (setq link (org-make-link cpltxt)))
  6161. ((buffer-file-name (buffer-base-buffer))
  6162. ;; Just link to this file here.
  6163. (setq cpltxt (concat "file:"
  6164. (abbreviate-file-name
  6165. (buffer-file-name (buffer-base-buffer)))))
  6166. ;; Add a context string
  6167. (when (org-xor org-context-in-file-links arg)
  6168. (setq txt (if (org-region-active-p)
  6169. (buffer-substring (region-beginning) (region-end))
  6170. (buffer-substring (point-at-bol) (point-at-eol))))
  6171. ;; Only use search option if there is some text.
  6172. (when (string-match "\\S-" txt)
  6173. (setq cpltxt
  6174. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6175. desc "NONE")))
  6176. (setq link (org-make-link cpltxt)))
  6177. ((interactive-p)
  6178. (error "Cannot link to a buffer which is not visiting a file"))
  6179. (t (setq link nil)))
  6180. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6181. (setq link (or link cpltxt)
  6182. desc (or desc cpltxt))
  6183. (if (equal desc "NONE") (setq desc nil))
  6184. (if (and (interactive-p) link)
  6185. (progn
  6186. (setq org-stored-links
  6187. (cons (list link desc) org-stored-links))
  6188. (message "Stored: %s" (or desc link)))
  6189. (and link (org-make-link-string link desc)))))
  6190. (defun org-store-link-props (&rest plist)
  6191. "Store link properties, extract names and addresses."
  6192. (let (x adr)
  6193. (when (setq x (plist-get plist :from))
  6194. (setq adr (mail-extract-address-components x))
  6195. (plist-put plist :fromname (car adr))
  6196. (plist-put plist :fromaddress (nth 1 adr)))
  6197. (when (setq x (plist-get plist :to))
  6198. (setq adr (mail-extract-address-components x))
  6199. (plist-put plist :toname (car adr))
  6200. (plist-put plist :toaddress (nth 1 adr))))
  6201. (let ((from (plist-get plist :from))
  6202. (to (plist-get plist :to)))
  6203. (when (and from to org-from-is-user-regexp)
  6204. (plist-put plist :fromto
  6205. (if (string-match org-from-is-user-regexp from)
  6206. (concat "to %t")
  6207. (concat "from %f")))))
  6208. (setq org-store-link-plist plist))
  6209. (defun org-add-link-props (&rest plist)
  6210. "Add these properties to the link property list."
  6211. (let (key value)
  6212. (while plist
  6213. (setq key (pop plist) value (pop plist))
  6214. (setq org-store-link-plist
  6215. (plist-put org-store-link-plist key value)))))
  6216. (defun org-email-link-description (&optional fmt)
  6217. "Return the description part of an email link.
  6218. This takes information from `org-store-link-plist' and formats it
  6219. according to FMT (default from `org-email-link-description-format')."
  6220. (setq fmt (or fmt org-email-link-description-format))
  6221. (let* ((p org-store-link-plist)
  6222. (to (plist-get p :toaddress))
  6223. (from (plist-get p :fromaddress))
  6224. (table
  6225. (list
  6226. (cons "%c" (plist-get p :fromto))
  6227. (cons "%F" (plist-get p :from))
  6228. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6229. (cons "%T" (plist-get p :to))
  6230. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6231. (cons "%s" (plist-get p :subject))
  6232. (cons "%m" (plist-get p :message-id)))))
  6233. (when (string-match "%c" fmt)
  6234. ;; Check if the user wrote this message
  6235. (if (and org-from-is-user-regexp from to
  6236. (save-match-data (string-match org-from-is-user-regexp from)))
  6237. (setq fmt (replace-match "to %t" t t fmt))
  6238. (setq fmt (replace-match "from %f" t t fmt))))
  6239. (org-replace-escapes fmt table)))
  6240. (defun org-make-org-heading-search-string (&optional string heading)
  6241. "Make search string for STRING or current headline."
  6242. (interactive)
  6243. (let ((s (or string (org-get-heading))))
  6244. (unless (and string (not heading))
  6245. ;; We are using a headline, clean up garbage in there.
  6246. (if (string-match org-todo-regexp s)
  6247. (setq s (replace-match "" t t s)))
  6248. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6249. (setq s (replace-match "" t t s)))
  6250. (setq s (org-trim s))
  6251. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6252. org-comment-string "\\)") s)
  6253. (setq s (replace-match "" t t s)))
  6254. (while (string-match org-ts-regexp s)
  6255. (setq s (replace-match "" t t s))))
  6256. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6257. (setq s (replace-match " " t t s)))
  6258. (or string (setq s (concat "*" s))) ; Add * for headlines
  6259. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6260. (defun org-make-link (&rest strings)
  6261. "Concatenate STRINGS."
  6262. (apply 'concat strings))
  6263. (defun org-make-link-string (link &optional description)
  6264. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6265. (unless (string-match "\\S-" link)
  6266. (error "Empty link"))
  6267. (when (stringp description)
  6268. ;; Remove brackets from the description, they are fatal.
  6269. (while (string-match "\\[" description)
  6270. (setq description (replace-match "{" t t description)))
  6271. (while (string-match "\\]" description)
  6272. (setq description (replace-match "}" t t description))))
  6273. (when (equal (org-link-escape link) description)
  6274. ;; No description needed, it is identical
  6275. (setq description nil))
  6276. (when (and (not description)
  6277. (not (equal link (org-link-escape link))))
  6278. (setq description link))
  6279. (concat "[[" (org-link-escape link) "]"
  6280. (if description (concat "[" description "]") "")
  6281. "]"))
  6282. (defconst org-link-escape-chars
  6283. '((?\ . "%20")
  6284. (?\[ . "%5B")
  6285. (?\] . "%5D")
  6286. (?\340 . "%E0") ; `a
  6287. (?\342 . "%E2") ; ^a
  6288. (?\347 . "%E7") ; ,c
  6289. (?\350 . "%E8") ; `e
  6290. (?\351 . "%E9") ; 'e
  6291. (?\352 . "%EA") ; ^e
  6292. (?\356 . "%EE") ; ^i
  6293. (?\364 . "%F4") ; ^o
  6294. (?\371 . "%F9") ; `u
  6295. (?\373 . "%FB") ; ^u
  6296. (?\; . "%3B")
  6297. (?? . "%3F")
  6298. (?= . "%3D")
  6299. (?+ . "%2B")
  6300. )
  6301. "Association list of escapes for some characters problematic in links.
  6302. This is the list that is used for internal purposes.")
  6303. (defconst org-link-escape-chars-browser
  6304. '((?\ . "%20")) ; 32 for the SPC char
  6305. "Association list of escapes for some characters problematic in links.
  6306. This is the list that is used before handing over to the browser.")
  6307. (defun org-link-escape (text &optional table)
  6308. "Escape charaters in TEXT that are problematic for links."
  6309. (setq table (or table org-link-escape-chars))
  6310. (when text
  6311. (let ((re (mapconcat (lambda (x) (regexp-quote
  6312. (char-to-string (car x))))
  6313. table "\\|")))
  6314. (while (string-match re text)
  6315. (setq text
  6316. (replace-match
  6317. (cdr (assoc (string-to-char (match-string 0 text))
  6318. table))
  6319. t t text)))
  6320. text)))
  6321. (defun org-link-unescape (text &optional table)
  6322. "Reverse the action of `org-link-escape'."
  6323. (setq table (or table org-link-escape-chars))
  6324. (when text
  6325. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6326. table "\\|")))
  6327. (while (string-match re text)
  6328. (setq text
  6329. (replace-match
  6330. (char-to-string (car (rassoc (match-string 0 text) table)))
  6331. t t text)))
  6332. text)))
  6333. (defun org-xor (a b)
  6334. "Exclusive or."
  6335. (if a (not b) b))
  6336. (defun org-get-header (header)
  6337. "Find a header field in the current buffer."
  6338. (save-excursion
  6339. (goto-char (point-min))
  6340. (let ((case-fold-search t) s)
  6341. (cond
  6342. ((eq header 'from)
  6343. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  6344. (setq s (match-string 1)))
  6345. (while (string-match "\"" s)
  6346. (setq s (replace-match "" t t s)))
  6347. (if (string-match "[<(].*" s)
  6348. (setq s (replace-match "" t t s))))
  6349. ((eq header 'message-id)
  6350. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  6351. (setq s (match-string 1))))
  6352. ((eq header 'subject)
  6353. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  6354. (setq s (match-string 1)))))
  6355. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  6356. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  6357. s)))
  6358. (defun org-fixup-message-id-for-http (s)
  6359. "Replace special characters in a message id, so it can be used in an http query."
  6360. (while (string-match "<" s)
  6361. (setq s (replace-match "%3C" t t s)))
  6362. (while (string-match ">" s)
  6363. (setq s (replace-match "%3E" t t s)))
  6364. (while (string-match "@" s)
  6365. (setq s (replace-match "%40" t t s)))
  6366. s)
  6367. ;;;###autoload
  6368. (defun org-insert-link-global ()
  6369. "Insert a link like Org-mode does.
  6370. This command can be called in any mode to insert a link in Org-mode syntax."
  6371. (interactive)
  6372. (org-load-modules-maybe)
  6373. (org-run-like-in-org-mode 'org-insert-link))
  6374. (defun org-insert-link (&optional complete-file)
  6375. "Insert a link. At the prompt, enter the link.
  6376. Completion can be used to select a link previously stored with
  6377. `org-store-link'. When the empty string is entered (i.e. if you just
  6378. press RET at the prompt), the link defaults to the most recently
  6379. stored link. As SPC triggers completion in the minibuffer, you need to
  6380. use M-SPC or C-q SPC to force the insertion of a space character.
  6381. You will also be prompted for a description, and if one is given, it will
  6382. be displayed in the buffer instead of the link.
  6383. If there is already a link at point, this command will allow you to edit link
  6384. and description parts.
  6385. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
  6386. selected using completion. The path to the file will be relative to
  6387. the current directory if the file is in the current directory or a
  6388. subdirectory. Otherwise, the link will be the absolute path as
  6389. completed in the minibuffer (i.e. normally ~/path/to/file).
  6390. With two \\[universal-argument] prefixes, enforce an absolute path even if the file
  6391. is in the current directory or below.
  6392. With three \\[universal-argument] prefixes, negate the meaning of
  6393. `org-keep-stored-link-after-insertion'."
  6394. (interactive "P")
  6395. (let* ((wcf (current-window-configuration))
  6396. (region (if (org-region-active-p)
  6397. (buffer-substring (region-beginning) (region-end))))
  6398. (remove (and region (list (region-beginning) (region-end))))
  6399. (desc region)
  6400. tmphist ; byte-compile incorrectly complains about this
  6401. link entry file)
  6402. (cond
  6403. ((org-in-regexp org-bracket-link-regexp 1)
  6404. ;; We do have a link at point, and we are going to edit it.
  6405. (setq remove (list (match-beginning 0) (match-end 0)))
  6406. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6407. (setq link (read-string "Link: "
  6408. (org-link-unescape
  6409. (org-match-string-no-properties 1)))))
  6410. ((or (org-in-regexp org-angle-link-re)
  6411. (org-in-regexp org-plain-link-re))
  6412. ;; Convert to bracket link
  6413. (setq remove (list (match-beginning 0) (match-end 0))
  6414. link (read-string "Link: "
  6415. (org-remove-angle-brackets (match-string 0)))))
  6416. ((equal complete-file '(4))
  6417. ;; Completing read for file names.
  6418. (setq file (read-file-name "File: "))
  6419. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6420. (pwd1 (file-name-as-directory (abbreviate-file-name
  6421. (expand-file-name ".")))))
  6422. (cond
  6423. ((equal complete-file '(16))
  6424. (setq link (org-make-link
  6425. "file:"
  6426. (abbreviate-file-name (expand-file-name file)))))
  6427. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6428. (setq link (org-make-link "file:" (match-string 1 file))))
  6429. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6430. (expand-file-name file))
  6431. (setq link (org-make-link
  6432. "file:" (match-string 1 (expand-file-name file)))))
  6433. (t (setq link (org-make-link "file:" file))))))
  6434. (t
  6435. ;; Read link, with completion for stored links.
  6436. (with-output-to-temp-buffer "*Org Links*"
  6437. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6438. (when org-stored-links
  6439. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6440. (princ (mapconcat
  6441. (lambda (x)
  6442. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6443. (reverse org-stored-links) "\n"))))
  6444. (let ((cw (selected-window)))
  6445. (select-window (get-buffer-window "*Org Links*"))
  6446. (shrink-window-if-larger-than-buffer)
  6447. (setq truncate-lines t)
  6448. (select-window cw))
  6449. ;; Fake a link history, containing the stored links.
  6450. (setq tmphist (append (mapcar 'car org-stored-links)
  6451. org-insert-link-history))
  6452. (unwind-protect
  6453. (setq link (org-completing-read
  6454. "Link: "
  6455. (append
  6456. (mapcar (lambda (x) (list (concat (car x) ":")))
  6457. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6458. (mapcar (lambda (x) (list (concat x ":")))
  6459. org-link-types))
  6460. nil nil nil
  6461. 'tmphist
  6462. (or (car (car org-stored-links)))))
  6463. (set-window-configuration wcf)
  6464. (kill-buffer "*Org Links*"))
  6465. (setq entry (assoc link org-stored-links))
  6466. (or entry (push link org-insert-link-history))
  6467. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6468. (not org-keep-stored-link-after-insertion))
  6469. (setq org-stored-links (delq (assoc link org-stored-links)
  6470. org-stored-links)))
  6471. (setq desc (or desc (nth 1 entry)))))
  6472. (if (string-match org-plain-link-re link)
  6473. ;; URL-like link, normalize the use of angular brackets.
  6474. (setq link (org-make-link (org-remove-angle-brackets link))))
  6475. ;; Check if we are linking to the current file with a search option
  6476. ;; If yes, simplify the link by using only the search option.
  6477. (when (and buffer-file-name
  6478. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  6479. (let* ((path (match-string 1 link))
  6480. (case-fold-search nil)
  6481. (search (match-string 2 link)))
  6482. (save-match-data
  6483. (if (equal (file-truename buffer-file-name) (file-truename path))
  6484. ;; We are linking to this same file, with a search option
  6485. (setq link search)))))
  6486. ;; Check if we can/should use a relative path. If yes, simplify the link
  6487. (when (string-match "\\<file:\\(.*\\)" link)
  6488. (let* ((path (match-string 1 link))
  6489. (origpath path)
  6490. (case-fold-search nil))
  6491. (cond
  6492. ((eq org-link-file-path-type 'absolute)
  6493. (setq path (abbreviate-file-name (expand-file-name path))))
  6494. ((eq org-link-file-path-type 'noabbrev)
  6495. (setq path (expand-file-name path)))
  6496. ((eq org-link-file-path-type 'relative)
  6497. (setq path (file-relative-name path)))
  6498. (t
  6499. (save-match-data
  6500. (if (string-match (concat "^" (regexp-quote
  6501. (file-name-as-directory
  6502. (expand-file-name "."))))
  6503. (expand-file-name path))
  6504. ;; We are linking a file with relative path name.
  6505. (setq path (substring (expand-file-name path)
  6506. (match-end 0)))))))
  6507. (setq link (concat "file:" path))
  6508. (if (equal desc origpath)
  6509. (setq desc path))))
  6510. (setq desc (read-string "Description: " desc))
  6511. (unless (string-match "\\S-" desc) (setq desc nil))
  6512. (if remove (apply 'delete-region remove))
  6513. (insert (org-make-link-string link desc))))
  6514. (defun org-completing-read (&rest args)
  6515. (let ((minibuffer-local-completion-map
  6516. (copy-keymap minibuffer-local-completion-map)))
  6517. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6518. (apply 'completing-read args)))
  6519. ;;; Opening/following a link
  6520. (defvar org-link-search-failed nil)
  6521. (defun org-next-link ()
  6522. "Move forward to the next link.
  6523. If the link is in hidden text, expose it."
  6524. (interactive)
  6525. (when (and org-link-search-failed (eq this-command last-command))
  6526. (goto-char (point-min))
  6527. (message "Link search wrapped back to beginning of buffer"))
  6528. (setq org-link-search-failed nil)
  6529. (let* ((pos (point))
  6530. (ct (org-context))
  6531. (a (assoc :link ct)))
  6532. (if a (goto-char (nth 2 a)))
  6533. (if (re-search-forward org-any-link-re nil t)
  6534. (progn
  6535. (goto-char (match-beginning 0))
  6536. (if (org-invisible-p) (org-show-context)))
  6537. (goto-char pos)
  6538. (setq org-link-search-failed t)
  6539. (error "No further link found"))))
  6540. (defun org-previous-link ()
  6541. "Move backward to the previous link.
  6542. If the link is in hidden text, expose it."
  6543. (interactive)
  6544. (when (and org-link-search-failed (eq this-command last-command))
  6545. (goto-char (point-max))
  6546. (message "Link search wrapped back to end of buffer"))
  6547. (setq org-link-search-failed nil)
  6548. (let* ((pos (point))
  6549. (ct (org-context))
  6550. (a (assoc :link ct)))
  6551. (if a (goto-char (nth 1 a)))
  6552. (if (re-search-backward org-any-link-re nil t)
  6553. (progn
  6554. (goto-char (match-beginning 0))
  6555. (if (org-invisible-p) (org-show-context)))
  6556. (goto-char pos)
  6557. (setq org-link-search-failed t)
  6558. (error "No further link found"))))
  6559. (defun org-find-file-at-mouse (ev)
  6560. "Open file link or URL at mouse."
  6561. (interactive "e")
  6562. (mouse-set-point ev)
  6563. (org-open-at-point 'in-emacs))
  6564. (defun org-open-at-mouse (ev)
  6565. "Open file link or URL at mouse."
  6566. (interactive "e")
  6567. (mouse-set-point ev)
  6568. (org-open-at-point))
  6569. (defvar org-window-config-before-follow-link nil
  6570. "The window configuration before following a link.
  6571. This is saved in case the need arises to restore it.")
  6572. (defvar org-open-link-marker (make-marker)
  6573. "Marker pointing to the location where `org-open-at-point; was called.")
  6574. ;;;###autoload
  6575. (defun org-open-at-point-global ()
  6576. "Follow a link like Org-mode does.
  6577. This command can be called in any mode to follow a link that has
  6578. Org-mode syntax."
  6579. (interactive)
  6580. (org-run-like-in-org-mode 'org-open-at-point))
  6581. (defun org-open-at-point (&optional in-emacs)
  6582. "Open link at or after point.
  6583. If there is no link at point, this function will search forward up to
  6584. the end of the current subtree.
  6585. Normally, files will be opened by an appropriate application. If the
  6586. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  6587. (interactive "P")
  6588. (org-load-modules-maybe)
  6589. (move-marker org-open-link-marker (point))
  6590. (setq org-window-config-before-follow-link (current-window-configuration))
  6591. (org-remove-occur-highlights nil nil t)
  6592. (if (org-at-timestamp-p t)
  6593. (org-follow-timestamp-link)
  6594. (let (type path link line search (pos (point)))
  6595. (catch 'match
  6596. (save-excursion
  6597. (skip-chars-forward "^]\n\r")
  6598. (when (org-in-regexp org-bracket-link-regexp)
  6599. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  6600. (while (string-match " *\n *" link)
  6601. (setq link (replace-match " " t t link)))
  6602. (setq link (org-link-expand-abbrev link))
  6603. (if (string-match org-link-re-with-space2 link)
  6604. (setq type (match-string 1 link) path (match-string 2 link))
  6605. (setq type "thisfile" path link))
  6606. (throw 'match t)))
  6607. (when (get-text-property (point) 'org-linked-text)
  6608. (setq type "thisfile"
  6609. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6610. (1+ (point)) (point))
  6611. path (buffer-substring
  6612. (previous-single-property-change pos 'org-linked-text)
  6613. (next-single-property-change pos 'org-linked-text)))
  6614. (throw 'match t))
  6615. (save-excursion
  6616. (when (or (org-in-regexp org-angle-link-re)
  6617. (org-in-regexp org-plain-link-re))
  6618. (setq type (match-string 1) path (match-string 2))
  6619. (throw 'match t)))
  6620. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6621. (setq type "tree-match"
  6622. path (match-string 1))
  6623. (throw 'match t))
  6624. (save-excursion
  6625. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6626. (setq type "tags"
  6627. path (match-string 1))
  6628. (while (string-match ":" path)
  6629. (setq path (replace-match "+" t t path)))
  6630. (throw 'match t))))
  6631. (unless path
  6632. (error "No link found"))
  6633. ;; Remove any trailing spaces in path
  6634. (if (string-match " +\\'" path)
  6635. (setq path (replace-match "" t t path)))
  6636. (cond
  6637. ((assoc type org-link-protocols)
  6638. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6639. ((equal type "mailto")
  6640. (let ((cmd (car org-link-mailto-program))
  6641. (args (cdr org-link-mailto-program)) args1
  6642. (address path) (subject "") a)
  6643. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6644. (setq address (match-string 1 path)
  6645. subject (org-link-escape (match-string 2 path))))
  6646. (while args
  6647. (cond
  6648. ((not (stringp (car args))) (push (pop args) args1))
  6649. (t (setq a (pop args))
  6650. (if (string-match "%a" a)
  6651. (setq a (replace-match address t t a)))
  6652. (if (string-match "%s" a)
  6653. (setq a (replace-match subject t t a)))
  6654. (push a args1))))
  6655. (apply cmd (nreverse args1))))
  6656. ((member type '("http" "https" "ftp" "news"))
  6657. (browse-url (concat type ":" (org-link-escape
  6658. path org-link-escape-chars-browser))))
  6659. ((member type '("message"))
  6660. (browse-url (concat type ":" path)))
  6661. ((string= type "tags")
  6662. (org-tags-view in-emacs path))
  6663. ((string= type "thisfile")
  6664. (if in-emacs
  6665. (switch-to-buffer-other-window
  6666. (org-get-buffer-for-internal-link (current-buffer)))
  6667. (org-mark-ring-push))
  6668. (let ((cmd `(org-link-search
  6669. ,path
  6670. ,(cond ((equal in-emacs '(4)) 'occur)
  6671. ((equal in-emacs '(16)) 'org-occur)
  6672. (t nil))
  6673. ,pos)))
  6674. (condition-case nil (eval cmd)
  6675. (error (progn (widen) (eval cmd))))))
  6676. ((string= type "tree-match")
  6677. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6678. ((string= type "file")
  6679. (if (string-match "::\\([0-9]+\\)\\'" path)
  6680. (setq line (string-to-number (match-string 1 path))
  6681. path (substring path 0 (match-beginning 0)))
  6682. (if (string-match "::\\(.+\\)\\'" path)
  6683. (setq search (match-string 1 path)
  6684. path (substring path 0 (match-beginning 0)))))
  6685. (if (string-match "[*?{]" (file-name-nondirectory path))
  6686. (dired path)
  6687. (org-open-file path in-emacs line search)))
  6688. ((string= type "news")
  6689. (require 'org-gnus)
  6690. (org-gnus-follow-link path))
  6691. ((string= type "shell")
  6692. (let ((cmd path))
  6693. (if (or (not org-confirm-shell-link-function)
  6694. (funcall org-confirm-shell-link-function
  6695. (format "Execute \"%s\" in shell? "
  6696. (org-add-props cmd nil
  6697. 'face 'org-warning))))
  6698. (progn
  6699. (message "Executing %s" cmd)
  6700. (shell-command cmd))
  6701. (error "Abort"))))
  6702. ((string= type "elisp")
  6703. (let ((cmd path))
  6704. (if (or (not org-confirm-elisp-link-function)
  6705. (funcall org-confirm-elisp-link-function
  6706. (format "Execute \"%s\" as elisp? "
  6707. (org-add-props cmd nil
  6708. 'face 'org-warning))))
  6709. (message "%s => %s" cmd (eval (read cmd)))
  6710. (error "Abort"))))
  6711. (t
  6712. (browse-url-at-point)))))
  6713. (move-marker org-open-link-marker nil)
  6714. (run-hook-with-args 'org-follow-link-hook))
  6715. ;;; File search
  6716. (defvar org-create-file-search-functions nil
  6717. "List of functions to construct the right search string for a file link.
  6718. These functions are called in turn with point at the location to
  6719. which the link should point.
  6720. A function in the hook should first test if it would like to
  6721. handle this file type, for example by checking the major-mode or
  6722. the file extension. If it decides not to handle this file, it
  6723. should just return nil to give other functions a chance. If it
  6724. does handle the file, it must return the search string to be used
  6725. when following the link. The search string will be part of the
  6726. file link, given after a double colon, and `org-open-at-point'
  6727. will automatically search for it. If special measures must be
  6728. taken to make the search successful, another function should be
  6729. added to the companion hook `org-execute-file-search-functions',
  6730. which see.
  6731. A function in this hook may also use `setq' to set the variable
  6732. `description' to provide a suggestion for the descriptive text to
  6733. be used for this link when it gets inserted into an Org-mode
  6734. buffer with \\[org-insert-link].")
  6735. (defvar org-execute-file-search-functions nil
  6736. "List of functions to execute a file search triggered by a link.
  6737. Functions added to this hook must accept a single argument, the
  6738. search string that was part of the file link, the part after the
  6739. double colon. The function must first check if it would like to
  6740. handle this search, for example by checking the major-mode or the
  6741. file extension. If it decides not to handle this search, it
  6742. should just return nil to give other functions a chance. If it
  6743. does handle the search, it must return a non-nil value to keep
  6744. other functions from trying.
  6745. Each function can access the current prefix argument through the
  6746. variable `current-prefix-argument'. Note that a single prefix is
  6747. used to force opening a link in Emacs, so it may be good to only
  6748. use a numeric or double prefix to guide the search function.
  6749. In case this is needed, a function in this hook can also restore
  6750. the window configuration before `org-open-at-point' was called using:
  6751. (set-window-configuration org-window-config-before-follow-link)")
  6752. (defun org-link-search (s &optional type avoid-pos)
  6753. "Search for a link search option.
  6754. If S is surrounded by forward slashes, it is interpreted as a
  6755. regular expression. In org-mode files, this will create an `org-occur'
  6756. sparse tree. In ordinary files, `occur' will be used to list matches.
  6757. If the current buffer is in `dired-mode', grep will be used to search
  6758. in all files. If AVOID-POS is given, ignore matches near that position."
  6759. (let ((case-fold-search t)
  6760. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6761. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6762. (append '(("") (" ") ("\t") ("\n"))
  6763. org-emphasis-alist)
  6764. "\\|") "\\)"))
  6765. (pos (point))
  6766. (pre "") (post "")
  6767. words re0 re1 re2 re3 re4 re5 re2a reall)
  6768. (cond
  6769. ;; First check if there are any special
  6770. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6771. ;; Now try the builtin stuff
  6772. ((save-excursion
  6773. (goto-char (point-min))
  6774. (and
  6775. (re-search-forward
  6776. (concat "<<" (regexp-quote s0) ">>") nil t)
  6777. (setq pos (match-beginning 0))))
  6778. ;; There is an exact target for this
  6779. (goto-char pos))
  6780. ((string-match "^/\\(.*\\)/$" s)
  6781. ;; A regular expression
  6782. (cond
  6783. ((org-mode-p)
  6784. (org-occur (match-string 1 s)))
  6785. ;;((eq major-mode 'dired-mode)
  6786. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6787. (t (org-do-occur (match-string 1 s)))))
  6788. (t
  6789. ;; A normal search strings
  6790. (when (equal (string-to-char s) ?*)
  6791. ;; Anchor on headlines, post may include tags.
  6792. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6793. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6794. s (substring s 1)))
  6795. (remove-text-properties
  6796. 0 (length s)
  6797. '(face nil mouse-face nil keymap nil fontified nil) s)
  6798. ;; Make a series of regular expressions to find a match
  6799. (setq words (org-split-string s "[ \n\r\t]+")
  6800. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6801. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6802. "\\)" markers)
  6803. re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6804. re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6805. re1 (concat pre re2 post)
  6806. re3 (concat pre re4 post)
  6807. re5 (concat pre ".*" re4)
  6808. re2 (concat pre re2)
  6809. re2a (concat pre re2a)
  6810. re4 (concat pre re4)
  6811. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6812. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6813. re5 "\\)"
  6814. ))
  6815. (cond
  6816. ((eq type 'org-occur) (org-occur reall))
  6817. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6818. (t (goto-char (point-min))
  6819. (if (or (org-search-not-self 1 re0 nil t)
  6820. (org-search-not-self 1 re1 nil t)
  6821. (org-search-not-self 1 re2 nil t)
  6822. (org-search-not-self 1 re2a nil t)
  6823. (org-search-not-self 1 re3 nil t)
  6824. (org-search-not-self 1 re4 nil t)
  6825. (org-search-not-self 1 re5 nil t)
  6826. )
  6827. (goto-char (match-beginning 1))
  6828. (goto-char pos)
  6829. (error "No match")))))
  6830. (t
  6831. ;; Normal string-search
  6832. (goto-char (point-min))
  6833. (if (search-forward s nil t)
  6834. (goto-char (match-beginning 0))
  6835. (error "No match"))))
  6836. (and (org-mode-p) (org-show-context 'link-search))))
  6837. (defun org-search-not-self (group &rest args)
  6838. "Execute `re-search-forward', but only accept matches that do not
  6839. enclose the position of `org-open-link-marker'."
  6840. (let ((m org-open-link-marker))
  6841. (catch 'exit
  6842. (while (apply 're-search-forward args)
  6843. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6844. (goto-char (match-end group))
  6845. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6846. (> (match-beginning 0) (marker-position m))
  6847. (< (match-end 0) (marker-position m)))
  6848. (save-match-data
  6849. (or (not (org-in-regexp
  6850. org-bracket-link-analytic-regexp 1))
  6851. (not (match-end 4)) ; no description
  6852. (and (<= (match-beginning 4) (point))
  6853. (>= (match-end 4) (point))))))
  6854. (throw 'exit (point))))))))
  6855. (defun org-get-buffer-for-internal-link (buffer)
  6856. "Return a buffer to be used for displaying the link target of internal links."
  6857. (cond
  6858. ((not org-display-internal-link-with-indirect-buffer)
  6859. buffer)
  6860. ((string-match "(Clone)$" (buffer-name buffer))
  6861. (message "Buffer is already a clone, not making another one")
  6862. ;; we also do not modify visibility in this case
  6863. buffer)
  6864. (t ; make a new indirect buffer for displaying the link
  6865. (let* ((bn (buffer-name buffer))
  6866. (ibn (concat bn "(Clone)"))
  6867. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6868. (with-current-buffer ib (org-overview))
  6869. ib))))
  6870. (defun org-do-occur (regexp &optional cleanup)
  6871. "Call the Emacs command `occur'.
  6872. If CLEANUP is non-nil, remove the printout of the regular expression
  6873. in the *Occur* buffer. This is useful if the regex is long and not useful
  6874. to read."
  6875. (occur regexp)
  6876. (when cleanup
  6877. (let ((cwin (selected-window)) win beg end)
  6878. (when (setq win (get-buffer-window "*Occur*"))
  6879. (select-window win))
  6880. (goto-char (point-min))
  6881. (when (re-search-forward "match[a-z]+" nil t)
  6882. (setq beg (match-end 0))
  6883. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6884. (setq end (1- (match-beginning 0)))))
  6885. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6886. (goto-char (point-min))
  6887. (select-window cwin))))
  6888. ;;; The mark ring for links jumps
  6889. (defvar org-mark-ring nil
  6890. "Mark ring for positions before jumps in Org-mode.")
  6891. (defvar org-mark-ring-last-goto nil
  6892. "Last position in the mark ring used to go back.")
  6893. ;; Fill and close the ring
  6894. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6895. (loop for i from 1 to org-mark-ring-length do
  6896. (push (make-marker) org-mark-ring))
  6897. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6898. org-mark-ring)
  6899. (defun org-mark-ring-push (&optional pos buffer)
  6900. "Put the current position or POS into the mark ring and rotate it."
  6901. (interactive)
  6902. (setq pos (or pos (point)))
  6903. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6904. (move-marker (car org-mark-ring)
  6905. (or pos (point))
  6906. (or buffer (current-buffer)))
  6907. (message "%s"
  6908. (substitute-command-keys
  6909. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6910. (defun org-mark-ring-goto (&optional n)
  6911. "Jump to the previous position in the mark ring.
  6912. With prefix arg N, jump back that many stored positions. When
  6913. called several times in succession, walk through the entire ring.
  6914. Org-mode commands jumping to a different position in the current file,
  6915. or to another Org-mode file, automatically push the old position
  6916. onto the ring."
  6917. (interactive "p")
  6918. (let (p m)
  6919. (if (eq last-command this-command)
  6920. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6921. (setq p org-mark-ring))
  6922. (setq org-mark-ring-last-goto p)
  6923. (setq m (car p))
  6924. (switch-to-buffer (marker-buffer m))
  6925. (goto-char m)
  6926. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6927. (defun org-remove-angle-brackets (s)
  6928. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6929. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6930. s)
  6931. (defun org-add-angle-brackets (s)
  6932. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6933. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6934. s)
  6935. ;;; Following specific links
  6936. (defun org-follow-timestamp-link ()
  6937. (cond
  6938. ((org-at-date-range-p t)
  6939. (let ((org-agenda-start-on-weekday)
  6940. (t1 (match-string 1))
  6941. (t2 (match-string 2)))
  6942. (setq t1 (time-to-days (org-time-string-to-time t1))
  6943. t2 (time-to-days (org-time-string-to-time t2)))
  6944. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6945. ((org-at-timestamp-p t)
  6946. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6947. (substring (match-string 1) 0 10)))
  6948. 1))
  6949. (t (error "This should not happen"))))
  6950. ;;; Following file links
  6951. (defun org-open-file (path &optional in-emacs line search)
  6952. "Open the file at PATH.
  6953. First, this expands any special file name abbreviations. Then the
  6954. configuration variable `org-file-apps' is checked if it contains an
  6955. entry for this file type, and if yes, the corresponding command is launched.
  6956. If no application is found, Emacs simply visits the file.
  6957. With optional argument IN-EMACS, Emacs will visit the file.
  6958. Optional LINE specifies a line to go to, optional SEARCH a string to
  6959. search for. If LINE or SEARCH is given, the file will always be
  6960. opened in Emacs.
  6961. If the file does not exist, an error is thrown."
  6962. (setq in-emacs (or in-emacs line search))
  6963. (let* ((file (if (equal path "")
  6964. buffer-file-name
  6965. (substitute-in-file-name (expand-file-name path))))
  6966. (apps (append org-file-apps (org-default-apps)))
  6967. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6968. (dirp (if remp nil (file-directory-p file)))
  6969. (dfile (downcase file))
  6970. (old-buffer (current-buffer))
  6971. (old-pos (point))
  6972. (old-mode major-mode)
  6973. ext cmd)
  6974. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6975. (setq ext (match-string 1 dfile))
  6976. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6977. (setq ext (match-string 1 dfile))))
  6978. (if in-emacs
  6979. (setq cmd 'emacs)
  6980. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6981. (and dirp (cdr (assoc 'directory apps)))
  6982. (cdr (assoc ext apps))
  6983. (cdr (assoc t apps)))))
  6984. (when (eq cmd 'mailcap)
  6985. (require 'mailcap)
  6986. (mailcap-parse-mailcaps)
  6987. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6988. (command (mailcap-mime-info mime-type)))
  6989. (if (stringp command)
  6990. (setq cmd command)
  6991. (setq cmd 'emacs))))
  6992. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  6993. (not (file-exists-p file))
  6994. (not org-open-non-existing-files))
  6995. (error "No such file: %s" file))
  6996. (cond
  6997. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  6998. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  6999. (while (string-match "['\"]%s['\"]" cmd)
  7000. (setq cmd (replace-match "%s" t t cmd)))
  7001. (while (string-match "%s" cmd)
  7002. (setq cmd (replace-match
  7003. (save-match-data (shell-quote-argument file))
  7004. t t cmd)))
  7005. (save-window-excursion
  7006. (start-process-shell-command cmd nil cmd)))
  7007. ((or (stringp cmd)
  7008. (eq cmd 'emacs))
  7009. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7010. (widen)
  7011. (if line (goto-line line)
  7012. (if search (org-link-search search))))
  7013. ((consp cmd)
  7014. (eval cmd))
  7015. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7016. (and (org-mode-p) (eq old-mode 'org-mode)
  7017. (or (not (equal old-buffer (current-buffer)))
  7018. (not (equal old-pos (point))))
  7019. (org-mark-ring-push old-pos old-buffer))))
  7020. (defun org-default-apps ()
  7021. "Return the default applications for this operating system."
  7022. (cond
  7023. ((eq system-type 'darwin)
  7024. org-file-apps-defaults-macosx)
  7025. ((eq system-type 'windows-nt)
  7026. org-file-apps-defaults-windowsnt)
  7027. (t org-file-apps-defaults-gnu)))
  7028. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7029. (defun org-file-remote-p (file)
  7030. "Test whether FILE specifies a location on a remote system.
  7031. Return non-nil if the location is indeed remote.
  7032. For example, the filename \"/user@host:/foo\" specifies a location
  7033. on the system \"/user@host:\"."
  7034. (cond ((fboundp 'file-remote-p)
  7035. (file-remote-p file))
  7036. ((fboundp 'tramp-handle-file-remote-p)
  7037. (tramp-handle-file-remote-p file))
  7038. ((and (boundp 'ange-ftp-name-format)
  7039. (string-match (car ange-ftp-name-format) file))
  7040. t)
  7041. (t nil)))
  7042. ;;;; Hooks for remember.el, and refiling
  7043. (defun org-get-org-file ()
  7044. "Read a filename, with default directory `org-directory'."
  7045. (let ((default (or org-default-notes-file remember-data-file)))
  7046. (read-file-name (format "File name [%s]: " default)
  7047. (file-name-as-directory org-directory)
  7048. default)))
  7049. (defun org-notes-order-reversed-p ()
  7050. "Check if the current file should receive notes in reversed order."
  7051. (cond
  7052. ((not org-reverse-note-order) nil)
  7053. ((eq t org-reverse-note-order) t)
  7054. ((not (listp org-reverse-note-order)) nil)
  7055. (t (catch 'exit
  7056. (let ((all org-reverse-note-order)
  7057. entry)
  7058. (while (setq entry (pop all))
  7059. (if (string-match (car entry) buffer-file-name)
  7060. (throw 'exit (cdr entry))))
  7061. nil)))))
  7062. ;;;; Refiling
  7063. (defvar org-refile-target-table nil
  7064. "The list of refile targets, created by `org-refile'.")
  7065. (defvar org-agenda-new-buffers nil
  7066. "Buffers created to visit agenda files.")
  7067. (defun org-get-refile-targets (&optional default-buffer)
  7068. "Produce a table with refile targets."
  7069. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  7070. targets txt re files f desc descre)
  7071. (with-current-buffer (or default-buffer (current-buffer))
  7072. (while (setq entry (pop entries))
  7073. (setq files (car entry) desc (cdr entry))
  7074. (cond
  7075. ((null files) (setq files (list (current-buffer))))
  7076. ((eq files 'org-agenda-files)
  7077. (setq files (org-agenda-files 'unrestricted)))
  7078. ((and (symbolp files) (fboundp files))
  7079. (setq files (funcall files)))
  7080. ((and (symbolp files) (boundp files))
  7081. (setq files (symbol-value files))))
  7082. (if (stringp files) (setq files (list files)))
  7083. (cond
  7084. ((eq (car desc) :tag)
  7085. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7086. ((eq (car desc) :todo)
  7087. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7088. ((eq (car desc) :regexp)
  7089. (setq descre (cdr desc)))
  7090. ((eq (car desc) :level)
  7091. (setq descre (concat "^\\*\\{" (number-to-string
  7092. (if org-odd-levels-only
  7093. (1- (* 2 (cdr desc)))
  7094. (cdr desc)))
  7095. "\\}[ \t]")))
  7096. ((eq (car desc) :maxlevel)
  7097. (setq descre (concat "^\\*\\{1," (number-to-string
  7098. (if org-odd-levels-only
  7099. (1- (* 2 (cdr desc)))
  7100. (cdr desc)))
  7101. "\\}[ \t]")))
  7102. (t (error "Bad refiling target description %s" desc)))
  7103. (while (setq f (pop files))
  7104. (save-excursion
  7105. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7106. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7107. (save-excursion
  7108. (save-restriction
  7109. (widen)
  7110. (goto-char (point-min))
  7111. (while (re-search-forward descre nil t)
  7112. (goto-char (point-at-bol))
  7113. (when (looking-at org-complex-heading-regexp)
  7114. (setq txt (match-string 4)
  7115. re (concat "^" (regexp-quote
  7116. (buffer-substring (match-beginning 1)
  7117. (match-end 4)))))
  7118. (if (match-end 5) (setq re (concat re "[ \t]+"
  7119. (regexp-quote
  7120. (match-string 5)))))
  7121. (setq re (concat re "[ \t]*$"))
  7122. (when org-refile-use-outline-path
  7123. (setq txt (mapconcat 'identity
  7124. (append
  7125. (if (eq org-refile-use-outline-path 'file)
  7126. (list (file-name-nondirectory
  7127. (buffer-file-name (buffer-base-buffer))))
  7128. (if (eq org-refile-use-outline-path 'full-file-path)
  7129. (list (buffer-file-name (buffer-base-buffer)))))
  7130. (org-get-outline-path)
  7131. (list txt))
  7132. "/")))
  7133. (push (list txt f re (point)) targets))
  7134. (goto-char (point-at-eol))))))))
  7135. (nreverse targets))))
  7136. (defun org-get-outline-path ()
  7137. "Return the outline path to the current entry, as a list."
  7138. (let (rtn)
  7139. (save-excursion
  7140. (while (org-up-heading-safe)
  7141. (when (looking-at org-complex-heading-regexp)
  7142. (push (org-match-string-no-properties 4) rtn)))
  7143. rtn)))
  7144. (defvar org-refile-history nil
  7145. "History for refiling operations.")
  7146. (defun org-refile (&optional goto default-buffer)
  7147. "Move the entry at point to another heading.
  7148. The list of target headings is compiled using the information in
  7149. `org-refile-targets', which see. This list is created upon first use, and
  7150. you can update it by calling this command with a double prefix (`C-u C-u').
  7151. FIXME: Can we find a better way of updating?
  7152. At the target location, the entry is filed as a subitem of the target heading.
  7153. Depending on `org-reverse-note-order', the new subitem will either be the
  7154. first of the last subitem.
  7155. With prefix arg GOTO, the command will only visit the target location,
  7156. not actually move anything.
  7157. With a double prefix `C-c C-c', go to the location where the last refiling
  7158. operation has put the subtree.
  7159. With a double prefix argument, the command can be used to jump to any
  7160. heading in the current buffer."
  7161. (interactive "P")
  7162. (let* ((cbuf (current-buffer))
  7163. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7164. pos it nbuf file re level reversed)
  7165. (if (equal goto '(16))
  7166. (org-refile-goto-last-stored)
  7167. (when (setq it (org-refile-get-location
  7168. (if goto "Goto: " "Refile to: ") default-buffer))
  7169. (setq file (nth 1 it)
  7170. re (nth 2 it)
  7171. pos (nth 3 it))
  7172. (setq nbuf (or (find-buffer-visiting file)
  7173. (find-file-noselect file)))
  7174. (if goto
  7175. (progn
  7176. (switch-to-buffer nbuf)
  7177. (goto-char pos)
  7178. (org-show-context 'org-goto))
  7179. (org-copy-special)
  7180. (save-excursion
  7181. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7182. (find-file-noselect file))))
  7183. (setq reversed (org-notes-order-reversed-p))
  7184. (save-excursion
  7185. (save-restriction
  7186. (widen)
  7187. (goto-char pos)
  7188. (looking-at outline-regexp)
  7189. (setq level (org-get-valid-level (funcall outline-level) 1))
  7190. (goto-char
  7191. (if reversed
  7192. (outline-next-heading)
  7193. (or (save-excursion (outline-get-next-sibling))
  7194. (org-end-of-subtree t t)
  7195. (point-max))))
  7196. (bookmark-set "org-refile-last-stored")
  7197. (org-paste-subtree level))))
  7198. (org-cut-special)
  7199. (message "Entry refiled to \"%s\"" (car it)))))))
  7200. (defun org-refile-goto-last-stored ()
  7201. "Go to the location where the last refile was stored."
  7202. (interactive)
  7203. (bookmark-jump "org-refile-last-stored")
  7204. (message "This is the location of the last refile"))
  7205. (defun org-refile-get-location (&optional prompt default-buffer)
  7206. "Prompt the user for a refile location, using PROMPT."
  7207. (let ((org-refile-targets org-refile-targets)
  7208. (org-refile-use-outline-path org-refile-use-outline-path))
  7209. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7210. (unless org-refile-target-table
  7211. (error "No refile targets"))
  7212. (let* ((cbuf (current-buffer))
  7213. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7214. (fname (and filename (file-truename filename)))
  7215. (tbl (mapcar
  7216. (lambda (x)
  7217. (if (not (equal fname (file-truename (nth 1 x))))
  7218. (cons (concat (car x) " (" (file-name-nondirectory
  7219. (nth 1 x)) ")")
  7220. (cdr x))
  7221. x))
  7222. org-refile-target-table))
  7223. (completion-ignore-case t))
  7224. (assoc (completing-read prompt tbl nil t nil 'org-refile-history)
  7225. tbl)))
  7226. ;;;; Dynamic blocks
  7227. (defun org-find-dblock (name)
  7228. "Find the first dynamic block with name NAME in the buffer.
  7229. If not found, stay at current position and return nil."
  7230. (let (pos)
  7231. (save-excursion
  7232. (goto-char (point-min))
  7233. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7234. nil t)
  7235. (match-beginning 0))))
  7236. (if pos (goto-char pos))
  7237. pos))
  7238. (defconst org-dblock-start-re
  7239. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7240. "Matches the startline of a dynamic block, with parameters.")
  7241. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7242. "Matches the end of a dyhamic block.")
  7243. (defun org-create-dblock (plist)
  7244. "Create a dynamic block section, with parameters taken from PLIST.
  7245. PLIST must containe a :name entry which is used as name of the block."
  7246. (unless (bolp) (newline))
  7247. (let ((name (plist-get plist :name)))
  7248. (insert "#+BEGIN: " name)
  7249. (while plist
  7250. (if (eq (car plist) :name)
  7251. (setq plist (cddr plist))
  7252. (insert " " (prin1-to-string (pop plist)))))
  7253. (insert "\n\n#+END:\n")
  7254. (beginning-of-line -2)))
  7255. (defun org-prepare-dblock ()
  7256. "Prepare dynamic block for refresh.
  7257. This empties the block, puts the cursor at the insert position and returns
  7258. the property list including an extra property :name with the block name."
  7259. (unless (looking-at org-dblock-start-re)
  7260. (error "Not at a dynamic block"))
  7261. (let* ((begdel (1+ (match-end 0)))
  7262. (name (org-no-properties (match-string 1)))
  7263. (params (append (list :name name)
  7264. (read (concat "(" (match-string 3) ")")))))
  7265. (unless (re-search-forward org-dblock-end-re nil t)
  7266. (error "Dynamic block not terminated"))
  7267. (setq params
  7268. (append params
  7269. (list :content (buffer-substring
  7270. begdel (match-beginning 0)))))
  7271. (delete-region begdel (match-beginning 0))
  7272. (goto-char begdel)
  7273. (open-line 1)
  7274. params))
  7275. (defun org-map-dblocks (&optional command)
  7276. "Apply COMMAND to all dynamic blocks in the current buffer.
  7277. If COMMAND is not given, use `org-update-dblock'."
  7278. (let ((cmd (or command 'org-update-dblock))
  7279. pos)
  7280. (save-excursion
  7281. (goto-char (point-min))
  7282. (while (re-search-forward org-dblock-start-re nil t)
  7283. (goto-char (setq pos (match-beginning 0)))
  7284. (condition-case nil
  7285. (funcall cmd)
  7286. (error (message "Error during update of dynamic block")))
  7287. (goto-char pos)
  7288. (unless (re-search-forward org-dblock-end-re nil t)
  7289. (error "Dynamic block not terminated"))))))
  7290. (defun org-dblock-update (&optional arg)
  7291. "User command for updating dynamic blocks.
  7292. Update the dynamic block at point. With prefix ARG, update all dynamic
  7293. blocks in the buffer."
  7294. (interactive "P")
  7295. (if arg
  7296. (org-update-all-dblocks)
  7297. (or (looking-at org-dblock-start-re)
  7298. (org-beginning-of-dblock))
  7299. (org-update-dblock)))
  7300. (defun org-update-dblock ()
  7301. "Update the dynamic block at point
  7302. This means to empty the block, parse for parameters and then call
  7303. the correct writing function."
  7304. (save-window-excursion
  7305. (let* ((pos (point))
  7306. (line (org-current-line))
  7307. (params (org-prepare-dblock))
  7308. (name (plist-get params :name))
  7309. (cmd (intern (concat "org-dblock-write:" name))))
  7310. (message "Updating dynamic block `%s' at line %d..." name line)
  7311. (funcall cmd params)
  7312. (message "Updating dynamic block `%s' at line %d...done" name line)
  7313. (goto-char pos))))
  7314. (defun org-beginning-of-dblock ()
  7315. "Find the beginning of the dynamic block at point.
  7316. Error if there is no scuh block at point."
  7317. (let ((pos (point))
  7318. beg)
  7319. (end-of-line 1)
  7320. (if (and (re-search-backward org-dblock-start-re nil t)
  7321. (setq beg (match-beginning 0))
  7322. (re-search-forward org-dblock-end-re nil t)
  7323. (> (match-end 0) pos))
  7324. (goto-char beg)
  7325. (goto-char pos)
  7326. (error "Not in a dynamic block"))))
  7327. (defun org-update-all-dblocks ()
  7328. "Update all dynamic blocks in the buffer.
  7329. This function can be used in a hook."
  7330. (when (org-mode-p)
  7331. (org-map-dblocks 'org-update-dblock)))
  7332. ;;;; Completion
  7333. (defconst org-additional-option-like-keywords
  7334. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7335. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7336. "BEGIN_EXAMPLE" "END_EXAMPLE"))
  7337. (defun org-complete (&optional arg)
  7338. "Perform completion on word at point.
  7339. At the beginning of a headline, this completes TODO keywords as given in
  7340. `org-todo-keywords'.
  7341. If the current word is preceded by a backslash, completes the TeX symbols
  7342. that are supported for HTML support.
  7343. If the current word is preceded by \"#+\", completes special words for
  7344. setting file options.
  7345. In the line after \"#+STARTUP:, complete valid keywords.\"
  7346. At all other locations, this simply calls the value of
  7347. `org-completion-fallback-command'."
  7348. (interactive "P")
  7349. (org-without-partial-completion
  7350. (catch 'exit
  7351. (let* ((end (point))
  7352. (beg1 (save-excursion
  7353. (skip-chars-backward (org-re "[:alnum:]_@"))
  7354. (point)))
  7355. (beg (save-excursion
  7356. (skip-chars-backward "a-zA-Z0-9_:$")
  7357. (point)))
  7358. (confirm (lambda (x) (stringp (car x))))
  7359. (searchhead (equal (char-before beg) ?*))
  7360. (tag (and (equal (char-before beg1) ?:)
  7361. (equal (char-after (point-at-bol)) ?*)))
  7362. (prop (and (equal (char-before beg1) ?:)
  7363. (not (equal (char-after (point-at-bol)) ?*))))
  7364. (texp (equal (char-before beg) ?\\))
  7365. (link (equal (char-before beg) ?\[))
  7366. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7367. beg)
  7368. "#+"))
  7369. (startup (string-match "^#\\+STARTUP:.*"
  7370. (buffer-substring (point-at-bol) (point))))
  7371. (completion-ignore-case opt)
  7372. (type nil)
  7373. (tbl nil)
  7374. (table (cond
  7375. (opt
  7376. (setq type :opt)
  7377. (require 'org-exp)
  7378. (append
  7379. (mapcar
  7380. (lambda (x)
  7381. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7382. (cons (match-string 2 x) (match-string 1 x)))
  7383. (org-split-string (org-get-current-options) "\n"))
  7384. (mapcar 'list org-additional-option-like-keywords)))
  7385. (startup
  7386. (setq type :startup)
  7387. org-startup-options)
  7388. (link (append org-link-abbrev-alist-local
  7389. org-link-abbrev-alist))
  7390. (texp
  7391. (setq type :tex)
  7392. org-html-entities)
  7393. ((string-match "\\`\\*+[ \t]+\\'"
  7394. (buffer-substring (point-at-bol) beg))
  7395. (setq type :todo)
  7396. (mapcar 'list org-todo-keywords-1))
  7397. (searchhead
  7398. (setq type :searchhead)
  7399. (save-excursion
  7400. (goto-char (point-min))
  7401. (while (re-search-forward org-todo-line-regexp nil t)
  7402. (push (list
  7403. (org-make-org-heading-search-string
  7404. (match-string 3) t))
  7405. tbl)))
  7406. tbl)
  7407. (tag (setq type :tag beg beg1)
  7408. (or org-tag-alist (org-get-buffer-tags)))
  7409. (prop (setq type :prop beg beg1)
  7410. (mapcar 'list (org-buffer-property-keys nil t t)))
  7411. (t (progn
  7412. (call-interactively org-completion-fallback-command)
  7413. (throw 'exit nil)))))
  7414. (pattern (buffer-substring-no-properties beg end))
  7415. (completion (try-completion pattern table confirm)))
  7416. (cond ((eq completion t)
  7417. (if (not (assoc (upcase pattern) table))
  7418. (message "Already complete")
  7419. (if (and (equal type :opt)
  7420. (not (member (car (assoc (upcase pattern) table))
  7421. org-additional-option-like-keywords)))
  7422. (insert (substring (cdr (assoc (upcase pattern) table))
  7423. (length pattern)))
  7424. (if (memq type '(:tag :prop)) (insert ":")))))
  7425. ((null completion)
  7426. (message "Can't find completion for \"%s\"" pattern)
  7427. (ding))
  7428. ((not (string= pattern completion))
  7429. (delete-region beg end)
  7430. (if (string-match " +$" completion)
  7431. (setq completion (replace-match "" t t completion)))
  7432. (insert completion)
  7433. (if (get-buffer-window "*Completions*")
  7434. (delete-window (get-buffer-window "*Completions*")))
  7435. (if (assoc completion table)
  7436. (if (eq type :todo) (insert " ")
  7437. (if (memq type '(:tag :prop)) (insert ":"))))
  7438. (if (and (equal type :opt) (assoc completion table))
  7439. (message "%s" (substitute-command-keys
  7440. "Press \\[org-complete] again to insert example settings"))))
  7441. (t
  7442. (message "Making completion list...")
  7443. (let ((list (sort (all-completions pattern table confirm)
  7444. 'string<)))
  7445. (with-output-to-temp-buffer "*Completions*"
  7446. (condition-case nil
  7447. ;; Protection needed for XEmacs and emacs 21
  7448. (display-completion-list list pattern)
  7449. (error (display-completion-list list)))))
  7450. (message "Making completion list...%s" "done")))))))
  7451. ;;;; TODO, DEADLINE, Comments
  7452. (defun org-toggle-comment ()
  7453. "Change the COMMENT state of an entry."
  7454. (interactive)
  7455. (save-excursion
  7456. (org-back-to-heading)
  7457. (let (case-fold-search)
  7458. (if (looking-at (concat outline-regexp
  7459. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7460. (replace-match "" t t nil 1)
  7461. (if (looking-at outline-regexp)
  7462. (progn
  7463. (goto-char (match-end 0))
  7464. (insert org-comment-string " ")))))))
  7465. (defvar org-last-todo-state-is-todo nil
  7466. "This is non-nil when the last TODO state change led to a TODO state.
  7467. If the last change removed the TODO tag or switched to DONE, then
  7468. this is nil.")
  7469. (defvar org-setting-tags nil) ; dynamically skiped
  7470. ;; FIXME: better place
  7471. (defun org-property-or-variable-value (var &optional inherit)
  7472. "Check if there is a property fixing the value of VAR.
  7473. If yes, return this value. If not, return the current value of the variable."
  7474. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  7475. (if (and prop (stringp prop) (string-match "\\S-" prop))
  7476. (read prop)
  7477. (symbol-value var))))
  7478. (defun org-parse-local-options (string var)
  7479. "Parse STRING for startup setting relevant for variable VAR."
  7480. (let ((rtn (symbol-value var))
  7481. e opts)
  7482. (save-match-data
  7483. (if (or (not string) (not (string-match "\\S-" string)))
  7484. rtn
  7485. (setq opts (delq nil (mapcar (lambda (x)
  7486. (setq e (assoc x org-startup-options))
  7487. (if (eq (nth 1 e) var) e nil))
  7488. (org-split-string string "[ \t]+"))))
  7489. (if (not opts)
  7490. rtn
  7491. (setq rtn nil)
  7492. (while (setq e (pop opts))
  7493. (if (not (nth 3 e))
  7494. (setq rtn (nth 2 e))
  7495. (if (not (listp rtn)) (setq rtn nil))
  7496. (push (nth 2 e) rtn)))
  7497. rtn)))))
  7498. (defvar org-blocker-hook nil
  7499. "Hook for functions that are allowed to block a state change.
  7500. Each function gets as its single argument a property list, see
  7501. `org-trigger-hook' for more information about this list.
  7502. If any of the functions in this hook returns nil, the state change
  7503. is blocked.")
  7504. (defvar org-trigger-hook nil
  7505. "Hook for functions that are triggered by a state change.
  7506. Each function gets as its single argument a property list with at least
  7507. the following elements:
  7508. (:type type-of-change :position pos-at-entry-start
  7509. :from old-state :to new-state)
  7510. Depending on the type, more properties may be present.
  7511. This mechanism is currently implemented for:
  7512. TODO state changes
  7513. ------------------
  7514. :type todo-state-change
  7515. :from previous state (keyword as a string), or nil
  7516. :to new state (keyword as a string), or nil")
  7517. (defun org-todo (&optional arg)
  7518. "Change the TODO state of an item.
  7519. The state of an item is given by a keyword at the start of the heading,
  7520. like
  7521. *** TODO Write paper
  7522. *** DONE Call mom
  7523. The different keywords are specified in the variable `org-todo-keywords'.
  7524. By default the available states are \"TODO\" and \"DONE\".
  7525. So for this example: when the item starts with TODO, it is changed to DONE.
  7526. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7527. DONE are present, add TODO at the beginning of the heading.
  7528. With C-u prefix arg, use completion to determine the new state.
  7529. With numeric prefix arg, switch to that state.
  7530. For calling through lisp, arg is also interpreted in the following way:
  7531. 'none -> empty state
  7532. \"\"(empty string) -> switch to empty state
  7533. 'done -> switch to DONE
  7534. 'nextset -> switch to the next set of keywords
  7535. 'previousset -> switch to the previous set of keywords
  7536. \"WAITING\" -> switch to the specified keyword, but only if it
  7537. really is a member of `org-todo-keywords'."
  7538. (interactive "P")
  7539. (save-excursion
  7540. (catch 'exit
  7541. (org-back-to-heading)
  7542. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7543. (or (looking-at (concat " +" org-todo-regexp " *"))
  7544. (looking-at " *"))
  7545. (let* ((match-data (match-data))
  7546. (startpos (point-at-bol))
  7547. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7548. (org-log-done org-log-done)
  7549. (org-log-repeat org-log-repeat)
  7550. (org-todo-log-states org-todo-log-states)
  7551. (this (match-string 1))
  7552. (hl-pos (match-beginning 0))
  7553. (head (org-get-todo-sequence-head this))
  7554. (ass (assoc head org-todo-kwd-alist))
  7555. (interpret (nth 1 ass))
  7556. (done-word (nth 3 ass))
  7557. (final-done-word (nth 4 ass))
  7558. (last-state (or this ""))
  7559. (completion-ignore-case t)
  7560. (member (member this org-todo-keywords-1))
  7561. (tail (cdr member))
  7562. (state (cond
  7563. ((and org-todo-key-trigger
  7564. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  7565. (and (not arg) org-use-fast-todo-selection
  7566. (not (eq org-use-fast-todo-selection 'prefix)))))
  7567. ;; Use fast selection
  7568. (org-fast-todo-selection))
  7569. ((and (equal arg '(4))
  7570. (or (not org-use-fast-todo-selection)
  7571. (not org-todo-key-trigger)))
  7572. ;; Read a state with completion
  7573. (completing-read "State: " (mapcar (lambda(x) (list x))
  7574. org-todo-keywords-1)
  7575. nil t))
  7576. ((eq arg 'right)
  7577. (if this
  7578. (if tail (car tail) nil)
  7579. (car org-todo-keywords-1)))
  7580. ((eq arg 'left)
  7581. (if (equal member org-todo-keywords-1)
  7582. nil
  7583. (if this
  7584. (nth (- (length org-todo-keywords-1) (length tail) 2)
  7585. org-todo-keywords-1)
  7586. (org-last org-todo-keywords-1))))
  7587. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7588. (setq arg nil))) ; hack to fall back to cycling
  7589. (arg
  7590. ;; user or caller requests a specific state
  7591. (cond
  7592. ((equal arg "") nil)
  7593. ((eq arg 'none) nil)
  7594. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7595. ((eq arg 'nextset)
  7596. (or (car (cdr (member head org-todo-heads)))
  7597. (car org-todo-heads)))
  7598. ((eq arg 'previousset)
  7599. (let ((org-todo-heads (reverse org-todo-heads)))
  7600. (or (car (cdr (member head org-todo-heads)))
  7601. (car org-todo-heads))))
  7602. ((car (member arg org-todo-keywords-1)))
  7603. ((nth (1- (prefix-numeric-value arg))
  7604. org-todo-keywords-1))))
  7605. ((null member) (or head (car org-todo-keywords-1)))
  7606. ((equal this final-done-word) nil) ;; -> make empty
  7607. ((null tail) nil) ;; -> first entry
  7608. ((eq interpret 'sequence)
  7609. (car tail))
  7610. ((memq interpret '(type priority))
  7611. (if (eq this-command last-command)
  7612. (car tail)
  7613. (if (> (length tail) 0)
  7614. (or done-word (car org-done-keywords))
  7615. nil)))
  7616. (t nil)))
  7617. (next (if state (concat " " state " ") " "))
  7618. (change-plist (list :type 'todo-state-change :from this :to state
  7619. :position startpos))
  7620. dolog now-done-p)
  7621. (when org-blocker-hook
  7622. (unless (save-excursion
  7623. (save-match-data
  7624. (run-hook-with-args-until-failure
  7625. 'org-blocker-hook change-plist)))
  7626. (if (interactive-p)
  7627. (error "TODO state change from %s to %s blocked" this state)
  7628. ;; fail silently
  7629. (message "TODO state change from %s to %s blocked" this state)
  7630. (throw 'exit nil))))
  7631. (store-match-data match-data)
  7632. (replace-match next t t)
  7633. (unless (pos-visible-in-window-p hl-pos)
  7634. (message "TODO state changed to %s" (org-trim next)))
  7635. (unless head
  7636. (setq head (org-get-todo-sequence-head state)
  7637. ass (assoc head org-todo-kwd-alist)
  7638. interpret (nth 1 ass)
  7639. done-word (nth 3 ass)
  7640. final-done-word (nth 4 ass)))
  7641. (when (memq arg '(nextset previousset))
  7642. (message "Keyword-Set %d/%d: %s"
  7643. (- (length org-todo-sets) -1
  7644. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7645. (length org-todo-sets)
  7646. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7647. (setq org-last-todo-state-is-todo
  7648. (not (member state org-done-keywords)))
  7649. (setq now-done-p (and (member state org-done-keywords)
  7650. (not (member this org-done-keywords))))
  7651. (and logging (org-local-logging logging))
  7652. (when (and (or org-todo-log-states org-log-done)
  7653. (not (memq arg '(nextset previousset))))
  7654. ;; we need to look at recording a time and note
  7655. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7656. (nth 2 (assoc this org-todo-log-states))))
  7657. (when (and state
  7658. (member state org-not-done-keywords)
  7659. (not (member this org-not-done-keywords)))
  7660. ;; This is now a todo state and was not one before
  7661. ;; If there was a CLOSED time stamp, get rid of it.
  7662. (org-add-planning-info nil nil 'closed))
  7663. (when (and now-done-p org-log-done)
  7664. ;; It is now done, and it was not done before
  7665. (org-add-planning-info 'closed (org-current-time))
  7666. (if (and (not dolog) (eq 'note org-log-done))
  7667. (org-add-log-setup 'done state 'findpos 'note)))
  7668. (when (and state dolog)
  7669. ;; This is a non-nil state, and we need to log it
  7670. (org-add-log-setup 'state state 'findpos dolog)))
  7671. ;; Fixup tag positioning
  7672. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7673. (run-hooks 'org-after-todo-state-change-hook)
  7674. (if (and arg (not (member state org-done-keywords)))
  7675. (setq head (org-get-todo-sequence-head state)))
  7676. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7677. ;; Do we need to trigger a repeat?
  7678. (when now-done-p (org-auto-repeat-maybe state))
  7679. ;; Fixup cursor location if close to the keyword
  7680. (if (and (outline-on-heading-p)
  7681. (not (bolp))
  7682. (save-excursion (beginning-of-line 1)
  7683. (looking-at org-todo-line-regexp))
  7684. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7685. (progn
  7686. (goto-char (or (match-end 2) (match-end 1)))
  7687. (just-one-space)))
  7688. (when org-trigger-hook
  7689. (save-excursion
  7690. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  7691. (defun org-local-logging (value)
  7692. "Get logging settings from a property VALUE."
  7693. (let* (words w a)
  7694. ;; directly set the variables, they are already local.
  7695. (setq org-log-done nil
  7696. org-log-repeat nil
  7697. org-todo-log-states nil)
  7698. (setq words (org-split-string value))
  7699. (while (setq w (pop words))
  7700. (cond
  7701. ((setq a (assoc w org-startup-options))
  7702. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7703. (set (nth 1 a) (nth 2 a))))
  7704. ((setq a (org-extract-log-state-settings w))
  7705. (and (member (car a) org-todo-keywords-1)
  7706. (push a org-todo-log-states)))))))
  7707. (defun org-get-todo-sequence-head (kwd)
  7708. "Return the head of the TODO sequence to which KWD belongs.
  7709. If KWD is not set, check if there is a text property remembering the
  7710. right sequence."
  7711. (let (p)
  7712. (cond
  7713. ((not kwd)
  7714. (or (get-text-property (point-at-bol) 'org-todo-head)
  7715. (progn
  7716. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  7717. nil (point-at-eol)))
  7718. (get-text-property p 'org-todo-head))))
  7719. ((not (member kwd org-todo-keywords-1))
  7720. (car org-todo-keywords-1))
  7721. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  7722. (defun org-fast-todo-selection ()
  7723. "Fast TODO keyword selection with single keys.
  7724. Returns the new TODO keyword, or nil if no state change should occur."
  7725. (let* ((fulltable org-todo-key-alist)
  7726. (done-keywords org-done-keywords) ;; needed for the faces.
  7727. (maxlen (apply 'max (mapcar
  7728. (lambda (x)
  7729. (if (stringp (car x)) (string-width (car x)) 0))
  7730. fulltable)))
  7731. (expert nil)
  7732. (fwidth (+ maxlen 3 1 3))
  7733. (ncol (/ (- (window-width) 4) fwidth))
  7734. tg cnt e c tbl
  7735. groups ingroup)
  7736. (save-window-excursion
  7737. (if expert
  7738. (set-buffer (get-buffer-create " *Org todo*"))
  7739. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  7740. (erase-buffer)
  7741. (org-set-local 'org-done-keywords done-keywords)
  7742. (setq tbl fulltable cnt 0)
  7743. (while (setq e (pop tbl))
  7744. (cond
  7745. ((equal e '(:startgroup))
  7746. (push '() groups) (setq ingroup t)
  7747. (when (not (= cnt 0))
  7748. (setq cnt 0)
  7749. (insert "\n"))
  7750. (insert "{ "))
  7751. ((equal e '(:endgroup))
  7752. (setq ingroup nil cnt 0)
  7753. (insert "}\n"))
  7754. (t
  7755. (setq tg (car e) c (cdr e))
  7756. (if ingroup (push tg (car groups)))
  7757. (setq tg (org-add-props tg nil 'face
  7758. (org-get-todo-face tg)))
  7759. (if (and (= cnt 0) (not ingroup)) (insert " "))
  7760. (insert "[" c "] " tg (make-string
  7761. (- fwidth 4 (length tg)) ?\ ))
  7762. (when (= (setq cnt (1+ cnt)) ncol)
  7763. (insert "\n")
  7764. (if ingroup (insert " "))
  7765. (setq cnt 0)))))
  7766. (insert "\n")
  7767. (goto-char (point-min))
  7768. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  7769. (fit-window-to-buffer))
  7770. (message "[a-z..]:Set [SPC]:clear")
  7771. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  7772. (cond
  7773. ((or (= c ?\C-g)
  7774. (and (= c ?q) (not (rassoc c fulltable))))
  7775. (setq quit-flag t))
  7776. ((= c ?\ ) nil)
  7777. ((setq e (rassoc c fulltable) tg (car e))
  7778. tg)
  7779. (t (setq quit-flag t))))))
  7780. (defun org-entry-is-todo-p ()
  7781. (member (org-get-todo-state) org-not-done-keywords))
  7782. (defun org-entry-is-done-p ()
  7783. (member (org-get-todo-state) org-done-keywords))
  7784. (defun org-get-todo-state ()
  7785. (save-excursion
  7786. (org-back-to-heading t)
  7787. (and (looking-at org-todo-line-regexp)
  7788. (match-end 2)
  7789. (match-string 2))))
  7790. (defun org-at-date-range-p (&optional inactive-ok)
  7791. "Is the cursor inside a date range?"
  7792. (interactive)
  7793. (save-excursion
  7794. (catch 'exit
  7795. (let ((pos (point)))
  7796. (skip-chars-backward "^[<\r\n")
  7797. (skip-chars-backward "<[")
  7798. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7799. (>= (match-end 0) pos)
  7800. (throw 'exit t))
  7801. (skip-chars-backward "^<[\r\n")
  7802. (skip-chars-backward "<[")
  7803. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7804. (>= (match-end 0) pos)
  7805. (throw 'exit t)))
  7806. nil)))
  7807. (defun org-get-repeat ()
  7808. "Check if tere is a deadline/schedule with repeater in this entry."
  7809. (save-match-data
  7810. (save-excursion
  7811. (org-back-to-heading t)
  7812. (if (re-search-forward
  7813. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  7814. (match-string 1)))))
  7815. (defvar org-last-changed-timestamp)
  7816. (defvar org-log-post-message)
  7817. (defvar org-log-note-purpose)
  7818. (defvar org-log-note-how)
  7819. (defun org-auto-repeat-maybe (done-word)
  7820. "Check if the current headline contains a repeated deadline/schedule.
  7821. If yes, set TODO state back to what it was and change the base date
  7822. of repeating deadline/scheduled time stamps to new date.
  7823. This function is run automatically after each state change to a DONE state."
  7824. ;; last-state is dynamically scoped into this function
  7825. (let* ((repeat (org-get-repeat))
  7826. (aa (assoc last-state org-todo-kwd-alist))
  7827. (interpret (nth 1 aa))
  7828. (head (nth 2 aa))
  7829. (whata '(("d" . day) ("m" . month) ("y" . year)))
  7830. (msg "Entry repeats: ")
  7831. (org-log-done nil)
  7832. (org-todo-log-states nil)
  7833. (nshiftmax 10) (nshift 0)
  7834. re type n what ts mb0 time)
  7835. (when repeat
  7836. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  7837. (org-todo (if (eq interpret 'type) last-state head))
  7838. (when org-log-repeat
  7839. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  7840. (memq 'org-add-log-note post-command-hook))
  7841. ;; OK, we are already setup for some record
  7842. (if (eq org-log-repeat 'note)
  7843. ;; make sure we take a note, not only a time stamp
  7844. (setq org-log-note-how 'note))
  7845. ;; Set up for taking a record
  7846. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  7847. 'findpos org-log-repeat)))
  7848. (org-back-to-heading t)
  7849. (org-add-planning-info nil nil 'closed)
  7850. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  7851. org-deadline-time-regexp "\\)\\|\\("
  7852. org-ts-regexp "\\)"))
  7853. (while (re-search-forward
  7854. re (save-excursion (outline-next-heading) (point)) t)
  7855. (setq type (if (match-end 1) org-scheduled-string
  7856. (if (match-end 3) org-deadline-string "Plain:"))
  7857. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0)))
  7858. mb0 (match-beginning 0))
  7859. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  7860. (setq n (string-to-number (match-string 2 ts))
  7861. what (match-string 3 ts))
  7862. (if (equal what "w") (setq n (* n 7) what "d"))
  7863. ;; Preparation, see if we need to modify the start date for the change
  7864. (when (match-end 1)
  7865. (setq time (save-match-data (org-time-string-to-time ts)))
  7866. (cond
  7867. ((equal (match-string 1 ts) ".")
  7868. ;; Shift starting date to today
  7869. (org-timestamp-change
  7870. (- (time-to-days (current-time)) (time-to-days time))
  7871. 'day))
  7872. ((equal (match-string 1 ts) "+")
  7873. (while (or (= nshift 0)
  7874. (<= (time-to-days time) (time-to-days (current-time))))
  7875. (when (= (incf nshift) nshiftmax)
  7876. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  7877. (error "Abort")))
  7878. (org-timestamp-change n (cdr (assoc what whata)))
  7879. (org-at-timestamp-p t)
  7880. (setq ts (match-string 1))
  7881. (setq time (save-match-data (org-time-string-to-time ts))))
  7882. (org-timestamp-change (- n) (cdr (assoc what whata)))
  7883. ;; rematch, so that we have everything in place for the real shift
  7884. (org-at-timestamp-p t)
  7885. (setq ts (match-string 1))
  7886. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  7887. (org-timestamp-change n (cdr (assoc what whata)))
  7888. (setq msg (concat msg type org-last-changed-timestamp " "))))
  7889. (setq org-log-post-message msg)
  7890. (message "%s" msg))))
  7891. (defun org-show-todo-tree (arg)
  7892. "Make a compact tree which shows all headlines marked with TODO.
  7893. The tree will show the lines where the regexp matches, and all higher
  7894. headlines above the match.
  7895. With a \\[universal-argument] prefix, also show the DONE entries.
  7896. With a numeric prefix N, construct a sparse tree for the Nth element
  7897. of `org-todo-keywords-1'."
  7898. (interactive "P")
  7899. (let ((case-fold-search nil)
  7900. (kwd-re
  7901. (cond ((null arg) org-not-done-regexp)
  7902. ((equal arg '(4))
  7903. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  7904. (mapcar 'list org-todo-keywords-1))))
  7905. (concat "\\("
  7906. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  7907. "\\)\\>")))
  7908. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  7909. (regexp-quote (nth (1- (prefix-numeric-value arg))
  7910. org-todo-keywords-1)))
  7911. (t (error "Invalid prefix argument: %s" arg)))))
  7912. (message "%d TODO entries found"
  7913. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  7914. (defun org-deadline (&optional remove)
  7915. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  7916. With argument REMOVE, remove any deadline from the item."
  7917. (interactive "P")
  7918. (if remove
  7919. (progn
  7920. (org-remove-timestamp-with-keyword org-deadline-string)
  7921. (message "Item no longer has a deadline."))
  7922. (org-add-planning-info 'deadline nil 'closed)))
  7923. (defun org-schedule (&optional remove)
  7924. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  7925. With argument REMOVE, remove any scheduling date from the item."
  7926. (interactive "P")
  7927. (if remove
  7928. (progn
  7929. (org-remove-timestamp-with-keyword org-scheduled-string)
  7930. (message "Item is no longer scheduled."))
  7931. (org-add-planning-info 'scheduled nil 'closed)))
  7932. (defun org-remove-timestamp-with-keyword (keyword)
  7933. "Remove all time stamps with KEYWORD in the current entry."
  7934. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  7935. beg)
  7936. (save-excursion
  7937. (org-back-to-heading t)
  7938. (setq beg (point))
  7939. (org-end-of-subtree t t)
  7940. (while (re-search-backward re beg t)
  7941. (replace-match "")
  7942. (unless (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  7943. (delete-region (point-at-bol) (min (1+ (point)) (point-max))))))))
  7944. (defun org-add-planning-info (what &optional time &rest remove)
  7945. "Insert new timestamp with keyword in the line directly after the headline.
  7946. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  7947. If non is given, the user is prompted for a date.
  7948. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  7949. be removed."
  7950. (interactive)
  7951. (let (org-time-was-given org-end-time-was-given ts
  7952. end default-time default-input)
  7953. (when (and (not time) (memq what '(scheduled deadline)))
  7954. ;; Try to get a default date/time from existing timestamp
  7955. (save-excursion
  7956. (org-back-to-heading t)
  7957. (setq end (save-excursion (outline-next-heading) (point)))
  7958. (when (re-search-forward (if (eq what 'scheduled)
  7959. org-scheduled-time-regexp
  7960. org-deadline-time-regexp)
  7961. end t)
  7962. (setq ts (match-string 1)
  7963. default-time
  7964. (apply 'encode-time (org-parse-time-string ts))
  7965. default-input (and ts (org-get-compact-tod ts))))))
  7966. (when what
  7967. ;; If necessary, get the time from the user
  7968. (setq time (or time (org-read-date nil 'to-time nil nil
  7969. default-time default-input))))
  7970. (when (and org-insert-labeled-timestamps-at-point
  7971. (member what '(scheduled deadline)))
  7972. (insert
  7973. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  7974. (org-insert-time-stamp time org-time-was-given
  7975. nil nil nil (list org-end-time-was-given))
  7976. (setq what nil))
  7977. (save-excursion
  7978. (save-restriction
  7979. (let (col list elt ts buffer-invisibility-spec)
  7980. (org-back-to-heading t)
  7981. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  7982. (goto-char (match-end 1))
  7983. (setq col (current-column))
  7984. (goto-char (match-end 0))
  7985. (if (eobp) (insert "\n") (forward-char 1))
  7986. (if (and (not (looking-at outline-regexp))
  7987. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  7988. "[^\r\n]*"))
  7989. (not (equal (match-string 1) org-clock-string)))
  7990. (narrow-to-region (match-beginning 0) (match-end 0))
  7991. (insert-before-markers "\n")
  7992. (backward-char 1)
  7993. (narrow-to-region (point) (point))
  7994. (indent-to-column col))
  7995. ;; Check if we have to remove something.
  7996. (setq list (cons what remove))
  7997. (while list
  7998. (setq elt (pop list))
  7999. (goto-char (point-min))
  8000. (when (or (and (eq elt 'scheduled)
  8001. (re-search-forward org-scheduled-time-regexp nil t))
  8002. (and (eq elt 'deadline)
  8003. (re-search-forward org-deadline-time-regexp nil t))
  8004. (and (eq elt 'closed)
  8005. (re-search-forward org-closed-time-regexp nil t)))
  8006. (replace-match "")
  8007. (if (looking-at "--+<[^>]+>") (replace-match ""))
  8008. (if (looking-at " +") (replace-match ""))))
  8009. (goto-char (point-max))
  8010. (when what
  8011. (insert
  8012. (if (not (equal (char-before) ?\ )) " " "")
  8013. (cond ((eq what 'scheduled) org-scheduled-string)
  8014. ((eq what 'deadline) org-deadline-string)
  8015. ((eq what 'closed) org-closed-string))
  8016. " ")
  8017. (setq ts (org-insert-time-stamp
  8018. time
  8019. (or org-time-was-given
  8020. (and (eq what 'closed) org-log-done-with-time))
  8021. (eq what 'closed)
  8022. nil nil (list org-end-time-was-given)))
  8023. (end-of-line 1))
  8024. (goto-char (point-min))
  8025. (widen)
  8026. (if (looking-at "[ \t]+\r?\n")
  8027. (replace-match ""))
  8028. ts)))))
  8029. (defvar org-log-note-marker (make-marker))
  8030. (defvar org-log-note-purpose nil)
  8031. (defvar org-log-note-state nil)
  8032. (defvar org-log-note-how nil)
  8033. (defvar org-log-note-window-configuration nil)
  8034. (defvar org-log-note-return-to (make-marker))
  8035. (defvar org-log-post-message nil
  8036. "Message to be displayed after a log note has been stored.
  8037. The auto-repeater uses this.")
  8038. (defun org-add-note ()
  8039. "Add a note to the current entry.
  8040. This is done in the same way as adding a state change note."
  8041. (interactive)
  8042. (org-add-log-setup 'note nil t nil))
  8043. (defun org-add-log-setup (&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. ((eq org-log-note-purpose 'note)
  8092. "this entry")
  8093. (t (error "This should not happen")))))
  8094. (org-set-local 'org-finish-function 'org-store-log-note)))
  8095. (defvar org-note-abort nil) ; dynamically scoped
  8096. (defun org-store-log-note ()
  8097. "Finish taking a log note, and insert it to where it belongs."
  8098. (let ((txt (buffer-string))
  8099. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  8100. lines ind)
  8101. (kill-buffer (current-buffer))
  8102. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  8103. (setq txt (replace-match "" t t txt)))
  8104. (if (string-match "\\s-+\\'" txt)
  8105. (setq txt (replace-match "" t t txt)))
  8106. (setq lines (org-split-string txt "\n"))
  8107. (when (and note (string-match "\\S-" note))
  8108. (setq note
  8109. (org-replace-escapes
  8110. note
  8111. (list (cons "%u" (user-login-name))
  8112. (cons "%U" user-full-name)
  8113. (cons "%t" (format-time-string
  8114. (org-time-stamp-format 'long 'inactive)
  8115. (current-time)))
  8116. (cons "%s" (if org-log-note-state
  8117. (concat "\"" org-log-note-state "\"")
  8118. "")))))
  8119. (if lines (setq note (concat note " \\\\")))
  8120. (push note lines))
  8121. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  8122. (when lines
  8123. (save-excursion
  8124. (set-buffer (marker-buffer org-log-note-marker))
  8125. (save-excursion
  8126. (goto-char org-log-note-marker)
  8127. (move-marker org-log-note-marker nil)
  8128. (end-of-line 1)
  8129. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  8130. (indent-relative nil)
  8131. (insert "- " (pop lines))
  8132. (org-indent-line-function)
  8133. (beginning-of-line 1)
  8134. (looking-at "[ \t]*")
  8135. (setq ind (concat (match-string 0) " "))
  8136. (end-of-line 1)
  8137. (while lines (insert "\n" ind (pop lines)))))))
  8138. (set-window-configuration org-log-note-window-configuration)
  8139. (with-current-buffer (marker-buffer org-log-note-return-to)
  8140. (goto-char org-log-note-return-to))
  8141. (move-marker org-log-note-return-to nil)
  8142. (and org-log-post-message (message "%s" org-log-post-message)))
  8143. ;; FIXME: what else would be useful?
  8144. ;; - priority
  8145. ;; - date
  8146. (defun org-sparse-tree (&optional arg)
  8147. "Create a sparse tree, prompt for the details.
  8148. This command can create sparse trees. You first need to select the type
  8149. of match used to create the tree:
  8150. t Show entries with a specific TODO keyword.
  8151. T Show entries selected by a tags match.
  8152. p Enter a property name and its value (both with completion on existing
  8153. names/values) and show entries with that property.
  8154. r Show entries matching a regular expression
  8155. d Show deadlines due within `org-deadline-warning-days'."
  8156. (interactive "P")
  8157. (let (ans kwd value)
  8158. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  8159. (setq ans (read-char-exclusive))
  8160. (cond
  8161. ((equal ans ?d)
  8162. (call-interactively 'org-check-deadlines))
  8163. ((equal ans ?b)
  8164. (call-interactively 'org-check-before-date))
  8165. ((equal ans ?t)
  8166. (org-show-todo-tree '(4)))
  8167. ((equal ans ?T)
  8168. (call-interactively 'org-tags-sparse-tree))
  8169. ((member ans '(?p ?P))
  8170. (setq kwd (completing-read "Property: "
  8171. (mapcar 'list (org-buffer-property-keys))))
  8172. (setq value (completing-read "Value: "
  8173. (mapcar 'list (org-property-values kwd))))
  8174. (unless (string-match "\\`{.*}\\'" value)
  8175. (setq value (concat "\"" value "\"")))
  8176. (org-tags-sparse-tree arg (concat kwd "=" value)))
  8177. ((member ans '(?r ?R ?/))
  8178. (call-interactively 'org-occur))
  8179. (t (error "No such sparse tree command \"%c\"" ans)))))
  8180. (defvar org-occur-highlights nil
  8181. "List of overlays used for occur matches.")
  8182. (make-variable-buffer-local 'org-occur-highlights)
  8183. (defvar org-occur-parameters nil
  8184. "Parameters of the active org-occur calls.
  8185. This is a list, each call to org-occur pushes as cons cell,
  8186. containing the regular expression and the callback, onto the list.
  8187. The list can contain several entries if `org-occur' has been called
  8188. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  8189. will only contain one set of parameters. When the highlights are
  8190. removed (for example with `C-c C-c', or with the next edit (depending
  8191. on `org-remove-highlights-with-change'), this variable is emptied
  8192. as well.")
  8193. (make-variable-buffer-local 'org-occur-parameters)
  8194. (defun org-occur (regexp &optional keep-previous callback)
  8195. "Make a compact tree which shows all matches of REGEXP.
  8196. The tree will show the lines where the regexp matches, and all higher
  8197. headlines above the match. It will also show the heading after the match,
  8198. to make sure editing the matching entry is easy.
  8199. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  8200. call to `org-occur' will be kept, to allow stacking of calls to this
  8201. command.
  8202. If CALLBACK is non-nil, it is a function which is called to confirm
  8203. that the match should indeed be shown."
  8204. (interactive "sRegexp: \nP")
  8205. (unless keep-previous
  8206. (org-remove-occur-highlights nil nil t))
  8207. (push (cons regexp callback) org-occur-parameters)
  8208. (let ((cnt 0))
  8209. (save-excursion
  8210. (goto-char (point-min))
  8211. (if (or (not keep-previous) ; do not want to keep
  8212. (not org-occur-highlights)) ; no previous matches
  8213. ;; hide everything
  8214. (org-overview))
  8215. (while (re-search-forward regexp nil t)
  8216. (when (or (not callback)
  8217. (save-match-data (funcall callback)))
  8218. (setq cnt (1+ cnt))
  8219. (when org-highlight-sparse-tree-matches
  8220. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  8221. (org-show-context 'occur-tree))))
  8222. (when org-remove-highlights-with-change
  8223. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  8224. nil 'local))
  8225. (unless org-sparse-tree-open-archived-trees
  8226. (org-hide-archived-subtrees (point-min) (point-max)))
  8227. (run-hooks 'org-occur-hook)
  8228. (if (interactive-p)
  8229. (message "%d match(es) for regexp %s" cnt regexp))
  8230. cnt))
  8231. (defun org-show-context (&optional key)
  8232. "Make sure point and context and visible.
  8233. How much context is shown depends upon the variables
  8234. `org-show-hierarchy-above', `org-show-following-heading'. and
  8235. `org-show-siblings'."
  8236. (let ((heading-p (org-on-heading-p t))
  8237. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  8238. (following-p (org-get-alist-option org-show-following-heading key))
  8239. (entry-p (org-get-alist-option org-show-entry-below key))
  8240. (siblings-p (org-get-alist-option org-show-siblings key)))
  8241. (catch 'exit
  8242. ;; Show heading or entry text
  8243. (if (and heading-p (not entry-p))
  8244. (org-flag-heading nil) ; only show the heading
  8245. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  8246. (org-show-hidden-entry))) ; show entire entry
  8247. (when following-p
  8248. ;; Show next sibling, or heading below text
  8249. (save-excursion
  8250. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  8251. (org-flag-heading nil))))
  8252. (when siblings-p (org-show-siblings))
  8253. (when hierarchy-p
  8254. ;; show all higher headings, possibly with siblings
  8255. (save-excursion
  8256. (while (and (condition-case nil
  8257. (progn (org-up-heading-all 1) t)
  8258. (error nil))
  8259. (not (bobp)))
  8260. (org-flag-heading nil)
  8261. (when siblings-p (org-show-siblings))))))))
  8262. (defun org-reveal (&optional siblings)
  8263. "Show current entry, hierarchy above it, and the following headline.
  8264. This can be used to show a consistent set of context around locations
  8265. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  8266. not t for the search context.
  8267. With optional argument SIBLINGS, on each level of the hierarchy all
  8268. siblings are shown. This repairs the tree structure to what it would
  8269. look like when opened with hierarchical calls to `org-cycle'."
  8270. (interactive "P")
  8271. (let ((org-show-hierarchy-above t)
  8272. (org-show-following-heading t)
  8273. (org-show-siblings (if siblings t org-show-siblings)))
  8274. (org-show-context nil)))
  8275. (defun org-highlight-new-match (beg end)
  8276. "Highlight from BEG to END and mark the highlight is an occur headline."
  8277. (let ((ov (org-make-overlay beg end)))
  8278. (org-overlay-put ov 'face 'secondary-selection)
  8279. (push ov org-occur-highlights)))
  8280. (defun org-remove-occur-highlights (&optional beg end noremove)
  8281. "Remove the occur highlights from the buffer.
  8282. BEG and END are ignored. If NOREMOVE is nil, remove this function
  8283. from the `before-change-functions' in the current buffer."
  8284. (interactive)
  8285. (unless org-inhibit-highlight-removal
  8286. (mapc 'org-delete-overlay org-occur-highlights)
  8287. (setq org-occur-highlights nil)
  8288. (setq org-occur-parameters nil)
  8289. (unless noremove
  8290. (remove-hook 'before-change-functions
  8291. 'org-remove-occur-highlights 'local))))
  8292. ;;;; Priorities
  8293. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  8294. "Regular expression matching the priority indicator.")
  8295. (defvar org-remove-priority-next-time nil)
  8296. (defun org-priority-up ()
  8297. "Increase the priority of the current item."
  8298. (interactive)
  8299. (org-priority 'up))
  8300. (defun org-priority-down ()
  8301. "Decrease the priority of the current item."
  8302. (interactive)
  8303. (org-priority 'down))
  8304. (defun org-priority (&optional action)
  8305. "Change the priority of an item by ARG.
  8306. ACTION can be `set', `up', `down', or a character."
  8307. (interactive)
  8308. (setq action (or action 'set))
  8309. (let (current new news have remove)
  8310. (save-excursion
  8311. (org-back-to-heading)
  8312. (if (looking-at org-priority-regexp)
  8313. (setq current (string-to-char (match-string 2))
  8314. have t)
  8315. (setq current org-default-priority))
  8316. (cond
  8317. ((or (eq action 'set) (integerp action))
  8318. (if (integerp action)
  8319. (setq new action)
  8320. (message "Priority %c-%c, SPC to remove: " org-highest-priority org-lowest-priority)
  8321. (setq new (read-char-exclusive)))
  8322. (if (and (= (upcase org-highest-priority) org-highest-priority)
  8323. (= (upcase org-lowest-priority) org-lowest-priority))
  8324. (setq new (upcase new)))
  8325. (cond ((equal new ?\ ) (setq remove t))
  8326. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  8327. (error "Priority must be between `%c' and `%c'"
  8328. org-highest-priority org-lowest-priority))))
  8329. ((eq action 'up)
  8330. (if (and (not have) (eq last-command this-command))
  8331. (setq new org-lowest-priority)
  8332. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8333. org-default-priority (1- current)))))
  8334. ((eq action 'down)
  8335. (if (and (not have) (eq last-command this-command))
  8336. (setq new org-highest-priority)
  8337. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8338. org-default-priority (1+ current)))))
  8339. (t (error "Invalid action")))
  8340. (if (or (< (upcase new) org-highest-priority)
  8341. (> (upcase new) org-lowest-priority))
  8342. (setq remove t))
  8343. (setq news (format "%c" new))
  8344. (if have
  8345. (if remove
  8346. (replace-match "" t t nil 1)
  8347. (replace-match news t t nil 2))
  8348. (if remove
  8349. (error "No priority cookie found in line")
  8350. (looking-at org-todo-line-regexp)
  8351. (if (match-end 2)
  8352. (progn
  8353. (goto-char (match-end 2))
  8354. (insert " [#" news "]"))
  8355. (goto-char (match-beginning 3))
  8356. (insert "[#" news "] ")))))
  8357. (org-preserve-lc (org-set-tags nil 'align))
  8358. (if remove
  8359. (message "Priority removed")
  8360. (message "Priority of current item set to %s" news))))
  8361. (defun org-get-priority (s)
  8362. "Find priority cookie and return priority."
  8363. (save-match-data
  8364. (if (not (string-match org-priority-regexp s))
  8365. (* 1000 (- org-lowest-priority org-default-priority))
  8366. (* 1000 (- org-lowest-priority
  8367. (string-to-char (match-string 2 s)))))))
  8368. ;;;; Tags
  8369. (defun org-scan-tags (action matcher &optional todo-only)
  8370. "Scan headline tags with inheritance and produce output ACTION.
  8371. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  8372. evaluated, testing if a given set of tags qualifies a headline for
  8373. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  8374. are included in the output."
  8375. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8376. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  8377. (org-re
  8378. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  8379. (props (list 'face nil
  8380. 'done-face 'org-done
  8381. 'undone-face nil
  8382. 'mouse-face 'highlight
  8383. 'org-not-done-regexp org-not-done-regexp
  8384. 'org-todo-regexp org-todo-regexp
  8385. 'keymap org-agenda-keymap
  8386. 'help-echo
  8387. (format "mouse-2 or RET jump to org file %s"
  8388. (abbreviate-file-name
  8389. (or (buffer-file-name (buffer-base-buffer))
  8390. (buffer-name (buffer-base-buffer)))))))
  8391. (case-fold-search nil)
  8392. lspos
  8393. tags tags-list tags-alist (llast 0) rtn level category i txt
  8394. todo marker entry priority)
  8395. (save-excursion
  8396. (goto-char (point-min))
  8397. (when (eq action 'sparse-tree)
  8398. (org-overview)
  8399. (org-remove-occur-highlights))
  8400. (while (re-search-forward re nil t)
  8401. (catch :skip
  8402. (setq todo (if (match-end 1) (match-string 2))
  8403. tags (if (match-end 4) (match-string 4)))
  8404. (goto-char (setq lspos (1+ (match-beginning 0))))
  8405. (setq level (org-reduced-level (funcall outline-level))
  8406. category (org-get-category))
  8407. (setq i llast llast level)
  8408. ;; remove tag lists from same and sublevels
  8409. (while (>= i level)
  8410. (when (setq entry (assoc i tags-alist))
  8411. (setq tags-alist (delete entry tags-alist)))
  8412. (setq i (1- i)))
  8413. ;; add the next tags
  8414. (when tags
  8415. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8416. tags-alist
  8417. (cons (cons level tags) tags-alist)))
  8418. ;; compile tags for current headline
  8419. (setq tags-list
  8420. (if org-use-tag-inheritance
  8421. (apply 'append (mapcar 'cdr tags-alist))
  8422. tags))
  8423. (when (and tags org-use-tag-inheritance
  8424. (not (eq t org-use-tag-inheritance)))
  8425. ;; selective inheritance, remove uninherited ones
  8426. (setcdr (car tags-alist)
  8427. (org-remove-uniherited-tags (cdar tags-alist))))
  8428. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  8429. (eval matcher)
  8430. (or (not org-agenda-skip-archived-trees)
  8431. (not (member org-archive-tag tags-list))))
  8432. (and (eq action 'agenda) (org-agenda-skip))
  8433. ;; list this headline
  8434. (if (eq action 'sparse-tree)
  8435. (progn
  8436. (and org-highlight-sparse-tree-matches
  8437. (org-get-heading) (match-end 0)
  8438. (org-highlight-new-match
  8439. (match-beginning 0) (match-beginning 1)))
  8440. (org-show-context 'tags-tree))
  8441. (setq txt (org-format-agenda-item
  8442. ""
  8443. (concat
  8444. (if org-tags-match-list-sublevels
  8445. (make-string (1- level) ?.) "")
  8446. (org-get-heading))
  8447. category tags-list)
  8448. priority (org-get-priority txt))
  8449. (goto-char lspos)
  8450. (setq marker (org-agenda-new-marker))
  8451. (org-add-props txt props
  8452. 'org-marker marker 'org-hd-marker marker 'org-category category
  8453. 'priority priority 'type "tagsmatch")
  8454. (push txt rtn))
  8455. ;; if we are to skip sublevels, jump to end of subtree
  8456. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8457. (when (and (eq action 'sparse-tree)
  8458. (not org-sparse-tree-open-archived-trees))
  8459. (org-hide-archived-subtrees (point-min) (point-max)))
  8460. (nreverse rtn)))
  8461. (defun org-remove-uniherited-tags (tags)
  8462. "Remove all tags that are not inherited from the list TAGS."
  8463. (cond
  8464. ((eq org-use-tag-inheritance t) tags)
  8465. ((not org-use-tag-inheritance) nil)
  8466. ((stringp org-use-tag-inheritance)
  8467. (delq nil (mapcar
  8468. (lambda (x) (if (string-match org-use-tag-inheritance x) x nil))
  8469. tags)))
  8470. ((listp org-use-tag-inheritance)
  8471. (org-delete-all org-use-tag-inheritance tags))))
  8472. (defvar todo-only) ;; dynamically scoped
  8473. (defun org-tags-sparse-tree (&optional todo-only match)
  8474. "Create a sparse tree according to tags string MATCH.
  8475. MATCH can contain positive and negative selection of tags, like
  8476. \"+WORK+URGENT-WITHBOSS\".
  8477. If optional argument TODO_ONLY is non-nil, only select lines that are
  8478. also TODO lines."
  8479. (interactive "P")
  8480. (org-prepare-agenda-buffers (list (current-buffer)))
  8481. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  8482. (defvar org-cached-props nil)
  8483. (defun org-cached-entry-get (pom property)
  8484. (if (or (eq t org-use-property-inheritance)
  8485. (and (stringp org-use-property-inheritance)
  8486. (string-match org-use-property-inheritance property))
  8487. (and (listp org-use-property-inheritance)
  8488. (member property org-use-property-inheritance)))
  8489. ;; Caching is not possible, check it directly
  8490. (org-entry-get pom property 'inherit)
  8491. ;; Get all properties, so that we can do complicated checks easily
  8492. (cdr (assoc property (or org-cached-props
  8493. (setq org-cached-props
  8494. (org-entry-properties pom)))))))
  8495. (defun org-global-tags-completion-table (&optional files)
  8496. "Return the list of all tags in all agenda buffer/files."
  8497. (save-excursion
  8498. (org-uniquify
  8499. (delq nil
  8500. (apply 'append
  8501. (mapcar
  8502. (lambda (file)
  8503. (set-buffer (find-file-noselect file))
  8504. (append (org-get-buffer-tags)
  8505. (mapcar (lambda (x) (if (stringp (car-safe x))
  8506. (list (car-safe x)) nil))
  8507. org-tag-alist)))
  8508. (if (and files (car files))
  8509. files
  8510. (org-agenda-files))))))))
  8511. (defun org-make-tags-matcher (match)
  8512. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8513. ;; todo-only is scoped dynamically into this function, and the function
  8514. ;; may change it it the matcher asksk for it.
  8515. (unless match
  8516. ;; Get a new match request, with completion
  8517. (let ((org-last-tags-completion-table
  8518. (org-global-tags-completion-table)))
  8519. (setq match (completing-read
  8520. "Match: " 'org-tags-completion-function nil nil nil
  8521. 'org-tags-history))))
  8522. ;; Parse the string and create a lisp form
  8523. (let ((match0 match)
  8524. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL=\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)=\\({[^}]+}\\|\"[^\"]*\"\\)\\|[[:alnum:]_@]+\\)"))
  8525. minus tag mm
  8526. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  8527. orterms term orlist re-p level-p prop-p pn pv cat-p gv)
  8528. (if (string-match "/+" match)
  8529. ;; match contains also a todo-matching request
  8530. (progn
  8531. (setq tagsmatch (substring match 0 (match-beginning 0))
  8532. todomatch (substring match (match-end 0)))
  8533. (if (string-match "^!" todomatch)
  8534. (setq todo-only t todomatch (substring todomatch 1)))
  8535. (if (string-match "^\\s-*$" todomatch)
  8536. (setq todomatch nil)))
  8537. ;; only matching tags
  8538. (setq tagsmatch match todomatch nil))
  8539. ;; Make the tags matcher
  8540. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8541. (setq tagsmatcher t)
  8542. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8543. (while (setq term (pop orterms))
  8544. (while (and (equal (substring term -1) "\\") orterms)
  8545. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8546. (while (string-match re term)
  8547. (setq minus (and (match-end 1)
  8548. (equal (match-string 1 term) "-"))
  8549. tag (match-string 2 term)
  8550. re-p (equal (string-to-char tag) ?{)
  8551. level-p (match-end 3)
  8552. prop-p (match-end 4)
  8553. mm (cond
  8554. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8555. (level-p `(= level ,(string-to-number
  8556. (match-string 3 term))))
  8557. (prop-p
  8558. (setq pn (match-string 4 term)
  8559. pv (match-string 5 term)
  8560. cat-p (equal pn "CATEGORY")
  8561. re-p (equal (string-to-char pv) ?{)
  8562. pv (substring pv 1 -1))
  8563. (if (equal pn "CATEGORY")
  8564. (setq gv '(get-text-property (point) 'org-category))
  8565. (setq gv `(org-cached-entry-get nil ,pn)))
  8566. (if re-p
  8567. `(string-match ,pv (or ,gv ""))
  8568. `(equal ,pv (or ,gv ""))))
  8569. (t `(member ,(downcase tag) tags-list)))
  8570. mm (if minus (list 'not mm) mm)
  8571. term (substring term (match-end 0)))
  8572. (push mm tagsmatcher))
  8573. (push (if (> (length tagsmatcher) 1)
  8574. (cons 'and tagsmatcher)
  8575. (car tagsmatcher))
  8576. orlist)
  8577. (setq tagsmatcher nil))
  8578. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8579. (setq tagsmatcher
  8580. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  8581. ;; Make the todo matcher
  8582. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8583. (setq todomatcher t)
  8584. (setq orterms (org-split-string todomatch "|") orlist nil)
  8585. (while (setq term (pop orterms))
  8586. (while (string-match re term)
  8587. (setq minus (and (match-end 1)
  8588. (equal (match-string 1 term) "-"))
  8589. kwd (match-string 2 term)
  8590. re-p (equal (string-to-char kwd) ?{)
  8591. term (substring term (match-end 0))
  8592. mm (if re-p
  8593. `(string-match ,(substring kwd 1 -1) todo)
  8594. (list 'equal 'todo kwd))
  8595. mm (if minus (list 'not mm) mm))
  8596. (push mm todomatcher))
  8597. (push (if (> (length todomatcher) 1)
  8598. (cons 'and todomatcher)
  8599. (car todomatcher))
  8600. orlist)
  8601. (setq todomatcher nil))
  8602. (setq todomatcher (if (> (length orlist) 1)
  8603. (cons 'or orlist) (car orlist))))
  8604. ;; Return the string and lisp forms of the matcher
  8605. (setq matcher (if todomatcher
  8606. (list 'and tagsmatcher todomatcher)
  8607. tagsmatcher))
  8608. (cons match0 matcher)))
  8609. (defun org-match-any-p (re list)
  8610. "Does re match any element of list?"
  8611. (setq list (mapcar (lambda (x) (string-match re x)) list))
  8612. (delq nil list))
  8613. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  8614. (defvar org-tags-overlay (org-make-overlay 1 1))
  8615. (org-detach-overlay org-tags-overlay)
  8616. (defun org-align-tags-here (to-col)
  8617. ;; Assumes that this is a headline
  8618. (let ((pos (point)) (col (current-column)) tags)
  8619. (beginning-of-line 1)
  8620. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8621. (< pos (match-beginning 2)))
  8622. (progn
  8623. (setq tags (match-string 2))
  8624. (goto-char (match-beginning 1))
  8625. (insert " ")
  8626. (delete-region (point) (1+ (match-end 0)))
  8627. (backward-char 1)
  8628. (move-to-column
  8629. (max (1+ (current-column))
  8630. (1+ col)
  8631. (if (> to-col 0)
  8632. to-col
  8633. (- (abs to-col) (length tags))))
  8634. t)
  8635. (insert tags)
  8636. (move-to-column (min (current-column) col) t))
  8637. (goto-char pos))))
  8638. (defun org-set-tags (&optional arg just-align)
  8639. "Set the tags for the current headline.
  8640. With prefix ARG, realign all tags in headings in the current buffer."
  8641. (interactive "P")
  8642. (let* ((re (concat "^" outline-regexp))
  8643. (current (org-get-tags-string))
  8644. (col (current-column))
  8645. (org-setting-tags t)
  8646. table current-tags inherited-tags ; computed below when needed
  8647. tags p0 c0 c1 rpl)
  8648. (if arg
  8649. (save-excursion
  8650. (goto-char (point-min))
  8651. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  8652. (while (re-search-forward re nil t)
  8653. (org-set-tags nil t)
  8654. (end-of-line 1)))
  8655. (message "All tags realigned to column %d" org-tags-column))
  8656. (if just-align
  8657. (setq tags current)
  8658. ;; Get a new set of tags from the user
  8659. (save-excursion
  8660. (setq table (or org-tag-alist (org-get-buffer-tags))
  8661. org-last-tags-completion-table table
  8662. current-tags (org-split-string current ":")
  8663. inherited-tags (nreverse
  8664. (nthcdr (length current-tags)
  8665. (nreverse (org-get-tags-at))))
  8666. tags
  8667. (if (or (eq t org-use-fast-tag-selection)
  8668. (and org-use-fast-tag-selection
  8669. (delq nil (mapcar 'cdr table))))
  8670. (org-fast-tag-selection
  8671. current-tags inherited-tags table
  8672. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  8673. (let ((org-add-colon-after-tag-completion t))
  8674. (org-trim
  8675. (org-without-partial-completion
  8676. (completing-read "Tags: " 'org-tags-completion-function
  8677. nil nil current 'org-tags-history)))))))
  8678. (while (string-match "[-+&]+" tags)
  8679. ;; No boolean logic, just a list
  8680. (setq tags (replace-match ":" t t tags))))
  8681. (if (string-match "\\`[\t ]*\\'" tags)
  8682. (setq tags "")
  8683. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  8684. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  8685. ;; Insert new tags at the correct column
  8686. (beginning-of-line 1)
  8687. (cond
  8688. ((and (equal current "") (equal tags "")))
  8689. ((re-search-forward
  8690. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  8691. (point-at-eol) t)
  8692. (if (equal tags "")
  8693. (setq rpl "")
  8694. (goto-char (match-beginning 0))
  8695. (setq c0 (current-column) p0 (point)
  8696. c1 (max (1+ c0) (if (> org-tags-column 0)
  8697. org-tags-column
  8698. (- (- org-tags-column) (length tags))))
  8699. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  8700. (replace-match rpl t t)
  8701. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  8702. tags)
  8703. (t (error "Tags alignment failed")))
  8704. (move-to-column col)
  8705. (unless just-align
  8706. (run-hooks 'org-after-tags-change-hook)))))
  8707. (defun org-change-tag-in-region (beg end tag off)
  8708. "Add or remove TAG for each entry in the region.
  8709. This works in the agenda, and also in an org-mode buffer."
  8710. (interactive
  8711. (list (region-beginning) (region-end)
  8712. (let ((org-last-tags-completion-table
  8713. (if (org-mode-p)
  8714. (org-get-buffer-tags)
  8715. (org-global-tags-completion-table))))
  8716. (completing-read
  8717. "Tag: " 'org-tags-completion-function nil nil nil
  8718. 'org-tags-history))
  8719. (progn
  8720. (message "[s]et or [r]emove? ")
  8721. (equal (read-char-exclusive) ?r))))
  8722. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8723. (let ((agendap (equal major-mode 'org-agenda-mode))
  8724. l1 l2 m buf pos newhead (cnt 0))
  8725. (goto-char end)
  8726. (setq l2 (1- (org-current-line)))
  8727. (goto-char beg)
  8728. (setq l1 (org-current-line))
  8729. (loop for l from l1 to l2 do
  8730. (goto-line l)
  8731. (setq m (get-text-property (point) 'org-hd-marker))
  8732. (when (or (and (org-mode-p) (org-on-heading-p))
  8733. (and agendap m))
  8734. (setq buf (if agendap (marker-buffer m) (current-buffer))
  8735. pos (if agendap m (point)))
  8736. (with-current-buffer buf
  8737. (save-excursion
  8738. (save-restriction
  8739. (goto-char pos)
  8740. (setq cnt (1+ cnt))
  8741. (org-toggle-tag tag (if off 'off 'on))
  8742. (setq newhead (org-get-heading)))))
  8743. (and agendap (org-agenda-change-all-lines newhead m))))
  8744. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  8745. (defun org-tags-completion-function (string predicate &optional flag)
  8746. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  8747. (confirm (lambda (x) (stringp (car x)))))
  8748. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  8749. (setq s1 (match-string 1 string)
  8750. s2 (match-string 2 string))
  8751. (setq s1 "" s2 string))
  8752. (cond
  8753. ((eq flag nil)
  8754. ;; try completion
  8755. (setq rtn (try-completion s2 ctable confirm))
  8756. (if (stringp rtn)
  8757. (setq rtn
  8758. (concat s1 s2 (substring rtn (length s2))
  8759. (if (and org-add-colon-after-tag-completion
  8760. (assoc rtn ctable))
  8761. ":" ""))))
  8762. rtn)
  8763. ((eq flag t)
  8764. ;; all-completions
  8765. (all-completions s2 ctable confirm)
  8766. )
  8767. ((eq flag 'lambda)
  8768. ;; exact match?
  8769. (assoc s2 ctable)))
  8770. ))
  8771. (defun org-fast-tag-insert (kwd tags face &optional end)
  8772. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  8773. (insert (format "%-12s" (concat kwd ":"))
  8774. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  8775. (or end "")))
  8776. (defun org-fast-tag-show-exit (flag)
  8777. (save-excursion
  8778. (goto-line 3)
  8779. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  8780. (replace-match ""))
  8781. (when flag
  8782. (end-of-line 1)
  8783. (move-to-column (- (window-width) 19) t)
  8784. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  8785. (defun org-set-current-tags-overlay (current prefix)
  8786. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  8787. (if (featurep 'xemacs)
  8788. (org-overlay-display org-tags-overlay (concat prefix s)
  8789. 'secondary-selection)
  8790. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  8791. (org-overlay-display org-tags-overlay (concat prefix s)))))
  8792. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  8793. "Fast tag selection with single keys.
  8794. CURRENT is the current list of tags in the headline, INHERITED is the
  8795. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  8796. possibly with grouping information. TODO-TABLE is a similar table with
  8797. TODO keywords, should these have keys assigned to them.
  8798. If the keys are nil, a-z are automatically assigned.
  8799. Returns the new tags string, or nil to not change the current settings."
  8800. (let* ((fulltable (append table todo-table))
  8801. (maxlen (apply 'max (mapcar
  8802. (lambda (x)
  8803. (if (stringp (car x)) (string-width (car x)) 0))
  8804. fulltable)))
  8805. (buf (current-buffer))
  8806. (expert (eq org-fast-tag-selection-single-key 'expert))
  8807. (buffer-tags nil)
  8808. (fwidth (+ maxlen 3 1 3))
  8809. (ncol (/ (- (window-width) 4) fwidth))
  8810. (i-face 'org-done)
  8811. (c-face 'org-todo)
  8812. tg cnt e c char c1 c2 ntable tbl rtn
  8813. ov-start ov-end ov-prefix
  8814. (exit-after-next org-fast-tag-selection-single-key)
  8815. (done-keywords org-done-keywords)
  8816. groups ingroup)
  8817. (save-excursion
  8818. (beginning-of-line 1)
  8819. (if (looking-at
  8820. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8821. (setq ov-start (match-beginning 1)
  8822. ov-end (match-end 1)
  8823. ov-prefix "")
  8824. (setq ov-start (1- (point-at-eol))
  8825. ov-end (1+ ov-start))
  8826. (skip-chars-forward "^\n\r")
  8827. (setq ov-prefix
  8828. (concat
  8829. (buffer-substring (1- (point)) (point))
  8830. (if (> (current-column) org-tags-column)
  8831. " "
  8832. (make-string (- org-tags-column (current-column)) ?\ ))))))
  8833. (org-move-overlay org-tags-overlay ov-start ov-end)
  8834. (save-window-excursion
  8835. (if expert
  8836. (set-buffer (get-buffer-create " *Org tags*"))
  8837. (delete-other-windows)
  8838. (split-window-vertically)
  8839. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  8840. (erase-buffer)
  8841. (org-set-local 'org-done-keywords done-keywords)
  8842. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  8843. (org-fast-tag-insert "Current" current c-face "\n\n")
  8844. (org-fast-tag-show-exit exit-after-next)
  8845. (org-set-current-tags-overlay current ov-prefix)
  8846. (setq tbl fulltable char ?a cnt 0)
  8847. (while (setq e (pop tbl))
  8848. (cond
  8849. ((equal e '(:startgroup))
  8850. (push '() groups) (setq ingroup t)
  8851. (when (not (= cnt 0))
  8852. (setq cnt 0)
  8853. (insert "\n"))
  8854. (insert "{ "))
  8855. ((equal e '(:endgroup))
  8856. (setq ingroup nil cnt 0)
  8857. (insert "}\n"))
  8858. (t
  8859. (setq tg (car e) c2 nil)
  8860. (if (cdr e)
  8861. (setq c (cdr e))
  8862. ;; automatically assign a character.
  8863. (setq c1 (string-to-char
  8864. (downcase (substring
  8865. tg (if (= (string-to-char tg) ?@) 1 0)))))
  8866. (if (or (rassoc c1 ntable) (rassoc c1 table))
  8867. (while (or (rassoc char ntable) (rassoc char table))
  8868. (setq char (1+ char)))
  8869. (setq c2 c1))
  8870. (setq c (or c2 char)))
  8871. (if ingroup (push tg (car groups)))
  8872. (setq tg (org-add-props tg nil 'face
  8873. (cond
  8874. ((not (assoc tg table))
  8875. (org-get-todo-face tg))
  8876. ((member tg current) c-face)
  8877. ((member tg inherited) i-face)
  8878. (t nil))))
  8879. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8880. (insert "[" c "] " tg (make-string
  8881. (- fwidth 4 (length tg)) ?\ ))
  8882. (push (cons tg c) ntable)
  8883. (when (= (setq cnt (1+ cnt)) ncol)
  8884. (insert "\n")
  8885. (if ingroup (insert " "))
  8886. (setq cnt 0)))))
  8887. (setq ntable (nreverse ntable))
  8888. (insert "\n")
  8889. (goto-char (point-min))
  8890. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  8891. (fit-window-to-buffer))
  8892. (setq rtn
  8893. (catch 'exit
  8894. (while t
  8895. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  8896. (if groups " [!] no groups" " [!]groups")
  8897. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  8898. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8899. (cond
  8900. ((= c ?\r) (throw 'exit t))
  8901. ((= c ?!)
  8902. (setq groups (not groups))
  8903. (goto-char (point-min))
  8904. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  8905. ((= c ?\C-c)
  8906. (if (not expert)
  8907. (org-fast-tag-show-exit
  8908. (setq exit-after-next (not exit-after-next)))
  8909. (setq expert nil)
  8910. (delete-other-windows)
  8911. (split-window-vertically)
  8912. (org-switch-to-buffer-other-window " *Org tags*")
  8913. (and (fboundp 'fit-window-to-buffer)
  8914. (fit-window-to-buffer))))
  8915. ((or (= c ?\C-g)
  8916. (and (= c ?q) (not (rassoc c ntable))))
  8917. (org-detach-overlay org-tags-overlay)
  8918. (setq quit-flag t))
  8919. ((= c ?\ )
  8920. (setq current nil)
  8921. (if exit-after-next (setq exit-after-next 'now)))
  8922. ((= c ?\t)
  8923. (condition-case nil
  8924. (setq tg (completing-read
  8925. "Tag: "
  8926. (or buffer-tags
  8927. (with-current-buffer buf
  8928. (org-get-buffer-tags)))))
  8929. (quit (setq tg "")))
  8930. (when (string-match "\\S-" tg)
  8931. (add-to-list 'buffer-tags (list tg))
  8932. (if (member tg current)
  8933. (setq current (delete tg current))
  8934. (push tg current)))
  8935. (if exit-after-next (setq exit-after-next 'now)))
  8936. ((setq e (rassoc c todo-table) tg (car e))
  8937. (with-current-buffer buf
  8938. (save-excursion (org-todo tg)))
  8939. (if exit-after-next (setq exit-after-next 'now)))
  8940. ((setq e (rassoc c ntable) tg (car e))
  8941. (if (member tg current)
  8942. (setq current (delete tg current))
  8943. (loop for g in groups do
  8944. (if (member tg g)
  8945. (mapc (lambda (x)
  8946. (setq current (delete x current)))
  8947. g)))
  8948. (push tg current))
  8949. (if exit-after-next (setq exit-after-next 'now))))
  8950. ;; Create a sorted list
  8951. (setq current
  8952. (sort current
  8953. (lambda (a b)
  8954. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  8955. (if (eq exit-after-next 'now) (throw 'exit t))
  8956. (goto-char (point-min))
  8957. (beginning-of-line 2)
  8958. (delete-region (point) (point-at-eol))
  8959. (org-fast-tag-insert "Current" current c-face)
  8960. (org-set-current-tags-overlay current ov-prefix)
  8961. (while (re-search-forward
  8962. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  8963. (setq tg (match-string 1))
  8964. (add-text-properties
  8965. (match-beginning 1) (match-end 1)
  8966. (list 'face
  8967. (cond
  8968. ((member tg current) c-face)
  8969. ((member tg inherited) i-face)
  8970. (t (get-text-property (match-beginning 1) 'face))))))
  8971. (goto-char (point-min)))))
  8972. (org-detach-overlay org-tags-overlay)
  8973. (if rtn
  8974. (mapconcat 'identity current ":")
  8975. nil))))
  8976. (defun org-get-tags-string ()
  8977. "Get the TAGS string in the current headline."
  8978. (unless (org-on-heading-p t)
  8979. (error "Not on a heading"))
  8980. (save-excursion
  8981. (beginning-of-line 1)
  8982. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8983. (org-match-string-no-properties 1)
  8984. "")))
  8985. (defun org-get-tags ()
  8986. "Get the list of tags specified in the current headline."
  8987. (org-split-string (org-get-tags-string) ":"))
  8988. (defun org-get-buffer-tags ()
  8989. "Get a table of all tags used in the buffer, for completion."
  8990. (let (tags)
  8991. (save-excursion
  8992. (goto-char (point-min))
  8993. (while (re-search-forward
  8994. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  8995. (when (equal (char-after (point-at-bol 0)) ?*)
  8996. (mapc (lambda (x) (add-to-list 'tags x))
  8997. (org-split-string (org-match-string-no-properties 1) ":")))))
  8998. (mapcar 'list tags)))
  8999. ;;;; Properties
  9000. ;;; Setting and retrieving properties
  9001. (defconst org-special-properties
  9002. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
  9003. "TIMESTAMP" "TIMESTAMP_IA")
  9004. "The special properties valid in Org-mode.
  9005. These are properties that are not defined in the property drawer,
  9006. but in some other way.")
  9007. (defconst org-default-properties
  9008. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  9009. "LOCATION" "LOGGING" "COLUMNS")
  9010. "Some properties that are used by Org-mode for various purposes.
  9011. Being in this list makes sure that they are offered for completion.")
  9012. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  9013. "Regular expression matching the first line of a property drawer.")
  9014. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  9015. "Regular expression matching the first line of a property drawer.")
  9016. (defun org-property-action ()
  9017. "Do an action on properties."
  9018. (interactive)
  9019. (let (c)
  9020. (org-at-property-p)
  9021. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  9022. (setq c (read-char-exclusive))
  9023. (cond
  9024. ((equal c ?s)
  9025. (call-interactively 'org-set-property))
  9026. ((equal c ?d)
  9027. (call-interactively 'org-delete-property))
  9028. ((equal c ?D)
  9029. (call-interactively 'org-delete-property-globally))
  9030. ((equal c ?c)
  9031. (call-interactively 'org-compute-property-at-point))
  9032. (t (error "No such property action %c" c)))))
  9033. (defun org-at-property-p ()
  9034. "Is the cursor in a property line?"
  9035. ;; FIXME: Does not check if we are actually in the drawer.
  9036. ;; FIXME: also returns true on any drawers.....
  9037. ;; This is used by C-c C-c for property action.
  9038. (save-excursion
  9039. (beginning-of-line 1)
  9040. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  9041. (defun org-get-property-block (&optional beg end force)
  9042. "Return the (beg . end) range of the body of the property drawer.
  9043. BEG and END can be beginning and end of subtree, if not given
  9044. they will be found.
  9045. If the drawer does not exist and FORCE is non-nil, create the drawer."
  9046. (catch 'exit
  9047. (save-excursion
  9048. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  9049. (end (or end (progn (outline-next-heading) (point)))))
  9050. (goto-char beg)
  9051. (if (re-search-forward org-property-start-re end t)
  9052. (setq beg (1+ (match-end 0)))
  9053. (if force
  9054. (save-excursion
  9055. (org-insert-property-drawer)
  9056. (setq end (progn (outline-next-heading) (point))))
  9057. (throw 'exit nil))
  9058. (goto-char beg)
  9059. (if (re-search-forward org-property-start-re end t)
  9060. (setq beg (1+ (match-end 0)))))
  9061. (if (re-search-forward org-property-end-re end t)
  9062. (setq end (match-beginning 0))
  9063. (or force (throw 'exit nil))
  9064. (goto-char beg)
  9065. (setq end beg)
  9066. (org-indent-line-function)
  9067. (insert ":END:\n"))
  9068. (cons beg end)))))
  9069. (defun org-entry-properties (&optional pom which)
  9070. "Get all properties of the entry at point-or-marker POM.
  9071. This includes the TODO keyword, the tags, time strings for deadline,
  9072. scheduled, and clocking, and any additional properties defined in the
  9073. entry. The return value is an alist, keys may occur multiple times
  9074. if the property key was used several times.
  9075. POM may also be nil, in which case the current entry is used.
  9076. If WHICH is nil or `all', get all properties. If WHICH is
  9077. `special' or `standard', only get that subclass."
  9078. (setq which (or which 'all))
  9079. (org-with-point-at pom
  9080. (let ((clockstr (substring org-clock-string 0 -1))
  9081. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  9082. beg end range props sum-props key value string clocksum)
  9083. (save-excursion
  9084. (when (condition-case nil (org-back-to-heading t) (error nil))
  9085. (setq beg (point))
  9086. (setq sum-props (get-text-property (point) 'org-summaries))
  9087. (setq clocksum (get-text-property (point) :org-clock-minutes))
  9088. (outline-next-heading)
  9089. (setq end (point))
  9090. (when (memq which '(all special))
  9091. ;; Get the special properties, like TODO and tags
  9092. (goto-char beg)
  9093. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  9094. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  9095. (when (looking-at org-priority-regexp)
  9096. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  9097. (when (and (setq value (org-get-tags-string))
  9098. (string-match "\\S-" value))
  9099. (push (cons "TAGS" value) props))
  9100. (when (setq value (org-get-tags-at))
  9101. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  9102. props))
  9103. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  9104. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  9105. string (if (equal key clockstr)
  9106. (org-no-properties
  9107. (org-trim
  9108. (buffer-substring
  9109. (match-beginning 3) (goto-char (point-at-eol)))))
  9110. (substring (org-match-string-no-properties 3) 1 -1)))
  9111. (unless key
  9112. (if (= (char-after (match-beginning 3)) ?\[)
  9113. (setq key "TIMESTAMP_IA")
  9114. (setq key "TIMESTAMP")))
  9115. (when (or (equal key clockstr) (not (assoc key props)))
  9116. (push (cons key string) props)))
  9117. )
  9118. (when (memq which '(all standard))
  9119. ;; Get the standard properties, like :PORP: ...
  9120. (setq range (org-get-property-block beg end))
  9121. (when range
  9122. (goto-char (car range))
  9123. (while (re-search-forward
  9124. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  9125. (cdr range) t)
  9126. (setq key (org-match-string-no-properties 1)
  9127. value (org-trim (or (org-match-string-no-properties 2) "")))
  9128. (unless (member key excluded)
  9129. (push (cons key (or value "")) props)))))
  9130. (if clocksum
  9131. (push (cons "CLOCKSUM"
  9132. (org-column-number-to-string (/ (float clocksum) 60.)
  9133. 'add_times))
  9134. props))
  9135. (append sum-props (nreverse props)))))))
  9136. (defun org-entry-get (pom property &optional inherit)
  9137. "Get value of PROPERTY for entry at point-or-marker POM.
  9138. If INHERIT is non-nil and the entry does not have the property,
  9139. then also check higher levels of the hierarchy.
  9140. If INHERIT is the symbol `selective', use inheritance only if the setting
  9141. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  9142. If the property is present but empty, the return value is the empty string.
  9143. If the property is not present at all, nil is returned."
  9144. (org-with-point-at pom
  9145. (if (and inherit (if (eq inherit 'selective)
  9146. (org-property-inherit-p property)
  9147. t))
  9148. (org-entry-get-with-inheritance property)
  9149. (if (member property org-special-properties)
  9150. ;; We need a special property. Use brute force, get all properties.
  9151. (cdr (assoc property (org-entry-properties nil 'special)))
  9152. (let ((range (org-get-property-block)))
  9153. (if (and range
  9154. (goto-char (car range))
  9155. (re-search-forward
  9156. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  9157. (cdr range) t))
  9158. ;; Found the property, return it.
  9159. (if (match-end 1)
  9160. (org-match-string-no-properties 1)
  9161. "")))))))
  9162. (defun org-entry-delete (pom property)
  9163. "Delete the property PROPERTY from entry at point-or-marker POM."
  9164. (org-with-point-at pom
  9165. (if (member property org-special-properties)
  9166. nil ; cannot delete these properties.
  9167. (let ((range (org-get-property-block)))
  9168. (if (and range
  9169. (goto-char (car range))
  9170. (re-search-forward
  9171. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  9172. (cdr range) t))
  9173. (progn
  9174. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  9175. t)
  9176. nil)))))
  9177. ;; Multi-values properties are properties that contain multiple values
  9178. ;; These values are assumed to be single words, separated by whitespace.
  9179. (defun org-entry-add-to-multivalued-property (pom property value)
  9180. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  9181. (let* ((old (org-entry-get pom property))
  9182. (values (and old (org-split-string old "[ \t]"))))
  9183. (unless (member value values)
  9184. (setq values (cons value values))
  9185. (org-entry-put pom property
  9186. (mapconcat 'identity values " ")))))
  9187. (defun org-entry-remove-from-multivalued-property (pom property value)
  9188. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  9189. (let* ((old (org-entry-get pom property))
  9190. (values (and old (org-split-string old "[ \t]"))))
  9191. (when (member value values)
  9192. (setq values (delete value values))
  9193. (org-entry-put pom property
  9194. (mapconcat 'identity values " ")))))
  9195. (defun org-entry-member-in-multivalued-property (pom property value)
  9196. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  9197. (let* ((old (org-entry-get pom property))
  9198. (values (and old (org-split-string old "[ \t]"))))
  9199. (member value values)))
  9200. (defvar org-entry-property-inherited-from (make-marker))
  9201. (defun org-entry-get-with-inheritance (property)
  9202. "Get entry property, and search higher levels if not present."
  9203. (let (tmp)
  9204. (save-excursion
  9205. (save-restriction
  9206. (widen)
  9207. (catch 'ex
  9208. (while t
  9209. (when (setq tmp (org-entry-get nil property))
  9210. (org-back-to-heading t)
  9211. (move-marker org-entry-property-inherited-from (point))
  9212. (throw 'ex tmp))
  9213. (or (org-up-heading-safe) (throw 'ex nil)))))
  9214. (or tmp (cdr (assoc property org-local-properties))
  9215. (cdr (assoc property org-global-properties))))))
  9216. (defun org-entry-put (pom property value)
  9217. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  9218. (org-with-point-at pom
  9219. (org-back-to-heading t)
  9220. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  9221. range)
  9222. (cond
  9223. ((equal property "TODO")
  9224. (when (and (stringp value) (string-match "\\S-" value)
  9225. (not (member value org-todo-keywords-1)))
  9226. (error "\"%s\" is not a valid TODO state" value))
  9227. (if (or (not value)
  9228. (not (string-match "\\S-" value)))
  9229. (setq value 'none))
  9230. (org-todo value)
  9231. (org-set-tags nil 'align))
  9232. ((equal property "PRIORITY")
  9233. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  9234. (string-to-char value) ?\ ))
  9235. (org-set-tags nil 'align))
  9236. ((equal property "SCHEDULED")
  9237. (if (re-search-forward org-scheduled-time-regexp end t)
  9238. (cond
  9239. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9240. ((eq value 'later) (org-timestamp-change 1 'day))
  9241. (t (call-interactively 'org-schedule)))
  9242. (call-interactively 'org-schedule)))
  9243. ((equal property "DEADLINE")
  9244. (if (re-search-forward org-deadline-time-regexp end t)
  9245. (cond
  9246. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9247. ((eq value 'later) (org-timestamp-change 1 'day))
  9248. (t (call-interactively 'org-deadline)))
  9249. (call-interactively 'org-deadline)))
  9250. ((member property org-special-properties)
  9251. (error "The %s property can not yet be set with `org-entry-put'"
  9252. property))
  9253. (t ; a non-special property
  9254. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  9255. (setq range (org-get-property-block beg end 'force))
  9256. (goto-char (car range))
  9257. (if (re-search-forward
  9258. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  9259. (progn
  9260. (delete-region (match-beginning 1) (match-end 1))
  9261. (goto-char (match-beginning 1)))
  9262. (goto-char (cdr range))
  9263. (insert "\n")
  9264. (backward-char 1)
  9265. (org-indent-line-function)
  9266. (insert ":" property ":"))
  9267. (and value (insert " " value))
  9268. (org-indent-line-function)))))))
  9269. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  9270. "Get all property keys in the current buffer.
  9271. With INCLUDE-SPECIALS, also list the special properties that relect things
  9272. like tags and TODO state.
  9273. With INCLUDE-DEFAULTS, also include properties that has special meaning
  9274. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  9275. With INCLUDE-COLUMNS, also include property names given in COLUMN
  9276. formats in the current buffer."
  9277. (let (rtn range cfmt cols s p)
  9278. (save-excursion
  9279. (save-restriction
  9280. (widen)
  9281. (goto-char (point-min))
  9282. (while (re-search-forward org-property-start-re nil t)
  9283. (setq range (org-get-property-block))
  9284. (goto-char (car range))
  9285. (while (re-search-forward
  9286. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  9287. (cdr range) t)
  9288. (add-to-list 'rtn (org-match-string-no-properties 1)))
  9289. (outline-next-heading))))
  9290. (when include-specials
  9291. (setq rtn (append org-special-properties rtn)))
  9292. (when include-defaults
  9293. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  9294. (when include-columns
  9295. (save-excursion
  9296. (save-restriction
  9297. (widen)
  9298. (goto-char (point-min))
  9299. (while (re-search-forward
  9300. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  9301. nil t)
  9302. (setq cfmt (match-string 2) s 0)
  9303. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  9304. cfmt s)
  9305. (setq s (match-end 0)
  9306. p (match-string 1 cfmt))
  9307. (unless (or (equal p "ITEM")
  9308. (member p org-special-properties))
  9309. (add-to-list 'rtn (match-string 1 cfmt))))))))
  9310. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  9311. (defun org-property-values (key)
  9312. "Return a list of all values of property KEY."
  9313. (save-excursion
  9314. (save-restriction
  9315. (widen)
  9316. (goto-char (point-min))
  9317. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  9318. values)
  9319. (while (re-search-forward re nil t)
  9320. (add-to-list 'values (org-trim (match-string 1))))
  9321. (delete "" values)))))
  9322. (defun org-insert-property-drawer ()
  9323. "Insert a property drawer into the current entry."
  9324. (interactive)
  9325. (org-back-to-heading t)
  9326. (looking-at outline-regexp)
  9327. (let ((indent (- (match-end 0)(match-beginning 0)))
  9328. (beg (point))
  9329. (re (concat "^[ \t]*" org-keyword-time-regexp))
  9330. end hiddenp)
  9331. (outline-next-heading)
  9332. (setq end (point))
  9333. (goto-char beg)
  9334. (while (re-search-forward re end t))
  9335. (setq hiddenp (org-invisible-p))
  9336. (end-of-line 1)
  9337. (and (equal (char-after) ?\n) (forward-char 1))
  9338. (org-skip-over-state-notes)
  9339. (skip-chars-backward " \t\n\r")
  9340. (if (eq (char-before) ?*) (forward-char 1))
  9341. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  9342. (beginning-of-line 0)
  9343. (indent-to-column indent)
  9344. (beginning-of-line 2)
  9345. (indent-to-column indent)
  9346. (beginning-of-line 0)
  9347. (if hiddenp
  9348. (save-excursion
  9349. (org-back-to-heading t)
  9350. (hide-entry))
  9351. (org-flag-drawer t))))
  9352. (defun org-set-property (property value)
  9353. "In the current entry, set PROPERTY to VALUE.
  9354. When called interactively, this will prompt for a property name, offering
  9355. completion on existing and default properties. And then it will prompt
  9356. for a value, offering competion either on allowed values (via an inherited
  9357. xxx_ALL property) or on existing values in other instances of this property
  9358. in the current file."
  9359. (interactive
  9360. (let* ((prop (completing-read
  9361. "Property: " (mapcar 'list (org-buffer-property-keys nil t t))))
  9362. (cur (org-entry-get nil prop))
  9363. (allowed (org-property-get-allowed-values nil prop 'table))
  9364. (existing (mapcar 'list (org-property-values prop)))
  9365. (val (if allowed
  9366. (completing-read "Value: " allowed nil 'req-match)
  9367. (completing-read
  9368. (concat "Value" (if (and cur (string-match "\\S-" cur))
  9369. (concat "[" cur "]") "")
  9370. ": ")
  9371. existing nil nil "" nil cur))))
  9372. (list prop (if (equal val "") cur val))))
  9373. (unless (equal (org-entry-get nil property) value)
  9374. (org-entry-put nil property value)))
  9375. (defun org-delete-property (property)
  9376. "In the current entry, delete PROPERTY."
  9377. (interactive
  9378. (let* ((prop (completing-read
  9379. "Property: " (org-entry-properties nil 'standard))))
  9380. (list prop)))
  9381. (message "Property %s %s" property
  9382. (if (org-entry-delete nil property)
  9383. "deleted"
  9384. "was not present in the entry")))
  9385. (defun org-delete-property-globally (property)
  9386. "Remove PROPERTY globally, from all entries."
  9387. (interactive
  9388. (let* ((prop (completing-read
  9389. "Globally remove property: "
  9390. (mapcar 'list (org-buffer-property-keys)))))
  9391. (list prop)))
  9392. (save-excursion
  9393. (save-restriction
  9394. (widen)
  9395. (goto-char (point-min))
  9396. (let ((cnt 0))
  9397. (while (re-search-forward
  9398. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  9399. nil t)
  9400. (setq cnt (1+ cnt))
  9401. (replace-match ""))
  9402. (message "Property \"%s\" removed from %d entries" property cnt)))))
  9403. (defvar org-columns-current-fmt-compiled) ; defined below
  9404. (defun org-compute-property-at-point ()
  9405. "Compute the property at point.
  9406. This looks for an enclosing column format, extracts the operator and
  9407. then applies it to the proerty in the column format's scope."
  9408. (interactive)
  9409. (unless (org-at-property-p)
  9410. (error "Not at a property"))
  9411. (let ((prop (org-match-string-no-properties 2)))
  9412. (org-columns-get-format-and-top-level)
  9413. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  9414. (error "No operator defined for property %s" prop))
  9415. (org-columns-compute prop)))
  9416. (defun org-property-get-allowed-values (pom property &optional table)
  9417. "Get allowed values for the property PROPERTY.
  9418. When TABLE is non-nil, return an alist that can directly be used for
  9419. completion."
  9420. (let (vals)
  9421. (cond
  9422. ((equal property "TODO")
  9423. (setq vals (org-with-point-at pom
  9424. (append org-todo-keywords-1 '("")))))
  9425. ((equal property "PRIORITY")
  9426. (let ((n org-lowest-priority))
  9427. (while (>= n org-highest-priority)
  9428. (push (char-to-string n) vals)
  9429. (setq n (1- n)))))
  9430. ((member property org-special-properties))
  9431. (t
  9432. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  9433. (when (and vals (string-match "\\S-" vals))
  9434. (setq vals (car (read-from-string (concat "(" vals ")"))))
  9435. (setq vals (mapcar (lambda (x)
  9436. (cond ((stringp x) x)
  9437. ((numberp x) (number-to-string x))
  9438. ((symbolp x) (symbol-name x))
  9439. (t "???")))
  9440. vals)))))
  9441. (if table (mapcar 'list vals) vals)))
  9442. (defun org-property-previous-allowed-value (&optional previous)
  9443. "Switch to the next allowed value for this property."
  9444. (interactive)
  9445. (org-property-next-allowed-value t))
  9446. (defun org-property-next-allowed-value (&optional previous)
  9447. "Switch to the next allowed value for this property."
  9448. (interactive)
  9449. (unless (org-at-property-p)
  9450. (error "Not at a property"))
  9451. (let* ((key (match-string 2))
  9452. (value (match-string 3))
  9453. (allowed (or (org-property-get-allowed-values (point) key)
  9454. (and (member value '("[ ]" "[-]" "[X]"))
  9455. '("[ ]" "[X]"))))
  9456. nval)
  9457. (unless allowed
  9458. (error "Allowed values for this property have not been defined"))
  9459. (if previous (setq allowed (reverse allowed)))
  9460. (if (member value allowed)
  9461. (setq nval (car (cdr (member value allowed)))))
  9462. (setq nval (or nval (car allowed)))
  9463. (if (equal nval value)
  9464. (error "Only one allowed value for this property"))
  9465. (org-at-property-p)
  9466. (replace-match (concat " :" key ": " nval) t t)
  9467. (org-indent-line-function)
  9468. (beginning-of-line 1)
  9469. (skip-chars-forward " \t")))
  9470. (defun org-find-entry-with-id (ident)
  9471. "Locate the entry that contains the ID property with exact value IDENT.
  9472. IDENT can be a string, a symbol or a number, this function will search for
  9473. the string representation of it.
  9474. Return the position where this entry starts, or nil if there is no such entry."
  9475. (let ((id (cond
  9476. ((stringp ident) ident)
  9477. ((symbol-name ident) (symbol-name ident))
  9478. ((numberp ident) (number-to-string ident))
  9479. (t (error "IDENT %s must be a string, symbol or number" ident))))
  9480. (case-fold-search nil))
  9481. (save-excursion
  9482. (save-restriction
  9483. (widen)
  9484. (goto-char (point-min))
  9485. (when (re-search-forward
  9486. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  9487. nil t)
  9488. (org-back-to-heading)
  9489. (point))))))
  9490. ;;; Column View
  9491. (defvar org-columns-overlays nil
  9492. "Holds the list of current column overlays.")
  9493. (defvar org-columns-current-fmt nil
  9494. "Local variable, holds the currently active column format.")
  9495. (defvar org-columns-current-fmt-compiled nil
  9496. "Local variable, holds the currently active column format.
  9497. This is the compiled version of the format.")
  9498. (defvar org-columns-current-widths nil
  9499. "Loval variable, holds the currently widths of fields.")
  9500. (defvar org-columns-current-maxwidths nil
  9501. "Loval variable, holds the currently active maximum column widths.")
  9502. (defvar org-columns-begin-marker (make-marker)
  9503. "Points to the position where last a column creation command was called.")
  9504. (defvar org-columns-top-level-marker (make-marker)
  9505. "Points to the position where current columns region starts.")
  9506. (defvar org-columns-map (make-sparse-keymap)
  9507. "The keymap valid in column display.")
  9508. (defun org-columns-content ()
  9509. "Switch to contents view while in columns view."
  9510. (interactive)
  9511. (org-overview)
  9512. (org-content))
  9513. (org-defkey org-columns-map "c" 'org-columns-content)
  9514. (org-defkey org-columns-map "o" 'org-overview)
  9515. (org-defkey org-columns-map "e" 'org-columns-edit-value)
  9516. (org-defkey org-columns-map "\C-c\C-t" 'org-columns-todo)
  9517. (org-defkey org-columns-map "\C-c\C-c" 'org-columns-set-tags-or-toggle)
  9518. (org-defkey org-columns-map "\C-c\C-o" 'org-columns-open-link)
  9519. (org-defkey org-columns-map "v" 'org-columns-show-value)
  9520. (org-defkey org-columns-map "q" 'org-columns-quit)
  9521. (org-defkey org-columns-map "r" 'org-columns-redo)
  9522. (org-defkey org-columns-map "g" 'org-columns-redo)
  9523. (org-defkey org-columns-map [left] 'backward-char)
  9524. (org-defkey org-columns-map "\M-b" 'backward-char)
  9525. (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
  9526. (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
  9527. (org-defkey org-columns-map "\M-f" (lambda () (interactive) (goto-char (1+ (point)))))
  9528. (org-defkey org-columns-map [right] (lambda () (interactive) (goto-char (1+ (point)))))
  9529. (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
  9530. (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
  9531. (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
  9532. (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
  9533. (org-defkey org-columns-map "<" 'org-columns-narrow)
  9534. (org-defkey org-columns-map ">" 'org-columns-widen)
  9535. (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
  9536. (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
  9537. (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
  9538. (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
  9539. (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
  9540. '("Column"
  9541. ["Edit property" org-columns-edit-value t]
  9542. ["Next allowed value" org-columns-next-allowed-value t]
  9543. ["Previous allowed value" org-columns-previous-allowed-value t]
  9544. ["Show full value" org-columns-show-value t]
  9545. ["Edit allowed values" org-columns-edit-allowed t]
  9546. "--"
  9547. ["Edit column attributes" org-columns-edit-attributes t]
  9548. ["Increase column width" org-columns-widen t]
  9549. ["Decrease column width" org-columns-narrow t]
  9550. "--"
  9551. ["Move column right" org-columns-move-right t]
  9552. ["Move column left" org-columns-move-left t]
  9553. ["Add column" org-columns-new t]
  9554. ["Delete column" org-columns-delete t]
  9555. "--"
  9556. ["CONTENTS" org-columns-content t]
  9557. ["OVERVIEW" org-overview t]
  9558. ["Refresh columns display" org-columns-redo t]
  9559. "--"
  9560. ["Open link" org-columns-open-link t]
  9561. "--"
  9562. ["Quit" org-columns-quit t]))
  9563. (defun org-columns-new-overlay (beg end &optional string face)
  9564. "Create a new column overlay and add it to the list."
  9565. (let ((ov (org-make-overlay beg end)))
  9566. (org-overlay-put ov 'face (or face 'secondary-selection))
  9567. (org-overlay-display ov string face)
  9568. (push ov org-columns-overlays)
  9569. ov))
  9570. (defun org-columns-display-here (&optional props)
  9571. "Overlay the current line with column display."
  9572. (interactive)
  9573. (let* ((fmt org-columns-current-fmt-compiled)
  9574. (beg (point-at-bol))
  9575. (level-face (save-excursion
  9576. (beginning-of-line 1)
  9577. (and (looking-at "\\(\\**\\)\\(\\* \\)")
  9578. (org-get-level-face 2))))
  9579. (color (list :foreground
  9580. (face-attribute (or level-face 'default) :foreground)))
  9581. props pom property ass width f string ov column val modval)
  9582. ;; Check if the entry is in another buffer.
  9583. (unless props
  9584. (if (eq major-mode 'org-agenda-mode)
  9585. (setq pom (or (get-text-property (point) 'org-hd-marker)
  9586. (get-text-property (point) 'org-marker))
  9587. props (if pom (org-entry-properties pom) nil))
  9588. (setq props (org-entry-properties nil))))
  9589. ;; Walk the format
  9590. (while (setq column (pop fmt))
  9591. (setq property (car column)
  9592. ass (if (equal property "ITEM")
  9593. (cons "ITEM"
  9594. (save-match-data
  9595. (org-no-properties
  9596. (org-remove-tabs
  9597. (buffer-substring-no-properties
  9598. (point-at-bol) (point-at-eol))))))
  9599. (assoc property props))
  9600. width (or (cdr (assoc property org-columns-current-maxwidths))
  9601. (nth 2 column)
  9602. (length property))
  9603. f (format "%%-%d.%ds | " width width)
  9604. val (or (cdr ass) "")
  9605. modval (if (equal property "ITEM")
  9606. (org-columns-cleanup-item val org-columns-current-fmt-compiled))
  9607. string (format f (or modval val)))
  9608. ;; Create the overlay
  9609. (org-unmodified
  9610. (setq ov (org-columns-new-overlay
  9611. beg (setq beg (1+ beg)) string
  9612. (list color 'org-column)))
  9613. ;;; (list (get-text-property (point-at-bol) 'face) 'org-column)))
  9614. (org-overlay-put ov 'keymap org-columns-map)
  9615. (org-overlay-put ov 'org-columns-key property)
  9616. (org-overlay-put ov 'org-columns-value (cdr ass))
  9617. (org-overlay-put ov 'org-columns-value-modified modval)
  9618. (org-overlay-put ov 'org-columns-pom pom)
  9619. (org-overlay-put ov 'org-columns-format f))
  9620. (if (or (not (char-after beg))
  9621. (equal (char-after beg) ?\n))
  9622. (let ((inhibit-read-only t))
  9623. (save-excursion
  9624. (goto-char beg)
  9625. (org-unmodified (insert " ")))))) ;; FIXME: add props and remove later?
  9626. ;; Make the rest of the line disappear.
  9627. (org-unmodified
  9628. (setq ov (org-columns-new-overlay beg (point-at-eol)))
  9629. (org-overlay-put ov 'invisible t)
  9630. (org-overlay-put ov 'keymap org-columns-map)
  9631. (org-overlay-put ov 'intangible t)
  9632. (push ov org-columns-overlays)
  9633. (setq ov (org-make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
  9634. (org-overlay-put ov 'keymap org-columns-map)
  9635. (push ov org-columns-overlays)
  9636. (let ((inhibit-read-only t))
  9637. (put-text-property (max (point-min) (1- (point-at-bol)))
  9638. (min (point-max) (1+ (point-at-eol)))
  9639. 'read-only "Type `e' to edit property")))))
  9640. (defvar org-columns-full-header-line-format nil
  9641. "Fthe full header line format, will be shifted by horizontal scrolling." )
  9642. (defvar org-previous-header-line-format nil
  9643. "The header line format before column view was turned on.")
  9644. (defvar org-columns-inhibit-recalculation nil
  9645. "Inhibit recomputing of columns on column view startup.")
  9646. (defvar header-line-format)
  9647. (defvar org-columns-previous-hscroll 0)
  9648. (defun org-columns-display-here-title ()
  9649. "Overlay the newline before the current line with the table title."
  9650. (interactive)
  9651. (let ((fmt org-columns-current-fmt-compiled)
  9652. string (title "")
  9653. property width f column str widths)
  9654. (while (setq column (pop fmt))
  9655. (setq property (car column)
  9656. str (or (nth 1 column) property)
  9657. width (or (cdr (assoc property org-columns-current-maxwidths))
  9658. (nth 2 column)
  9659. (length str))
  9660. widths (push width widths)
  9661. f (format "%%-%d.%ds | " width width)
  9662. string (format f str)
  9663. title (concat title string)))
  9664. (setq title (concat
  9665. (org-add-props " " nil 'display '(space :align-to 0))
  9666. (org-add-props title nil 'face '(:weight bold :underline t :inherit default))))
  9667. (org-set-local 'org-previous-header-line-format header-line-format)
  9668. (org-set-local 'org-columns-current-widths (nreverse widths))
  9669. (setq org-columns-full-header-line-format title)
  9670. (setq org-columns-previous-hscroll -1)
  9671. ; (org-columns-hscoll-title)
  9672. (org-add-hook 'post-command-hook 'org-columns-hscoll-title nil 'local)))
  9673. (defun org-columns-hscoll-title ()
  9674. "Set the header-line-format so that it scrolls along with the table."
  9675. (sit-for .0001) ; need to force a redisplay to update window-hscroll
  9676. (when (not (= (window-hscroll) org-columns-previous-hscroll))
  9677. (setq header-line-format
  9678. (concat (substring org-columns-full-header-line-format 0 1)
  9679. (substring org-columns-full-header-line-format
  9680. (1+ (window-hscroll))))
  9681. org-columns-previous-hscroll (window-hscroll))
  9682. (force-mode-line-update)))
  9683. (defun org-columns-remove-overlays ()
  9684. "Remove all currently active column overlays."
  9685. (interactive)
  9686. (when (marker-buffer org-columns-begin-marker)
  9687. (with-current-buffer (marker-buffer org-columns-begin-marker)
  9688. (when (local-variable-p 'org-previous-header-line-format)
  9689. (setq header-line-format org-previous-header-line-format)
  9690. (kill-local-variable 'org-previous-header-line-format)
  9691. (remove-hook 'post-command-hook 'org-columns-hscoll-title 'local))
  9692. (move-marker org-columns-begin-marker nil)
  9693. (move-marker org-columns-top-level-marker nil)
  9694. (org-unmodified
  9695. (mapc 'org-delete-overlay org-columns-overlays)
  9696. (setq org-columns-overlays nil)
  9697. (let ((inhibit-read-only t))
  9698. (remove-text-properties (point-min) (point-max) '(read-only t)))))))
  9699. (defun org-columns-cleanup-item (item fmt)
  9700. "Remove from ITEM what is a column in the format FMT."
  9701. (if (not org-complex-heading-regexp)
  9702. item
  9703. (when (string-match org-complex-heading-regexp item)
  9704. (concat
  9705. (org-add-props (concat (match-string 1 item) " ") nil
  9706. 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
  9707. (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
  9708. (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
  9709. " " (match-string 4 item)
  9710. (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))))
  9711. (defun org-columns-show-value ()
  9712. "Show the full value of the property."
  9713. (interactive)
  9714. (let ((value (get-char-property (point) 'org-columns-value)))
  9715. (message "Value is: %s" (or value ""))))
  9716. (defun org-columns-quit ()
  9717. "Remove the column overlays and in this way exit column editing."
  9718. (interactive)
  9719. (org-unmodified
  9720. (org-columns-remove-overlays)
  9721. (let ((inhibit-read-only t))
  9722. (remove-text-properties (point-min) (point-max) '(read-only t))))
  9723. (when (eq major-mode 'org-agenda-mode)
  9724. (message
  9725. "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
  9726. (defun org-columns-check-computed ()
  9727. "Check if this column value is computed.
  9728. If yes, throw an error indicating that changing it does not make sense."
  9729. (let ((val (get-char-property (point) 'org-columns-value)))
  9730. (when (and (stringp val)
  9731. (get-char-property 0 'org-computed val))
  9732. (error "This value is computed from the entry's children"))))
  9733. (defun org-columns-todo (&optional arg)
  9734. "Change the TODO state during column view."
  9735. (interactive "P")
  9736. (org-columns-edit-value "TODO"))
  9737. (defun org-columns-set-tags-or-toggle (&optional arg)
  9738. "Toggle checkbox at point, or set tags for current headline."
  9739. (interactive "P")
  9740. (if (string-match "\\`\\[[ xX-]\\]\\'"
  9741. (get-char-property (point) 'org-columns-value))
  9742. (org-columns-next-allowed-value)
  9743. (org-columns-edit-value "TAGS")))
  9744. (defun org-columns-edit-value (&optional key)
  9745. "Edit the value of the property at point in column view.
  9746. Where possible, use the standard interface for changing this line."
  9747. (interactive)
  9748. (org-columns-check-computed)
  9749. (let* ((external-key key)
  9750. (col (current-column))
  9751. (key (or key (get-char-property (point) 'org-columns-key)))
  9752. (value (get-char-property (point) 'org-columns-value))
  9753. (bol (point-at-bol)) (eol (point-at-eol))
  9754. (pom (or (get-text-property bol 'org-hd-marker)
  9755. (point))) ; keep despite of compiler waring
  9756. (line-overlays
  9757. (delq nil (mapcar (lambda (x)
  9758. (and (eq (overlay-buffer x) (current-buffer))
  9759. (>= (overlay-start x) bol)
  9760. (<= (overlay-start x) eol)
  9761. x))
  9762. org-columns-overlays)))
  9763. nval eval allowed)
  9764. (cond
  9765. ((equal key "CLOCKSUM")
  9766. (error "This special column cannot be edited"))
  9767. ((equal key "ITEM")
  9768. (setq eval '(org-with-point-at pom
  9769. (org-edit-headline))))
  9770. ((equal key "TODO")
  9771. (setq eval '(org-with-point-at pom
  9772. (let ((current-prefix-arg
  9773. (if external-key current-prefix-arg '(4))))
  9774. (call-interactively 'org-todo)))))
  9775. ((equal key "PRIORITY")
  9776. (setq eval '(org-with-point-at pom
  9777. (call-interactively 'org-priority))))
  9778. ((equal key "TAGS")
  9779. (setq eval '(org-with-point-at pom
  9780. (let ((org-fast-tag-selection-single-key
  9781. (if (eq org-fast-tag-selection-single-key 'expert)
  9782. t org-fast-tag-selection-single-key)))
  9783. (call-interactively 'org-set-tags)))))
  9784. ((equal key "DEADLINE")
  9785. (setq eval '(org-with-point-at pom
  9786. (call-interactively 'org-deadline))))
  9787. ((equal key "SCHEDULED")
  9788. (setq eval '(org-with-point-at pom
  9789. (call-interactively 'org-schedule))))
  9790. (t
  9791. (setq allowed (org-property-get-allowed-values pom key 'table))
  9792. (if allowed
  9793. (setq nval (completing-read "Value: " allowed nil t))
  9794. (setq nval (read-string "Edit: " value)))
  9795. (setq nval (org-trim nval))
  9796. (when (not (equal nval value))
  9797. (setq eval '(org-entry-put pom key nval)))))
  9798. (when eval
  9799. (let ((inhibit-read-only t))
  9800. (remove-text-properties (max (point-min) (1- bol)) eol '(read-only t))
  9801. (unwind-protect
  9802. (progn
  9803. (setq org-columns-overlays
  9804. (org-delete-all line-overlays org-columns-overlays))
  9805. (mapc 'org-delete-overlay line-overlays)
  9806. (org-columns-eval eval))
  9807. (org-columns-display-here))))
  9808. (move-to-column col)
  9809. (if (and (org-mode-p)
  9810. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  9811. (org-columns-update key))))
  9812. (defun org-edit-headline () ; FIXME: this is not columns specific
  9813. "Edit the current headline, the part without TODO keyword, TAGS."
  9814. (org-back-to-heading)
  9815. (when (looking-at org-todo-line-regexp)
  9816. (let ((pre (buffer-substring (match-beginning 0) (match-beginning 3)))
  9817. (txt (match-string 3))
  9818. (post "")
  9819. txt2)
  9820. (if (string-match (org-re "[ \t]+:[[:alnum:]:_@]+:[ \t]*$") txt)
  9821. (setq post (match-string 0 txt)
  9822. txt (substring txt 0 (match-beginning 0))))
  9823. (setq txt2 (read-string "Edit: " txt))
  9824. (when (not (equal txt txt2))
  9825. (beginning-of-line 1)
  9826. (insert pre txt2 post)
  9827. (delete-region (point) (point-at-eol))
  9828. (org-set-tags nil t)))))
  9829. (defun org-columns-edit-allowed ()
  9830. "Edit the list of allowed values for the current property."
  9831. (interactive)
  9832. (let* ((key (get-char-property (point) 'org-columns-key))
  9833. (key1 (concat key "_ALL"))
  9834. (allowed (org-entry-get (point) key1 t))
  9835. nval)
  9836. ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
  9837. (setq nval (read-string "Allowed: " allowed))
  9838. (org-entry-put
  9839. (cond ((marker-position org-entry-property-inherited-from)
  9840. org-entry-property-inherited-from)
  9841. ((marker-position org-columns-top-level-marker)
  9842. org-columns-top-level-marker))
  9843. key1 nval)))
  9844. (defun org-columns-eval (form)
  9845. (let (hidep)
  9846. (save-excursion
  9847. (beginning-of-line 1)
  9848. ;; `next-line' is needed here, because it skips invisible line.
  9849. (condition-case nil (org-no-warnings (next-line 1)) (error nil))
  9850. (setq hidep (org-on-heading-p 1)))
  9851. (eval form)
  9852. (and hidep (hide-entry))))
  9853. (defun org-columns-previous-allowed-value ()
  9854. "Switch to the previous allowed value for this column."
  9855. (interactive)
  9856. (org-columns-next-allowed-value t))
  9857. (defun org-columns-next-allowed-value (&optional previous)
  9858. "Switch to the next allowed value for this column."
  9859. (interactive)
  9860. (org-columns-check-computed)
  9861. (let* ((col (current-column))
  9862. (key (get-char-property (point) 'org-columns-key))
  9863. (value (get-char-property (point) 'org-columns-value))
  9864. (bol (point-at-bol)) (eol (point-at-eol))
  9865. (pom (or (get-text-property bol 'org-hd-marker)
  9866. (point))) ; keep despite of compiler waring
  9867. (line-overlays
  9868. (delq nil (mapcar (lambda (x)
  9869. (and (eq (overlay-buffer x) (current-buffer))
  9870. (>= (overlay-start x) bol)
  9871. (<= (overlay-start x) eol)
  9872. x))
  9873. org-columns-overlays)))
  9874. (allowed (or (org-property-get-allowed-values pom key)
  9875. (and (memq
  9876. (nth 4 (assoc key org-columns-current-fmt-compiled))
  9877. '(checkbox checkbox-n-of-m checkbox-percent))
  9878. '("[ ]" "[X]"))))
  9879. nval)
  9880. (when (equal key "ITEM")
  9881. (error "Cannot edit item headline from here"))
  9882. (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
  9883. (error "Allowed values for this property have not been defined"))
  9884. (if (member key '("SCHEDULED" "DEADLINE"))
  9885. (setq nval (if previous 'earlier 'later))
  9886. (if previous (setq allowed (reverse allowed)))
  9887. (if (member value allowed)
  9888. (setq nval (car (cdr (member value allowed)))))
  9889. (setq nval (or nval (car allowed)))
  9890. (if (equal nval value)
  9891. (error "Only one allowed value for this property")))
  9892. (let ((inhibit-read-only t))
  9893. (remove-text-properties (1- bol) eol '(read-only t))
  9894. (unwind-protect
  9895. (progn
  9896. (setq org-columns-overlays
  9897. (org-delete-all line-overlays org-columns-overlays))
  9898. (mapc 'org-delete-overlay line-overlays)
  9899. (org-columns-eval '(org-entry-put pom key nval)))
  9900. (org-columns-display-here)))
  9901. (move-to-column col)
  9902. (if (and (org-mode-p)
  9903. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  9904. (org-columns-update key))))
  9905. (defun org-verify-version (task)
  9906. (cond
  9907. ((eq task 'columns)
  9908. (if (or (featurep 'xemacs)
  9909. (< emacs-major-version 22))
  9910. (error "Emacs 22 is required for the columns feature")))))
  9911. (defun org-columns-open-link (&optional arg)
  9912. (interactive "P")
  9913. (let ((value (get-char-property (point) 'org-columns-value)))
  9914. (org-open-link-from-string value arg)))
  9915. (defun org-open-link-from-string (s &optional arg)
  9916. "Open a link in the string S, as if it was in Org-mode."
  9917. (interactive)
  9918. (with-temp-buffer
  9919. (let ((org-inhibit-startup t))
  9920. (org-mode)
  9921. (insert s)
  9922. (goto-char (point-min))
  9923. (org-open-at-point arg))))
  9924. (defun org-columns-get-format-and-top-level ()
  9925. (let (fmt)
  9926. (when (condition-case nil (org-back-to-heading) (error nil))
  9927. (move-marker org-entry-property-inherited-from nil)
  9928. (setq fmt (org-entry-get nil "COLUMNS" t)))
  9929. (setq fmt (or fmt org-columns-default-format))
  9930. (org-set-local 'org-columns-current-fmt fmt)
  9931. (org-columns-compile-format fmt)
  9932. (if (marker-position org-entry-property-inherited-from)
  9933. (move-marker org-columns-top-level-marker
  9934. org-entry-property-inherited-from)
  9935. (move-marker org-columns-top-level-marker (point)))
  9936. fmt))
  9937. (defun org-columns ()
  9938. "Turn on column view on an org-mode file."
  9939. (interactive)
  9940. (org-verify-version 'columns)
  9941. (org-columns-remove-overlays)
  9942. (move-marker org-columns-begin-marker (point))
  9943. (let (beg end fmt cache maxwidths)
  9944. (setq fmt (org-columns-get-format-and-top-level))
  9945. (save-excursion
  9946. (goto-char org-columns-top-level-marker)
  9947. (setq beg (point))
  9948. (unless org-columns-inhibit-recalculation
  9949. (org-columns-compute-all))
  9950. (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
  9951. (point-max)))
  9952. ;; Get and cache the properties
  9953. (goto-char beg)
  9954. (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
  9955. (save-excursion
  9956. (save-restriction
  9957. (narrow-to-region beg end)
  9958. (org-clock-sum))))
  9959. (while (re-search-forward (concat "^" outline-regexp) end t)
  9960. (push (cons (org-current-line) (org-entry-properties)) cache))
  9961. (when cache
  9962. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  9963. (org-set-local 'org-columns-current-maxwidths maxwidths)
  9964. (org-columns-display-here-title)
  9965. (mapc (lambda (x)
  9966. (goto-line (car x))
  9967. (org-columns-display-here (cdr x)))
  9968. cache)))))
  9969. (defun org-columns-new (&optional prop title width op fmt &rest rest)
  9970. "Insert a new column, to the left of the current column."
  9971. (interactive)
  9972. (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
  9973. cell)
  9974. (setq prop (completing-read
  9975. "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
  9976. nil nil prop))
  9977. (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
  9978. (setq width (read-string "Column width: " (if width (number-to-string width))))
  9979. (if (string-match "\\S-" width)
  9980. (setq width (string-to-number width))
  9981. (setq width nil))
  9982. (setq fmt (completing-read "Summary [none]: "
  9983. '(("none") ("add_numbers") ("currency") ("add_times") ("checkbox") ("checkbox-n-of-m") ("checkbox-percent"))
  9984. nil t))
  9985. (if (string-match "\\S-" fmt)
  9986. (setq fmt (intern fmt))
  9987. (setq fmt nil))
  9988. (if (eq fmt 'none) (setq fmt nil))
  9989. (if editp
  9990. (progn
  9991. (setcar editp prop)
  9992. (setcdr editp (list title width nil fmt)))
  9993. (setq cell (nthcdr (1- (current-column))
  9994. org-columns-current-fmt-compiled))
  9995. (setcdr cell (cons (list prop title width nil fmt)
  9996. (cdr cell))))
  9997. (org-columns-store-format)
  9998. (org-columns-redo)))
  9999. (defun org-columns-delete ()
  10000. "Delete the column at point from columns view."
  10001. (interactive)
  10002. (let* ((n (current-column))
  10003. (title (nth 1 (nth n org-columns-current-fmt-compiled))))
  10004. (when (y-or-n-p
  10005. (format "Are you sure you want to remove column \"%s\"? " title))
  10006. (setq org-columns-current-fmt-compiled
  10007. (delq (nth n org-columns-current-fmt-compiled)
  10008. org-columns-current-fmt-compiled))
  10009. (org-columns-store-format)
  10010. (org-columns-redo)
  10011. (if (>= (current-column) (length org-columns-current-fmt-compiled))
  10012. (backward-char 1)))))
  10013. (defun org-columns-edit-attributes ()
  10014. "Edit the attributes of the current column."
  10015. (interactive)
  10016. (let* ((n (current-column))
  10017. (info (nth n org-columns-current-fmt-compiled)))
  10018. (apply 'org-columns-new info)))
  10019. (defun org-columns-widen (arg)
  10020. "Make the column wider by ARG characters."
  10021. (interactive "p")
  10022. (let* ((n (current-column))
  10023. (entry (nth n org-columns-current-fmt-compiled))
  10024. (width (or (nth 2 entry)
  10025. (cdr (assoc (car entry) org-columns-current-maxwidths)))))
  10026. (setq width (max 1 (+ width arg)))
  10027. (setcar (nthcdr 2 entry) width)
  10028. (org-columns-store-format)
  10029. (org-columns-redo)))
  10030. (defun org-columns-narrow (arg)
  10031. "Make the column nrrower by ARG characters."
  10032. (interactive "p")
  10033. (org-columns-widen (- arg)))
  10034. (defun org-columns-move-right ()
  10035. "Swap this column with the one to the right."
  10036. (interactive)
  10037. (let* ((n (current-column))
  10038. (cell (nthcdr n org-columns-current-fmt-compiled))
  10039. e)
  10040. (when (>= n (1- (length org-columns-current-fmt-compiled)))
  10041. (error "Cannot shift this column further to the right"))
  10042. (setq e (car cell))
  10043. (setcar cell (car (cdr cell)))
  10044. (setcdr cell (cons e (cdr (cdr cell))))
  10045. (org-columns-store-format)
  10046. (org-columns-redo)
  10047. (forward-char 1)))
  10048. (defun org-columns-move-left ()
  10049. "Swap this column with the one to the left."
  10050. (interactive)
  10051. (let* ((n (current-column)))
  10052. (when (= n 0)
  10053. (error "Cannot shift this column further to the left"))
  10054. (backward-char 1)
  10055. (org-columns-move-right)
  10056. (backward-char 1)))
  10057. (defun org-columns-store-format ()
  10058. "Store the text version of the current columns format in appropriate place.
  10059. This is either in the COLUMNS property of the node starting the current column
  10060. display, or in the #+COLUMNS line of the current buffer."
  10061. (let (fmt (cnt 0))
  10062. (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
  10063. (org-set-local 'org-columns-current-fmt fmt)
  10064. (if (marker-position org-columns-top-level-marker)
  10065. (save-excursion
  10066. (goto-char org-columns-top-level-marker)
  10067. (if (and (org-at-heading-p)
  10068. (org-entry-get nil "COLUMNS"))
  10069. (org-entry-put nil "COLUMNS" fmt)
  10070. (goto-char (point-min))
  10071. ;; Overwrite all #+COLUMNS lines....
  10072. (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
  10073. (setq cnt (1+ cnt))
  10074. (replace-match (concat "#+COLUMNS: " fmt) t t))
  10075. (unless (> cnt 0)
  10076. (goto-char (point-min))
  10077. (or (org-on-heading-p t) (outline-next-heading))
  10078. (let ((inhibit-read-only t))
  10079. (insert-before-markers "#+COLUMNS: " fmt "\n")))
  10080. (org-set-local 'org-columns-default-format fmt))))))
  10081. (defvar org-overriding-columns-format nil
  10082. "When set, overrides any other definition.")
  10083. (defvar org-agenda-view-columns-initially nil
  10084. "When set, switch to columns view immediately after creating the agenda.")
  10085. (defun org-agenda-columns ()
  10086. "Turn on column view in the agenda."
  10087. (interactive)
  10088. (org-verify-version 'columns)
  10089. (org-columns-remove-overlays)
  10090. (move-marker org-columns-begin-marker (point))
  10091. (let (fmt cache maxwidths m)
  10092. (cond
  10093. ((and (local-variable-p 'org-overriding-columns-format)
  10094. org-overriding-columns-format)
  10095. (setq fmt org-overriding-columns-format))
  10096. ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
  10097. (setq fmt (org-entry-get m "COLUMNS" t)))
  10098. ((and (boundp 'org-columns-current-fmt)
  10099. (local-variable-p 'org-columns-current-fmt)
  10100. org-columns-current-fmt)
  10101. (setq fmt org-columns-current-fmt))
  10102. ((setq m (next-single-property-change (point-min) 'org-hd-marker))
  10103. (setq m (get-text-property m 'org-hd-marker))
  10104. (setq fmt (org-entry-get m "COLUMNS" t))))
  10105. (setq fmt (or fmt org-columns-default-format))
  10106. (org-set-local 'org-columns-current-fmt fmt)
  10107. (org-columns-compile-format fmt)
  10108. (save-excursion
  10109. ;; Get and cache the properties
  10110. (goto-char (point-min))
  10111. (while (not (eobp))
  10112. (when (setq m (or (get-text-property (point) 'org-hd-marker)
  10113. (get-text-property (point) 'org-marker)))
  10114. (push (cons (org-current-line) (org-entry-properties m)) cache))
  10115. (beginning-of-line 2))
  10116. (when cache
  10117. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  10118. (org-set-local 'org-columns-current-maxwidths maxwidths)
  10119. (org-columns-display-here-title)
  10120. (mapc (lambda (x)
  10121. (goto-line (car x))
  10122. (org-columns-display-here (cdr x)))
  10123. cache)))))
  10124. (defun org-columns-get-autowidth-alist (s cache)
  10125. "Derive the maximum column widths from the format and the cache."
  10126. (let ((start 0) rtn)
  10127. (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
  10128. (push (cons (match-string 1 s) 1) rtn)
  10129. (setq start (match-end 0)))
  10130. (mapc (lambda (x)
  10131. (setcdr x (apply 'max
  10132. (mapcar
  10133. (lambda (y)
  10134. (length (or (cdr (assoc (car x) (cdr y))) " ")))
  10135. cache))))
  10136. rtn)
  10137. rtn))
  10138. (defun org-columns-compute-all ()
  10139. "Compute all columns that have operators defined."
  10140. (org-unmodified
  10141. (remove-text-properties (point-min) (point-max) '(org-summaries t)))
  10142. (let ((columns org-columns-current-fmt-compiled) col)
  10143. (while (setq col (pop columns))
  10144. (when (nth 3 col)
  10145. (save-excursion
  10146. (org-columns-compute (car col)))))))
  10147. (defun org-columns-update (property)
  10148. "Recompute PROPERTY, and update the columns display for it."
  10149. (org-columns-compute property)
  10150. (let (fmt val pos)
  10151. (save-excursion
  10152. (mapc (lambda (ov)
  10153. (when (equal (org-overlay-get ov 'org-columns-key) property)
  10154. (setq pos (org-overlay-start ov))
  10155. (goto-char pos)
  10156. (when (setq val (cdr (assoc property
  10157. (get-text-property
  10158. (point-at-bol) 'org-summaries))))
  10159. (setq fmt (org-overlay-get ov 'org-columns-format))
  10160. (org-overlay-put ov 'org-columns-value val)
  10161. (org-overlay-put ov 'display (format fmt val)))))
  10162. org-columns-overlays))))
  10163. (defun org-columns-compute (property)
  10164. "Sum the values of property PROPERTY hierarchically, for the entire buffer."
  10165. (interactive)
  10166. (let* ((re (concat "^" outline-regexp))
  10167. (lmax 30) ; Does anyone use deeper levels???
  10168. (lsum (make-vector lmax 0))
  10169. (lflag (make-vector lmax nil))
  10170. (level 0)
  10171. (ass (assoc property org-columns-current-fmt-compiled))
  10172. (format (nth 4 ass))
  10173. (printf (nth 5 ass))
  10174. (beg org-columns-top-level-marker)
  10175. last-level val valflag flag end sumpos sum-alist sum str str1 useval)
  10176. (save-excursion
  10177. ;; Find the region to compute
  10178. (goto-char beg)
  10179. (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
  10180. (goto-char end)
  10181. ;; Walk the tree from the back and do the computations
  10182. (while (re-search-backward re beg t)
  10183. (setq sumpos (match-beginning 0)
  10184. last-level level
  10185. level (org-outline-level)
  10186. val (org-entry-get nil property)
  10187. valflag (and val (string-match "\\S-" val)))
  10188. (cond
  10189. ((< level last-level)
  10190. ;; put the sum of lower levels here as a property
  10191. (setq sum (aref lsum last-level) ; current sum
  10192. flag (aref lflag last-level) ; any valid entries from children?
  10193. str (org-column-number-to-string sum format printf)
  10194. str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
  10195. useval (if flag str1 (if valflag val ""))
  10196. sum-alist (get-text-property sumpos 'org-summaries))
  10197. (if (assoc property sum-alist)
  10198. (setcdr (assoc property sum-alist) useval)
  10199. (push (cons property useval) sum-alist)
  10200. (org-unmodified
  10201. (add-text-properties sumpos (1+ sumpos)
  10202. (list 'org-summaries sum-alist))))
  10203. (when val
  10204. (org-entry-put nil property (if flag str val)))
  10205. ;; add current to current level accumulator
  10206. (when (or flag valflag)
  10207. (aset lsum level (+ (aref lsum level)
  10208. (if flag sum (org-column-string-to-number
  10209. (if flag str val) format))))
  10210. (aset lflag level t))
  10211. ;; clear accumulators for deeper levels
  10212. (loop for l from (1+ level) to (1- lmax) do
  10213. (aset lsum l 0)
  10214. (aset lflag l nil)))
  10215. ((>= level last-level)
  10216. ;; add what we have here to the accumulator for this level
  10217. (aset lsum level (+ (aref lsum level)
  10218. (org-column-string-to-number (or val "0") format)))
  10219. (and valflag (aset lflag level t)))
  10220. (t (error "This should not happen")))))))
  10221. (defun org-columns-redo ()
  10222. "Construct the column display again."
  10223. (interactive)
  10224. (message "Recomputing columns...")
  10225. (save-excursion
  10226. (if (marker-position org-columns-begin-marker)
  10227. (goto-char org-columns-begin-marker))
  10228. (org-columns-remove-overlays)
  10229. (if (org-mode-p)
  10230. (call-interactively 'org-columns)
  10231. (call-interactively 'org-agenda-columns)))
  10232. (message "Recomputing columns...done"))
  10233. (defun org-columns-not-in-agenda ()
  10234. (if (eq major-mode 'org-agenda-mode)
  10235. (error "This command is only allowed in Org-mode buffers")))
  10236. (defun org-string-to-number (s)
  10237. "Convert string to number, and interpret hh:mm:ss."
  10238. (if (not (string-match ":" s))
  10239. (string-to-number s)
  10240. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  10241. (while l
  10242. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  10243. sum)))
  10244. (defun org-column-number-to-string (n fmt &optional printf)
  10245. "Convert a computed column number to a string value, according to FMT."
  10246. (cond
  10247. ((eq fmt 'add_times)
  10248. (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
  10249. (format "%d:%02d" h m)))
  10250. ((eq fmt 'checkbox)
  10251. (cond ((= n (floor n)) "[X]")
  10252. ((> n 1.) "[-]")
  10253. (t "[ ]")))
  10254. ((memq fmt '(checkbox-n-of-m checkbox-percent))
  10255. (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
  10256. (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
  10257. (printf (format printf n))
  10258. ((eq fmt 'currency)
  10259. (format "%.2f" n))
  10260. (t (number-to-string n))))
  10261. (defun org-nofm-to-completion (n m &optional percent)
  10262. (if (not percent)
  10263. (format "[%d/%d]" n m)
  10264. (format "[%d%%]"(floor (+ 0.5 (* 100. (/ (* 1.0 n) m)))))))
  10265. (defun org-column-string-to-number (s fmt)
  10266. "Convert a column value to a number that can be used for column computing."
  10267. (cond
  10268. ((string-match ":" s)
  10269. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  10270. (while l
  10271. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  10272. sum))
  10273. ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
  10274. (if (equal s "[X]") 1. 0.000001))
  10275. (t (string-to-number s))))
  10276. (defun org-columns-uncompile-format (cfmt)
  10277. "Turn the compiled columns format back into a string representation."
  10278. (let ((rtn "") e s prop title op width fmt printf)
  10279. (while (setq e (pop cfmt))
  10280. (setq prop (car e)
  10281. title (nth 1 e)
  10282. width (nth 2 e)
  10283. op (nth 3 e)
  10284. fmt (nth 4 e)
  10285. printf (nth 5 e))
  10286. (cond
  10287. ((eq fmt 'add_times) (setq op ":"))
  10288. ((eq fmt 'checkbox) (setq op "X"))
  10289. ((eq fmt 'checkbox-n-of-m) (setq op "X/"))
  10290. ((eq fmt 'checkbox-percent) (setq op "X%"))
  10291. ((eq fmt 'add_numbers) (setq op "+"))
  10292. ((eq fmt 'currency) (setq op "$")))
  10293. (if (and op printf) (setq op (concat op ";" printf)))
  10294. (if (equal title prop) (setq title nil))
  10295. (setq s (concat "%" (if width (number-to-string width))
  10296. prop
  10297. (if title (concat "(" title ")"))
  10298. (if op (concat "{" op "}"))))
  10299. (setq rtn (concat rtn " " s)))
  10300. (org-trim rtn)))
  10301. (defun org-columns-compile-format (fmt)
  10302. "Turn a column format string into an alist of specifications.
  10303. The alist has one entry for each column in the format. The elements of
  10304. that list are:
  10305. property the property
  10306. title the title field for the columns
  10307. width the column width in characters, can be nil for automatic
  10308. operator the operator if any
  10309. format the output format for computed results, derived from operator
  10310. printf a printf format for computed values"
  10311. (let ((start 0) width prop title op f printf)
  10312. (setq org-columns-current-fmt-compiled nil)
  10313. (while (string-match
  10314. (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
  10315. fmt start)
  10316. (setq start (match-end 0)
  10317. width (match-string 1 fmt)
  10318. prop (match-string 2 fmt)
  10319. title (or (match-string 3 fmt) prop)
  10320. op (match-string 4 fmt)
  10321. f nil
  10322. printf nil)
  10323. (if width (setq width (string-to-number width)))
  10324. (when (and op (string-match ";" op))
  10325. (setq printf (substring op (match-end 0))
  10326. op (substring op 0 (match-beginning 0))))
  10327. (cond
  10328. ((equal op "+") (setq f 'add_numbers))
  10329. ((equal op "$") (setq f 'currency))
  10330. ((equal op ":") (setq f 'add_times))
  10331. ((equal op "X") (setq f 'checkbox))
  10332. ((equal op "X/") (setq f 'checkbox-n-of-m))
  10333. ((equal op "X%") (setq f 'checkbox-percent))
  10334. )
  10335. (push (list prop title width op f printf) org-columns-current-fmt-compiled))
  10336. (setq org-columns-current-fmt-compiled
  10337. (nreverse org-columns-current-fmt-compiled))))
  10338. ;;; Dynamic block for Column view
  10339. (defun org-columns-capture-view (&optional maxlevel skip-empty-rows)
  10340. "Get the column view of the current buffer or subtree.
  10341. The first optional argument MAXLEVEL sets the level limit. A
  10342. second optional argument SKIP-EMPTY-ROWS tells whether to skip
  10343. empty rows, an empty row being one where all the column view
  10344. specifiers except ITEM are empty. This function returns a list
  10345. containing the title row and all other rows. Each row is a list
  10346. of fields."
  10347. (save-excursion
  10348. (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
  10349. (n (length title)) row tbl)
  10350. (goto-char (point-min))
  10351. (while (and (re-search-forward "^\\(\\*+\\) " nil t)
  10352. (or (null maxlevel)
  10353. (>= maxlevel
  10354. (if org-odd-levels-only
  10355. (/ (1+ (length (match-string 1))) 2)
  10356. (length (match-string 1))))))
  10357. (when (get-char-property (match-beginning 0) 'org-columns-key)
  10358. (setq row nil)
  10359. (loop for i from 0 to (1- n) do
  10360. (push (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
  10361. (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
  10362. "")
  10363. row))
  10364. (setq row (nreverse row))
  10365. (unless (and skip-empty-rows
  10366. (eq 1 (length (delete "" (delete-dups row)))))
  10367. (push row tbl))))
  10368. (append (list title 'hline) (nreverse tbl)))))
  10369. (defun org-dblock-write:columnview (params)
  10370. "Write the column view table.
  10371. PARAMS is a property list of parameters:
  10372. :width enforce same column widths with <N> specifiers.
  10373. :id the :ID: property of the entry where the columns view
  10374. should be built, as a string. When `local', call locally.
  10375. When `global' call column view with the cursor at the beginning
  10376. of the buffer (usually this means that the whole buffer switches
  10377. to column view).
  10378. :hlines When t, insert a hline before each item. When a number, insert
  10379. a hline before each level <= that number.
  10380. :vlines When t, make each column a colgroup to enforce vertical lines.
  10381. :maxlevel When set to a number, don't capture headlines below this level.
  10382. :skip-empty-rows
  10383. When t, skip rows where all specifiers other than ITEM are empty."
  10384. (let ((pos (move-marker (make-marker) (point)))
  10385. (hlines (plist-get params :hlines))
  10386. (vlines (plist-get params :vlines))
  10387. (maxlevel (plist-get params :maxlevel))
  10388. (skip-empty-rows (plist-get params :skip-empty-rows))
  10389. tbl id idpos nfields tmp)
  10390. (save-excursion
  10391. (save-restriction
  10392. (when (setq id (plist-get params :id))
  10393. (cond ((not id) nil)
  10394. ((eq id 'global) (goto-char (point-min)))
  10395. ((eq id 'local) nil)
  10396. ((setq idpos (org-find-entry-with-id id))
  10397. (goto-char idpos))
  10398. (t (error "Cannot find entry with :ID: %s" id))))
  10399. (org-columns)
  10400. (setq tbl (org-columns-capture-view maxlevel skip-empty-rows))
  10401. (setq nfields (length (car tbl)))
  10402. (org-columns-quit)))
  10403. (goto-char pos)
  10404. (move-marker pos nil)
  10405. (when tbl
  10406. (when (plist-get params :hlines)
  10407. (setq tmp nil)
  10408. (while tbl
  10409. (if (eq (car tbl) 'hline)
  10410. (push (pop tbl) tmp)
  10411. (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
  10412. (if (and (not (eq (car tmp) 'hline))
  10413. (or (eq hlines t)
  10414. (and (numberp hlines) (<= (- (match-end 1) (match-beginning 1)) hlines))))
  10415. (push 'hline tmp)))
  10416. (push (pop tbl) tmp)))
  10417. (setq tbl (nreverse tmp)))
  10418. (when vlines
  10419. (setq tbl (mapcar (lambda (x)
  10420. (if (eq 'hline x) x (cons "" x)))
  10421. tbl))
  10422. (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
  10423. (setq pos (point))
  10424. (insert (org-listtable-to-string tbl))
  10425. (when (plist-get params :width)
  10426. (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
  10427. org-columns-current-widths "|")))
  10428. (goto-char pos)
  10429. (org-table-align))))
  10430. (defun org-listtable-to-string (tbl)
  10431. "Convert a listtable TBL to a string that contains the Org-mode table.
  10432. The table still need to be alligned. The resulting string has no leading
  10433. and tailing newline characters."
  10434. (mapconcat
  10435. (lambda (x)
  10436. (cond
  10437. ((listp x)
  10438. (concat "|" (mapconcat 'identity x "|") "|"))
  10439. ((eq x 'hline) "|-|")
  10440. (t (error "Garbage in listtable: %s" x))))
  10441. tbl "\n"))
  10442. (defun org-insert-columns-dblock ()
  10443. "Create a dynamic block capturing a column view table."
  10444. (interactive)
  10445. (let ((defaults '(:name "columnview" :hlines 1))
  10446. (id (completing-read
  10447. "Capture columns (local, global, entry with :ID: property) [local]: "
  10448. (append '(("global") ("local"))
  10449. (mapcar 'list (org-property-values "ID"))))))
  10450. (if (equal id "") (setq id 'local))
  10451. (if (equal id "global") (setq id 'global))
  10452. (setq defaults (append defaults (list :id id)))
  10453. (org-create-dblock defaults)
  10454. (org-update-dblock)))
  10455. ;;;; Timestamps
  10456. (defvar org-last-changed-timestamp nil)
  10457. (defvar org-time-was-given) ; dynamically scoped parameter
  10458. (defvar org-end-time-was-given) ; dynamically scoped parameter
  10459. (defvar org-ts-what) ; dynamically scoped parameter
  10460. (defun org-time-stamp (arg)
  10461. "Prompt for a date/time and insert a time stamp.
  10462. If the user specifies a time like HH:MM, or if this command is called
  10463. with a prefix argument, the time stamp will contain date and time.
  10464. Otherwise, only the date will be included. All parts of a date not
  10465. specified by the user will be filled in from the current date/time.
  10466. So if you press just return without typing anything, the time stamp
  10467. will represent the current date/time. If there is already a timestamp
  10468. at the cursor, it will be modified."
  10469. (interactive "P")
  10470. (let* ((ts nil)
  10471. (default-time
  10472. ;; Default time is either today, or, when entering a range,
  10473. ;; the range start.
  10474. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  10475. (save-excursion
  10476. (re-search-backward
  10477. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  10478. (- (point) 20) t)))
  10479. (apply 'encode-time (org-parse-time-string (match-string 1)))
  10480. (current-time)))
  10481. (default-input (and ts (org-get-compact-tod ts)))
  10482. org-time-was-given org-end-time-was-given time)
  10483. (cond
  10484. ((and (org-at-timestamp-p)
  10485. (eq last-command 'org-time-stamp)
  10486. (eq this-command 'org-time-stamp))
  10487. (insert "--")
  10488. (setq time (let ((this-command this-command))
  10489. (org-read-date arg 'totime nil nil default-time default-input)))
  10490. (org-insert-time-stamp time (or org-time-was-given arg)))
  10491. ((org-at-timestamp-p)
  10492. (setq time (let ((this-command this-command))
  10493. (org-read-date arg 'totime nil nil default-time default-input)))
  10494. (when (org-at-timestamp-p) ; just to get the match data
  10495. (replace-match "")
  10496. (setq org-last-changed-timestamp
  10497. (org-insert-time-stamp
  10498. time (or org-time-was-given arg)
  10499. nil nil nil (list org-end-time-was-given))))
  10500. (message "Timestamp updated"))
  10501. (t
  10502. (setq time (let ((this-command this-command))
  10503. (org-read-date arg 'totime nil nil default-time default-input)))
  10504. (org-insert-time-stamp time (or org-time-was-given arg)
  10505. nil nil nil (list org-end-time-was-given))))))
  10506. ;; FIXME: can we use this for something else????
  10507. ;; like computing time differences?????
  10508. (defun org-get-compact-tod (s)
  10509. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  10510. (let* ((t1 (match-string 1 s))
  10511. (h1 (string-to-number (match-string 2 s)))
  10512. (m1 (string-to-number (match-string 3 s)))
  10513. (t2 (and (match-end 4) (match-string 5 s)))
  10514. (h2 (and t2 (string-to-number (match-string 6 s))))
  10515. (m2 (and t2 (string-to-number (match-string 7 s))))
  10516. dh dm)
  10517. (if (not t2)
  10518. t1
  10519. (setq dh (- h2 h1) dm (- m2 m1))
  10520. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  10521. (concat t1 "+" (number-to-string dh)
  10522. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  10523. (defun org-time-stamp-inactive (&optional arg)
  10524. "Insert an inactive time stamp.
  10525. An inactive time stamp is enclosed in square brackets instead of angle
  10526. brackets. It is inactive in the sense that it does not trigger agenda entries,
  10527. does not link to the calendar and cannot be changed with the S-cursor keys.
  10528. So these are more for recording a certain time/date."
  10529. (interactive "P")
  10530. (let (org-time-was-given org-end-time-was-given time)
  10531. (setq time (org-read-date arg 'totime))
  10532. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  10533. nil nil (list org-end-time-was-given))))
  10534. (defvar org-date-ovl (org-make-overlay 1 1))
  10535. (org-overlay-put org-date-ovl 'face 'org-warning)
  10536. (org-detach-overlay org-date-ovl)
  10537. (defvar org-ans1) ; dynamically scoped parameter
  10538. (defvar org-ans2) ; dynamically scoped parameter
  10539. (defvar org-plain-time-of-day-regexp) ; defined below
  10540. (defvar org-read-date-overlay nil)
  10541. (defvar org-dcst nil) ; dynamically scoped
  10542. (defun org-read-date (&optional with-time to-time from-string prompt
  10543. default-time default-input)
  10544. "Read a date, possibly a time, and make things smooth for the user.
  10545. The prompt will suggest to enter an ISO date, but you can also enter anything
  10546. which will at least partially be understood by `parse-time-string'.
  10547. Unrecognized parts of the date will default to the current day, month, year,
  10548. hour and minute. If this command is called to replace a timestamp at point,
  10549. of to enter the second timestamp of a range, the default time is taken from the
  10550. existing stamp. For example,
  10551. 3-2-5 --> 2003-02-05
  10552. feb 15 --> currentyear-02-15
  10553. sep 12 9 --> 2009-09-12
  10554. 12:45 --> today 12:45
  10555. 22 sept 0:34 --> currentyear-09-22 0:34
  10556. 12 --> currentyear-currentmonth-12
  10557. Fri --> nearest Friday (today or later)
  10558. etc.
  10559. Furthermore you can specify a relative date by giving, as the *first* thing
  10560. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  10561. change in days weeks, months, years.
  10562. With a single plus or minus, the date is relative to today. With a double
  10563. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  10564. +4d --> four days from today
  10565. +4 --> same as above
  10566. +2w --> two weeks from today
  10567. ++5 --> five days from default date
  10568. The function understands only English month and weekday abbreviations,
  10569. but this can be configured with the variables `parse-time-months' and
  10570. `parse-time-weekdays'.
  10571. While prompting, a calendar is popped up - you can also select the
  10572. date with the mouse (button 1). The calendar shows a period of three
  10573. months. To scroll it to other months, use the keys `>' and `<'.
  10574. If you don't like the calendar, turn it off with
  10575. \(setq org-read-date-popup-calendar nil)
  10576. With optional argument TO-TIME, the date will immediately be converted
  10577. to an internal time.
  10578. With an optional argument WITH-TIME, the prompt will suggest to also
  10579. insert a time. Note that when WITH-TIME is not set, you can still
  10580. enter a time, and this function will inform the calling routine about
  10581. this change. The calling routine may then choose to change the format
  10582. used to insert the time stamp into the buffer to include the time.
  10583. With optional argument FROM-STRING, read from this string instead from
  10584. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  10585. the time/date that is used for everything that is not specified by the
  10586. user."
  10587. (require 'parse-time)
  10588. (let* ((org-time-stamp-rounding-minutes
  10589. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  10590. (org-dcst org-display-custom-times)
  10591. (ct (org-current-time))
  10592. (def (or default-time ct))
  10593. (defdecode (decode-time def))
  10594. (dummy (progn
  10595. (when (< (nth 2 defdecode) org-extend-today-until)
  10596. (setcar (nthcdr 2 defdecode) -1)
  10597. (setcar (nthcdr 1 defdecode) 59)
  10598. (setq def (apply 'encode-time defdecode)
  10599. defdecode (decode-time def)))))
  10600. (calendar-move-hook nil)
  10601. (view-diary-entries-initially nil)
  10602. (view-calendar-holidays-initially nil)
  10603. (timestr (format-time-string
  10604. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  10605. (prompt (concat (if prompt (concat prompt " ") "")
  10606. (format "Date+time [%s]: " timestr)))
  10607. ans (org-ans0 "") org-ans1 org-ans2 final)
  10608. (cond
  10609. (from-string (setq ans from-string))
  10610. (org-read-date-popup-calendar
  10611. (save-excursion
  10612. (save-window-excursion
  10613. (calendar)
  10614. (calendar-forward-day (- (time-to-days def)
  10615. (calendar-absolute-from-gregorian
  10616. (calendar-current-date))))
  10617. (org-eval-in-calendar nil t)
  10618. (let* ((old-map (current-local-map))
  10619. (map (copy-keymap calendar-mode-map))
  10620. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  10621. (org-defkey map (kbd "RET") 'org-calendar-select)
  10622. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  10623. 'org-calendar-select-mouse)
  10624. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  10625. 'org-calendar-select-mouse)
  10626. (org-defkey minibuffer-local-map [(meta shift left)]
  10627. (lambda () (interactive)
  10628. (org-eval-in-calendar '(calendar-backward-month 1))))
  10629. (org-defkey minibuffer-local-map [(meta shift right)]
  10630. (lambda () (interactive)
  10631. (org-eval-in-calendar '(calendar-forward-month 1))))
  10632. (org-defkey minibuffer-local-map [(meta shift up)]
  10633. (lambda () (interactive)
  10634. (org-eval-in-calendar '(calendar-backward-year 1))))
  10635. (org-defkey minibuffer-local-map [(meta shift down)]
  10636. (lambda () (interactive)
  10637. (org-eval-in-calendar '(calendar-forward-year 1))))
  10638. (org-defkey minibuffer-local-map [(shift up)]
  10639. (lambda () (interactive)
  10640. (org-eval-in-calendar '(calendar-backward-week 1))))
  10641. (org-defkey minibuffer-local-map [(shift down)]
  10642. (lambda () (interactive)
  10643. (org-eval-in-calendar '(calendar-forward-week 1))))
  10644. (org-defkey minibuffer-local-map [(shift left)]
  10645. (lambda () (interactive)
  10646. (org-eval-in-calendar '(calendar-backward-day 1))))
  10647. (org-defkey minibuffer-local-map [(shift right)]
  10648. (lambda () (interactive)
  10649. (org-eval-in-calendar '(calendar-forward-day 1))))
  10650. (org-defkey minibuffer-local-map ">"
  10651. (lambda () (interactive)
  10652. (org-eval-in-calendar '(scroll-calendar-left 1))))
  10653. (org-defkey minibuffer-local-map "<"
  10654. (lambda () (interactive)
  10655. (org-eval-in-calendar '(scroll-calendar-right 1))))
  10656. (unwind-protect
  10657. (progn
  10658. (use-local-map map)
  10659. (add-hook 'post-command-hook 'org-read-date-display)
  10660. (setq org-ans0 (read-string prompt default-input nil nil))
  10661. ;; org-ans0: from prompt
  10662. ;; org-ans1: from mouse click
  10663. ;; org-ans2: from calendar motion
  10664. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  10665. (remove-hook 'post-command-hook 'org-read-date-display)
  10666. (use-local-map old-map)
  10667. (when org-read-date-overlay
  10668. (org-delete-overlay org-read-date-overlay)
  10669. (setq org-read-date-overlay nil)))))))
  10670. (t ; Naked prompt only
  10671. (unwind-protect
  10672. (setq ans (read-string prompt default-input nil timestr))
  10673. (when org-read-date-overlay
  10674. (org-delete-overlay org-read-date-overlay)
  10675. (setq org-read-date-overlay nil)))))
  10676. (setq final (org-read-date-analyze ans def defdecode))
  10677. (if to-time
  10678. (apply 'encode-time final)
  10679. (if (and (boundp 'org-time-was-given) org-time-was-given)
  10680. (format "%04d-%02d-%02d %02d:%02d"
  10681. (nth 5 final) (nth 4 final) (nth 3 final)
  10682. (nth 2 final) (nth 1 final))
  10683. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  10684. (defvar def)
  10685. (defvar defdecode)
  10686. (defvar with-time)
  10687. (defun org-read-date-display ()
  10688. "Display the currrent date prompt interpretation in the minibuffer."
  10689. (when org-read-date-display-live
  10690. (when org-read-date-overlay
  10691. (org-delete-overlay org-read-date-overlay))
  10692. (let ((p (point)))
  10693. (end-of-line 1)
  10694. (while (not (equal (buffer-substring
  10695. (max (point-min) (- (point) 4)) (point))
  10696. " "))
  10697. (insert " "))
  10698. (goto-char p))
  10699. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  10700. " " (or org-ans1 org-ans2)))
  10701. (org-end-time-was-given nil)
  10702. (f (org-read-date-analyze ans def defdecode))
  10703. (fmts (if org-dcst
  10704. org-time-stamp-custom-formats
  10705. org-time-stamp-formats))
  10706. (fmt (if (or with-time
  10707. (and (boundp 'org-time-was-given) org-time-was-given))
  10708. (cdr fmts)
  10709. (car fmts)))
  10710. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  10711. (when (and org-end-time-was-given
  10712. (string-match org-plain-time-of-day-regexp txt))
  10713. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  10714. org-end-time-was-given
  10715. (substring txt (match-end 0)))))
  10716. (setq org-read-date-overlay
  10717. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  10718. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  10719. (defun org-read-date-analyze (ans def defdecode)
  10720. "Analyze the combined answer of the date prompt."
  10721. ;; FIXME: cleanup and comment
  10722. (let (delta deltan deltaw deltadef year month day
  10723. hour minute second wday pm h2 m2 tl wday1
  10724. iso-year iso-weekday iso-week iso-year iso-date)
  10725. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  10726. (setq ans (replace-match "" t t ans)
  10727. deltan (car delta)
  10728. deltaw (nth 1 delta)
  10729. deltadef (nth 2 delta)))
  10730. ;; Check if there is an iso week date in there
  10731. ;; If yes, sore the info and ostpone interpreting it until the rest
  10732. ;; of the parsing is done
  10733. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  10734. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  10735. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  10736. iso-week (string-to-number (match-string 2 ans)))
  10737. (setq ans (replace-match "" t t ans)))
  10738. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  10739. (when (string-match
  10740. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  10741. (setq year (if (match-end 2)
  10742. (string-to-number (match-string 2 ans))
  10743. (string-to-number (format-time-string "%Y")))
  10744. month (string-to-number (match-string 3 ans))
  10745. day (string-to-number (match-string 4 ans)))
  10746. (if (< year 100) (setq year (+ 2000 year)))
  10747. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  10748. t nil ans)))
  10749. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  10750. ;; If there is a time with am/pm, and *no* time without it, we convert
  10751. ;; so that matching will be successful.
  10752. (loop for i from 1 to 2 do ; twice, for end time as well
  10753. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  10754. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  10755. (setq hour (string-to-number (match-string 1 ans))
  10756. minute (if (match-end 3)
  10757. (string-to-number (match-string 3 ans))
  10758. 0)
  10759. pm (equal ?p
  10760. (string-to-char (downcase (match-string 4 ans)))))
  10761. (if (and (= hour 12) (not pm))
  10762. (setq hour 0)
  10763. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  10764. (setq ans (replace-match (format "%02d:%02d" hour minute)
  10765. t t ans))))
  10766. ;; Check if a time range is given as a duration
  10767. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  10768. (setq hour (string-to-number (match-string 1 ans))
  10769. h2 (+ hour (string-to-number (match-string 3 ans)))
  10770. minute (string-to-number (match-string 2 ans))
  10771. m2 (+ minute (if (match-end 5) (string-to-number
  10772. (match-string 5 ans))0)))
  10773. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  10774. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  10775. t t ans)))
  10776. ;; Check if there is a time range
  10777. (when (boundp 'org-end-time-was-given)
  10778. (setq org-time-was-given nil)
  10779. (when (and (string-match org-plain-time-of-day-regexp ans)
  10780. (match-end 8))
  10781. (setq org-end-time-was-given (match-string 8 ans))
  10782. (setq ans (concat (substring ans 0 (match-beginning 7))
  10783. (substring ans (match-end 7))))))
  10784. (setq tl (parse-time-string ans)
  10785. day (or (nth 3 tl) (nth 3 defdecode))
  10786. month (or (nth 4 tl)
  10787. (if (and org-read-date-prefer-future
  10788. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  10789. (1+ (nth 4 defdecode))
  10790. (nth 4 defdecode)))
  10791. year (or (nth 5 tl)
  10792. (if (and org-read-date-prefer-future
  10793. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  10794. (1+ (nth 5 defdecode))
  10795. (nth 5 defdecode)))
  10796. hour (or (nth 2 tl) (nth 2 defdecode))
  10797. minute (or (nth 1 tl) (nth 1 defdecode))
  10798. second (or (nth 0 tl) 0)
  10799. wday (nth 6 tl))
  10800. ;; Special date definitions below
  10801. (cond
  10802. (iso-week
  10803. ;; There was an iso week
  10804. (setq year (or iso-year year)
  10805. day (or iso-weekday wday 1)
  10806. wday nil ; to make sure that the trigger below does not match
  10807. iso-date (calendar-gregorian-from-absolute
  10808. (calendar-absolute-from-iso
  10809. (list iso-week day year))))
  10810. ; FIXME: Should we also push ISO weeks into the future?
  10811. ; (when (and org-read-date-prefer-future
  10812. ; (not iso-year)
  10813. ; (< (calendar-absolute-from-gregorian iso-date)
  10814. ; (time-to-days (current-time))))
  10815. ; (setq year (1+ year)
  10816. ; iso-date (calendar-gregorian-from-absolute
  10817. ; (calendar-absolute-from-iso
  10818. ; (list iso-week day year)))))
  10819. (setq month (car iso-date)
  10820. year (nth 2 iso-date)
  10821. day (nth 1 iso-date)))
  10822. (deltan
  10823. (unless deltadef
  10824. (let ((now (decode-time (current-time))))
  10825. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  10826. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  10827. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  10828. ((equal deltaw "m") (setq month (+ month deltan)))
  10829. ((equal deltaw "y") (setq year (+ year deltan)))))
  10830. ((and wday (not (nth 3 tl)))
  10831. ;; Weekday was given, but no day, so pick that day in the week
  10832. ;; on or after the derived date.
  10833. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  10834. (unless (equal wday wday1)
  10835. (setq day (+ day (% (- wday wday1 -7) 7))))))
  10836. (if (and (boundp 'org-time-was-given)
  10837. (nth 2 tl))
  10838. (setq org-time-was-given t))
  10839. (if (< year 100) (setq year (+ 2000 year)))
  10840. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  10841. (list second minute hour day month year)))
  10842. (defvar parse-time-weekdays)
  10843. (defun org-read-date-get-relative (s today default)
  10844. "Check string S for special relative date string.
  10845. TODAY and DEFAULT are internal times, for today and for a default.
  10846. Return shift list (N what def-flag)
  10847. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  10848. N is the number of WHATs to shift.
  10849. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  10850. the DEFAULT date rather than TODAY."
  10851. (when (string-match
  10852. (concat
  10853. "\\`[ \t]*\\([-+]\\{1,2\\}\\)"
  10854. "\\([0-9]+\\)?"
  10855. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  10856. "\\([ \t]\\|$\\)") s)
  10857. (let* ((dir (if (match-end 1)
  10858. (string-to-char (substring (match-string 1 s) -1))
  10859. ?+))
  10860. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  10861. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  10862. (what (if (match-end 3) (match-string 3 s) "d"))
  10863. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  10864. (date (if rel default today))
  10865. (wday (nth 6 (decode-time date)))
  10866. delta)
  10867. (if wday1
  10868. (progn
  10869. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  10870. (if (= dir ?-) (setq delta (- delta 7)))
  10871. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  10872. (list delta "d" rel))
  10873. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  10874. (defun org-eval-in-calendar (form &optional keepdate)
  10875. "Eval FORM in the calendar window and return to current window.
  10876. Also, store the cursor date in variable org-ans2."
  10877. (let ((sw (selected-window)))
  10878. (select-window (get-buffer-window "*Calendar*"))
  10879. (eval form)
  10880. (when (and (not keepdate) (calendar-cursor-to-date))
  10881. (let* ((date (calendar-cursor-to-date))
  10882. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10883. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  10884. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  10885. (select-window sw)))
  10886. ; ;; Update the prompt to show new default date
  10887. ; (save-excursion
  10888. ; (goto-char (point-min))
  10889. ; (when (and org-ans2
  10890. ; (re-search-forward "\\[[-0-9]+\\]" nil t)
  10891. ; (get-text-property (match-end 0) 'field))
  10892. ; (let ((inhibit-read-only t))
  10893. ; (replace-match (concat "[" org-ans2 "]") t t)
  10894. ; (add-text-properties (point-min) (1+ (match-end 0))
  10895. ; (text-properties-at (1+ (point-min)))))))))
  10896. (defun org-calendar-select ()
  10897. "Return to `org-read-date' with the date currently selected.
  10898. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10899. (interactive)
  10900. (when (calendar-cursor-to-date)
  10901. (let* ((date (calendar-cursor-to-date))
  10902. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10903. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10904. (if (active-minibuffer-window) (exit-minibuffer))))
  10905. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  10906. "Insert a date stamp for the date given by the internal TIME.
  10907. WITH-HM means, use the stamp format that includes the time of the day.
  10908. INACTIVE means use square brackets instead of angular ones, so that the
  10909. stamp will not contribute to the agenda.
  10910. PRE and POST are optional strings to be inserted before and after the
  10911. stamp.
  10912. The command returns the inserted time stamp."
  10913. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  10914. stamp)
  10915. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  10916. (insert-before-markers (or pre ""))
  10917. (insert-before-markers (setq stamp (format-time-string fmt time)))
  10918. (when (listp extra)
  10919. (setq extra (car extra))
  10920. (if (and (stringp extra)
  10921. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  10922. (setq extra (format "-%02d:%02d"
  10923. (string-to-number (match-string 1 extra))
  10924. (string-to-number (match-string 2 extra))))
  10925. (setq extra nil)))
  10926. (when extra
  10927. (backward-char 1)
  10928. (insert-before-markers extra)
  10929. (forward-char 1))
  10930. (insert-before-markers (or post ""))
  10931. stamp))
  10932. (defun org-toggle-time-stamp-overlays ()
  10933. "Toggle the use of custom time stamp formats."
  10934. (interactive)
  10935. (setq org-display-custom-times (not org-display-custom-times))
  10936. (unless org-display-custom-times
  10937. (let ((p (point-min)) (bmp (buffer-modified-p)))
  10938. (while (setq p (next-single-property-change p 'display))
  10939. (if (and (get-text-property p 'display)
  10940. (eq (get-text-property p 'face) 'org-date))
  10941. (remove-text-properties
  10942. p (setq p (next-single-property-change p 'display))
  10943. '(display t))))
  10944. (set-buffer-modified-p bmp)))
  10945. (if (featurep 'xemacs)
  10946. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  10947. (org-restart-font-lock)
  10948. (setq org-table-may-need-update t)
  10949. (if org-display-custom-times
  10950. (message "Time stamps are overlayed with custom format")
  10951. (message "Time stamp overlays removed")))
  10952. (defun org-display-custom-time (beg end)
  10953. "Overlay modified time stamp format over timestamp between BED and END."
  10954. (let* ((ts (buffer-substring beg end))
  10955. t1 w1 with-hm tf time str w2 (off 0))
  10956. (save-match-data
  10957. (setq t1 (org-parse-time-string ts t))
  10958. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  10959. (setq off (- (match-end 0) (match-beginning 0)))))
  10960. (setq end (- end off))
  10961. (setq w1 (- end beg)
  10962. with-hm (and (nth 1 t1) (nth 2 t1))
  10963. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  10964. time (org-fix-decoded-time t1)
  10965. str (org-add-props
  10966. (format-time-string
  10967. (substring tf 1 -1) (apply 'encode-time time))
  10968. nil 'mouse-face 'highlight)
  10969. w2 (length str))
  10970. (if (not (= w2 w1))
  10971. (add-text-properties (1+ beg) (+ 2 beg)
  10972. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  10973. (if (featurep 'xemacs)
  10974. (progn
  10975. (put-text-property beg end 'invisible t)
  10976. (put-text-property beg end 'end-glyph (make-glyph str)))
  10977. (put-text-property beg end 'display str))))
  10978. (defun org-translate-time (string)
  10979. "Translate all timestamps in STRING to custom format.
  10980. But do this only if the variable `org-display-custom-times' is set."
  10981. (when org-display-custom-times
  10982. (save-match-data
  10983. (let* ((start 0)
  10984. (re org-ts-regexp-both)
  10985. t1 with-hm inactive tf time str beg end)
  10986. (while (setq start (string-match re string start))
  10987. (setq beg (match-beginning 0)
  10988. end (match-end 0)
  10989. t1 (save-match-data
  10990. (org-parse-time-string (substring string beg end) t))
  10991. with-hm (and (nth 1 t1) (nth 2 t1))
  10992. inactive (equal (substring string beg (1+ beg)) "[")
  10993. tf (funcall (if with-hm 'cdr 'car)
  10994. org-time-stamp-custom-formats)
  10995. time (org-fix-decoded-time t1)
  10996. str (format-time-string
  10997. (concat
  10998. (if inactive "[" "<") (substring tf 1 -1)
  10999. (if inactive "]" ">"))
  11000. (apply 'encode-time time))
  11001. string (replace-match str t t string)
  11002. start (+ start (length str)))))))
  11003. string)
  11004. (defun org-fix-decoded-time (time)
  11005. "Set 0 instead of nil for the first 6 elements of time.
  11006. Don't touch the rest."
  11007. (let ((n 0))
  11008. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  11009. (defun org-days-to-time (timestamp-string)
  11010. "Difference between TIMESTAMP-STRING and now in days."
  11011. (- (time-to-days (org-time-string-to-time timestamp-string))
  11012. (time-to-days (current-time))))
  11013. (defun org-deadline-close (timestamp-string &optional ndays)
  11014. "Is the time in TIMESTAMP-STRING close to the current date?"
  11015. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  11016. (and (< (org-days-to-time timestamp-string) ndays)
  11017. (not (org-entry-is-done-p))))
  11018. (defun org-get-wdays (ts)
  11019. "Get the deadline lead time appropriate for timestring TS."
  11020. (cond
  11021. ((<= org-deadline-warning-days 0)
  11022. ;; 0 or negative, enforce this value no matter what
  11023. (- org-deadline-warning-days))
  11024. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  11025. ;; lead time is specified.
  11026. (floor (* (string-to-number (match-string 1 ts))
  11027. (cdr (assoc (match-string 2 ts)
  11028. '(("d" . 1) ("w" . 7)
  11029. ("m" . 30.4) ("y" . 365.25)))))))
  11030. ;; go for the default.
  11031. (t org-deadline-warning-days)))
  11032. (defun org-calendar-select-mouse (ev)
  11033. "Return to `org-read-date' with the date currently selected.
  11034. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11035. (interactive "e")
  11036. (mouse-set-point ev)
  11037. (when (calendar-cursor-to-date)
  11038. (let* ((date (calendar-cursor-to-date))
  11039. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11040. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11041. (if (active-minibuffer-window) (exit-minibuffer))))
  11042. (defun org-check-deadlines (ndays)
  11043. "Check if there are any deadlines due or past due.
  11044. A deadline is considered due if it happens within `org-deadline-warning-days'
  11045. days from today's date. If the deadline appears in an entry marked DONE,
  11046. it is not shown. The prefix arg NDAYS can be used to test that many
  11047. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  11048. (interactive "P")
  11049. (let* ((org-warn-days
  11050. (cond
  11051. ((equal ndays '(4)) 100000)
  11052. (ndays (prefix-numeric-value ndays))
  11053. (t (abs org-deadline-warning-days))))
  11054. (case-fold-search nil)
  11055. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  11056. (callback
  11057. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  11058. (message "%d deadlines past-due or due within %d days"
  11059. (org-occur regexp nil callback)
  11060. org-warn-days)))
  11061. (defun org-check-before-date (date)
  11062. "Check if there are deadlines or scheduled entries before DATE."
  11063. (interactive (list (org-read-date)))
  11064. (let ((case-fold-search nil)
  11065. (regexp (concat "\\<\\(" org-deadline-string
  11066. "\\|" org-scheduled-string
  11067. "\\) *<\\([^>]+\\)>"))
  11068. (callback
  11069. (lambda () (time-less-p
  11070. (org-time-string-to-time (match-string 2))
  11071. (org-time-string-to-time date)))))
  11072. (message "%d entries before %s"
  11073. (org-occur regexp nil callback) date)))
  11074. (defun org-evaluate-time-range (&optional to-buffer)
  11075. "Evaluate a time range by computing the difference between start and end.
  11076. Normally the result is just printed in the echo area, but with prefix arg
  11077. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  11078. If the time range is actually in a table, the result is inserted into the
  11079. next column.
  11080. For time difference computation, a year is assumed to be exactly 365
  11081. days in order to avoid rounding problems."
  11082. (interactive "P")
  11083. (or
  11084. (org-clock-update-time-maybe)
  11085. (save-excursion
  11086. (unless (org-at-date-range-p t)
  11087. (goto-char (point-at-bol))
  11088. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  11089. (if (not (org-at-date-range-p t))
  11090. (error "Not at a time-stamp range, and none found in current line")))
  11091. (let* ((ts1 (match-string 1))
  11092. (ts2 (match-string 2))
  11093. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  11094. (match-end (match-end 0))
  11095. (time1 (org-time-string-to-time ts1))
  11096. (time2 (org-time-string-to-time ts2))
  11097. (t1 (time-to-seconds time1))
  11098. (t2 (time-to-seconds time2))
  11099. (diff (abs (- t2 t1)))
  11100. (negative (< (- t2 t1) 0))
  11101. ;; (ys (floor (* 365 24 60 60)))
  11102. (ds (* 24 60 60))
  11103. (hs (* 60 60))
  11104. (fy "%dy %dd %02d:%02d")
  11105. (fy1 "%dy %dd")
  11106. (fd "%dd %02d:%02d")
  11107. (fd1 "%dd")
  11108. (fh "%02d:%02d")
  11109. y d h m align)
  11110. (if havetime
  11111. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11112. y 0
  11113. d (floor (/ diff ds)) diff (mod diff ds)
  11114. h (floor (/ diff hs)) diff (mod diff hs)
  11115. m (floor (/ diff 60)))
  11116. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11117. y 0
  11118. d (floor (+ (/ diff ds) 0.5))
  11119. h 0 m 0))
  11120. (if (not to-buffer)
  11121. (message "%s" (org-make-tdiff-string y d h m))
  11122. (if (org-at-table-p)
  11123. (progn
  11124. (goto-char match-end)
  11125. (setq align t)
  11126. (and (looking-at " *|") (goto-char (match-end 0))))
  11127. (goto-char match-end))
  11128. (if (looking-at
  11129. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  11130. (replace-match ""))
  11131. (if negative (insert " -"))
  11132. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  11133. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  11134. (insert " " (format fh h m))))
  11135. (if align (org-table-align))
  11136. (message "Time difference inserted")))))
  11137. (defun org-make-tdiff-string (y d h m)
  11138. (let ((fmt "")
  11139. (l nil))
  11140. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  11141. l (push y l)))
  11142. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  11143. l (push d l)))
  11144. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  11145. l (push h l)))
  11146. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  11147. l (push m l)))
  11148. (apply 'format fmt (nreverse l))))
  11149. (defun org-time-string-to-time (s)
  11150. (apply 'encode-time (org-parse-time-string s)))
  11151. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  11152. "Convert a time stamp to an absolute day number.
  11153. If there is a specifyer for a cyclic time stamp, get the closest date to
  11154. DAYNR.
  11155. PREFER and SHOW_ALL are passed through to `org-closest-date'."
  11156. (cond
  11157. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  11158. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  11159. daynr
  11160. (+ daynr 1000)))
  11161. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  11162. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  11163. (time-to-days (current-time))) (match-string 0 s)
  11164. prefer show-all))
  11165. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  11166. (defun org-time-from-absolute (d)
  11167. "Return the time corresponding to date D.
  11168. D may be an absolute day number, or a calendar-type list (month day year)."
  11169. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  11170. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  11171. (defun org-calendar-holiday ()
  11172. "List of holidays, for Diary display in Org-mode."
  11173. (require 'holidays)
  11174. (let ((hl (funcall
  11175. (if (fboundp 'calendar-check-holidays)
  11176. 'calendar-check-holidays 'check-calendar-holidays) date)))
  11177. (if hl (mapconcat 'identity hl "; "))))
  11178. (defun org-diary-sexp-entry (sexp entry date)
  11179. "Process a SEXP diary ENTRY for DATE."
  11180. (require 'diary-lib)
  11181. (let ((result (if calendar-debug-sexp
  11182. (let ((stack-trace-on-error t))
  11183. (eval (car (read-from-string sexp))))
  11184. (condition-case nil
  11185. (eval (car (read-from-string sexp)))
  11186. (error
  11187. (beep)
  11188. (message "Bad sexp at line %d in %s: %s"
  11189. (org-current-line)
  11190. (buffer-file-name) sexp)
  11191. (sleep-for 2))))))
  11192. (cond ((stringp result) result)
  11193. ((and (consp result)
  11194. (stringp (cdr result))) (cdr result))
  11195. (result entry)
  11196. (t nil))))
  11197. (defun org-diary-to-ical-string (frombuf)
  11198. "Get iCalendar entries from diary entries in buffer FROMBUF.
  11199. This uses the icalendar.el library."
  11200. (let* ((tmpdir (if (featurep 'xemacs)
  11201. (temp-directory)
  11202. temporary-file-directory))
  11203. (tmpfile (make-temp-name
  11204. (expand-file-name "orgics" tmpdir)))
  11205. buf rtn b e)
  11206. (save-excursion
  11207. (set-buffer frombuf)
  11208. (icalendar-export-region (point-min) (point-max) tmpfile)
  11209. (setq buf (find-buffer-visiting tmpfile))
  11210. (set-buffer buf)
  11211. (goto-char (point-min))
  11212. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  11213. (setq b (match-beginning 0)))
  11214. (goto-char (point-max))
  11215. (if (re-search-backward "^END:VEVENT" nil t)
  11216. (setq e (match-end 0)))
  11217. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  11218. (kill-buffer buf)
  11219. (kill-buffer frombuf)
  11220. (delete-file tmpfile)
  11221. rtn))
  11222. (defun org-closest-date (start current change prefer show-all)
  11223. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  11224. When PREFER is `past' return a date that is either CURRENT or past.
  11225. When PREFER is `future', return a date that is either CURRENT or future.
  11226. When SHOW-ALL is nil, only return the current occurence of a time stamp."
  11227. ;; Make the proper lists from the dates
  11228. (catch 'exit
  11229. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  11230. dn dw sday cday n1 n2
  11231. d m y y1 y2 date1 date2 nmonths nm ny m2)
  11232. (setq start (org-date-to-gregorian start)
  11233. current (org-date-to-gregorian
  11234. (if show-all
  11235. current
  11236. (time-to-days (current-time))))
  11237. sday (calendar-absolute-from-gregorian start)
  11238. cday (calendar-absolute-from-gregorian current))
  11239. (if (<= cday sday) (throw 'exit sday))
  11240. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  11241. (setq dn (string-to-number (match-string 1 change))
  11242. dw (cdr (assoc (match-string 2 change) a1)))
  11243. (error "Invalid change specifyer: %s" change))
  11244. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  11245. (cond
  11246. ((eq dw 'day)
  11247. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  11248. n2 (+ n1 dn)))
  11249. ((eq dw 'year)
  11250. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  11251. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  11252. (setq date1 (list m d y1)
  11253. n1 (calendar-absolute-from-gregorian date1)
  11254. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  11255. n2 (calendar-absolute-from-gregorian date2)))
  11256. ((eq dw 'month)
  11257. ;; approx number of month between the tow dates
  11258. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  11259. ;; How often does dn fit in there?
  11260. (setq d (nth 1 start) m (car start) y (nth 2 start)
  11261. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  11262. m (+ m nm)
  11263. ny (floor (/ m 12))
  11264. y (+ y ny)
  11265. m (- m (* ny 12)))
  11266. (while (> m 12) (setq m (- m 12) y (1+ y)))
  11267. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  11268. (setq m2 (+ m dn) y2 y)
  11269. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11270. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  11271. (while (< n2 cday)
  11272. (setq n1 n2 m m2 y y2)
  11273. (setq m2 (+ m dn) y2 y)
  11274. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11275. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  11276. (if show-all
  11277. (cond
  11278. ((eq prefer 'past) n1)
  11279. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11280. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  11281. (cond
  11282. ((eq prefer 'past) n1)
  11283. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11284. (t (if (= cday n1) n1 n2)))))))
  11285. (defun org-date-to-gregorian (date)
  11286. "Turn any specification of DATE into a gregorian date for the calendar."
  11287. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  11288. ((and (listp date) (= (length date) 3)) date)
  11289. ((stringp date)
  11290. (setq date (org-parse-time-string date))
  11291. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  11292. ((listp date)
  11293. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  11294. (defun org-parse-time-string (s &optional nodefault)
  11295. "Parse the standard Org-mode time string.
  11296. This should be a lot faster than the normal `parse-time-string'.
  11297. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  11298. hour and minute fields will be nil if not given."
  11299. (if (string-match org-ts-regexp0 s)
  11300. (list 0
  11301. (if (or (match-beginning 8) (not nodefault))
  11302. (string-to-number (or (match-string 8 s) "0")))
  11303. (if (or (match-beginning 7) (not nodefault))
  11304. (string-to-number (or (match-string 7 s) "0")))
  11305. (string-to-number (match-string 4 s))
  11306. (string-to-number (match-string 3 s))
  11307. (string-to-number (match-string 2 s))
  11308. nil nil nil)
  11309. (make-list 9 0)))
  11310. (defun org-timestamp-up (&optional arg)
  11311. "Increase the date item at the cursor by one.
  11312. If the cursor is on the year, change the year. If it is on the month or
  11313. the day, change that.
  11314. With prefix ARG, change by that many units."
  11315. (interactive "p")
  11316. (org-timestamp-change (prefix-numeric-value arg)))
  11317. (defun org-timestamp-down (&optional arg)
  11318. "Decrease the date item at the cursor by one.
  11319. If the cursor is on the year, change the year. If it is on the month or
  11320. the day, change that.
  11321. With prefix ARG, change by that many units."
  11322. (interactive "p")
  11323. (org-timestamp-change (- (prefix-numeric-value arg))))
  11324. (defun org-timestamp-up-day (&optional arg)
  11325. "Increase the date in the time stamp by one day.
  11326. With prefix ARG, change that many days."
  11327. (interactive "p")
  11328. (if (and (not (org-at-timestamp-p t))
  11329. (org-on-heading-p))
  11330. (org-todo 'up)
  11331. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  11332. (defun org-timestamp-down-day (&optional arg)
  11333. "Decrease the date in the time stamp by one day.
  11334. With prefix ARG, change that many days."
  11335. (interactive "p")
  11336. (if (and (not (org-at-timestamp-p t))
  11337. (org-on-heading-p))
  11338. (org-todo 'down)
  11339. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  11340. (defun org-at-timestamp-p (&optional inactive-ok)
  11341. "Determine if the cursor is in or at a timestamp."
  11342. (interactive)
  11343. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  11344. (pos (point))
  11345. (ans (or (looking-at tsr)
  11346. (save-excursion
  11347. (skip-chars-backward "^[<\n\r\t")
  11348. (if (> (point) (point-min)) (backward-char 1))
  11349. (and (looking-at tsr)
  11350. (> (- (match-end 0) pos) -1))))))
  11351. (and ans
  11352. (boundp 'org-ts-what)
  11353. (setq org-ts-what
  11354. (cond
  11355. ((= pos (match-beginning 0)) 'bracket)
  11356. ((= pos (1- (match-end 0))) 'bracket)
  11357. ((org-pos-in-match-range pos 2) 'year)
  11358. ((org-pos-in-match-range pos 3) 'month)
  11359. ((org-pos-in-match-range pos 7) 'hour)
  11360. ((org-pos-in-match-range pos 8) 'minute)
  11361. ((or (org-pos-in-match-range pos 4)
  11362. (org-pos-in-match-range pos 5)) 'day)
  11363. ((and (> pos (or (match-end 8) (match-end 5)))
  11364. (< pos (match-end 0)))
  11365. (- pos (or (match-end 8) (match-end 5))))
  11366. (t 'day))))
  11367. ans))
  11368. (defun org-toggle-timestamp-type ()
  11369. "Toggle the type (<active> or [inactive]) of a time stamp."
  11370. (interactive)
  11371. (when (org-at-timestamp-p t)
  11372. (save-excursion
  11373. (goto-char (match-beginning 0))
  11374. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  11375. (goto-char (1- (match-end 0)))
  11376. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  11377. (message "Timestamp is now %sactive"
  11378. (if (equal (char-before) ?>) "in" ""))))
  11379. (defun org-timestamp-change (n &optional what)
  11380. "Change the date in the time stamp at point.
  11381. The date will be changed by N times WHAT. WHAT can be `day', `month',
  11382. `year', `minute', `second'. If WHAT is not given, the cursor position
  11383. in the timestamp determines what will be changed."
  11384. (let ((pos (point))
  11385. with-hm inactive
  11386. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  11387. org-ts-what
  11388. extra rem
  11389. ts time time0)
  11390. (if (not (org-at-timestamp-p t))
  11391. (error "Not at a timestamp"))
  11392. (if (and (not what) (eq org-ts-what 'bracket))
  11393. (org-toggle-timestamp-type)
  11394. (if (and (not what) (not (eq org-ts-what 'day))
  11395. org-display-custom-times
  11396. (get-text-property (point) 'display)
  11397. (not (get-text-property (1- (point)) 'display)))
  11398. (setq org-ts-what 'day))
  11399. (setq org-ts-what (or what org-ts-what)
  11400. inactive (= (char-after (match-beginning 0)) ?\[)
  11401. ts (match-string 0))
  11402. (replace-match "")
  11403. (if (string-match
  11404. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  11405. ts)
  11406. (setq extra (match-string 1 ts)))
  11407. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  11408. (setq with-hm t))
  11409. (setq time0 (org-parse-time-string ts))
  11410. (when (and (eq org-ts-what 'minute)
  11411. (eq current-prefix-arg nil))
  11412. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  11413. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  11414. (setcar (cdr time0) (+ (nth 1 time0)
  11415. (if (> n 0) (- rem) (- dm rem))))))
  11416. (setq time
  11417. (encode-time (or (car time0) 0)
  11418. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  11419. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  11420. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  11421. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  11422. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  11423. (nthcdr 6 time0)))
  11424. (when (integerp org-ts-what)
  11425. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  11426. (if (eq what 'calendar)
  11427. (let ((cal-date (org-get-date-from-calendar)))
  11428. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  11429. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  11430. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  11431. (setcar time0 (or (car time0) 0))
  11432. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  11433. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  11434. (setq time (apply 'encode-time time0))))
  11435. (setq org-last-changed-timestamp
  11436. (org-insert-time-stamp time with-hm inactive nil nil extra))
  11437. (org-clock-update-time-maybe)
  11438. (goto-char pos)
  11439. ;; Try to recenter the calendar window, if any
  11440. (if (and org-calendar-follow-timestamp-change
  11441. (get-buffer-window "*Calendar*" t)
  11442. (memq org-ts-what '(day month year)))
  11443. (org-recenter-calendar (time-to-days time))))))
  11444. ;; FIXME: does not yet work for lead times
  11445. (defun org-modify-ts-extra (s pos n dm)
  11446. "Change the different parts of the lead-time and repeat fields in timestamp."
  11447. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  11448. ng h m new rem)
  11449. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  11450. (cond
  11451. ((or (org-pos-in-match-range pos 2)
  11452. (org-pos-in-match-range pos 3))
  11453. (setq m (string-to-number (match-string 3 s))
  11454. h (string-to-number (match-string 2 s)))
  11455. (if (org-pos-in-match-range pos 2)
  11456. (setq h (+ h n))
  11457. (setq n (* dm (org-no-warnings (signum n))))
  11458. (when (not (= 0 (setq rem (% m dm))))
  11459. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  11460. (setq m (+ m n)))
  11461. (if (< m 0) (setq m (+ m 60) h (1- h)))
  11462. (if (> m 59) (setq m (- m 60) h (1+ h)))
  11463. (setq h (min 24 (max 0 h)))
  11464. (setq ng 1 new (format "-%02d:%02d" h m)))
  11465. ((org-pos-in-match-range pos 6)
  11466. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  11467. ((org-pos-in-match-range pos 5)
  11468. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  11469. ((org-pos-in-match-range pos 9)
  11470. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  11471. ((org-pos-in-match-range pos 8)
  11472. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  11473. (when ng
  11474. (setq s (concat
  11475. (substring s 0 (match-beginning ng))
  11476. new
  11477. (substring s (match-end ng))))))
  11478. s))
  11479. (defun org-recenter-calendar (date)
  11480. "If the calendar is visible, recenter it to DATE."
  11481. (let* ((win (selected-window))
  11482. (cwin (get-buffer-window "*Calendar*" t))
  11483. (calendar-move-hook nil))
  11484. (when cwin
  11485. (select-window cwin)
  11486. (calendar-goto-date (if (listp date) date
  11487. (calendar-gregorian-from-absolute date)))
  11488. (select-window win))))
  11489. (defun org-goto-calendar (&optional arg)
  11490. "Go to the Emacs calendar at the current date.
  11491. If there is a time stamp in the current line, go to that date.
  11492. A prefix ARG can be used to force the current date."
  11493. (interactive "P")
  11494. (let ((tsr org-ts-regexp) diff
  11495. (calendar-move-hook nil)
  11496. (view-calendar-holidays-initially nil)
  11497. (view-diary-entries-initially nil))
  11498. (if (or (org-at-timestamp-p)
  11499. (save-excursion
  11500. (beginning-of-line 1)
  11501. (looking-at (concat ".*" tsr))))
  11502. (let ((d1 (time-to-days (current-time)))
  11503. (d2 (time-to-days
  11504. (org-time-string-to-time (match-string 1)))))
  11505. (setq diff (- d2 d1))))
  11506. (calendar)
  11507. (calendar-goto-today)
  11508. (if (and diff (not arg)) (calendar-forward-day diff))))
  11509. (defun org-get-date-from-calendar ()
  11510. "Return a list (month day year) of date at point in calendar."
  11511. (with-current-buffer "*Calendar*"
  11512. (save-match-data
  11513. (calendar-cursor-to-date))))
  11514. (defun org-date-from-calendar ()
  11515. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  11516. If there is already a time stamp at the cursor position, update it."
  11517. (interactive)
  11518. (if (org-at-timestamp-p t)
  11519. (org-timestamp-change 0 'calendar)
  11520. (let ((cal-date (org-get-date-from-calendar)))
  11521. (org-insert-time-stamp
  11522. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  11523. ;;; The clock for measuring work time.
  11524. (defvar org-mode-line-string "")
  11525. (put 'org-mode-line-string 'risky-local-variable t)
  11526. (defvar org-mode-line-timer nil)
  11527. (defvar org-clock-heading "")
  11528. (defvar org-clock-start-time "")
  11529. (defun org-update-mode-line ()
  11530. (let* ((delta (- (time-to-seconds (current-time))
  11531. (time-to-seconds org-clock-start-time)))
  11532. (h (floor delta 3600))
  11533. (m (floor (- delta (* 3600 h)) 60)))
  11534. (setq org-mode-line-string
  11535. (propertize (format "-[%d:%02d (%s)]" h m org-clock-heading)
  11536. 'help-echo "Org-mode clock is running"))
  11537. (force-mode-line-update)))
  11538. (defvar org-clock-marker (make-marker)
  11539. "Marker recording the last clock-in.")
  11540. (defvar org-clock-mode-line-entry nil
  11541. "Information for the modeline about the running clock.")
  11542. (defun org-clock-in ()
  11543. "Start the clock on the current item.
  11544. If necessary, clock-out of the currently active clock."
  11545. (interactive)
  11546. (org-clock-out t)
  11547. (let (ts)
  11548. (save-excursion
  11549. (org-back-to-heading t)
  11550. (when (and org-clock-in-switch-to-state
  11551. (not (looking-at (concat outline-regexp "[ \t]*"
  11552. org-clock-in-switch-to-state
  11553. "\\>"))))
  11554. (org-todo org-clock-in-switch-to-state))
  11555. (if (and org-clock-heading-function
  11556. (functionp org-clock-heading-function))
  11557. (setq org-clock-heading (funcall org-clock-heading-function))
  11558. (if (looking-at org-complex-heading-regexp)
  11559. (setq org-clock-heading (match-string 4))
  11560. (setq org-clock-heading "???")))
  11561. (setq org-clock-heading (propertize org-clock-heading 'face nil))
  11562. (org-clock-find-position)
  11563. (insert "\n") (backward-char 1)
  11564. (indent-relative)
  11565. (insert org-clock-string " ")
  11566. (setq org-clock-start-time (current-time))
  11567. (setq ts (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  11568. (move-marker org-clock-marker (point) (buffer-base-buffer))
  11569. (or global-mode-string (setq global-mode-string '("")))
  11570. (or (memq 'org-mode-line-string global-mode-string)
  11571. (setq global-mode-string
  11572. (append global-mode-string '(org-mode-line-string))))
  11573. (org-update-mode-line)
  11574. (setq org-mode-line-timer (run-with-timer 60 60 'org-update-mode-line))
  11575. (message "Clock started at %s" ts))))
  11576. (defun org-clock-find-position ()
  11577. "Find the location where the next clock line should be inserted."
  11578. (org-back-to-heading t)
  11579. (catch 'exit
  11580. (let ((beg (save-excursion
  11581. (beginning-of-line 2)
  11582. (or (bolp) (newline))
  11583. (point)))
  11584. (end (progn (outline-next-heading) (point)))
  11585. (re (concat "^[ \t]*" org-clock-string))
  11586. (cnt 0)
  11587. first last)
  11588. (goto-char beg)
  11589. (when (eobp) (newline) (setq end (max (point) end)))
  11590. (when (re-search-forward "^[ \t]*:CLOCK:" end t)
  11591. ;; we seem to have a CLOCK drawer, so go there.
  11592. (beginning-of-line 2)
  11593. (throw 'exit t))
  11594. ;; Lets count the CLOCK lines
  11595. (goto-char beg)
  11596. (while (re-search-forward re end t)
  11597. (setq first (or first (match-beginning 0))
  11598. last (match-beginning 0)
  11599. cnt (1+ cnt)))
  11600. (when (and (integerp org-clock-into-drawer)
  11601. (>= (1+ cnt) org-clock-into-drawer))
  11602. ;; Wrap current entries into a new drawer
  11603. (goto-char last)
  11604. (beginning-of-line 2)
  11605. (if (org-at-item-p) (org-end-of-item))
  11606. (insert ":END:\n")
  11607. (beginning-of-line 0)
  11608. (org-indent-line-function)
  11609. (goto-char first)
  11610. (insert ":CLOCK:\n")
  11611. (beginning-of-line 0)
  11612. (org-indent-line-function)
  11613. (org-flag-drawer t)
  11614. (beginning-of-line 2)
  11615. (throw 'exit nil))
  11616. (goto-char beg)
  11617. (while (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  11618. (not (equal (match-string 1) org-clock-string)))
  11619. ;; Planning info, skip to after it
  11620. (beginning-of-line 2)
  11621. (or (bolp) (newline)))
  11622. (when (eq t org-clock-into-drawer)
  11623. (insert ":CLOCK:\n:END:\n")
  11624. (beginning-of-line -1)
  11625. (org-indent-line-function)
  11626. (org-flag-drawer t)
  11627. (beginning-of-line 2)
  11628. (org-indent-line-function)))))
  11629. (defun org-clock-out (&optional fail-quietly)
  11630. "Stop the currently running clock.
  11631. If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
  11632. (interactive)
  11633. (catch 'exit
  11634. (if (not (marker-buffer org-clock-marker))
  11635. (if fail-quietly (throw 'exit t) (error "No active clock")))
  11636. (let (ts te s h m)
  11637. (save-excursion
  11638. (set-buffer (marker-buffer org-clock-marker))
  11639. (goto-char org-clock-marker)
  11640. (beginning-of-line 1)
  11641. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  11642. (equal (match-string 1) org-clock-string))
  11643. (setq ts (match-string 2))
  11644. (if fail-quietly (throw 'exit nil) (error "Clock start time is gone")))
  11645. (goto-char (match-end 0))
  11646. (delete-region (point) (point-at-eol))
  11647. (insert "--")
  11648. (setq te (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  11649. (setq s (- (time-to-seconds (apply 'encode-time (org-parse-time-string te)))
  11650. (time-to-seconds (apply 'encode-time (org-parse-time-string ts))))
  11651. h (floor (/ s 3600))
  11652. s (- s (* 3600 h))
  11653. m (floor (/ s 60))
  11654. s (- s (* 60 s)))
  11655. (insert " => " (format "%2d:%02d" h m))
  11656. (move-marker org-clock-marker nil)
  11657. (when org-log-note-clock-out
  11658. (org-add-log-setup 'clock-out))
  11659. (when org-mode-line-timer
  11660. (cancel-timer org-mode-line-timer)
  11661. (setq org-mode-line-timer nil))
  11662. (setq global-mode-string
  11663. (delq 'org-mode-line-string global-mode-string))
  11664. (force-mode-line-update)
  11665. (message "Clock stopped at %s after HH:MM = %d:%02d" te h m)))))
  11666. (defun org-clock-cancel ()
  11667. "Cancel the running clock be removing the start timestamp."
  11668. (interactive)
  11669. (if (not (marker-buffer org-clock-marker))
  11670. (error "No active clock"))
  11671. (save-excursion
  11672. (set-buffer (marker-buffer org-clock-marker))
  11673. (goto-char org-clock-marker)
  11674. (delete-region (1- (point-at-bol)) (point-at-eol)))
  11675. (setq global-mode-string
  11676. (delq 'org-mode-line-string global-mode-string))
  11677. (force-mode-line-update)
  11678. (message "Clock canceled"))
  11679. (defun org-clock-goto (&optional delete-windows)
  11680. "Go to the currently clocked-in entry."
  11681. (interactive "P")
  11682. (if (not (marker-buffer org-clock-marker))
  11683. (error "No active clock"))
  11684. (switch-to-buffer-other-window
  11685. (marker-buffer org-clock-marker))
  11686. (if delete-windows (delete-other-windows))
  11687. (goto-char org-clock-marker)
  11688. (org-show-entry)
  11689. (org-back-to-heading)
  11690. (org-cycle-hide-drawers 'children)
  11691. (recenter))
  11692. (defvar org-clock-file-total-minutes nil
  11693. "Holds the file total time in minutes, after a call to `org-clock-sum'.")
  11694. (make-variable-buffer-local 'org-clock-file-total-minutes)
  11695. (defun org-clock-sum (&optional tstart tend)
  11696. "Sum the times for each subtree.
  11697. Puts the resulting times in minutes as a text property on each headline."
  11698. (interactive)
  11699. (let* ((bmp (buffer-modified-p))
  11700. (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
  11701. org-clock-string
  11702. "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
  11703. (lmax 30)
  11704. (ltimes (make-vector lmax 0))
  11705. (t1 0)
  11706. (level 0)
  11707. ts te dt
  11708. time)
  11709. (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t))
  11710. (save-excursion
  11711. (goto-char (point-max))
  11712. (while (re-search-backward re nil t)
  11713. (cond
  11714. ((match-end 2)
  11715. ;; Two time stamps
  11716. (setq ts (match-string 2)
  11717. te (match-string 3)
  11718. ts (time-to-seconds
  11719. (apply 'encode-time (org-parse-time-string ts)))
  11720. te (time-to-seconds
  11721. (apply 'encode-time (org-parse-time-string te)))
  11722. ts (if tstart (max ts tstart) ts)
  11723. te (if tend (min te tend) te)
  11724. dt (- te ts)
  11725. t1 (if (> dt 0) (+ t1 (floor (/ dt 60))) t1)))
  11726. ((match-end 4)
  11727. ;; A naket time
  11728. (setq t1 (+ t1 (string-to-number (match-string 5))
  11729. (* 60 (string-to-number (match-string 4))))))
  11730. (t ;; A headline
  11731. (setq level (- (match-end 1) (match-beginning 1)))
  11732. (when (or (> t1 0) (> (aref ltimes level) 0))
  11733. (loop for l from 0 to level do
  11734. (aset ltimes l (+ (aref ltimes l) t1)))
  11735. (setq t1 0 time (aref ltimes level))
  11736. (loop for l from level to (1- lmax) do
  11737. (aset ltimes l 0))
  11738. (goto-char (match-beginning 0))
  11739. (put-text-property (point) (point-at-eol) :org-clock-minutes time)))))
  11740. (setq org-clock-file-total-minutes (aref ltimes 0)))
  11741. (set-buffer-modified-p bmp)))
  11742. (defun org-clock-display (&optional total-only)
  11743. "Show subtree times in the entire buffer.
  11744. If TOTAL-ONLY is non-nil, only show the total time for the entire file
  11745. in the echo area."
  11746. (interactive)
  11747. (org-remove-clock-overlays)
  11748. (let (time h m p)
  11749. (org-clock-sum)
  11750. (unless total-only
  11751. (save-excursion
  11752. (goto-char (point-min))
  11753. (while (or (and (equal (setq p (point)) (point-min))
  11754. (get-text-property p :org-clock-minutes))
  11755. (setq p (next-single-property-change
  11756. (point) :org-clock-minutes)))
  11757. (goto-char p)
  11758. (when (setq time (get-text-property p :org-clock-minutes))
  11759. (org-put-clock-overlay time (funcall outline-level))))
  11760. (setq h (/ org-clock-file-total-minutes 60)
  11761. m (- org-clock-file-total-minutes (* 60 h)))
  11762. ;; Arrange to remove the overlays upon next change.
  11763. (when org-remove-highlights-with-change
  11764. (org-add-hook 'before-change-functions 'org-remove-clock-overlays
  11765. nil 'local))))
  11766. (message "Total file time: %d:%02d (%d hours and %d minutes)" h m h m)))
  11767. (defvar org-clock-overlays nil)
  11768. (make-variable-buffer-local 'org-clock-overlays)
  11769. (defun org-put-clock-overlay (time &optional level)
  11770. "Put an overlays on the current line, displaying TIME.
  11771. If LEVEL is given, prefix time with a corresponding number of stars.
  11772. This creates a new overlay and stores it in `org-clock-overlays', so that it
  11773. will be easy to remove."
  11774. (let* ((c 60) (h (floor (/ time 60))) (m (- time (* 60 h)))
  11775. (l (if level (org-get-valid-level level 0) 0))
  11776. (off 0)
  11777. ov tx)
  11778. (move-to-column c)
  11779. (unless (eolp) (skip-chars-backward "^ \t"))
  11780. (skip-chars-backward " \t")
  11781. (setq ov (org-make-overlay (1- (point)) (point-at-eol))
  11782. tx (concat (buffer-substring (1- (point)) (point))
  11783. (make-string (+ off (max 0 (- c (current-column)))) ?.)
  11784. (org-add-props (format "%s %2d:%02d%s"
  11785. (make-string l ?*) h m
  11786. (make-string (- 16 l) ?\ ))
  11787. '(face secondary-selection))
  11788. ""))
  11789. (if (not (featurep 'xemacs))
  11790. (org-overlay-put ov 'display tx)
  11791. (org-overlay-put ov 'invisible t)
  11792. (org-overlay-put ov 'end-glyph (make-glyph tx)))
  11793. (push ov org-clock-overlays)))
  11794. (defun org-remove-clock-overlays (&optional beg end noremove)
  11795. "Remove the occur highlights from the buffer.
  11796. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11797. from the `before-change-functions' in the current buffer."
  11798. (interactive)
  11799. (unless org-inhibit-highlight-removal
  11800. (mapc 'org-delete-overlay org-clock-overlays)
  11801. (setq org-clock-overlays nil)
  11802. (unless noremove
  11803. (remove-hook 'before-change-functions
  11804. 'org-remove-clock-overlays 'local))))
  11805. (defun org-clock-out-if-current ()
  11806. "Clock out if the current entry contains the running clock.
  11807. This is used to stop the clock after a TODO entry is marked DONE,
  11808. and is only done if the variable `org-clock-out-when-done' is not nil."
  11809. (when (and org-clock-out-when-done
  11810. (member state org-done-keywords)
  11811. (equal (marker-buffer org-clock-marker) (current-buffer))
  11812. (< (point) org-clock-marker)
  11813. (> (save-excursion (outline-next-heading) (point))
  11814. org-clock-marker))
  11815. ;; Clock out, but don't accept a logging message for this.
  11816. (let ((org-log-note-clock-out nil))
  11817. (org-clock-out))))
  11818. (add-hook 'org-after-todo-state-change-hook
  11819. 'org-clock-out-if-current)
  11820. (defun org-check-running-clock ()
  11821. "Check if the current buffer contains the running clock.
  11822. If yes, offer to stop it and to save the buffer with the changes."
  11823. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  11824. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  11825. (buffer-name))))
  11826. (org-clock-out)
  11827. (when (y-or-n-p "Save changed buffer?")
  11828. (save-buffer))))
  11829. (defun org-clock-report (&optional arg)
  11830. "Create a table containing a report about clocked time.
  11831. If the cursor is inside an existing clocktable block, then the table
  11832. will be updated. If not, a new clocktable will be inserted.
  11833. When called with a prefix argument, move to the first clock table in the
  11834. buffer and update it."
  11835. (interactive "P")
  11836. (org-remove-clock-overlays)
  11837. (when arg
  11838. (org-find-dblock "clocktable")
  11839. (org-show-entry))
  11840. (if (org-in-clocktable-p)
  11841. (goto-char (org-in-clocktable-p))
  11842. (org-create-dblock (list :name "clocktable"
  11843. :maxlevel 2 :scope 'file)))
  11844. (org-update-dblock))
  11845. (defun org-in-clocktable-p ()
  11846. "Check if the cursor is in a clocktable."
  11847. (let ((pos (point)) start)
  11848. (save-excursion
  11849. (end-of-line 1)
  11850. (and (re-search-backward "^#\\+BEGIN:[ \t]+clocktable" nil t)
  11851. (setq start (match-beginning 0))
  11852. (re-search-forward "^#\\+END:.*" nil t)
  11853. (>= (match-end 0) pos)
  11854. start))))
  11855. (defun org-clock-update-time-maybe ()
  11856. "If this is a CLOCK line, update it and return t.
  11857. Otherwise, return nil."
  11858. (interactive)
  11859. (save-excursion
  11860. (beginning-of-line 1)
  11861. (skip-chars-forward " \t")
  11862. (when (looking-at org-clock-string)
  11863. (let ((re (concat "[ \t]*" org-clock-string
  11864. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  11865. "\\([ \t]*=>.*\\)?"))
  11866. ts te h m s)
  11867. (if (not (looking-at re))
  11868. nil
  11869. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  11870. (end-of-line 1)
  11871. (setq ts (match-string 1)
  11872. te (match-string 2))
  11873. (setq s (- (time-to-seconds
  11874. (apply 'encode-time (org-parse-time-string te)))
  11875. (time-to-seconds
  11876. (apply 'encode-time (org-parse-time-string ts))))
  11877. h (floor (/ s 3600))
  11878. s (- s (* 3600 h))
  11879. m (floor (/ s 60))
  11880. s (- s (* 60 s)))
  11881. (insert " => " (format "%2d:%02d" h m))
  11882. t)))))
  11883. (defun org-clock-special-range (key &optional time as-strings)
  11884. "Return two times bordering a special time range.
  11885. Key is a symbol specifying the range and can be one of `today', `yesterday',
  11886. `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
  11887. A week starts Monday 0:00 and ends Sunday 24:00.
  11888. The range is determined relative to TIME. TIME defaults to the current time.
  11889. The return value is a cons cell with two internal times like the ones
  11890. returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
  11891. the returned times will be formatted strings."
  11892. (if (integerp key) (setq key (intern (number-to-string key))))
  11893. (let* ((tm (decode-time (or time (current-time))))
  11894. (s 0) (m (nth 1 tm)) (h (nth 2 tm))
  11895. (d (nth 3 tm)) (month (nth 4 tm)) (y (nth 5 tm))
  11896. (dow (nth 6 tm))
  11897. (skey (symbol-name key))
  11898. (shift 0)
  11899. s1 m1 h1 d1 month1 y1 diff ts te fm txt w date)
  11900. (cond
  11901. ((string-match "^[0-9]+$" skey)
  11902. (setq y (string-to-number skey) m 1 d 1 key 'year))
  11903. ((string-match "^\\([0-9]+\\)-\\([0-9]\\{1,2\\}\\)$" skey)
  11904. (setq y (string-to-number (match-string 1 skey))
  11905. month (string-to-number (match-string 2 skey))
  11906. d 1 key 'month))
  11907. ((string-match "^\\([0-9]+\\)-[wW]\\([0-9]\\{1,2\\}\\)$" skey)
  11908. (require 'cal-iso)
  11909. (setq y (string-to-number (match-string 1 skey))
  11910. w (string-to-number (match-string 2 skey)))
  11911. (setq date (calendar-gregorian-from-absolute
  11912. (calendar-absolute-from-iso (list w 1 y))))
  11913. (setq d (nth 1 date) month (car date) y (nth 2 date)
  11914. key 'week))
  11915. ((string-match "^\\([0-9]+\\)-\\([0-9]\\{1,2\\}\\)-\\([0-9]\\{1,2\\}\\)$" skey)
  11916. (setq y (string-to-number (match-string 1 skey))
  11917. month (string-to-number (match-string 2 skey))
  11918. d (string-to-number (match-string 3 skey))
  11919. key 'day))
  11920. ((string-match "\\([-+][0-9]+\\)$" skey)
  11921. (setq shift (string-to-number (match-string 1 skey))
  11922. key (intern (substring skey 0 (match-beginning 1))))))
  11923. (unless shift
  11924. (cond ((eq key 'yesterday) (setq key 'today shift -1))
  11925. ((eq key 'lastweek) (setq key 'week shift -1))
  11926. ((eq key 'lastmonth) (setq key 'month shift -1))
  11927. ((eq key 'lastyear) (setq key 'year shift -1))))
  11928. (cond
  11929. ((memq key '(day today))
  11930. (setq d (+ d shift) h 0 m 0 h1 24 m1 0))
  11931. ((memq key '(week thisweek))
  11932. (setq diff (+ (* -7 shift) (if (= dow 0) 6 (1- dow)))
  11933. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  11934. ((memq key '(month thismonth))
  11935. (setq d 1 h 0 m 0 d1 1 month (+ month shift) month1 (1+ month) h1 0 m1 0))
  11936. ((memq key '(year thisyear))
  11937. (setq m 0 h 0 d 1 month 1 y (+ y shift) y1 (1+ y)))
  11938. (t (error "No such time block %s" key)))
  11939. (setq ts (encode-time s m h d month y)
  11940. te (encode-time (or s1 s) (or m1 m) (or h1 h)
  11941. (or d1 d) (or month1 month) (or y1 y)))
  11942. (setq fm (cdr org-time-stamp-formats))
  11943. (cond
  11944. ((memq key '(day today))
  11945. (setq txt (format-time-string "%A, %B %d, %Y" ts)))
  11946. ((memq key '(week thisweek))
  11947. (setq txt (format-time-string "week %G-W%V" ts)))
  11948. ((memq key '(month thismonth))
  11949. (setq txt (format-time-string "%B %Y" ts)))
  11950. ((memq key '(year thisyear))
  11951. (setq txt (format-time-string "the year %Y" ts))))
  11952. (if as-strings
  11953. (list (format-time-string fm ts) (format-time-string fm te) txt)
  11954. (list ts te txt))))
  11955. (defun org-clocktable-try-shift (dir n)
  11956. "Try to shift the :block date of the clocktable at point.
  11957. Point must be in the #+BEGIN: line of a clocktable, or this function
  11958. will throw an error.
  11959. DIR is a direction, a symbol `left', `right', `up', or `down'.
  11960. Both `left' and `down' shift the block toward the past, `up' and `right'
  11961. push it toward the future.
  11962. N is the number of shift steps to take. The size of the step depends on
  11963. the currently selected interval size."
  11964. (setq n (prefix-numeric-value n))
  11965. (and (memq dir '(left down)) (setq n (- n)))
  11966. (save-excursion
  11967. (goto-char (point-at-bol))
  11968. (when (looking-at "#\\+BEGIN: clocktable\\>.*?:block[ \t]+\\(\\S-+\\)")
  11969. (let* ((b (match-beginning 1)) (e (match-end 1))
  11970. (s (match-string 1))
  11971. block shift ins y mw d date wp m)
  11972. (cond
  11973. ((string-match "^\\(today\\|thisweek\\|thismonth\\|thisyear\\)\\([-+][0-9]+\\)?$" s)
  11974. (setq block (match-string 1 s)
  11975. shift (if (match-end 2)
  11976. (string-to-number (match-string 2 s))
  11977. 0))
  11978. (setq shift (+ shift n))
  11979. (setq ins (if (= shift 0) block (format "%s%+d" block shift))))
  11980. ((string-match "\\([0-9]+\\)\\(-\\([wW]?\\)\\([0-9]\\{1,2\\}\\)\\(-\\([0-9]\\{1,2\\}\\)\\)?\\)?" s)
  11981. ;; 1 1 2 3 3 4 4 5 6 6 5 2
  11982. (setq y (string-to-number (match-string 1 s))
  11983. wp (and (match-end 3) (match-string 3 s))
  11984. mw (and (match-end 4) (string-to-number (match-string 4 s)))
  11985. d (and (match-end 6) (string-to-number (match-string 6 s))))
  11986. (cond
  11987. (d (setq ins (format-time-string
  11988. "%Y-%m-%d"
  11989. (encode-time 0 0 0 (+ d n) m y))))
  11990. ((and wp mw (> (length wp) 0))
  11991. (require 'cal-iso)
  11992. (setq date (calendar-gregorian-from-absolute (calendar-absolute-from-iso (list (+ mw n) 1 y))))
  11993. (setq ins (format-time-string
  11994. "%G-W%V"
  11995. (encode-time 0 0 0 (nth 1 date) (car date) (nth 2 date)))))
  11996. (mw
  11997. (setq ins (format-time-string
  11998. "%Y-%m"
  11999. (encode-time 0 0 0 1 (+ mw n) y))))
  12000. (y
  12001. (setq ins (number-to-string (+ y n))))))
  12002. (t (error "Cannot shift clocktable block")))
  12003. (when ins
  12004. (goto-char b)
  12005. (insert ins)
  12006. (delete-region (point) (+ (point) (- e b)))
  12007. (beginning-of-line 1)
  12008. (org-update-dblock)
  12009. t)))))
  12010. (defun org-dblock-write:clocktable (params)
  12011. "Write the standard clocktable."
  12012. (catch 'exit
  12013. (let* ((hlchars '((1 . "*") (2 . "/")))
  12014. (ins (make-marker))
  12015. (total-time nil)
  12016. (scope (plist-get params :scope))
  12017. (tostring (plist-get params :tostring))
  12018. (multifile (plist-get params :multifile))
  12019. (header (plist-get params :header))
  12020. (maxlevel (or (plist-get params :maxlevel) 3))
  12021. (step (plist-get params :step))
  12022. (emph (plist-get params :emphasize))
  12023. (ts (plist-get params :tstart))
  12024. (te (plist-get params :tend))
  12025. (block (plist-get params :block))
  12026. (link (plist-get params :link))
  12027. ipos time p level hlc hdl
  12028. cc beg end pos tbl tbl1 range-text)
  12029. (when step
  12030. (org-clocktable-steps params)
  12031. (throw 'exit nil))
  12032. (when block
  12033. (setq cc (org-clock-special-range block nil t)
  12034. ts (car cc) te (nth 1 cc) range-text (nth 2 cc)))
  12035. (if ts (setq ts (time-to-seconds
  12036. (apply 'encode-time (org-parse-time-string ts)))))
  12037. (if te (setq te (time-to-seconds
  12038. (apply 'encode-time (org-parse-time-string te)))))
  12039. (move-marker ins (point))
  12040. (setq ipos (point))
  12041. ;; Get the right scope
  12042. (setq pos (point))
  12043. (save-restriction
  12044. (cond
  12045. ((not scope))
  12046. ((eq scope 'file) (widen))
  12047. ((eq scope 'subtree) (org-narrow-to-subtree))
  12048. ((eq scope 'tree)
  12049. (while (org-up-heading-safe))
  12050. (org-narrow-to-subtree))
  12051. ((and (symbolp scope) (string-match "^tree\\([0-9]+\\)$"
  12052. (symbol-name scope)))
  12053. (setq level (string-to-number (match-string 1 (symbol-name scope))))
  12054. (catch 'exit
  12055. (while (org-up-heading-safe)
  12056. (looking-at outline-regexp)
  12057. (if (<= (org-reduced-level (funcall outline-level)) level)
  12058. (throw 'exit nil))))
  12059. (org-narrow-to-subtree))
  12060. ((or (listp scope) (eq scope 'agenda))
  12061. (let* ((files (if (listp scope) scope (org-agenda-files)))
  12062. (scope 'agenda)
  12063. (p1 (copy-sequence params))
  12064. file)
  12065. (plist-put p1 :tostring t)
  12066. (plist-put p1 :multifile t)
  12067. (plist-put p1 :scope 'file)
  12068. (org-prepare-agenda-buffers files)
  12069. (while (setq file (pop files))
  12070. (with-current-buffer (find-buffer-visiting file)
  12071. (setq tbl1 (org-dblock-write:clocktable p1))
  12072. (when tbl1
  12073. (push (org-clocktable-add-file
  12074. file
  12075. (concat "| |*File time*|*"
  12076. (org-minutes-to-hours
  12077. org-clock-file-total-minutes)
  12078. "*|\n"
  12079. tbl1)) tbl)
  12080. (setq total-time (+ (or total-time 0)
  12081. org-clock-file-total-minutes))))))))
  12082. (goto-char pos)
  12083. (unless (eq scope 'agenda)
  12084. (org-clock-sum ts te)
  12085. (goto-char (point-min))
  12086. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  12087. (goto-char p)
  12088. (when (setq time (get-text-property p :org-clock-minutes))
  12089. (save-excursion
  12090. (beginning-of-line 1)
  12091. (when (and (looking-at (org-re "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[[:alnum:]_@:]+:\\)?[ \t]*$"))
  12092. (setq level (org-reduced-level
  12093. (- (match-end 1) (match-beginning 1))))
  12094. (<= level maxlevel))
  12095. (setq hlc (if emph (or (cdr (assoc level hlchars)) "") "")
  12096. hdl (if (not link)
  12097. (match-string 2)
  12098. (org-make-link-string
  12099. (format "file:%s::%s"
  12100. (buffer-file-name)
  12101. (save-match-data
  12102. (org-make-org-heading-search-string
  12103. (match-string 2))))
  12104. (match-string 2))))
  12105. (if (and (not multifile) (= level 1)) (push "|-" tbl))
  12106. (push (concat
  12107. "| " (int-to-string level) "|" hlc hdl hlc " |"
  12108. (make-string (1- level) ?|)
  12109. hlc (org-minutes-to-hours time) hlc
  12110. " |") tbl))))))
  12111. (setq tbl (nreverse tbl))
  12112. (if tostring
  12113. (if tbl (mapconcat 'identity tbl "\n") nil)
  12114. (goto-char ins)
  12115. (insert-before-markers
  12116. (or header
  12117. (concat
  12118. "Clock summary at ["
  12119. (substring
  12120. (format-time-string (cdr org-time-stamp-formats))
  12121. 1 -1)
  12122. "]"
  12123. (if block (concat ", for " range-text ".") "")
  12124. "\n\n"))
  12125. (if (eq scope 'agenda) "|File" "")
  12126. "|L|Headline|Time|\n")
  12127. (setq total-time (or total-time org-clock-file-total-minutes))
  12128. (insert-before-markers
  12129. "|-\n|"
  12130. (if (eq scope 'agenda) "|" "")
  12131. "|"
  12132. "*Total time*| *"
  12133. (org-minutes-to-hours total-time)
  12134. "*|\n|-\n")
  12135. (setq tbl (delq nil tbl))
  12136. (if (and (stringp (car tbl)) (> (length (car tbl)) 1)
  12137. (equal (substring (car tbl) 0 2) "|-"))
  12138. (pop tbl))
  12139. (insert-before-markers (mapconcat
  12140. 'identity (delq nil tbl)
  12141. (if (eq scope 'agenda) "\n|-\n" "\n")))
  12142. (backward-delete-char 1)
  12143. (goto-char ipos)
  12144. (skip-chars-forward "^|")
  12145. (org-table-align))))))
  12146. (defun org-minutes-to-hours (m)
  12147. (let ((h (/ m 60)))
  12148. (setq m (- m (* 60 h)))
  12149. (format "%d:%02d" h m)))
  12150. (defun org-clocktable-steps (params)
  12151. (let* ((p1 (copy-sequence params))
  12152. (ts (plist-get p1 :tstart))
  12153. (te (plist-get p1 :tend))
  12154. (step0 (plist-get p1 :step))
  12155. (step (cdr (assoc step0 '((day . 86400) (week . 604800)))))
  12156. (block (plist-get p1 :block))
  12157. cc range-text)
  12158. (when block
  12159. (setq cc (org-clock-special-range block nil t)
  12160. ts (car cc) te (nth 1 cc) range-text (nth 2 cc)))
  12161. (if ts (setq ts (time-to-seconds
  12162. (apply 'encode-time (org-parse-time-string ts)))))
  12163. (if te (setq te (time-to-seconds
  12164. (apply 'encode-time (org-parse-time-string te)))))
  12165. (plist-put p1 :header "")
  12166. (plist-put p1 :step nil)
  12167. (plist-put p1 :block nil)
  12168. (while (< ts te)
  12169. (or (bolp) (insert "\n"))
  12170. (plist-put p1 :tstart (format-time-string
  12171. (car org-time-stamp-formats)
  12172. (seconds-to-time ts)))
  12173. (plist-put p1 :tend (format-time-string
  12174. (car org-time-stamp-formats)
  12175. (seconds-to-time (setq ts (+ ts step)))))
  12176. (insert "\n" (if (eq step0 'day) "Daily report: " "Weekly report starting on: ")
  12177. (plist-get p1 :tstart) "\n")
  12178. (org-dblock-write:clocktable p1)
  12179. (re-search-forward "#\\+END:")
  12180. (end-of-line 0))))
  12181. (defun org-clocktable-add-file (file table)
  12182. (if table
  12183. (let ((lines (org-split-string table "\n"))
  12184. (ff (file-name-nondirectory file)))
  12185. (mapconcat 'identity
  12186. (mapcar (lambda (x)
  12187. (if (string-match org-table-dataline-regexp x)
  12188. (concat "|" ff x)
  12189. x))
  12190. lines)
  12191. "\n"))))
  12192. ;; FIXME: I don't think anybody uses this, ask David
  12193. (defun org-collect-clock-time-entries ()
  12194. "Return an internal list with clocking information.
  12195. This list has one entry for each CLOCK interval.
  12196. FIXME: describe the elements."
  12197. (interactive)
  12198. (let ((re (concat "^[ \t]*" org-clock-string
  12199. " *\\[\\(.*?\\)\\]--\\[\\(.*?\\)\\]"))
  12200. rtn beg end next cont level title total closedp leafp
  12201. clockpos titlepos h m donep)
  12202. (save-excursion
  12203. (org-clock-sum)
  12204. (goto-char (point-min))
  12205. (while (re-search-forward re nil t)
  12206. (setq clockpos (match-beginning 0)
  12207. beg (match-string 1) end (match-string 2)
  12208. cont (match-end 0))
  12209. (setq beg (apply 'encode-time (org-parse-time-string beg))
  12210. end (apply 'encode-time (org-parse-time-string end)))
  12211. (org-back-to-heading t)
  12212. (setq donep (org-entry-is-done-p))
  12213. (setq titlepos (point)
  12214. total (or (get-text-property (1+ (point)) :org-clock-minutes) 0)
  12215. h (/ total 60) m (- total (* 60 h))
  12216. total (cons h m))
  12217. (looking-at "\\(\\*+\\) +\\(.*\\)")
  12218. (setq level (- (match-end 1) (match-beginning 1))
  12219. title (org-match-string-no-properties 2))
  12220. (save-excursion (outline-next-heading) (setq next (point)))
  12221. (setq closedp (re-search-forward org-closed-time-regexp next t))
  12222. (goto-char next)
  12223. (setq leafp (and (looking-at "^\\*+ ")
  12224. (<= (- (match-end 0) (point)) level)))
  12225. (push (list beg end clockpos closedp donep
  12226. total title titlepos level leafp)
  12227. rtn)
  12228. (goto-char cont)))
  12229. (nreverse rtn)))
  12230. ;;;; Agenda files
  12231. (defun org-agenda-files (&optional unrestricted)
  12232. "Get the list of agenda files.
  12233. Optional UNRESTRICTED means return the full list even if a restriction
  12234. is currently in place."
  12235. (let ((files
  12236. (cond
  12237. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  12238. ((stringp org-agenda-files) (org-read-agenda-file-list))
  12239. ((listp org-agenda-files) org-agenda-files)
  12240. (t (error "Invalid value of `org-agenda-files'")))))
  12241. (setq files (apply 'append
  12242. (mapcar (lambda (f)
  12243. (if (file-directory-p f)
  12244. (directory-files f t
  12245. org-agenda-file-regexp)
  12246. (list f)))
  12247. files)))
  12248. (if org-agenda-skip-unavailable-files
  12249. (delq nil
  12250. (mapcar (function
  12251. (lambda (file)
  12252. (and (file-readable-p file) file)))
  12253. files))
  12254. files))) ; `org-check-agenda-file' will remove them from the list
  12255. (defun org-edit-agenda-file-list ()
  12256. "Edit the list of agenda files.
  12257. Depending on setup, this either uses customize to edit the variable
  12258. `org-agenda-files', or it visits the file that is holding the list. In the
  12259. latter case, the buffer is set up in a way that saving it automatically kills
  12260. the buffer and restores the previous window configuration."
  12261. (interactive)
  12262. (if (stringp org-agenda-files)
  12263. (let ((cw (current-window-configuration)))
  12264. (find-file org-agenda-files)
  12265. (org-set-local 'org-window-configuration cw)
  12266. (org-add-hook 'after-save-hook
  12267. (lambda ()
  12268. (set-window-configuration
  12269. (prog1 org-window-configuration
  12270. (kill-buffer (current-buffer))))
  12271. (org-install-agenda-files-menu)
  12272. (message "New agenda file list installed"))
  12273. nil 'local)
  12274. (message "%s" (substitute-command-keys
  12275. "Edit list and finish with \\[save-buffer]")))
  12276. (customize-variable 'org-agenda-files)))
  12277. (defun org-store-new-agenda-file-list (list)
  12278. "Set new value for the agenda file list and save it correcly."
  12279. (if (stringp org-agenda-files)
  12280. (let ((f org-agenda-files) b)
  12281. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  12282. (with-temp-file f
  12283. (insert (mapconcat 'identity list "\n") "\n")))
  12284. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  12285. (setq org-agenda-files list)
  12286. (customize-save-variable 'org-agenda-files org-agenda-files))))
  12287. (defun org-read-agenda-file-list ()
  12288. "Read the list of agenda files from a file."
  12289. (when (stringp org-agenda-files)
  12290. (with-temp-buffer
  12291. (insert-file-contents org-agenda-files)
  12292. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  12293. ;;;###autoload
  12294. (defun org-cycle-agenda-files ()
  12295. "Cycle through the files in `org-agenda-files'.
  12296. If the current buffer visits an agenda file, find the next one in the list.
  12297. If the current buffer does not, find the first agenda file."
  12298. (interactive)
  12299. (let* ((fs (org-agenda-files t))
  12300. (files (append fs (list (car fs))))
  12301. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  12302. file)
  12303. (unless files (error "No agenda files"))
  12304. (catch 'exit
  12305. (while (setq file (pop files))
  12306. (if (equal (file-truename file) tcf)
  12307. (when (car files)
  12308. (find-file (car files))
  12309. (throw 'exit t))))
  12310. (find-file (car fs)))
  12311. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  12312. (defun org-agenda-file-to-front (&optional to-end)
  12313. "Move/add the current file to the top of the agenda file list.
  12314. If the file is not present in the list, it is added to the front. If it is
  12315. present, it is moved there. With optional argument TO-END, add/move to the
  12316. end of the list."
  12317. (interactive "P")
  12318. (let ((org-agenda-skip-unavailable-files nil)
  12319. (file-alist (mapcar (lambda (x)
  12320. (cons (file-truename x) x))
  12321. (org-agenda-files t)))
  12322. (ctf (file-truename buffer-file-name))
  12323. x had)
  12324. (setq x (assoc ctf file-alist) had x)
  12325. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  12326. (if to-end
  12327. (setq file-alist (append (delq x file-alist) (list x)))
  12328. (setq file-alist (cons x (delq x file-alist))))
  12329. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  12330. (org-install-agenda-files-menu)
  12331. (message "File %s to %s of agenda file list"
  12332. (if had "moved" "added") (if to-end "end" "front"))))
  12333. (defun org-remove-file (&optional file)
  12334. "Remove current file from the list of files in variable `org-agenda-files'.
  12335. These are the files which are being checked for agenda entries.
  12336. Optional argument FILE means, use this file instead of the current."
  12337. (interactive)
  12338. (let* ((org-agenda-skip-unavailable-files nil)
  12339. (file (or file buffer-file-name))
  12340. (true-file (file-truename file))
  12341. (afile (abbreviate-file-name file))
  12342. (files (delq nil (mapcar
  12343. (lambda (x)
  12344. (if (equal true-file
  12345. (file-truename x))
  12346. nil x))
  12347. (org-agenda-files t)))))
  12348. (if (not (= (length files) (length (org-agenda-files t))))
  12349. (progn
  12350. (org-store-new-agenda-file-list files)
  12351. (org-install-agenda-files-menu)
  12352. (message "Removed file: %s" afile))
  12353. (message "File was not in list: %s (not removed)" afile))))
  12354. (defun org-file-menu-entry (file)
  12355. (vector file (list 'find-file file) t))
  12356. (defun org-check-agenda-file (file)
  12357. "Make sure FILE exists. If not, ask user what to do."
  12358. (when (not (file-exists-p file))
  12359. (message "non-existent file %s. [R]emove from list or [A]bort?"
  12360. (abbreviate-file-name file))
  12361. (let ((r (downcase (read-char-exclusive))))
  12362. (cond
  12363. ((equal r ?r)
  12364. (org-remove-file file)
  12365. (throw 'nextfile t))
  12366. (t (error "Abort"))))))
  12367. (defun org-get-agenda-file-buffer (file)
  12368. "Get a buffer visiting FILE. If the buffer needs to be created, add
  12369. it to the list of buffers which might be released later."
  12370. (let ((buf (org-find-base-buffer-visiting file)))
  12371. (if buf
  12372. buf ; just return it
  12373. ;; Make a new buffer and remember it
  12374. (setq buf (find-file-noselect file))
  12375. (if buf (push buf org-agenda-new-buffers))
  12376. buf)))
  12377. (defun org-release-buffers (blist)
  12378. "Release all buffers in list, asking the user for confirmation when needed.
  12379. When a buffer is unmodified, it is just killed. When modified, it is saved
  12380. \(if the user agrees) and then killed."
  12381. (let (buf file)
  12382. (while (setq buf (pop blist))
  12383. (setq file (buffer-file-name buf))
  12384. (when (and (buffer-modified-p buf)
  12385. file
  12386. (y-or-n-p (format "Save file %s? " file)))
  12387. (with-current-buffer buf (save-buffer)))
  12388. (kill-buffer buf))))
  12389. (defun org-prepare-agenda-buffers (files)
  12390. "Create buffers for all agenda files, protect archived trees and comments."
  12391. (interactive)
  12392. (let ((pa '(:org-archived t))
  12393. (pc '(:org-comment t))
  12394. (pall '(:org-archived t :org-comment t))
  12395. (inhibit-read-only t)
  12396. (rea (concat ":" org-archive-tag ":"))
  12397. bmp file re)
  12398. (save-excursion
  12399. (save-restriction
  12400. (while (setq file (pop files))
  12401. (if (bufferp file)
  12402. (set-buffer file)
  12403. (org-check-agenda-file file)
  12404. (set-buffer (org-get-agenda-file-buffer file)))
  12405. (widen)
  12406. (setq bmp (buffer-modified-p))
  12407. (org-refresh-category-properties)
  12408. (setq org-todo-keywords-for-agenda
  12409. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  12410. (setq org-done-keywords-for-agenda
  12411. (append org-done-keywords-for-agenda org-done-keywords))
  12412. (save-excursion
  12413. (remove-text-properties (point-min) (point-max) pall)
  12414. (when org-agenda-skip-archived-trees
  12415. (goto-char (point-min))
  12416. (while (re-search-forward rea nil t)
  12417. (if (org-on-heading-p t)
  12418. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  12419. (goto-char (point-min))
  12420. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  12421. (while (re-search-forward re nil t)
  12422. (add-text-properties
  12423. (match-beginning 0) (org-end-of-subtree t) pc)))
  12424. (set-buffer-modified-p bmp))))))
  12425. ;;;; Embedded LaTeX
  12426. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  12427. "Keymap for the minor `org-cdlatex-mode'.")
  12428. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  12429. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  12430. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  12431. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  12432. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  12433. (defvar org-cdlatex-texmathp-advice-is-done nil
  12434. "Flag remembering if we have applied the advice to texmathp already.")
  12435. (define-minor-mode org-cdlatex-mode
  12436. "Toggle the minor `org-cdlatex-mode'.
  12437. This mode supports entering LaTeX environment and math in LaTeX fragments
  12438. in Org-mode.
  12439. \\{org-cdlatex-mode-map}"
  12440. nil " OCDL" nil
  12441. (when org-cdlatex-mode (require 'cdlatex))
  12442. (unless org-cdlatex-texmathp-advice-is-done
  12443. (setq org-cdlatex-texmathp-advice-is-done t)
  12444. (defadvice texmathp (around org-math-always-on activate)
  12445. "Always return t in org-mode buffers.
  12446. This is because we want to insert math symbols without dollars even outside
  12447. the LaTeX math segments. If Orgmode thinks that point is actually inside
  12448. en embedded LaTeX fragement, let texmathp do its job.
  12449. \\[org-cdlatex-mode-map]"
  12450. (interactive)
  12451. (let (p)
  12452. (cond
  12453. ((not (org-mode-p)) ad-do-it)
  12454. ((eq this-command 'cdlatex-math-symbol)
  12455. (setq ad-return-value t
  12456. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  12457. (t
  12458. (let ((p (org-inside-LaTeX-fragment-p)))
  12459. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  12460. (setq ad-return-value t
  12461. texmathp-why '("Org-mode embedded math" . 0))
  12462. (if p ad-do-it)))))))))
  12463. (defun turn-on-org-cdlatex ()
  12464. "Unconditionally turn on `org-cdlatex-mode'."
  12465. (org-cdlatex-mode 1))
  12466. (defun org-inside-LaTeX-fragment-p ()
  12467. "Test if point is inside a LaTeX fragment.
  12468. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  12469. sequence appearing also before point.
  12470. Even though the matchers for math are configurable, this function assumes
  12471. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  12472. delimiters are skipped when they have been removed by customization.
  12473. The return value is nil, or a cons cell with the delimiter and
  12474. and the position of this delimiter.
  12475. This function does a reasonably good job, but can locally be fooled by
  12476. for example currency specifications. For example it will assume being in
  12477. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  12478. fragments that are properly closed, but during editing, we have to live
  12479. with the uncertainty caused by missing closing delimiters. This function
  12480. looks only before point, not after."
  12481. (catch 'exit
  12482. (let ((pos (point))
  12483. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  12484. (lim (progn
  12485. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  12486. (point)))
  12487. dd-on str (start 0) m re)
  12488. (goto-char pos)
  12489. (when dodollar
  12490. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  12491. re (nth 1 (assoc "$" org-latex-regexps)))
  12492. (while (string-match re str start)
  12493. (cond
  12494. ((= (match-end 0) (length str))
  12495. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  12496. ((= (match-end 0) (- (length str) 5))
  12497. (throw 'exit nil))
  12498. (t (setq start (match-end 0))))))
  12499. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  12500. (goto-char pos)
  12501. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  12502. (and (match-beginning 2) (throw 'exit nil))
  12503. ;; count $$
  12504. (while (re-search-backward "\\$\\$" lim t)
  12505. (setq dd-on (not dd-on)))
  12506. (goto-char pos)
  12507. (if dd-on (cons "$$" m))))))
  12508. (defun org-try-cdlatex-tab ()
  12509. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  12510. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  12511. - inside a LaTeX fragment, or
  12512. - after the first word in a line, where an abbreviation expansion could
  12513. insert a LaTeX environment."
  12514. (when org-cdlatex-mode
  12515. (cond
  12516. ((save-excursion
  12517. (skip-chars-backward "a-zA-Z0-9*")
  12518. (skip-chars-backward " \t")
  12519. (bolp))
  12520. (cdlatex-tab) t)
  12521. ((org-inside-LaTeX-fragment-p)
  12522. (cdlatex-tab) t)
  12523. (t nil))))
  12524. (defun org-cdlatex-underscore-caret (&optional arg)
  12525. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  12526. Revert to the normal definition outside of these fragments."
  12527. (interactive "P")
  12528. (if (org-inside-LaTeX-fragment-p)
  12529. (call-interactively 'cdlatex-sub-superscript)
  12530. (let (org-cdlatex-mode)
  12531. (call-interactively (key-binding (vector last-input-event))))))
  12532. (defun org-cdlatex-math-modify (&optional arg)
  12533. "Execute `cdlatex-math-modify' in LaTeX fragments.
  12534. Revert to the normal definition outside of these fragments."
  12535. (interactive "P")
  12536. (if (org-inside-LaTeX-fragment-p)
  12537. (call-interactively 'cdlatex-math-modify)
  12538. (let (org-cdlatex-mode)
  12539. (call-interactively (key-binding (vector last-input-event))))))
  12540. (defvar org-latex-fragment-image-overlays nil
  12541. "List of overlays carrying the images of latex fragments.")
  12542. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  12543. (defun org-remove-latex-fragment-image-overlays ()
  12544. "Remove all overlays with LaTeX fragment images in current buffer."
  12545. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  12546. (setq org-latex-fragment-image-overlays nil))
  12547. (defun org-preview-latex-fragment (&optional subtree)
  12548. "Preview the LaTeX fragment at point, or all locally or globally.
  12549. If the cursor is in a LaTeX fragment, create the image and overlay
  12550. it over the source code. If there is no fragment at point, display
  12551. all fragments in the current text, from one headline to the next. With
  12552. prefix SUBTREE, display all fragments in the current subtree. With a
  12553. double prefix `C-u C-u', or when the cursor is before the first headline,
  12554. display all fragments in the buffer.
  12555. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  12556. (interactive "P")
  12557. (org-remove-latex-fragment-image-overlays)
  12558. (save-excursion
  12559. (save-restriction
  12560. (let (beg end at msg)
  12561. (cond
  12562. ((or (equal subtree '(16))
  12563. (not (save-excursion
  12564. (re-search-backward (concat "^" outline-regexp) nil t))))
  12565. (setq beg (point-min) end (point-max)
  12566. msg "Creating images for buffer...%s"))
  12567. ((equal subtree '(4))
  12568. (org-back-to-heading)
  12569. (setq beg (point) end (org-end-of-subtree t)
  12570. msg "Creating images for subtree...%s"))
  12571. (t
  12572. (if (setq at (org-inside-LaTeX-fragment-p))
  12573. (goto-char (max (point-min) (- (cdr at) 2)))
  12574. (org-back-to-heading))
  12575. (setq beg (point) end (progn (outline-next-heading) (point))
  12576. msg (if at "Creating image...%s"
  12577. "Creating images for entry...%s"))))
  12578. (message msg "")
  12579. (narrow-to-region beg end)
  12580. (goto-char beg)
  12581. (org-format-latex
  12582. (concat "ltxpng/" (file-name-sans-extension
  12583. (file-name-nondirectory
  12584. buffer-file-name)))
  12585. default-directory 'overlays msg at 'forbuffer)
  12586. (message msg "done. Use `C-c C-c' to remove images.")))))
  12587. (defvar org-latex-regexps
  12588. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  12589. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  12590. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  12591. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  12592. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  12593. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  12594. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  12595. "Regular expressions for matching embedded LaTeX.")
  12596. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  12597. "Replace LaTeX fragments with links to an image, and produce images."
  12598. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  12599. (let* ((prefixnodir (file-name-nondirectory prefix))
  12600. (absprefix (expand-file-name prefix dir))
  12601. (todir (file-name-directory absprefix))
  12602. (opt org-format-latex-options)
  12603. (matchers (plist-get opt :matchers))
  12604. (re-list org-latex-regexps)
  12605. (cnt 0) txt link beg end re e checkdir
  12606. m n block linkfile movefile ov)
  12607. ;; Check if there are old images files with this prefix, and remove them
  12608. (when (file-directory-p todir)
  12609. (mapc 'delete-file
  12610. (directory-files
  12611. todir 'full
  12612. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  12613. ;; Check the different regular expressions
  12614. (while (setq e (pop re-list))
  12615. (setq m (car e) re (nth 1 e) n (nth 2 e)
  12616. block (if (nth 3 e) "\n\n" ""))
  12617. (when (member m matchers)
  12618. (goto-char (point-min))
  12619. (while (re-search-forward re nil t)
  12620. (when (or (not at) (equal (cdr at) (match-beginning n)))
  12621. (setq txt (match-string n)
  12622. beg (match-beginning n) end (match-end n)
  12623. cnt (1+ cnt)
  12624. linkfile (format "%s_%04d.png" prefix cnt)
  12625. movefile (format "%s_%04d.png" absprefix cnt)
  12626. link (concat block "[[file:" linkfile "]]" block))
  12627. (if msg (message msg cnt))
  12628. (goto-char beg)
  12629. (unless checkdir ; make sure the directory exists
  12630. (setq checkdir t)
  12631. (or (file-directory-p todir) (make-directory todir)))
  12632. (org-create-formula-image
  12633. txt movefile opt forbuffer)
  12634. (if overlays
  12635. (progn
  12636. (setq ov (org-make-overlay beg end))
  12637. (if (featurep 'xemacs)
  12638. (progn
  12639. (org-overlay-put ov 'invisible t)
  12640. (org-overlay-put
  12641. ov 'end-glyph
  12642. (make-glyph (vector 'png :file movefile))))
  12643. (org-overlay-put
  12644. ov 'display
  12645. (list 'image :type 'png :file movefile :ascent 'center)))
  12646. (push ov org-latex-fragment-image-overlays)
  12647. (goto-char end))
  12648. (delete-region beg end)
  12649. (insert link))))))))
  12650. ;; This function borrows from Ganesh Swami's latex2png.el
  12651. (defun org-create-formula-image (string tofile options buffer)
  12652. (let* ((tmpdir (if (featurep 'xemacs)
  12653. (temp-directory)
  12654. temporary-file-directory))
  12655. (texfilebase (make-temp-name
  12656. (expand-file-name "orgtex" tmpdir)))
  12657. (texfile (concat texfilebase ".tex"))
  12658. (dvifile (concat texfilebase ".dvi"))
  12659. (pngfile (concat texfilebase ".png"))
  12660. (fnh (face-attribute 'default :height nil))
  12661. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  12662. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  12663. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  12664. "Black"))
  12665. (bg (or (plist-get options (if buffer :background :html-background))
  12666. "Transparent")))
  12667. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  12668. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  12669. (with-temp-file texfile
  12670. (insert org-format-latex-header
  12671. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  12672. (let ((dir default-directory))
  12673. (condition-case nil
  12674. (progn
  12675. (cd tmpdir)
  12676. (call-process "latex" nil nil nil texfile))
  12677. (error nil))
  12678. (cd dir))
  12679. (if (not (file-exists-p dvifile))
  12680. (progn (message "Failed to create dvi file from %s" texfile) nil)
  12681. (call-process "dvipng" nil nil nil
  12682. "-E" "-fg" fg "-bg" bg
  12683. "-D" dpi
  12684. ;;"-x" scale "-y" scale
  12685. "-T" "tight"
  12686. "-o" pngfile
  12687. dvifile)
  12688. (if (not (file-exists-p pngfile))
  12689. (progn (message "Failed to create png file from %s" texfile) nil)
  12690. ;; Use the requested file name and clean up
  12691. (copy-file pngfile tofile 'replace)
  12692. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  12693. (delete-file (concat texfilebase e)))
  12694. pngfile))))
  12695. (defun org-dvipng-color (attr)
  12696. "Return an rgb color specification for dvipng."
  12697. (apply 'format "rgb %s %s %s"
  12698. (mapcar 'org-normalize-color
  12699. (color-values (face-attribute 'default attr nil)))))
  12700. (defun org-normalize-color (value)
  12701. "Return string to be used as color value for an RGB component."
  12702. (format "%g" (/ value 65535.0)))
  12703. ;;;; Key bindings
  12704. ;; Make `C-c C-x' a prefix key
  12705. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  12706. ;; TAB key with modifiers
  12707. (org-defkey org-mode-map "\C-i" 'org-cycle)
  12708. (org-defkey org-mode-map [(tab)] 'org-cycle)
  12709. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  12710. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  12711. (org-defkey org-mode-map "\M-\t" 'org-complete)
  12712. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  12713. ;; The following line is necessary under Suse GNU/Linux
  12714. (unless (featurep 'xemacs)
  12715. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  12716. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  12717. (define-key org-mode-map [backtab] 'org-shifttab)
  12718. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  12719. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  12720. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  12721. ;; Cursor keys with modifiers
  12722. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  12723. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  12724. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  12725. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  12726. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  12727. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  12728. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  12729. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  12730. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  12731. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  12732. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  12733. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  12734. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  12735. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  12736. ;;; Extra keys for tty access.
  12737. ;; We only set them when really needed because otherwise the
  12738. ;; menus don't show the simple keys
  12739. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  12740. (not window-system))
  12741. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  12742. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  12743. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  12744. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  12745. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  12746. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  12747. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  12748. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  12749. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  12750. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  12751. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  12752. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  12753. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  12754. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  12755. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  12756. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  12757. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  12758. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  12759. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  12760. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  12761. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  12762. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  12763. ;; All the other keys
  12764. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  12765. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  12766. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  12767. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  12768. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  12769. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  12770. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  12771. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  12772. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  12773. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  12774. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  12775. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  12776. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  12777. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  12778. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  12779. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  12780. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  12781. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  12782. (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
  12783. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  12784. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  12785. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  12786. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  12787. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  12788. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  12789. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  12790. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  12791. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  12792. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  12793. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  12794. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  12795. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  12796. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  12797. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  12798. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  12799. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  12800. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  12801. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12802. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  12803. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  12804. (org-defkey org-mode-map "\C-c^" 'org-sort)
  12805. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  12806. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  12807. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  12808. (org-defkey org-mode-map "\C-m" 'org-return)
  12809. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  12810. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  12811. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  12812. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  12813. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  12814. (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
  12815. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  12816. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  12817. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  12818. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  12819. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  12820. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  12821. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  12822. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  12823. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  12824. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  12825. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  12826. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  12827. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  12828. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  12829. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  12830. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  12831. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  12832. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  12833. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  12834. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  12835. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  12836. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  12837. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  12838. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  12839. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  12840. (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
  12841. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  12842. (when (featurep 'xemacs)
  12843. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  12844. (defvar org-table-auto-blank-field) ; defined in org-table.el
  12845. (defun org-self-insert-command (N)
  12846. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  12847. If the cursor is in a table looking at whitespace, the whitespace is
  12848. overwritten, and the table is not marked as requiring realignment."
  12849. (interactive "p")
  12850. (if (and (org-table-p)
  12851. (progn
  12852. ;; check if we blank the field, and if that triggers align
  12853. (and (featurep 'org-table) org-table-auto-blank-field
  12854. (member last-command
  12855. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  12856. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  12857. ;; got extra space, this field does not determine column width
  12858. (let (org-table-may-need-update) (org-table-blank-field))
  12859. ;; no extra space, this field may determine column width
  12860. (org-table-blank-field)))
  12861. t)
  12862. (eq N 1)
  12863. (looking-at "[^|\n]* |"))
  12864. (let (org-table-may-need-update)
  12865. (goto-char (1- (match-end 0)))
  12866. (delete-backward-char 1)
  12867. (goto-char (match-beginning 0))
  12868. (self-insert-command N))
  12869. (setq org-table-may-need-update t)
  12870. (self-insert-command N)
  12871. (org-fix-tags-on-the-fly)))
  12872. (defun org-fix-tags-on-the-fly ()
  12873. (when (and (equal (char-after (point-at-bol)) ?*)
  12874. (org-on-heading-p))
  12875. (org-align-tags-here org-tags-column)))
  12876. (defun org-delete-backward-char (N)
  12877. "Like `delete-backward-char', insert whitespace at field end in tables.
  12878. When deleting backwards, in tables this function will insert whitespace in
  12879. front of the next \"|\" separator, to keep the table aligned. The table will
  12880. still be marked for re-alignment if the field did fill the entire column,
  12881. because, in this case the deletion might narrow the column."
  12882. (interactive "p")
  12883. (if (and (org-table-p)
  12884. (eq N 1)
  12885. (string-match "|" (buffer-substring (point-at-bol) (point)))
  12886. (looking-at ".*?|"))
  12887. (let ((pos (point))
  12888. (noalign (looking-at "[^|\n\r]* |"))
  12889. (c org-table-may-need-update))
  12890. (backward-delete-char N)
  12891. (skip-chars-forward "^|")
  12892. (insert " ")
  12893. (goto-char (1- pos))
  12894. ;; noalign: if there were two spaces at the end, this field
  12895. ;; does not determine the width of the column.
  12896. (if noalign (setq org-table-may-need-update c)))
  12897. (backward-delete-char N)
  12898. (org-fix-tags-on-the-fly)))
  12899. (defun org-delete-char (N)
  12900. "Like `delete-char', but insert whitespace at field end in tables.
  12901. When deleting characters, in tables this function will insert whitespace in
  12902. front of the next \"|\" separator, to keep the table aligned. The table will
  12903. still be marked for re-alignment if the field did fill the entire column,
  12904. because, in this case the deletion might narrow the column."
  12905. (interactive "p")
  12906. (if (and (org-table-p)
  12907. (not (bolp))
  12908. (not (= (char-after) ?|))
  12909. (eq N 1))
  12910. (if (looking-at ".*?|")
  12911. (let ((pos (point))
  12912. (noalign (looking-at "[^|\n\r]* |"))
  12913. (c org-table-may-need-update))
  12914. (replace-match (concat
  12915. (substring (match-string 0) 1 -1)
  12916. " |"))
  12917. (goto-char pos)
  12918. ;; noalign: if there were two spaces at the end, this field
  12919. ;; does not determine the width of the column.
  12920. (if noalign (setq org-table-may-need-update c)))
  12921. (delete-char N))
  12922. (delete-char N)
  12923. (org-fix-tags-on-the-fly)))
  12924. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  12925. (put 'org-self-insert-command 'delete-selection t)
  12926. (put 'orgtbl-self-insert-command 'delete-selection t)
  12927. (put 'org-delete-char 'delete-selection 'supersede)
  12928. (put 'org-delete-backward-char 'delete-selection 'supersede)
  12929. ;; Make `flyspell-mode' delay after some commands
  12930. (put 'org-self-insert-command 'flyspell-delayed t)
  12931. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  12932. (put 'org-delete-char 'flyspell-delayed t)
  12933. (put 'org-delete-backward-char 'flyspell-delayed t)
  12934. ;; Make pabbrev-mode expand after org-mode commands
  12935. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  12936. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  12937. ;; How to do this: Measure non-white length of current string
  12938. ;; If equal to column width, we should realign.
  12939. (defun org-remap (map &rest commands)
  12940. "In MAP, remap the functions given in COMMANDS.
  12941. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  12942. (let (new old)
  12943. (while commands
  12944. (setq old (pop commands) new (pop commands))
  12945. (if (fboundp 'command-remapping)
  12946. (org-defkey map (vector 'remap old) new)
  12947. (substitute-key-definition old new map global-map)))))
  12948. (when (eq org-enable-table-editor 'optimized)
  12949. ;; If the user wants maximum table support, we need to hijack
  12950. ;; some standard editing functions
  12951. (org-remap org-mode-map
  12952. 'self-insert-command 'org-self-insert-command
  12953. 'delete-char 'org-delete-char
  12954. 'delete-backward-char 'org-delete-backward-char)
  12955. (org-defkey org-mode-map "|" 'org-force-self-insert))
  12956. (defun org-shiftcursor-error ()
  12957. "Throw an error because Shift-Cursor command was applied in wrong context."
  12958. (error "This command is active in special context like tables, headlines or timestamps"))
  12959. (defun org-shifttab (&optional arg)
  12960. "Global visibility cycling or move to previous table field.
  12961. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  12962. on context.
  12963. See the individual commands for more information."
  12964. (interactive "P")
  12965. (cond
  12966. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  12967. (arg (message "Content view to level: ")
  12968. (org-content (prefix-numeric-value arg))
  12969. (setq org-cycle-global-status 'overview))
  12970. (t (call-interactively 'org-global-cycle))))
  12971. (defun org-shiftmetaleft ()
  12972. "Promote subtree or delete table column.
  12973. Calls `org-promote-subtree', `org-outdent-item',
  12974. or `org-table-delete-column', depending on context.
  12975. See the individual commands for more information."
  12976. (interactive)
  12977. (cond
  12978. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  12979. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  12980. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12981. (t (org-shiftcursor-error))))
  12982. (defun org-shiftmetaright ()
  12983. "Demote subtree or insert table column.
  12984. Calls `org-demote-subtree', `org-indent-item',
  12985. or `org-table-insert-column', depending on context.
  12986. See the individual commands for more information."
  12987. (interactive)
  12988. (cond
  12989. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  12990. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  12991. ((org-at-item-p) (call-interactively 'org-indent-item))
  12992. (t (org-shiftcursor-error))))
  12993. (defun org-shiftmetaup (&optional arg)
  12994. "Move subtree up or kill table row.
  12995. Calls `org-move-subtree-up' or `org-table-kill-row' or
  12996. `org-move-item-up' depending on context. See the individual commands
  12997. for more information."
  12998. (interactive "P")
  12999. (cond
  13000. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  13001. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13002. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13003. (t (org-shiftcursor-error))))
  13004. (defun org-shiftmetadown (&optional arg)
  13005. "Move subtree down or insert table row.
  13006. Calls `org-move-subtree-down' or `org-table-insert-row' or
  13007. `org-move-item-down', depending on context. See the individual
  13008. commands for more information."
  13009. (interactive "P")
  13010. (cond
  13011. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  13012. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13013. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13014. (t (org-shiftcursor-error))))
  13015. (defun org-metaleft (&optional arg)
  13016. "Promote heading or move table column to left.
  13017. Calls `org-do-promote' or `org-table-move-column', depending on context.
  13018. With no specific context, calls the Emacs default `backward-word'.
  13019. See the individual commands for more information."
  13020. (interactive "P")
  13021. (cond
  13022. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  13023. ((or (org-on-heading-p) (org-region-active-p))
  13024. (call-interactively 'org-do-promote))
  13025. ((org-at-item-p) (call-interactively 'org-outdent-item))
  13026. (t (call-interactively 'backward-word))))
  13027. (defun org-metaright (&optional arg)
  13028. "Demote subtree or move table column to right.
  13029. Calls `org-do-demote' or `org-table-move-column', depending on context.
  13030. With no specific context, calls the Emacs default `forward-word'.
  13031. See the individual commands for more information."
  13032. (interactive "P")
  13033. (cond
  13034. ((org-at-table-p) (call-interactively 'org-table-move-column))
  13035. ((or (org-on-heading-p) (org-region-active-p))
  13036. (call-interactively 'org-do-demote))
  13037. ((org-at-item-p) (call-interactively 'org-indent-item))
  13038. (t (call-interactively 'forward-word))))
  13039. (defun org-metaup (&optional arg)
  13040. "Move subtree up or move table row up.
  13041. Calls `org-move-subtree-up' or `org-table-move-row' or
  13042. `org-move-item-up', depending on context. See the individual commands
  13043. for more information."
  13044. (interactive "P")
  13045. (cond
  13046. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  13047. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13048. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13049. (t (transpose-lines 1) (beginning-of-line -1))))
  13050. (defun org-metadown (&optional arg)
  13051. "Move subtree down or move table row down.
  13052. Calls `org-move-subtree-down' or `org-table-move-row' or
  13053. `org-move-item-down', depending on context. See the individual
  13054. commands for more information."
  13055. (interactive "P")
  13056. (cond
  13057. ((org-at-table-p) (call-interactively 'org-table-move-row))
  13058. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13059. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13060. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  13061. (defun org-shiftup (&optional arg)
  13062. "Increase item in timestamp or increase priority of current headline.
  13063. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  13064. depending on context. See the individual commands for more information."
  13065. (interactive "P")
  13066. (cond
  13067. ((org-at-timestamp-p t)
  13068. (call-interactively (if org-edit-timestamp-down-means-later
  13069. 'org-timestamp-down 'org-timestamp-up)))
  13070. ((org-on-heading-p) (call-interactively 'org-priority-up))
  13071. ((org-at-item-p) (call-interactively 'org-previous-item))
  13072. ((org-clocktable-try-shift 'up arg))
  13073. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  13074. (defun org-shiftdown (&optional arg)
  13075. "Decrease item in timestamp or decrease priority of current headline.
  13076. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  13077. depending on context. See the individual commands for more information."
  13078. (interactive "P")
  13079. (cond
  13080. ((org-at-timestamp-p t)
  13081. (call-interactively (if org-edit-timestamp-down-means-later
  13082. 'org-timestamp-up 'org-timestamp-down)))
  13083. ((org-on-heading-p) (call-interactively 'org-priority-down))
  13084. ((org-clocktable-try-shift 'down arg))
  13085. (t (call-interactively 'org-next-item))))
  13086. (defun org-shiftright (&optional arg)
  13087. "Next TODO keyword or timestamp one day later, depending on context."
  13088. (interactive "P")
  13089. (cond
  13090. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  13091. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  13092. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  13093. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  13094. ((org-clocktable-try-shift 'right arg))
  13095. (t (org-shiftcursor-error))))
  13096. (defun org-shiftleft (&optional arg)
  13097. "Previous TODO keyword or timestamp one day earlier, depending on context."
  13098. (interactive "P")
  13099. (cond
  13100. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  13101. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  13102. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  13103. ((org-at-property-p)
  13104. (call-interactively 'org-property-previous-allowed-value))
  13105. ((org-clocktable-try-shift 'left arg))
  13106. (t (org-shiftcursor-error))))
  13107. (defun org-shiftcontrolright ()
  13108. "Switch to next TODO set."
  13109. (interactive)
  13110. (cond
  13111. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  13112. (t (org-shiftcursor-error))))
  13113. (defun org-shiftcontrolleft ()
  13114. "Switch to previous TODO set."
  13115. (interactive)
  13116. (cond
  13117. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  13118. (t (org-shiftcursor-error))))
  13119. (defun org-ctrl-c-ret ()
  13120. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  13121. (interactive)
  13122. (cond
  13123. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  13124. (t (call-interactively 'org-insert-heading))))
  13125. (defun org-copy-special ()
  13126. "Copy region in table or copy current subtree.
  13127. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  13128. See the individual commands for more information."
  13129. (interactive)
  13130. (call-interactively
  13131. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  13132. (defun org-cut-special ()
  13133. "Cut region in table or cut current subtree.
  13134. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  13135. See the individual commands for more information."
  13136. (interactive)
  13137. (call-interactively
  13138. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  13139. (defun org-paste-special (arg)
  13140. "Paste rectangular region into table, or past subtree relative to level.
  13141. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  13142. See the individual commands for more information."
  13143. (interactive "P")
  13144. (if (org-at-table-p)
  13145. (org-table-paste-rectangle)
  13146. (org-paste-subtree arg)))
  13147. (defun org-ctrl-c-ctrl-c (&optional arg)
  13148. "Set tags in headline, or update according to changed information at point.
  13149. This command does many different things, depending on context:
  13150. - If the cursor is in a headline, prompt for tags and insert them
  13151. into the current line, aligned to `org-tags-column'. When called
  13152. with prefix arg, realign all tags in the current buffer.
  13153. - If the cursor is in one of the special #+KEYWORD lines, this
  13154. triggers scanning the buffer for these lines and updating the
  13155. information.
  13156. - If the cursor is inside a table, realign the table. This command
  13157. works even if the automatic table editor has been turned off.
  13158. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  13159. the entire table.
  13160. - If the cursor is a the beginning of a dynamic block, update it.
  13161. - If the cursor is inside a table created by the table.el package,
  13162. activate that table.
  13163. - If the current buffer is a remember buffer, close note and file it.
  13164. with a prefix argument, file it without further interaction to the default
  13165. location.
  13166. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  13167. links in this buffer.
  13168. - If the cursor is on a numbered item in a plain list, renumber the
  13169. ordered list.
  13170. - If the cursor is on a checkbox, toggle it."
  13171. (interactive "P")
  13172. (let ((org-enable-table-editor t))
  13173. (cond
  13174. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  13175. org-occur-highlights
  13176. org-latex-fragment-image-overlays)
  13177. (and (boundp 'org-clock-overlays) (org-remove-clock-overlays))
  13178. (org-remove-occur-highlights)
  13179. (org-remove-latex-fragment-image-overlays)
  13180. (message "Temporary highlights/overlays removed from current buffer"))
  13181. ((and (local-variable-p 'org-finish-function (current-buffer))
  13182. (fboundp org-finish-function))
  13183. (funcall org-finish-function))
  13184. ((org-at-property-p)
  13185. (call-interactively 'org-property-action))
  13186. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  13187. ((org-on-heading-p) (call-interactively 'org-set-tags))
  13188. ((org-at-table.el-p)
  13189. (require 'table)
  13190. (beginning-of-line 1)
  13191. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  13192. (call-interactively 'table-recognize-table))
  13193. ((org-at-table-p)
  13194. (org-table-maybe-eval-formula)
  13195. (if arg
  13196. (call-interactively 'org-table-recalculate)
  13197. (org-table-maybe-recalculate-line))
  13198. (call-interactively 'org-table-align))
  13199. ((org-at-item-checkbox-p)
  13200. (call-interactively 'org-toggle-checkbox))
  13201. ((org-at-item-p)
  13202. (call-interactively 'org-maybe-renumber-ordered-list))
  13203. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  13204. ;; Dynamic block
  13205. (beginning-of-line 1)
  13206. (org-update-dblock))
  13207. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  13208. (cond
  13209. ((equal (match-string 1) "TBLFM")
  13210. ;; Recalculate the table before this line
  13211. (save-excursion
  13212. (beginning-of-line 1)
  13213. (skip-chars-backward " \r\n\t")
  13214. (if (org-at-table-p)
  13215. (org-call-with-arg 'org-table-recalculate t))))
  13216. (t
  13217. (call-interactively 'org-mode-restart))))
  13218. (t (error "C-c C-c can do nothing useful at this location.")))))
  13219. (defun org-mode-restart ()
  13220. "Restart Org-mode, to scan again for special lines.
  13221. Also updates the keyword regular expressions."
  13222. (interactive)
  13223. (let ((org-inhibit-startup t)) (org-mode))
  13224. (message "Org-mode restarted to refresh keyword and special line setup"))
  13225. (defun org-kill-note-or-show-branches ()
  13226. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  13227. (interactive)
  13228. (if (not org-finish-function)
  13229. (call-interactively 'show-branches)
  13230. (let ((org-note-abort t))
  13231. (funcall org-finish-function))))
  13232. (defun org-return (&optional indent)
  13233. "Goto next table row or insert a newline.
  13234. Calls `org-table-next-row' or `newline', depending on context.
  13235. See the individual commands for more information."
  13236. (interactive)
  13237. (cond
  13238. ((bobp) (if indent (newline-and-indent) (newline)))
  13239. ((and (org-at-heading-p)
  13240. (looking-at
  13241. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  13242. (org-show-entry)
  13243. (end-of-line 1)
  13244. (newline))
  13245. ((org-at-table-p)
  13246. (org-table-justify-field-maybe)
  13247. (call-interactively 'org-table-next-row))
  13248. (t (if indent (newline-and-indent) (newline)))))
  13249. (defun org-return-indent ()
  13250. "Goto next table row or insert a newline and indent.
  13251. Calls `org-table-next-row' or `newline-and-indent', depending on
  13252. context. See the individual commands for more information."
  13253. (interactive)
  13254. (org-return t))
  13255. (defun org-ctrl-c-star ()
  13256. "Compute table, or change heading status of lines.
  13257. Calls `org-table-recalculate' or `org-toggle-region-headlines',
  13258. depending on context. This will also turn a plain list item or a normal
  13259. line into a subheading."
  13260. (interactive)
  13261. (cond
  13262. ((org-at-table-p)
  13263. (call-interactively 'org-table-recalculate))
  13264. ((org-region-active-p)
  13265. ;; Convert all lines in region to list items
  13266. (call-interactively 'org-toggle-region-headings))
  13267. ((org-on-heading-p)
  13268. (org-toggle-region-headings (point-at-bol)
  13269. (min (1+ (point-at-eol)) (point-max))))
  13270. ((org-at-item-p)
  13271. ;; Convert to heading
  13272. (let ((level (save-match-data
  13273. (save-excursion
  13274. (condition-case nil
  13275. (progn
  13276. (org-back-to-heading t)
  13277. (funcall outline-level))
  13278. (error 0))))))
  13279. (replace-match
  13280. (concat (make-string (org-get-valid-level level 1) ?*) " ") t t)))
  13281. (t (org-toggle-region-headings (point-at-bol)
  13282. (min (1+ (point-at-eol)) (point-max))))))
  13283. (defun org-ctrl-c-minus ()
  13284. "Insert separator line in table or modify bullet status of line.
  13285. Also turns a plain line or a region of lines into list items.
  13286. Calls `org-table-insert-hline', `org-toggle-region-items', or
  13287. `org-cycle-list-bullet', depending on context."
  13288. (interactive)
  13289. (cond
  13290. ((org-at-table-p)
  13291. (call-interactively 'org-table-insert-hline))
  13292. ((org-on-heading-p)
  13293. ;; Convert to item
  13294. (save-excursion
  13295. (beginning-of-line 1)
  13296. (if (looking-at "\\*+ ")
  13297. (replace-match (concat (make-string (- (match-end 0) (point) 1) ?\ ) "- ")))))
  13298. ((org-region-active-p)
  13299. ;; Convert all lines in region to list items
  13300. (call-interactively 'org-toggle-region-items))
  13301. ((org-in-item-p)
  13302. (call-interactively 'org-cycle-list-bullet))
  13303. (t (org-toggle-region-items (point-at-bol)
  13304. (min (1+ (point-at-eol)) (point-max))))))
  13305. (defun org-toggle-region-items (beg end)
  13306. "Convert all lines in region to list items.
  13307. If the first line is already an item, convert all list items in the region
  13308. to normal lines."
  13309. (interactive "r")
  13310. (let (l2 l)
  13311. (save-excursion
  13312. (goto-char end)
  13313. (setq l2 (org-current-line))
  13314. (goto-char beg)
  13315. (beginning-of-line 1)
  13316. (setq l (1- (org-current-line)))
  13317. (if (org-at-item-p)
  13318. ;; We already have items, de-itemize
  13319. (while (< (setq l (1+ l)) l2)
  13320. (when (org-at-item-p)
  13321. (goto-char (match-beginning 2))
  13322. (delete-region (match-beginning 2) (match-end 2))
  13323. (and (looking-at "[ \t]+") (replace-match "")))
  13324. (beginning-of-line 2))
  13325. (while (< (setq l (1+ l)) l2)
  13326. (unless (org-at-item-p)
  13327. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13328. (replace-match "\\1- \\2")))
  13329. (beginning-of-line 2))))))
  13330. (defun org-toggle-region-headings (beg end)
  13331. "Convert all lines in region to list items.
  13332. If the first line is already an item, convert all list items in the region
  13333. to normal lines."
  13334. (interactive "r")
  13335. (let (l2 l)
  13336. (save-excursion
  13337. (goto-char end)
  13338. (setq l2 (org-current-line))
  13339. (goto-char beg)
  13340. (beginning-of-line 1)
  13341. (setq l (1- (org-current-line)))
  13342. (if (org-on-heading-p)
  13343. ;; We already have headlines, de-star them
  13344. (while (< (setq l (1+ l)) l2)
  13345. (when (org-on-heading-p t)
  13346. (and (looking-at outline-regexp) (replace-match "")))
  13347. (beginning-of-line 2))
  13348. (let* ((stars (save-excursion
  13349. (re-search-backward org-complex-heading-regexp nil t)
  13350. (or (match-string 1) "*")))
  13351. (add-stars (if org-odd-levels-only "**" "*"))
  13352. (rpl (concat stars add-stars " \\2")))
  13353. (while (< (setq l (1+ l)) l2)
  13354. (unless (org-on-heading-p)
  13355. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13356. (replace-match rpl)))
  13357. (beginning-of-line 2)))))))
  13358. (defun org-meta-return (&optional arg)
  13359. "Insert a new heading or wrap a region in a table.
  13360. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  13361. See the individual commands for more information."
  13362. (interactive "P")
  13363. (cond
  13364. ((org-at-table-p)
  13365. (call-interactively 'org-table-wrap-region))
  13366. (t (call-interactively 'org-insert-heading))))
  13367. ;;; Menu entries
  13368. ;; Define the Org-mode menus
  13369. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  13370. '("Tbl"
  13371. ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
  13372. ["Next Field" org-cycle (org-at-table-p)]
  13373. ["Previous Field" org-shifttab (org-at-table-p)]
  13374. ["Next Row" org-return (org-at-table-p)]
  13375. "--"
  13376. ["Blank Field" org-table-blank-field (org-at-table-p)]
  13377. ["Edit Field" org-table-edit-field (org-at-table-p)]
  13378. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  13379. "--"
  13380. ("Column"
  13381. ["Move Column Left" org-metaleft (org-at-table-p)]
  13382. ["Move Column Right" org-metaright (org-at-table-p)]
  13383. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  13384. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  13385. ("Row"
  13386. ["Move Row Up" org-metaup (org-at-table-p)]
  13387. ["Move Row Down" org-metadown (org-at-table-p)]
  13388. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  13389. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  13390. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  13391. "--"
  13392. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  13393. ("Rectangle"
  13394. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  13395. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  13396. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  13397. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  13398. "--"
  13399. ("Calculate"
  13400. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  13401. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  13402. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  13403. "--"
  13404. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  13405. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  13406. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  13407. "--"
  13408. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  13409. "--"
  13410. ["Sum Column/Rectangle" org-table-sum
  13411. (or (org-at-table-p) (org-region-active-p))]
  13412. ["Which Column?" org-table-current-column (org-at-table-p)])
  13413. ["Debug Formulas"
  13414. org-table-toggle-formula-debugger
  13415. :style toggle :selected org-table-formula-debug]
  13416. ["Show Col/Row Numbers"
  13417. org-table-toggle-coordinate-overlays
  13418. :style toggle :selected org-table-overlay-coordinates]
  13419. "--"
  13420. ["Create" org-table-create (and (not (org-at-table-p))
  13421. org-enable-table-editor)]
  13422. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  13423. ["Import from File" org-table-import (not (org-at-table-p))]
  13424. ["Export to File" org-table-export (org-at-table-p)]
  13425. "--"
  13426. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  13427. (easy-menu-define org-org-menu org-mode-map "Org menu"
  13428. '("Org"
  13429. ("Show/Hide"
  13430. ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
  13431. ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
  13432. ["Sparse Tree" org-occur t]
  13433. ["Reveal Context" org-reveal t]
  13434. ["Show All" show-all t]
  13435. "--"
  13436. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  13437. "--"
  13438. ["New Heading" org-insert-heading t]
  13439. ("Navigate Headings"
  13440. ["Up" outline-up-heading t]
  13441. ["Next" outline-next-visible-heading t]
  13442. ["Previous" outline-previous-visible-heading t]
  13443. ["Next Same Level" outline-forward-same-level t]
  13444. ["Previous Same Level" outline-backward-same-level t]
  13445. "--"
  13446. ["Jump" org-goto t])
  13447. ("Edit Structure"
  13448. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  13449. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  13450. "--"
  13451. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  13452. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  13453. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  13454. "--"
  13455. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  13456. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  13457. ["Demote Heading" org-metaright (not (org-at-table-p))]
  13458. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  13459. "--"
  13460. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  13461. "--"
  13462. ["Convert to odd levels" org-convert-to-odd-levels t]
  13463. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  13464. ("Editing"
  13465. ["Emphasis..." org-emphasize t])
  13466. ("Archive"
  13467. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  13468. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  13469. ; :active t :keys "C-u C-c C-x C-a"]
  13470. ["Sparse trees open ARCHIVE trees"
  13471. (setq org-sparse-tree-open-archived-trees
  13472. (not org-sparse-tree-open-archived-trees))
  13473. :style toggle :selected org-sparse-tree-open-archived-trees]
  13474. ["Cycling opens ARCHIVE trees"
  13475. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  13476. :style toggle :selected org-cycle-open-archived-trees]
  13477. ["Agenda includes ARCHIVE trees"
  13478. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  13479. :style toggle :selected (not org-agenda-skip-archived-trees)]
  13480. "--"
  13481. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  13482. ; ["Check and Move Children" (org-archive-subtree '(4))
  13483. ; :active t :keys "C-u C-c C-x C-s"]
  13484. )
  13485. "--"
  13486. ("TODO Lists"
  13487. ["TODO/DONE/-" org-todo t]
  13488. ("Select keyword"
  13489. ["Next keyword" org-shiftright (org-on-heading-p)]
  13490. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  13491. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  13492. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  13493. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  13494. ["Show TODO Tree" org-show-todo-tree t]
  13495. ["Global TODO list" org-todo-list t]
  13496. "--"
  13497. ["Set Priority" org-priority t]
  13498. ["Priority Up" org-shiftup t]
  13499. ["Priority Down" org-shiftdown t])
  13500. ("TAGS and Properties"
  13501. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  13502. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  13503. "--"
  13504. ["Set property" 'org-set-property t]
  13505. ["Column view of properties" org-columns t]
  13506. ["Insert Column View DBlock" org-insert-columns-dblock t])
  13507. ("Dates and Scheduling"
  13508. ["Timestamp" org-time-stamp t]
  13509. ["Timestamp (inactive)" org-time-stamp-inactive t]
  13510. ("Change Date"
  13511. ["1 Day Later" org-shiftright t]
  13512. ["1 Day Earlier" org-shiftleft t]
  13513. ["1 ... Later" org-shiftup t]
  13514. ["1 ... Earlier" org-shiftdown t])
  13515. ["Compute Time Range" org-evaluate-time-range t]
  13516. ["Schedule Item" org-schedule t]
  13517. ["Deadline" org-deadline t]
  13518. "--"
  13519. ["Custom time format" org-toggle-time-stamp-overlays
  13520. :style radio :selected org-display-custom-times]
  13521. "--"
  13522. ["Goto Calendar" org-goto-calendar t]
  13523. ["Date from Calendar" org-date-from-calendar t])
  13524. ("Logging work"
  13525. ["Clock in" org-clock-in t]
  13526. ["Clock out" org-clock-out t]
  13527. ["Clock cancel" org-clock-cancel t]
  13528. ["Goto running clock" org-clock-goto t]
  13529. ["Display times" org-clock-display t]
  13530. ["Create clock table" org-clock-report t]
  13531. "--"
  13532. ["Record DONE time"
  13533. (progn (setq org-log-done (not org-log-done))
  13534. (message "Switching to %s will %s record a timestamp"
  13535. (car org-done-keywords)
  13536. (if org-log-done "automatically" "not")))
  13537. :style toggle :selected org-log-done])
  13538. "--"
  13539. ["Agenda Command..." org-agenda t]
  13540. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  13541. ("File List for Agenda")
  13542. ("Special views current file"
  13543. ["TODO Tree" org-show-todo-tree t]
  13544. ["Check Deadlines" org-check-deadlines t]
  13545. ["Timeline" org-timeline t]
  13546. ["Tags Tree" org-tags-sparse-tree t])
  13547. "--"
  13548. ("Hyperlinks"
  13549. ["Store Link (Global)" org-store-link t]
  13550. ["Insert Link" org-insert-link t]
  13551. ["Follow Link" org-open-at-point t]
  13552. "--"
  13553. ["Next link" org-next-link t]
  13554. ["Previous link" org-previous-link t]
  13555. "--"
  13556. ["Descriptive Links"
  13557. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  13558. :style radio :selected (member '(org-link) buffer-invisibility-spec)]
  13559. ["Literal Links"
  13560. (progn
  13561. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  13562. :style radio :selected (not (member '(org-link) buffer-invisibility-spec))])
  13563. "--"
  13564. ["Export/Publish..." org-export t]
  13565. ("LaTeX"
  13566. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  13567. :selected org-cdlatex-mode]
  13568. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  13569. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  13570. ["Modify math symbol" org-cdlatex-math-modify
  13571. (org-inside-LaTeX-fragment-p)]
  13572. ["Export LaTeX fragments as images"
  13573. (if (featurep 'org-exp)
  13574. (setq org-export-with-LaTeX-fragments
  13575. (not org-export-with-LaTeX-fragments))
  13576. (require 'org-exp))
  13577. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  13578. org-export-with-LaTeX-fragments)])
  13579. "--"
  13580. ("Documentation"
  13581. ["Show Version" org-version t]
  13582. ["Info Documentation" org-info t])
  13583. ("Customize"
  13584. ["Browse Org Group" org-customize t]
  13585. "--"
  13586. ["Expand This Menu" org-create-customize-menu
  13587. (fboundp 'customize-menu-create)])
  13588. "--"
  13589. ["Refresh setup" org-mode-restart t]
  13590. ))
  13591. (defun org-info (&optional node)
  13592. "Read documentation for Org-mode in the info system.
  13593. With optional NODE, go directly to that node."
  13594. (interactive)
  13595. (info (format "(org)%s" (or node ""))))
  13596. (defun org-install-agenda-files-menu ()
  13597. (let ((bl (buffer-list)))
  13598. (save-excursion
  13599. (while bl
  13600. (set-buffer (pop bl))
  13601. (if (org-mode-p) (setq bl nil)))
  13602. (when (org-mode-p)
  13603. (easy-menu-change
  13604. '("Org") "File List for Agenda"
  13605. (append
  13606. (list
  13607. ["Edit File List" (org-edit-agenda-file-list) t]
  13608. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  13609. ["Remove Current File from List" org-remove-file t]
  13610. ["Cycle through agenda files" org-cycle-agenda-files t]
  13611. ["Occur in all agenda files" org-occur-in-agenda-files t]
  13612. "--")
  13613. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  13614. ;;;; Documentation
  13615. (defun org-customize ()
  13616. "Call the customize function with org as argument."
  13617. (interactive)
  13618. (customize-browse 'org))
  13619. (defun org-create-customize-menu ()
  13620. "Create a full customization menu for Org-mode, insert it into the menu."
  13621. (interactive)
  13622. (if (fboundp 'customize-menu-create)
  13623. (progn
  13624. (easy-menu-change
  13625. '("Org") "Customize"
  13626. `(["Browse Org group" org-customize t]
  13627. "--"
  13628. ,(customize-menu-create 'org)
  13629. ["Set" Custom-set t]
  13630. ["Save" Custom-save t]
  13631. ["Reset to Current" Custom-reset-current t]
  13632. ["Reset to Saved" Custom-reset-saved t]
  13633. ["Reset to Standard Settings" Custom-reset-standard t]))
  13634. (message "\"Org\"-menu now contains full customization menu"))
  13635. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  13636. ;;;; Miscellaneous stuff
  13637. ;;; Generally useful functions
  13638. (defun org-force-self-insert (N)
  13639. "Needed to enforce self-insert under remapping."
  13640. (interactive "p")
  13641. (self-insert-command N))
  13642. (defun org-string-width (s)
  13643. "Compute width of string, ignoring invisible characters.
  13644. This ignores character with invisibility property `org-link', and also
  13645. characters with property `org-cwidth', because these will become invisible
  13646. upon the next fontification round."
  13647. (let (b l)
  13648. (when (or (eq t buffer-invisibility-spec)
  13649. (assq 'org-link buffer-invisibility-spec))
  13650. (while (setq b (text-property-any 0 (length s)
  13651. 'invisible 'org-link s))
  13652. (setq s (concat (substring s 0 b)
  13653. (substring s (or (next-single-property-change
  13654. b 'invisible s) (length s)))))))
  13655. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  13656. (setq s (concat (substring s 0 b)
  13657. (substring s (or (next-single-property-change
  13658. b 'org-cwidth s) (length s))))))
  13659. (setq l (string-width s) b -1)
  13660. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  13661. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  13662. l))
  13663. (defun org-trim (s)
  13664. "Remove whitespace at beginning and end of string."
  13665. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  13666. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  13667. s)
  13668. (defun org-wrap (string &optional width lines)
  13669. "Wrap string to either a number of lines, or a width in characters.
  13670. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  13671. that costs. If there is a word longer than WIDTH, the text is actually
  13672. wrapped to the length of that word.
  13673. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  13674. many lines, whatever width that takes.
  13675. The return value is a list of lines, without newlines at the end."
  13676. (let* ((words (org-split-string string "[ \t\n]+"))
  13677. (maxword (apply 'max (mapcar 'org-string-width words)))
  13678. w ll)
  13679. (cond (width
  13680. (org-do-wrap words (max maxword width)))
  13681. (lines
  13682. (setq w maxword)
  13683. (setq ll (org-do-wrap words maxword))
  13684. (if (<= (length ll) lines)
  13685. ll
  13686. (setq ll words)
  13687. (while (> (length ll) lines)
  13688. (setq w (1+ w))
  13689. (setq ll (org-do-wrap words w)))
  13690. ll))
  13691. (t (error "Cannot wrap this")))))
  13692. (defun org-do-wrap (words width)
  13693. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  13694. (let (lines line)
  13695. (while words
  13696. (setq line (pop words))
  13697. (while (and words (< (+ (length line) (length (car words))) width))
  13698. (setq line (concat line " " (pop words))))
  13699. (setq lines (push line lines)))
  13700. (nreverse lines)))
  13701. (defun org-split-string (string &optional separators)
  13702. "Splits STRING into substrings at SEPARATORS.
  13703. No empty strings are returned if there are matches at the beginning
  13704. and end of string."
  13705. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  13706. (start 0)
  13707. notfirst
  13708. (list nil))
  13709. (while (and (string-match rexp string
  13710. (if (and notfirst
  13711. (= start (match-beginning 0))
  13712. (< start (length string)))
  13713. (1+ start) start))
  13714. (< (match-beginning 0) (length string)))
  13715. (setq notfirst t)
  13716. (or (eq (match-beginning 0) 0)
  13717. (and (eq (match-beginning 0) (match-end 0))
  13718. (eq (match-beginning 0) start))
  13719. (setq list
  13720. (cons (substring string start (match-beginning 0))
  13721. list)))
  13722. (setq start (match-end 0)))
  13723. (or (eq start (length string))
  13724. (setq list
  13725. (cons (substring string start)
  13726. list)))
  13727. (nreverse list)))
  13728. (defun org-context ()
  13729. "Return a list of contexts of the current cursor position.
  13730. If several contexts apply, all are returned.
  13731. Each context entry is a list with a symbol naming the context, and
  13732. two positions indicating start and end of the context. Possible
  13733. contexts are:
  13734. :headline anywhere in a headline
  13735. :headline-stars on the leading stars in a headline
  13736. :todo-keyword on a TODO keyword (including DONE) in a headline
  13737. :tags on the TAGS in a headline
  13738. :priority on the priority cookie in a headline
  13739. :item on the first line of a plain list item
  13740. :item-bullet on the bullet/number of a plain list item
  13741. :checkbox on the checkbox in a plain list item
  13742. :table in an org-mode table
  13743. :table-special on a special filed in a table
  13744. :table-table in a table.el table
  13745. :link on a hyperlink
  13746. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  13747. :target on a <<target>>
  13748. :radio-target on a <<<radio-target>>>
  13749. :latex-fragment on a LaTeX fragment
  13750. :latex-preview on a LaTeX fragment with overlayed preview image
  13751. This function expects the position to be visible because it uses font-lock
  13752. faces as a help to recognize the following contexts: :table-special, :link,
  13753. and :keyword."
  13754. (let* ((f (get-text-property (point) 'face))
  13755. (faces (if (listp f) f (list f)))
  13756. (p (point)) clist o)
  13757. ;; First the large context
  13758. (cond
  13759. ((org-on-heading-p t)
  13760. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  13761. (when (progn
  13762. (beginning-of-line 1)
  13763. (looking-at org-todo-line-tags-regexp))
  13764. (push (org-point-in-group p 1 :headline-stars) clist)
  13765. (push (org-point-in-group p 2 :todo-keyword) clist)
  13766. (push (org-point-in-group p 4 :tags) clist))
  13767. (goto-char p)
  13768. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  13769. (if (looking-at "\\[#[A-Z0-9]\\]")
  13770. (push (org-point-in-group p 0 :priority) clist)))
  13771. ((org-at-item-p)
  13772. (push (org-point-in-group p 2 :item-bullet) clist)
  13773. (push (list :item (point-at-bol)
  13774. (save-excursion (org-end-of-item) (point)))
  13775. clist)
  13776. (and (org-at-item-checkbox-p)
  13777. (push (org-point-in-group p 0 :checkbox) clist)))
  13778. ((org-at-table-p)
  13779. (push (list :table (org-table-begin) (org-table-end)) clist)
  13780. (if (memq 'org-formula faces)
  13781. (push (list :table-special
  13782. (previous-single-property-change p 'face)
  13783. (next-single-property-change p 'face)) clist)))
  13784. ((org-at-table-p 'any)
  13785. (push (list :table-table) clist)))
  13786. (goto-char p)
  13787. ;; Now the small context
  13788. (cond
  13789. ((org-at-timestamp-p)
  13790. (push (org-point-in-group p 0 :timestamp) clist))
  13791. ((memq 'org-link faces)
  13792. (push (list :link
  13793. (previous-single-property-change p 'face)
  13794. (next-single-property-change p 'face)) clist))
  13795. ((memq 'org-special-keyword faces)
  13796. (push (list :keyword
  13797. (previous-single-property-change p 'face)
  13798. (next-single-property-change p 'face)) clist))
  13799. ((org-on-target-p)
  13800. (push (org-point-in-group p 0 :target) clist)
  13801. (goto-char (1- (match-beginning 0)))
  13802. (if (looking-at org-radio-target-regexp)
  13803. (push (org-point-in-group p 0 :radio-target) clist))
  13804. (goto-char p))
  13805. ((setq o (car (delq nil
  13806. (mapcar
  13807. (lambda (x)
  13808. (if (memq x org-latex-fragment-image-overlays) x))
  13809. (org-overlays-at (point))))))
  13810. (push (list :latex-fragment
  13811. (org-overlay-start o) (org-overlay-end o)) clist)
  13812. (push (list :latex-preview
  13813. (org-overlay-start o) (org-overlay-end o)) clist))
  13814. ((org-inside-LaTeX-fragment-p)
  13815. ;; FIXME: positions wrong.
  13816. (push (list :latex-fragment (point) (point)) clist)))
  13817. (setq clist (nreverse (delq nil clist)))
  13818. clist))
  13819. ;; FIXME: Compare with at-regexp-p Do we need both?
  13820. (defun org-in-regexp (re &optional nlines visually)
  13821. "Check if point is inside a match of regexp.
  13822. Normally only the current line is checked, but you can include NLINES extra
  13823. lines both before and after point into the search.
  13824. If VISUALLY is set, require that the cursor is not after the match but
  13825. really on, so that the block visually is on the match."
  13826. (catch 'exit
  13827. (let ((pos (point))
  13828. (eol (point-at-eol (+ 1 (or nlines 0))))
  13829. (inc (if visually 1 0)))
  13830. (save-excursion
  13831. (beginning-of-line (- 1 (or nlines 0)))
  13832. (while (re-search-forward re eol t)
  13833. (if (and (<= (match-beginning 0) pos)
  13834. (>= (+ inc (match-end 0)) pos))
  13835. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  13836. (defun org-at-regexp-p (regexp)
  13837. "Is point inside a match of REGEXP in the current line?"
  13838. (catch 'exit
  13839. (save-excursion
  13840. (let ((pos (point)) (end (point-at-eol)))
  13841. (beginning-of-line 1)
  13842. (while (re-search-forward regexp end t)
  13843. (if (and (<= (match-beginning 0) pos)
  13844. (>= (match-end 0) pos))
  13845. (throw 'exit t)))
  13846. nil))))
  13847. (defun org-occur-in-agenda-files (regexp &optional nlines)
  13848. "Call `multi-occur' with buffers for all agenda files."
  13849. (interactive "sOrg-files matching: \np")
  13850. (let* ((files (org-agenda-files))
  13851. (tnames (mapcar 'file-truename files))
  13852. (extra org-agenda-text-search-extra-files)
  13853. f)
  13854. (while (setq f (pop extra))
  13855. (unless (member (file-truename f) tnames)
  13856. (add-to-list 'files f 'append)
  13857. (add-to-list 'tnames (file-truename f) 'append)))
  13858. (multi-occur
  13859. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  13860. regexp)))
  13861. (if (boundp 'occur-mode-find-occurrence-hook)
  13862. ;; Emacs 23
  13863. (add-hook 'occur-mode-find-occurrence-hook
  13864. (lambda ()
  13865. (when (org-mode-p)
  13866. (org-reveal))))
  13867. ;; Emacs 22
  13868. (defadvice occur-mode-goto-occurrence
  13869. (after org-occur-reveal activate)
  13870. (and (org-mode-p) (org-reveal)))
  13871. (defadvice occur-mode-goto-occurrence-other-window
  13872. (after org-occur-reveal activate)
  13873. (and (org-mode-p) (org-reveal)))
  13874. (defadvice occur-mode-display-occurrence
  13875. (after org-occur-reveal activate)
  13876. (when (org-mode-p)
  13877. (let ((pos (occur-mode-find-occurrence)))
  13878. (with-current-buffer (marker-buffer pos)
  13879. (save-excursion
  13880. (goto-char pos)
  13881. (org-reveal)))))))
  13882. (defun org-uniquify (list)
  13883. "Remove duplicate elements from LIST."
  13884. (let (res)
  13885. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  13886. res))
  13887. (defun org-delete-all (elts list)
  13888. "Remove all elements in ELTS from LIST."
  13889. (while elts
  13890. (setq list (delete (pop elts) list)))
  13891. list)
  13892. (defun org-back-over-empty-lines ()
  13893. "Move backwards over witespace, to the beginning of the first empty line.
  13894. Returns the number of empty lines passed."
  13895. (let ((pos (point)))
  13896. (skip-chars-backward " \t\n\r")
  13897. (beginning-of-line 2)
  13898. (goto-char (min (point) pos))
  13899. (count-lines (point) pos)))
  13900. (defun org-skip-whitespace ()
  13901. (skip-chars-forward " \t\n\r"))
  13902. (defun org-point-in-group (point group &optional context)
  13903. "Check if POINT is in match-group GROUP.
  13904. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  13905. match. If the match group does ot exist or point is not inside it,
  13906. return nil."
  13907. (and (match-beginning group)
  13908. (>= point (match-beginning group))
  13909. (<= point (match-end group))
  13910. (if context
  13911. (list context (match-beginning group) (match-end group))
  13912. t)))
  13913. (defun org-switch-to-buffer-other-window (&rest args)
  13914. "Switch to buffer in a second window on the current frame.
  13915. In particular, do not allow pop-up frames."
  13916. (let (pop-up-frames special-display-buffer-names special-display-regexps
  13917. special-display-function)
  13918. (apply 'switch-to-buffer-other-window args)))
  13919. (defun org-combine-plists (&rest plists)
  13920. "Create a single property list from all plists in PLISTS.
  13921. The process starts by copying the first list, and then setting properties
  13922. from the other lists. Settings in the last list are the most significant
  13923. ones and overrule settings in the other lists."
  13924. (let ((rtn (copy-sequence (pop plists)))
  13925. p v ls)
  13926. (while plists
  13927. (setq ls (pop plists))
  13928. (while ls
  13929. (setq p (pop ls) v (pop ls))
  13930. (setq rtn (plist-put rtn p v))))
  13931. rtn))
  13932. (defun org-move-line-down (arg)
  13933. "Move the current line down. With prefix argument, move it past ARG lines."
  13934. (interactive "p")
  13935. (let ((col (current-column))
  13936. beg end pos)
  13937. (beginning-of-line 1) (setq beg (point))
  13938. (beginning-of-line 2) (setq end (point))
  13939. (beginning-of-line (+ 1 arg))
  13940. (setq pos (move-marker (make-marker) (point)))
  13941. (insert (delete-and-extract-region beg end))
  13942. (goto-char pos)
  13943. (move-to-column col)))
  13944. (defun org-move-line-up (arg)
  13945. "Move the current line up. With prefix argument, move it past ARG lines."
  13946. (interactive "p")
  13947. (let ((col (current-column))
  13948. beg end pos)
  13949. (beginning-of-line 1) (setq beg (point))
  13950. (beginning-of-line 2) (setq end (point))
  13951. (beginning-of-line (- arg))
  13952. (setq pos (move-marker (make-marker) (point)))
  13953. (insert (delete-and-extract-region beg end))
  13954. (goto-char pos)
  13955. (move-to-column col)))
  13956. (defun org-replace-escapes (string table)
  13957. "Replace %-escapes in STRING with values in TABLE.
  13958. TABLE is an association list with keys like \"%a\" and string values.
  13959. The sequences in STRING may contain normal field width and padding information,
  13960. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  13961. so values can contain further %-escapes if they are define later in TABLE."
  13962. (let ((case-fold-search nil)
  13963. e re rpl)
  13964. (while (setq e (pop table))
  13965. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  13966. (while (string-match re string)
  13967. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  13968. (cdr e)))
  13969. (setq string (replace-match rpl t t string))))
  13970. string))
  13971. (defun org-sublist (list start end)
  13972. "Return a section of LIST, from START to END.
  13973. Counting starts at 1."
  13974. (let (rtn (c start))
  13975. (setq list (nthcdr (1- start) list))
  13976. (while (and list (<= c end))
  13977. (push (pop list) rtn)
  13978. (setq c (1+ c)))
  13979. (nreverse rtn)))
  13980. (defun org-find-base-buffer-visiting (file)
  13981. "Like `find-buffer-visiting' but alway return the base buffer and
  13982. not an indirect buffer."
  13983. (let ((buf (find-buffer-visiting file)))
  13984. (if buf
  13985. (or (buffer-base-buffer buf) buf)
  13986. nil)))
  13987. (defun org-image-file-name-regexp ()
  13988. "Return regexp matching the file names of images."
  13989. (if (fboundp 'image-file-name-regexp)
  13990. (image-file-name-regexp)
  13991. (let ((image-file-name-extensions
  13992. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  13993. "xbm" "xpm" "pbm" "pgm" "ppm")))
  13994. (concat "\\."
  13995. (regexp-opt (nconc (mapcar 'upcase
  13996. image-file-name-extensions)
  13997. image-file-name-extensions)
  13998. t)
  13999. "\\'"))))
  14000. (defun org-file-image-p (file)
  14001. "Return non-nil if FILE is an image."
  14002. (save-match-data
  14003. (string-match (org-image-file-name-regexp) file)))
  14004. ;;; Paragraph filling stuff.
  14005. ;; We want this to be just right, so use the full arsenal.
  14006. (defun org-indent-line-function ()
  14007. "Indent line like previous, but further if previous was headline or item."
  14008. (interactive)
  14009. (let* ((pos (point))
  14010. (itemp (org-at-item-p))
  14011. column bpos bcol tpos tcol bullet btype bullet-type)
  14012. ;; Find the previous relevant line
  14013. (beginning-of-line 1)
  14014. (cond
  14015. ((looking-at "#") (setq column 0))
  14016. ((looking-at "\\*+ ") (setq column 0))
  14017. (t
  14018. (beginning-of-line 0)
  14019. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  14020. (beginning-of-line 0))
  14021. (cond
  14022. ((looking-at "\\*+[ \t]+")
  14023. (goto-char (match-end 0))
  14024. (setq column (current-column)))
  14025. ((org-in-item-p)
  14026. (org-beginning-of-item)
  14027. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  14028. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\)?")
  14029. (setq bpos (match-beginning 1) tpos (match-end 0)
  14030. bcol (progn (goto-char bpos) (current-column))
  14031. tcol (progn (goto-char tpos) (current-column))
  14032. bullet (match-string 1)
  14033. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  14034. (if (not itemp)
  14035. (setq column tcol)
  14036. (goto-char pos)
  14037. (beginning-of-line 1)
  14038. (if (looking-at "\\S-")
  14039. (progn
  14040. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  14041. (setq bullet (match-string 1)
  14042. btype (if (string-match "[0-9]" bullet) "n" bullet))
  14043. (setq column (if (equal btype bullet-type) bcol tcol)))
  14044. (setq column (org-get-indentation)))))
  14045. (t (setq column (org-get-indentation))))))
  14046. (goto-char pos)
  14047. (if (<= (current-column) (current-indentation))
  14048. (indent-line-to column)
  14049. (save-excursion (indent-line-to column)))
  14050. (setq column (current-column))
  14051. (beginning-of-line 1)
  14052. (if (looking-at
  14053. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  14054. (replace-match (concat "\\1" (format org-property-format
  14055. (match-string 2) (match-string 3)))
  14056. t nil))
  14057. (move-to-column column)))
  14058. (defun org-set-autofill-regexps ()
  14059. (interactive)
  14060. ;; In the paragraph separator we include headlines, because filling
  14061. ;; text in a line directly attached to a headline would otherwise
  14062. ;; fill the headline as well.
  14063. (org-set-local 'comment-start-skip "^#+[ \t]*")
  14064. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  14065. ;; The paragraph starter includes hand-formatted lists.
  14066. (org-set-local 'paragraph-start
  14067. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  14068. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  14069. ;; But only if the user has not turned off tables or fixed-width regions
  14070. (org-set-local
  14071. 'auto-fill-inhibit-regexp
  14072. (concat "\\*+ \\|#\\+"
  14073. "\\|[ \t]*" org-keyword-time-regexp
  14074. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  14075. (concat
  14076. "\\|[ \t]*["
  14077. (if org-enable-table-editor "|" "")
  14078. (if org-enable-fixed-width-editor ":" "")
  14079. "]"))))
  14080. ;; We use our own fill-paragraph function, to make sure that tables
  14081. ;; and fixed-width regions are not wrapped. That function will pass
  14082. ;; through to `fill-paragraph' when appropriate.
  14083. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  14084. ; Adaptive filling: To get full control, first make sure that
  14085. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  14086. (org-set-local 'adaptive-fill-regexp "\000")
  14087. (org-set-local 'adaptive-fill-function
  14088. 'org-adaptive-fill-function)
  14089. (org-set-local
  14090. 'align-mode-rules-list
  14091. '((org-in-buffer-settings
  14092. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  14093. (modes . '(org-mode))))))
  14094. (defun org-fill-paragraph (&optional justify)
  14095. "Re-align a table, pass through to fill-paragraph if no table."
  14096. (let ((table-p (org-at-table-p))
  14097. (table.el-p (org-at-table.el-p)))
  14098. (cond ((and (equal (char-after (point-at-bol)) ?*)
  14099. (save-excursion (goto-char (point-at-bol))
  14100. (looking-at outline-regexp)))
  14101. t) ; skip headlines
  14102. (table.el-p t) ; skip table.el tables
  14103. (table-p (org-table-align) t) ; align org-mode tables
  14104. (t nil)))) ; call paragraph-fill
  14105. ;; For reference, this is the default value of adaptive-fill-regexp
  14106. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  14107. (defun org-adaptive-fill-function ()
  14108. "Return a fill prefix for org-mode files.
  14109. In particular, this makes sure hanging paragraphs for hand-formatted lists
  14110. work correctly."
  14111. (cond ((looking-at "#[ \t]+")
  14112. (match-string 0))
  14113. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  14114. (save-excursion
  14115. (goto-char (match-end 0))
  14116. (make-string (current-column) ?\ )))
  14117. (t nil)))
  14118. ;;; Other stuff.
  14119. (defun org-toggle-fixed-width-section (arg)
  14120. "Toggle the fixed-width export.
  14121. If there is no active region, the QUOTE keyword at the current headline is
  14122. inserted or removed. When present, it causes the text between this headline
  14123. and the next to be exported as fixed-width text, and unmodified.
  14124. If there is an active region, this command adds or removes a colon as the
  14125. first character of this line. If the first character of a line is a colon,
  14126. this line is also exported in fixed-width font."
  14127. (interactive "P")
  14128. (let* ((cc 0)
  14129. (regionp (org-region-active-p))
  14130. (beg (if regionp (region-beginning) (point)))
  14131. (end (if regionp (region-end)))
  14132. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  14133. (case-fold-search nil)
  14134. (re "[ \t]*\\(:\\)")
  14135. off)
  14136. (if regionp
  14137. (save-excursion
  14138. (goto-char beg)
  14139. (setq cc (current-column))
  14140. (beginning-of-line 1)
  14141. (setq off (looking-at re))
  14142. (while (> nlines 0)
  14143. (setq nlines (1- nlines))
  14144. (beginning-of-line 1)
  14145. (cond
  14146. (arg
  14147. (move-to-column cc t)
  14148. (insert ":\n")
  14149. (forward-line -1))
  14150. ((and off (looking-at re))
  14151. (replace-match "" t t nil 1))
  14152. ((not off) (move-to-column cc t) (insert ":")))
  14153. (forward-line 1)))
  14154. (save-excursion
  14155. (org-back-to-heading)
  14156. (if (looking-at (concat outline-regexp
  14157. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  14158. (replace-match "" t t nil 1)
  14159. (if (looking-at outline-regexp)
  14160. (progn
  14161. (goto-char (match-end 0))
  14162. (insert org-quote-string " "))))))))
  14163. ;;;; Functions extending outline functionality
  14164. (defun org-beginning-of-line (&optional arg)
  14165. "Go to the beginning of the current line. If that is invisible, continue
  14166. to a visible line beginning. This makes the function of C-a more intuitive.
  14167. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14168. first attempt, and only move to after the tags when the cursor is already
  14169. beyond the end of the headline."
  14170. (interactive "P")
  14171. (let ((pos (point)))
  14172. (beginning-of-line 1)
  14173. (if (bobp)
  14174. nil
  14175. (backward-char 1)
  14176. (if (org-invisible-p)
  14177. (while (and (not (bobp)) (org-invisible-p))
  14178. (backward-char 1)
  14179. (beginning-of-line 1))
  14180. (forward-char 1)))
  14181. (when org-special-ctrl-a/e
  14182. (cond
  14183. ((and (looking-at org-todo-line-regexp)
  14184. (= (char-after (match-end 1)) ?\ ))
  14185. (goto-char
  14186. (if (eq org-special-ctrl-a/e t)
  14187. (cond ((> pos (match-beginning 3)) (match-beginning 3))
  14188. ((= pos (point)) (match-beginning 3))
  14189. (t (point)))
  14190. (cond ((> pos (point)) (point))
  14191. ((not (eq last-command this-command)) (point))
  14192. (t (match-beginning 3))))))
  14193. ((org-at-item-p)
  14194. (goto-char
  14195. (if (eq org-special-ctrl-a/e t)
  14196. (cond ((> pos (match-end 4)) (match-end 4))
  14197. ((= pos (point)) (match-end 4))
  14198. (t (point)))
  14199. (cond ((> pos (point)) (point))
  14200. ((not (eq last-command this-command)) (point))
  14201. (t (match-end 4))))))))))
  14202. (defun org-end-of-line (&optional arg)
  14203. "Go to the end of the line.
  14204. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14205. first attempt, and only move to after the tags when the cursor is already
  14206. beyond the end of the headline."
  14207. (interactive "P")
  14208. (if (or (not org-special-ctrl-a/e)
  14209. (not (org-on-heading-p)))
  14210. (end-of-line arg)
  14211. (let ((pos (point)))
  14212. (beginning-of-line 1)
  14213. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14214. (if (eq org-special-ctrl-a/e t)
  14215. (if (or (< pos (match-beginning 1))
  14216. (= pos (match-end 0)))
  14217. (goto-char (match-beginning 1))
  14218. (goto-char (match-end 0)))
  14219. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  14220. (goto-char (match-end 0))
  14221. (goto-char (match-beginning 1))))
  14222. (end-of-line arg)))))
  14223. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  14224. (define-key org-mode-map "\C-e" 'org-end-of-line)
  14225. (defun org-kill-line (&optional arg)
  14226. "Kill line, to tags or end of line."
  14227. (interactive "P")
  14228. (cond
  14229. ((or (not org-special-ctrl-k)
  14230. (bolp)
  14231. (not (org-on-heading-p)))
  14232. (call-interactively 'kill-line))
  14233. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  14234. (kill-region (point) (match-beginning 1))
  14235. (org-set-tags nil t))
  14236. (t (kill-region (point) (point-at-eol)))))
  14237. (define-key org-mode-map "\C-k" 'org-kill-line)
  14238. (defun org-invisible-p ()
  14239. "Check if point is at a character currently not visible."
  14240. ;; Early versions of noutline don't have `outline-invisible-p'.
  14241. (if (fboundp 'outline-invisible-p)
  14242. (outline-invisible-p)
  14243. (get-char-property (point) 'invisible)))
  14244. (defun org-invisible-p2 ()
  14245. "Check if point is at a character currently not visible."
  14246. (save-excursion
  14247. (if (and (eolp) (not (bobp))) (backward-char 1))
  14248. ;; Early versions of noutline don't have `outline-invisible-p'.
  14249. (if (fboundp 'outline-invisible-p)
  14250. (outline-invisible-p)
  14251. (get-char-property (point) 'invisible))))
  14252. (defalias 'org-back-to-heading 'outline-back-to-heading)
  14253. (defalias 'org-on-heading-p 'outline-on-heading-p)
  14254. (defalias 'org-at-heading-p 'outline-on-heading-p)
  14255. (defun org-at-heading-or-item-p ()
  14256. (or (org-on-heading-p) (org-at-item-p)))
  14257. (defun org-on-target-p ()
  14258. (or (org-in-regexp org-radio-target-regexp)
  14259. (org-in-regexp org-target-regexp)))
  14260. (defun org-up-heading-all (arg)
  14261. "Move to the heading line of which the present line is a subheading.
  14262. This function considers both visible and invisible heading lines.
  14263. With argument, move up ARG levels."
  14264. (if (fboundp 'outline-up-heading-all)
  14265. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  14266. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  14267. (defun org-up-heading-safe ()
  14268. "Move to the heading line of which the present line is a subheading.
  14269. This version will not throw an error. It will return the level of the
  14270. headline found, or nil if no higher level is found."
  14271. (let ((pos (point)) start-level level
  14272. (re (concat "^" outline-regexp)))
  14273. (catch 'exit
  14274. (outline-back-to-heading t)
  14275. (setq start-level (funcall outline-level))
  14276. (if (equal start-level 1) (throw 'exit nil))
  14277. (while (re-search-backward re nil t)
  14278. (setq level (funcall outline-level))
  14279. (if (< level start-level) (throw 'exit level)))
  14280. nil)))
  14281. (defun org-first-sibling-p ()
  14282. "Is this heading the first child of its parents?"
  14283. (interactive)
  14284. (let ((re (concat "^" outline-regexp))
  14285. level l)
  14286. (unless (org-at-heading-p t)
  14287. (error "Not at a heading"))
  14288. (setq level (funcall outline-level))
  14289. (save-excursion
  14290. (if (not (re-search-backward re nil t))
  14291. t
  14292. (setq l (funcall outline-level))
  14293. (< l level)))))
  14294. (defun org-goto-sibling (&optional previous)
  14295. "Goto the next sibling, even if it is invisible.
  14296. When PREVIOUS is set, go to the previous sibling instead. Returns t
  14297. when a sibling was found. When none is found, return nil and don't
  14298. move point."
  14299. (let ((fun (if previous 're-search-backward 're-search-forward))
  14300. (pos (point))
  14301. (re (concat "^" outline-regexp))
  14302. level l)
  14303. (when (condition-case nil (org-back-to-heading t) (error nil))
  14304. (setq level (funcall outline-level))
  14305. (catch 'exit
  14306. (or previous (forward-char 1))
  14307. (while (funcall fun re nil t)
  14308. (setq l (funcall outline-level))
  14309. (when (< l level) (goto-char pos) (throw 'exit nil))
  14310. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  14311. (goto-char pos)
  14312. nil))))
  14313. (defun org-show-siblings ()
  14314. "Show all siblings of the current headline."
  14315. (save-excursion
  14316. (while (org-goto-sibling) (org-flag-heading nil)))
  14317. (save-excursion
  14318. (while (org-goto-sibling 'previous)
  14319. (org-flag-heading nil))))
  14320. (defun org-show-hidden-entry ()
  14321. "Show an entry where even the heading is hidden."
  14322. (save-excursion
  14323. (org-show-entry)))
  14324. (defun org-flag-heading (flag &optional entry)
  14325. "Flag the current heading. FLAG non-nil means make invisible.
  14326. When ENTRY is non-nil, show the entire entry."
  14327. (save-excursion
  14328. (org-back-to-heading t)
  14329. ;; Check if we should show the entire entry
  14330. (if entry
  14331. (progn
  14332. (org-show-entry)
  14333. (save-excursion
  14334. (and (outline-next-heading)
  14335. (org-flag-heading nil))))
  14336. (outline-flag-region (max (point-min) (1- (point)))
  14337. (save-excursion (outline-end-of-heading) (point))
  14338. flag))))
  14339. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  14340. ;; This is an exact copy of the original function, but it uses
  14341. ;; `org-back-to-heading', to make it work also in invisible
  14342. ;; trees. And is uses an invisible-OK argument.
  14343. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  14344. (org-back-to-heading invisible-OK)
  14345. (let ((first t)
  14346. (level (funcall outline-level)))
  14347. (while (and (not (eobp))
  14348. (or first (> (funcall outline-level) level)))
  14349. (setq first nil)
  14350. (outline-next-heading))
  14351. (unless to-heading
  14352. (if (memq (preceding-char) '(?\n ?\^M))
  14353. (progn
  14354. ;; Go to end of line before heading
  14355. (forward-char -1)
  14356. (if (memq (preceding-char) '(?\n ?\^M))
  14357. ;; leave blank line before heading
  14358. (forward-char -1))))))
  14359. (point))
  14360. (defun org-show-subtree ()
  14361. "Show everything after this heading at deeper levels."
  14362. (outline-flag-region
  14363. (point)
  14364. (save-excursion
  14365. (outline-end-of-subtree) (outline-next-heading) (point))
  14366. nil))
  14367. (defun org-show-entry ()
  14368. "Show the body directly following this heading.
  14369. Show the heading too, if it is currently invisible."
  14370. (interactive)
  14371. (save-excursion
  14372. (condition-case nil
  14373. (progn
  14374. (org-back-to-heading t)
  14375. (outline-flag-region
  14376. (max (point-min) (1- (point)))
  14377. (save-excursion
  14378. (re-search-forward
  14379. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  14380. (or (match-beginning 1) (point-max)))
  14381. nil))
  14382. (error nil))))
  14383. (defun org-make-options-regexp (kwds)
  14384. "Make a regular expression for keyword lines."
  14385. (concat
  14386. "^"
  14387. "#?[ \t]*\\+\\("
  14388. (mapconcat 'regexp-quote kwds "\\|")
  14389. "\\):[ \t]*"
  14390. "\\(.+\\)"))
  14391. ;; Make isearch reveal the necessary context
  14392. (defun org-isearch-end ()
  14393. "Reveal context after isearch exits."
  14394. (when isearch-success ; only if search was successful
  14395. (if (featurep 'xemacs)
  14396. ;; Under XEmacs, the hook is run in the correct place,
  14397. ;; we directly show the context.
  14398. (org-show-context 'isearch)
  14399. ;; In Emacs the hook runs *before* restoring the overlays.
  14400. ;; So we have to use a one-time post-command-hook to do this.
  14401. ;; (Emacs 22 has a special variable, see function `org-mode')
  14402. (unless (and (boundp 'isearch-mode-end-hook-quit)
  14403. isearch-mode-end-hook-quit)
  14404. ;; Only when the isearch was not quitted.
  14405. (org-add-hook 'post-command-hook 'org-isearch-post-command
  14406. 'append 'local)))))
  14407. (defun org-isearch-post-command ()
  14408. "Remove self from hook, and show context."
  14409. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  14410. (org-show-context 'isearch))
  14411. ;;;; Integration with and fixes for other packages
  14412. ;;; Imenu support
  14413. (defvar org-imenu-markers nil
  14414. "All markers currently used by Imenu.")
  14415. (make-variable-buffer-local 'org-imenu-markers)
  14416. (defun org-imenu-new-marker (&optional pos)
  14417. "Return a new marker for use by Imenu, and remember the marker."
  14418. (let ((m (make-marker)))
  14419. (move-marker m (or pos (point)))
  14420. (push m org-imenu-markers)
  14421. m))
  14422. (defun org-imenu-get-tree ()
  14423. "Produce the index for Imenu."
  14424. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  14425. (setq org-imenu-markers nil)
  14426. (let* ((n org-imenu-depth)
  14427. (re (concat "^" outline-regexp))
  14428. (subs (make-vector (1+ n) nil))
  14429. (last-level 0)
  14430. m tree level head)
  14431. (save-excursion
  14432. (save-restriction
  14433. (widen)
  14434. (goto-char (point-max))
  14435. (while (re-search-backward re nil t)
  14436. (setq level (org-reduced-level (funcall outline-level)))
  14437. (when (<= level n)
  14438. (looking-at org-complex-heading-regexp)
  14439. (setq head (org-match-string-no-properties 4)
  14440. m (org-imenu-new-marker))
  14441. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  14442. (if (>= level last-level)
  14443. (push (cons head m) (aref subs level))
  14444. (push (cons head (aref subs (1+ level))) (aref subs level))
  14445. (loop for i from (1+ level) to n do (aset subs i nil)))
  14446. (setq last-level level)))))
  14447. (aref subs 1)))
  14448. (eval-after-load "imenu"
  14449. '(progn
  14450. (add-hook 'imenu-after-jump-hook
  14451. (lambda () (org-show-context 'org-goto)))))
  14452. ;; Speedbar support
  14453. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  14454. "Overlay marking the agenda restriction line in speedbar.")
  14455. (org-overlay-put org-speedbar-restriction-lock-overlay
  14456. 'face 'org-agenda-restriction-lock)
  14457. (org-overlay-put org-speedbar-restriction-lock-overlay
  14458. 'help-echo "Agendas are currently limited to this item.")
  14459. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  14460. (defun org-speedbar-set-agenda-restriction ()
  14461. "Restrict future agenda commands to the location at point in speedbar.
  14462. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  14463. (interactive)
  14464. (let (p m tp np dir txt w)
  14465. (cond
  14466. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14467. 'org-imenu t))
  14468. (setq m (get-text-property p 'org-imenu-marker))
  14469. (save-excursion
  14470. (save-restriction
  14471. (set-buffer (marker-buffer m))
  14472. (goto-char m)
  14473. (org-agenda-set-restriction-lock 'subtree))))
  14474. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14475. 'speedbar-function 'speedbar-find-file))
  14476. (setq tp (previous-single-property-change
  14477. (1+ p) 'speedbar-function)
  14478. np (next-single-property-change
  14479. tp 'speedbar-function)
  14480. dir (speedbar-line-directory)
  14481. txt (buffer-substring-no-properties (or tp (point-min))
  14482. (or np (point-max))))
  14483. (save-excursion
  14484. (save-restriction
  14485. (set-buffer (find-file-noselect
  14486. (let ((default-directory dir))
  14487. (expand-file-name txt))))
  14488. (unless (org-mode-p)
  14489. (error "Cannot restrict to non-Org-mode file"))
  14490. (org-agenda-set-restriction-lock 'file))))
  14491. (t (error "Don't know how to restrict Org-mode's agenda")))
  14492. (org-move-overlay org-speedbar-restriction-lock-overlay
  14493. (point-at-bol) (point-at-eol))
  14494. (setq current-prefix-arg nil)
  14495. (org-agenda-maybe-redo)))
  14496. (eval-after-load "speedbar"
  14497. '(progn
  14498. (speedbar-add-supported-extension ".org")
  14499. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  14500. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  14501. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  14502. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  14503. (add-hook 'speedbar-visiting-tag-hook
  14504. (lambda () (org-show-context 'org-goto)))))
  14505. ;;; Fixes and Hacks
  14506. ;; Make flyspell not check words in links, to not mess up our keymap
  14507. (defun org-mode-flyspell-verify ()
  14508. "Don't let flyspell put overlays at active buttons."
  14509. (not (get-text-property (point) 'keymap)))
  14510. ;; Make `bookmark-jump' show the jump location if it was hidden.
  14511. (eval-after-load "bookmark"
  14512. '(if (boundp 'bookmark-after-jump-hook)
  14513. ;; We can use the hook
  14514. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  14515. ;; Hook not available, use advice
  14516. (defadvice bookmark-jump (after org-make-visible activate)
  14517. "Make the position visible."
  14518. (org-bookmark-jump-unhide))))
  14519. (defun org-bookmark-jump-unhide ()
  14520. "Unhide the current position, to show the bookmark location."
  14521. (and (org-mode-p)
  14522. (or (org-invisible-p)
  14523. (save-excursion (goto-char (max (point-min) (1- (point))))
  14524. (org-invisible-p)))
  14525. (org-show-context 'bookmark-jump)))
  14526. ;; Make session.el ignore our circular variable
  14527. (eval-after-load "session"
  14528. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  14529. ;;;; Experimental code
  14530. (defun org-closed-in-range ()
  14531. "Sparse tree of items closed in a certain time range.
  14532. Still experimental, may disappear in the future."
  14533. (interactive)
  14534. ;; Get the time interval from the user.
  14535. (let* ((time1 (time-to-seconds
  14536. (org-read-date nil 'to-time nil "Starting date: ")))
  14537. (time2 (time-to-seconds
  14538. (org-read-date nil 'to-time nil "End date:")))
  14539. ;; callback function
  14540. (callback (lambda ()
  14541. (let ((time
  14542. (time-to-seconds
  14543. (apply 'encode-time
  14544. (org-parse-time-string
  14545. (match-string 1))))))
  14546. ;; check if time in interval
  14547. (and (>= time time1) (<= time time2))))))
  14548. ;; make tree, check each match with the callback
  14549. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  14550. ;;;; Finish up
  14551. (provide 'org)
  14552. (run-hooks 'org-load-hook)
  14553. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  14554. ;;; org.el ends here
  14555. (defun org-get-heading (&optional no-tags)
  14556. "Return the heading of the current entry, without the stars."
  14557. (save-excursion
  14558. (org-back-to-heading t)
  14559. (if (looking-at
  14560. (if no-tags
  14561. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  14562. "\\*+[ \t]+\\([^\r\n]*\\)"))
  14563. (match-string 1) "")))
  14564. (defun org-get-category (&optional pos)
  14565. "Get the category applying to position POS."
  14566. (get-text-property (or pos (point)) 'org-category))
  14567. (defun org-get-tags-at (&optional pos)
  14568. "Get a list of all headline tags applicable at POS.
  14569. POS defaults to point. If tags are inherited, the list contains
  14570. the targets in the same sequence as the headlines appear, i.e.
  14571. sthe tags of the current headline come last."
  14572. (interactive)
  14573. (let (tags ltags lastpos parent)
  14574. (save-excursion
  14575. (save-restriction
  14576. (widen)
  14577. (goto-char (or pos (point)))
  14578. (save-match-data
  14579. (condition-case nil
  14580. (progn
  14581. (org-back-to-heading t)
  14582. (while (not (equal lastpos (point)))
  14583. (setq lastpos (point))
  14584. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  14585. (setq ltags (org-split-string
  14586. (org-match-string-no-properties 1) ":"))
  14587. (setq tags (append (org-remove-uniherited-tags ltags)
  14588. tags)))
  14589. (or org-use-tag-inheritance (error ""))
  14590. (org-up-heading-all 1)
  14591. (setq parent t)))
  14592. (error nil))))
  14593. tags)))
  14594. (defun org-days-to-iso-week (days)
  14595. "Return the iso week number."
  14596. (require 'cal-iso)
  14597. (car (calendar-iso-from-absolute days)))
  14598. (defun org-small-year-to-year (year)
  14599. "Convert 2-digit years into 4-digit years.
  14600. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  14601. The year 2000 cannot be abbreviated. Any year lager than 99
  14602. is retrned unchanged."
  14603. (if (< year 38)
  14604. (setq year (+ 2000 year))
  14605. (if (< year 100)
  14606. (setq year (+ 1900 year))))
  14607. year)